blob: cfc69a8f67a991e7f4f8606f009b9d42fd30d568 [file] [log] [blame]
Justin Klaassen10d07c82017-09-15 17:58:39 -04001/*
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;
20import com.android.systemui.Dependency;
21import com.android.systemui.statusbar.phone.StatusBarWindowManager;
Justin Klaassen10d07c82017-09-15 17:58:39 -040022import com.android.systemui.statusbar.policy.RemoteInputView;
23
Justin Klaassen98fe7812018-01-03 13:39:41 -050024import android.app.Notification;
25import android.app.RemoteInput;
26import android.content.Context;
27import android.os.SystemProperties;
Justin Klaassen10d07c82017-09-15 17:58:39 -040028import android.util.ArrayMap;
29import android.util.ArraySet;
30import android.util.Pair;
31
32import java.lang.ref.WeakReference;
33import java.util.ArrayList;
Justin Klaassen98fe7812018-01-03 13:39:41 -050034import java.util.List;
Justin Klaassen10d07c82017-09-15 17:58:39 -040035
36/**
37 * Keeps track of the currently active {@link RemoteInputView}s.
38 */
39public class RemoteInputController {
Justin Klaassen98fe7812018-01-03 13:39:41 -050040 private static final boolean ENABLE_REMOTE_INPUT =
41 SystemProperties.getBoolean("debug.enable_remote_input", true);
Justin Klaassen10d07c82017-09-15 17:58:39 -040042
43 private final ArrayList<Pair<WeakReference<NotificationData.Entry>, Object>> mOpen
44 = new ArrayList<>();
45 private final ArrayMap<String, Object> mSpinning = new ArrayMap<>();
46 private final ArrayList<Callback> mCallbacks = new ArrayList<>(3);
Justin Klaassen4217cf82017-11-30 18:18:21 -050047 private final Delegate mDelegate;
Justin Klaassen10d07c82017-09-15 17:58:39 -040048
Justin Klaassen4217cf82017-11-30 18:18:21 -050049 public RemoteInputController(Delegate delegate) {
Justin Klaassen4217cf82017-11-30 18:18:21 -050050 mDelegate = delegate;
Justin Klaassen10d07c82017-09-15 17:58:39 -040051 }
52
53 /**
Justin Klaassen98fe7812018-01-03 13:39:41 -050054 * 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 /**
Justin Klaassen10d07c82017-09-15 17:58:39 -0400101 * 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) {
107 Preconditions.checkNotNull(entry);
108 Preconditions.checkNotNull(token);
109
110 boolean found = pruneWeakThenRemoveAndContains(
111 entry /* contains */, null /* remove */, token /* removeToken */);
112 if (!found) {
113 mOpen.add(new Pair<>(new WeakReference<>(entry), token));
114 }
115
116 apply(entry);
117 }
118
119 /**
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) {
128 Preconditions.checkNotNull(entry);
129
130 pruneWeakThenRemoveAndContains(null /* contains */, entry /* remove */, token);
131
132 apply(entry);
133 }
134
135 /**
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);
146 }
147
148 /**
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 }
162 }
163
164 public boolean isSpinning(String key) {
165 return mSpinning.containsKey(key);
166 }
167
168 private void apply(NotificationData.Entry entry) {
Justin Klaassen4217cf82017-11-30 18:18:21 -0500169 mDelegate.setRemoteInputActive(entry, isRemoteInputActive(entry));
Justin Klaassen10d07c82017-09-15 17:58:39 -0400170 boolean remoteInputActive = isRemoteInputActive();
171 int N = mCallbacks.size();
172 for (int i = 0; i < N; i++) {
173 mCallbacks.get(i).onRemoteInputActive(remoteInputActive);
174 }
175 }
176
177 /**
178 * @return true if {@param entry} has an active RemoteInput
179 */
180 public boolean isRemoteInputActive(NotificationData.Entry entry) {
181 return pruneWeakThenRemoveAndContains(entry /* contains */, null /* remove */,
182 null /* removeToken */);
183 }
184
185 /**
186 * @return true if any entry has an active RemoteInput
187 */
188 public boolean isRemoteInputActive() {
189 pruneWeakThenRemoveAndContains(null /* contains */, null /* remove */,
190 null /* removeToken */);
191 return !mOpen.isEmpty();
192 }
193
194 /**
195 * Prunes dangling weak references, removes entries referring to {@param remove} and returns
196 * whether {@param contains} is part of the array in a single loop.
197 * @param remove if non-null, removes this entry from the active remote inputs
198 * @param removeToken if non-null, only removes an entry if this matches the token when the
199 * entry was added.
200 * @return true if {@param contains} is in the set of active remote inputs
201 */
202 private boolean pruneWeakThenRemoveAndContains(
203 NotificationData.Entry contains, NotificationData.Entry remove, Object removeToken) {
204 boolean found = false;
205 for (int i = mOpen.size() - 1; i >= 0; i--) {
206 NotificationData.Entry item = mOpen.get(i).first.get();
207 Object itemToken = mOpen.get(i).second;
208 boolean removeTokenMatches = (removeToken == null || itemToken == removeToken);
209
210 if (item == null || (item == remove && removeTokenMatches)) {
211 mOpen.remove(i);
212 } else if (item == contains) {
213 if (removeToken != null && removeToken != itemToken) {
214 // We need to update the token. Remove here and let caller reinsert it.
215 mOpen.remove(i);
216 } else {
217 found = true;
218 }
219 }
220 }
221 return found;
222 }
223
224
225 public void addCallback(Callback callback) {
226 Preconditions.checkNotNull(callback);
227 mCallbacks.add(callback);
228 }
229
230 public void remoteInputSent(NotificationData.Entry entry) {
231 int N = mCallbacks.size();
232 for (int i = 0; i < N; i++) {
233 mCallbacks.get(i).onRemoteInputSent(entry);
234 }
235 }
236
237 public void closeRemoteInputs() {
238 if (mOpen.size() == 0) {
239 return;
240 }
241
242 // Make a copy because closing the remote inputs will modify mOpen.
243 ArrayList<NotificationData.Entry> list = new ArrayList<>(mOpen.size());
244 for (int i = mOpen.size() - 1; i >= 0; i--) {
245 NotificationData.Entry item = mOpen.get(i).first.get();
246 if (item != null && item.row != null) {
247 list.add(item);
248 }
249 }
250
251 for (int i = list.size() - 1; i >= 0; i--) {
252 NotificationData.Entry item = list.get(i);
253 if (item.row != null) {
254 item.row.closeRemoteInput();
255 }
256 }
257 }
258
Justin Klaassen4217cf82017-11-30 18:18:21 -0500259 public void requestDisallowLongPressAndDismiss() {
260 mDelegate.requestDisallowLongPressAndDismiss();
261 }
262
263 public void lockScrollTo(NotificationData.Entry entry) {
264 mDelegate.lockScrollTo(entry);
265 }
266
Justin Klaassen10d07c82017-09-15 17:58:39 -0400267 public interface Callback {
268 default void onRemoteInputActive(boolean active) {}
269
270 default void onRemoteInputSent(NotificationData.Entry entry) {}
271 }
Justin Klaassen4217cf82017-11-30 18:18:21 -0500272
273 public interface Delegate {
274 /**
275 * Activate remote input if necessary.
276 */
277 void setRemoteInputActive(NotificationData.Entry entry, boolean remoteInputActive);
278
279 /**
280 * Request that the view does not dismiss nor perform long press for the current touch.
281 */
282 void requestDisallowLongPressAndDismiss();
283
284 /**
285 * Request that the view is made visible by scrolling to it, and keep the scroll locked until
286 * the user scrolls, or {@param v} loses focus or is detached.
287 */
288 void lockScrollTo(NotificationData.Entry entry);
289 }
Justin Klaassen10d07c82017-09-15 17:58:39 -0400290}