blob: 97e3d22c6a46833df07c0c9a4b5c22862b96acca [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) {
Jason Monk421a9412017-02-06 09:15:21 -080050 addCallback(Dependency.get(StatusBarWindowManager.class));
yoshiki iguchiff4696c2017-11-22 16:47:41 +090051 mDelegate = delegate;
Adrian Roos1c0ca502015-10-07 12:20:42 -070052 }
53
Adrian Roos7813dd72016-09-23 17:12:17 -070054 /**
Eliot Courtney21bc05f2017-10-19 17:03:34 +090055 * 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 Roos7813dd72016-09-23 17:12:17 -0700102 * 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 Roos1c0ca502015-10-07 12:20:42 -0700108 Preconditions.checkNotNull(entry);
Adrian Roos7813dd72016-09-23 17:12:17 -0700109 Preconditions.checkNotNull(token);
Adrian Roos1c0ca502015-10-07 12:20:42 -0700110
111 boolean found = pruneWeakThenRemoveAndContains(
Adrian Roos7813dd72016-09-23 17:12:17 -0700112 entry /* contains */, null /* remove */, token /* removeToken */);
Adrian Roos1c0ca502015-10-07 12:20:42 -0700113 if (!found) {
Adrian Roos7813dd72016-09-23 17:12:17 -0700114 mOpen.add(new Pair<>(new WeakReference<>(entry), token));
Adrian Roos1c0ca502015-10-07 12:20:42 -0700115 }
116
117 apply(entry);
118 }
119
Adrian Roos7813dd72016-09-23 17:12:17 -0700120 /**
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 Roos1c0ca502015-10-07 12:20:42 -0700129 Preconditions.checkNotNull(entry);
130
Adrian Roos7813dd72016-09-23 17:12:17 -0700131 pruneWeakThenRemoveAndContains(null /* contains */, entry /* remove */, token);
Adrian Roos1c0ca502015-10-07 12:20:42 -0700132
133 apply(entry);
134 }
135
Adrian Roos7813dd72016-09-23 17:12:17 -0700136 /**
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 Roosc0a579e2016-03-30 16:43:58 -0700147 }
148
Adrian Roos7813dd72016-09-23 17:12:17 -0700149 /**
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 Roosc0a579e2016-03-30 16:43:58 -0700163 }
164
165 public boolean isSpinning(String key) {
Adrian Roos7813dd72016-09-23 17:12:17 -0700166 return mSpinning.containsKey(key);
Adrian Roosc0a579e2016-03-30 16:43:58 -0700167 }
168
Adrian Roos1c0ca502015-10-07 12:20:42 -0700169 private void apply(NotificationData.Entry entry) {
yoshiki iguchiff4696c2017-11-22 16:47:41 +0900170 mDelegate.setRemoteInputActive(entry, isRemoteInputActive(entry));
Adrian Roosd28ccd72016-01-06 15:23:14 +0100171 boolean remoteInputActive = isRemoteInputActive();
172 int N = mCallbacks.size();
173 for (int i = 0; i < N; i++) {
174 mCallbacks.get(i).onRemoteInputActive(remoteInputActive);
175 }
Adrian Roos1c0ca502015-10-07 12:20:42 -0700176 }
177
178 /**
179 * @return true if {@param entry} has an active RemoteInput
180 */
181 public boolean isRemoteInputActive(NotificationData.Entry entry) {
Adrian Roos7813dd72016-09-23 17:12:17 -0700182 return pruneWeakThenRemoveAndContains(entry /* contains */, null /* remove */,
183 null /* removeToken */);
Adrian Roos1c0ca502015-10-07 12:20:42 -0700184 }
185
186 /**
187 * @return true if any entry has an active RemoteInput
188 */
189 public boolean isRemoteInputActive() {
Adrian Roos7813dd72016-09-23 17:12:17 -0700190 pruneWeakThenRemoveAndContains(null /* contains */, null /* remove */,
191 null /* removeToken */);
Adrian Roosc0a579e2016-03-30 16:43:58 -0700192 return !mOpen.isEmpty();
Adrian Roos1c0ca502015-10-07 12:20:42 -0700193 }
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 Roos7813dd72016-09-23 17:12:17 -0700199 * @param removeToken if non-null, only removes an entry if this matches the token when the
200 * entry was added.
Adrian Roos1c0ca502015-10-07 12:20:42 -0700201 * @return true if {@param contains} is in the set of active remote inputs
202 */
203 private boolean pruneWeakThenRemoveAndContains(
Adrian Roos7813dd72016-09-23 17:12:17 -0700204 NotificationData.Entry contains, NotificationData.Entry remove, Object removeToken) {
Adrian Roos1c0ca502015-10-07 12:20:42 -0700205 boolean found = false;
Adrian Roosc0a579e2016-03-30 16:43:58 -0700206 for (int i = mOpen.size() - 1; i >= 0; i--) {
Adrian Roos7813dd72016-09-23 17:12:17 -0700207 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 Roosc0a579e2016-03-30 16:43:58 -0700212 mOpen.remove(i);
Adrian Roos1c0ca502015-10-07 12:20:42 -0700213 } else if (item == contains) {
Adrian Roos7813dd72016-09-23 17:12:17 -0700214 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 Roos1c0ca502015-10-07 12:20:42 -0700220 }
221 }
222 return found;
223 }
224
225
Adrian Roosd28ccd72016-01-06 15:23:14 +0100226 public void addCallback(Callback callback) {
227 Preconditions.checkNotNull(callback);
228 mCallbacks.add(callback);
229 }
230
Adrian Roosc0a579e2016-03-30 16:43:58 -0700231 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 Roos07215352016-06-23 11:19:28 -0700238 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 Roos7813dd72016-09-23 17:12:17 -0700246 NotificationData.Entry item = mOpen.get(i).first.get();
Adrian Roos07215352016-06-23 11:19:28 -0700247 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 iguchiff4696c2017-11-22 16:47:41 +0900260 public void requestDisallowLongPressAndDismiss() {
261 mDelegate.requestDisallowLongPressAndDismiss();
262 }
263
264 public void lockScrollTo(NotificationData.Entry entry) {
265 mDelegate.lockScrollTo(entry);
266 }
267
Adrian Roosd28ccd72016-01-06 15:23:14 +0100268 public interface Callback {
Adrian Roosc0a579e2016-03-30 16:43:58 -0700269 default void onRemoteInputActive(boolean active) {}
270
271 default void onRemoteInputSent(NotificationData.Entry entry) {}
Adrian Roosd28ccd72016-01-06 15:23:14 +0100272 }
yoshiki iguchiff4696c2017-11-22 16:47:41 +0900273
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 Roos1c0ca502015-10-07 12:20:42 -0700291}