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