blob: 5fea674f47b1ff8d279f456ad5eb5e96790e46c4 [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;
20import com.android.systemui.statusbar.phone.StatusBarWindowManager;
21import com.android.systemui.statusbar.policy.HeadsUpManager;
22import com.android.systemui.statusbar.policy.RemoteInputView;
23
Adrian Roosc0a579e2016-03-30 16:43:58 -070024import android.util.ArraySet;
25
Adrian Roos1c0ca502015-10-07 12:20:42 -070026import java.lang.ref.WeakReference;
27import java.util.ArrayList;
28
29/**
30 * Keeps track of the currently active {@link RemoteInputView}s.
31 */
32public class RemoteInputController {
33
Adrian Roosc0a579e2016-03-30 16:43:58 -070034 private final ArrayList<WeakReference<NotificationData.Entry>> mOpen = new ArrayList<>();
35 private final ArraySet<String> mSpinning = new ArraySet<>();
Adrian Roosd28ccd72016-01-06 15:23:14 +010036 private final ArrayList<Callback> mCallbacks = new ArrayList<>(3);
Adrian Roos1c0ca502015-10-07 12:20:42 -070037 private final HeadsUpManager mHeadsUpManager;
38
39 public RemoteInputController(StatusBarWindowManager sbwm, HeadsUpManager headsUpManager) {
Adrian Roosd28ccd72016-01-06 15:23:14 +010040 addCallback(sbwm);
Adrian Roos1c0ca502015-10-07 12:20:42 -070041 mHeadsUpManager = headsUpManager;
42 }
43
44 public void addRemoteInput(NotificationData.Entry entry) {
45 Preconditions.checkNotNull(entry);
46
47 boolean found = pruneWeakThenRemoveAndContains(
48 entry /* contains */, null /* remove */);
49 if (!found) {
Adrian Roosc0a579e2016-03-30 16:43:58 -070050 mOpen.add(new WeakReference<>(entry));
Adrian Roos1c0ca502015-10-07 12:20:42 -070051 }
52
53 apply(entry);
54 }
55
56 public void removeRemoteInput(NotificationData.Entry entry) {
57 Preconditions.checkNotNull(entry);
58
59 pruneWeakThenRemoveAndContains(null /* contains */, entry /* remove */);
60
61 apply(entry);
62 }
63
Adrian Roosc0a579e2016-03-30 16:43:58 -070064 public void addSpinning(String key) {
65 mSpinning.add(key);
66 }
67
68 public void removeSpinning(String key) {
69 mSpinning.remove(key);
70 }
71
72 public boolean isSpinning(String key) {
73 return mSpinning.contains(key);
74 }
75
Adrian Roos1c0ca502015-10-07 12:20:42 -070076 private void apply(NotificationData.Entry entry) {
Adrian Roos1c0ca502015-10-07 12:20:42 -070077 mHeadsUpManager.setRemoteInputActive(entry, isRemoteInputActive(entry));
Adrian Roosd28ccd72016-01-06 15:23:14 +010078 boolean remoteInputActive = isRemoteInputActive();
79 int N = mCallbacks.size();
80 for (int i = 0; i < N; i++) {
81 mCallbacks.get(i).onRemoteInputActive(remoteInputActive);
82 }
Adrian Roos1c0ca502015-10-07 12:20:42 -070083 }
84
85 /**
86 * @return true if {@param entry} has an active RemoteInput
87 */
88 public boolean isRemoteInputActive(NotificationData.Entry entry) {
89 return pruneWeakThenRemoveAndContains(entry /* contains */, null /* remove */);
90 }
91
92 /**
93 * @return true if any entry has an active RemoteInput
94 */
95 public boolean isRemoteInputActive() {
96 pruneWeakThenRemoveAndContains(null /* contains */, null /* remove */);
Adrian Roosc0a579e2016-03-30 16:43:58 -070097 return !mOpen.isEmpty();
Adrian Roos1c0ca502015-10-07 12:20:42 -070098 }
99
100 /**
101 * Prunes dangling weak references, removes entries referring to {@param remove} and returns
102 * whether {@param contains} is part of the array in a single loop.
103 * @param remove if non-null, removes this entry from the active remote inputs
104 * @return true if {@param contains} is in the set of active remote inputs
105 */
106 private boolean pruneWeakThenRemoveAndContains(
107 NotificationData.Entry contains, NotificationData.Entry remove) {
108 boolean found = false;
Adrian Roosc0a579e2016-03-30 16:43:58 -0700109 for (int i = mOpen.size() - 1; i >= 0; i--) {
110 NotificationData.Entry item = mOpen.get(i).get();
Adrian Roos1c0ca502015-10-07 12:20:42 -0700111 if (item == null || item == remove) {
Adrian Roosc0a579e2016-03-30 16:43:58 -0700112 mOpen.remove(i);
Adrian Roos1c0ca502015-10-07 12:20:42 -0700113 } else if (item == contains) {
114 found = true;
115 }
116 }
117 return found;
118 }
119
120
Adrian Roosd28ccd72016-01-06 15:23:14 +0100121 public void addCallback(Callback callback) {
122 Preconditions.checkNotNull(callback);
123 mCallbacks.add(callback);
124 }
125
Adrian Roosc0a579e2016-03-30 16:43:58 -0700126 public void remoteInputSent(NotificationData.Entry entry) {
127 int N = mCallbacks.size();
128 for (int i = 0; i < N; i++) {
129 mCallbacks.get(i).onRemoteInputSent(entry);
130 }
131 }
132
Adrian Roosd28ccd72016-01-06 15:23:14 +0100133 public interface Callback {
Adrian Roosc0a579e2016-03-30 16:43:58 -0700134 default void onRemoteInputActive(boolean active) {}
135
136 default void onRemoteInputSent(NotificationData.Entry entry) {}
Adrian Roosd28ccd72016-01-06 15:23:14 +0100137 }
Adrian Roos1c0ca502015-10-07 12:20:42 -0700138}