NotificationListenerService.java revision 5ab5c748a850da558a0bcdf9e680228aae4a7646
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 /** Value signifying that the user has not expressed a per-app visibility override value. 1007 * @hide */ 1008 public static final int VISIBILITY_NO_OVERRIDE = NotificationManager.VISIBILITY_NO_OVERRIDE; 1009 1010 /** 1011 * Value signifying that the user has not expressed an importance. 1012 * 1013 * This value is for persisting preferences, and should never be associated with 1014 * an actual notification. 1015 * 1016 * @hide 1017 */ 1018 public static final int IMPORTANCE_UNSPECIFIED = NotificationManager.IMPORTANCE_UNSPECIFIED; 1019 1020 /** 1021 * A notification with no importance: shows nowhere, is blocked. 1022 * 1023 * @hide 1024 */ 1025 public static final int IMPORTANCE_NONE = NotificationManager.IMPORTANCE_NONE; 1026 1027 /** 1028 * Min notification importance: only shows in the shade, below the fold. 1029 * 1030 * @hide 1031 */ 1032 public static final int IMPORTANCE_MIN = NotificationManager.IMPORTANCE_MIN; 1033 1034 /** 1035 * Low notification importance: shows everywhere, but is not intrusive. 1036 * 1037 * @hide 1038 */ 1039 public static final int IMPORTANCE_LOW = NotificationManager.IMPORTANCE_LOW; 1040 1041 /** 1042 * Default notification importance: shows everywhere, allowed to makes noise, 1043 * but does not visually intrude. 1044 * 1045 * @hide 1046 */ 1047 public static final int IMPORTANCE_DEFAULT = NotificationManager.IMPORTANCE_DEFAULT; 1048 1049 /** 1050 * Higher notification importance: shows everywhere, allowed to makes noise and peek. 1051 * 1052 * @hide 1053 */ 1054 public static final int IMPORTANCE_HIGH = NotificationManager.IMPORTANCE_HIGH; 1055 1056 /** 1057 * Highest notification importance: shows everywhere, allowed to makes noise, peek, and 1058 * use full screen intents. 1059 * 1060 * @hide 1061 */ 1062 public static final int IMPORTANCE_MAX = NotificationManager.IMPORTANCE_MAX; 1063 1064 private String mKey; 1065 private int mRank = -1; 1066 private boolean mIsAmbient; 1067 private boolean mMatchesInterruptionFilter; 1068 private int mVisibilityOverride; 1069 private int mSuppressedVisualEffects; 1070 private @NotificationManager.Importance int mImportance; 1071 private CharSequence mImportanceExplanation; 1072 // System specified group key. 1073 private String mOverrideGroupKey; 1074 1075 public Ranking() {} 1076 1077 /** 1078 * Returns the key of the notification this Ranking applies to. 1079 */ 1080 public String getKey() { 1081 return mKey; 1082 } 1083 1084 /** 1085 * Returns the rank of the notification. 1086 * 1087 * @return the rank of the notification, that is the 0-based index in 1088 * the list of active notifications. 1089 */ 1090 public int getRank() { 1091 return mRank; 1092 } 1093 1094 /** 1095 * Returns whether the notification is an ambient notification, that is 1096 * a notification that doesn't require the user's immediate attention. 1097 */ 1098 public boolean isAmbient() { 1099 return mIsAmbient; 1100 } 1101 1102 /** 1103 * Returns the user specificed visibility for the package that posted 1104 * this notification, or 1105 * {@link NotificationListenerService.Ranking#VISIBILITY_NO_OVERRIDE} if 1106 * no such preference has been expressed. 1107 * @hide 1108 */ 1109 public int getVisibilityOverride() { 1110 return mVisibilityOverride; 1111 } 1112 1113 /** 1114 * Returns the type(s) of visual effects that should be suppressed for this notification. 1115 * See {@link #SUPPRESSED_EFFECT_SCREEN_OFF}, {@link #SUPPRESSED_EFFECT_SCREEN_ON}. 1116 */ 1117 public int getSuppressedVisualEffects() { 1118 return mSuppressedVisualEffects; 1119 } 1120 1121 /** 1122 * Returns whether the notification matches the user's interruption 1123 * filter. 1124 * 1125 * @return {@code true} if the notification is allowed by the filter, or 1126 * {@code false} if it is blocked. 1127 */ 1128 public boolean matchesInterruptionFilter() { 1129 return mMatchesInterruptionFilter; 1130 } 1131 1132 /** 1133 * Returns the importance of the notification, which dictates its 1134 * modes of presentation, see: {@link NotificationManager#IMPORTANCE_DEFAULT}, etc. 1135 * 1136 * @return the rank of the notification 1137 */ 1138 public @NotificationManager.Importance int getImportance() { 1139 return mImportance; 1140 } 1141 1142 /** 1143 * If the importance has been overriden by user preference, then this will be non-null, 1144 * and should be displayed to the user. 1145 * 1146 * @return the explanation for the importance, or null if it is the natural importance 1147 */ 1148 public CharSequence getImportanceExplanation() { 1149 return mImportanceExplanation; 1150 } 1151 1152 /** 1153 * If the system has overriden the group key, then this will be non-null, and this 1154 * key should be used to bundle notifications. 1155 */ 1156 public String getOverrideGroupKey() { 1157 return mOverrideGroupKey; 1158 } 1159 1160 private void populate(String key, int rank, boolean matchesInterruptionFilter, 1161 int visibilityOverride, int suppressedVisualEffects, int importance, 1162 CharSequence explanation, String overrideGroupKey) { 1163 mKey = key; 1164 mRank = rank; 1165 mIsAmbient = importance < IMPORTANCE_LOW; 1166 mMatchesInterruptionFilter = matchesInterruptionFilter; 1167 mVisibilityOverride = visibilityOverride; 1168 mSuppressedVisualEffects = suppressedVisualEffects; 1169 mImportance = importance; 1170 mImportanceExplanation = explanation; 1171 mOverrideGroupKey = overrideGroupKey; 1172 } 1173 1174 /** 1175 * {@hide} 1176 */ 1177 public static String importanceToString(int importance) { 1178 switch (importance) { 1179 case IMPORTANCE_UNSPECIFIED: 1180 return "UNSPECIFIED"; 1181 case IMPORTANCE_NONE: 1182 return "NONE"; 1183 case IMPORTANCE_MIN: 1184 return "MIN"; 1185 case IMPORTANCE_LOW: 1186 return "LOW"; 1187 case IMPORTANCE_DEFAULT: 1188 return "DEFAULT"; 1189 case IMPORTANCE_HIGH: 1190 return "HIGH"; 1191 case IMPORTANCE_MAX: 1192 return "MAX"; 1193 default: 1194 return "UNKNOWN(" + String.valueOf(importance) + ")"; 1195 } 1196 } 1197 } 1198 1199 /** 1200 * Provides access to ranking information on currently active 1201 * notifications. 1202 * 1203 * <p> 1204 * Note that this object represents a ranking snapshot that only applies to 1205 * notifications active at the time of retrieval. 1206 */ 1207 public static class RankingMap implements Parcelable { 1208 private final NotificationRankingUpdate mRankingUpdate; 1209 private ArrayMap<String,Integer> mRanks; 1210 private ArraySet<Object> mIntercepted; 1211 private ArrayMap<String, Integer> mVisibilityOverrides; 1212 private ArrayMap<String, Integer> mSuppressedVisualEffects; 1213 private ArrayMap<String, Integer> mImportance; 1214 private ArrayMap<String, String> mImportanceExplanation; 1215 private ArrayMap<String, String> mOverrideGroupKeys; 1216 1217 private RankingMap(NotificationRankingUpdate rankingUpdate) { 1218 mRankingUpdate = rankingUpdate; 1219 } 1220 1221 /** 1222 * Request the list of notification keys in their current ranking 1223 * order. 1224 * 1225 * @return An array of active notification keys, in their ranking order. 1226 */ 1227 public String[] getOrderedKeys() { 1228 return mRankingUpdate.getOrderedKeys(); 1229 } 1230 1231 /** 1232 * Populates outRanking with ranking information for the notification 1233 * with the given key. 1234 * 1235 * @return true if a valid key has been passed and outRanking has 1236 * been populated; false otherwise 1237 */ 1238 public boolean getRanking(String key, Ranking outRanking) { 1239 int rank = getRank(key); 1240 outRanking.populate(key, rank, !isIntercepted(key), 1241 getVisibilityOverride(key), getSuppressedVisualEffects(key), 1242 getImportance(key), getImportanceExplanation(key), getOverrideGroupKey(key)); 1243 return rank >= 0; 1244 } 1245 1246 private int getRank(String key) { 1247 synchronized (this) { 1248 if (mRanks == null) { 1249 buildRanksLocked(); 1250 } 1251 } 1252 Integer rank = mRanks.get(key); 1253 return rank != null ? rank : -1; 1254 } 1255 1256 private boolean isIntercepted(String key) { 1257 synchronized (this) { 1258 if (mIntercepted == null) { 1259 buildInterceptedSetLocked(); 1260 } 1261 } 1262 return mIntercepted.contains(key); 1263 } 1264 1265 private int getVisibilityOverride(String key) { 1266 synchronized (this) { 1267 if (mVisibilityOverrides == null) { 1268 buildVisibilityOverridesLocked(); 1269 } 1270 } 1271 Integer override = mVisibilityOverrides.get(key); 1272 if (override == null) { 1273 return Ranking.VISIBILITY_NO_OVERRIDE; 1274 } 1275 return override.intValue(); 1276 } 1277 1278 private int getSuppressedVisualEffects(String key) { 1279 synchronized (this) { 1280 if (mSuppressedVisualEffects == null) { 1281 buildSuppressedVisualEffectsLocked(); 1282 } 1283 } 1284 Integer suppressed = mSuppressedVisualEffects.get(key); 1285 if (suppressed == null) { 1286 return 0; 1287 } 1288 return suppressed.intValue(); 1289 } 1290 1291 private int getImportance(String key) { 1292 synchronized (this) { 1293 if (mImportance == null) { 1294 buildImportanceLocked(); 1295 } 1296 } 1297 Integer importance = mImportance.get(key); 1298 if (importance == null) { 1299 return Ranking.IMPORTANCE_DEFAULT; 1300 } 1301 return importance.intValue(); 1302 } 1303 1304 private String getImportanceExplanation(String key) { 1305 synchronized (this) { 1306 if (mImportanceExplanation == null) { 1307 buildImportanceExplanationLocked(); 1308 } 1309 } 1310 return mImportanceExplanation.get(key); 1311 } 1312 1313 private String getOverrideGroupKey(String key) { 1314 synchronized (this) { 1315 if (mOverrideGroupKeys == null) { 1316 buildOverrideGroupKeys(); 1317 } 1318 } 1319 return mOverrideGroupKeys.get(key); 1320 } 1321 1322 // Locked by 'this' 1323 private void buildRanksLocked() { 1324 String[] orderedKeys = mRankingUpdate.getOrderedKeys(); 1325 mRanks = new ArrayMap<>(orderedKeys.length); 1326 for (int i = 0; i < orderedKeys.length; i++) { 1327 String key = orderedKeys[i]; 1328 mRanks.put(key, i); 1329 } 1330 } 1331 1332 // Locked by 'this' 1333 private void buildInterceptedSetLocked() { 1334 String[] dndInterceptedKeys = mRankingUpdate.getInterceptedKeys(); 1335 mIntercepted = new ArraySet<>(dndInterceptedKeys.length); 1336 Collections.addAll(mIntercepted, dndInterceptedKeys); 1337 } 1338 1339 // Locked by 'this' 1340 private void buildVisibilityOverridesLocked() { 1341 Bundle visibilityBundle = mRankingUpdate.getVisibilityOverrides(); 1342 mVisibilityOverrides = new ArrayMap<>(visibilityBundle.size()); 1343 for (String key: visibilityBundle.keySet()) { 1344 mVisibilityOverrides.put(key, visibilityBundle.getInt(key)); 1345 } 1346 } 1347 1348 // Locked by 'this' 1349 private void buildSuppressedVisualEffectsLocked() { 1350 Bundle suppressedBundle = mRankingUpdate.getSuppressedVisualEffects(); 1351 mSuppressedVisualEffects = new ArrayMap<>(suppressedBundle.size()); 1352 for (String key: suppressedBundle.keySet()) { 1353 mSuppressedVisualEffects.put(key, suppressedBundle.getInt(key)); 1354 } 1355 } 1356 // Locked by 'this' 1357 private void buildImportanceLocked() { 1358 String[] orderedKeys = mRankingUpdate.getOrderedKeys(); 1359 int[] importance = mRankingUpdate.getImportance(); 1360 mImportance = new ArrayMap<>(orderedKeys.length); 1361 for (int i = 0; i < orderedKeys.length; i++) { 1362 String key = orderedKeys[i]; 1363 mImportance.put(key, importance[i]); 1364 } 1365 } 1366 1367 // Locked by 'this' 1368 private void buildImportanceExplanationLocked() { 1369 Bundle explanationBundle = mRankingUpdate.getImportanceExplanation(); 1370 mImportanceExplanation = new ArrayMap<>(explanationBundle.size()); 1371 for (String key: explanationBundle.keySet()) { 1372 mImportanceExplanation.put(key, explanationBundle.getString(key)); 1373 } 1374 } 1375 1376 // Locked by 'this' 1377 private void buildOverrideGroupKeys() { 1378 Bundle overrideGroupKeys = mRankingUpdate.getOverrideGroupKeys(); 1379 mOverrideGroupKeys = new ArrayMap<>(overrideGroupKeys.size()); 1380 for (String key: overrideGroupKeys.keySet()) { 1381 mOverrideGroupKeys.put(key, overrideGroupKeys.getString(key)); 1382 } 1383 } 1384 1385 // ----------- Parcelable 1386 1387 @Override 1388 public int describeContents() { 1389 return 0; 1390 } 1391 1392 @Override 1393 public void writeToParcel(Parcel dest, int flags) { 1394 dest.writeParcelable(mRankingUpdate, flags); 1395 } 1396 1397 public static final Creator<RankingMap> CREATOR = new Creator<RankingMap>() { 1398 @Override 1399 public RankingMap createFromParcel(Parcel source) { 1400 NotificationRankingUpdate rankingUpdate = source.readParcelable(null); 1401 return new RankingMap(rankingUpdate); 1402 } 1403 1404 @Override 1405 public RankingMap[] newArray(int size) { 1406 return new RankingMap[size]; 1407 } 1408 }; 1409 } 1410 1411 private final class MyHandler extends Handler { 1412 public static final int MSG_ON_NOTIFICATION_POSTED = 1; 1413 public static final int MSG_ON_NOTIFICATION_REMOVED = 2; 1414 public static final int MSG_ON_LISTENER_CONNECTED = 3; 1415 public static final int MSG_ON_NOTIFICATION_RANKING_UPDATE = 4; 1416 public static final int MSG_ON_LISTENER_HINTS_CHANGED = 5; 1417 public static final int MSG_ON_INTERRUPTION_FILTER_CHANGED = 6; 1418 1419 public MyHandler(Looper looper) { 1420 super(looper, null, false); 1421 } 1422 1423 @Override 1424 public void handleMessage(Message msg) { 1425 if (!isConnected) { 1426 return; 1427 } 1428 switch (msg.what) { 1429 case MSG_ON_NOTIFICATION_POSTED: { 1430 SomeArgs args = (SomeArgs) msg.obj; 1431 StatusBarNotification sbn = (StatusBarNotification) args.arg1; 1432 RankingMap rankingMap = (RankingMap) args.arg2; 1433 args.recycle(); 1434 onNotificationPosted(sbn, rankingMap); 1435 } break; 1436 1437 case MSG_ON_NOTIFICATION_REMOVED: { 1438 SomeArgs args = (SomeArgs) msg.obj; 1439 StatusBarNotification sbn = (StatusBarNotification) args.arg1; 1440 RankingMap rankingMap = (RankingMap) args.arg2; 1441 args.recycle(); 1442 onNotificationRemoved(sbn, rankingMap); 1443 } break; 1444 1445 case MSG_ON_LISTENER_CONNECTED: { 1446 onListenerConnected(); 1447 } break; 1448 1449 case MSG_ON_NOTIFICATION_RANKING_UPDATE: { 1450 RankingMap rankingMap = (RankingMap) msg.obj; 1451 onNotificationRankingUpdate(rankingMap); 1452 } break; 1453 1454 case MSG_ON_LISTENER_HINTS_CHANGED: { 1455 final int hints = msg.arg1; 1456 onListenerHintsChanged(hints); 1457 } break; 1458 1459 case MSG_ON_INTERRUPTION_FILTER_CHANGED: { 1460 final int interruptionFilter = msg.arg1; 1461 onInterruptionFilterChanged(interruptionFilter); 1462 } break; 1463 } 1464 } 1465 } 1466} 1467