Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 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 | |
| 17 | package android.service.notification; |
| 18 | |
Jeff Brown | 5c507c1 | 2014-06-05 17:14:39 -0700 | [diff] [blame] | 19 | import android.annotation.SystemApi; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 20 | import android.annotation.SdkConstant; |
| 21 | import android.app.INotificationManager; |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 22 | import android.app.Notification; |
| 23 | import android.app.Notification.Builder; |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 24 | import android.app.NotificationManager; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 25 | import android.app.Service; |
Chris Wren | 1941fc7 | 2014-05-14 15:20:51 -0400 | [diff] [blame] | 26 | import android.content.ComponentName; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 29 | import android.content.pm.ParceledListSlice; |
Daniel Sandler | f5a7838 | 2015-05-15 23:59:36 -0400 | [diff] [blame] | 30 | import android.graphics.drawable.BitmapDrawable; |
| 31 | import android.graphics.drawable.Drawable; |
| 32 | import android.graphics.drawable.Icon; |
| 33 | import android.graphics.Bitmap; |
Julia Reynolds | d9228f1 | 2015-10-20 10:37:27 -0400 | [diff] [blame] | 34 | import android.os.Build; |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 35 | import android.os.Bundle; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 36 | import android.os.IBinder; |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 37 | import android.os.Parcel; |
| 38 | import android.os.Parcelable; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 39 | import android.os.RemoteException; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 40 | import android.os.ServiceManager; |
Christoph Studer | dda48f1 | 2014-07-29 23:13:16 +0200 | [diff] [blame] | 41 | import android.util.ArrayMap; |
| 42 | import android.util.ArraySet; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 43 | import android.util.Log; |
| 44 | |
Chris Wren | 24fb894 | 2015-06-18 14:33:56 -0400 | [diff] [blame] | 45 | import java.util.ArrayList; |
Christoph Studer | dda48f1 | 2014-07-29 23:13:16 +0200 | [diff] [blame] | 46 | import java.util.Collections; |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 47 | import java.util.List; |
| 48 | |
Scott Main | 04667da | 2013-04-25 16:57:16 -0700 | [diff] [blame] | 49 | /** |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 50 | * A service that receives calls from the system when new notifications are |
| 51 | * posted or removed, or their ranking changed. |
Scott Main | 04667da | 2013-04-25 16:57:16 -0700 | [diff] [blame] | 52 | * <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 | * <service android:name=".NotificationListener" |
| 57 | * android:label="@string/service_name" |
| 58 | * android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> |
| 59 | * <intent-filter> |
| 60 | * <action android:name="android.service.notification.NotificationListenerService" /> |
| 61 | * </intent-filter> |
| 62 | * </service></pre> |
| 63 | */ |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 64 | public abstract class NotificationListenerService extends Service { |
| 65 | // TAG = "NotificationListenerService[MySubclass]" |
| 66 | private final String TAG = NotificationListenerService.class.getSimpleName() |
| 67 | + "[" + getClass().getSimpleName() + "]"; |
| 68 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 69 | /** |
| 70 | * {@link #getCurrentInterruptionFilter() Interruption filter} constant - |
| 71 | * Normal interruption filter. |
| 72 | */ |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 73 | public static final int INTERRUPTION_FILTER_ALL |
| 74 | = NotificationManager.INTERRUPTION_FILTER_ALL; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 75 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 76 | /** |
| 77 | * {@link #getCurrentInterruptionFilter() Interruption filter} constant - |
| 78 | * Priority interruption filter. |
| 79 | */ |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 80 | public static final int INTERRUPTION_FILTER_PRIORITY |
| 81 | = NotificationManager.INTERRUPTION_FILTER_PRIORITY; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 82 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 83 | /** |
| 84 | * {@link #getCurrentInterruptionFilter() Interruption filter} constant - |
| 85 | * No interruptions filter. |
| 86 | */ |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 87 | public static final int INTERRUPTION_FILTER_NONE |
| 88 | = NotificationManager.INTERRUPTION_FILTER_NONE; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 89 | |
John Spurlock | 4f1163c | 2015-04-02 17:41:21 -0400 | [diff] [blame] | 90 | /** |
| 91 | * {@link #getCurrentInterruptionFilter() Interruption filter} constant - |
| 92 | * Alarms only interruption filter. |
| 93 | */ |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 94 | public static final int INTERRUPTION_FILTER_ALARMS |
| 95 | = NotificationManager.INTERRUPTION_FILTER_ALARMS; |
John Spurlock | 4f1163c | 2015-04-02 17:41:21 -0400 | [diff] [blame] | 96 | |
John Spurlock | 8310410 | 2015-02-12 23:25:12 -0500 | [diff] [blame] | 97 | /** {@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 Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 103 | public static final int INTERRUPTION_FILTER_UNKNOWN |
| 104 | = NotificationManager.INTERRUPTION_FILTER_UNKNOWN; |
John Spurlock | 8310410 | 2015-02-12 23:25:12 -0500 | [diff] [blame] | 105 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 106 | /** {@link #getCurrentListenerHints() Listener hints} constant - the primary device UI |
| 107 | * should disable notification sound, vibrating and other visual or aural effects. |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 108 | * This does not change the interruption filter, only the effects. **/ |
| 109 | public static final int HINT_HOST_DISABLE_EFFECTS = 1; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 110 | |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 111 | 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 Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 116 | /** |
| 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 Studer | 223f44e | 2014-09-02 14:59:32 +0200 | [diff] [blame] | 136 | * <li>{@link Notification#EXTRA_BIG_TEXT extras[EXTRA_BIG_TEXT]}</li> |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 137 | * </ol> |
| 138 | * |
| 139 | * @hide |
| 140 | */ |
| 141 | @SystemApi |
| 142 | public static final int TRIM_LIGHT = 1; |
| 143 | |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 144 | private INotificationListenerWrapper mWrapper = null; |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 145 | private RankingMap mRankingMap; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 146 | |
| 147 | private INotificationManager mNoMan; |
| 148 | |
Chris Wren | 1941fc7 | 2014-05-14 15:20:51 -0400 | [diff] [blame] | 149 | /** Only valid after a successful call to (@link registerAsService}. */ |
| 150 | private int mCurrentUser; |
| 151 | |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 152 | |
| 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 Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 157 | /** |
| 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 Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 171 | 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 Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 187 | |
| 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 Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 193 | * <P> |
| 194 | * NOTE: The {@link StatusBarNotification} object you receive will be "light"; that is, the |
Scott Main | 04667da | 2013-04-25 16:57:16 -0700 | [diff] [blame] | 195 | * result from {@link StatusBarNotification#getNotification} may be missing some heavyweight |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 196 | * 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 Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 200 | * |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 201 | * @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 Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 204 | */ |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 205 | 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 Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 232 | |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 233 | /** |
| 234 | * Implement this method to learn about when the listener is enabled and connected to |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 235 | * the notification manager. You are safe to call {@link #getActiveNotifications()} |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 236 | * at this time. |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 237 | */ |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 238 | public void onListenerConnected() { |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 239 | // optional |
| 240 | } |
| 241 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 242 | /** |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 243 | * Implement this method to be notified when the notification ranking changes. |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 244 | * |
| 245 | * @param rankingMap The current ranking map that can be used to retrieve ranking information |
| 246 | * for active notifications. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 247 | */ |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 248 | public void onNotificationRankingUpdate(RankingMap rankingMap) { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 249 | // optional |
| 250 | } |
| 251 | |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 252 | /** |
| 253 | * Implement this method to be notified when the |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 254 | * {@link #getCurrentListenerHints() Listener hints} change. |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 255 | * |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 256 | * @param hints The current {@link #getCurrentListenerHints() listener hints}. |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 257 | */ |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 258 | public void onListenerHintsChanged(int hints) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 259 | // optional |
| 260 | } |
| 261 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 262 | /** |
| 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 Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 273 | 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 Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 300 | * <p> |
| 301 | * @deprecated Use {@link #cancelNotification(String key)} |
Dianne Hackborn | 955d8d6 | 2014-10-07 20:17:19 -0700 | [diff] [blame] | 302 | * instead. Beginning with {@link android.os.Build.VERSION_CODES#LOLLIPOP} this method will no longer |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 303 | * cancel the notification. It will continue to cancel the notification for applications |
Dianne Hackborn | 955d8d6 | 2014-10-07 20:17:19 -0700 | [diff] [blame] | 304 | * whose {@code targetSdkVersion} is earlier than {@link android.os.Build.VERSION_CODES#LOLLIPOP}. |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 305 | */ |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 306 | public final void cancelNotification(String pkg, String tag, int id) { |
John Spurlock | da9a3be | 2014-02-12 12:12:26 -0500 | [diff] [blame] | 307 | if (!isBound()) return; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 308 | try { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 309 | 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 Sandler | f5a7838 | 2015-05-15 23:59:36 -0400 | [diff] [blame] | 336 | new String[] { key }); |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 337 | } 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 Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 351 | * {@see #cancelNotification(String, String, int)} |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 352 | */ |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 353 | public final void cancelAllNotifications() { |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 354 | 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 Spurlock | da9a3be | 2014-02-12 12:12:26 -0500 | [diff] [blame] | 368 | if (!isBound()) return; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 369 | try { |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 370 | getNotificationInterface().cancelNotificationsFromListener(mWrapper, keys); |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 371 | } catch (android.os.RemoteException ex) { |
| 372 | Log.v(TAG, "Unable to contact notification manager", ex); |
| 373 | } |
| 374 | } |
| 375 | |
Daniel Sandler | 25cf8ce | 2013-04-24 15:34:57 -0400 | [diff] [blame] | 376 | /** |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 377 | * Inform the notification manager that these notifications have been viewed by the |
Amith Yamasani | c6ecbce | 2015-06-23 12:58:43 -0700 | [diff] [blame] | 378 | * 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 Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 381 | * @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 Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 393 | * 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 Sandler | 25cf8ce | 2013-04-24 15:34:57 -0400 | [diff] [blame] | 419 | * Request the list of outstanding notifications (that is, those that are visible to the |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 420 | * current user). Useful when you don't know what's already been posted. |
Daniel Sandler | 25cf8ce | 2013-04-24 15:34:57 -0400 | [diff] [blame] | 421 | * |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 422 | * @return An array of active notifications, sorted in natural order. |
Daniel Sandler | 25cf8ce | 2013-04-24 15:34:57 -0400 | [diff] [blame] | 423 | */ |
| 424 | public StatusBarNotification[] getActiveNotifications() { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 425 | 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 Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 440 | } |
| 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 Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 447 | * @param keys the keys of the notifications to request |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 448 | * @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 Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 452 | 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 Sandler | 25cf8ce | 2013-04-24 15:34:57 -0400 | [diff] [blame] | 471 | try { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 472 | ParceledListSlice<StatusBarNotification> parceledList = getNotificationInterface() |
| 473 | .getActiveNotificationsFromListener(mWrapper, keys, trim); |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 474 | List<StatusBarNotification> list = parceledList.getList(); |
Chris Wren | 24fb894 | 2015-06-18 14:33:56 -0400 | [diff] [blame] | 475 | ArrayList<StatusBarNotification> corruptNotifications = null; |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 476 | int N = list.size(); |
| 477 | for (int i = 0; i < N; i++) { |
Chris Wren | 24fb894 | 2015-06-18 14:33:56 -0400 | [diff] [blame] | 478 | StatusBarNotification sbn = list.get(i); |
| 479 | Notification notification = sbn.getNotification(); |
| 480 | try { |
Chris Wren | 24fb894 | 2015-06-18 14:33:56 -0400 | [diff] [blame] | 481 | // convert icon metadata to legacy format for older clients |
| 482 | createLegacyIconExtras(notification); |
Julia Reynolds | d9228f1 | 2015-10-20 10:37:27 -0400 | [diff] [blame] | 483 | // populate remote views for older clients. |
| 484 | maybePopulateRemoteViews(notification); |
Chris Wren | 24fb894 | 2015-06-18 14:33:56 -0400 | [diff] [blame] | 485 | } 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 Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 493 | } |
Chris Wren | 24fb894 | 2015-06-18 14:33:56 -0400 | [diff] [blame] | 494 | if (corruptNotifications != null) { |
| 495 | list.removeAll(corruptNotifications); |
| 496 | } |
| 497 | return list.toArray(new StatusBarNotification[list.size()]); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 498 | } catch (android.os.RemoteException ex) { |
| 499 | Log.v(TAG, "Unable to contact notification manager", ex); |
| 500 | } |
| 501 | return null; |
| 502 | } |
| 503 | |
| 504 | /** |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 505 | * Gets the set of hints representing current state. |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 506 | * |
| 507 | * <p> |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 508 | * The current state may differ from the requested state if the hint represents state |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 509 | * shared across all listeners or a feature the notification host does not support or refuses |
| 510 | * to grant. |
| 511 | * |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 512 | * @return Zero or more of the HINT_ constants. |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 513 | */ |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 514 | public final int getCurrentListenerHints() { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 515 | if (!isBound()) return 0; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 516 | try { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 517 | return getNotificationInterface().getHintsFromListener(mWrapper); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 518 | } catch (android.os.RemoteException ex) { |
| 519 | Log.v(TAG, "Unable to contact notification manager", ex); |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 520 | 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 & 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 Spurlock | 8310410 | 2015-02-12 23:25:12 -0500 | [diff] [blame] | 539 | * @return One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when |
| 540 | * unavailable. |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 541 | */ |
| 542 | public final int getCurrentInterruptionFilter() { |
John Spurlock | 8310410 | 2015-02-12 23:25:12 -0500 | [diff] [blame] | 543 | if (!isBound()) return INTERRUPTION_FILTER_UNKNOWN; |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 544 | try { |
Chris Wren | 957ed70 | 2014-09-24 18:17:36 -0400 | [diff] [blame] | 545 | return getNotificationInterface().getInterruptionFilterFromListener(mWrapper); |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 546 | } catch (android.os.RemoteException ex) { |
| 547 | Log.v(TAG, "Unable to contact notification manager", ex); |
John Spurlock | 8310410 | 2015-02-12 23:25:12 -0500 | [diff] [blame] | 548 | return INTERRUPTION_FILTER_UNKNOWN; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
| 552 | /** |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 553 | * Sets the desired {@link #getCurrentListenerHints() listener hints}. |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 554 | * |
| 555 | * <p> |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 556 | * This is merely a request, the host may or may not choose to take action depending |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 557 | * on other listener requests or other global state. |
| 558 | * <p> |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 559 | * Listen for updates using {@link #onListenerHintsChanged(int)}. |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 560 | * |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 561 | * @param hints One or more of the HINT_ constants. |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 562 | */ |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 563 | public final void requestListenerHints(int hints) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 564 | if (!isBound()) return; |
| 565 | try { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 566 | getNotificationInterface().requestHintsFromListener(mWrapper, hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 567 | } catch (android.os.RemoteException ex) { |
| 568 | Log.v(TAG, "Unable to contact notification manager", ex); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | /** |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 573 | * 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 Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 594 | * Returns current ranking information. |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 595 | * |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 596 | * <p> |
| 597 | * The returned object represents the current ranking snapshot and only |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 598 | * 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 Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 606 | * |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 607 | * @return A {@link RankingMap} object providing access to ranking information |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 608 | */ |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 609 | public RankingMap getCurrentRanking() { |
| 610 | return mRankingMap; |
Daniel Sandler | 25cf8ce | 2013-04-24 15:34:57 -0400 | [diff] [blame] | 611 | } |
| 612 | |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 613 | @Override |
| 614 | public IBinder onBind(Intent intent) { |
| 615 | if (mWrapper == null) { |
| 616 | mWrapper = new INotificationListenerWrapper(); |
| 617 | } |
| 618 | return mWrapper; |
| 619 | } |
| 620 | |
John Spurlock | da9a3be | 2014-02-12 12:12:26 -0500 | [diff] [blame] | 621 | 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 Wren | 1941fc7 | 2014-05-14 15:20:51 -0400 | [diff] [blame] | 629 | /** |
| 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 Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 635 | * @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 Wren | 1941fc7 | 2014-05-14 15:20:51 -0400 | [diff] [blame] | 637 | * @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 Brown | 5c507c1 | 2014-06-05 17:14:39 -0700 | [diff] [blame] | 641 | @SystemApi |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 642 | public void registerAsSystemService(Context context, ComponentName componentName, |
| 643 | int currentUser) throws RemoteException { |
| 644 | mSystemContext = context; |
Chris Wren | 1941fc7 | 2014-05-14 15:20:51 -0400 | [diff] [blame] | 645 | 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 Brown | 5c507c1 | 2014-06-05 17:14:39 -0700 | [diff] [blame] | 660 | @SystemApi |
Chris Wren | 1941fc7 | 2014-05-14 15:20:51 -0400 | [diff] [blame] | 661 | public void unregisterAsSystemService() throws RemoteException { |
| 662 | if (mWrapper != null) { |
| 663 | INotificationManager noMan = getNotificationInterface(); |
| 664 | noMan.unregisterListener(mWrapper, mCurrentUser); |
| 665 | } |
| 666 | } |
| 667 | |
Daniel Sandler | f5a7838 | 2015-05-15 23:59:36 -0400 | [diff] [blame] | 668 | /** 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 Sandler | 99a37f1 | 2015-06-09 14:34:38 -0400 | [diff] [blame] | 672 | if (smallIcon != null && smallIcon.getType() == Icon.TYPE_RESOURCE) { |
Daniel Sandler | f5a7838 | 2015-05-15 23:59:36 -0400 | [diff] [blame] | 673 | 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 Reynolds | d9228f1 | 2015-10-20 10:37:27 -0400 | [diff] [blame] | 686 | /** |
| 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 Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 698 | private class INotificationListenerWrapper extends INotificationListener.Stub { |
| 699 | @Override |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 700 | public void onNotificationPosted(IStatusBarNotificationHolder sbnHolder, |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 701 | NotificationRankingUpdate update) { |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 702 | 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 Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 709 | |
Chris Wren | 24fb894 | 2015-06-18 14:33:56 -0400 | [diff] [blame] | 710 | try { |
Julia Reynolds | d9228f1 | 2015-10-20 10:37:27 -0400 | [diff] [blame] | 711 | Notification notification = sbn.getNotification(); |
Chris Wren | 24fb894 | 2015-06-18 14:33:56 -0400 | [diff] [blame] | 712 | // convert icon metadata to legacy format for older clients |
| 713 | createLegacyIconExtras(sbn.getNotification()); |
Julia Reynolds | d9228f1 | 2015-10-20 10:37:27 -0400 | [diff] [blame] | 714 | maybePopulateRemoteViews(sbn.getNotification()); |
Chris Wren | 24fb894 | 2015-06-18 14:33:56 -0400 | [diff] [blame] | 715 | } 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 Sandler | f5a7838 | 2015-05-15 23:59:36 -0400 | [diff] [blame] | 721 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 722 | // protect subclass from concurrent modifications of (@link mNotificationKeys}. |
| 723 | synchronized (mWrapper) { |
| 724 | applyUpdate(update); |
| 725 | try { |
Chris Wren | 24fb894 | 2015-06-18 14:33:56 -0400 | [diff] [blame] | 726 | 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 Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 732 | } catch (Throwable t) { |
| 733 | Log.w(TAG, "Error running onNotificationPosted", t); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 734 | } |
John Spurlock | c133ab8 | 2013-06-10 15:16:22 -0400 | [diff] [blame] | 735 | } |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 736 | } |
| 737 | @Override |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 738 | public void onNotificationRemoved(IStatusBarNotificationHolder sbnHolder, |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 739 | NotificationRankingUpdate update) { |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 740 | 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 Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 747 | // protect subclass from concurrent modifications of (@link mNotificationKeys}. |
| 748 | synchronized (mWrapper) { |
| 749 | applyUpdate(update); |
| 750 | try { |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 751 | NotificationListenerService.this.onNotificationRemoved(sbn, mRankingMap); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 752 | } catch (Throwable t) { |
| 753 | Log.w(TAG, "Error running onNotificationRemoved", t); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 754 | } |
John Spurlock | c133ab8 | 2013-06-10 15:16:22 -0400 | [diff] [blame] | 755 | } |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 756 | } |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 757 | @Override |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 758 | public void onListenerConnected(NotificationRankingUpdate update) { |
| 759 | // protect subclass from concurrent modifications of (@link mNotificationKeys}. |
| 760 | synchronized (mWrapper) { |
| 761 | applyUpdate(update); |
| 762 | try { |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 763 | NotificationListenerService.this.onListenerConnected(); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 764 | } catch (Throwable t) { |
| 765 | Log.w(TAG, "Error running onListenerConnected", t); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 766 | } |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 767 | } |
| 768 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 769 | @Override |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 770 | public void onNotificationRankingUpdate(NotificationRankingUpdate update) |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 771 | throws RemoteException { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 772 | // protect subclass from concurrent modifications of (@link mNotificationKeys}. |
| 773 | synchronized (mWrapper) { |
| 774 | applyUpdate(update); |
| 775 | try { |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 776 | NotificationListenerService.this.onNotificationRankingUpdate(mRankingMap); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 777 | } catch (Throwable t) { |
| 778 | Log.w(TAG, "Error running onNotificationRankingUpdate", t); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 779 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 780 | } |
| 781 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 782 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 783 | public void onListenerHintsChanged(int hints) throws RemoteException { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 784 | try { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 785 | NotificationListenerService.this.onListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 786 | } catch (Throwable t) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 787 | Log.w(TAG, "Error running onListenerHintsChanged", t); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 788 | } |
| 789 | } |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 790 | |
| 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 Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 799 | } |
| 800 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 801 | private void applyUpdate(NotificationRankingUpdate update) { |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 802 | mRankingMap = new RankingMap(update); |
| 803 | } |
| 804 | |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 805 | private Context getContext() { |
| 806 | if (mSystemContext != null) { |
| 807 | return mSystemContext; |
| 808 | } |
| 809 | return this; |
| 810 | } |
| 811 | |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 812 | /** |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 813 | * Stores ranking related information on a currently active notification. |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 814 | * |
| 815 | * <p> |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 816 | * 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 Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 819 | */ |
| 820 | public static class Ranking { |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 821 | /** 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 Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 825 | /** |
| 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 Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 860 | private String mKey; |
| 861 | private int mRank = -1; |
| 862 | private boolean mIsAmbient; |
Christoph Studer | ce7d6d2 | 2014-08-26 19:21:31 +0200 | [diff] [blame] | 863 | private boolean mMatchesInterruptionFilter; |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 864 | private int mVisibilityOverride; |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 865 | private int mSuppressedVisualEffects; |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 866 | |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 867 | public Ranking() {} |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 868 | |
| 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 Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 895 | * 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 Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 905 | /** |
| 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 Studer | ce7d6d2 | 2014-08-26 19:21:31 +0200 | [diff] [blame] | 913 | /** |
| 914 | * Returns whether the notification matches the user's interruption |
| 915 | * filter. |
Chris Wren | 0fef44d | 2014-09-30 13:05:14 -0400 | [diff] [blame] | 916 | * |
| 917 | * @return {@code true} if the notification is allowed by the filter, or |
| 918 | * {@code false} if it is blocked. |
Christoph Studer | ce7d6d2 | 2014-08-26 19:21:31 +0200 | [diff] [blame] | 919 | */ |
| 920 | public boolean matchesInterruptionFilter() { |
| 921 | return mMatchesInterruptionFilter; |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 922 | } |
| 923 | |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 924 | /** |
| 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 Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 945 | private void populate(String key, int rank, boolean isAmbient, |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 946 | boolean matchesInterruptionFilter, int visibilityOverride, |
| 947 | int suppressedVisualEffects) { |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 948 | mKey = key; |
| 949 | mRank = rank; |
| 950 | mIsAmbient = isAmbient; |
Christoph Studer | ce7d6d2 | 2014-08-26 19:21:31 +0200 | [diff] [blame] | 951 | mMatchesInterruptionFilter = matchesInterruptionFilter; |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 952 | mVisibilityOverride = visibilityOverride; |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 953 | mSuppressedVisualEffects = suppressedVisualEffects; |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 954 | } |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 955 | } |
| 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 Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 965 | public static class RankingMap implements Parcelable { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 966 | private final NotificationRankingUpdate mRankingUpdate; |
Christoph Studer | dda48f1 | 2014-07-29 23:13:16 +0200 | [diff] [blame] | 967 | private ArrayMap<String,Integer> mRanks; |
| 968 | private ArraySet<Object> mIntercepted; |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 969 | private ArrayMap<String, Integer> mVisibilityOverrides; |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 970 | private ArrayMap<String, Integer> mSuppressedVisualEffects; |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 971 | |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 972 | private RankingMap(NotificationRankingUpdate rankingUpdate) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 973 | 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 Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 987 | * Populates outRanking with ranking information for the notification |
| 988 | * with the given key. |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 989 | * |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 990 | * @return true if a valid key has been passed and outRanking has |
| 991 | * been populated; false otherwise |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 992 | */ |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 993 | public boolean getRanking(String key, Ranking outRanking) { |
| 994 | int rank = getRank(key); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 995 | outRanking.populate(key, rank, isAmbient(key), !isIntercepted(key), |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 996 | getVisibilityOverride(key), getSuppressedVisualEffects(key)); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 997 | return rank >= 0; |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 998 | } |
| 999 | |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 1000 | private int getRank(String key) { |
Christoph Studer | dda48f1 | 2014-07-29 23:13:16 +0200 | [diff] [blame] | 1001 | synchronized (this) { |
| 1002 | if (mRanks == null) { |
| 1003 | buildRanksLocked(); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 1004 | } |
| 1005 | } |
Christoph Studer | dda48f1 | 2014-07-29 23:13:16 +0200 | [diff] [blame] | 1006 | Integer rank = mRanks.get(key); |
| 1007 | return rank != null ? rank : -1; |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | private boolean isAmbient(String key) { |
Christoph Studer | ce2e23f | 2014-08-03 20:12:28 +0200 | [diff] [blame] | 1011 | int firstAmbientIndex = mRankingUpdate.getFirstAmbientIndex(); |
| 1012 | if (firstAmbientIndex < 0) { |
| 1013 | return false; |
| 1014 | } |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 1015 | int rank = getRank(key); |
Christoph Studer | ce2e23f | 2014-08-03 20:12:28 +0200 | [diff] [blame] | 1016 | return rank >= 0 && rank >= firstAmbientIndex; |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | private boolean isIntercepted(String key) { |
Christoph Studer | dda48f1 | 2014-07-29 23:13:16 +0200 | [diff] [blame] | 1020 | synchronized (this) { |
| 1021 | if (mIntercepted == null) { |
| 1022 | buildInterceptedSetLocked(); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 1023 | } |
| 1024 | } |
Christoph Studer | dda48f1 | 2014-07-29 23:13:16 +0200 | [diff] [blame] | 1025 | return mIntercepted.contains(key); |
| 1026 | } |
| 1027 | |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 1028 | private int getVisibilityOverride(String key) { |
| 1029 | synchronized (this) { |
| 1030 | if (mVisibilityOverrides == null) { |
| 1031 | buildVisibilityOverridesLocked(); |
| 1032 | } |
| 1033 | } |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 1034 | Integer override = mVisibilityOverrides.get(key); |
| 1035 | if (override == null) { |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 1036 | return Ranking.VISIBILITY_NO_OVERRIDE; |
| 1037 | } |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 1038 | 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 Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 1052 | } |
| 1053 | |
Christoph Studer | dda48f1 | 2014-07-29 23:13:16 +0200 | [diff] [blame] | 1054 | // 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 Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 1069 | } |
| 1070 | |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 1071 | // 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 Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 1080 | // 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 Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 1089 | // ----------- 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 Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 1101 | public static final Creator<RankingMap> CREATOR = new Creator<RankingMap>() { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 1102 | @Override |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 1103 | public RankingMap createFromParcel(Parcel source) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 1104 | NotificationRankingUpdate rankingUpdate = source.readParcelable(null); |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 1105 | return new RankingMap(rankingUpdate); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | @Override |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 1109 | public RankingMap[] newArray(int size) { |
| 1110 | return new RankingMap[size]; |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 1111 | } |
| 1112 | }; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1113 | } |
| 1114 | } |