Merge "Fix calendar alarms not being triggered after 22h"
am: 3525221de1

Change-Id: I0c7dec74757a4f38aa82f774bc0d83beb30e23bc
diff --git a/src/com/android/providers/calendar/CalendarAlarmManager.java b/src/com/android/providers/calendar/CalendarAlarmManager.java
index c77b654..8586e6b 100644
--- a/src/com/android/providers/calendar/CalendarAlarmManager.java
+++ b/src/com/android/providers/calendar/CalendarAlarmManager.java
@@ -63,16 +63,6 @@
     /* package */static final Uri SCHEDULE_ALARM_URI = Uri.withAppendedPath(
             CalendarContract.CONTENT_URI, SCHEDULE_ALARM_PATH);
 
-    /**
-     * If no alarms are scheduled in the next 24h, check for future alarms again after this period
-     * has passed. Scheduling the check 15 minutes earlier than 24h to prevent the scheduler alarm
-     * from using up the alarms quota for reminders during dozing.
-     *
-     * @see AlarmManager#setExactAndAllowWhileIdle
-     */
-    private static final long ALARM_CHECK_WHEN_NO_ALARM_IS_SCHEDULED_INTERVAL_MILLIS =
-            DateUtils.DAY_IN_MILLIS - (15 * DateUtils.MINUTE_IN_MILLIS);
-
     static final String INVALID_CALENDARALERTS_SELECTOR =
     "_id IN (SELECT ca." + CalendarAlerts._ID + " FROM "
             + Tables.CALENDAR_ALERTS + " AS ca"
@@ -278,7 +268,10 @@
 
         final long currentMillis = System.currentTimeMillis();
         final long start = currentMillis - SCHEDULE_ALARM_SLACK;
-        final long end = start + (24 * 60 * 60 * 1000);
+        final long end = currentMillis + DateUtils.DAY_IN_MILLIS;
+
+        boolean alarmScheduled = false;
+
         if (Log.isLoggable(CalendarProvider2.TAG, Log.DEBUG)) {
             time.set(start);
             String startTimeStr = time.format(" %a, %b %d, %Y %I:%M%P");
@@ -459,6 +452,7 @@
                 }
 
                 scheduleAlarm(alarmTime);
+                alarmScheduled = true;
             }
         } finally {
             if (cursor != null) {
@@ -469,14 +463,17 @@
         // Refresh notification bar
         if (rowsDeleted > 0) {
             scheduleAlarm(currentMillis);
+            alarmScheduled = true;
         }
 
-        // No event alarm is scheduled, check again in 24 hours. If a new
-        // event is inserted before the next alarm check, then this method
-        // will be run again when the new event is inserted.
-        if (nextAlarmTime == Long.MAX_VALUE) {
-            scheduleNextAlarmCheck(
-                    currentMillis + ALARM_CHECK_WHEN_NO_ALARM_IS_SCHEDULED_INTERVAL_MILLIS);
+        // No event alarm is scheduled, check again in 24 hours - 15
+        // minutes. Scheduling the check 15 minutes earlier than 24
+        // hours to prevent the scheduler alarm from using up the
+        // alarms quota for reminders during dozing. If a new event is
+        // inserted before the next alarm check, then this method will
+        // be run again when the new event is inserted.
+        if (!alarmScheduled) {
+            scheduleNextAlarmCheck(end - (15 * DateUtils.MINUTE_IN_MILLIS));
         }
     }
 
@@ -516,6 +513,10 @@
         mAlarmManager.cancel(operation);
     }
 
+    /**
+     * Only run inside scheduleNextAlarmLocked, please!
+     * mAlarmScheduled is specific to that method, currently.
+     */
     public void scheduleAlarm(long alarmTime) {
         // Debug log for investigating dozing related bugs, remove it once we confirm it is stable.
         if (Build.IS_DEBUGGABLE) {