blob: 4f5a48f25695301abd49e9022bc869f920e2dc96 [file] [log] [blame]
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001/*
2 * Copyright (C) 2018 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.bubbles;
18
Mady Mellord1c78b262018-11-06 18:04:40 -080019import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080020import static android.view.View.VISIBLE;
21import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
22
23import static com.android.systemui.bubbles.BubbleMovementHelper.EDGE_OVERLAP;
24
Mady Mellor5549dd22018-11-06 18:07:34 -080025import android.app.Notification;
26import android.app.NotificationManager;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080027import android.content.Context;
28import android.graphics.Point;
Mady Mellord1c78b262018-11-06 18:04:40 -080029import android.graphics.Rect;
Mady Mellor5549dd22018-11-06 18:07:34 -080030import android.service.notification.StatusBarNotification;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080031import android.view.ViewGroup;
32import android.view.WindowManager;
33import android.widget.FrameLayout;
34
Mady Mellorebdbbb92018-11-15 14:36:48 -080035import com.android.internal.annotations.VisibleForTesting;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080036import com.android.systemui.Dependency;
37import com.android.systemui.R;
38import com.android.systemui.statusbar.notification.NotificationData;
39import com.android.systemui.statusbar.phone.StatusBarWindowController;
40
Mady Mellor5549dd22018-11-06 18:07:34 -080041import java.util.ArrayList;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080042import java.util.HashMap;
43import java.util.Map;
44
45/**
46 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
47 * Bubbles can be expanded to show more content.
48 *
49 * The controller manages addition, removal, and visible state of bubbles on screen.
50 */
51public class BubbleController {
52 private static final int MAX_BUBBLES = 5; // TODO: actually enforce this
53
54 private static final String TAG = "BubbleController";
55
Mady Mellor5549dd22018-11-06 18:07:34 -080056 // Enables some subset of notifs to automatically become bubbles
57 public static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
58 // When a bubble is dismissed, recreate it as a notification
59 public static final boolean DEBUG_DEMOTE_TO_NOTIF = false;
60
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080061 private Context mContext;
Mady Mellor5549dd22018-11-06 18:07:34 -080062 private BubbleDismissListener mDismissListener;
Mady Mellord1c78b262018-11-06 18:04:40 -080063 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -080064 private BubbleExpandListener mExpandListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080065
66 private Map<String, BubbleView> mBubbles = new HashMap<>();
67 private BubbleStackView mStackView;
68 private Point mDisplaySize;
69
70 // Bubbles get added to the status bar view
Mady Mellorebdbbb92018-11-15 14:36:48 -080071 @VisibleForTesting
72 protected StatusBarWindowController mStatusBarWindowController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080073
Mady Mellord1c78b262018-11-06 18:04:40 -080074 // Used for determining view rect for touch interaction
75 private Rect mTempRect = new Rect();
76
Mady Mellor5549dd22018-11-06 18:07:34 -080077 /**
78 * Listener to find out about bubble / bubble stack dismissal events.
79 */
80 public interface BubbleDismissListener {
81 /**
82 * Called when the entire stack of bubbles is dismissed by the user.
83 */
84 void onStackDismissed();
85
86 /**
87 * Called when a specific bubble is dismissed by the user.
88 */
89 void onBubbleDismissed(String key);
90 }
91
Mady Mellord1c78b262018-11-06 18:04:40 -080092 /**
93 * Listener to be notified when some states of the bubbles change.
94 */
95 public interface BubbleStateChangeListener {
96 /**
97 * Called when the stack has bubbles or no longer has bubbles.
98 */
99 void onHasBubblesChanged(boolean hasBubbles);
100 }
101
Mady Mellorcd9b1302018-11-06 18:08:04 -0800102 /**
103 * Listener to find out about stack expansion / collapse events.
104 */
105 public interface BubbleExpandListener {
106 /**
107 * Called when the expansion state of the bubble stack changes.
108 *
109 * @param isExpanding whether it's expanding or collapsing
110 * @param amount fraction of how expanded or collapsed it is, 1 being fully, 0 at the start
111 */
112 void onBubbleExpandChanged(boolean isExpanding, float amount);
113 }
114
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800115 public BubbleController(Context context) {
116 mContext = context;
117 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
118 mDisplaySize = new Point();
119 wm.getDefaultDisplay().getSize(mDisplaySize);
120 mStatusBarWindowController = Dependency.get(StatusBarWindowController.class);
121 }
122
123 /**
Mady Mellor5549dd22018-11-06 18:07:34 -0800124 * Set a listener to be notified of bubble dismissal events.
125 */
126 public void setDismissListener(BubbleDismissListener listener) {
127 mDismissListener = listener;
128 }
129
130 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800131 * Set a listener to be notified when some states of the bubbles change.
132 */
133 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
134 mStateChangeListener = listener;
135 }
136
137 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800138 * Set a listener to be notified of bubble expand events.
139 */
140 public void setExpandListener(BubbleExpandListener listener) {
141 mExpandListener = listener;
142 if (mStackView != null) {
143 mStackView.setExpandListener(mExpandListener);
144 }
145 }
146
147 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800148 * Whether or not there are bubbles present, regardless of them being visible on the
149 * screen (e.g. if on AOD).
150 */
151 public boolean hasBubbles() {
152 return mBubbles.size() > 0;
153 }
154
155 /**
156 * Whether the stack of bubbles is expanded or not.
157 */
158 public boolean isStackExpanded() {
159 return mStackView != null && mStackView.isExpanded();
160 }
161
162 /**
163 * Tell the stack of bubbles to collapse.
164 */
165 public void collapseStack() {
166 if (mStackView != null) {
167 mStackView.animateExpansion(false);
168 }
169 }
170
171 /**
172 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
173 */
174 public void dismissStack() {
Mady Mellord1c78b262018-11-06 18:04:40 -0800175 if (mStackView == null) {
176 return;
177 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800178 Point startPoint = getStartPoint(mStackView.getStackWidth(), mDisplaySize);
179 // Reset the position of the stack (TODO - or should we save / respect last user position?)
180 mStackView.setPosition(startPoint.x, startPoint.y);
181 for (String key: mBubbles.keySet()) {
182 removeBubble(key);
183 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800184 if (mDismissListener != null) {
185 mDismissListener.onStackDismissed();
186 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800187 updateBubblesShowing();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800188 }
189
190 /**
191 * Adds a bubble associated with the provided notification entry or updates it if it exists.
192 */
193 public void addBubble(NotificationData.Entry notif) {
194 if (mBubbles.containsKey(notif.key)) {
195 // It's an update
196 BubbleView bubble = mBubbles.get(notif.key);
197 mStackView.updateBubble(bubble, notif);
198 } else {
199 // It's new
200 BubbleView bubble = new BubbleView(mContext);
201 bubble.setNotif(notif);
202 mBubbles.put(bubble.getKey(), bubble);
203
Mady Mellord1c78b262018-11-06 18:04:40 -0800204 boolean setPosition = mStackView != null && mStackView.getVisibility() != VISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800205 if (mStackView == null) {
206 setPosition = true;
207 mStackView = new BubbleStackView(mContext);
Mady Mellord1c78b262018-11-06 18:04:40 -0800208 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800209 // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim
210 // between bubble and the shade
211 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
212 sbv.addView(mStackView, bubblePosition,
213 new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
Mady Mellorcd9b1302018-11-06 18:08:04 -0800214 if (mExpandListener != null) {
215 mStackView.setExpandListener(mExpandListener);
216 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800217 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800218 mStackView.addBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800219 if (setPosition) {
220 // Need to add the bubble to the stack before we can know the width
221 Point startPoint = getStartPoint(mStackView.getStackWidth(), mDisplaySize);
222 mStackView.setPosition(startPoint.x, startPoint.y);
Mady Mellord1c78b262018-11-06 18:04:40 -0800223 mStackView.setVisibility(VISIBLE);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800224 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800225 updateBubblesShowing();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800226 }
227 }
228
229 /**
230 * Removes the bubble associated with the {@param uri}.
231 */
232 public void removeBubble(String key) {
233 BubbleView bv = mBubbles.get(key);
Mady Mellord1c78b262018-11-06 18:04:40 -0800234 if (mStackView != null && bv != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800235 mStackView.removeBubble(bv);
236 bv.getEntry().setBubbleDismissed(true);
237 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800238 if (mDismissListener != null) {
239 mDismissListener.onBubbleDismissed(key);
240 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800241 updateBubblesShowing();
242 }
243
244 private void updateBubblesShowing() {
245 boolean hasBubblesShowing = false;
246 for (BubbleView bv : mBubbles.values()) {
247 if (!bv.getEntry().isBubbleDismissed()) {
248 hasBubblesShowing = true;
249 break;
250 }
251 }
252 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
253 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
254 if (mStackView != null && !hasBubblesShowing) {
255 mStackView.setVisibility(INVISIBLE);
256 }
257 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
258 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
259 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800260 }
261
262 /**
263 * Sets the visibility of the bubbles, doesn't un-bubble them, just changes visibility.
264 */
265 public void updateVisibility(boolean visible) {
266 if (mStackView == null) {
267 return;
268 }
269 ArrayList<BubbleView> viewsToRemove = new ArrayList<>();
270 for (BubbleView bv : mBubbles.values()) {
271 NotificationData.Entry entry = bv.getEntry();
272 if (entry != null) {
273 if (entry.row.isRemoved() || entry.isBubbleDismissed() || entry.row.isDismissed()) {
274 viewsToRemove.add(bv);
275 }
276 }
277 }
278 for (BubbleView view : viewsToRemove) {
279 mBubbles.remove(view.getKey());
280 mStackView.removeBubble(view);
Mady Mellor5549dd22018-11-06 18:07:34 -0800281 }
282 if (mStackView != null) {
Mady Mellord1c78b262018-11-06 18:04:40 -0800283 mStackView.setVisibility(visible ? VISIBLE : INVISIBLE);
284 if (!visible) {
285 collapseStack();
286 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800287 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800288 updateBubblesShowing();
289 }
290
291 /**
292 * Rect indicating the touchable region for the bubble stack / expanded stack.
293 */
294 public Rect getTouchableRegion() {
295 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
296 return null;
297 }
298 mStackView.getBoundsOnScreen(mTempRect);
299 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800300 }
301
Mady Mellorebdbbb92018-11-15 14:36:48 -0800302 @VisibleForTesting
303 public BubbleStackView getStackView() {
304 return mStackView;
305 }
306
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800307 // TODO: factor in PIP location / maybe last place user had it
308 /**
309 * Gets an appropriate starting point to position the bubble stack.
310 */
311 public static Point getStartPoint(int size, Point displaySize) {
312 final int x = displaySize.x - size + EDGE_OVERLAP;
313 final int y = displaySize.y / 4;
314 return new Point(x, y);
315 }
316
317 /**
318 * Gets an appropriate position for the bubble when the stack is expanded.
319 */
320 public static Point getExpandPoint(BubbleStackView view, int size, Point displaySize) {
321 // Same place for now..
322 return new Point(EDGE_OVERLAP, size);
323 }
324
Mady Mellor5549dd22018-11-06 18:07:34 -0800325 /**
326 * Whether the notification should bubble or not.
327 */
328 public static boolean shouldAutoBubble(NotificationData.Entry entry, int priority,
329 boolean canAppOverlay) {
330 if (!DEBUG_ENABLE_AUTO_BUBBLE || entry.isBubbleDismissed()) {
331 return false;
332 }
333 StatusBarNotification n = entry.notification;
334 boolean hasRemoteInput = false;
335 if (n.getNotification().actions != null) {
336 for (Notification.Action action : n.getNotification().actions) {
337 if (action.getRemoteInputs() != null) {
338 hasRemoteInput = true;
339 break;
340 }
341 }
342 }
343 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
344 boolean shouldBubble = priority >= NotificationManager.IMPORTANCE_HIGH
345 || Notification.MessagingStyle.class.equals(style)
346 || Notification.CATEGORY_MESSAGE.equals(n.getNotification().category)
347 || hasRemoteInput
348 || canAppOverlay;
349 return shouldBubble && !entry.isBubbleDismissed();
350 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800351}