OSDN Git Service

Fix a couple race conditions with expired alarms.
authorPatrick Scott <phanna@android.com>
Fri, 18 Sep 2009 20:02:09 +0000 (16:02 -0400)
committerPatrick Scott <phanna@android.com>
Fri, 18 Sep 2009 20:35:48 +0000 (16:35 -0400)
Log the alarm time in a pretty format for easier debugging. Disable expired
alarms when calculating the next alert. This solves 2 problems: an expired alarm
remains checked in the UI and the expired alarm is sent to the alarm manager. I
believe the second problem is causing bug 2080160.

src/com/android/alarmclock/AlarmReceiver.java
src/com/android/alarmclock/Alarms.java

index 3487001..97374ef 100644 (file)
@@ -27,6 +27,9 @@ import android.content.BroadcastReceiver;
 import android.database.Cursor;
 import android.os.Parcel;
 
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
 /**
  * Glue class: connects AlarmAlert IntentReceiver to AlarmAlert
  * activity.  Passes through Alarm ID.
@@ -74,16 +77,17 @@ public class AlarmReceiver extends BroadcastReceiver {
             return;
         }
 
+        // Intentionally verbose: always log the alarm time to provide useful
+        // information in bug reports.
         long now = System.currentTimeMillis();
-        /* FIXME Intentionally verbose: always log this until we've
-           fully debugged the app failing to start up */
+        SimpleDateFormat format =
+                new SimpleDateFormat("HH:mm:ss.SSS aaa");
         Log.v("AlarmReceiver.onReceive() id " + alarm.id + " setFor "
-                + alarm.time + " now " + now);
+                + format.format(new Date(alarm.time)));
 
         if (now > alarm.time + STALE_WINDOW * 1000) {
             if (Log.LOGV) {
-                Log.v("AlarmReceiver ignoring stale alarm intent id" + alarm.id
-                        + " setFor " + alarm.time + " now " + now);
+                Log.v("AlarmReceiver ignoring stale alarm");
             }
             return;
         }
@@ -118,9 +122,11 @@ public class AlarmReceiver extends BroadcastReceiver {
         // Disable this alarm if it does not repeat.
         if (!alarm.daysOfWeek.isRepeatSet()) {
             Alarms.enableAlarm(context, alarm.id, false);
+        } else {
+            // Enable the next alert if there is one. The above call to
+            // enableAlarm will call setNextAlert so avoid calling it twice.
+            Alarms.setNextAlert(context);
         }
-        // Enable the next alert if there is one.
-        Alarms.setNextAlert(context);
 
         // Play the alarm alert and vibrate the device.
         Intent playAlarm = new Intent(Alarms.ALARM_ALERT_ACTION);
index d4973fa..63a67d7 100644 (file)
@@ -237,6 +237,7 @@ public class Alarms {
     public static Alarm calculateNextAlert(final Context context) {
         Alarm alarm = null;
         long minTime = Long.MAX_VALUE;
+        long now = System.currentTimeMillis();
         Cursor cursor = getFilteredAlarmsCursor(context.getContentResolver());
         if (cursor != null) {
             if (cursor.moveToFirst()) {
@@ -247,6 +248,10 @@ public class Alarms {
                     if (a.time == 0) {
                         a.time = calculateAlarm(a.hour, a.minutes, a.daysOfWeek)
                                 .getTimeInMillis();
+                    } else if (a.time < now) {
+                        // Expired alarm, disable it and move along.
+                        enableAlarmInternal(context, a, false);
+                        continue;
                     }
                     if (a.time < minTime) {
                         minTime = a.time;