blob: f243b00143a289f267180967a2f560000e10d9c8 [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<>();
33 private final StatusBarWindowManager mStatusBarWindowManager;
34 private final HeadsUpManager mHeadsUpManager;
35
36 public RemoteInputController(StatusBarWindowManager sbwm, HeadsUpManager headsUpManager) {
37 mStatusBarWindowManager = sbwm;
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) {
62 mStatusBarWindowManager.setRemoteInputActive(isRemoteInputActive());
63 mHeadsUpManager.setRemoteInputActive(entry, isRemoteInputActive(entry));
64 }
65
66 /**
67 * @return true if {@param entry} has an active RemoteInput
68 */
69 public boolean isRemoteInputActive(NotificationData.Entry entry) {
70 return pruneWeakThenRemoveAndContains(entry /* contains */, null /* remove */);
71 }
72
73 /**
74 * @return true if any entry has an active RemoteInput
75 */
76 public boolean isRemoteInputActive() {
77 pruneWeakThenRemoveAndContains(null /* contains */, null /* remove */);
78 return !mRemoteInputs.isEmpty();
79 }
80
81 /**
82 * Prunes dangling weak references, removes entries referring to {@param remove} and returns
83 * whether {@param contains} is part of the array in a single loop.
84 * @param remove if non-null, removes this entry from the active remote inputs
85 * @return true if {@param contains} is in the set of active remote inputs
86 */
87 private boolean pruneWeakThenRemoveAndContains(
88 NotificationData.Entry contains, NotificationData.Entry remove) {
89 boolean found = false;
90 for (int i = mRemoteInputs.size() - 1; i >= 0; i--) {
91 NotificationData.Entry item = mRemoteInputs.get(i).get();
92 if (item == null || item == remove) {
93 mRemoteInputs.remove(i);
94 } else if (item == contains) {
95 found = true;
96 }
97 }
98 return found;
99 }
100
101
102}