diff options
author | Sara Ting <sarating@google.com> | 2012-10-18 11:46:33 -0700 |
---|---|---|
committer | Sara Ting <sarating@google.com> | 2012-10-24 15:27:45 -0700 |
commit | 708745373284ae3e816fad16554ecb95574b5ef3 (patch) | |
tree | 9e36c309376be36e908d8469822a1bf550f32f13 /src/com/android/calendar/alerts | |
parent | 21049d25dcf8f055dc046a889b0a514833ad599a (diff) | |
download | android_packages_apps_Calendar-708745373284ae3e816fad16554ecb95574b5ef3.tar.gz android_packages_apps_Calendar-708745373284ae3e816fad16554ecb95574b5ef3.tar.bz2 android_packages_apps_Calendar-708745373284ae3e816fad16554ecb95574b5ef3.zip |
Minimize chance of race condition bug that misses alarms by clearing/rescheduling alarms.
This was properly fixed in the provider but this tries to minimize the bug until that fix is rolled out everywhere.
Bug:7221716
Change-Id: I4cb32b48caf31c8372770b74d7983e54480b755a
Diffstat (limited to 'src/com/android/calendar/alerts')
-rw-r--r-- | src/com/android/calendar/alerts/AlertService.java | 15 | ||||
-rw-r--r-- | src/com/android/calendar/alerts/InitAlarmsService.java | 54 |
2 files changed, 67 insertions, 2 deletions
diff --git a/src/com/android/calendar/alerts/AlertService.java b/src/com/android/calendar/alerts/AlertService.java index ac1ca26d..8c2d3d2e 100644 --- a/src/com/android/calendar/alerts/AlertService.java +++ b/src/com/android/calendar/alerts/AlertService.java @@ -177,8 +177,19 @@ public class AlertService extends Service { action.equals(android.provider.CalendarContract.ACTION_EVENT_REMINDER) || action.equals(Intent.ACTION_LOCALE_CHANGED)) { updateAlertNotification(this); - } else if (action.equals(Intent.ACTION_BOOT_COMPLETED) - || action.equals(Intent.ACTION_TIME_CHANGED)) { + } else if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { + // The provider usually initiates this setting up of alarms on startup, + // but there was a bug (b/7221716) where a race condition caused this step to be + // skipped, resulting in missed alarms. This is a stopgap to minimize this bug + // for devices that don't have the provider fix, by initiating this a 2nd time here. + // However, it would still theoretically be possible to hit the race condition + // the 2nd time and still miss alarms. + // + // TODO: Remove this when the provider fix is rolled out everywhere. + Intent intent = new Intent(); + intent.setClass(this, InitAlarmsService.class); + startService(intent); + } else if (action.equals(Intent.ACTION_TIME_CHANGED)) { doTimeChanged(); } else if (action.equals(AlertReceiver.ACTION_DISMISS_OLD_REMINDERS)) { dismissOldAlerts(this); diff --git a/src/com/android/calendar/alerts/InitAlarmsService.java b/src/com/android/calendar/alerts/InitAlarmsService.java new file mode 100644 index 00000000..7655dc8c --- /dev/null +++ b/src/com/android/calendar/alerts/InitAlarmsService.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.calendar.alerts; + +import android.app.IntentService; +import android.content.ContentValues; +import android.content.Intent; +import android.net.Uri; +import android.os.SystemClock; +import android.provider.CalendarContract; +import android.util.Log; + +/** + * Service for clearing all scheduled alerts from the CalendarAlerts table and + * rescheduling them. This is expected to be called only on boot up, to restore + * the AlarmManager alarms that were lost on device restart. + */ +public class InitAlarmsService extends IntentService { + private static final String TAG = "InitAlarmsService"; + private static final String SCHEDULE_ALARM_REMOVE_PATH = "schedule_alarms_remove"; + private static final Uri SCHEDULE_ALARM_REMOVE_URI = Uri.withAppendedPath( + CalendarContract.CONTENT_URI, SCHEDULE_ALARM_REMOVE_PATH); + + // Delay for rescheduling the alarms must be great enough to minimize race + // conditions with the provider's boot up actions. + private static final long DELAY_MS = 30000; + + public InitAlarmsService() { + super("InitAlarmsService"); + } + + @Override + protected void onHandleIntent(Intent intent) { + // Delay to avoid race condition of in-progress alarm scheduling in provider. + SystemClock.sleep(DELAY_MS); + Log.d(TAG, "Clearing and rescheduling alarms."); + getContentResolver().update(SCHEDULE_ALARM_REMOVE_URI, new ContentValues(), null, + null); + } +} |