OSDN Git Service

Zen automatic rule page ui changes
[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 isPriorityCategoryEnabled(int categoryType) {
92         return (mPolicy.priorityCategories & categoryType) != 0;
93     }
94
95     protected int getNewPriorityCategories(boolean allow, int categoryType) {
96         int priorityCategories = mPolicy.priorityCategories;
97         if (allow) {
98             priorityCategories |= categoryType;
99         } else {
100             priorityCategories &= ~categoryType;
101         }
102         return priorityCategories;
103     }
104
105     protected int getPriorityCallSenders() {
106         if (isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) {
107             return mPolicy.priorityCallSenders;
108         }
109
110         return SOURCE_NONE;
111     }
112
113     protected int getPriorityMessageSenders() {
114         if (isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)) {
115             return mPolicy.priorityMessageSenders;
116         }
117         return SOURCE_NONE;
118     }
119
120     protected void saveVisualEffectsPolicy(int category, boolean canBypass) {
121         int suppressedEffects = getNewSuppressedEffects(!canBypass, category);
122         savePolicy(mPolicy.priorityCategories, mPolicy.priorityCallSenders,
123                 mPolicy.priorityMessageSenders, suppressedEffects);
124     }
125
126     protected void saveSoundPolicy(int category, boolean allow) {
127         int priorityCategories = getNewPriorityCategories(allow, category);
128         savePolicy(priorityCategories, mPolicy.priorityCallSenders,
129                 mPolicy.priorityMessageSenders, mPolicy.suppressedVisualEffects);
130     }
131
132     protected void savePolicy(int priorityCategories, int priorityCallSenders,
133             int priorityMessageSenders, int suppressedVisualEffects) {
134         mPolicy = new NotificationManager.Policy(priorityCategories, priorityCallSenders,
135                 priorityMessageSenders,
136                 suppressedVisualEffects);
137         mNotificationManager.setNotificationPolicy(mPolicy);
138     }
139
140     protected int getNewSuppressedEffects(boolean suppress, int effectType) {
141         int effects = mPolicy.suppressedVisualEffects;
142         if (suppress) {
143             effects |= effectType;
144         } else {
145             effects &= ~effectType;
146         }
147         return effects;
148     }
149
150     protected boolean isEffectAllowed(int effect) {
151         return (mPolicy.suppressedVisualEffects & effect) == 0;
152     }
153
154     protected void saveSenders(int category, int val) {
155         int priorityCallSenders = getPriorityCallSenders();
156         int priorityMessagesSenders = getPriorityMessageSenders();
157         int categorySenders = getPrioritySenders(category);
158
159         final boolean allowSenders = val != SOURCE_NONE;
160         final int allowSendersFrom = val == SOURCE_NONE ? categorySenders : val;
161
162         String stringCategory = "";
163         if (category == NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) {
164             stringCategory = "Calls";
165             priorityCallSenders = allowSendersFrom;
166         }
167
168         if (category == NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) {
169             stringCategory = "Messages";
170             priorityMessagesSenders = allowSendersFrom;
171         }
172
173         savePolicy(getNewPriorityCategories(allowSenders, category),
174             priorityCallSenders, priorityMessagesSenders, mPolicy.suppressedVisualEffects);
175
176         if (ZenModeSettingsBase.DEBUG) Log.d(TAG, "onPrefChange allow" +
177                 stringCategory + "=" + allowSenders + " allow" + stringCategory + "From="
178                 + ZenModeConfig.sourceToString(allowSendersFrom));
179     }
180
181     protected String getSendersKey(int category) {
182         switch (getZenMode()) {
183             case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
184             case Settings.Global.ZEN_MODE_ALARMS:
185                 return getKeyFromSetting(SOURCE_NONE);
186             default:
187                 int prioritySenders = getPrioritySenders(category);
188                 return getKeyFromSetting(isPriorityCategoryEnabled(category)
189                         ? prioritySenders : SOURCE_NONE);
190             }
191     }
192
193     private int getPrioritySenders(int category) {
194         int categorySenders = -1;
195
196         if (category == NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) {
197             return getPriorityCallSenders();
198         }
199
200         if (category == NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) {
201             return getPriorityMessageSenders();
202         }
203
204         return categorySenders;
205     }
206
207     protected static String getKeyFromSetting(int contactType) {
208         switch (contactType) {
209             case NotificationManager.Policy.PRIORITY_SENDERS_ANY:
210                 return ZEN_MODE_FROM_ANYONE;
211             case NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS:
212                 return ZEN_MODE_FROM_CONTACTS;
213             case NotificationManager.Policy.PRIORITY_SENDERS_STARRED:
214                 return ZEN_MODE_FROM_STARRED;
215             case SOURCE_NONE:
216             default:
217                 return ZEN_MODE_FROM_NONE;
218         }
219     }
220
221     protected int getContactsSummary(int category) {
222         int contactType = -1;
223
224         // SOURCE_NONE can be used when in total silence or alarms only
225         // (policy is based on user's preferences but the UI displayed is based on zenMode)
226         if (category == SOURCE_NONE) {
227             return R.string.zen_mode_from_none;
228         }
229
230         if (category == NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) {
231             if (isPriorityCategoryEnabled(category)) {
232                 contactType = getPriorityMessageSenders();
233             }
234         } else if (category == NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) {
235             if (isPriorityCategoryEnabled(category)) {
236                 contactType = getPriorityCallSenders();
237             }
238         }
239
240         switch (contactType) {
241             case NotificationManager.Policy.PRIORITY_SENDERS_ANY:
242                 return R.string.zen_mode_from_anyone;
243             case NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS:
244                 return  R.string.zen_mode_from_contacts;
245             case NotificationManager.Policy.PRIORITY_SENDERS_STARRED:
246                 return  R.string.zen_mode_from_starred;
247             case SOURCE_NONE:
248             default:
249                 return R.string.zen_mode_from_none;
250         }
251     }
252
253     protected static int getSettingFromPrefKey(String key) {
254         switch (key) {
255             case ZEN_MODE_FROM_ANYONE:
256                 return NotificationManager.Policy.PRIORITY_SENDERS_ANY;
257             case ZEN_MODE_FROM_CONTACTS:
258                 return NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS;
259             case ZEN_MODE_FROM_STARRED:
260                 return NotificationManager.Policy.PRIORITY_SENDERS_STARRED;
261             case ZEN_MODE_FROM_NONE:
262             default:
263                 return SOURCE_NONE;
264         }
265     }
266
267     public boolean removeZenRule(String ruleId) {
268         return NotificationManager.from(mContext).removeAutomaticZenRule(ruleId);
269     }
270
271     protected String addZenRule(AutomaticZenRule rule) {
272         try {
273             String id = NotificationManager.from(mContext).addAutomaticZenRule(rule);
274             NotificationManager.from(mContext).getAutomaticZenRule(id);
275             return id;
276         } catch (Exception e) {
277             return null;
278         }
279     }
280 }