NotificationListenerService.java revision 5feceebb892d4cb5777cea3c6174b206705d456b
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.annotation.SdkConstant;
20import android.app.INotificationManager;
21import android.app.Service;
22import android.content.Context;
23import android.content.Intent;
24import android.os.IBinder;
25import android.os.ServiceManager;
26import android.util.Log;
27
28public abstract class NotificationListenerService extends Service {
29    // TAG = "NotificationListenerService[MySubclass]"
30    private final String TAG = NotificationListenerService.class.getSimpleName()
31            + "[" + getClass().getSimpleName() + "]";
32
33    private INotificationListenerWrapper mWrapper = null;
34
35    private INotificationManager mNoMan;
36
37    /**
38     * The {@link Intent} that must be declared as handled by the service.
39     */
40    @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
41    public static final String SERVICE_INTERFACE
42            = "android.service.notification.NotificationListenerService";
43
44    /**
45     * Implement this method to learn about new notifications as they are posted by apps.
46     *
47     * @param sbn A data structure encapsulating the original {@link android.app.Notification}
48     *            object as well as its identifying information (tag and id) and source
49     *            (package name).
50     */
51    public abstract void onNotificationPosted(StatusBarNotification sbn);
52
53    /**
54     * Implement this method to learn when notifications are removed.
55     * <P>
56     * This might occur because the user has dismissed the notification using system UI (or another
57     * notification listener) or because the app has withdrawn the notification.
58     *
59     * @param sbn A data structure encapsulating the original {@link android.app.Notification}
60     *            object as well as its identifying information (tag and id) and source
61     *            (package name).
62     */
63    public abstract void onNotificationRemoved(StatusBarNotification sbn);
64
65    private final INotificationManager getNotificationInterface() {
66        if (mNoMan == null) {
67            mNoMan = INotificationManager.Stub.asInterface(
68                    ServiceManager.getService(Context.NOTIFICATION_SERVICE));
69        }
70        return mNoMan;
71    }
72
73    /**
74     * Inform the notification manager about dismissal of a single notification.
75     * <p>
76     * Use this if your listener has a user interface that allows the user to dismiss individual
77     * notifications, similar to the behavior of Android's status bar and notification panel.
78     * It should be called after the user dismisses a single notification using your UI;
79     * upon being informed, the notification manager will actually remove the notification
80     * and you will get an {@link #onNotificationRemoved(StatusBarNotification)} callback.
81     * <P>
82     * <b>Note:</b> If your listener allows the user to fire a notification's
83     * {@link android.app.Notification#contentIntent} by tapping/clicking/etc., you should call
84     * this method at that time <i>if</i> the Notification in question has the
85     * {@link android.app.Notification#FLAG_AUTO_CANCEL} flag set.
86     *
87     * @param pkg Package of the notifying app.
88     * @param tag Tag of the notification as specified by the notifying app in
89     *     {@link android.app.NotificationManager#notify(String, int, android.app.Notification)}.
90     * @param id  ID of the notification as specified by the notifying app in
91     *     {@link android.app.NotificationManager#notify(String, int, android.app.Notification)}.
92     */
93    public final void clearNotification(String pkg, String tag, int id) {
94        try {
95            getNotificationInterface().clearNotificationFromListener(mWrapper, pkg, tag, id);
96        } catch (android.os.RemoteException ex) {
97            Log.v(TAG, "Unable to contact notification manager", ex);
98        }
99    }
100
101    /**
102     * Inform the notification manager about dismissal of all notifications.
103     * <p>
104     * Use this if your listener has a user interface that allows the user to dismiss all
105     * notifications, similar to the behavior of Android's status bar and notification panel.
106     * It should be called after the user invokes the "dismiss all" function of your UI;
107     * upon being informed, the notification manager will actually remove all active notifications
108     * and you will get multiple {@link #onNotificationRemoved(StatusBarNotification)} callbacks.
109     *
110     * {@see #clearNotification(String, String, int)}
111     */
112    public final void clearAllNotifications() {
113        try {
114            getNotificationInterface().clearAllNotificationsFromListener(mWrapper);
115        } catch (android.os.RemoteException ex) {
116            Log.v(TAG, "Unable to contact notification manager", ex);
117        }
118    }
119
120    @Override
121    public IBinder onBind(Intent intent) {
122        if (mWrapper == null) {
123            mWrapper = new INotificationListenerWrapper();
124        }
125        return mWrapper;
126    }
127
128    private class INotificationListenerWrapper extends INotificationListener.Stub {
129        @Override
130        public void onNotificationPosted(StatusBarNotification sbn) {
131            NotificationListenerService.this.onNotificationPosted(sbn);
132        }
133        @Override
134        public void onNotificationRemoved(StatusBarNotification sbn) {
135            NotificationListenerService.this.onNotificationRemoved(sbn);
136        }
137    }
138}
139