summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/notification/ZenModeSettings.java
blob: b3c93ebf540327b8d1cfb5827631c126caf2ff7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.notification;

import android.app.AutomaticZenRule;
import android.app.FragmentManager;
import android.app.NotificationManager;
import android.app.NotificationManager.Policy;
import android.content.Context;
import android.provider.SearchIndexableResource;
import android.provider.Settings;
import android.service.notification.ZenModeConfig;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.CheckBoxPreference;

import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.search.SearchIndexable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

@SearchIndexable
public class ZenModeSettings extends ZenModeSettingsBase {
    private static final String KEY_SOUND = "zen_effect_sound";
    @Override
    public void onResume() {
        super.onResume();
        CheckBoxPreference soundPreference =
                (CheckBoxPreference) getPreferenceScreen().findPreference(KEY_SOUND);
        if (soundPreference != null) {
            soundPreference.setChecked(true);
        }
    }

    @Override
    protected int getPreferenceScreenResId() {
        return R.xml.zen_mode_settings;
    }

    @Override
    public int getMetricsCategory() {
        return MetricsEvent.NOTIFICATION_ZEN_MODE;
    }

    @Override
    protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
        return buildPreferenceControllers(context, getLifecycle(), getFragmentManager());
    }

    @Override
    public int getHelpResource() {
        return R.string.help_uri_interruptions;
    }

    private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
            Lifecycle lifecycle, FragmentManager fragmentManager) {
        List<AbstractPreferenceController> controllers = new ArrayList<>();
        controllers.add(new ZenModeBehaviorPreferenceController(context, lifecycle));
        controllers.add(new ZenModeBlockedEffectsPreferenceController(context, lifecycle));
        controllers.add(new ZenModeDurationPreferenceController(context, lifecycle,
                fragmentManager));
        controllers.add(new ZenModeAutomationPreferenceController(context));
        controllers.add(new ZenModeButtonPreferenceController(context, lifecycle, fragmentManager));
        controllers.add(new ZenModeSettingsFooterPreferenceController(context, lifecycle));
        return controllers;
    }

    public static class SummaryBuilder {

        private Context mContext;

        public SummaryBuilder(Context context) {
            mContext = context;
        }

        // these should match NotificationManager.Policy#ALL_PRIORITY_CATEGORIES
        private static final int[] ALL_PRIORITY_CATEGORIES = {
                Policy.PRIORITY_CATEGORY_ALARMS,
                Policy.PRIORITY_CATEGORY_MEDIA,
                Policy.PRIORITY_CATEGORY_SYSTEM,
                Policy.PRIORITY_CATEGORY_REMINDERS,
                Policy.PRIORITY_CATEGORY_EVENTS,
                Policy.PRIORITY_CATEGORY_MESSAGES,
                Policy.PRIORITY_CATEGORY_CALLS,
                Policy.PRIORITY_CATEGORY_REPEAT_CALLERS,
        };

        String getBehaviorSettingSummary(Policy policy, int zenMode) {
            List<String> enabledCategories = getEnabledCategories(policy);

            int numCategories = enabledCategories.size();
            if (numCategories == 0) {
                return mContext.getString(R.string.zen_mode_no_exceptions);
            } else if (numCategories == 1) {
                return enabledCategories.get(0);
            } else if (numCategories == 2) {
                return mContext.getString(R.string.join_two_items, enabledCategories.get(0),
                        enabledCategories.get(1).toLowerCase());
            } else if (numCategories == 3){
                String secondaryText = mContext.getString(R.string.join_two_unrelated_items,
                        enabledCategories.get(0), enabledCategories.get(1).toLowerCase());
                return mContext.getString(R.string.join_many_items_last, secondaryText,
                        enabledCategories.get(2).toLowerCase());
            } else {
                String secondaryText = mContext.getString(R.string.join_many_items_middle,
                        enabledCategories.get(0), enabledCategories.get(1).toLowerCase());
                secondaryText = mContext.getString(R.string.join_many_items_middle, secondaryText,
                        enabledCategories.get(2).toLowerCase());
                return mContext.getString(R.string.join_many_items_last, secondaryText,
                        mContext.getString(R.string.zen_mode_other_options));
            }
        }

        String getSoundSummary() {
            int zenMode = NotificationManager.from(mContext).getZenMode();

            if (zenMode != Settings.Global.ZEN_MODE_OFF) {
                ZenModeConfig config = NotificationManager.from(mContext).getZenModeConfig();
                String description = ZenModeConfig.getDescription(mContext, true, config, false);

                if (description == null) {
                    return mContext.getString(R.string.zen_mode_sound_summary_on);
                } else {
                    return mContext.getString(R.string.zen_mode_sound_summary_on_with_info,
                            description);
                }
            } else {
                final int count = getEnabledAutomaticRulesCount();
                if (count > 0) {
                    return mContext.getString(R.string.zen_mode_sound_summary_off_with_info,
                            mContext.getResources().getQuantityString(
                                    R.plurals.zen_mode_sound_summary_summary_off_info,
                                    count, count));
                }

                return mContext.getString(R.string.zen_mode_sound_summary_off);
            }
        }

        String getBlockedEffectsSummary(Policy policy) {
            List<String> blockedStrings = new ArrayList<>();
            if (Policy.areAnyScreenOffEffectsSuppressed(policy.suppressedVisualEffects)) {
                blockedStrings.add(mContext.getResources().getString(
                        R.string.zen_mode_block_effect_summary_screen_off));
            }
            if (Policy.areAnyScreenOnEffectsSuppressed(policy.suppressedVisualEffects)) {
                blockedStrings.add(mContext.getResources().getString(
                        R.string.zen_mode_block_effect_summary_screen_on));
            }

            if (blockedStrings.size() == 0) {
                return mContext.getResources().getString(
                        R.string.zen_mode_block_effect_summary_none);
            } else if (blockedStrings.size() == 1) {
                return blockedStrings.get(0);
            } else {
                return mContext.getResources().getString(R.string.join_two_unrelated_items,
                        blockedStrings.get(0), blockedStrings.get(1));
            }
        }

        String getAutomaticRulesSummary() {
            final int count = getEnabledAutomaticRulesCount();
            return count == 0 ? mContext.getString(R.string.zen_mode_settings_summary_off)
                : mContext.getResources().getQuantityString(
                    R.plurals.zen_mode_settings_summary_on, count, count);
        }

        @VisibleForTesting
        int getEnabledAutomaticRulesCount() {
            int count = 0;
            final Map<String, AutomaticZenRule> ruleMap =
                NotificationManager.from(mContext).getAutomaticZenRules();
            if (ruleMap != null) {
                for (Entry<String, AutomaticZenRule> ruleEntry : ruleMap.entrySet()) {
                    final AutomaticZenRule rule = ruleEntry.getValue();
                    if (rule != null && rule.isEnabled()) {
                        count++;
                    }
                }
            }
            return count;
        }

        private List<String> getEnabledCategories(Policy policy) {
            List<String> enabledCategories = new ArrayList<>();
            for (int category : ALL_PRIORITY_CATEGORIES) {
                if (isCategoryEnabled(policy, category)) {
                    if (category == Policy.PRIORITY_CATEGORY_ALARMS) {
                        enabledCategories.add(mContext.getString(R.string.zen_mode_alarms));
                    } else if (category == Policy.PRIORITY_CATEGORY_MEDIA) {
                        enabledCategories.add(mContext.getString(
                                R.string.zen_mode_media));
                    } else if (category == Policy.PRIORITY_CATEGORY_SYSTEM) {
                        enabledCategories.add(mContext.getString(
                                R.string.zen_mode_system));
                    } else if (category == Policy.PRIORITY_CATEGORY_REMINDERS) {
                        enabledCategories.add(mContext.getString(R.string.zen_mode_reminders));
                    } else if (category == Policy.PRIORITY_CATEGORY_EVENTS) {
                        enabledCategories.add(mContext.getString(R.string.zen_mode_events));
                    } else if (category == Policy.PRIORITY_CATEGORY_MESSAGES) {
                        if (policy.priorityMessageSenders == Policy.PRIORITY_SENDERS_ANY) {
                            enabledCategories.add(mContext.getString(
                                    R.string.zen_mode_all_messages));
                        } else {
                            enabledCategories.add(mContext.getString(
                                    R.string.zen_mode_selected_messages));
                        }
                    } else if (category == Policy.PRIORITY_CATEGORY_CALLS) {
                        if (policy.priorityCallSenders == Policy.PRIORITY_SENDERS_ANY) {
                            enabledCategories.add(mContext.getString(
                                    R.string.zen_mode_all_callers));
                        } else {
                            enabledCategories.add(mContext.getString(
                                    R.string.zen_mode_selected_callers));
                        }
                    } else if (category == Policy.PRIORITY_CATEGORY_REPEAT_CALLERS) {
                        if (!enabledCategories.contains(mContext.getString(
                                R.string.zen_mode_all_callers))) {
                            enabledCategories.add(mContext.getString(
                                    R.string.zen_mode_repeat_callers));
                        }
                    }
                }
            }
            return enabledCategories;
        }

        private boolean isCategoryEnabled(Policy policy, int categoryType) {
            return (policy.priorityCategories & categoryType) != 0;
        }

        private boolean isEffectSuppressed(Policy policy, int effect) {
            return (policy.suppressedVisualEffects & effect) != 0;
        }
    }

    /**
     * For Search.
     */
    public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
            new BaseSearchIndexProvider() {

                @Override
                public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
                        boolean enabled) {
                    final SearchIndexableResource sir = new SearchIndexableResource(context);
                    sir.xmlResId = R.xml.zen_mode_settings;
                    return Arrays.asList(sir);
                }

                @Override
                public List<String> getNonIndexableKeys(Context context) {
                    List<String> keys = super.getNonIndexableKeys(context);
                    keys.add(ZenModeDurationPreferenceController.KEY);
                    keys.add(ZenModeButtonPreferenceController.KEY);
                    return keys;
                }

                @Override
                public List<AbstractPreferenceController> createPreferenceControllers(Context
                        context) {
                    return buildPreferenceControllers(context, null, null);
                }
            };
}