OSDN Git Service

557d6248960df4ece01b0c9ca539593dc3e915ce
[android-x86/packages-apps-Settings.git] / src / com / android / settings / notification / ZenModeSettings.java
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
17 package com.android.settings.notification;
18
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
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;
35
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Map.Entry;
41
42 public class ZenModeSettings extends ZenModeSettingsBase {
43     @Override
44     protected int getPreferenceScreenResId() {
45         return R.xml.zen_mode_settings;
46     }
47
48     @Override
49     public int getMetricsCategory() {
50         return MetricsEvent.NOTIFICATION_ZEN_MODE;
51     }
52
53     @Override
54     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
55         return buildPreferenceControllers(context, getLifecycle(), getFragmentManager());
56     }
57
58     @Override
59     public int getHelpResource() {
60         return R.string.help_uri_interruptions;
61     }
62
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));
71         return controllers;
72     }
73
74     public static class SummaryBuilder {
75
76         private Context mContext;
77
78         public SummaryBuilder(Context context) {
79             mContext = context;
80         }
81
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,
92         };
93
94         String getBehaviorSettingSummary(Policy policy, int zenMode) {
95             List<String> enabledCategories;
96
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);
101             } else {
102                 enabledCategories = getEnabledCategories(policy);
103             }
104
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);
109             }
110
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);
116             }
117
118             // custom
119             return mContext.getString(R.string.zen_mode_behavior_summary_custom);
120         }
121
122         String getSoundSummary() {
123             int zenMode = NotificationManager.from(mContext).getZenMode();
124
125             if (zenMode != Settings.Global.ZEN_MODE_OFF) {
126                 ZenModeConfig config = NotificationManager.from(mContext).getZenModeConfig();
127                 String description = ZenModeConfig.getDescription(mContext, true, config);
128
129                 if (description == null) {
130                     return mContext.getString(R.string.zen_mode_sound_summary_on);
131                 } else {
132                     return mContext.getString(R.string.zen_mode_sound_summary_on_with_info,
133                             description);
134                 }
135             } else {
136                 final int count = getEnabledAutomaticRulesCount();
137                 if (count > 0) {
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,
141                                     count, count));
142                 }
143
144                 return mContext.getString(R.string.zen_mode_sound_summary_off);
145             }
146         }
147
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);
155             }
156             return mContext.getResources().getString(
157                     R.string.zen_mode_block_effect_summary_some);
158         }
159
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);
165         }
166
167         @VisibleForTesting
168         int getEnabledAutomaticRulesCount() {
169             int count = 0;
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()) {
176                         count++;
177                     }
178                 }
179             }
180             return count;
181         }
182
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));
203                         } else {
204                             enabledCategories.add(mContext.getString(
205                                     R.string.zen_mode_selected_messages));
206                         }
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));
211                         } else {
212                             enabledCategories.add(mContext.getString(
213                                     R.string.zen_mode_selected_callers));
214                         }
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));
220                         }
221                     }
222                 }
223             }
224             return enabledCategories;
225         }
226
227         private boolean isCategoryEnabled(Policy policy, int categoryType) {
228             return (policy.priorityCategories & categoryType) != 0;
229         }
230
231         private boolean isEffectSuppressed(Policy policy, int effect) {
232             return (policy.suppressedVisualEffects & effect) != 0;
233         }
234     }
235
236     /**
237      * For Search.
238      */
239     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
240             new BaseSearchIndexProvider() {
241
242                 @Override
243                 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
244                         boolean enabled) {
245                     final SearchIndexableResource sir = new SearchIndexableResource(context);
246                     sir.xmlResId = R.xml.zen_mode_settings;
247                     return Arrays.asList(sir);
248                 }
249
250                 @Override
251                 public List<String> getNonIndexableKeys(Context context) {
252                     List<String> keys = super.getNonIndexableKeys(context);
253                     keys.add(ZenModeButtonPreferenceController.KEY);
254                     return keys;
255                 }
256
257                 @Override
258                 public List<AbstractPreferenceController> createPreferenceControllers(Context
259                         context) {
260                     return buildPreferenceControllers(context, null, null);
261                 }
262             };
263 }