blob: eb85589f3781cbf9ac34a5499c9508becfcc2299 [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;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080021import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080022
Mady Mellor3f2efdb2018-11-21 11:30:45 -080023import static com.android.systemui.statusbar.StatusBarState.SHADE;
24import static com.android.systemui.statusbar.notification.NotificationAlertingManager.alertAgain;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080025
Mark Renouf08bc42a2019-03-07 13:01:59 -050026import static java.lang.annotation.RetentionPolicy.SOURCE;
27
Mady Mellorb4991e62019-01-10 15:14:51 -080028import android.annotation.Nullable;
Mark Renoufc808f062019-02-07 15:20:37 -050029import android.app.ActivityManager.RunningTaskInfo;
Mark Renoufcecc77b2019-01-30 16:32:24 -050030import android.app.ActivityTaskManager;
31import android.app.IActivityTaskManager;
Mady Mellorb4991e62019-01-10 15:14:51 -080032import android.app.INotificationManager;
Mady Mellor5549dd22018-11-06 18:07:34 -080033import android.app.Notification;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080034import android.content.Context;
Joshua Tsujia19515f2019-02-13 18:02:29 -050035import android.content.pm.ParceledListSlice;
Mady Mellord1c78b262018-11-06 18:04:40 -080036import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050037import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080038import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080039import android.provider.Settings;
Mady Mellor5549dd22018-11-06 18:07:34 -080040import android.service.notification.StatusBarNotification;
Mark Renoufcecc77b2019-01-30 16:32:24 -050041import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050042import android.view.IPinnedStackController;
43import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080044import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080045import android.widget.FrameLayout;
46
Mark Renouf08bc42a2019-03-07 13:01:59 -050047import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050048import androidx.annotation.MainThread;
49
Mady Mellorebdbbb92018-11-15 14:36:48 -080050import com.android.internal.annotations.VisibleForTesting;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080051import com.android.internal.statusbar.NotificationVisibility;
Ned Burns01e38212019-01-03 16:32:52 -050052import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080053import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050054import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050055import com.android.systemui.shared.system.ActivityManagerWrapper;
56import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050057import com.android.systemui.shared.system.WindowManagerWrapper;
Ned Burns01e38212019-01-03 16:32:52 -050058import com.android.systemui.statusbar.notification.NotificationEntryListener;
59import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080060import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050061import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050062import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080063import com.android.systemui.statusbar.phone.StatusBarWindowController;
64
Mark Renouf08bc42a2019-03-07 13:01:59 -050065import java.lang.annotation.Retention;
66
Jason Monk27d01a622018-12-10 15:57:09 -050067import javax.inject.Inject;
68import javax.inject.Singleton;
69
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080070/**
71 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
72 * Bubbles can be expanded to show more content.
73 *
74 * The controller manages addition, removal, and visible state of bubbles on screen.
75 */
Jason Monk27d01a622018-12-10 15:57:09 -050076@Singleton
Mady Mellor3d82e682019-02-05 13:34:48 -080077public class BubbleController implements BubbleExpandedView.OnBubbleBlockedListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080078
79 private static final String TAG = "BubbleController";
80
Mark Renouf08bc42a2019-03-07 13:01:59 -050081 private static final int MAX_BUBBLES = 5; // TODO: actually enforce this
82
83 @Retention(SOURCE)
84 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
85 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION})
86 @interface DismissReason {}
87 static final int DISMISS_USER_GESTURE = 1;
88 static final int DISMISS_AGED = 2;
89 static final int DISMISS_TASK_FINISHED = 3;
90 static final int DISMISS_BLOCKED = 4;
91 static final int DISMISS_NOTIF_CANCEL = 5;
92 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
93
Mady Mellor5549dd22018-11-06 18:07:34 -080094 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -050095 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -080096
Mady Mellor44ee2fe2019-01-30 17:51:16 -080097 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -080098 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Mady Mellor44ee2fe2019-01-30 17:51:16 -080099 /** Auto bubble flags set whether different notif types should be presented as a bubble */
Mady Mellorceced172018-11-27 11:18:39 -0800100 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
101 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
102 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800103
104 /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500105 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -0800106
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800107 /** Whether the row of bubble circles are anchored to the top or bottom of the screen. */
108 private static final String ENABLE_BUBBLES_AT_TOP = "experiment_enable_top_bubbles";
109 /** Flag to position the header below the activity view */
110 private static final String ENABLE_BUBBLE_FOOTER = "experiment_enable_bubble_footer";
111
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500112 private static final String BUBBLE_STIFFNESS = "experiment_bubble_stiffness";
113 private static final String BUBBLE_BOUNCINESS = "experiment_bubble_bounciness";
114
Ned Burns01e38212019-01-03 16:32:52 -0500115 private final Context mContext;
116 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500117 private final IActivityTaskManager mActivityTaskManager;
118 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800119 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800120 private BubbleExpandListener mExpandListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800121
Mady Mellor3dff9e62019-02-05 18:12:53 -0800122 private BubbleData mBubbleData;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800123 private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800124
125 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500126 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800127 private StatusBarStateListener mStatusBarStateListener;
128
129 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider =
130 Dependency.get(NotificationInterruptionStateProvider.class);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800131
Mady Mellorb4991e62019-01-10 15:14:51 -0800132 private INotificationManager mNotificationManagerService;
133
Mady Mellord1c78b262018-11-06 18:04:40 -0800134 // Used for determining view rect for touch interaction
135 private Rect mTempRect = new Rect();
136
Mady Mellor5549dd22018-11-06 18:07:34 -0800137 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800138 * Listener to be notified when some states of the bubbles change.
139 */
140 public interface BubbleStateChangeListener {
141 /**
142 * Called when the stack has bubbles or no longer has bubbles.
143 */
144 void onHasBubblesChanged(boolean hasBubbles);
145 }
146
Mady Mellorcd9b1302018-11-06 18:08:04 -0800147 /**
148 * Listener to find out about stack expansion / collapse events.
149 */
150 public interface BubbleExpandListener {
151 /**
152 * Called when the expansion state of the bubble stack changes.
Mady Mellorcd9b1302018-11-06 18:08:04 -0800153 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800154 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800155 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800156 void onBubbleExpandChanged(boolean isExpanding, String key);
157 }
158
159 /**
160 * Listens for the current state of the status bar and updates the visibility state
161 * of bubbles as needed.
162 */
163 private class StatusBarStateListener implements StatusBarStateController.StateListener {
164 private int mState;
165 /**
166 * Returns the current status bar state.
167 */
168 public int getCurrentState() {
169 return mState;
170 }
171
172 @Override
173 public void onStateChanged(int newState) {
174 mState = newState;
175 updateVisibility();
176 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800177 }
178
Jason Monk27d01a622018-12-10 15:57:09 -0500179 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800180 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
181 BubbleData data) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800182 mContext = context;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800183
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800184 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500185 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800186
187 try {
188 mNotificationManagerService = INotificationManager.Stub.asInterface(
189 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
190 } catch (ServiceManager.ServiceNotFoundException e) {
191 e.printStackTrace();
192 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800193
194 mStatusBarWindowController = statusBarWindowController;
195 mStatusBarStateListener = new StatusBarStateListener();
196 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500197
198 mActivityTaskManager = ActivityTaskManager.getService();
199 mTaskStackListener = new BubbleTaskStackListener();
200 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800201
Joshua Tsujia19515f2019-02-13 18:02:29 -0500202 try {
203 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
204 } catch (RemoteException e) {
205 e.printStackTrace();
206 }
207
Mady Mellorcfd06c12019-02-13 14:32:12 -0800208 mBubbleData = data;
Mady Mellor5549dd22018-11-06 18:07:34 -0800209 }
210
211 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800212 * Set a listener to be notified when some states of the bubbles change.
213 */
214 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
215 mStateChangeListener = listener;
216 }
217
218 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800219 * Set a listener to be notified of bubble expand events.
220 */
221 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100222 mExpandListener = ((isExpanding, key) -> {
223 if (listener != null) {
224 listener.onBubbleExpandChanged(isExpanding, key);
225 }
226 mStatusBarWindowController.setBubbleExpanded(isExpanding);
227 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800228 if (mStackView != null) {
229 mStackView.setExpandListener(mExpandListener);
230 }
231 }
232
233 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800234 * Whether or not there are bubbles present, regardless of them being visible on the
235 * screen (e.g. if on AOD).
236 */
237 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800238 if (mStackView == null) {
239 return false;
240 }
241 for (Bubble bubble : mBubbleData.getBubbles()) {
242 if (!bubble.entry.isBubbleDismissed()) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800243 return true;
244 }
245 }
246 return false;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800247 }
248
249 /**
250 * Whether the stack of bubbles is expanded or not.
251 */
252 public boolean isStackExpanded() {
253 return mStackView != null && mStackView.isExpanded();
254 }
255
256 /**
257 * Tell the stack of bubbles to collapse.
258 */
259 public void collapseStack() {
260 if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800261 mStackView.collapseStack();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800262 }
263 }
264
265 /**
266 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
267 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500268 void dismissStack(@DismissReason int reason) {
Mady Mellord1c78b262018-11-06 18:04:40 -0800269 if (mStackView == null) {
270 return;
271 }
Mark Renouf08bc42a2019-03-07 13:01:59 -0500272 mStackView.stackDismissed(reason);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800273
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800274 updateVisibility();
Ned Burns01e38212019-01-03 16:32:52 -0500275 mNotificationEntryManager.updateNotifications();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800276 }
277
278 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500279 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
280 * is forwarded a back key down/up pair.
281 */
282 public void performBackPressIfNeeded() {
283 if (mStackView != null) {
284 mStackView.performBackPressIfNeeded();
285 }
286 }
287
288 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800289 * Adds or updates a bubble associated with the provided notification entry.
290 *
291 * @param notif the notification associated with this bubble.
292 * @param updatePosition whether this update should promote the bubble to the top of the stack.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800293 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800294 public void updateBubble(NotificationEntry notif, boolean updatePosition) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800295 if (mStackView != null && mBubbleData.getBubble(notif.key) != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800296 // It's an update
Mady Mellor3dff9e62019-02-05 18:12:53 -0800297 mStackView.updateBubble(notif, updatePosition);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800298 } else {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800299 if (mStackView == null) {
Mady Mellorcfd06c12019-02-13 14:32:12 -0800300 mStackView = new BubbleStackView(mContext, mBubbleData);
Mady Mellord1c78b262018-11-06 18:04:40 -0800301 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800302 // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim
303 // between bubble and the shade
304 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
305 sbv.addView(mStackView, bubblePosition,
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800306 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
Mady Mellorcd9b1302018-11-06 18:08:04 -0800307 if (mExpandListener != null) {
308 mStackView.setExpandListener(mExpandListener);
309 }
Mady Mellore8e07712019-01-23 12:45:33 -0800310 mStackView.setOnBlockedListener(this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800311 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800312 // It's new
Mady Mellor3dff9e62019-02-05 18:12:53 -0800313 mStackView.addBubble(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800314 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800315 updateVisibility();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800316 }
317
318 /**
319 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500320 * <p>
321 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800322 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500323 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500324 void removeBubble(String key, int reason) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800325 if (mStackView != null) {
Mark Renouf08bc42a2019-03-07 13:01:59 -0500326 mStackView.removeBubble(key, reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800327 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800328 mNotificationEntryManager.updateNotifications();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800329 updateVisibility();
Mady Mellord1c78b262018-11-06 18:04:40 -0800330 }
331
Mady Mellore8e07712019-01-23 12:45:33 -0800332 @Override
333 public void onBubbleBlocked(NotificationEntry entry) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800334 Object[] bubbles = mBubbleData.getBubbles().toArray();
Mady Mellore8e07712019-01-23 12:45:33 -0800335 for (int i = 0; i < bubbles.length; i++) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800336 NotificationEntry e = ((Bubble) bubbles[i]).entry;
Mady Mellore8e07712019-01-23 12:45:33 -0800337 boolean samePackage = entry.notification.getPackageName().equals(
338 e.notification.getPackageName());
339 if (samePackage) {
Mark Renouf08bc42a2019-03-07 13:01:59 -0500340 removeBubble(entry.key, DISMISS_BLOCKED);
Mady Mellore8e07712019-01-23 12:45:33 -0800341 }
342 }
343 }
344
Ned Burns01e38212019-01-03 16:32:52 -0500345 @SuppressWarnings("FieldCanBeLocal")
346 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
347 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500348 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800349 if (!areBubblesEnabled(mContext)) {
350 return;
351 }
352 if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800353 // TODO: handle group summaries
354 // It's a new notif, it shows in the shade and as a bubble
Ned Burns01e38212019-01-03 16:32:52 -0500355 entry.setIsBubble(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800356 entry.setShowInShadeWhenBubble(true);
357 }
358 }
359
360 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500361 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800362 if (!areBubblesEnabled(mContext)) {
363 return;
364 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800365 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
366 updateBubble(entry, true /* updatePosition */);
367 }
368 }
369
370 @Override
371 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800372 if (!areBubblesEnabled(mContext)) {
373 return;
374 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800375 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
376 && alertAgain(entry, entry.notification.getNotification())) {
377 entry.setShowInShadeWhenBubble(true);
378 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800379 updateBubble(entry, true /* updatePosition */);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800380 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800381 }
382 }
383
384 @Override
385 public void onEntryRemoved(NotificationEntry entry,
386 @Nullable NotificationVisibility visibility,
387 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800388 if (!areBubblesEnabled(mContext)) {
389 return;
390 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800391 entry.setShowInShadeWhenBubble(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800392 if (mStackView != null) {
393 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800394 }
395 if (!removedByUser) {
396 // This was a cancel so we should remove the bubble
Mark Renouf08bc42a2019-03-07 13:01:59 -0500397 removeBubble(entry.key, DISMISS_NOTIF_CANCEL);
Ned Burns01e38212019-01-03 16:32:52 -0500398 }
399 }
400 };
401
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800402 /**
403 * Lets any listeners know if bubble state has changed.
404 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800405 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800406 if (mStackView == null) {
407 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800408 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800409
Mady Mellord1c78b262018-11-06 18:04:40 -0800410 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800411 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800412 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800413 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
414 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
415 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800416 }
417
418 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800419 * Updates the visibility of the bubbles based on current state.
420 * Does not un-bubble, just hides or un-hides. Will notify any
421 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800422 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800423 public void updateVisibility() {
424 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
425 // Bubbles only appear in unlocked shade
426 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
427 } else if (mStackView != null) {
428 mStackView.setVisibility(INVISIBLE);
429 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800430 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800431 updateBubblesShowing();
432 }
433
434 /**
435 * Rect indicating the touchable region for the bubble stack / expanded stack.
436 */
437 public Rect getTouchableRegion() {
438 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
439 return null;
440 }
441 mStackView.getBoundsOnScreen(mTempRect);
442 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800443 }
444
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800445 @VisibleForTesting
446 BubbleStackView getStackView() {
447 return mStackView;
448 }
449
Mady Mellor5549dd22018-11-06 18:07:34 -0800450 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800451 * Whether the notification has been developer configured to bubble and is allowed by the user.
452 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800453 @VisibleForTesting
454 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800455 StatusBarNotification n = entry.notification;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800456 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
457 && n.getNotification().getBubbleMetadata().getIntent() != null;
Julia Reynolds4509ce72019-01-31 13:12:43 -0500458 return hasOverlayIntent && entry.canBubble;
Mady Mellorb4991e62019-01-10 15:14:51 -0800459 }
460
461 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800462 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800463 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800464 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800465 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800466 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800467 return false;
468 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800469 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800470
471 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
472 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
473 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
474
Mady Mellor5549dd22018-11-06 18:07:34 -0800475 boolean hasRemoteInput = false;
476 if (n.getNotification().actions != null) {
477 for (Notification.Action action : n.getNotification().actions) {
478 if (action.getRemoteInputs() != null) {
479 hasRemoteInput = true;
480 break;
481 }
482 }
483 }
Mady Mellor711f9562018-12-05 14:53:46 -0800484 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
485 && n.isOngoing();
486 boolean isMusic = n.getNotification().hasMediaSession();
487 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800488
Mady Mellor5549dd22018-11-06 18:07:34 -0800489 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800490 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
491 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800492 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800493 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800494 || autoBubbleAll;
495 }
496
Mark Renoufcecc77b2019-01-30 16:32:24 -0500497 /**
498 * This task stack listener is responsible for responding to tasks moved to the front
499 * which are on the default (main) display. When this happens, expanded bubbles must be
500 * collapsed so the user may interact with the app which was just moved to the front.
501 * <p>
502 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
503 * these calls via a main thread Handler.
504 */
505 @MainThread
506 private class BubbleTaskStackListener extends TaskStackChangeListener {
507
Mark Renoufcecc77b2019-01-30 16:32:24 -0500508 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500509 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
510 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500511 mStackView.collapseStack();
512 }
513 }
514
Mark Renoufcecc77b2019-01-30 16:32:24 -0500515 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500516 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500517 if (mStackView != null) {
518 mStackView.collapseStack();
519 }
520 }
521 }
522
Mady Mellorceced172018-11-27 11:18:39 -0800523 private static boolean shouldAutoBubbleMessages(Context context) {
524 return Settings.Secure.getInt(context.getContentResolver(),
525 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
526 }
527
528 private static boolean shouldAutoBubbleOngoing(Context context) {
529 return Settings.Secure.getInt(context.getContentResolver(),
530 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
531 }
532
533 private static boolean shouldAutoBubbleAll(Context context) {
534 return Settings.Secure.getInt(context.getContentResolver(),
535 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800536 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500537
Mady Mellor3dff9e62019-02-05 18:12:53 -0800538 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500539 return Settings.Secure.getInt(context.getContentResolver(),
540 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
541 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800542
543 private static boolean areBubblesEnabled(Context context) {
544 return Settings.Secure.getInt(context.getContentResolver(),
545 ENABLE_BUBBLES, 1) != 0;
546 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500547
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800548 /**
549 * Whether bubbles should be positioned at the top of the screen or not.
550 */
551 public static boolean showBubblesAtTop(Context context) {
552 return Settings.Secure.getInt(context.getContentResolver(),
553 ENABLE_BUBBLES_AT_TOP, 0) != 0;
554 }
555
556 /**
557 * Whether the bubble chrome should display as a footer or not (in which case it's a header).
558 */
559 public static boolean useFooter(Context context) {
560 return Settings.Secure.getInt(context.getContentResolver(),
561 ENABLE_BUBBLE_FOOTER, 0) != 0;
562 }
563
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500564 /** Default stiffness to use for bubble physics animations. */
565 public static int getBubbleStiffness(Context context, int defaultStiffness) {
566 return Settings.Secure.getInt(
567 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
568 }
569
570 /** Default bounciness/damping ratio to use for bubble physics animations. */
571 public static float getBubbleBounciness(Context context, float defaultBounciness) {
572 return Settings.Secure.getInt(
573 context.getContentResolver(),
574 BUBBLE_BOUNCINESS,
575 (int) (defaultBounciness * 100)) / 100f;
576 }
577
Joshua Tsujia19515f2019-02-13 18:02:29 -0500578 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
579 private class BubblesImeListener extends IPinnedStackListener.Stub {
580
581 @Override
582 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
583 }
584
585 @Override
586 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
587 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
588 int displayRotation) throws RemoteException {}
589
590 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500591 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
592 if (mStackView != null && mStackView.getBubbleCount() > 0) {
593 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500594 }
595 }
596
597 @Override
598 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
599 throws RemoteException {}
600
601 @Override
602 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
603
604 @Override
605 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
606 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800607}