• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package android.app;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.SdkConstant;
23 import android.annotation.SystemApi;
24 import android.annotation.SystemService;
25 import android.annotation.TestApi;
26 import android.annotation.UnsupportedAppUsage;
27 import android.app.Notification.Builder;
28 import android.content.ComponentName;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.pm.ParceledListSlice;
32 import android.graphics.drawable.Icon;
33 import android.net.Uri;
34 import android.os.Build;
35 import android.os.Bundle;
36 import android.os.Handler;
37 import android.os.IBinder;
38 import android.os.Parcel;
39 import android.os.Parcelable;
40 import android.os.RemoteException;
41 import android.os.ServiceManager;
42 import android.os.StrictMode;
43 import android.os.UserHandle;
44 import android.provider.Settings.Global;
45 import android.service.notification.Adjustment;
46 import android.service.notification.Condition;
47 import android.service.notification.StatusBarNotification;
48 import android.service.notification.ZenModeConfig;
49 import android.util.Log;
50 import android.util.proto.ProtoOutputStream;
51 
52 import java.lang.annotation.Retention;
53 import java.lang.annotation.RetentionPolicy;
54 import java.util.ArrayList;
55 import java.util.Arrays;
56 import java.util.HashMap;
57 import java.util.List;
58 import java.util.Map;
59 import java.util.Objects;
60 
61 /**
62  * Class to notify the user of events that happen.  This is how you tell
63  * the user that something has happened in the background. {@more}
64  *
65  * Notifications can take different forms:
66  * <ul>
67  *      <li>A persistent icon that goes in the status bar and is accessible
68  *          through the launcher, (when the user selects it, a designated Intent
69  *          can be launched),</li>
70  *      <li>Turning on or flashing LEDs on the device, or</li>
71  *      <li>Alerting the user by flashing the backlight, playing a sound,
72  *          or vibrating.</li>
73  * </ul>
74  *
75  * <p>
76  * Each of the notify methods takes an int id parameter and optionally a
77  * {@link String} tag parameter, which may be {@code null}.  These parameters
78  * are used to form a pair (tag, id), or ({@code null}, id) if tag is
79  * unspecified.  This pair identifies this notification from your app to the
80  * system, so that pair should be unique within your app.  If you call one
81  * of the notify methods with a (tag, id) pair that is currently active and
82  * a new set of notification parameters, it will be updated.  For example,
83  * if you pass a new status bar icon, the old icon in the status bar will
84  * be replaced with the new one.  This is also the same tag and id you pass
85  * to the {@link #cancel(int)} or {@link #cancel(String, int)} method to clear
86  * this notification.
87  *
88  * <div class="special reference">
89  * <h3>Developer Guides</h3>
90  * <p>For a guide to creating notifications, read the
91  * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
92  * developer guide.</p>
93  * </div>
94  *
95  * @see android.app.Notification
96  */
97 @SystemService(Context.NOTIFICATION_SERVICE)
98 public class NotificationManager {
99     private static String TAG = "NotificationManager";
100     private static boolean localLOGV = false;
101 
102     /**
103      * Intent that is broadcast when an application is blocked or unblocked.
104      *
105      * This broadcast is only sent to the app whose block state has changed.
106      *
107      * Input: nothing
108      * Output: {@link #EXTRA_BLOCKED_STATE}
109      */
110     @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
111     public static final String ACTION_APP_BLOCK_STATE_CHANGED =
112             "android.app.action.APP_BLOCK_STATE_CHANGED";
113 
114     /**
115      * Intent that is broadcast when a {@link NotificationChannel} is blocked
116      * (when {@link NotificationChannel#getImportance()} is {@link #IMPORTANCE_NONE}) or unblocked
117      * (when {@link NotificationChannel#getImportance()} is anything other than
118      * {@link #IMPORTANCE_NONE}).
119      *
120      * This broadcast is only sent to the app that owns the channel that has changed.
121      *
122      * Input: nothing
123      * Output: {@link #EXTRA_NOTIFICATION_CHANNEL_ID}
124      * Output: {@link #EXTRA_BLOCKED_STATE}
125      */
126     @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
127     public static final String ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED =
128             "android.app.action.NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED";
129 
130     /**
131      * Extra for {@link #ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED} containing the id of the
132      * {@link NotificationChannel} which has a new blocked state.
133      *
134      * The value will be the {@link NotificationChannel#getId()} of the channel.
135      */
136     public static final String EXTRA_NOTIFICATION_CHANNEL_ID =
137             "android.app.extra.NOTIFICATION_CHANNEL_ID";
138 
139     /**
140      * Extra for {@link #ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED} containing the id
141      * of the {@link NotificationChannelGroup} which has a new blocked state.
142      *
143      * The value will be the {@link NotificationChannelGroup#getId()} of the group.
144      */
145     public static final String EXTRA_NOTIFICATION_CHANNEL_GROUP_ID =
146             "android.app.extra.NOTIFICATION_CHANNEL_GROUP_ID";
147 
148 
149     /**
150      * Extra for {@link #ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED} or
151      * {@link #ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED} containing the new blocked
152      * state as a boolean.
153      *
154      * The value will be {@code true} if this channel or group is now blocked and {@code false} if
155      * this channel or group is now unblocked.
156      */
157     public static final String EXTRA_BLOCKED_STATE = "android.app.extra.BLOCKED_STATE";
158 
159     /**
160      * Intent that is broadcast when a {@link NotificationChannelGroup} is
161      * {@link NotificationChannelGroup#isBlocked() blocked} or unblocked.
162      *
163      * This broadcast is only sent to the app that owns the channel group that has changed.
164      *
165      * Input: nothing
166      * Output: {@link #EXTRA_NOTIFICATION_CHANNEL_GROUP_ID}
167      * Output: {@link #EXTRA_BLOCKED_STATE}
168      */
169     @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
170     public static final String ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED =
171             "android.app.action.NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED";
172 
173     /**
174      * Intent that is broadcast when the state of {@link #getEffectsSuppressor()} changes.
175      * This broadcast is only sent to registered receivers.
176      *
177      * @hide
178      */
179     @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
180     public static final String ACTION_EFFECTS_SUPPRESSOR_CHANGED
181             = "android.os.action.ACTION_EFFECTS_SUPPRESSOR_CHANGED";
182 
183     /**
184      * Intent that is broadcast when the state of {@link #isNotificationPolicyAccessGranted()}
185      * changes.
186      *
187      * This broadcast is only sent to registered receivers, and only to the apps that have changed.
188      */
189     @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
190     public static final String ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED
191             = "android.app.action.NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED";
192 
193     /**
194      * Intent that is broadcast when the state of getNotificationPolicy() changes.
195      * This broadcast is only sent to registered receivers.
196      */
197     @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
198     public static final String ACTION_NOTIFICATION_POLICY_CHANGED
199             = "android.app.action.NOTIFICATION_POLICY_CHANGED";
200 
201     /**
202      * Intent that is broadcast when the state of getCurrentInterruptionFilter() changes.
203      * This broadcast is only sent to registered receivers.
204      */
205     @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
206     public static final String ACTION_INTERRUPTION_FILTER_CHANGED
207             = "android.app.action.INTERRUPTION_FILTER_CHANGED";
208 
209     /**
210      * Intent that is broadcast when the state of getCurrentInterruptionFilter() changes.
211      * @hide
212      */
213     @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
214     public static final String ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL
215             = "android.app.action.INTERRUPTION_FILTER_CHANGED_INTERNAL";
216 
217     /** @hide */
218     @IntDef(prefix = { "INTERRUPTION_FILTER_" }, value = {
219             INTERRUPTION_FILTER_NONE, INTERRUPTION_FILTER_PRIORITY, INTERRUPTION_FILTER_ALARMS,
220             INTERRUPTION_FILTER_ALL, INTERRUPTION_FILTER_UNKNOWN
221     })
222     @Retention(RetentionPolicy.SOURCE)
223     public @interface InterruptionFilter {}
224 
225     /**
226      * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
227      *     Normal interruption filter - no notifications are suppressed.
228      */
229     public static final int INTERRUPTION_FILTER_ALL = 1;
230 
231     /**
232      * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
233      *     Priority interruption filter - all notifications are suppressed except those that match
234      *     the priority criteria. Some audio streams are muted. See
235      *     {@link Policy#priorityCallSenders}, {@link Policy#priorityCategories},
236      *     {@link Policy#priorityMessageSenders} to define or query this criteria. Users can
237      *     additionally specify packages that can bypass this interruption filter.
238      */
239     public static final int INTERRUPTION_FILTER_PRIORITY = 2;
240 
241     /**
242      * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
243      *     No interruptions filter - all notifications are suppressed and all audio streams (except
244      *     those used for phone calls) and vibrations are muted.
245      */
246     public static final int INTERRUPTION_FILTER_NONE = 3;
247 
248     /**
249      * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
250      *     Alarms only interruption filter - all notifications except those of category
251      *     {@link Notification#CATEGORY_ALARM} are suppressed. Some audio streams are muted.
252      */
253     public static final int INTERRUPTION_FILTER_ALARMS = 4;
254 
255     /** {@link #getCurrentInterruptionFilter() Interruption filter} constant - returned when
256      * the value is unavailable for any reason.
257      */
258     public static final int INTERRUPTION_FILTER_UNKNOWN = 0;
259 
260     /** @hide */
261     @IntDef(prefix = { "IMPORTANCE_" }, value = {
262             IMPORTANCE_UNSPECIFIED, IMPORTANCE_NONE,
263             IMPORTANCE_MIN, IMPORTANCE_LOW, IMPORTANCE_DEFAULT, IMPORTANCE_HIGH
264     })
265     @Retention(RetentionPolicy.SOURCE)
266     public @interface Importance {}
267 
268     /**
269      * Activity Action: Launch an Automatic Zen Rule configuration screen
270      * <p>
271      * Input: Optionally, {@link #EXTRA_AUTOMATIC_RULE_ID}, if the configuration screen for an
272      * existing rule should be displayed. If the rule id is missing or null, apps should display
273      * a configuration screen where users can create a new instance of the rule.
274      * <p>
275      * Output: Nothing
276      * <p>
277      *     You can have multiple activities handling this intent, if you support multiple
278      *     {@link AutomaticZenRule rules}. In order for the system to properly display all of your
279      *     rule types so that users can create new instances or configure existing ones, you need
280      *     to add some extra metadata ({@link #META_DATA_AUTOMATIC_RULE_TYPE})
281      *     to your activity tag in your manifest. If you'd like to limit the number of rules a user
282      *     can create from this flow, you can additionally optionally include
283      *     {@link #META_DATA_RULE_INSTANCE_LIMIT}.
284      *
285      *     For example,
286      *     &lt;meta-data
287      *         android:name="android.app.zen.automatic.ruleType"
288      *         android:value="@string/my_condition_rule">
289      *     &lt;/meta-data>
290      *     &lt;meta-data
291      *         android:name="android.app.zen.automatic.ruleInstanceLimit"
292      *         android:value="1">
293      *     &lt;/meta-data>
294      * </p>
295      * </p>
296      *
297      * @see {@link #addAutomaticZenRule(AutomaticZenRule)}
298      */
299     @SdkConstant(SdkConstant.SdkConstantType.ACTIVITY_INTENT_ACTION)
300     public static final String ACTION_AUTOMATIC_ZEN_RULE =
301             "android.app.action.AUTOMATIC_ZEN_RULE";
302 
303     /**
304      * Used as an optional string extra on {@link #ACTION_AUTOMATIC_ZEN_RULE} intents. If
305      * provided, contains the id of the {@link AutomaticZenRule} (as returned from
306      * {@link NotificationManager#addAutomaticZenRule(AutomaticZenRule)}) for which configuration
307      * settings should be displayed.
308      */
309     public static final String EXTRA_AUTOMATIC_RULE_ID = "android.app.extra.AUTOMATIC_RULE_ID";
310 
311     /**
312      * A required {@code meta-data} tag for activities that handle
313      * {@link #ACTION_AUTOMATIC_ZEN_RULE}.
314      *
315      * This tag should contain a localized name of the type of the zen rule provided by the
316      * activity.
317      */
318     public static final String META_DATA_AUTOMATIC_RULE_TYPE =
319             "android.service.zen.automatic.ruleType";
320 
321     /**
322      * An optional {@code meta-data} tag for activities that handle
323      * {@link #ACTION_AUTOMATIC_ZEN_RULE}.
324      *
325      * This tag should contain the maximum number of rule instances that
326      * can be created for this rule type. Omit or enter a value <= 0 to allow unlimited instances.
327      */
328     public static final String META_DATA_RULE_INSTANCE_LIMIT =
329             "android.service.zen.automatic.ruleInstanceLimit";
330 
331     /** Value signifying that the user has not expressed a per-app visibility override value.
332      * @hide */
333     public static final int VISIBILITY_NO_OVERRIDE = -1000;
334 
335     /**
336      * Value signifying that the user has not expressed an importance.
337      *
338      * This value is for persisting preferences, and should never be associated with
339      * an actual notification.
340      */
341     public static final int IMPORTANCE_UNSPECIFIED = -1000;
342 
343     /**
344      * A notification with no importance: does not show in the shade.
345      */
346     public static final int IMPORTANCE_NONE = 0;
347 
348     /**
349      * Min notification importance: only shows in the shade, below the fold.  This should
350      * not be used with {@link Service#startForeground(int, Notification) Service.startForeground}
351      * since a foreground service is supposed to be something the user cares about so it does
352      * not make semantic sense to mark its notification as minimum importance.  If you do this
353      * as of Android version {@link android.os.Build.VERSION_CODES#O}, the system will show
354      * a higher-priority notification about your app running in the background.
355      */
356     public static final int IMPORTANCE_MIN = 1;
357 
358     /**
359      * Low notification importance: Shows in the shade, and potentially in the status bar
360      * (see {@link #shouldHideSilentStatusBarIcons()}), but is not audibly intrusive.
361      */
362     public static final int IMPORTANCE_LOW = 2;
363 
364     /**
365      * Default notification importance: shows everywhere, makes noise, but does not visually
366      * intrude.
367      */
368     public static final int IMPORTANCE_DEFAULT = 3;
369 
370     /**
371      * Higher notification importance: shows everywhere, makes noise and peeks. May use full screen
372      * intents.
373      */
374     public static final int IMPORTANCE_HIGH = 4;
375 
376     /**
377      * Unused.
378      */
379     public static final int IMPORTANCE_MAX = 5;
380 
381     @UnsupportedAppUsage
382     private static INotificationManager sService;
383 
384     /** @hide */
385     @UnsupportedAppUsage
getService()386     static public INotificationManager getService()
387     {
388         if (sService != null) {
389             return sService;
390         }
391         IBinder b = ServiceManager.getService("notification");
392         sService = INotificationManager.Stub.asInterface(b);
393         return sService;
394     }
395 
396     @UnsupportedAppUsage
NotificationManager(Context context, Handler handler)397     /*package*/ NotificationManager(Context context, Handler handler)
398     {
399         mContext = context;
400     }
401 
402     /** {@hide} */
403     @UnsupportedAppUsage
from(Context context)404     public static NotificationManager from(Context context) {
405         return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
406     }
407 
408     /**
409      * Post a notification to be shown in the status bar. If a notification with
410      * the same id has already been posted by your application and has not yet been canceled, it
411      * will be replaced by the updated information.
412      *
413      * @param id An identifier for this notification unique within your
414      *        application.
415      * @param notification A {@link Notification} object describing what to show the user. Must not
416      *        be null.
417      */
notify(int id, Notification notification)418     public void notify(int id, Notification notification)
419     {
420         notify(null, id, notification);
421     }
422 
423     /**
424      * Posts a notification to be shown in the status bar. If a notification with
425      * the same tag and id has already been posted by your application and has not yet been
426      * canceled, it will be replaced by the updated information.
427      *
428      * All {@link android.service.notification.NotificationListenerService listener services} will
429      * be granted {@link Intent#FLAG_GRANT_READ_URI_PERMISSION} access to any {@link Uri uris}
430      * provided on this notification or the
431      * {@link NotificationChannel} this notification is posted to using
432      * {@link Context#grantUriPermission(String, Uri, int)}. Permission will be revoked when the
433      * notification is canceled, or you can revoke permissions with
434      * {@link Context#revokeUriPermission(Uri, int)}.
435      *
436      * @param tag A string identifier for this notification.  May be {@code null}.
437      * @param id An identifier for this notification.  The pair (tag, id) must be unique
438      *        within your application.
439      * @param notification A {@link Notification} object describing what to
440      *        show the user. Must not be null.
441      */
notify(String tag, int id, Notification notification)442     public void notify(String tag, int id, Notification notification)
443     {
444         notifyAsUser(tag, id, notification, mContext.getUser());
445     }
446 
447     /**
448      * Posts a notification as a specified package to be shown in the status bar. If a notification
449      * with the same tag and id has already been posted for that package and has not yet been
450      * canceled, it will be replaced by the updated information.
451      *
452      * All {@link android.service.notification.NotificationListenerService listener services} will
453      * be granted {@link Intent#FLAG_GRANT_READ_URI_PERMISSION} access to any {@link Uri uris}
454      * provided on this notification or the
455      * {@link NotificationChannel} this notification is posted to using
456      * {@link Context#grantUriPermission(String, Uri, int)}. Permission will be revoked when the
457      * notification is canceled, or you can revoke permissions with
458      * {@link Context#revokeUriPermission(Uri, int)}.
459      *
460      * @param targetPackage The package to post the notification as. The package must have granted
461      *                      you access to post notifications on their behalf with
462      *                      {@link #setNotificationDelegate(String)}.
463      * @param tag A string identifier for this notification.  May be {@code null}.
464      * @param id An identifier for this notification.  The pair (tag, id) must be unique
465      *        within your application.
466      * @param notification A {@link Notification} object describing what to
467      *        show the user. Must not be null.
468      */
notifyAsPackage(@onNull String targetPackage, @NonNull String tag, int id, @NonNull Notification notification)469     public void notifyAsPackage(@NonNull String targetPackage, @NonNull String tag, int id,
470             @NonNull Notification notification) {
471         INotificationManager service = getService();
472         String sender = mContext.getPackageName();
473 
474         try {
475             if (localLOGV) Log.v(TAG, sender + ": notify(" + id + ", " + notification + ")");
476             service.enqueueNotificationWithTag(targetPackage, sender, tag, id,
477                     fixNotification(notification), mContext.getUser().getIdentifier());
478         } catch (RemoteException e) {
479             throw e.rethrowFromSystemServer();
480         }
481     }
482 
483     /**
484      * @hide
485      */
486     @UnsupportedAppUsage
notifyAsUser(String tag, int id, Notification notification, UserHandle user)487     public void notifyAsUser(String tag, int id, Notification notification, UserHandle user)
488     {
489         INotificationManager service = getService();
490         String pkg = mContext.getPackageName();
491 
492         try {
493             if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
494             service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id,
495                     fixNotification(notification), user.getIdentifier());
496         } catch (RemoteException e) {
497             throw e.rethrowFromSystemServer();
498         }
499     }
500 
fixNotification(Notification notification)501     private Notification fixNotification(Notification notification) {
502         String pkg = mContext.getPackageName();
503         // Fix the notification as best we can.
504         Notification.addFieldsFromContext(mContext, notification);
505 
506         if (notification.sound != null) {
507             notification.sound = notification.sound.getCanonicalUri();
508             if (StrictMode.vmFileUriExposureEnabled()) {
509                 notification.sound.checkFileUriExposed("Notification.sound");
510             }
511 
512         }
513         fixLegacySmallIcon(notification, pkg);
514         if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
515             if (notification.getSmallIcon() == null) {
516                 throw new IllegalArgumentException("Invalid notification (no valid small icon): "
517                         + notification);
518             }
519         }
520 
521         notification.reduceImageSizes(mContext);
522 
523         ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
524         boolean isLowRam = am.isLowRamDevice();
525         return Builder.maybeCloneStrippedForDelivery(notification, isLowRam, mContext);
526     }
527 
fixLegacySmallIcon(Notification n, String pkg)528     private void fixLegacySmallIcon(Notification n, String pkg) {
529         if (n.getSmallIcon() == null && n.icon != 0) {
530             n.setSmallIcon(Icon.createWithResource(pkg, n.icon));
531         }
532     }
533 
534     /**
535      * Cancel a previously shown notification.  If it's transient, the view
536      * will be hidden.  If it's persistent, it will be removed from the status
537      * bar.
538      */
cancel(int id)539     public void cancel(int id)
540     {
541         cancel(null, id);
542     }
543 
544     /**
545      * Cancel a previously shown notification.  If it's transient, the view
546      * will be hidden.  If it's persistent, it will be removed from the status
547      * bar.
548      */
cancel(String tag, int id)549     public void cancel(String tag, int id)
550     {
551         cancelAsUser(tag, id, mContext.getUser());
552     }
553 
554     /**
555      * @hide
556      */
557     @UnsupportedAppUsage
cancelAsUser(String tag, int id, UserHandle user)558     public void cancelAsUser(String tag, int id, UserHandle user)
559     {
560         INotificationManager service = getService();
561         String pkg = mContext.getPackageName();
562         if (localLOGV) Log.v(TAG, pkg + ": cancel(" + id + ")");
563         try {
564             service.cancelNotificationWithTag(pkg, tag, id, user.getIdentifier());
565         } catch (RemoteException e) {
566             throw e.rethrowFromSystemServer();
567         }
568     }
569 
570     /**
571      * Cancel all previously shown notifications. See {@link #cancel} for the
572      * detailed behavior.
573      */
cancelAll()574     public void cancelAll()
575     {
576         INotificationManager service = getService();
577         String pkg = mContext.getPackageName();
578         if (localLOGV) Log.v(TAG, pkg + ": cancelAll()");
579         try {
580             service.cancelAllNotifications(pkg, mContext.getUserId());
581         } catch (RemoteException e) {
582             throw e.rethrowFromSystemServer();
583         }
584     }
585 
586     /**
587      * Allows a package to post notifications on your behalf using
588      * {@link #notifyAsPackage(String, String, int, Notification)}.
589      *
590      * This can be used to allow persistent processes to post notifications based on messages
591      * received on your behalf from the cloud, without your process having to wake up.
592      *
593      * You can check if you have an allowed delegate with {@link #getNotificationDelegate()} and
594      * revoke your delegate by passing null to this method.
595      *
596      * @param delegate Package name of the app which can send notifications on your behalf.
597      */
setNotificationDelegate(@ullable String delegate)598     public void setNotificationDelegate(@Nullable String delegate) {
599         INotificationManager service = getService();
600         String pkg = mContext.getPackageName();
601         if (localLOGV) Log.v(TAG, pkg + ": cancelAll()");
602         try {
603             service.setNotificationDelegate(pkg, delegate);
604         } catch (RemoteException e) {
605             throw e.rethrowFromSystemServer();
606         }
607     }
608 
609     /**
610      * Returns the {@link #setNotificationDelegate(String) delegate} that can post notifications on
611      * your behalf, if there currently is one.
612      */
getNotificationDelegate()613     public @Nullable String getNotificationDelegate() {
614         INotificationManager service = getService();
615         String pkg = mContext.getPackageName();
616         try {
617             return service.getNotificationDelegate(pkg);
618         } catch (RemoteException e) {
619             throw e.rethrowFromSystemServer();
620         }
621     }
622 
623     /**
624      * Returns whether you are allowed to post notifications on behalf of a given package, with
625      * {@link #notifyAsPackage(String, String, int, Notification)}.
626      *
627      * See {@link #setNotificationDelegate(String)}.
628      */
canNotifyAsPackage(@onNull String pkg)629     public boolean canNotifyAsPackage(@NonNull String pkg) {
630         INotificationManager service = getService();
631         try {
632             return service.canNotifyAsPackage(mContext.getPackageName(), pkg, mContext.getUserId());
633         } catch (RemoteException e) {
634             throw e.rethrowFromSystemServer();
635         }
636     }
637 
638     /**
639      * Creates a group container for {@link NotificationChannel} objects.
640      *
641      * This can be used to rename an existing group.
642      * <p>
643      *     Group information is only used for presentation, not for behavior. Groups are optional
644      *     for channels, and you can have a mix of channels that belong to groups and channels
645      *     that do not.
646      * </p>
647      * <p>
648      *     For example, if your application supports multiple accounts, and those accounts will
649      *     have similar channels, you can create a group for each account with account specific
650      *     labels instead of appending account information to each channel's label.
651      * </p>
652      *
653      * @param group The group to create
654      */
createNotificationChannelGroup(@onNull NotificationChannelGroup group)655     public void createNotificationChannelGroup(@NonNull NotificationChannelGroup group) {
656         createNotificationChannelGroups(Arrays.asList(group));
657     }
658 
659     /**
660      * Creates multiple notification channel groups.
661      *
662      * @param groups The list of groups to create
663      */
createNotificationChannelGroups(@onNull List<NotificationChannelGroup> groups)664     public void createNotificationChannelGroups(@NonNull List<NotificationChannelGroup> groups) {
665         INotificationManager service = getService();
666         try {
667             service.createNotificationChannelGroups(mContext.getPackageName(),
668                     new ParceledListSlice(groups));
669         } catch (RemoteException e) {
670             throw e.rethrowFromSystemServer();
671         }
672     }
673 
674     /**
675      * Creates a notification channel that notifications can be posted to.
676      *
677      * This can also be used to restore a deleted channel and to update an existing channel's
678      * name, description, group, and/or importance.
679      *
680      * <p>The name and description should only be changed if the locale changes
681      * or in response to the user renaming this channel. For example, if a user has a channel
682      * named 'John Doe' that represents messages from a 'John Doe', and 'John Doe' changes his name
683      * to 'John Smith,' the channel can be renamed to match.
684      *
685      * <p>The importance of an existing channel will only be changed if the new importance is lower
686      * than the current value and the user has not altered any settings on this channel.
687      *
688      * <p>The group an existing channel will only be changed if the channel does not already
689      * belong to a group.
690      *
691      * All other fields are ignored for channels that already exist.
692      *
693      * @param channel  the channel to create.  Note that the created channel may differ from this
694      *                 value. If the provided channel is malformed, a RemoteException will be
695      *                 thrown.
696      */
createNotificationChannel(@onNull NotificationChannel channel)697     public void createNotificationChannel(@NonNull NotificationChannel channel) {
698         createNotificationChannels(Arrays.asList(channel));
699     }
700 
701     /**
702      * Creates multiple notification channels that different notifications can be posted to. See
703      * {@link #createNotificationChannel(NotificationChannel)}.
704      *
705      * @param channels the list of channels to attempt to create.
706      */
createNotificationChannels(@onNull List<NotificationChannel> channels)707     public void createNotificationChannels(@NonNull List<NotificationChannel> channels) {
708         INotificationManager service = getService();
709         try {
710             service.createNotificationChannels(mContext.getPackageName(),
711                     new ParceledListSlice(channels));
712         } catch (RemoteException e) {
713             throw e.rethrowFromSystemServer();
714         }
715     }
716 
717     /**
718      * Returns the notification channel settings for a given channel id.
719      *
720      * <p>The channel must belong to your package, or to a package you are an approved notification
721      * delegate for (see {@link #canNotifyAsPackage(String)}), or it will not be returned. To query
722      * a channel as a notification delegate, call this method from a context created for that
723      * package (see {@link Context#createPackageContext(String, int)}).</p>
724      */
getNotificationChannel(String channelId)725     public NotificationChannel getNotificationChannel(String channelId) {
726         INotificationManager service = getService();
727         try {
728             return service.getNotificationChannel(mContext.getOpPackageName(),
729                     mContext.getUserId(), mContext.getPackageName(), channelId);
730         } catch (RemoteException e) {
731             throw e.rethrowFromSystemServer();
732         }
733     }
734 
735     /**
736      * Returns all notification channels belonging to the calling package.
737      *
738      * <p>Approved notification delegates (see {@link #canNotifyAsPackage(String)}) can query
739      * notification channels belonging to packages they are the delegate for. To do so, call this
740      * method from a context created for that package (see
741      * {@link Context#createPackageContext(String, int)}).</p>
742      */
getNotificationChannels()743     public List<NotificationChannel> getNotificationChannels() {
744         INotificationManager service = getService();
745         try {
746             return service.getNotificationChannels(mContext.getOpPackageName(),
747                     mContext.getPackageName(), mContext.getUserId()).getList();
748         } catch (RemoteException e) {
749             throw e.rethrowFromSystemServer();
750         }
751     }
752 
753     /**
754      * Deletes the given notification channel.
755      *
756      * <p>If you {@link #createNotificationChannel(NotificationChannel) create} a new channel with
757      * this same id, the deleted channel will be un-deleted with all of the same settings it
758      * had before it was deleted.
759      */
deleteNotificationChannel(String channelId)760     public void deleteNotificationChannel(String channelId) {
761         INotificationManager service = getService();
762         try {
763             service.deleteNotificationChannel(mContext.getPackageName(), channelId);
764         } catch (RemoteException e) {
765             throw e.rethrowFromSystemServer();
766         }
767     }
768 
769     /**
770      * Returns the notification channel group settings for a given channel group id.
771      *
772      * The channel group must belong to your package, or null will be returned.
773      */
getNotificationChannelGroup(String channelGroupId)774     public NotificationChannelGroup getNotificationChannelGroup(String channelGroupId) {
775         INotificationManager service = getService();
776         try {
777             return service.getNotificationChannelGroup(mContext.getPackageName(), channelGroupId);
778         } catch (RemoteException e) {
779             throw e.rethrowFromSystemServer();
780         }
781     }
782 
783     /**
784      * Returns all notification channel groups belonging to the calling app.
785      */
getNotificationChannelGroups()786     public List<NotificationChannelGroup> getNotificationChannelGroups() {
787         INotificationManager service = getService();
788         try {
789             final ParceledListSlice<NotificationChannelGroup> parceledList =
790                     service.getNotificationChannelGroups(mContext.getPackageName());
791             if (parceledList != null) {
792                 return parceledList.getList();
793             }
794         } catch (RemoteException e) {
795             throw e.rethrowFromSystemServer();
796         }
797         return new ArrayList<>();
798     }
799 
800     /**
801      * Deletes the given notification channel group, and all notification channels that
802      * belong to it.
803      */
deleteNotificationChannelGroup(String groupId)804     public void deleteNotificationChannelGroup(String groupId) {
805         INotificationManager service = getService();
806         try {
807             service.deleteNotificationChannelGroup(mContext.getPackageName(), groupId);
808         } catch (RemoteException e) {
809             throw e.rethrowFromSystemServer();
810         }
811     }
812 
813     /**
814      * @hide
815      */
816     @TestApi
getEffectsSuppressor()817     public ComponentName getEffectsSuppressor() {
818         INotificationManager service = getService();
819         try {
820             return service.getEffectsSuppressor();
821         } catch (RemoteException e) {
822             throw e.rethrowFromSystemServer();
823         }
824     }
825 
826     /**
827      * @hide
828      */
829     @TestApi
matchesCallFilter(Bundle extras)830     public boolean matchesCallFilter(Bundle extras) {
831         INotificationManager service = getService();
832         try {
833             return service.matchesCallFilter(extras);
834         } catch (RemoteException e) {
835             throw e.rethrowFromSystemServer();
836         }
837     }
838 
839     /**
840      * @hide
841      */
isSystemConditionProviderEnabled(String path)842     public boolean isSystemConditionProviderEnabled(String path) {
843         INotificationManager service = getService();
844         try {
845             return service.isSystemConditionProviderEnabled(path);
846         } catch (RemoteException e) {
847             throw e.rethrowFromSystemServer();
848         }
849     }
850 
851     /**
852      * @hide
853      */
854     @UnsupportedAppUsage
setZenMode(int mode, Uri conditionId, String reason)855     public void setZenMode(int mode, Uri conditionId, String reason) {
856         INotificationManager service = getService();
857         try {
858             service.setZenMode(mode, conditionId, reason);
859         } catch (RemoteException e) {
860             throw e.rethrowFromSystemServer();
861         }
862     }
863 
864     /**
865      * @hide
866      */
getZenMode()867     public int getZenMode() {
868         INotificationManager service = getService();
869         try {
870             return service.getZenMode();
871         } catch (RemoteException e) {
872             throw e.rethrowFromSystemServer();
873         }
874     }
875 
876     /**
877      * @hide
878      */
879     @UnsupportedAppUsage
getZenModeConfig()880     public ZenModeConfig getZenModeConfig() {
881         INotificationManager service = getService();
882         try {
883             return service.getZenModeConfig();
884         } catch (RemoteException e) {
885             throw e.rethrowFromSystemServer();
886         }
887     }
888 
889     /**
890      * @hide
891      */
getConsolidatedNotificationPolicy()892     public NotificationManager.Policy getConsolidatedNotificationPolicy() {
893         INotificationManager service = getService();
894         try {
895             return service.getConsolidatedNotificationPolicy();
896         } catch (RemoteException e) {
897             throw e.rethrowFromSystemServer();
898         }
899     }
900 
901     /**
902      * @hide
903      */
getRuleInstanceCount(ComponentName owner)904     public int getRuleInstanceCount(ComponentName owner) {
905         INotificationManager service = getService();
906         try {
907             return service.getRuleInstanceCount(owner);
908         } catch (RemoteException e) {
909             throw e.rethrowFromSystemServer();
910         }
911     }
912 
913     /**
914      * Returns AutomaticZenRules owned by the caller.
915      *
916      * <p>
917      * Throws a SecurityException if policy access is granted to this package.
918      * See {@link #isNotificationPolicyAccessGranted}.
919      */
getAutomaticZenRules()920     public Map<String, AutomaticZenRule> getAutomaticZenRules() {
921         INotificationManager service = getService();
922         try {
923             List<ZenModeConfig.ZenRule> rules = service.getZenRules();
924             Map<String, AutomaticZenRule> ruleMap = new HashMap<>();
925             for (ZenModeConfig.ZenRule rule : rules) {
926                 ruleMap.put(rule.id, new AutomaticZenRule(rule.name, rule.component,
927                         rule.configurationActivity, rule.conditionId, rule.zenPolicy,
928                         zenModeToInterruptionFilter(rule.zenMode), rule.enabled,
929                         rule.creationTime));
930             }
931             return ruleMap;
932         } catch (RemoteException e) {
933             throw e.rethrowFromSystemServer();
934         }
935     }
936 
937     /**
938      * Returns the AutomaticZenRule with the given id, if it exists and the caller has access.
939      *
940      * <p>
941      * Throws a SecurityException if policy access is granted to this package.
942      * See {@link #isNotificationPolicyAccessGranted}.
943      *
944      * <p>
945      * Returns null if there are no zen rules that match the given id, or if the calling package
946      * doesn't own the matching rule. See {@link AutomaticZenRule#getOwner}.
947      */
getAutomaticZenRule(String id)948     public AutomaticZenRule getAutomaticZenRule(String id) {
949         INotificationManager service = getService();
950         try {
951             return service.getAutomaticZenRule(id);
952         } catch (RemoteException e) {
953             throw e.rethrowFromSystemServer();
954         }
955     }
956 
957     /**
958      * Creates the given zen rule.
959      *
960      * <p>
961      * Throws a SecurityException if policy access is granted to this package.
962      * See {@link #isNotificationPolicyAccessGranted}.
963      *
964      * @param automaticZenRule the rule to create.
965      * @return The id of the newly created rule; null if the rule could not be created.
966      */
addAutomaticZenRule(AutomaticZenRule automaticZenRule)967     public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) {
968         INotificationManager service = getService();
969         try {
970             return service.addAutomaticZenRule(automaticZenRule);
971         } catch (RemoteException e) {
972             throw e.rethrowFromSystemServer();
973         }
974     }
975 
976     /**
977      * Updates the given zen rule.
978      *
979      * <p>
980      * Throws a SecurityException if policy access is granted to this package.
981      * See {@link #isNotificationPolicyAccessGranted}.
982      *
983      * <p>
984      * Callers can only update rules that they own. See {@link AutomaticZenRule#getOwner}.
985      * @param id The id of the rule to update
986      * @param automaticZenRule the rule to update.
987      * @return Whether the rule was successfully updated.
988      */
updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule)989     public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) {
990         INotificationManager service = getService();
991         try {
992             return service.updateAutomaticZenRule(id, automaticZenRule);
993         } catch (RemoteException e) {
994             throw e.rethrowFromSystemServer();
995         }
996     }
997 
998     /**
999      * Informs the notification manager that the state of an {@link AutomaticZenRule} has changed.
1000      * Use this method to put the system into Do Not Disturb mode or request that it exits Do Not
1001      * Disturb mode. The calling app must own the provided {@link android.app.AutomaticZenRule}.
1002      * <p>
1003      *     This method can be used in conjunction with or as a replacement to
1004      *     {@link android.service.notification.ConditionProviderService#notifyCondition(Condition)}.
1005      * </p>
1006      * @param id The id of the rule whose state should change
1007      * @param condition The new state of this rule
1008      */
setAutomaticZenRuleState(@onNull String id, @NonNull Condition condition)1009     public void setAutomaticZenRuleState(@NonNull String id, @NonNull Condition condition) {
1010         INotificationManager service = getService();
1011         try {
1012             service.setAutomaticZenRuleState(id, condition);
1013         } catch (RemoteException e) {
1014             throw e.rethrowFromSystemServer();
1015         }
1016     }
1017 
1018     /**
1019      * Deletes the automatic zen rule with the given id.
1020      *
1021      * <p>
1022      * Throws a SecurityException if policy access is granted to this package.
1023      * See {@link #isNotificationPolicyAccessGranted}.
1024      *
1025      * <p>
1026      * Callers can only delete rules that they own. See {@link AutomaticZenRule#getOwner}.
1027      * @param id the id of the rule to delete.
1028      * @return Whether the rule was successfully deleted.
1029      */
removeAutomaticZenRule(String id)1030     public boolean removeAutomaticZenRule(String id) {
1031         INotificationManager service = getService();
1032         try {
1033             return service.removeAutomaticZenRule(id);
1034         } catch (RemoteException e) {
1035             throw e.rethrowFromSystemServer();
1036         }
1037     }
1038 
1039     /**
1040      * Deletes all automatic zen rules owned by the given package.
1041      *
1042      * @hide
1043      */
removeAutomaticZenRules(String packageName)1044     public boolean removeAutomaticZenRules(String packageName) {
1045         INotificationManager service = getService();
1046         try {
1047             return service.removeAutomaticZenRules(packageName);
1048         } catch (RemoteException e) {
1049             throw e.rethrowFromSystemServer();
1050         }
1051     }
1052 
1053     /**
1054      * Returns the user specified importance for notifications from the calling
1055      * package.
1056      */
getImportance()1057     public @Importance int getImportance() {
1058         INotificationManager service = getService();
1059         try {
1060             return service.getPackageImportance(mContext.getPackageName());
1061         } catch (RemoteException e) {
1062             throw e.rethrowFromSystemServer();
1063         }
1064     }
1065 
1066     /**
1067      * Returns whether notifications from the calling package are blocked.
1068      */
areNotificationsEnabled()1069     public boolean areNotificationsEnabled() {
1070         INotificationManager service = getService();
1071         try {
1072             return service.areNotificationsEnabled(mContext.getPackageName());
1073         } catch (RemoteException e) {
1074             throw e.rethrowFromSystemServer();
1075         }
1076     }
1077 
1078 
1079     /**
1080      * Sets whether notifications posted by this app can appear outside of the
1081      * notification shade, floating over other apps' content.
1082      *
1083      * <p>This value will be ignored for notifications that are posted to channels that do not
1084      * allow bubbles ({@link NotificationChannel#canBubble()}.
1085      *
1086      * @see Notification#getBubbleMetadata()
1087      */
areBubblesAllowed()1088     public boolean areBubblesAllowed() {
1089         INotificationManager service = getService();
1090         try {
1091             return service.areBubblesAllowed(mContext.getPackageName());
1092         } catch (RemoteException e) {
1093             throw e.rethrowFromSystemServer();
1094         }
1095     }
1096 
1097     /**
1098      * Returns whether notifications from this package are temporarily hidden. This
1099      * could be done because the package was marked as distracting to the user via
1100      * {@code PackageManager#setDistractingPackageRestrictions(String[], int)} or because the
1101      * package is {@code PackageManager#setPackagesSuspended(String[], boolean, PersistableBundle,
1102      * PersistableBundle, SuspendDialogInfo) suspended}.
1103      */
areNotificationsPaused()1104     public boolean areNotificationsPaused() {
1105         INotificationManager service = getService();
1106         try {
1107             return service.isPackagePaused(mContext.getPackageName());
1108         } catch (RemoteException e) {
1109             throw e.rethrowFromSystemServer();
1110         }
1111     }
1112 
1113     /**
1114      * Checks the ability to modify notification do not disturb policy for the calling package.
1115      *
1116      * <p>
1117      * Returns true if the calling package can modify notification policy.
1118      *
1119      * <p>
1120      * Apps can request policy access by sending the user to the activity that matches the system
1121      * intent action {@link android.provider.Settings#ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS}.
1122      *
1123      * <p>
1124      * Use {@link #ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED} to listen for
1125      * user grant or denial of this access.
1126      */
isNotificationPolicyAccessGranted()1127     public boolean isNotificationPolicyAccessGranted() {
1128         INotificationManager service = getService();
1129         try {
1130             return service.isNotificationPolicyAccessGranted(mContext.getOpPackageName());
1131         } catch (RemoteException e) {
1132             throw e.rethrowFromSystemServer();
1133         }
1134     }
1135 
1136     /**
1137      * Checks whether the user has approved a given
1138      * {@link android.service.notification.NotificationListenerService}.
1139      *
1140      * <p>
1141      * The listener service must belong to the calling app.
1142      *
1143      * <p>
1144      * Apps can request notification listener access by sending the user to the activity that
1145      * matches the system intent action
1146      * {@link android.provider.Settings#ACTION_NOTIFICATION_LISTENER_SETTINGS}.
1147      */
isNotificationListenerAccessGranted(ComponentName listener)1148     public boolean isNotificationListenerAccessGranted(ComponentName listener) {
1149         INotificationManager service = getService();
1150         try {
1151             return service.isNotificationListenerAccessGranted(listener);
1152         } catch (RemoteException e) {
1153             throw e.rethrowFromSystemServer();
1154         }
1155     }
1156 
1157     /**
1158      * Checks whether the user has approved a given
1159      * {@link android.service.notification.NotificationAssistantService}.
1160      *
1161      * <p>
1162      * The assistant service must belong to the calling app.
1163      *
1164      * <p>
1165      * Apps can request notification assistant access by sending the user to the activity that
1166      * matches the system intent action
1167      * TODO: STOPSHIP: Add correct intent
1168      * {@link android.provider.Settings#ACTION_MANAGE_DEFAULT_APPS_SETTINGS}.
1169      * @hide
1170      */
1171     @SystemApi
1172     @TestApi
isNotificationAssistantAccessGranted(@onNull ComponentName assistant)1173     public boolean isNotificationAssistantAccessGranted(@NonNull ComponentName assistant) {
1174         INotificationManager service = getService();
1175         try {
1176             return service.isNotificationAssistantAccessGranted(assistant);
1177         } catch (RemoteException e) {
1178             throw e.rethrowFromSystemServer();
1179         }
1180     }
1181 
1182     /**
1183      * Returns whether the user wants silent notifications (see {@link #IMPORTANCE_LOW} to appear
1184      * in the status bar.
1185      *
1186      * <p>Only available for {@link #isNotificationListenerAccessGranted(ComponentName) notification
1187      * listeners}.
1188      */
shouldHideSilentStatusBarIcons()1189     public boolean shouldHideSilentStatusBarIcons() {
1190         INotificationManager service = getService();
1191         try {
1192             return service.shouldHideSilentStatusIcons(mContext.getOpPackageName());
1193         } catch (RemoteException e) {
1194             throw e.rethrowFromSystemServer();
1195         }
1196     }
1197 
1198     /**
1199      * Returns the list of {@link android.service.notification.Adjustment adjustment keys} that can
1200      * be modified by the current {@link android.service.notification.NotificationAssistantService}.
1201      *
1202      * <p>Only callable by the current
1203      * {@link android.service.notification.NotificationAssistantService}.
1204      * See {@link #isNotificationAssistantAccessGranted(ComponentName)}</p>
1205      * @hide
1206      */
1207     @SystemApi
1208     @TestApi
getAllowedAssistantAdjustments()1209     public @NonNull @Adjustment.Keys List<String> getAllowedAssistantAdjustments() {
1210         INotificationManager service = getService();
1211         try {
1212             return service.getAllowedAssistantAdjustments(mContext.getOpPackageName());
1213         } catch (RemoteException e) {
1214             throw e.rethrowFromSystemServer();
1215         }
1216     }
1217 
1218     /**
1219      * @hide
1220      */
1221     @TestApi
allowAssistantAdjustment(String capability)1222     public void allowAssistantAdjustment(String capability) {
1223         INotificationManager service = getService();
1224         try {
1225             service.allowAssistantAdjustment(capability);
1226         } catch (RemoteException e) {
1227             throw e.rethrowFromSystemServer();
1228         }
1229     }
1230 
1231     /**
1232      * @hide
1233      */
1234     @TestApi
disallowAssistantAdjustment(String capability)1235     public void disallowAssistantAdjustment(String capability) {
1236         INotificationManager service = getService();
1237         try {
1238             service.disallowAssistantAdjustment(capability);
1239         } catch (RemoteException e) {
1240             throw e.rethrowFromSystemServer();
1241         }
1242     }
1243 
1244     /** @hide */
isNotificationPolicyAccessGrantedForPackage(String pkg)1245     public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {
1246         INotificationManager service = getService();
1247         try {
1248             return service.isNotificationPolicyAccessGrantedForPackage(pkg);
1249         } catch (RemoteException e) {
1250             throw e.rethrowFromSystemServer();
1251         }
1252     }
1253 
1254     /**
1255      * @hide
1256      */
getEnabledNotificationListenerPackages()1257     public List<String> getEnabledNotificationListenerPackages() {
1258         INotificationManager service = getService();
1259         try {
1260             return service.getEnabledNotificationListenerPackages();
1261         } catch (RemoteException e) {
1262             throw e.rethrowFromSystemServer();
1263         }
1264     }
1265 
1266     /**
1267      * Gets the current user-specified default notification policy.
1268      *
1269      * <p>
1270      */
getNotificationPolicy()1271     public Policy getNotificationPolicy() {
1272         INotificationManager service = getService();
1273         try {
1274             return service.getNotificationPolicy(mContext.getOpPackageName());
1275         } catch (RemoteException e) {
1276             throw e.rethrowFromSystemServer();
1277         }
1278     }
1279 
1280     /**
1281      * Sets the current notification policy.
1282      *
1283      * <p>
1284      * Only available if policy access is granted to this package.
1285      * See {@link #isNotificationPolicyAccessGranted}.
1286      *
1287      * @param policy The new desired policy.
1288      */
setNotificationPolicy(@onNull Policy policy)1289     public void setNotificationPolicy(@NonNull Policy policy) {
1290         checkRequired("policy", policy);
1291         INotificationManager service = getService();
1292         try {
1293             service.setNotificationPolicy(mContext.getOpPackageName(), policy);
1294         } catch (RemoteException e) {
1295             throw e.rethrowFromSystemServer();
1296         }
1297     }
1298 
1299     /** @hide */
setNotificationPolicyAccessGranted(String pkg, boolean granted)1300     public void setNotificationPolicyAccessGranted(String pkg, boolean granted) {
1301         INotificationManager service = getService();
1302         try {
1303             service.setNotificationPolicyAccessGranted(pkg, granted);
1304         } catch (RemoteException e) {
1305             throw e.rethrowFromSystemServer();
1306         }
1307     }
1308 
1309     /** @hide */
setNotificationListenerAccessGranted(ComponentName listener, boolean granted)1310     public void setNotificationListenerAccessGranted(ComponentName listener, boolean granted) {
1311         INotificationManager service = getService();
1312         try {
1313             service.setNotificationListenerAccessGranted(listener, granted);
1314         } catch (RemoteException e) {
1315             throw e.rethrowFromSystemServer();
1316         }
1317     }
1318 
1319     /** @hide */
setNotificationListenerAccessGrantedForUser(ComponentName listener, int userId, boolean granted)1320     public void setNotificationListenerAccessGrantedForUser(ComponentName listener, int userId,
1321             boolean granted) {
1322         INotificationManager service = getService();
1323         try {
1324             service.setNotificationListenerAccessGrantedForUser(listener, userId, granted);
1325         } catch (RemoteException e) {
1326             throw e.rethrowFromSystemServer();
1327         }
1328     }
1329 
1330     /**
1331      * Grants/revokes Notification Assistant access to {@code assistant} for current user.
1332      * To grant access for a particular user, obtain this service by using the {@link Context}
1333      * provided by {@link Context#createPackageContextAsUser}
1334      *
1335      * @param assistant Name of component to grant/revoke access or {@code null} to revoke access to
1336      *                  current assistant
1337      * @param granted Grant/revoke access
1338      * @hide
1339      */
1340     @SystemApi
1341     @TestApi
setNotificationAssistantAccessGranted(@ullable ComponentName assistant, boolean granted)1342     public void setNotificationAssistantAccessGranted(@Nullable ComponentName assistant,
1343             boolean granted) {
1344         INotificationManager service = getService();
1345         try {
1346             service.setNotificationAssistantAccessGranted(assistant, granted);
1347         } catch (RemoteException e) {
1348             throw e.rethrowFromSystemServer();
1349         }
1350     }
1351 
1352     /** @hide */
getEnabledNotificationListeners(int userId)1353     public List<ComponentName> getEnabledNotificationListeners(int userId) {
1354         INotificationManager service = getService();
1355         try {
1356             return service.getEnabledNotificationListeners(userId);
1357         } catch (RemoteException e) {
1358             throw e.rethrowFromSystemServer();
1359         }
1360     }
1361 
1362     /** @hide */
1363     @SystemApi
1364     @TestApi
getAllowedNotificationAssistant()1365     public @Nullable ComponentName getAllowedNotificationAssistant() {
1366         INotificationManager service = getService();
1367         try {
1368             return service.getAllowedNotificationAssistant();
1369         } catch (RemoteException e) {
1370             throw e.rethrowFromSystemServer();
1371         }
1372     }
1373 
1374 
1375     private Context mContext;
1376 
checkRequired(String name, Object value)1377     private static void checkRequired(String name, Object value) {
1378         if (value == null) {
1379             throw new IllegalArgumentException(name + " is required");
1380         }
1381     }
1382 
1383     /**
1384      * Notification policy configuration.  Represents user-preferences for notification
1385      * filtering.
1386      */
1387     public static class Policy implements android.os.Parcelable {
1388         /** Reminder notifications are prioritized. */
1389         public static final int PRIORITY_CATEGORY_REMINDERS = 1 << 0;
1390         /** Event notifications are prioritized. */
1391         public static final int PRIORITY_CATEGORY_EVENTS = 1 << 1;
1392         /** Message notifications are prioritized. */
1393         public static final int PRIORITY_CATEGORY_MESSAGES = 1 << 2;
1394         /** Calls are prioritized. */
1395         public static final int PRIORITY_CATEGORY_CALLS = 1 << 3;
1396         /** Calls from repeat callers are prioritized. */
1397         public static final int PRIORITY_CATEGORY_REPEAT_CALLERS = 1 << 4;
1398         /** Alarms are prioritized */
1399         public static final int PRIORITY_CATEGORY_ALARMS = 1 << 5;
1400         /** Media, game, voice navigation are prioritized */
1401         public static final int PRIORITY_CATEGORY_MEDIA = 1 << 6;
1402         /**System (catch-all for non-never suppressible sounds) are prioritized */
1403         public static final int PRIORITY_CATEGORY_SYSTEM = 1 << 7;
1404 
1405         /**
1406          * @hide
1407          */
1408         public static final int[] ALL_PRIORITY_CATEGORIES = {
1409             PRIORITY_CATEGORY_ALARMS,
1410             PRIORITY_CATEGORY_MEDIA,
1411             PRIORITY_CATEGORY_SYSTEM,
1412             PRIORITY_CATEGORY_REMINDERS,
1413             PRIORITY_CATEGORY_EVENTS,
1414             PRIORITY_CATEGORY_MESSAGES,
1415             PRIORITY_CATEGORY_CALLS,
1416             PRIORITY_CATEGORY_REPEAT_CALLERS,
1417         };
1418 
1419         /** Any sender is prioritized. */
1420         public static final int PRIORITY_SENDERS_ANY = 0;
1421         /** Saved contacts are prioritized. */
1422         public static final int PRIORITY_SENDERS_CONTACTS = 1;
1423         /** Only starred contacts are prioritized. */
1424         public static final int PRIORITY_SENDERS_STARRED = 2;
1425 
1426         /** Notification categories to prioritize. Bitmask of PRIORITY_CATEGORY_* constants. */
1427         public final int priorityCategories;
1428 
1429         /** Notification senders to prioritize for calls. One of:
1430          * PRIORITY_SENDERS_ANY, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED */
1431         public final int priorityCallSenders;
1432 
1433         /** Notification senders to prioritize for messages. One of:
1434          * PRIORITY_SENDERS_ANY, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED */
1435         public final int priorityMessageSenders;
1436 
1437         /**
1438          * @hide
1439          */
1440         public static final int SUPPRESSED_EFFECTS_UNSET = -1;
1441 
1442         /**
1443          * Whether notifications suppressed by DND should not interrupt visually (e.g. with
1444          * notification lights or by turning the screen on) when the screen is off.
1445          *
1446          * @deprecated use {@link #SUPPRESSED_EFFECT_FULL_SCREEN_INTENT} and
1447          * {@link #SUPPRESSED_EFFECT_AMBIENT} and {@link #SUPPRESSED_EFFECT_LIGHTS} individually.
1448          */
1449         @Deprecated
1450         public static final int SUPPRESSED_EFFECT_SCREEN_OFF = 1 << 0;
1451         /**
1452          * Whether notifications suppressed by DND should not interrupt visually when the screen
1453          * is on (e.g. by peeking onto the screen).
1454          *
1455          * @deprecated use {@link #SUPPRESSED_EFFECT_PEEK}.
1456          */
1457         @Deprecated
1458         public static final int SUPPRESSED_EFFECT_SCREEN_ON = 1 << 1;
1459 
1460         /**
1461          * Whether {@link Notification#fullScreenIntent full screen intents} from
1462          * notifications intercepted by DND are blocked.
1463          */
1464         public static final int SUPPRESSED_EFFECT_FULL_SCREEN_INTENT = 1 << 2;
1465 
1466         /**
1467          * Whether {@link NotificationChannel#shouldShowLights() notification lights} from
1468          * notifications intercepted by DND are blocked.
1469          */
1470         public static final int SUPPRESSED_EFFECT_LIGHTS = 1 << 3;
1471 
1472         /**
1473          * Whether notifications intercepted by DND are prevented from peeking.
1474          */
1475         public static final int SUPPRESSED_EFFECT_PEEK = 1 << 4;
1476 
1477         /**
1478          * Whether notifications intercepted by DND are prevented from appearing in the status bar,
1479          * on devices that support status bars.
1480          */
1481         public static final int SUPPRESSED_EFFECT_STATUS_BAR = 1 << 5;
1482 
1483         /**
1484          * Whether {@link NotificationChannel#canShowBadge() badges} from
1485          * notifications intercepted by DND are blocked on devices that support badging.
1486          */
1487         public static final int SUPPRESSED_EFFECT_BADGE = 1 << 6;
1488 
1489         /**
1490          * Whether notification intercepted by DND are prevented from appearing on ambient displays
1491          * on devices that support ambient display.
1492          */
1493         public static final int SUPPRESSED_EFFECT_AMBIENT = 1 << 7;
1494 
1495         /**
1496          * Whether notification intercepted by DND are prevented from appearing in notification
1497          * list views like the notification shade or lockscreen on devices that support those
1498          * views.
1499          */
1500         public static final int SUPPRESSED_EFFECT_NOTIFICATION_LIST = 1 << 8;
1501 
1502         private static final int[] ALL_SUPPRESSED_EFFECTS = {
1503                 SUPPRESSED_EFFECT_SCREEN_OFF,
1504                 SUPPRESSED_EFFECT_SCREEN_ON,
1505                 SUPPRESSED_EFFECT_FULL_SCREEN_INTENT,
1506                 SUPPRESSED_EFFECT_LIGHTS,
1507                 SUPPRESSED_EFFECT_PEEK,
1508                 SUPPRESSED_EFFECT_STATUS_BAR,
1509                 SUPPRESSED_EFFECT_BADGE,
1510                 SUPPRESSED_EFFECT_AMBIENT,
1511                 SUPPRESSED_EFFECT_NOTIFICATION_LIST
1512         };
1513 
1514         private static final int[] SCREEN_OFF_SUPPRESSED_EFFECTS = {
1515                 SUPPRESSED_EFFECT_SCREEN_OFF,
1516                 SUPPRESSED_EFFECT_FULL_SCREEN_INTENT,
1517                 SUPPRESSED_EFFECT_LIGHTS,
1518                 SUPPRESSED_EFFECT_AMBIENT,
1519         };
1520 
1521         private static final int[] SCREEN_ON_SUPPRESSED_EFFECTS = {
1522                 SUPPRESSED_EFFECT_SCREEN_ON,
1523                 SUPPRESSED_EFFECT_PEEK,
1524                 SUPPRESSED_EFFECT_STATUS_BAR,
1525                 SUPPRESSED_EFFECT_BADGE,
1526                 SUPPRESSED_EFFECT_NOTIFICATION_LIST
1527         };
1528 
1529         /**
1530          * Visual effects to suppress for a notification that is filtered by Do Not Disturb mode.
1531          * Bitmask of SUPPRESSED_EFFECT_* constants.
1532          */
1533         public final int suppressedVisualEffects;
1534 
1535         /**
1536          * @hide
1537          */
1538         public static final int STATE_CHANNELS_BYPASSING_DND = 1 << 0;
1539 
1540         /**
1541          * @hide
1542          */
1543         public static final int STATE_UNSET = -1;
1544 
1545         /**
1546          * Notification state information that is necessary to determine Do Not Disturb behavior.
1547          * Bitmask of STATE_* constants.
1548          * @hide
1549          */
1550         public final int state;
1551 
1552         /**
1553          * Constructs a policy for Do Not Disturb priority mode behavior.
1554          *
1555          * <p>
1556          *     Apps that target API levels below {@link Build.VERSION_CODES#P} cannot
1557          *     change user-designated values to allow or disallow
1558          *     {@link Policy#PRIORITY_CATEGORY_ALARMS}, {@link Policy#PRIORITY_CATEGORY_SYSTEM}, and
1559          *     {@link Policy#PRIORITY_CATEGORY_MEDIA} from bypassing dnd.
1560          *
1561          * @param priorityCategories bitmask of categories of notifications that can bypass DND.
1562          * @param priorityCallSenders which callers can bypass DND.
1563          * @param priorityMessageSenders which message senders can bypass DND.
1564          */
Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders)1565         public Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders) {
1566             this(priorityCategories, priorityCallSenders, priorityMessageSenders,
1567                     SUPPRESSED_EFFECTS_UNSET, STATE_UNSET);
1568         }
1569 
1570         /**
1571          * Constructs a policy for Do Not Disturb priority mode behavior.
1572          *
1573          * <p>
1574          *     Apps that target API levels below {@link Build.VERSION_CODES#P} cannot
1575          *     change user-designated values to allow or disallow
1576          *     {@link Policy#PRIORITY_CATEGORY_ALARMS}, {@link Policy#PRIORITY_CATEGORY_SYSTEM}, and
1577          *     {@link Policy#PRIORITY_CATEGORY_MEDIA} from bypassing dnd.
1578          * <p>
1579          *     Additionally, apps that target API levels below {@link Build.VERSION_CODES#P} can
1580          *     only modify the {@link #SUPPRESSED_EFFECT_SCREEN_ON} and
1581          *     {@link #SUPPRESSED_EFFECT_SCREEN_OFF} bits of the suppressed visual effects field.
1582          *     All other suppressed effects will be ignored and reconstituted from the screen on
1583          *     and screen off values.
1584          * <p>
1585          *     Apps that target {@link Build.VERSION_CODES#P} or above can set any
1586          *     suppressed visual effects. However, if any suppressed effects >
1587          *     {@link #SUPPRESSED_EFFECT_SCREEN_ON} are set, {@link #SUPPRESSED_EFFECT_SCREEN_ON}
1588          *     and {@link #SUPPRESSED_EFFECT_SCREEN_OFF} will be ignored and reconstituted from
1589          *     the more specific suppressed visual effect bits. Apps should migrate to targeting
1590          *     specific effects instead of the deprecated {@link #SUPPRESSED_EFFECT_SCREEN_ON} and
1591          *     {@link #SUPPRESSED_EFFECT_SCREEN_OFF} effects.
1592          *
1593          * @param priorityCategories bitmask of categories of notifications that can bypass DND.
1594          * @param priorityCallSenders which callers can bypass DND.
1595          * @param priorityMessageSenders which message senders can bypass DND.
1596          * @param suppressedVisualEffects which visual interruptions should be suppressed from
1597          *                                notifications that are filtered by DND.
1598          */
Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders, int suppressedVisualEffects)1599         public Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders,
1600                 int suppressedVisualEffects) {
1601             this.priorityCategories = priorityCategories;
1602             this.priorityCallSenders = priorityCallSenders;
1603             this.priorityMessageSenders = priorityMessageSenders;
1604             this.suppressedVisualEffects = suppressedVisualEffects;
1605             this.state = STATE_UNSET;
1606         }
1607 
1608         /** @hide */
Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders, int suppressedVisualEffects, int state)1609         public Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders,
1610                 int suppressedVisualEffects, int state) {
1611             this.priorityCategories = priorityCategories;
1612             this.priorityCallSenders = priorityCallSenders;
1613             this.priorityMessageSenders = priorityMessageSenders;
1614             this.suppressedVisualEffects = suppressedVisualEffects;
1615             this.state = state;
1616         }
1617 
1618         /** @hide */
Policy(Parcel source)1619         public Policy(Parcel source) {
1620             this(source.readInt(), source.readInt(), source.readInt(), source.readInt(),
1621                     source.readInt());
1622         }
1623 
1624         @Override
writeToParcel(Parcel dest, int flags)1625         public void writeToParcel(Parcel dest, int flags) {
1626             dest.writeInt(priorityCategories);
1627             dest.writeInt(priorityCallSenders);
1628             dest.writeInt(priorityMessageSenders);
1629             dest.writeInt(suppressedVisualEffects);
1630             dest.writeInt(state);
1631         }
1632 
1633         @Override
describeContents()1634         public int describeContents() {
1635             return 0;
1636         }
1637 
1638         @Override
hashCode()1639         public int hashCode() {
1640             return Objects.hash(priorityCategories, priorityCallSenders, priorityMessageSenders,
1641                     suppressedVisualEffects);
1642         }
1643 
1644         @Override
equals(Object o)1645         public boolean equals(Object o) {
1646             if (!(o instanceof Policy)) return false;
1647             if (o == this) return true;
1648             final Policy other = (Policy) o;
1649             return other.priorityCategories == priorityCategories
1650                     && other.priorityCallSenders == priorityCallSenders
1651                     && other.priorityMessageSenders == priorityMessageSenders
1652                     && suppressedVisualEffectsEqual(suppressedVisualEffects,
1653                     other.suppressedVisualEffects);
1654         }
1655 
1656 
suppressedVisualEffectsEqual(int suppressedEffects, int otherSuppressedVisualEffects)1657         private boolean suppressedVisualEffectsEqual(int suppressedEffects,
1658                 int otherSuppressedVisualEffects) {
1659             if (suppressedEffects == otherSuppressedVisualEffects) {
1660                 return true;
1661             }
1662 
1663             if ((suppressedEffects & SUPPRESSED_EFFECT_SCREEN_ON) != 0) {
1664                 suppressedEffects |= SUPPRESSED_EFFECT_PEEK;
1665             }
1666             if ((suppressedEffects & SUPPRESSED_EFFECT_SCREEN_OFF) != 0) {
1667                 suppressedEffects |= SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
1668                 suppressedEffects |= SUPPRESSED_EFFECT_LIGHTS;
1669                 suppressedEffects |= SUPPRESSED_EFFECT_AMBIENT;
1670             }
1671 
1672             if ((otherSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_ON) != 0) {
1673                 otherSuppressedVisualEffects |= SUPPRESSED_EFFECT_PEEK;
1674             }
1675             if ((otherSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_OFF) != 0) {
1676                 otherSuppressedVisualEffects |= SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
1677                 otherSuppressedVisualEffects |= SUPPRESSED_EFFECT_LIGHTS;
1678                 otherSuppressedVisualEffects |= SUPPRESSED_EFFECT_AMBIENT;
1679             }
1680 
1681             if ((suppressedEffects & SUPPRESSED_EFFECT_SCREEN_ON)
1682                     != (otherSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_ON)) {
1683                 int currSuppressedEffects = (suppressedEffects & SUPPRESSED_EFFECT_SCREEN_ON) != 0
1684                         ? otherSuppressedVisualEffects : suppressedEffects;
1685                 if ((currSuppressedEffects & SUPPRESSED_EFFECT_PEEK) == 0) {
1686                     return false;
1687                 }
1688             }
1689 
1690             if ((suppressedEffects & SUPPRESSED_EFFECT_SCREEN_OFF)
1691                     != (otherSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_OFF)) {
1692                 int currSuppressedEffects = (suppressedEffects & SUPPRESSED_EFFECT_SCREEN_OFF) != 0
1693                         ? otherSuppressedVisualEffects : suppressedEffects;
1694                 if ((currSuppressedEffects & SUPPRESSED_EFFECT_FULL_SCREEN_INTENT) == 0
1695                         || (currSuppressedEffects & SUPPRESSED_EFFECT_LIGHTS) == 0
1696                         || (currSuppressedEffects & SUPPRESSED_EFFECT_AMBIENT) == 0) {
1697                     return false;
1698                 }
1699             }
1700 
1701             int thisWithoutOldEffects = suppressedEffects
1702                     & ~SUPPRESSED_EFFECT_SCREEN_ON
1703                     & ~SUPPRESSED_EFFECT_SCREEN_OFF;
1704             int otherWithoutOldEffects = otherSuppressedVisualEffects
1705                     & ~SUPPRESSED_EFFECT_SCREEN_ON
1706                     & ~SUPPRESSED_EFFECT_SCREEN_OFF;
1707             return thisWithoutOldEffects == otherWithoutOldEffects;
1708         }
1709 
1710         @Override
toString()1711         public String toString() {
1712             return "NotificationManager.Policy["
1713                     + "priorityCategories=" + priorityCategoriesToString(priorityCategories)
1714                     + ",priorityCallSenders=" + prioritySendersToString(priorityCallSenders)
1715                     + ",priorityMessageSenders=" + prioritySendersToString(priorityMessageSenders)
1716                     + ",suppressedVisualEffects="
1717                     + suppressedEffectsToString(suppressedVisualEffects)
1718                     + ",areChannelsBypassingDnd=" + (((state & STATE_CHANNELS_BYPASSING_DND) != 0)
1719                         ? "true" : "false")
1720                     + "]";
1721         }
1722 
1723         /** @hide */
writeToProto(ProtoOutputStream proto, long fieldId)1724         public void writeToProto(ProtoOutputStream proto, long fieldId) {
1725             final long pToken = proto.start(fieldId);
1726 
1727             bitwiseToProtoEnum(proto, PolicyProto.PRIORITY_CATEGORIES, priorityCategories);
1728             proto.write(PolicyProto.PRIORITY_CALL_SENDER, priorityCallSenders);
1729             proto.write(PolicyProto.PRIORITY_MESSAGE_SENDER, priorityMessageSenders);
1730             bitwiseToProtoEnum(
1731                     proto, PolicyProto.SUPPRESSED_VISUAL_EFFECTS, suppressedVisualEffects);
1732 
1733             proto.end(pToken);
1734         }
1735 
bitwiseToProtoEnum(ProtoOutputStream proto, long fieldId, int data)1736         private static void bitwiseToProtoEnum(ProtoOutputStream proto, long fieldId, int data) {
1737             for (int i = 1; data > 0; ++i, data >>>= 1) {
1738                 if ((data & 1) == 1) {
1739                     proto.write(fieldId, i);
1740                 }
1741             }
1742         }
1743 
1744         /**
1745          * @hide
1746          */
getAllSuppressedVisualEffects()1747         public static int getAllSuppressedVisualEffects() {
1748             int effects = 0;
1749             for (int i = 0; i < ALL_SUPPRESSED_EFFECTS.length; i++) {
1750                 effects |= ALL_SUPPRESSED_EFFECTS[i];
1751             }
1752             return effects;
1753         }
1754 
1755         /**
1756          * @hide
1757          */
areAllVisualEffectsSuppressed(int effects)1758         public static boolean areAllVisualEffectsSuppressed(int effects) {
1759             for (int i = 0; i < ALL_SUPPRESSED_EFFECTS.length; i++) {
1760                 final int effect = ALL_SUPPRESSED_EFFECTS[i];
1761                 if ((effects & effect) == 0) {
1762                     return false;
1763                 }
1764             }
1765             return true;
1766         }
1767 
toggleEffects(int currentEffects, int[] effects, boolean suppress)1768         private static int toggleEffects(int currentEffects, int[] effects, boolean suppress) {
1769             for (int i = 0; i < effects.length; i++) {
1770                 final int effect = effects[i];
1771                 if (suppress) {
1772                     currentEffects |= effect;
1773                 } else {
1774                     currentEffects &= ~effect;
1775                 }
1776             }
1777             return currentEffects;
1778         }
1779 
suppressedEffectsToString(int effects)1780         public static String suppressedEffectsToString(int effects) {
1781             if (effects <= 0) return "";
1782             final StringBuilder sb = new StringBuilder();
1783             for (int i = 0; i < ALL_SUPPRESSED_EFFECTS.length; i++) {
1784                 final int effect = ALL_SUPPRESSED_EFFECTS[i];
1785                 if ((effects & effect) != 0) {
1786                     if (sb.length() > 0) sb.append(',');
1787                     sb.append(effectToString(effect));
1788                 }
1789                 effects &= ~effect;
1790             }
1791             if (effects != 0) {
1792                 if (sb.length() > 0) sb.append(',');
1793                 sb.append("UNKNOWN_").append(effects);
1794             }
1795             return sb.toString();
1796         }
1797 
priorityCategoriesToString(int priorityCategories)1798         public static String priorityCategoriesToString(int priorityCategories) {
1799             if (priorityCategories == 0) return "";
1800             final StringBuilder sb = new StringBuilder();
1801             for (int i = 0; i < ALL_PRIORITY_CATEGORIES.length; i++) {
1802                 final int priorityCategory = ALL_PRIORITY_CATEGORIES[i];
1803                 if ((priorityCategories & priorityCategory) != 0) {
1804                     if (sb.length() > 0) sb.append(',');
1805                     sb.append(priorityCategoryToString(priorityCategory));
1806                 }
1807                 priorityCategories &= ~priorityCategory;
1808             }
1809             if (priorityCategories != 0) {
1810                 if (sb.length() > 0) sb.append(',');
1811                 sb.append("PRIORITY_CATEGORY_UNKNOWN_").append(priorityCategories);
1812             }
1813             return sb.toString();
1814         }
1815 
effectToString(int effect)1816         private static String effectToString(int effect) {
1817             switch (effect) {
1818                 case SUPPRESSED_EFFECT_FULL_SCREEN_INTENT:
1819                     return "SUPPRESSED_EFFECT_FULL_SCREEN_INTENT";
1820                 case SUPPRESSED_EFFECT_LIGHTS:
1821                     return "SUPPRESSED_EFFECT_LIGHTS";
1822                 case SUPPRESSED_EFFECT_PEEK:
1823                     return "SUPPRESSED_EFFECT_PEEK";
1824                 case SUPPRESSED_EFFECT_STATUS_BAR:
1825                     return "SUPPRESSED_EFFECT_STATUS_BAR";
1826                 case SUPPRESSED_EFFECT_BADGE:
1827                     return "SUPPRESSED_EFFECT_BADGE";
1828                 case SUPPRESSED_EFFECT_AMBIENT:
1829                     return "SUPPRESSED_EFFECT_AMBIENT";
1830                 case SUPPRESSED_EFFECT_NOTIFICATION_LIST:
1831                     return "SUPPRESSED_EFFECT_NOTIFICATION_LIST";
1832                 case SUPPRESSED_EFFECT_SCREEN_OFF:
1833                     return "SUPPRESSED_EFFECT_SCREEN_OFF";
1834                 case SUPPRESSED_EFFECT_SCREEN_ON:
1835                     return "SUPPRESSED_EFFECT_SCREEN_ON";
1836                 case SUPPRESSED_EFFECTS_UNSET:
1837                     return "SUPPRESSED_EFFECTS_UNSET";
1838                 default: return "UNKNOWN_" + effect;
1839             }
1840         }
1841 
priorityCategoryToString(int priorityCategory)1842         private static String priorityCategoryToString(int priorityCategory) {
1843             switch (priorityCategory) {
1844                 case PRIORITY_CATEGORY_REMINDERS: return "PRIORITY_CATEGORY_REMINDERS";
1845                 case PRIORITY_CATEGORY_EVENTS: return "PRIORITY_CATEGORY_EVENTS";
1846                 case PRIORITY_CATEGORY_MESSAGES: return "PRIORITY_CATEGORY_MESSAGES";
1847                 case PRIORITY_CATEGORY_CALLS: return "PRIORITY_CATEGORY_CALLS";
1848                 case PRIORITY_CATEGORY_REPEAT_CALLERS: return "PRIORITY_CATEGORY_REPEAT_CALLERS";
1849                 case PRIORITY_CATEGORY_ALARMS: return "PRIORITY_CATEGORY_ALARMS";
1850                 case PRIORITY_CATEGORY_MEDIA: return "PRIORITY_CATEGORY_MEDIA";
1851                 case PRIORITY_CATEGORY_SYSTEM: return "PRIORITY_CATEGORY_SYSTEM";
1852                 default: return "PRIORITY_CATEGORY_UNKNOWN_" + priorityCategory;
1853             }
1854         }
1855 
prioritySendersToString(int prioritySenders)1856         public static String prioritySendersToString(int prioritySenders) {
1857             switch (prioritySenders) {
1858                 case PRIORITY_SENDERS_ANY: return "PRIORITY_SENDERS_ANY";
1859                 case PRIORITY_SENDERS_CONTACTS: return "PRIORITY_SENDERS_CONTACTS";
1860                 case PRIORITY_SENDERS_STARRED: return "PRIORITY_SENDERS_STARRED";
1861                 default: return "PRIORITY_SENDERS_UNKNOWN_" + prioritySenders;
1862             }
1863         }
1864 
1865         public static final @android.annotation.NonNull Parcelable.Creator<Policy> CREATOR = new Parcelable.Creator<Policy>() {
1866             @Override
1867             public Policy createFromParcel(Parcel in) {
1868                 return new Policy(in);
1869             }
1870 
1871             @Override
1872             public Policy[] newArray(int size) {
1873                 return new Policy[size];
1874             }
1875         };
1876 
1877         /** @hide **/
allowAlarms()1878         public boolean allowAlarms() {
1879             return (priorityCategories & PRIORITY_CATEGORY_ALARMS) != 0;
1880         }
1881 
1882         /** @hide **/
allowMedia()1883         public boolean allowMedia() {
1884             return (priorityCategories & PRIORITY_CATEGORY_MEDIA) != 0;
1885         }
1886 
1887         /** @hide **/
allowSystem()1888         public boolean allowSystem() {
1889             return (priorityCategories & PRIORITY_CATEGORY_SYSTEM) != 0;
1890         }
1891 
1892         /** @hide **/
allowRepeatCallers()1893         public boolean allowRepeatCallers() {
1894             return (priorityCategories & PRIORITY_CATEGORY_REPEAT_CALLERS) != 0;
1895         }
1896 
1897         /** @hide **/
allowCalls()1898         public boolean allowCalls() {
1899             return (priorityCategories & PRIORITY_CATEGORY_CALLS) != 0;
1900         }
1901 
1902         /** @hide **/
allowMessages()1903         public boolean allowMessages() {
1904             return (priorityCategories & PRIORITY_CATEGORY_MESSAGES) != 0;
1905         }
1906 
1907         /** @hide **/
allowEvents()1908         public boolean allowEvents() {
1909             return (priorityCategories & PRIORITY_CATEGORY_EVENTS) != 0;
1910         }
1911 
1912         /** @hide **/
allowReminders()1913         public boolean allowReminders() {
1914             return (priorityCategories & PRIORITY_CATEGORY_REMINDERS) != 0;
1915         }
1916 
1917         /** @hide **/
allowCallsFrom()1918         public int allowCallsFrom() {
1919             return priorityCallSenders;
1920         }
1921 
1922         /** @hide **/
allowMessagesFrom()1923         public int allowMessagesFrom() {
1924             return priorityMessageSenders;
1925         }
1926 
1927         /** @hide **/
showFullScreenIntents()1928         public boolean showFullScreenIntents() {
1929             return (suppressedVisualEffects & SUPPRESSED_EFFECT_FULL_SCREEN_INTENT) == 0;
1930         }
1931 
1932         /** @hide **/
showLights()1933         public boolean showLights() {
1934             return (suppressedVisualEffects & SUPPRESSED_EFFECT_LIGHTS) == 0;
1935         }
1936 
1937         /** @hide **/
showPeeking()1938         public boolean showPeeking() {
1939             return (suppressedVisualEffects & SUPPRESSED_EFFECT_PEEK) == 0;
1940         }
1941 
1942         /** @hide **/
showStatusBarIcons()1943         public boolean showStatusBarIcons() {
1944             return (suppressedVisualEffects & SUPPRESSED_EFFECT_STATUS_BAR) == 0;
1945         }
1946 
1947         /** @hide **/
showAmbient()1948         public boolean showAmbient() {
1949             return (suppressedVisualEffects & SUPPRESSED_EFFECT_AMBIENT) == 0;
1950         }
1951 
1952         /** @hide **/
showBadges()1953         public boolean showBadges() {
1954             return (suppressedVisualEffects & SUPPRESSED_EFFECT_BADGE) == 0;
1955         }
1956 
1957         /** @hide **/
showInNotificationList()1958         public boolean showInNotificationList() {
1959             return (suppressedVisualEffects & SUPPRESSED_EFFECT_NOTIFICATION_LIST) == 0;
1960         }
1961 
1962         /**
1963          * returns a deep copy of this policy
1964          * @hide
1965          */
copy()1966         public Policy copy() {
1967             final Parcel parcel = Parcel.obtain();
1968             try {
1969                 writeToParcel(parcel, 0);
1970                 parcel.setDataPosition(0);
1971                 return new Policy(parcel);
1972             } finally {
1973                 parcel.recycle();
1974             }
1975         }
1976     }
1977 
1978     /**
1979      * Recover a list of active notifications: ones that have been posted by the calling app that
1980      * have not yet been dismissed by the user or {@link #cancel(String, int)}ed by the app.
1981      *
1982      * <p><Each notification is embedded in a {@link StatusBarNotification} object, including the
1983      * original <code>tag</code> and <code>id</code> supplied to
1984      * {@link #notify(String, int, Notification) notify()}
1985      * (via {@link StatusBarNotification#getTag() getTag()} and
1986      * {@link StatusBarNotification#getId() getId()}) as well as a copy of the original
1987      * {@link Notification} object (via {@link StatusBarNotification#getNotification()}).
1988      * </p>
1989      * <p>From {@link Build.VERSION_CODES#Q}, will also return notifications you've posted as an
1990      * app's notification delegate via
1991      * {@link NotificationManager#notifyAsPackage(String, String, int, Notification)}.
1992      * </p>
1993      *
1994      * @return An array of {@link StatusBarNotification}.
1995      */
getActiveNotifications()1996     public StatusBarNotification[] getActiveNotifications() {
1997         final INotificationManager service = getService();
1998         final String pkg = mContext.getPackageName();
1999         try {
2000             final ParceledListSlice<StatusBarNotification> parceledList
2001                     = service.getAppActiveNotifications(pkg, mContext.getUserId());
2002             if (parceledList != null) {
2003                 final List<StatusBarNotification> list = parceledList.getList();
2004                 return list.toArray(new StatusBarNotification[list.size()]);
2005             }
2006         } catch (RemoteException e) {
2007             throw e.rethrowFromSystemServer();
2008         }
2009         return new StatusBarNotification[0];
2010     }
2011 
2012     /**
2013      * Gets the current notification interruption filter.
2014      * <p>
2015      * The interruption filter defines which notifications are allowed to
2016      * interrupt the user (e.g. via sound &amp; vibration) and is applied
2017      * globally.
2018      */
getCurrentInterruptionFilter()2019     public final @InterruptionFilter int getCurrentInterruptionFilter() {
2020         final INotificationManager service = getService();
2021         try {
2022             return zenModeToInterruptionFilter(service.getZenMode());
2023         } catch (RemoteException e) {
2024             throw e.rethrowFromSystemServer();
2025         }
2026     }
2027 
2028     /**
2029      * Sets the current notification interruption filter.
2030      * <p>
2031      * The interruption filter defines which notifications are allowed to
2032      * interrupt the user (e.g. via sound &amp; vibration) and is applied
2033      * globally.
2034      * <p>
2035      * Only available if policy access is granted to this package. See
2036      * {@link #isNotificationPolicyAccessGranted}.
2037      */
setInterruptionFilter(@nterruptionFilter int interruptionFilter)2038     public final void setInterruptionFilter(@InterruptionFilter int interruptionFilter) {
2039         final INotificationManager service = getService();
2040         try {
2041             service.setInterruptionFilter(mContext.getOpPackageName(), interruptionFilter);
2042         } catch (RemoteException e) {
2043             throw e.rethrowFromSystemServer();
2044         }
2045     }
2046 
2047     /** @hide */
zenModeToInterruptionFilter(int zen)2048     public static int zenModeToInterruptionFilter(int zen) {
2049         switch (zen) {
2050             case Global.ZEN_MODE_OFF: return INTERRUPTION_FILTER_ALL;
2051             case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return INTERRUPTION_FILTER_PRIORITY;
2052             case Global.ZEN_MODE_ALARMS: return INTERRUPTION_FILTER_ALARMS;
2053             case Global.ZEN_MODE_NO_INTERRUPTIONS: return INTERRUPTION_FILTER_NONE;
2054             default: return INTERRUPTION_FILTER_UNKNOWN;
2055         }
2056     }
2057 
2058     /** @hide */
zenModeFromInterruptionFilter(int interruptionFilter, int defValue)2059     public static int zenModeFromInterruptionFilter(int interruptionFilter, int defValue) {
2060         switch (interruptionFilter) {
2061             case INTERRUPTION_FILTER_ALL: return Global.ZEN_MODE_OFF;
2062             case INTERRUPTION_FILTER_PRIORITY: return Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
2063             case INTERRUPTION_FILTER_ALARMS: return Global.ZEN_MODE_ALARMS;
2064             case INTERRUPTION_FILTER_NONE:  return Global.ZEN_MODE_NO_INTERRUPTIONS;
2065             default: return defValue;
2066         }
2067     }
2068 }
2069