blob: 05838abe184a78cad9233d46d7ccc2a1bef23f76 [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) {
752 mBubbleData.promoteBubbleFromOverflow(bubble);
753 }
754
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800755 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400756 * Request the stack expand if needed, then select the specified Bubble as current.
757 *
758 * @param notificationKey the notification key for the bubble to be selected
759 */
760 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400761 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
762 if (bubble != null) {
763 mBubbleData.setSelectedBubble(bubble);
764 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400765 }
766 }
767
768 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800769 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
770 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500771 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400772 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800773 }
774
775 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500776 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
777 * is forwarded a back key down/up pair.
778 */
779 public void performBackPressIfNeeded() {
780 if (mStackView != null) {
781 mStackView.performBackPressIfNeeded();
782 }
783 }
784
785 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800786 * Adds or updates a bubble associated with the provided notification entry.
787 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400788 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800789 */
Mark Renouff97ed462019-04-05 13:46:24 -0400790 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700791 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400792 }
793
794 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700795 updateBubble(notif, suppressFlyout, true /* showInShade */);
796 }
797
798 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800799 if (mStackView == null) {
800 // Lazy init stack view when a bubble is created
801 ensureStackViewCreated();
802 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700803 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400804 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700805 notif.setInterruption();
806 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800807 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
808 bubble.setInflateSynchronously(mInflateSynchronously);
809 bubble.inflate(
810 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
811 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700812 }
813
814 /**
815 * Called when a user has indicated that an active notification should be shown as a bubble.
816 * <p>
817 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
818 * the notification from appearing in the shade.
819 *
820 * @param entry the notification to show as a bubble.
821 */
822 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800823 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
824 Log.d(TAG, "onUserCreatedBubble: " + entry.getKey());
825 }
Heemin Seogba6337f2019-12-10 15:34:37 -0800826 mShadeController.collapsePanel(true);
Mady Mellor7f234902019-10-20 12:06:29 -0700827 entry.setFlagBubble(true);
828 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
Mady Mellorff076eb2019-11-13 10:12:06 -0800829 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800830 mUserBlockedBubbles.remove(entry.getKey());
Mady Mellor7f234902019-10-20 12:06:29 -0700831 }
832
833 /**
834 * Called when a user has indicated that an active notification appearing as a bubble should
835 * no longer be shown as a bubble.
836 *
837 * @param entry the notification to no longer show as a bubble.
838 */
839 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800840 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
841 Log.d(TAG, "onUserDemotedBubble: " + entry.getKey());
842 }
Mady Mellor7f234902019-10-20 12:06:29 -0700843 entry.setFlagBubble(false);
Beverlya53fb0d2020-01-29 15:26:13 -0500844 removeBubble(entry, DISMISS_BLOCKED);
Mady Mellorff076eb2019-11-13 10:12:06 -0800845 mUserCreatedBubbles.remove(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800846 if (BubbleExperimentConfig.isPackageWhitelistedToAutoBubble(
847 mContext, entry.getSbn().getPackageName())) {
848 // This package is whitelist but user demoted the bubble, let's save it so we don't
849 // auto-bubble for the whitelist again.
850 mUserBlockedBubbles.add(entry.getKey());
851 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800852 }
853
854 /**
855 * Whether this bubble was explicitly created by the user via a SysUI affordance.
856 */
857 boolean isUserCreatedBubble(String key) {
858 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800859 }
860
Beverlya53fb0d2020-01-29 15:26:13 -0500861 boolean isSummaryOfUserCreatedBubble(NotificationEntry entry) {
862 if (isSummaryOfBubbles(entry)) {
863 List<Bubble> bubbleChildren =
864 mBubbleData.getBubblesInGroup(entry.getSbn().getGroupKey());
865 for (int i = 0; i < bubbleChildren.size(); i++) {
866 // Check if any are user-created (i.e. experimental bubbles)
867 if (isUserCreatedBubble(bubbleChildren.get(i).getKey())) {
868 return true;
869 }
870 }
871 }
872 return false;
873 }
874
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800875 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500876 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500877 * <p>
878 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800879 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500880 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500881 void removeBubble(NotificationEntry entry, int reason) {
882 if (mBubbleData.hasBubbleWithKey(entry.getKey())) {
883 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800884 }
885 }
886
Beverlyed8aea22020-01-22 16:52:47 -0500887 private void onEntryAdded(NotificationEntry entry) {
888 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
889 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
890 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
891 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor22f2f072019-04-18 13:26:18 -0700892
Beverlyed8aea22020-01-22 16:52:47 -0500893 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
894 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
895 if (wasAdjusted && !previouslyUserCreated) {
896 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
897 mUserCreatedBubbles.add(entry.getKey());
Mady Mellorc2ff0112019-03-28 14:18:06 -0700898 }
Beverlyed8aea22020-01-22 16:52:47 -0500899 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700900 }
901 }
902
Beverlyed8aea22020-01-22 16:52:47 -0500903 private void onEntryUpdated(NotificationEntry entry) {
904 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
905 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
906 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
907 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700908
Beverlyed8aea22020-01-22 16:52:47 -0500909 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
910 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
911 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
912 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500913 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500914 } else if (shouldBubble) {
915 if (wasAdjusted && !previouslyUserCreated) {
916 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
917 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800918 }
Beverlyed8aea22020-01-22 16:52:47 -0500919 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800920 }
Beverlyed8aea22020-01-22 16:52:47 -0500921 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800922
Beverlya53fb0d2020-01-29 15:26:13 -0500923 private void onEntryRemoved(NotificationEntry entry) {
924 if (isSummaryOfBubbles(entry)) {
925 final String groupKey = entry.getSbn().getGroupKey();
926 mBubbleData.removeSuppressedSummary(groupKey);
927
928 // Remove any associated bubble children with the summary
929 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
930 for (int i = 0; i < bubbleChildren.size(); i++) {
931 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
932 }
933 } else {
934 removeBubble(entry, DISMISS_NOTIF_CANCEL);
935 }
936 }
937
Beverlyed8aea22020-01-22 16:52:47 -0500938 private void onRankingUpdated(RankingMap rankingMap) {
939 // Forward to BubbleData to block any bubbles which should no longer be shown
940 mBubbleData.notificationRankingUpdated(rankingMap);
941 }
Ned Burns01e38212019-01-03 16:32:52 -0500942
Mark Renouf71a3af62019-04-08 15:02:54 -0400943 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400944 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400945
Mark Renouf3bc5b362019-04-05 14:37:59 -0400946 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400947 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800948 // Update bubbles in overflow.
949 if (mOverflowCallback != null) {
950 mOverflowCallback.run();
951 }
952
Mark Renouf82a40e62019-05-23 16:16:24 -0400953 // Collapsing? Do this first before remaining steps.
954 if (update.expandedChanged && !update.expanded) {
955 mStackView.setExpanded(false);
956 }
957
958 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700959 ArrayList<Pair<Bubble, Integer>> removedBubbles =
960 new ArrayList<>(update.removedBubbles);
961 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400962 final Bubble bubble = removed.first;
963 @DismissReason final int reason = removed.second;
964 mStackView.removeBubble(bubble);
Mark Renoufc19b4732019-06-26 12:08:33 -0400965 // If the bubble is removed for user switching, leave the notification in place.
966 if (reason != DISMISS_USER_CHANGED) {
967 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800968 && !bubble.showInShade()) {
Beverlyed8aea22020-01-22 16:52:47 -0500969 // The bubble is now gone & the notification is hidden from the shade, so
970 // time to actually remove it
971 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -0500972 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -0500973 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400974 } else {
975 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400976 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700977
Mark Renoufc19b4732019-06-26 12:08:33 -0400978 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
979 // will get right result back
980 try {
981 mBarService.onNotificationBubbleChanged(bubble.getKey(),
982 false /* isBubble */);
983 } catch (RemoteException e) {
984 // Bad things have happened
985 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400986 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700987
Ned Burns00b4b2d2019-10-17 22:09:27 -0400988 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500989 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
990 // Time to potentially remove the summary
991 for (NotifCallback cb : mCallbacks) {
992 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -0700993 }
994 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700995 }
996 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400997
Lyn Hanc47e1712020-01-28 21:43:34 -0800998 if (update.addedBubble != null) {
999 mStackView.addBubble(update.addedBubble);
1000 }
1001
Mark Renouf82a40e62019-05-23 16:16:24 -04001002 if (update.updatedBubble != null) {
1003 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -04001004 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001005
Lyn Hanb58c7562020-01-07 14:29:20 -08001006 // At this point, the correct bubbles are inflated in the stack.
1007 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -04001008 if (update.orderChanged) {
1009 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -04001010 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001011
Mark Renouf82a40e62019-05-23 16:16:24 -04001012 if (update.selectionChanged) {
1013 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -07001014 if (update.selectedBubble != null) {
1015 mNotificationGroupManager.updateSuppression(
1016 update.selectedBubble.getEntry());
1017 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001018 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001019
Mark Renouf82a40e62019-05-23 16:16:24 -04001020 // Expanding? Apply this last.
1021 if (update.expandedChanged && update.expanded) {
1022 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001023 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001024
Beverlyed8aea22020-01-22 16:52:47 -05001025 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001026 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001027 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001028 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001029
Issei Suzukia8d07312019-06-07 12:56:19 +02001030 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001031 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001032 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001033 mBubbleData.getSelectedBubble()));
1034
1035 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001036 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001037 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001038 mStackView.getExpandedBubble()));
1039 }
Lyn Hanb58c7562020-01-07 14:29:20 -08001040 Log.d(TAG, "\n[BubbleData] overflow:");
1041 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
1042 null));
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001043 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001044 }
1045 };
1046
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001047 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001048 * We intercept notification entries (including group summaries) dismissed by the user when
1049 * there is an active bubble associated with it. We do this so that developers can still
1050 * cancel it (and hence the bubbles associated with it). However, these intercepted
1051 * notifications should then be hidden from the shade since the user has cancelled them, so we
1052 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1053 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001054 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001055 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001056 */
Beverlya53fb0d2020-01-29 15:26:13 -05001057 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001058 if (entry == null) {
1059 return false;
1060 }
Beverlyed8aea22020-01-22 16:52:47 -05001061
Beverlya53fb0d2020-01-29 15:26:13 -05001062 final boolean interceptBubbleDismissal = mBubbleData.hasBubbleWithKey(entry.getKey())
1063 && entry.isBubble();
1064 final boolean interceptSummaryDismissal = isSummaryOfBubbles(entry);
Beverlyed8aea22020-01-22 16:52:47 -05001065
Beverlya53fb0d2020-01-29 15:26:13 -05001066 if (interceptSummaryDismissal) {
1067 handleSummaryDismissalInterception(entry);
1068 } else if (interceptBubbleDismissal) {
1069 Bubble bubble = mBubbleData.getBubbleWithKey(entry.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001070 bubble.setSuppressNotification(true);
1071 bubble.setShowDot(false /* show */, true /* animate */);
Beverlya53fb0d2020-01-29 15:26:13 -05001072 } else {
Beverlyed8aea22020-01-22 16:52:47 -05001073 return false;
1074 }
Beverlya53fb0d2020-01-29 15:26:13 -05001075
1076 // Update the shade
1077 for (NotifCallback cb : mCallbacks) {
1078 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1079 }
1080 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001081 }
1082
Beverlya53fb0d2020-01-29 15:26:13 -05001083 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1084 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001085 return false;
1086 }
Beverlya53fb0d2020-01-29 15:26:13 -05001087
1088 String groupKey = entry.getSbn().getGroupKey();
1089 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1090 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1091 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1092 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1093 return (isSuppressedSummary || isSummary)
1094 && bubbleChildren != null
1095 && !bubbleChildren.isEmpty();
1096 }
1097
1098 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1099 // current children in the row:
1100 final List<NotificationEntry> children = summary.getChildren();
1101 if (children != null) {
1102 for (int i = 0; i < children.size(); i++) {
1103 NotificationEntry child = children.get(i);
1104 if (mBubbleData.hasBubbleWithKey(child.getKey())) {
1105 // Suppress the bubbled child
1106 // As far as group manager is concerned, once a child is no longer shown
1107 // in the shade, it is essentially removed.
1108 Bubble bubbleChild = mBubbleData.getBubbleWithKey(child.getKey());
1109 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1110 bubbleChild.setSuppressNotification(true);
1111 bubbleChild.setShowDot(false /* show */, true /* animate */);
1112 } else {
1113 // non-bubbled children can be removed
1114 for (NotifCallback cb : mCallbacks) {
1115 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1116 }
1117 }
1118 }
1119 }
1120
1121 // And since all children are removed, remove the summary.
1122 mNotificationGroupManager.onEntryRemoved(summary);
1123
1124 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1125 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1126 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001127 }
1128
1129 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001130 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001131 * Updates the visibility of the bubbles based on current state.
1132 * Does not un-bubble, just hides or un-hides. Notifies any
1133 * {@link BubbleStateChangeListener}s of visibility changes.
1134 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001135 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001136 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001137 if (mStackView == null) {
1138 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001139 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001140 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1141 // Bubbles only appear in unlocked shade
1142 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001143 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001144 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001145 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001146
Mady Mellor698d9e82019-08-01 23:11:53 +00001147 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001148 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001149 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001150 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001151 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1152 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1153 }
1154
1155 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001156 }
1157
1158 /**
1159 * Rect indicating the touchable region for the bubble stack / expanded stack.
1160 */
1161 public Rect getTouchableRegion() {
1162 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1163 return null;
1164 }
1165 mStackView.getBoundsOnScreen(mTempRect);
1166 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001167 }
1168
Mady Mellor390bff42019-04-05 15:09:01 -07001169 /**
1170 * The display id of the expanded view, if the stack is expanded and not occluded by the
1171 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1172 */
1173 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001174 final Bubble bubble = getExpandedBubble(context);
1175 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
1176 }
1177
1178 @Nullable
1179 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001180 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001181 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001182 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001183 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001184 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +02001185 final Bubble expandedBubble = mStackView.getExpandedBubble();
1186 if (defaultDisplay && expandedBubble != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001187 && !mNotificationShadeWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001188 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -07001189 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001190 return null;
Mady Mellor390bff42019-04-05 15:09:01 -07001191 }
1192
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001193 @VisibleForTesting
1194 BubbleStackView getStackView() {
1195 return mStackView;
1196 }
1197
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001198 /**
1199 * Description of current bubble state.
1200 */
1201 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1202 pw.println("BubbleController state:");
1203 mBubbleData.dump(fd, pw, args);
1204 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001205 if (mStackView != null) {
1206 mStackView.dump(fd, pw, args);
1207 }
1208 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001209 }
1210
Mady Mellore80930e2019-03-21 16:00:45 -07001211 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001212 * This task stack listener is responsible for responding to tasks moved to the front
1213 * which are on the default (main) display. When this happens, expanded bubbles must be
1214 * collapsed so the user may interact with the app which was just moved to the front.
1215 * <p>
1216 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1217 * these calls via a main thread Handler.
1218 */
1219 @MainThread
1220 private class BubbleTaskStackListener extends TaskStackChangeListener {
1221
Mark Renoufcecc77b2019-01-30 16:32:24 -05001222 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001223 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1224 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001225 if (!mStackView.isExpansionAnimating()) {
1226 mBubbleData.setExpanded(false);
1227 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001228 }
1229 }
1230
Mark Renoufcecc77b2019-01-30 16:32:24 -05001231 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001232 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001233 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001234 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001235 }
1236 }
Mark Renouf446251d2019-04-26 10:22:41 -04001237
1238 @Override
1239 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1240 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1241 mBubbleData.setExpanded(false);
1242 }
1243 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001244
1245 @Override
1246 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001247 if (mStackView == null) {
1248 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001249 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001250 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001251 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001252
1253 @Override
1254 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001255 final Bubble expandedBubble = mStackView != null
1256 ? mStackView.getExpandedBubble()
1257 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001258 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1259 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001260 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001261 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001262 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001263 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001264 }
1265
Mady Mellorca0c24c2019-05-16 16:14:32 -07001266 /**
1267 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1268 *
1269 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1270 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1271 *
1272 * @param context the context to use.
1273 * @param entry the entry to bubble.
1274 */
1275 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1276 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001277 ? entry.getBubbleMetadata().getBubbleIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001278 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001279 if (entry.getBubbleMetadata() != null
1280 && entry.getBubbleMetadata().getShortcutId() != null) {
1281 return true;
1282 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001283 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001284 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001285 return false;
1286 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001287 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1288 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001289 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001290 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001291 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001292 Log.w(TAG, "Unable to send as bubble, "
1293 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001294 + intent);
1295 return false;
1296 }
1297 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001298 Log.w(TAG, "Unable to send as bubble, "
1299 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001300 + intent);
1301 return false;
1302 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001303 return true;
1304 }
1305
Joshua Tsujia19515f2019-02-13 18:02:29 -05001306 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001307 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001308 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001309 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1310 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1311 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001312 }
1313 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001314 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001315}