2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.notification;
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;
29 import com.android.settings.R;
31 public class ZenModeBackend {
33 protected static final String ZEN_MODE_FROM_ANYONE = "zen_mode_from_anyone";
35 protected static final String ZEN_MODE_FROM_CONTACTS = "zen_mode_from_contacts";
37 protected static final String ZEN_MODE_FROM_STARRED = "zen_mode_from_starred";
39 protected static final String ZEN_MODE_FROM_NONE = "zen_mode_from_none";
40 protected static final int SOURCE_NONE = -1;
42 private static ZenModeBackend sInstance;
44 protected int mZenMode;
45 /** gets policy last set by updatePolicy **/
46 protected NotificationManager.Policy mPolicy;
47 private final NotificationManager mNotificationManager;
49 private String TAG = "ZenModeSettingsBackend";
50 private final Context mContext;
52 public static ZenModeBackend getInstance(Context context) {
53 if (sInstance == null) {
54 sInstance = new ZenModeBackend(context);
59 public ZenModeBackend(Context context) {
61 mNotificationManager = (NotificationManager) context.getSystemService(
62 Context.NOTIFICATION_SERVICE);
67 protected void updatePolicy() {
68 if (mNotificationManager != null) {
69 mPolicy = mNotificationManager.getNotificationPolicy();
73 protected void updateZenMode() {
74 mZenMode = Settings.Global.getInt(mContext.getContentResolver(),
75 Settings.Global.ZEN_MODE, mZenMode);
78 protected boolean setZenRule(String id, AutomaticZenRule rule) {
79 return NotificationManager.from(mContext).updateAutomaticZenRule(id, rule);
82 protected void setZenMode(int zenMode) {
83 NotificationManager.from(mContext).setZenMode(zenMode, null, TAG);
84 mZenMode = getZenMode();
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,
92 mZenMode = getZenMode();
95 protected int getZenMode() {
96 mZenMode = Settings.Global.getInt(mContext.getContentResolver(),
97 Settings.Global.ZEN_MODE, mZenMode);
101 protected boolean isVisualEffectSuppressed(int visualEffect) {
102 return (mPolicy.suppressedVisualEffects & visualEffect) != 0;
105 protected boolean isPriorityCategoryEnabled(int categoryType) {
106 return (mPolicy.priorityCategories & categoryType) != 0;
109 protected int getNewPriorityCategories(boolean allow, int categoryType) {
110 int priorityCategories = mPolicy.priorityCategories;
112 priorityCategories |= categoryType;
114 priorityCategories &= ~categoryType;
116 return priorityCategories;
119 protected int getPriorityCallSenders() {
120 if (isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) {
121 return mPolicy.priorityCallSenders;
127 protected int getPriorityMessageSenders() {
128 if (isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)) {
129 return mPolicy.priorityMessageSenders;
134 protected void saveVisualEffectsPolicy(int category, boolean suppress) {
135 int suppressedEffects = getNewSuppressedEffects(suppress, category);
136 savePolicy(mPolicy.priorityCategories, mPolicy.priorityCallSenders,
137 mPolicy.priorityMessageSenders, suppressedEffects);
140 protected void saveSoundPolicy(int category, boolean allow) {
141 int priorityCategories = getNewPriorityCategories(allow, category);
142 savePolicy(priorityCategories, mPolicy.priorityCallSenders,
143 mPolicy.priorityMessageSenders, mPolicy.suppressedVisualEffects);
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);
154 protected int getNewSuppressedEffects(boolean suppress, int effectType) {
155 int effects = mPolicy.suppressedVisualEffects;
157 effects |= effectType;
159 effects &= ~effectType;
164 protected boolean isEffectAllowed(int effect) {
165 return (mPolicy.suppressedVisualEffects & effect) == 0;
168 protected void saveSenders(int category, int val) {
169 int priorityCallSenders = getPriorityCallSenders();
170 int priorityMessagesSenders = getPriorityMessageSenders();
171 int categorySenders = getPrioritySenders(category);
173 final boolean allowSenders = val != SOURCE_NONE;
174 final int allowSendersFrom = val == SOURCE_NONE ? categorySenders : val;
176 String stringCategory = "";
177 if (category == NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) {
178 stringCategory = "Calls";
179 priorityCallSenders = allowSendersFrom;
182 if (category == NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) {
183 stringCategory = "Messages";
184 priorityMessagesSenders = allowSendersFrom;
187 savePolicy(getNewPriorityCategories(allowSenders, category),
188 priorityCallSenders, priorityMessagesSenders, mPolicy.suppressedVisualEffects);
190 if (ZenModeSettingsBase.DEBUG) Log.d(TAG, "onPrefChange allow" +
191 stringCategory + "=" + allowSenders + " allow" + stringCategory + "From="
192 + ZenModeConfig.sourceToString(allowSendersFrom));
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);
201 int prioritySenders = getPrioritySenders(category);
202 return getKeyFromSetting(isPriorityCategoryEnabled(category)
203 ? prioritySenders : SOURCE_NONE);
207 private int getPrioritySenders(int category) {
208 int categorySenders = -1;
210 if (category == NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) {
211 return getPriorityCallSenders();
214 if (category == NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) {
215 return getPriorityMessageSenders();
218 return categorySenders;
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;
231 return ZEN_MODE_FROM_NONE;
235 protected int getContactsSummary(int category) {
236 int contactType = -1;
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;
244 if (category == NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) {
245 if (isPriorityCategoryEnabled(category)) {
246 contactType = getPriorityMessageSenders();
248 } else if (category == NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) {
249 if (isPriorityCategoryEnabled(category)) {
250 contactType = getPriorityCallSenders();
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;
263 return R.string.zen_mode_from_none;
267 protected static int getSettingFromPrefKey(String 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:
281 public boolean removeZenRule(String ruleId) {
282 return NotificationManager.from(mContext).removeAutomaticZenRule(ruleId);
285 protected String addZenRule(AutomaticZenRule rule) {
287 String id = NotificationManager.from(mContext).addAutomaticZenRule(rule);
288 NotificationManager.from(mContext).getAutomaticZenRule(id);
290 } catch (Exception e) {