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 17package com.android.deskclock; 18 19import android.content.BroadcastReceiver; 20import android.content.ContentResolver; 21import android.content.Context; 22import android.content.Intent; 23import android.content.SharedPreferences; 24import android.os.PowerManager.WakeLock; 25import android.preference.PreferenceManager; 26 27import com.android.deskclock.alarms.AlarmStateManager; 28import com.android.deskclock.provider.Alarm; 29 30import com.android.deskclock.provider.AlarmInstance; 31import com.android.deskclock.timer.TimerObj; 32 33public class AlarmInitReceiver extends BroadcastReceiver { 34 35 // A flag that indicates that switching the volume button default was done 36 private static final String PREF_VOLUME_DEF_DONE = "vol_def_done"; 37 38 /** 39 * Sets alarm on ACTION_BOOT_COMPLETED. Resets alarm on 40 * TIME_SET, TIMEZONE_CHANGED 41 */ 42 @Override 43 public void onReceive(final Context context, Intent intent) { 44 final String action = intent.getAction(); 45 Log.v("AlarmInitReceiver " + action); 46 47 final PendingResult result = goAsync(); 48 final WakeLock wl = AlarmAlertWakeLock.createPartialWakeLock(context); 49 wl.acquire(); 50 AsyncHandler.post(new Runnable() { 51 @Override public void run() { 52 // Remove the snooze alarm after a boot. 53 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { 54 // Clear stopwatch and timers data 55 SharedPreferences prefs = 56 PreferenceManager.getDefaultSharedPreferences(context); 57 Log.v("AlarmInitReceiver - Reset timers and clear stopwatch data"); 58 TimerObj.resetTimersInSharedPrefs(prefs); 59 Utils.clearSwSharedPref(prefs); 60 61 if (!prefs.getBoolean(PREF_VOLUME_DEF_DONE, false)) { 62 // Fix the default 63 Log.v("AlarmInitReceiver - resetting volume button default"); 64 switchVolumeButtonDefault(prefs); 65 } 66 } 67 68 // Register all instances after major time changes or phone restarts 69 ContentResolver contentResolver = context.getContentResolver(); 70 for (AlarmInstance instance : AlarmInstance.getInstances(contentResolver, null)) { 71 AlarmStateManager.registerInstance(context, instance, false); 72 } 73 AlarmStateManager.updateNextAlarm(context); 74 75 result.finish(); 76 Log.v("AlarmInitReceiver finished"); 77 wl.release(); 78 } 79 }); 80 } 81 82 private void switchVolumeButtonDefault(SharedPreferences prefs) { 83 SharedPreferences.Editor editor = prefs.edit(); 84 85 editor.putString(SettingsActivity.KEY_VOLUME_BEHAVIOR, 86 SettingsActivity.DEFAULT_VOLUME_BEHAVIOR); 87 88 // Make sure we do it only once 89 editor.putBoolean(PREF_VOLUME_DEF_DONE, true); 90 editor.apply(); 91 } 92} 93