- 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
|
||||
public void onBackPressed() {
|
||||
AlarmService.setAlarmStatus(AlarmService.ALARM_IGNORED);
|
||||
if (!AlarmService.alarmStatus.contentEquals(AlarmService.ALARM_SNOOZE_RUNNING)) {
|
||||
AlarmService.setAlarmStatus(AlarmService.ALARM_IGNORED);
|
||||
}
|
||||
AlarmService.snoozeAlarm(this);
|
||||
}
|
||||
|
||||
public void onUserLeaveHint() {
|
||||
AlarmService.setAlarmStatus(AlarmService.ALARM_IGNORED);
|
||||
if (!AlarmService.alarmStatus.contentEquals(AlarmService.ALARM_SNOOZE_RUNNING)) {
|
||||
AlarmService.setAlarmStatus(AlarmService.ALARM_IGNORED);
|
||||
}
|
||||
AlarmService.snoozeAlarm(this);
|
||||
}
|
||||
}
|
@ -66,15 +66,6 @@ public class AlarmNotify extends Service {
|
||||
// Allow the user to cancel by selecting the ContentText or ContentTitle
|
||||
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.notify(notifyID, notification.build());
|
||||
|
@ -16,10 +16,10 @@ import android.util.Log;
|
||||
import java.util.Calendar;
|
||||
|
||||
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 ALERT_LIFE = 1000*60*2; // 2 minutes
|
||||
private static final int SNOOZE_TIME = 1000*60;//60*5; // Snooze for 5 minutes if need be
|
||||
private static final int ALERT_LIFE = 1000*10;//60*2; // 2 minutes
|
||||
private static AlarmManager alarmManager;
|
||||
private static Intent alertActivityIntent, notifyIntent;
|
||||
private static Intent alarmServiceIntent, alertActivityIntent, notifyIntent;
|
||||
public static Boolean alarmStarted = false;
|
||||
public static volatile String alarmStatus; // Register ALARM_DISMISSED and its brethren here
|
||||
public static final String ALARM_RUNNING = "ALARM_RUNNING";
|
||||
@ -35,6 +35,7 @@ public class AlarmService extends Service {
|
||||
}
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
Log.d("AlarmService", "Destroying alarm");
|
||||
if (alarmStarted) {
|
||||
stopAlert(getApplicationContext());
|
||||
alarmStarted = false;
|
||||
@ -43,24 +44,31 @@ public class AlarmService extends Service {
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
alarmServiceIntent = intent;
|
||||
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
final Boolean alarmActive = sharedPref.getBoolean(getString(R.string.AlarmActivePref), true);
|
||||
final int gracePeriod = sharedPref.getInt(getString(R.string.GracePeriodPref), 60);
|
||||
final String alarmTimeStr = sharedPref.getString(getString(R.string.AlarmTimePref), null);
|
||||
|
||||
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) {
|
||||
stopAlert(getApplicationContext());
|
||||
stopService(notifyIntent);
|
||||
}
|
||||
alarmStarted = true;
|
||||
|
||||
// if nothing else happens, assume the alert was ignored.
|
||||
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
|
||||
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
|
||||
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
|
||||
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
|
||||
startAlert(this);
|
||||
}
|
||||
@ -109,7 +111,7 @@ public class AlarmService extends Service {
|
||||
}
|
||||
|
||||
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
|
||||
context.stopService(notifyIntent);
|
||||
|
||||
@ -125,10 +127,10 @@ public class AlarmService extends Service {
|
||||
if (alarmStatus.contentEquals(ALARM_DISMISSED) ||
|
||||
alarmStatus.contentEquals(ALARM_SNOOZED)) {
|
||||
return;
|
||||
}
|
||||
// Stop if we're running the snooze alert, or the snooze time is less than 10 seconds
|
||||
if (alarmStatus.contentEquals(ALARM_SNOOZE_RUNNING) || SNOOZE_TIME < 10000) {
|
||||
stopAlert(context);
|
||||
// Stop if we've already run the snooze alert
|
||||
} else if (alarmStatus.contentEquals(ALARM_SNOOZE_RUNNING)) {
|
||||
alarmStatus = ALARM_IGNORED;
|
||||
context.stopService(alarmServiceIntent);
|
||||
} else {
|
||||
alarmStatus = ALARM_IGNORED; // This is true, although we are about to switch to ALARM_SNOOZED
|
||||
snoozeAlarm(context);
|
||||
@ -138,39 +140,39 @@ public class AlarmService extends Service {
|
||||
}
|
||||
|
||||
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) {
|
||||
AlarmKlaxon.stop(context);
|
||||
AlarmAlertActivity.alertActivity.finish();
|
||||
// Display a notification if the alarm hasn't been dismissed
|
||||
if (!alarmStatus.contentEquals(ALARM_DISMISSED)) {
|
||||
context.startService(notifyIntent);
|
||||
}
|
||||
alarmStarted = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void dismissAlarm(final Context context) {
|
||||
Log.d("AlarmService", "Dismissing alarm");
|
||||
alarmStatus = ALARM_DISMISSED;
|
||||
// Close the alert and all notifications
|
||||
stopAlert(context);
|
||||
|
||||
// Cancel the graceAlarm
|
||||
Intent graceIntent = new Intent(context, GraceReceiver.class);
|
||||
PendingIntent gracePendingIntent = PendingIntent.getBroadcast(context, MainActivity.GRACE_REQUEST, graceIntent, 0);
|
||||
alarmManager.cancel(gracePendingIntent);
|
||||
|
||||
// Stop this service, along with the alert and all notifications
|
||||
context.stopService(alarmServiceIntent);
|
||||
}
|
||||
|
||||
public static void snoozeAlarm(final Context context) {
|
||||
Log.d("AlarmService", "Snoozing alarm");
|
||||
stopAlert(context);
|
||||
// Close the alert, stop the klaxon, and start the notification,
|
||||
// but only if there's time left before the gracePeriod triggers,
|
||||
// and we haven't snoozed before
|
||||
if (((System.currentTimeMillis() + SNOOZE_TIME) < graceEndTime) &&
|
||||
(!alarmStatus.contentEquals(ALARM_SNOOZE_RUNNING)) &&
|
||||
(!alarmStatus.contentEquals(ALARM_SNOOZED)) &&
|
||||
(!alarmStatus.contentEquals(ALARM_DISMISSED))) {
|
||||
stopAlert(context);
|
||||
new Handler().postDelayed(new Runnable() {
|
||||
public void run() {
|
||||
Log.d("AlarmService", "Resuming after snooze; status is " + alarmStatus);
|
||||
@ -184,6 +186,8 @@ public class AlarmService extends Service {
|
||||
}, SNOOZE_TIME);
|
||||
// Change alarm status from ignored to 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.
|
||||
AlarmService.setAlarmStatus(AlarmService.ALARM_DISMISSED);
|
||||
|
||||
// Display toast
|
||||
Toast.makeText(context, context.getString(R.string.alarmCancelToast), Toast.LENGTH_LONG).show();
|
||||
context.stopService(new Intent(context, AlarmService.class));
|
||||
|
||||
// Remove notification
|
||||
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);
|
||||
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 (GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS) {
|
||||
locationClient = new LocationClient(context,
|
||||
|
Loading…
Reference in New Issue
Block a user