blob: 2aaeb9d1370fa478bcd1a85a608055f485e50a6f [file] [log] [blame]
John Spurlockb2278d62015-04-07 12:47:12 -04001/**
2 * Copyright (c) 2015, 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
17package com.android.server.notification;
18
19import android.app.Notification;
20import android.content.ComponentName;
21import android.content.Context;
22import android.media.AudioAttributes;
23import android.media.AudioManager;
24import android.os.Bundle;
25import android.os.UserHandle;
26import android.provider.Settings.Global;
27import android.provider.Settings.Secure;
28import android.service.notification.ZenModeConfig;
29import android.telecom.TelecomManager;
John Spurlock1d7d2242015-04-10 08:10:22 -040030import android.util.ArrayMap;
John Spurlockb2278d62015-04-07 12:47:12 -040031import android.util.Slog;
32
John Spurlock1d7d2242015-04-10 08:10:22 -040033import java.io.PrintWriter;
34import java.util.Date;
John Spurlockb2278d62015-04-07 12:47:12 -040035import java.util.Objects;
36
37public class ZenModeFiltering {
38 private static final String TAG = ZenModeHelper.TAG;
39 private static final boolean DEBUG = ZenModeHelper.DEBUG;
40
John Spurlock1d7d2242015-04-10 08:10:22 -040041 static final RepeatCallers REPEAT_CALLERS = new RepeatCallers();
42
John Spurlockb2278d62015-04-07 12:47:12 -040043 private final Context mContext;
44
45 private ComponentName mDefaultPhoneApp;
46
47 public ZenModeFiltering(Context context) {
48 mContext = context;
49 }
50
John Spurlock1d7d2242015-04-10 08:10:22 -040051 public void dump(PrintWriter pw, String prefix) {
52 pw.print(prefix); pw.print("mDefaultPhoneApp="); pw.println(mDefaultPhoneApp);
53 pw.print(prefix); pw.print("RepeatCallers.mThresholdMinutes=");
54 pw.println(REPEAT_CALLERS.mThresholdMinutes);
55 synchronized (REPEAT_CALLERS) {
56 if (!REPEAT_CALLERS.mCalls.isEmpty()) {
57 pw.print(prefix); pw.println("RepeatCallers.mCalls=");
58 for (int i = 0; i < REPEAT_CALLERS.mCalls.size(); i++) {
59 pw.print(prefix); pw.print(" ");
60 pw.print(REPEAT_CALLERS.mCalls.keyAt(i));
61 pw.print(" at ");
62 pw.println(ts(REPEAT_CALLERS.mCalls.valueAt(i)));
63 }
64 }
65 }
66 }
67
68 private static String ts(long time) {
69 return new Date(time) + " (" + time + ")";
John Spurlockb2278d62015-04-07 12:47:12 -040070 }
71
72 /**
73 * @param extras extras of the notification with EXTRA_PEOPLE populated
74 * @param contactsTimeoutMs timeout in milliseconds to wait for contacts response
75 * @param timeoutAffinity affinity to return when the timeout specified via
76 * <code>contactsTimeoutMs</code> is hit
77 */
John Spurlock1d7d2242015-04-10 08:10:22 -040078 public static boolean matchesCallFilter(Context context, int zen, ZenModeConfig config,
79 UserHandle userHandle, Bundle extras, ValidateNotificationPeople validator,
80 int contactsTimeoutMs, float timeoutAffinity) {
John Spurlockb2278d62015-04-07 12:47:12 -040081 if (zen == Global.ZEN_MODE_NO_INTERRUPTIONS) return false; // nothing gets through
82 if (zen == Global.ZEN_MODE_ALARMS) return false; // not an alarm
83 if (zen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) {
John Spurlock1d7d2242015-04-10 08:10:22 -040084 if (config.allowRepeatCallers && REPEAT_CALLERS.isRepeat(context, extras)) return true;
85 if (!config.allowCalls) return false; // no other calls get through
John Spurlockb2278d62015-04-07 12:47:12 -040086 if (validator != null) {
87 final float contactAffinity = validator.getContactAffinity(userHandle, extras,
88 contactsTimeoutMs, timeoutAffinity);
89 return audienceMatches(config, contactAffinity);
90 }
91 }
92 return true;
93 }
94
John Spurlock1d7d2242015-04-10 08:10:22 -040095 private static Bundle extras(NotificationRecord record) {
96 return record != null && record.sbn != null && record.sbn.getNotification() != null
97 ? record.sbn.getNotification().extras : null;
98 }
99
John Spurlockb2278d62015-04-07 12:47:12 -0400100 public boolean shouldIntercept(int zen, ZenModeConfig config, NotificationRecord record) {
101 if (isSystem(record)) {
102 return false;
103 }
104 switch (zen) {
105 case Global.ZEN_MODE_NO_INTERRUPTIONS:
106 // #notevenalarms
107 ZenLog.traceIntercepted(record, "none");
108 return true;
109 case Global.ZEN_MODE_ALARMS:
110 if (isAlarm(record)) {
111 // Alarms only
112 return false;
113 }
114 ZenLog.traceIntercepted(record, "alarmsOnly");
115 return true;
116 case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS:
117 if (isAlarm(record)) {
118 // Alarms are always priority
119 return false;
120 }
121 // allow user-prioritized packages through in priority mode
122 if (record.getPackagePriority() == Notification.PRIORITY_MAX) {
123 ZenLog.traceNotIntercepted(record, "priorityApp");
124 return false;
125 }
126 if (isCall(record)) {
John Spurlock1d7d2242015-04-10 08:10:22 -0400127 if (config.allowRepeatCallers
128 && REPEAT_CALLERS.isRepeat(mContext, extras(record))) {
129 ZenLog.traceNotIntercepted(record, "repeatCaller");
130 return false;
131 }
John Spurlockb2278d62015-04-07 12:47:12 -0400132 if (!config.allowCalls) {
133 ZenLog.traceIntercepted(record, "!allowCalls");
134 return true;
135 }
136 return shouldInterceptAudience(config, record);
137 }
138 if (isMessage(record)) {
139 if (!config.allowMessages) {
140 ZenLog.traceIntercepted(record, "!allowMessages");
141 return true;
142 }
143 return shouldInterceptAudience(config, record);
144 }
145 if (isEvent(record)) {
146 if (!config.allowEvents) {
147 ZenLog.traceIntercepted(record, "!allowEvents");
148 return true;
149 }
150 return false;
151 }
152 if (isReminder(record)) {
153 if (!config.allowReminders) {
154 ZenLog.traceIntercepted(record, "!allowReminders");
155 return true;
156 }
157 return false;
158 }
159 ZenLog.traceIntercepted(record, "!priority");
160 return true;
161 default:
162 return false;
163 }
164 }
165
166 private static boolean shouldInterceptAudience(ZenModeConfig config,
167 NotificationRecord record) {
168 if (!audienceMatches(config, record.getContactAffinity())) {
169 ZenLog.traceIntercepted(record, "!audienceMatches");
170 return true;
171 }
172 return false;
173 }
174
175 private static boolean isSystem(NotificationRecord record) {
176 return record.isCategory(Notification.CATEGORY_SYSTEM);
177 }
178
179 private static boolean isAlarm(NotificationRecord record) {
180 return record.isCategory(Notification.CATEGORY_ALARM)
181 || record.isAudioStream(AudioManager.STREAM_ALARM)
182 || record.isAudioAttributesUsage(AudioAttributes.USAGE_ALARM);
183 }
184
185 private static boolean isEvent(NotificationRecord record) {
186 return record.isCategory(Notification.CATEGORY_EVENT);
187 }
188
189 private static boolean isReminder(NotificationRecord record) {
190 return record.isCategory(Notification.CATEGORY_REMINDER);
191 }
192
193 public boolean isCall(NotificationRecord record) {
194 return record != null && (isDefaultPhoneApp(record.sbn.getPackageName())
195 || record.isCategory(Notification.CATEGORY_CALL));
196 }
197
198 private boolean isDefaultPhoneApp(String pkg) {
199 if (mDefaultPhoneApp == null) {
200 final TelecomManager telecomm =
201 (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
202 mDefaultPhoneApp = telecomm != null ? telecomm.getDefaultPhoneApp() : null;
203 if (DEBUG) Slog.d(TAG, "Default phone app: " + mDefaultPhoneApp);
204 }
205 return pkg != null && mDefaultPhoneApp != null
206 && pkg.equals(mDefaultPhoneApp.getPackageName());
207 }
208
209 @SuppressWarnings("deprecation")
210 private boolean isDefaultMessagingApp(NotificationRecord record) {
211 final int userId = record.getUserId();
212 if (userId == UserHandle.USER_NULL || userId == UserHandle.USER_ALL) return false;
213 final String defaultApp = Secure.getStringForUser(mContext.getContentResolver(),
214 Secure.SMS_DEFAULT_APPLICATION, userId);
215 return Objects.equals(defaultApp, record.sbn.getPackageName());
216 }
217
218 private boolean isMessage(NotificationRecord record) {
219 return record.isCategory(Notification.CATEGORY_MESSAGE) || isDefaultMessagingApp(record);
220 }
221
222 private static boolean audienceMatches(ZenModeConfig config, float contactAffinity) {
223 switch (config.allowFrom) {
224 case ZenModeConfig.SOURCE_ANYONE:
225 return true;
226 case ZenModeConfig.SOURCE_CONTACT:
227 return contactAffinity >= ValidateNotificationPeople.VALID_CONTACT;
228 case ZenModeConfig.SOURCE_STAR:
229 return contactAffinity >= ValidateNotificationPeople.STARRED_CONTACT;
230 default:
231 Slog.w(TAG, "Encountered unknown source: " + config.allowFrom);
232 return true;
233 }
234 }
John Spurlock1d7d2242015-04-10 08:10:22 -0400235
236 private static class RepeatCallers {
237 private final ArrayMap<String, Long> mCalls = new ArrayMap<>();
238 private int mThresholdMinutes;
239
240 private synchronized boolean isRepeat(Context context, Bundle extras) {
241 if (mThresholdMinutes <= 0) {
242 mThresholdMinutes = context.getResources().getInteger(com.android.internal.R.integer
243 .config_zen_repeat_callers_threshold);
244 }
245 if (mThresholdMinutes <= 0 || extras == null) return false;
246 final String peopleString = peopleString(extras);
247 if (peopleString == null) return false;
248 final long now = System.currentTimeMillis();
249 final int N = mCalls.size();
250 for (int i = N - 1; i >= 0; i--) {
251 final long time = mCalls.valueAt(i);
252 if (time > now || (now - time) > mThresholdMinutes * 1000 * 60) {
253 mCalls.removeAt(i);
254 }
255 }
256 final boolean isRepeat = mCalls.containsKey(peopleString);
257 mCalls.put(peopleString, now);
258 return isRepeat;
259 }
260
261 private static String peopleString(Bundle extras) {
262 final String[] extraPeople = ValidateNotificationPeople.getExtraPeople(extras);
263 if (extraPeople == null || extraPeople.length == 0) return null;
264 final StringBuilder sb = new StringBuilder();
265 for (int i = 0; i < extraPeople.length; i++) {
266 String extraPerson = extraPeople[i];
267 if (extraPerson == null) continue;
268 extraPerson = extraPerson.trim();
269 if (extraPerson.isEmpty()) continue;
270 if (sb.length() > 0) {
271 sb.append('|');
272 }
273 sb.append(extraPerson);
274 }
275 return sb.length() == 0 ? null : sb.toString();
276 }
277 }
278
John Spurlockb2278d62015-04-07 12:47:12 -0400279}