From 08b09b110b6cd14e75d4c2e645c09f3ab37464a9 Mon Sep 17 00:00:00 2001 From: tim Date: Wed, 26 Mar 2014 13:34:37 +0200 Subject: [PATCH] Fix runnable to actually exit the alarm alert. --- .../hypoalarm/AlarmAlertActivity.java | 25 ++++++++++--------- .../org/treehouse/hypoalarm/AlarmNotify.java | 4 +-- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmAlertActivity.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmAlertActivity.java index 030794a..7afaebe 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmAlertActivity.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmAlertActivity.java @@ -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(); } diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java index 29e8e7a..fd4cc17 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java @@ -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 {