Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [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 com.android.systemui.statusbar; |
| 18 | |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 19 | import android.app.Notification; |
| 20 | import android.app.RemoteInput; |
| 21 | import android.content.Context; |
| 22 | import android.os.SystemProperties; |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 23 | import android.util.ArrayMap; |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 24 | import android.util.Pair; |
Adrian Roos | c0a579e | 2016-03-30 16:43:58 -0700 | [diff] [blame] | 25 | |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 26 | import com.android.internal.util.Preconditions; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 27 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 28 | import com.android.systemui.statusbar.policy.RemoteInputView; |
| 29 | |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 30 | import java.lang.ref.WeakReference; |
| 31 | import java.util.ArrayList; |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 32 | import java.util.List; |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Keeps track of the currently active {@link RemoteInputView}s. |
| 36 | */ |
| 37 | public class RemoteInputController { |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 38 | private static final boolean ENABLE_REMOTE_INPUT = |
| 39 | SystemProperties.getBoolean("debug.enable_remote_input", true); |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 40 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 41 | private final ArrayList<Pair<WeakReference<NotificationEntry>, Object>> mOpen |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 42 | = new ArrayList<>(); |
| 43 | private final ArrayMap<String, Object> mSpinning = new ArrayMap<>(); |
Adrian Roos | d28ccd7 | 2016-01-06 15:23:14 +0100 | [diff] [blame] | 44 | private final ArrayList<Callback> mCallbacks = new ArrayList<>(3); |
yoshiki iguchi | ff4696c | 2017-11-22 16:47:41 +0900 | [diff] [blame] | 45 | private final Delegate mDelegate; |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 46 | |
yoshiki iguchi | ff4696c | 2017-11-22 16:47:41 +0900 | [diff] [blame] | 47 | public RemoteInputController(Delegate delegate) { |
yoshiki iguchi | ff4696c | 2017-11-22 16:47:41 +0900 | [diff] [blame] | 48 | mDelegate = delegate; |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 51 | /** |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 52 | * Adds RemoteInput actions from the WearableExtender; to be removed once more apps support this |
| 53 | * via first-class API. |
| 54 | * |
| 55 | * TODO: Remove once enough apps specify remote inputs on their own. |
| 56 | */ |
| 57 | public static void processForRemoteInput(Notification n, Context context) { |
| 58 | if (!ENABLE_REMOTE_INPUT) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | if (n.extras != null && n.extras.containsKey("android.wearable.EXTENSIONS") && |
| 63 | (n.actions == null || n.actions.length == 0)) { |
| 64 | Notification.Action viableAction = null; |
| 65 | Notification.WearableExtender we = new Notification.WearableExtender(n); |
| 66 | |
| 67 | List<Notification.Action> actions = we.getActions(); |
| 68 | final int numActions = actions.size(); |
| 69 | |
| 70 | for (int i = 0; i < numActions; i++) { |
| 71 | Notification.Action action = actions.get(i); |
| 72 | if (action == null) { |
| 73 | continue; |
| 74 | } |
| 75 | RemoteInput[] remoteInputs = action.getRemoteInputs(); |
| 76 | if (remoteInputs == null) { |
| 77 | continue; |
| 78 | } |
| 79 | for (RemoteInput ri : remoteInputs) { |
| 80 | if (ri.getAllowFreeFormInput()) { |
| 81 | viableAction = action; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | if (viableAction != null) { |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (viableAction != null) { |
| 91 | Notification.Builder rebuilder = Notification.Builder.recoverBuilder(context, n); |
| 92 | rebuilder.setActions(viableAction); |
| 93 | rebuilder.build(); // will rewrite n |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 99 | * Adds a currently active remote input. |
| 100 | * |
| 101 | * @param entry the entry for which a remote input is now active. |
| 102 | * @param token a token identifying the view that is managing the remote input |
| 103 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 104 | public void addRemoteInput(NotificationEntry entry, Object token) { |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 105 | Preconditions.checkNotNull(entry); |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 106 | Preconditions.checkNotNull(token); |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 107 | |
| 108 | boolean found = pruneWeakThenRemoveAndContains( |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 109 | entry /* contains */, null /* remove */, token /* removeToken */); |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 110 | if (!found) { |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 111 | mOpen.add(new Pair<>(new WeakReference<>(entry), token)); |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | apply(entry); |
| 115 | } |
| 116 | |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 117 | /** |
| 118 | * Removes a currently active remote input. |
| 119 | * |
| 120 | * @param entry the entry for which a remote input should be removed. |
| 121 | * @param token a token identifying the view that is requesting the removal. If non-null, |
| 122 | * the entry is only removed if the token matches the last added token for this |
| 123 | * entry. If null, the entry is removed regardless. |
| 124 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 125 | public void removeRemoteInput(NotificationEntry entry, Object token) { |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 126 | Preconditions.checkNotNull(entry); |
| 127 | |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 128 | pruneWeakThenRemoveAndContains(null /* contains */, entry /* remove */, token); |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 129 | |
| 130 | apply(entry); |
| 131 | } |
| 132 | |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Adds a currently spinning (i.e. sending) remote input. |
| 135 | * |
| 136 | * @param key the key of the entry that's spinning. |
| 137 | * @param token the token of the view managing the remote input. |
| 138 | */ |
| 139 | public void addSpinning(String key, Object token) { |
| 140 | Preconditions.checkNotNull(key); |
| 141 | Preconditions.checkNotNull(token); |
| 142 | |
| 143 | mSpinning.put(key, token); |
Adrian Roos | c0a579e | 2016-03-30 16:43:58 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 146 | /** |
| 147 | * Removes a currently spinning remote input. |
| 148 | * |
| 149 | * @param key the key of the entry for which a remote input should be removed. |
| 150 | * @param token a token identifying the view that is requesting the removal. If non-null, |
| 151 | * the entry is only removed if the token matches the last added token for this |
| 152 | * entry. If null, the entry is removed regardless. |
| 153 | */ |
| 154 | public void removeSpinning(String key, Object token) { |
| 155 | Preconditions.checkNotNull(key); |
| 156 | |
| 157 | if (token == null || mSpinning.get(key) == token) { |
| 158 | mSpinning.remove(key); |
| 159 | } |
Adrian Roos | c0a579e | 2016-03-30 16:43:58 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | public boolean isSpinning(String key) { |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 163 | return mSpinning.containsKey(key); |
Adrian Roos | c0a579e | 2016-03-30 16:43:58 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Selim Cinek | 09b442e | 2018-05-04 15:46:18 -0700 | [diff] [blame] | 166 | /** |
| 167 | * Same as {@link #isSpinning}, but also verifies that the token is the same |
| 168 | * @param key the key that is spinning |
| 169 | * @param token the token that needs to be the same |
| 170 | * @return if this key with a given token is spinning |
| 171 | */ |
| 172 | public boolean isSpinning(String key, Object token) { |
| 173 | return mSpinning.get(key) == token; |
| 174 | } |
| 175 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 176 | private void apply(NotificationEntry entry) { |
yoshiki iguchi | ff4696c | 2017-11-22 16:47:41 +0900 | [diff] [blame] | 177 | mDelegate.setRemoteInputActive(entry, isRemoteInputActive(entry)); |
Adrian Roos | d28ccd7 | 2016-01-06 15:23:14 +0100 | [diff] [blame] | 178 | boolean remoteInputActive = isRemoteInputActive(); |
| 179 | int N = mCallbacks.size(); |
| 180 | for (int i = 0; i < N; i++) { |
| 181 | mCallbacks.get(i).onRemoteInputActive(remoteInputActive); |
| 182 | } |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /** |
| 186 | * @return true if {@param entry} has an active RemoteInput |
| 187 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 188 | public boolean isRemoteInputActive(NotificationEntry entry) { |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 189 | return pruneWeakThenRemoveAndContains(entry /* contains */, null /* remove */, |
| 190 | null /* removeToken */); |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
| 194 | * @return true if any entry has an active RemoteInput |
| 195 | */ |
| 196 | public boolean isRemoteInputActive() { |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 197 | pruneWeakThenRemoveAndContains(null /* contains */, null /* remove */, |
| 198 | null /* removeToken */); |
Adrian Roos | c0a579e | 2016-03-30 16:43:58 -0700 | [diff] [blame] | 199 | return !mOpen.isEmpty(); |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Prunes dangling weak references, removes entries referring to {@param remove} and returns |
| 204 | * whether {@param contains} is part of the array in a single loop. |
| 205 | * @param remove if non-null, removes this entry from the active remote inputs |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 206 | * @param removeToken if non-null, only removes an entry if this matches the token when the |
| 207 | * entry was added. |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 208 | * @return true if {@param contains} is in the set of active remote inputs |
| 209 | */ |
| 210 | private boolean pruneWeakThenRemoveAndContains( |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 211 | NotificationEntry contains, NotificationEntry remove, Object removeToken) { |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 212 | boolean found = false; |
Adrian Roos | c0a579e | 2016-03-30 16:43:58 -0700 | [diff] [blame] | 213 | for (int i = mOpen.size() - 1; i >= 0; i--) { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 214 | NotificationEntry item = mOpen.get(i).first.get(); |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 215 | Object itemToken = mOpen.get(i).second; |
| 216 | boolean removeTokenMatches = (removeToken == null || itemToken == removeToken); |
| 217 | |
| 218 | if (item == null || (item == remove && removeTokenMatches)) { |
Adrian Roos | c0a579e | 2016-03-30 16:43:58 -0700 | [diff] [blame] | 219 | mOpen.remove(i); |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 220 | } else if (item == contains) { |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 221 | if (removeToken != null && removeToken != itemToken) { |
| 222 | // We need to update the token. Remove here and let caller reinsert it. |
| 223 | mOpen.remove(i); |
| 224 | } else { |
| 225 | found = true; |
| 226 | } |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | return found; |
| 230 | } |
| 231 | |
| 232 | |
Adrian Roos | d28ccd7 | 2016-01-06 15:23:14 +0100 | [diff] [blame] | 233 | public void addCallback(Callback callback) { |
| 234 | Preconditions.checkNotNull(callback); |
| 235 | mCallbacks.add(callback); |
| 236 | } |
| 237 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 238 | public void remoteInputSent(NotificationEntry entry) { |
Adrian Roos | c0a579e | 2016-03-30 16:43:58 -0700 | [diff] [blame] | 239 | int N = mCallbacks.size(); |
| 240 | for (int i = 0; i < N; i++) { |
| 241 | mCallbacks.get(i).onRemoteInputSent(entry); |
| 242 | } |
| 243 | } |
| 244 | |
Adrian Roos | 0721535 | 2016-06-23 11:19:28 -0700 | [diff] [blame] | 245 | public void closeRemoteInputs() { |
| 246 | if (mOpen.size() == 0) { |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | // Make a copy because closing the remote inputs will modify mOpen. |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 251 | ArrayList<NotificationEntry> list = new ArrayList<>(mOpen.size()); |
Adrian Roos | 0721535 | 2016-06-23 11:19:28 -0700 | [diff] [blame] | 252 | for (int i = mOpen.size() - 1; i >= 0; i--) { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 253 | NotificationEntry entry = mOpen.get(i).first.get(); |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 254 | if (entry != null && entry.rowExists()) { |
| 255 | list.add(entry); |
Adrian Roos | 0721535 | 2016-06-23 11:19:28 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | for (int i = list.size() - 1; i >= 0; i--) { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 260 | NotificationEntry entry = list.get(i); |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 261 | if (entry.rowExists()) { |
| 262 | entry.closeRemoteInput(); |
Adrian Roos | 0721535 | 2016-06-23 11:19:28 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
yoshiki iguchi | ff4696c | 2017-11-22 16:47:41 +0900 | [diff] [blame] | 267 | public void requestDisallowLongPressAndDismiss() { |
| 268 | mDelegate.requestDisallowLongPressAndDismiss(); |
| 269 | } |
| 270 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 271 | public void lockScrollTo(NotificationEntry entry) { |
yoshiki iguchi | ff4696c | 2017-11-22 16:47:41 +0900 | [diff] [blame] | 272 | mDelegate.lockScrollTo(entry); |
| 273 | } |
| 274 | |
Adrian Roos | d28ccd7 | 2016-01-06 15:23:14 +0100 | [diff] [blame] | 275 | public interface Callback { |
Adrian Roos | c0a579e | 2016-03-30 16:43:58 -0700 | [diff] [blame] | 276 | default void onRemoteInputActive(boolean active) {} |
| 277 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 278 | default void onRemoteInputSent(NotificationEntry entry) {} |
Adrian Roos | d28ccd7 | 2016-01-06 15:23:14 +0100 | [diff] [blame] | 279 | } |
yoshiki iguchi | ff4696c | 2017-11-22 16:47:41 +0900 | [diff] [blame] | 280 | |
| 281 | public interface Delegate { |
| 282 | /** |
| 283 | * Activate remote input if necessary. |
| 284 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 285 | void setRemoteInputActive(NotificationEntry entry, boolean remoteInputActive); |
yoshiki iguchi | ff4696c | 2017-11-22 16:47:41 +0900 | [diff] [blame] | 286 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 287 | /** |
| 288 | * Request that the view does not dismiss nor perform long press for the current touch. |
| 289 | */ |
| 290 | void requestDisallowLongPressAndDismiss(); |
yoshiki iguchi | ff4696c | 2017-11-22 16:47:41 +0900 | [diff] [blame] | 291 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 292 | /** |
| 293 | * Request that the view is made visible by scrolling to it, and keep the scroll locked until |
| 294 | * the user scrolls, or {@param entry} loses focus or is detached. |
| 295 | */ |
| 296 | void lockScrollTo(NotificationEntry entry); |
yoshiki iguchi | ff4696c | 2017-11-22 16:47:41 +0900 | [diff] [blame] | 297 | } |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 298 | } |