blob: d741f1a142ca41b881ec3355df95b91a75f005b6 [file] [log] [blame]
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001/*
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
Jeff Brown5c507c12014-06-05 17:14:39 -070019import android.annotation.SystemApi;
Daniel Sandler5feceeb2013-03-22 18:29:23 -070020import android.annotation.SdkConstant;
21import android.app.INotificationManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020022import android.app.Notification;
23import android.app.Notification.Builder;
John Spurlock80774932015-05-07 17:38:50 -040024import android.app.NotificationManager;
Daniel Sandler5feceeb2013-03-22 18:29:23 -070025import android.app.Service;
Chris Wren1941fc72014-05-14 15:20:51 -040026import android.content.ComponentName;
Daniel Sandler5feceeb2013-03-22 18:29:23 -070027import android.content.Context;
28import android.content.Intent;
Christoph Studercee44ba2014-05-20 18:36:43 +020029import android.content.pm.ParceledListSlice;
Daniel Sandlerf5a78382015-05-15 23:59:36 -040030import android.graphics.drawable.BitmapDrawable;
31import android.graphics.drawable.Drawable;
32import android.graphics.drawable.Icon;
33import android.graphics.Bitmap;
Julia Reynoldsd9228f12015-10-20 10:37:27 -040034import android.os.Build;
Chris Wren3ad4e3a2014-09-02 17:23:51 -040035import android.os.Bundle;
Daniel Sandler5feceeb2013-03-22 18:29:23 -070036import android.os.IBinder;
Christoph Studer05ad4822014-05-16 14:16:03 +020037import android.os.Parcel;
38import android.os.Parcelable;
Chris Wrenf9536642014-04-17 10:01:54 -040039import android.os.RemoteException;
Daniel Sandler5feceeb2013-03-22 18:29:23 -070040import android.os.ServiceManager;
Christoph Studerdda48f12014-07-29 23:13:16 +020041import android.util.ArrayMap;
42import android.util.ArraySet;
Daniel Sandler5feceeb2013-03-22 18:29:23 -070043import android.util.Log;
44
Chris Wren24fb8942015-06-18 14:33:56 -040045import java.util.ArrayList;
Christoph Studerdda48f12014-07-29 23:13:16 +020046import java.util.Collections;
Christoph Studercee44ba2014-05-20 18:36:43 +020047import java.util.List;
48
Scott Main04667da2013-04-25 16:57:16 -070049/**
Christoph Studer05ad4822014-05-16 14:16:03 +020050 * A service that receives calls from the system when new notifications are
51 * posted or removed, or their ranking changed.
Scott Main04667da2013-04-25 16:57:16 -070052 * <p>To extend this class, you must declare the service in your manifest file with
53 * the {@link android.Manifest.permission#BIND_NOTIFICATION_LISTENER_SERVICE} permission
54 * and include an intent filter with the {@link #SERVICE_INTERFACE} action. For example:</p>
55 * <pre>
56 * &lt;service android:name=".NotificationListener"
57 * android:label="&#64;string/service_name"
58 * android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
59 * &lt;intent-filter>
60 * &lt;action android:name="android.service.notification.NotificationListenerService" />
61 * &lt;/intent-filter>
62 * &lt;/service></pre>
63 */
Daniel Sandler5feceeb2013-03-22 18:29:23 -070064public abstract class NotificationListenerService extends Service {
65 // TAG = "NotificationListenerService[MySubclass]"
66 private final String TAG = NotificationListenerService.class.getSimpleName()
67 + "[" + getClass().getSimpleName() + "]";
68
Christoph Studer85a384b2014-08-27 20:16:15 +020069 /**
70 * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
71 * Normal interruption filter.
72 */
John Spurlock80774932015-05-07 17:38:50 -040073 public static final int INTERRUPTION_FILTER_ALL
74 = NotificationManager.INTERRUPTION_FILTER_ALL;
John Spurlockd8afe3c2014-08-01 14:04:07 -040075
Christoph Studer85a384b2014-08-27 20:16:15 +020076 /**
77 * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
78 * Priority interruption filter.
79 */
John Spurlock80774932015-05-07 17:38:50 -040080 public static final int INTERRUPTION_FILTER_PRIORITY
81 = NotificationManager.INTERRUPTION_FILTER_PRIORITY;
John Spurlockd8afe3c2014-08-01 14:04:07 -040082
Christoph Studer85a384b2014-08-27 20:16:15 +020083 /**
84 * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
85 * No interruptions filter.
86 */
John Spurlock80774932015-05-07 17:38:50 -040087 public static final int INTERRUPTION_FILTER_NONE
88 = NotificationManager.INTERRUPTION_FILTER_NONE;
John Spurlockd8afe3c2014-08-01 14:04:07 -040089
John Spurlock4f1163c2015-04-02 17:41:21 -040090 /**
91 * {@link #getCurrentInterruptionFilter() Interruption filter} constant -
92 * Alarms only interruption filter.
93 */
John Spurlock80774932015-05-07 17:38:50 -040094 public static final int INTERRUPTION_FILTER_ALARMS
95 = NotificationManager.INTERRUPTION_FILTER_ALARMS;
John Spurlock4f1163c2015-04-02 17:41:21 -040096
John Spurlock83104102015-02-12 23:25:12 -050097 /** {@link #getCurrentInterruptionFilter() Interruption filter} constant - returned when
98 * the value is unavailable for any reason. For example, before the notification listener
99 * is connected.
100 *
101 * {@see #onListenerConnected()}
102 */
John Spurlock80774932015-05-07 17:38:50 -0400103 public static final int INTERRUPTION_FILTER_UNKNOWN
104 = NotificationManager.INTERRUPTION_FILTER_UNKNOWN;
John Spurlock83104102015-02-12 23:25:12 -0500105
John Spurlockd8afe3c2014-08-01 14:04:07 -0400106 /** {@link #getCurrentListenerHints() Listener hints} constant - the primary device UI
107 * should disable notification sound, vibrating and other visual or aural effects.
Christoph Studer85a384b2014-08-27 20:16:15 +0200108 * This does not change the interruption filter, only the effects. **/
109 public static final int HINT_HOST_DISABLE_EFFECTS = 1;
John Spurlock1fa865f2014-07-21 14:56:39 -0400110
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500111 public static final int SUPPRESSED_EFFECT_LIGHTS =
112 NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
113 public static final int SUPPRESSED_EFFECT_PEEK =
114 NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
115
Christoph Studerb82bc782014-08-20 14:29:43 +0200116 /**
117 * The full trim of the StatusBarNotification including all its features.
118 *
119 * @hide
120 */
121 @SystemApi
122 public static final int TRIM_FULL = 0;
123
124 /**
125 * A light trim of the StatusBarNotification excluding the following features:
126 *
127 * <ol>
128 * <li>{@link Notification#tickerView tickerView}</li>
129 * <li>{@link Notification#contentView contentView}</li>
130 * <li>{@link Notification#largeIcon largeIcon}</li>
131 * <li>{@link Notification#bigContentView bigContentView}</li>
132 * <li>{@link Notification#headsUpContentView headsUpContentView}</li>
133 * <li>{@link Notification#EXTRA_LARGE_ICON extras[EXTRA_LARGE_ICON]}</li>
134 * <li>{@link Notification#EXTRA_LARGE_ICON_BIG extras[EXTRA_LARGE_ICON_BIG]}</li>
135 * <li>{@link Notification#EXTRA_PICTURE extras[EXTRA_PICTURE]}</li>
Christoph Studer223f44e2014-09-02 14:59:32 +0200136 * <li>{@link Notification#EXTRA_BIG_TEXT extras[EXTRA_BIG_TEXT]}</li>
Christoph Studerb82bc782014-08-20 14:29:43 +0200137 * </ol>
138 *
139 * @hide
140 */
141 @SystemApi
142 public static final int TRIM_LIGHT = 1;
143
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700144 private INotificationListenerWrapper mWrapper = null;
Christoph Studerd0694b62014-06-04 16:36:01 +0200145 private RankingMap mRankingMap;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700146
147 private INotificationManager mNoMan;
148
Chris Wren1941fc72014-05-14 15:20:51 -0400149 /** Only valid after a successful call to (@link registerAsService}. */
150 private int mCurrentUser;
151
Christoph Studer4600f9b2014-07-22 22:44:43 +0200152
153 // This context is required for system services since NotificationListenerService isn't
154 // started as a real Service and hence no context is available.
155 private Context mSystemContext;
156
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700157 /**
158 * The {@link Intent} that must be declared as handled by the service.
159 */
160 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
161 public static final String SERVICE_INTERFACE
162 = "android.service.notification.NotificationListenerService";
163
164 /**
165 * Implement this method to learn about new notifications as they are posted by apps.
166 *
167 * @param sbn A data structure encapsulating the original {@link android.app.Notification}
168 * object as well as its identifying information (tag and id) and source
169 * (package name).
170 */
Christoph Studerd0694b62014-06-04 16:36:01 +0200171 public void onNotificationPosted(StatusBarNotification sbn) {
172 // optional
173 }
174
175 /**
176 * Implement this method to learn about new notifications as they are posted by apps.
177 *
178 * @param sbn A data structure encapsulating the original {@link android.app.Notification}
179 * object as well as its identifying information (tag and id) and source
180 * (package name).
181 * @param rankingMap The current ranking map that can be used to retrieve ranking information
182 * for active notifications, including the newly posted one.
183 */
184 public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) {
185 onNotificationPosted(sbn);
186 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700187
188 /**
189 * Implement this method to learn when notifications are removed.
190 * <P>
191 * This might occur because the user has dismissed the notification using system UI (or another
192 * notification listener) or because the app has withdrawn the notification.
Daniel Sandler1a497d32013-04-18 14:52:45 -0400193 * <P>
194 * NOTE: The {@link StatusBarNotification} object you receive will be "light"; that is, the
Scott Main04667da2013-04-25 16:57:16 -0700195 * result from {@link StatusBarNotification#getNotification} may be missing some heavyweight
Daniel Sandler1a497d32013-04-18 14:52:45 -0400196 * fields such as {@link android.app.Notification#contentView} and
197 * {@link android.app.Notification#largeIcon}. However, all other fields on
198 * {@link StatusBarNotification}, sufficient to match this call with a prior call to
199 * {@link #onNotificationPosted(StatusBarNotification)}, will be intact.
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700200 *
Daniel Sandler1a497d32013-04-18 14:52:45 -0400201 * @param sbn A data structure encapsulating at least the original information (tag and id)
202 * and source (package name) used to post the {@link android.app.Notification} that
203 * was just removed.
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700204 */
Christoph Studerd0694b62014-06-04 16:36:01 +0200205 public void onNotificationRemoved(StatusBarNotification sbn) {
206 // optional
207 }
208
209 /**
210 * Implement this method to learn when notifications are removed.
211 * <P>
212 * This might occur because the user has dismissed the notification using system UI (or another
213 * notification listener) or because the app has withdrawn the notification.
214 * <P>
215 * NOTE: The {@link StatusBarNotification} object you receive will be "light"; that is, the
216 * result from {@link StatusBarNotification#getNotification} may be missing some heavyweight
217 * fields such as {@link android.app.Notification#contentView} and
218 * {@link android.app.Notification#largeIcon}. However, all other fields on
219 * {@link StatusBarNotification}, sufficient to match this call with a prior call to
220 * {@link #onNotificationPosted(StatusBarNotification)}, will be intact.
221 *
222 * @param sbn A data structure encapsulating at least the original information (tag and id)
223 * and source (package name) used to post the {@link android.app.Notification} that
224 * was just removed.
225 * @param rankingMap The current ranking map that can be used to retrieve ranking information
226 * for active notifications.
227 *
228 */
229 public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {
230 onNotificationRemoved(sbn);
231 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700232
John Spurlocka4294292014-03-24 18:02:32 -0400233 /**
234 * Implement this method to learn about when the listener is enabled and connected to
Christoph Studercee44ba2014-05-20 18:36:43 +0200235 * the notification manager. You are safe to call {@link #getActiveNotifications()}
John Spurlocka4294292014-03-24 18:02:32 -0400236 * at this time.
John Spurlocka4294292014-03-24 18:02:32 -0400237 */
Christoph Studercee44ba2014-05-20 18:36:43 +0200238 public void onListenerConnected() {
John Spurlocka4294292014-03-24 18:02:32 -0400239 // optional
240 }
241
Chris Wrenf9536642014-04-17 10:01:54 -0400242 /**
Christoph Studer05ad4822014-05-16 14:16:03 +0200243 * Implement this method to be notified when the notification ranking changes.
Christoph Studerd0694b62014-06-04 16:36:01 +0200244 *
245 * @param rankingMap The current ranking map that can be used to retrieve ranking information
246 * for active notifications.
Chris Wrenf9536642014-04-17 10:01:54 -0400247 */
Christoph Studerd0694b62014-06-04 16:36:01 +0200248 public void onNotificationRankingUpdate(RankingMap rankingMap) {
Chris Wrenf9536642014-04-17 10:01:54 -0400249 // optional
250 }
251
John Spurlock1fa865f2014-07-21 14:56:39 -0400252 /**
253 * Implement this method to be notified when the
John Spurlockd8afe3c2014-08-01 14:04:07 -0400254 * {@link #getCurrentListenerHints() Listener hints} change.
John Spurlock1fa865f2014-07-21 14:56:39 -0400255 *
John Spurlockd8afe3c2014-08-01 14:04:07 -0400256 * @param hints The current {@link #getCurrentListenerHints() listener hints}.
John Spurlock1fa865f2014-07-21 14:56:39 -0400257 */
John Spurlockd8afe3c2014-08-01 14:04:07 -0400258 public void onListenerHintsChanged(int hints) {
John Spurlock1fa865f2014-07-21 14:56:39 -0400259 // optional
260 }
261
Christoph Studer85a384b2014-08-27 20:16:15 +0200262 /**
263 * Implement this method to be notified when the
264 * {@link #getCurrentInterruptionFilter() interruption filter} changed.
265 *
266 * @param interruptionFilter The current
267 * {@link #getCurrentInterruptionFilter() interruption filter}.
268 */
269 public void onInterruptionFilterChanged(int interruptionFilter) {
270 // optional
271 }
272
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700273 private final INotificationManager getNotificationInterface() {
274 if (mNoMan == null) {
275 mNoMan = INotificationManager.Stub.asInterface(
276 ServiceManager.getService(Context.NOTIFICATION_SERVICE));
277 }
278 return mNoMan;
279 }
280
281 /**
282 * Inform the notification manager about dismissal of a single notification.
283 * <p>
284 * Use this if your listener has a user interface that allows the user to dismiss individual
285 * notifications, similar to the behavior of Android's status bar and notification panel.
286 * It should be called after the user dismisses a single notification using your UI;
287 * upon being informed, the notification manager will actually remove the notification
288 * and you will get an {@link #onNotificationRemoved(StatusBarNotification)} callback.
289 * <P>
290 * <b>Note:</b> If your listener allows the user to fire a notification's
291 * {@link android.app.Notification#contentIntent} by tapping/clicking/etc., you should call
292 * this method at that time <i>if</i> the Notification in question has the
293 * {@link android.app.Notification#FLAG_AUTO_CANCEL} flag set.
294 *
295 * @param pkg Package of the notifying app.
296 * @param tag Tag of the notification as specified by the notifying app in
297 * {@link android.app.NotificationManager#notify(String, int, android.app.Notification)}.
298 * @param id ID of the notification as specified by the notifying app in
299 * {@link android.app.NotificationManager#notify(String, int, android.app.Notification)}.
Kenny Guya263e4e2014-03-03 18:24:03 +0000300 * <p>
301 * @deprecated Use {@link #cancelNotification(String key)}
Dianne Hackborn955d8d62014-10-07 20:17:19 -0700302 * instead. Beginning with {@link android.os.Build.VERSION_CODES#LOLLIPOP} this method will no longer
Kenny Guya263e4e2014-03-03 18:24:03 +0000303 * cancel the notification. It will continue to cancel the notification for applications
Dianne Hackborn955d8d62014-10-07 20:17:19 -0700304 * whose {@code targetSdkVersion} is earlier than {@link android.os.Build.VERSION_CODES#LOLLIPOP}.
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700305 */
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400306 public final void cancelNotification(String pkg, String tag, int id) {
John Spurlockda9a3be2014-02-12 12:12:26 -0500307 if (!isBound()) return;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700308 try {
Kenny Guya263e4e2014-03-03 18:24:03 +0000309 getNotificationInterface().cancelNotificationFromListener(
310 mWrapper, pkg, tag, id);
311 } catch (android.os.RemoteException ex) {
312 Log.v(TAG, "Unable to contact notification manager", ex);
313 }
314 }
315
316 /**
317 * Inform the notification manager about dismissal of a single notification.
318 * <p>
319 * Use this if your listener has a user interface that allows the user to dismiss individual
320 * notifications, similar to the behavior of Android's status bar and notification panel.
321 * It should be called after the user dismisses a single notification using your UI;
322 * upon being informed, the notification manager will actually remove the notification
323 * and you will get an {@link #onNotificationRemoved(StatusBarNotification)} callback.
324 * <P>
325 * <b>Note:</b> If your listener allows the user to fire a notification's
326 * {@link android.app.Notification#contentIntent} by tapping/clicking/etc., you should call
327 * this method at that time <i>if</i> the Notification in question has the
328 * {@link android.app.Notification#FLAG_AUTO_CANCEL} flag set.
329 * <p>
330 * @param key Notification to dismiss from {@link StatusBarNotification#getKey()}.
331 */
332 public final void cancelNotification(String key) {
333 if (!isBound()) return;
334 try {
335 getNotificationInterface().cancelNotificationsFromListener(mWrapper,
Daniel Sandlerf5a78382015-05-15 23:59:36 -0400336 new String[] { key });
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700337 } catch (android.os.RemoteException ex) {
338 Log.v(TAG, "Unable to contact notification manager", ex);
339 }
340 }
341
342 /**
343 * Inform the notification manager about dismissal of all notifications.
344 * <p>
345 * Use this if your listener has a user interface that allows the user to dismiss all
346 * notifications, similar to the behavior of Android's status bar and notification panel.
347 * It should be called after the user invokes the "dismiss all" function of your UI;
348 * upon being informed, the notification manager will actually remove all active notifications
349 * and you will get multiple {@link #onNotificationRemoved(StatusBarNotification)} callbacks.
350 *
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400351 * {@see #cancelNotification(String, String, int)}
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700352 */
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400353 public final void cancelAllNotifications() {
John Spurlocka4294292014-03-24 18:02:32 -0400354 cancelNotifications(null /*all*/);
355 }
356
357 /**
358 * Inform the notification manager about dismissal of specific notifications.
359 * <p>
360 * Use this if your listener has a user interface that allows the user to dismiss
361 * multiple notifications at once.
362 *
363 * @param keys Notifications to dismiss, or {@code null} to dismiss all.
364 *
365 * {@see #cancelNotification(String, String, int)}
366 */
367 public final void cancelNotifications(String[] keys) {
John Spurlockda9a3be2014-02-12 12:12:26 -0500368 if (!isBound()) return;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700369 try {
John Spurlocka4294292014-03-24 18:02:32 -0400370 getNotificationInterface().cancelNotificationsFromListener(mWrapper, keys);
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700371 } catch (android.os.RemoteException ex) {
372 Log.v(TAG, "Unable to contact notification manager", ex);
373 }
374 }
375
Daniel Sandler25cf8ce2013-04-24 15:34:57 -0400376 /**
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700377 * Inform the notification manager that these notifications have been viewed by the
Amith Yamasanic6ecbce2015-06-23 12:58:43 -0700378 * user. This should only be called when there is sufficient confidence that the user is
379 * looking at the notifications, such as when the notifications appear on the screen due to
380 * an explicit user interaction.
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700381 * @param keys Notifications to mark as seen.
382 */
383 public final void setNotificationsShown(String[] keys) {
384 if (!isBound()) return;
385 try {
386 getNotificationInterface().setNotificationsShownFromListener(mWrapper, keys);
387 } catch (android.os.RemoteException ex) {
388 Log.v(TAG, "Unable to contact notification manager", ex);
389 }
390 }
391
392 /**
Christoph Studerb82bc782014-08-20 14:29:43 +0200393 * Sets the notification trim that will be received via {@link #onNotificationPosted}.
394 *
395 * <p>
396 * Setting a trim other than {@link #TRIM_FULL} enables listeners that don't need access to the
397 * full notification features right away to reduce their memory footprint. Full notifications
398 * can be requested on-demand via {@link #getActiveNotifications(int)}.
399 *
400 * <p>
401 * Set to {@link #TRIM_FULL} initially.
402 *
403 * @hide
404 *
405 * @param trim trim of the notifications to be passed via {@link #onNotificationPosted}.
406 * See <code>TRIM_*</code> constants.
407 */
408 @SystemApi
409 public final void setOnNotificationPostedTrim(int trim) {
410 if (!isBound()) return;
411 try {
412 getNotificationInterface().setOnNotificationPostedTrimFromListener(mWrapper, trim);
413 } catch (RemoteException ex) {
414 Log.v(TAG, "Unable to contact notification manager", ex);
415 }
416 }
417
418 /**
Daniel Sandler25cf8ce2013-04-24 15:34:57 -0400419 * Request the list of outstanding notifications (that is, those that are visible to the
John Spurlocka4294292014-03-24 18:02:32 -0400420 * current user). Useful when you don't know what's already been posted.
Daniel Sandler25cf8ce2013-04-24 15:34:57 -0400421 *
Chris Wrenf9536642014-04-17 10:01:54 -0400422 * @return An array of active notifications, sorted in natural order.
Daniel Sandler25cf8ce2013-04-24 15:34:57 -0400423 */
424 public StatusBarNotification[] getActiveNotifications() {
Christoph Studerb82bc782014-08-20 14:29:43 +0200425 return getActiveNotifications(null, TRIM_FULL);
426 }
427
428 /**
429 * Request the list of outstanding notifications (that is, those that are visible to the
430 * current user). Useful when you don't know what's already been posted.
431 *
432 * @hide
433 *
434 * @param trim trim of the notifications to be returned. See <code>TRIM_*</code> constants.
435 * @return An array of active notifications, sorted in natural order.
436 */
437 @SystemApi
438 public StatusBarNotification[] getActiveNotifications(int trim) {
439 return getActiveNotifications(null, trim);
Dan Sandlerea75fdd2014-08-12 12:29:19 -0400440 }
441
442 /**
443 * Request one or more notifications by key. Useful if you have been keeping track of
444 * notifications but didn't want to retain the bits, and now need to go back and extract
445 * more data out of those notifications.
446 *
Christoph Studerb82bc782014-08-20 14:29:43 +0200447 * @param keys the keys of the notifications to request
Dan Sandlerea75fdd2014-08-12 12:29:19 -0400448 * @return An array of notifications corresponding to the requested keys, in the
449 * same order as the key list.
450 */
451 public StatusBarNotification[] getActiveNotifications(String[] keys) {
Christoph Studerb82bc782014-08-20 14:29:43 +0200452 return getActiveNotifications(keys, TRIM_FULL);
453 }
454
455 /**
456 * Request one or more notifications by key. Useful if you have been keeping track of
457 * notifications but didn't want to retain the bits, and now need to go back and extract
458 * more data out of those notifications.
459 *
460 * @hide
461 *
462 * @param keys the keys of the notifications to request
463 * @param trim trim of the notifications to be returned. See <code>TRIM_*</code> constants.
464 * @return An array of notifications corresponding to the requested keys, in the
465 * same order as the key list.
466 */
467 @SystemApi
468 public StatusBarNotification[] getActiveNotifications(String[] keys, int trim) {
469 if (!isBound())
470 return null;
Daniel Sandler25cf8ce2013-04-24 15:34:57 -0400471 try {
Christoph Studerb82bc782014-08-20 14:29:43 +0200472 ParceledListSlice<StatusBarNotification> parceledList = getNotificationInterface()
473 .getActiveNotificationsFromListener(mWrapper, keys, trim);
Christoph Studercee44ba2014-05-20 18:36:43 +0200474 List<StatusBarNotification> list = parceledList.getList();
Chris Wren24fb8942015-06-18 14:33:56 -0400475 ArrayList<StatusBarNotification> corruptNotifications = null;
Christoph Studer4600f9b2014-07-22 22:44:43 +0200476 int N = list.size();
477 for (int i = 0; i < N; i++) {
Chris Wren24fb8942015-06-18 14:33:56 -0400478 StatusBarNotification sbn = list.get(i);
479 Notification notification = sbn.getNotification();
480 try {
Chris Wren24fb8942015-06-18 14:33:56 -0400481 // convert icon metadata to legacy format for older clients
482 createLegacyIconExtras(notification);
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400483 // populate remote views for older clients.
484 maybePopulateRemoteViews(notification);
Chris Wren24fb8942015-06-18 14:33:56 -0400485 } catch (IllegalArgumentException e) {
486 if (corruptNotifications == null) {
487 corruptNotifications = new ArrayList<>(N);
488 }
489 corruptNotifications.add(sbn);
490 Log.w(TAG, "onNotificationPosted: can't rebuild notification from " +
491 sbn.getPackageName());
492 }
Christoph Studer4600f9b2014-07-22 22:44:43 +0200493 }
Chris Wren24fb8942015-06-18 14:33:56 -0400494 if (corruptNotifications != null) {
495 list.removeAll(corruptNotifications);
496 }
497 return list.toArray(new StatusBarNotification[list.size()]);
John Spurlocka4294292014-03-24 18:02:32 -0400498 } catch (android.os.RemoteException ex) {
499 Log.v(TAG, "Unable to contact notification manager", ex);
500 }
501 return null;
502 }
503
504 /**
John Spurlockd8afe3c2014-08-01 14:04:07 -0400505 * Gets the set of hints representing current state.
John Spurlock1fa865f2014-07-21 14:56:39 -0400506 *
507 * <p>
John Spurlockd8afe3c2014-08-01 14:04:07 -0400508 * The current state may differ from the requested state if the hint represents state
John Spurlock1fa865f2014-07-21 14:56:39 -0400509 * shared across all listeners or a feature the notification host does not support or refuses
510 * to grant.
511 *
Christoph Studer85a384b2014-08-27 20:16:15 +0200512 * @return Zero or more of the HINT_ constants.
John Spurlock1fa865f2014-07-21 14:56:39 -0400513 */
John Spurlockd8afe3c2014-08-01 14:04:07 -0400514 public final int getCurrentListenerHints() {
Christoph Studer85a384b2014-08-27 20:16:15 +0200515 if (!isBound()) return 0;
John Spurlock1fa865f2014-07-21 14:56:39 -0400516 try {
John Spurlockd8afe3c2014-08-01 14:04:07 -0400517 return getNotificationInterface().getHintsFromListener(mWrapper);
John Spurlock1fa865f2014-07-21 14:56:39 -0400518 } catch (android.os.RemoteException ex) {
519 Log.v(TAG, "Unable to contact notification manager", ex);
Christoph Studer85a384b2014-08-27 20:16:15 +0200520 return 0;
521 }
522 }
523
524 /**
525 * Gets the current notification interruption filter active on the host.
526 *
527 * <p>
528 * The interruption filter defines which notifications are allowed to interrupt the user
529 * (e.g. via sound &amp; vibration) and is applied globally. Listeners can find out whether
530 * a specific notification matched the interruption filter via
531 * {@link Ranking#matchesInterruptionFilter()}.
532 * <p>
533 * The current filter may differ from the previously requested filter if the notification host
534 * does not support or refuses to apply the requested filter, or if another component changed
535 * the filter in the meantime.
536 * <p>
537 * Listen for updates using {@link #onInterruptionFilterChanged(int)}.
538 *
John Spurlock83104102015-02-12 23:25:12 -0500539 * @return One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when
540 * unavailable.
Christoph Studer85a384b2014-08-27 20:16:15 +0200541 */
542 public final int getCurrentInterruptionFilter() {
John Spurlock83104102015-02-12 23:25:12 -0500543 if (!isBound()) return INTERRUPTION_FILTER_UNKNOWN;
Christoph Studer85a384b2014-08-27 20:16:15 +0200544 try {
Chris Wren957ed702014-09-24 18:17:36 -0400545 return getNotificationInterface().getInterruptionFilterFromListener(mWrapper);
Christoph Studer85a384b2014-08-27 20:16:15 +0200546 } catch (android.os.RemoteException ex) {
547 Log.v(TAG, "Unable to contact notification manager", ex);
John Spurlock83104102015-02-12 23:25:12 -0500548 return INTERRUPTION_FILTER_UNKNOWN;
John Spurlock1fa865f2014-07-21 14:56:39 -0400549 }
550 }
551
552 /**
John Spurlockd8afe3c2014-08-01 14:04:07 -0400553 * Sets the desired {@link #getCurrentListenerHints() listener hints}.
John Spurlock1fa865f2014-07-21 14:56:39 -0400554 *
555 * <p>
Christoph Studer85a384b2014-08-27 20:16:15 +0200556 * This is merely a request, the host may or may not choose to take action depending
John Spurlock1fa865f2014-07-21 14:56:39 -0400557 * on other listener requests or other global state.
558 * <p>
John Spurlockd8afe3c2014-08-01 14:04:07 -0400559 * Listen for updates using {@link #onListenerHintsChanged(int)}.
John Spurlock1fa865f2014-07-21 14:56:39 -0400560 *
John Spurlockd8afe3c2014-08-01 14:04:07 -0400561 * @param hints One or more of the HINT_ constants.
John Spurlock1fa865f2014-07-21 14:56:39 -0400562 */
John Spurlockd8afe3c2014-08-01 14:04:07 -0400563 public final void requestListenerHints(int hints) {
John Spurlock1fa865f2014-07-21 14:56:39 -0400564 if (!isBound()) return;
565 try {
John Spurlockd8afe3c2014-08-01 14:04:07 -0400566 getNotificationInterface().requestHintsFromListener(mWrapper, hints);
John Spurlock1fa865f2014-07-21 14:56:39 -0400567 } catch (android.os.RemoteException ex) {
568 Log.v(TAG, "Unable to contact notification manager", ex);
569 }
570 }
571
572 /**
Christoph Studer85a384b2014-08-27 20:16:15 +0200573 * Sets the desired {@link #getCurrentInterruptionFilter() interruption filter}.
574 *
575 * <p>
576 * This is merely a request, the host may or may not choose to apply the requested
577 * interruption filter depending on other listener requests or other global state.
578 * <p>
579 * Listen for updates using {@link #onInterruptionFilterChanged(int)}.
580 *
581 * @param interruptionFilter One of the INTERRUPTION_FILTER_ constants.
582 */
583 public final void requestInterruptionFilter(int interruptionFilter) {
584 if (!isBound()) return;
585 try {
586 getNotificationInterface()
587 .requestInterruptionFilterFromListener(mWrapper, interruptionFilter);
588 } catch (android.os.RemoteException ex) {
589 Log.v(TAG, "Unable to contact notification manager", ex);
590 }
591 }
592
593 /**
Christoph Studer05ad4822014-05-16 14:16:03 +0200594 * Returns current ranking information.
John Spurlocka4294292014-03-24 18:02:32 -0400595 *
Christoph Studer05ad4822014-05-16 14:16:03 +0200596 * <p>
597 * The returned object represents the current ranking snapshot and only
Christoph Studerd0694b62014-06-04 16:36:01 +0200598 * applies for currently active notifications.
599 * <p>
600 * Generally you should use the RankingMap that is passed with events such
601 * as {@link #onNotificationPosted(StatusBarNotification, RankingMap)},
602 * {@link #onNotificationRemoved(StatusBarNotification, RankingMap)}, and
603 * so on. This method should only be used when needing access outside of
604 * such events, for example to retrieve the RankingMap right after
605 * initialization.
Christoph Studer05ad4822014-05-16 14:16:03 +0200606 *
Christoph Studerd0694b62014-06-04 16:36:01 +0200607 * @return A {@link RankingMap} object providing access to ranking information
John Spurlocka4294292014-03-24 18:02:32 -0400608 */
Christoph Studerd0694b62014-06-04 16:36:01 +0200609 public RankingMap getCurrentRanking() {
610 return mRankingMap;
Daniel Sandler25cf8ce2013-04-24 15:34:57 -0400611 }
612
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700613 @Override
614 public IBinder onBind(Intent intent) {
615 if (mWrapper == null) {
616 mWrapper = new INotificationListenerWrapper();
617 }
618 return mWrapper;
619 }
620
John Spurlockda9a3be2014-02-12 12:12:26 -0500621 private boolean isBound() {
622 if (mWrapper == null) {
623 Log.w(TAG, "Notification listener service not yet bound.");
624 return false;
625 }
626 return true;
627 }
628
Chris Wren1941fc72014-05-14 15:20:51 -0400629 /**
630 * Directly register this service with the Notification Manager.
631 *
632 * <p>Only system services may use this call. It will fail for non-system callers.
633 * Apps should ask the user to add their listener in Settings.
634 *
Christoph Studer4600f9b2014-07-22 22:44:43 +0200635 * @param context Context required for accessing resources. Since this service isn't
636 * launched as a real Service when using this method, a context has to be passed in.
Chris Wren1941fc72014-05-14 15:20:51 -0400637 * @param componentName the component that will consume the notification information
638 * @param currentUser the user to use as the stream filter
639 * @hide
640 */
Jeff Brown5c507c12014-06-05 17:14:39 -0700641 @SystemApi
Christoph Studer4600f9b2014-07-22 22:44:43 +0200642 public void registerAsSystemService(Context context, ComponentName componentName,
643 int currentUser) throws RemoteException {
644 mSystemContext = context;
Chris Wren1941fc72014-05-14 15:20:51 -0400645 if (mWrapper == null) {
646 mWrapper = new INotificationListenerWrapper();
647 }
648 INotificationManager noMan = getNotificationInterface();
649 noMan.registerListener(mWrapper, componentName, currentUser);
650 mCurrentUser = currentUser;
651 }
652
653 /**
654 * Directly unregister this service from the Notification Manager.
655 *
656 * <P>This method will fail for listeners that were not registered
657 * with (@link registerAsService).
658 * @hide
659 */
Jeff Brown5c507c12014-06-05 17:14:39 -0700660 @SystemApi
Chris Wren1941fc72014-05-14 15:20:51 -0400661 public void unregisterAsSystemService() throws RemoteException {
662 if (mWrapper != null) {
663 INotificationManager noMan = getNotificationInterface();
664 noMan.unregisterListener(mWrapper, mCurrentUser);
665 }
666 }
667
Daniel Sandlerf5a78382015-05-15 23:59:36 -0400668 /** Convert new-style Icons to legacy representations for pre-M clients. */
669 private void createLegacyIconExtras(Notification n) {
670 Icon smallIcon = n.getSmallIcon();
671 Icon largeIcon = n.getLargeIcon();
Dan Sandler99a37f12015-06-09 14:34:38 -0400672 if (smallIcon != null && smallIcon.getType() == Icon.TYPE_RESOURCE) {
Daniel Sandlerf5a78382015-05-15 23:59:36 -0400673 n.extras.putInt(Notification.EXTRA_SMALL_ICON, smallIcon.getResId());
674 n.icon = smallIcon.getResId();
675 }
676 if (largeIcon != null) {
677 Drawable d = largeIcon.loadDrawable(getContext());
678 if (d != null && d instanceof BitmapDrawable) {
679 final Bitmap largeIconBits = ((BitmapDrawable) d).getBitmap();
680 n.extras.putParcelable(Notification.EXTRA_LARGE_ICON, largeIconBits);
681 n.largeIcon = largeIconBits;
682 }
683 }
684 }
685
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400686 /**
687 * Populates remote views for pre-N targeting apps.
688 */
689 private void maybePopulateRemoteViews(Notification notification) {
690 if (getContext().getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
691 Builder builder = Builder.recoverBuilder(getContext(), notification);
692 notification.contentView = builder.makeContentView();
693 notification.bigContentView = builder.makeBigContentView();
694 notification.headsUpContentView = builder.makeHeadsUpContentView();
695 }
696 }
697
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700698 private class INotificationListenerWrapper extends INotificationListener.Stub {
699 @Override
Griff Hazen84a00ea2014-09-02 17:10:47 -0700700 public void onNotificationPosted(IStatusBarNotificationHolder sbnHolder,
Christoph Studer05ad4822014-05-16 14:16:03 +0200701 NotificationRankingUpdate update) {
Griff Hazen84a00ea2014-09-02 17:10:47 -0700702 StatusBarNotification sbn;
703 try {
704 sbn = sbnHolder.get();
705 } catch (RemoteException e) {
706 Log.w(TAG, "onNotificationPosted: Error receiving StatusBarNotification", e);
707 return;
708 }
Christoph Studer4600f9b2014-07-22 22:44:43 +0200709
Chris Wren24fb8942015-06-18 14:33:56 -0400710 try {
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400711 Notification notification = sbn.getNotification();
Chris Wren24fb8942015-06-18 14:33:56 -0400712 // convert icon metadata to legacy format for older clients
713 createLegacyIconExtras(sbn.getNotification());
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400714 maybePopulateRemoteViews(sbn.getNotification());
Chris Wren24fb8942015-06-18 14:33:56 -0400715 } catch (IllegalArgumentException e) {
716 // drop corrupt notification
717 sbn = null;
718 Log.w(TAG, "onNotificationPosted: can't rebuild notification from " +
719 sbn.getPackageName());
720 }
Daniel Sandlerf5a78382015-05-15 23:59:36 -0400721
Christoph Studer05ad4822014-05-16 14:16:03 +0200722 // protect subclass from concurrent modifications of (@link mNotificationKeys}.
723 synchronized (mWrapper) {
724 applyUpdate(update);
725 try {
Chris Wren24fb8942015-06-18 14:33:56 -0400726 if (sbn != null) {
727 NotificationListenerService.this.onNotificationPosted(sbn, mRankingMap);
728 } else {
729 // still pass along the ranking map, it may contain other information
730 NotificationListenerService.this.onNotificationRankingUpdate(mRankingMap);
731 }
Christoph Studer05ad4822014-05-16 14:16:03 +0200732 } catch (Throwable t) {
733 Log.w(TAG, "Error running onNotificationPosted", t);
Chris Wrenf9536642014-04-17 10:01:54 -0400734 }
John Spurlockc133ab82013-06-10 15:16:22 -0400735 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700736 }
737 @Override
Griff Hazen84a00ea2014-09-02 17:10:47 -0700738 public void onNotificationRemoved(IStatusBarNotificationHolder sbnHolder,
Christoph Studer05ad4822014-05-16 14:16:03 +0200739 NotificationRankingUpdate update) {
Griff Hazen84a00ea2014-09-02 17:10:47 -0700740 StatusBarNotification sbn;
741 try {
742 sbn = sbnHolder.get();
743 } catch (RemoteException e) {
744 Log.w(TAG, "onNotificationRemoved: Error receiving StatusBarNotification", e);
745 return;
746 }
Christoph Studer05ad4822014-05-16 14:16:03 +0200747 // protect subclass from concurrent modifications of (@link mNotificationKeys}.
748 synchronized (mWrapper) {
749 applyUpdate(update);
750 try {
Christoph Studerd0694b62014-06-04 16:36:01 +0200751 NotificationListenerService.this.onNotificationRemoved(sbn, mRankingMap);
Christoph Studer05ad4822014-05-16 14:16:03 +0200752 } catch (Throwable t) {
753 Log.w(TAG, "Error running onNotificationRemoved", t);
Chris Wrenf9536642014-04-17 10:01:54 -0400754 }
John Spurlockc133ab82013-06-10 15:16:22 -0400755 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700756 }
John Spurlocka4294292014-03-24 18:02:32 -0400757 @Override
Christoph Studer05ad4822014-05-16 14:16:03 +0200758 public void onListenerConnected(NotificationRankingUpdate update) {
759 // protect subclass from concurrent modifications of (@link mNotificationKeys}.
760 synchronized (mWrapper) {
761 applyUpdate(update);
762 try {
Christoph Studercee44ba2014-05-20 18:36:43 +0200763 NotificationListenerService.this.onListenerConnected();
Christoph Studer05ad4822014-05-16 14:16:03 +0200764 } catch (Throwable t) {
765 Log.w(TAG, "Error running onListenerConnected", t);
Chris Wrenf9536642014-04-17 10:01:54 -0400766 }
John Spurlocka4294292014-03-24 18:02:32 -0400767 }
768 }
Chris Wrenf9536642014-04-17 10:01:54 -0400769 @Override
Christoph Studer05ad4822014-05-16 14:16:03 +0200770 public void onNotificationRankingUpdate(NotificationRankingUpdate update)
Chris Wrenf9536642014-04-17 10:01:54 -0400771 throws RemoteException {
Christoph Studer05ad4822014-05-16 14:16:03 +0200772 // protect subclass from concurrent modifications of (@link mNotificationKeys}.
773 synchronized (mWrapper) {
774 applyUpdate(update);
775 try {
Christoph Studerd0694b62014-06-04 16:36:01 +0200776 NotificationListenerService.this.onNotificationRankingUpdate(mRankingMap);
Christoph Studer05ad4822014-05-16 14:16:03 +0200777 } catch (Throwable t) {
778 Log.w(TAG, "Error running onNotificationRankingUpdate", t);
Chris Wrenf9536642014-04-17 10:01:54 -0400779 }
Chris Wrenf9536642014-04-17 10:01:54 -0400780 }
781 }
John Spurlock1fa865f2014-07-21 14:56:39 -0400782 @Override
John Spurlockd8afe3c2014-08-01 14:04:07 -0400783 public void onListenerHintsChanged(int hints) throws RemoteException {
John Spurlock1fa865f2014-07-21 14:56:39 -0400784 try {
John Spurlockd8afe3c2014-08-01 14:04:07 -0400785 NotificationListenerService.this.onListenerHintsChanged(hints);
John Spurlock1fa865f2014-07-21 14:56:39 -0400786 } catch (Throwable t) {
John Spurlockd8afe3c2014-08-01 14:04:07 -0400787 Log.w(TAG, "Error running onListenerHintsChanged", t);
John Spurlock1fa865f2014-07-21 14:56:39 -0400788 }
789 }
Christoph Studer85a384b2014-08-27 20:16:15 +0200790
791 @Override
792 public void onInterruptionFilterChanged(int interruptionFilter) throws RemoteException {
793 try {
794 NotificationListenerService.this.onInterruptionFilterChanged(interruptionFilter);
795 } catch (Throwable t) {
796 Log.w(TAG, "Error running onInterruptionFilterChanged", t);
797 }
798 }
Chris Wrenf9536642014-04-17 10:01:54 -0400799 }
800
Christoph Studer05ad4822014-05-16 14:16:03 +0200801 private void applyUpdate(NotificationRankingUpdate update) {
Christoph Studerd0694b62014-06-04 16:36:01 +0200802 mRankingMap = new RankingMap(update);
803 }
804
Christoph Studer4600f9b2014-07-22 22:44:43 +0200805 private Context getContext() {
806 if (mSystemContext != null) {
807 return mSystemContext;
808 }
809 return this;
810 }
811
Christoph Studerd0694b62014-06-04 16:36:01 +0200812 /**
Christoph Studer1d599da2014-06-12 15:25:59 +0200813 * Stores ranking related information on a currently active notification.
Christoph Studerd0694b62014-06-04 16:36:01 +0200814 *
815 * <p>
Christoph Studer1d599da2014-06-12 15:25:59 +0200816 * Ranking objects aren't automatically updated as notification events
817 * occur. Instead, ranking information has to be retrieved again via the
818 * current {@link RankingMap}.
Christoph Studerd0694b62014-06-04 16:36:01 +0200819 */
820 public static class Ranking {
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400821 /** Value signifying that the user has not expressed a per-app visibility override value.
822 * @hide */
823 public static final int VISIBILITY_NO_OVERRIDE = -1000;
824
Chris Wren9fa689f2015-11-20 16:44:53 -0500825 /**
826 * Value signifying thatn the has not expressed an importance.
827 *
828 * This value is for persisting preferences, and should never ve associated with
829 * an actual notification.
830 */
831 public static final int IMPORTANCE_UNSPECIFIED = -1000;
832
833 /**
834 * A notification with no importance: shows nowhere, is blocked.
835 */
836 public static final int IMPORTANCE_NONE = -2;
837
838 /**
839 * Low notification importance: only shows in the shade, below the fold.
840 */
841 public static final int IMPORTANCE_LOW = -1;
842
843 /**
844 * Default notification importance: shows everywhere, but is not intrusive.
845 */
846 public static final int IMPORTANCE_DEFAULT = 0;
847
848 /**
849 * Higher notification importance: shows everywhere, makes noise,
850 * but does not visually intrude.
851 */
852 public static final int IMPORTANCE_HIGH = 1;
853
854 /**
855 * Highest notification importance: shows everywhere, makes noise,
856 * and also visually intrudes.
857 */
858 public static final int IMPORTANCE_MAX = 2;
859
Christoph Studer1d599da2014-06-12 15:25:59 +0200860 private String mKey;
861 private int mRank = -1;
862 private boolean mIsAmbient;
Christoph Studerce7d6d22014-08-26 19:21:31 +0200863 private boolean mMatchesInterruptionFilter;
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400864 private int mVisibilityOverride;
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500865 private int mSuppressedVisualEffects;
Christoph Studerd0694b62014-06-04 16:36:01 +0200866
Christoph Studer1d599da2014-06-12 15:25:59 +0200867 public Ranking() {}
Christoph Studerd0694b62014-06-04 16:36:01 +0200868
869 /**
870 * Returns the key of the notification this Ranking applies to.
871 */
872 public String getKey() {
873 return mKey;
874 }
875
876 /**
877 * Returns the rank of the notification.
878 *
879 * @return the rank of the notification, that is the 0-based index in
880 * the list of active notifications.
881 */
882 public int getRank() {
883 return mRank;
884 }
885
886 /**
887 * Returns whether the notification is an ambient notification, that is
888 * a notification that doesn't require the user's immediate attention.
889 */
890 public boolean isAmbient() {
891 return mIsAmbient;
892 }
893
894 /**
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400895 * Returns the user specificed visibility for the package that posted
896 * this notification, or
897 * {@link NotificationListenerService.Ranking#VISIBILITY_NO_OVERRIDE} if
898 * no such preference has been expressed.
899 * @hide
900 */
901 public int getVisibilityOverride() {
902 return mVisibilityOverride;
903 }
904
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500905 /**
906 * Returns the type(s) of visual effects that should be suppressed for this notification.
907 * See {@link #SUPPRESSED_EFFECT_LIGHTS}, {@link #SUPPRESSED_EFFECT_PEEK}}.
908 */
909 public int getSuppressedVisualEffects() {
910 return mSuppressedVisualEffects;
911 }
912
Christoph Studerce7d6d22014-08-26 19:21:31 +0200913 /**
914 * Returns whether the notification matches the user's interruption
915 * filter.
Chris Wren0fef44d2014-09-30 13:05:14 -0400916 *
917 * @return {@code true} if the notification is allowed by the filter, or
918 * {@code false} if it is blocked.
Christoph Studerce7d6d22014-08-26 19:21:31 +0200919 */
920 public boolean matchesInterruptionFilter() {
921 return mMatchesInterruptionFilter;
Christoph Studer1d599da2014-06-12 15:25:59 +0200922 }
923
Chris Wren9fa689f2015-11-20 16:44:53 -0500924 /**
925 * Returns the importance of the notification, which dictates its
926 * modes of presentation, see: {@link #IMPORTANCE_DEFAULT}, etc.
927 *
928 * @return the rank of the notification
929 */
930 public int getImportance() {
931 return IMPORTANCE_DEFAULT; // TODO implement;
932 }
933
934 /**
935 * If the importance has been overriden by user preference, or by a
936 * {@link NotificationAssistantService}, then this will be non-null,
937 * and should be displayed to the user.
938 *
939 * @return the explanation for the importance, or null if it is the natural importance
940 */
941 public CharSequence getImportanceExplanation() {
942 return null; // TODO implement
943 }
944
Christoph Studer1d599da2014-06-12 15:25:59 +0200945 private void populate(String key, int rank, boolean isAmbient,
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500946 boolean matchesInterruptionFilter, int visibilityOverride,
947 int suppressedVisualEffects) {
Christoph Studer1d599da2014-06-12 15:25:59 +0200948 mKey = key;
949 mRank = rank;
950 mIsAmbient = isAmbient;
Christoph Studerce7d6d22014-08-26 19:21:31 +0200951 mMatchesInterruptionFilter = matchesInterruptionFilter;
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400952 mVisibilityOverride = visibilityOverride;
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500953 mSuppressedVisualEffects = suppressedVisualEffects;
Christoph Studerd0694b62014-06-04 16:36:01 +0200954 }
Christoph Studer05ad4822014-05-16 14:16:03 +0200955 }
956
957 /**
958 * Provides access to ranking information on currently active
959 * notifications.
960 *
961 * <p>
962 * Note that this object represents a ranking snapshot that only applies to
963 * notifications active at the time of retrieval.
964 */
Christoph Studerd0694b62014-06-04 16:36:01 +0200965 public static class RankingMap implements Parcelable {
Christoph Studer05ad4822014-05-16 14:16:03 +0200966 private final NotificationRankingUpdate mRankingUpdate;
Christoph Studerdda48f12014-07-29 23:13:16 +0200967 private ArrayMap<String,Integer> mRanks;
968 private ArraySet<Object> mIntercepted;
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400969 private ArrayMap<String, Integer> mVisibilityOverrides;
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500970 private ArrayMap<String, Integer> mSuppressedVisualEffects;
Christoph Studer05ad4822014-05-16 14:16:03 +0200971
Christoph Studerd0694b62014-06-04 16:36:01 +0200972 private RankingMap(NotificationRankingUpdate rankingUpdate) {
Christoph Studer05ad4822014-05-16 14:16:03 +0200973 mRankingUpdate = rankingUpdate;
974 }
975
976 /**
977 * Request the list of notification keys in their current ranking
978 * order.
979 *
980 * @return An array of active notification keys, in their ranking order.
981 */
982 public String[] getOrderedKeys() {
983 return mRankingUpdate.getOrderedKeys();
984 }
985
986 /**
Christoph Studer1d599da2014-06-12 15:25:59 +0200987 * Populates outRanking with ranking information for the notification
988 * with the given key.
Christoph Studer05ad4822014-05-16 14:16:03 +0200989 *
Christoph Studer1d599da2014-06-12 15:25:59 +0200990 * @return true if a valid key has been passed and outRanking has
991 * been populated; false otherwise
Christoph Studer05ad4822014-05-16 14:16:03 +0200992 */
Christoph Studer1d599da2014-06-12 15:25:59 +0200993 public boolean getRanking(String key, Ranking outRanking) {
994 int rank = getRank(key);
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400995 outRanking.populate(key, rank, isAmbient(key), !isIntercepted(key),
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500996 getVisibilityOverride(key), getSuppressedVisualEffects(key));
Christoph Studer1d599da2014-06-12 15:25:59 +0200997 return rank >= 0;
Christoph Studerd0694b62014-06-04 16:36:01 +0200998 }
999
Christoph Studer1d599da2014-06-12 15:25:59 +02001000 private int getRank(String key) {
Christoph Studerdda48f12014-07-29 23:13:16 +02001001 synchronized (this) {
1002 if (mRanks == null) {
1003 buildRanksLocked();
Christoph Studer05ad4822014-05-16 14:16:03 +02001004 }
1005 }
Christoph Studerdda48f12014-07-29 23:13:16 +02001006 Integer rank = mRanks.get(key);
1007 return rank != null ? rank : -1;
Christoph Studer1d599da2014-06-12 15:25:59 +02001008 }
1009
1010 private boolean isAmbient(String key) {
Christoph Studerce2e23f2014-08-03 20:12:28 +02001011 int firstAmbientIndex = mRankingUpdate.getFirstAmbientIndex();
1012 if (firstAmbientIndex < 0) {
1013 return false;
1014 }
Christoph Studer1d599da2014-06-12 15:25:59 +02001015 int rank = getRank(key);
Christoph Studerce2e23f2014-08-03 20:12:28 +02001016 return rank >= 0 && rank >= firstAmbientIndex;
Christoph Studer1d599da2014-06-12 15:25:59 +02001017 }
1018
1019 private boolean isIntercepted(String key) {
Christoph Studerdda48f12014-07-29 23:13:16 +02001020 synchronized (this) {
1021 if (mIntercepted == null) {
1022 buildInterceptedSetLocked();
Christoph Studer1d599da2014-06-12 15:25:59 +02001023 }
1024 }
Christoph Studerdda48f12014-07-29 23:13:16 +02001025 return mIntercepted.contains(key);
1026 }
1027
Chris Wren3ad4e3a2014-09-02 17:23:51 -04001028 private int getVisibilityOverride(String key) {
1029 synchronized (this) {
1030 if (mVisibilityOverrides == null) {
1031 buildVisibilityOverridesLocked();
1032 }
1033 }
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05001034 Integer override = mVisibilityOverrides.get(key);
1035 if (override == null) {
Chris Wren3ad4e3a2014-09-02 17:23:51 -04001036 return Ranking.VISIBILITY_NO_OVERRIDE;
1037 }
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05001038 return override.intValue();
1039 }
1040
1041 private int getSuppressedVisualEffects(String key) {
1042 synchronized (this) {
1043 if (mSuppressedVisualEffects == null) {
1044 buildSuppressedVisualEffectsLocked();
1045 }
1046 }
1047 Integer suppressed = mSuppressedVisualEffects.get(key);
1048 if (suppressed == null) {
1049 return 0;
1050 }
1051 return suppressed.intValue();
Chris Wren3ad4e3a2014-09-02 17:23:51 -04001052 }
1053
Christoph Studerdda48f12014-07-29 23:13:16 +02001054 // Locked by 'this'
1055 private void buildRanksLocked() {
1056 String[] orderedKeys = mRankingUpdate.getOrderedKeys();
1057 mRanks = new ArrayMap<>(orderedKeys.length);
1058 for (int i = 0; i < orderedKeys.length; i++) {
1059 String key = orderedKeys[i];
1060 mRanks.put(key, i);
1061 }
1062 }
1063
1064 // Locked by 'this'
1065 private void buildInterceptedSetLocked() {
1066 String[] dndInterceptedKeys = mRankingUpdate.getInterceptedKeys();
1067 mIntercepted = new ArraySet<>(dndInterceptedKeys.length);
1068 Collections.addAll(mIntercepted, dndInterceptedKeys);
Christoph Studer05ad4822014-05-16 14:16:03 +02001069 }
1070
Chris Wren3ad4e3a2014-09-02 17:23:51 -04001071 // Locked by 'this'
1072 private void buildVisibilityOverridesLocked() {
1073 Bundle visibilityBundle = mRankingUpdate.getVisibilityOverrides();
1074 mVisibilityOverrides = new ArrayMap<>(visibilityBundle.size());
1075 for (String key: visibilityBundle.keySet()) {
1076 mVisibilityOverrides.put(key, visibilityBundle.getInt(key));
1077 }
1078 }
1079
Julia Reynoldsf612869ae2015-11-05 16:48:55 -05001080 // Locked by 'this'
1081 private void buildSuppressedVisualEffectsLocked() {
1082 Bundle suppressedBundle = mRankingUpdate.getSuppressedVisualEffects();
1083 mSuppressedVisualEffects = new ArrayMap<>(suppressedBundle.size());
1084 for (String key: suppressedBundle.keySet()) {
1085 mSuppressedVisualEffects.put(key, suppressedBundle.getInt(key));
1086 }
1087 }
1088
Christoph Studer05ad4822014-05-16 14:16:03 +02001089 // ----------- Parcelable
1090
1091 @Override
1092 public int describeContents() {
1093 return 0;
1094 }
1095
1096 @Override
1097 public void writeToParcel(Parcel dest, int flags) {
1098 dest.writeParcelable(mRankingUpdate, flags);
1099 }
1100
Christoph Studerd0694b62014-06-04 16:36:01 +02001101 public static final Creator<RankingMap> CREATOR = new Creator<RankingMap>() {
Christoph Studer05ad4822014-05-16 14:16:03 +02001102 @Override
Christoph Studerd0694b62014-06-04 16:36:01 +02001103 public RankingMap createFromParcel(Parcel source) {
Christoph Studer05ad4822014-05-16 14:16:03 +02001104 NotificationRankingUpdate rankingUpdate = source.readParcelable(null);
Christoph Studerd0694b62014-06-04 16:36:01 +02001105 return new RankingMap(rankingUpdate);
Christoph Studer05ad4822014-05-16 14:16:03 +02001106 }
1107
1108 @Override
Christoph Studerd0694b62014-06-04 16:36:01 +02001109 public RankingMap[] newArray(int size) {
1110 return new RankingMap[size];
Christoph Studer05ad4822014-05-16 14:16:03 +02001111 }
1112 };
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001113 }
1114}