OSDN Git Service

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