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;
29 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
30 import com.android.settings.R;
31 import com.android.settings.search.BaseSearchIndexProvider;
32 import com.android.settings.search.Indexable;
33 import com.android.settingslib.core.AbstractPreferenceController;
34 import com.android.settingslib.core.lifecycle.Lifecycle;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.List;
40 import java.util.Map.Entry;
42 public class ZenModeSettings extends ZenModeSettingsBase {
44 protected int getPreferenceScreenResId() {
45 return R.xml.zen_mode_settings;
49 public int getMetricsCategory() {
50 return MetricsEvent.NOTIFICATION_ZEN_MODE;
54 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
55 return buildPreferenceControllers(context, getLifecycle(), getFragmentManager());
59 public int getHelpResource() {
60 return R.string.help_uri_interruptions;
63 private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
64 Lifecycle lifecycle, FragmentManager fragmentManager) {
65 List<AbstractPreferenceController> controllers = new ArrayList<>();
66 controllers.add(new ZenModeBehaviorPreferenceController(context, lifecycle));
67 controllers.add(new ZenModeBlockedEffectsPreferenceController(context, lifecycle));
68 controllers.add(new ZenModeAutomationPreferenceController(context));
69 controllers.add(new ZenModeButtonPreferenceController(context, lifecycle, fragmentManager));
70 controllers.add(new ZenModeSettingsFooterPreferenceController(context, lifecycle));
74 public static class SummaryBuilder {
76 private Context mContext;
78 public SummaryBuilder(Context context) {
82 // these should match NotificationManager.Policy#ALL_PRIORITY_CATEGORIES
83 private static final int[] ALL_PRIORITY_CATEGORIES = {
84 Policy.PRIORITY_CATEGORY_ALARMS,
85 Policy.PRIORITY_CATEGORY_MEDIA,
86 Policy.PRIORITY_CATEGORY_SYSTEM,
87 Policy.PRIORITY_CATEGORY_REMINDERS,
88 Policy.PRIORITY_CATEGORY_EVENTS,
89 Policy.PRIORITY_CATEGORY_MESSAGES,
90 Policy.PRIORITY_CATEGORY_CALLS,
91 Policy.PRIORITY_CATEGORY_REPEAT_CALLERS,
94 String getBehaviorSettingSummary(Policy policy, int zenMode) {
95 List<String> enabledCategories;
97 if (zenMode == Settings.Global.ZEN_MODE_NO_INTERRUPTIONS) {
98 return mContext.getString(R.string.zen_mode_behavior_total_silence);
99 } else if (zenMode == Settings.Global.ZEN_MODE_ALARMS) {
100 return mContext.getString(R.string.zen_mode_behavior_alarms_only);
102 enabledCategories = getEnabledCategories(policy);
105 // no sound categories can bypass dnd
106 int numCategories = enabledCategories.size();
107 if (numCategories == 0) {
108 return mContext.getString(R.string.zen_mode_behavior_total_silence);
111 // only alarms and media can bypass dnd
112 if (numCategories == 2 &&
113 isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_ALARMS) &&
114 isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_MEDIA)) {
115 return mContext.getString(R.string.zen_mode_behavior_alarms_only);
119 return mContext.getString(R.string.zen_mode_behavior_summary_custom);
122 String getSoundSummary() {
123 int zenMode = NotificationManager.from(mContext).getZenMode();
125 if (zenMode != Settings.Global.ZEN_MODE_OFF) {
126 ZenModeConfig config = NotificationManager.from(mContext).getZenModeConfig();
127 String description = ZenModeConfig.getDescription(mContext, true, config);
129 if (description == null) {
130 return mContext.getString(R.string.zen_mode_sound_summary_on);
132 return mContext.getString(R.string.zen_mode_sound_summary_on_with_info,
136 final int count = getEnabledAutomaticRulesCount();
138 return mContext.getString(R.string.zen_mode_sound_summary_off_with_info,
139 mContext.getResources().getQuantityString(
140 R.plurals.zen_mode_sound_summary_summary_off_info,
144 return mContext.getString(R.string.zen_mode_sound_summary_off);
148 String getBlockedEffectsSummary(Policy policy) {
149 if (policy.suppressedVisualEffects == 0) {
150 return mContext.getResources().getString(
151 R.string.zen_mode_block_effect_summary_sound);
152 } else if (Policy.areAllVisualEffectsSuppressed(policy.suppressedVisualEffects)) {
153 return mContext.getResources().getString(
154 R.string.zen_mode_block_effect_summary_all);
156 return mContext.getResources().getString(
157 R.string.zen_mode_block_effect_summary_some);
160 String getAutomaticRulesSummary() {
161 final int count = getEnabledAutomaticRulesCount();
162 return count == 0 ? mContext.getString(R.string.zen_mode_settings_summary_off)
163 : mContext.getResources().getQuantityString(
164 R.plurals.zen_mode_settings_summary_on, count, count);
168 int getEnabledAutomaticRulesCount() {
170 final Map<String, AutomaticZenRule> ruleMap =
171 NotificationManager.from(mContext).getAutomaticZenRules();
172 if (ruleMap != null) {
173 for (Entry<String, AutomaticZenRule> ruleEntry : ruleMap.entrySet()) {
174 final AutomaticZenRule rule = ruleEntry.getValue();
175 if (rule != null && rule.isEnabled()) {
183 private List<String> getEnabledCategories(Policy policy) {
184 List<String> enabledCategories = new ArrayList<>();
185 for (int category : ALL_PRIORITY_CATEGORIES) {
186 if (isCategoryEnabled(policy, category)) {
187 if (category == Policy.PRIORITY_CATEGORY_ALARMS) {
188 enabledCategories.add(mContext.getString(R.string.zen_mode_alarms));
189 } else if (category == Policy.PRIORITY_CATEGORY_MEDIA) {
190 enabledCategories.add(mContext.getString(
191 R.string.zen_mode_media));
192 } else if (category == Policy.PRIORITY_CATEGORY_SYSTEM) {
193 enabledCategories.add(mContext.getString(
194 R.string.zen_mode_system));
195 } else if (category == Policy.PRIORITY_CATEGORY_REMINDERS) {
196 enabledCategories.add(mContext.getString(R.string.zen_mode_reminders));
197 } else if (category == Policy.PRIORITY_CATEGORY_EVENTS) {
198 enabledCategories.add(mContext.getString(R.string.zen_mode_events));
199 } else if (category == Policy.PRIORITY_CATEGORY_MESSAGES) {
200 if (policy.priorityMessageSenders == Policy.PRIORITY_SENDERS_ANY) {
201 enabledCategories.add(mContext.getString(
202 R.string.zen_mode_all_messages));
204 enabledCategories.add(mContext.getString(
205 R.string.zen_mode_selected_messages));
207 } else if (category == Policy.PRIORITY_CATEGORY_CALLS) {
208 if (policy.priorityCallSenders == Policy.PRIORITY_SENDERS_ANY) {
209 enabledCategories.add(mContext.getString(
210 R.string.zen_mode_all_callers));
212 enabledCategories.add(mContext.getString(
213 R.string.zen_mode_selected_callers));
215 } else if (category == Policy.PRIORITY_CATEGORY_REPEAT_CALLERS) {
216 if (!enabledCategories.contains(mContext.getString(
217 R.string.zen_mode_all_callers))) {
218 enabledCategories.add(mContext.getString(
219 R.string.zen_mode_repeat_callers));
224 return enabledCategories;
227 private boolean isCategoryEnabled(Policy policy, int categoryType) {
228 return (policy.priorityCategories & categoryType) != 0;
231 private boolean isEffectSuppressed(Policy policy, int effect) {
232 return (policy.suppressedVisualEffects & effect) != 0;
239 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
240 new BaseSearchIndexProvider() {
243 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
245 final SearchIndexableResource sir = new SearchIndexableResource(context);
246 sir.xmlResId = R.xml.zen_mode_settings;
247 return Arrays.asList(sir);
251 public List<String> getNonIndexableKeys(Context context) {
252 List<String> keys = super.getNonIndexableKeys(context);
253 keys.add(ZenModeButtonPreferenceController.KEY);
258 public List<AbstractPreferenceController> createPreferenceControllers(Context
260 return buildPreferenceControllers(context, null, null);