Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.service.notification; |
| 18 | |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 19 | import static java.lang.annotation.RetentionPolicy.SOURCE; |
| 20 | |
| 21 | import android.annotation.IntDef; |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 22 | import android.annotation.NonNull; |
Fabian Kozynski | 867550e | 2019-02-28 12:59:57 -0500 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Julia Reynolds | 7eba593 | 2015-12-11 16:40:39 -0500 | [diff] [blame] | 24 | import android.annotation.SdkConstant; |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 25 | import android.annotation.SystemApi; |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 26 | import android.app.Notification; |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 27 | import android.app.NotificationChannel; |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 28 | import android.app.admin.DevicePolicyManager; |
| 29 | import android.content.ComponentName; |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 30 | import android.content.Context; |
Julia Reynolds | 7eba593 | 2015-12-11 16:40:39 -0500 | [diff] [blame] | 31 | import android.content.Intent; |
Svet Ganov | b8f53ee | 2016-02-18 08:38:56 -0800 | [diff] [blame] | 32 | import android.os.Handler; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 33 | import android.os.IBinder; |
Svet Ganov | b8f53ee | 2016-02-18 08:38:56 -0800 | [diff] [blame] | 34 | import android.os.Looper; |
| 35 | import android.os.Message; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 36 | import android.os.RemoteException; |
| 37 | import android.util.Log; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 38 | |
Svet Ganov | b8f53ee | 2016-02-18 08:38:56 -0800 | [diff] [blame] | 39 | import com.android.internal.os.SomeArgs; |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 40 | |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 41 | import java.lang.annotation.Retention; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 42 | import java.util.List; |
| 43 | |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 44 | /** |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 45 | * A service that helps the user manage notifications. |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 46 | * <p> |
| 47 | * Only one notification assistant can be active at a time. Unlike notification listener services, |
| 48 | * assistant services can additionally modify certain aspects about notifications |
| 49 | * (see {@link Adjustment}) before they are posted. |
| 50 | *<p> |
| 51 | * A note about managed profiles: Unlike {@link NotificationListenerService listener services}, |
| 52 | * NotificationAssistantServices are allowed to run in managed profiles |
| 53 | * (see {@link DevicePolicyManager#isManagedProfile(ComponentName)}), so they can access the |
| 54 | * information they need to create good {@link Adjustment adjustments}. To maintain the contract |
| 55 | * with {@link NotificationListenerService}, an assistant service will receive all of the |
| 56 | * callbacks from {@link NotificationListenerService} for the current user, managed profiles of |
| 57 | * that user, and ones that affect all users. However, |
| 58 | * {@link #onNotificationEnqueued(StatusBarNotification)} will only be called for notifications |
| 59 | * sent to the current user, and {@link Adjustment adjuments} will only be accepted for the |
| 60 | * current user. |
Julia Reynolds | fe99219 | 2018-09-07 10:33:19 -0400 | [diff] [blame] | 61 | * <p> |
| 62 | * All callbacks are called on the main thread. |
| 63 | * </p> |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 64 | * @hide |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 65 | */ |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 66 | @SystemApi |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 67 | public abstract class NotificationAssistantService extends NotificationListenerService { |
| 68 | private static final String TAG = "NotificationAssistants"; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 69 | |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 70 | /** @hide */ |
| 71 | @Retention(SOURCE) |
| 72 | @IntDef({SOURCE_FROM_APP, SOURCE_FROM_ASSISTANT}) |
| 73 | public @interface Source {} |
Tony Mak | 9f4e084 | 2019-03-05 16:24:36 +0000 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * To indicate an adjustment is from an app. |
| 77 | */ |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 78 | public static final int SOURCE_FROM_APP = 0; |
Tony Mak | 9f4e084 | 2019-03-05 16:24:36 +0000 | [diff] [blame] | 79 | /** |
| 80 | * To indicate an adjustment is from a {@link NotificationAssistantService}. |
| 81 | */ |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 82 | public static final int SOURCE_FROM_ASSISTANT = 1; |
| 83 | |
Julia Reynolds | 7eba593 | 2015-12-11 16:40:39 -0500 | [diff] [blame] | 84 | /** |
| 85 | * The {@link Intent} that must be declared as handled by the service. |
| 86 | */ |
| 87 | @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) |
| 88 | public static final String SERVICE_INTERFACE |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 89 | = "android.service.notification.NotificationAssistantService"; |
Julia Reynolds | 7eba593 | 2015-12-11 16:40:39 -0500 | [diff] [blame] | 90 | |
Rohan Shah | 0350dab | 2018-05-04 13:42:18 -0700 | [diff] [blame] | 91 | /** |
| 92 | * @hide |
| 93 | */ |
| 94 | protected Handler mHandler; |
Svet Ganov | b8f53ee | 2016-02-18 08:38:56 -0800 | [diff] [blame] | 95 | |
Svet Ganov | b8f53ee | 2016-02-18 08:38:56 -0800 | [diff] [blame] | 96 | @Override |
| 97 | protected void attachBaseContext(Context base) { |
| 98 | super.attachBaseContext(base); |
| 99 | mHandler = new MyHandler(getContext().getMainLooper()); |
| 100 | } |
| 101 | |
| 102 | @Override |
Fabian Kozynski | 867550e | 2019-02-28 12:59:57 -0500 | [diff] [blame] | 103 | public final @NonNull IBinder onBind(@Nullable Intent intent) { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 104 | if (mWrapper == null) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 105 | mWrapper = new NotificationAssistantServiceWrapper(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 106 | } |
| 107 | return mWrapper; |
| 108 | } |
| 109 | |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 110 | /** |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 111 | * A notification was snoozed until a context. For use with |
| 112 | * {@link Adjustment#KEY_SNOOZE_CRITERIA}. When the device reaches the given context, the |
| 113 | * assistant should restore the notification with {@link #unsnoozeNotification(String)}. |
| 114 | * |
| 115 | * @param sbn the notification to snooze |
| 116 | * @param snoozeCriterionId the {@link SnoozeCriterion#getId()} representing a device context. |
| 117 | */ |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 118 | abstract public void onNotificationSnoozedUntilContext(@NonNull StatusBarNotification sbn, |
| 119 | @NonNull String snoozeCriterionId); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 120 | |
| 121 | /** |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 122 | * A notification was posted by an app. Called before post. |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 123 | * |
Julia Reynolds | 79f0277 | 2019-02-15 11:38:48 -0500 | [diff] [blame] | 124 | * <p>Note: this method is only called if you don't override |
| 125 | * {@link #onNotificationEnqueued(StatusBarNotification, NotificationChannel)}.</p> |
| 126 | * |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 127 | * @param sbn the new notification |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 128 | * @return an adjustment or null to take no action, within 100ms. |
| 129 | */ |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 130 | abstract public @Nullable Adjustment onNotificationEnqueued(@NonNull StatusBarNotification sbn); |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * A notification was posted by an app. Called before post. |
| 134 | * |
| 135 | * @param sbn the new notification |
| 136 | * @param channel the channel the notification was posted to |
| 137 | * @return an adjustment or null to take no action, within 100ms. |
| 138 | */ |
Fabian Kozynski | 867550e | 2019-02-28 12:59:57 -0500 | [diff] [blame] | 139 | public @Nullable Adjustment onNotificationEnqueued(@NonNull StatusBarNotification sbn, |
| 140 | @NonNull NotificationChannel channel) { |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 141 | return onNotificationEnqueued(sbn); |
| 142 | } |
| 143 | |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 144 | |
| 145 | /** |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 146 | * Implement this method to learn when notifications are removed, how they were interacted with |
| 147 | * before removal, and why they were removed. |
| 148 | * <p> |
| 149 | * This might occur because the user has dismissed the notification using system UI (or another |
| 150 | * notification listener) or because the app has withdrawn the notification. |
| 151 | * <p> |
| 152 | * NOTE: The {@link StatusBarNotification} object you receive will be "light"; that is, the |
| 153 | * result from {@link StatusBarNotification#getNotification} may be missing some heavyweight |
| 154 | * fields such as {@link android.app.Notification#contentView} and |
| 155 | * {@link android.app.Notification#largeIcon}. However, all other fields on |
| 156 | * {@link StatusBarNotification}, sufficient to match this call with a prior call to |
| 157 | * {@link #onNotificationPosted(StatusBarNotification)}, will be intact. |
| 158 | * |
| 159 | ** @param sbn A data structure encapsulating at least the original information (tag and id) |
| 160 | * and source (package name) used to post the {@link android.app.Notification} that |
| 161 | * was just removed. |
| 162 | * @param rankingMap The current ranking map that can be used to retrieve ranking information |
| 163 | * for active notifications. |
| 164 | * @param stats Stats about how the user interacted with the notification before it was removed. |
| 165 | * @param reason see {@link #REASON_LISTENER_CANCEL}, etc. |
| 166 | */ |
| 167 | @Override |
Fabian Kozynski | 867550e | 2019-02-28 12:59:57 -0500 | [diff] [blame] | 168 | public void onNotificationRemoved(@NonNull StatusBarNotification sbn, |
| 169 | @NonNull RankingMap rankingMap, |
| 170 | @NonNull NotificationStats stats, int reason) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 171 | onNotificationRemoved(sbn, rankingMap, reason); |
| 172 | } |
| 173 | |
| 174 | /** |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 175 | * Implement this to know when a user has seen notifications, as triggered by |
| 176 | * {@link #setNotificationsShown(String[])}. |
| 177 | */ |
Fabian Kozynski | 867550e | 2019-02-28 12:59:57 -0500 | [diff] [blame] | 178 | public void onNotificationsSeen(@NonNull List<String> keys) { |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 179 | |
| 180 | } |
| 181 | |
| 182 | /** |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 183 | * Implement this to know when a notification change (expanded / collapsed) is visible to user. |
| 184 | * |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 185 | * @param key the notification key |
| 186 | * @param isUserAction whether the expanded change is caused by user action. |
| 187 | * @param isExpanded whether the notification is expanded. |
| 188 | */ |
| 189 | public void onNotificationExpansionChanged( |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 190 | @NonNull String key, boolean isUserAction, boolean isExpanded) {} |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 191 | |
| 192 | /** |
| 193 | * Implement this to know when a direct reply is sent from a notification. |
| 194 | * @param key the notification key |
| 195 | */ |
Tony Mak | 836147e | 2019-01-08 14:48:31 +0000 | [diff] [blame] | 196 | public void onNotificationDirectReplied(@NonNull String key) {} |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 197 | |
| 198 | /** |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 199 | * Implement this to know when a suggested reply is sent. |
| 200 | * @param key the notification key |
| 201 | * @param reply the reply that is just sent |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 202 | * @param source the source that provided the reply, e.g. SOURCE_FROM_APP |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 203 | */ |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 204 | public void onSuggestedReplySent(@NonNull String key, @NonNull CharSequence reply, |
| 205 | @Source int source) { |
| 206 | } |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 207 | |
| 208 | /** |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 209 | * Implement this to know when an action is clicked. |
| 210 | * @param key the notification key |
| 211 | * @param action the action that is just clicked |
| 212 | * @param source the source that provided the action, e.g. SOURCE_FROM_APP |
| 213 | */ |
Gustav Sennton | ced1d00 | 2019-01-11 16:19:45 +0000 | [diff] [blame] | 214 | public void onActionInvoked(@NonNull String key, @NonNull Notification.Action action, |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 215 | @Source int source) { |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 219 | * Updates a notification. N.B. this won’t cause |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 220 | * an existing notification to alert, but might allow a future update to |
| 221 | * this notification to alert. |
| 222 | * |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 223 | * @param adjustment the adjustment with an explanation |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 224 | */ |
Fabian Kozynski | 867550e | 2019-02-28 12:59:57 -0500 | [diff] [blame] | 225 | public final void adjustNotification(@NonNull Adjustment adjustment) { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 226 | if (!isBound()) return; |
| 227 | try { |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 228 | getNotificationInterface().applyAdjustmentFromAssistant(mWrapper, adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 229 | } catch (android.os.RemoteException ex) { |
| 230 | Log.v(TAG, "Unable to contact notification manager", ex); |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 231 | throw ex.rethrowFromSystemServer(); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Updates existing notifications. Re-ranking won't occur until all adjustments are applied. |
| 237 | * N.B. this won’t cause an existing notification to alert, but might allow a future update to |
| 238 | * these notifications to alert. |
| 239 | * |
| 240 | * @param adjustments a list of adjustments with explanations |
| 241 | */ |
Fabian Kozynski | 867550e | 2019-02-28 12:59:57 -0500 | [diff] [blame] | 242 | public final void adjustNotifications(@NonNull List<Adjustment> adjustments) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 243 | if (!isBound()) return; |
| 244 | try { |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 245 | getNotificationInterface().applyAdjustmentsFromAssistant(mWrapper, adjustments); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 246 | } catch (android.os.RemoteException ex) { |
| 247 | Log.v(TAG, "Unable to contact notification manager", ex); |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 248 | throw ex.rethrowFromSystemServer(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 249 | } |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 250 | } |
| 251 | |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 252 | /** |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 253 | * Inform the notification manager about un-snoozing a specific notification. |
| 254 | * <p> |
Julia Reynolds | 79f0277 | 2019-02-15 11:38:48 -0500 | [diff] [blame] | 255 | * This should only be used for notifications snoozed because of a contextual snooze suggestion |
| 256 | * you provided via {@link Adjustment#KEY_SNOOZE_CRITERIA}. Once un-snoozed, you will get a |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 257 | * {@link #onNotificationPosted(StatusBarNotification, RankingMap)} callback for the |
| 258 | * notification. |
| 259 | * @param key The key of the notification to snooze |
| 260 | */ |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 261 | public final void unsnoozeNotification(@NonNull String key) { |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 262 | if (!isBound()) return; |
| 263 | try { |
| 264 | getNotificationInterface().unsnoozeNotificationFromAssistant(mWrapper, key); |
| 265 | } catch (android.os.RemoteException ex) { |
| 266 | Log.v(TAG, "Unable to contact notification manager", ex); |
| 267 | } |
| 268 | } |
| 269 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 270 | private class NotificationAssistantServiceWrapper extends NotificationListenerWrapper { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 271 | @Override |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 272 | public void onNotificationEnqueuedWithChannel(IStatusBarNotificationHolder sbnHolder, |
| 273 | NotificationChannel channel) { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 274 | StatusBarNotification sbn; |
| 275 | try { |
| 276 | sbn = sbnHolder.get(); |
| 277 | } catch (RemoteException e) { |
| 278 | Log.w(TAG, "onNotificationEnqueued: Error receiving StatusBarNotification", e); |
| 279 | return; |
| 280 | } |
| 281 | |
Svet Ganov | b8f53ee | 2016-02-18 08:38:56 -0800 | [diff] [blame] | 282 | SomeArgs args = SomeArgs.obtain(); |
| 283 | args.arg1 = sbn; |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 284 | args.arg2 = channel; |
Svet Ganov | b8f53ee | 2016-02-18 08:38:56 -0800 | [diff] [blame] | 285 | mHandler.obtainMessage(MyHandler.MSG_ON_NOTIFICATION_ENQUEUED, |
| 286 | args).sendToTarget(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 287 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 288 | |
| 289 | @Override |
| 290 | public void onNotificationSnoozedUntilContext( |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 291 | IStatusBarNotificationHolder sbnHolder, String snoozeCriterionId) { |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 292 | StatusBarNotification sbn; |
| 293 | try { |
| 294 | sbn = sbnHolder.get(); |
| 295 | } catch (RemoteException e) { |
| 296 | Log.w(TAG, "onNotificationSnoozed: Error receiving StatusBarNotification", e); |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | SomeArgs args = SomeArgs.obtain(); |
| 301 | args.arg1 = sbn; |
| 302 | args.arg2 = snoozeCriterionId; |
| 303 | mHandler.obtainMessage(MyHandler.MSG_ON_NOTIFICATION_SNOOZED, |
| 304 | args).sendToTarget(); |
| 305 | } |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 306 | |
| 307 | @Override |
| 308 | public void onNotificationsSeen(List<String> keys) { |
| 309 | SomeArgs args = SomeArgs.obtain(); |
| 310 | args.arg1 = keys; |
| 311 | mHandler.obtainMessage(MyHandler.MSG_ON_NOTIFICATIONS_SEEN, |
| 312 | args).sendToTarget(); |
| 313 | } |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 314 | |
| 315 | @Override |
| 316 | public void onNotificationExpansionChanged(String key, boolean isUserAction, |
| 317 | boolean isExpanded) { |
| 318 | SomeArgs args = SomeArgs.obtain(); |
| 319 | args.arg1 = key; |
| 320 | args.argi1 = isUserAction ? 1 : 0; |
| 321 | args.argi2 = isExpanded ? 1 : 0; |
| 322 | mHandler.obtainMessage(MyHandler.MSG_ON_NOTIFICATION_EXPANSION_CHANGED, args) |
| 323 | .sendToTarget(); |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public void onNotificationDirectReply(String key) { |
| 328 | SomeArgs args = SomeArgs.obtain(); |
| 329 | args.arg1 = key; |
| 330 | mHandler.obtainMessage(MyHandler.MSG_ON_NOTIFICATION_DIRECT_REPLY_SENT, args) |
| 331 | .sendToTarget(); |
| 332 | } |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 333 | |
| 334 | @Override |
| 335 | public void onSuggestedReplySent(String key, CharSequence reply, int source) { |
| 336 | SomeArgs args = SomeArgs.obtain(); |
| 337 | args.arg1 = key; |
| 338 | args.arg2 = reply; |
| 339 | args.argi2 = source; |
| 340 | mHandler.obtainMessage(MyHandler.MSG_ON_SUGGESTED_REPLY_SENT, args).sendToTarget(); |
| 341 | } |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 342 | |
| 343 | @Override |
| 344 | public void onActionClicked(String key, Notification.Action action, int source) { |
| 345 | SomeArgs args = SomeArgs.obtain(); |
| 346 | args.arg1 = key; |
| 347 | args.arg2 = action; |
| 348 | args.argi2 = source; |
Gustav Sennton | ced1d00 | 2019-01-11 16:19:45 +0000 | [diff] [blame] | 349 | mHandler.obtainMessage(MyHandler.MSG_ON_ACTION_INVOKED, args).sendToTarget(); |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 350 | } |
Svet Ganov | b8f53ee | 2016-02-18 08:38:56 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | private final class MyHandler extends Handler { |
| 354 | public static final int MSG_ON_NOTIFICATION_ENQUEUED = 1; |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 355 | public static final int MSG_ON_NOTIFICATION_SNOOZED = 2; |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 356 | public static final int MSG_ON_NOTIFICATIONS_SEEN = 3; |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 357 | public static final int MSG_ON_NOTIFICATION_EXPANSION_CHANGED = 4; |
| 358 | public static final int MSG_ON_NOTIFICATION_DIRECT_REPLY_SENT = 5; |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 359 | public static final int MSG_ON_SUGGESTED_REPLY_SENT = 6; |
Gustav Sennton | ced1d00 | 2019-01-11 16:19:45 +0000 | [diff] [blame] | 360 | public static final int MSG_ON_ACTION_INVOKED = 7; |
Svet Ganov | b8f53ee | 2016-02-18 08:38:56 -0800 | [diff] [blame] | 361 | |
| 362 | public MyHandler(Looper looper) { |
| 363 | super(looper, null, false); |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public void handleMessage(Message msg) { |
| 368 | switch (msg.what) { |
| 369 | case MSG_ON_NOTIFICATION_ENQUEUED: { |
| 370 | SomeArgs args = (SomeArgs) msg.obj; |
| 371 | StatusBarNotification sbn = (StatusBarNotification) args.arg1; |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 372 | NotificationChannel channel = (NotificationChannel) args.arg2; |
Svet Ganov | b8f53ee | 2016-02-18 08:38:56 -0800 | [diff] [blame] | 373 | args.recycle(); |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 374 | Adjustment adjustment = onNotificationEnqueued(sbn, channel); |
Svet Ganov | b8f53ee | 2016-02-18 08:38:56 -0800 | [diff] [blame] | 375 | if (adjustment != null) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 376 | if (!isBound()) { |
| 377 | Log.w(TAG, "MSG_ON_NOTIFICATION_ENQUEUED: service not bound, skip."); |
| 378 | return; |
| 379 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 380 | try { |
| 381 | getNotificationInterface().applyEnqueuedAdjustmentFromAssistant( |
| 382 | mWrapper, adjustment); |
| 383 | } catch (android.os.RemoteException ex) { |
| 384 | Log.v(TAG, "Unable to contact notification manager", ex); |
| 385 | throw ex.rethrowFromSystemServer(); |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 386 | } catch (SecurityException e) { |
| 387 | // app cannot catch and recover from this, so do on their behalf |
| 388 | Log.w(TAG, "Enqueue adjustment failed; no longer connected", e); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 389 | } |
Svet Ganov | b8f53ee | 2016-02-18 08:38:56 -0800 | [diff] [blame] | 390 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 391 | break; |
| 392 | } |
| 393 | case MSG_ON_NOTIFICATION_SNOOZED: { |
| 394 | SomeArgs args = (SomeArgs) msg.obj; |
| 395 | StatusBarNotification sbn = (StatusBarNotification) args.arg1; |
| 396 | String snoozeCriterionId = (String) args.arg2; |
| 397 | args.recycle(); |
| 398 | onNotificationSnoozedUntilContext(sbn, snoozeCriterionId); |
| 399 | break; |
| 400 | } |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 401 | case MSG_ON_NOTIFICATIONS_SEEN: { |
| 402 | SomeArgs args = (SomeArgs) msg.obj; |
| 403 | List<String> keys = (List<String>) args.arg1; |
| 404 | args.recycle(); |
| 405 | onNotificationsSeen(keys); |
| 406 | break; |
| 407 | } |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 408 | case MSG_ON_NOTIFICATION_EXPANSION_CHANGED: { |
| 409 | SomeArgs args = (SomeArgs) msg.obj; |
| 410 | String key = (String) args.arg1; |
| 411 | boolean isUserAction = args.argi1 == 1; |
| 412 | boolean isExpanded = args.argi2 == 1; |
| 413 | args.recycle(); |
| 414 | onNotificationExpansionChanged(key, isUserAction, isExpanded); |
| 415 | break; |
| 416 | } |
| 417 | case MSG_ON_NOTIFICATION_DIRECT_REPLY_SENT: { |
| 418 | SomeArgs args = (SomeArgs) msg.obj; |
| 419 | String key = (String) args.arg1; |
| 420 | args.recycle(); |
Tony Mak | 836147e | 2019-01-08 14:48:31 +0000 | [diff] [blame] | 421 | onNotificationDirectReplied(key); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 422 | break; |
| 423 | } |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 424 | case MSG_ON_SUGGESTED_REPLY_SENT: { |
| 425 | SomeArgs args = (SomeArgs) msg.obj; |
| 426 | String key = (String) args.arg1; |
| 427 | CharSequence reply = (CharSequence) args.arg2; |
| 428 | int source = args.argi2; |
| 429 | args.recycle(); |
| 430 | onSuggestedReplySent(key, reply, source); |
| 431 | break; |
| 432 | } |
Gustav Sennton | ced1d00 | 2019-01-11 16:19:45 +0000 | [diff] [blame] | 433 | case MSG_ON_ACTION_INVOKED: { |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 434 | SomeArgs args = (SomeArgs) msg.obj; |
| 435 | String key = (String) args.arg1; |
| 436 | Notification.Action action = (Notification.Action) args.arg2; |
| 437 | int source = args.argi2; |
| 438 | args.recycle(); |
Gustav Sennton | ced1d00 | 2019-01-11 16:19:45 +0000 | [diff] [blame] | 439 | onActionInvoked(key, action, source); |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 440 | break; |
| 441 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | } |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 445 | } |