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;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
41 import java.util.Map.Entry;
43 public class ZenModeSettings extends ZenModeSettingsBase {
44 private static final String KEY_SOUND = "zen_effect_sound";
46 public void onResume() {
48 CheckBoxPreference soundPreference =
49 (CheckBoxPreference) getPreferenceScreen().findPreference(KEY_SOUND);
50 if (soundPreference != null) {
51 soundPreference.setChecked(true);
56 protected int getPreferenceScreenResId() {
57 return R.xml.zen_mode_settings;
61 public int getMetricsCategory() {
62 return MetricsEvent.NOTIFICATION_ZEN_MODE;
66 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
67 return buildPreferenceControllers(context, getLifecycle(), getFragmentManager());
71 public int getHelpResource() {
72 return R.string.help_uri_interruptions;
75 private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
76 Lifecycle lifecycle, FragmentManager fragmentManager) {
77 List<AbstractPreferenceController> controllers = new ArrayList<>();
78 controllers.add(new ZenModeBehaviorPreferenceController(context, lifecycle));
79 controllers.add(new ZenModeBlockedEffectsPreferenceController(context, lifecycle));
80 controllers.add(new ZenModeDurationPreferenceController(context, lifecycle,
82 controllers.add(new ZenModeAutomationPreferenceController(context));
83 controllers.add(new ZenModeButtonPreferenceController(context, lifecycle, fragmentManager));
84 controllers.add(new ZenModeSettingsFooterPreferenceController(context, lifecycle));
88 public static class SummaryBuilder {
90 private Context mContext;
92 public SummaryBuilder(Context context) {
96 // these should match NotificationManager.Policy#ALL_PRIORITY_CATEGORIES
97 private static final int[] ALL_PRIORITY_CATEGORIES = {
98 Policy.PRIORITY_CATEGORY_ALARMS,
99 Policy.PRIORITY_CATEGORY_MEDIA,
100 Policy.PRIORITY_CATEGORY_SYSTEM,
101 Policy.PRIORITY_CATEGORY_REMINDERS,
102 Policy.PRIORITY_CATEGORY_EVENTS,
103 Policy.PRIORITY_CATEGORY_MESSAGES,
104 Policy.PRIORITY_CATEGORY_CALLS,
105 Policy.PRIORITY_CATEGORY_REPEAT_CALLERS,
108 String getBehaviorSettingSummary(Policy policy, int zenMode) {
109 List<String> enabledCategories = getEnabledCategories(policy);
111 int numCategories = enabledCategories.size();
112 if (numCategories == 0) {
113 return mContext.getString(R.string.zen_mode_no_exceptions);
114 } else if (numCategories == 1) {
115 return enabledCategories.get(0);
116 } else if (numCategories == 2) {
117 return mContext.getString(R.string.join_two_items, enabledCategories.get(0),
118 enabledCategories.get(1).toLowerCase());
119 } else if (numCategories == 3){
120 String secondaryText = mContext.getString(R.string.join_two_unrelated_items,
121 enabledCategories.get(0), enabledCategories.get(1).toLowerCase());
122 return mContext.getString(R.string.join_many_items_last, secondaryText,
123 enabledCategories.get(2).toLowerCase());
125 String secondaryText = mContext.getString(R.string.join_many_items_middle,
126 enabledCategories.get(0), enabledCategories.get(1).toLowerCase());
127 secondaryText = mContext.getString(R.string.join_many_items_middle, secondaryText,
128 enabledCategories.get(2).toLowerCase());
129 return mContext.getString(R.string.join_many_items_last, secondaryText,
130 mContext.getString(R.string.zen_mode_other_options));
134 String getSoundSummary() {
135 int zenMode = NotificationManager.from(mContext).getZenMode();
137 if (zenMode != Settings.Global.ZEN_MODE_OFF) {
138 ZenModeConfig config = NotificationManager.from(mContext).getZenModeConfig();
139 String description = ZenModeConfig.getDescription(mContext, true, config, false);
141 if (description == null) {
142 return mContext.getString(R.string.zen_mode_sound_summary_on);
144 return mContext.getString(R.string.zen_mode_sound_summary_on_with_info,
148 final int count = getEnabledAutomaticRulesCount();
150 return mContext.getString(R.string.zen_mode_sound_summary_off_with_info,
151 mContext.getResources().getQuantityString(
152 R.plurals.zen_mode_sound_summary_summary_off_info,
156 return mContext.getString(R.string.zen_mode_sound_summary_off);
160 String getBlockedEffectsSummary(Policy policy) {
161 List<String> blockedStrings = new ArrayList<>();
162 if (Policy.areAnyScreenOffEffectsSuppressed(policy.suppressedVisualEffects)) {
163 blockedStrings.add(mContext.getResources().getString(
164 R.string.zen_mode_block_effect_summary_screen_off));
166 if (Policy.areAnyScreenOnEffectsSuppressed(policy.suppressedVisualEffects)) {
167 blockedStrings.add(mContext.getResources().getString(
168 R.string.zen_mode_block_effect_summary_screen_on));
171 if (blockedStrings.size() == 0) {
172 return mContext.getResources().getString(
173 R.string.zen_mode_block_effect_summary_none);
174 } else if (blockedStrings.size() == 1) {
175 return blockedStrings.get(0);
177 return mContext.getResources().getString(R.string.join_two_unrelated_items,
178 blockedStrings.get(0), blockedStrings.get(1));
182 String getAutomaticRulesSummary() {
183 final int count = getEnabledAutomaticRulesCount();
184 return count == 0 ? mContext.getString(R.string.zen_mode_settings_summary_off)
185 : mContext.getResources().getQuantityString(
186 R.plurals.zen_mode_settings_summary_on, count, count);
190 int getEnabledAutomaticRulesCount() {
192 final Map<String, AutomaticZenRule> ruleMap =
193 NotificationManager.from(mContext).getAutomaticZenRules();
194 if (ruleMap != null) {
195 for (Entry<String, AutomaticZenRule> ruleEntry : ruleMap.entrySet()) {
196 final AutomaticZenRule rule = ruleEntry.getValue();
197 if (rule != null && rule.isEnabled()) {
205 private List<String> getEnabledCategories(Policy policy) {
206 List<String> enabledCategories = new ArrayList<>();
207 for (int category : ALL_PRIORITY_CATEGORIES) {
208 if (isCategoryEnabled(policy, category)) {
209 if (category == Policy.PRIORITY_CATEGORY_ALARMS) {
210 enabledCategories.add(mContext.getString(R.string.zen_mode_alarms));
211 } else if (category == Policy.PRIORITY_CATEGORY_MEDIA) {
212 enabledCategories.add(mContext.getString(
213 R.string.zen_mode_media));
214 } else if (category == Policy.PRIORITY_CATEGORY_SYSTEM) {
215 enabledCategories.add(mContext.getString(
216 R.string.zen_mode_system));
217 } else if (category == Policy.PRIORITY_CATEGORY_REMINDERS) {
218 enabledCategories.add(mContext.getString(R.string.zen_mode_reminders));
219 } else if (category == Policy.PRIORITY_CATEGORY_EVENTS) {
220 enabledCategories.add(mContext.getString(R.string.zen_mode_events));
221 } else if (category == Policy.PRIORITY_CATEGORY_MESSAGES) {
222 if (policy.priorityMessageSenders == Policy.PRIORITY_SENDERS_ANY) {
223 enabledCategories.add(mContext.getString(
224 R.string.zen_mode_all_messages));
226 enabledCategories.add(mContext.getString(
227 R.string.zen_mode_selected_messages));
229 } else if (category == Policy.PRIORITY_CATEGORY_CALLS) {
230 if (policy.priorityCallSenders == Policy.PRIORITY_SENDERS_ANY) {
231 enabledCategories.add(mContext.getString(
232 R.string.zen_mode_all_callers));
234 enabledCategories.add(mContext.getString(
235 R.string.zen_mode_selected_callers));
237 } else if (category == Policy.PRIORITY_CATEGORY_REPEAT_CALLERS) {
238 if (!enabledCategories.contains(mContext.getString(
239 R.string.zen_mode_all_callers))) {
240 enabledCategories.add(mContext.getString(
241 R.string.zen_mode_repeat_callers));
246 return enabledCategories;
249 private boolean isCategoryEnabled(Policy policy, int categoryType) {
250 return (policy.priorityCategories & categoryType) != 0;
253 private boolean isEffectSuppressed(Policy policy, int effect) {
254 return (policy.suppressedVisualEffects & effect) != 0;
261 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
262 new BaseSearchIndexProvider() {
265 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
267 final SearchIndexableResource sir = new SearchIndexableResource(context);
268 sir.xmlResId = R.xml.zen_mode_settings;
269 return Arrays.asList(sir);
273 public List<String> getNonIndexableKeys(Context context) {
274 List<String> keys = super.getNonIndexableKeys(context);
275 keys.add(ZenModeDurationPreferenceController.KEY);
276 keys.add(ZenModeButtonPreferenceController.KEY);
281 public List<AbstractPreferenceController> createPreferenceControllers(Context
283 return buildPreferenceControllers(context, null, null);