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 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
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 | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 23 | import android.app.NotificationManager.Policy.Token; |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 24 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 26 | import android.content.pm.ParceledListSlice; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 27 | import android.net.Uri; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 28 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.os.Handler; |
| 30 | import android.os.IBinder; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 31 | import android.os.Parcel; |
| 32 | import android.os.Parcelable; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 33 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.os.ServiceManager; |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 35 | import android.os.StrictMode; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 36 | import android.os.UserHandle; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 37 | import android.provider.Settings.Global; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 38 | import android.service.notification.IConditionListener; |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 39 | import android.service.notification.StatusBarNotification; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 40 | import android.service.notification.ZenModeConfig; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.util.Log; |
| 42 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 43 | import java.util.Objects; |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 44 | import java.util.List; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 45 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | /** |
| 47 | * Class to notify the user of events that happen. This is how you tell |
| 48 | * the user that something has happened in the background. {@more} |
| 49 | * |
| 50 | * Notifications can take different forms: |
| 51 | * <ul> |
| 52 | * <li>A persistent icon that goes in the status bar and is accessible |
| 53 | * through the launcher, (when the user selects it, a designated Intent |
| 54 | * can be launched),</li> |
| 55 | * <li>Turning on or flashing LEDs on the device, or</li> |
| 56 | * <li>Alerting the user by flashing the backlight, playing a sound, |
| 57 | * or vibrating.</li> |
| 58 | * </ul> |
| 59 | * |
| 60 | * <p> |
Peter Collingbourne | b97c349 | 2010-10-13 20:04:52 +0100 | [diff] [blame] | 61 | * Each of the notify methods takes an int id parameter and optionally a |
| 62 | * {@link String} tag parameter, which may be {@code null}. These parameters |
| 63 | * are used to form a pair (tag, id), or ({@code null}, id) if tag is |
| 64 | * unspecified. This pair identifies this notification from your app to the |
| 65 | * system, so that pair should be unique within your app. If you call one |
| 66 | * of the notify methods with a (tag, id) pair that is currently active and |
| 67 | * a new set of notification parameters, it will be updated. For example, |
| 68 | * if you pass a new status bar icon, the old icon in the status bar will |
| 69 | * be replaced with the new one. This is also the same tag and id you pass |
| 70 | * to the {@link #cancel(int)} or {@link #cancel(String, int)} method to clear |
| 71 | * this notification. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | * |
| 73 | * <p> |
| 74 | * You do not instantiate this class directly; instead, retrieve it through |
| 75 | * {@link android.content.Context#getSystemService}. |
| 76 | * |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 77 | * <div class="special reference"> |
| 78 | * <h3>Developer Guides</h3> |
| 79 | * <p>For a guide to creating notifications, read the |
| 80 | * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a> |
| 81 | * developer guide.</p> |
| 82 | * </div> |
| 83 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | * @see android.app.Notification |
| 85 | * @see android.content.Context#getSystemService |
| 86 | */ |
| 87 | public class NotificationManager |
| 88 | { |
| 89 | private static String TAG = "NotificationManager"; |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 90 | private static boolean localLOGV = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 92 | /** |
| 93 | * Intent that is broadcast when the state of {@link #getEffectsSuppressor()} changes. |
| 94 | * This broadcast is only sent to registered receivers. |
| 95 | * |
| 96 | * @hide |
| 97 | */ |
| 98 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 99 | public static final String ACTION_EFFECTS_SUPPRESSOR_CHANGED |
| 100 | = "android.os.action.ACTION_EFFECTS_SUPPRESSOR_CHANGED"; |
| 101 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 102 | /** |
| 103 | * Intent that is broadcast when the state of getNotificationPolicy() changes. |
| 104 | * This broadcast is only sent to registered receivers. |
| 105 | */ |
| 106 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 107 | public static final String ACTION_NOTIFICATION_POLICY_CHANGED |
| 108 | = "android.app.action.NOTIFICATION_POLICY_CHANGED"; |
| 109 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | private static INotificationManager sService; |
| 111 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 112 | /** @hide */ |
| 113 | static public INotificationManager getService() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | { |
| 115 | if (sService != null) { |
| 116 | return sService; |
| 117 | } |
| 118 | IBinder b = ServiceManager.getService("notification"); |
| 119 | sService = INotificationManager.Stub.asInterface(b); |
| 120 | return sService; |
| 121 | } |
| 122 | |
| 123 | /*package*/ NotificationManager(Context context, Handler handler) |
| 124 | { |
| 125 | mContext = context; |
| 126 | } |
| 127 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 128 | /** {@hide} */ |
| 129 | public static NotificationManager from(Context context) { |
| 130 | return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
| 131 | } |
| 132 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | /** |
Daniel Sandler | e97a3bc | 2011-02-07 16:47:07 -0500 | [diff] [blame] | 134 | * Post a notification to be shown in the status bar. If a notification with |
| 135 | * the same id has already been posted by your application and has not yet been canceled, it |
| 136 | * will be replaced by the updated information. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | * |
| 138 | * @param id An identifier for this notification unique within your |
| 139 | * application. |
Daniel Sandler | e97a3bc | 2011-02-07 16:47:07 -0500 | [diff] [blame] | 140 | * @param notification A {@link Notification} object describing what to show the user. Must not |
| 141 | * be null. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | */ |
| 143 | public void notify(int id, Notification notification) |
| 144 | { |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 145 | notify(null, id, notification); |
| 146 | } |
| 147 | |
| 148 | /** |
Daniel Sandler | e97a3bc | 2011-02-07 16:47:07 -0500 | [diff] [blame] | 149 | * Post a notification to be shown in the status bar. If a notification with |
| 150 | * the same tag and id has already been posted by your application and has not yet been |
| 151 | * canceled, it will be replaced by the updated information. |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 152 | * |
Peter Collingbourne | b97c349 | 2010-10-13 20:04:52 +0100 | [diff] [blame] | 153 | * @param tag A string identifier for this notification. May be {@code null}. |
| 154 | * @param id An identifier for this notification. The pair (tag, id) must be unique |
| 155 | * within your application. |
Daniel Sandler | e97a3bc | 2011-02-07 16:47:07 -0500 | [diff] [blame] | 156 | * @param notification A {@link Notification} object describing what to |
| 157 | * show the user. Must not be null. |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 158 | */ |
| 159 | public void notify(String tag, int id, Notification notification) |
| 160 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | int[] idOut = new int[1]; |
| 162 | INotificationManager service = getService(); |
| 163 | String pkg = mContext.getPackageName(); |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 164 | if (notification.sound != null) { |
| 165 | notification.sound = notification.sound.getCanonicalUri(); |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 166 | if (StrictMode.vmFileUriExposureEnabled()) { |
| 167 | notification.sound.checkFileUriExposed("Notification.sound"); |
| 168 | } |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 169 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")"); |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 171 | Notification stripped = notification.clone(); |
| 172 | Builder.stripForDelivery(stripped); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | try { |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 174 | service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id, |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 175 | stripped, idOut, UserHandle.myUserId()); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 176 | if (id != idOut[0]) { |
| 177 | Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]); |
| 178 | } |
| 179 | } catch (RemoteException e) { |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @hide |
| 185 | */ |
| 186 | public void notifyAsUser(String tag, int id, Notification notification, UserHandle user) |
| 187 | { |
| 188 | int[] idOut = new int[1]; |
| 189 | INotificationManager service = getService(); |
| 190 | String pkg = mContext.getPackageName(); |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 191 | if (notification.sound != null) { |
| 192 | notification.sound = notification.sound.getCanonicalUri(); |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 193 | if (StrictMode.vmFileUriExposureEnabled()) { |
| 194 | notification.sound.checkFileUriExposed("Notification.sound"); |
| 195 | } |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 196 | } |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 197 | if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")"); |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 198 | Notification stripped = notification.clone(); |
| 199 | Builder.stripForDelivery(stripped); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 200 | try { |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 201 | service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id, |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 202 | stripped, idOut, user.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | if (id != idOut[0]) { |
| 204 | Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]); |
| 205 | } |
| 206 | } catch (RemoteException e) { |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Cancel a previously shown notification. If it's transient, the view |
| 212 | * will be hidden. If it's persistent, it will be removed from the status |
| 213 | * bar. |
| 214 | */ |
| 215 | public void cancel(int id) |
| 216 | { |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 217 | cancel(null, id); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Cancel a previously shown notification. If it's transient, the view |
| 222 | * will be hidden. If it's persistent, it will be removed from the status |
| 223 | * bar. |
| 224 | */ |
| 225 | public void cancel(String tag, int id) |
| 226 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | INotificationManager service = getService(); |
| 228 | String pkg = mContext.getPackageName(); |
| 229 | if (localLOGV) Log.v(TAG, pkg + ": cancel(" + id + ")"); |
| 230 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 231 | service.cancelNotificationWithTag(pkg, tag, id, UserHandle.myUserId()); |
| 232 | } catch (RemoteException e) { |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * @hide |
| 238 | */ |
| 239 | public void cancelAsUser(String tag, int id, UserHandle user) |
| 240 | { |
| 241 | INotificationManager service = getService(); |
| 242 | String pkg = mContext.getPackageName(); |
| 243 | if (localLOGV) Log.v(TAG, pkg + ": cancel(" + id + ")"); |
| 244 | try { |
| 245 | service.cancelNotificationWithTag(pkg, tag, id, user.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | } catch (RemoteException e) { |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Cancel all previously shown notifications. See {@link #cancel} for the |
| 252 | * detailed behavior. |
| 253 | */ |
| 254 | public void cancelAll() |
| 255 | { |
| 256 | INotificationManager service = getService(); |
| 257 | String pkg = mContext.getPackageName(); |
| 258 | if (localLOGV) Log.v(TAG, pkg + ": cancelAll()"); |
| 259 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 260 | service.cancelAllNotifications(pkg, UserHandle.myUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | } catch (RemoteException e) { |
| 262 | } |
| 263 | } |
| 264 | |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 265 | /** |
| 266 | * @hide |
| 267 | */ |
| 268 | public ComponentName getEffectsSuppressor() { |
| 269 | INotificationManager service = getService(); |
| 270 | try { |
| 271 | return service.getEffectsSuppressor(); |
| 272 | } catch (RemoteException e) { |
| 273 | return null; |
| 274 | } |
| 275 | } |
| 276 | |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 277 | /** |
| 278 | * @hide |
| 279 | */ |
| 280 | public boolean matchesCallFilter(Bundle extras) { |
| 281 | INotificationManager service = getService(); |
| 282 | try { |
| 283 | return service.matchesCallFilter(extras); |
| 284 | } catch (RemoteException e) { |
| 285 | return false; |
| 286 | } |
| 287 | } |
| 288 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 289 | /** |
| 290 | * @hide |
| 291 | */ |
| 292 | public boolean isSystemConditionProviderEnabled(String path) { |
| 293 | INotificationManager service = getService(); |
| 294 | try { |
| 295 | return service.isSystemConditionProviderEnabled(path); |
| 296 | } catch (RemoteException e) { |
| 297 | return false; |
| 298 | } |
| 299 | } |
| 300 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 301 | /** |
| 302 | * @hide |
| 303 | */ |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 304 | public void setZenMode(int mode, Uri conditionId, String reason) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 305 | INotificationManager service = getService(); |
| 306 | try { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 307 | service.setZenMode(mode, conditionId, reason); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 308 | } catch (RemoteException e) { |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * @hide |
| 314 | */ |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 315 | public boolean setZenModeConfig(ZenModeConfig config, String reason) { |
| 316 | INotificationManager service = getService(); |
| 317 | try { |
| 318 | return service.setZenModeConfig(config, reason); |
| 319 | } catch (RemoteException e) { |
| 320 | return false; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * @hide |
| 326 | */ |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 327 | public void requestZenModeConditions(IConditionListener listener, int relevance) { |
| 328 | INotificationManager service = getService(); |
| 329 | try { |
| 330 | service.requestZenModeConditions(listener, relevance); |
| 331 | } catch (RemoteException e) { |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * @hide |
| 337 | */ |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 338 | public int getZenMode() { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 339 | INotificationManager service = getService(); |
| 340 | try { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 341 | return service.getZenMode(); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 342 | } catch (RemoteException e) { |
| 343 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 344 | return Global.ZEN_MODE_OFF; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /** |
| 348 | * @hide |
| 349 | */ |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 350 | public ZenModeConfig getZenModeConfig() { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 351 | INotificationManager service = getService(); |
| 352 | try { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 353 | return service.getZenModeConfig(); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 354 | } catch (RemoteException e) { |
| 355 | } |
| 356 | return null; |
| 357 | } |
| 358 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 359 | /** |
| 360 | * Requests a notification policy token for the calling package. |
| 361 | * |
| 362 | * @param callback required, used to receive the granted token or the deny signal. |
| 363 | * @param handler The handler used when receiving the result. |
| 364 | * If null, the current thread is used. |
| 365 | */ |
| 366 | public void requestNotificationPolicyToken(@NonNull final Policy.Token.RequestCallback callback, |
| 367 | @Nullable Handler handler) { |
| 368 | checkRequired("callback", callback); |
| 369 | final Handler h = handler != null ? handler : new Handler(); |
| 370 | INotificationManager service = getService(); |
| 371 | try { |
| 372 | service.requestNotificationPolicyToken(mContext.getOpPackageName(), |
| 373 | new INotificationManagerCallback.Stub() { |
| 374 | @Override |
| 375 | public void onPolicyToken(final Token token) throws RemoteException { |
| 376 | h.post(new Runnable() { |
| 377 | @Override |
| 378 | public void run() { |
| 379 | if (token != null) { |
| 380 | callback.onTokenGranted(token); |
| 381 | } else { |
| 382 | callback.onTokenDenied(); |
| 383 | } |
| 384 | } |
| 385 | }); |
| 386 | } |
| 387 | }); |
| 388 | } catch (RemoteException e) { |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Checks a given notification policy token. |
| 394 | * |
| 395 | * Returns true if the token is still valid for managing policy. |
| 396 | */ |
| 397 | public boolean isNotificationPolicyTokenValid(@NonNull Policy.Token token) { |
| 398 | if (token == null) return false; |
| 399 | INotificationManager service = getService(); |
| 400 | try { |
| 401 | return service.isNotificationPolicyTokenValid(mContext.getOpPackageName(), token); |
| 402 | } catch (RemoteException e) { |
| 403 | } |
| 404 | return false; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Gets the current notification policy. |
| 409 | * |
| 410 | * @param token A valid notification policy token is required to access the current policy. |
| 411 | */ |
| 412 | public Policy getNotificationPolicy(@NonNull Policy.Token token) { |
| 413 | checkRequired("token", token); |
| 414 | INotificationManager service = getService(); |
| 415 | try { |
| 416 | return service.getNotificationPolicy(token); |
| 417 | } catch (RemoteException e) { |
| 418 | } |
| 419 | return null; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Sets the current notification policy. |
| 424 | * |
| 425 | * @param token A valid notification policy token is required to modify the current policy. |
| 426 | * @param policy The new desired policy. |
| 427 | */ |
| 428 | public void setNotificationPolicy(@NonNull Policy.Token token, @NonNull Policy policy) { |
| 429 | checkRequired("token", token); |
| 430 | checkRequired("policy", policy); |
| 431 | INotificationManager service = getService(); |
| 432 | try { |
| 433 | service.setNotificationPolicy(token, policy); |
| 434 | } catch (RemoteException e) { |
| 435 | } |
| 436 | } |
| 437 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | private Context mContext; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 439 | |
| 440 | private static void checkRequired(String name, Object value) { |
| 441 | if (value == null) { |
| 442 | throw new IllegalArgumentException(name + " is required"); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Notification policy configuration. Represents user-preferences for notification |
| 448 | * filtering and prioritization. |
| 449 | */ |
| 450 | public static class Policy implements android.os.Parcelable { |
| 451 | /** Reminder notifications are prioritized. */ |
| 452 | public static final int PRIORITY_CATEGORY_REMINDERS = 1 << 0; |
| 453 | /** Event notifications are prioritized. */ |
| 454 | public static final int PRIORITY_CATEGORY_EVENTS = 1 << 1; |
| 455 | /** Message notifications are prioritized. */ |
| 456 | public static final int PRIORITY_CATEGORY_MESSAGES = 1 << 2; |
| 457 | /** Calls are prioritized. */ |
| 458 | public static final int PRIORITY_CATEGORY_CALLS = 1 << 3; |
| 459 | /** Calls from repeat callers are prioritized. */ |
| 460 | public static final int PRIORITY_CATEGORY_REPEAT_CALLERS = 1 << 4; |
| 461 | |
| 462 | private static final int[] ALL_PRIORITY_CATEGORIES = { |
| 463 | PRIORITY_CATEGORY_REMINDERS, |
| 464 | PRIORITY_CATEGORY_EVENTS, |
| 465 | PRIORITY_CATEGORY_MESSAGES, |
| 466 | PRIORITY_CATEGORY_CALLS, |
| 467 | PRIORITY_CATEGORY_REPEAT_CALLERS, |
| 468 | }; |
| 469 | |
| 470 | /** Any sender is prioritized. */ |
| 471 | public static final int PRIORITY_SENDERS_ANY = 0; |
| 472 | /** Saved contacts are prioritized. */ |
| 473 | public static final int PRIORITY_SENDERS_CONTACTS = 1; |
| 474 | /** Only starred contacts are prioritized. */ |
| 475 | public static final int PRIORITY_SENDERS_STARRED = 2; |
| 476 | |
| 477 | /** Notification categories to prioritize. Bitmask of PRIORITY_CATEGORY_* constants. */ |
| 478 | public final int priorityCategories; |
| 479 | |
| 480 | /** Notification senders to prioritize. One of: |
| 481 | * PRIORITY_SENDERS_ANY, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED */ |
| 482 | public final int prioritySenders; |
| 483 | |
| 484 | public Policy(int priorityCategories, int prioritySenders) { |
| 485 | this.priorityCategories = priorityCategories; |
| 486 | this.prioritySenders = prioritySenders; |
| 487 | } |
| 488 | |
| 489 | /** @hide */ |
| 490 | public Policy(Parcel source) { |
| 491 | this(source.readInt(), source.readInt()); |
| 492 | } |
| 493 | |
| 494 | @Override |
| 495 | public void writeToParcel(Parcel dest, int flags) { |
| 496 | dest.writeInt(priorityCategories); |
| 497 | dest.writeInt(prioritySenders); |
| 498 | } |
| 499 | |
| 500 | @Override |
| 501 | public int describeContents() { |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | @Override |
| 506 | public int hashCode() { |
| 507 | return Objects.hash(priorityCategories, prioritySenders); |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public boolean equals(Object o) { |
| 512 | if (!(o instanceof Policy)) return false; |
| 513 | if (o == this) return true; |
| 514 | final Policy other = (Policy) o; |
| 515 | return other.priorityCategories == priorityCategories |
| 516 | && other.prioritySenders == prioritySenders; |
| 517 | } |
| 518 | |
| 519 | @Override |
| 520 | public String toString() { |
| 521 | return "NotificationManager.Policy[" |
| 522 | + "priorityCategories=" + priorityCategoriesToString(priorityCategories) |
| 523 | + ",prioritySenders=" + prioritySendersToString(prioritySenders) |
| 524 | + "]"; |
| 525 | } |
| 526 | |
| 527 | public static String priorityCategoriesToString(int priorityCategories) { |
| 528 | if (priorityCategories == 0) return ""; |
| 529 | final StringBuilder sb = new StringBuilder(); |
| 530 | for (int i = 0; i < ALL_PRIORITY_CATEGORIES.length; i++) { |
| 531 | final int priorityCategory = ALL_PRIORITY_CATEGORIES[i]; |
| 532 | if ((priorityCategories & priorityCategory) != 0) { |
| 533 | if (sb.length() > 0) sb.append(','); |
| 534 | sb.append(priorityCategoryToString(priorityCategory)); |
| 535 | } |
| 536 | priorityCategories &= ~priorityCategory; |
| 537 | } |
| 538 | if (priorityCategories != 0) { |
| 539 | if (sb.length() > 0) sb.append(','); |
| 540 | sb.append("PRIORITY_CATEGORY_UNKNOWN_").append(priorityCategories); |
| 541 | } |
| 542 | return sb.toString(); |
| 543 | } |
| 544 | |
| 545 | private static String priorityCategoryToString(int priorityCategory) { |
| 546 | switch (priorityCategory) { |
| 547 | case PRIORITY_CATEGORY_REMINDERS: return "PRIORITY_CATEGORY_REMINDERS"; |
| 548 | case PRIORITY_CATEGORY_EVENTS: return "PRIORITY_CATEGORY_EVENTS"; |
| 549 | case PRIORITY_CATEGORY_MESSAGES: return "PRIORITY_CATEGORY_MESSAGES"; |
| 550 | case PRIORITY_CATEGORY_CALLS: return "PRIORITY_CATEGORY_CALLS"; |
| 551 | case PRIORITY_CATEGORY_REPEAT_CALLERS: return "PRIORITY_CATEGORY_REPEAT_CALLERS"; |
| 552 | default: return "PRIORITY_CATEGORY_UNKNOWN_" + priorityCategory; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | public static String prioritySendersToString(int prioritySenders) { |
| 557 | switch (prioritySenders) { |
| 558 | case PRIORITY_SENDERS_ANY: return "PRIORITY_SENDERS_ANY"; |
| 559 | case PRIORITY_SENDERS_CONTACTS: return "PRIORITY_SENDERS_CONTACTS"; |
| 560 | case PRIORITY_SENDERS_STARRED: return "PRIORITY_SENDERS_STARRED"; |
| 561 | default: return "PRIORITY_SENDERS_UNKNOWN_" + prioritySenders; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | public static final Parcelable.Creator<Policy> CREATOR = new Parcelable.Creator<Policy>() { |
| 566 | @Override |
| 567 | public Policy createFromParcel(Parcel in) { |
| 568 | return new Policy(in); |
| 569 | } |
| 570 | |
| 571 | @Override |
| 572 | public Policy[] newArray(int size) { |
| 573 | return new Policy[size]; |
| 574 | } |
| 575 | }; |
| 576 | |
| 577 | /** |
| 578 | * Represents a client-specific token required to manage notification policy. |
| 579 | */ |
| 580 | public static class Token implements Parcelable { |
| 581 | private final IBinder mBinder; |
| 582 | |
| 583 | /** @hide */ |
| 584 | public Token(IBinder binder) { |
| 585 | if (binder == null) throw new IllegalArgumentException("Binder required for token"); |
| 586 | mBinder = binder; |
| 587 | } |
| 588 | |
| 589 | @Override |
| 590 | public int describeContents() { |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | @Override |
| 595 | public int hashCode() { |
| 596 | return Objects.hash(mBinder); |
| 597 | } |
| 598 | |
| 599 | @Override |
| 600 | public boolean equals(Object o) { |
| 601 | if (!(o instanceof Token)) return false; |
| 602 | if (o == this) return true; |
| 603 | final Token other = (Token) o; |
| 604 | return Objects.equals(other.mBinder, mBinder); |
| 605 | } |
| 606 | |
| 607 | @Override |
| 608 | public String toString() { |
| 609 | return String.format("NotificationManager.Token[0x%08x]", |
| 610 | System.identityHashCode(mBinder)); |
| 611 | } |
| 612 | |
| 613 | @Override |
| 614 | public void writeToParcel(Parcel dest, int flags) { |
| 615 | dest.writeStrongBinder(mBinder); |
| 616 | } |
| 617 | |
| 618 | public static final Parcelable.Creator<Token> CREATOR |
| 619 | = new Parcelable.Creator<Token>() { |
| 620 | @Override |
| 621 | public Token createFromParcel(Parcel in) { |
| 622 | return new Token(in.readStrongBinder()); |
| 623 | } |
| 624 | |
| 625 | @Override |
| 626 | public Token[] newArray(int size) { |
| 627 | return new Token[size]; |
| 628 | } |
| 629 | }; |
| 630 | |
| 631 | /** Callback for receiving the result of a token request. */ |
| 632 | public static abstract class RequestCallback { |
| 633 | /** |
| 634 | * Received if the request was granted for this package. |
| 635 | * |
| 636 | * @param token can be used to manage notification policy. |
| 637 | */ |
| 638 | public abstract void onTokenGranted(Policy.Token token); |
| 639 | |
| 640 | /** |
| 641 | * Received if the request was denied for this package. |
| 642 | */ |
| 643 | public abstract void onTokenDenied(); |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 648 | /** |
| 649 | * Recover a list of active notifications: ones that have been posted by the calling app that |
| 650 | * have not yet been dismissed by the user or {@link #cancel(String, int)}ed by the app. |
| 651 | * |
| 652 | * Each notification is embedded in a {@link StatusBarNotification} object, including the |
| 653 | * original <code>tag</code> and <code>id</code> supplied to |
| 654 | * {@link #notify(String, int, Notification) notify()} |
| 655 | * (via {@link StatusBarNotification#getTag() getTag()} and |
| 656 | * {@link StatusBarNotification#getId() getId()}) as well as a copy of the original |
| 657 | * {@link Notification} object (via {@link StatusBarNotification#getNotification()}). |
| 658 | * |
| 659 | * @return An array of {@link StatusBarNotification}. |
| 660 | */ |
| 661 | public StatusBarNotification[] getActiveNotifications() { |
| 662 | final INotificationManager service = getService(); |
| 663 | final String pkg = mContext.getPackageName(); |
| 664 | try { |
| 665 | final ParceledListSlice<StatusBarNotification> parceledList |
| 666 | = service.getAppActiveNotifications(pkg, UserHandle.myUserId()); |
| 667 | final List<StatusBarNotification> list = parceledList.getList(); |
| 668 | return list.toArray(new StatusBarNotification[list.size()]); |
| 669 | } catch (RemoteException e) { |
| 670 | Log.e(TAG, "Unable to talk to notification manager. Woe!", e); |
| 671 | } |
| 672 | return new StatusBarNotification[0]; |
| 673 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 674 | } |