1 /* 2 * Copyright (C) 2007 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 17 package com.android.deskclock; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.SharedPreferences; 23 import android.os.PowerManager.WakeLock; 24 import android.preference.PreferenceManager; 25 26 import com.android.deskclock.alarms.AlarmStateManager; 27 28 import com.android.deskclock.timer.TimerObj; 29 30 public class AlarmInitReceiver extends BroadcastReceiver { 31 32 // A flag that indicates that switching the volume button default was done 33 private static final String PREF_VOLUME_DEF_DONE = "vol_def_done"; 34 35 /** 36 * This receiver handles a variety of actions: 37 * 38 * <ul> 39 * <li>Clean up backup data that was recently restored to this device on 40 * ACTION_COMPLETE_RESTORE.</li> 41 * <li>Clean up backup data that was recently restored to this device and reset timers and 42 * clear stopwatch on ACTION_BOOT_COMPLETED</li> 43 * <li>Fix alarm states on ACTION_BOOT_COMPLETED, TIME_SET, TIMEZONE_CHANGED, 44 * and LOCALE_CHANGED</li> 45 * </ul> 46 */ 47 @Override onReceive(final Context context, Intent intent)48 public void onReceive(final Context context, Intent intent) { 49 final String action = intent.getAction(); 50 LogUtils.v("AlarmInitReceiver " + action); 51 52 final PendingResult result = goAsync(); 53 final WakeLock wl = AlarmAlertWakeLock.createPartialWakeLock(context); 54 wl.acquire(); 55 56 // We need to increment the global id out of the async task to prevent race conditions 57 AlarmStateManager.updateGlobalIntentId(context); 58 59 AsyncHandler.post(new Runnable() { 60 @Override public void run() { 61 try { 62 if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { 63 // Clear stopwatch and timers data 64 final SharedPreferences prefs = 65 PreferenceManager.getDefaultSharedPreferences(context); 66 LogUtils.v("AlarmInitReceiver - Reset timers and clear stopwatch data"); 67 TimerObj.resetTimersInSharedPrefs(prefs); 68 Utils.clearSwSharedPref(prefs); 69 70 if (!prefs.getBoolean(PREF_VOLUME_DEF_DONE, false)) { 71 // Fix the default 72 LogUtils.v("AlarmInitReceiver - resetting volume button default"); 73 switchVolumeButtonDefault(prefs); 74 } 75 } 76 77 // Process restored data if any exists 78 if (!DeskClockBackupAgent.processRestoredData(context)) { 79 // Update all the alarm instances on time change event 80 AlarmStateManager.fixAlarmInstances(context); 81 } 82 } finally { 83 result.finish(); 84 wl.release(); 85 LogUtils.v("AlarmInitReceiver finished"); 86 } 87 } 88 }); 89 } 90 switchVolumeButtonDefault(SharedPreferences prefs)91 private void switchVolumeButtonDefault(SharedPreferences prefs) { 92 SharedPreferences.Editor editor = prefs.edit(); 93 94 editor.putString(SettingsActivity.KEY_VOLUME_BEHAVIOR, 95 SettingsActivity.DEFAULT_VOLUME_BEHAVIOR); 96 97 // Make sure we do it only once 98 editor.putBoolean(PREF_VOLUME_DEF_DONE, true); 99 editor.apply(); 100 } 101 } 102