OSDN Git Service

b3c93ebf540327b8d1cfb5827631c126caf2ff7b
[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 import android.support.v7.preference.CheckBoxPreference;
29
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;
37
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Map.Entry;
43
44 @SearchIndexable
45 public class ZenModeSettings extends ZenModeSettingsBase {
46     private static final String KEY_SOUND = "zen_effect_sound";
47     @Override
48     public void onResume() {
49         super.onResume();
50         CheckBoxPreference soundPreference =
51                 (CheckBoxPreference) getPreferenceScreen().findPreference(KEY_SOUND);
52         if (soundPreference != null) {
53             soundPreference.setChecked(true);
54         }
55     }
56
57     @Override
58     protected int getPreferenceScreenResId() {
59         return R.xml.zen_mode_settings;
60     }
61
62     @Override
63     public int getMetricsCategory() {
64         return MetricsEvent.NOTIFICATION_ZEN_MODE;
65     }
66
67     @Override
68     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
69         return buildPreferenceControllers(context, getLifecycle(), getFragmentManager());
70     }
71
72     @Override
73     public int getHelpResource() {
74         return R.string.help_uri_interruptions;
75     }
76
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,
83                 fragmentManager));
84         controllers.add(new ZenModeAutomationPreferenceController(context));
85         controllers.add(new ZenModeButtonPreferenceController(context, lifecycle, fragmentManager));
86         controllers.add(new ZenModeSettingsFooterPreferenceController(context, lifecycle));
87         return controllers;
88     }
89
90     public static class SummaryBuilder {
91
92         private Context mContext;
93
94         public SummaryBuilder(Context context) {
95             mContext = context;
96         }
97
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,
108         };
109
110         String getBehaviorSettingSummary(Policy policy, int zenMode) {
111             List<String> enabledCategories = getEnabledCategories(policy);
112
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());
126             } else {
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));
133             }
134         }
135
136         String getSoundSummary() {
137             int zenMode = NotificationManager.from(mContext).getZenMode();
138
139             if (zenMode != Settings.Global.ZEN_MODE_OFF) {
140                 ZenModeConfig config = NotificationManager.from(mContext).getZenModeConfig();
141                 String description = ZenModeConfig.getDescription(mContext, true, config, false);
142
143                 if (description == null) {
144                     return mContext.getString(R.string.zen_mode_sound_summary_on);
145                 } else {
146                     return mContext.getString(R.string.zen_mode_sound_summary_on_with_info,
147                             description);
148                 }
149             } else {
150                 final int count = getEnabledAutomaticRulesCount();
151                 if (count > 0) {
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,
155                                     count, count));
156                 }
157
158                 return mContext.getString(R.string.zen_mode_sound_summary_off);
159             }
160         }
161
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));
167             }
168             if (Policy.areAnyScreenOnEffectsSuppressed(policy.suppressedVisualEffects)) {
169                 blockedStrings.add(mContext.getResources().getString(
170                         R.string.zen_mode_block_effect_summary_screen_on));
171             }
172
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);
178             } else {
179                 return mContext.getResources().getString(R.string.join_two_unrelated_items,
180                         blockedStrings.get(0), blockedStrings.get(1));
181             }
182         }
183
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);
189         }
190
191         @VisibleForTesting
192         int getEnabledAutomaticRulesCount() {
193             int count = 0;
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()) {
200                         count++;
201                     }
202                 }
203             }
204             return count;
205         }
206
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));
227                         } else {
228                             enabledCategories.add(mContext.getString(
229                                     R.string.zen_mode_selected_messages));
230                         }
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));
235                         } else {
236                             enabledCategories.add(mContext.getString(
237                                     R.string.zen_mode_selected_callers));
238                         }
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));
244                         }
245                     }
246                 }
247             }
248             return enabledCategories;
249         }
250
251         private boolean isCategoryEnabled(Policy policy, int categoryType) {
252             return (policy.priorityCategories & categoryType) != 0;
253         }
254
255         private boolean isEffectSuppressed(Policy policy, int effect) {
256             return (policy.suppressedVisualEffects & effect) != 0;
257         }
258     }
259
260     /**
261      * For Search.
262      */
263     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
264             new BaseSearchIndexProvider() {
265
266                 @Override
267                 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
268                         boolean enabled) {
269                     final SearchIndexableResource sir = new SearchIndexableResource(context);
270                     sir.xmlResId = R.xml.zen_mode_settings;
271                     return Arrays.asList(sir);
272                 }
273
274                 @Override
275                 public List<String> getNonIndexableKeys(Context context) {
276                     List<String> keys = super.getNonIndexableKeys(context);
277                     keys.add(ZenModeDurationPreferenceController.KEY);
278                     keys.add(ZenModeButtonPreferenceController.KEY);
279                     return keys;
280                 }
281
282                 @Override
283                 public List<AbstractPreferenceController> createPreferenceControllers(Context
284                         context) {
285                     return buildPreferenceControllers(context, null, null);
286                 }
287             };
288 }