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; |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 21 | import android.annotation.SdkConstant; |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 22 | import android.app.Notification.Builder; |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 23 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 25 | import android.content.pm.ParceledListSlice; |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 26 | import android.graphics.drawable.Icon; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 27 | import android.net.Uri; |
Dan Sandler | 4e78706 | 2015-06-17 15:09:48 -0400 | [diff] [blame] | 28 | import android.os.Build; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 29 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.os.Handler; |
| 31 | import android.os.IBinder; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 32 | import android.os.Parcel; |
| 33 | import android.os.Parcelable; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 34 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.os.ServiceManager; |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 36 | import android.os.StrictMode; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 37 | import android.os.UserHandle; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 38 | import android.provider.Settings.Global; |
Chris Wren | 5ab5c74 | 2016-05-10 15:32:23 -0400 | [diff] [blame] | 39 | import android.service.notification.NotificationListenerService.Ranking; |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 40 | import android.service.notification.StatusBarNotification; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 41 | import android.service.notification.ZenModeConfig; |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 42 | import android.util.ArraySet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.util.Log; |
| 44 | |
Julia Reynolds | 0edb50c | 2016-02-26 14:08:25 -0500 | [diff] [blame] | 45 | import java.lang.annotation.Retention; |
| 46 | import java.lang.annotation.RetentionPolicy; |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 47 | import java.util.HashMap; |
Chris Wren | 5ab5c74 | 2016-05-10 15:32:23 -0400 | [diff] [blame] | 48 | import java.util.List; |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 49 | import java.util.Map; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 50 | import java.util.Objects; |
| 51 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | /** |
| 53 | * Class to notify the user of events that happen. This is how you tell |
| 54 | * the user that something has happened in the background. {@more} |
| 55 | * |
| 56 | * Notifications can take different forms: |
| 57 | * <ul> |
| 58 | * <li>A persistent icon that goes in the status bar and is accessible |
| 59 | * through the launcher, (when the user selects it, a designated Intent |
| 60 | * can be launched),</li> |
| 61 | * <li>Turning on or flashing LEDs on the device, or</li> |
| 62 | * <li>Alerting the user by flashing the backlight, playing a sound, |
| 63 | * or vibrating.</li> |
| 64 | * </ul> |
| 65 | * |
| 66 | * <p> |
Peter Collingbourne | b97c349 | 2010-10-13 20:04:52 +0100 | [diff] [blame] | 67 | * Each of the notify methods takes an int id parameter and optionally a |
| 68 | * {@link String} tag parameter, which may be {@code null}. These parameters |
| 69 | * are used to form a pair (tag, id), or ({@code null}, id) if tag is |
| 70 | * unspecified. This pair identifies this notification from your app to the |
| 71 | * system, so that pair should be unique within your app. If you call one |
| 72 | * of the notify methods with a (tag, id) pair that is currently active and |
| 73 | * a new set of notification parameters, it will be updated. For example, |
| 74 | * if you pass a new status bar icon, the old icon in the status bar will |
| 75 | * be replaced with the new one. This is also the same tag and id you pass |
| 76 | * to the {@link #cancel(int)} or {@link #cancel(String, int)} method to clear |
| 77 | * this notification. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | * |
| 79 | * <p> |
| 80 | * You do not instantiate this class directly; instead, retrieve it through |
| 81 | * {@link android.content.Context#getSystemService}. |
| 82 | * |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 83 | * <div class="special reference"> |
| 84 | * <h3>Developer Guides</h3> |
| 85 | * <p>For a guide to creating notifications, read the |
| 86 | * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a> |
| 87 | * developer guide.</p> |
| 88 | * </div> |
| 89 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | * @see android.app.Notification |
| 91 | * @see android.content.Context#getSystemService |
| 92 | */ |
| 93 | public class NotificationManager |
| 94 | { |
| 95 | private static String TAG = "NotificationManager"; |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 96 | private static boolean localLOGV = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 98 | /** |
| 99 | * Intent that is broadcast when the state of {@link #getEffectsSuppressor()} changes. |
| 100 | * This broadcast is only sent to registered receivers. |
| 101 | * |
| 102 | * @hide |
| 103 | */ |
| 104 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 105 | public static final String ACTION_EFFECTS_SUPPRESSOR_CHANGED |
| 106 | = "android.os.action.ACTION_EFFECTS_SUPPRESSOR_CHANGED"; |
| 107 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 108 | /** |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 109 | * Intent that is broadcast when the state of {@link #isNotificationPolicyAccessGranted()} |
| 110 | * changes. |
| 111 | * |
| 112 | * This broadcast is only sent to registered receivers, and only to the apps that have changed. |
| 113 | */ |
| 114 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 115 | public static final String ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED |
| 116 | = "android.app.action.NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED"; |
| 117 | |
| 118 | /** |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 119 | * Intent that is broadcast when the state of getNotificationPolicy() changes. |
| 120 | * This broadcast is only sent to registered receivers. |
| 121 | */ |
| 122 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 123 | public static final String ACTION_NOTIFICATION_POLICY_CHANGED |
| 124 | = "android.app.action.NOTIFICATION_POLICY_CHANGED"; |
| 125 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 126 | /** |
| 127 | * Intent that is broadcast when the state of getCurrentInterruptionFilter() changes. |
| 128 | * This broadcast is only sent to registered receivers. |
| 129 | */ |
| 130 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 131 | public static final String ACTION_INTERRUPTION_FILTER_CHANGED |
| 132 | = "android.app.action.INTERRUPTION_FILTER_CHANGED"; |
| 133 | |
| 134 | /** |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 135 | * Intent that is broadcast when the state of getCurrentInterruptionFilter() changes. |
| 136 | * @hide |
| 137 | */ |
| 138 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 139 | public static final String ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL |
| 140 | = "android.app.action.INTERRUPTION_FILTER_CHANGED_INTERNAL"; |
| 141 | |
Julia Reynolds | 0edb50c | 2016-02-26 14:08:25 -0500 | [diff] [blame] | 142 | /** @hide */ |
| 143 | @IntDef({INTERRUPTION_FILTER_NONE, INTERRUPTION_FILTER_PRIORITY, INTERRUPTION_FILTER_ALARMS, |
| 144 | INTERRUPTION_FILTER_ALL, INTERRUPTION_FILTER_UNKNOWN}) |
| 145 | @Retention(RetentionPolicy.SOURCE) |
| 146 | public @interface InterruptionFilter {} |
| 147 | |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 148 | /** |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 149 | * {@link #getCurrentInterruptionFilter() Interruption filter} constant - |
Julia Reynolds | dd0605b | 2016-04-06 10:26:54 -0400 | [diff] [blame] | 150 | * Normal interruption filter - no notifications are suppressed. |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 151 | */ |
| 152 | public static final int INTERRUPTION_FILTER_ALL = 1; |
| 153 | |
| 154 | /** |
| 155 | * {@link #getCurrentInterruptionFilter() Interruption filter} constant - |
Julia Reynolds | dd0605b | 2016-04-06 10:26:54 -0400 | [diff] [blame] | 156 | * Priority interruption filter - all notifications are suppressed except those that match |
| 157 | * the priority criteria. Some audio streams are muted. See |
| 158 | * {@link Policy#priorityCallSenders}, {@link Policy#priorityCategories}, |
| 159 | * {@link Policy#priorityMessageSenders} to define or query this criteria. Users can |
| 160 | * additionally specify packages that can bypass this interruption filter. |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 161 | */ |
| 162 | public static final int INTERRUPTION_FILTER_PRIORITY = 2; |
| 163 | |
| 164 | /** |
| 165 | * {@link #getCurrentInterruptionFilter() Interruption filter} constant - |
Julia Reynolds | dd0605b | 2016-04-06 10:26:54 -0400 | [diff] [blame] | 166 | * No interruptions filter - all notifications are suppressed and all audio streams (except |
| 167 | * those used for phone calls) and vibrations are muted. |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 168 | */ |
| 169 | public static final int INTERRUPTION_FILTER_NONE = 3; |
| 170 | |
| 171 | /** |
| 172 | * {@link #getCurrentInterruptionFilter() Interruption filter} constant - |
Julia Reynolds | dd0605b | 2016-04-06 10:26:54 -0400 | [diff] [blame] | 173 | * Alarms only interruption filter - all notifications except those of category |
| 174 | * {@link Notification#CATEGORY_ALARM} are suppressed. Some audio streams are muted. |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 175 | */ |
| 176 | public static final int INTERRUPTION_FILTER_ALARMS = 4; |
| 177 | |
| 178 | /** {@link #getCurrentInterruptionFilter() Interruption filter} constant - returned when |
| 179 | * the value is unavailable for any reason. |
| 180 | */ |
| 181 | public static final int INTERRUPTION_FILTER_UNKNOWN = 0; |
| 182 | |
Chris Wren | 5ab5c74 | 2016-05-10 15:32:23 -0400 | [diff] [blame] | 183 | /** @hide */ |
| 184 | @IntDef({VISIBILITY_NO_OVERRIDE, IMPORTANCE_UNSPECIFIED, IMPORTANCE_NONE, |
| 185 | IMPORTANCE_MIN, IMPORTANCE_LOW, IMPORTANCE_DEFAULT, IMPORTANCE_HIGH, |
| 186 | IMPORTANCE_MAX}) |
| 187 | @Retention(RetentionPolicy.SOURCE) |
| 188 | public @interface Importance {} |
| 189 | |
| 190 | /** Value signifying that the user has not expressed a per-app visibility override value. |
| 191 | * @hide */ |
| 192 | public static final int VISIBILITY_NO_OVERRIDE = -1000; |
| 193 | /** |
| 194 | * Value signifying that the user has not expressed an importance. |
| 195 | * |
| 196 | * This value is for persisting preferences, and should never be associated with |
| 197 | * an actual notification. |
| 198 | */ |
| 199 | public static final int IMPORTANCE_UNSPECIFIED = -1000; |
| 200 | |
| 201 | /** |
| 202 | * A notification with no importance: shows nowhere, is blocked. |
| 203 | */ |
| 204 | public static final int IMPORTANCE_NONE = 0; |
| 205 | |
| 206 | /** |
| 207 | * Min notification importance: only shows in the shade, below the fold. |
| 208 | */ |
| 209 | public static final int IMPORTANCE_MIN = 1; |
| 210 | |
| 211 | /** |
| 212 | * Low notification importance: shows everywhere, but is not intrusive. |
| 213 | */ |
| 214 | public static final int IMPORTANCE_LOW = 2; |
| 215 | |
| 216 | /** |
| 217 | * Default notification importance: shows everywhere, allowed to makes noise, |
| 218 | * but does not visually intrude. |
| 219 | */ |
| 220 | public static final int IMPORTANCE_DEFAULT = 3; |
| 221 | |
| 222 | /** |
| 223 | * Higher notification importance: shows everywhere, allowed to makes noise and peek. |
| 224 | */ |
| 225 | public static final int IMPORTANCE_HIGH = 4; |
| 226 | |
| 227 | /** |
| 228 | * Highest notification importance: shows everywhere, allowed to makes noise, peek, and |
| 229 | * use full screen intents. |
| 230 | */ |
| 231 | public static final int IMPORTANCE_MAX = 5; |
| 232 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | private static INotificationManager sService; |
| 234 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 235 | /** @hide */ |
| 236 | static public INotificationManager getService() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | { |
| 238 | if (sService != null) { |
| 239 | return sService; |
| 240 | } |
| 241 | IBinder b = ServiceManager.getService("notification"); |
| 242 | sService = INotificationManager.Stub.asInterface(b); |
| 243 | return sService; |
| 244 | } |
| 245 | |
| 246 | /*package*/ NotificationManager(Context context, Handler handler) |
| 247 | { |
| 248 | mContext = context; |
| 249 | } |
| 250 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 251 | /** {@hide} */ |
| 252 | public static NotificationManager from(Context context) { |
| 253 | return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
| 254 | } |
| 255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | /** |
Daniel Sandler | e97a3bc | 2011-02-07 16:47:07 -0500 | [diff] [blame] | 257 | * Post a notification to be shown in the status bar. If a notification with |
| 258 | * the same id has already been posted by your application and has not yet been canceled, it |
| 259 | * will be replaced by the updated information. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | * |
| 261 | * @param id An identifier for this notification unique within your |
| 262 | * application. |
Daniel Sandler | e97a3bc | 2011-02-07 16:47:07 -0500 | [diff] [blame] | 263 | * @param notification A {@link Notification} object describing what to show the user. Must not |
| 264 | * be null. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | */ |
| 266 | public void notify(int id, Notification notification) |
| 267 | { |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 268 | notify(null, id, notification); |
| 269 | } |
| 270 | |
| 271 | /** |
Daniel Sandler | e97a3bc | 2011-02-07 16:47:07 -0500 | [diff] [blame] | 272 | * Post a notification to be shown in the status bar. If a notification with |
| 273 | * the same tag and id has already been posted by your application and has not yet been |
| 274 | * canceled, it will be replaced by the updated information. |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 275 | * |
Peter Collingbourne | b97c349 | 2010-10-13 20:04:52 +0100 | [diff] [blame] | 276 | * @param tag A string identifier for this notification. May be {@code null}. |
| 277 | * @param id An identifier for this notification. The pair (tag, id) must be unique |
| 278 | * within your application. |
Daniel Sandler | e97a3bc | 2011-02-07 16:47:07 -0500 | [diff] [blame] | 279 | * @param notification A {@link Notification} object describing what to |
| 280 | * show the user. Must not be null. |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 281 | */ |
| 282 | public void notify(String tag, int id, Notification notification) |
| 283 | { |
Julia Reynolds | d9228f1 | 2015-10-20 10:37:27 -0400 | [diff] [blame] | 284 | notifyAsUser(tag, id, notification, new UserHandle(UserHandle.myUserId())); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /** |
| 288 | * @hide |
| 289 | */ |
| 290 | public void notifyAsUser(String tag, int id, Notification notification, UserHandle user) |
| 291 | { |
| 292 | int[] idOut = new int[1]; |
| 293 | INotificationManager service = getService(); |
| 294 | String pkg = mContext.getPackageName(); |
Julia Reynolds | da30354 | 2015-11-23 14:00:20 -0500 | [diff] [blame] | 295 | // Fix the notification as best we can. |
| 296 | Notification.addFieldsFromContext(mContext, notification); |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 297 | if (notification.sound != null) { |
| 298 | notification.sound = notification.sound.getCanonicalUri(); |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 299 | if (StrictMode.vmFileUriExposureEnabled()) { |
Jeff Sharkey | ac3be9a | 2016-02-01 10:39:30 -0700 | [diff] [blame] | 300 | notification.sound.checkFileUriExposed("Notification.sound"); |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 301 | } |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 302 | } |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 303 | fixLegacySmallIcon(notification, pkg); |
Julia Reynolds | d9228f1 | 2015-10-20 10:37:27 -0400 | [diff] [blame] | 304 | if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) { |
| 305 | if (notification.getSmallIcon() == null) { |
| 306 | throw new IllegalArgumentException("Invalid notification (no valid small icon): " |
| 307 | + notification); |
| 308 | } |
| 309 | } |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 310 | if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")"); |
Adrian Roos | 184bfe02 | 2016-03-03 13:41:44 -0800 | [diff] [blame] | 311 | final Notification copy = Builder.maybeCloneStrippedForDelivery(notification); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 312 | try { |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 313 | service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id, |
Julia Reynolds | d9228f1 | 2015-10-20 10:37:27 -0400 | [diff] [blame] | 314 | copy, idOut, user.getIdentifier()); |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 315 | if (localLOGV && id != idOut[0]) { |
| 316 | Log.v(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | } |
| 318 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 319 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 323 | private void fixLegacySmallIcon(Notification n, String pkg) { |
| 324 | if (n.getSmallIcon() == null && n.icon != 0) { |
| 325 | n.setSmallIcon(Icon.createWithResource(pkg, n.icon)); |
| 326 | } |
| 327 | } |
| 328 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | /** |
| 330 | * Cancel a previously shown notification. If it's transient, the view |
| 331 | * will be hidden. If it's persistent, it will be removed from the status |
| 332 | * bar. |
| 333 | */ |
| 334 | public void cancel(int id) |
| 335 | { |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 336 | cancel(null, id); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Cancel a previously shown notification. If it's transient, the view |
| 341 | * will be hidden. If it's persistent, it will be removed from the status |
| 342 | * bar. |
| 343 | */ |
| 344 | public void cancel(String tag, int id) |
| 345 | { |
Julia Reynolds | d9228f1 | 2015-10-20 10:37:27 -0400 | [diff] [blame] | 346 | cancelAsUser(tag, id, new UserHandle(UserHandle.myUserId())); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /** |
| 350 | * @hide |
| 351 | */ |
| 352 | public void cancelAsUser(String tag, int id, UserHandle user) |
| 353 | { |
| 354 | INotificationManager service = getService(); |
| 355 | String pkg = mContext.getPackageName(); |
| 356 | if (localLOGV) Log.v(TAG, pkg + ": cancel(" + id + ")"); |
| 357 | try { |
| 358 | service.cancelNotificationWithTag(pkg, tag, id, user.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 360 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Cancel all previously shown notifications. See {@link #cancel} for the |
| 366 | * detailed behavior. |
| 367 | */ |
| 368 | public void cancelAll() |
| 369 | { |
| 370 | INotificationManager service = getService(); |
| 371 | String pkg = mContext.getPackageName(); |
| 372 | if (localLOGV) Log.v(TAG, pkg + ": cancelAll()"); |
| 373 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 374 | service.cancelAllNotifications(pkg, UserHandle.myUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 376 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 380 | /** |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 381 | * Creates a notification channel that notifications can be posted to. |
| 382 | */ |
| 383 | public void createNotificationChannel(NotificationChannel channel) { |
| 384 | INotificationManager service = getService(); |
| 385 | try { |
| 386 | service.createNotificationChannel(mContext.getPackageName(), channel); |
| 387 | } catch (RemoteException e) { |
| 388 | throw e.rethrowFromSystemServer(); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Returns the notification channel settings for a given channel id. |
| 394 | */ |
| 395 | public NotificationChannel getNotificationChannel(String channelId) { |
| 396 | INotificationManager service = getService(); |
| 397 | try { |
| 398 | return service.getNotificationChannel(mContext.getPackageName(), channelId); |
| 399 | } catch (RemoteException e) { |
| 400 | throw e.rethrowFromSystemServer(); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Returns all notification channels created by the calling app. |
| 406 | */ |
| 407 | public List<NotificationChannel> getNotificationChannels() { |
| 408 | INotificationManager service = getService(); |
| 409 | try { |
| 410 | return service.getNotificationChannels(mContext.getPackageName()).getList(); |
| 411 | } catch (RemoteException e) { |
| 412 | throw e.rethrowFromSystemServer(); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Updates settings for a given channel. |
| 418 | */ |
| 419 | public void updateNotificationChannel(NotificationChannel channel) { |
| 420 | INotificationManager service = getService(); |
| 421 | try { |
| 422 | service.updateNotificationChannel(mContext.getPackageName(), channel); |
| 423 | } catch (RemoteException e) { |
| 424 | throw e.rethrowFromSystemServer(); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Deletes the given notification channel. |
| 430 | */ |
| 431 | public void deleteNotificationChannel(String channelId) { |
| 432 | INotificationManager service = getService(); |
| 433 | try { |
| 434 | service.deleteNotificationChannel(mContext.getPackageName(), channelId); |
| 435 | } catch (RemoteException e) { |
| 436 | throw e.rethrowFromSystemServer(); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /** |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 441 | * @hide |
| 442 | */ |
| 443 | public ComponentName getEffectsSuppressor() { |
| 444 | INotificationManager service = getService(); |
| 445 | try { |
| 446 | return service.getEffectsSuppressor(); |
| 447 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 448 | throw e.rethrowFromSystemServer(); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 452 | /** |
| 453 | * @hide |
| 454 | */ |
| 455 | public boolean matchesCallFilter(Bundle extras) { |
| 456 | INotificationManager service = getService(); |
| 457 | try { |
| 458 | return service.matchesCallFilter(extras); |
| 459 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 460 | throw e.rethrowFromSystemServer(); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 464 | /** |
| 465 | * @hide |
| 466 | */ |
| 467 | public boolean isSystemConditionProviderEnabled(String path) { |
| 468 | INotificationManager service = getService(); |
| 469 | try { |
| 470 | return service.isSystemConditionProviderEnabled(path); |
| 471 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 472 | throw e.rethrowFromSystemServer(); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 476 | /** |
| 477 | * @hide |
| 478 | */ |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 479 | public void setZenMode(int mode, Uri conditionId, String reason) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 480 | INotificationManager service = getService(); |
| 481 | try { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 482 | service.setZenMode(mode, conditionId, reason); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 483 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 484 | throw e.rethrowFromSystemServer(); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * @hide |
| 490 | */ |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 491 | public int getZenMode() { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 492 | INotificationManager service = getService(); |
| 493 | try { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 494 | return service.getZenMode(); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 495 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 496 | throw e.rethrowFromSystemServer(); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * @hide |
| 502 | */ |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 503 | public ZenModeConfig getZenModeConfig() { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 504 | INotificationManager service = getService(); |
| 505 | try { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 506 | return service.getZenModeConfig(); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 507 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 508 | throw e.rethrowFromSystemServer(); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 509 | } |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 510 | } |
| 511 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 512 | /** |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 513 | * @hide |
| 514 | */ |
| 515 | public int getRuleInstanceCount(ComponentName owner) { |
| 516 | INotificationManager service = getService(); |
| 517 | try { |
| 518 | return service.getRuleInstanceCount(owner); |
| 519 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 520 | throw e.rethrowFromSystemServer(); |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 521 | } |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /** |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 525 | * Returns AutomaticZenRules owned by the caller. |
| 526 | * |
| 527 | * <p> |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 528 | * Throws a SecurityException if policy access is granted to this package. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 529 | * See {@link #isNotificationPolicyAccessGranted}. |
| 530 | */ |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 531 | public Map<String, AutomaticZenRule> getAutomaticZenRules() { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 532 | INotificationManager service = getService(); |
| 533 | try { |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 534 | List<ZenModeConfig.ZenRule> rules = service.getZenRules(); |
| 535 | Map<String, AutomaticZenRule> ruleMap = new HashMap<>(); |
| 536 | for (ZenModeConfig.ZenRule rule : rules) { |
| 537 | ruleMap.put(rule.id, new AutomaticZenRule(rule.name, rule.component, |
| 538 | rule.conditionId, zenModeToInterruptionFilter(rule.zenMode), rule.enabled, |
| 539 | rule.creationTime)); |
| 540 | } |
| 541 | return ruleMap; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 542 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 543 | throw e.rethrowFromSystemServer(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 544 | } |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /** |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 548 | * 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] | 549 | * |
| 550 | * <p> |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 551 | * Throws a SecurityException if policy access is granted to this package. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 552 | * See {@link #isNotificationPolicyAccessGranted}. |
| 553 | * |
| 554 | * <p> |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 555 | * 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] | 556 | * doesn't own the matching rule. See {@link AutomaticZenRule#getOwner}. |
| 557 | */ |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 558 | public AutomaticZenRule getAutomaticZenRule(String id) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 559 | INotificationManager service = getService(); |
| 560 | try { |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 561 | return service.getAutomaticZenRule(id); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 562 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 563 | throw e.rethrowFromSystemServer(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 564 | } |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | /** |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 568 | * Creates the given zen rule. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 569 | * |
| 570 | * <p> |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 571 | * Throws a SecurityException if policy access is granted to this package. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 572 | * See {@link #isNotificationPolicyAccessGranted}. |
| 573 | * |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 574 | * @param automaticZenRule the rule to create. |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 575 | * @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] | 576 | */ |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 577 | public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 578 | INotificationManager service = getService(); |
| 579 | try { |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 580 | return service.addAutomaticZenRule(automaticZenRule); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 581 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 582 | throw e.rethrowFromSystemServer(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 583 | } |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /** |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 587 | * Updates the given zen rule. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 588 | * |
| 589 | * <p> |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 590 | * Throws a SecurityException if policy access is granted to this package. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 591 | * See {@link #isNotificationPolicyAccessGranted}. |
| 592 | * |
| 593 | * <p> |
| 594 | * Callers can only update rules that they own. See {@link AutomaticZenRule#getOwner}. |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 595 | * @param id The id of the rule to update |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 596 | * @param automaticZenRule the rule to update. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 597 | * @return Whether the rule was successfully updated. |
| 598 | */ |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 599 | public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 600 | INotificationManager service = getService(); |
| 601 | try { |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 602 | return service.updateAutomaticZenRule(id, automaticZenRule); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 603 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 604 | throw e.rethrowFromSystemServer(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 605 | } |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /** |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 609 | * Deletes the automatic zen rule with the given id. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 610 | * |
| 611 | * <p> |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 612 | * Throws a SecurityException if policy access is granted to this package. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 613 | * See {@link #isNotificationPolicyAccessGranted}. |
| 614 | * |
| 615 | * <p> |
| 616 | * Callers can only delete rules that they own. See {@link AutomaticZenRule#getOwner}. |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 617 | * @param id the id of the rule to delete. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 618 | * @return Whether the rule was successfully deleted. |
| 619 | */ |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 620 | public boolean removeAutomaticZenRule(String id) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 621 | INotificationManager service = getService(); |
| 622 | try { |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 623 | return service.removeAutomaticZenRule(id); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 624 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 625 | throw e.rethrowFromSystemServer(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 626 | } |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /** |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 630 | * Deletes all automatic zen rules owned by the given package. |
| 631 | * |
| 632 | * @hide |
| 633 | */ |
| 634 | public boolean removeAutomaticZenRules(String packageName) { |
| 635 | INotificationManager service = getService(); |
| 636 | try { |
| 637 | return service.removeAutomaticZenRules(packageName); |
| 638 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 639 | throw e.rethrowFromSystemServer(); |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 640 | } |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 641 | } |
| 642 | |
Julia Reynolds | 0edb50c | 2016-02-26 14:08:25 -0500 | [diff] [blame] | 643 | /** |
| 644 | * Returns the user specified importance for notifications from the calling package. |
Chris Wren | 5ab5c74 | 2016-05-10 15:32:23 -0400 | [diff] [blame] | 645 | * |
| 646 | * @return An importance level, such as {@link #IMPORTANCE_DEFAULT}. |
Julia Reynolds | 0edb50c | 2016-02-26 14:08:25 -0500 | [diff] [blame] | 647 | */ |
Chris Wren | 5ab5c74 | 2016-05-10 15:32:23 -0400 | [diff] [blame] | 648 | public @Importance int getImportance() { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 649 | INotificationManager service = getService(); |
| 650 | try { |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 651 | return service.getPackageImportance(mContext.getPackageName()); |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 652 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 653 | throw e.rethrowFromSystemServer(); |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 654 | } |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 655 | } |
| 656 | |
Julia Reynolds | 0edb50c | 2016-02-26 14:08:25 -0500 | [diff] [blame] | 657 | /** |
| 658 | * Returns whether notifications from the calling package are blocked. |
| 659 | */ |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 660 | public boolean areNotificationsEnabled() { |
| 661 | INotificationManager service = getService(); |
| 662 | try { |
| 663 | return service.areNotificationsEnabled(mContext.getPackageName()); |
| 664 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 665 | throw e.rethrowFromSystemServer(); |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 666 | } |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 667 | } |
| 668 | |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 669 | /** |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 670 | * Checks the ability to read/modify notification policy for the calling package. |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 671 | * |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 672 | * <p> |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 673 | * Returns true if the calling package can read/modify notification policy. |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 674 | * |
| 675 | * <p> |
| 676 | * Request policy access by sending the user to the activity that matches the system intent |
| 677 | * action {@link android.provider.Settings#ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS}. |
| 678 | * |
| 679 | * <p> |
| 680 | * Use {@link #ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED} to listen for |
| 681 | * user grant or denial of this access. |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 682 | */ |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 683 | public boolean isNotificationPolicyAccessGranted() { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 684 | INotificationManager service = getService(); |
| 685 | try { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 686 | return service.isNotificationPolicyAccessGranted(mContext.getOpPackageName()); |
| 687 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 688 | throw e.rethrowFromSystemServer(); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 689 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | /** @hide */ |
| 693 | public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) { |
| 694 | INotificationManager service = getService(); |
| 695 | try { |
| 696 | return service.isNotificationPolicyAccessGrantedForPackage(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 697 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 698 | throw e.rethrowFromSystemServer(); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 699 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /** |
| 703 | * Gets the current notification policy. |
| 704 | * |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 705 | * <p> |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 706 | * Only available if policy access is granted to this package. |
| 707 | * See {@link #isNotificationPolicyAccessGranted}. |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 708 | */ |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 709 | public Policy getNotificationPolicy() { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 710 | INotificationManager service = getService(); |
| 711 | try { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 712 | return service.getNotificationPolicy(mContext.getOpPackageName()); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 713 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 714 | throw e.rethrowFromSystemServer(); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 715 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Sets the current notification policy. |
| 720 | * |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 721 | * <p> |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 722 | * Only available if policy access is granted to this package. |
| 723 | * See {@link #isNotificationPolicyAccessGranted}. |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 724 | * |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 725 | * @param policy The new desired policy. |
| 726 | */ |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 727 | public void setNotificationPolicy(@NonNull Policy policy) { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 728 | checkRequired("policy", policy); |
| 729 | INotificationManager service = getService(); |
| 730 | try { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 731 | service.setNotificationPolicy(mContext.getOpPackageName(), policy); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 732 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 733 | throw e.rethrowFromSystemServer(); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 737 | /** @hide */ |
| 738 | public void setNotificationPolicyAccessGranted(String pkg, boolean granted) { |
| 739 | INotificationManager service = getService(); |
| 740 | try { |
| 741 | service.setNotificationPolicyAccessGranted(pkg, granted); |
| 742 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 743 | throw e.rethrowFromSystemServer(); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
| 747 | /** @hide */ |
| 748 | public ArraySet<String> getPackagesRequestingNotificationPolicyAccess() { |
| 749 | INotificationManager service = getService(); |
| 750 | try { |
| 751 | final String[] pkgs = service.getPackagesRequestingNotificationPolicyAccess(); |
| 752 | if (pkgs != null && pkgs.length > 0) { |
| 753 | final ArraySet<String> rt = new ArraySet<>(pkgs.length); |
| 754 | for (int i = 0; i < pkgs.length; i++) { |
| 755 | rt.add(pkgs[i]); |
| 756 | } |
| 757 | return rt; |
| 758 | } |
| 759 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 760 | throw e.rethrowFromSystemServer(); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 761 | } |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 762 | return new ArraySet<>(); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 763 | } |
| 764 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | private Context mContext; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 766 | |
| 767 | private static void checkRequired(String name, Object value) { |
| 768 | if (value == null) { |
| 769 | throw new IllegalArgumentException(name + " is required"); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * Notification policy configuration. Represents user-preferences for notification |
Julia Reynolds | cedacef | 2016-03-04 08:18:47 -0500 | [diff] [blame] | 775 | * filtering. |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 776 | */ |
| 777 | public static class Policy implements android.os.Parcelable { |
| 778 | /** Reminder notifications are prioritized. */ |
| 779 | public static final int PRIORITY_CATEGORY_REMINDERS = 1 << 0; |
| 780 | /** Event notifications are prioritized. */ |
| 781 | public static final int PRIORITY_CATEGORY_EVENTS = 1 << 1; |
| 782 | /** Message notifications are prioritized. */ |
| 783 | public static final int PRIORITY_CATEGORY_MESSAGES = 1 << 2; |
| 784 | /** Calls are prioritized. */ |
| 785 | public static final int PRIORITY_CATEGORY_CALLS = 1 << 3; |
| 786 | /** Calls from repeat callers are prioritized. */ |
| 787 | public static final int PRIORITY_CATEGORY_REPEAT_CALLERS = 1 << 4; |
| 788 | |
| 789 | private static final int[] ALL_PRIORITY_CATEGORIES = { |
| 790 | PRIORITY_CATEGORY_REMINDERS, |
| 791 | PRIORITY_CATEGORY_EVENTS, |
| 792 | PRIORITY_CATEGORY_MESSAGES, |
| 793 | PRIORITY_CATEGORY_CALLS, |
| 794 | PRIORITY_CATEGORY_REPEAT_CALLERS, |
| 795 | }; |
| 796 | |
| 797 | /** Any sender is prioritized. */ |
| 798 | public static final int PRIORITY_SENDERS_ANY = 0; |
| 799 | /** Saved contacts are prioritized. */ |
| 800 | public static final int PRIORITY_SENDERS_CONTACTS = 1; |
| 801 | /** Only starred contacts are prioritized. */ |
| 802 | public static final int PRIORITY_SENDERS_STARRED = 2; |
| 803 | |
| 804 | /** Notification categories to prioritize. Bitmask of PRIORITY_CATEGORY_* constants. */ |
| 805 | public final int priorityCategories; |
| 806 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 807 | /** Notification senders to prioritize for calls. One of: |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 808 | * PRIORITY_SENDERS_ANY, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED */ |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 809 | public final int priorityCallSenders; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 810 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 811 | /** Notification senders to prioritize for messages. One of: |
| 812 | * PRIORITY_SENDERS_ANY, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED */ |
| 813 | public final int priorityMessageSenders; |
| 814 | |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 815 | /** |
| 816 | * @hide |
| 817 | */ |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 818 | public static final int SUPPRESSED_EFFECTS_UNSET = -1; |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 819 | /** |
Julia Reynolds | cedacef | 2016-03-04 08:18:47 -0500 | [diff] [blame] | 820 | * Whether notifications suppressed by DND should not interrupt visually (e.g. with |
| 821 | * notification lights or by turning the screen on) when the screen is off. |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 822 | */ |
| 823 | public static final int SUPPRESSED_EFFECT_SCREEN_OFF = 1 << 0; |
| 824 | /** |
Julia Reynolds | cedacef | 2016-03-04 08:18:47 -0500 | [diff] [blame] | 825 | * Whether notifications suppressed by DND should not interrupt visually when the screen |
| 826 | * is on (e.g. by peeking onto the screen). |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 827 | */ |
| 828 | public static final int SUPPRESSED_EFFECT_SCREEN_ON = 1 << 1; |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 829 | |
| 830 | private static final int[] ALL_SUPPRESSED_EFFECTS = { |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 831 | SUPPRESSED_EFFECT_SCREEN_OFF, |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 832 | SUPPRESSED_EFFECT_SCREEN_ON, |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 833 | }; |
| 834 | |
| 835 | /** |
| 836 | * Visual effects to suppress for a notification that is filtered by Do Not Disturb mode. |
| 837 | * Bitmask of SUPPRESSED_EFFECT_* constants. |
| 838 | */ |
| 839 | public final int suppressedVisualEffects; |
| 840 | |
Julia Reynolds | cedacef | 2016-03-04 08:18:47 -0500 | [diff] [blame] | 841 | /** |
| 842 | * Constructs a policy for Do Not Disturb priority mode behavior. |
| 843 | * |
| 844 | * @param priorityCategories bitmask of categories of notifications that can bypass DND. |
| 845 | * @param priorityCallSenders which callers can bypass DND. |
| 846 | * @param priorityMessageSenders which message senders can bypass DND. |
| 847 | */ |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 848 | public Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders) { |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 849 | this(priorityCategories, priorityCallSenders, priorityMessageSenders, |
| 850 | SUPPRESSED_EFFECTS_UNSET); |
| 851 | } |
| 852 | |
Julia Reynolds | cedacef | 2016-03-04 08:18:47 -0500 | [diff] [blame] | 853 | /** |
| 854 | * Constructs a policy for Do Not Disturb priority mode behavior. |
| 855 | * |
| 856 | * @param priorityCategories bitmask of categories of notifications that can bypass DND. |
| 857 | * @param priorityCallSenders which callers can bypass DND. |
| 858 | * @param priorityMessageSenders which message senders can bypass DND. |
| 859 | * @param suppressedVisualEffects which visual interruptions should be suppressed from |
| 860 | * notifications that are filtered by DND. |
| 861 | */ |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 862 | public Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders, |
| 863 | int suppressedVisualEffects) { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 864 | this.priorityCategories = priorityCategories; |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 865 | this.priorityCallSenders = priorityCallSenders; |
| 866 | this.priorityMessageSenders = priorityMessageSenders; |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 867 | this.suppressedVisualEffects = suppressedVisualEffects; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | /** @hide */ |
| 871 | public Policy(Parcel source) { |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 872 | this(source.readInt(), source.readInt(), source.readInt(), source.readInt()); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | @Override |
| 876 | public void writeToParcel(Parcel dest, int flags) { |
| 877 | dest.writeInt(priorityCategories); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 878 | dest.writeInt(priorityCallSenders); |
| 879 | dest.writeInt(priorityMessageSenders); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 880 | dest.writeInt(suppressedVisualEffects); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | @Override |
| 884 | public int describeContents() { |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | @Override |
| 889 | public int hashCode() { |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 890 | return Objects.hash(priorityCategories, priorityCallSenders, priorityMessageSenders, |
| 891 | suppressedVisualEffects); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | @Override |
| 895 | public boolean equals(Object o) { |
| 896 | if (!(o instanceof Policy)) return false; |
| 897 | if (o == this) return true; |
| 898 | final Policy other = (Policy) o; |
| 899 | return other.priorityCategories == priorityCategories |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 900 | && other.priorityCallSenders == priorityCallSenders |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 901 | && other.priorityMessageSenders == priorityMessageSenders |
| 902 | && other.suppressedVisualEffects == suppressedVisualEffects; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | @Override |
| 906 | public String toString() { |
| 907 | return "NotificationManager.Policy[" |
| 908 | + "priorityCategories=" + priorityCategoriesToString(priorityCategories) |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 909 | + ",priorityCallSenders=" + prioritySendersToString(priorityCallSenders) |
| 910 | + ",priorityMessageSenders=" + prioritySendersToString(priorityMessageSenders) |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 911 | + ",suppressedVisualEffects=" |
| 912 | + suppressedEffectsToString(suppressedVisualEffects) |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 913 | + "]"; |
| 914 | } |
| 915 | |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 916 | public static String suppressedEffectsToString(int effects) { |
| 917 | if (effects <= 0) return ""; |
| 918 | final StringBuilder sb = new StringBuilder(); |
| 919 | for (int i = 0; i < ALL_SUPPRESSED_EFFECTS.length; i++) { |
| 920 | final int effect = ALL_SUPPRESSED_EFFECTS[i]; |
| 921 | if ((effects & effect) != 0) { |
| 922 | if (sb.length() > 0) sb.append(','); |
| 923 | sb.append(effectToString(effect)); |
| 924 | } |
| 925 | effects &= ~effect; |
| 926 | } |
| 927 | if (effects != 0) { |
| 928 | if (sb.length() > 0) sb.append(','); |
| 929 | sb.append("UNKNOWN_").append(effects); |
| 930 | } |
| 931 | return sb.toString(); |
| 932 | } |
| 933 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 934 | public static String priorityCategoriesToString(int priorityCategories) { |
| 935 | if (priorityCategories == 0) return ""; |
| 936 | final StringBuilder sb = new StringBuilder(); |
| 937 | for (int i = 0; i < ALL_PRIORITY_CATEGORIES.length; i++) { |
| 938 | final int priorityCategory = ALL_PRIORITY_CATEGORIES[i]; |
| 939 | if ((priorityCategories & priorityCategory) != 0) { |
| 940 | if (sb.length() > 0) sb.append(','); |
| 941 | sb.append(priorityCategoryToString(priorityCategory)); |
| 942 | } |
| 943 | priorityCategories &= ~priorityCategory; |
| 944 | } |
| 945 | if (priorityCategories != 0) { |
| 946 | if (sb.length() > 0) sb.append(','); |
| 947 | sb.append("PRIORITY_CATEGORY_UNKNOWN_").append(priorityCategories); |
| 948 | } |
| 949 | return sb.toString(); |
| 950 | } |
| 951 | |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 952 | private static String effectToString(int effect) { |
| 953 | switch (effect) { |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 954 | case SUPPRESSED_EFFECT_SCREEN_OFF: return "SUPPRESSED_EFFECT_SCREEN_OFF"; |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 955 | case SUPPRESSED_EFFECT_SCREEN_ON: return "SUPPRESSED_EFFECT_SCREEN_ON"; |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 956 | case SUPPRESSED_EFFECTS_UNSET: return "SUPPRESSED_EFFECTS_UNSET"; |
| 957 | default: return "UNKNOWN_" + effect; |
| 958 | } |
| 959 | } |
| 960 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 961 | private static String priorityCategoryToString(int priorityCategory) { |
| 962 | switch (priorityCategory) { |
| 963 | case PRIORITY_CATEGORY_REMINDERS: return "PRIORITY_CATEGORY_REMINDERS"; |
| 964 | case PRIORITY_CATEGORY_EVENTS: return "PRIORITY_CATEGORY_EVENTS"; |
| 965 | case PRIORITY_CATEGORY_MESSAGES: return "PRIORITY_CATEGORY_MESSAGES"; |
| 966 | case PRIORITY_CATEGORY_CALLS: return "PRIORITY_CATEGORY_CALLS"; |
| 967 | case PRIORITY_CATEGORY_REPEAT_CALLERS: return "PRIORITY_CATEGORY_REPEAT_CALLERS"; |
| 968 | default: return "PRIORITY_CATEGORY_UNKNOWN_" + priorityCategory; |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | public static String prioritySendersToString(int prioritySenders) { |
| 973 | switch (prioritySenders) { |
| 974 | case PRIORITY_SENDERS_ANY: return "PRIORITY_SENDERS_ANY"; |
| 975 | case PRIORITY_SENDERS_CONTACTS: return "PRIORITY_SENDERS_CONTACTS"; |
| 976 | case PRIORITY_SENDERS_STARRED: return "PRIORITY_SENDERS_STARRED"; |
| 977 | default: return "PRIORITY_SENDERS_UNKNOWN_" + prioritySenders; |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | public static final Parcelable.Creator<Policy> CREATOR = new Parcelable.Creator<Policy>() { |
| 982 | @Override |
| 983 | public Policy createFromParcel(Parcel in) { |
| 984 | return new Policy(in); |
| 985 | } |
| 986 | |
| 987 | @Override |
| 988 | public Policy[] newArray(int size) { |
| 989 | return new Policy[size]; |
| 990 | } |
| 991 | }; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 992 | } |
| 993 | |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 994 | /** |
| 995 | * Recover a list of active notifications: ones that have been posted by the calling app that |
| 996 | * have not yet been dismissed by the user or {@link #cancel(String, int)}ed by the app. |
| 997 | * |
| 998 | * Each notification is embedded in a {@link StatusBarNotification} object, including the |
| 999 | * original <code>tag</code> and <code>id</code> supplied to |
| 1000 | * {@link #notify(String, int, Notification) notify()} |
| 1001 | * (via {@link StatusBarNotification#getTag() getTag()} and |
| 1002 | * {@link StatusBarNotification#getId() getId()}) as well as a copy of the original |
| 1003 | * {@link Notification} object (via {@link StatusBarNotification#getNotification()}). |
| 1004 | * |
| 1005 | * @return An array of {@link StatusBarNotification}. |
| 1006 | */ |
| 1007 | public StatusBarNotification[] getActiveNotifications() { |
| 1008 | final INotificationManager service = getService(); |
| 1009 | final String pkg = mContext.getPackageName(); |
| 1010 | try { |
| 1011 | final ParceledListSlice<StatusBarNotification> parceledList |
| 1012 | = service.getAppActiveNotifications(pkg, UserHandle.myUserId()); |
| 1013 | final List<StatusBarNotification> list = parceledList.getList(); |
| 1014 | return list.toArray(new StatusBarNotification[list.size()]); |
| 1015 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1016 | throw e.rethrowFromSystemServer(); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1017 | } |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1018 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1019 | |
| 1020 | /** |
| 1021 | * Gets the current notification interruption filter. |
| 1022 | * |
| 1023 | * <p> |
| 1024 | * The interruption filter defines which notifications are allowed to interrupt the user |
| 1025 | * (e.g. via sound & vibration) and is applied globally. |
| 1026 | * @return One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when |
| 1027 | * unavailable. |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1028 | */ |
Julia Reynolds | 0edb50c | 2016-02-26 14:08:25 -0500 | [diff] [blame] | 1029 | public final @InterruptionFilter int getCurrentInterruptionFilter() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1030 | final INotificationManager service = getService(); |
| 1031 | try { |
| 1032 | return zenModeToInterruptionFilter(service.getZenMode()); |
| 1033 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1034 | throw e.rethrowFromSystemServer(); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1035 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * Sets the current notification interruption filter. |
| 1040 | * |
| 1041 | * <p> |
| 1042 | * The interruption filter defines which notifications are allowed to interrupt the user |
| 1043 | * (e.g. via sound & vibration) and is applied globally. |
| 1044 | * @return One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when |
| 1045 | * unavailable. |
| 1046 | * |
| 1047 | * <p> |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 1048 | * Only available if policy access is granted to this package. |
| 1049 | * See {@link #isNotificationPolicyAccessGranted}. |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1050 | */ |
| 1051 | public final void setInterruptionFilter(int interruptionFilter) { |
| 1052 | final INotificationManager service = getService(); |
| 1053 | try { |
| 1054 | service.setInterruptionFilter(mContext.getOpPackageName(), interruptionFilter); |
| 1055 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1056 | throw e.rethrowFromSystemServer(); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | /** @hide */ |
| 1061 | public static int zenModeToInterruptionFilter(int zen) { |
| 1062 | switch (zen) { |
| 1063 | case Global.ZEN_MODE_OFF: return INTERRUPTION_FILTER_ALL; |
| 1064 | case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return INTERRUPTION_FILTER_PRIORITY; |
| 1065 | case Global.ZEN_MODE_ALARMS: return INTERRUPTION_FILTER_ALARMS; |
| 1066 | case Global.ZEN_MODE_NO_INTERRUPTIONS: return INTERRUPTION_FILTER_NONE; |
| 1067 | default: return INTERRUPTION_FILTER_UNKNOWN; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | /** @hide */ |
| 1072 | public static int zenModeFromInterruptionFilter(int interruptionFilter, int defValue) { |
| 1073 | switch (interruptionFilter) { |
| 1074 | case INTERRUPTION_FILTER_ALL: return Global.ZEN_MODE_OFF; |
| 1075 | case INTERRUPTION_FILTER_PRIORITY: return Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 1076 | case INTERRUPTION_FILTER_ALARMS: return Global.ZEN_MODE_ALARMS; |
| 1077 | case INTERRUPTION_FILTER_NONE: return Global.ZEN_MODE_NO_INTERRUPTIONS; |
| 1078 | default: return defValue; |
| 1079 | } |
| 1080 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | } |