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