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

View File

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