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.timer.TimerObj; 27 28 public class AlarmInitReceiver extends BroadcastReceiver { 29 30 // A flag that indicates that switching the volume button default was done 31 private static final String PREF_VOLUME_DEF_DONE = "vol_def_done"; 32 33 /** 34 * Sets alarm on ACTION_BOOT_COMPLETED. Resets alarm on 35 * TIME_SET, TIMEZONE_CHANGED 36 */ 37 @Override 38 public void onReceive(final Context context, Intent intent) { 39 final String action = intent.getAction(); 40 if (Log.LOGV) Log.v("AlarmInitReceiver " + action); 41 42 final PendingResult result = goAsync(); 43 final WakeLock wl = AlarmAlertWakeLock.createPartialWakeLock(context); 44 wl.acquire(); 45 AsyncHandler.post(new Runnable() { 46 @Override public void run() { 47 // Remove the snooze alarm after a boot. 48 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { 49 Alarms.saveSnoozeAlert(context, Alarms.INVALID_ALARM_ID, -1); 50 Alarms.disableExpiredAlarms(context); 51 52 // Clear stopwatch and timers data 53 SharedPreferences prefs = 54 PreferenceManager.getDefaultSharedPreferences(context); 55 Log.v("AlarmInitReceiver - Reset timers and clear stopwatch data"); 56 TimerObj.resetTimersInSharedPrefs(prefs); 57 Utils.clearSwSharedPref(prefs); 58 59 if (!prefs.getBoolean(PREF_VOLUME_DEF_DONE, false)) { 60 // Fix the default 61 Log.v("AlarmInitReceiver - resetting volume button default"); 62 switchVolumeButtonDefault(prefs); 63 } 64 } 65 Alarms.setNextAlert(context); 66 result.finish(); 67 Log.v("AlarmInitReceiver finished"); 68 wl.release(); 69 } 70 }); 71 } 72 73 private void switchVolumeButtonDefault(SharedPreferences prefs) { 74 SharedPreferences.Editor editor = prefs.edit(); 75 76 editor.putString(SettingsActivity.KEY_VOLUME_BEHAVIOR, 77 SettingsActivity.DEFAULT_VOLUME_BEHAVIOR); 78 79 // Make sure we do it only once 80 editor.putBoolean(PREF_VOLUME_DEF_DONE, true); 81 editor.apply(); 82 } 83 } 84