NotificationAssistantService.java revision 7eba593b8d88a9b087f06ef146d82c4c77a92473
1/*
2 * Copyright (C) 2015 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.Notification;
21import android.content.Intent;
22import android.net.Uri;
23import android.os.Parcel;
24import android.os.Parcelable;
25
26/**
27 * A service that helps the user manage notifications by modifying the
28 * relative importance of notifications.
29 * <p>To extend this class, you must declare the service in your manifest file with
30 * the {@link android.Manifest.permission#BIND_NOTIFICATION_ASSISTANT_SERVICE} permission
31 * and include an intent filter with the {@link #SERVICE_INTERFACE} action. For example:</p>
32 * <pre>
33 * &lt;service android:name=".NotificationAssistant"
34 *          android:label="&#64;string/service_name"
35 *          android:permission="android.permission.BIND_NOTIFICATION_ASSISTANT_SERVICE">
36 *     &lt;intent-filter>
37 *         &lt;action android:name="android.service.notification.NotificationAssistantService" />
38 *     &lt;/intent-filter>
39 * &lt;/service></pre>
40 */
41public abstract class NotificationAssistantService extends NotificationListenerService {
42    /**
43     * The {@link Intent} that must be declared as handled by the service.
44     */
45    @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
46    public static final String SERVICE_INTERFACE
47            = "android.service.notification.NotificationAssistantService";
48
49    /** Notification was canceled by the status bar reporting a click. */
50    public static final int REASON_DELEGATE_CLICK = 1;
51
52    /** Notification was canceled by the status bar reporting a user dismissal. */
53    public static final int REASON_DELEGATE_CANCEL = 2;
54
55    /** Notification was canceled by the status bar reporting a user dismiss all. */
56    public static final int REASON_DELEGATE_CANCEL_ALL = 3;
57
58    /** Notification was canceled by the status bar reporting an inflation error. */
59    public static final int REASON_DELEGATE_ERROR = 4;
60
61    /** Notification was canceled by the package manager modifying the package. */
62    public static final int REASON_PACKAGE_CHANGED = 5;
63
64    /** Notification was canceled by the owning user context being stopped. */
65    public static final int REASON_USER_STOPPED = 6;
66
67    /** Notification was canceled by the user banning the package. */
68    public static final int REASON_PACKAGE_BANNED = 7;
69
70    /** Notification was canceled by the app canceling this specific notification. */
71    public static final int REASON_APP_CANCEL = 8;
72
73    /** Notification was canceled by the app cancelling all its notifications. */
74    public static final int REASON_APP_CANCEL_ALL = 9;
75
76    /** Notification was canceled by a listener reporting a user dismissal. */
77    public static final int REASON_LISTENER_CANCEL = 10;
78
79    /** Notification was canceled by a listener reporting a user dismiss all. */
80    public static final int REASON_LISTENER_CANCEL_ALL = 11;
81
82    /** Notification was canceled because it was a member of a canceled group. */
83    public static final int REASON_GROUP_SUMMARY_CANCELED = 12;
84
85    /** Notification was canceled because it was an invisible member of a group. */
86    public static final int REASON_GROUP_OPTIMIZATION = 13;
87
88    /**
89     * A notification was posted by an app. Called before alert.
90     *
91     * @param sbn the new notification
92     * @param importance the initial importance of the notification.
93     * @param user true if the initial importance reflects an explicit user preference.
94     * @return an adjustment or null to take no action, within 100ms.
95     */
96    abstract public NotificationAdjustment onNotificationEnqueued(StatusBarNotification sbn,
97          int importance, boolean user);
98
99    /**
100     * The visibility of a notification has changed.
101     *
102     * @param key the notification key
103     * @param time milliseconds since midnight, January 1, 1970 UTC.
104     * @param visible true if the notification became visible, false if hidden.
105     */
106    public void onNotificationVisibilityChanged(String key, long time, boolean visible)
107    {
108        // Do nothing, Override this to collect visibility statistics.
109    }
110
111    /**
112     * The user clicked on a notification.
113     *
114     * @param key the notification key
115     * @param time milliseconds since midnight, January 1, 1970 UTC.
116     */
117    public void onNotificationClick(String key, long time)
118    {
119        // Do nothing, Override this to collect click statistics
120    }
121
122    /**
123     * The user clicked on a notification action.
124     *
125     * @param key the notification key
126     * @param time milliseconds since midnight, January 1, 1970 UTC.
127     * @param actionIndex the index of the action button that was pressed.
128     */
129    public void onNotificationActionClick(String key, long time, int actionIndex)
130    {
131        // Do nothing, Override this to collect action button click statistics
132    }
133
134    /**
135     * A notification was removed.
136
137     * @param key the notification key
138     * @param time milliseconds since midnight, January 1, 1970 UTC.
139     * @param reason see {@link #REASON_LISTENER_CANCEL}, etc.
140     */
141    public void onNotificationRemoved(String key, long time, int reason) {
142        // Do nothing, Override this to collect dismissal statistics
143    }
144
145    /**
146     * Change the importance of an existing notification.  N.B. this won’t cause
147     * an existing notification to alert, but might allow a future update to
148     * this notification to alert.
149     *
150     * @param key the notification key
151     * @param adjustment the new importance with an explanation
152     */
153    public final void adjustImportance(String key, NotificationAdjustment adjustment)
154    {
155        // TODO: pack up the adjustment and send it to the NotificationManager.
156    }
157
158    /**
159     * Add an annotation to a an existing notification. The delete intent will
160     * be fired when the host notification is deleted, or when this annotation
161     * is removed or replaced.
162     *
163     * @param key the notification key
164     * @param annotation the new annotation object
165     */
166    public final void setAnnotation(String key, Notification annotation)
167    {
168        // TODO: pack up the annotation and send it to the NotificationManager.
169    }
170
171    /**
172     * Remove the annotation from a notification.
173     *
174     * @param key the notification key
175     */
176    public final void clearAnnotation(String key)
177    {
178        // TODO: ask the NotificationManager to clear the annotation.
179    }
180}
181