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 isVisualEffectSuppressed(int visualEffect) {
92 return (mPolicy.suppressedVisualEffects & visualEffect) != 0;
95 protected boolean isPriorityCategoryEnabled(int categoryType) {
96 return (mPolicy.priorityCategories & categoryType) != 0;
99 protected int getNewPriorityCategories(boolean allow, int categoryType) {
100 int priorityCategories = mPolicy.priorityCategories;
102 priorityCategories |= categoryType;
104 priorityCategories &= ~categoryType;
106 return priorityCategories;
109 protected int getPriorityCallSenders() {
110 if (isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) {
111 return mPolicy.priorityCallSenders;
117 protected int getPriorityMessageSenders() {
118 if (isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)) {
119 return mPolicy.priorityMessageSenders;
124 protected void saveVisualEffectsPolicy(int category, boolean suppress) {
125 int suppressedEffects = getNewSuppressedEffects(suppress, category);
126 savePolicy(mPolicy.priorityCategories, mPolicy.priorityCallSenders,
127 mPolicy.priorityMessageSenders, suppressedEffects);
130 protected void saveSoundPolicy(int category, boolean allow) {
131 int priorityCategories = getNewPriorityCategories(allow, category);
132 savePolicy(priorityCategories, mPolicy.priorityCallSenders,
133 mPolicy.priorityMessageSenders, mPolicy.suppressedVisualEffects);
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);
144 protected int getNewSuppressedEffects(boolean suppress, int effectType) {
145 int effects = mPolicy.suppressedVisualEffects;
147 effects |= effectType;
149 effects &= ~effectType;
154 protected boolean isEffectAllowed(int effect) {
155 return (mPolicy.suppressedVisualEffects & effect) == 0;
158 protected void saveSenders(int category, int val) {
159 int priorityCallSenders = getPriorityCallSenders();
160 int priorityMessagesSenders = getPriorityMessageSenders();
161 int categorySenders = getPrioritySenders(category);
163 final boolean allowSenders = val != SOURCE_NONE;
164 final int allowSendersFrom = val == SOURCE_NONE ? categorySenders : val;
166 String stringCategory = "";
167 if (category == NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) {
168 stringCategory = "Calls";
169 priorityCallSenders = allowSendersFrom;
172 if (category == NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) {
173 stringCategory = "Messages";
174 priorityMessagesSenders = allowSendersFrom;
177 savePolicy(getNewPriorityCategories(allowSenders, category),
178 priorityCallSenders, priorityMessagesSenders, mPolicy.suppressedVisualEffects);
180 if (ZenModeSettingsBase.DEBUG) Log.d(TAG, "onPrefChange allow" +
181 stringCategory + "=" + allowSenders + " allow" + stringCategory + "From="
182 + ZenModeConfig.sourceToString(allowSendersFrom));
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);
191 int prioritySenders = getPrioritySenders(category);
192 return getKeyFromSetting(isPriorityCategoryEnabled(category)
193 ? prioritySenders : SOURCE_NONE);
197 private int getPrioritySenders(int category) {
198 int categorySenders = -1;
200 if (category == NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) {
201 return getPriorityCallSenders();
204 if (category == NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) {
205 return getPriorityMessageSenders();
208 return categorySenders;
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;
221 return ZEN_MODE_FROM_NONE;
225 protected int getContactsSummary(int category) {
226 int contactType = -1;
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;
234 if (category == NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) {
235 if (isPriorityCategoryEnabled(category)) {
236 contactType = getPriorityMessageSenders();
238 } else if (category == NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) {
239 if (isPriorityCategoryEnabled(category)) {
240 contactType = getPriorityCallSenders();
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;
253 return R.string.zen_mode_from_none;
257 protected static int getSettingFromPrefKey(String 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:
271 public boolean removeZenRule(String ruleId) {
272 return NotificationManager.from(mContext).removeAutomaticZenRule(ruleId);
275 protected String addZenRule(AutomaticZenRule rule) {
277 String id = NotificationManager.from(mContext).addAutomaticZenRule(rule);
278 NotificationManager.from(mContext).getAutomaticZenRule(id);
280 } catch (Exception e) {