Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | package com.android.systemui.statusbar; |
| 17 | |
| 18 | import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY; |
| 19 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 21 | import android.app.ActivityManager; |
Sunny Goyal | 0279453 | 2018-08-22 15:18:37 -0700 | [diff] [blame] | 22 | import android.app.ActivityOptions; |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 23 | import android.app.KeyguardManager; |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 24 | import android.app.Notification; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 25 | import android.app.PendingIntent; |
| 26 | import android.app.RemoteInput; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
| 29 | import android.os.RemoteException; |
| 30 | import android.os.ServiceManager; |
| 31 | import android.os.SystemClock; |
| 32 | import android.os.SystemProperties; |
| 33 | import android.os.UserManager; |
| 34 | import android.service.notification.StatusBarNotification; |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 35 | import android.text.TextUtils; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 36 | import android.util.ArraySet; |
| 37 | import android.util.Log; |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 38 | import android.util.Pair; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 39 | import android.view.MotionEvent; |
| 40 | import android.view.View; |
| 41 | import android.view.ViewGroup; |
| 42 | import android.view.ViewParent; |
| 43 | import android.widget.RemoteViews; |
| 44 | import android.widget.TextView; |
| 45 | |
| 46 | import com.android.internal.annotations.VisibleForTesting; |
| 47 | import com.android.internal.statusbar.IStatusBarService; |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 48 | import com.android.internal.statusbar.NotificationVisibility; |
Eliot Courtney | 6c313d3 | 2017-12-14 19:57:51 +0900 | [diff] [blame] | 49 | import com.android.systemui.Dependency; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 50 | import com.android.systemui.Dumpable; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 51 | import com.android.systemui.statusbar.notification.NotificationData; |
| 52 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
| 53 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 54 | import com.android.systemui.statusbar.phone.ShadeController; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 55 | import com.android.systemui.statusbar.policy.RemoteInputView; |
| 56 | |
| 57 | import java.io.FileDescriptor; |
| 58 | import java.io.PrintWriter; |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 59 | import java.util.ArrayList; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 60 | import java.util.Set; |
| 61 | |
| 62 | /** |
| 63 | * Class for handling remote input state over a set of notifications. This class handles things |
| 64 | * like keeping notifications temporarily that were cancelled as a response to a remote input |
| 65 | * interaction, keeping track of notifications to remove when NotificationPresenter is collapsed, |
| 66 | * and handling clicks on remote views. |
| 67 | */ |
| 68 | public class NotificationRemoteInputManager implements Dumpable { |
| 69 | public static final boolean ENABLE_REMOTE_INPUT = |
| 70 | SystemProperties.getBoolean("debug.enable_remote_input", true); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 71 | public static boolean FORCE_REMOTE_INPUT_HISTORY = |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 72 | SystemProperties.getBoolean("debug.force_remoteinput_history", true); |
| 73 | private static final boolean DEBUG = false; |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 74 | private static final String TAG = "NotifRemoteInputManager"; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * How long to wait before auto-dismissing a notification that was kept for remote input, and |
| 78 | * has now sent a remote input. We auto-dismiss, because the app may not see a reason to cancel |
| 79 | * these given that they technically don't exist anymore. We wait a bit in case the app issues |
| 80 | * an update. |
| 81 | */ |
| 82 | private static final int REMOTE_INPUT_KEPT_ENTRY_AUTO_CANCEL_DELAY = 200; |
| 83 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Notifications that are already removed but are kept around because we want to show the |
| 86 | * remote input history. See {@link RemoteInputHistoryExtender} and |
| 87 | * {@link SmartReplyHistoryExtender}. |
| 88 | */ |
| 89 | protected final ArraySet<String> mKeysKeptForRemoteInputHistory = new ArraySet<>(); |
| 90 | |
| 91 | /** |
| 92 | * Notifications that are already removed but are kept around because the remote input is |
| 93 | * actively being used (i.e. user is typing in it). See {@link RemoteInputActiveExtender}. |
| 94 | */ |
| 95 | protected final ArraySet<NotificationData.Entry> mEntriesKeptForRemoteInputActive = |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 96 | new ArraySet<>(); |
Eliot Courtney | 6c313d3 | 2017-12-14 19:57:51 +0900 | [diff] [blame] | 97 | |
| 98 | // Dependencies: |
| 99 | protected final NotificationLockscreenUserManager mLockscreenUserManager = |
| 100 | Dependency.get(NotificationLockscreenUserManager.class); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 101 | protected final SmartReplyController mSmartReplyController = |
| 102 | Dependency.get(SmartReplyController.class); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 103 | private final NotificationEntryManager mEntryManager |
| 104 | = Dependency.get(NotificationEntryManager.class); |
| 105 | |
| 106 | // Lazy |
| 107 | private ShadeController mShadeController; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 108 | |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 109 | protected final Context mContext; |
| 110 | private final UserManager mUserManager; |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 111 | private final KeyguardManager mKeyguardManager; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 112 | |
| 113 | protected RemoteInputController mRemoteInputController; |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 114 | protected NotificationLifetimeExtender.NotificationSafeToRemoveCallback |
| 115 | mNotificationLifetimeFinishedCallback; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 116 | protected IStatusBarService mBarService; |
| 117 | protected Callback mCallback; |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 118 | protected final ArrayList<NotificationLifetimeExtender> mLifetimeExtenders = new ArrayList<>(); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 119 | |
| 120 | private final RemoteViews.OnClickHandler mOnClickHandler = new RemoteViews.OnClickHandler() { |
| 121 | |
| 122 | @Override |
| 123 | public boolean onClickHandler( |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 124 | View view, PendingIntent pendingIntent, RemoteViews.RemoteResponse response) { |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 125 | getShadeController().wakeUpIfDozing(SystemClock.uptimeMillis(), view); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 126 | |
| 127 | if (handleRemoteInput(view, pendingIntent)) { |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | if (DEBUG) { |
| 132 | Log.v(TAG, "Notification click handler invoked for intent: " + pendingIntent); |
| 133 | } |
| 134 | logActionClick(view); |
| 135 | // The intent we are sending is for the application, which |
| 136 | // won't have permission to immediately start an activity after |
| 137 | // the user switches to home. We know it is safe to do at this |
| 138 | // point, so make sure new activity switches are now allowed. |
| 139 | try { |
| 140 | ActivityManager.getService().resumeAppSwitches(); |
| 141 | } catch (RemoteException e) { |
| 142 | } |
yoshiki iguchi | 67c166d | 2018-11-26 13:11:36 +0900 | [diff] [blame] | 143 | return mCallback.handleRemoteViewClick(view, pendingIntent, () -> { |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 144 | Pair<Intent, ActivityOptions> options = response.getLaunchOptions(view); |
| 145 | options.second.setLaunchWindowingMode( |
| 146 | WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY); |
| 147 | return RemoteViews.startPendingIntent(view, pendingIntent, options); |
| 148 | }); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | private void logActionClick(View view) { |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 152 | Integer actionIndex = (Integer) |
| 153 | view.getTag(com.android.internal.R.id.notification_action_index_tag); |
| 154 | if (actionIndex == null) { |
| 155 | Log.e(TAG, "Couldn't retrieve the actionIndex from the clicked button"); |
| 156 | return; |
| 157 | } |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 158 | ViewParent parent = view.getParent(); |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 159 | StatusBarNotification statusBarNotification = getNotificationForParent(parent); |
| 160 | if (statusBarNotification == null) { |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 161 | Log.w(TAG, "Couldn't determine notification for click."); |
| 162 | return; |
| 163 | } |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 164 | String key = statusBarNotification.getKey(); |
| 165 | int buttonIndex = -1; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 166 | // If this is a default template, determine the index of the button. |
| 167 | if (view.getId() == com.android.internal.R.id.action0 && |
| 168 | parent != null && parent instanceof ViewGroup) { |
| 169 | ViewGroup actionGroup = (ViewGroup) parent; |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 170 | buttonIndex = actionGroup.indexOfChild(view); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 171 | } |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 172 | final int count = mEntryManager.getNotificationData().getActiveNotifications().size(); |
| 173 | final int rank = mEntryManager.getNotificationData().getRank(key); |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 174 | final Notification.Action action = |
| 175 | statusBarNotification.getNotification().actions[actionIndex]; |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 176 | final NotificationVisibility nv = NotificationVisibility.obtain(key, rank, count, true); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 177 | try { |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 178 | mBarService.onNotificationActionClick(key, buttonIndex, action, nv, false); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 179 | } catch (RemoteException e) { |
| 180 | // Ignore |
| 181 | } |
| 182 | } |
| 183 | |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 184 | private StatusBarNotification getNotificationForParent(ViewParent parent) { |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 185 | while (parent != null) { |
| 186 | if (parent instanceof ExpandableNotificationRow) { |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 187 | return ((ExpandableNotificationRow) parent).getStatusBarNotification(); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 188 | } |
| 189 | parent = parent.getParent(); |
| 190 | } |
| 191 | return null; |
| 192 | } |
| 193 | |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 194 | private boolean handleRemoteInput(View view, PendingIntent pendingIntent) { |
| 195 | if (mCallback.shouldHandleRemoteInput(view, pendingIntent)) { |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | Object tag = view.getTag(com.android.internal.R.id.remote_input_tag); |
| 200 | RemoteInput[] inputs = null; |
| 201 | if (tag instanceof RemoteInput[]) { |
| 202 | inputs = (RemoteInput[]) tag; |
| 203 | } |
| 204 | |
| 205 | if (inputs == null) { |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | RemoteInput input = null; |
| 210 | |
| 211 | for (RemoteInput i : inputs) { |
| 212 | if (i.getAllowFreeFormInput()) { |
| 213 | input = i; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if (input == null) { |
| 218 | return false; |
| 219 | } |
| 220 | |
Milo Sredkov | b2af7f9 | 2018-12-04 15:22:18 +0000 | [diff] [blame] | 221 | return activateRemoteInput(view, inputs, input, pendingIntent); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 222 | } |
| 223 | }; |
| 224 | |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 225 | private ShadeController getShadeController() { |
| 226 | if (mShadeController == null) { |
| 227 | mShadeController = Dependency.get(ShadeController.class); |
| 228 | } |
| 229 | return mShadeController; |
| 230 | } |
| 231 | |
Eliot Courtney | 6c313d3 | 2017-12-14 19:57:51 +0900 | [diff] [blame] | 232 | public NotificationRemoteInputManager(Context context) { |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 233 | mContext = context; |
| 234 | mBarService = IStatusBarService.Stub.asInterface( |
| 235 | ServiceManager.getService(Context.STATUS_BAR_SERVICE)); |
| 236 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 237 | addLifetimeExtenders(); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 238 | mKeyguardManager = context.getSystemService(KeyguardManager.class); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 239 | } |
| 240 | |
Gus Prevas | 21437b3 | 2018-12-05 10:36:13 -0500 | [diff] [blame] | 241 | /** Initializes this component with the provided dependencies. */ |
| 242 | public void setUpWithCallback(Callback callback, RemoteInputController.Delegate delegate) { |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 243 | mCallback = callback; |
| 244 | mRemoteInputController = new RemoteInputController(delegate); |
| 245 | mRemoteInputController.addCallback(new RemoteInputController.Callback() { |
| 246 | @Override |
| 247 | public void onRemoteInputSent(NotificationData.Entry entry) { |
Kenny Guy | 8cc15d2 | 2018-05-09 09:50:55 +0100 | [diff] [blame] | 248 | if (FORCE_REMOTE_INPUT_HISTORY |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 249 | && isNotificationKeptForRemoteInputHistory(entry.key)) { |
| 250 | mNotificationLifetimeFinishedCallback.onSafeToRemove(entry.key); |
| 251 | } else if (mEntriesKeptForRemoteInputActive.contains(entry)) { |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 252 | // We're currently holding onto this notification, but from the apps point of |
| 253 | // view it is already canceled, so we'll need to cancel it on the apps behalf |
| 254 | // after sending - unless the app posts an update in the mean time, so wait a |
| 255 | // bit. |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 256 | Dependency.get(Dependency.MAIN_HANDLER).postDelayed(() -> { |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 257 | if (mEntriesKeptForRemoteInputActive.remove(entry)) { |
| 258 | mNotificationLifetimeFinishedCallback.onSafeToRemove(entry.key); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 259 | } |
| 260 | }, REMOTE_INPUT_KEPT_ENTRY_AUTO_CANCEL_DELAY); |
| 261 | } |
Amith Yamasani | 129b481 | 2018-02-08 13:55:59 -0800 | [diff] [blame] | 262 | try { |
| 263 | mBarService.onNotificationDirectReplied(entry.notification.getKey()); |
| 264 | } catch (RemoteException e) { |
| 265 | // Nothing to do, system going down |
| 266 | } |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 267 | } |
| 268 | }); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 269 | mSmartReplyController.setCallback((entry, reply) -> { |
| 270 | StatusBarNotification newSbn = |
| 271 | rebuildNotificationWithRemoteInput(entry, reply, true /* showSpinner */); |
| 272 | mEntryManager.updateNotification(newSbn, null /* ranking */); |
| 273 | }); |
| 274 | } |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 275 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 276 | /** |
Milo Sredkov | b2af7f9 | 2018-12-04 15:22:18 +0000 | [diff] [blame] | 277 | * Activates a given {@link RemoteInput} |
| 278 | * |
| 279 | * @param view The view of the action button or suggestion chip that was tapped. |
| 280 | * @param inputs The remote inputs that need to be sent to the app. |
| 281 | * @param input The remote input that needs to be activated. |
| 282 | * @param pendingIntent The pending intent to be sent to the app. |
| 283 | * @return Whether the {@link RemoteInput} was activated. |
| 284 | */ |
| 285 | public boolean activateRemoteInput(View view, RemoteInput[] inputs, RemoteInput input, |
| 286 | PendingIntent pendingIntent) { |
| 287 | |
| 288 | ViewParent p = view.getParent(); |
| 289 | RemoteInputView riv = null; |
| 290 | while (p != null) { |
| 291 | if (p instanceof View) { |
| 292 | View pv = (View) p; |
| 293 | if (pv.isRootNamespace()) { |
| 294 | riv = findRemoteInputView(pv); |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | p = p.getParent(); |
| 299 | } |
| 300 | ExpandableNotificationRow row = null; |
| 301 | while (p != null) { |
| 302 | if (p instanceof ExpandableNotificationRow) { |
| 303 | row = (ExpandableNotificationRow) p; |
| 304 | break; |
| 305 | } |
| 306 | p = p.getParent(); |
| 307 | } |
| 308 | |
| 309 | if (row == null) { |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | row.setUserExpanded(true); |
| 314 | |
| 315 | if (!mLockscreenUserManager.shouldAllowLockscreenRemoteInput()) { |
| 316 | final int userId = pendingIntent.getCreatorUserHandle().getIdentifier(); |
| 317 | if (mLockscreenUserManager.isLockscreenPublicMode(userId)) { |
| 318 | mCallback.onLockedRemoteInput(row, view); |
| 319 | return true; |
| 320 | } |
| 321 | if (mUserManager.getUserInfo(userId).isManagedProfile() |
| 322 | && mKeyguardManager.isDeviceLocked(userId)) { |
| 323 | mCallback.onLockedWorkRemoteInput(userId, row, view); |
| 324 | return true; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if (riv == null) { |
| 329 | riv = findRemoteInputView(row.getPrivateLayout().getExpandedChild()); |
| 330 | if (riv == null) { |
| 331 | return false; |
| 332 | } |
| 333 | if (!row.getPrivateLayout().getExpandedChild().isShown()) { |
| 334 | mCallback.onMakeExpandedVisibleForRemoteInput(row, view); |
| 335 | return true; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | int width = view.getWidth(); |
| 340 | if (view instanceof TextView) { |
| 341 | // Center the reveal on the text which might be off-center from the TextView |
| 342 | TextView tv = (TextView) view; |
| 343 | if (tv.getLayout() != null) { |
| 344 | int innerWidth = (int) tv.getLayout().getLineWidth(0); |
| 345 | innerWidth += tv.getCompoundPaddingLeft() + tv.getCompoundPaddingRight(); |
| 346 | width = Math.min(width, innerWidth); |
| 347 | } |
| 348 | } |
| 349 | int cx = view.getLeft() + width / 2; |
| 350 | int cy = view.getTop() + view.getHeight() / 2; |
| 351 | int w = riv.getWidth(); |
| 352 | int h = riv.getHeight(); |
| 353 | int r = Math.max( |
| 354 | Math.max(cx + cy, cx + (h - cy)), |
| 355 | Math.max((w - cx) + cy, (w - cx) + (h - cy))); |
| 356 | |
| 357 | riv.setRevealParameters(cx, cy, r); |
| 358 | riv.setPendingIntent(pendingIntent); |
| 359 | riv.setRemoteInput(inputs, input); |
| 360 | riv.focusAnimated(); |
| 361 | |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | private RemoteInputView findRemoteInputView(View v) { |
| 366 | if (v == null) { |
| 367 | return null; |
| 368 | } |
| 369 | return (RemoteInputView) v.findViewWithTag(RemoteInputView.VIEW_TAG); |
| 370 | } |
| 371 | |
| 372 | /** |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 373 | * Adds all the notification lifetime extenders. Each extender represents a reason for the |
| 374 | * NotificationRemoteInputManager to keep a notification lifetime extended. |
| 375 | */ |
| 376 | protected void addLifetimeExtenders() { |
| 377 | mLifetimeExtenders.add(new RemoteInputHistoryExtender()); |
| 378 | mLifetimeExtenders.add(new SmartReplyHistoryExtender()); |
| 379 | mLifetimeExtenders.add(new RemoteInputActiveExtender()); |
| 380 | } |
| 381 | |
| 382 | public ArrayList<NotificationLifetimeExtender> getLifetimeExtenders() { |
| 383 | return mLifetimeExtenders; |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | public RemoteInputController getController() { |
| 387 | return mRemoteInputController; |
| 388 | } |
| 389 | |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 390 | public void onPerformRemoveNotification(StatusBarNotification n, |
| 391 | NotificationData.Entry entry) { |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 392 | if (mKeysKeptForRemoteInputHistory.contains(n.getKey())) { |
| 393 | mKeysKeptForRemoteInputHistory.remove(n.getKey()); |
| 394 | } |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 395 | if (mRemoteInputController.isRemoteInputActive(entry)) { |
| 396 | mRemoteInputController.removeRemoteInput(entry, null); |
| 397 | } |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 398 | } |
| 399 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 400 | public void onPanelCollapsed() { |
| 401 | for (int i = 0; i < mEntriesKeptForRemoteInputActive.size(); i++) { |
| 402 | NotificationData.Entry entry = mEntriesKeptForRemoteInputActive.valueAt(i); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 403 | mRemoteInputController.removeRemoteInput(entry, null); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 404 | if (mNotificationLifetimeFinishedCallback != null) { |
| 405 | mNotificationLifetimeFinishedCallback.onSafeToRemove(entry.key); |
| 406 | } |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 407 | } |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 408 | mEntriesKeptForRemoteInputActive.clear(); |
| 409 | } |
| 410 | |
| 411 | public boolean isNotificationKeptForRemoteInputHistory(String key) { |
| 412 | return mKeysKeptForRemoteInputHistory.contains(key); |
| 413 | } |
| 414 | |
| 415 | public boolean shouldKeepForRemoteInputHistory(NotificationData.Entry entry) { |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 416 | if (entry.isDismissed()) { |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 417 | return false; |
| 418 | } |
| 419 | if (!FORCE_REMOTE_INPUT_HISTORY) { |
| 420 | return false; |
| 421 | } |
| 422 | return (mRemoteInputController.isSpinning(entry.key) || entry.hasJustSentRemoteInput()); |
| 423 | } |
| 424 | |
| 425 | public boolean shouldKeepForSmartReplyHistory(NotificationData.Entry entry) { |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 426 | if (entry.isDismissed()) { |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 427 | return false; |
| 428 | } |
| 429 | if (!FORCE_REMOTE_INPUT_HISTORY) { |
| 430 | return false; |
| 431 | } |
| 432 | return mSmartReplyController.isSendingSmartReply(entry.key); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | public void checkRemoteInputOutside(MotionEvent event) { |
| 436 | if (event.getAction() == MotionEvent.ACTION_OUTSIDE // touch outside the source bar |
| 437 | && event.getX() == 0 && event.getY() == 0 // a touch outside both bars |
| 438 | && mRemoteInputController.isRemoteInputActive()) { |
| 439 | mRemoteInputController.closeRemoteInputs(); |
| 440 | } |
| 441 | } |
| 442 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 443 | @VisibleForTesting |
| 444 | StatusBarNotification rebuildNotificationForCanceledSmartReplies( |
| 445 | NotificationData.Entry entry) { |
| 446 | return rebuildNotificationWithRemoteInput(entry, null /* remoteInputTest */, |
| 447 | false /* showSpinner */); |
| 448 | } |
| 449 | |
| 450 | @VisibleForTesting |
| 451 | StatusBarNotification rebuildNotificationWithRemoteInput(NotificationData.Entry entry, |
| 452 | CharSequence remoteInputText, boolean showSpinner) { |
| 453 | StatusBarNotification sbn = entry.notification; |
| 454 | |
| 455 | Notification.Builder b = Notification.Builder |
| 456 | .recoverBuilder(mContext, sbn.getNotification().clone()); |
| 457 | if (remoteInputText != null) { |
| 458 | CharSequence[] oldHistory = sbn.getNotification().extras |
| 459 | .getCharSequenceArray(Notification.EXTRA_REMOTE_INPUT_HISTORY); |
| 460 | CharSequence[] newHistory; |
| 461 | if (oldHistory == null) { |
| 462 | newHistory = new CharSequence[1]; |
| 463 | } else { |
| 464 | newHistory = new CharSequence[oldHistory.length + 1]; |
| 465 | System.arraycopy(oldHistory, 0, newHistory, 1, oldHistory.length); |
| 466 | } |
| 467 | newHistory[0] = String.valueOf(remoteInputText); |
| 468 | b.setRemoteInputHistory(newHistory); |
| 469 | } |
| 470 | b.setShowRemoteInputSpinner(showSpinner); |
| 471 | b.setHideSmartReplies(true); |
| 472 | |
| 473 | Notification newNotification = b.build(); |
| 474 | |
| 475 | // Undo any compatibility view inflation |
| 476 | newNotification.contentView = sbn.getNotification().contentView; |
| 477 | newNotification.bigContentView = sbn.getNotification().bigContentView; |
| 478 | newNotification.headsUpContentView = sbn.getNotification().headsUpContentView; |
| 479 | |
| 480 | return new StatusBarNotification( |
| 481 | sbn.getPackageName(), |
| 482 | sbn.getOpPkg(), |
| 483 | sbn.getId(), |
| 484 | sbn.getTag(), |
| 485 | sbn.getUid(), |
| 486 | sbn.getInitialPid(), |
| 487 | newNotification, |
| 488 | sbn.getUser(), |
| 489 | sbn.getOverrideGroupKey(), |
| 490 | sbn.getPostTime()); |
| 491 | } |
| 492 | |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 493 | @Override |
| 494 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 495 | pw.println("NotificationRemoteInputManager state:"); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 496 | pw.print(" mKeysKeptForRemoteInputHistory: "); |
| 497 | pw.println(mKeysKeptForRemoteInputHistory); |
| 498 | pw.print(" mEntriesKeptForRemoteInputActive: "); |
| 499 | pw.println(mEntriesKeptForRemoteInputActive); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | public void bindRow(ExpandableNotificationRow row) { |
| 503 | row.setRemoteInputController(mRemoteInputController); |
| 504 | row.setRemoteViewClickHandler(mOnClickHandler); |
| 505 | } |
| 506 | |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 507 | @VisibleForTesting |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 508 | public Set<NotificationData.Entry> getEntriesKeptForRemoteInputActive() { |
| 509 | return mEntriesKeptForRemoteInputActive; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * NotificationRemoteInputManager has multiple reasons to keep notification lifetime extended |
| 514 | * so we implement multiple NotificationLifetimeExtenders |
| 515 | */ |
| 516 | protected abstract class RemoteInputExtender implements NotificationLifetimeExtender { |
| 517 | @Override |
| 518 | public void setCallback(NotificationSafeToRemoveCallback callback) { |
| 519 | if (mNotificationLifetimeFinishedCallback == null) { |
| 520 | mNotificationLifetimeFinishedCallback = callback; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Notification is kept alive as it was cancelled in response to a remote input interaction. |
| 527 | * This allows us to show what you replied and allows you to continue typing into it. |
| 528 | */ |
| 529 | protected class RemoteInputHistoryExtender extends RemoteInputExtender { |
| 530 | @Override |
| 531 | public boolean shouldExtendLifetime(@NonNull NotificationData.Entry entry) { |
| 532 | return shouldKeepForRemoteInputHistory(entry); |
| 533 | } |
| 534 | |
| 535 | @Override |
Kevin | e9e938c | 2018-09-06 13:38:11 -0700 | [diff] [blame] | 536 | public void setShouldManageLifetime(NotificationData.Entry entry, |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 537 | boolean shouldExtend) { |
| 538 | if (shouldExtend) { |
| 539 | CharSequence remoteInputText = entry.remoteInputText; |
| 540 | if (TextUtils.isEmpty(remoteInputText)) { |
| 541 | remoteInputText = entry.remoteInputTextWhenReset; |
| 542 | } |
| 543 | StatusBarNotification newSbn = rebuildNotificationWithRemoteInput(entry, |
| 544 | remoteInputText, false /* showSpinner */); |
| 545 | entry.onRemoteInputInserted(); |
| 546 | |
| 547 | if (newSbn == null) { |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | mEntryManager.updateNotification(newSbn, null); |
| 552 | |
| 553 | // Ensure the entry hasn't already been removed. This can happen if there is an |
| 554 | // inflation exception while updating the remote history |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 555 | if (entry.isRemoved()) { |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 556 | return; |
| 557 | } |
| 558 | |
| 559 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 560 | Log.d(TAG, "Keeping notification around after sending remote input " |
| 561 | + entry.key); |
| 562 | } |
| 563 | |
| 564 | mKeysKeptForRemoteInputHistory.add(entry.key); |
| 565 | } else { |
| 566 | mKeysKeptForRemoteInputHistory.remove(entry.key); |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Notification is kept alive for smart reply history. Similar to REMOTE_INPUT_HISTORY but with |
| 573 | * {@link SmartReplyController} specific logic |
| 574 | */ |
| 575 | protected class SmartReplyHistoryExtender extends RemoteInputExtender { |
| 576 | @Override |
| 577 | public boolean shouldExtendLifetime(@NonNull NotificationData.Entry entry) { |
| 578 | return shouldKeepForSmartReplyHistory(entry); |
| 579 | } |
| 580 | |
| 581 | @Override |
Kevin | e9e938c | 2018-09-06 13:38:11 -0700 | [diff] [blame] | 582 | public void setShouldManageLifetime(NotificationData.Entry entry, |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 583 | boolean shouldExtend) { |
| 584 | if (shouldExtend) { |
| 585 | StatusBarNotification newSbn = rebuildNotificationForCanceledSmartReplies(entry); |
| 586 | |
| 587 | if (newSbn == null) { |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | mEntryManager.updateNotification(newSbn, null); |
| 592 | |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 593 | if (entry.isRemoved()) { |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 594 | return; |
| 595 | } |
| 596 | |
| 597 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 598 | Log.d(TAG, "Keeping notification around after sending smart reply " |
| 599 | + entry.key); |
| 600 | } |
| 601 | |
| 602 | mKeysKeptForRemoteInputHistory.add(entry.key); |
| 603 | } else { |
| 604 | mKeysKeptForRemoteInputHistory.remove(entry.key); |
| 605 | mSmartReplyController.stopSending(entry); |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Notification is kept alive because the user is still using the remote input |
| 612 | */ |
| 613 | protected class RemoteInputActiveExtender extends RemoteInputExtender { |
| 614 | @Override |
| 615 | public boolean shouldExtendLifetime(@NonNull NotificationData.Entry entry) { |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 616 | if (entry.isDismissed()) { |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 617 | return false; |
| 618 | } |
| 619 | return mRemoteInputController.isRemoteInputActive(entry); |
| 620 | } |
| 621 | |
| 622 | @Override |
Kevin | e9e938c | 2018-09-06 13:38:11 -0700 | [diff] [blame] | 623 | public void setShouldManageLifetime(NotificationData.Entry entry, |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 624 | boolean shouldExtend) { |
| 625 | if (shouldExtend) { |
| 626 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 627 | Log.d(TAG, "Keeping notification around while remote input active " |
| 628 | + entry.key); |
| 629 | } |
| 630 | mEntriesKeptForRemoteInputActive.add(entry); |
| 631 | } else { |
| 632 | mEntriesKeptForRemoteInputActive.remove(entry); |
| 633 | } |
| 634 | } |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /** |
| 638 | * Callback for various remote input related events, or for providing information that |
| 639 | * NotificationRemoteInputManager needs to know to decide what to do. |
| 640 | */ |
| 641 | public interface Callback { |
| 642 | |
| 643 | /** |
| 644 | * Called when remote input was activated but the device is locked. |
| 645 | * |
| 646 | * @param row |
| 647 | * @param clicked |
| 648 | */ |
| 649 | void onLockedRemoteInput(ExpandableNotificationRow row, View clicked); |
| 650 | |
| 651 | /** |
| 652 | * Called when remote input was activated but the device is locked and in a managed profile. |
| 653 | * |
| 654 | * @param userId |
| 655 | * @param row |
| 656 | * @param clicked |
| 657 | */ |
| 658 | void onLockedWorkRemoteInput(int userId, ExpandableNotificationRow row, View clicked); |
| 659 | |
| 660 | /** |
| 661 | * Called when a row should be made expanded for the purposes of remote input. |
| 662 | * |
| 663 | * @param row |
| 664 | * @param clickedView |
| 665 | */ |
| 666 | void onMakeExpandedVisibleForRemoteInput(ExpandableNotificationRow row, View clickedView); |
| 667 | |
| 668 | /** |
| 669 | * Return whether or not remote input should be handled for this view. |
| 670 | * |
| 671 | * @param view |
| 672 | * @param pendingIntent |
| 673 | * @return true iff the remote input should be handled |
| 674 | */ |
| 675 | boolean shouldHandleRemoteInput(View view, PendingIntent pendingIntent); |
| 676 | |
| 677 | /** |
| 678 | * Performs any special handling for a remote view click. The default behaviour can be |
| 679 | * called through the defaultHandler parameter. |
| 680 | * |
yoshiki iguchi | 67c166d | 2018-11-26 13:11:36 +0900 | [diff] [blame] | 681 | * @param view |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 682 | * @param pendingIntent |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 683 | * @param defaultHandler |
| 684 | * @return true iff the click was handled |
| 685 | */ |
yoshiki iguchi | 67c166d | 2018-11-26 13:11:36 +0900 | [diff] [blame] | 686 | boolean handleRemoteViewClick(View view, PendingIntent pendingIntent, |
| 687 | ClickHandler defaultHandler); |
Eliot Courtney | e77edea | 2017-11-15 14:25:21 +0900 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /** |
| 691 | * Helper interface meant for passing the default on click behaviour to NotificationPresenter, |
| 692 | * so it may do its own handling before invoking the default behaviour. |
| 693 | */ |
| 694 | public interface ClickHandler { |
| 695 | /** |
| 696 | * Tries to handle a click on a remote view. |
| 697 | * |
| 698 | * @return true iff the click was handled |
| 699 | */ |
| 700 | boolean handleClick(); |
| 701 | } |
| 702 | } |