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