AlarmInitReceiver.java revision 2eb3e5467cb76617fd6da3ea4a3f222d537a6bd9
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.Context; 21import android.content.Intent; 22import android.content.SharedPreferences; 23import android.os.PowerManager.WakeLock; 24import android.preference.PreferenceManager; 25 26import com.android.deskclock.provider.Alarm; 27 28import com.android.deskclock.timer.TimerObj; 29 30public 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 * Sets alarm on ACTION_BOOT_COMPLETED. Resets alarm on 37 * TIME_SET, TIMEZONE_CHANGED 38 */ 39 @Override 40 public void onReceive(final Context context, Intent intent) { 41 final String action = intent.getAction(); 42 if (Log.LOGV) Log.v("AlarmInitReceiver " + action); 43 44 final PendingResult result = goAsync(); 45 final WakeLock wl = AlarmAlertWakeLock.createPartialWakeLock(context); 46 wl.acquire(); 47 AsyncHandler.post(new Runnable() { 48 @Override public void run() { 49 // Remove the snooze alarm after a boot. 50 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { 51 Alarms.saveSnoozeAlert(context, Alarm.INVALID_ID, -1); 52 Alarms.disableExpiredAlarms(context); 53 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 Alarms.setNextAlert(context); 68 result.finish(); 69 Log.v("AlarmInitReceiver finished"); 70 wl.release(); 71 } 72 }); 73 } 74 75 private void switchVolumeButtonDefault(SharedPreferences prefs) { 76 SharedPreferences.Editor editor = prefs.edit(); 77 78 editor.putString(SettingsActivity.KEY_VOLUME_BEHAVIOR, 79 SettingsActivity.DEFAULT_VOLUME_BEHAVIOR); 80 81 // Make sure we do it only once 82 editor.putBoolean(PREF_VOLUME_DEF_DONE, true); 83 editor.apply(); 84 } 85} 86