blob: 762e5f21cd63cd948366e63ffa1c977e496ce5f7 [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 Mellor3a0a1b42019-05-23 06:40:21 -070019import static android.app.Notification.FLAG_BUBBLE;
Mady Mellorc2ff0112019-03-28 14:18:06 -070020import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL;
21import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL;
22import static android.service.notification.NotificationListenerService.REASON_CANCEL;
23import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -070024import static android.service.notification.NotificationListenerService.REASON_CLICK;
25import static android.service.notification.NotificationListenerService.REASON_GROUP_SUMMARY_CANCELED;
Mady Mellor390bff42019-04-05 15:09:01 -070026import static android.view.Display.DEFAULT_DISPLAY;
27import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080028import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080029import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080030import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080031
Issei Suzukia8d07312019-06-07 12:56:19 +020032import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_CONTROLLER;
Mady Mellorff076eb2019-11-13 10:12:06 -080033import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_EXPERIMENTS;
Issei Suzukia8d07312019-06-07 12:56:19 +020034import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
35import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080036import static com.android.systemui.statusbar.StatusBarState.SHADE;
Mady Mellor1a4e86f2019-05-03 16:07:23 -070037import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080038
Mark Renoufba5ab512019-05-02 15:21:01 -040039import static java.lang.annotation.ElementType.FIELD;
40import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
41import static java.lang.annotation.ElementType.PARAMETER;
Mark Renouf08bc42a2019-03-07 13:01:59 -050042import static java.lang.annotation.RetentionPolicy.SOURCE;
43
Mark Renoufc19b4732019-06-26 12:08:33 -040044import android.annotation.UserIdInt;
Mark Renoufc808f062019-02-07 15:20:37 -050045import android.app.ActivityManager.RunningTaskInfo;
Mady Mellor66efd5e2019-05-15 13:38:11 -070046import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070047import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080048import android.content.Context;
Mady Mellorca0c24c2019-05-16 16:14:32 -070049import android.content.pm.ActivityInfo;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080050import android.content.pm.PackageManager;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040051import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080052import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050053import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080054import android.os.ServiceManager;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040055import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040056import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040057import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040058import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040059import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040060import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050061import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080062import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080063import android.widget.FrameLayout;
64
Mark Renouf08bc42a2019-03-07 13:01:59 -050065import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050066import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040067import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050068
Mady Mellorebdbbb92018-11-15 14:36:48 -080069import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070070import com.android.internal.statusbar.IStatusBarService;
Beverlya53fb0d2020-01-29 15:26:13 -050071import com.android.internal.statusbar.NotificationVisibility;
72import com.android.systemui.DumpController;
73import com.android.systemui.Dumpable;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080074import com.android.systemui.R;
Beverlya53fb0d2020-01-29 15:26:13 -050075import com.android.systemui.bubbles.BubbleController.BubbleExpandListener;
76import com.android.systemui.bubbles.BubbleController.BubbleStateChangeListener;
77import com.android.systemui.bubbles.BubbleController.NotifCallback;
Beverly8fdb5332019-02-04 14:29:49 -050078import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050079import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000080import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050081import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050082import com.android.systemui.shared.system.WindowManagerWrapper;
Beverlya53fb0d2020-01-29 15:26:13 -050083import com.android.systemui.statusbar.FeatureFlags;
Mark Renoufc19b4732019-06-26 12:08:33 -040084import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070085import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050086import com.android.systemui.statusbar.notification.NotificationEntryListener;
87import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080088import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Beverlya53fb0d2020-01-29 15:26:13 -050089import com.android.systemui.statusbar.notification.collection.NotifCollection;
90import com.android.systemui.statusbar.notification.collection.NotifPipeline;
Ned Burnsf81c4c42019-01-07 14:10:43 -050091import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Beverlya53fb0d2020-01-29 15:26:13 -050092import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener;
Mady Mellor22f2f072019-04-18 13:26:18 -070093import com.android.systemui.statusbar.phone.NotificationGroupManager;
wilsonshihe8321942019-10-18 18:39:46 +080094import com.android.systemui.statusbar.phone.NotificationShadeWindowController;
Mady Mellor7f234902019-10-20 12:06:29 -070095import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080096import com.android.systemui.statusbar.phone.StatusBar;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070097import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040098import com.android.systemui.statusbar.policy.ZenModeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080099
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700100import java.io.FileDescriptor;
101import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500102import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -0400103import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -0700104import java.util.ArrayList;
Mady Mellorff076eb2019-11-13 10:12:06 -0800105import java.util.HashSet;
Lyn Hanb58c7562020-01-07 14:29:20 -0800106import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500107
Jason Monk27d01a622018-12-10 15:57:09 -0500108import javax.inject.Inject;
109import javax.inject.Singleton;
110
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800111/**
112 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
113 * Bubbles can be expanded to show more content.
114 *
115 * The controller manages addition, removal, and visible state of bubbles on screen.
116 */
Jason Monk27d01a622018-12-10 15:57:09 -0500117@Singleton
Beverlya53fb0d2020-01-29 15:26:13 -0500118public class BubbleController implements ConfigurationController.ConfigurationListener, Dumpable {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800119
Issei Suzukia8d07312019-06-07 12:56:19 +0200120 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800121
Mark Renouf08bc42a2019-03-07 13:01:59 -0500122 @Retention(SOURCE)
123 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400124 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700125 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400126 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500127 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700128
Mark Renouf08bc42a2019-03-07 13:01:59 -0500129 static final int DISMISS_USER_GESTURE = 1;
130 static final int DISMISS_AGED = 2;
131 static final int DISMISS_TASK_FINISHED = 3;
132 static final int DISMISS_BLOCKED = 4;
133 static final int DISMISS_NOTIF_CANCEL = 5;
134 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700135 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400136 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700137 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700138 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500139
Ned Burns01e38212019-01-03 16:32:52 -0500140 private final Context mContext;
141 private final NotificationEntryManager mNotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500142 private final NotifPipeline mNotifPipeline;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500143 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800144 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800145 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100146 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700147 private final NotificationGroupManager mNotificationGroupManager;
Heemin Seogba6337f2019-12-10 15:34:37 -0800148 private final ShadeController mShadeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800149
Mady Mellor3dff9e62019-02-05 18:12:53 -0800150 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400151 @Nullable private BubbleStackView mStackView;
Mady Mellor247ca2c2019-12-02 16:18:59 -0800152 private BubbleIconFactory mBubbleIconFactory;
Lyn Hanb58c7562020-01-07 14:29:20 -0800153 private int mMaxBubbles;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800154
Mark Renoufc19b4732019-06-26 12:08:33 -0400155 // Tracks the id of the current (foreground) user.
156 private int mCurrentUserId;
157 // Saves notification keys of active bubbles when users are switched.
158 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
159
Mady Mellorff076eb2019-11-13 10:12:06 -0800160 // Saves notification keys of user created "fake" bubbles so that we can allow notifications
161 // like these to bubble by default. Doesn't persist across reboots, not a long-term solution.
162 private final HashSet<String> mUserCreatedBubbles;
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800163 // If we're auto-bubbling bubbles via a whitelist, we need to track which notifs from that app
164 // have been "demoted" back to a notification so that we don't auto-bubbles those again.
165 // Doesn't persist across reboots, not a long-term solution.
166 private final HashSet<String> mUserBlockedBubbles;
Mady Mellorff076eb2019-11-13 10:12:06 -0800167
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800168 // Bubbles get added to the status bar view
wilsonshihe8321942019-10-18 18:39:46 +0800169 private final NotificationShadeWindowController mNotificationShadeWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400170 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800171 private StatusBarStateListener mStatusBarStateListener;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500172
Lyn Hanb58c7562020-01-07 14:29:20 -0800173 // Callback that updates BubbleOverflowActivity on data change.
174 @Nullable private Runnable mOverflowCallback = null;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800175
Mady Melloraa8fef22019-04-11 13:36:40 -0700176 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700177 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800178
Mady Mellord1c78b262018-11-06 18:04:40 -0800179 // Used for determining view rect for touch interaction
180 private Rect mTempRect = new Rect();
181
Mark Renoufc19b4732019-06-26 12:08:33 -0400182 // Listens to user switch so bubbles can be saved and restored.
183 private final NotificationLockscreenUserManager mNotifUserManager;
184
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400185 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
186 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
187
Mady Mellor3df7ab02019-12-09 15:07:10 -0800188 private boolean mInflateSynchronously;
189
Beverlyed8aea22020-01-22 16:52:47 -0500190 // TODO (b/145659174): allow for multiple callbacks to support the "shadow" new notif pipeline
191 private final List<NotifCallback> mCallbacks = new ArrayList<>();
192
Mady Mellor5549dd22018-11-06 18:07:34 -0800193 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800194 * Listener to be notified when some states of the bubbles change.
195 */
196 public interface BubbleStateChangeListener {
197 /**
198 * Called when the stack has bubbles or no longer has bubbles.
199 */
200 void onHasBubblesChanged(boolean hasBubbles);
201 }
202
Mady Mellorcd9b1302018-11-06 18:08:04 -0800203 /**
204 * Listener to find out about stack expansion / collapse events.
205 */
206 public interface BubbleExpandListener {
207 /**
208 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700209 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800210 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800211 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800212 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800213 void onBubbleExpandChanged(boolean isExpanding, String key);
214 }
215
216 /**
Mady Mellorf44b6832020-01-14 13:26:14 -0800217 * Listener to be notified when a bubbles' notification suppression state changes.
218 */
219 public interface NotificationSuppressionChangedListener {
220 /**
221 * Called when the notification suppression state of a bubble changes.
222 */
223 void onBubbleNotificationSuppressionChange(Bubble bubble);
Beverlyed8aea22020-01-22 16:52:47 -0500224 }
Mady Mellorf44b6832020-01-14 13:26:14 -0800225
Beverlyed8aea22020-01-22 16:52:47 -0500226 /**
227 * Callback for when the BubbleController wants to interact with the notification pipeline to:
228 * - Remove a previously bubbled notification
229 * - Update the notification shade since bubbled notification should/shouldn't be showing
230 */
231 public interface NotifCallback {
232 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500233 * Called when a bubbled notification that was hidden from the shade is now being removed
234 * This can happen when an app cancels a bubbled notification or when the user dismisses a
235 * bubble.
Beverlyed8aea22020-01-22 16:52:47 -0500236 */
Beverlya53fb0d2020-01-29 15:26:13 -0500237 void removeNotification(NotificationEntry entry, int reason);
Beverlyed8aea22020-01-22 16:52:47 -0500238
239 /**
240 * Called when a bubbled notification has changed whether it should be
241 * filtered from the shade.
242 */
Beverlya53fb0d2020-01-29 15:26:13 -0500243 void invalidateNotifications(String reason);
Beverlyed8aea22020-01-22 16:52:47 -0500244
245 /**
246 * Called on a bubbled entry that has been removed when there are no longer
247 * bubbled entries in its group.
248 *
249 * Checks whether its group has any other (non-bubbled) children. If it doesn't,
250 * removes all remnants of the group's summary from the notification pipeline.
251 * TODO: (b/145659174) Only old pipeline needs this - delete post-migration.
252 */
253 void maybeCancelSummary(NotificationEntry entry);
Mady Mellorf44b6832020-01-14 13:26:14 -0800254 }
255
256 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800257 * Listens for the current state of the status bar and updates the visibility state
258 * of bubbles as needed.
259 */
260 private class StatusBarStateListener implements StatusBarStateController.StateListener {
261 private int mState;
262 /**
263 * Returns the current status bar state.
264 */
265 public int getCurrentState() {
266 return mState;
267 }
268
269 @Override
270 public void onStateChanged(int newState) {
271 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400272 boolean shouldCollapse = (mState != SHADE);
273 if (shouldCollapse) {
274 collapseStack();
275 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700276 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800277 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800278 }
279
Jason Monk27d01a622018-12-10 15:57:09 -0500280 @Inject
Mady Mellor7f234902019-10-20 12:06:29 -0700281 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800282 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700283 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800284 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700285 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700286 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400287 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400288 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700289 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700290 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500291 NotificationEntryManager entryManager,
292 NotifPipeline notifPipeline,
293 FeatureFlags featureFlags,
294 DumpController dumpController) {
wilsonshihe8321942019-10-18 18:39:46 +0800295 this(context, notificationShadeWindowController, statusBarStateController, shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700296 data, null /* synchronizer */, configurationController, interruptionStateProvider,
Beverlya53fb0d2020-01-29 15:26:13 -0500297 zenModeController, notifUserManager, groupManager, entryManager,
298 notifPipeline, featureFlags, dumpController);
Mady Mellor7f234902019-10-20 12:06:29 -0700299 }
300
301 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800302 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700303 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800304 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700305 BubbleData data,
306 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
307 ConfigurationController configurationController,
308 NotificationInterruptionStateProvider interruptionStateProvider,
309 ZenModeController zenModeController,
310 NotificationLockscreenUserManager notifUserManager,
311 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500312 NotificationEntryManager entryManager,
313 NotifPipeline notifPipeline,
314 FeatureFlags featureFlags,
315 DumpController dumpController) {
316 dumpController.registerDumpable(TAG, this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800317 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800318 mShadeController = shadeController;
Mady Melloraa8fef22019-04-11 13:36:40 -0700319 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400320 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400321 mZenModeController = zenModeController;
322 mZenModeController.addCallback(new ZenModeController.Callback() {
323 @Override
324 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800325 for (Bubble b : mBubbleData.getBubbles()) {
326 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700327 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400328 }
329
330 @Override
331 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800332 for (Bubble b : mBubbleData.getBubbles()) {
333 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700334 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400335 }
336 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700337
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700338 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800339
Mady Mellorf44b6832020-01-14 13:26:14 -0800340 mMaxBubbles = mContext.getResources().getInteger(R.integer.bubbles_max_rendered);
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400341 mBubbleData = data;
342 mBubbleData.setListener(mBubbleDataListener);
Mady Mellorf44b6832020-01-14 13:26:14 -0800343 mBubbleData.setSuppressionChangedListener(new NotificationSuppressionChangedListener() {
344 @Override
345 public void onBubbleNotificationSuppressionChange(Bubble bubble) {
346 // Make sure NoMan knows it's not showing in the shade anymore so anyone querying it
347 // can tell.
348 try {
349 mBarService.onBubbleNotificationSuppressionChanged(bubble.getKey(),
350 !bubble.showInShade());
351 } catch (RemoteException e) {
352 // Bad things have happened
353 }
354 }
355 });
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400356
Mady Mellor7f234902019-10-20 12:06:29 -0700357 mNotificationEntryManager = entryManager;
Mady Mellor22f2f072019-04-18 13:26:18 -0700358 mNotificationGroupManager = groupManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500359 mNotifPipeline = notifPipeline;
360
361 if (!featureFlags.isNewNotifPipelineRenderingEnabled()) {
362 setupNEM();
363 } else {
364 setupNotifPipeline();
365 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800366
wilsonshihe8321942019-10-18 18:39:46 +0800367 mNotificationShadeWindowController = notificationShadeWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800368 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700369 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500370
Mark Renoufcecc77b2019-01-30 16:32:24 -0500371 mTaskStackListener = new BubbleTaskStackListener();
372 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800373
Joshua Tsujia19515f2019-02-13 18:02:29 -0500374 try {
375 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
376 } catch (RemoteException e) {
377 e.printStackTrace();
378 }
Issei Suzukic0387542019-03-08 17:31:14 +0100379 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700380
381 mBarService = IStatusBarService.Stub.asInterface(
382 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400383
384 mSavedBubbleKeysPerUser = new SparseSetArray<>();
385 mCurrentUserId = mNotifUserManager.getCurrentUserId();
386 mNotifUserManager.addUserChangedListener(
Steve Elliottb47f1c72019-12-19 12:39:26 -0500387 new NotificationLockscreenUserManager.UserChangedListener() {
388 @Override
389 public void onUserChanged(int newUserId) {
390 BubbleController.this.saveBubbles(mCurrentUserId);
391 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
392 BubbleController.this.restoreBubbles(newUserId);
393 mCurrentUserId = newUserId;
394 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400395 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800396
397 mUserCreatedBubbles = new HashSet<>();
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800398 mUserBlockedBubbles = new HashSet<>();
Aran Inkaa4dfa72019-11-18 16:49:07 -0500399
Mady Mellor247ca2c2019-12-02 16:18:59 -0800400 mBubbleIconFactory = new BubbleIconFactory(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800401 }
402
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400403 /**
Beverlyed8aea22020-01-22 16:52:47 -0500404 * See {@link NotifCallback}.
405 */
406 public void addNotifCallback(NotifCallback callback) {
407 mCallbacks.add(callback);
408 }
409
410 private void setupNEM() {
411 mNotificationEntryManager.addNotificationEntryListener(
412 new NotificationEntryListener() {
413 @Override
Mady Mellorf9439ab2020-01-30 16:06:53 -0800414 public void onPendingEntryAdded(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -0500415 onEntryAdded(entry);
416 }
417
418 @Override
419 public void onPreEntryUpdated(NotificationEntry entry) {
420 onEntryUpdated(entry);
421 }
422
423 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500424 public void onEntryRemoved(
425 NotificationEntry entry,
426 @android.annotation.Nullable NotificationVisibility visibility,
427 boolean removedByUser) {
428 BubbleController.this.onEntryRemoved(entry);
429 }
430
431 @Override
Beverlyed8aea22020-01-22 16:52:47 -0500432 public void onNotificationRankingUpdated(RankingMap rankingMap) {
433 onRankingUpdated(rankingMap);
434 }
435 });
436
Evan Laird04373662020-01-24 17:37:39 -0500437 mNotificationEntryManager.addNotificationRemoveInterceptor(
Beverlyed8aea22020-01-22 16:52:47 -0500438 new NotificationRemoveInterceptor() {
439 @Override
Evan Laird04373662020-01-24 17:37:39 -0500440 public boolean onNotificationRemoveRequested(
Beverlya53fb0d2020-01-29 15:26:13 -0500441 String key,
442 NotificationEntry entry,
443 int dismissReason) {
444 final boolean isClearAll = dismissReason == REASON_CANCEL_ALL;
445 final boolean isUserDimiss = dismissReason == REASON_CANCEL
446 || dismissReason == REASON_CLICK;
447 final boolean isAppCancel = dismissReason == REASON_APP_CANCEL
448 || dismissReason == REASON_APP_CANCEL_ALL;
449 final boolean isSummaryCancel =
450 dismissReason == REASON_GROUP_SUMMARY_CANCELED;
451
452 // Need to check for !appCancel here because the notification may have
453 // previously been dismissed & entry.isRowDismissed would still be true
454 boolean userRemovedNotif =
455 (entry != null && entry.isRowDismissed() && !isAppCancel)
456 || isClearAll || isUserDimiss || isSummaryCancel;
457
458 if (userRemovedNotif || isUserCreatedBubble(key)
459 || isSummaryOfUserCreatedBubble(entry)) {
460 return handleDismissalInterception(entry);
461 }
462
463 return false;
Beverlyed8aea22020-01-22 16:52:47 -0500464 }
465 });
466
467 mNotificationGroupManager.addOnGroupChangeListener(
468 new NotificationGroupManager.OnGroupChangeListener() {
469 @Override
470 public void onGroupSuppressionChanged(
471 NotificationGroupManager.NotificationGroup group,
472 boolean suppressed) {
473 // More notifications could be added causing summary to no longer
474 // be suppressed -- in this case need to remove the key.
475 final String groupKey = group.summary != null
476 ? group.summary.getSbn().getGroupKey()
477 : null;
478 if (!suppressed && groupKey != null
479 && mBubbleData.isSummarySuppressed(groupKey)) {
480 mBubbleData.removeSuppressedSummary(groupKey);
481 }
482 }
483 });
484
485 addNotifCallback(new NotifCallback() {
486 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500487 public void removeNotification(NotificationEntry entry, int reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500488 mNotificationEntryManager.performRemoveNotification(entry.getSbn(),
Beverlya53fb0d2020-01-29 15:26:13 -0500489 reason);
Beverlyed8aea22020-01-22 16:52:47 -0500490 }
491
492 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500493 public void invalidateNotifications(String reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500494 mNotificationEntryManager.updateNotifications(reason);
495 }
496
497 @Override
498 public void maybeCancelSummary(NotificationEntry entry) {
499 // Check if removed bubble has an associated suppressed group summary that needs
500 // to be removed now.
Beverlya53fb0d2020-01-29 15:26:13 -0500501 final String groupKey = entry.getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500502 if (mBubbleData.isSummarySuppressed(groupKey)) {
Beverlya53fb0d2020-01-29 15:26:13 -0500503 mBubbleData.removeSuppressedSummary(groupKey);
Beverlyed8aea22020-01-22 16:52:47 -0500504
505 final NotificationEntry summary =
506 mNotificationEntryManager.getActiveNotificationUnfiltered(
507 mBubbleData.getSummaryKey(groupKey));
Beverlya53fb0d2020-01-29 15:26:13 -0500508 if (summary != null) {
509 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
510 UNDEFINED_DISMISS_REASON);
511 }
Beverlyed8aea22020-01-22 16:52:47 -0500512 }
513
Beverlya53fb0d2020-01-29 15:26:13 -0500514 // Check if we still need to remove the summary from NoManGroup because the summary
515 // may not be in the mBubbleData.mSuppressedGroupKeys list and removed above.
516 // For example:
517 // 1. Bubbled notifications (group) is posted to shade and are visible bubbles
518 // 2. User expands bubbles so now their respective notifications in the shade are
519 // hidden, including the group summary
520 // 3. User removes all bubbles
521 // 4. We expect all the removed bubbles AND the summary (note: the summary was
522 // never added to the suppressedSummary list in BubbleData, so we add this check)
Beverlyed8aea22020-01-22 16:52:47 -0500523 NotificationEntry summary =
524 mNotificationGroupManager.getLogicalGroupSummary(entry.getSbn());
525 if (summary != null) {
526 ArrayList<NotificationEntry> summaryChildren =
527 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
528 boolean isSummaryThisNotif = summary.getKey().equals(entry.getKey());
529 if (!isSummaryThisNotif && (summaryChildren == null
530 || summaryChildren.isEmpty())) {
531 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
532 UNDEFINED_DISMISS_REASON);
533 }
534 }
535 }
536 });
537 }
538
Beverlya53fb0d2020-01-29 15:26:13 -0500539 private void setupNotifPipeline() {
540 mNotifPipeline.addCollectionListener(new NotifCollectionListener() {
541 @Override
542 public void onEntryAdded(NotificationEntry entry) {
543 BubbleController.this.onEntryAdded(entry);
544 }
545
546 @Override
547 public void onEntryUpdated(NotificationEntry entry) {
548 BubbleController.this.onEntryUpdated(entry);
549 }
550
551 @Override
552 public void onRankingUpdate(RankingMap rankingMap) {
553 onRankingUpdated(rankingMap);
554 }
555
556 @Override
557 public void onEntryRemoved(NotificationEntry entry,
558 @NotifCollection.CancellationReason int reason) {
559 BubbleController.this.onEntryRemoved(entry);
560 }
561 });
562 }
563
Beverlyed8aea22020-01-22 16:52:47 -0500564 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800565 * Sets whether to perform inflation on the same thread as the caller. This method should only
566 * be used in tests, not in production.
567 */
568 @VisibleForTesting
569 void setInflateSynchronously(boolean inflateSynchronously) {
570 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800571 }
572
Lyn Hanb58c7562020-01-07 14:29:20 -0800573 void setOverflowCallback(Runnable updateOverflow) {
574 mOverflowCallback = updateOverflow;
575 }
576
577 /**
578 * @return Bubbles for updating overflow.
579 */
580 List<Bubble> getOverflowBubbles() {
581 return mBubbleData.getOverflowBubbles();
582 }
583
584
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400585 /**
586 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
587 * method initializes the stack view and adds it to the StatusBar just above the scrim.
588 */
589 private void ensureStackViewCreated() {
590 if (mStackView == null) {
591 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
wilsonshihe8321942019-10-18 18:39:46 +0800592 ViewGroup nsv = mNotificationShadeWindowController.getNotificationShadeView();
593 int bubbleScrimIndex = nsv.indexOfChild(nsv.findViewById(R.id.scrim_for_bubble));
Lyn Hanbde48202019-05-29 19:18:29 -0700594 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
wilsonshihe8321942019-10-18 18:39:46 +0800595 nsv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400596 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
597 if (mExpandListener != null) {
598 mStackView.setExpandListener(mExpandListener);
599 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400600 }
601 }
602
Mark Renoufc19b4732019-06-26 12:08:33 -0400603 /**
604 * Records the notification key for any active bubbles. These are used to restore active
605 * bubbles when the user returns to the foreground.
606 *
607 * @param userId the id of the user
608 */
609 private void saveBubbles(@UserIdInt int userId) {
610 // First clear any existing keys that might be stored.
611 mSavedBubbleKeysPerUser.remove(userId);
612 // Add in all active bubbles for the current user.
613 for (Bubble bubble: mBubbleData.getBubbles()) {
614 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
615 }
616 }
617
618 /**
619 * Promotes existing notifications to Bubbles if they were previously bubbles.
620 *
621 * @param userId the id of the user
622 */
623 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400624 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
625 if (savedBubbleKeys == null) {
626 // There were no bubbles saved for this used.
627 return;
628 }
Evan Laird181de622019-10-24 09:53:02 -0400629 for (NotificationEntry e :
630 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400631 if (savedBubbleKeys.contains(e.getKey())
Mark Renoufc19b4732019-06-26 12:08:33 -0400632 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
633 && canLaunchInActivityView(mContext, e)) {
634 updateBubble(e, /* suppressFlyout= */ true);
635 }
636 }
637 // Finally, remove the entries for this user now that bubbles are restored.
638 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
639 }
640
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700641 @Override
642 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800643 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700644 }
645
646 @Override
647 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800648 updateForThemeChanges();
649 }
650
651 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700652 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700653 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700654 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800655 mBubbleIconFactory = new BubbleIconFactory(mContext);
656 for (Bubble b: mBubbleData.getBubbles()) {
657 // Reload each bubble
658 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
659 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700660 }
661
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400662 @Override
663 public void onConfigChanged(Configuration newConfig) {
664 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400665 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700666 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400667 }
668 }
669
Mady Mellor5549dd22018-11-06 18:07:34 -0800670 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800671 * Set a listener to be notified when some states of the bubbles change.
672 */
673 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
674 mStateChangeListener = listener;
675 }
676
677 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800678 * Set a listener to be notified of bubble expand events.
679 */
680 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100681 mExpandListener = ((isExpanding, key) -> {
682 if (listener != null) {
683 listener.onBubbleExpandChanged(isExpanding, key);
684 }
wilsonshihe8321942019-10-18 18:39:46 +0800685 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100686 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800687 if (mStackView != null) {
688 mStackView.setExpandListener(mExpandListener);
689 }
690 }
691
692 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800693 * Whether or not there are bubbles present, regardless of them being visible on the
694 * screen (e.g. if on AOD).
695 */
696 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800697 if (mStackView == null) {
698 return false;
699 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400700 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800701 }
702
703 /**
704 * Whether the stack of bubbles is expanded or not.
705 */
706 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400707 return mBubbleData.isExpanded();
708 }
709
710 /**
711 * Tell the stack of bubbles to expand.
712 */
713 public void expandStack() {
714 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800715 }
716
717 /**
718 * Tell the stack of bubbles to collapse.
719 */
720 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400721 mBubbleData.setExpanded(false /* expanded */);
722 }
723
Mady Mellorce23c462019-06-17 17:30:07 -0700724 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700725 * True if either:
726 * (1) There is a bubble associated with the provided key and if its notification is hidden
727 * from the shade.
728 * (2) There is a group summary associated with the provided key that is hidden from the shade
729 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700730 *
Mady Mellore28fe102019-07-09 15:33:32 -0700731 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700732 */
Beverlyed8aea22020-01-22 16:52:47 -0500733 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
734 String key = entry.getKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700735 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800736 && !mBubbleData.getBubbleWithKey(key).showInShade();
Beverlyed8aea22020-01-22 16:52:47 -0500737
738 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700739 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700740 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Beverlyed8aea22020-01-22 16:52:47 -0500741
Mady Mellore4348272019-07-29 17:43:36 -0700742 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700743 }
744
Mark Renouf71a3af62019-04-08 15:02:54 -0400745 @VisibleForTesting
746 void selectBubble(String key) {
747 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellor247ca2c2019-12-02 16:18:59 -0800748 mBubbleData.setSelectedBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800749 }
750
Lyn Hanb58c7562020-01-07 14:29:20 -0800751 void promoteBubbleFromOverflow(Bubble bubble) {
Lyn Han1e19d7f2020-02-05 19:10:58 -0800752 bubble.setInflateSynchronously(mInflateSynchronously);
753 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
Lyn Hanb58c7562020-01-07 14:29:20 -0800754 }
755
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800756 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400757 * Request the stack expand if needed, then select the specified Bubble as current.
758 *
759 * @param notificationKey the notification key for the bubble to be selected
760 */
761 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400762 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
763 if (bubble != null) {
764 mBubbleData.setSelectedBubble(bubble);
765 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400766 }
767 }
768
769 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800770 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
771 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500772 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400773 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800774 }
775
776 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500777 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
778 * is forwarded a back key down/up pair.
779 */
780 public void performBackPressIfNeeded() {
781 if (mStackView != null) {
782 mStackView.performBackPressIfNeeded();
783 }
784 }
785
786 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800787 * Adds or updates a bubble associated with the provided notification entry.
788 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400789 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800790 */
Mark Renouff97ed462019-04-05 13:46:24 -0400791 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700792 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400793 }
794
795 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700796 updateBubble(notif, suppressFlyout, true /* showInShade */);
797 }
798
799 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800800 if (mStackView == null) {
801 // Lazy init stack view when a bubble is created
802 ensureStackViewCreated();
803 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700804 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400805 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700806 notif.setInterruption();
807 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800808 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
809 bubble.setInflateSynchronously(mInflateSynchronously);
810 bubble.inflate(
811 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
812 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700813 }
814
815 /**
816 * Called when a user has indicated that an active notification should be shown as a bubble.
817 * <p>
818 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
819 * the notification from appearing in the shade.
820 *
821 * @param entry the notification to show as a bubble.
822 */
823 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800824 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
825 Log.d(TAG, "onUserCreatedBubble: " + entry.getKey());
826 }
Heemin Seogba6337f2019-12-10 15:34:37 -0800827 mShadeController.collapsePanel(true);
Mady Mellor7f234902019-10-20 12:06:29 -0700828 entry.setFlagBubble(true);
829 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
Mady Mellorff076eb2019-11-13 10:12:06 -0800830 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800831 mUserBlockedBubbles.remove(entry.getKey());
Mady Mellor7f234902019-10-20 12:06:29 -0700832 }
833
834 /**
835 * Called when a user has indicated that an active notification appearing as a bubble should
836 * no longer be shown as a bubble.
837 *
838 * @param entry the notification to no longer show as a bubble.
839 */
840 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800841 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
842 Log.d(TAG, "onUserDemotedBubble: " + entry.getKey());
843 }
Mady Mellor7f234902019-10-20 12:06:29 -0700844 entry.setFlagBubble(false);
Beverlya53fb0d2020-01-29 15:26:13 -0500845 removeBubble(entry, DISMISS_BLOCKED);
Mady Mellorff076eb2019-11-13 10:12:06 -0800846 mUserCreatedBubbles.remove(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800847 if (BubbleExperimentConfig.isPackageWhitelistedToAutoBubble(
848 mContext, entry.getSbn().getPackageName())) {
849 // This package is whitelist but user demoted the bubble, let's save it so we don't
850 // auto-bubble for the whitelist again.
851 mUserBlockedBubbles.add(entry.getKey());
852 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800853 }
854
855 /**
856 * Whether this bubble was explicitly created by the user via a SysUI affordance.
857 */
858 boolean isUserCreatedBubble(String key) {
859 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800860 }
861
Beverlya53fb0d2020-01-29 15:26:13 -0500862 boolean isSummaryOfUserCreatedBubble(NotificationEntry entry) {
863 if (isSummaryOfBubbles(entry)) {
864 List<Bubble> bubbleChildren =
865 mBubbleData.getBubblesInGroup(entry.getSbn().getGroupKey());
866 for (int i = 0; i < bubbleChildren.size(); i++) {
867 // Check if any are user-created (i.e. experimental bubbles)
868 if (isUserCreatedBubble(bubbleChildren.get(i).getKey())) {
869 return true;
870 }
871 }
872 }
873 return false;
874 }
875
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800876 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500877 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500878 * <p>
879 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800880 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500881 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500882 void removeBubble(NotificationEntry entry, int reason) {
883 if (mBubbleData.hasBubbleWithKey(entry.getKey())) {
884 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800885 }
886 }
887
Beverlyed8aea22020-01-22 16:52:47 -0500888 private void onEntryAdded(NotificationEntry entry) {
889 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
890 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
891 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
892 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor22f2f072019-04-18 13:26:18 -0700893
Beverlyed8aea22020-01-22 16:52:47 -0500894 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
895 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
896 if (wasAdjusted && !previouslyUserCreated) {
897 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
898 mUserCreatedBubbles.add(entry.getKey());
Mady Mellorc2ff0112019-03-28 14:18:06 -0700899 }
Beverlyed8aea22020-01-22 16:52:47 -0500900 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700901 }
902 }
903
Beverlyed8aea22020-01-22 16:52:47 -0500904 private void onEntryUpdated(NotificationEntry entry) {
905 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
906 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
907 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
908 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700909
Beverlyed8aea22020-01-22 16:52:47 -0500910 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
911 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
912 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
913 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500914 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500915 } else if (shouldBubble) {
916 if (wasAdjusted && !previouslyUserCreated) {
917 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
918 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800919 }
Beverlyed8aea22020-01-22 16:52:47 -0500920 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800921 }
Beverlyed8aea22020-01-22 16:52:47 -0500922 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800923
Beverlya53fb0d2020-01-29 15:26:13 -0500924 private void onEntryRemoved(NotificationEntry entry) {
925 if (isSummaryOfBubbles(entry)) {
926 final String groupKey = entry.getSbn().getGroupKey();
927 mBubbleData.removeSuppressedSummary(groupKey);
928
929 // Remove any associated bubble children with the summary
930 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
931 for (int i = 0; i < bubbleChildren.size(); i++) {
932 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
933 }
934 } else {
935 removeBubble(entry, DISMISS_NOTIF_CANCEL);
936 }
937 }
938
Beverlyed8aea22020-01-22 16:52:47 -0500939 private void onRankingUpdated(RankingMap rankingMap) {
940 // Forward to BubbleData to block any bubbles which should no longer be shown
941 mBubbleData.notificationRankingUpdated(rankingMap);
942 }
Ned Burns01e38212019-01-03 16:32:52 -0500943
Mark Renouf71a3af62019-04-08 15:02:54 -0400944 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400945 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400946
Mark Renouf3bc5b362019-04-05 14:37:59 -0400947 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400948 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800949 // Update bubbles in overflow.
950 if (mOverflowCallback != null) {
951 mOverflowCallback.run();
952 }
953
Mark Renouf82a40e62019-05-23 16:16:24 -0400954 // Collapsing? Do this first before remaining steps.
955 if (update.expandedChanged && !update.expanded) {
956 mStackView.setExpanded(false);
957 }
958
959 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700960 ArrayList<Pair<Bubble, Integer>> removedBubbles =
961 new ArrayList<>(update.removedBubbles);
962 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400963 final Bubble bubble = removed.first;
964 @DismissReason final int reason = removed.second;
965 mStackView.removeBubble(bubble);
Mark Renoufc19b4732019-06-26 12:08:33 -0400966 // If the bubble is removed for user switching, leave the notification in place.
967 if (reason != DISMISS_USER_CHANGED) {
968 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800969 && !bubble.showInShade()) {
Beverlyed8aea22020-01-22 16:52:47 -0500970 // The bubble is now gone & the notification is hidden from the shade, so
971 // time to actually remove it
972 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -0500973 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -0500974 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400975 } else {
976 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400977 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700978
Mark Renoufc19b4732019-06-26 12:08:33 -0400979 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
980 // will get right result back
981 try {
982 mBarService.onNotificationBubbleChanged(bubble.getKey(),
983 false /* isBubble */);
984 } catch (RemoteException e) {
985 // Bad things have happened
986 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400987 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700988
Ned Burns00b4b2d2019-10-17 22:09:27 -0400989 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500990 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
991 // Time to potentially remove the summary
992 for (NotifCallback cb : mCallbacks) {
993 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -0700994 }
995 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700996 }
997 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400998
Lyn Hanc47e1712020-01-28 21:43:34 -0800999 if (update.addedBubble != null) {
1000 mStackView.addBubble(update.addedBubble);
1001 }
1002
Mark Renouf82a40e62019-05-23 16:16:24 -04001003 if (update.updatedBubble != null) {
1004 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -04001005 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001006
Lyn Hanb58c7562020-01-07 14:29:20 -08001007 // At this point, the correct bubbles are inflated in the stack.
1008 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -04001009 if (update.orderChanged) {
1010 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -04001011 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001012
Mark Renouf82a40e62019-05-23 16:16:24 -04001013 if (update.selectionChanged) {
1014 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -07001015 if (update.selectedBubble != null) {
1016 mNotificationGroupManager.updateSuppression(
1017 update.selectedBubble.getEntry());
1018 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001019 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001020
Mark Renouf82a40e62019-05-23 16:16:24 -04001021 // Expanding? Apply this last.
1022 if (update.expandedChanged && update.expanded) {
1023 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001024 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001025
Beverlyed8aea22020-01-22 16:52:47 -05001026 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001027 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001028 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001029 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001030
Issei Suzukia8d07312019-06-07 12:56:19 +02001031 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001032 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001033 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001034 mBubbleData.getSelectedBubble()));
1035
1036 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001037 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001038 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001039 mStackView.getExpandedBubble()));
1040 }
Lyn Hanb58c7562020-01-07 14:29:20 -08001041 Log.d(TAG, "\n[BubbleData] overflow:");
1042 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
1043 null));
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001044 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001045 }
1046 };
1047
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001048 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001049 * We intercept notification entries (including group summaries) dismissed by the user when
1050 * there is an active bubble associated with it. We do this so that developers can still
1051 * cancel it (and hence the bubbles associated with it). However, these intercepted
1052 * notifications should then be hidden from the shade since the user has cancelled them, so we
1053 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1054 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001055 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001056 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001057 */
Beverlya53fb0d2020-01-29 15:26:13 -05001058 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001059 if (entry == null) {
1060 return false;
1061 }
Beverlyed8aea22020-01-22 16:52:47 -05001062
Beverlya53fb0d2020-01-29 15:26:13 -05001063 final boolean interceptBubbleDismissal = mBubbleData.hasBubbleWithKey(entry.getKey())
1064 && entry.isBubble();
1065 final boolean interceptSummaryDismissal = isSummaryOfBubbles(entry);
Beverlyed8aea22020-01-22 16:52:47 -05001066
Beverlya53fb0d2020-01-29 15:26:13 -05001067 if (interceptSummaryDismissal) {
1068 handleSummaryDismissalInterception(entry);
1069 } else if (interceptBubbleDismissal) {
1070 Bubble bubble = mBubbleData.getBubbleWithKey(entry.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001071 bubble.setSuppressNotification(true);
1072 bubble.setShowDot(false /* show */, true /* animate */);
Beverlya53fb0d2020-01-29 15:26:13 -05001073 } else {
Beverlyed8aea22020-01-22 16:52:47 -05001074 return false;
1075 }
Beverlya53fb0d2020-01-29 15:26:13 -05001076
1077 // Update the shade
1078 for (NotifCallback cb : mCallbacks) {
1079 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1080 }
1081 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001082 }
1083
Beverlya53fb0d2020-01-29 15:26:13 -05001084 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1085 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001086 return false;
1087 }
Beverlya53fb0d2020-01-29 15:26:13 -05001088
1089 String groupKey = entry.getSbn().getGroupKey();
1090 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1091 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1092 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1093 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1094 return (isSuppressedSummary || isSummary)
1095 && bubbleChildren != null
1096 && !bubbleChildren.isEmpty();
1097 }
1098
1099 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1100 // current children in the row:
1101 final List<NotificationEntry> children = summary.getChildren();
1102 if (children != null) {
1103 for (int i = 0; i < children.size(); i++) {
1104 NotificationEntry child = children.get(i);
1105 if (mBubbleData.hasBubbleWithKey(child.getKey())) {
1106 // Suppress the bubbled child
1107 // As far as group manager is concerned, once a child is no longer shown
1108 // in the shade, it is essentially removed.
1109 Bubble bubbleChild = mBubbleData.getBubbleWithKey(child.getKey());
1110 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1111 bubbleChild.setSuppressNotification(true);
1112 bubbleChild.setShowDot(false /* show */, true /* animate */);
1113 } else {
1114 // non-bubbled children can be removed
1115 for (NotifCallback cb : mCallbacks) {
1116 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1117 }
1118 }
1119 }
1120 }
1121
1122 // And since all children are removed, remove the summary.
1123 mNotificationGroupManager.onEntryRemoved(summary);
1124
1125 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1126 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1127 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001128 }
1129
1130 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001131 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001132 * Updates the visibility of the bubbles based on current state.
1133 * Does not un-bubble, just hides or un-hides. Notifies any
1134 * {@link BubbleStateChangeListener}s of visibility changes.
1135 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001136 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001137 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001138 if (mStackView == null) {
1139 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001140 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001141 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1142 // Bubbles only appear in unlocked shade
1143 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001144 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001145 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001146 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001147
Mady Mellor698d9e82019-08-01 23:11:53 +00001148 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001149 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001150 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001151 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001152 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1153 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1154 }
1155
1156 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001157 }
1158
1159 /**
1160 * Rect indicating the touchable region for the bubble stack / expanded stack.
1161 */
1162 public Rect getTouchableRegion() {
1163 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1164 return null;
1165 }
1166 mStackView.getBoundsOnScreen(mTempRect);
1167 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001168 }
1169
Mady Mellor390bff42019-04-05 15:09:01 -07001170 /**
1171 * The display id of the expanded view, if the stack is expanded and not occluded by the
1172 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1173 */
1174 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001175 final Bubble bubble = getExpandedBubble(context);
1176 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
1177 }
1178
1179 @Nullable
1180 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001181 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001182 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001183 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001184 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001185 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +02001186 final Bubble expandedBubble = mStackView.getExpandedBubble();
1187 if (defaultDisplay && expandedBubble != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001188 && !mNotificationShadeWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001189 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -07001190 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001191 return null;
Mady Mellor390bff42019-04-05 15:09:01 -07001192 }
1193
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001194 @VisibleForTesting
1195 BubbleStackView getStackView() {
1196 return mStackView;
1197 }
1198
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001199 /**
1200 * Description of current bubble state.
1201 */
1202 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1203 pw.println("BubbleController state:");
1204 mBubbleData.dump(fd, pw, args);
1205 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001206 if (mStackView != null) {
1207 mStackView.dump(fd, pw, args);
1208 }
1209 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001210 }
1211
Mady Mellore80930e2019-03-21 16:00:45 -07001212 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001213 * This task stack listener is responsible for responding to tasks moved to the front
1214 * which are on the default (main) display. When this happens, expanded bubbles must be
1215 * collapsed so the user may interact with the app which was just moved to the front.
1216 * <p>
1217 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1218 * these calls via a main thread Handler.
1219 */
1220 @MainThread
1221 private class BubbleTaskStackListener extends TaskStackChangeListener {
1222
Mark Renoufcecc77b2019-01-30 16:32:24 -05001223 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001224 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1225 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001226 if (!mStackView.isExpansionAnimating()) {
1227 mBubbleData.setExpanded(false);
1228 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001229 }
1230 }
1231
Mark Renoufcecc77b2019-01-30 16:32:24 -05001232 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001233 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001234 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001235 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001236 }
1237 }
Mark Renouf446251d2019-04-26 10:22:41 -04001238
1239 @Override
1240 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1241 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1242 mBubbleData.setExpanded(false);
1243 }
1244 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001245
1246 @Override
1247 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001248 if (mStackView == null) {
1249 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001250 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001251 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001252 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001253
1254 @Override
1255 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001256 final Bubble expandedBubble = mStackView != null
1257 ? mStackView.getExpandedBubble()
1258 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001259 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1260 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001261 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001262 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001263 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001264 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001265 }
1266
Mady Mellorca0c24c2019-05-16 16:14:32 -07001267 /**
1268 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1269 *
1270 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1271 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1272 *
1273 * @param context the context to use.
1274 * @param entry the entry to bubble.
1275 */
1276 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1277 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001278 ? entry.getBubbleMetadata().getBubbleIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001279 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001280 if (entry.getBubbleMetadata() != null
1281 && entry.getBubbleMetadata().getShortcutId() != null) {
1282 return true;
1283 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001284 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001285 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001286 return false;
1287 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001288 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1289 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001290 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001291 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001292 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001293 Log.w(TAG, "Unable to send as bubble, "
1294 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001295 + intent);
1296 return false;
1297 }
1298 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001299 Log.w(TAG, "Unable to send as bubble, "
1300 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001301 + intent);
1302 return false;
1303 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001304 return true;
1305 }
1306
Joshua Tsujia19515f2019-02-13 18:02:29 -05001307 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001308 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001309 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001310 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1311 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1312 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001313 }
1314 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001315 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001316}