ZenModeSettings.java revision 6d26d10fb3e5e2e491d1da0881eda192d30eec04
172438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock/*
272438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock * Copyright (C) 2014 The Android Open Source Project
372438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock *
472438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock * Licensed under the Apache License, Version 2.0 (the "License");
572438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock * you may not use this file except in compliance with the License.
672438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock * You may obtain a copy of the License at
772438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock *
872438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock *      http://www.apache.org/licenses/LICENSE-2.0
972438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock *
1072438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock * Unless required by applicable law or agreed to in writing, software
1172438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock * distributed under the License is distributed on an "AS IS" BASIS,
1272438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1372438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock * See the License for the specific language governing permissions and
1472438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock * limitations under the License.
1572438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock */
1672438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
174a35051565b0ce6d29313c3e4ffe19e1dde78db0John Spurlockpackage com.android.settings.notification;
1872438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
197f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlockimport android.app.ActionBar;
207f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlockimport android.app.Activity;
21cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport android.app.Dialog;
22cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport android.app.DialogFragment;
23cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport android.app.FragmentManager;
24cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport android.app.INotificationManager;
25cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport android.app.TimePickerDialog;
2672438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlockimport android.content.Context;
27019b87f17a51fef7fd4b6696dbea5c50fe8b6d07Fabrice Di Meglioimport android.content.res.Resources;
284a35051565b0ce6d29313c3e4ffe19e1dde78db0John Spurlockimport android.database.ContentObserver;
2972438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlockimport android.graphics.Typeface;
307f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlockimport android.net.Uri;
317f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlockimport android.os.AsyncTask;
3272438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlockimport android.os.Bundle;
337f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlockimport android.os.Handler;
34cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport android.os.ServiceManager;
35cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport android.preference.Preference;
36cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport android.preference.Preference.OnPreferenceChangeListener;
37cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport android.preference.PreferenceCategory;
38cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport android.preference.PreferenceScreen;
39cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport android.preference.SwitchPreference;
407f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlockimport android.provider.Settings.Global;
41cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport android.service.notification.ZenModeConfig;
42cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport android.text.format.DateFormat;
4372438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlockimport android.util.Log;
4472438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlockimport android.view.Gravity;
453726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlockimport android.view.Menu;
463726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlockimport android.view.MenuInflater;
473726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlockimport android.view.MenuItem;
4872438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlockimport android.view.View;
4972438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlockimport android.view.ViewGroup;
5072438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlockimport android.widget.CompoundButton;
5172438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlockimport android.widget.CompoundButton.OnCheckedChangeListener;
527f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlockimport android.widget.Switch;
5372438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlockimport android.widget.TextView;
54cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport android.widget.TimePicker;
554a35051565b0ce6d29313c3e4ffe19e1dde78db0John Spurlock
564a35051565b0ce6d29313c3e4ffe19e1dde78db0John Spurlockimport com.android.settings.R;
574a35051565b0ce6d29313c3e4ffe19e1dde78db0John Spurlockimport com.android.settings.SettingsPreferenceFragment;
58cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport com.android.settings.search.BaseSearchIndexProvider;
59019b87f17a51fef7fd4b6696dbea5c50fe8b6d07Fabrice Di Meglioimport com.android.settings.search.Indexable;
60019b87f17a51fef7fd4b6696dbea5c50fe8b6d07Fabrice Di Meglioimport com.android.settings.search.SearchIndexableRaw;
6172438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
62019b87f17a51fef7fd4b6696dbea5c50fe8b6d07Fabrice Di Meglioimport java.util.ArrayList;
63cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport java.util.Calendar;
64019b87f17a51fef7fd4b6696dbea5c50fe8b6d07Fabrice Di Meglioimport java.util.List;
65cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlockimport java.util.Objects;
66019b87f17a51fef7fd4b6696dbea5c50fe8b6d07Fabrice Di Meglio
67019b87f17a51fef7fd4b6696dbea5c50fe8b6d07Fabrice Di Megliopublic class ZenModeSettings extends SettingsPreferenceFragment implements Indexable {
6872438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock    private static final String TAG = "ZenModeSettings";
69cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private static final boolean DEBUG = true;
70cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
71cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private static final String KEY_GENERAL = "general";
72cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private static final String KEY_CALLS = "phone_calls";
73cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private static final String KEY_MESSAGES = "messages";
74cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
75cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private static final String KEY_AUTOMATIC = "automatic";
76cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private static final String KEY_WHEN = "when";
7772438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
787f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock    private final Handler mHandler = new Handler();
797f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock    private final SettingsObserver mSettingsObserver = new SettingsObserver();
807f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock
817f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock    private Switch mSwitch;
823726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock    private Activity mActivity;
83cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private Context mContext;
843726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock    private MenuItem mSearch;
85cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private ZenModeConfig mConfig;
86cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private boolean mDisableListeners;
87cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private SwitchPreference mCalls;
88cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private SwitchPreference mMessages;
89cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private DropDownPreference mStarred;
90cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private DropDownPreference mWhen;
91cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private TimePickerPreference mStart;
92cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private TimePickerPreference mEnd;
937f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock
947f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock    @Override
957f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock    public void onCreate(Bundle icicle) {
967f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock        super.onCreate(icicle);
97cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mContext = mActivity = getActivity();
98ede14355180b84c634cade7e3f02d67be64cbff4Fabrice Di Meglio        mSwitch = new Switch(mActivity.getActionBar().getThemedContext());
99cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        final Resources res = mContext.getResources();
100cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        final int p = res.getDimensionPixelSize(R.dimen.content_margin_left);
1013726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock        mSwitch.setPadding(0, 0, p, 0);
1023726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock        setHasOptionsMenu(true);
103cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
104cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        addPreferencesFromResource(R.xml.zen_mode_settings);
105cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        final PreferenceScreen root = getPreferenceScreen();
106cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
107cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mConfig = getZenModeConfig();
108cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        if (DEBUG) Log.d(TAG, "Loaded mConfig=" + mConfig);
109cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
110cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        final PreferenceCategory general = (PreferenceCategory) root.findPreference(KEY_GENERAL);
111cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
112cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mCalls = (SwitchPreference) root.findPreference(KEY_CALLS);
113cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mCalls.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
114cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            @Override
115cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            public boolean onPreferenceChange(Preference preference, Object newValue) {
116cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (mDisableListeners) return true;
117cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                final boolean val = (Boolean) newValue;
118cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (val == mConfig.allowCalls) return true;
119cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (DEBUG) Log.d(TAG, "onPrefChange allowCalls=" + val);
120cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                final ZenModeConfig newConfig = mConfig.copy();
121cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                newConfig.allowCalls = val;
122cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                return setZenModeConfig(newConfig);
123cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            }
124cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        });
125cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
126cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mMessages = (SwitchPreference) root.findPreference(KEY_MESSAGES);
127cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mMessages.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
128cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            @Override
129cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            public boolean onPreferenceChange(Preference preference, Object newValue) {
130cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (mDisableListeners) return true;
131cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                final boolean val = (Boolean) newValue;
132cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (val == mConfig.allowMessages) return true;
133cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (DEBUG) Log.d(TAG, "onPrefChange allowMessages=" + val);
134cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                final ZenModeConfig newConfig = mConfig.copy();
135cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                newConfig.allowMessages = val;
136cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                return setZenModeConfig(newConfig);
137cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            }
138cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        });
139cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
140cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mStarred = new DropDownPreference(mContext);
141cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mStarred.setEnabled(false);
142cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mStarred.setTitle(R.string.zen_mode_from);
1436d26d10fb3e5e2e491d1da0881eda192d30eec04John Spurlock        mStarred.setDropDownWidth(R.dimen.zen_mode_dropdown_width);
144cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mStarred.addItem(R.string.zen_mode_from_anyone);
145cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mStarred.addItem(R.string.zen_mode_from_starred);
146cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mStarred.addItem(R.string.zen_mode_from_contacts);
147cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        general.addPreference(mStarred);
148cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
149cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        final Preference alarmInfo = new Preference(mContext) {
150cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            @Override
151cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            public View getView(View convertView, ViewGroup parent) {
152cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                final TextView tv = new TextView(mContext);
153cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                tv.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.ITALIC));
154cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                tv.setPadding(p, p, p, p);
155cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                tv.setText(R.string.zen_mode_alarm_info);
156cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                return tv;
157cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            }
158cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        };
159cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        alarmInfo.setPersistent(false);
160cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        alarmInfo.setSelectable(false);
161cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        general.addPreference(alarmInfo);
162cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
163cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        final PreferenceCategory auto = (PreferenceCategory) root.findPreference(KEY_AUTOMATIC);
164cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
165cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mWhen = new DropDownPreference(mContext);
166cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mWhen.setKey(KEY_WHEN);
167cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mWhen.setTitle(R.string.zen_mode_when);
1686d26d10fb3e5e2e491d1da0881eda192d30eec04John Spurlock        mWhen.setDropDownWidth(R.dimen.zen_mode_dropdown_width);
169cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mWhen.addItem(R.string.zen_mode_when_never);
170cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mWhen.addItem(R.string.zen_mode_when_every_night);
171cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mWhen.addItem(R.string.zen_mode_when_weeknights);
172cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mWhen.setCallback(new DropDownPreference.Callback() {
173cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            @Override
174cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            public boolean onItemSelected(int pos) {
175cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (mDisableListeners) return true;
176cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                final String mode = pos == 1 ? ZenModeConfig.SLEEP_MODE_NIGHTS :
177cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                    pos == 2 ? ZenModeConfig.SLEEP_MODE_WEEKNIGHTS : null;
178cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (Objects.equals(mode, mConfig.sleepMode)) return true;
179cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (DEBUG) Log.d(TAG, "onPrefChange sleepMode=" + mode);
180cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                final ZenModeConfig newConfig = mConfig.copy();
181cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                newConfig.sleepMode = mode;
182cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                return setZenModeConfig(newConfig);
183cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            }
184cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        });
185cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        auto.addPreference(mWhen);
186cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
187cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        final FragmentManager mgr = getFragmentManager();
188cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
189cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mStart = new TimePickerPreference(mContext, mgr);
190cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mStart.setTitle(R.string.zen_mode_start_time);
191cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mStart.setCallback(new TimePickerPreference.Callback() {
192cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            @Override
193cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            public boolean onSetTime(int hour, int minute) {
194cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (mDisableListeners) return true;
195cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (!ZenModeConfig.isValidHour(hour)) return false;
196cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (!ZenModeConfig.isValidMinute(minute)) return false;
197cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (hour == mConfig.sleepStartHour && minute == mConfig.sleepStartMinute) {
198cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                    return true;
199cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                }
200cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (DEBUG) Log.d(TAG, "onPrefChange sleepStart h=" + hour + " m=" + minute);
201cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                final ZenModeConfig newConfig = mConfig.copy();
202cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                newConfig.sleepStartHour = hour;
203cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                newConfig.sleepStartMinute = minute;
204cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                return setZenModeConfig(newConfig);
205cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            }
206cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        });
207cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        auto.addPreference(mStart);
208cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
209cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mEnd = new TimePickerPreference(mContext, mgr);
210cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mEnd.setTitle(R.string.zen_mode_end_time);
211cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mEnd.setSummaryFormat(R.string.zen_mode_end_time_summary_format);
212cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mEnd.setCallback(new TimePickerPreference.Callback() {
213cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            @Override
214cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            public boolean onSetTime(int hour, int minute) {
215cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (mDisableListeners) return true;
216cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (!ZenModeConfig.isValidHour(hour)) return false;
217cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (!ZenModeConfig.isValidMinute(minute)) return false;
218cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (hour == mConfig.sleepEndHour && minute == mConfig.sleepEndMinute) {
219cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                    return true;
220cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                }
221cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (DEBUG) Log.d(TAG, "onPrefChange sleepEnd h=" + hour + " m=" + minute);
222cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                final ZenModeConfig newConfig = mConfig.copy();
223cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                newConfig.sleepEndHour = hour;
224cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                newConfig.sleepEndMinute = minute;
225cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                return setZenModeConfig(newConfig);
226cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            }
227cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        });
228cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        auto.addPreference(mEnd);
229cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
230cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mStart.setDependency(mWhen.getKey());
231cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mEnd.setDependency(mWhen.getKey());
232cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
233cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        updateControls();
234cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    }
235cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
236cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private void updateControls() {
237cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mDisableListeners = true;
238cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mCalls.setChecked(mConfig.allowCalls);
239cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mMessages.setChecked(mConfig.allowMessages);
240cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mStarred.setSelectedItem(0);
241cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mWhen.setSelectedItem(
242cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                ZenModeConfig.SLEEP_MODE_NIGHTS.equals(mConfig.sleepMode) ? 1 :
243cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                ZenModeConfig.SLEEP_MODE_WEEKNIGHTS.equals(mConfig.sleepMode) ? 2 : 0);
244cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mStart.setTime(mConfig.sleepStartHour, mConfig.sleepStartMinute);
245cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mEnd.setTime(mConfig.sleepEndHour, mConfig.sleepEndMinute);
246cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        mDisableListeners = false;
2477f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock    }
2487f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock
2497f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock    @Override
2503726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
2513726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock        super.onCreateOptionsMenu(menu, inflater);
2523726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock        mSearch = menu.findItem(R.id.search);
2533726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock        if (mSearch != null) mSearch.setVisible(false);
2547f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock    }
2557f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock
2567f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock    @Override
2577f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock    public void onResume() {
2587f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock        super.onResume();
259cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        updateZenMode();
2607f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock        mSettingsObserver.register();
2613726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock        mActivity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
2623726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock                ActionBar.DISPLAY_SHOW_CUSTOM);
2633726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock        mActivity.getActionBar().setCustomView(mSwitch, new ActionBar.LayoutParams(
2643726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock                ActionBar.LayoutParams.WRAP_CONTENT,
2653726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock                ActionBar.LayoutParams.WRAP_CONTENT,
2663726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock                Gravity.CENTER_VERTICAL | Gravity.END));
2673726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock        if (mSearch != null) mSearch.setVisible(false);
2683726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock    }
2693726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock
2703726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock    @Override
2713726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock    public void onPause() {
2723726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock        super.onPause();
2733726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock        mSettingsObserver.unregister();
2743726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock        mActivity.getActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_CUSTOM);
2753726525d50d480a3d954c103a844f0c4b3c7d3e0John Spurlock        if (mSearch != null) mSearch.setVisible(true);
2767f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock    }
2777f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock
278cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private void updateZenMode() {
2797f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock        mSwitch.setOnCheckedChangeListener(null);
2807f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock        final boolean zenMode = Global.getInt(getContentResolver(),
2817f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock                Global.ZEN_MODE, Global.ZEN_MODE_OFF) != Global.ZEN_MODE_OFF;
2827f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock        mSwitch.setChecked(zenMode);
2837f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock        mSwitch.setOnCheckedChangeListener(mSwitchListener);
2847f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock    }
28572438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
286cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private void updateZenModeConfig() {
287cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        final ZenModeConfig config = getZenModeConfig();
288cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        if (Objects.equals(config, mConfig)) return;
289cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        if (DEBUG) Log.d(TAG, "updateZenModeConfig");
290cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        updateControls();
29172438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock    }
29272438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
293cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private ZenModeConfig getZenModeConfig() {
294cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        final INotificationManager nm = INotificationManager.Stub.asInterface(
295cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                ServiceManager.getService(Context.NOTIFICATION_SERVICE));
296cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        try {
297cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            return nm.getZenModeConfig();
298cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        } catch (Exception e) {
299cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock           Log.w(TAG, "Error calling NoMan", e);
300cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock           return new ZenModeConfig();
301cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        }
302cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    }
303cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
304cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private boolean setZenModeConfig(ZenModeConfig config) {
305cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        final INotificationManager nm = INotificationManager.Stub.asInterface(
306cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                ServiceManager.getService(Context.NOTIFICATION_SERVICE));
307cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        try {
308cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            final boolean success = nm.setZenModeConfig(config);
309cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            if (success) {
310cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                mConfig = config;
311cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (DEBUG) Log.d(TAG, "Saved mConfig=" + mConfig);
312cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            }
313cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            return success;
314cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        } catch (Exception e) {
315cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock           Log.w(TAG, "Error calling NoMan", e);
316cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock           return false;
317cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        }
31872438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock    }
31972438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
3207f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock    private final OnCheckedChangeListener mSwitchListener = new OnCheckedChangeListener() {
3217f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock        @Override
3227f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock        public void onCheckedChanged(CompoundButton buttonView, final boolean isChecked) {
3237f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock            AsyncTask.execute(new Runnable() {
3247f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock                @Override
3257f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock                public void run() {
3267f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock                    final int v = isChecked ? Global.ZEN_MODE_ON : Global.ZEN_MODE_OFF;
3277f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock                    Global.putInt(getContentResolver(), Global.ZEN_MODE, v);
3287f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock                }
3297f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock            });
3307f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock        }
3317f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock    };
3327f502bf824955b2b3a43f8f7f32c00cf00f736feJohn Spurlock
333cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    // Enable indexing of searchable data
334cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
335cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        new BaseSearchIndexProvider() {
336cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            @Override
337cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
338cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
339cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                add(result, context, R.string.zen_mode_settings_title);
340cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                add(result, context, R.string.zen_mode_general_category);
341cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                add(result, context, R.string.zen_mode_phone_calls);
342cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                add(result, context, R.string.zen_mode_messages);
343cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                add(result, context, R.string.zen_mode_automatic_category);
344cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                add(result, context, R.string.zen_mode_when);
345cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                add(result, context, R.string.zen_mode_start_time);
346cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                add(result, context, R.string.zen_mode_end_time);
347cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                return result;
348cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            }
34972438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
350cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            private void add(List<SearchIndexableRaw> result, Context context, int title) {
351cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                final Resources res = context.getResources();
352cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                final SearchIndexableRaw data = new SearchIndexableRaw(context);
353cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                data.title = res.getString(title);
354cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                data.screenTitle = res.getString(R.string.zen_mode_settings_title);
355cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                result.add(data);
356cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            }
357cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        };
35872438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
359cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private final class SettingsObserver extends ContentObserver {
360cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        private final Uri ZEN_MODE_URI = Global.getUriFor(Global.ZEN_MODE);
361cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        private final Uri ZEN_MODE_CONFIG_ETAG_URI = Global.getUriFor(Global.ZEN_MODE_CONFIG_ETAG);
36272438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
363cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        public SettingsObserver() {
364cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            super(mHandler);
36572438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock        }
36672438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
367cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        public void register() {
368cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            getContentResolver().registerContentObserver(ZEN_MODE_URI, false, this);
369cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            getContentResolver().registerContentObserver(ZEN_MODE_CONFIG_ETAG_URI, false, this);
37072438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock        }
37172438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
372cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        public void unregister() {
373cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            getContentResolver().unregisterContentObserver(this);
37472438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock        }
37572438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
376cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        @Override
377cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        public void onChange(boolean selfChange, Uri uri) {
378cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            super.onChange(selfChange, uri);
379cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            if (ZEN_MODE_URI.equals(uri)) {
380cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                updateZenMode();
381cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            }
382cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            if (ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) {
383cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                updateZenModeConfig();
384cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            }
38572438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock        }
386cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    }
38772438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
388cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    private static class TimePickerPreference extends Preference {
389cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        private final Context mContext;
39072438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
391cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        private int mSummaryFormat;
392cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        private int mHourOfDay;
393cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        private int mMinute;
394cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        private Callback mCallback;
395cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock
396cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        public TimePickerPreference(Context context, final FragmentManager mgr) {
397cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            super(context);
398cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            mContext = context;
399cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            setPersistent(false);
400cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            setOnPreferenceClickListener(new OnPreferenceClickListener(){
401cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                @Override
402cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                public boolean onPreferenceClick(Preference preference) {
403cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                    final TimePickerFragment frag = new TimePickerFragment();
404cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                    frag.pref = TimePickerPreference.this;
405cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                    frag.show(mgr, TimePickerPreference.class.getName());
406cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                    return true;
407cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                }
408cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            });
40972438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock        }
41072438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
411cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        public void setCallback(Callback callback) {
412cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            mCallback = callback;
41372438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock        }
41472438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
415cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        public void setSummaryFormat(int resId) {
416cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            mSummaryFormat = resId;
417cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            updateSummary();
418cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        }
41972438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
420cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        public void setTime(int hourOfDay, int minute) {
421cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            if (mCallback != null && !mCallback.onSetTime(hourOfDay, minute)) return;
422cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            mHourOfDay = hourOfDay;
423cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            mMinute = minute;
424cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            updateSummary();
425cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        }
42672438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock
427cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        private void updateSummary() {
428cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            final Calendar c = Calendar.getInstance();
429cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            c.set(Calendar.HOUR_OF_DAY, mHourOfDay);
430cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            c.set(Calendar.MINUTE, mMinute);
431cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            String time = DateFormat.getTimeFormat(mContext).format(c.getTime());
432cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            if (mSummaryFormat != 0) {
433cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                time = mContext.getResources().getString(mSummaryFormat, time);
43472438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock            }
435cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            setSummary(time);
43672438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock        }
437019b87f17a51fef7fd4b6696dbea5c50fe8b6d07Fabrice Di Meglio
438cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        public static class TimePickerFragment extends DialogFragment implements
439cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                TimePickerDialog.OnTimeSetListener {
440cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            public TimePickerPreference pref;
441019b87f17a51fef7fd4b6696dbea5c50fe8b6d07Fabrice Di Meglio
442cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            @Override
443cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            public Dialog onCreateDialog(Bundle savedInstanceState) {
444cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                final boolean usePref = pref != null && pref.mHourOfDay >= 0 && pref.mMinute >= 0;
445cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                final Calendar c = Calendar.getInstance();
446cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                final int hour = usePref ? pref.mHourOfDay : c.get(Calendar.HOUR_OF_DAY);
447cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                final int minute = usePref ? pref.mMinute : c.get(Calendar.MINUTE);
448cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                return new TimePickerDialog(getActivity(), this, hour, minute,
449cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                        DateFormat.is24HourFormat(getActivity()));
450cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            }
451019b87f17a51fef7fd4b6696dbea5c50fe8b6d07Fabrice Di Meglio
452cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
453cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                if (pref != null) {
454cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                    pref.setTime(hourOfDay, minute);
455cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock                }
456cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            }
457cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        }
458019b87f17a51fef7fd4b6696dbea5c50fe8b6d07Fabrice Di Meglio
459cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        public interface Callback {
460cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock            boolean onSetTime(int hour, int minute);
461cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock        }
462cc819a67f49ab4bfb42e51454bec54c03dd4f595John Spurlock    }
46372438063453d2141218cf7fa4d8c1da6de6cbf4cJohn Spurlock}
464