blob: 39cd2b5e901d8c84817d3725c86f0458ffe997e7 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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 android.app;
18
Julia Reynolds0edb50c2016-02-26 14:08:25 -050019import android.annotation.IntDef;
John Spurlock1fc476d2015-04-14 16:05:20 -040020import android.annotation.NonNull;
John Spurlockb4782522014-08-22 14:54:46 -040021import android.annotation.SdkConstant;
Christoph Studer4600f9b2014-07-22 22:44:43 +020022import android.app.Notification.Builder;
John Spurlockb4782522014-08-22 14:54:46 -040023import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
Dan Sandler994349c2015-04-15 11:02:54 -040025import android.content.pm.ParceledListSlice;
Dan Sandlerd63f9322015-05-06 15:18:49 -040026import android.graphics.drawable.Icon;
John Spurlockb2278d62015-04-07 12:47:12 -040027import android.net.Uri;
Dan Sandler4e787062015-06-17 15:09:48 -040028import android.os.Build;
John Spurlock2b122f42014-08-27 16:29:47 -040029import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.Handler;
31import android.os.IBinder;
John Spurlock1fc476d2015-04-14 16:05:20 -040032import android.os.Parcel;
33import android.os.Parcelable;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070034import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.ServiceManager;
Jeff Sharkeya14acd22013-04-02 18:27:45 -070036import android.os.StrictMode;
Dianne Hackborn41203752012-08-31 14:05:51 -070037import android.os.UserHandle;
John Spurlockb2278d62015-04-07 12:47:12 -040038import android.provider.Settings.Global;
Chris Wren5ab5c742016-05-10 15:32:23 -040039import android.service.notification.NotificationListenerService.Ranking;
Dan Sandler994349c2015-04-15 11:02:54 -040040import android.service.notification.StatusBarNotification;
John Spurlockcdb57ae2015-02-11 19:04:11 -050041import android.service.notification.ZenModeConfig;
John Spurlock80774932015-05-07 17:38:50 -040042import android.util.ArraySet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.util.Log;
44
Julia Reynolds0edb50c2016-02-26 14:08:25 -050045import java.lang.annotation.Retention;
46import java.lang.annotation.RetentionPolicy;
Julia Reynolds361e82d32016-02-26 18:19:49 -050047import java.util.HashMap;
Chris Wren5ab5c742016-05-10 15:32:23 -040048import java.util.List;
Julia Reynolds361e82d32016-02-26 18:19:49 -050049import java.util.Map;
John Spurlock1fc476d2015-04-14 16:05:20 -040050import java.util.Objects;
51
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052/**
53 * Class to notify the user of events that happen. This is how you tell
54 * the user that something has happened in the background. {@more}
55 *
56 * Notifications can take different forms:
57 * <ul>
58 * <li>A persistent icon that goes in the status bar and is accessible
59 * through the launcher, (when the user selects it, a designated Intent
60 * can be launched),</li>
61 * <li>Turning on or flashing LEDs on the device, or</li>
62 * <li>Alerting the user by flashing the backlight, playing a sound,
63 * or vibrating.</li>
64 * </ul>
65 *
66 * <p>
Peter Collingbourneb97c3492010-10-13 20:04:52 +010067 * Each of the notify methods takes an int id parameter and optionally a
68 * {@link String} tag parameter, which may be {@code null}. These parameters
69 * are used to form a pair (tag, id), or ({@code null}, id) if tag is
70 * unspecified. This pair identifies this notification from your app to the
71 * system, so that pair should be unique within your app. If you call one
72 * of the notify methods with a (tag, id) pair that is currently active and
73 * a new set of notification parameters, it will be updated. For example,
74 * if you pass a new status bar icon, the old icon in the status bar will
75 * be replaced with the new one. This is also the same tag and id you pass
76 * to the {@link #cancel(int)} or {@link #cancel(String, int)} method to clear
77 * this notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 *
79 * <p>
80 * You do not instantiate this class directly; instead, retrieve it through
81 * {@link android.content.Context#getSystemService}.
82 *
Joe Fernandez558459f2011-10-13 16:47:36 -070083 * <div class="special reference">
84 * <h3>Developer Guides</h3>
85 * <p>For a guide to creating notifications, read the
86 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
87 * developer guide.</p>
88 * </div>
89 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 * @see android.app.Notification
91 * @see android.content.Context#getSystemService
92 */
93public class NotificationManager
94{
95 private static String TAG = "NotificationManager";
Joe Onorato43a17652011-04-06 19:22:23 -070096 private static boolean localLOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
John Spurlockb4782522014-08-22 14:54:46 -040098 /**
99 * Intent that is broadcast when the state of {@link #getEffectsSuppressor()} changes.
100 * This broadcast is only sent to registered receivers.
101 *
102 * @hide
103 */
104 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
105 public static final String ACTION_EFFECTS_SUPPRESSOR_CHANGED
106 = "android.os.action.ACTION_EFFECTS_SUPPRESSOR_CHANGED";
107
John Spurlock1fc476d2015-04-14 16:05:20 -0400108 /**
John Spurlock7c74f782015-06-04 13:01:42 -0400109 * Intent that is broadcast when the state of {@link #isNotificationPolicyAccessGranted()}
110 * changes.
111 *
112 * This broadcast is only sent to registered receivers, and only to the apps that have changed.
113 */
114 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
115 public static final String ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED
116 = "android.app.action.NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED";
117
118 /**
John Spurlock1fc476d2015-04-14 16:05:20 -0400119 * Intent that is broadcast when the state of getNotificationPolicy() changes.
120 * This broadcast is only sent to registered receivers.
121 */
122 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
123 public static final String ACTION_NOTIFICATION_POLICY_CHANGED
124 = "android.app.action.NOTIFICATION_POLICY_CHANGED";
125
John Spurlock80774932015-05-07 17:38:50 -0400126 /**
127 * Intent that is broadcast when the state of getCurrentInterruptionFilter() changes.
128 * This broadcast is only sent to registered receivers.
129 */
130 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
131 public static final String ACTION_INTERRUPTION_FILTER_CHANGED
132 = "android.app.action.INTERRUPTION_FILTER_CHANGED";
133
134 /**
Jason Monka9927322015-12-13 16:22:37 -0500135 * Intent that is broadcast when the state of getCurrentInterruptionFilter() changes.
136 * @hide
137 */
138 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
139 public static final String ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL
140 = "android.app.action.INTERRUPTION_FILTER_CHANGED_INTERNAL";
141
Julia Reynolds0edb50c2016-02-26 14:08:25 -0500142 /** @hide */
143 @IntDef({INTERRUPTION_FILTER_NONE, INTERRUPTION_FILTER_PRIORITY, INTERRUPTION_FILTER_ALARMS,
144 INTERRUPTION_FILTER_ALL, INTERRUPTION_FILTER_UNKNOWN})
145 @Retention(RetentionPolicy.SOURCE)
146 public @interface InterruptionFilter {}
147
Jason Monka9927322015-12-13 16:22:37 -0500148 /**
John Spurlock80774932015-05-07 17:38:50 -0400149 * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
Julia Reynoldsdd0605b2016-04-06 10:26:54 -0400150 * Normal interruption filter - no notifications are suppressed.
John Spurlock80774932015-05-07 17:38:50 -0400151 */
152 public static final int INTERRUPTION_FILTER_ALL = 1;
153
154 /**
155 * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
Julia Reynoldsdd0605b2016-04-06 10:26:54 -0400156 * Priority interruption filter - all notifications are suppressed except those that match
157 * the priority criteria. Some audio streams are muted. See
158 * {@link Policy#priorityCallSenders}, {@link Policy#priorityCategories},
159 * {@link Policy#priorityMessageSenders} to define or query this criteria. Users can
160 * additionally specify packages that can bypass this interruption filter.
John Spurlock80774932015-05-07 17:38:50 -0400161 */
162 public static final int INTERRUPTION_FILTER_PRIORITY = 2;
163
164 /**
165 * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
Julia Reynoldsdd0605b2016-04-06 10:26:54 -0400166 * No interruptions filter - all notifications are suppressed and all audio streams (except
167 * those used for phone calls) and vibrations are muted.
John Spurlock80774932015-05-07 17:38:50 -0400168 */
169 public static final int INTERRUPTION_FILTER_NONE = 3;
170
171 /**
172 * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
Julia Reynoldsdd0605b2016-04-06 10:26:54 -0400173 * Alarms only interruption filter - all notifications except those of category
174 * {@link Notification#CATEGORY_ALARM} are suppressed. Some audio streams are muted.
John Spurlock80774932015-05-07 17:38:50 -0400175 */
176 public static final int INTERRUPTION_FILTER_ALARMS = 4;
177
178 /** {@link #getCurrentInterruptionFilter() Interruption filter} constant - returned when
179 * the value is unavailable for any reason.
180 */
181 public static final int INTERRUPTION_FILTER_UNKNOWN = 0;
182
Chris Wren5ab5c742016-05-10 15:32:23 -0400183 /** @hide */
184 @IntDef({VISIBILITY_NO_OVERRIDE, IMPORTANCE_UNSPECIFIED, IMPORTANCE_NONE,
185 IMPORTANCE_MIN, IMPORTANCE_LOW, IMPORTANCE_DEFAULT, IMPORTANCE_HIGH,
186 IMPORTANCE_MAX})
187 @Retention(RetentionPolicy.SOURCE)
188 public @interface Importance {}
189
190 /** Value signifying that the user has not expressed a per-app visibility override value.
191 * @hide */
192 public static final int VISIBILITY_NO_OVERRIDE = -1000;
193 /**
194 * Value signifying that the user has not expressed an importance.
195 *
196 * This value is for persisting preferences, and should never be associated with
197 * an actual notification.
198 */
199 public static final int IMPORTANCE_UNSPECIFIED = -1000;
200
201 /**
202 * A notification with no importance: shows nowhere, is blocked.
203 */
204 public static final int IMPORTANCE_NONE = 0;
205
206 /**
207 * Min notification importance: only shows in the shade, below the fold.
208 */
209 public static final int IMPORTANCE_MIN = 1;
210
211 /**
212 * Low notification importance: shows everywhere, but is not intrusive.
213 */
214 public static final int IMPORTANCE_LOW = 2;
215
216 /**
217 * Default notification importance: shows everywhere, allowed to makes noise,
218 * but does not visually intrude.
219 */
220 public static final int IMPORTANCE_DEFAULT = 3;
221
222 /**
223 * Higher notification importance: shows everywhere, allowed to makes noise and peek.
224 */
225 public static final int IMPORTANCE_HIGH = 4;
226
227 /**
228 * Highest notification importance: shows everywhere, allowed to makes noise, peek, and
229 * use full screen intents.
230 */
231 public static final int IMPORTANCE_MAX = 5;
232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 private static INotificationManager sService;
234
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700235 /** @hide */
236 static public INotificationManager getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 {
238 if (sService != null) {
239 return sService;
240 }
241 IBinder b = ServiceManager.getService("notification");
242 sService = INotificationManager.Stub.asInterface(b);
243 return sService;
244 }
245
246 /*package*/ NotificationManager(Context context, Handler handler)
247 {
248 mContext = context;
249 }
250
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700251 /** {@hide} */
252 public static NotificationManager from(Context context) {
253 return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
254 }
255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500257 * Post a notification to be shown in the status bar. If a notification with
258 * the same id has already been posted by your application and has not yet been canceled, it
259 * will be replaced by the updated information.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 *
261 * @param id An identifier for this notification unique within your
262 * application.
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500263 * @param notification A {@link Notification} object describing what to show the user. Must not
264 * be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 */
266 public void notify(int id, Notification notification)
267 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700268 notify(null, id, notification);
269 }
270
271 /**
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500272 * Post a notification to be shown in the status bar. If a notification with
273 * the same tag and id has already been posted by your application and has not yet been
274 * canceled, it will be replaced by the updated information.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700275 *
Peter Collingbourneb97c3492010-10-13 20:04:52 +0100276 * @param tag A string identifier for this notification. May be {@code null}.
277 * @param id An identifier for this notification. The pair (tag, id) must be unique
278 * within your application.
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500279 * @param notification A {@link Notification} object describing what to
280 * show the user. Must not be null.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700281 */
282 public void notify(String tag, int id, Notification notification)
283 {
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400284 notifyAsUser(tag, id, notification, new UserHandle(UserHandle.myUserId()));
Dianne Hackborn41203752012-08-31 14:05:51 -0700285 }
286
287 /**
288 * @hide
289 */
290 public void notifyAsUser(String tag, int id, Notification notification, UserHandle user)
291 {
292 int[] idOut = new int[1];
293 INotificationManager service = getService();
294 String pkg = mContext.getPackageName();
Julia Reynoldsda303542015-11-23 14:00:20 -0500295 // Fix the notification as best we can.
296 Notification.addFieldsFromContext(mContext, notification);
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700297 if (notification.sound != null) {
298 notification.sound = notification.sound.getCanonicalUri();
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700299 if (StrictMode.vmFileUriExposureEnabled()) {
Jeff Sharkeyac3be9a2016-02-01 10:39:30 -0700300 notification.sound.checkFileUriExposed("Notification.sound");
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700301 }
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700302 }
Dan Sandlerd63f9322015-05-06 15:18:49 -0400303 fixLegacySmallIcon(notification, pkg);
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400304 if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
305 if (notification.getSmallIcon() == null) {
306 throw new IllegalArgumentException("Invalid notification (no valid small icon): "
307 + notification);
308 }
309 }
Dianne Hackborn41203752012-08-31 14:05:51 -0700310 if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
Adrian Roos184bfe022016-03-03 13:41:44 -0800311 final Notification copy = Builder.maybeCloneStrippedForDelivery(notification);
Dianne Hackborn41203752012-08-31 14:05:51 -0700312 try {
Dianne Hackborn95d78532013-09-11 09:51:14 -0700313 service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id,
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400314 copy, idOut, user.getIdentifier());
Chris Wrena61f1792016-08-04 11:24:42 -0400315 if (localLOGV && id != idOut[0]) {
316 Log.v(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 }
318 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700319 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 }
321 }
322
Dan Sandlerd63f9322015-05-06 15:18:49 -0400323 private void fixLegacySmallIcon(Notification n, String pkg) {
324 if (n.getSmallIcon() == null && n.icon != 0) {
325 n.setSmallIcon(Icon.createWithResource(pkg, n.icon));
326 }
327 }
328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 /**
330 * Cancel a previously shown notification. If it's transient, the view
331 * will be hidden. If it's persistent, it will be removed from the status
332 * bar.
333 */
334 public void cancel(int id)
335 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700336 cancel(null, id);
337 }
338
339 /**
340 * Cancel a previously shown notification. If it's transient, the view
341 * will be hidden. If it's persistent, it will be removed from the status
342 * bar.
343 */
344 public void cancel(String tag, int id)
345 {
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400346 cancelAsUser(tag, id, new UserHandle(UserHandle.myUserId()));
Dianne Hackborn41203752012-08-31 14:05:51 -0700347 }
348
349 /**
350 * @hide
351 */
352 public void cancelAsUser(String tag, int id, UserHandle user)
353 {
354 INotificationManager service = getService();
355 String pkg = mContext.getPackageName();
356 if (localLOGV) Log.v(TAG, pkg + ": cancel(" + id + ")");
357 try {
358 service.cancelNotificationWithTag(pkg, tag, id, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700360 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
362 }
363
364 /**
365 * Cancel all previously shown notifications. See {@link #cancel} for the
366 * detailed behavior.
367 */
368 public void cancelAll()
369 {
370 INotificationManager service = getService();
371 String pkg = mContext.getPackageName();
372 if (localLOGV) Log.v(TAG, pkg + ": cancelAll()");
373 try {
Dianne Hackborn41203752012-08-31 14:05:51 -0700374 service.cancelAllNotifications(pkg, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700376 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
378 }
379
John Spurlockb4782522014-08-22 14:54:46 -0400380 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400381 * Creates a notification channel that notifications can be posted to.
382 */
383 public void createNotificationChannel(NotificationChannel channel) {
384 INotificationManager service = getService();
385 try {
386 service.createNotificationChannel(mContext.getPackageName(), channel);
387 } catch (RemoteException e) {
388 throw e.rethrowFromSystemServer();
389 }
390 }
391
392 /**
393 * Returns the notification channel settings for a given channel id.
394 */
395 public NotificationChannel getNotificationChannel(String channelId) {
396 INotificationManager service = getService();
397 try {
398 return service.getNotificationChannel(mContext.getPackageName(), channelId);
399 } catch (RemoteException e) {
400 throw e.rethrowFromSystemServer();
401 }
402 }
403
404 /**
405 * Returns all notification channels created by the calling app.
406 */
407 public List<NotificationChannel> getNotificationChannels() {
408 INotificationManager service = getService();
409 try {
410 return service.getNotificationChannels(mContext.getPackageName()).getList();
411 } catch (RemoteException e) {
412 throw e.rethrowFromSystemServer();
413 }
414 }
415
416 /**
417 * Updates settings for a given channel.
418 */
419 public void updateNotificationChannel(NotificationChannel channel) {
420 INotificationManager service = getService();
421 try {
422 service.updateNotificationChannel(mContext.getPackageName(), channel);
423 } catch (RemoteException e) {
424 throw e.rethrowFromSystemServer();
425 }
426 }
427
428 /**
429 * Deletes the given notification channel.
430 */
431 public void deleteNotificationChannel(String channelId) {
432 INotificationManager service = getService();
433 try {
434 service.deleteNotificationChannel(mContext.getPackageName(), channelId);
435 } catch (RemoteException e) {
436 throw e.rethrowFromSystemServer();
437 }
438 }
439
440 /**
John Spurlockb4782522014-08-22 14:54:46 -0400441 * @hide
442 */
443 public ComponentName getEffectsSuppressor() {
444 INotificationManager service = getService();
445 try {
446 return service.getEffectsSuppressor();
447 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700448 throw e.rethrowFromSystemServer();
John Spurlockb4782522014-08-22 14:54:46 -0400449 }
450 }
451
John Spurlock2b122f42014-08-27 16:29:47 -0400452 /**
453 * @hide
454 */
455 public boolean matchesCallFilter(Bundle extras) {
456 INotificationManager service = getService();
457 try {
458 return service.matchesCallFilter(extras);
459 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700460 throw e.rethrowFromSystemServer();
John Spurlock2b122f42014-08-27 16:29:47 -0400461 }
462 }
463
John Spurlock530052a2014-11-30 16:26:19 -0500464 /**
465 * @hide
466 */
467 public boolean isSystemConditionProviderEnabled(String path) {
468 INotificationManager service = getService();
469 try {
470 return service.isSystemConditionProviderEnabled(path);
471 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700472 throw e.rethrowFromSystemServer();
John Spurlock530052a2014-11-30 16:26:19 -0500473 }
474 }
475
John Spurlockcdb57ae2015-02-11 19:04:11 -0500476 /**
477 * @hide
478 */
John Spurlockb2278d62015-04-07 12:47:12 -0400479 public void setZenMode(int mode, Uri conditionId, String reason) {
John Spurlockcdb57ae2015-02-11 19:04:11 -0500480 INotificationManager service = getService();
481 try {
John Spurlockb2278d62015-04-07 12:47:12 -0400482 service.setZenMode(mode, conditionId, reason);
John Spurlockcdb57ae2015-02-11 19:04:11 -0500483 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700484 throw e.rethrowFromSystemServer();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500485 }
486 }
487
488 /**
489 * @hide
490 */
John Spurlockb2278d62015-04-07 12:47:12 -0400491 public int getZenMode() {
John Spurlockcdb57ae2015-02-11 19:04:11 -0500492 INotificationManager service = getService();
493 try {
John Spurlockb2278d62015-04-07 12:47:12 -0400494 return service.getZenMode();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500495 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700496 throw e.rethrowFromSystemServer();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500497 }
498 }
499
500 /**
501 * @hide
502 */
John Spurlockb2278d62015-04-07 12:47:12 -0400503 public ZenModeConfig getZenModeConfig() {
John Spurlockcdb57ae2015-02-11 19:04:11 -0500504 INotificationManager service = getService();
505 try {
John Spurlockb2278d62015-04-07 12:47:12 -0400506 return service.getZenModeConfig();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500507 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700508 throw e.rethrowFromSystemServer();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500509 }
John Spurlockcdb57ae2015-02-11 19:04:11 -0500510 }
511
John Spurlock1fc476d2015-04-14 16:05:20 -0400512 /**
Julia Reynolds43b70cd2016-01-14 15:05:34 -0500513 * @hide
514 */
515 public int getRuleInstanceCount(ComponentName owner) {
516 INotificationManager service = getService();
517 try {
518 return service.getRuleInstanceCount(owner);
519 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700520 throw e.rethrowFromSystemServer();
Julia Reynolds43b70cd2016-01-14 15:05:34 -0500521 }
Julia Reynolds43b70cd2016-01-14 15:05:34 -0500522 }
523
524 /**
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400525 * Returns AutomaticZenRules owned by the caller.
526 *
527 * <p>
Julia Reynolds361e82d32016-02-26 18:19:49 -0500528 * Throws a SecurityException if policy access is granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400529 * See {@link #isNotificationPolicyAccessGranted}.
530 */
Julia Reynolds361e82d32016-02-26 18:19:49 -0500531 public Map<String, AutomaticZenRule> getAutomaticZenRules() {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400532 INotificationManager service = getService();
533 try {
Julia Reynolds361e82d32016-02-26 18:19:49 -0500534 List<ZenModeConfig.ZenRule> rules = service.getZenRules();
535 Map<String, AutomaticZenRule> ruleMap = new HashMap<>();
536 for (ZenModeConfig.ZenRule rule : rules) {
537 ruleMap.put(rule.id, new AutomaticZenRule(rule.name, rule.component,
538 rule.conditionId, zenModeToInterruptionFilter(rule.zenMode), rule.enabled,
539 rule.creationTime));
540 }
541 return ruleMap;
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400542 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700543 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400544 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400545 }
546
547 /**
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400548 * Returns the AutomaticZenRule with the given id, if it exists and the caller has access.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400549 *
550 * <p>
Julia Reynolds361e82d32016-02-26 18:19:49 -0500551 * Throws a SecurityException if policy access is granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400552 * See {@link #isNotificationPolicyAccessGranted}.
553 *
554 * <p>
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400555 * Returns null if there are no zen rules that match the given id, or if the calling package
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400556 * doesn't own the matching rule. See {@link AutomaticZenRule#getOwner}.
557 */
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400558 public AutomaticZenRule getAutomaticZenRule(String id) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400559 INotificationManager service = getService();
560 try {
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400561 return service.getAutomaticZenRule(id);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400562 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700563 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400564 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400565 }
566
567 /**
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400568 * Creates the given zen rule.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400569 *
570 * <p>
Julia Reynolds361e82d32016-02-26 18:19:49 -0500571 * Throws a SecurityException if policy access is granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400572 * See {@link #isNotificationPolicyAccessGranted}.
573 *
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400574 * @param automaticZenRule the rule to create.
Julia Reynolds361e82d32016-02-26 18:19:49 -0500575 * @return The id of the newly created rule; null if the rule could not be created.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400576 */
Julia Reynolds361e82d32016-02-26 18:19:49 -0500577 public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400578 INotificationManager service = getService();
579 try {
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400580 return service.addAutomaticZenRule(automaticZenRule);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400581 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700582 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400583 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400584 }
585
586 /**
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400587 * Updates the given zen rule.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400588 *
589 * <p>
Julia Reynolds361e82d32016-02-26 18:19:49 -0500590 * Throws a SecurityException if policy access is granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400591 * See {@link #isNotificationPolicyAccessGranted}.
592 *
593 * <p>
594 * Callers can only update rules that they own. See {@link AutomaticZenRule#getOwner}.
Julia Reynolds361e82d32016-02-26 18:19:49 -0500595 * @param id The id of the rule to update
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400596 * @param automaticZenRule the rule to update.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400597 * @return Whether the rule was successfully updated.
598 */
Julia Reynolds361e82d32016-02-26 18:19:49 -0500599 public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400600 INotificationManager service = getService();
601 try {
Julia Reynolds361e82d32016-02-26 18:19:49 -0500602 return service.updateAutomaticZenRule(id, automaticZenRule);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400603 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700604 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400605 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400606 }
607
608 /**
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400609 * Deletes the automatic zen rule with the given id.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400610 *
611 * <p>
Julia Reynolds361e82d32016-02-26 18:19:49 -0500612 * Throws a SecurityException if policy access is granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400613 * See {@link #isNotificationPolicyAccessGranted}.
614 *
615 * <p>
616 * Callers can only delete rules that they own. See {@link AutomaticZenRule#getOwner}.
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400617 * @param id the id of the rule to delete.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400618 * @return Whether the rule was successfully deleted.
619 */
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400620 public boolean removeAutomaticZenRule(String id) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400621 INotificationManager service = getService();
622 try {
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400623 return service.removeAutomaticZenRule(id);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400624 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700625 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400626 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400627 }
628
629 /**
Julia Reynoldsc8e54e82015-11-30 16:43:05 -0500630 * Deletes all automatic zen rules owned by the given package.
631 *
632 * @hide
633 */
634 public boolean removeAutomaticZenRules(String packageName) {
635 INotificationManager service = getService();
636 try {
637 return service.removeAutomaticZenRules(packageName);
638 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700639 throw e.rethrowFromSystemServer();
Julia Reynoldsc8e54e82015-11-30 16:43:05 -0500640 }
Julia Reynoldsc8e54e82015-11-30 16:43:05 -0500641 }
642
Julia Reynolds0edb50c2016-02-26 14:08:25 -0500643 /**
644 * Returns the user specified importance for notifications from the calling package.
Chris Wren5ab5c742016-05-10 15:32:23 -0400645 *
646 * @return An importance level, such as {@link #IMPORTANCE_DEFAULT}.
Julia Reynolds0edb50c2016-02-26 14:08:25 -0500647 */
Chris Wren5ab5c742016-05-10 15:32:23 -0400648 public @Importance int getImportance() {
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500649 INotificationManager service = getService();
650 try {
Julia Reynoldsef37f282016-02-12 09:11:27 -0500651 return service.getPackageImportance(mContext.getPackageName());
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500652 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700653 throw e.rethrowFromSystemServer();
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500654 }
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500655 }
656
Julia Reynolds0edb50c2016-02-26 14:08:25 -0500657 /**
658 * Returns whether notifications from the calling package are blocked.
659 */
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500660 public boolean areNotificationsEnabled() {
661 INotificationManager service = getService();
662 try {
663 return service.areNotificationsEnabled(mContext.getPackageName());
664 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700665 throw e.rethrowFromSystemServer();
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500666 }
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500667 }
668
Julia Reynoldsc8e54e82015-11-30 16:43:05 -0500669 /**
John Spurlock80774932015-05-07 17:38:50 -0400670 * Checks the ability to read/modify notification policy for the calling package.
John Spurlock1fc476d2015-04-14 16:05:20 -0400671 *
John Spurlock7c74f782015-06-04 13:01:42 -0400672 * <p>
John Spurlock80774932015-05-07 17:38:50 -0400673 * Returns true if the calling package can read/modify notification policy.
John Spurlock7c74f782015-06-04 13:01:42 -0400674 *
675 * <p>
676 * Request policy access by sending the user to the activity that matches the system intent
677 * action {@link android.provider.Settings#ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS}.
678 *
679 * <p>
680 * Use {@link #ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED} to listen for
681 * user grant or denial of this access.
John Spurlock1fc476d2015-04-14 16:05:20 -0400682 */
John Spurlock80774932015-05-07 17:38:50 -0400683 public boolean isNotificationPolicyAccessGranted() {
John Spurlock1fc476d2015-04-14 16:05:20 -0400684 INotificationManager service = getService();
685 try {
John Spurlock80774932015-05-07 17:38:50 -0400686 return service.isNotificationPolicyAccessGranted(mContext.getOpPackageName());
687 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700688 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -0400689 }
John Spurlock80774932015-05-07 17:38:50 -0400690 }
691
692 /** @hide */
693 public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {
694 INotificationManager service = getService();
695 try {
696 return service.isNotificationPolicyAccessGrantedForPackage(pkg);
John Spurlock1fc476d2015-04-14 16:05:20 -0400697 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700698 throw e.rethrowFromSystemServer();
John Spurlock1fc476d2015-04-14 16:05:20 -0400699 }
John Spurlock1fc476d2015-04-14 16:05:20 -0400700 }
701
702 /**
703 * Gets the current notification policy.
704 *
John Spurlock80774932015-05-07 17:38:50 -0400705 * <p>
John Spurlock7c74f782015-06-04 13:01:42 -0400706 * Only available if policy access is granted to this package.
707 * See {@link #isNotificationPolicyAccessGranted}.
John Spurlock1fc476d2015-04-14 16:05:20 -0400708 */
John Spurlock80774932015-05-07 17:38:50 -0400709 public Policy getNotificationPolicy() {
John Spurlock1fc476d2015-04-14 16:05:20 -0400710 INotificationManager service = getService();
711 try {
John Spurlock80774932015-05-07 17:38:50 -0400712 return service.getNotificationPolicy(mContext.getOpPackageName());
John Spurlock1fc476d2015-04-14 16:05:20 -0400713 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700714 throw e.rethrowFromSystemServer();
John Spurlock1fc476d2015-04-14 16:05:20 -0400715 }
John Spurlock1fc476d2015-04-14 16:05:20 -0400716 }
717
718 /**
719 * Sets the current notification policy.
720 *
John Spurlock80774932015-05-07 17:38:50 -0400721 * <p>
John Spurlock7c74f782015-06-04 13:01:42 -0400722 * Only available if policy access is granted to this package.
723 * See {@link #isNotificationPolicyAccessGranted}.
John Spurlock80774932015-05-07 17:38:50 -0400724 *
John Spurlock1fc476d2015-04-14 16:05:20 -0400725 * @param policy The new desired policy.
726 */
John Spurlock80774932015-05-07 17:38:50 -0400727 public void setNotificationPolicy(@NonNull Policy policy) {
John Spurlock1fc476d2015-04-14 16:05:20 -0400728 checkRequired("policy", policy);
729 INotificationManager service = getService();
730 try {
John Spurlock80774932015-05-07 17:38:50 -0400731 service.setNotificationPolicy(mContext.getOpPackageName(), policy);
John Spurlock1fc476d2015-04-14 16:05:20 -0400732 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700733 throw e.rethrowFromSystemServer();
John Spurlock1fc476d2015-04-14 16:05:20 -0400734 }
735 }
736
John Spurlock80774932015-05-07 17:38:50 -0400737 /** @hide */
738 public void setNotificationPolicyAccessGranted(String pkg, boolean granted) {
739 INotificationManager service = getService();
740 try {
741 service.setNotificationPolicyAccessGranted(pkg, granted);
742 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700743 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -0400744 }
745 }
746
747 /** @hide */
748 public ArraySet<String> getPackagesRequestingNotificationPolicyAccess() {
749 INotificationManager service = getService();
750 try {
751 final String[] pkgs = service.getPackagesRequestingNotificationPolicyAccess();
752 if (pkgs != null && pkgs.length > 0) {
753 final ArraySet<String> rt = new ArraySet<>(pkgs.length);
754 for (int i = 0; i < pkgs.length; i++) {
755 rt.add(pkgs[i]);
756 }
757 return rt;
758 }
759 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700760 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -0400761 }
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700762 return new ArraySet<>();
John Spurlock80774932015-05-07 17:38:50 -0400763 }
764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 private Context mContext;
John Spurlock1fc476d2015-04-14 16:05:20 -0400766
767 private static void checkRequired(String name, Object value) {
768 if (value == null) {
769 throw new IllegalArgumentException(name + " is required");
770 }
771 }
772
773 /**
774 * Notification policy configuration. Represents user-preferences for notification
Julia Reynoldscedacef2016-03-04 08:18:47 -0500775 * filtering.
John Spurlock1fc476d2015-04-14 16:05:20 -0400776 */
777 public static class Policy implements android.os.Parcelable {
778 /** Reminder notifications are prioritized. */
779 public static final int PRIORITY_CATEGORY_REMINDERS = 1 << 0;
780 /** Event notifications are prioritized. */
781 public static final int PRIORITY_CATEGORY_EVENTS = 1 << 1;
782 /** Message notifications are prioritized. */
783 public static final int PRIORITY_CATEGORY_MESSAGES = 1 << 2;
784 /** Calls are prioritized. */
785 public static final int PRIORITY_CATEGORY_CALLS = 1 << 3;
786 /** Calls from repeat callers are prioritized. */
787 public static final int PRIORITY_CATEGORY_REPEAT_CALLERS = 1 << 4;
788
789 private static final int[] ALL_PRIORITY_CATEGORIES = {
790 PRIORITY_CATEGORY_REMINDERS,
791 PRIORITY_CATEGORY_EVENTS,
792 PRIORITY_CATEGORY_MESSAGES,
793 PRIORITY_CATEGORY_CALLS,
794 PRIORITY_CATEGORY_REPEAT_CALLERS,
795 };
796
797 /** Any sender is prioritized. */
798 public static final int PRIORITY_SENDERS_ANY = 0;
799 /** Saved contacts are prioritized. */
800 public static final int PRIORITY_SENDERS_CONTACTS = 1;
801 /** Only starred contacts are prioritized. */
802 public static final int PRIORITY_SENDERS_STARRED = 2;
803
804 /** Notification categories to prioritize. Bitmask of PRIORITY_CATEGORY_* constants. */
805 public final int priorityCategories;
806
John Spurlock80774932015-05-07 17:38:50 -0400807 /** Notification senders to prioritize for calls. One of:
John Spurlock1fc476d2015-04-14 16:05:20 -0400808 * PRIORITY_SENDERS_ANY, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED */
John Spurlock80774932015-05-07 17:38:50 -0400809 public final int priorityCallSenders;
John Spurlock1fc476d2015-04-14 16:05:20 -0400810
John Spurlock80774932015-05-07 17:38:50 -0400811 /** Notification senders to prioritize for messages. One of:
812 * PRIORITY_SENDERS_ANY, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED */
813 public final int priorityMessageSenders;
814
Julia Reynoldsd5607292016-02-05 15:25:58 -0500815 /**
816 * @hide
817 */
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500818 public static final int SUPPRESSED_EFFECTS_UNSET = -1;
Julia Reynoldsd5607292016-02-05 15:25:58 -0500819 /**
Julia Reynoldscedacef2016-03-04 08:18:47 -0500820 * Whether notifications suppressed by DND should not interrupt visually (e.g. with
821 * notification lights or by turning the screen on) when the screen is off.
Julia Reynoldsd5607292016-02-05 15:25:58 -0500822 */
823 public static final int SUPPRESSED_EFFECT_SCREEN_OFF = 1 << 0;
824 /**
Julia Reynoldscedacef2016-03-04 08:18:47 -0500825 * Whether notifications suppressed by DND should not interrupt visually when the screen
826 * is on (e.g. by peeking onto the screen).
Julia Reynoldsd5607292016-02-05 15:25:58 -0500827 */
828 public static final int SUPPRESSED_EFFECT_SCREEN_ON = 1 << 1;
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500829
830 private static final int[] ALL_SUPPRESSED_EFFECTS = {
Julia Reynoldsd5607292016-02-05 15:25:58 -0500831 SUPPRESSED_EFFECT_SCREEN_OFF,
Julia Reynolds61721582016-01-05 08:35:25 -0500832 SUPPRESSED_EFFECT_SCREEN_ON,
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500833 };
834
835 /**
836 * Visual effects to suppress for a notification that is filtered by Do Not Disturb mode.
837 * Bitmask of SUPPRESSED_EFFECT_* constants.
838 */
839 public final int suppressedVisualEffects;
840
Julia Reynoldscedacef2016-03-04 08:18:47 -0500841 /**
842 * Constructs a policy for Do Not Disturb priority mode behavior.
843 *
844 * @param priorityCategories bitmask of categories of notifications that can bypass DND.
845 * @param priorityCallSenders which callers can bypass DND.
846 * @param priorityMessageSenders which message senders can bypass DND.
847 */
John Spurlock80774932015-05-07 17:38:50 -0400848 public Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders) {
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500849 this(priorityCategories, priorityCallSenders, priorityMessageSenders,
850 SUPPRESSED_EFFECTS_UNSET);
851 }
852
Julia Reynoldscedacef2016-03-04 08:18:47 -0500853 /**
854 * Constructs a policy for Do Not Disturb priority mode behavior.
855 *
856 * @param priorityCategories bitmask of categories of notifications that can bypass DND.
857 * @param priorityCallSenders which callers can bypass DND.
858 * @param priorityMessageSenders which message senders can bypass DND.
859 * @param suppressedVisualEffects which visual interruptions should be suppressed from
860 * notifications that are filtered by DND.
861 */
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500862 public Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders,
863 int suppressedVisualEffects) {
John Spurlock1fc476d2015-04-14 16:05:20 -0400864 this.priorityCategories = priorityCategories;
John Spurlock80774932015-05-07 17:38:50 -0400865 this.priorityCallSenders = priorityCallSenders;
866 this.priorityMessageSenders = priorityMessageSenders;
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500867 this.suppressedVisualEffects = suppressedVisualEffects;
John Spurlock1fc476d2015-04-14 16:05:20 -0400868 }
869
870 /** @hide */
871 public Policy(Parcel source) {
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500872 this(source.readInt(), source.readInt(), source.readInt(), source.readInt());
John Spurlock1fc476d2015-04-14 16:05:20 -0400873 }
874
875 @Override
876 public void writeToParcel(Parcel dest, int flags) {
877 dest.writeInt(priorityCategories);
John Spurlock80774932015-05-07 17:38:50 -0400878 dest.writeInt(priorityCallSenders);
879 dest.writeInt(priorityMessageSenders);
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500880 dest.writeInt(suppressedVisualEffects);
John Spurlock1fc476d2015-04-14 16:05:20 -0400881 }
882
883 @Override
884 public int describeContents() {
885 return 0;
886 }
887
888 @Override
889 public int hashCode() {
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500890 return Objects.hash(priorityCategories, priorityCallSenders, priorityMessageSenders,
891 suppressedVisualEffects);
John Spurlock1fc476d2015-04-14 16:05:20 -0400892 }
893
894 @Override
895 public boolean equals(Object o) {
896 if (!(o instanceof Policy)) return false;
897 if (o == this) return true;
898 final Policy other = (Policy) o;
899 return other.priorityCategories == priorityCategories
John Spurlock80774932015-05-07 17:38:50 -0400900 && other.priorityCallSenders == priorityCallSenders
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500901 && other.priorityMessageSenders == priorityMessageSenders
902 && other.suppressedVisualEffects == suppressedVisualEffects;
John Spurlock1fc476d2015-04-14 16:05:20 -0400903 }
904
905 @Override
906 public String toString() {
907 return "NotificationManager.Policy["
908 + "priorityCategories=" + priorityCategoriesToString(priorityCategories)
John Spurlock80774932015-05-07 17:38:50 -0400909 + ",priorityCallSenders=" + prioritySendersToString(priorityCallSenders)
910 + ",priorityMessageSenders=" + prioritySendersToString(priorityMessageSenders)
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500911 + ",suppressedVisualEffects="
912 + suppressedEffectsToString(suppressedVisualEffects)
John Spurlock1fc476d2015-04-14 16:05:20 -0400913 + "]";
914 }
915
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500916 public static String suppressedEffectsToString(int effects) {
917 if (effects <= 0) return "";
918 final StringBuilder sb = new StringBuilder();
919 for (int i = 0; i < ALL_SUPPRESSED_EFFECTS.length; i++) {
920 final int effect = ALL_SUPPRESSED_EFFECTS[i];
921 if ((effects & effect) != 0) {
922 if (sb.length() > 0) sb.append(',');
923 sb.append(effectToString(effect));
924 }
925 effects &= ~effect;
926 }
927 if (effects != 0) {
928 if (sb.length() > 0) sb.append(',');
929 sb.append("UNKNOWN_").append(effects);
930 }
931 return sb.toString();
932 }
933
John Spurlock1fc476d2015-04-14 16:05:20 -0400934 public static String priorityCategoriesToString(int priorityCategories) {
935 if (priorityCategories == 0) return "";
936 final StringBuilder sb = new StringBuilder();
937 for (int i = 0; i < ALL_PRIORITY_CATEGORIES.length; i++) {
938 final int priorityCategory = ALL_PRIORITY_CATEGORIES[i];
939 if ((priorityCategories & priorityCategory) != 0) {
940 if (sb.length() > 0) sb.append(',');
941 sb.append(priorityCategoryToString(priorityCategory));
942 }
943 priorityCategories &= ~priorityCategory;
944 }
945 if (priorityCategories != 0) {
946 if (sb.length() > 0) sb.append(',');
947 sb.append("PRIORITY_CATEGORY_UNKNOWN_").append(priorityCategories);
948 }
949 return sb.toString();
950 }
951
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500952 private static String effectToString(int effect) {
953 switch (effect) {
Julia Reynoldsd5607292016-02-05 15:25:58 -0500954 case SUPPRESSED_EFFECT_SCREEN_OFF: return "SUPPRESSED_EFFECT_SCREEN_OFF";
Julia Reynolds61721582016-01-05 08:35:25 -0500955 case SUPPRESSED_EFFECT_SCREEN_ON: return "SUPPRESSED_EFFECT_SCREEN_ON";
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500956 case SUPPRESSED_EFFECTS_UNSET: return "SUPPRESSED_EFFECTS_UNSET";
957 default: return "UNKNOWN_" + effect;
958 }
959 }
960
John Spurlock1fc476d2015-04-14 16:05:20 -0400961 private static String priorityCategoryToString(int priorityCategory) {
962 switch (priorityCategory) {
963 case PRIORITY_CATEGORY_REMINDERS: return "PRIORITY_CATEGORY_REMINDERS";
964 case PRIORITY_CATEGORY_EVENTS: return "PRIORITY_CATEGORY_EVENTS";
965 case PRIORITY_CATEGORY_MESSAGES: return "PRIORITY_CATEGORY_MESSAGES";
966 case PRIORITY_CATEGORY_CALLS: return "PRIORITY_CATEGORY_CALLS";
967 case PRIORITY_CATEGORY_REPEAT_CALLERS: return "PRIORITY_CATEGORY_REPEAT_CALLERS";
968 default: return "PRIORITY_CATEGORY_UNKNOWN_" + priorityCategory;
969 }
970 }
971
972 public static String prioritySendersToString(int prioritySenders) {
973 switch (prioritySenders) {
974 case PRIORITY_SENDERS_ANY: return "PRIORITY_SENDERS_ANY";
975 case PRIORITY_SENDERS_CONTACTS: return "PRIORITY_SENDERS_CONTACTS";
976 case PRIORITY_SENDERS_STARRED: return "PRIORITY_SENDERS_STARRED";
977 default: return "PRIORITY_SENDERS_UNKNOWN_" + prioritySenders;
978 }
979 }
980
981 public static final Parcelable.Creator<Policy> CREATOR = new Parcelable.Creator<Policy>() {
982 @Override
983 public Policy createFromParcel(Parcel in) {
984 return new Policy(in);
985 }
986
987 @Override
988 public Policy[] newArray(int size) {
989 return new Policy[size];
990 }
991 };
John Spurlock1fc476d2015-04-14 16:05:20 -0400992 }
993
Dan Sandler994349c2015-04-15 11:02:54 -0400994 /**
995 * Recover a list of active notifications: ones that have been posted by the calling app that
996 * have not yet been dismissed by the user or {@link #cancel(String, int)}ed by the app.
997 *
998 * Each notification is embedded in a {@link StatusBarNotification} object, including the
999 * original <code>tag</code> and <code>id</code> supplied to
1000 * {@link #notify(String, int, Notification) notify()}
1001 * (via {@link StatusBarNotification#getTag() getTag()} and
1002 * {@link StatusBarNotification#getId() getId()}) as well as a copy of the original
1003 * {@link Notification} object (via {@link StatusBarNotification#getNotification()}).
1004 *
1005 * @return An array of {@link StatusBarNotification}.
1006 */
1007 public StatusBarNotification[] getActiveNotifications() {
1008 final INotificationManager service = getService();
1009 final String pkg = mContext.getPackageName();
1010 try {
1011 final ParceledListSlice<StatusBarNotification> parceledList
1012 = service.getAppActiveNotifications(pkg, UserHandle.myUserId());
1013 final List<StatusBarNotification> list = parceledList.getList();
1014 return list.toArray(new StatusBarNotification[list.size()]);
1015 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001016 throw e.rethrowFromSystemServer();
Dan Sandler994349c2015-04-15 11:02:54 -04001017 }
Dan Sandler994349c2015-04-15 11:02:54 -04001018 }
John Spurlock80774932015-05-07 17:38:50 -04001019
1020 /**
1021 * Gets the current notification interruption filter.
1022 *
1023 * <p>
1024 * The interruption filter defines which notifications are allowed to interrupt the user
1025 * (e.g. via sound &amp; vibration) and is applied globally.
1026 * @return One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when
1027 * unavailable.
John Spurlock80774932015-05-07 17:38:50 -04001028 */
Julia Reynolds0edb50c2016-02-26 14:08:25 -05001029 public final @InterruptionFilter int getCurrentInterruptionFilter() {
John Spurlock80774932015-05-07 17:38:50 -04001030 final INotificationManager service = getService();
1031 try {
1032 return zenModeToInterruptionFilter(service.getZenMode());
1033 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001034 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -04001035 }
John Spurlock80774932015-05-07 17:38:50 -04001036 }
1037
1038 /**
1039 * Sets the current notification interruption filter.
1040 *
1041 * <p>
1042 * The interruption filter defines which notifications are allowed to interrupt the user
1043 * (e.g. via sound &amp; vibration) and is applied globally.
1044 * @return One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when
1045 * unavailable.
1046 *
1047 * <p>
John Spurlock7c74f782015-06-04 13:01:42 -04001048 * Only available if policy access is granted to this package.
1049 * See {@link #isNotificationPolicyAccessGranted}.
John Spurlock80774932015-05-07 17:38:50 -04001050 */
1051 public final void setInterruptionFilter(int interruptionFilter) {
1052 final INotificationManager service = getService();
1053 try {
1054 service.setInterruptionFilter(mContext.getOpPackageName(), interruptionFilter);
1055 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001056 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -04001057 }
1058 }
1059
1060 /** @hide */
1061 public static int zenModeToInterruptionFilter(int zen) {
1062 switch (zen) {
1063 case Global.ZEN_MODE_OFF: return INTERRUPTION_FILTER_ALL;
1064 case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return INTERRUPTION_FILTER_PRIORITY;
1065 case Global.ZEN_MODE_ALARMS: return INTERRUPTION_FILTER_ALARMS;
1066 case Global.ZEN_MODE_NO_INTERRUPTIONS: return INTERRUPTION_FILTER_NONE;
1067 default: return INTERRUPTION_FILTER_UNKNOWN;
1068 }
1069 }
1070
1071 /** @hide */
1072 public static int zenModeFromInterruptionFilter(int interruptionFilter, int defValue) {
1073 switch (interruptionFilter) {
1074 case INTERRUPTION_FILTER_ALL: return Global.ZEN_MODE_OFF;
1075 case INTERRUPTION_FILTER_PRIORITY: return Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
1076 case INTERRUPTION_FILTER_ALARMS: return Global.ZEN_MODE_ALARMS;
1077 case INTERRUPTION_FILTER_NONE: return Global.ZEN_MODE_NO_INTERRUPTIONS;
1078 default: return defValue;
1079 }
1080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081}