blob: 811b9c082be26b3e8dd1704cf49b6c1f9a290e03 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Julia Reynolds0edb50c2016-02-26 14:08:25 -050019import android.annotation.IntDef;
John Spurlock1fc476d2015-04-14 16:05:20 -040020import android.annotation.NonNull;
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -040021import android.annotation.Nullable;
John Spurlockb4782522014-08-22 14:54:46 -040022import android.annotation.SdkConstant;
Julia Reynolds12ad7ca2019-01-28 09:29:16 -050023import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060024import android.annotation.SystemService;
Julia Reynoldsb6c1f992016-11-22 09:26:46 -050025import android.annotation.TestApi;
Christoph Studer4600f9b2014-07-22 22:44:43 +020026import android.app.Notification.Builder;
Artur Satayevc895b1b2019-12-10 17:47:51 +000027import android.compat.annotation.UnsupportedAppUsage;
John Spurlockb4782522014-08-22 14:54:46 -040028import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.Context;
Julia Reynoldse0d711f2017-09-01 08:50:47 -040030import android.content.Intent;
Dan Sandler994349c2015-04-15 11:02:54 -040031import android.content.pm.ParceledListSlice;
Dan Sandlerd63f9322015-05-06 15:18:49 -040032import android.graphics.drawable.Icon;
John Spurlockb2278d62015-04-07 12:47:12 -040033import android.net.Uri;
Dan Sandler4e787062015-06-17 15:09:48 -040034import android.os.Build;
John Spurlock2b122f42014-08-27 16:29:47 -040035import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Handler;
37import android.os.IBinder;
John Spurlock1fc476d2015-04-14 16:05:20 -040038import android.os.Parcel;
39import android.os.Parcelable;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070040import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.ServiceManager;
Jeff Sharkeya14acd22013-04-02 18:27:45 -070042import android.os.StrictMode;
Dianne Hackborn41203752012-08-31 14:05:51 -070043import android.os.UserHandle;
John Spurlockb2278d62015-04-07 12:47:12 -040044import android.provider.Settings.Global;
Julia Reynoldsad6dd352019-03-07 16:46:22 -050045import android.service.notification.Adjustment;
Julia Reynolds68062072018-08-06 15:38:21 -040046import android.service.notification.Condition;
Dan Sandler994349c2015-04-15 11:02:54 -040047import android.service.notification.StatusBarNotification;
John Spurlockcdb57ae2015-02-11 19:04:11 -050048import android.service.notification.ZenModeConfig;
Julia Reynolds24edc002020-01-29 16:35:32 -050049import android.service.notification.ZenPolicy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.Log;
Kweku Adams5ec78cd2017-09-25 16:29:54 -070051import android.util.proto.ProtoOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
Julia Reynolds0edb50c2016-02-26 14:08:25 -050053import java.lang.annotation.Retention;
54import java.lang.annotation.RetentionPolicy;
Beverlyc629ee42018-10-02 16:14:07 -040055import java.util.ArrayList;
Geoffrey Pitsch03533712017-01-05 10:30:07 -050056import java.util.Arrays;
Julia Reynolds361e82d32016-02-26 18:19:49 -050057import java.util.HashMap;
Chris Wren5ab5c742016-05-10 15:32:23 -040058import java.util.List;
Julia Reynolds361e82d32016-02-26 18:19:49 -050059import java.util.Map;
John Spurlock1fc476d2015-04-14 16:05:20 -040060import java.util.Objects;
61
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062/**
63 * Class to notify the user of events that happen. This is how you tell
64 * the user that something has happened in the background. {@more}
65 *
66 * Notifications can take different forms:
67 * <ul>
68 * <li>A persistent icon that goes in the status bar and is accessible
69 * through the launcher, (when the user selects it, a designated Intent
70 * can be launched),</li>
71 * <li>Turning on or flashing LEDs on the device, or</li>
72 * <li>Alerting the user by flashing the backlight, playing a sound,
73 * or vibrating.</li>
74 * </ul>
75 *
76 * <p>
Peter Collingbourneb97c3492010-10-13 20:04:52 +010077 * Each of the notify methods takes an int id parameter and optionally a
78 * {@link String} tag parameter, which may be {@code null}. These parameters
79 * are used to form a pair (tag, id), or ({@code null}, id) if tag is
80 * unspecified. This pair identifies this notification from your app to the
81 * system, so that pair should be unique within your app. If you call one
82 * of the notify methods with a (tag, id) pair that is currently active and
83 * a new set of notification parameters, it will be updated. For example,
84 * if you pass a new status bar icon, the old icon in the status bar will
85 * be replaced with the new one. This is also the same tag and id you pass
86 * to the {@link #cancel(int)} or {@link #cancel(String, int)} method to clear
87 * this notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 *
Joe Fernandez558459f2011-10-13 16:47:36 -070089 * <div class="special reference">
90 * <h3>Developer Guides</h3>
91 * <p>For a guide to creating notifications, read the
92 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
93 * developer guide.</p>
94 * </div>
95 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * @see android.app.Notification
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060098@SystemService(Context.NOTIFICATION_SERVICE)
99public class NotificationManager {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 private static String TAG = "NotificationManager";
Joe Onorato43a17652011-04-06 19:22:23 -0700101 private static boolean localLOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
John Spurlockb4782522014-08-22 14:54:46 -0400103 /**
Julia Reynoldsfc9767b2018-01-22 17:45:16 -0500104 * Intent that is broadcast when an application is blocked or unblocked.
105 *
106 * This broadcast is only sent to the app whose block state has changed.
107 *
108 * Input: nothing
Julia Reynolds44ff7c92018-02-05 10:02:30 -0500109 * Output: {@link #EXTRA_BLOCKED_STATE}
Julia Reynoldsfc9767b2018-01-22 17:45:16 -0500110 */
111 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
112 public static final String ACTION_APP_BLOCK_STATE_CHANGED =
113 "android.app.action.APP_BLOCK_STATE_CHANGED";
114
115 /**
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -0500116 * Intent that is broadcast when a {@link NotificationChannel} is blocked
117 * (when {@link NotificationChannel#getImportance()} is {@link #IMPORTANCE_NONE}) or unblocked
118 * (when {@link NotificationChannel#getImportance()} is anything other than
119 * {@link #IMPORTANCE_NONE}).
120 *
121 * This broadcast is only sent to the app that owns the channel that has changed.
122 *
123 * Input: nothing
Julia Reynolds44ff7c92018-02-05 10:02:30 -0500124 * Output: {@link #EXTRA_NOTIFICATION_CHANNEL_ID}
125 * Output: {@link #EXTRA_BLOCKED_STATE}
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -0500126 */
127 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
128 public static final String ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED =
129 "android.app.action.NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED";
130
131 /**
Julia Reynolds44ff7c92018-02-05 10:02:30 -0500132 * Extra for {@link #ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED} containing the id of the
133 * {@link NotificationChannel} which has a new blocked state.
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -0500134 *
Julia Reynolds44ff7c92018-02-05 10:02:30 -0500135 * The value will be the {@link NotificationChannel#getId()} of the channel.
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -0500136 */
Julia Reynolds44ff7c92018-02-05 10:02:30 -0500137 public static final String EXTRA_NOTIFICATION_CHANNEL_ID =
138 "android.app.extra.NOTIFICATION_CHANNEL_ID";
139
140 /**
141 * Extra for {@link #ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED} containing the id
142 * of the {@link NotificationChannelGroup} which has a new blocked state.
143 *
144 * The value will be the {@link NotificationChannelGroup#getId()} of the group.
145 */
146 public static final String EXTRA_NOTIFICATION_CHANNEL_GROUP_ID =
147 "android.app.extra.NOTIFICATION_CHANNEL_GROUP_ID";
148
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -0500149
150 /**
151 * Extra for {@link #ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED} or
152 * {@link #ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED} containing the new blocked
153 * state as a boolean.
154 *
155 * The value will be {@code true} if this channel or group is now blocked and {@code false} if
156 * this channel or group is now unblocked.
157 */
158 public static final String EXTRA_BLOCKED_STATE = "android.app.extra.BLOCKED_STATE";
159
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -0500160 /**
161 * Intent that is broadcast when a {@link NotificationChannelGroup} is
162 * {@link NotificationChannelGroup#isBlocked() blocked} or unblocked.
163 *
164 * This broadcast is only sent to the app that owns the channel group that has changed.
165 *
166 * Input: nothing
Julia Reynolds44ff7c92018-02-05 10:02:30 -0500167 * Output: {@link #EXTRA_NOTIFICATION_CHANNEL_GROUP_ID}
168 * Output: {@link #EXTRA_BLOCKED_STATE}
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -0500169 */
170 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
171 public static final String ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED =
172 "android.app.action.NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED";
173
174 /**
Julia Reynolds3ec93302019-07-11 15:27:31 -0400175 * Intent that is broadcast when the status of an {@link AutomaticZenRule} has changed.
176 *
177 * <p>Use this to know whether you need to continue monitor to device state in order to
178 * provide up-to-date states (with {@link #setAutomaticZenRuleState(String, Condition)}) for
179 * this rule.</p>
180 *
181 * Input: nothing
182 * Output: {@link #EXTRA_AUTOMATIC_ZEN_RULE_ID}
183 * Output: {@link #EXTRA_AUTOMATIC_ZEN_RULE_STATUS}
184 */
185 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
186 public static final String ACTION_AUTOMATIC_ZEN_RULE_STATUS_CHANGED =
187 "android.app.action.AUTOMATIC_ZEN_RULE_STATUS_CHANGED";
188
189 /**
190 * Integer extra for {@link #ACTION_AUTOMATIC_ZEN_RULE_STATUS_CHANGED} containing the state of
191 * the {@link AutomaticZenRule}.
192 *
193 * <p>
194 * The value will be one of {@link #AUTOMATIC_RULE_STATUS_ENABLED},
195 * {@link #AUTOMATIC_RULE_STATUS_DISABLED}, {@link #AUTOMATIC_RULE_STATUS_REMOVED},
196 * {@link #AUTOMATIC_RULE_STATUS_UNKNOWN}.
197 * </p>
198 */
199 public static final String EXTRA_AUTOMATIC_ZEN_RULE_STATUS =
200 "android.app.extra.AUTOMATIC_ZEN_RULE_STATUS";
201
202 /**
203 * String extra for {@link #ACTION_AUTOMATIC_ZEN_RULE_STATUS_CHANGED} containing the id of the
204 * {@link AutomaticZenRule} (see {@link #addAutomaticZenRule(AutomaticZenRule)}) that has
205 * changed.
206 */
207 public static final String EXTRA_AUTOMATIC_ZEN_RULE_ID =
208 "android.app.extra.AUTOMATIC_ZEN_RULE_ID";
209
210 /** @hide */
211 @IntDef(prefix = { "AUTOMATIC_RULE_STATUS" }, value = {
212 AUTOMATIC_RULE_STATUS_ENABLED, AUTOMATIC_RULE_STATUS_DISABLED,
213 AUTOMATIC_RULE_STATUS_REMOVED, AUTOMATIC_RULE_STATUS_UNKNOWN
214 })
215 @Retention(RetentionPolicy.SOURCE)
216 public @interface AutomaticZenRuleStatus {}
217
218 /**
219 * Constant value for {@link #EXTRA_AUTOMATIC_ZEN_RULE_STATUS} - the current status of the
220 * rule is unknown at your target sdk version, and you should continue to provide state changes
221 * via {@link #setAutomaticZenRuleState(String, Condition)}.
222 */
223 public static final int AUTOMATIC_RULE_STATUS_UNKNOWN = -1;
224
225 /**
226 * Constant value for {@link #EXTRA_AUTOMATIC_ZEN_RULE_STATUS} - the given rule currently
227 * exists and is enabled. You should continue to provide state changes via
228 * {@link #setAutomaticZenRuleState(String, Condition)}.
229 */
230 public static final int AUTOMATIC_RULE_STATUS_ENABLED = 1;
231
232 /**
233 * Constant value for {@link #EXTRA_AUTOMATIC_ZEN_RULE_STATUS} - the given rule currently
234 * exists but is disabled. You do not need to continue to provide state changes via
235 * {@link #setAutomaticZenRuleState(String, Condition)} until the rule is reenabled.
236 */
237 public static final int AUTOMATIC_RULE_STATUS_DISABLED = 2;
238
239 /**
240 * Constant value for {@link #EXTRA_AUTOMATIC_ZEN_RULE_STATUS} - the given rule has been
241 * deleted. Further calls to {@link #setAutomaticZenRuleState(String, Condition)} will be
242 * ignored.
243 */
244 public static final int AUTOMATIC_RULE_STATUS_REMOVED = 3;
245
246 /**
John Spurlockb4782522014-08-22 14:54:46 -0400247 * Intent that is broadcast when the state of {@link #getEffectsSuppressor()} changes.
248 * This broadcast is only sent to registered receivers.
249 *
250 * @hide
251 */
252 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
253 public static final String ACTION_EFFECTS_SUPPRESSOR_CHANGED
254 = "android.os.action.ACTION_EFFECTS_SUPPRESSOR_CHANGED";
255
John Spurlock1fc476d2015-04-14 16:05:20 -0400256 /**
John Spurlock7c74f782015-06-04 13:01:42 -0400257 * Intent that is broadcast when the state of {@link #isNotificationPolicyAccessGranted()}
258 * changes.
259 *
260 * This broadcast is only sent to registered receivers, and only to the apps that have changed.
261 */
262 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
263 public static final String ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED
264 = "android.app.action.NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED";
265
266 /**
John Spurlock1fc476d2015-04-14 16:05:20 -0400267 * Intent that is broadcast when the state of getNotificationPolicy() changes.
268 * This broadcast is only sent to registered receivers.
269 */
270 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
271 public static final String ACTION_NOTIFICATION_POLICY_CHANGED
272 = "android.app.action.NOTIFICATION_POLICY_CHANGED";
273
John Spurlock80774932015-05-07 17:38:50 -0400274 /**
275 * Intent that is broadcast when the state of getCurrentInterruptionFilter() changes.
276 * This broadcast is only sent to registered receivers.
277 */
278 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
279 public static final String ACTION_INTERRUPTION_FILTER_CHANGED
280 = "android.app.action.INTERRUPTION_FILTER_CHANGED";
281
282 /**
Jason Monka9927322015-12-13 16:22:37 -0500283 * Intent that is broadcast when the state of getCurrentInterruptionFilter() changes.
284 * @hide
285 */
286 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
287 public static final String ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL
288 = "android.app.action.INTERRUPTION_FILTER_CHANGED_INTERNAL";
289
Julia Reynolds0edb50c2016-02-26 14:08:25 -0500290 /** @hide */
Jeff Sharkey6503bd82017-04-19 23:24:18 -0600291 @IntDef(prefix = { "INTERRUPTION_FILTER_" }, value = {
292 INTERRUPTION_FILTER_NONE, INTERRUPTION_FILTER_PRIORITY, INTERRUPTION_FILTER_ALARMS,
293 INTERRUPTION_FILTER_ALL, INTERRUPTION_FILTER_UNKNOWN
294 })
Julia Reynolds0edb50c2016-02-26 14:08:25 -0500295 @Retention(RetentionPolicy.SOURCE)
296 public @interface InterruptionFilter {}
297
Jason Monka9927322015-12-13 16:22:37 -0500298 /**
John Spurlock80774932015-05-07 17:38:50 -0400299 * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
Julia Reynoldsdd0605b2016-04-06 10:26:54 -0400300 * Normal interruption filter - no notifications are suppressed.
John Spurlock80774932015-05-07 17:38:50 -0400301 */
302 public static final int INTERRUPTION_FILTER_ALL = 1;
303
304 /**
305 * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
Julia Reynoldsdd0605b2016-04-06 10:26:54 -0400306 * Priority interruption filter - all notifications are suppressed except those that match
307 * the priority criteria. Some audio streams are muted. See
308 * {@link Policy#priorityCallSenders}, {@link Policy#priorityCategories},
309 * {@link Policy#priorityMessageSenders} to define or query this criteria. Users can
310 * additionally specify packages that can bypass this interruption filter.
John Spurlock80774932015-05-07 17:38:50 -0400311 */
312 public static final int INTERRUPTION_FILTER_PRIORITY = 2;
313
314 /**
315 * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
Julia Reynoldsdd0605b2016-04-06 10:26:54 -0400316 * No interruptions filter - all notifications are suppressed and all audio streams (except
317 * those used for phone calls) and vibrations are muted.
John Spurlock80774932015-05-07 17:38:50 -0400318 */
319 public static final int INTERRUPTION_FILTER_NONE = 3;
320
321 /**
322 * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
Julia Reynoldsdd0605b2016-04-06 10:26:54 -0400323 * Alarms only interruption filter - all notifications except those of category
324 * {@link Notification#CATEGORY_ALARM} are suppressed. Some audio streams are muted.
John Spurlock80774932015-05-07 17:38:50 -0400325 */
326 public static final int INTERRUPTION_FILTER_ALARMS = 4;
327
328 /** {@link #getCurrentInterruptionFilter() Interruption filter} constant - returned when
329 * the value is unavailable for any reason.
330 */
331 public static final int INTERRUPTION_FILTER_UNKNOWN = 0;
332
Chris Wren5ab5c742016-05-10 15:32:23 -0400333 /** @hide */
Jeff Sharkey6503bd82017-04-19 23:24:18 -0600334 @IntDef(prefix = { "IMPORTANCE_" }, value = {
335 IMPORTANCE_UNSPECIFIED, IMPORTANCE_NONE,
336 IMPORTANCE_MIN, IMPORTANCE_LOW, IMPORTANCE_DEFAULT, IMPORTANCE_HIGH
337 })
Chris Wren5ab5c742016-05-10 15:32:23 -0400338 @Retention(RetentionPolicy.SOURCE)
339 public @interface Importance {}
340
Julia Reynolds68062072018-08-06 15:38:21 -0400341 /**
342 * Activity Action: Launch an Automatic Zen Rule configuration screen
343 * <p>
344 * Input: Optionally, {@link #EXTRA_AUTOMATIC_RULE_ID}, if the configuration screen for an
345 * existing rule should be displayed. If the rule id is missing or null, apps should display
346 * a configuration screen where users can create a new instance of the rule.
347 * <p>
348 * Output: Nothing
349 * <p>
350 * You can have multiple activities handling this intent, if you support multiple
351 * {@link AutomaticZenRule rules}. In order for the system to properly display all of your
352 * rule types so that users can create new instances or configure existing ones, you need
353 * to add some extra metadata ({@link #META_DATA_AUTOMATIC_RULE_TYPE})
354 * to your activity tag in your manifest. If you'd like to limit the number of rules a user
355 * can create from this flow, you can additionally optionally include
356 * {@link #META_DATA_RULE_INSTANCE_LIMIT}.
357 *
358 * For example,
359 * &lt;meta-data
360 * android:name="android.app.zen.automatic.ruleType"
361 * android:value="@string/my_condition_rule">
362 * &lt;/meta-data>
363 * &lt;meta-data
364 * android:name="android.app.zen.automatic.ruleInstanceLimit"
365 * android:value="1">
366 * &lt;/meta-data>
367 * </p>
368 * </p>
369 *
370 * @see {@link #addAutomaticZenRule(AutomaticZenRule)}
371 */
372 @SdkConstant(SdkConstant.SdkConstantType.ACTIVITY_INTENT_ACTION)
373 public static final String ACTION_AUTOMATIC_ZEN_RULE =
374 "android.app.action.AUTOMATIC_ZEN_RULE";
375
376 /**
377 * Used as an optional string extra on {@link #ACTION_AUTOMATIC_ZEN_RULE} intents. If
378 * provided, contains the id of the {@link AutomaticZenRule} (as returned from
379 * {@link NotificationManager#addAutomaticZenRule(AutomaticZenRule)}) for which configuration
380 * settings should be displayed.
381 */
382 public static final String EXTRA_AUTOMATIC_RULE_ID = "android.app.extra.AUTOMATIC_RULE_ID";
383
384 /**
385 * A required {@code meta-data} tag for activities that handle
386 * {@link #ACTION_AUTOMATIC_ZEN_RULE}.
387 *
388 * This tag should contain a localized name of the type of the zen rule provided by the
389 * activity.
390 */
Julia Reynolds395bf7052019-03-21 11:41:17 -0400391 public static final String META_DATA_AUTOMATIC_RULE_TYPE =
392 "android.service.zen.automatic.ruleType";
Julia Reynolds68062072018-08-06 15:38:21 -0400393
394 /**
395 * An optional {@code meta-data} tag for activities that handle
396 * {@link #ACTION_AUTOMATIC_ZEN_RULE}.
397 *
398 * This tag should contain the maximum number of rule instances that
399 * can be created for this rule type. Omit or enter a value <= 0 to allow unlimited instances.
400 */
401 public static final String META_DATA_RULE_INSTANCE_LIMIT =
Julia Reynolds395bf7052019-03-21 11:41:17 -0400402 "android.service.zen.automatic.ruleInstanceLimit";
Julia Reynolds68062072018-08-06 15:38:21 -0400403
Chris Wren5ab5c742016-05-10 15:32:23 -0400404 /** Value signifying that the user has not expressed a per-app visibility override value.
405 * @hide */
406 public static final int VISIBILITY_NO_OVERRIDE = -1000;
Julia Reynolds85769912016-10-25 09:08:57 -0400407
Chris Wren5ab5c742016-05-10 15:32:23 -0400408 /**
409 * Value signifying that the user has not expressed an importance.
410 *
411 * This value is for persisting preferences, and should never be associated with
412 * an actual notification.
413 */
414 public static final int IMPORTANCE_UNSPECIFIED = -1000;
415
416 /**
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500417 * A notification with no importance: does not show in the shade.
Chris Wren5ab5c742016-05-10 15:32:23 -0400418 */
419 public static final int IMPORTANCE_NONE = 0;
420
421 /**
Dianne Hackborn6e5bd3f2017-07-31 14:22:04 -0700422 * Min notification importance: only shows in the shade, below the fold. This should
423 * not be used with {@link Service#startForeground(int, Notification) Service.startForeground}
424 * since a foreground service is supposed to be something the user cares about so it does
425 * not make semantic sense to mark its notification as minimum importance. If you do this
426 * as of Android version {@link android.os.Build.VERSION_CODES#O}, the system will show
427 * a higher-priority notification about your app running in the background.
Chris Wren5ab5c742016-05-10 15:32:23 -0400428 */
429 public static final int IMPORTANCE_MIN = 1;
430
431 /**
Julia Reynolds12ad7ca2019-01-28 09:29:16 -0500432 * Low notification importance: Shows in the shade, and potentially in the status bar
433 * (see {@link #shouldHideSilentStatusBarIcons()}), but is not audibly intrusive.
Chris Wren5ab5c742016-05-10 15:32:23 -0400434 */
435 public static final int IMPORTANCE_LOW = 2;
436
437 /**
Julia Reynolds85769912016-10-25 09:08:57 -0400438 * Default notification importance: shows everywhere, makes noise, but does not visually
439 * intrude.
Chris Wren5ab5c742016-05-10 15:32:23 -0400440 */
441 public static final int IMPORTANCE_DEFAULT = 3;
442
443 /**
Julia Reynolds85769912016-10-25 09:08:57 -0400444 * Higher notification importance: shows everywhere, makes noise and peeks. May use full screen
445 * intents.
Chris Wren5ab5c742016-05-10 15:32:23 -0400446 */
447 public static final int IMPORTANCE_HIGH = 4;
448
449 /**
Julia Reynolds85769912016-10-25 09:08:57 -0400450 * Unused.
Chris Wren5ab5c742016-05-10 15:32:23 -0400451 */
452 public static final int IMPORTANCE_MAX = 5;
453
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100454 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 private static INotificationManager sService;
456
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700457 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100458 @UnsupportedAppUsage
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700459 static public INotificationManager getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 {
461 if (sService != null) {
462 return sService;
463 }
464 IBinder b = ServiceManager.getService("notification");
465 sService = INotificationManager.Stub.asInterface(b);
466 return sService;
467 }
468
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100469 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 /*package*/ NotificationManager(Context context, Handler handler)
471 {
472 mContext = context;
473 }
474
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700475 /** {@hide} */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100476 @UnsupportedAppUsage
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700477 public static NotificationManager from(Context context) {
478 return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
479 }
480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 /**
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500482 * Post a notification to be shown in the status bar. If a notification with
483 * the same id has already been posted by your application and has not yet been canceled, it
484 * will be replaced by the updated information.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 *
486 * @param id An identifier for this notification unique within your
487 * application.
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500488 * @param notification A {@link Notification} object describing what to show the user. Must not
489 * be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 */
491 public void notify(int id, Notification notification)
492 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700493 notify(null, id, notification);
494 }
495
496 /**
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400497 * Posts a notification to be shown in the status bar. If a notification with
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500498 * the same tag and id has already been posted by your application and has not yet been
499 * canceled, it will be replaced by the updated information.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700500 *
Julia Reynoldse0d711f2017-09-01 08:50:47 -0400501 * All {@link android.service.notification.NotificationListenerService listener services} will
502 * be granted {@link Intent#FLAG_GRANT_READ_URI_PERMISSION} access to any {@link Uri uris}
503 * provided on this notification or the
504 * {@link NotificationChannel} this notification is posted to using
505 * {@link Context#grantUriPermission(String, Uri, int)}. Permission will be revoked when the
506 * notification is canceled, or you can revoke permissions with
507 * {@link Context#revokeUriPermission(Uri, int)}.
508 *
Peter Collingbourneb97c3492010-10-13 20:04:52 +0100509 * @param tag A string identifier for this notification. May be {@code null}.
510 * @param id An identifier for this notification. The pair (tag, id) must be unique
511 * within your application.
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500512 * @param notification A {@link Notification} object describing what to
513 * show the user. Must not be null.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700514 */
515 public void notify(String tag, int id, Notification notification)
516 {
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700517 notifyAsUser(tag, id, notification, mContext.getUser());
Dianne Hackborn41203752012-08-31 14:05:51 -0700518 }
519
520 /**
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400521 * Posts a notification as a specified package to be shown in the status bar. If a notification
522 * with the same tag and id has already been posted for that package and has not yet been
523 * canceled, it will be replaced by the updated information.
524 *
525 * All {@link android.service.notification.NotificationListenerService listener services} will
526 * be granted {@link Intent#FLAG_GRANT_READ_URI_PERMISSION} access to any {@link Uri uris}
527 * provided on this notification or the
528 * {@link NotificationChannel} this notification is posted to using
529 * {@link Context#grantUriPermission(String, Uri, int)}. Permission will be revoked when the
530 * notification is canceled, or you can revoke permissions with
531 * {@link Context#revokeUriPermission(Uri, int)}.
532 *
533 * @param targetPackage The package to post the notification as. The package must have granted
534 * you access to post notifications on their behalf with
535 * {@link #setNotificationDelegate(String)}.
536 * @param tag A string identifier for this notification. May be {@code null}.
537 * @param id An identifier for this notification. The pair (tag, id) must be unique
538 * within your application.
539 * @param notification A {@link Notification} object describing what to
540 * show the user. Must not be null.
541 */
Julia Reynoldse4a47dd2019-06-07 13:40:59 -0400542 public void notifyAsPackage(@NonNull String targetPackage, @Nullable String tag, int id,
Fabian Kozynski867550e2019-02-28 12:59:57 -0500543 @NonNull Notification notification) {
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400544 INotificationManager service = getService();
545 String sender = mContext.getPackageName();
546
547 try {
548 if (localLOGV) Log.v(TAG, sender + ": notify(" + id + ", " + notification + ")");
549 service.enqueueNotificationWithTag(targetPackage, sender, tag, id,
550 fixNotification(notification), mContext.getUser().getIdentifier());
551 } catch (RemoteException e) {
552 throw e.rethrowFromSystemServer();
553 }
554 }
555
556 /**
Dianne Hackborn41203752012-08-31 14:05:51 -0700557 * @hide
558 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100559 @UnsupportedAppUsage
Dianne Hackborn41203752012-08-31 14:05:51 -0700560 public void notifyAsUser(String tag, int id, Notification notification, UserHandle user)
561 {
Dianne Hackborn41203752012-08-31 14:05:51 -0700562 INotificationManager service = getService();
563 String pkg = mContext.getPackageName();
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400564
565 try {
566 if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
567 service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id,
568 fixNotification(notification), user.getIdentifier());
569 } catch (RemoteException e) {
570 throw e.rethrowFromSystemServer();
571 }
572 }
573
574 private Notification fixNotification(Notification notification) {
575 String pkg = mContext.getPackageName();
Julia Reynoldsda303542015-11-23 14:00:20 -0500576 // Fix the notification as best we can.
577 Notification.addFieldsFromContext(mContext, notification);
Julia Reynoldse0d711f2017-09-01 08:50:47 -0400578
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700579 if (notification.sound != null) {
580 notification.sound = notification.sound.getCanonicalUri();
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700581 if (StrictMode.vmFileUriExposureEnabled()) {
Jeff Sharkeyac3be9a2016-02-01 10:39:30 -0700582 notification.sound.checkFileUriExposed("Notification.sound");
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700583 }
Julia Reynoldse0d711f2017-09-01 08:50:47 -0400584
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700585 }
Dan Sandlerd63f9322015-05-06 15:18:49 -0400586 fixLegacySmallIcon(notification, pkg);
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400587 if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
588 if (notification.getSmallIcon() == null) {
589 throw new IllegalArgumentException("Invalid notification (no valid small icon): "
590 + notification);
591 }
592 }
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400593
Selim Cinekd0426622017-07-11 13:19:59 +0200594 notification.reduceImageSizes(mContext);
Rajeev Kumarbc79dcd2020-02-04 19:54:30 -0800595 return Builder.maybeCloneStrippedForDelivery(notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 }
597
Dan Sandlerd63f9322015-05-06 15:18:49 -0400598 private void fixLegacySmallIcon(Notification n, String pkg) {
599 if (n.getSmallIcon() == null && n.icon != 0) {
600 n.setSmallIcon(Icon.createWithResource(pkg, n.icon));
601 }
602 }
603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 /**
Julia Reynoldse4a47dd2019-06-07 13:40:59 -0400605 * Cancels a previously posted notification.
606 *
607 * <p>If the notification does not currently represent a
608 * {@link Service#startForeground(int, Notification) foreground service}, it will be
609 * removed from the UI and live
610 * {@link android.service.notification.NotificationListenerService notification listeners}
611 * will be informed so they can remove the notification from their UIs.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 */
613 public void cancel(int id)
614 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700615 cancel(null, id);
616 }
617
618 /**
Julia Reynoldse4a47dd2019-06-07 13:40:59 -0400619 * Cancels a previously posted notification.
620 *
621 * <p>If the notification does not currently represent a
622 * {@link Service#startForeground(int, Notification) foreground service}, it will be
623 * removed from the UI and live
624 * {@link android.service.notification.NotificationListenerService notification listeners}
625 * will be informed so they can remove the notification from their UIs.</p>
Fred Quintana6ecaff12009-09-25 14:23:13 -0700626 */
Julia Reynoldse4a47dd2019-06-07 13:40:59 -0400627 public void cancel(@Nullable String tag, int id)
Fred Quintana6ecaff12009-09-25 14:23:13 -0700628 {
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700629 cancelAsUser(tag, id, mContext.getUser());
Dianne Hackborn41203752012-08-31 14:05:51 -0700630 }
631
632 /**
Julia Reynoldse4a47dd2019-06-07 13:40:59 -0400633 * Cancels a previously posted notification.
634 *
635 * <p>If the notification does not currently represent a
636 * {@link Service#startForeground(int, Notification) foreground service}, it will be
637 * removed from the UI and live
638 * {@link android.service.notification.NotificationListenerService notification listeners}
639 * will be informed so they can remove the notification from their UIs.</p>
640 *
641 * <p>This method may be used by {@link #getNotificationDelegate() a notification delegate} to
642 * cancel notifications that they have posted via {@link #notifyAsPackage(String, String, int,
643 * Notification)}.</p>
644 *
645 * @param targetPackage The package to cancel the notification as. If this package is not your
646 * package, you can only cancel notifications you posted with
647 * {@link #notifyAsPackage(String, String, int, Notification).
648 * @param tag A string identifier for this notification. May be {@code null}.
649 * @param id An identifier for this notification.
650 */
651 public void cancelAsPackage(@NonNull String targetPackage, @Nullable String tag, int id) {
652 INotificationManager service = getService();
653 try {
654 service.cancelNotificationWithTag(targetPackage, mContext.getOpPackageName(),
655 tag, id, mContext.getUser().getIdentifier());
656 } catch (RemoteException e) {
657 throw e.rethrowFromSystemServer();
658 }
659 }
660
661 /**
Dianne Hackborn41203752012-08-31 14:05:51 -0700662 * @hide
663 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100664 @UnsupportedAppUsage
Dianne Hackborn41203752012-08-31 14:05:51 -0700665 public void cancelAsUser(String tag, int id, UserHandle user)
666 {
667 INotificationManager service = getService();
668 String pkg = mContext.getPackageName();
669 if (localLOGV) Log.v(TAG, pkg + ": cancel(" + id + ")");
670 try {
Julia Reynoldse4a47dd2019-06-07 13:40:59 -0400671 service.cancelNotificationWithTag(
672 pkg, mContext.getOpPackageName(), tag, id, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700674 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 }
676 }
677
678 /**
679 * Cancel all previously shown notifications. See {@link #cancel} for the
680 * detailed behavior.
681 */
682 public void cancelAll()
683 {
684 INotificationManager service = getService();
685 String pkg = mContext.getPackageName();
686 if (localLOGV) Log.v(TAG, pkg + ": cancelAll()");
687 try {
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700688 service.cancelAllNotifications(pkg, mContext.getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700690 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 }
692 }
693
John Spurlockb4782522014-08-22 14:54:46 -0400694 /**
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400695 * Allows a package to post notifications on your behalf using
696 * {@link #notifyAsPackage(String, String, int, Notification)}.
697 *
698 * This can be used to allow persistent processes to post notifications based on messages
699 * received on your behalf from the cloud, without your process having to wake up.
700 *
701 * You can check if you have an allowed delegate with {@link #getNotificationDelegate()} and
Julia Reynoldscbc45e72019-03-07 12:31:52 -0500702 * revoke your delegate by passing null to this method.
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400703 *
704 * @param delegate Package name of the app which can send notifications on your behalf.
705 */
Julia Reynoldscbc45e72019-03-07 12:31:52 -0500706 public void setNotificationDelegate(@Nullable String delegate) {
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400707 INotificationManager service = getService();
708 String pkg = mContext.getPackageName();
709 if (localLOGV) Log.v(TAG, pkg + ": cancelAll()");
710 try {
711 service.setNotificationDelegate(pkg, delegate);
712 } catch (RemoteException e) {
713 throw e.rethrowFromSystemServer();
714 }
715 }
716
717 /**
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400718 * Returns the {@link #setNotificationDelegate(String) delegate} that can post notifications on
719 * your behalf, if there currently is one.
720 */
721 public @Nullable String getNotificationDelegate() {
722 INotificationManager service = getService();
723 String pkg = mContext.getPackageName();
724 try {
725 return service.getNotificationDelegate(pkg);
726 } catch (RemoteException e) {
727 throw e.rethrowFromSystemServer();
728 }
729 }
730
731 /**
732 * Returns whether you are allowed to post notifications on behalf of a given package, with
733 * {@link #notifyAsPackage(String, String, int, Notification)}.
734 *
735 * See {@link #setNotificationDelegate(String)}.
736 */
Fabian Kozynski867550e2019-02-28 12:59:57 -0500737 public boolean canNotifyAsPackage(@NonNull String pkg) {
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400738 INotificationManager service = getService();
739 try {
Julia Reynolds7a6d07a2019-03-18 11:31:56 -0400740 return service.canNotifyAsPackage(mContext.getPackageName(), pkg, mContext.getUserId());
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400741 } catch (RemoteException e) {
742 throw e.rethrowFromSystemServer();
743 }
744 }
745
746 /**
Julia Reynolds59e152e2017-01-25 17:42:53 -0500747 * Creates a group container for {@link NotificationChannel} objects.
748 *
Julia Reynolds1d97e6a2017-03-13 15:05:40 -0400749 * This can be used to rename an existing group.
Julia Reynolds59e152e2017-01-25 17:42:53 -0500750 * <p>
751 * Group information is only used for presentation, not for behavior. Groups are optional
752 * for channels, and you can have a mix of channels that belong to groups and channels
753 * that do not.
754 * </p>
755 * <p>
756 * For example, if your application supports multiple accounts, and those accounts will
757 * have similar channels, you can create a group for each account with account specific
758 * labels instead of appending account information to each channel's label.
759 * </p>
760 *
761 * @param group The group to create
762 */
763 public void createNotificationChannelGroup(@NonNull NotificationChannelGroup group) {
764 createNotificationChannelGroups(Arrays.asList(group));
765 }
766
767 /**
768 * Creates multiple notification channel groups.
769 *
770 * @param groups The list of groups to create
771 */
772 public void createNotificationChannelGroups(@NonNull List<NotificationChannelGroup> groups) {
773 INotificationManager service = getService();
774 try {
775 service.createNotificationChannelGroups(mContext.getPackageName(),
776 new ParceledListSlice(groups));
777 } catch (RemoteException e) {
778 throw e.rethrowFromSystemServer();
779 }
780 }
781
782 /**
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500783 * Creates a notification channel that notifications can be posted to.
784 *
Julia Reynolds2c891c92017-03-17 14:23:47 -0400785 * This can also be used to restore a deleted channel and to update an existing channel's
Julia Reynolds005c8b92017-08-24 10:35:53 -0400786 * name, description, group, and/or importance.
Julia Reynolds0ffc13b2017-04-27 13:54:36 -0400787 *
788 * <p>The name and description should only be changed if the locale changes
Julia Reynolds2c891c92017-03-17 14:23:47 -0400789 * or in response to the user renaming this channel. For example, if a user has a channel
790 * named 'John Doe' that represents messages from a 'John Doe', and 'John Doe' changes his name
791 * to 'John Smith,' the channel can be renamed to match.
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400792 *
793 * <p>The importance of an existing channel will only be changed if the new importance is lower
794 * than the current value and the user has not altered any settings on this channel.
795 *
Julia Reynolds005c8b92017-08-24 10:35:53 -0400796 * <p>The group an existing channel will only be changed if the channel does not already
797 * belong to a group.
798 *
Julia Reynolds2c891c92017-03-17 14:23:47 -0400799 * All other fields are ignored for channels that already exist.
Julia Reynolds59e152e2017-01-25 17:42:53 -0500800 *
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500801 * @param channel the channel to create. Note that the created channel may differ from this
Julia Reynoldse8665332017-03-13 13:00:41 -0400802 * value. If the provided channel is malformed, a RemoteException will be
Julia Reynolds1d97e6a2017-03-13 15:05:40 -0400803 * thrown.
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400804 */
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500805 public void createNotificationChannel(@NonNull NotificationChannel channel) {
806 createNotificationChannels(Arrays.asList(channel));
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500807 }
808
809 /**
Julia Reynolds1d97e6a2017-03-13 15:05:40 -0400810 * Creates multiple notification channels that different notifications can be posted to. See
811 * {@link #createNotificationChannel(NotificationChannel)}.
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500812 *
Julia Reynolds1d97e6a2017-03-13 15:05:40 -0400813 * @param channels the list of channels to attempt to create.
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500814 */
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500815 public void createNotificationChannels(@NonNull List<NotificationChannel> channels) {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400816 INotificationManager service = getService();
817 try {
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500818 service.createNotificationChannels(mContext.getPackageName(),
819 new ParceledListSlice(channels));
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400820 } catch (RemoteException e) {
821 throw e.rethrowFromSystemServer();
822 }
823 }
824
825 /**
826 * Returns the notification channel settings for a given channel id.
Julia Reynolds0ffc13b2017-04-27 13:54:36 -0400827 *
Julia Reynoldsb4a9e9c2019-03-20 15:46:08 -0400828 * <p>The channel must belong to your package, or to a package you are an approved notification
829 * delegate for (see {@link #canNotifyAsPackage(String)}), or it will not be returned. To query
830 * a channel as a notification delegate, call this method from a context created for that
831 * package (see {@link Context#createPackageContext(String, int)}).</p>
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400832 */
833 public NotificationChannel getNotificationChannel(String channelId) {
834 INotificationManager service = getService();
835 try {
Julia Reynoldsb4a9e9c2019-03-20 15:46:08 -0400836 return service.getNotificationChannel(mContext.getOpPackageName(),
837 mContext.getUserId(), mContext.getPackageName(), channelId);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400838 } catch (RemoteException e) {
839 throw e.rethrowFromSystemServer();
840 }
841 }
842
843 /**
Julia Reynolds0f767342019-12-18 09:11:55 -0500844 * Returns the notification channel settings for a given channel and conversation id.
845 *
846 * <p>The channel must belong to your package, or to a package you are an approved notification
847 * delegate for (see {@link #canNotifyAsPackage(String)}), or it will not be returned. To query
848 * a channel as a notification delegate, call this method from a context created for that
849 * package (see {@link Context#createPackageContext(String, int)}).</p>
850 */
851 public @Nullable NotificationChannel getNotificationChannel(@NonNull String channelId,
852 @NonNull String conversationId) {
853 INotificationManager service = getService();
854 try {
855 return service.getConversationNotificationChannel(mContext.getOpPackageName(),
Julia Reynolds12ba4cf2020-01-10 16:01:38 -0500856 mContext.getUserId(), mContext.getPackageName(), channelId, true,
857 conversationId);
Julia Reynolds0f767342019-12-18 09:11:55 -0500858 } catch (RemoteException e) {
859 throw e.rethrowFromSystemServer();
860 }
861 }
862
863 /**
Julia Reynolds0ffc13b2017-04-27 13:54:36 -0400864 * Returns all notification channels belonging to the calling package.
Julia Reynoldsb4a9e9c2019-03-20 15:46:08 -0400865 *
866 * <p>Approved notification delegates (see {@link #canNotifyAsPackage(String)}) can query
867 * notification channels belonging to packages they are the delegate for. To do so, call this
868 * method from a context created for that package (see
869 * {@link Context#createPackageContext(String, int)}).</p>
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400870 */
871 public List<NotificationChannel> getNotificationChannels() {
872 INotificationManager service = getService();
873 try {
Julia Reynoldsb4a9e9c2019-03-20 15:46:08 -0400874 return service.getNotificationChannels(mContext.getOpPackageName(),
875 mContext.getPackageName(), mContext.getUserId()).getList();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400876 } catch (RemoteException e) {
877 throw e.rethrowFromSystemServer();
878 }
879 }
880
881 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400882 * Deletes the given notification channel.
Julia Reynolds0ffc13b2017-04-27 13:54:36 -0400883 *
884 * <p>If you {@link #createNotificationChannel(NotificationChannel) create} a new channel with
885 * this same id, the deleted channel will be un-deleted with all of the same settings it
886 * had before it was deleted.
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400887 */
888 public void deleteNotificationChannel(String channelId) {
889 INotificationManager service = getService();
890 try {
891 service.deleteNotificationChannel(mContext.getPackageName(), channelId);
892 } catch (RemoteException e) {
893 throw e.rethrowFromSystemServer();
894 }
895 }
896
897 /**
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -0500898 * Returns the notification channel group settings for a given channel group id.
899 *
900 * The channel group must belong to your package, or null will be returned.
901 */
902 public NotificationChannelGroup getNotificationChannelGroup(String channelGroupId) {
903 INotificationManager service = getService();
904 try {
905 return service.getNotificationChannelGroup(mContext.getPackageName(), channelGroupId);
906 } catch (RemoteException e) {
907 throw e.rethrowFromSystemServer();
908 }
909 }
910
911 /**
Julia Reynolds9bfba592017-03-15 14:03:55 -0400912 * Returns all notification channel groups belonging to the calling app.
913 */
914 public List<NotificationChannelGroup> getNotificationChannelGroups() {
915 INotificationManager service = getService();
916 try {
Beverlyc629ee42018-10-02 16:14:07 -0400917 final ParceledListSlice<NotificationChannelGroup> parceledList =
918 service.getNotificationChannelGroups(mContext.getPackageName());
919 if (parceledList != null) {
920 return parceledList.getList();
921 }
Julia Reynolds9bfba592017-03-15 14:03:55 -0400922 } catch (RemoteException e) {
923 throw e.rethrowFromSystemServer();
924 }
Beverlyc629ee42018-10-02 16:14:07 -0400925 return new ArrayList<>();
Julia Reynolds9bfba592017-03-15 14:03:55 -0400926 }
927
928 /**
Julia Reynolds0ffc13b2017-04-27 13:54:36 -0400929 * Deletes the given notification channel group, and all notification channels that
930 * belong to it.
Julia Reynolds9bfba592017-03-15 14:03:55 -0400931 */
932 public void deleteNotificationChannelGroup(String groupId) {
933 INotificationManager service = getService();
934 try {
935 service.deleteNotificationChannelGroup(mContext.getPackageName(), groupId);
936 } catch (RemoteException e) {
937 throw e.rethrowFromSystemServer();
938 }
939 }
940
941 /**
John Spurlockb4782522014-08-22 14:54:46 -0400942 * @hide
943 */
Julia Reynoldsb6c1f992016-11-22 09:26:46 -0500944 @TestApi
John Spurlockb4782522014-08-22 14:54:46 -0400945 public ComponentName getEffectsSuppressor() {
946 INotificationManager service = getService();
947 try {
948 return service.getEffectsSuppressor();
949 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700950 throw e.rethrowFromSystemServer();
John Spurlockb4782522014-08-22 14:54:46 -0400951 }
952 }
953
John Spurlock2b122f42014-08-27 16:29:47 -0400954 /**
955 * @hide
956 */
Julia Reynolds87621942019-01-29 16:19:36 -0500957 @TestApi
John Spurlock2b122f42014-08-27 16:29:47 -0400958 public boolean matchesCallFilter(Bundle extras) {
959 INotificationManager service = getService();
960 try {
961 return service.matchesCallFilter(extras);
962 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700963 throw e.rethrowFromSystemServer();
John Spurlock2b122f42014-08-27 16:29:47 -0400964 }
965 }
966
John Spurlock530052a2014-11-30 16:26:19 -0500967 /**
968 * @hide
969 */
970 public boolean isSystemConditionProviderEnabled(String path) {
971 INotificationManager service = getService();
972 try {
973 return service.isSystemConditionProviderEnabled(path);
974 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700975 throw e.rethrowFromSystemServer();
John Spurlock530052a2014-11-30 16:26:19 -0500976 }
977 }
978
John Spurlockcdb57ae2015-02-11 19:04:11 -0500979 /**
980 * @hide
981 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100982 @UnsupportedAppUsage
John Spurlockb2278d62015-04-07 12:47:12 -0400983 public void setZenMode(int mode, Uri conditionId, String reason) {
John Spurlockcdb57ae2015-02-11 19:04:11 -0500984 INotificationManager service = getService();
985 try {
John Spurlockb2278d62015-04-07 12:47:12 -0400986 service.setZenMode(mode, conditionId, reason);
John Spurlockcdb57ae2015-02-11 19:04:11 -0500987 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700988 throw e.rethrowFromSystemServer();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500989 }
990 }
991
992 /**
993 * @hide
994 */
John Spurlockb2278d62015-04-07 12:47:12 -0400995 public int getZenMode() {
John Spurlockcdb57ae2015-02-11 19:04:11 -0500996 INotificationManager service = getService();
997 try {
John Spurlockb2278d62015-04-07 12:47:12 -0400998 return service.getZenMode();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500999 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001000 throw e.rethrowFromSystemServer();
John Spurlockcdb57ae2015-02-11 19:04:11 -05001001 }
1002 }
1003
1004 /**
1005 * @hide
1006 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001007 @UnsupportedAppUsage
John Spurlockb2278d62015-04-07 12:47:12 -04001008 public ZenModeConfig getZenModeConfig() {
John Spurlockcdb57ae2015-02-11 19:04:11 -05001009 INotificationManager service = getService();
1010 try {
John Spurlockb2278d62015-04-07 12:47:12 -04001011 return service.getZenModeConfig();
John Spurlockcdb57ae2015-02-11 19:04:11 -05001012 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001013 throw e.rethrowFromSystemServer();
John Spurlockcdb57ae2015-02-11 19:04:11 -05001014 }
John Spurlockcdb57ae2015-02-11 19:04:11 -05001015 }
1016
John Spurlock1fc476d2015-04-14 16:05:20 -04001017 /**
Beverly430b71a2020-03-16 14:17:31 -04001018 * Returns the currently applied notification policy.
1019 *
Beverlyd994e2e2019-10-11 14:36:37 -04001020 * <p>
Beverly430b71a2020-03-16 14:17:31 -04001021 * If {@link #getCurrentInterruptionFilter} is equal to {@link #INTERRUPTION_FILTER_ALL},
1022 * then the consolidated notification policy will match the default notification policy
1023 * returned by {@link #getNotificationPolicy}.
Beverlyd994e2e2019-10-11 14:36:37 -04001024 * </p>
Julia Reynolds43b70cd2016-01-14 15:05:34 -05001025 */
Beverly430b71a2020-03-16 14:17:31 -04001026 public @NonNull NotificationManager.Policy getConsolidatedNotificationPolicy() {
Beverlyff2df9b2018-10-10 16:54:10 -04001027 INotificationManager service = getService();
1028 try {
1029 return service.getConsolidatedNotificationPolicy();
1030 } catch (RemoteException e) {
1031 throw e.rethrowFromSystemServer();
1032 }
1033 }
1034
1035 /**
1036 * @hide
1037 */
Julia Reynolds43b70cd2016-01-14 15:05:34 -05001038 public int getRuleInstanceCount(ComponentName owner) {
1039 INotificationManager service = getService();
1040 try {
1041 return service.getRuleInstanceCount(owner);
1042 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001043 throw e.rethrowFromSystemServer();
Julia Reynolds43b70cd2016-01-14 15:05:34 -05001044 }
Julia Reynolds43b70cd2016-01-14 15:05:34 -05001045 }
1046
1047 /**
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001048 * Returns AutomaticZenRules owned by the caller.
1049 *
1050 * <p>
Jay Aliomer8e3aac52019-10-16 13:48:39 -04001051 * Throws a SecurityException if policy access is not granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001052 * See {@link #isNotificationPolicyAccessGranted}.
1053 */
Julia Reynolds361e82d32016-02-26 18:19:49 -05001054 public Map<String, AutomaticZenRule> getAutomaticZenRules() {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001055 INotificationManager service = getService();
1056 try {
Julia Reynolds361e82d32016-02-26 18:19:49 -05001057 List<ZenModeConfig.ZenRule> rules = service.getZenRules();
1058 Map<String, AutomaticZenRule> ruleMap = new HashMap<>();
1059 for (ZenModeConfig.ZenRule rule : rules) {
Julia Reynolds68062072018-08-06 15:38:21 -04001060 ruleMap.put(rule.id, new AutomaticZenRule(rule.name, rule.component,
1061 rule.configurationActivity, rule.conditionId, rule.zenPolicy,
1062 zenModeToInterruptionFilter(rule.zenMode), rule.enabled,
1063 rule.creationTime));
Julia Reynolds361e82d32016-02-26 18:19:49 -05001064 }
1065 return ruleMap;
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001066 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001067 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001068 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001069 }
1070
1071 /**
Julia Reynolds4fe98d62015-10-06 16:23:41 -04001072 * Returns the AutomaticZenRule with the given id, if it exists and the caller has access.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001073 *
1074 * <p>
Jay Aliomer8e3aac52019-10-16 13:48:39 -04001075 * Throws a SecurityException if policy access is not granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001076 * See {@link #isNotificationPolicyAccessGranted}.
1077 *
1078 * <p>
Julia Reynolds4fe98d62015-10-06 16:23:41 -04001079 * Returns null if there are no zen rules that match the given id, or if the calling package
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001080 * doesn't own the matching rule. See {@link AutomaticZenRule#getOwner}.
1081 */
Julia Reynolds4fe98d62015-10-06 16:23:41 -04001082 public AutomaticZenRule getAutomaticZenRule(String id) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001083 INotificationManager service = getService();
1084 try {
Julia Reynolds4fe98d62015-10-06 16:23:41 -04001085 return service.getAutomaticZenRule(id);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001086 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001087 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001088 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001089 }
1090
1091 /**
Julia Reynolds4fe98d62015-10-06 16:23:41 -04001092 * Creates the given zen rule.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001093 *
1094 * <p>
Jay Aliomer8e3aac52019-10-16 13:48:39 -04001095 * Throws a SecurityException if policy access is not granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001096 * See {@link #isNotificationPolicyAccessGranted}.
1097 *
Julia Reynolds4fe98d62015-10-06 16:23:41 -04001098 * @param automaticZenRule the rule to create.
Julia Reynolds361e82d32016-02-26 18:19:49 -05001099 * @return The id of the newly created rule; null if the rule could not be created.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001100 */
Julia Reynolds361e82d32016-02-26 18:19:49 -05001101 public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001102 INotificationManager service = getService();
1103 try {
Julia Reynolds4fe98d62015-10-06 16:23:41 -04001104 return service.addAutomaticZenRule(automaticZenRule);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001105 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001106 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001107 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001108 }
1109
1110 /**
Julia Reynolds4fe98d62015-10-06 16:23:41 -04001111 * Updates the given zen rule.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001112 *
1113 * <p>
Jay Aliomer8e3aac52019-10-16 13:48:39 -04001114 * Throws a SecurityException if policy access is not granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001115 * See {@link #isNotificationPolicyAccessGranted}.
1116 *
1117 * <p>
1118 * Callers can only update rules that they own. See {@link AutomaticZenRule#getOwner}.
Julia Reynolds361e82d32016-02-26 18:19:49 -05001119 * @param id The id of the rule to update
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -05001120 * @param automaticZenRule the rule to update.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001121 * @return Whether the rule was successfully updated.
1122 */
Julia Reynolds361e82d32016-02-26 18:19:49 -05001123 public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001124 INotificationManager service = getService();
1125 try {
Julia Reynolds361e82d32016-02-26 18:19:49 -05001126 return service.updateAutomaticZenRule(id, automaticZenRule);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001127 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001128 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001129 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001130 }
1131
1132 /**
Julia Reynolds68062072018-08-06 15:38:21 -04001133 * Informs the notification manager that the state of an {@link AutomaticZenRule} has changed.
1134 * Use this method to put the system into Do Not Disturb mode or request that it exits Do Not
1135 * Disturb mode. The calling app must own the provided {@link android.app.AutomaticZenRule}.
1136 * <p>
1137 * This method can be used in conjunction with or as a replacement to
1138 * {@link android.service.notification.ConditionProviderService#notifyCondition(Condition)}.
1139 * </p>
1140 * @param id The id of the rule whose state should change
1141 * @param condition The new state of this rule
1142 */
Fabian Kozynski867550e2019-02-28 12:59:57 -05001143 public void setAutomaticZenRuleState(@NonNull String id, @NonNull Condition condition) {
Julia Reynolds68062072018-08-06 15:38:21 -04001144 INotificationManager service = getService();
1145 try {
1146 service.setAutomaticZenRuleState(id, condition);
1147 } catch (RemoteException e) {
1148 throw e.rethrowFromSystemServer();
1149 }
1150 }
1151
1152 /**
Julia Reynolds4fe98d62015-10-06 16:23:41 -04001153 * Deletes the automatic zen rule with the given id.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001154 *
1155 * <p>
Jay Aliomer8e3aac52019-10-16 13:48:39 -04001156 * Throws a SecurityException if policy access is not granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001157 * See {@link #isNotificationPolicyAccessGranted}.
1158 *
1159 * <p>
1160 * Callers can only delete rules that they own. See {@link AutomaticZenRule#getOwner}.
Julia Reynolds4fe98d62015-10-06 16:23:41 -04001161 * @param id the id of the rule to delete.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001162 * @return Whether the rule was successfully deleted.
1163 */
Julia Reynolds4fe98d62015-10-06 16:23:41 -04001164 public boolean removeAutomaticZenRule(String id) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001165 INotificationManager service = getService();
1166 try {
Julia Reynolds4fe98d62015-10-06 16:23:41 -04001167 return service.removeAutomaticZenRule(id);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001168 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001169 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001170 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -04001171 }
1172
1173 /**
Julia Reynoldsc8e54e82015-11-30 16:43:05 -05001174 * Deletes all automatic zen rules owned by the given package.
1175 *
1176 * @hide
1177 */
1178 public boolean removeAutomaticZenRules(String packageName) {
1179 INotificationManager service = getService();
1180 try {
1181 return service.removeAutomaticZenRules(packageName);
1182 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001183 throw e.rethrowFromSystemServer();
Julia Reynoldsc8e54e82015-11-30 16:43:05 -05001184 }
Julia Reynoldsc8e54e82015-11-30 16:43:05 -05001185 }
1186
Julia Reynolds0edb50c2016-02-26 14:08:25 -05001187 /**
Jeff Sharkey6503bd82017-04-19 23:24:18 -06001188 * Returns the user specified importance for notifications from the calling
1189 * package.
Julia Reynolds0edb50c2016-02-26 14:08:25 -05001190 */
Chris Wren5ab5c742016-05-10 15:32:23 -04001191 public @Importance int getImportance() {
Julia Reynolds81afbcd2016-02-09 14:54:08 -05001192 INotificationManager service = getService();
1193 try {
Julia Reynoldsef37f282016-02-12 09:11:27 -05001194 return service.getPackageImportance(mContext.getPackageName());
Julia Reynolds81afbcd2016-02-09 14:54:08 -05001195 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001196 throw e.rethrowFromSystemServer();
Julia Reynolds81afbcd2016-02-09 14:54:08 -05001197 }
Julia Reynolds81afbcd2016-02-09 14:54:08 -05001198 }
1199
Julia Reynolds0edb50c2016-02-26 14:08:25 -05001200 /**
1201 * Returns whether notifications from the calling package are blocked.
1202 */
Julia Reynolds81afbcd2016-02-09 14:54:08 -05001203 public boolean areNotificationsEnabled() {
1204 INotificationManager service = getService();
1205 try {
1206 return service.areNotificationsEnabled(mContext.getPackageName());
1207 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001208 throw e.rethrowFromSystemServer();
Julia Reynolds81afbcd2016-02-09 14:54:08 -05001209 }
Julia Reynolds81afbcd2016-02-09 14:54:08 -05001210 }
1211
Julia Reynolds33ab8a02018-12-17 16:19:52 -05001212
1213 /**
1214 * Sets whether notifications posted by this app can appear outside of the
1215 * notification shade, floating over other apps' content.
1216 *
1217 * <p>This value will be ignored for notifications that are posted to channels that do not
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001218 * allow bubbles ({@link NotificationChannel#canBubble()}.
Julia Reynolds33ab8a02018-12-17 16:19:52 -05001219 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001220 * @see Notification#getBubbleMetadata()
Julia Reynolds33ab8a02018-12-17 16:19:52 -05001221 */
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001222 public boolean areBubblesAllowed() {
Julia Reynolds33ab8a02018-12-17 16:19:52 -05001223 INotificationManager service = getService();
1224 try {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001225 return service.areBubblesAllowed(mContext.getPackageName());
Julia Reynolds33ab8a02018-12-17 16:19:52 -05001226 } catch (RemoteException e) {
1227 throw e.rethrowFromSystemServer();
1228 }
1229 }
1230
Julia Reynoldsc8e54e82015-11-30 16:43:05 -05001231 /**
Julia Reynoldsdeb2d0b2019-06-17 15:34:26 -04001232 * Silences the current notification sound, if ones currently playing.
1233 * <p>
1234 * It is intended to handle use-cases such as silencing a ringing call
1235 * when the user presses the volume button during ringing.
1236 * <p>
1237 * If this method is called prior to when the notification begins playing, the sound will not be
1238 * silenced. As such it is not intended as a means to avoid playing of a sound.
1239 * @hide
1240 */
1241 public void silenceNotificationSound() {
1242 INotificationManager service = getService();
1243 try {
1244 service.silenceNotificationSound();
1245 } catch (RemoteException e) {
1246 throw e.rethrowFromSystemServer();
1247 }
1248 }
1249
1250 /**
Julia Reynoldsb40cd092019-01-25 09:35:02 -05001251 * Returns whether notifications from this package are temporarily hidden. This
1252 * could be done because the package was marked as distracting to the user via
1253 * {@code PackageManager#setDistractingPackageRestrictions(String[], int)} or because the
1254 * package is {@code PackageManager#setPackagesSuspended(String[], boolean, PersistableBundle,
1255 * PersistableBundle, SuspendDialogInfo) suspended}.
1256 */
1257 public boolean areNotificationsPaused() {
1258 INotificationManager service = getService();
1259 try {
1260 return service.isPackagePaused(mContext.getPackageName());
1261 } catch (RemoteException e) {
1262 throw e.rethrowFromSystemServer();
1263 }
1264 }
1265
1266 /**
Beverly312ef3e2017-11-15 11:12:51 -05001267 * Checks the ability to modify notification do not disturb policy for the calling package.
John Spurlock1fc476d2015-04-14 16:05:20 -04001268 *
John Spurlock7c74f782015-06-04 13:01:42 -04001269 * <p>
Beverly312ef3e2017-11-15 11:12:51 -05001270 * Returns true if the calling package can modify notification policy.
John Spurlock7c74f782015-06-04 13:01:42 -04001271 *
1272 * <p>
Julia Reynoldsb852e562017-06-06 16:14:18 -04001273 * Apps can request policy access by sending the user to the activity that matches the system
1274 * intent action {@link android.provider.Settings#ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS}.
John Spurlock7c74f782015-06-04 13:01:42 -04001275 *
1276 * <p>
1277 * Use {@link #ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED} to listen for
1278 * user grant or denial of this access.
John Spurlock1fc476d2015-04-14 16:05:20 -04001279 */
John Spurlock80774932015-05-07 17:38:50 -04001280 public boolean isNotificationPolicyAccessGranted() {
John Spurlock1fc476d2015-04-14 16:05:20 -04001281 INotificationManager service = getService();
1282 try {
John Spurlock80774932015-05-07 17:38:50 -04001283 return service.isNotificationPolicyAccessGranted(mContext.getOpPackageName());
1284 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001285 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -04001286 }
John Spurlock80774932015-05-07 17:38:50 -04001287 }
1288
Julia Reynoldsb852e562017-06-06 16:14:18 -04001289 /**
1290 * Checks whether the user has approved a given
1291 * {@link android.service.notification.NotificationListenerService}.
1292 *
1293 * <p>
1294 * The listener service must belong to the calling app.
1295 *
1296 * <p>
1297 * Apps can request notification listener access by sending the user to the activity that
1298 * matches the system intent action
1299 * {@link android.provider.Settings#ACTION_NOTIFICATION_LISTENER_SETTINGS}.
1300 */
1301 public boolean isNotificationListenerAccessGranted(ComponentName listener) {
1302 INotificationManager service = getService();
1303 try {
1304 return service.isNotificationListenerAccessGranted(listener);
1305 } catch (RemoteException e) {
1306 throw e.rethrowFromSystemServer();
1307 }
1308 }
1309
Fabian Kozynskid9425662019-01-29 13:08:30 -05001310 /**
1311 * Checks whether the user has approved a given
1312 * {@link android.service.notification.NotificationAssistantService}.
1313 *
1314 * <p>
1315 * The assistant service must belong to the calling app.
1316 *
1317 * <p>
1318 * Apps can request notification assistant access by sending the user to the activity that
1319 * matches the system intent action
1320 * TODO: STOPSHIP: Add correct intent
1321 * {@link android.provider.Settings#ACTION_MANAGE_DEFAULT_APPS_SETTINGS}.
Julia Reynoldsd0ceefa2019-03-03 16:10:52 -05001322 * @hide
Fabian Kozynskid9425662019-01-29 13:08:30 -05001323 */
Julia Reynoldsd0ceefa2019-03-03 16:10:52 -05001324 @SystemApi
Julia Reynoldsdc6adc62019-04-08 10:35:40 -04001325 @TestApi
Fabian Kozynski867550e2019-02-28 12:59:57 -05001326 public boolean isNotificationAssistantAccessGranted(@NonNull ComponentName assistant) {
Julia Reynoldsb852e562017-06-06 16:14:18 -04001327 INotificationManager service = getService();
1328 try {
1329 return service.isNotificationAssistantAccessGranted(assistant);
1330 } catch (RemoteException e) {
1331 throw e.rethrowFromSystemServer();
1332 }
1333 }
1334
Julia Reynolds12ad7ca2019-01-28 09:29:16 -05001335 /**
1336 * Returns whether the user wants silent notifications (see {@link #IMPORTANCE_LOW} to appear
1337 * in the status bar.
1338 *
1339 * <p>Only available for {@link #isNotificationListenerAccessGranted(ComponentName) notification
1340 * listeners}.
1341 */
1342 public boolean shouldHideSilentStatusBarIcons() {
1343 INotificationManager service = getService();
1344 try {
1345 return service.shouldHideSilentStatusIcons(mContext.getOpPackageName());
1346 } catch (RemoteException e) {
1347 throw e.rethrowFromSystemServer();
1348 }
1349 }
1350
Julia Reynoldsad6dd352019-03-07 16:46:22 -05001351 /**
1352 * Returns the list of {@link android.service.notification.Adjustment adjustment keys} that can
1353 * be modified by the current {@link android.service.notification.NotificationAssistantService}.
1354 *
1355 * <p>Only callable by the current
1356 * {@link android.service.notification.NotificationAssistantService}.
1357 * See {@link #isNotificationAssistantAccessGranted(ComponentName)}</p>
1358 * @hide
1359 */
1360 @SystemApi
Julia Reynoldsdc6adc62019-04-08 10:35:40 -04001361 @TestApi
Julia Reynolds088c4482019-04-10 12:43:27 -04001362 public @NonNull @Adjustment.Keys List<String> getAllowedAssistantAdjustments() {
Julia Reynoldsad6dd352019-03-07 16:46:22 -05001363 INotificationManager service = getService();
1364 try {
Julia Reynolds088c4482019-04-10 12:43:27 -04001365 return service.getAllowedAssistantAdjustments(mContext.getOpPackageName());
Julia Reynoldsad6dd352019-03-07 16:46:22 -05001366 } catch (RemoteException e) {
1367 throw e.rethrowFromSystemServer();
1368 }
1369 }
1370
Julia Reynoldsdc6adc62019-04-08 10:35:40 -04001371 /**
1372 * @hide
1373 */
1374 @TestApi
Julia Reynolds088c4482019-04-10 12:43:27 -04001375 public void allowAssistantAdjustment(String capability) {
Julia Reynoldsdc6adc62019-04-08 10:35:40 -04001376 INotificationManager service = getService();
1377 try {
Julia Reynolds088c4482019-04-10 12:43:27 -04001378 service.allowAssistantAdjustment(capability);
Julia Reynoldsdc6adc62019-04-08 10:35:40 -04001379 } catch (RemoteException e) {
1380 throw e.rethrowFromSystemServer();
1381 }
1382 }
1383
1384 /**
1385 * @hide
1386 */
1387 @TestApi
Julia Reynolds088c4482019-04-10 12:43:27 -04001388 public void disallowAssistantAdjustment(String capability) {
Julia Reynoldsdc6adc62019-04-08 10:35:40 -04001389 INotificationManager service = getService();
1390 try {
Julia Reynolds088c4482019-04-10 12:43:27 -04001391 service.disallowAssistantAdjustment(capability);
Julia Reynoldsdc6adc62019-04-08 10:35:40 -04001392 } catch (RemoteException e) {
1393 throw e.rethrowFromSystemServer();
1394 }
1395 }
1396
John Spurlock80774932015-05-07 17:38:50 -04001397 /** @hide */
1398 public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {
1399 INotificationManager service = getService();
1400 try {
1401 return service.isNotificationPolicyAccessGrantedForPackage(pkg);
John Spurlock1fc476d2015-04-14 16:05:20 -04001402 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001403 throw e.rethrowFromSystemServer();
John Spurlock1fc476d2015-04-14 16:05:20 -04001404 }
John Spurlock1fc476d2015-04-14 16:05:20 -04001405 }
1406
1407 /**
Julia Reynoldsb852e562017-06-06 16:14:18 -04001408 * @hide
1409 */
1410 public List<String> getEnabledNotificationListenerPackages() {
1411 INotificationManager service = getService();
1412 try {
1413 return service.getEnabledNotificationListenerPackages();
1414 } catch (RemoteException e) {
1415 throw e.rethrowFromSystemServer();
1416 }
1417 }
1418
1419 /**
Beverlyff2df9b2018-10-10 16:54:10 -04001420 * Gets the current user-specified default notification policy.
John Spurlock1fc476d2015-04-14 16:05:20 -04001421 *
John Spurlock80774932015-05-07 17:38:50 -04001422 * <p>
John Spurlock1fc476d2015-04-14 16:05:20 -04001423 */
John Spurlock80774932015-05-07 17:38:50 -04001424 public Policy getNotificationPolicy() {
John Spurlock1fc476d2015-04-14 16:05:20 -04001425 INotificationManager service = getService();
1426 try {
John Spurlock80774932015-05-07 17:38:50 -04001427 return service.getNotificationPolicy(mContext.getOpPackageName());
John Spurlock1fc476d2015-04-14 16:05:20 -04001428 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001429 throw e.rethrowFromSystemServer();
John Spurlock1fc476d2015-04-14 16:05:20 -04001430 }
John Spurlock1fc476d2015-04-14 16:05:20 -04001431 }
1432
1433 /**
1434 * Sets the current notification policy.
1435 *
John Spurlock80774932015-05-07 17:38:50 -04001436 * <p>
John Spurlock7c74f782015-06-04 13:01:42 -04001437 * Only available if policy access is granted to this package.
1438 * See {@link #isNotificationPolicyAccessGranted}.
John Spurlock80774932015-05-07 17:38:50 -04001439 *
John Spurlock1fc476d2015-04-14 16:05:20 -04001440 * @param policy The new desired policy.
1441 */
John Spurlock80774932015-05-07 17:38:50 -04001442 public void setNotificationPolicy(@NonNull Policy policy) {
John Spurlock1fc476d2015-04-14 16:05:20 -04001443 checkRequired("policy", policy);
1444 INotificationManager service = getService();
1445 try {
John Spurlock80774932015-05-07 17:38:50 -04001446 service.setNotificationPolicy(mContext.getOpPackageName(), policy);
John Spurlock1fc476d2015-04-14 16:05:20 -04001447 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001448 throw e.rethrowFromSystemServer();
John Spurlock1fc476d2015-04-14 16:05:20 -04001449 }
1450 }
1451
John Spurlock80774932015-05-07 17:38:50 -04001452 /** @hide */
1453 public void setNotificationPolicyAccessGranted(String pkg, boolean granted) {
1454 INotificationManager service = getService();
1455 try {
1456 service.setNotificationPolicyAccessGranted(pkg, granted);
1457 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001458 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -04001459 }
1460 }
1461
1462 /** @hide */
Julia Reynoldsb852e562017-06-06 16:14:18 -04001463 public void setNotificationListenerAccessGranted(ComponentName listener, boolean granted) {
John Spurlock80774932015-05-07 17:38:50 -04001464 INotificationManager service = getService();
1465 try {
Julia Reynoldsb852e562017-06-06 16:14:18 -04001466 service.setNotificationListenerAccessGranted(listener, granted);
John Spurlock80774932015-05-07 17:38:50 -04001467 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001468 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -04001469 }
Julia Reynoldsb852e562017-06-06 16:14:18 -04001470 }
1471
1472 /** @hide */
1473 public void setNotificationListenerAccessGrantedForUser(ComponentName listener, int userId,
1474 boolean granted) {
1475 INotificationManager service = getService();
1476 try {
1477 service.setNotificationListenerAccessGrantedForUser(listener, userId, granted);
1478 } catch (RemoteException e) {
1479 throw e.rethrowFromSystemServer();
1480 }
1481 }
1482
Fabian Kozynskid9425662019-01-29 13:08:30 -05001483 /**
1484 * Grants/revokes Notification Assistant access to {@code assistant} for current user.
Fabian Kozynskicbfbb1d2019-03-15 09:51:39 -04001485 * To grant access for a particular user, obtain this service by using the {@link Context}
1486 * provided by {@link Context#createPackageContextAsUser}
Fabian Kozynskid9425662019-01-29 13:08:30 -05001487 *
1488 * @param assistant Name of component to grant/revoke access or {@code null} to revoke access to
1489 * current assistant
1490 * @param granted Grant/revoke access
1491 * @hide
1492 */
1493 @SystemApi
Julia Reynoldsdc6adc62019-04-08 10:35:40 -04001494 @TestApi
Fabian Kozynski5747d632019-03-11 14:23:23 -04001495 public void setNotificationAssistantAccessGranted(@Nullable ComponentName assistant,
1496 boolean granted) {
Fabian Kozynskid9425662019-01-29 13:08:30 -05001497 INotificationManager service = getService();
1498 try {
1499 service.setNotificationAssistantAccessGranted(assistant, granted);
1500 } catch (RemoteException e) {
1501 throw e.rethrowFromSystemServer();
1502 }
1503 }
1504
Julia Reynoldsb852e562017-06-06 16:14:18 -04001505 /** @hide */
1506 public List<ComponentName> getEnabledNotificationListeners(int userId) {
1507 INotificationManager service = getService();
1508 try {
1509 return service.getEnabledNotificationListeners(userId);
1510 } catch (RemoteException e) {
1511 throw e.rethrowFromSystemServer();
1512 }
John Spurlock80774932015-05-07 17:38:50 -04001513 }
1514
Fabian Kozynskid9425662019-01-29 13:08:30 -05001515 /** @hide */
1516 @SystemApi
Julia Reynoldsdc6adc62019-04-08 10:35:40 -04001517 @TestApi
Fabian Kozynskid9425662019-01-29 13:08:30 -05001518 public @Nullable ComponentName getAllowedNotificationAssistant() {
1519 INotificationManager service = getService();
1520 try {
1521 return service.getAllowedNotificationAssistant();
1522 } catch (RemoteException e) {
1523 throw e.rethrowFromSystemServer();
1524 }
1525 }
1526
1527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 private Context mContext;
John Spurlock1fc476d2015-04-14 16:05:20 -04001529
1530 private static void checkRequired(String name, Object value) {
1531 if (value == null) {
1532 throw new IllegalArgumentException(name + " is required");
1533 }
1534 }
1535
1536 /**
1537 * Notification policy configuration. Represents user-preferences for notification
Julia Reynoldscedacef2016-03-04 08:18:47 -05001538 * filtering.
John Spurlock1fc476d2015-04-14 16:05:20 -04001539 */
1540 public static class Policy implements android.os.Parcelable {
1541 /** Reminder notifications are prioritized. */
1542 public static final int PRIORITY_CATEGORY_REMINDERS = 1 << 0;
1543 /** Event notifications are prioritized. */
1544 public static final int PRIORITY_CATEGORY_EVENTS = 1 << 1;
1545 /** Message notifications are prioritized. */
1546 public static final int PRIORITY_CATEGORY_MESSAGES = 1 << 2;
1547 /** Calls are prioritized. */
1548 public static final int PRIORITY_CATEGORY_CALLS = 1 << 3;
1549 /** Calls from repeat callers are prioritized. */
1550 public static final int PRIORITY_CATEGORY_REPEAT_CALLERS = 1 << 4;
Beverly04216872017-09-28 10:55:32 -04001551 /** Alarms are prioritized */
1552 public static final int PRIORITY_CATEGORY_ALARMS = 1 << 5;
Beverlyd6964762018-02-16 14:07:03 -05001553 /** Media, game, voice navigation are prioritized */
1554 public static final int PRIORITY_CATEGORY_MEDIA = 1 << 6;
1555 /**System (catch-all for non-never suppressible sounds) are prioritized */
1556 public static final int PRIORITY_CATEGORY_SYSTEM = 1 << 7;
Julia Reynolds24edc002020-01-29 16:35:32 -05001557 /**
1558 * Conversations are allowed through DND.
1559 */
1560 public static final int PRIORITY_CATEGORY_CONVERSATIONS = 1 << 8;
John Spurlock1fc476d2015-04-14 16:05:20 -04001561
Beverlyd6964762018-02-16 14:07:03 -05001562 /**
1563 * @hide
1564 */
1565 public static final int[] ALL_PRIORITY_CATEGORIES = {
Julia Reynolds24edc002020-01-29 16:35:32 -05001566 PRIORITY_CATEGORY_ALARMS,
1567 PRIORITY_CATEGORY_MEDIA,
1568 PRIORITY_CATEGORY_SYSTEM,
1569 PRIORITY_CATEGORY_REMINDERS,
1570 PRIORITY_CATEGORY_EVENTS,
1571 PRIORITY_CATEGORY_MESSAGES,
1572 PRIORITY_CATEGORY_CALLS,
1573 PRIORITY_CATEGORY_REPEAT_CALLERS,
1574 PRIORITY_CATEGORY_CONVERSATIONS,
John Spurlock1fc476d2015-04-14 16:05:20 -04001575 };
1576
Julia Reynolds229684b2020-02-23 20:39:04 -05001577 /** @hide */
1578 @IntDef(prefix = { "PRIORITY_SENDERS_" }, value = {
1579 PRIORITY_SENDERS_ANY,
1580 PRIORITY_SENDERS_CONTACTS,
1581 PRIORITY_SENDERS_STARRED,
1582 })
1583 @Retention(RetentionPolicy.SOURCE)
1584 public @interface PrioritySenders {}
1585
John Spurlock1fc476d2015-04-14 16:05:20 -04001586 /** Any sender is prioritized. */
1587 public static final int PRIORITY_SENDERS_ANY = 0;
1588 /** Saved contacts are prioritized. */
1589 public static final int PRIORITY_SENDERS_CONTACTS = 1;
1590 /** Only starred contacts are prioritized. */
1591 public static final int PRIORITY_SENDERS_STARRED = 2;
1592
Julia Reynolds24edc002020-01-29 16:35:32 -05001593
1594 /** @hide */
1595 @IntDef(prefix = { "CONVERSATION_SENDERS_" }, value = {
1596 CONVERSATION_SENDERS_ANYONE,
1597 CONVERSATION_SENDERS_IMPORTANT,
1598 CONVERSATION_SENDERS_NONE,
1599 })
1600 @Retention(RetentionPolicy.SOURCE)
1601 public @interface ConversationSenders {}
1602 /**
1603 * Used to indicate all conversations can bypass dnd.
1604 */
1605 public static final int CONVERSATION_SENDERS_ANYONE = ZenPolicy.CONVERSATION_SENDERS_ANYONE;
1606
1607 /**
1608 * Used to indicate important conversations can bypass dnd.
1609 */
1610 public static final int CONVERSATION_SENDERS_IMPORTANT =
1611 ZenPolicy.CONVERSATION_SENDERS_IMPORTANT;
1612
1613 /**
1614 * Used to indicate no conversations can bypass dnd.
1615 */
1616 public static final int CONVERSATION_SENDERS_NONE = ZenPolicy.CONVERSATION_SENDERS_NONE;
1617
John Spurlock1fc476d2015-04-14 16:05:20 -04001618 /** Notification categories to prioritize. Bitmask of PRIORITY_CATEGORY_* constants. */
1619 public final int priorityCategories;
1620
John Spurlock80774932015-05-07 17:38:50 -04001621 /** Notification senders to prioritize for calls. One of:
John Spurlock1fc476d2015-04-14 16:05:20 -04001622 * PRIORITY_SENDERS_ANY, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED */
John Spurlock80774932015-05-07 17:38:50 -04001623 public final int priorityCallSenders;
John Spurlock1fc476d2015-04-14 16:05:20 -04001624
John Spurlock80774932015-05-07 17:38:50 -04001625 /** Notification senders to prioritize for messages. One of:
1626 * PRIORITY_SENDERS_ANY, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED */
1627 public final int priorityMessageSenders;
1628
Julia Reynoldsd5607292016-02-05 15:25:58 -05001629 /**
Julia Reynolds24edc002020-01-29 16:35:32 -05001630 * Notification senders to prioritize for conversations. One of:
1631 * {@link #CONVERSATION_SENDERS_NONE}, {@link #CONVERSATION_SENDERS_IMPORTANT},
1632 * {@link #CONVERSATION_SENDERS_ANYONE}.
1633 */
1634 public final int priorityConversationSenders;
1635
1636 /**
1637 * @hide
1638 */
1639 public static final int CONVERSATION_SENDERS_UNSET = -1;
1640
1641 /**
Julia Reynoldsd5607292016-02-05 15:25:58 -05001642 * @hide
1643 */
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05001644 public static final int SUPPRESSED_EFFECTS_UNSET = -1;
Julia Reynoldsccc6ae62018-03-01 16:24:49 -05001645
Julia Reynoldsd5607292016-02-05 15:25:58 -05001646 /**
Julia Reynoldscedacef2016-03-04 08:18:47 -05001647 * Whether notifications suppressed by DND should not interrupt visually (e.g. with
1648 * notification lights or by turning the screen on) when the screen is off.
Julia Reynoldsccc6ae62018-03-01 16:24:49 -05001649 *
1650 * @deprecated use {@link #SUPPRESSED_EFFECT_FULL_SCREEN_INTENT} and
1651 * {@link #SUPPRESSED_EFFECT_AMBIENT} and {@link #SUPPRESSED_EFFECT_LIGHTS} individually.
Julia Reynoldsd5607292016-02-05 15:25:58 -05001652 */
Julia Reynoldsccc6ae62018-03-01 16:24:49 -05001653 @Deprecated
Julia Reynoldsd5607292016-02-05 15:25:58 -05001654 public static final int SUPPRESSED_EFFECT_SCREEN_OFF = 1 << 0;
1655 /**
Julia Reynoldscedacef2016-03-04 08:18:47 -05001656 * Whether notifications suppressed by DND should not interrupt visually when the screen
1657 * is on (e.g. by peeking onto the screen).
Julia Reynoldsccc6ae62018-03-01 16:24:49 -05001658 *
1659 * @deprecated use {@link #SUPPRESSED_EFFECT_PEEK}.
Julia Reynoldsd5607292016-02-05 15:25:58 -05001660 */
Julia Reynoldsccc6ae62018-03-01 16:24:49 -05001661 @Deprecated
Julia Reynoldsd5607292016-02-05 15:25:58 -05001662 public static final int SUPPRESSED_EFFECT_SCREEN_ON = 1 << 1;
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05001663
Julia Reynoldsccc6ae62018-03-01 16:24:49 -05001664 /**
1665 * Whether {@link Notification#fullScreenIntent full screen intents} from
1666 * notifications intercepted by DND are blocked.
1667 */
1668 public static final int SUPPRESSED_EFFECT_FULL_SCREEN_INTENT = 1 << 2;
1669
1670 /**
1671 * Whether {@link NotificationChannel#shouldShowLights() notification lights} from
1672 * notifications intercepted by DND are blocked.
1673 */
1674 public static final int SUPPRESSED_EFFECT_LIGHTS = 1 << 3;
1675
1676 /**
1677 * Whether notifications intercepted by DND are prevented from peeking.
1678 */
1679 public static final int SUPPRESSED_EFFECT_PEEK = 1 << 4;
1680
1681 /**
1682 * Whether notifications intercepted by DND are prevented from appearing in the status bar,
1683 * on devices that support status bars.
1684 */
1685 public static final int SUPPRESSED_EFFECT_STATUS_BAR = 1 << 5;
1686
1687 /**
1688 * Whether {@link NotificationChannel#canShowBadge() badges} from
1689 * notifications intercepted by DND are blocked on devices that support badging.
1690 */
1691 public static final int SUPPRESSED_EFFECT_BADGE = 1 << 6;
1692
1693 /**
1694 * Whether notification intercepted by DND are prevented from appearing on ambient displays
1695 * on devices that support ambient display.
1696 */
1697 public static final int SUPPRESSED_EFFECT_AMBIENT = 1 << 7;
1698
1699 /**
1700 * Whether notification intercepted by DND are prevented from appearing in notification
1701 * list views like the notification shade or lockscreen on devices that support those
1702 * views.
1703 */
1704 public static final int SUPPRESSED_EFFECT_NOTIFICATION_LIST = 1 << 8;
1705
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05001706 private static final int[] ALL_SUPPRESSED_EFFECTS = {
Julia Reynoldsd5607292016-02-05 15:25:58 -05001707 SUPPRESSED_EFFECT_SCREEN_OFF,
Julia Reynolds61721582016-01-05 08:35:25 -05001708 SUPPRESSED_EFFECT_SCREEN_ON,
Julia Reynoldsccc6ae62018-03-01 16:24:49 -05001709 SUPPRESSED_EFFECT_FULL_SCREEN_INTENT,
1710 SUPPRESSED_EFFECT_LIGHTS,
1711 SUPPRESSED_EFFECT_PEEK,
1712 SUPPRESSED_EFFECT_STATUS_BAR,
1713 SUPPRESSED_EFFECT_BADGE,
1714 SUPPRESSED_EFFECT_AMBIENT,
1715 SUPPRESSED_EFFECT_NOTIFICATION_LIST
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05001716 };
1717
1718 /**
1719 * Visual effects to suppress for a notification that is filtered by Do Not Disturb mode.
1720 * Bitmask of SUPPRESSED_EFFECT_* constants.
1721 */
1722 public final int suppressedVisualEffects;
1723
Julia Reynoldscedacef2016-03-04 08:18:47 -05001724 /**
Beverly86d076f2018-04-17 14:44:52 -04001725 * @hide
1726 */
1727 public static final int STATE_CHANNELS_BYPASSING_DND = 1 << 0;
1728
1729 /**
1730 * @hide
1731 */
1732 public static final int STATE_UNSET = -1;
1733
1734 /**
1735 * Notification state information that is necessary to determine Do Not Disturb behavior.
1736 * Bitmask of STATE_* constants.
1737 * @hide
1738 */
1739 public final int state;
1740
1741 /**
Julia Reynoldscedacef2016-03-04 08:18:47 -05001742 * Constructs a policy for Do Not Disturb priority mode behavior.
1743 *
Julia Reynoldsccc6ae62018-03-01 16:24:49 -05001744 * <p>
1745 * Apps that target API levels below {@link Build.VERSION_CODES#P} cannot
1746 * change user-designated values to allow or disallow
1747 * {@link Policy#PRIORITY_CATEGORY_ALARMS}, {@link Policy#PRIORITY_CATEGORY_SYSTEM}, and
1748 * {@link Policy#PRIORITY_CATEGORY_MEDIA} from bypassing dnd.
1749 *
Julia Reynoldscedacef2016-03-04 08:18:47 -05001750 * @param priorityCategories bitmask of categories of notifications that can bypass DND.
1751 * @param priorityCallSenders which callers can bypass DND.
1752 * @param priorityMessageSenders which message senders can bypass DND.
1753 */
John Spurlock80774932015-05-07 17:38:50 -04001754 public Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders) {
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05001755 this(priorityCategories, priorityCallSenders, priorityMessageSenders,
Julia Reynolds24edc002020-01-29 16:35:32 -05001756 SUPPRESSED_EFFECTS_UNSET, STATE_UNSET, CONVERSATION_SENDERS_UNSET);
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05001757 }
1758
Julia Reynoldscedacef2016-03-04 08:18:47 -05001759 /**
1760 * Constructs a policy for Do Not Disturb priority mode behavior.
1761 *
Julia Reynoldsccc6ae62018-03-01 16:24:49 -05001762 * <p>
Julia Reynolds24edc002020-01-29 16:35:32 -05001763 * Apps that target API levels below {@link Build.VERSION_CODES#R} cannot
Julia Reynoldsccc6ae62018-03-01 16:24:49 -05001764 * change user-designated values to allow or disallow
Julia Reynolds24edc002020-01-29 16:35:32 -05001765 * {@link Policy#PRIORITY_CATEGORY_CONVERSATIONS}, from bypassing dnd.
Julia Reynoldsccc6ae62018-03-01 16:24:49 -05001766 * <p>
1767 * Additionally, apps that target API levels below {@link Build.VERSION_CODES#P} can
1768 * only modify the {@link #SUPPRESSED_EFFECT_SCREEN_ON} and
1769 * {@link #SUPPRESSED_EFFECT_SCREEN_OFF} bits of the suppressed visual effects field.
1770 * All other suppressed effects will be ignored and reconstituted from the screen on
1771 * and screen off values.
1772 * <p>
1773 * Apps that target {@link Build.VERSION_CODES#P} or above can set any
1774 * suppressed visual effects. However, if any suppressed effects >
1775 * {@link #SUPPRESSED_EFFECT_SCREEN_ON} are set, {@link #SUPPRESSED_EFFECT_SCREEN_ON}
1776 * and {@link #SUPPRESSED_EFFECT_SCREEN_OFF} will be ignored and reconstituted from
1777 * the more specific suppressed visual effect bits. Apps should migrate to targeting
1778 * specific effects instead of the deprecated {@link #SUPPRESSED_EFFECT_SCREEN_ON} and
1779 * {@link #SUPPRESSED_EFFECT_SCREEN_OFF} effects.
1780 *
Julia Reynoldscedacef2016-03-04 08:18:47 -05001781 * @param priorityCategories bitmask of categories of notifications that can bypass DND.
1782 * @param priorityCallSenders which callers can bypass DND.
1783 * @param priorityMessageSenders which message senders can bypass DND.
1784 * @param suppressedVisualEffects which visual interruptions should be suppressed from
1785 * notifications that are filtered by DND.
1786 */
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05001787 public Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders,
1788 int suppressedVisualEffects) {
Julia Reynolds24edc002020-01-29 16:35:32 -05001789 this(priorityCategories, priorityCallSenders, priorityMessageSenders,
1790 suppressedVisualEffects, STATE_UNSET, CONVERSATION_SENDERS_UNSET);
1791 }
1792
1793 /**
1794 * Constructs a policy for Do Not Disturb priority mode behavior.
1795 *
1796 * <p>
1797 * Apps that target API levels below {@link Build.VERSION_CODES#P} cannot
1798 * change user-designated values to allow or disallow
1799 * {@link Policy#PRIORITY_CATEGORY_CONVERSATIONS} from bypassing dnd. If you do need
1800 * to change them, use a {@link ZenPolicy} associated with an {@link AutomaticZenRule}
1801 * instead of changing the global setting.
1802 * <p>
1803 * Apps that target API levels below {@link Build.VERSION_CODES#P} cannot
1804 * change user-designated values to allow or disallow
1805 * {@link Policy#PRIORITY_CATEGORY_ALARMS},
1806 * {@link Policy#PRIORITY_CATEGORY_SYSTEM}, and
1807 * {@link Policy#PRIORITY_CATEGORY_MEDIA} from bypassing dnd.
1808 * <p>
1809 * Additionally, apps that target API levels below {@link Build.VERSION_CODES#P} can
1810 * only modify the {@link #SUPPRESSED_EFFECT_SCREEN_ON} and
1811 * {@link #SUPPRESSED_EFFECT_SCREEN_OFF} bits of the suppressed visual effects field.
1812 * All other suppressed effects will be ignored and reconstituted from the screen on
1813 * and screen off values.
1814 * <p>
1815 * Apps that target {@link Build.VERSION_CODES#P} or above can set any
1816 * suppressed visual effects. However, if any suppressed effects >
1817 * {@link #SUPPRESSED_EFFECT_SCREEN_ON} are set, {@link #SUPPRESSED_EFFECT_SCREEN_ON}
1818 * and {@link #SUPPRESSED_EFFECT_SCREEN_OFF} will be ignored and reconstituted from
1819 * the more specific suppressed visual effect bits. Apps should migrate to targeting
1820 * specific effects instead of the deprecated {@link #SUPPRESSED_EFFECT_SCREEN_ON} and
1821 * {@link #SUPPRESSED_EFFECT_SCREEN_OFF} effects.
1822 *
1823 * @param priorityCategories bitmask of categories of notifications that can bypass DND.
1824 * @param priorityCallSenders which callers can bypass DND.
1825 * @param priorityMessageSenders which message senders can bypass DND.
1826 * @param suppressedVisualEffects which visual interruptions should be suppressed from
1827 * notifications that are filtered by DND.
1828 */
Julia Reynolds229684b2020-02-23 20:39:04 -05001829 public Policy(int priorityCategories, @PrioritySenders int priorityCallSenders,
1830 @PrioritySenders int priorityMessageSenders,
1831 int suppressedVisualEffects, @ConversationSenders int priorityConversationSenders) {
Julia Reynolds24edc002020-01-29 16:35:32 -05001832 this(priorityCategories, priorityCallSenders, priorityMessageSenders,
1833 suppressedVisualEffects, STATE_UNSET, priorityConversationSenders);
Beverly86d076f2018-04-17 14:44:52 -04001834 }
1835
1836 /** @hide */
1837 public Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders,
Julia Reynolds24edc002020-01-29 16:35:32 -05001838 int suppressedVisualEffects, int state, int priorityConversationSenders) {
Beverly86d076f2018-04-17 14:44:52 -04001839 this.priorityCategories = priorityCategories;
1840 this.priorityCallSenders = priorityCallSenders;
1841 this.priorityMessageSenders = priorityMessageSenders;
1842 this.suppressedVisualEffects = suppressedVisualEffects;
1843 this.state = state;
Julia Reynolds24edc002020-01-29 16:35:32 -05001844 this.priorityConversationSenders = priorityConversationSenders;
John Spurlock1fc476d2015-04-14 16:05:20 -04001845 }
1846
Julia Reynolds24edc002020-01-29 16:35:32 -05001847
John Spurlock1fc476d2015-04-14 16:05:20 -04001848 /** @hide */
1849 public Policy(Parcel source) {
Beverly86d076f2018-04-17 14:44:52 -04001850 this(source.readInt(), source.readInt(), source.readInt(), source.readInt(),
Julia Reynolds24edc002020-01-29 16:35:32 -05001851 source.readInt(), source.readInt());
John Spurlock1fc476d2015-04-14 16:05:20 -04001852 }
1853
1854 @Override
1855 public void writeToParcel(Parcel dest, int flags) {
1856 dest.writeInt(priorityCategories);
John Spurlock80774932015-05-07 17:38:50 -04001857 dest.writeInt(priorityCallSenders);
1858 dest.writeInt(priorityMessageSenders);
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05001859 dest.writeInt(suppressedVisualEffects);
Beverly86d076f2018-04-17 14:44:52 -04001860 dest.writeInt(state);
Julia Reynolds24edc002020-01-29 16:35:32 -05001861 dest.writeInt(priorityConversationSenders);
John Spurlock1fc476d2015-04-14 16:05:20 -04001862 }
1863
1864 @Override
1865 public int describeContents() {
1866 return 0;
1867 }
1868
1869 @Override
1870 public int hashCode() {
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05001871 return Objects.hash(priorityCategories, priorityCallSenders, priorityMessageSenders,
Julia Reynolds24edc002020-01-29 16:35:32 -05001872 suppressedVisualEffects, state, priorityConversationSenders);
John Spurlock1fc476d2015-04-14 16:05:20 -04001873 }
1874
1875 @Override
1876 public boolean equals(Object o) {
1877 if (!(o instanceof Policy)) return false;
1878 if (o == this) return true;
1879 final Policy other = (Policy) o;
1880 return other.priorityCategories == priorityCategories
John Spurlock80774932015-05-07 17:38:50 -04001881 && other.priorityCallSenders == priorityCallSenders
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05001882 && other.priorityMessageSenders == priorityMessageSenders
Beverly9cb05c92018-11-28 10:03:23 -05001883 && suppressedVisualEffectsEqual(suppressedVisualEffects,
Beverly4f8b0222019-10-24 13:35:03 -04001884 other.suppressedVisualEffects)
Julia Reynolds24edc002020-01-29 16:35:32 -05001885 && other.state == this.state
1886 && other.priorityConversationSenders == this.priorityConversationSenders;
Beverly9cb05c92018-11-28 10:03:23 -05001887 }
1888
Beverly9cb05c92018-11-28 10:03:23 -05001889 private boolean suppressedVisualEffectsEqual(int suppressedEffects,
1890 int otherSuppressedVisualEffects) {
1891 if (suppressedEffects == otherSuppressedVisualEffects) {
1892 return true;
1893 }
1894
1895 if ((suppressedEffects & SUPPRESSED_EFFECT_SCREEN_ON) != 0) {
1896 suppressedEffects |= SUPPRESSED_EFFECT_PEEK;
1897 }
1898 if ((suppressedEffects & SUPPRESSED_EFFECT_SCREEN_OFF) != 0) {
1899 suppressedEffects |= SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
1900 suppressedEffects |= SUPPRESSED_EFFECT_LIGHTS;
1901 suppressedEffects |= SUPPRESSED_EFFECT_AMBIENT;
1902 }
1903
1904 if ((otherSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_ON) != 0) {
1905 otherSuppressedVisualEffects |= SUPPRESSED_EFFECT_PEEK;
1906 }
1907 if ((otherSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_OFF) != 0) {
1908 otherSuppressedVisualEffects |= SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
1909 otherSuppressedVisualEffects |= SUPPRESSED_EFFECT_LIGHTS;
1910 otherSuppressedVisualEffects |= SUPPRESSED_EFFECT_AMBIENT;
1911 }
1912
1913 if ((suppressedEffects & SUPPRESSED_EFFECT_SCREEN_ON)
1914 != (otherSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_ON)) {
1915 int currSuppressedEffects = (suppressedEffects & SUPPRESSED_EFFECT_SCREEN_ON) != 0
1916 ? otherSuppressedVisualEffects : suppressedEffects;
1917 if ((currSuppressedEffects & SUPPRESSED_EFFECT_PEEK) == 0) {
1918 return false;
1919 }
1920 }
1921
1922 if ((suppressedEffects & SUPPRESSED_EFFECT_SCREEN_OFF)
1923 != (otherSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_OFF)) {
1924 int currSuppressedEffects = (suppressedEffects & SUPPRESSED_EFFECT_SCREEN_OFF) != 0
1925 ? otherSuppressedVisualEffects : suppressedEffects;
1926 if ((currSuppressedEffects & SUPPRESSED_EFFECT_FULL_SCREEN_INTENT) == 0
1927 || (currSuppressedEffects & SUPPRESSED_EFFECT_LIGHTS) == 0
1928 || (currSuppressedEffects & SUPPRESSED_EFFECT_AMBIENT) == 0) {
1929 return false;
1930 }
1931 }
1932
1933 int thisWithoutOldEffects = suppressedEffects
1934 & ~SUPPRESSED_EFFECT_SCREEN_ON
1935 & ~SUPPRESSED_EFFECT_SCREEN_OFF;
1936 int otherWithoutOldEffects = otherSuppressedVisualEffects
1937 & ~SUPPRESSED_EFFECT_SCREEN_ON
1938 & ~SUPPRESSED_EFFECT_SCREEN_OFF;
1939 return thisWithoutOldEffects == otherWithoutOldEffects;
John Spurlock1fc476d2015-04-14 16:05:20 -04001940 }
1941
1942 @Override
1943 public String toString() {
1944 return "NotificationManager.Policy["
1945 + "priorityCategories=" + priorityCategoriesToString(priorityCategories)
John Spurlock80774932015-05-07 17:38:50 -04001946 + ",priorityCallSenders=" + prioritySendersToString(priorityCallSenders)
1947 + ",priorityMessageSenders=" + prioritySendersToString(priorityMessageSenders)
Julia Reynolds24edc002020-01-29 16:35:32 -05001948 + ",priorityConvSenders="
1949 + conversationSendersToString(priorityConversationSenders)
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05001950 + ",suppressedVisualEffects="
1951 + suppressedEffectsToString(suppressedVisualEffects)
Beverly86d076f2018-04-17 14:44:52 -04001952 + ",areChannelsBypassingDnd=" + (((state & STATE_CHANNELS_BYPASSING_DND) != 0)
1953 ? "true" : "false")
John Spurlock1fc476d2015-04-14 16:05:20 -04001954 + "]";
1955 }
1956
Kweku Adams5ec78cd2017-09-25 16:29:54 -07001957 /** @hide */
Jeffrey Huangcb782852019-12-05 11:28:11 -08001958 public void dumpDebug(ProtoOutputStream proto, long fieldId) {
Kweku Adams5ec78cd2017-09-25 16:29:54 -07001959 final long pToken = proto.start(fieldId);
1960
1961 bitwiseToProtoEnum(proto, PolicyProto.PRIORITY_CATEGORIES, priorityCategories);
1962 proto.write(PolicyProto.PRIORITY_CALL_SENDER, priorityCallSenders);
1963 proto.write(PolicyProto.PRIORITY_MESSAGE_SENDER, priorityMessageSenders);
1964 bitwiseToProtoEnum(
1965 proto, PolicyProto.SUPPRESSED_VISUAL_EFFECTS, suppressedVisualEffects);
1966
1967 proto.end(pToken);
1968 }
1969
1970 private static void bitwiseToProtoEnum(ProtoOutputStream proto, long fieldId, int data) {
1971 for (int i = 1; data > 0; ++i, data >>>= 1) {
1972 if ((data & 1) == 1) {
1973 proto.write(fieldId, i);
1974 }
1975 }
1976 }
1977
Julia Reynoldsccc6ae62018-03-01 16:24:49 -05001978 /**
1979 * @hide
1980 */
1981 public static int getAllSuppressedVisualEffects() {
1982 int effects = 0;
1983 for (int i = 0; i < ALL_SUPPRESSED_EFFECTS.length; i++) {
1984 effects |= ALL_SUPPRESSED_EFFECTS[i];
1985 }
1986 return effects;
1987 }
1988
1989 /**
1990 * @hide
1991 */
1992 public static boolean areAllVisualEffectsSuppressed(int effects) {
1993 for (int i = 0; i < ALL_SUPPRESSED_EFFECTS.length; i++) {
1994 final int effect = ALL_SUPPRESSED_EFFECTS[i];
1995 if ((effects & effect) == 0) {
1996 return false;
1997 }
1998 }
1999 return true;
2000 }
2001
Julia Reynolds9aa1c9e2018-04-09 11:31:15 -04002002 private static int toggleEffects(int currentEffects, int[] effects, boolean suppress) {
2003 for (int i = 0; i < effects.length; i++) {
2004 final int effect = effects[i];
2005 if (suppress) {
2006 currentEffects |= effect;
2007 } else {
2008 currentEffects &= ~effect;
2009 }
2010 }
2011 return currentEffects;
2012 }
2013
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05002014 public static String suppressedEffectsToString(int effects) {
2015 if (effects <= 0) return "";
2016 final StringBuilder sb = new StringBuilder();
2017 for (int i = 0; i < ALL_SUPPRESSED_EFFECTS.length; i++) {
2018 final int effect = ALL_SUPPRESSED_EFFECTS[i];
2019 if ((effects & effect) != 0) {
2020 if (sb.length() > 0) sb.append(',');
2021 sb.append(effectToString(effect));
2022 }
2023 effects &= ~effect;
2024 }
2025 if (effects != 0) {
2026 if (sb.length() > 0) sb.append(',');
2027 sb.append("UNKNOWN_").append(effects);
2028 }
2029 return sb.toString();
2030 }
2031
John Spurlock1fc476d2015-04-14 16:05:20 -04002032 public static String priorityCategoriesToString(int priorityCategories) {
2033 if (priorityCategories == 0) return "";
2034 final StringBuilder sb = new StringBuilder();
2035 for (int i = 0; i < ALL_PRIORITY_CATEGORIES.length; i++) {
2036 final int priorityCategory = ALL_PRIORITY_CATEGORIES[i];
2037 if ((priorityCategories & priorityCategory) != 0) {
2038 if (sb.length() > 0) sb.append(',');
2039 sb.append(priorityCategoryToString(priorityCategory));
2040 }
2041 priorityCategories &= ~priorityCategory;
2042 }
2043 if (priorityCategories != 0) {
2044 if (sb.length() > 0) sb.append(',');
2045 sb.append("PRIORITY_CATEGORY_UNKNOWN_").append(priorityCategories);
2046 }
2047 return sb.toString();
2048 }
2049
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05002050 private static String effectToString(int effect) {
2051 switch (effect) {
Julia Reynoldsccc6ae62018-03-01 16:24:49 -05002052 case SUPPRESSED_EFFECT_FULL_SCREEN_INTENT:
2053 return "SUPPRESSED_EFFECT_FULL_SCREEN_INTENT";
2054 case SUPPRESSED_EFFECT_LIGHTS:
2055 return "SUPPRESSED_EFFECT_LIGHTS";
2056 case SUPPRESSED_EFFECT_PEEK:
2057 return "SUPPRESSED_EFFECT_PEEK";
2058 case SUPPRESSED_EFFECT_STATUS_BAR:
2059 return "SUPPRESSED_EFFECT_STATUS_BAR";
2060 case SUPPRESSED_EFFECT_BADGE:
2061 return "SUPPRESSED_EFFECT_BADGE";
2062 case SUPPRESSED_EFFECT_AMBIENT:
2063 return "SUPPRESSED_EFFECT_AMBIENT";
2064 case SUPPRESSED_EFFECT_NOTIFICATION_LIST:
2065 return "SUPPRESSED_EFFECT_NOTIFICATION_LIST";
2066 case SUPPRESSED_EFFECT_SCREEN_OFF:
2067 return "SUPPRESSED_EFFECT_SCREEN_OFF";
2068 case SUPPRESSED_EFFECT_SCREEN_ON:
2069 return "SUPPRESSED_EFFECT_SCREEN_ON";
2070 case SUPPRESSED_EFFECTS_UNSET:
2071 return "SUPPRESSED_EFFECTS_UNSET";
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05002072 default: return "UNKNOWN_" + effect;
2073 }
2074 }
2075
John Spurlock1fc476d2015-04-14 16:05:20 -04002076 private static String priorityCategoryToString(int priorityCategory) {
2077 switch (priorityCategory) {
2078 case PRIORITY_CATEGORY_REMINDERS: return "PRIORITY_CATEGORY_REMINDERS";
2079 case PRIORITY_CATEGORY_EVENTS: return "PRIORITY_CATEGORY_EVENTS";
2080 case PRIORITY_CATEGORY_MESSAGES: return "PRIORITY_CATEGORY_MESSAGES";
2081 case PRIORITY_CATEGORY_CALLS: return "PRIORITY_CATEGORY_CALLS";
2082 case PRIORITY_CATEGORY_REPEAT_CALLERS: return "PRIORITY_CATEGORY_REPEAT_CALLERS";
Beverly04216872017-09-28 10:55:32 -04002083 case PRIORITY_CATEGORY_ALARMS: return "PRIORITY_CATEGORY_ALARMS";
Beverlyd6964762018-02-16 14:07:03 -05002084 case PRIORITY_CATEGORY_MEDIA: return "PRIORITY_CATEGORY_MEDIA";
2085 case PRIORITY_CATEGORY_SYSTEM: return "PRIORITY_CATEGORY_SYSTEM";
Julia Reynolds24edc002020-01-29 16:35:32 -05002086 case PRIORITY_CATEGORY_CONVERSATIONS: return "PRIORITY_CATEGORY_CONVERSATIONS";
John Spurlock1fc476d2015-04-14 16:05:20 -04002087 default: return "PRIORITY_CATEGORY_UNKNOWN_" + priorityCategory;
2088 }
2089 }
2090
2091 public static String prioritySendersToString(int prioritySenders) {
2092 switch (prioritySenders) {
2093 case PRIORITY_SENDERS_ANY: return "PRIORITY_SENDERS_ANY";
2094 case PRIORITY_SENDERS_CONTACTS: return "PRIORITY_SENDERS_CONTACTS";
2095 case PRIORITY_SENDERS_STARRED: return "PRIORITY_SENDERS_STARRED";
2096 default: return "PRIORITY_SENDERS_UNKNOWN_" + prioritySenders;
2097 }
2098 }
2099
Julia Reynolds24edc002020-01-29 16:35:32 -05002100 /**
2101 * @hide
2102 */
2103 public static @NonNull String conversationSendersToString(int priorityConversationSenders) {
2104 switch (priorityConversationSenders) {
2105 case CONVERSATION_SENDERS_ANYONE:
2106 return "anyone";
2107 case CONVERSATION_SENDERS_IMPORTANT:
2108 return "important";
2109 case CONVERSATION_SENDERS_NONE:
2110 return "none";
2111 case CONVERSATION_SENDERS_UNSET:
2112 return "unset";
2113 }
2114 return "invalidConversationType{" + priorityConversationSenders + "}";
2115 }
2116
2117 public static final @android.annotation.NonNull Parcelable.Creator<Policy> CREATOR
2118 = new Parcelable.Creator<Policy>() {
John Spurlock1fc476d2015-04-14 16:05:20 -04002119 @Override
2120 public Policy createFromParcel(Parcel in) {
2121 return new Policy(in);
2122 }
2123
2124 @Override
2125 public Policy[] newArray(int size) {
2126 return new Policy[size];
2127 }
2128 };
Beverlyff2df9b2018-10-10 16:54:10 -04002129
2130 /** @hide **/
2131 public boolean allowAlarms() {
2132 return (priorityCategories & PRIORITY_CATEGORY_ALARMS) != 0;
2133 }
2134
2135 /** @hide **/
2136 public boolean allowMedia() {
2137 return (priorityCategories & PRIORITY_CATEGORY_MEDIA) != 0;
2138 }
2139
2140 /** @hide **/
2141 public boolean allowSystem() {
2142 return (priorityCategories & PRIORITY_CATEGORY_SYSTEM) != 0;
2143 }
2144
2145 /** @hide **/
2146 public boolean allowRepeatCallers() {
2147 return (priorityCategories & PRIORITY_CATEGORY_REPEAT_CALLERS) != 0;
2148 }
2149
2150 /** @hide **/
2151 public boolean allowCalls() {
2152 return (priorityCategories & PRIORITY_CATEGORY_CALLS) != 0;
2153 }
2154
2155 /** @hide **/
Julia Reynolds24edc002020-01-29 16:35:32 -05002156 public boolean allowConversations() {
2157 return (priorityCategories & PRIORITY_CATEGORY_CONVERSATIONS) != 0;
2158 }
2159
2160 /** @hide **/
Beverlyff2df9b2018-10-10 16:54:10 -04002161 public boolean allowMessages() {
2162 return (priorityCategories & PRIORITY_CATEGORY_MESSAGES) != 0;
2163 }
2164
2165 /** @hide **/
2166 public boolean allowEvents() {
2167 return (priorityCategories & PRIORITY_CATEGORY_EVENTS) != 0;
2168 }
2169
2170 /** @hide **/
2171 public boolean allowReminders() {
2172 return (priorityCategories & PRIORITY_CATEGORY_REMINDERS) != 0;
2173 }
2174
2175 /** @hide **/
2176 public int allowCallsFrom() {
2177 return priorityCallSenders;
2178 }
2179
2180 /** @hide **/
2181 public int allowMessagesFrom() {
2182 return priorityMessageSenders;
2183 }
2184
Beverly12196702018-12-12 15:05:51 -05002185 /** @hide **/
Julia Reynolds24edc002020-01-29 16:35:32 -05002186 public int allowConversationsFrom() {
2187 return priorityConversationSenders;
2188 }
2189
2190 /** @hide **/
Beverly12196702018-12-12 15:05:51 -05002191 public boolean showFullScreenIntents() {
2192 return (suppressedVisualEffects & SUPPRESSED_EFFECT_FULL_SCREEN_INTENT) == 0;
2193 }
2194
2195 /** @hide **/
2196 public boolean showLights() {
2197 return (suppressedVisualEffects & SUPPRESSED_EFFECT_LIGHTS) == 0;
2198 }
2199
2200 /** @hide **/
2201 public boolean showPeeking() {
2202 return (suppressedVisualEffects & SUPPRESSED_EFFECT_PEEK) == 0;
2203 }
2204
2205 /** @hide **/
2206 public boolean showStatusBarIcons() {
2207 return (suppressedVisualEffects & SUPPRESSED_EFFECT_STATUS_BAR) == 0;
2208 }
2209
2210 /** @hide **/
2211 public boolean showAmbient() {
2212 return (suppressedVisualEffects & SUPPRESSED_EFFECT_AMBIENT) == 0;
2213 }
2214
2215 /** @hide **/
2216 public boolean showBadges() {
2217 return (suppressedVisualEffects & SUPPRESSED_EFFECT_BADGE) == 0;
2218 }
2219
2220 /** @hide **/
2221 public boolean showInNotificationList() {
2222 return (suppressedVisualEffects & SUPPRESSED_EFFECT_NOTIFICATION_LIST) == 0;
2223 }
2224
Beverlyff2df9b2018-10-10 16:54:10 -04002225 /**
2226 * returns a deep copy of this policy
2227 * @hide
2228 */
2229 public Policy copy() {
2230 final Parcel parcel = Parcel.obtain();
2231 try {
2232 writeToParcel(parcel, 0);
2233 parcel.setDataPosition(0);
2234 return new Policy(parcel);
2235 } finally {
2236 parcel.recycle();
2237 }
2238 }
John Spurlock1fc476d2015-04-14 16:05:20 -04002239 }
2240
Dan Sandler994349c2015-04-15 11:02:54 -04002241 /**
2242 * Recover a list of active notifications: ones that have been posted by the calling app that
2243 * have not yet been dismissed by the user or {@link #cancel(String, int)}ed by the app.
2244 *
Julia Reynoldsd12392c2018-12-17 14:06:04 -05002245 * <p><Each notification is embedded in a {@link StatusBarNotification} object, including the
Dan Sandler994349c2015-04-15 11:02:54 -04002246 * original <code>tag</code> and <code>id</code> supplied to
2247 * {@link #notify(String, int, Notification) notify()}
2248 * (via {@link StatusBarNotification#getTag() getTag()} and
2249 * {@link StatusBarNotification#getId() getId()}) as well as a copy of the original
2250 * {@link Notification} object (via {@link StatusBarNotification#getNotification()}).
Julia Reynoldsd12392c2018-12-17 14:06:04 -05002251 * </p>
2252 * <p>From {@link Build.VERSION_CODES#Q}, will also return notifications you've posted as an
2253 * app's notification delegate via
2254 * {@link NotificationManager#notifyAsPackage(String, String, int, Notification)}.
2255 * </p>
Dan Sandler994349c2015-04-15 11:02:54 -04002256 *
2257 * @return An array of {@link StatusBarNotification}.
2258 */
2259 public StatusBarNotification[] getActiveNotifications() {
2260 final INotificationManager service = getService();
2261 final String pkg = mContext.getPackageName();
2262 try {
2263 final ParceledListSlice<StatusBarNotification> parceledList
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002264 = service.getAppActiveNotifications(pkg, mContext.getUserId());
Julia Reynolds34a80842018-09-21 13:01:00 -04002265 if (parceledList != null) {
2266 final List<StatusBarNotification> list = parceledList.getList();
2267 return list.toArray(new StatusBarNotification[list.size()]);
2268 }
Dan Sandler994349c2015-04-15 11:02:54 -04002269 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002270 throw e.rethrowFromSystemServer();
Dan Sandler994349c2015-04-15 11:02:54 -04002271 }
Julia Reynolds34a80842018-09-21 13:01:00 -04002272 return new StatusBarNotification[0];
Dan Sandler994349c2015-04-15 11:02:54 -04002273 }
John Spurlock80774932015-05-07 17:38:50 -04002274
2275 /**
2276 * Gets the current notification interruption filter.
John Spurlock80774932015-05-07 17:38:50 -04002277 * <p>
Jeff Sharkey6503bd82017-04-19 23:24:18 -06002278 * The interruption filter defines which notifications are allowed to
2279 * interrupt the user (e.g. via sound &amp; vibration) and is applied
2280 * globally.
John Spurlock80774932015-05-07 17:38:50 -04002281 */
Julia Reynolds0edb50c2016-02-26 14:08:25 -05002282 public final @InterruptionFilter int getCurrentInterruptionFilter() {
John Spurlock80774932015-05-07 17:38:50 -04002283 final INotificationManager service = getService();
2284 try {
2285 return zenModeToInterruptionFilter(service.getZenMode());
2286 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002287 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -04002288 }
John Spurlock80774932015-05-07 17:38:50 -04002289 }
2290
2291 /**
2292 * Sets the current notification interruption filter.
John Spurlock80774932015-05-07 17:38:50 -04002293 * <p>
Jeff Sharkey6503bd82017-04-19 23:24:18 -06002294 * The interruption filter defines which notifications are allowed to
2295 * interrupt the user (e.g. via sound &amp; vibration) and is applied
2296 * globally.
John Spurlock80774932015-05-07 17:38:50 -04002297 * <p>
Jeff Sharkey6503bd82017-04-19 23:24:18 -06002298 * Only available if policy access is granted to this package. See
2299 * {@link #isNotificationPolicyAccessGranted}.
John Spurlock80774932015-05-07 17:38:50 -04002300 */
Jeff Sharkey6503bd82017-04-19 23:24:18 -06002301 public final void setInterruptionFilter(@InterruptionFilter int interruptionFilter) {
John Spurlock80774932015-05-07 17:38:50 -04002302 final INotificationManager service = getService();
2303 try {
2304 service.setInterruptionFilter(mContext.getOpPackageName(), interruptionFilter);
2305 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002306 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -04002307 }
2308 }
2309
2310 /** @hide */
2311 public static int zenModeToInterruptionFilter(int zen) {
2312 switch (zen) {
2313 case Global.ZEN_MODE_OFF: return INTERRUPTION_FILTER_ALL;
2314 case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return INTERRUPTION_FILTER_PRIORITY;
2315 case Global.ZEN_MODE_ALARMS: return INTERRUPTION_FILTER_ALARMS;
2316 case Global.ZEN_MODE_NO_INTERRUPTIONS: return INTERRUPTION_FILTER_NONE;
2317 default: return INTERRUPTION_FILTER_UNKNOWN;
2318 }
2319 }
2320
2321 /** @hide */
2322 public static int zenModeFromInterruptionFilter(int interruptionFilter, int defValue) {
2323 switch (interruptionFilter) {
2324 case INTERRUPTION_FILTER_ALL: return Global.ZEN_MODE_OFF;
2325 case INTERRUPTION_FILTER_PRIORITY: return Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
2326 case INTERRUPTION_FILTER_ALARMS: return Global.ZEN_MODE_ALARMS;
2327 case INTERRUPTION_FILTER_NONE: return Global.ZEN_MODE_NO_INTERRUPTIONS;
2328 default: return defValue;
2329 }
2330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331}