ZenModeRuleSettingsBase.java revision f7748e4ae15e49f9f4a9cc15a372e9a6d3c35c56
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 17package com.android.settings.notification; 18 19import android.app.AlertDialog; 20import android.app.AutomaticZenRule; 21import android.app.NotificationManager; 22import android.content.Context; 23import android.content.DialogInterface; 24import android.content.DialogInterface.OnClickListener; 25import android.content.Intent; 26import android.net.Uri; 27import android.os.Bundle; 28import android.preference.Preference; 29import android.preference.Preference.OnPreferenceChangeListener; 30import android.preference.Preference.OnPreferenceClickListener; 31import android.preference.PreferenceScreen; 32import android.service.notification.ConditionProviderService; 33import android.util.Log; 34import android.view.Menu; 35import android.view.MenuInflater; 36import android.view.MenuItem; 37import android.view.View; 38import android.widget.Switch; 39import android.widget.Toast; 40 41import com.android.internal.logging.MetricsLogger; 42import com.android.settings.DropDownPreference; 43import com.android.settings.R; 44import com.android.settings.SettingsActivity; 45import com.android.settings.widget.SwitchBar; 46 47public 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 mName; 59 60 private boolean mDeleting; 61 private Preference mRuleName; 62 private SwitchBar mSwitchBar; 63 private DropDownPreference mZenMode; 64 private Toast mEnabledToast; 65 66 abstract protected void onCreateInternal(); 67 abstract protected boolean setRule(AutomaticZenRule rule); 68 abstract protected String getZenModeDependency(); 69 abstract protected void updateControlsInternal(); 70 abstract protected int getEnabledToastText(); 71 72 @Override 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 mName = intent.getStringExtra(ConditionProviderService.EXTRA_RULE_NAME); 87 if (DEBUG) Log.d(TAG, "mName=" + mName); 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(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 134 public void onResume() { 135 super.onResume(); 136 updateControls(); 137 } 138 139 @Override 140 public void onActivityCreated(Bundle savedInstanceState) { 141 super.onActivityCreated(savedInstanceState); 142 143 final SettingsActivity activity = (SettingsActivity) getActivity(); 144 mSwitchBar = activity.getSwitchBar(); 145 mSwitchBar.addOnSwitchChangeListener(this); 146 mSwitchBar.show(); 147 } 148 149 @Override 150 public void onDestroyView() { 151 super.onDestroyView(); 152 mSwitchBar.removeOnSwitchChangeListener(this); 153 mSwitchBar.hide(); 154 } 155 156 @Override 157 public void onSwitchChanged(Switch switchView, boolean isChecked) { 158 if (DEBUG) Log.d(TAG, "onSwitchChanged " + isChecked); 159 if (mDisableListeners) return; 160 final boolean enabled = isChecked; 161 if (enabled == mRule.isEnabled()) return; 162 MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ENABLE_RULE, enabled); 163 if (DEBUG) Log.d(TAG, "onSwitchChanged enabled=" + enabled); 164 mRule.setEnabled(enabled); 165 setZenRule(mRule); 166 if (enabled) { 167 final int toastText = getEnabledToastText(); 168 if (toastText != 0) { 169 mEnabledToast = Toast.makeText(mContext, toastText, Toast.LENGTH_SHORT); 170 mEnabledToast.show(); 171 } 172 } else { 173 if (mEnabledToast != null) { 174 mEnabledToast.cancel(); 175 } 176 } 177 } 178 179 protected void updateRule(Uri newConditionId) { 180 mRule.setConditionId(newConditionId); 181 setZenRule(mRule); 182 } 183 184 @Override 185 protected void onZenModeChanged() { 186 // noop 187 } 188 189 @Override 190 protected void onZenModeConfigChanged() { 191 if (!refreshRuleOrFinish()) { 192 updateControls(); 193 } 194 } 195 196 @Override 197 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 198 if (DEBUG) Log.d(TAG, "onCreateOptionsMenu"); 199 inflater.inflate(R.menu.zen_mode_rule, menu); 200 } 201 202 @Override 203 public boolean onOptionsItemSelected(MenuItem item) { 204 if (DEBUG) Log.d(TAG, "onOptionsItemSelected " + item.getItemId()); 205 if (item.getItemId() == R.id.delete) { 206 MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_DELETE_RULE); 207 showDeleteRuleDialog(); 208 return true; 209 } 210 return super.onOptionsItemSelected(item); 211 } 212 213 private void showRuleNameDialog() { 214 new ZenRuleNameDialog(mContext, null, mRule.getName(), mRules) { 215 @Override 216 public void onOk(String ruleName) { 217 renameZenRule(mRule.getName(), ruleName); 218 } 219 }.show(); 220 } 221 222 private boolean refreshRuleOrFinish() { 223 mRule = getZenRule(); 224 if (DEBUG) Log.d(TAG, "mRule=" + mRule); 225 if (!setRule(mRule)) { 226 toastAndFinish(); 227 return true; 228 } 229 return false; 230 } 231 232 private void showDeleteRuleDialog() { 233 final AlertDialog dialog = new AlertDialog.Builder(mContext) 234 .setMessage(getString(R.string.zen_mode_delete_rule_confirmation, mRule.getName())) 235 .setNegativeButton(R.string.cancel, null) 236 .setPositiveButton(R.string.zen_mode_delete_rule_button, new OnClickListener() { 237 @Override 238 public void onClick(DialogInterface dialog, int which) { 239 MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_DELETE_RULE_OK); 240 mDeleting = true; 241 removeZenRule(mRule.getName()); 242 } 243 }) 244 .show(); 245 final View messageView = dialog.findViewById(android.R.id.message); 246 if (messageView != null) { 247 messageView.setTextDirection(View.TEXT_DIRECTION_LOCALE); 248 } 249 } 250 251 private void toastAndFinish() { 252 if (!mDeleting) { 253 Toast.makeText(mContext, R.string.zen_mode_rule_not_found_text, Toast.LENGTH_SHORT) 254 .show(); 255 } 256 getActivity().finish(); 257 } 258 259 private void updateRuleName() { 260 getActivity().setTitle(mRule.getName()); 261 mRuleName.setSummary(mRule.getName()); 262 } 263 264 private AutomaticZenRule getZenRule() { 265 return NotificationManager.from(mContext).getAutomaticZenRule(mName); 266 } 267 268 private boolean renameZenRule(String oldName, String newName) { 269 final boolean success = 270 NotificationManager.from(mContext).renameAutomaticZenRule(oldName, newName); 271 if (success) { 272 mName = newName; 273 } 274 maybeRefreshRules(success, true); 275 return success; 276 } 277 278 private void updateControls() { 279 mDisableListeners = true; 280 updateRuleName(); 281 updateControlsInternal(); 282 mZenMode.setValue(Integer.toString(mRule.getInterruptionFilter())); 283 if (mSwitchBar != null) { 284 mSwitchBar.setChecked(mRule.isEnabled()); 285 } 286 mDisableListeners = false; 287 } 288 289} 290