2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.service.notification;
19 import android.annotation.SystemApi;
20 import android.annotation.SdkConstant;
21 import android.app.INotificationManager;
22 import android.app.Notification;
23 import android.app.Notification.Builder;
24 import android.app.Service;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.pm.ParceledListSlice;
29 import android.os.Bundle;
30 import android.os.IBinder;
31 import android.os.Parcel;
32 import android.os.Parcelable;
33 import android.os.RemoteException;
34 import android.os.ServiceManager;
35 import android.util.ArrayMap;
36 import android.util.ArraySet;
37 import android.util.Log;
39 import java.util.Collections;
40 import java.util.List;
43 * A service that receives calls from the system when new notifications are
44 * posted or removed, or their ranking changed.
45 * <p>To extend this class, you must declare the service in your manifest file with
46 * the {@link android.Manifest.permission#BIND_NOTIFICATION_LISTENER_SERVICE} permission
47 * and include an intent filter with the {@link #SERVICE_INTERFACE} action. For example:</p>
49 * <service android:name=".NotificationListener"
50 * android:label="@string/service_name"
51 * android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
53 * <action android:name="android.service.notification.NotificationListenerService" />
57 public abstract class NotificationListenerService extends Service {
58 // TAG = "NotificationListenerService[MySubclass]"
59 private final String TAG = NotificationListenerService.class.getSimpleName()
60 + "[" + getClass().getSimpleName() + "]";
63 * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
64 * Normal interruption filter.
66 public static final int INTERRUPTION_FILTER_ALL = 1;
69 * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
70 * Priority interruption filter.
72 public static final int INTERRUPTION_FILTER_PRIORITY = 2;
75 * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
76 * No interruptions filter.
78 public static final int INTERRUPTION_FILTER_NONE = 3;
80 /** {@link #getCurrentListenerHints() Listener hints} constant - the primary device UI
81 * should disable notification sound, vibrating and other visual or aural effects.
82 * This does not change the interruption filter, only the effects. **/
83 public static final int HINT_HOST_DISABLE_EFFECTS = 1;
86 * The full trim of the StatusBarNotification including all its features.
91 public static final int TRIM_FULL = 0;
94 * A light trim of the StatusBarNotification excluding the following features:
97 * <li>{@link Notification#tickerView tickerView}</li>
98 * <li>{@link Notification#contentView contentView}</li>
99 * <li>{@link Notification#largeIcon largeIcon}</li>
100 * <li>{@link Notification#bigContentView bigContentView}</li>
101 * <li>{@link Notification#headsUpContentView headsUpContentView}</li>
102 * <li>{@link Notification#EXTRA_LARGE_ICON extras[EXTRA_LARGE_ICON]}</li>
103 * <li>{@link Notification#EXTRA_LARGE_ICON_BIG extras[EXTRA_LARGE_ICON_BIG]}</li>
104 * <li>{@link Notification#EXTRA_PICTURE extras[EXTRA_PICTURE]}</li>
105 * <li>{@link Notification#EXTRA_BIG_TEXT extras[EXTRA_BIG_TEXT]}</li>
111 public static final int TRIM_LIGHT = 1;
113 private INotificationListenerWrapper mWrapper = null;
114 private RankingMap mRankingMap;
116 private INotificationManager mNoMan;
118 /** Only valid after a successful call to (@link registerAsService}. */
119 private int mCurrentUser;
122 // This context is required for system services since NotificationListenerService isn't
123 // started as a real Service and hence no context is available.
124 private Context mSystemContext;
127 * The {@link Intent} that must be declared as handled by the service.
129 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
130 public static final String SERVICE_INTERFACE
131 = "android.service.notification.NotificationListenerService";
134 * Implement this method to learn about new notifications as they are posted by apps.
136 * @param sbn A data structure encapsulating the original {@link android.app.Notification}
137 * object as well as its identifying information (tag and id) and source
140 public void onNotificationPosted(StatusBarNotification sbn) {
145 * Implement this method to learn about new notifications as they are posted by apps.
147 * @param sbn A data structure encapsulating the original {@link android.app.Notification}
148 * object as well as its identifying information (tag and id) and source
150 * @param rankingMap The current ranking map that can be used to retrieve ranking information
151 * for active notifications, including the newly posted one.
153 public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) {
154 onNotificationPosted(sbn);
158 * Implement this method to learn when notifications are removed.
160 * This might occur because the user has dismissed the notification using system UI (or another
161 * notification listener) or because the app has withdrawn the notification.
163 * NOTE: The {@link StatusBarNotification} object you receive will be "light"; that is, the
164 * result from {@link StatusBarNotification#getNotification} may be missing some heavyweight
165 * fields such as {@link android.app.Notification#contentView} and
166 * {@link android.app.Notification#largeIcon}. However, all other fields on
167 * {@link StatusBarNotification}, sufficient to match this call with a prior call to
168 * {@link #onNotificationPosted(StatusBarNotification)}, will be intact.
170 * @param sbn A data structure encapsulating at least the original information (tag and id)
171 * and source (package name) used to post the {@link android.app.Notification} that
174 public void onNotificationRemoved(StatusBarNotification sbn) {
179 * Implement this method to learn when notifications are removed.
181 * This might occur because the user has dismissed the notification using system UI (or another
182 * notification listener) or because the app has withdrawn the notification.
184 * NOTE: The {@link StatusBarNotification} object you receive will be "light"; that is, the
185 * result from {@link StatusBarNotification#getNotification} may be missing some heavyweight
186 * fields such as {@link android.app.Notification#contentView} and
187 * {@link android.app.Notification#largeIcon}. However, all other fields on
188 * {@link StatusBarNotification}, sufficient to match this call with a prior call to
189 * {@link #onNotificationPosted(StatusBarNotification)}, will be intact.
191 * @param sbn A data structure encapsulating at least the original information (tag and id)
192 * and source (package name) used to post the {@link android.app.Notification} that
194 * @param rankingMap The current ranking map that can be used to retrieve ranking information
195 * for active notifications.
198 public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {
199 onNotificationRemoved(sbn);
203 * Implement this method to learn about when the listener is enabled and connected to
204 * the notification manager. You are safe to call {@link #getActiveNotifications()}
207 public void onListenerConnected() {
212 * Implement this method to be notified when the notification ranking changes.
214 * @param rankingMap The current ranking map that can be used to retrieve ranking information
215 * for active notifications.
217 public void onNotificationRankingUpdate(RankingMap rankingMap) {
222 * Implement this method to be notified when the
223 * {@link #getCurrentListenerHints() Listener hints} change.
225 * @param hints The current {@link #getCurrentListenerHints() listener hints}.
227 public void onListenerHintsChanged(int hints) {
232 * Implement this method to be notified when the
233 * {@link #getCurrentInterruptionFilter() interruption filter} changed.
235 * @param interruptionFilter The current
236 * {@link #getCurrentInterruptionFilter() interruption filter}.
238 public void onInterruptionFilterChanged(int interruptionFilter) {
242 private final INotificationManager getNotificationInterface() {
243 if (mNoMan == null) {
244 mNoMan = INotificationManager.Stub.asInterface(
245 ServiceManager.getService(Context.NOTIFICATION_SERVICE));
251 * Inform the notification manager about dismissal of a single notification.
253 * Use this if your listener has a user interface that allows the user to dismiss individual
254 * notifications, similar to the behavior of Android's status bar and notification panel.
255 * It should be called after the user dismisses a single notification using your UI;
256 * upon being informed, the notification manager will actually remove the notification
257 * and you will get an {@link #onNotificationRemoved(StatusBarNotification)} callback.
259 * <b>Note:</b> If your listener allows the user to fire a notification's
260 * {@link android.app.Notification#contentIntent} by tapping/clicking/etc., you should call
261 * this method at that time <i>if</i> the Notification in question has the
262 * {@link android.app.Notification#FLAG_AUTO_CANCEL} flag set.
264 * @param pkg Package of the notifying app.
265 * @param tag Tag of the notification as specified by the notifying app in
266 * {@link android.app.NotificationManager#notify(String, int, android.app.Notification)}.
267 * @param id ID of the notification as specified by the notifying app in
268 * {@link android.app.NotificationManager#notify(String, int, android.app.Notification)}.
270 * @deprecated Use {@link #cancelNotification(String key)}
271 * instead. Beginning with {@link android.os.Build.VERSION_CODES#L} this method will no longer
272 * cancel the notification. It will continue to cancel the notification for applications
273 * whose {@code targetSdkVersion} is earlier than {@link android.os.Build.VERSION_CODES#L}.
275 public final void cancelNotification(String pkg, String tag, int id) {
276 if (!isBound()) return;
278 getNotificationInterface().cancelNotificationFromListener(
279 mWrapper, pkg, tag, id);
280 } catch (android.os.RemoteException ex) {
281 Log.v(TAG, "Unable to contact notification manager", ex);
286 * Inform the notification manager about dismissal of a single notification.
288 * Use this if your listener has a user interface that allows the user to dismiss individual
289 * notifications, similar to the behavior of Android's status bar and notification panel.
290 * It should be called after the user dismisses a single notification using your UI;
291 * upon being informed, the notification manager will actually remove the notification
292 * and you will get an {@link #onNotificationRemoved(StatusBarNotification)} callback.
294 * <b>Note:</b> If your listener allows the user to fire a notification's
295 * {@link android.app.Notification#contentIntent} by tapping/clicking/etc., you should call
296 * this method at that time <i>if</i> the Notification in question has the
297 * {@link android.app.Notification#FLAG_AUTO_CANCEL} flag set.
299 * @param key Notification to dismiss from {@link StatusBarNotification#getKey()}.
301 public final void cancelNotification(String key) {
302 if (!isBound()) return;
304 getNotificationInterface().cancelNotificationsFromListener(mWrapper,
306 } catch (android.os.RemoteException ex) {
307 Log.v(TAG, "Unable to contact notification manager", ex);
312 * Inform the notification manager about dismissal of all notifications.
314 * Use this if your listener has a user interface that allows the user to dismiss all
315 * notifications, similar to the behavior of Android's status bar and notification panel.
316 * It should be called after the user invokes the "dismiss all" function of your UI;
317 * upon being informed, the notification manager will actually remove all active notifications
318 * and you will get multiple {@link #onNotificationRemoved(StatusBarNotification)} callbacks.
320 * {@see #cancelNotification(String, String, int)}
322 public final void cancelAllNotifications() {
323 cancelNotifications(null /*all*/);
327 * Inform the notification manager about dismissal of specific notifications.
329 * Use this if your listener has a user interface that allows the user to dismiss
330 * multiple notifications at once.
332 * @param keys Notifications to dismiss, or {@code null} to dismiss all.
334 * {@see #cancelNotification(String, String, int)}
336 public final void cancelNotifications(String[] keys) {
337 if (!isBound()) return;
339 getNotificationInterface().cancelNotificationsFromListener(mWrapper, keys);
340 } catch (android.os.RemoteException ex) {
341 Log.v(TAG, "Unable to contact notification manager", ex);
346 * Sets the notification trim that will be received via {@link #onNotificationPosted}.
349 * Setting a trim other than {@link #TRIM_FULL} enables listeners that don't need access to the
350 * full notification features right away to reduce their memory footprint. Full notifications
351 * can be requested on-demand via {@link #getActiveNotifications(int)}.
354 * Set to {@link #TRIM_FULL} initially.
358 * @param trim trim of the notifications to be passed via {@link #onNotificationPosted}.
359 * See <code>TRIM_*</code> constants.
362 public final void setOnNotificationPostedTrim(int trim) {
363 if (!isBound()) return;
365 getNotificationInterface().setOnNotificationPostedTrimFromListener(mWrapper, trim);
366 } catch (RemoteException ex) {
367 Log.v(TAG, "Unable to contact notification manager", ex);
372 * Request the list of outstanding notifications (that is, those that are visible to the
373 * current user). Useful when you don't know what's already been posted.
375 * @return An array of active notifications, sorted in natural order.
377 public StatusBarNotification[] getActiveNotifications() {
378 return getActiveNotifications(null, TRIM_FULL);
382 * Request the list of outstanding notifications (that is, those that are visible to the
383 * current user). Useful when you don't know what's already been posted.
387 * @param trim trim of the notifications to be returned. See <code>TRIM_*</code> constants.
388 * @return An array of active notifications, sorted in natural order.
391 public StatusBarNotification[] getActiveNotifications(int trim) {
392 return getActiveNotifications(null, trim);
396 * Request one or more notifications by key. Useful if you have been keeping track of
397 * notifications but didn't want to retain the bits, and now need to go back and extract
398 * more data out of those notifications.
400 * @param keys the keys of the notifications to request
401 * @return An array of notifications corresponding to the requested keys, in the
402 * same order as the key list.
404 public StatusBarNotification[] getActiveNotifications(String[] keys) {
405 return getActiveNotifications(keys, TRIM_FULL);
409 * Request one or more notifications by key. Useful if you have been keeping track of
410 * notifications but didn't want to retain the bits, and now need to go back and extract
411 * more data out of those notifications.
415 * @param keys the keys of the notifications to request
416 * @param trim trim of the notifications to be returned. See <code>TRIM_*</code> constants.
417 * @return An array of notifications corresponding to the requested keys, in the
418 * same order as the key list.
421 public StatusBarNotification[] getActiveNotifications(String[] keys, int trim) {
425 ParceledListSlice<StatusBarNotification> parceledList = getNotificationInterface()
426 .getActiveNotificationsFromListener(mWrapper, keys, trim);
427 List<StatusBarNotification> list = parceledList.getList();
430 for (int i = 0; i < N; i++) {
431 Notification notification = list.get(i).getNotification();
432 Builder.rebuild(getContext(), notification);
434 return list.toArray(new StatusBarNotification[N]);
435 } catch (android.os.RemoteException ex) {
436 Log.v(TAG, "Unable to contact notification manager", ex);
442 * Gets the set of hints representing current state.
445 * The current state may differ from the requested state if the hint represents state
446 * shared across all listeners or a feature the notification host does not support or refuses
449 * @return Zero or more of the HINT_ constants.
451 public final int getCurrentListenerHints() {
452 if (!isBound()) return 0;
454 return getNotificationInterface().getHintsFromListener(mWrapper);
455 } catch (android.os.RemoteException ex) {
456 Log.v(TAG, "Unable to contact notification manager", ex);
462 * Gets the current notification interruption filter active on the host.
465 * The interruption filter defines which notifications are allowed to interrupt the user
466 * (e.g. via sound & vibration) and is applied globally. Listeners can find out whether
467 * a specific notification matched the interruption filter via
468 * {@link Ranking#matchesInterruptionFilter()}.
470 * The current filter may differ from the previously requested filter if the notification host
471 * does not support or refuses to apply the requested filter, or if another component changed
472 * the filter in the meantime.
474 * Listen for updates using {@link #onInterruptionFilterChanged(int)}.
476 * @return One of the INTERRUPTION_FILTER_ constants, or 0 on errors.
478 public final int getCurrentInterruptionFilter() {
479 if (!isBound()) return 0;
481 return getNotificationInterface().getHintsFromListener(mWrapper);
482 } catch (android.os.RemoteException ex) {
483 Log.v(TAG, "Unable to contact notification manager", ex);
489 * Sets the desired {@link #getCurrentListenerHints() listener hints}.
492 * This is merely a request, the host may or may not choose to take action depending
493 * on other listener requests or other global state.
495 * Listen for updates using {@link #onListenerHintsChanged(int)}.
497 * @param hints One or more of the HINT_ constants.
499 public final void requestListenerHints(int hints) {
500 if (!isBound()) return;
502 getNotificationInterface().requestHintsFromListener(mWrapper, hints);
503 } catch (android.os.RemoteException ex) {
504 Log.v(TAG, "Unable to contact notification manager", ex);
509 * Sets the desired {@link #getCurrentInterruptionFilter() interruption filter}.
512 * This is merely a request, the host may or may not choose to apply the requested
513 * interruption filter depending on other listener requests or other global state.
515 * Listen for updates using {@link #onInterruptionFilterChanged(int)}.
517 * @param interruptionFilter One of the INTERRUPTION_FILTER_ constants.
519 public final void requestInterruptionFilter(int interruptionFilter) {
520 if (!isBound()) return;
522 getNotificationInterface()
523 .requestInterruptionFilterFromListener(mWrapper, interruptionFilter);
524 } catch (android.os.RemoteException ex) {
525 Log.v(TAG, "Unable to contact notification manager", ex);
530 * Returns current ranking information.
533 * The returned object represents the current ranking snapshot and only
534 * applies for currently active notifications.
536 * Generally you should use the RankingMap that is passed with events such
537 * as {@link #onNotificationPosted(StatusBarNotification, RankingMap)},
538 * {@link #onNotificationRemoved(StatusBarNotification, RankingMap)}, and
539 * so on. This method should only be used when needing access outside of
540 * such events, for example to retrieve the RankingMap right after
543 * @return A {@link RankingMap} object providing access to ranking information
545 public RankingMap getCurrentRanking() {
550 public IBinder onBind(Intent intent) {
551 if (mWrapper == null) {
552 mWrapper = new INotificationListenerWrapper();
557 private boolean isBound() {
558 if (mWrapper == null) {
559 Log.w(TAG, "Notification listener service not yet bound.");
566 * Directly register this service with the Notification Manager.
568 * <p>Only system services may use this call. It will fail for non-system callers.
569 * Apps should ask the user to add their listener in Settings.
571 * @param context Context required for accessing resources. Since this service isn't
572 * launched as a real Service when using this method, a context has to be passed in.
573 * @param componentName the component that will consume the notification information
574 * @param currentUser the user to use as the stream filter
578 public void registerAsSystemService(Context context, ComponentName componentName,
579 int currentUser) throws RemoteException {
580 mSystemContext = context;
581 if (mWrapper == null) {
582 mWrapper = new INotificationListenerWrapper();
584 INotificationManager noMan = getNotificationInterface();
585 noMan.registerListener(mWrapper, componentName, currentUser);
586 mCurrentUser = currentUser;
590 * Directly unregister this service from the Notification Manager.
592 * <P>This method will fail for listeners that were not registered
593 * with (@link registerAsService).
597 public void unregisterAsSystemService() throws RemoteException {
598 if (mWrapper != null) {
599 INotificationManager noMan = getNotificationInterface();
600 noMan.unregisterListener(mWrapper, mCurrentUser);
604 private class INotificationListenerWrapper extends INotificationListener.Stub {
606 public void onNotificationPosted(IStatusBarNotificationHolder sbnHolder,
607 NotificationRankingUpdate update) {
608 StatusBarNotification sbn;
610 sbn = sbnHolder.get();
611 } catch (RemoteException e) {
612 Log.w(TAG, "onNotificationPosted: Error receiving StatusBarNotification", e);
615 Notification.Builder.rebuild(getContext(), sbn.getNotification());
617 // protect subclass from concurrent modifications of (@link mNotificationKeys}.
618 synchronized (mWrapper) {
621 NotificationListenerService.this.onNotificationPosted(sbn, mRankingMap);
622 } catch (Throwable t) {
623 Log.w(TAG, "Error running onNotificationPosted", t);
628 public void onNotificationRemoved(IStatusBarNotificationHolder sbnHolder,
629 NotificationRankingUpdate update) {
630 StatusBarNotification sbn;
632 sbn = sbnHolder.get();
633 } catch (RemoteException e) {
634 Log.w(TAG, "onNotificationRemoved: Error receiving StatusBarNotification", e);
637 // protect subclass from concurrent modifications of (@link mNotificationKeys}.
638 synchronized (mWrapper) {
641 NotificationListenerService.this.onNotificationRemoved(sbn, mRankingMap);
642 } catch (Throwable t) {
643 Log.w(TAG, "Error running onNotificationRemoved", t);
648 public void onListenerConnected(NotificationRankingUpdate update) {
649 // protect subclass from concurrent modifications of (@link mNotificationKeys}.
650 synchronized (mWrapper) {
653 NotificationListenerService.this.onListenerConnected();
654 } catch (Throwable t) {
655 Log.w(TAG, "Error running onListenerConnected", t);
660 public void onNotificationRankingUpdate(NotificationRankingUpdate update)
661 throws RemoteException {
662 // protect subclass from concurrent modifications of (@link mNotificationKeys}.
663 synchronized (mWrapper) {
666 NotificationListenerService.this.onNotificationRankingUpdate(mRankingMap);
667 } catch (Throwable t) {
668 Log.w(TAG, "Error running onNotificationRankingUpdate", t);
673 public void onListenerHintsChanged(int hints) throws RemoteException {
675 NotificationListenerService.this.onListenerHintsChanged(hints);
676 } catch (Throwable t) {
677 Log.w(TAG, "Error running onListenerHintsChanged", t);
682 public void onInterruptionFilterChanged(int interruptionFilter) throws RemoteException {
684 NotificationListenerService.this.onInterruptionFilterChanged(interruptionFilter);
685 } catch (Throwable t) {
686 Log.w(TAG, "Error running onInterruptionFilterChanged", t);
691 private void applyUpdate(NotificationRankingUpdate update) {
692 mRankingMap = new RankingMap(update);
695 private Context getContext() {
696 if (mSystemContext != null) {
697 return mSystemContext;
703 * Stores ranking related information on a currently active notification.
706 * Ranking objects aren't automatically updated as notification events
707 * occur. Instead, ranking information has to be retrieved again via the
708 * current {@link RankingMap}.
710 public static class Ranking {
711 /** Value signifying that the user has not expressed a per-app visibility override value.
713 public static final int VISIBILITY_NO_OVERRIDE = -1000;
716 private int mRank = -1;
717 private boolean mIsAmbient;
718 private boolean mMatchesInterruptionFilter;
719 private int mVisibilityOverride;
724 * Returns the key of the notification this Ranking applies to.
726 public String getKey() {
731 * Returns the rank of the notification.
733 * @return the rank of the notification, that is the 0-based index in
734 * the list of active notifications.
736 public int getRank() {
741 * Returns whether the notification is an ambient notification, that is
742 * a notification that doesn't require the user's immediate attention.
744 public boolean isAmbient() {
749 * Returns the user specificed visibility for the package that posted
750 * this notification, or
751 * {@link NotificationListenerService.Ranking#VISIBILITY_NO_OVERRIDE} if
752 * no such preference has been expressed.
755 public int getVisibilityOverride() {
756 return mVisibilityOverride;
760 * Returns whether the notification meets the user's interruption
765 public boolean meetsInterruptionFilter() {
766 return mMatchesInterruptionFilter;
770 * Returns whether the notification matches the user's interruption
773 public boolean matchesInterruptionFilter() {
774 return mMatchesInterruptionFilter;
777 private void populate(String key, int rank, boolean isAmbient,
778 boolean matchesInterruptionFilter, int visibilityOverride) {
781 mIsAmbient = isAmbient;
782 mMatchesInterruptionFilter = matchesInterruptionFilter;
783 mVisibilityOverride = visibilityOverride;
788 * Provides access to ranking information on currently active
792 * Note that this object represents a ranking snapshot that only applies to
793 * notifications active at the time of retrieval.
795 public static class RankingMap implements Parcelable {
796 private final NotificationRankingUpdate mRankingUpdate;
797 private ArrayMap<String,Integer> mRanks;
798 private ArraySet<Object> mIntercepted;
799 private ArrayMap<String, Integer> mVisibilityOverrides;
801 private RankingMap(NotificationRankingUpdate rankingUpdate) {
802 mRankingUpdate = rankingUpdate;
806 * Request the list of notification keys in their current ranking
809 * @return An array of active notification keys, in their ranking order.
811 public String[] getOrderedKeys() {
812 return mRankingUpdate.getOrderedKeys();
816 * Populates outRanking with ranking information for the notification
817 * with the given key.
819 * @return true if a valid key has been passed and outRanking has
820 * been populated; false otherwise
822 public boolean getRanking(String key, Ranking outRanking) {
823 int rank = getRank(key);
824 outRanking.populate(key, rank, isAmbient(key), !isIntercepted(key),
825 getVisibilityOverride(key));
829 private int getRank(String key) {
830 synchronized (this) {
831 if (mRanks == null) {
835 Integer rank = mRanks.get(key);
836 return rank != null ? rank : -1;
839 private boolean isAmbient(String key) {
840 int firstAmbientIndex = mRankingUpdate.getFirstAmbientIndex();
841 if (firstAmbientIndex < 0) {
844 int rank = getRank(key);
845 return rank >= 0 && rank >= firstAmbientIndex;
848 private boolean isIntercepted(String key) {
849 synchronized (this) {
850 if (mIntercepted == null) {
851 buildInterceptedSetLocked();
854 return mIntercepted.contains(key);
857 private int getVisibilityOverride(String key) {
858 synchronized (this) {
859 if (mVisibilityOverrides == null) {
860 buildVisibilityOverridesLocked();
863 Integer overide = mVisibilityOverrides.get(key);
864 if (overide == null) {
865 return Ranking.VISIBILITY_NO_OVERRIDE;
867 return overide.intValue();
871 private void buildRanksLocked() {
872 String[] orderedKeys = mRankingUpdate.getOrderedKeys();
873 mRanks = new ArrayMap<>(orderedKeys.length);
874 for (int i = 0; i < orderedKeys.length; i++) {
875 String key = orderedKeys[i];
881 private void buildInterceptedSetLocked() {
882 String[] dndInterceptedKeys = mRankingUpdate.getInterceptedKeys();
883 mIntercepted = new ArraySet<>(dndInterceptedKeys.length);
884 Collections.addAll(mIntercepted, dndInterceptedKeys);
888 private void buildVisibilityOverridesLocked() {
889 Bundle visibilityBundle = mRankingUpdate.getVisibilityOverrides();
890 mVisibilityOverrides = new ArrayMap<>(visibilityBundle.size());
891 for (String key: visibilityBundle.keySet()) {
892 mVisibilityOverrides.put(key, visibilityBundle.getInt(key));
896 // ----------- Parcelable
899 public int describeContents() {
904 public void writeToParcel(Parcel dest, int flags) {
905 dest.writeParcelable(mRankingUpdate, flags);
908 public static final Creator<RankingMap> CREATOR = new Creator<RankingMap>() {
910 public RankingMap createFromParcel(Parcel source) {
911 NotificationRankingUpdate rankingUpdate = source.readParcelable(null);
912 return new RankingMap(rankingUpdate);
916 public RankingMap[] newArray(int size) {
917 return new RankingMap[size];