Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 70874537 authored by Sara Ting's avatar Sara Ting
Browse files

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
parent 21049d25
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="com.google.android.googleapps.permission.GOOGLE_AUTH.mail" />
    <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="16"></uses-sdk>

@@ -168,10 +169,12 @@
        <receiver android:name=".alerts.AlertReceiver">
            <intent-filter>
                <action android:name="android.intent.action.EVENT_REMINDER" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.TIME_SET" />
                <data android:scheme="content" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.LOCALE_CHANGED" />
            </intent-filter>
@@ -188,6 +191,8 @@

        <service android:name=".alerts.SnoozeAlarmsService" />

        <service android:name=".alerts.InitAlarmsService" />

        <!-- Declarations for the widget -->
        <receiver android:name=".widget.CalendarAppWidgetProvider" android:label="@string/gadget_title">
            <intent-filter>
+13 −2
Original line number Diff line number Diff line
@@ -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);
+54 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}