Fix runnable to actually exit the alarm alert.

This commit is contained in:
Timothy Allen 2014-03-26 13:34:37 +02:00
parent 68c2abd06d
commit 08b09b110b
2 changed files with 15 additions and 14 deletions

View File

@ -27,12 +27,11 @@ public class AlarmAlertActivity extends Activity {
// TODO correct alert lifetime
private static final int ALERT_LIFE = 1000*10;//1000*60*2; // 2 minutes
private static final long[] vPattern = {500, 500};
private static final Handler handler = new Handler();
private static Runnable r;
private static AlarmManager graceManager;
private static PendingIntent gracePendingIntent;
private static Intent notifyIntent;
private static Vibrator vibrator;
private static Boolean userCancelled;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -41,7 +40,7 @@ public class AlarmAlertActivity extends Activity {
Window window = getWindow();
// Set to use the full screen
int fullScreen = WindowManager.LayoutParams.FLAG_FULLSCREEN;
// if KitKat, mimic the main alarm
// TODO if KitKat, mimic the main alarm
fullScreen = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
@ -56,21 +55,23 @@ public class AlarmAlertActivity extends Activity {
stopService(notifyIntent);
// Turn off the alert activity, and switch to a notification
r = new Runnable() {
new Handler().postDelayed(new Runnable() {
public void run() {
// Close the dialogue
finish();
// Switch to notification if the Activity has not been closed by the user
startService(notifyIntent);
if (!userCancelled) {
// Close the dialogue and switch to notification
// if the Activity has not been closed by the user
finish();
startService(notifyIntent);
}
Log.d("AlarmAlertActivity", "Started notification");
}
};
handler.postDelayed(r, ALERT_LIFE);
}, ALERT_LIFE);
}
@Override
public void onStart() {
super.onStart();
userCancelled = false;
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(vPattern, 0);
@ -86,6 +87,8 @@ public class AlarmAlertActivity extends Activity {
gracePendingIntent = PendingIntent.getBroadcast(getApplicationContext(), MainActivity.GRACE_REQUEST, graceIntent, 0);
graceManager.cancel(gracePendingIntent);
Log.d("AlarmAlertActivity", "Cancelled grace alarm.");
// Ensure we don't load a notification now
userCancelled = true;
// Close the dialogue (stop vibration &c)
finish();
}
@ -93,8 +96,6 @@ public class AlarmAlertActivity extends Activity {
}
public void onStop() {
// Ensure we don't load a notification now
handler.removeCallbacks(r);
vibrator.cancel();
super.onStop();
}

View File

@ -86,7 +86,7 @@ public class AlarmNotify extends Service {
int secondsLeft = (int) ((endTime - System.currentTimeMillis())) / (1000);
int gracePeriodSeconds = gracePeriod * 60;
// Multiply each int by 1000 for greater progress resolution
int progress = ((((gracePeriodSeconds * 1000) - (secondsLeft * 1000)) * max) / (gracePeriodSeconds * 1000));
int progress = (((gracePeriodSeconds * 1000) - (secondsLeft * 1000)) * max) / (gracePeriodSeconds * 1000);
while (progress < max) {
// Stop the thread if cancelled elsewhere
@ -102,7 +102,7 @@ public class AlarmNotify extends Service {
// Prepare secondsLeft and progress for the next loop
secondsLeft = secondsLeft - (UPDATE_INTERVAL / 1000);
// Multiply each int by 1000 for greater progress resolution
progress = ((((gracePeriodSeconds * 1000) - (secondsLeft * 1000)) * max) / (gracePeriodSeconds * 1000));
progress = (((gracePeriodSeconds * 1000) - (secondsLeft * 1000)) * max) / (gracePeriodSeconds * 1000);
// Sleeps the thread, simulating an operation
// that takes time
try {