1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.calendar.alerts;
18
19import android.app.IntentService;
20import android.content.ContentValues;
21import android.content.Intent;
22import android.net.Uri;
23import android.os.SystemClock;
24import android.provider.CalendarContract;
25import android.util.Log;
26
27/**
28 * Service for clearing all scheduled alerts from the CalendarAlerts table and
29 * rescheduling them.  This is expected to be called only on boot up, to restore
30 * the AlarmManager alarms that were lost on device restart.
31 */
32public class InitAlarmsService extends IntentService {
33    private static final String TAG = "InitAlarmsService";
34    private static final String SCHEDULE_ALARM_REMOVE_PATH = "schedule_alarms_remove";
35    private static final Uri SCHEDULE_ALARM_REMOVE_URI = Uri.withAppendedPath(
36            CalendarContract.CONTENT_URI, SCHEDULE_ALARM_REMOVE_PATH);
37
38    // Delay for rescheduling the alarms must be great enough to minimize race
39    // conditions with the provider's boot up actions.
40    private static final long DELAY_MS = 30000;
41
42    public InitAlarmsService() {
43        super("InitAlarmsService");
44    }
45
46    @Override
47    protected void onHandleIntent(Intent intent) {
48        // Delay to avoid race condition of in-progress alarm scheduling in provider.
49        SystemClock.sleep(DELAY_MS);
50        Log.d(TAG, "Clearing and rescheduling alarms.");
51        try {
52            getContentResolver().update(SCHEDULE_ALARM_REMOVE_URI, new ContentValues(), null,
53                    null);
54        } catch (java.lang.IllegalArgumentException e) {
55            // java.lang.IllegalArgumentException:
56            //     Unknown URI content://com.android.calendar/schedule_alarms_remove
57
58            // Until b/7742576 is resolved, just catch the exception so the app won't crash
59            Log.e(TAG, "update failed: " + e.toString());
60        }
61    }
62}
63