blob: 0a5902658aa00dab210e45e770a1b12f11655639 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
John Spurlock1fc476d2015-04-14 16:05:20 -040019import android.annotation.NonNull;
20import android.annotation.Nullable;
John Spurlockb4782522014-08-22 14:54:46 -040021import android.annotation.SdkConstant;
Christoph Studer4600f9b2014-07-22 22:44:43 +020022import android.app.Notification.Builder;
John Spurlock1fc476d2015-04-14 16:05:20 -040023import android.app.NotificationManager.Policy.Token;
John Spurlockb4782522014-08-22 14:54:46 -040024import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.Context;
Dan Sandler994349c2015-04-15 11:02:54 -040026import android.content.pm.ParceledListSlice;
John Spurlockb2278d62015-04-07 12:47:12 -040027import android.net.Uri;
John Spurlock2b122f42014-08-27 16:29:47 -040028import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.os.Handler;
30import android.os.IBinder;
John Spurlock1fc476d2015-04-14 16:05:20 -040031import android.os.Parcel;
32import android.os.Parcelable;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070033import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.ServiceManager;
Jeff Sharkeya14acd22013-04-02 18:27:45 -070035import android.os.StrictMode;
Dianne Hackborn41203752012-08-31 14:05:51 -070036import android.os.UserHandle;
John Spurlockb2278d62015-04-07 12:47:12 -040037import android.provider.Settings.Global;
John Spurlockcdb57ae2015-02-11 19:04:11 -050038import android.service.notification.IConditionListener;
Dan Sandler994349c2015-04-15 11:02:54 -040039import android.service.notification.StatusBarNotification;
John Spurlockcdb57ae2015-02-11 19:04:11 -050040import android.service.notification.ZenModeConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.util.Log;
42
John Spurlock1fc476d2015-04-14 16:05:20 -040043import java.util.Objects;
Dan Sandler994349c2015-04-15 11:02:54 -040044import java.util.List;
John Spurlock1fc476d2015-04-14 16:05:20 -040045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046/**
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 Collingbourneb97c3492010-10-13 20:04:52 +010061 * 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 Project9066cfe2009-03-03 19:31:44 -080072 *
73 * <p>
74 * You do not instantiate this class directly; instead, retrieve it through
75 * {@link android.content.Context#getSystemService}.
76 *
Joe Fernandez558459f2011-10-13 16:47:36 -070077 * <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 Project9066cfe2009-03-03 19:31:44 -080084 * @see android.app.Notification
85 * @see android.content.Context#getSystemService
86 */
87public class NotificationManager
88{
89 private static String TAG = "NotificationManager";
Joe Onorato43a17652011-04-06 19:22:23 -070090 private static boolean localLOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
John Spurlockb4782522014-08-22 14:54:46 -040092 /**
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 Spurlock1fc476d2015-04-14 16:05:20 -0400102 /**
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 Project9066cfe2009-03-03 19:31:44 -0800110 private static INotificationManager sService;
111
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700112 /** @hide */
113 static public INotificationManager getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 {
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 Sharkey69ddab42012-08-25 00:05:46 -0700128 /** {@hide} */
129 public static NotificationManager from(Context context) {
130 return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
131 }
132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 /**
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500134 * 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 Project9066cfe2009-03-03 19:31:44 -0800137 *
138 * @param id An identifier for this notification unique within your
139 * application.
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500140 * @param notification A {@link Notification} object describing what to show the user. Must not
141 * be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 */
143 public void notify(int id, Notification notification)
144 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700145 notify(null, id, notification);
146 }
147
148 /**
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500149 * 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 Quintana6ecaff12009-09-25 14:23:13 -0700152 *
Peter Collingbourneb97c3492010-10-13 20:04:52 +0100153 * @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 Sandlere97a3bc2011-02-07 16:47:07 -0500156 * @param notification A {@link Notification} object describing what to
157 * show the user. Must not be null.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700158 */
159 public void notify(String tag, int id, Notification notification)
160 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 int[] idOut = new int[1];
162 INotificationManager service = getService();
163 String pkg = mContext.getPackageName();
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700164 if (notification.sound != null) {
165 notification.sound = notification.sound.getCanonicalUri();
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700166 if (StrictMode.vmFileUriExposureEnabled()) {
167 notification.sound.checkFileUriExposed("Notification.sound");
168 }
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
Christoph Studer4600f9b2014-07-22 22:44:43 +0200171 Notification stripped = notification.clone();
172 Builder.stripForDelivery(stripped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 try {
Dianne Hackborn95d78532013-09-11 09:51:14 -0700174 service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id,
Christoph Studer4600f9b2014-07-22 22:44:43 +0200175 stripped, idOut, UserHandle.myUserId());
Dianne Hackborn41203752012-08-31 14:05:51 -0700176 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 Sharkey65c4a2b2012-09-25 17:22:27 -0700191 if (notification.sound != null) {
192 notification.sound = notification.sound.getCanonicalUri();
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700193 if (StrictMode.vmFileUriExposureEnabled()) {
194 notification.sound.checkFileUriExposed("Notification.sound");
195 }
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700196 }
Dianne Hackborn41203752012-08-31 14:05:51 -0700197 if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
Christoph Studer4600f9b2014-07-22 22:44:43 +0200198 Notification stripped = notification.clone();
199 Builder.stripForDelivery(stripped);
Dianne Hackborn41203752012-08-31 14:05:51 -0700200 try {
Dianne Hackborn95d78532013-09-11 09:51:14 -0700201 service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id,
Christoph Studer4600f9b2014-07-22 22:44:43 +0200202 stripped, idOut, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 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 Quintana6ecaff12009-09-25 14:23:13 -0700217 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 Project9066cfe2009-03-03 19:31:44 -0800227 INotificationManager service = getService();
228 String pkg = mContext.getPackageName();
229 if (localLOGV) Log.v(TAG, pkg + ": cancel(" + id + ")");
230 try {
Dianne Hackborn41203752012-08-31 14:05:51 -0700231 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 Project9066cfe2009-03-03 19:31:44 -0800246 } 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 Hackborn41203752012-08-31 14:05:51 -0700260 service.cancelAllNotifications(pkg, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 } catch (RemoteException e) {
262 }
263 }
264
John Spurlockb4782522014-08-22 14:54:46 -0400265 /**
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 Spurlock2b122f42014-08-27 16:29:47 -0400277 /**
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 Spurlock530052a2014-11-30 16:26:19 -0500289 /**
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 Spurlockcdb57ae2015-02-11 19:04:11 -0500301 /**
302 * @hide
303 */
John Spurlockb2278d62015-04-07 12:47:12 -0400304 public void setZenMode(int mode, Uri conditionId, String reason) {
John Spurlockcdb57ae2015-02-11 19:04:11 -0500305 INotificationManager service = getService();
306 try {
John Spurlockb2278d62015-04-07 12:47:12 -0400307 service.setZenMode(mode, conditionId, reason);
John Spurlockcdb57ae2015-02-11 19:04:11 -0500308 } catch (RemoteException e) {
309 }
310 }
311
312 /**
313 * @hide
314 */
John Spurlockb2278d62015-04-07 12:47:12 -0400315 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 Spurlockcdb57ae2015-02-11 19:04:11 -0500327 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 Spurlockb2278d62015-04-07 12:47:12 -0400338 public int getZenMode() {
John Spurlockcdb57ae2015-02-11 19:04:11 -0500339 INotificationManager service = getService();
340 try {
John Spurlockb2278d62015-04-07 12:47:12 -0400341 return service.getZenMode();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500342 } catch (RemoteException e) {
343 }
John Spurlockb2278d62015-04-07 12:47:12 -0400344 return Global.ZEN_MODE_OFF;
John Spurlockcdb57ae2015-02-11 19:04:11 -0500345 }
346
347 /**
348 * @hide
349 */
John Spurlockb2278d62015-04-07 12:47:12 -0400350 public ZenModeConfig getZenModeConfig() {
John Spurlockcdb57ae2015-02-11 19:04:11 -0500351 INotificationManager service = getService();
352 try {
John Spurlockb2278d62015-04-07 12:47:12 -0400353 return service.getZenModeConfig();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500354 } catch (RemoteException e) {
355 }
356 return null;
357 }
358
John Spurlock1fc476d2015-04-14 16:05:20 -0400359 /**
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 Project9066cfe2009-03-03 19:31:44 -0800438 private Context mContext;
John Spurlock1fc476d2015-04-14 16:05:20 -0400439
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 Sandler994349c2015-04-15 11:02:54 -0400648 /**
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 Project9066cfe2009-03-03 19:31:44 -0800674}