1 /* 2 ** Copyright 2006, 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 ** See the License for the specific language governing permissions and 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** limitations under the License. 15 */ 16 17 package com.android.providers.calendar; 18 19 import android.content.BroadcastReceiver; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.os.PowerManager; 24 import android.util.Log; 25 26 import java.util.concurrent.ExecutorService; 27 import java.util.concurrent.Executors; 28 29 /** 30 * This IntentReceiver executes when the boot completes and ensures that 31 * the Calendar provider has started and then initializes the alarm 32 * scheduler for the Calendar provider. This needs to be done after 33 * the boot completes because the alarm manager may not have been started 34 * yet. 35 */ 36 public class CalendarReceiver extends BroadcastReceiver { 37 private static final String TAG = "CalendarReceiver"; 38 39 private final ExecutorService executor = Executors.newCachedThreadPool(); 40 private PowerManager.WakeLock mWakeLock; 41 42 @Override onReceive(Context context, Intent intent)43 public void onReceive(Context context, Intent intent) { 44 if (mWakeLock == null) { 45 PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 46 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CalendarReceiver_Provider"); 47 mWakeLock.setReferenceCounted(true); 48 } 49 mWakeLock.acquire(); 50 51 final String action = intent.getAction(); 52 final ContentResolver cr = context.getContentResolver(); 53 final PendingResult result = goAsync(); 54 executor.submit(new Runnable() { 55 @Override 56 public void run() { 57 if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { 58 removeScheduledAlarms(cr); 59 } 60 result.finish(); 61 mWakeLock.release(); 62 } 63 }); 64 } 65 66 /* 67 * Remove alarms from the CalendarAlerts table that have been marked 68 * as "scheduled" but not fired yet. We do this because the 69 * AlarmManagerService loses all information about alarms when the 70 * power turns off but we store the information in a database table 71 * that persists across reboots. See the documentation for 72 * scheduleNextAlarmLocked() for more information. 73 * 74 * We don't expect this to be called more than once. If it were, we would have to 75 * worry about serializing the use of the service. 76 */ removeScheduledAlarms(ContentResolver resolver)77 private void removeScheduledAlarms(ContentResolver resolver) { 78 if (Log.isLoggable(TAG, Log.DEBUG)) { 79 Log.d(TAG, "Removing scheduled alarms"); 80 } 81 resolver.update(CalendarAlarmManager.SCHEDULE_ALARM_REMOVE_URI, null /* values */, 82 null /* where */, null /* selectionArgs */); 83 } 84 } 85