From a1ed6eb45bfba2c7f578fd0ea9db2e6fbef5bae7 Mon Sep 17 00:00:00 2001 From: tim Date: Sat, 13 Sep 2014 19:17:53 +0200 Subject: [PATCH] Fix integer overflow in milliseconds. --- HypoAlarm/HypoAlarm-HypoAlarm.iml | 6 +++--- .../org/treehouse/HypoAlarm/AlarmNotify.java | 21 +++++++++---------- .../org/treehouse/HypoAlarm/AlarmService.java | 1 + 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/HypoAlarm/HypoAlarm-HypoAlarm.iml b/HypoAlarm/HypoAlarm-HypoAlarm.iml index 4821e21..1826aaa 100644 --- a/HypoAlarm/HypoAlarm-HypoAlarm.iml +++ b/HypoAlarm/HypoAlarm-HypoAlarm.iml @@ -31,22 +31,22 @@ + - + - + - 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 068b968..72dd5eb 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/HypoAlarm/AlarmNotify.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/HypoAlarm/AlarmNotify.java @@ -68,7 +68,6 @@ public class AlarmNotify extends Service { // Allow the user to cancel by selecting the ContentText or ContentTitle notification.setContentIntent(cancellerPendingIntent); - nm.cancel(notifyID); nm.notify(notifyID, notification.build()); @@ -79,9 +78,10 @@ public class AlarmNotify extends Service { // Make progress out of 1000 and not just 100, for greater resolution int max = 1000; // Count in milliseconds for greater progress resolution - int milliSecondsLeft = (int) ((AlarmService.graceEndTime - System.currentTimeMillis())); - int gracePeriodMilliSeconds = gracePeriod * 60 * 1000; - int progress = ((gracePeriodMilliSeconds - milliSecondsLeft) * max) / gracePeriodMilliSeconds; + long graceEndTime = AlarmService.graceEndTime; + long milliSecondsLeft = ((graceEndTime - System.currentTimeMillis())); + long gracePeriodMilliSeconds = gracePeriod * 60 * 1000; + int progress = (int) (((gracePeriodMilliSeconds - milliSecondsLeft) * max) / gracePeriodMilliSeconds); while (progress < max) { // Stop the thread if the notification is cancelled elsewhere @@ -89,8 +89,10 @@ public class AlarmNotify extends Service { return; } // Recalculate milliSecondsLeft so that progress gets updated correctly. - milliSecondsLeft = (int) ((AlarmService.graceEndTime - System.currentTimeMillis())); - int minutesLeft = (milliSecondsLeft / 1000) / 60; + milliSecondsLeft = ((graceEndTime - System.currentTimeMillis())); + // Recalculate the progress + progress = (int) (((gracePeriodMilliSeconds - milliSecondsLeft) * max) / gracePeriodMilliSeconds); + int minutesLeft = (int) ((milliSecondsLeft / 1000) / 60); if (Build.VERSION.SDK_INT >= 11) { notification.setContentText(String.format(getString(R.string.notificationText), MainActivity.MinutesToGracePeriodStr(minutesLeft))); @@ -98,11 +100,8 @@ public class AlarmNotify extends Service { // Update the notification nm.notify(notifyID, notification.build()); } - // Prepare secondsLeft and progress for the next loop - // Recalculate the progress - progress = ((gracePeriodMilliSeconds - milliSecondsLeft) * max) / gracePeriodMilliSeconds; - //Log.d("AlarmNotify", "milliSecondsLeft is " + milliSecondsLeft + " and progress is " + progress + " (gracePeriodMilliSeconds is " + gracePeriodMilliSeconds + ")"); - Log.d("AlarmNotify", "progress is " + progress + "; max is " + max); + //Log.d("AlarmNotify", "milliSecondsLeft is " + milliSecondsLeft + " and gracePeriodMilliSeconds is " + gracePeriodMilliSeconds); + //Log.d("AlarmNotify", "progress is " + progress + "; max is " + max); // Sleep until we need to update again try { diff --git a/HypoAlarm/src/main/java/za/org/treehouse/HypoAlarm/AlarmService.java b/HypoAlarm/src/main/java/za/org/treehouse/HypoAlarm/AlarmService.java index 6e3d532..ed9a992 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/HypoAlarm/AlarmService.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/HypoAlarm/AlarmService.java @@ -177,6 +177,7 @@ public class AlarmService extends Service { // Change alarm status from ignored to snoozed alarmStatus = ALARM_SNOOZED; } else { + Log.d("AlarmService", "Actually, not snoozing!"); context.stopService(alarmServiceIntent); } }