blob: d7e47c27dc718e4ede3f8d59378f6c42c6cc722f [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
24import java.lang.ref.WeakReference;
25import java.util.ArrayList;
26
27/**
28 * Keeps track of the currently active {@link RemoteInputView}s.
29 */
30public class RemoteInputController {
31
32 private final ArrayList<WeakReference<NotificationData.Entry>> mRemoteInputs = new ArrayList<>();
Adrian Roosd28ccd72016-01-06 15:23:14 +010033 private final ArrayList<Callback> mCallbacks = new ArrayList<>(3);
Adrian Roos1c0ca502015-10-07 12:20:42 -070034 private final HeadsUpManager mHeadsUpManager;
35
36 public RemoteInputController(StatusBarWindowManager sbwm, HeadsUpManager headsUpManager) {
Adrian Roosd28ccd72016-01-06 15:23:14 +010037 addCallback(sbwm);
Adrian Roos1c0ca502015-10-07 12:20:42 -070038 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 Roos1c0ca502015-10-07 12:20:42 -070062 mHeadsUpManager.setRemoteInputActive(entry, isRemoteInputActive(entry));
Adrian Roosd28ccd72016-01-06 15:23:14 +010063 boolean remoteInputActive = isRemoteInputActive();
64 int N = mCallbacks.size();
65 for (int i = 0; i < N; i++) {
66 mCallbacks.get(i).onRemoteInputActive(remoteInputActive);
67 }
Adrian Roos1c0ca502015-10-07 12:20:42 -070068 }
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 Roosd28ccd72016-01-06 15:23:14 +0100106 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 Roos1c0ca502015-10-07 12:20:42 -0700114}