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.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;
27 import com.android.settings.R;
29 public class ZenModeBackend {
31 protected static final String ZEN_MODE_FROM_ANYONE = "zen_mode_from_anyone";
33 protected static final String ZEN_MODE_FROM_CONTACTS = "zen_mode_from_contacts";
35 protected static final String ZEN_MODE_FROM_STARRED = "zen_mode_from_starred";
37 protected static final String ZEN_MODE_FROM_NONE = "zen_mode_from_none";
38 protected static final int SOURCE_NONE = -1;
40 private static ZenModeBackend sInstance;
42 protected int mZenMode;
43 /** gets policy last set by updatePolicy **/
44 protected NotificationManager.Policy mPolicy;
45 private final NotificationManager mNotificationManager;
47 private String TAG = "ZenModeSettingsBackend";
48 private final Context mContext;
50 public static ZenModeBackend getInstance(Context context) {
51 if (sInstance == null) {
52 sInstance = new ZenModeBackend(context);
57 public ZenModeBackend(Context context) {
59 mNotificationManager = (NotificationManager) context.getSystemService(
60 Context.NOTIFICATION_SERVICE);
65 protected void updatePolicy() {
66 if (mNotificationManager != null) {
67 mPolicy = mNotificationManager.getNotificationPolicy();
71 protected void updateZenMode() {
72 mZenMode = Settings.Global.getInt(mContext.getContentResolver(),
73 Settings.Global.ZEN_MODE, mZenMode);
76 protected boolean setZenRule(String id, AutomaticZenRule rule) {
77 return NotificationManager.from(mContext).updateAutomaticZenRule(id, rule);
80 protected void setZenMode(int zenMode) {
81 NotificationManager.from(mContext).setZenMode(zenMode, null, TAG);
85 protected int getZenMode() {
86 mZenMode = Settings.Global.getInt(mContext.getContentResolver(),
87 Settings.Global.ZEN_MODE, mZenMode);
91 protected boolean isPriorityCategoryEnabled(int categoryType) {
92 return (mPolicy.priorityCategories & categoryType) != 0;
95 protected int getNewPriorityCategories(boolean allow, int categoryType) {
96 int priorityCategories = mPolicy.priorityCategories;
98 priorityCategories |= categoryType;
100 priorityCategories &= ~categoryType;
102 return priorityCategories;
105 protected int getPriorityCallSenders() {
106 if (isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) {
107 return mPolicy.priorityCallSenders;
113 protected int getPriorityMessageSenders() {
114 if (isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)) {
115 return mPolicy.priorityMessageSenders;
120 protected void saveVisualEffectsPolicy(int category, boolean canBypass) {
121 int suppressedEffects = getNewSuppressedEffects(!canBypass, category);
122 savePolicy(mPolicy.priorityCategories, mPolicy.priorityCallSenders,
123 mPolicy.priorityMessageSenders, suppressedEffects);
126 protected void saveSoundPolicy(int category, boolean allow) {
127 int priorityCategories = getNewPriorityCategories(allow, category);
128 savePolicy(priorityCategories, mPolicy.priorityCallSenders,
129 mPolicy.priorityMessageSenders, mPolicy.suppressedVisualEffects);
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);
140 protected int getNewSuppressedEffects(boolean suppress, int effectType) {
141 int effects = mPolicy.suppressedVisualEffects;
143 effects |= effectType;
145 effects &= ~effectType;
150 protected boolean isEffectAllowed(int effect) {
151 return (mPolicy.suppressedVisualEffects & effect) == 0;
154 protected void saveSenders(int category, int val) {
155 int priorityCallSenders = getPriorityCallSenders();
156 int priorityMessagesSenders = getPriorityMessageSenders();
157 int categorySenders = getPrioritySenders(category);
159 final boolean allowSenders = val != SOURCE_NONE;
160 final int allowSendersFrom = val == SOURCE_NONE ? categorySenders : val;
162 String stringCategory = "";
163 if (category == NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) {
164 stringCategory = "Calls";
165 priorityCallSenders = allowSendersFrom;
168 if (category == NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) {
169 stringCategory = "Messages";
170 priorityMessagesSenders = allowSendersFrom;
173 savePolicy(getNewPriorityCategories(allowSenders, category),
174 priorityCallSenders, priorityMessagesSenders, mPolicy.suppressedVisualEffects);
176 if (ZenModeSettingsBase.DEBUG) Log.d(TAG, "onPrefChange allow" +
177 stringCategory + "=" + allowSenders + " allow" + stringCategory + "From="
178 + ZenModeConfig.sourceToString(allowSendersFrom));
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);
187 int prioritySenders = getPrioritySenders(category);
188 return getKeyFromSetting(isPriorityCategoryEnabled(category)
189 ? prioritySenders : SOURCE_NONE);
193 private int getPrioritySenders(int category) {
194 int categorySenders = -1;
196 if (category == NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) {
197 return getPriorityCallSenders();
200 if (category == NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) {
201 return getPriorityMessageSenders();
204 return categorySenders;
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;
217 return ZEN_MODE_FROM_NONE;
221 protected int getContactsSummary(int category) {
222 int contactType = -1;
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;
230 if (category == NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) {
231 if (isPriorityCategoryEnabled(category)) {
232 contactType = getPriorityMessageSenders();
234 } else if (category == NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) {
235 if (isPriorityCategoryEnabled(category)) {
236 contactType = getPriorityCallSenders();
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;
249 return R.string.zen_mode_from_none;
253 protected static int getSettingFromPrefKey(String 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:
267 public boolean removeZenRule(String ruleId) {
268 return NotificationManager.from(mContext).removeAutomaticZenRule(ruleId);
271 protected String addZenRule(AutomaticZenRule rule) {
273 String id = NotificationManager.from(mContext).addAutomaticZenRule(rule);
274 NotificationManager.from(mContext).getAutomaticZenRule(id);
276 } catch (Exception e) {