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