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; |
| 20 | import com.android.systemui.statusbar.phone.StatusBarWindowManager; |
| 21 | import com.android.systemui.statusbar.policy.HeadsUpManager; |
| 22 | import com.android.systemui.statusbar.policy.RemoteInputView; |
| 23 | |
| 24 | import java.lang.ref.WeakReference; |
| 25 | import java.util.ArrayList; |
| 26 | |
| 27 | /** |
| 28 | * Keeps track of the currently active {@link RemoteInputView}s. |
| 29 | */ |
| 30 | public class RemoteInputController { |
| 31 | |
| 32 | private final ArrayList<WeakReference<NotificationData.Entry>> mRemoteInputs = new ArrayList<>(); |
Adrian Roos | d28ccd7 | 2016-01-06 15:23:14 +0100 | [diff] [blame] | 33 | private final ArrayList<Callback> mCallbacks = new ArrayList<>(3); |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 34 | private final HeadsUpManager mHeadsUpManager; |
| 35 | |
| 36 | public RemoteInputController(StatusBarWindowManager sbwm, HeadsUpManager headsUpManager) { |
Adrian Roos | d28ccd7 | 2016-01-06 15:23:14 +0100 | [diff] [blame] | 37 | addCallback(sbwm); |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 38 | mHeadsUpManager = headsUpManager; |
| 39 | } |
| 40 | |
| 41 | public void addRemoteInput(NotificationData.Entry entry) { |
| 42 | Preconditions.checkNotNull(entry); |
| 43 | |
| 44 | boolean found = pruneWeakThenRemoveAndContains( |
| 45 | entry /* contains */, null /* remove */); |
| 46 | if (!found) { |
| 47 | mRemoteInputs.add(new WeakReference<>(entry)); |
| 48 | } |
| 49 | |
| 50 | apply(entry); |
| 51 | } |
| 52 | |
| 53 | public void removeRemoteInput(NotificationData.Entry entry) { |
| 54 | Preconditions.checkNotNull(entry); |
| 55 | |
| 56 | pruneWeakThenRemoveAndContains(null /* contains */, entry /* remove */); |
| 57 | |
| 58 | apply(entry); |
| 59 | } |
| 60 | |
| 61 | private void apply(NotificationData.Entry entry) { |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 62 | mHeadsUpManager.setRemoteInputActive(entry, isRemoteInputActive(entry)); |
Adrian Roos | d28ccd7 | 2016-01-06 15:23:14 +0100 | [diff] [blame] | 63 | boolean remoteInputActive = isRemoteInputActive(); |
| 64 | int N = mCallbacks.size(); |
| 65 | for (int i = 0; i < N; i++) { |
| 66 | mCallbacks.get(i).onRemoteInputActive(remoteInputActive); |
| 67 | } |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @return true if {@param entry} has an active RemoteInput |
| 72 | */ |
| 73 | public boolean isRemoteInputActive(NotificationData.Entry entry) { |
| 74 | return pruneWeakThenRemoveAndContains(entry /* contains */, null /* remove */); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @return true if any entry has an active RemoteInput |
| 79 | */ |
| 80 | public boolean isRemoteInputActive() { |
| 81 | pruneWeakThenRemoveAndContains(null /* contains */, null /* remove */); |
| 82 | return !mRemoteInputs.isEmpty(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Prunes dangling weak references, removes entries referring to {@param remove} and returns |
| 87 | * whether {@param contains} is part of the array in a single loop. |
| 88 | * @param remove if non-null, removes this entry from the active remote inputs |
| 89 | * @return true if {@param contains} is in the set of active remote inputs |
| 90 | */ |
| 91 | private boolean pruneWeakThenRemoveAndContains( |
| 92 | NotificationData.Entry contains, NotificationData.Entry remove) { |
| 93 | boolean found = false; |
| 94 | for (int i = mRemoteInputs.size() - 1; i >= 0; i--) { |
| 95 | NotificationData.Entry item = mRemoteInputs.get(i).get(); |
| 96 | if (item == null || item == remove) { |
| 97 | mRemoteInputs.remove(i); |
| 98 | } else if (item == contains) { |
| 99 | found = true; |
| 100 | } |
| 101 | } |
| 102 | return found; |
| 103 | } |
| 104 | |
| 105 | |
Adrian Roos | d28ccd7 | 2016-01-06 15:23:14 +0100 | [diff] [blame] | 106 | public void addCallback(Callback callback) { |
| 107 | Preconditions.checkNotNull(callback); |
| 108 | mCallbacks.add(callback); |
| 109 | } |
| 110 | |
| 111 | public interface Callback { |
| 112 | void onRemoteInputActive(boolean active); |
| 113 | } |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 114 | } |