ZenModeSettings.java revision 8ef00dfe4b83cd4aa302946b9b986fa6b9921701
1/* 2 * Copyright (C) 2014 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.Dialog; 21import android.app.DialogFragment; 22import android.app.FragmentManager; 23import android.app.INotificationManager; 24import android.app.TimePickerDialog; 25import android.content.Context; 26import android.content.DialogInterface; 27import android.content.DialogInterface.OnDismissListener; 28import android.content.pm.PackageManager; 29import android.content.res.Resources; 30import android.database.ContentObserver; 31import android.net.Uri; 32import android.os.Bundle; 33import android.os.Handler; 34import android.os.ServiceManager; 35import android.preference.Preference; 36import android.preference.Preference.OnPreferenceChangeListener; 37import android.preference.Preference.OnPreferenceClickListener; 38import android.preference.PreferenceCategory; 39import android.preference.PreferenceScreen; 40import android.preference.SwitchPreference; 41import android.provider.Settings.Global; 42import android.service.notification.Condition; 43import android.service.notification.ZenModeConfig; 44import android.text.format.DateFormat; 45import android.util.Log; 46import android.util.SparseArray; 47import android.widget.TimePicker; 48 49import com.android.settings.R; 50import com.android.settings.SettingsPreferenceFragment; 51import com.android.settings.Utils; 52import com.android.settings.search.BaseSearchIndexProvider; 53import com.android.settings.search.Indexable; 54import com.android.settings.search.SearchIndexableRaw; 55 56import java.text.SimpleDateFormat; 57import java.util.ArrayList; 58import java.util.Calendar; 59import java.util.List; 60import java.util.Objects; 61 62public class ZenModeSettings extends SettingsPreferenceFragment implements Indexable { 63 private static final String TAG = "ZenModeSettings"; 64 private static final boolean DEBUG = true; 65 66 private static final String KEY_ZEN_MODE = "zen_mode"; 67 private static final String KEY_IMPORTANT = "important"; 68 private static final String KEY_CALLS = "phone_calls"; 69 private static final String KEY_MESSAGES = "messages"; 70 private static final String KEY_STARRED = "starred"; 71 private static final String KEY_ALARM_INFO = "alarm_info"; 72 73 private static final String KEY_DOWNTIME = "downtime"; 74 private static final String KEY_DAYS = "days"; 75 private static final String KEY_START_TIME = "start_time"; 76 private static final String KEY_END_TIME = "end_time"; 77 78 private static final String KEY_AUTOMATION = "automation"; 79 private static final String KEY_ENTRY = "entry"; 80 private static final String KEY_CONDITION_PROVIDERS = "manage_condition_providers"; 81 82 private static final SettingPref PREF_ZEN_MODE = new SettingPref(SettingPref.TYPE_GLOBAL, 83 KEY_ZEN_MODE, Global.ZEN_MODE, Global.ZEN_MODE_OFF, Global.ZEN_MODE_OFF, 84 Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, Global.ZEN_MODE_NO_INTERRUPTIONS) { 85 protected String getCaption(Resources res, int value) { 86 switch (value) { 87 case Global.ZEN_MODE_NO_INTERRUPTIONS: 88 return res.getString(R.string.zen_mode_option_no_interruptions); 89 case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: 90 return res.getString(R.string.zen_mode_option_important_interruptions); 91 default: 92 return res.getString(R.string.zen_mode_option_off); 93 } 94 } 95 }; 96 97 private static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat("EEE"); 98 99 private static SparseArray<String> allKeyTitles(Context context) { 100 final SparseArray<String> rt = new SparseArray<String>(); 101 rt.put(R.string.zen_mode_important_category, KEY_IMPORTANT); 102 if (Utils.isVoiceCapable(context)) { 103 rt.put(R.string.zen_mode_phone_calls, KEY_CALLS); 104 rt.put(R.string.zen_mode_option_title, KEY_ZEN_MODE); 105 } else { 106 rt.put(R.string.zen_mode_option_title_novoice, KEY_ZEN_MODE); 107 } 108 rt.put(R.string.zen_mode_messages, KEY_MESSAGES); 109 rt.put(R.string.zen_mode_from_starred, KEY_STARRED); 110 rt.put(R.string.zen_mode_alarm_info, KEY_ALARM_INFO); 111 rt.put(R.string.zen_mode_downtime_category, KEY_DOWNTIME); 112 rt.put(R.string.zen_mode_downtime_days, KEY_DAYS); 113 rt.put(R.string.zen_mode_start_time, KEY_START_TIME); 114 rt.put(R.string.zen_mode_end_time, KEY_END_TIME); 115 rt.put(R.string.zen_mode_automation_category, KEY_AUTOMATION); 116 rt.put(R.string.manage_condition_providers, KEY_CONDITION_PROVIDERS); 117 return rt; 118 } 119 120 private final Handler mHandler = new Handler(); 121 private final SettingsObserver mSettingsObserver = new SettingsObserver(); 122 123 private Context mContext; 124 private PackageManager mPM; 125 private ZenModeConfig mConfig; 126 private boolean mDisableListeners; 127 private SwitchPreference mCalls; 128 private SwitchPreference mMessages; 129 private DropDownPreference mStarred; 130 private Preference mDays; 131 private TimePickerPreference mStart; 132 private TimePickerPreference mEnd; 133 private PreferenceCategory mAutomationCategory; 134 private Preference mEntry; 135 private Preference mConditionProviders; 136 137 @Override 138 public void onCreate(Bundle savedInstanceState) { 139 super.onCreate(savedInstanceState); 140 mContext = getActivity(); 141 mPM = mContext.getPackageManager(); 142 143 addPreferencesFromResource(R.xml.zen_mode_settings); 144 final PreferenceScreen root = getPreferenceScreen(); 145 146 mConfig = getZenModeConfig(); 147 if (DEBUG) Log.d(TAG, "Loaded mConfig=" + mConfig); 148 149 final Preference zenMode = PREF_ZEN_MODE.init(this); 150 if (!Utils.isVoiceCapable(mContext)) { 151 zenMode.setTitle(R.string.zen_mode_option_title_novoice); 152 } 153 154 final PreferenceCategory important = 155 (PreferenceCategory) root.findPreference(KEY_IMPORTANT); 156 157 mCalls = (SwitchPreference) important.findPreference(KEY_CALLS); 158 if (Utils.isVoiceCapable(mContext)) { 159 mCalls.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 160 @Override 161 public boolean onPreferenceChange(Preference preference, Object newValue) { 162 if (mDisableListeners) return true; 163 final boolean val = (Boolean) newValue; 164 if (val == mConfig.allowCalls) return true; 165 if (DEBUG) Log.d(TAG, "onPrefChange allowCalls=" + val); 166 final ZenModeConfig newConfig = mConfig.copy(); 167 newConfig.allowCalls = val; 168 return setZenModeConfig(newConfig); 169 } 170 }); 171 } else { 172 important.removePreference(mCalls); 173 mCalls = null; 174 } 175 176 mMessages = (SwitchPreference) important.findPreference(KEY_MESSAGES); 177 mMessages.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 178 @Override 179 public boolean onPreferenceChange(Preference preference, Object newValue) { 180 if (mDisableListeners) return true; 181 final boolean val = (Boolean) newValue; 182 if (val == mConfig.allowMessages) return true; 183 if (DEBUG) Log.d(TAG, "onPrefChange allowMessages=" + val); 184 final ZenModeConfig newConfig = mConfig.copy(); 185 newConfig.allowMessages = val; 186 return setZenModeConfig(newConfig); 187 } 188 }); 189 190 mStarred = (DropDownPreference) important.findPreference(KEY_STARRED); 191 mStarred.setDropDownWidth(R.dimen.zen_mode_dropdown_width); 192 mStarred.addItem(R.string.zen_mode_from_anyone, ZenModeConfig.SOURCE_ANYONE); 193 mStarred.addItem(R.string.zen_mode_from_starred, ZenModeConfig.SOURCE_STAR); 194 mStarred.addItem(R.string.zen_mode_from_contacts, ZenModeConfig.SOURCE_CONTACT); 195 mStarred.setCallback(new DropDownPreference.Callback() { 196 @Override 197 public boolean onItemSelected(int pos, Object newValue) { 198 if (mDisableListeners) return true; 199 final int val = (Integer) newValue; 200 if (val == mConfig.allowFrom) return true; 201 if (DEBUG) Log.d(TAG, "onPrefChange allowFrom=" + 202 ZenModeConfig.sourceToString(val)); 203 final ZenModeConfig newConfig = mConfig.copy(); 204 newConfig.allowFrom = val; 205 return setZenModeConfig(newConfig); 206 } 207 }); 208 important.addPreference(mStarred); 209 210 final PreferenceCategory downtime = (PreferenceCategory) root.findPreference(KEY_DOWNTIME); 211 212 mDays = downtime.findPreference(KEY_DAYS); 213 mDays.setOnPreferenceClickListener(new OnPreferenceClickListener() { 214 @Override 215 public boolean onPreferenceClick(Preference preference) { 216 new AlertDialog.Builder(mContext) 217 .setTitle(R.string.zen_mode_downtime_days) 218 .setView(new ZenModeDowntimeDaysSelection(mContext, mConfig.sleepMode) { 219 @Override 220 protected void onChanged(String mode) { 221 if (mDisableListeners) return; 222 if (Objects.equals(mode, mConfig.sleepMode)) return; 223 if (DEBUG) Log.d(TAG, "days.onChanged sleepMode=" + mode); 224 final ZenModeConfig newConfig = mConfig.copy(); 225 newConfig.sleepMode = mode; 226 setZenModeConfig(newConfig); 227 } 228 }) 229 .setOnDismissListener(new OnDismissListener() { 230 @Override 231 public void onDismiss(DialogInterface dialog) { 232 updateDays(); 233 } 234 }) 235 .setPositiveButton(R.string.done_button, null) 236 .show(); 237 return true; 238 } 239 }); 240 241 final FragmentManager mgr = getFragmentManager(); 242 243 mStart = new TimePickerPreference(mContext, mgr); 244 mStart.setKey(KEY_START_TIME); 245 mStart.setTitle(R.string.zen_mode_start_time); 246 mStart.setCallback(new TimePickerPreference.Callback() { 247 @Override 248 public boolean onSetTime(int hour, int minute) { 249 if (mDisableListeners) return true; 250 if (!ZenModeConfig.isValidHour(hour)) return false; 251 if (!ZenModeConfig.isValidMinute(minute)) return false; 252 if (hour == mConfig.sleepStartHour && minute == mConfig.sleepStartMinute) { 253 return true; 254 } 255 if (DEBUG) Log.d(TAG, "onPrefChange sleepStart h=" + hour + " m=" + minute); 256 final ZenModeConfig newConfig = mConfig.copy(); 257 newConfig.sleepStartHour = hour; 258 newConfig.sleepStartMinute = minute; 259 return setZenModeConfig(newConfig); 260 } 261 }); 262 downtime.addPreference(mStart); 263 mStart.setDependency(mDays.getKey()); 264 265 mEnd = new TimePickerPreference(mContext, mgr); 266 mEnd.setKey(KEY_END_TIME); 267 mEnd.setTitle(R.string.zen_mode_end_time); 268 mEnd.setCallback(new TimePickerPreference.Callback() { 269 @Override 270 public boolean onSetTime(int hour, int minute) { 271 if (mDisableListeners) return true; 272 if (!ZenModeConfig.isValidHour(hour)) return false; 273 if (!ZenModeConfig.isValidMinute(minute)) return false; 274 if (hour == mConfig.sleepEndHour && minute == mConfig.sleepEndMinute) { 275 return true; 276 } 277 if (DEBUG) Log.d(TAG, "onPrefChange sleepEnd h=" + hour + " m=" + minute); 278 final ZenModeConfig newConfig = mConfig.copy(); 279 newConfig.sleepEndHour = hour; 280 newConfig.sleepEndMinute = minute; 281 return setZenModeConfig(newConfig); 282 } 283 }); 284 downtime.addPreference(mEnd); 285 mEnd.setDependency(mDays.getKey()); 286 287 mAutomationCategory = (PreferenceCategory) findPreference(KEY_AUTOMATION); 288 mEntry = findPreference(KEY_ENTRY); 289 mEntry.setOnPreferenceClickListener(new OnPreferenceClickListener() { 290 @Override 291 public boolean onPreferenceClick(Preference preference) { 292 new AlertDialog.Builder(mContext) 293 .setTitle(R.string.zen_mode_entry_conditions_title) 294 .setView(new ZenModeAutomaticConditionSelection(mContext)) 295 .setOnDismissListener(new OnDismissListener() { 296 @Override 297 public void onDismiss(DialogInterface dialog) { 298 refreshAutomationSection(); 299 } 300 }) 301 .setPositiveButton(R.string.dlg_ok, null) 302 .show(); 303 return true; 304 } 305 }); 306 mConditionProviders = findPreference(KEY_CONDITION_PROVIDERS); 307 308 updateControls(); 309 } 310 311 private void updateDays() { 312 if (mConfig != null) { 313 final int[] days = ZenModeConfig.tryParseDays(mConfig.sleepMode); 314 if (days != null && days.length != 0) { 315 final StringBuilder sb = new StringBuilder(); 316 final Calendar c = Calendar.getInstance(); 317 for (int i = 0; i < ZenModeConfig.ALL_DAYS.length; i++) { 318 final int day = ZenModeConfig.ALL_DAYS[i]; 319 for (int j = 0; j < days.length; j++) { 320 if (day == days[j]) { 321 c.set(Calendar.DAY_OF_WEEK, day); 322 if (sb.length() > 0) { 323 sb.append(mContext.getString(R.string.summary_divider_text)); 324 } 325 sb.append(DAY_FORMAT.format(c.getTime())); 326 break; 327 } 328 } 329 } 330 if (sb.length() > 0) { 331 mDays.setSummary(sb); 332 mDays.notifyDependencyChange(false); 333 return; 334 } 335 } 336 } 337 mDays.setSummary(R.string.zen_mode_downtime_days_none); 338 mDays.notifyDependencyChange(true); 339 } 340 341 private void updateEndSummary() { 342 final int startMin = 60 * mConfig.sleepStartHour + mConfig.sleepStartMinute; 343 final int endMin = 60 * mConfig.sleepEndHour + mConfig.sleepEndMinute; 344 final boolean nextDay = startMin >= endMin; 345 mEnd.setSummaryFormat(nextDay ? R.string.zen_mode_end_time_summary_format : 0); 346 } 347 348 private void updateControls() { 349 mDisableListeners = true; 350 if (mCalls != null) { 351 mCalls.setChecked(mConfig.allowCalls); 352 } 353 mMessages.setChecked(mConfig.allowMessages); 354 mStarred.setSelectedValue(mConfig.allowFrom); 355 updateStarredEnabled(); 356 updateDays(); 357 mStart.setTime(mConfig.sleepStartHour, mConfig.sleepStartMinute); 358 mEnd.setTime(mConfig.sleepEndHour, mConfig.sleepEndMinute); 359 mDisableListeners = false; 360 refreshAutomationSection(); 361 updateEndSummary(); 362 } 363 364 private void updateStarredEnabled() { 365 mStarred.setEnabled(mConfig.allowCalls || mConfig.allowMessages); 366 } 367 368 private void refreshAutomationSection() { 369 if (mConditionProviders != null) { 370 final int total = ConditionProviderSettings.getProviderCount(mPM); 371 if (total == 0) { 372 getPreferenceScreen().removePreference(mAutomationCategory); 373 } else { 374 final int n = ConditionProviderSettings.getEnabledProviderCount(mContext); 375 if (n == 0) { 376 mConditionProviders.setSummary(getResources().getString( 377 R.string.manage_condition_providers_summary_zero)); 378 } else { 379 mConditionProviders.setSummary(String.format(getResources().getQuantityString( 380 R.plurals.manage_condition_providers_summary_nonzero, 381 n, n))); 382 } 383 final String entrySummary = getEntryConditionSummary(); 384 if (n == 0 || entrySummary == null) { 385 mEntry.setSummary(R.string.zen_mode_entry_conditions_summary_none); 386 } else { 387 mEntry.setSummary(entrySummary); 388 } 389 } 390 } 391 } 392 393 private String getEntryConditionSummary() { 394 final INotificationManager nm = INotificationManager.Stub.asInterface( 395 ServiceManager.getService(Context.NOTIFICATION_SERVICE)); 396 try { 397 final Condition[] automatic = nm.getAutomaticZenModeConditions(); 398 if (automatic == null || automatic.length == 0) { 399 return null; 400 } 401 final String divider = getString(R.string.summary_divider_text); 402 final StringBuilder sb = new StringBuilder(); 403 for (int i = 0; i < automatic.length; i++) { 404 if (i > 0) sb.append(divider); 405 sb.append(automatic[i].summary); 406 } 407 return sb.toString(); 408 } catch (Exception e) { 409 Log.w(TAG, "Error calling getAutomaticZenModeConditions", e); 410 return null; 411 } 412 } 413 414 @Override 415 public void onResume() { 416 super.onResume(); 417 updateControls(); 418 mSettingsObserver.register(); 419 } 420 421 @Override 422 public void onPause() { 423 super.onPause(); 424 mSettingsObserver.unregister(); 425 } 426 427 private void updateZenModeConfig() { 428 final ZenModeConfig config = getZenModeConfig(); 429 if (Objects.equals(config, mConfig)) return; 430 mConfig = config; 431 if (DEBUG) Log.d(TAG, "updateZenModeConfig mConfig=" + mConfig); 432 updateControls(); 433 } 434 435 private ZenModeConfig getZenModeConfig() { 436 final INotificationManager nm = INotificationManager.Stub.asInterface( 437 ServiceManager.getService(Context.NOTIFICATION_SERVICE)); 438 try { 439 return nm.getZenModeConfig(); 440 } catch (Exception e) { 441 Log.w(TAG, "Error calling NoMan", e); 442 return new ZenModeConfig(); 443 } 444 } 445 446 private boolean setZenModeConfig(ZenModeConfig config) { 447 final INotificationManager nm = INotificationManager.Stub.asInterface( 448 ServiceManager.getService(Context.NOTIFICATION_SERVICE)); 449 try { 450 final boolean success = nm.setZenModeConfig(config); 451 if (success) { 452 mConfig = config; 453 if (DEBUG) Log.d(TAG, "Saved mConfig=" + mConfig); 454 updateEndSummary(); 455 updateStarredEnabled(); 456 } 457 return success; 458 } catch (Exception e) { 459 Log.w(TAG, "Error calling NoMan", e); 460 return false; 461 } 462 } 463 464 protected void putZenModeSetting(int value) { 465 Global.putInt(getContentResolver(), Global.ZEN_MODE, value); 466 } 467 468 protected ZenModeConditionSelection newConditionSelection() { 469 return new ZenModeConditionSelection(mContext); 470 } 471 472 // Enable indexing of searchable data 473 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 474 new BaseSearchIndexProvider() { 475 @Override 476 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) { 477 final SparseArray<String> keyTitles = allKeyTitles(context); 478 final int N = keyTitles.size(); 479 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>(N); 480 final Resources res = context.getResources(); 481 for (int i = 0; i < N; i++) { 482 final SearchIndexableRaw data = new SearchIndexableRaw(context); 483 data.key = keyTitles.valueAt(i); 484 data.title = res.getString(keyTitles.keyAt(i)); 485 data.screenTitle = res.getString(R.string.zen_mode_settings_title); 486 result.add(data); 487 } 488 return result; 489 } 490 491 public List<String> getNonIndexableKeys(Context context) { 492 final ArrayList<String> rt = new ArrayList<String>(); 493 if (!Utils.isVoiceCapable(context)) { 494 rt.add(KEY_CALLS); 495 } 496 return rt; 497 } 498 }; 499 500 private final class SettingsObserver extends ContentObserver { 501 private final Uri ZEN_MODE_URI = Global.getUriFor(Global.ZEN_MODE); 502 private final Uri ZEN_MODE_CONFIG_ETAG_URI = Global.getUriFor(Global.ZEN_MODE_CONFIG_ETAG); 503 504 public SettingsObserver() { 505 super(mHandler); 506 } 507 508 public void register() { 509 getContentResolver().registerContentObserver(ZEN_MODE_URI, false, this); 510 getContentResolver().registerContentObserver(ZEN_MODE_CONFIG_ETAG_URI, false, this); 511 } 512 513 public void unregister() { 514 getContentResolver().unregisterContentObserver(this); 515 } 516 517 @Override 518 public void onChange(boolean selfChange, Uri uri) { 519 super.onChange(selfChange, uri); 520 if (ZEN_MODE_URI.equals(uri)) { 521 PREF_ZEN_MODE.update(mContext); 522 } 523 if (ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) { 524 updateZenModeConfig(); 525 } 526 } 527 } 528 529 private static class TimePickerPreference extends Preference { 530 private final Context mContext; 531 532 private int mSummaryFormat; 533 private int mHourOfDay; 534 private int mMinute; 535 private Callback mCallback; 536 537 public TimePickerPreference(Context context, final FragmentManager mgr) { 538 super(context); 539 mContext = context; 540 setPersistent(false); 541 setOnPreferenceClickListener(new OnPreferenceClickListener(){ 542 @Override 543 public boolean onPreferenceClick(Preference preference) { 544 final TimePickerFragment frag = new TimePickerFragment(); 545 frag.pref = TimePickerPreference.this; 546 frag.show(mgr, TimePickerPreference.class.getName()); 547 return true; 548 } 549 }); 550 } 551 552 public void setCallback(Callback callback) { 553 mCallback = callback; 554 } 555 556 public void setSummaryFormat(int resId) { 557 mSummaryFormat = resId; 558 updateSummary(); 559 } 560 561 public void setTime(int hourOfDay, int minute) { 562 if (mCallback != null && !mCallback.onSetTime(hourOfDay, minute)) return; 563 mHourOfDay = hourOfDay; 564 mMinute = minute; 565 updateSummary(); 566 } 567 568 private void updateSummary() { 569 final Calendar c = Calendar.getInstance(); 570 c.set(Calendar.HOUR_OF_DAY, mHourOfDay); 571 c.set(Calendar.MINUTE, mMinute); 572 String time = DateFormat.getTimeFormat(mContext).format(c.getTime()); 573 if (mSummaryFormat != 0) { 574 time = mContext.getResources().getString(mSummaryFormat, time); 575 } 576 setSummary(time); 577 } 578 579 public static class TimePickerFragment extends DialogFragment implements 580 TimePickerDialog.OnTimeSetListener { 581 public TimePickerPreference pref; 582 583 @Override 584 public Dialog onCreateDialog(Bundle savedInstanceState) { 585 final boolean usePref = pref != null && pref.mHourOfDay >= 0 && pref.mMinute >= 0; 586 final Calendar c = Calendar.getInstance(); 587 final int hour = usePref ? pref.mHourOfDay : c.get(Calendar.HOUR_OF_DAY); 588 final int minute = usePref ? pref.mMinute : c.get(Calendar.MINUTE); 589 return new TimePickerDialog(getActivity(), this, hour, minute, 590 DateFormat.is24HourFormat(getActivity())); 591 } 592 593 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 594 if (pref != null) { 595 pref.setTime(hourOfDay, minute); 596 } 597 } 598 } 599 600 public interface Callback { 601 boolean onSetTime(int hour, int minute); 602 } 603 } 604} 605