NotificationListenerService.java revision b66a16d9751d5e0f2b6816367e7d8919d707c692
1/* 2 * Copyright (C) 2013 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.service.notification; 18 19import android.os.Handler; 20import android.os.Looper; 21import android.os.Message; 22 23import android.annotation.IntDef; 24import android.annotation.SystemApi; 25import android.annotation.SdkConstant; 26import android.app.INotificationManager; 27import android.app.Notification; 28import android.app.Notification.Builder; 29import android.app.NotificationManager; 30import android.app.Service; 31import android.content.ComponentName; 32import android.content.Context; 33import android.content.Intent; 34import android.content.pm.ParceledListSlice; 35import android.graphics.drawable.BitmapDrawable; 36import android.graphics.drawable.Drawable; 37import android.graphics.drawable.Icon; 38import android.graphics.Bitmap; 39import android.os.Build; 40import android.os.Bundle; 41import android.os.IBinder; 42import android.os.Parcel; 43import android.os.Parcelable; 44import android.os.RemoteException; 45import android.os.ServiceManager; 46import android.util.ArrayMap; 47import android.util.ArraySet; 48import android.util.Log; 49import android.widget.RemoteViews; 50import com.android.internal.annotations.GuardedBy; 51import com.android.internal.os.SomeArgs; 52import java.lang.annotation.Retention; 53import java.lang.annotation.RetentionPolicy; 54import java.util.ArrayList; 55import java.util.Collections; 56import java.util.List; 57 58/** 59 * A service that receives calls from the system when new notifications are 60 * posted or removed, or their ranking changed. 61 * <p>To extend this class, you must declare the service in your manifest file with 62 * the {@link android.Manifest.permission#BIND_NOTIFICATION_LISTENER_SERVICE} permission 63 * and include an intent filter with the {@link #SERVICE_INTERFACE} action. For example:</p> 64 * <pre> 65 * <service android:name=".NotificationListener" 66 * android:label="@string/service_name" 67 * android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> 68 * <intent-filter> 69 * <action android:name="android.service.notification.NotificationListenerService" /> 70 * </intent-filter> 71 * </service></pre> 72 * 73 * <p>The service should wait for the {@link #onListenerConnected()} event 74 * before performing any operations. The {@link #requestRebind(ComponentName)} 75 * method is the <i>only</i> one that is safe to call before {@link #onListenerConnected()} 76 * or after {@link #onListenerDisconnected()}. 77 * </p> 78 */ 79public abstract class NotificationListenerService extends Service { 80 // TAG = "NotificationListenerService[MySubclass]" 81 private final String TAG = NotificationListenerService.class.getSimpleName() 82 + "[" + getClass().getSimpleName() + "]"; 83 84 /** 85 * {@link #getCurrentInterruptionFilter() Interruption filter} constant - 86 * Normal interruption filter. 87 */ 88 public static final int INTERRUPTION_FILTER_ALL 89 = NotificationManager.INTERRUPTION_FILTER_ALL; 90 91 /** 92 * {@link #getCurrentInterruptionFilter() Interruption filter} constant - 93 * Priority interruption filter. 94 */ 95 public static final int INTERRUPTION_FILTER_PRIORITY 96 = NotificationManager.INTERRUPTION_FILTER_PRIORITY; 97 98 /** 99 * {@link #getCurrentInterruptionFilter() Interruption filter} constant - 100 * No interruptions filter. 101 */ 102 public static final int INTERRUPTION_FILTER_NONE 103 = NotificationManager.INTERRUPTION_FILTER_NONE; 104 105 /** 106 * {@link #getCurrentInterruptionFilter() Interruption filter} constant - 107 * Alarms only interruption filter. 108 */ 109 public static final int INTERRUPTION_FILTER_ALARMS 110 = NotificationManager.INTERRUPTION_FILTER_ALARMS; 111 112 /** {@link #getCurrentInterruptionFilter() Interruption filter} constant - returned when 113 * the value is unavailable for any reason. For example, before the notification listener 114 * is connected. 115 * 116 * {@see #onListenerConnected()} 117 */ 118 public static final int INTERRUPTION_FILTER_UNKNOWN 119 = NotificationManager.INTERRUPTION_FILTER_UNKNOWN; 120 121 /** {@link #getCurrentListenerHints() Listener hints} constant - the primary device UI 122 * should disable notification sound, vibrating and other visual or aural effects. 123 * This does not change the interruption filter, only the effects. **/ 124 public static final int HINT_HOST_DISABLE_EFFECTS = 1; 125 126 /** {@link #getCurrentListenerHints() Listener hints} constant - the primary device UI 127 * should disable notification sound, but not phone calls. 128 * This does not change the interruption filter, only the effects. **/ 129 public static final int HINT_HOST_DISABLE_NOTIFICATION_EFFECTS = 1 << 1; 130 131 /** {@link #getCurrentListenerHints() Listener hints} constant - the primary device UI 132 * should disable phone call sounds, buyt not notification sound. 133 * This does not change the interruption filter, only the effects. **/ 134 public static final int HINT_HOST_DISABLE_CALL_EFFECTS = 1 << 2; 135 136 /** 137 * Whether notification suppressed by DND should not interruption visually when the screen is 138 * off. 139 */ 140 public static final int SUPPRESSED_EFFECT_SCREEN_OFF = 141 NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF; 142 /** 143 * Whether notification suppressed by DND should not interruption visually when the screen is 144 * on. 145 */ 146 public static final int SUPPRESSED_EFFECT_SCREEN_ON = 147 NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON; 148 149 /** 150 * The full trim of the StatusBarNotification including all its features. 151 * 152 * @hide 153 */ 154 @SystemApi 155 public static final int TRIM_FULL = 0; 156 157 /** 158 * A light trim of the StatusBarNotification excluding the following features: 159 * 160 * <ol> 161 * <li>{@link Notification#tickerView tickerView}</li> 162 * <li>{@link Notification#contentView contentView}</li> 163 * <li>{@link Notification#largeIcon largeIcon}</li> 164 * <li>{@link Notification#bigContentView bigContentView}</li> 165 * <li>{@link Notification#headsUpContentView headsUpContentView}</li> 166 * <li>{@link Notification#EXTRA_LARGE_ICON extras[EXTRA_LARGE_ICON]}</li> 167 * <li>{@link Notification#EXTRA_LARGE_ICON_BIG extras[EXTRA_LARGE_ICON_BIG]}</li> 168 * <li>{@link Notification#EXTRA_PICTURE extras[EXTRA_PICTURE]}</li> 169 * <li>{@link Notification#EXTRA_BIG_TEXT extras[EXTRA_BIG_TEXT]}</li> 170 * </ol> 171 * 172 * @hide 173 */ 174 @SystemApi 175 public static final int TRIM_LIGHT = 1; 176 177 private final Object mLock = new Object(); 178 179 private Handler mHandler; 180 181 /** @hide */ 182 protected NotificationListenerWrapper mWrapper = null; 183 private boolean isConnected = false; 184 185 @GuardedBy("mLock") 186 private RankingMap mRankingMap; 187 188 private INotificationManager mNoMan; 189 190 /** 191 * Only valid after a successful call to (@link registerAsService}. 192 * @hide 193 */ 194 protected int mCurrentUser; 195 196 /** 197 * This context is required for system services since NotificationListenerService isn't 198 * started as a real Service and hence no context is available.. 199 * @hide 200 */ 201 protected Context mSystemContext; 202 203 /** 204 * The {@link Intent} that must be declared as handled by the service. 205 */ 206 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) 207 public static final String SERVICE_INTERFACE 208 = "android.service.notification.NotificationListenerService"; 209 210 @Override 211 protected void attachBaseContext(Context base) { 212 super.attachBaseContext(base); 213 mHandler = new MyHandler(getMainLooper()); 214 } 215 216 /** 217 * Implement this method to learn about new notifications as they are posted by apps. 218 * 219 * @param sbn A data structure encapsulating the original {@link android.app.Notification} 220 * object as well as its identifying information (tag and id) and source 221 * (package name). 222 */ 223 public void onNotificationPosted(StatusBarNotification sbn) { 224 // optional 225 } 226 227 /** 228 * Implement this method to learn about new notifications as they are posted by apps. 229 * 230 * @param sbn A data structure encapsulating the original {@link android.app.Notification} 231 * object as well as its identifying information (tag and id) and source 232 * (package name). 233 * @param rankingMap The current ranking map that can be used to retrieve ranking information 234 * for active notifications, including the newly posted one. 235 */ 236 public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) { 237 onNotificationPosted(sbn); 238 } 239 240 /** 241 * Implement this method to learn when notifications are removed. 242 * <p> 243 * This might occur because the user has dismissed the notification using system UI (or another 244 * notification listener) or because the app has withdrawn the notification. 245 * <p> 246 * NOTE: The {@link StatusBarNotification} object you receive will be "light"; that is, the 247 * result from {@link StatusBarNotification#getNotification} may be missing some heavyweight 248 * fields such as {@link android.app.Notification#contentView} and 249 * {@link android.app.Notification#largeIcon}. However, all other fields on 250 * {@link StatusBarNotification}, sufficient to match this call with a prior call to 251 * {@link #onNotificationPosted(StatusBarNotification)}, will be intact. 252 * 253 * @param sbn A data structure encapsulating at least the original information (tag and id) 254 * and source (package name) used to post the {@link android.app.Notification} that 255 * was just removed. 256 */ 257 public void onNotificationRemoved(StatusBarNotification sbn) { 258 // optional 259 } 260 261 /** 262 * Implement this method to learn when notifications are removed. 263 * <p> 264 * This might occur because the user has dismissed the notification using system UI (or another 265 * notification listener) or because the app has withdrawn the notification. 266 * <p> 267 * NOTE: The {@link StatusBarNotification} object you receive will be "light"; that is, the 268 * result from {@link StatusBarNotification#getNotification} may be missing some heavyweight 269 * fields such as {@link android.app.Notification#contentView} and 270 * {@link android.app.Notification#largeIcon}. However, all other fields on 271 * {@link StatusBarNotification}, sufficient to match this call with a prior call to 272 * {@link #onNotificationPosted(StatusBarNotification)}, will be intact. 273 * 274 * @param sbn A data structure encapsulating at least the original information (tag and id) 275 * and source (package name) used to post the {@link android.app.Notification} that 276 * was just removed. 277 * @param rankingMap The current ranking map that can be used to retrieve ranking information 278 * for active notifications. 279 * 280 */ 281 public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) { 282 onNotificationRemoved(sbn); 283 } 284 285 /** 286 * Implement this method to learn about when the listener is enabled and connected to 287 * the notification manager. You are safe to call {@link #getActiveNotifications()} 288 * at this time. 289 */ 290 public void onListenerConnected() { 291 // optional 292 } 293 294 /** 295 * Implement this method to learn about when the listener is disconnected from the 296 * notification manager.You will not receive any events after this call, and may only 297 * call {@link #requestRebind(ComponentName)} at this time. 298 */ 299 public void onListenerDisconnected() { 300 // optional 301 } 302 303 /** 304 * Implement this method to be notified when the notification ranking changes. 305 * 306 * @param rankingMap The current ranking map that can be used to retrieve ranking information 307 * for active notifications. 308 */ 309 public void onNotificationRankingUpdate(RankingMap rankingMap) { 310 // optional 311 } 312 313 /** 314 * Implement this method to be notified when the 315 * {@link #getCurrentListenerHints() Listener hints} change. 316 * 317 * @param hints The current {@link #getCurrentListenerHints() listener hints}. 318 */ 319 public void onListenerHintsChanged(int hints) { 320 // optional 321 } 322 323 /** 324 * Implement this method to be notified when the 325 * {@link #getCurrentInterruptionFilter() interruption filter} changed. 326 * 327 * @param interruptionFilter The current 328 * {@link #getCurrentInterruptionFilter() interruption filter}. 329 */ 330 public void onInterruptionFilterChanged(int interruptionFilter) { 331 // optional 332 } 333 334 /** @hide */ 335 protected final INotificationManager getNotificationInterface() { 336 if (mNoMan == null) { 337 mNoMan = INotificationManager.Stub.asInterface( 338 ServiceManager.getService(Context.NOTIFICATION_SERVICE)); 339 } 340 return mNoMan; 341 } 342 343 /** 344 * Inform the notification manager about dismissal of a single notification. 345 * <p> 346 * Use this if your listener has a user interface that allows the user to dismiss individual 347 * notifications, similar to the behavior of Android's status bar and notification panel. 348 * It should be called after the user dismisses a single notification using your UI; 349 * upon being informed, the notification manager will actually remove the notification 350 * and you will get an {@link #onNotificationRemoved(StatusBarNotification)} callback. 351 * <p> 352 * <b>Note:</b> If your listener allows the user to fire a notification's 353 * {@link android.app.Notification#contentIntent} by tapping/clicking/etc., you should call 354 * this method at that time <i>if</i> the Notification in question has the 355 * {@link android.app.Notification#FLAG_AUTO_CANCEL} flag set. 356 * 357 * <p>The service should wait for the {@link #onListenerConnected()} event 358 * before performing this operation. 359 * 360 * @param pkg Package of the notifying app. 361 * @param tag Tag of the notification as specified by the notifying app in 362 * {@link android.app.NotificationManager#notify(String, int, android.app.Notification)}. 363 * @param id ID of the notification as specified by the notifying app in 364 * {@link android.app.NotificationManager#notify(String, int, android.app.Notification)}. 365 * <p> 366 * @deprecated Use {@link #cancelNotification(String key)} 367 * instead. Beginning with {@link android.os.Build.VERSION_CODES#LOLLIPOP} this method will no longer 368 * cancel the notification. It will continue to cancel the notification for applications 369 * whose {@code targetSdkVersion} is earlier than {@link android.os.Build.VERSION_CODES#LOLLIPOP}. 370 */ 371 public final void cancelNotification(String pkg, String tag, int id) { 372 if (!isBound()) return; 373 try { 374 getNotificationInterface().cancelNotificationFromListener( 375 mWrapper, pkg, tag, id); 376 } catch (android.os.RemoteException ex) { 377 Log.v(TAG, "Unable to contact notification manager", ex); 378 } 379 } 380 381 /** 382 * Inform the notification manager about dismissal of a single notification. 383 * <p> 384 * Use this if your listener has a user interface that allows the user to dismiss individual 385 * notifications, similar to the behavior of Android's status bar and notification panel. 386 * It should be called after the user dismisses a single notification using your UI; 387 * upon being informed, the notification manager will actually remove the notification 388 * and you will get an {@link #onNotificationRemoved(StatusBarNotification)} callback. 389 * <p> 390 * <b>Note:</b> If your listener allows the user to fire a notification's 391 * {@link android.app.Notification#contentIntent} by tapping/clicking/etc., you should call 392 * this method at that time <i>if</i> the Notification in question has the 393 * {@link android.app.Notification#FLAG_AUTO_CANCEL} flag set. 394 * <p> 395 * 396 * <p>The service should wait for the {@link #onListenerConnected()} event 397 * before performing this operation. 398 * 399 * @param key Notification to dismiss from {@link StatusBarNotification#getKey()}. 400 */ 401 public final void cancelNotification(String key) { 402 if (!isBound()) return; 403 try { 404 getNotificationInterface().cancelNotificationsFromListener(mWrapper, 405 new String[] { key }); 406 } catch (android.os.RemoteException ex) { 407 Log.v(TAG, "Unable to contact notification manager", ex); 408 } 409 } 410 411 /** 412 * Inform the notification manager about dismissal of all notifications. 413 * <p> 414 * Use this if your listener has a user interface that allows the user to dismiss all 415 * notifications, similar to the behavior of Android's status bar and notification panel. 416 * It should be called after the user invokes the "dismiss all" function of your UI; 417 * upon being informed, the notification manager will actually remove all active notifications 418 * and you will get multiple {@link #onNotificationRemoved(StatusBarNotification)} callbacks. 419 * 420 * <p>The service should wait for the {@link #onListenerConnected()} event 421 * before performing this operation. 422 * 423 * {@see #cancelNotification(String, String, int)} 424 */ 425 public final void cancelAllNotifications() { 426 cancelNotifications(null /*all*/); 427 } 428 429 /** 430 * Inform the notification manager about dismissal of specific notifications. 431 * <p> 432 * Use this if your listener has a user interface that allows the user to dismiss 433 * multiple notifications at once. 434 * 435 * <p>The service should wait for the {@link #onListenerConnected()} event 436 * before performing this operation. 437 * 438 * @param keys Notifications to dismiss, or {@code null} to dismiss all. 439 * 440 * {@see #cancelNotification(String, String, int)} 441 */ 442 public final void cancelNotifications(String[] keys) { 443 if (!isBound()) return; 444 try { 445 getNotificationInterface().cancelNotificationsFromListener(mWrapper, keys); 446 } catch (android.os.RemoteException ex) { 447 Log.v(TAG, "Unable to contact notification manager", ex); 448 } 449 } 450 451 /** 452 * Inform the notification manager that these notifications have been viewed by the 453 * user. This should only be called when there is sufficient confidence that the user is 454 * looking at the notifications, such as when the notifications appear on the screen due to 455 * an explicit user interaction. 456 * 457 * <p>The service should wait for the {@link #onListenerConnected()} event 458 * before performing this operation. 459 * 460 * @param keys Notifications to mark as seen. 461 */ 462 public final void setNotificationsShown(String[] keys) { 463 if (!isBound()) return; 464 try { 465 getNotificationInterface().setNotificationsShownFromListener(mWrapper, keys); 466 } catch (android.os.RemoteException ex) { 467 Log.v(TAG, "Unable to contact notification manager", ex); 468 } 469 } 470 471 /** 472 * Sets the notification trim that will be received via {@link #onNotificationPosted}. 473 * 474 * <p> 475 * Setting a trim other than {@link #TRIM_FULL} enables listeners that don't need access to the 476 * full notification features right away to reduce their memory footprint. Full notifications 477 * can be requested on-demand via {@link #getActiveNotifications(int)}. 478 * 479 * <p> 480 * Set to {@link #TRIM_FULL} initially. 481 * 482 * <p>The service should wait for the {@link #onListenerConnected()} event 483 * before performing this operation. 484 * 485 * @hide 486 * 487 * @param trim trim of the notifications to be passed via {@link #onNotificationPosted}. 488 * See <code>TRIM_*</code> constants. 489 */ 490 @SystemApi 491 public final void setOnNotificationPostedTrim(int trim) { 492 if (!isBound()) return; 493 try { 494 getNotificationInterface().setOnNotificationPostedTrimFromListener(mWrapper, trim); 495 } catch (RemoteException ex) { 496 Log.v(TAG, "Unable to contact notification manager", ex); 497 } 498 } 499 500 /** 501 * Request the list of outstanding notifications (that is, those that are visible to the 502 * current user). Useful when you don't know what's already been posted. 503 * 504 * <p>The service should wait for the {@link #onListenerConnected()} event 505 * before performing this operation. 506 * 507 * @return An array of active notifications, sorted in natural order. 508 */ 509 public StatusBarNotification[] getActiveNotifications() { 510 return getActiveNotifications(null, TRIM_FULL); 511 } 512 513 /** 514 * Request the list of outstanding notifications (that is, those that are visible to the 515 * current user). Useful when you don't know what's already been posted. 516 * 517 * @hide 518 * 519 * @param trim trim of the notifications to be returned. See <code>TRIM_*</code> constants. 520 * @return An array of active notifications, sorted in natural order. 521 */ 522 @SystemApi 523 public StatusBarNotification[] getActiveNotifications(int trim) { 524 return getActiveNotifications(null, trim); 525 } 526 527 /** 528 * Request one or more notifications by key. Useful if you have been keeping track of 529 * notifications but didn't want to retain the bits, and now need to go back and extract 530 * more data out of those notifications. 531 * 532 * <p>The service should wait for the {@link #onListenerConnected()} event 533 * before performing this operation. 534 * 535 * @param keys the keys of the notifications to request 536 * @return An array of notifications corresponding to the requested keys, in the 537 * same order as the key list. 538 */ 539 public StatusBarNotification[] getActiveNotifications(String[] keys) { 540 return getActiveNotifications(keys, TRIM_FULL); 541 } 542 543 /** 544 * Request one or more notifications by key. Useful if you have been keeping track of 545 * notifications but didn't want to retain the bits, and now need to go back and extract 546 * more data out of those notifications. 547 * 548 * @hide 549 * 550 * @param keys the keys of the notifications to request 551 * @param trim trim of the notifications to be returned. See <code>TRIM_*</code> constants. 552 * @return An array of notifications corresponding to the requested keys, in the 553 * same order as the key list. 554 */ 555 @SystemApi 556 public StatusBarNotification[] getActiveNotifications(String[] keys, int trim) { 557 if (!isBound()) 558 return null; 559 try { 560 ParceledListSlice<StatusBarNotification> parceledList = getNotificationInterface() 561 .getActiveNotificationsFromListener(mWrapper, keys, trim); 562 List<StatusBarNotification> list = parceledList.getList(); 563 ArrayList<StatusBarNotification> corruptNotifications = null; 564 int N = list.size(); 565 for (int i = 0; i < N; i++) { 566 StatusBarNotification sbn = list.get(i); 567 Notification notification = sbn.getNotification(); 568 try { 569 // convert icon metadata to legacy format for older clients 570 createLegacyIconExtras(notification); 571 // populate remote views for older clients. 572 maybePopulateRemoteViews(notification); 573 } catch (IllegalArgumentException e) { 574 if (corruptNotifications == null) { 575 corruptNotifications = new ArrayList<>(N); 576 } 577 corruptNotifications.add(sbn); 578 Log.w(TAG, "onNotificationPosted: can't rebuild notification from " + 579 sbn.getPackageName()); 580 } 581 } 582 if (corruptNotifications != null) { 583 list.removeAll(corruptNotifications); 584 } 585 return list.toArray(new StatusBarNotification[list.size()]); 586 } catch (android.os.RemoteException ex) { 587 Log.v(TAG, "Unable to contact notification manager", ex); 588 } 589 return null; 590 } 591 592 /** 593 * Gets the set of hints representing current state. 594 * 595 * <p> 596 * The current state may differ from the requested state if the hint represents state 597 * shared across all listeners or a feature the notification host does not support or refuses 598 * to grant. 599 * 600 * <p>The service should wait for the {@link #onListenerConnected()} event 601 * before performing this operation. 602 * 603 * @return Zero or more of the HINT_ constants. 604 */ 605 public final int getCurrentListenerHints() { 606 if (!isBound()) return 0; 607 try { 608 return getNotificationInterface().getHintsFromListener(mWrapper); 609 } catch (android.os.RemoteException ex) { 610 Log.v(TAG, "Unable to contact notification manager", ex); 611 return 0; 612 } 613 } 614 615 /** 616 * Gets the current notification interruption filter active on the host. 617 * 618 * <p> 619 * The interruption filter defines which notifications are allowed to interrupt the user 620 * (e.g. via sound & vibration) and is applied globally. Listeners can find out whether 621 * a specific notification matched the interruption filter via 622 * {@link Ranking#matchesInterruptionFilter()}. 623 * <p> 624 * The current filter may differ from the previously requested filter if the notification host 625 * does not support or refuses to apply the requested filter, or if another component changed 626 * the filter in the meantime. 627 * <p> 628 * Listen for updates using {@link #onInterruptionFilterChanged(int)}. 629 * 630 * <p>The service should wait for the {@link #onListenerConnected()} event 631 * before performing this operation. 632 * 633 * @return One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when 634 * unavailable. 635 */ 636 public final int getCurrentInterruptionFilter() { 637 if (!isBound()) return INTERRUPTION_FILTER_UNKNOWN; 638 try { 639 return getNotificationInterface().getInterruptionFilterFromListener(mWrapper); 640 } catch (android.os.RemoteException ex) { 641 Log.v(TAG, "Unable to contact notification manager", ex); 642 return INTERRUPTION_FILTER_UNKNOWN; 643 } 644 } 645 646 /** 647 * Sets the desired {@link #getCurrentListenerHints() listener hints}. 648 * 649 * <p> 650 * This is merely a request, the host may or may not choose to take action depending 651 * on other listener requests or other global state. 652 * <p> 653 * Listen for updates using {@link #onListenerHintsChanged(int)}. 654 * 655 * <p>The service should wait for the {@link #onListenerConnected()} event 656 * before performing this operation. 657 * 658 * @param hints One or more of the HINT_ constants. 659 */ 660 public final void requestListenerHints(int hints) { 661 if (!isBound()) return; 662 try { 663 getNotificationInterface().requestHintsFromListener(mWrapper, hints); 664 } catch (android.os.RemoteException ex) { 665 Log.v(TAG, "Unable to contact notification manager", ex); 666 } 667 } 668 669 /** 670 * Sets the desired {@link #getCurrentInterruptionFilter() interruption filter}. 671 * 672 * <p> 673 * This is merely a request, the host may or may not choose to apply the requested 674 * interruption filter depending on other listener requests or other global state. 675 * <p> 676 * Listen for updates using {@link #onInterruptionFilterChanged(int)}. 677 * 678 * <p>The service should wait for the {@link #onListenerConnected()} event 679 * before performing this operation. 680 * 681 * @param interruptionFilter One of the INTERRUPTION_FILTER_ constants. 682 */ 683 public final void requestInterruptionFilter(int interruptionFilter) { 684 if (!isBound()) return; 685 try { 686 getNotificationInterface() 687 .requestInterruptionFilterFromListener(mWrapper, interruptionFilter); 688 } catch (android.os.RemoteException ex) { 689 Log.v(TAG, "Unable to contact notification manager", ex); 690 } 691 } 692 693 /** 694 * Returns current ranking information. 695 * 696 * <p> 697 * The returned object represents the current ranking snapshot and only 698 * applies for currently active notifications. 699 * <p> 700 * Generally you should use the RankingMap that is passed with events such 701 * as {@link #onNotificationPosted(StatusBarNotification, RankingMap)}, 702 * {@link #onNotificationRemoved(StatusBarNotification, RankingMap)}, and 703 * so on. This method should only be used when needing access outside of 704 * such events, for example to retrieve the RankingMap right after 705 * initialization. 706 * 707 * <p>The service should wait for the {@link #onListenerConnected()} event 708 * before performing this operation. 709 * 710 * @return A {@link RankingMap} object providing access to ranking information 711 */ 712 public RankingMap getCurrentRanking() { 713 synchronized (mLock) { 714 return mRankingMap; 715 } 716 } 717 718 /** 719 * This is not the lifecycle event you are looking for. 720 * 721 * <p>The service should wait for the {@link #onListenerConnected()} event 722 * before performing any operations. 723 */ 724 @Override 725 public IBinder onBind(Intent intent) { 726 if (mWrapper == null) { 727 mWrapper = new NotificationListenerWrapper(); 728 } 729 return mWrapper; 730 } 731 732 /** @hide */ 733 protected boolean isBound() { 734 if (mWrapper == null) { 735 Log.w(TAG, "Notification listener service not yet bound."); 736 return false; 737 } 738 return true; 739 } 740 741 @Override 742 public void onDestroy() { 743 onListenerDisconnected(); 744 super.onDestroy(); 745 } 746 747 /** 748 * Directly register this service with the Notification Manager. 749 * 750 * <p>Only system services may use this call. It will fail for non-system callers. 751 * Apps should ask the user to add their listener in Settings. 752 * 753 * @param context Context required for accessing resources. Since this service isn't 754 * launched as a real Service when using this method, a context has to be passed in. 755 * @param componentName the component that will consume the notification information 756 * @param currentUser the user to use as the stream filter 757 * @hide 758 */ 759 @SystemApi 760 public void registerAsSystemService(Context context, ComponentName componentName, 761 int currentUser) throws RemoteException { 762 if (mWrapper == null) { 763 mWrapper = new NotificationListenerWrapper(); 764 } 765 mSystemContext = context; 766 INotificationManager noMan = getNotificationInterface(); 767 mHandler = new MyHandler(context.getMainLooper()); 768 mCurrentUser = currentUser; 769 noMan.registerListener(mWrapper, componentName, currentUser); 770 } 771 772 /** 773 * Directly unregister this service from the Notification Manager. 774 * 775 * <p>This method will fail for listeners that were not registered 776 * with (@link registerAsService). 777 * @hide 778 */ 779 @SystemApi 780 public void unregisterAsSystemService() throws RemoteException { 781 if (mWrapper != null) { 782 INotificationManager noMan = getNotificationInterface(); 783 noMan.unregisterListener(mWrapper, mCurrentUser); 784 } 785 } 786 787 /** 788 * Request that the listener be rebound, after a previous call to (@link requestUnbind). 789 * 790 * <p>This method will fail for listeners that have 791 * not been granted the permission by the user. 792 */ 793 public static void requestRebind(ComponentName componentName) 794 throws RemoteException { 795 INotificationManager noMan = INotificationManager.Stub.asInterface( 796 ServiceManager.getService(Context.NOTIFICATION_SERVICE)); 797 noMan.requestBindListener(componentName); 798 } 799 800 /** 801 * Request that the service be unbound. 802 * 803 * <p>This will no longer receive updates until 804 * {@link #requestRebind(ComponentName)} is called. 805 * The service will likely be kiled by the system after this call. 806 * 807 * <p>The service should wait for the {@link #onListenerConnected()} event 808 * before performing this operation. I know it's tempting, but you must wait. 809 */ 810 public final void requestUnbind() throws RemoteException { 811 if (mWrapper != null) { 812 INotificationManager noMan = getNotificationInterface(); 813 noMan.requestUnbindListener(mWrapper); 814 // Disable future messages. 815 isConnected = false; 816 } 817 } 818 819 /** Convert new-style Icons to legacy representations for pre-M clients. */ 820 private void createLegacyIconExtras(Notification n) { 821 Icon smallIcon = n.getSmallIcon(); 822 Icon largeIcon = n.getLargeIcon(); 823 if (smallIcon != null && smallIcon.getType() == Icon.TYPE_RESOURCE) { 824 n.extras.putInt(Notification.EXTRA_SMALL_ICON, smallIcon.getResId()); 825 n.icon = smallIcon.getResId(); 826 } 827 if (largeIcon != null) { 828 Drawable d = largeIcon.loadDrawable(getContext()); 829 if (d != null && d instanceof BitmapDrawable) { 830 final Bitmap largeIconBits = ((BitmapDrawable) d).getBitmap(); 831 n.extras.putParcelable(Notification.EXTRA_LARGE_ICON, largeIconBits); 832 n.largeIcon = largeIconBits; 833 } 834 } 835 } 836 837 /** 838 * Populates remote views for pre-N targeting apps. 839 */ 840 private void maybePopulateRemoteViews(Notification notification) { 841 if (getContext().getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) { 842 Builder builder = Builder.recoverBuilder(getContext(), notification); 843 844 // Some styles wrap Notification's contentView, bigContentView and headsUpContentView. 845 // First inflate them all, only then set them to avoid recursive wrapping. 846 RemoteViews content = builder.createContentView(); 847 RemoteViews big = builder.createBigContentView(); 848 RemoteViews headsUp = builder.createHeadsUpContentView(); 849 850 notification.contentView = content; 851 notification.bigContentView = big; 852 notification.headsUpContentView = headsUp; 853 } 854 } 855 856 /** @hide */ 857 protected class NotificationListenerWrapper extends INotificationListener.Stub { 858 @Override 859 public void onNotificationPosted(IStatusBarNotificationHolder sbnHolder, 860 NotificationRankingUpdate update) { 861 StatusBarNotification sbn; 862 try { 863 sbn = sbnHolder.get(); 864 } catch (RemoteException e) { 865 Log.w(TAG, "onNotificationPosted: Error receiving StatusBarNotification", e); 866 return; 867 } 868 869 try { 870 // convert icon metadata to legacy format for older clients 871 createLegacyIconExtras(sbn.getNotification()); 872 maybePopulateRemoteViews(sbn.getNotification()); 873 } catch (IllegalArgumentException e) { 874 // warn and drop corrupt notification 875 Log.w(TAG, "onNotificationPosted: can't rebuild notification from " + 876 sbn.getPackageName()); 877 sbn = null; 878 } 879 880 // protect subclass from concurrent modifications of (@link mNotificationKeys}. 881 synchronized (mLock) { 882 applyUpdateLocked(update); 883 if (sbn != null) { 884 SomeArgs args = SomeArgs.obtain(); 885 args.arg1 = sbn; 886 args.arg2 = mRankingMap; 887 mHandler.obtainMessage(MyHandler.MSG_ON_NOTIFICATION_POSTED, 888 args).sendToTarget(); 889 } else { 890 // still pass along the ranking map, it may contain other information 891 mHandler.obtainMessage(MyHandler.MSG_ON_NOTIFICATION_RANKING_UPDATE, 892 mRankingMap).sendToTarget(); 893 } 894 } 895 896 } 897 898 @Override 899 public void onNotificationRemoved(IStatusBarNotificationHolder sbnHolder, 900 NotificationRankingUpdate update) { 901 StatusBarNotification sbn; 902 try { 903 sbn = sbnHolder.get(); 904 } catch (RemoteException e) { 905 Log.w(TAG, "onNotificationRemoved: Error receiving StatusBarNotification", e); 906 return; 907 } 908 // protect subclass from concurrent modifications of (@link mNotificationKeys}. 909 synchronized (mLock) { 910 applyUpdateLocked(update); 911 SomeArgs args = SomeArgs.obtain(); 912 args.arg1 = sbn; 913 args.arg2 = mRankingMap; 914 mHandler.obtainMessage(MyHandler.MSG_ON_NOTIFICATION_REMOVED, 915 args).sendToTarget(); 916 } 917 918 } 919 920 @Override 921 public void onListenerConnected(NotificationRankingUpdate update) { 922 // protect subclass from concurrent modifications of (@link mNotificationKeys}. 923 synchronized (mLock) { 924 applyUpdateLocked(update); 925 } 926 isConnected = true; 927 mHandler.obtainMessage(MyHandler.MSG_ON_LISTENER_CONNECTED).sendToTarget(); 928 } 929 930 @Override 931 public void onNotificationRankingUpdate(NotificationRankingUpdate update) 932 throws RemoteException { 933 // protect subclass from concurrent modifications of (@link mNotificationKeys}. 934 synchronized (mLock) { 935 applyUpdateLocked(update); 936 mHandler.obtainMessage(MyHandler.MSG_ON_NOTIFICATION_RANKING_UPDATE, 937 mRankingMap).sendToTarget(); 938 } 939 940 } 941 942 @Override 943 public void onListenerHintsChanged(int hints) throws RemoteException { 944 mHandler.obtainMessage(MyHandler.MSG_ON_LISTENER_HINTS_CHANGED, 945 hints, 0).sendToTarget(); 946 } 947 948 @Override 949 public void onInterruptionFilterChanged(int interruptionFilter) throws RemoteException { 950 mHandler.obtainMessage(MyHandler.MSG_ON_INTERRUPTION_FILTER_CHANGED, 951 interruptionFilter, 0).sendToTarget(); 952 } 953 954 @Override 955 public void onNotificationEnqueued(IStatusBarNotificationHolder notificationHolder, 956 int importance, boolean user) throws RemoteException { 957 // no-op in the listener 958 } 959 960 @Override 961 public void onNotificationVisibilityChanged(String key, long time, boolean visible) 962 throws RemoteException { 963 // no-op in the listener 964 } 965 966 @Override 967 public void onNotificationClick(String key, long time) throws RemoteException { 968 // no-op in the listener 969 } 970 971 @Override 972 public void onNotificationActionClick(String key, long time, int actionIndex) 973 throws RemoteException { 974 // no-op in the listener 975 } 976 977 @Override 978 public void onNotificationRemovedReason(String key, long time, int reason) 979 throws RemoteException { 980 // no-op in the listener 981 } 982 } 983 984 private void applyUpdateLocked(NotificationRankingUpdate update) { 985 mRankingMap = new RankingMap(update); 986 } 987 988 /** @hide */ 989 protected Context getContext() { 990 if (mSystemContext != null) { 991 return mSystemContext; 992 } 993 return this; 994 } 995 996 /** 997 * Stores ranking related information on a currently active notification. 998 * 999 * <p> 1000 * Ranking objects aren't automatically updated as notification events 1001 * occur. Instead, ranking information has to be retrieved again via the 1002 * current {@link RankingMap}. 1003 */ 1004 public static class Ranking { 1005 1006 /** @hide */ 1007 @IntDef({VISIBILITY_NO_OVERRIDE, IMPORTANCE_UNSPECIFIED, IMPORTANCE_NONE, 1008 IMPORTANCE_MIN, IMPORTANCE_LOW, IMPORTANCE_DEFAULT, IMPORTANCE_HIGH, 1009 IMPORTANCE_MAX}) 1010 @Retention(RetentionPolicy.SOURCE) 1011 public @interface Importance {} 1012 1013 /** Value signifying that the user has not expressed a per-app visibility override value. 1014 * @hide */ 1015 public static final int VISIBILITY_NO_OVERRIDE = -1000; 1016 1017 /** 1018 * Value signifying that the user has not expressed an importance. 1019 * 1020 * This value is for persisting preferences, and should never be associated with 1021 * an actual notification. 1022 */ 1023 public static final int IMPORTANCE_UNSPECIFIED = -1000; 1024 1025 /** 1026 * A notification with no importance: shows nowhere, is blocked. 1027 */ 1028 public static final int IMPORTANCE_NONE = 0; 1029 1030 /** 1031 * Min notification importance: only shows in the shade, below the fold. 1032 */ 1033 public static final int IMPORTANCE_MIN = 1; 1034 1035 /** 1036 * Low notification importance: shows everywhere, but is not intrusive. 1037 */ 1038 public static final int IMPORTANCE_LOW = 2; 1039 1040 /** 1041 * Default notification importance: shows everywhere, allowed to makes noise, 1042 * but does not visually intrude. 1043 */ 1044 public static final int IMPORTANCE_DEFAULT = 3; 1045 1046 /** 1047 * Higher notification importance: shows everywhere, allowed to makes noise and peek. 1048 */ 1049 public static final int IMPORTANCE_HIGH = 4; 1050 1051 /** 1052 * Highest notification importance: shows everywhere, allowed to makes noise, peek, and 1053 * use full screen intents. 1054 */ 1055 public static final int IMPORTANCE_MAX = 5; 1056 1057 private String mKey; 1058 private int mRank = -1; 1059 private boolean mIsAmbient; 1060 private boolean mMatchesInterruptionFilter; 1061 private int mVisibilityOverride; 1062 private int mSuppressedVisualEffects; 1063 private @Importance int mImportance; 1064 private CharSequence mImportanceExplanation; 1065 // System specified group key. 1066 private String mOverrideGroupKey; 1067 1068 public Ranking() {} 1069 1070 /** 1071 * Returns the key of the notification this Ranking applies to. 1072 */ 1073 public String getKey() { 1074 return mKey; 1075 } 1076 1077 /** 1078 * Returns the rank of the notification. 1079 * 1080 * @return the rank of the notification, that is the 0-based index in 1081 * the list of active notifications. 1082 */ 1083 public int getRank() { 1084 return mRank; 1085 } 1086 1087 /** 1088 * Returns whether the notification is an ambient notification, that is 1089 * a notification that doesn't require the user's immediate attention. 1090 */ 1091 public boolean isAmbient() { 1092 return mIsAmbient; 1093 } 1094 1095 /** 1096 * Returns the user specificed visibility for the package that posted 1097 * this notification, or 1098 * {@link NotificationListenerService.Ranking#VISIBILITY_NO_OVERRIDE} if 1099 * no such preference has been expressed. 1100 * @hide 1101 */ 1102 public int getVisibilityOverride() { 1103 return mVisibilityOverride; 1104 } 1105 1106 /** 1107 * Returns the type(s) of visual effects that should be suppressed for this notification. 1108 * See {@link #SUPPRESSED_EFFECT_SCREEN_OFF}, {@link #SUPPRESSED_EFFECT_SCREEN_ON}. 1109 */ 1110 public int getSuppressedVisualEffects() { 1111 return mSuppressedVisualEffects; 1112 } 1113 1114 /** 1115 * Returns whether the notification matches the user's interruption 1116 * filter. 1117 * 1118 * @return {@code true} if the notification is allowed by the filter, or 1119 * {@code false} if it is blocked. 1120 */ 1121 public boolean matchesInterruptionFilter() { 1122 return mMatchesInterruptionFilter; 1123 } 1124 1125 /** 1126 * Returns the importance of the notification, which dictates its 1127 * modes of presentation, see: {@link #IMPORTANCE_DEFAULT}, etc. 1128 * 1129 * @return the rank of the notification 1130 */ 1131 public @Importance int getImportance() { 1132 return mImportance; 1133 } 1134 1135 /** 1136 * If the importance has been overriden by user preference, then this will be non-null, 1137 * and should be displayed to the user. 1138 * 1139 * @return the explanation for the importance, or null if it is the natural importance 1140 */ 1141 public CharSequence getImportanceExplanation() { 1142 return mImportanceExplanation; 1143 } 1144 1145 /** 1146 * If the system has overriden the group key, then this will be non-null, and this 1147 * key should be used to bundle notifications. 1148 */ 1149 public String getOverrideGroupKey() { 1150 return mOverrideGroupKey; 1151 } 1152 1153 private void populate(String key, int rank, boolean matchesInterruptionFilter, 1154 int visibilityOverride, int suppressedVisualEffects, int importance, 1155 CharSequence explanation, String overrideGroupKey) { 1156 mKey = key; 1157 mRank = rank; 1158 mIsAmbient = importance < IMPORTANCE_LOW; 1159 mMatchesInterruptionFilter = matchesInterruptionFilter; 1160 mVisibilityOverride = visibilityOverride; 1161 mSuppressedVisualEffects = suppressedVisualEffects; 1162 mImportance = importance; 1163 mImportanceExplanation = explanation; 1164 mOverrideGroupKey = overrideGroupKey; 1165 } 1166 1167 /** 1168 * {@hide} 1169 */ 1170 public static String importanceToString(int importance) { 1171 switch (importance) { 1172 case IMPORTANCE_UNSPECIFIED: 1173 return "UNSPECIFIED"; 1174 case IMPORTANCE_NONE: 1175 return "NONE"; 1176 case IMPORTANCE_MIN: 1177 return "MIN"; 1178 case IMPORTANCE_LOW: 1179 return "LOW"; 1180 case IMPORTANCE_DEFAULT: 1181 return "DEFAULT"; 1182 case IMPORTANCE_HIGH: 1183 return "HIGH"; 1184 case IMPORTANCE_MAX: 1185 return "MAX"; 1186 default: 1187 return "UNKNOWN(" + String.valueOf(importance) + ")"; 1188 } 1189 } 1190 } 1191 1192 /** 1193 * Provides access to ranking information on currently active 1194 * notifications. 1195 * 1196 * <p> 1197 * Note that this object represents a ranking snapshot that only applies to 1198 * notifications active at the time of retrieval. 1199 */ 1200 public static class RankingMap implements Parcelable { 1201 private final NotificationRankingUpdate mRankingUpdate; 1202 private ArrayMap<String,Integer> mRanks; 1203 private ArraySet<Object> mIntercepted; 1204 private ArrayMap<String, Integer> mVisibilityOverrides; 1205 private ArrayMap<String, Integer> mSuppressedVisualEffects; 1206 private ArrayMap<String, Integer> mImportance; 1207 private ArrayMap<String, String> mImportanceExplanation; 1208 private ArrayMap<String, String> mOverrideGroupKeys; 1209 1210 private RankingMap(NotificationRankingUpdate rankingUpdate) { 1211 mRankingUpdate = rankingUpdate; 1212 } 1213 1214 /** 1215 * Request the list of notification keys in their current ranking 1216 * order. 1217 * 1218 * @return An array of active notification keys, in their ranking order. 1219 */ 1220 public String[] getOrderedKeys() { 1221 return mRankingUpdate.getOrderedKeys(); 1222 } 1223 1224 /** 1225 * Populates outRanking with ranking information for the notification 1226 * with the given key. 1227 * 1228 * @return true if a valid key has been passed and outRanking has 1229 * been populated; false otherwise 1230 */ 1231 public boolean getRanking(String key, Ranking outRanking) { 1232 int rank = getRank(key); 1233 outRanking.populate(key, rank, !isIntercepted(key), 1234 getVisibilityOverride(key), getSuppressedVisualEffects(key), 1235 getImportance(key), getImportanceExplanation(key), getOverrideGroupKey(key)); 1236 return rank >= 0; 1237 } 1238 1239 private int getRank(String key) { 1240 synchronized (this) { 1241 if (mRanks == null) { 1242 buildRanksLocked(); 1243 } 1244 } 1245 Integer rank = mRanks.get(key); 1246 return rank != null ? rank : -1; 1247 } 1248 1249 private boolean isIntercepted(String key) { 1250 synchronized (this) { 1251 if (mIntercepted == null) { 1252 buildInterceptedSetLocked(); 1253 } 1254 } 1255 return mIntercepted.contains(key); 1256 } 1257 1258 private int getVisibilityOverride(String key) { 1259 synchronized (this) { 1260 if (mVisibilityOverrides == null) { 1261 buildVisibilityOverridesLocked(); 1262 } 1263 } 1264 Integer override = mVisibilityOverrides.get(key); 1265 if (override == null) { 1266 return Ranking.VISIBILITY_NO_OVERRIDE; 1267 } 1268 return override.intValue(); 1269 } 1270 1271 private int getSuppressedVisualEffects(String key) { 1272 synchronized (this) { 1273 if (mSuppressedVisualEffects == null) { 1274 buildSuppressedVisualEffectsLocked(); 1275 } 1276 } 1277 Integer suppressed = mSuppressedVisualEffects.get(key); 1278 if (suppressed == null) { 1279 return 0; 1280 } 1281 return suppressed.intValue(); 1282 } 1283 1284 private int getImportance(String key) { 1285 synchronized (this) { 1286 if (mImportance == null) { 1287 buildImportanceLocked(); 1288 } 1289 } 1290 Integer importance = mImportance.get(key); 1291 if (importance == null) { 1292 return Ranking.IMPORTANCE_DEFAULT; 1293 } 1294 return importance.intValue(); 1295 } 1296 1297 private String getImportanceExplanation(String key) { 1298 synchronized (this) { 1299 if (mImportanceExplanation == null) { 1300 buildImportanceExplanationLocked(); 1301 } 1302 } 1303 return mImportanceExplanation.get(key); 1304 } 1305 1306 private String getOverrideGroupKey(String key) { 1307 synchronized (this) { 1308 if (mOverrideGroupKeys == null) { 1309 buildOverrideGroupKeys(); 1310 } 1311 } 1312 return mOverrideGroupKeys.get(key); 1313 } 1314 1315 // Locked by 'this' 1316 private void buildRanksLocked() { 1317 String[] orderedKeys = mRankingUpdate.getOrderedKeys(); 1318 mRanks = new ArrayMap<>(orderedKeys.length); 1319 for (int i = 0; i < orderedKeys.length; i++) { 1320 String key = orderedKeys[i]; 1321 mRanks.put(key, i); 1322 } 1323 } 1324 1325 // Locked by 'this' 1326 private void buildInterceptedSetLocked() { 1327 String[] dndInterceptedKeys = mRankingUpdate.getInterceptedKeys(); 1328 mIntercepted = new ArraySet<>(dndInterceptedKeys.length); 1329 Collections.addAll(mIntercepted, dndInterceptedKeys); 1330 } 1331 1332 // Locked by 'this' 1333 private void buildVisibilityOverridesLocked() { 1334 Bundle visibilityBundle = mRankingUpdate.getVisibilityOverrides(); 1335 mVisibilityOverrides = new ArrayMap<>(visibilityBundle.size()); 1336 for (String key: visibilityBundle.keySet()) { 1337 mVisibilityOverrides.put(key, visibilityBundle.getInt(key)); 1338 } 1339 } 1340 1341 // Locked by 'this' 1342 private void buildSuppressedVisualEffectsLocked() { 1343 Bundle suppressedBundle = mRankingUpdate.getSuppressedVisualEffects(); 1344 mSuppressedVisualEffects = new ArrayMap<>(suppressedBundle.size()); 1345 for (String key: suppressedBundle.keySet()) { 1346 mSuppressedVisualEffects.put(key, suppressedBundle.getInt(key)); 1347 } 1348 } 1349 // Locked by 'this' 1350 private void buildImportanceLocked() { 1351 String[] orderedKeys = mRankingUpdate.getOrderedKeys(); 1352 int[] importance = mRankingUpdate.getImportance(); 1353 mImportance = new ArrayMap<>(orderedKeys.length); 1354 for (int i = 0; i < orderedKeys.length; i++) { 1355 String key = orderedKeys[i]; 1356 mImportance.put(key, importance[i]); 1357 } 1358 } 1359 1360 // Locked by 'this' 1361 private void buildImportanceExplanationLocked() { 1362 Bundle explanationBundle = mRankingUpdate.getImportanceExplanation(); 1363 mImportanceExplanation = new ArrayMap<>(explanationBundle.size()); 1364 for (String key: explanationBundle.keySet()) { 1365 mImportanceExplanation.put(key, explanationBundle.getString(key)); 1366 } 1367 } 1368 1369 // Locked by 'this' 1370 private void buildOverrideGroupKeys() { 1371 Bundle overrideGroupKeys = mRankingUpdate.getOverrideGroupKeys(); 1372 mOverrideGroupKeys = new ArrayMap<>(overrideGroupKeys.size()); 1373 for (String key: overrideGroupKeys.keySet()) { 1374 mOverrideGroupKeys.put(key, overrideGroupKeys.getString(key)); 1375 } 1376 } 1377 1378 // ----------- Parcelable 1379 1380 @Override 1381 public int describeContents() { 1382 return 0; 1383 } 1384 1385 @Override 1386 public void writeToParcel(Parcel dest, int flags) { 1387 dest.writeParcelable(mRankingUpdate, flags); 1388 } 1389 1390 public static final Creator<RankingMap> CREATOR = new Creator<RankingMap>() { 1391 @Override 1392 public RankingMap createFromParcel(Parcel source) { 1393 NotificationRankingUpdate rankingUpdate = source.readParcelable(null); 1394 return new RankingMap(rankingUpdate); 1395 } 1396 1397 @Override 1398 public RankingMap[] newArray(int size) { 1399 return new RankingMap[size]; 1400 } 1401 }; 1402 } 1403 1404 private final class MyHandler extends Handler { 1405 public static final int MSG_ON_NOTIFICATION_POSTED = 1; 1406 public static final int MSG_ON_NOTIFICATION_REMOVED = 2; 1407 public static final int MSG_ON_LISTENER_CONNECTED = 3; 1408 public static final int MSG_ON_NOTIFICATION_RANKING_UPDATE = 4; 1409 public static final int MSG_ON_LISTENER_HINTS_CHANGED = 5; 1410 public static final int MSG_ON_INTERRUPTION_FILTER_CHANGED = 6; 1411 1412 public MyHandler(Looper looper) { 1413 super(looper, null, false); 1414 } 1415 1416 @Override 1417 public void handleMessage(Message msg) { 1418 if (!isConnected) { 1419 return; 1420 } 1421 switch (msg.what) { 1422 case MSG_ON_NOTIFICATION_POSTED: { 1423 SomeArgs args = (SomeArgs) msg.obj; 1424 StatusBarNotification sbn = (StatusBarNotification) args.arg1; 1425 RankingMap rankingMap = (RankingMap) args.arg2; 1426 args.recycle(); 1427 onNotificationPosted(sbn, rankingMap); 1428 } break; 1429 1430 case MSG_ON_NOTIFICATION_REMOVED: { 1431 SomeArgs args = (SomeArgs) msg.obj; 1432 StatusBarNotification sbn = (StatusBarNotification) args.arg1; 1433 RankingMap rankingMap = (RankingMap) args.arg2; 1434 args.recycle(); 1435 onNotificationRemoved(sbn, rankingMap); 1436 } break; 1437 1438 case MSG_ON_LISTENER_CONNECTED: { 1439 onListenerConnected(); 1440 } break; 1441 1442 case MSG_ON_NOTIFICATION_RANKING_UPDATE: { 1443 RankingMap rankingMap = (RankingMap) msg.obj; 1444 onNotificationRankingUpdate(rankingMap); 1445 } break; 1446 1447 case MSG_ON_LISTENER_HINTS_CHANGED: { 1448 final int hints = msg.arg1; 1449 onListenerHintsChanged(hints); 1450 } break; 1451 1452 case MSG_ON_INTERRUPTION_FILTER_CHANGED: { 1453 final int interruptionFilter = msg.arg1; 1454 onInterruptionFilterChanged(interruptionFilter); 1455 } break; 1456 } 1457 } 1458 } 1459} 1460