Fix race condition for missing alerts after boot up.

After boot up, it was possible to miss scheduling alerts due to improper synchronization of the BOOT_COMPLETED and TIME_CHANGED actions.

Bug:7221716
Change-Id: Id97266a19bb1ff8182576f687c34e10ef8644dc6
diff --git a/src/com/android/providers/calendar/CalendarAlarmManager.java b/src/com/android/providers/calendar/CalendarAlarmManager.java
index f25d3de..5f8c00c 100644
--- a/src/com/android/providers/calendar/CalendarAlarmManager.java
+++ b/src/com/android/providers/calendar/CalendarAlarmManager.java
@@ -137,7 +137,13 @@
     }
 
     void scheduleNextAlarm(boolean removeAlarms) {
-        if (!mNextAlarmCheckScheduled.getAndSet(true)) {
+        // We must always run the following when 'removeAlarms' is true.  Previously it
+        // was possible to have a race condition on startup between TIME_CHANGED and
+        // BOOT_COMPLETED broadcast actions.  This resulted in alarms being
+        // missed (Bug 7221716) when the TIME_CHANGED broadcast ('removeAlarms' = false)
+        // happened right before the BOOT_COMPLETED ('removeAlarms' = true), and the
+        // BOOT_COMPLETED action was skipped since there was concurrent scheduling in progress.
+        if (!mNextAlarmCheckScheduled.getAndSet(true) || removeAlarms) {
             if (Log.isLoggable(CalendarProvider2.TAG, Log.DEBUG)) {
                 Log.d(CalendarProvider2.TAG, "Scheduling check of next Alarm");
             }