OSDN Git Service

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