blob: b0d55361299a2cb61608e76cf930b2cefe42aea5 [file] [log] [blame]
Adrian Roos1c0ca502015-10-07 12:20:42 -07001/*
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
17package com.android.systemui.statusbar;
18
19import com.android.internal.util.Preconditions;
Jason Monk421a9412017-02-06 09:15:21 -080020import com.android.systemui.Dependency;
Adrian Roos1c0ca502015-10-07 12:20:42 -070021import com.android.systemui.statusbar.phone.StatusBarWindowManager;
Adrian Roos1c0ca502015-10-07 12:20:42 -070022import com.android.systemui.statusbar.policy.RemoteInputView;
23
Eliot Courtney21bc05f2017-10-19 17:03:34 +090024import android.app.Notification;
25import android.app.RemoteInput;
26import android.content.Context;
27import android.os.SystemProperties;
Adrian Roos7813dd72016-09-23 17:12:17 -070028import android.util.ArrayMap;
Adrian Roosc0a579e2016-03-30 16:43:58 -070029import android.util.ArraySet;
Adrian Roos7813dd72016-09-23 17:12:17 -070030import android.util.Pair;
Adrian Roosc0a579e2016-03-30 16:43:58 -070031
Adrian Roos1c0ca502015-10-07 12:20:42 -070032import java.lang.ref.WeakReference;
33import java.util.ArrayList;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090034import java.util.List;
Adrian Roos1c0ca502015-10-07 12:20:42 -070035
36/**
37 * Keeps track of the currently active {@link RemoteInputView}s.
38 */
39public class RemoteInputController {
Eliot Courtney21bc05f2017-10-19 17:03:34 +090040 private static final boolean ENABLE_REMOTE_INPUT =
41 SystemProperties.getBoolean("debug.enable_remote_input", true);
Adrian Roos1c0ca502015-10-07 12:20:42 -070042
Adrian Roos7813dd72016-09-23 17:12:17 -070043 private final ArrayList<Pair<WeakReference<NotificationData.Entry>, Object>> mOpen
44 = new ArrayList<>();
45 private final ArrayMap<String, Object> mSpinning = new ArrayMap<>();
Adrian Roosd28ccd72016-01-06 15:23:14 +010046 private final ArrayList<Callback> mCallbacks = new ArrayList<>(3);
yoshiki iguchiff4696c2017-11-22 16:47:41 +090047 private final Delegate mDelegate;
Adrian Roos1c0ca502015-10-07 12:20:42 -070048
yoshiki iguchiff4696c2017-11-22 16:47:41 +090049 public RemoteInputController(Delegate delegate) {
yoshiki iguchiff4696c2017-11-22 16:47:41 +090050 mDelegate = delegate;
Adrian Roos1c0ca502015-10-07 12:20:42 -070051 }
52
Adrian Roos7813dd72016-09-23 17:12:17 -070053 /**
Eliot Courtney21bc05f2017-10-19 17:03:34 +090054 * Adds RemoteInput actions from the WearableExtender; to be removed once more apps support this
55 * via first-class API.
56 *
57 * TODO: Remove once enough apps specify remote inputs on their own.
58 */
59 public static void processForRemoteInput(Notification n, Context context) {
60 if (!ENABLE_REMOTE_INPUT) {
61 return;
62 }
63
64 if (n.extras != null && n.extras.containsKey("android.wearable.EXTENSIONS") &&
65 (n.actions == null || n.actions.length == 0)) {
66 Notification.Action viableAction = null;
67 Notification.WearableExtender we = new Notification.WearableExtender(n);
68
69 List<Notification.Action> actions = we.getActions();
70 final int numActions = actions.size();
71
72 for (int i = 0; i < numActions; i++) {
73 Notification.Action action = actions.get(i);
74 if (action == null) {
75 continue;
76 }
77 RemoteInput[] remoteInputs = action.getRemoteInputs();
78 if (remoteInputs == null) {
79 continue;
80 }
81 for (RemoteInput ri : remoteInputs) {
82 if (ri.getAllowFreeFormInput()) {
83 viableAction = action;
84 break;
85 }
86 }
87 if (viableAction != null) {
88 break;
89 }
90 }
91
92 if (viableAction != null) {
93 Notification.Builder rebuilder = Notification.Builder.recoverBuilder(context, n);
94 rebuilder.setActions(viableAction);
95 rebuilder.build(); // will rewrite n
96 }
97 }
98 }
99
100 /**
Adrian Roos7813dd72016-09-23 17:12:17 -0700101 * Adds a currently active remote input.
102 *
103 * @param entry the entry for which a remote input is now active.
104 * @param token a token identifying the view that is managing the remote input
105 */
106 public void addRemoteInput(NotificationData.Entry entry, Object token) {
Adrian Roos1c0ca502015-10-07 12:20:42 -0700107 Preconditions.checkNotNull(entry);
Adrian Roos7813dd72016-09-23 17:12:17 -0700108 Preconditions.checkNotNull(token);
Adrian Roos1c0ca502015-10-07 12:20:42 -0700109
110 boolean found = pruneWeakThenRemoveAndContains(
Adrian Roos7813dd72016-09-23 17:12:17 -0700111 entry /* contains */, null /* remove */, token /* removeToken */);
Adrian Roos1c0ca502015-10-07 12:20:42 -0700112 if (!found) {
Adrian Roos7813dd72016-09-23 17:12:17 -0700113 mOpen.add(new Pair<>(new WeakReference<>(entry), token));
Adrian Roos1c0ca502015-10-07 12:20:42 -0700114 }
115
116 apply(entry);
117 }
118
Adrian Roos7813dd72016-09-23 17:12:17 -0700119 /**
120 * Removes a currently active remote input.
121 *
122 * @param entry the entry for which a remote input should be removed.
123 * @param token a token identifying the view that is requesting the removal. If non-null,
124 * the entry is only removed if the token matches the last added token for this
125 * entry. If null, the entry is removed regardless.
126 */
127 public void removeRemoteInput(NotificationData.Entry entry, Object token) {
Adrian Roos1c0ca502015-10-07 12:20:42 -0700128 Preconditions.checkNotNull(entry);
129
Adrian Roos7813dd72016-09-23 17:12:17 -0700130 pruneWeakThenRemoveAndContains(null /* contains */, entry /* remove */, token);
Adrian Roos1c0ca502015-10-07 12:20:42 -0700131
132 apply(entry);
133 }
134
Adrian Roos7813dd72016-09-23 17:12:17 -0700135 /**
136 * Adds a currently spinning (i.e. sending) remote input.
137 *
138 * @param key the key of the entry that's spinning.
139 * @param token the token of the view managing the remote input.
140 */
141 public void addSpinning(String key, Object token) {
142 Preconditions.checkNotNull(key);
143 Preconditions.checkNotNull(token);
144
145 mSpinning.put(key, token);
Adrian Roosc0a579e2016-03-30 16:43:58 -0700146 }
147
Adrian Roos7813dd72016-09-23 17:12:17 -0700148 /**
149 * Removes a currently spinning remote input.
150 *
151 * @param key the key of the entry for which a remote input should be removed.
152 * @param token a token identifying the view that is requesting the removal. If non-null,
153 * the entry is only removed if the token matches the last added token for this
154 * entry. If null, the entry is removed regardless.
155 */
156 public void removeSpinning(String key, Object token) {
157 Preconditions.checkNotNull(key);
158
159 if (token == null || mSpinning.get(key) == token) {
160 mSpinning.remove(key);
161 }
Adrian Roosc0a579e2016-03-30 16:43:58 -0700162 }
163
164 public boolean isSpinning(String key) {
Adrian Roos7813dd72016-09-23 17:12:17 -0700165 return mSpinning.containsKey(key);
Adrian Roosc0a579e2016-03-30 16:43:58 -0700166 }
167
Selim Cinek09b442e2018-05-04 15:46:18 -0700168 /**
169 * Same as {@link #isSpinning}, but also verifies that the token is the same
170 * @param key the key that is spinning
171 * @param token the token that needs to be the same
172 * @return if this key with a given token is spinning
173 */
174 public boolean isSpinning(String key, Object token) {
175 return mSpinning.get(key) == token;
176 }
177
Adrian Roos1c0ca502015-10-07 12:20:42 -0700178 private void apply(NotificationData.Entry entry) {
yoshiki iguchiff4696c2017-11-22 16:47:41 +0900179 mDelegate.setRemoteInputActive(entry, isRemoteInputActive(entry));
Adrian Roosd28ccd72016-01-06 15:23:14 +0100180 boolean remoteInputActive = isRemoteInputActive();
181 int N = mCallbacks.size();
182 for (int i = 0; i < N; i++) {
183 mCallbacks.get(i).onRemoteInputActive(remoteInputActive);
184 }
Adrian Roos1c0ca502015-10-07 12:20:42 -0700185 }
186
187 /**
188 * @return true if {@param entry} has an active RemoteInput
189 */
190 public boolean isRemoteInputActive(NotificationData.Entry entry) {
Adrian Roos7813dd72016-09-23 17:12:17 -0700191 return pruneWeakThenRemoveAndContains(entry /* contains */, null /* remove */,
192 null /* removeToken */);
Adrian Roos1c0ca502015-10-07 12:20:42 -0700193 }
194
195 /**
196 * @return true if any entry has an active RemoteInput
197 */
198 public boolean isRemoteInputActive() {
Adrian Roos7813dd72016-09-23 17:12:17 -0700199 pruneWeakThenRemoveAndContains(null /* contains */, null /* remove */,
200 null /* removeToken */);
Adrian Roosc0a579e2016-03-30 16:43:58 -0700201 return !mOpen.isEmpty();
Adrian Roos1c0ca502015-10-07 12:20:42 -0700202 }
203
204 /**
205 * Prunes dangling weak references, removes entries referring to {@param remove} and returns
206 * whether {@param contains} is part of the array in a single loop.
207 * @param remove if non-null, removes this entry from the active remote inputs
Adrian Roos7813dd72016-09-23 17:12:17 -0700208 * @param removeToken if non-null, only removes an entry if this matches the token when the
209 * entry was added.
Adrian Roos1c0ca502015-10-07 12:20:42 -0700210 * @return true if {@param contains} is in the set of active remote inputs
211 */
212 private boolean pruneWeakThenRemoveAndContains(
Adrian Roos7813dd72016-09-23 17:12:17 -0700213 NotificationData.Entry contains, NotificationData.Entry remove, Object removeToken) {
Adrian Roos1c0ca502015-10-07 12:20:42 -0700214 boolean found = false;
Adrian Roosc0a579e2016-03-30 16:43:58 -0700215 for (int i = mOpen.size() - 1; i >= 0; i--) {
Adrian Roos7813dd72016-09-23 17:12:17 -0700216 NotificationData.Entry item = mOpen.get(i).first.get();
217 Object itemToken = mOpen.get(i).second;
218 boolean removeTokenMatches = (removeToken == null || itemToken == removeToken);
219
220 if (item == null || (item == remove && removeTokenMatches)) {
Adrian Roosc0a579e2016-03-30 16:43:58 -0700221 mOpen.remove(i);
Adrian Roos1c0ca502015-10-07 12:20:42 -0700222 } else if (item == contains) {
Adrian Roos7813dd72016-09-23 17:12:17 -0700223 if (removeToken != null && removeToken != itemToken) {
224 // We need to update the token. Remove here and let caller reinsert it.
225 mOpen.remove(i);
226 } else {
227 found = true;
228 }
Adrian Roos1c0ca502015-10-07 12:20:42 -0700229 }
230 }
231 return found;
232 }
233
234
Adrian Roosd28ccd72016-01-06 15:23:14 +0100235 public void addCallback(Callback callback) {
236 Preconditions.checkNotNull(callback);
237 mCallbacks.add(callback);
238 }
239
Adrian Roosc0a579e2016-03-30 16:43:58 -0700240 public void remoteInputSent(NotificationData.Entry entry) {
241 int N = mCallbacks.size();
242 for (int i = 0; i < N; i++) {
243 mCallbacks.get(i).onRemoteInputSent(entry);
244 }
245 }
246
Adrian Roos07215352016-06-23 11:19:28 -0700247 public void closeRemoteInputs() {
248 if (mOpen.size() == 0) {
249 return;
250 }
251
252 // Make a copy because closing the remote inputs will modify mOpen.
253 ArrayList<NotificationData.Entry> list = new ArrayList<>(mOpen.size());
254 for (int i = mOpen.size() - 1; i >= 0; i--) {
Adrian Roos7813dd72016-09-23 17:12:17 -0700255 NotificationData.Entry item = mOpen.get(i).first.get();
Adrian Roos07215352016-06-23 11:19:28 -0700256 if (item != null && item.row != null) {
257 list.add(item);
258 }
259 }
260
261 for (int i = list.size() - 1; i >= 0; i--) {
262 NotificationData.Entry item = list.get(i);
263 if (item.row != null) {
264 item.row.closeRemoteInput();
265 }
266 }
267 }
268
yoshiki iguchiff4696c2017-11-22 16:47:41 +0900269 public void requestDisallowLongPressAndDismiss() {
270 mDelegate.requestDisallowLongPressAndDismiss();
271 }
272
273 public void lockScrollTo(NotificationData.Entry entry) {
274 mDelegate.lockScrollTo(entry);
275 }
276
Adrian Roosd28ccd72016-01-06 15:23:14 +0100277 public interface Callback {
Adrian Roosc0a579e2016-03-30 16:43:58 -0700278 default void onRemoteInputActive(boolean active) {}
279
280 default void onRemoteInputSent(NotificationData.Entry entry) {}
Adrian Roosd28ccd72016-01-06 15:23:14 +0100281 }
yoshiki iguchiff4696c2017-11-22 16:47:41 +0900282
283 public interface Delegate {
284 /**
285 * Activate remote input if necessary.
286 */
287 void setRemoteInputActive(NotificationData.Entry entry, boolean remoteInputActive);
288
289 /**
290 * Request that the view does not dismiss nor perform long press for the current touch.
291 */
292 void requestDisallowLongPressAndDismiss();
293
294 /**
295 * Request that the view is made visible by scrolling to it, and keep the scroll locked until
296 * the user scrolls, or {@param v} loses focus or is detached.
297 */
298 void lockScrollTo(NotificationData.Entry entry);
299 }
Adrian Roos1c0ca502015-10-07 12:20:42 -0700300}