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
51        // We need to increment the global id out of the async task to prevent
52        // race conditions
53        AlarmStateManager.updateGloablIntentId(context);
54        AsyncHandler.post(new Runnable() {
55            @Override public void run() {
56                // Remove the snooze alarm after a boot.
57                if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
58                    // Clear stopwatch and timers data
59                    SharedPreferences prefs =
60                            PreferenceManager.getDefaultSharedPreferences(context);
61                    Log.v("AlarmInitReceiver - Reset timers and clear stopwatch data");
62                    TimerObj.resetTimersInSharedPrefs(prefs);
63                    Utils.clearSwSharedPref(prefs);
64
65                    if (!prefs.getBoolean(PREF_VOLUME_DEF_DONE, false)) {
66                        // Fix the default
67                        Log.v("AlarmInitReceiver - resetting volume button default");
68                        switchVolumeButtonDefault(prefs);
69                    }
70                }
71
72                // Update all the alarm instances on time change event
73                AlarmStateManager.fixAlarmInstances(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