OSDN Git Service

Merge tag 'android-6.0.1_r74' into HEAD
[android-x86/packages-apps-Settings.git] / src / com / android / settings / notification / ZenModeSettings.java
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * Copyright (C) 2016 The CyanogenMod Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package com.android.settings.notification;
19
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.os.Bundle;
23 import android.preference.Preference;
24 import android.preference.PreferenceScreen;
25 import android.util.SparseArray;
26
27 import com.android.internal.logging.MetricsLogger;
28 import com.android.settings.R;
29 import com.android.settings.Utils;
30 import com.android.settings.search.BaseSearchIndexProvider;
31 import com.android.settings.search.Indexable;
32 import com.android.settings.search.SearchIndexableRaw;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 public class ZenModeSettings extends ZenModeSettingsBase implements Indexable {
38     private static final String KEY_PRIORITY_SETTINGS = "priority_settings";
39     private static final String KEY_AUTOMATION_SETTINGS = "automation_settings";
40     private static final String KEY_ZEN_ACCESS = "manage_zen_access";
41
42     private Preference mPrioritySettings;
43     private Preference mZenAccess;
44
45     @Override
46     public void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48
49         addPreferencesFromResource(R.xml.zen_mode_settings);
50         final PreferenceScreen root = getPreferenceScreen();
51
52         mPrioritySettings = root.findPreference(KEY_PRIORITY_SETTINGS);
53         if (!isScheduleSupported(mContext)) {
54             removePreference(KEY_AUTOMATION_SETTINGS);
55         }
56
57         mZenAccess = findPreference(KEY_ZEN_ACCESS);
58         refreshZenAccess();
59     }
60
61     @Override
62     public void onResume() {
63         super.onResume();
64         updateControls();
65         refreshZenAccess();
66     }
67
68     @Override
69     protected int getMetricsCategory() {
70         return MetricsLogger.NOTIFICATION_ZEN_MODE;
71     }
72
73     @Override
74     protected void onZenModeChanged() {
75         updateControls();
76     }
77
78     @Override
79     protected void onZenModeConfigChanged() {
80         updateControls();
81     }
82
83     private void updateControls() {
84         updatePrioritySettingsSummary();
85     }
86
87     private void updatePrioritySettingsSummary() {
88         final ArrayList<String> items = new ArrayList<>();
89         items.add(getString(R.string.zen_mode_alarms));
90         if (mConfig.allowReminders) {
91             items.add(getString(R.string.zen_mode_summary_reminders));
92         }
93         if (mConfig.allowEvents) {
94             items.add(getString(R.string.zen_mode_summary_events));
95         }
96         if (mConfig.allowCalls || mConfig.allowRepeatCallers) {
97             items.add(getString(R.string.zen_mode_summary_selected_callers));
98         }
99         if (mConfig.allowMessages) {
100             items.add(getString(R.string.zen_mode_summary_selected_messages));
101         }
102         mPrioritySettings.setSummary(Utils.join(getResources(), items));
103     }
104
105     private static SparseArray<String> allKeyTitles(Context context) {
106         final SparseArray<String> rt = new SparseArray<String>();
107         rt.put(R.string.zen_mode_priority_settings_title, KEY_PRIORITY_SETTINGS);
108         rt.put(R.string.zen_mode_automation_settings_title, KEY_AUTOMATION_SETTINGS);
109         return rt;
110     }
111
112     @Override
113     protected int getHelpResource() {
114         return R.string.help_uri_interruptions;
115     }
116
117     // Enable indexing of searchable data
118     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
119         new BaseSearchIndexProvider() {
120
121             @Override
122             public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
123                 final SparseArray<String> keyTitles = allKeyTitles(context);
124                 final int N = keyTitles.size();
125                 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>(N);
126                 final Resources res = context.getResources();
127                 for (int i = 0; i < N; i++) {
128                     final SearchIndexableRaw data = new SearchIndexableRaw(context);
129                     data.key = keyTitles.valueAt(i);
130                     data.title = res.getString(keyTitles.keyAt(i));
131                     data.screenTitle = res.getString(R.string.zen_mode_settings_title);
132                     result.add(data);
133                 }
134                 return result;
135             }
136
137             @Override
138             public List<String> getNonIndexableKeys(Context context) {
139                 final ArrayList<String> rt = new ArrayList<String>();
140                 if (!isScheduleSupported(context)) {
141                     rt.add(KEY_AUTOMATION_SETTINGS);
142                 }
143                 return rt;
144             }
145         };
146
147     // === Zen access ===
148
149     private void refreshZenAccess() {
150         // noop for now
151     }
152 }