blob: 05f4aef3b6c61e5b3027110e437c7da2c6413549 [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;
Mady Mellore80930e2019-03-21 16:00:45 -070029import android.app.ActivityManager;
Mark Renoufc808f062019-02-07 15:20:37 -050030import android.app.ActivityManager.RunningTaskInfo;
Mark Renoufcecc77b2019-01-30 16:32:24 -050031import android.app.ActivityTaskManager;
32import android.app.IActivityTaskManager;
Mady Mellorb4991e62019-01-10 15:14:51 -080033import android.app.INotificationManager;
Mady Mellor5549dd22018-11-06 18:07:34 -080034import android.app.Notification;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080035import android.content.Context;
Joshua Tsujia19515f2019-02-13 18:02:29 -050036import android.content.pm.ParceledListSlice;
Mady Mellord1c78b262018-11-06 18:04:40 -080037import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050038import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080039import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080040import android.provider.Settings;
Mady Mellor5549dd22018-11-06 18:07:34 -080041import android.service.notification.StatusBarNotification;
Mark Renoufcecc77b2019-01-30 16:32:24 -050042import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050043import android.view.IPinnedStackController;
44import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080045import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080046import android.widget.FrameLayout;
47
Mark Renouf08bc42a2019-03-07 13:01:59 -050048import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050049import androidx.annotation.MainThread;
50
Mady Mellorebdbbb92018-11-15 14:36:48 -080051import com.android.internal.annotations.VisibleForTesting;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080052import com.android.internal.statusbar.NotificationVisibility;
Ned Burns01e38212019-01-03 16:32:52 -050053import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080054import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050055import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050056import com.android.systemui.shared.system.ActivityManagerWrapper;
57import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050058import com.android.systemui.shared.system.WindowManagerWrapper;
Ned Burns01e38212019-01-03 16:32:52 -050059import com.android.systemui.statusbar.notification.NotificationEntryListener;
60import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080061import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050062import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050063import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080064import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070065import com.android.systemui.statusbar.policy.ConfigurationController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080066
Mark Renouf08bc42a2019-03-07 13:01:59 -050067import java.lang.annotation.Retention;
Mady Mellore80930e2019-03-21 16:00:45 -070068import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050069
Jason Monk27d01a622018-12-10 15:57:09 -050070import javax.inject.Inject;
71import javax.inject.Singleton;
72
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080073/**
74 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
75 * Bubbles can be expanded to show more content.
76 *
77 * The controller manages addition, removal, and visible state of bubbles on screen.
78 */
Jason Monk27d01a622018-12-10 15:57:09 -050079@Singleton
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070080public class BubbleController implements BubbleExpandedView.OnBubbleBlockedListener,
81 ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080082
83 private static final String TAG = "BubbleController";
84
Mark Renouf08bc42a2019-03-07 13:01:59 -050085 @Retention(SOURCE)
86 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
87 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION})
88 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070089
Mark Renouf08bc42a2019-03-07 13:01:59 -050090 static final int DISMISS_USER_GESTURE = 1;
91 static final int DISMISS_AGED = 2;
92 static final int DISMISS_TASK_FINISHED = 3;
93 static final int DISMISS_BLOCKED = 4;
94 static final int DISMISS_NOTIF_CANCEL = 5;
95 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
96
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -040097 static final int MAX_BUBBLES = 5; // TODO: actually enforce this
98
Mady Mellor5549dd22018-11-06 18:07:34 -080099 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -0500100 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -0800101
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800102 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800103 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800104 /** Auto bubble flags set whether different notif types should be presented as a bubble */
Mady Mellorceced172018-11-27 11:18:39 -0800105 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
106 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
107 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800108
109 /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500110 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -0800111
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;
Issei Suzukic0387542019-03-08 17:31:14 +0100121 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800122
Mady Mellor3dff9e62019-02-05 18:12:53 -0800123 private BubbleData mBubbleData;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800124 private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800125
126 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500127 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800128 private StatusBarStateListener mStatusBarStateListener;
129
130 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider =
131 Dependency.get(NotificationInterruptionStateProvider.class);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800132
Mady Mellorb4991e62019-01-10 15:14:51 -0800133 private INotificationManager mNotificationManagerService;
134
Mady Mellord1c78b262018-11-06 18:04:40 -0800135 // Used for determining view rect for touch interaction
136 private Rect mTempRect = new Rect();
137
Mady Mellor5549dd22018-11-06 18:07:34 -0800138 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800139 * Listener to be notified when some states of the bubbles change.
140 */
141 public interface BubbleStateChangeListener {
142 /**
143 * Called when the stack has bubbles or no longer has bubbles.
144 */
145 void onHasBubblesChanged(boolean hasBubbles);
146 }
147
Mady Mellorcd9b1302018-11-06 18:08:04 -0800148 /**
149 * Listener to find out about stack expansion / collapse events.
150 */
151 public interface BubbleExpandListener {
152 /**
153 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700154 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800155 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800156 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800157 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800158 void onBubbleExpandChanged(boolean isExpanding, String key);
159 }
160
161 /**
162 * Listens for the current state of the status bar and updates the visibility state
163 * of bubbles as needed.
164 */
165 private class StatusBarStateListener implements StatusBarStateController.StateListener {
166 private int mState;
167 /**
168 * Returns the current status bar state.
169 */
170 public int getCurrentState() {
171 return mState;
172 }
173
174 @Override
175 public void onStateChanged(int newState) {
176 mState = newState;
177 updateVisibility();
178 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800179 }
180
Jason Monk27d01a622018-12-10 15:57:09 -0500181 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800182 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700183 BubbleData data, ConfigurationController configurationController) {
184 this(context, statusBarWindowController, data, null /* synchronizer */,
185 configurationController);
Issei Suzukic0387542019-03-08 17:31:14 +0100186 }
187
188 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700189 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
190 ConfigurationController configurationController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800191 mContext = context;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700192 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800193
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800194 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500195 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800196
197 try {
198 mNotificationManagerService = INotificationManager.Stub.asInterface(
199 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
200 } catch (ServiceManager.ServiceNotFoundException e) {
201 e.printStackTrace();
202 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800203
204 mStatusBarWindowController = statusBarWindowController;
205 mStatusBarStateListener = new StatusBarStateListener();
206 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500207
208 mActivityTaskManager = ActivityTaskManager.getService();
209 mTaskStackListener = new BubbleTaskStackListener();
210 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800211
Joshua Tsujia19515f2019-02-13 18:02:29 -0500212 try {
213 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
214 } catch (RemoteException e) {
215 e.printStackTrace();
216 }
217
Mady Mellorcfd06c12019-02-13 14:32:12 -0800218 mBubbleData = data;
Mark Renouf3bc5b362019-04-05 14:37:59 -0400219 mBubbleData.setListener(mBubbleDataListener);
Issei Suzukic0387542019-03-08 17:31:14 +0100220 mSurfaceSynchronizer = synchronizer;
Mady Mellor5549dd22018-11-06 18:07:34 -0800221 }
222
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400223 /**
224 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
225 * method initializes the stack view and adds it to the StatusBar just above the scrim.
226 */
227 private void ensureStackViewCreated() {
228 if (mStackView == null) {
229 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
230 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
231 // TODO(b/130237686): When you expand the shade on top of expanded bubble, there is no
232 // scrim between bubble and the shade
233 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
234 sbv.addView(mStackView, bubblePosition,
235 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
236 if (mExpandListener != null) {
237 mStackView.setExpandListener(mExpandListener);
238 }
239 mStackView.setOnBlockedListener(this);
240 }
241 }
242
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700243 @Override
244 public void onUiModeChanged() {
245 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700246 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700247 }
248 }
249
250 @Override
251 public void onOverlayChanged() {
252 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700253 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700254 }
255 }
256
Mady Mellor5549dd22018-11-06 18:07:34 -0800257 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800258 * Set a listener to be notified when some states of the bubbles change.
259 */
260 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
261 mStateChangeListener = listener;
262 }
263
264 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800265 * Set a listener to be notified of bubble expand events.
266 */
267 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100268 mExpandListener = ((isExpanding, key) -> {
269 if (listener != null) {
270 listener.onBubbleExpandChanged(isExpanding, key);
271 }
272 mStatusBarWindowController.setBubbleExpanded(isExpanding);
273 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800274 if (mStackView != null) {
275 mStackView.setExpandListener(mExpandListener);
276 }
277 }
278
279 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800280 * Whether or not there are bubbles present, regardless of them being visible on the
281 * screen (e.g. if on AOD).
282 */
283 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800284 if (mStackView == null) {
285 return false;
286 }
287 for (Bubble bubble : mBubbleData.getBubbles()) {
288 if (!bubble.entry.isBubbleDismissed()) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800289 return true;
290 }
291 }
292 return false;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800293 }
294
295 /**
296 * Whether the stack of bubbles is expanded or not.
297 */
298 public boolean isStackExpanded() {
299 return mStackView != null && mStackView.isExpanded();
300 }
301
302 /**
303 * Tell the stack of bubbles to collapse.
304 */
305 public void collapseStack() {
306 if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800307 mStackView.collapseStack();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800308 }
309 }
310
311 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400312 * Request the stack expand if needed, then select the specified Bubble as current.
313 *
314 * @param notificationKey the notification key for the bubble to be selected
315 */
316 public void expandStackAndSelectBubble(String notificationKey) {
317 if (mStackView != null && mBubbleData.getBubble(notificationKey) != null) {
318 mStackView.setExpandedBubble(notificationKey);
319 }
320 }
321
322 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800323 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
324 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500325 void dismissStack(@DismissReason int reason) {
Mady Mellord1c78b262018-11-06 18:04:40 -0800326 if (mStackView == null) {
327 return;
328 }
Mark Renouf08bc42a2019-03-07 13:01:59 -0500329 mStackView.stackDismissed(reason);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800330
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800331 updateVisibility();
Ned Burns01e38212019-01-03 16:32:52 -0500332 mNotificationEntryManager.updateNotifications();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800333 }
334
335 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500336 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
337 * is forwarded a back key down/up pair.
338 */
339 public void performBackPressIfNeeded() {
340 if (mStackView != null) {
341 mStackView.performBackPressIfNeeded();
342 }
343 }
344
345 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800346 * Adds or updates a bubble associated with the provided notification entry.
347 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400348 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800349 */
Mark Renouff97ed462019-04-05 13:46:24 -0400350 void updateBubble(NotificationEntry notif) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800351 if (mStackView != null && mBubbleData.getBubble(notif.key) != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800352 // It's an update
Mark Renouff97ed462019-04-05 13:46:24 -0400353 mStackView.updateBubble(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800354 } else {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800355 // It's new
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400356 ensureStackViewCreated();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800357 mStackView.addBubble(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800358 }
Mady Mellore80930e2019-03-21 16:00:45 -0700359 if (shouldAutoExpand(notif)) {
360 mStackView.setExpandedBubble(notif);
361 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800362 updateVisibility();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800363 }
364
365 /**
366 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500367 * <p>
368 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800369 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500370 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500371 void removeBubble(String key, int reason) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800372 if (mStackView != null) {
Mark Renouf08bc42a2019-03-07 13:01:59 -0500373 mStackView.removeBubble(key, reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800374 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800375 mNotificationEntryManager.updateNotifications();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800376 updateVisibility();
Mady Mellord1c78b262018-11-06 18:04:40 -0800377 }
378
Mady Mellore8e07712019-01-23 12:45:33 -0800379 @Override
380 public void onBubbleBlocked(NotificationEntry entry) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800381 Object[] bubbles = mBubbleData.getBubbles().toArray();
Mady Mellore8e07712019-01-23 12:45:33 -0800382 for (int i = 0; i < bubbles.length; i++) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800383 NotificationEntry e = ((Bubble) bubbles[i]).entry;
Mady Mellore8e07712019-01-23 12:45:33 -0800384 boolean samePackage = entry.notification.getPackageName().equals(
385 e.notification.getPackageName());
386 if (samePackage) {
Mark Renouf08bc42a2019-03-07 13:01:59 -0500387 removeBubble(entry.key, DISMISS_BLOCKED);
Mady Mellore8e07712019-01-23 12:45:33 -0800388 }
389 }
390 }
391
Ned Burns01e38212019-01-03 16:32:52 -0500392 @SuppressWarnings("FieldCanBeLocal")
393 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
394 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500395 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800396 if (!areBubblesEnabled(mContext)) {
397 return;
398 }
399 if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700400 // TODO: handle group summaries?
401 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800402 }
403 }
404
405 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500406 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800407 if (!areBubblesEnabled(mContext)) {
408 return;
409 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800410 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400411 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800412 }
413 }
414
415 @Override
416 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800417 if (!areBubblesEnabled(mContext)) {
418 return;
419 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800420 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
421 && alertAgain(entry, entry.notification.getNotification())) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700422 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800423 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mark Renouff97ed462019-04-05 13:46:24 -0400424 updateBubble(entry);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800425 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800426 }
427 }
428
429 @Override
430 public void onEntryRemoved(NotificationEntry entry,
431 @Nullable NotificationVisibility visibility,
432 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800433 if (!areBubblesEnabled(mContext)) {
434 return;
435 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800436 entry.setShowInShadeWhenBubble(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800437 if (mStackView != null) {
438 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800439 }
440 if (!removedByUser) {
441 // This was a cancel so we should remove the bubble
Mark Renouf08bc42a2019-03-07 13:01:59 -0500442 removeBubble(entry.key, DISMISS_NOTIF_CANCEL);
Ned Burns01e38212019-01-03 16:32:52 -0500443 }
444 }
445 };
446
Mark Renouf3bc5b362019-04-05 14:37:59 -0400447 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
448 @Override
449 public void onBubbleAdded(Bubble bubble) {
450
451 }
452
453 @Override
454 public void onBubbleRemoved(Bubble bubble, int reason) {
455
456 }
457
458 public void onBubbleUpdated(Bubble bubble) {
459
460 }
461
462 @Override
463 public void onOrderChanged(List<Bubble> bubbles) {
464
465 }
466
467 @Override
468 public void onSelectionChanged(Bubble selectedBubble) {
469
470 }
471
472 @Override
473 public void onExpandedChanged(boolean expanded) {
474
475 }
476
477 @Override
478 public void showFlyoutText(Bubble bubble, String text) {
479
480 }
481
482 @Override
483 public void apply() {
484
485 }
486 };
487
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800488 /**
489 * Lets any listeners know if bubble state has changed.
490 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800491 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800492 if (mStackView == null) {
493 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800494 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800495
Mady Mellord1c78b262018-11-06 18:04:40 -0800496 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800497 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800498 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800499 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
500 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
501 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800502 }
503
504 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800505 * Updates the visibility of the bubbles based on current state.
506 * Does not un-bubble, just hides or un-hides. Will notify any
507 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800508 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800509 public void updateVisibility() {
510 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
511 // Bubbles only appear in unlocked shade
512 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
513 } else if (mStackView != null) {
514 mStackView.setVisibility(INVISIBLE);
515 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800516 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800517 updateBubblesShowing();
518 }
519
520 /**
521 * Rect indicating the touchable region for the bubble stack / expanded stack.
522 */
523 public Rect getTouchableRegion() {
524 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
525 return null;
526 }
527 mStackView.getBoundsOnScreen(mTempRect);
528 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800529 }
530
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800531 @VisibleForTesting
532 BubbleStackView getStackView() {
533 return mStackView;
534 }
535
Mady Mellor5549dd22018-11-06 18:07:34 -0800536 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800537 * Whether the notification has been developer configured to bubble and is allowed by the user.
538 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800539 @VisibleForTesting
540 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800541 StatusBarNotification n = entry.notification;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800542 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
543 && n.getNotification().getBubbleMetadata().getIntent() != null;
Julia Reynolds4509ce72019-01-31 13:12:43 -0500544 return hasOverlayIntent && entry.canBubble;
Mady Mellorb4991e62019-01-10 15:14:51 -0800545 }
546
547 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800548 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800549 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800550 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800551 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800552 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800553 return false;
554 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800555 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800556
557 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
558 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
559 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
560
Mady Mellor5549dd22018-11-06 18:07:34 -0800561 boolean hasRemoteInput = false;
562 if (n.getNotification().actions != null) {
563 for (Notification.Action action : n.getNotification().actions) {
564 if (action.getRemoteInputs() != null) {
565 hasRemoteInput = true;
566 break;
567 }
568 }
569 }
Mady Mellor711f9562018-12-05 14:53:46 -0800570 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
571 && n.isOngoing();
572 boolean isMusic = n.getNotification().hasMediaSession();
573 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800574
Mady Mellor5549dd22018-11-06 18:07:34 -0800575 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800576 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
577 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800578 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800579 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800580 || autoBubbleAll;
581 }
582
Mady Mellore80930e2019-03-21 16:00:45 -0700583 private boolean shouldAutoExpand(NotificationEntry entry) {
584 Notification.BubbleMetadata metadata = entry.getBubbleMetadata();
585 return metadata != null && metadata.getAutoExpandBubble()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400586 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellore80930e2019-03-21 16:00:45 -0700587 }
588
Mady Mellor8a1f0252019-04-01 11:31:34 -0700589 private void updateShowInShadeForSuppressNotification(NotificationEntry entry) {
590 boolean suppressNotification = entry.getBubbleMetadata() != null
591 && entry.getBubbleMetadata().getSuppressNotification()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400592 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellor8a1f0252019-04-01 11:31:34 -0700593 entry.setShowInShadeWhenBubble(!suppressNotification);
594 }
595
Mady Mellore80930e2019-03-21 16:00:45 -0700596 /**
597 * Return true if the applications with the package name is running in foreground.
598 *
Steven Wu8ba8ca92019-04-11 10:47:42 -0400599 * @param context application context.
Mady Mellore80930e2019-03-21 16:00:45 -0700600 * @param pkgName application package name.
601 */
Steven Wu8ba8ca92019-04-11 10:47:42 -0400602 public static boolean isForegroundApp(Context context, String pkgName) {
603 ActivityManager am = context.getSystemService(ActivityManager.class);
Mady Mellore80930e2019-03-21 16:00:45 -0700604 List<RunningTaskInfo> tasks = am.getRunningTasks(1 /* maxNum */);
605 return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
606 }
607
Mark Renoufcecc77b2019-01-30 16:32:24 -0500608 /**
609 * This task stack listener is responsible for responding to tasks moved to the front
610 * which are on the default (main) display. When this happens, expanded bubbles must be
611 * collapsed so the user may interact with the app which was just moved to the front.
612 * <p>
613 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
614 * these calls via a main thread Handler.
615 */
616 @MainThread
617 private class BubbleTaskStackListener extends TaskStackChangeListener {
618
Mark Renoufcecc77b2019-01-30 16:32:24 -0500619 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500620 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
621 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500622 mStackView.collapseStack();
623 }
624 }
625
Mark Renoufcecc77b2019-01-30 16:32:24 -0500626 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500627 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500628 if (mStackView != null) {
629 mStackView.collapseStack();
630 }
631 }
632 }
633
Mady Mellorceced172018-11-27 11:18:39 -0800634 private static boolean shouldAutoBubbleMessages(Context context) {
635 return Settings.Secure.getInt(context.getContentResolver(),
636 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
637 }
638
639 private static boolean shouldAutoBubbleOngoing(Context context) {
640 return Settings.Secure.getInt(context.getContentResolver(),
641 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
642 }
643
644 private static boolean shouldAutoBubbleAll(Context context) {
645 return Settings.Secure.getInt(context.getContentResolver(),
646 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800647 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500648
Mady Mellor3dff9e62019-02-05 18:12:53 -0800649 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500650 return Settings.Secure.getInt(context.getContentResolver(),
651 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
652 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800653
654 private static boolean areBubblesEnabled(Context context) {
655 return Settings.Secure.getInt(context.getContentResolver(),
656 ENABLE_BUBBLES, 1) != 0;
657 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500658
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500659 /** Default stiffness to use for bubble physics animations. */
660 public static int getBubbleStiffness(Context context, int defaultStiffness) {
661 return Settings.Secure.getInt(
662 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
663 }
664
665 /** Default bounciness/damping ratio to use for bubble physics animations. */
666 public static float getBubbleBounciness(Context context, float defaultBounciness) {
667 return Settings.Secure.getInt(
668 context.getContentResolver(),
669 BUBBLE_BOUNCINESS,
670 (int) (defaultBounciness * 100)) / 100f;
671 }
672
Joshua Tsujia19515f2019-02-13 18:02:29 -0500673 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
674 private class BubblesImeListener extends IPinnedStackListener.Stub {
675
676 @Override
677 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
678 }
679
680 @Override
681 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
682 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
683 int displayRotation) throws RemoteException {}
684
685 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500686 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
687 if (mStackView != null && mStackView.getBubbleCount() > 0) {
688 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500689 }
690 }
691
692 @Override
693 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
694 throws RemoteException {}
695
696 @Override
697 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
698
699 @Override
700 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
701 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800702}