- Fix a bug where hitting the back button would snooze multiple times.
This commit is contained in:
parent
6532182fb8
commit
e6266d4ed6
@ -107,12 +107,16 @@ public class AlarmAlertActivity extends Activity {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onBackPressed() {
|
public void onBackPressed() {
|
||||||
AlarmService.setAlarmStatus(AlarmService.ALARM_IGNORED);
|
if (!AlarmService.alarmStatus.contentEquals(AlarmService.ALARM_SNOOZE_RUNNING)) {
|
||||||
|
AlarmService.setAlarmStatus(AlarmService.ALARM_IGNORED);
|
||||||
|
}
|
||||||
AlarmService.snoozeAlarm(this);
|
AlarmService.snoozeAlarm(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onUserLeaveHint() {
|
public void onUserLeaveHint() {
|
||||||
AlarmService.setAlarmStatus(AlarmService.ALARM_IGNORED);
|
if (!AlarmService.alarmStatus.contentEquals(AlarmService.ALARM_SNOOZE_RUNNING)) {
|
||||||
|
AlarmService.setAlarmStatus(AlarmService.ALARM_IGNORED);
|
||||||
|
}
|
||||||
AlarmService.snoozeAlarm(this);
|
AlarmService.snoozeAlarm(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -66,15 +66,6 @@ public class AlarmNotify extends Service {
|
|||||||
// Allow the user to cancel by selecting the ContentText or ContentTitle
|
// Allow the user to cancel by selecting the ContentText or ContentTitle
|
||||||
notification.setContentIntent(cancellerPendingIntent);
|
notification.setContentIntent(cancellerPendingIntent);
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO load alert activity (without sound or vibration) on select?
|
|
||||||
* TODO This would allow the user to test competence
|
|
||||||
* something like:
|
|
||||||
Intent alarmAlertIntent = new Intent(this, AlarmAlertActivity.class);
|
|
||||||
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, 0, alarmAlertIntent, 0);
|
|
||||||
notification.setContentIntent(alarmPen`dingIntent);
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
nm.cancel(notifyID);
|
nm.cancel(notifyID);
|
||||||
nm.notify(notifyID, notification.build());
|
nm.notify(notifyID, notification.build());
|
||||||
|
@ -16,10 +16,10 @@ import android.util.Log;
|
|||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
|
||||||
public class AlarmService extends Service {
|
public class AlarmService extends Service {
|
||||||
private static final int SNOOZE_TIME = 1000*60*5; // Snooze for 5 minutes if need be
|
private static final int SNOOZE_TIME = 1000*60;//60*5; // Snooze for 5 minutes if need be
|
||||||
private static final int ALERT_LIFE = 1000*60*2; // 2 minutes
|
private static final int ALERT_LIFE = 1000*10;//60*2; // 2 minutes
|
||||||
private static AlarmManager alarmManager;
|
private static AlarmManager alarmManager;
|
||||||
private static Intent alertActivityIntent, notifyIntent;
|
private static Intent alarmServiceIntent, alertActivityIntent, notifyIntent;
|
||||||
public static Boolean alarmStarted = false;
|
public static Boolean alarmStarted = false;
|
||||||
public static volatile String alarmStatus; // Register ALARM_DISMISSED and its brethren here
|
public static volatile String alarmStatus; // Register ALARM_DISMISSED and its brethren here
|
||||||
public static final String ALARM_RUNNING = "ALARM_RUNNING";
|
public static final String ALARM_RUNNING = "ALARM_RUNNING";
|
||||||
@ -35,6 +35,7 @@ public class AlarmService extends Service {
|
|||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
|
Log.d("AlarmService", "Destroying alarm");
|
||||||
if (alarmStarted) {
|
if (alarmStarted) {
|
||||||
stopAlert(getApplicationContext());
|
stopAlert(getApplicationContext());
|
||||||
alarmStarted = false;
|
alarmStarted = false;
|
||||||
@ -43,24 +44,31 @@ public class AlarmService extends Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
|
alarmServiceIntent = intent;
|
||||||
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
|
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
final Boolean alarmActive = sharedPref.getBoolean(getString(R.string.AlarmActivePref), true);
|
final Boolean alarmActive = sharedPref.getBoolean(getString(R.string.AlarmActivePref), true);
|
||||||
final int gracePeriod = sharedPref.getInt(getString(R.string.GracePeriodPref), 60);
|
final int gracePeriod = sharedPref.getInt(getString(R.string.GracePeriodPref), 60);
|
||||||
final String alarmTimeStr = sharedPref.getString(getString(R.string.AlarmTimePref), null);
|
final String alarmTimeStr = sharedPref.getString(getString(R.string.AlarmTimePref), null);
|
||||||
|
|
||||||
if (alarmActive) {
|
if (alarmActive) {
|
||||||
|
// Cancel the pre-alarm notification, if it exists
|
||||||
|
stopService(new Intent(this, PreAlarmNotify.class));
|
||||||
|
|
||||||
|
// Set up intents for later use
|
||||||
|
notifyIntent = new Intent(this, AlarmNotify.class);
|
||||||
|
alertActivityIntent = new Intent(this, AlarmAlertActivity.class);
|
||||||
|
alertActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
|
||||||
|
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
|
||||||
|
|
||||||
if (alarmStarted) {
|
if (alarmStarted) {
|
||||||
stopAlert(getApplicationContext());
|
stopAlert(getApplicationContext());
|
||||||
|
stopService(notifyIntent);
|
||||||
}
|
}
|
||||||
alarmStarted = true;
|
alarmStarted = true;
|
||||||
|
|
||||||
// if nothing else happens, assume the alert was ignored.
|
// if nothing else happens, assume the alert was ignored.
|
||||||
alarmStatus = ALARM_RUNNING;
|
alarmStatus = ALARM_RUNNING;
|
||||||
|
|
||||||
// Cancel the pre-alarm notification, if it exists
|
|
||||||
stopService(new Intent(this, PreAlarmNotify.class));
|
|
||||||
|
|
||||||
// If dialing, active in a phone call, or on hold, don't bother with the alarm, just reset it for tomorrow
|
// If dialing, active in a phone call, or on hold, don't bother with the alarm, just reset it for tomorrow
|
||||||
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
|
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
|
||||||
if (telephonyManager.getCallState() != TelephonyManager.CALL_STATE_OFFHOOK) {
|
if (telephonyManager.getCallState() != TelephonyManager.CALL_STATE_OFFHOOK) {
|
||||||
@ -82,12 +90,6 @@ public class AlarmService extends Service {
|
|||||||
// Calculate when the grace period (converted from minutes to milliseconds) ends
|
// Calculate when the grace period (converted from minutes to milliseconds) ends
|
||||||
graceEndTime = System.currentTimeMillis() + (gracePeriod * 60 * 1000);
|
graceEndTime = System.currentTimeMillis() + (gracePeriod * 60 * 1000);
|
||||||
|
|
||||||
// Set up intents for later use
|
|
||||||
notifyIntent = new Intent(this, AlarmNotify.class);
|
|
||||||
alertActivityIntent = new Intent(this, AlarmAlertActivity.class);
|
|
||||||
alertActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
|
|
||||||
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
|
|
||||||
|
|
||||||
// Allow user to acknowledge alarm and cancel grace alarm
|
// Allow user to acknowledge alarm and cancel grace alarm
|
||||||
startAlert(this);
|
startAlert(this);
|
||||||
}
|
}
|
||||||
@ -109,7 +111,7 @@ public class AlarmService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void startAlert(final Context context) {
|
public static void startAlert(final Context context) {
|
||||||
Log.d("AlarmService", "Starting alarm; status is " + alarmStatus);
|
Log.d("AlarmService", "Starting alert; status is " + alarmStatus);
|
||||||
// Turn off any notifications first
|
// Turn off any notifications first
|
||||||
context.stopService(notifyIntent);
|
context.stopService(notifyIntent);
|
||||||
|
|
||||||
@ -125,10 +127,10 @@ public class AlarmService extends Service {
|
|||||||
if (alarmStatus.contentEquals(ALARM_DISMISSED) ||
|
if (alarmStatus.contentEquals(ALARM_DISMISSED) ||
|
||||||
alarmStatus.contentEquals(ALARM_SNOOZED)) {
|
alarmStatus.contentEquals(ALARM_SNOOZED)) {
|
||||||
return;
|
return;
|
||||||
}
|
// Stop if we've already run the snooze alert
|
||||||
// Stop if we're running the snooze alert, or the snooze time is less than 10 seconds
|
} else if (alarmStatus.contentEquals(ALARM_SNOOZE_RUNNING)) {
|
||||||
if (alarmStatus.contentEquals(ALARM_SNOOZE_RUNNING) || SNOOZE_TIME < 10000) {
|
alarmStatus = ALARM_IGNORED;
|
||||||
stopAlert(context);
|
context.stopService(alarmServiceIntent);
|
||||||
} else {
|
} else {
|
||||||
alarmStatus = ALARM_IGNORED; // This is true, although we are about to switch to ALARM_SNOOZED
|
alarmStatus = ALARM_IGNORED; // This is true, although we are about to switch to ALARM_SNOOZED
|
||||||
snoozeAlarm(context);
|
snoozeAlarm(context);
|
||||||
@ -138,39 +140,39 @@ public class AlarmService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void stopAlert(final Context context) {
|
public static void stopAlert(final Context context) {
|
||||||
Log.d("AlarmService", "Stopping alarm; status is " + alarmStatus);
|
Log.d("AlarmService", "Stopping alert; status is " + alarmStatus);
|
||||||
if (alarmStarted) {
|
if (alarmStarted) {
|
||||||
AlarmKlaxon.stop(context);
|
AlarmKlaxon.stop(context);
|
||||||
AlarmAlertActivity.alertActivity.finish();
|
AlarmAlertActivity.alertActivity.finish();
|
||||||
// Display a notification if the alarm hasn't been dismissed
|
|
||||||
if (!alarmStatus.contentEquals(ALARM_DISMISSED)) {
|
if (!alarmStatus.contentEquals(ALARM_DISMISSED)) {
|
||||||
context.startService(notifyIntent);
|
context.startService(notifyIntent);
|
||||||
}
|
}
|
||||||
alarmStarted = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void dismissAlarm(final Context context) {
|
public static void dismissAlarm(final Context context) {
|
||||||
Log.d("AlarmService", "Dismissing alarm");
|
Log.d("AlarmService", "Dismissing alarm");
|
||||||
alarmStatus = ALARM_DISMISSED;
|
alarmStatus = ALARM_DISMISSED;
|
||||||
// Close the alert and all notifications
|
|
||||||
stopAlert(context);
|
|
||||||
|
|
||||||
// Cancel the graceAlarm
|
// Cancel the graceAlarm
|
||||||
Intent graceIntent = new Intent(context, GraceReceiver.class);
|
Intent graceIntent = new Intent(context, GraceReceiver.class);
|
||||||
PendingIntent gracePendingIntent = PendingIntent.getBroadcast(context, MainActivity.GRACE_REQUEST, graceIntent, 0);
|
PendingIntent gracePendingIntent = PendingIntent.getBroadcast(context, MainActivity.GRACE_REQUEST, graceIntent, 0);
|
||||||
alarmManager.cancel(gracePendingIntent);
|
alarmManager.cancel(gracePendingIntent);
|
||||||
|
|
||||||
|
// Stop this service, along with the alert and all notifications
|
||||||
|
context.stopService(alarmServiceIntent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void snoozeAlarm(final Context context) {
|
public static void snoozeAlarm(final Context context) {
|
||||||
Log.d("AlarmService", "Snoozing alarm");
|
Log.d("AlarmService", "Snoozing alarm");
|
||||||
stopAlert(context);
|
|
||||||
// Close the alert, stop the klaxon, and start the notification,
|
// Close the alert, stop the klaxon, and start the notification,
|
||||||
// but only if there's time left before the gracePeriod triggers,
|
// but only if there's time left before the gracePeriod triggers,
|
||||||
// and we haven't snoozed before
|
// and we haven't snoozed before
|
||||||
if (((System.currentTimeMillis() + SNOOZE_TIME) < graceEndTime) &&
|
if (((System.currentTimeMillis() + SNOOZE_TIME) < graceEndTime) &&
|
||||||
|
(!alarmStatus.contentEquals(ALARM_SNOOZE_RUNNING)) &&
|
||||||
(!alarmStatus.contentEquals(ALARM_SNOOZED)) &&
|
(!alarmStatus.contentEquals(ALARM_SNOOZED)) &&
|
||||||
(!alarmStatus.contentEquals(ALARM_DISMISSED))) {
|
(!alarmStatus.contentEquals(ALARM_DISMISSED))) {
|
||||||
|
stopAlert(context);
|
||||||
new Handler().postDelayed(new Runnable() {
|
new Handler().postDelayed(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
Log.d("AlarmService", "Resuming after snooze; status is " + alarmStatus);
|
Log.d("AlarmService", "Resuming after snooze; status is " + alarmStatus);
|
||||||
@ -184,6 +186,8 @@ public class AlarmService extends Service {
|
|||||||
}, SNOOZE_TIME);
|
}, SNOOZE_TIME);
|
||||||
// Change alarm status from ignored to snoozed
|
// Change alarm status from ignored to snoozed
|
||||||
alarmStatus = ALARM_SNOOZED;
|
alarmStatus = ALARM_SNOOZED;
|
||||||
|
} else {
|
||||||
|
context.stopService(alarmServiceIntent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,11 +20,12 @@ public class CancelGraceReceiver extends BroadcastReceiver {
|
|||||||
|
|
||||||
// Ensure that any snoozes that are pending never happen.
|
// Ensure that any snoozes that are pending never happen.
|
||||||
AlarmService.setAlarmStatus(AlarmService.ALARM_DISMISSED);
|
AlarmService.setAlarmStatus(AlarmService.ALARM_DISMISSED);
|
||||||
|
context.stopService(new Intent(context, AlarmService.class));
|
||||||
// Display toast
|
|
||||||
Toast.makeText(context, context.getString(R.string.alarmCancelToast), Toast.LENGTH_LONG).show();
|
|
||||||
|
|
||||||
// Remove notification
|
// Remove notification
|
||||||
context.stopService(new Intent(context, AlarmNotify.class));
|
context.stopService(new Intent(context, AlarmNotify.class));
|
||||||
|
|
||||||
|
// Display toast
|
||||||
|
Toast.makeText(context, context.getString(R.string.alarmCancelToast), Toast.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -34,6 +34,9 @@ public class GraceReceiver extends BroadcastReceiver {
|
|||||||
phoneNumber = sharedPref.getString(context.getString(R.string.PhoneNumberPref), null);
|
phoneNumber = sharedPref.getString(context.getString(R.string.PhoneNumberPref), null);
|
||||||
message = sharedPref.getString(context.getString(R.string.MessagePref), context.getString(R.string.defaultMessage));
|
message = sharedPref.getString(context.getString(R.string.MessagePref), context.getString(R.string.defaultMessage));
|
||||||
|
|
||||||
|
// Stop any lingering alarm service
|
||||||
|
context.stopService(new Intent(context, AlarmService.class));
|
||||||
|
|
||||||
if (alarmActive) {
|
if (alarmActive) {
|
||||||
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS) {
|
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS) {
|
||||||
locationClient = new LocationClient(context,
|
locationClient = new LocationClient(context,
|
||||||
|
Loading…
Reference in New Issue
Block a user