OSDN Git Service

88174170df0127a3ed1a3dd15721aa9f29bb124e
[android-x86/packages-apps-Settings.git] / src / com / android / settings / notification / ZenModeBackend.java
1 /*
2  * Copyright (C) 2017 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.NotificationManager;
21 import android.content.Context;
22 import android.provider.Settings;
23 import android.service.notification.ZenModeConfig;
24 import android.support.annotation.VisibleForTesting;
25 import android.util.Log;
26
27 import com.android.settings.R;
28
29 public class ZenModeBackend {
30     @VisibleForTesting
31     protected static final String ZEN_MODE_FROM_ANYONE = "zen_mode_from_anyone";
32     @VisibleForTesting
33     protected static final String ZEN_MODE_FROM_CONTACTS = "zen_mode_from_contacts";
34     @VisibleForTesting
35     protected static final String ZEN_MODE_FROM_STARRED = "zen_mode_from_starred";
36     @VisibleForTesting
37     protected static final String ZEN_MODE_FROM_NONE = "zen_mode_from_none";
38     protected static final int SOURCE_NONE = -1;
39
40     private static ZenModeBackend sInstance;
41
42     protected int mZenMode;
43     /** gets policy last set by updatePolicy **/
44     protected NotificationManager.Policy mPolicy;
45     private final NotificationManager mNotificationManager;
46
47     private String TAG = "ZenModeSettingsBackend";
48     private final Context mContext;
49
50     public static ZenModeBackend getInstance(Context context) {
51         if (sInstance == null) {
52             sInstance = new ZenModeBackend(context);
53         }
54         return sInstance;
55     }
56
57     public ZenModeBackend(Context context) {
58         mContext = context;
59         mNotificationManager = (NotificationManager) context.getSystemService(
60                 Context.NOTIFICATION_SERVICE);
61         updateZenMode();
62         updatePolicy();
63     }
64
65     protected void updatePolicy() {
66         if (mNotificationManager != null) {
67             mPolicy = mNotificationManager.getNotificationPolicy();
68         }
69     }
70
71     protected void updateZenMode() {
72         mZenMode = Settings.Global.getInt(mContext.getContentResolver(),
73                 Settings.Global.ZEN_MODE, mZenMode);
74     }
75
76     protected boolean setZenRule(String id, AutomaticZenRule rule) {
77         return NotificationManager.from(mContext).updateAutomaticZenRule(id, rule);
78     }
79
80     protected void setZenMode(int zenMode) {
81         NotificationManager.from(mContext).setZenMode(zenMode, null, TAG);
82         mZenMode = zenMode;
83     }
84
85     protected int getZenMode() {
86         mZenMode = Settings.Global.getInt(mContext.getContentResolver(),
87                 Settings.Global.ZEN_MODE, mZenMode);
88         return mZenMode;
89     }
90
91     protected boolean isVisualEffectSuppressed(int visualEffect) {
92         return (mPolicy.suppressedVisualEffects & visualEffect) != 0;
93     }
94
95     protected boolean isPriorityCategoryEnabled(int categoryType) {
96         return (mPolicy.priorityCategories & categoryType) != 0;
97     }
98
99     protected int getNewPriorityCategories(boolean allow, int categoryType) {
100         int priorityCategories = mPolicy.priorityCategories;
101         if (allow) {
102             priorityCategories |= categoryType;
103         } else {
104             priorityCategories &= ~categoryType;
105         }
106         return priorityCategories;
107     }
108
109     protected int getPriorityCallSenders() {
110         if (isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) {
111             return mPolicy.priorityCallSenders;
112         }
113
114         return SOURCE_NONE;
115     }
116
117     protected int getPriorityMessageSenders() {
118         if (isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)) {
119             return mPolicy.priorityMessageSenders;
120         }
121         return SOURCE_NONE;
122     }
123
124     protected void saveVisualEffectsPolicy(int category, boolean suppress) {
125         int suppressedEffects = getNewSuppressedEffects(suppress, category);
126         savePolicy(mPolicy.priorityCategories, mPolicy.priorityCallSenders,
127                 mPolicy.priorityMessageSenders, suppressedEffects);
128     }
129
130     protected void saveSoundPolicy(int category, boolean allow) {
131         int priorityCategories = getNewPriorityCategories(allow, category);
132         savePolicy(priorityCategories, mPolicy.priorityCallSenders,
133                 mPolicy.priorityMessageSenders, mPolicy.suppressedVisualEffects);
134     }
135
136     protected void savePolicy(int priorityCategories, int priorityCallSenders,
137             int priorityMessageSenders, int suppressedVisualEffects) {
138         mPolicy = new NotificationManager.Policy(priorityCategories, priorityCallSenders,
139                 priorityMessageSenders,
140                 suppressedVisualEffects);
141         mNotificationManager.setNotificationPolicy(mPolicy);
142     }
143
144     protected int getNewSuppressedEffects(boolean suppress, int effectType) {
145         int effects = mPolicy.suppressedVisualEffects;
146         if (suppress) {
147             effects |= effectType;
148         } else {
149             effects &= ~effectType;
150         }
151         return effects;
152     }
153
154     protected boolean isEffectAllowed(int effect) {
155         return (mPolicy.suppressedVisualEffects & effect) == 0;
156     }
157
158     protected void saveSenders(int category, int val) {
159         int priorityCallSenders = getPriorityCallSenders();
160         int priorityMessagesSenders = getPriorityMessageSenders();
161         int categorySenders = getPrioritySenders(category);
162
163         final boolean allowSenders = val != SOURCE_NONE;
164         final int allowSendersFrom = val == SOURCE_NONE ? categorySenders : val;
165
166         String stringCategory = "";
167         if (category == NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) {
168             stringCategory = "Calls";
169             priorityCallSenders = allowSendersFrom;
170         }
171
172         if (category == NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) {
173             stringCategory = "Messages";
174             priorityMessagesSenders = allowSendersFrom;
175         }
176
177         savePolicy(getNewPriorityCategories(allowSenders, category),
178             priorityCallSenders, priorityMessagesSenders, mPolicy.suppressedVisualEffects);
179
180         if (ZenModeSettingsBase.DEBUG) Log.d(TAG, "onPrefChange allow" +
181                 stringCategory + "=" + allowSenders + " allow" + stringCategory + "From="
182                 + ZenModeConfig.sourceToString(allowSendersFrom));
183     }
184
185     protected String getSendersKey(int category) {
186         switch (getZenMode()) {
187             case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
188             case Settings.Global.ZEN_MODE_ALARMS:
189                 return getKeyFromSetting(SOURCE_NONE);
190             default:
191                 int prioritySenders = getPrioritySenders(category);
192                 return getKeyFromSetting(isPriorityCategoryEnabled(category)
193                         ? prioritySenders : SOURCE_NONE);
194             }
195     }
196
197     private int getPrioritySenders(int category) {
198         int categorySenders = -1;
199
200         if (category == NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) {
201             return getPriorityCallSenders();
202         }
203
204         if (category == NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) {
205             return getPriorityMessageSenders();
206         }
207
208         return categorySenders;
209     }
210
211     protected static String getKeyFromSetting(int contactType) {
212         switch (contactType) {
213             case NotificationManager.Policy.PRIORITY_SENDERS_ANY:
214                 return ZEN_MODE_FROM_ANYONE;
215             case NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS:
216                 return ZEN_MODE_FROM_CONTACTS;
217             case NotificationManager.Policy.PRIORITY_SENDERS_STARRED:
218                 return ZEN_MODE_FROM_STARRED;
219             case SOURCE_NONE:
220             default:
221                 return ZEN_MODE_FROM_NONE;
222         }
223     }
224
225     protected int getContactsSummary(int category) {
226         int contactType = -1;
227
228         // SOURCE_NONE can be used when in total silence or alarms only
229         // (policy is based on user's preferences but the UI displayed is based on zenMode)
230         if (category == SOURCE_NONE) {
231             return R.string.zen_mode_from_none;
232         }
233
234         if (category == NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) {
235             if (isPriorityCategoryEnabled(category)) {
236                 contactType = getPriorityMessageSenders();
237             }
238         } else if (category == NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) {
239             if (isPriorityCategoryEnabled(category)) {
240                 contactType = getPriorityCallSenders();
241             }
242         }
243
244         switch (contactType) {
245             case NotificationManager.Policy.PRIORITY_SENDERS_ANY:
246                 return R.string.zen_mode_from_anyone;
247             case NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS:
248                 return  R.string.zen_mode_from_contacts;
249             case NotificationManager.Policy.PRIORITY_SENDERS_STARRED:
250                 return  R.string.zen_mode_from_starred;
251             case SOURCE_NONE:
252             default:
253                 return R.string.zen_mode_from_none;
254         }
255     }
256
257     protected static int getSettingFromPrefKey(String key) {
258         switch (key) {
259             case ZEN_MODE_FROM_ANYONE:
260                 return NotificationManager.Policy.PRIORITY_SENDERS_ANY;
261             case ZEN_MODE_FROM_CONTACTS:
262                 return NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS;
263             case ZEN_MODE_FROM_STARRED:
264                 return NotificationManager.Policy.PRIORITY_SENDERS_STARRED;
265             case ZEN_MODE_FROM_NONE:
266             default:
267                 return SOURCE_NONE;
268         }
269     }
270
271     public boolean removeZenRule(String ruleId) {
272         return NotificationManager.from(mContext).removeAutomaticZenRule(ruleId);
273     }
274
275     protected String addZenRule(AutomaticZenRule rule) {
276         try {
277             String id = NotificationManager.from(mContext).addAutomaticZenRule(rule);
278             NotificationManager.from(mContext).getAutomaticZenRule(id);
279             return id;
280         } catch (Exception e) {
281             return null;
282         }
283     }
284 }