2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.notification;
19 import android.app.AutomaticZenRule;
20 import android.app.FragmentManager;
21 import android.app.NotificationManager;
22 import android.app.NotificationManager.Policy;
23 import android.content.Context;
24 import android.provider.SearchIndexableResource;
25 import android.provider.Settings;
26 import android.service.notification.ZenModeConfig;
27 import android.support.annotation.VisibleForTesting;
28 import android.support.v7.preference.CheckBoxPreference;
30 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
31 import com.android.settings.R;
32 import com.android.settings.search.BaseSearchIndexProvider;
33 import com.android.settings.search.Indexable;
34 import com.android.settingslib.core.AbstractPreferenceController;
35 import com.android.settingslib.core.lifecycle.Lifecycle;
36 import com.android.settingslib.search.SearchIndexable;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
42 import java.util.Map.Entry;
45 public class ZenModeSettings extends ZenModeSettingsBase {
46 private static final String KEY_SOUND = "zen_effect_sound";
48 public void onResume() {
50 CheckBoxPreference soundPreference =
51 (CheckBoxPreference) getPreferenceScreen().findPreference(KEY_SOUND);
52 if (soundPreference != null) {
53 soundPreference.setChecked(true);
58 protected int getPreferenceScreenResId() {
59 return R.xml.zen_mode_settings;
63 public int getMetricsCategory() {
64 return MetricsEvent.NOTIFICATION_ZEN_MODE;
68 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
69 return buildPreferenceControllers(context, getLifecycle(), getFragmentManager());
73 public int getHelpResource() {
74 return R.string.help_uri_interruptions;
77 private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
78 Lifecycle lifecycle, FragmentManager fragmentManager) {
79 List<AbstractPreferenceController> controllers = new ArrayList<>();
80 controllers.add(new ZenModeBehaviorPreferenceController(context, lifecycle));
81 controllers.add(new ZenModeBlockedEffectsPreferenceController(context, lifecycle));
82 controllers.add(new ZenModeDurationPreferenceController(context, lifecycle,
84 controllers.add(new ZenModeAutomationPreferenceController(context));
85 controllers.add(new ZenModeButtonPreferenceController(context, lifecycle, fragmentManager));
86 controllers.add(new ZenModeSettingsFooterPreferenceController(context, lifecycle));
90 public static class SummaryBuilder {
92 private Context mContext;
94 public SummaryBuilder(Context context) {
98 // these should match NotificationManager.Policy#ALL_PRIORITY_CATEGORIES
99 private static final int[] ALL_PRIORITY_CATEGORIES = {
100 Policy.PRIORITY_CATEGORY_ALARMS,
101 Policy.PRIORITY_CATEGORY_MEDIA,
102 Policy.PRIORITY_CATEGORY_SYSTEM,
103 Policy.PRIORITY_CATEGORY_REMINDERS,
104 Policy.PRIORITY_CATEGORY_EVENTS,
105 Policy.PRIORITY_CATEGORY_MESSAGES,
106 Policy.PRIORITY_CATEGORY_CALLS,
107 Policy.PRIORITY_CATEGORY_REPEAT_CALLERS,
110 String getBehaviorSettingSummary(Policy policy, int zenMode) {
111 List<String> enabledCategories = getEnabledCategories(policy);
113 int numCategories = enabledCategories.size();
114 if (numCategories == 0) {
115 return mContext.getString(R.string.zen_mode_no_exceptions);
116 } else if (numCategories == 1) {
117 return enabledCategories.get(0);
118 } else if (numCategories == 2) {
119 return mContext.getString(R.string.join_two_items, enabledCategories.get(0),
120 enabledCategories.get(1).toLowerCase());
121 } else if (numCategories == 3){
122 String secondaryText = mContext.getString(R.string.join_two_unrelated_items,
123 enabledCategories.get(0), enabledCategories.get(1).toLowerCase());
124 return mContext.getString(R.string.join_many_items_last, secondaryText,
125 enabledCategories.get(2).toLowerCase());
127 String secondaryText = mContext.getString(R.string.join_many_items_middle,
128 enabledCategories.get(0), enabledCategories.get(1).toLowerCase());
129 secondaryText = mContext.getString(R.string.join_many_items_middle, secondaryText,
130 enabledCategories.get(2).toLowerCase());
131 return mContext.getString(R.string.join_many_items_last, secondaryText,
132 mContext.getString(R.string.zen_mode_other_options));
136 String getSoundSummary() {
137 int zenMode = NotificationManager.from(mContext).getZenMode();
139 if (zenMode != Settings.Global.ZEN_MODE_OFF) {
140 ZenModeConfig config = NotificationManager.from(mContext).getZenModeConfig();
141 String description = ZenModeConfig.getDescription(mContext, true, config, false);
143 if (description == null) {
144 return mContext.getString(R.string.zen_mode_sound_summary_on);
146 return mContext.getString(R.string.zen_mode_sound_summary_on_with_info,
150 final int count = getEnabledAutomaticRulesCount();
152 return mContext.getString(R.string.zen_mode_sound_summary_off_with_info,
153 mContext.getResources().getQuantityString(
154 R.plurals.zen_mode_sound_summary_summary_off_info,
158 return mContext.getString(R.string.zen_mode_sound_summary_off);
162 String getBlockedEffectsSummary(Policy policy) {
163 List<String> blockedStrings = new ArrayList<>();
164 if (Policy.areAnyScreenOffEffectsSuppressed(policy.suppressedVisualEffects)) {
165 blockedStrings.add(mContext.getResources().getString(
166 R.string.zen_mode_block_effect_summary_screen_off));
168 if (Policy.areAnyScreenOnEffectsSuppressed(policy.suppressedVisualEffects)) {
169 blockedStrings.add(mContext.getResources().getString(
170 R.string.zen_mode_block_effect_summary_screen_on));
173 if (blockedStrings.size() == 0) {
174 return mContext.getResources().getString(
175 R.string.zen_mode_block_effect_summary_none);
176 } else if (blockedStrings.size() == 1) {
177 return blockedStrings.get(0);
179 return mContext.getResources().getString(R.string.join_two_unrelated_items,
180 blockedStrings.get(0), blockedStrings.get(1));
184 String getAutomaticRulesSummary() {
185 final int count = getEnabledAutomaticRulesCount();
186 return count == 0 ? mContext.getString(R.string.zen_mode_settings_summary_off)
187 : mContext.getResources().getQuantityString(
188 R.plurals.zen_mode_settings_summary_on, count, count);
192 int getEnabledAutomaticRulesCount() {
194 final Map<String, AutomaticZenRule> ruleMap =
195 NotificationManager.from(mContext).getAutomaticZenRules();
196 if (ruleMap != null) {
197 for (Entry<String, AutomaticZenRule> ruleEntry : ruleMap.entrySet()) {
198 final AutomaticZenRule rule = ruleEntry.getValue();
199 if (rule != null && rule.isEnabled()) {
207 private List<String> getEnabledCategories(Policy policy) {
208 List<String> enabledCategories = new ArrayList<>();
209 for (int category : ALL_PRIORITY_CATEGORIES) {
210 if (isCategoryEnabled(policy, category)) {
211 if (category == Policy.PRIORITY_CATEGORY_ALARMS) {
212 enabledCategories.add(mContext.getString(R.string.zen_mode_alarms));
213 } else if (category == Policy.PRIORITY_CATEGORY_MEDIA) {
214 enabledCategories.add(mContext.getString(
215 R.string.zen_mode_media));
216 } else if (category == Policy.PRIORITY_CATEGORY_SYSTEM) {
217 enabledCategories.add(mContext.getString(
218 R.string.zen_mode_system));
219 } else if (category == Policy.PRIORITY_CATEGORY_REMINDERS) {
220 enabledCategories.add(mContext.getString(R.string.zen_mode_reminders));
221 } else if (category == Policy.PRIORITY_CATEGORY_EVENTS) {
222 enabledCategories.add(mContext.getString(R.string.zen_mode_events));
223 } else if (category == Policy.PRIORITY_CATEGORY_MESSAGES) {
224 if (policy.priorityMessageSenders == Policy.PRIORITY_SENDERS_ANY) {
225 enabledCategories.add(mContext.getString(
226 R.string.zen_mode_all_messages));
228 enabledCategories.add(mContext.getString(
229 R.string.zen_mode_selected_messages));
231 } else if (category == Policy.PRIORITY_CATEGORY_CALLS) {
232 if (policy.priorityCallSenders == Policy.PRIORITY_SENDERS_ANY) {
233 enabledCategories.add(mContext.getString(
234 R.string.zen_mode_all_callers));
236 enabledCategories.add(mContext.getString(
237 R.string.zen_mode_selected_callers));
239 } else if (category == Policy.PRIORITY_CATEGORY_REPEAT_CALLERS) {
240 if (!enabledCategories.contains(mContext.getString(
241 R.string.zen_mode_all_callers))) {
242 enabledCategories.add(mContext.getString(
243 R.string.zen_mode_repeat_callers));
248 return enabledCategories;
251 private boolean isCategoryEnabled(Policy policy, int categoryType) {
252 return (policy.priorityCategories & categoryType) != 0;
255 private boolean isEffectSuppressed(Policy policy, int effect) {
256 return (policy.suppressedVisualEffects & effect) != 0;
263 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
264 new BaseSearchIndexProvider() {
267 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
269 final SearchIndexableResource sir = new SearchIndexableResource(context);
270 sir.xmlResId = R.xml.zen_mode_settings;
271 return Arrays.asList(sir);
275 public List<String> getNonIndexableKeys(Context context) {
276 List<String> keys = super.getNonIndexableKeys(context);
277 keys.add(ZenModeDurationPreferenceController.KEY);
278 keys.add(ZenModeButtonPreferenceController.KEY);
283 public List<AbstractPreferenceController> createPreferenceControllers(Context
285 return buildPreferenceControllers(context, null, null);