1 /*
2  * Copyright (C) 2015 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.settings.notification;
18 
19 import android.app.AlertDialog;
20 import android.app.AutomaticZenRule;
21 import android.app.NotificationManager;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.DialogInterface.OnClickListener;
25 import android.content.Intent;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.service.notification.ConditionProviderService;
29 import android.support.v7.preference.DropDownPreference;
30 import android.support.v7.preference.Preference;
31 import android.support.v7.preference.Preference.OnPreferenceChangeListener;
32 import android.support.v7.preference.Preference.OnPreferenceClickListener;
33 import android.support.v7.preference.PreferenceScreen;
34 import android.util.Log;
35 import android.view.Menu;
36 import android.view.MenuInflater;
37 import android.view.MenuItem;
38 import android.view.View;
39 import android.widget.Switch;
40 import android.widget.Toast;
41 
42 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
43 import com.android.settings.R;
44 import com.android.settings.SettingsActivity;
45 import com.android.settings.widget.SwitchBar;
46 
47 public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
48         implements SwitchBar.OnSwitchChangeListener {
49     protected static final String TAG = ZenModeSettingsBase.TAG;
50     protected static final boolean DEBUG = ZenModeSettingsBase.DEBUG;
51 
52     private static final String KEY_RULE_NAME = "rule_name";
53     private static final String KEY_ZEN_MODE = "zen_mode";
54 
55     protected Context mContext;
56     protected boolean mDisableListeners;
57     protected AutomaticZenRule mRule;
58     protected String mId;
59 
60     private boolean mDeleting;
61     private Preference mRuleName;
62     private SwitchBar mSwitchBar;
63     private DropDownPreference mZenMode;
64     private Toast mEnabledToast;
65 
onCreateInternal()66     abstract protected void onCreateInternal();
setRule(AutomaticZenRule rule)67     abstract protected boolean setRule(AutomaticZenRule rule);
getZenModeDependency()68     abstract protected String getZenModeDependency();
updateControlsInternal()69     abstract protected void updateControlsInternal();
getEnabledToastText()70     abstract protected int getEnabledToastText();
71 
72     @Override
onCreate(Bundle icicle)73     public void onCreate(Bundle icicle) {
74         super.onCreate(icicle);
75 
76         mContext = getActivity();
77 
78         final Intent intent = getActivity().getIntent();
79         if (DEBUG) Log.d(TAG, "onCreate getIntent()=" + intent);
80         if (intent == null) {
81             Log.w(TAG, "No intent");
82             toastAndFinish();
83             return;
84         }
85 
86         mId = intent.getStringExtra(ConditionProviderService.EXTRA_RULE_ID);
87         if (DEBUG) Log.d(TAG, "mId=" + mId);
88         if (refreshRuleOrFinish()) {
89             return;
90         }
91 
92         setHasOptionsMenu(true);
93 
94         onCreateInternal();
95 
96         final PreferenceScreen root = getPreferenceScreen();
97         mRuleName = root.findPreference(KEY_RULE_NAME);
98         mRuleName.setOnPreferenceClickListener(new OnPreferenceClickListener() {
99             @Override
100             public boolean onPreferenceClick(Preference preference) {
101                 showRuleNameDialog();
102                 return true;
103             }
104         });
105 
106         mZenMode = (DropDownPreference) root.findPreference(KEY_ZEN_MODE);
107         mZenMode.setEntries(new CharSequence[] {
108                 getString(R.string.zen_mode_option_important_interruptions),
109                 getString(R.string.zen_mode_option_alarms),
110                 getString(R.string.zen_mode_option_no_interruptions),
111         });
112         mZenMode.setEntryValues(new CharSequence[] {
113                 Integer.toString(NotificationManager.INTERRUPTION_FILTER_PRIORITY),
114                 Integer.toString(NotificationManager.INTERRUPTION_FILTER_ALARMS),
115                 Integer.toString(NotificationManager.INTERRUPTION_FILTER_NONE),
116         });
117         mZenMode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
118             @Override
119             public boolean onPreferenceChange(Preference preference, Object newValue) {
120                 if (mDisableListeners) return false;
121                 final int zenMode = Integer.parseInt((String) newValue);
122                 if (zenMode == mRule.getInterruptionFilter()) return false;
123                 if (DEBUG) Log.d(TAG, "onPrefChange zenMode=" + zenMode);
124                 mRule.setInterruptionFilter(zenMode);
125                 setZenRule(mId, mRule);
126                 return true;
127             }
128         });
129         mZenMode.setOrder(10);  // sort at the bottom of the category
130         mZenMode.setDependency(getZenModeDependency());
131     }
132 
133     @Override
onResume()134     public void onResume() {
135         super.onResume();
136         if (isUiRestricted()) {
137             return;
138         }
139         updateControls();
140     }
141 
142     @Override
onActivityCreated(Bundle savedInstanceState)143     public void onActivityCreated(Bundle savedInstanceState) {
144         super.onActivityCreated(savedInstanceState);
145 
146         final SettingsActivity activity = (SettingsActivity) getActivity();
147         mSwitchBar = activity.getSwitchBar();
148         mSwitchBar.addOnSwitchChangeListener(this);
149         mSwitchBar.show();
150     }
151 
152     @Override
onDestroyView()153     public void onDestroyView() {
154         super.onDestroyView();
155         mSwitchBar.removeOnSwitchChangeListener(this);
156         mSwitchBar.hide();
157     }
158 
159     @Override
onSwitchChanged(Switch switchView, boolean isChecked)160     public void onSwitchChanged(Switch switchView, boolean isChecked) {
161         if (DEBUG) Log.d(TAG, "onSwitchChanged " + isChecked);
162         if (mDisableListeners) return;
163         final boolean enabled = isChecked;
164         if (enabled == mRule.isEnabled()) return;
165         mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_ZEN_ENABLE_RULE, enabled);
166         if (DEBUG) Log.d(TAG, "onSwitchChanged enabled=" + enabled);
167         mRule.setEnabled(enabled);
168         setZenRule(mId, mRule);
169         if (enabled) {
170             final int toastText = getEnabledToastText();
171             if (toastText != 0) {
172                 mEnabledToast = Toast.makeText(mContext, toastText, Toast.LENGTH_SHORT);
173                 mEnabledToast.show();
174             }
175         } else {
176             if (mEnabledToast != null) {
177                 mEnabledToast.cancel();
178             }
179         }
180     }
181 
updateRule(Uri newConditionId)182     protected void updateRule(Uri newConditionId) {
183         mRule.setConditionId(newConditionId);
184         setZenRule(mId, mRule);
185     }
186 
187     @Override
onZenModeChanged()188     protected void onZenModeChanged() {
189         // noop
190     }
191 
192     @Override
onZenModeConfigChanged()193     protected void onZenModeConfigChanged() {
194         if (!refreshRuleOrFinish()) {
195             updateControls();
196         }
197     }
198 
199     @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)200     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
201         if (DEBUG) Log.d(TAG, "onCreateOptionsMenu");
202         inflater.inflate(R.menu.zen_mode_rule, menu);
203     }
204 
205     @Override
onOptionsItemSelected(MenuItem item)206     public boolean onOptionsItemSelected(MenuItem item) {
207         if (DEBUG) Log.d(TAG, "onOptionsItemSelected " + item.getItemId());
208         if (item.getItemId() == R.id.delete) {
209             mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_ZEN_DELETE_RULE);
210             showDeleteRuleDialog();
211             return true;
212         }
213         return super.onOptionsItemSelected(item);
214     }
215 
showRuleNameDialog()216     private void showRuleNameDialog() {
217         new ZenRuleNameDialog(mContext, mRule.getName()) {
218             @Override
219             public void onOk(String ruleName) {
220                 mRule.setName(ruleName);
221                 setZenRule(mId, mRule);
222             }
223         }.show();
224     }
225 
refreshRuleOrFinish()226     private boolean refreshRuleOrFinish() {
227         mRule = getZenRule();
228         if (DEBUG) Log.d(TAG, "mRule=" + mRule);
229         if (!setRule(mRule)) {
230             toastAndFinish();
231             return true;
232         }
233         return false;
234     }
235 
showDeleteRuleDialog()236     private void showDeleteRuleDialog() {
237         final AlertDialog dialog = new AlertDialog.Builder(mContext)
238                 .setMessage(getString(R.string.zen_mode_delete_rule_confirmation, mRule.getName()))
239                 .setNegativeButton(R.string.cancel, null)
240                 .setPositiveButton(R.string.zen_mode_delete_rule_button, new OnClickListener() {
241                     @Override
242                     public void onClick(DialogInterface dialog, int which) {
243                         mMetricsFeatureProvider.action(mContext,
244                                 MetricsEvent.ACTION_ZEN_DELETE_RULE_OK);
245                         mDeleting = true;
246                         removeZenRule(mId);
247                     }
248                 })
249                 .show();
250         final View messageView = dialog.findViewById(android.R.id.message);
251         if (messageView != null) {
252             messageView.setTextDirection(View.TEXT_DIRECTION_LOCALE);
253         }
254     }
255 
toastAndFinish()256     private void toastAndFinish() {
257         if (!mDeleting) {
258             Toast.makeText(mContext, R.string.zen_mode_rule_not_found_text, Toast.LENGTH_SHORT)
259                     .show();
260         }
261         getActivity().finish();
262     }
263 
updateRuleName()264     private void updateRuleName() {
265         getActivity().setTitle(mRule.getName());
266         mRuleName.setSummary(mRule.getName());
267     }
268 
getZenRule()269     private AutomaticZenRule getZenRule() {
270         return NotificationManager.from(mContext).getAutomaticZenRule(mId);
271     }
272 
updateControls()273     private void updateControls() {
274         mDisableListeners = true;
275         updateRuleName();
276         updateControlsInternal();
277         mZenMode.setValue(Integer.toString(mRule.getInterruptionFilter()));
278         if (mSwitchBar != null) {
279             mSwitchBar.setChecked(mRule.isEnabled());
280         }
281         mDisableListeners = false;
282     }
283 
284 }
285