blob: 1f94dbd5e59afe792b41a27a299cbc627a64e881 [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;
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +010075import com.android.systemui.bubbles.dagger.BubbleModule;
Beverly8fdb5332019-02-04 14:29:49 -050076import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050077import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000078import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050079import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050080import com.android.systemui.shared.system.WindowManagerWrapper;
Beverlya53fb0d2020-01-29 15:26:13 -050081import com.android.systemui.statusbar.FeatureFlags;
Mark Renoufc19b4732019-06-26 12:08:33 -040082import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070083import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050084import com.android.systemui.statusbar.notification.NotificationEntryListener;
85import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080086import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Beverlya53fb0d2020-01-29 15:26:13 -050087import com.android.systemui.statusbar.notification.collection.NotifCollection;
88import com.android.systemui.statusbar.notification.collection.NotifPipeline;
Ned Burnsf81c4c42019-01-07 14:10:43 -050089import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Beverlya53fb0d2020-01-29 15:26:13 -050090import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener;
Mady Mellor22f2f072019-04-18 13:26:18 -070091import com.android.systemui.statusbar.phone.NotificationGroupManager;
wilsonshihe8321942019-10-18 18:39:46 +080092import com.android.systemui.statusbar.phone.NotificationShadeWindowController;
Mady Mellor7f234902019-10-20 12:06:29 -070093import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080094import com.android.systemui.statusbar.phone.StatusBar;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070095import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040096import com.android.systemui.statusbar.policy.ZenModeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080097
Mady Mellor70cba7bb2019-07-02 15:06:07 -070098import java.io.FileDescriptor;
99import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500100import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -0400101import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -0700102import java.util.ArrayList;
Mady Mellorff076eb2019-11-13 10:12:06 -0800103import java.util.HashSet;
Lyn Hanb58c7562020-01-07 14:29:20 -0800104import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500105
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800106/**
107 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
108 * Bubbles can be expanded to show more content.
109 *
110 * The controller manages addition, removal, and visible state of bubbles on screen.
111 */
Beverlya53fb0d2020-01-29 15:26:13 -0500112public class BubbleController implements ConfigurationController.ConfigurationListener, Dumpable {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800113
Issei Suzukia8d07312019-06-07 12:56:19 +0200114 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800115
Mark Renouf08bc42a2019-03-07 13:01:59 -0500116 @Retention(SOURCE)
117 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400118 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700119 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400120 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500121 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700122
Mark Renouf08bc42a2019-03-07 13:01:59 -0500123 static final int DISMISS_USER_GESTURE = 1;
124 static final int DISMISS_AGED = 2;
125 static final int DISMISS_TASK_FINISHED = 3;
126 static final int DISMISS_BLOCKED = 4;
127 static final int DISMISS_NOTIF_CANCEL = 5;
128 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700129 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400130 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700131 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700132 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500133
Ned Burns01e38212019-01-03 16:32:52 -0500134 private final Context mContext;
135 private final NotificationEntryManager mNotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500136 private final NotifPipeline mNotifPipeline;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500137 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800138 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800139 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100140 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700141 private final NotificationGroupManager mNotificationGroupManager;
Heemin Seogba6337f2019-12-10 15:34:37 -0800142 private final ShadeController mShadeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800143
Mady Mellor3dff9e62019-02-05 18:12:53 -0800144 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400145 @Nullable private BubbleStackView mStackView;
Mady Mellor247ca2c2019-12-02 16:18:59 -0800146 private BubbleIconFactory mBubbleIconFactory;
Lyn Hanb58c7562020-01-07 14:29:20 -0800147 private int mMaxBubbles;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800148
Mark Renoufc19b4732019-06-26 12:08:33 -0400149 // Tracks the id of the current (foreground) user.
150 private int mCurrentUserId;
151 // Saves notification keys of active bubbles when users are switched.
152 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
153
Mady Mellorff076eb2019-11-13 10:12:06 -0800154 // Saves notification keys of user created "fake" bubbles so that we can allow notifications
155 // like these to bubble by default. Doesn't persist across reboots, not a long-term solution.
156 private final HashSet<String> mUserCreatedBubbles;
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800157 // If we're auto-bubbling bubbles via a whitelist, we need to track which notifs from that app
158 // have been "demoted" back to a notification so that we don't auto-bubbles those again.
159 // Doesn't persist across reboots, not a long-term solution.
160 private final HashSet<String> mUserBlockedBubbles;
Mady Mellorff076eb2019-11-13 10:12:06 -0800161
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800162 // Bubbles get added to the status bar view
wilsonshihe8321942019-10-18 18:39:46 +0800163 private final NotificationShadeWindowController mNotificationShadeWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400164 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800165 private StatusBarStateListener mStatusBarStateListener;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500166
Lyn Hanb58c7562020-01-07 14:29:20 -0800167 // Callback that updates BubbleOverflowActivity on data change.
168 @Nullable private Runnable mOverflowCallback = null;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800169
Mady Melloraa8fef22019-04-11 13:36:40 -0700170 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700171 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800172
Mady Mellord1c78b262018-11-06 18:04:40 -0800173 // Used for determining view rect for touch interaction
174 private Rect mTempRect = new Rect();
175
Mark Renoufc19b4732019-06-26 12:08:33 -0400176 // Listens to user switch so bubbles can be saved and restored.
177 private final NotificationLockscreenUserManager mNotifUserManager;
178
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400179 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
180 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
181
Mady Mellor3df7ab02019-12-09 15:07:10 -0800182 private boolean mInflateSynchronously;
183
Beverlyed8aea22020-01-22 16:52:47 -0500184 // TODO (b/145659174): allow for multiple callbacks to support the "shadow" new notif pipeline
185 private final List<NotifCallback> mCallbacks = new ArrayList<>();
186
Mady Mellor5549dd22018-11-06 18:07:34 -0800187 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800188 * Listener to be notified when some states of the bubbles change.
189 */
190 public interface BubbleStateChangeListener {
191 /**
192 * Called when the stack has bubbles or no longer has bubbles.
193 */
194 void onHasBubblesChanged(boolean hasBubbles);
195 }
196
Mady Mellorcd9b1302018-11-06 18:08:04 -0800197 /**
198 * Listener to find out about stack expansion / collapse events.
199 */
200 public interface BubbleExpandListener {
201 /**
202 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700203 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800204 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800205 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800206 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800207 void onBubbleExpandChanged(boolean isExpanding, String key);
208 }
209
210 /**
Mady Mellorf44b6832020-01-14 13:26:14 -0800211 * Listener to be notified when a bubbles' notification suppression state changes.
212 */
213 public interface NotificationSuppressionChangedListener {
214 /**
215 * Called when the notification suppression state of a bubble changes.
216 */
217 void onBubbleNotificationSuppressionChange(Bubble bubble);
Beverlyed8aea22020-01-22 16:52:47 -0500218 }
Mady Mellorf44b6832020-01-14 13:26:14 -0800219
Beverlyed8aea22020-01-22 16:52:47 -0500220 /**
221 * Callback for when the BubbleController wants to interact with the notification pipeline to:
222 * - Remove a previously bubbled notification
223 * - Update the notification shade since bubbled notification should/shouldn't be showing
224 */
225 public interface NotifCallback {
226 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500227 * Called when a bubbled notification that was hidden from the shade is now being removed
228 * This can happen when an app cancels a bubbled notification or when the user dismisses a
229 * bubble.
Beverlyed8aea22020-01-22 16:52:47 -0500230 */
Beverlya53fb0d2020-01-29 15:26:13 -0500231 void removeNotification(NotificationEntry entry, int reason);
Beverlyed8aea22020-01-22 16:52:47 -0500232
233 /**
234 * Called when a bubbled notification has changed whether it should be
235 * filtered from the shade.
236 */
Beverlya53fb0d2020-01-29 15:26:13 -0500237 void invalidateNotifications(String reason);
Beverlyed8aea22020-01-22 16:52:47 -0500238
239 /**
240 * Called on a bubbled entry that has been removed when there are no longer
241 * bubbled entries in its group.
242 *
243 * Checks whether its group has any other (non-bubbled) children. If it doesn't,
244 * removes all remnants of the group's summary from the notification pipeline.
245 * TODO: (b/145659174) Only old pipeline needs this - delete post-migration.
246 */
247 void maybeCancelSummary(NotificationEntry entry);
Mady Mellorf44b6832020-01-14 13:26:14 -0800248 }
249
250 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800251 * Listens for the current state of the status bar and updates the visibility state
252 * of bubbles as needed.
253 */
254 private class StatusBarStateListener implements StatusBarStateController.StateListener {
255 private int mState;
256 /**
257 * Returns the current status bar state.
258 */
259 public int getCurrentState() {
260 return mState;
261 }
262
263 @Override
264 public void onStateChanged(int newState) {
265 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400266 boolean shouldCollapse = (mState != SHADE);
267 if (shouldCollapse) {
268 collapseStack();
269 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700270 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800271 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800272 }
273
Mady Mellor7f234902019-10-20 12:06:29 -0700274 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800275 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700276 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800277 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700278 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700279 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400280 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400281 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700282 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700283 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500284 NotificationEntryManager entryManager,
285 NotifPipeline notifPipeline,
286 FeatureFlags featureFlags,
287 DumpController dumpController) {
wilsonshihe8321942019-10-18 18:39:46 +0800288 this(context, notificationShadeWindowController, statusBarStateController, shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700289 data, null /* synchronizer */, configurationController, interruptionStateProvider,
Beverlya53fb0d2020-01-29 15:26:13 -0500290 zenModeController, notifUserManager, groupManager, entryManager,
291 notifPipeline, featureFlags, dumpController);
Mady Mellor7f234902019-10-20 12:06:29 -0700292 }
293
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +0100294 /**
295 * Injected constructor. See {@link BubbleModule}.
296 */
Mady Mellor7f234902019-10-20 12:06:29 -0700297 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800298 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700299 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800300 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700301 BubbleData data,
302 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
303 ConfigurationController configurationController,
304 NotificationInterruptionStateProvider interruptionStateProvider,
305 ZenModeController zenModeController,
306 NotificationLockscreenUserManager notifUserManager,
307 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500308 NotificationEntryManager entryManager,
309 NotifPipeline notifPipeline,
310 FeatureFlags featureFlags,
311 DumpController dumpController) {
312 dumpController.registerDumpable(TAG, this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800313 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800314 mShadeController = shadeController;
Mady Melloraa8fef22019-04-11 13:36:40 -0700315 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400316 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400317 mZenModeController = zenModeController;
318 mZenModeController.addCallback(new ZenModeController.Callback() {
319 @Override
320 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800321 for (Bubble b : mBubbleData.getBubbles()) {
322 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700323 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400324 }
325
326 @Override
327 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800328 for (Bubble b : mBubbleData.getBubbles()) {
329 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700330 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400331 }
332 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700333
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700334 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800335
Mady Mellorf44b6832020-01-14 13:26:14 -0800336 mMaxBubbles = mContext.getResources().getInteger(R.integer.bubbles_max_rendered);
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400337 mBubbleData = data;
338 mBubbleData.setListener(mBubbleDataListener);
Mady Mellorf44b6832020-01-14 13:26:14 -0800339 mBubbleData.setSuppressionChangedListener(new NotificationSuppressionChangedListener() {
340 @Override
341 public void onBubbleNotificationSuppressionChange(Bubble bubble) {
342 // Make sure NoMan knows it's not showing in the shade anymore so anyone querying it
343 // can tell.
344 try {
345 mBarService.onBubbleNotificationSuppressionChanged(bubble.getKey(),
346 !bubble.showInShade());
347 } catch (RemoteException e) {
348 // Bad things have happened
349 }
350 }
351 });
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400352
Mady Mellor7f234902019-10-20 12:06:29 -0700353 mNotificationEntryManager = entryManager;
Mady Mellor22f2f072019-04-18 13:26:18 -0700354 mNotificationGroupManager = groupManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500355 mNotifPipeline = notifPipeline;
356
357 if (!featureFlags.isNewNotifPipelineRenderingEnabled()) {
358 setupNEM();
359 } else {
360 setupNotifPipeline();
361 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800362
wilsonshihe8321942019-10-18 18:39:46 +0800363 mNotificationShadeWindowController = notificationShadeWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800364 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700365 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500366
Mark Renoufcecc77b2019-01-30 16:32:24 -0500367 mTaskStackListener = new BubbleTaskStackListener();
368 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800369
Joshua Tsujia19515f2019-02-13 18:02:29 -0500370 try {
371 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
372 } catch (RemoteException e) {
373 e.printStackTrace();
374 }
Issei Suzukic0387542019-03-08 17:31:14 +0100375 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700376
377 mBarService = IStatusBarService.Stub.asInterface(
378 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400379
380 mSavedBubbleKeysPerUser = new SparseSetArray<>();
381 mCurrentUserId = mNotifUserManager.getCurrentUserId();
382 mNotifUserManager.addUserChangedListener(
Steve Elliottb47f1c72019-12-19 12:39:26 -0500383 new NotificationLockscreenUserManager.UserChangedListener() {
384 @Override
385 public void onUserChanged(int newUserId) {
386 BubbleController.this.saveBubbles(mCurrentUserId);
387 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
388 BubbleController.this.restoreBubbles(newUserId);
389 mCurrentUserId = newUserId;
390 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400391 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800392
393 mUserCreatedBubbles = new HashSet<>();
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800394 mUserBlockedBubbles = new HashSet<>();
Aran Inkaa4dfa72019-11-18 16:49:07 -0500395
Mady Mellor247ca2c2019-12-02 16:18:59 -0800396 mBubbleIconFactory = new BubbleIconFactory(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800397 }
398
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400399 /**
Beverlyed8aea22020-01-22 16:52:47 -0500400 * See {@link NotifCallback}.
401 */
402 public void addNotifCallback(NotifCallback callback) {
403 mCallbacks.add(callback);
404 }
405
406 private void setupNEM() {
407 mNotificationEntryManager.addNotificationEntryListener(
408 new NotificationEntryListener() {
409 @Override
Mady Mellorf9439ab2020-01-30 16:06:53 -0800410 public void onPendingEntryAdded(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -0500411 onEntryAdded(entry);
412 }
413
414 @Override
415 public void onPreEntryUpdated(NotificationEntry entry) {
416 onEntryUpdated(entry);
417 }
418
419 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500420 public void onEntryRemoved(
421 NotificationEntry entry,
422 @android.annotation.Nullable NotificationVisibility visibility,
423 boolean removedByUser) {
424 BubbleController.this.onEntryRemoved(entry);
425 }
426
427 @Override
Beverlyed8aea22020-01-22 16:52:47 -0500428 public void onNotificationRankingUpdated(RankingMap rankingMap) {
429 onRankingUpdated(rankingMap);
430 }
431 });
432
Evan Laird04373662020-01-24 17:37:39 -0500433 mNotificationEntryManager.addNotificationRemoveInterceptor(
Beverlyed8aea22020-01-22 16:52:47 -0500434 new NotificationRemoveInterceptor() {
435 @Override
Evan Laird04373662020-01-24 17:37:39 -0500436 public boolean onNotificationRemoveRequested(
Beverlya53fb0d2020-01-29 15:26:13 -0500437 String key,
438 NotificationEntry entry,
439 int dismissReason) {
440 final boolean isClearAll = dismissReason == REASON_CANCEL_ALL;
441 final boolean isUserDimiss = dismissReason == REASON_CANCEL
442 || dismissReason == REASON_CLICK;
443 final boolean isAppCancel = dismissReason == REASON_APP_CANCEL
444 || dismissReason == REASON_APP_CANCEL_ALL;
445 final boolean isSummaryCancel =
446 dismissReason == REASON_GROUP_SUMMARY_CANCELED;
447
448 // Need to check for !appCancel here because the notification may have
449 // previously been dismissed & entry.isRowDismissed would still be true
450 boolean userRemovedNotif =
451 (entry != null && entry.isRowDismissed() && !isAppCancel)
452 || isClearAll || isUserDimiss || isSummaryCancel;
453
454 if (userRemovedNotif || isUserCreatedBubble(key)
455 || isSummaryOfUserCreatedBubble(entry)) {
456 return handleDismissalInterception(entry);
457 }
458
459 return false;
Beverlyed8aea22020-01-22 16:52:47 -0500460 }
461 });
462
463 mNotificationGroupManager.addOnGroupChangeListener(
464 new NotificationGroupManager.OnGroupChangeListener() {
465 @Override
466 public void onGroupSuppressionChanged(
467 NotificationGroupManager.NotificationGroup group,
468 boolean suppressed) {
469 // More notifications could be added causing summary to no longer
470 // be suppressed -- in this case need to remove the key.
471 final String groupKey = group.summary != null
472 ? group.summary.getSbn().getGroupKey()
473 : null;
474 if (!suppressed && groupKey != null
475 && mBubbleData.isSummarySuppressed(groupKey)) {
476 mBubbleData.removeSuppressedSummary(groupKey);
477 }
478 }
479 });
480
481 addNotifCallback(new NotifCallback() {
482 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500483 public void removeNotification(NotificationEntry entry, int reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500484 mNotificationEntryManager.performRemoveNotification(entry.getSbn(),
Beverlya53fb0d2020-01-29 15:26:13 -0500485 reason);
Beverlyed8aea22020-01-22 16:52:47 -0500486 }
487
488 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500489 public void invalidateNotifications(String reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500490 mNotificationEntryManager.updateNotifications(reason);
491 }
492
493 @Override
494 public void maybeCancelSummary(NotificationEntry entry) {
495 // Check if removed bubble has an associated suppressed group summary that needs
496 // to be removed now.
Beverlya53fb0d2020-01-29 15:26:13 -0500497 final String groupKey = entry.getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500498 if (mBubbleData.isSummarySuppressed(groupKey)) {
Beverlya53fb0d2020-01-29 15:26:13 -0500499 mBubbleData.removeSuppressedSummary(groupKey);
Beverlyed8aea22020-01-22 16:52:47 -0500500
501 final NotificationEntry summary =
502 mNotificationEntryManager.getActiveNotificationUnfiltered(
503 mBubbleData.getSummaryKey(groupKey));
Beverlya53fb0d2020-01-29 15:26:13 -0500504 if (summary != null) {
505 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
506 UNDEFINED_DISMISS_REASON);
507 }
Beverlyed8aea22020-01-22 16:52:47 -0500508 }
509
Beverlya53fb0d2020-01-29 15:26:13 -0500510 // Check if we still need to remove the summary from NoManGroup because the summary
511 // may not be in the mBubbleData.mSuppressedGroupKeys list and removed above.
512 // For example:
513 // 1. Bubbled notifications (group) is posted to shade and are visible bubbles
514 // 2. User expands bubbles so now their respective notifications in the shade are
515 // hidden, including the group summary
516 // 3. User removes all bubbles
517 // 4. We expect all the removed bubbles AND the summary (note: the summary was
518 // never added to the suppressedSummary list in BubbleData, so we add this check)
Beverlyed8aea22020-01-22 16:52:47 -0500519 NotificationEntry summary =
520 mNotificationGroupManager.getLogicalGroupSummary(entry.getSbn());
521 if (summary != null) {
522 ArrayList<NotificationEntry> summaryChildren =
523 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
524 boolean isSummaryThisNotif = summary.getKey().equals(entry.getKey());
525 if (!isSummaryThisNotif && (summaryChildren == null
526 || summaryChildren.isEmpty())) {
527 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
528 UNDEFINED_DISMISS_REASON);
529 }
530 }
531 }
532 });
533 }
534
Beverlya53fb0d2020-01-29 15:26:13 -0500535 private void setupNotifPipeline() {
536 mNotifPipeline.addCollectionListener(new NotifCollectionListener() {
537 @Override
538 public void onEntryAdded(NotificationEntry entry) {
539 BubbleController.this.onEntryAdded(entry);
540 }
541
542 @Override
543 public void onEntryUpdated(NotificationEntry entry) {
544 BubbleController.this.onEntryUpdated(entry);
545 }
546
547 @Override
548 public void onRankingUpdate(RankingMap rankingMap) {
549 onRankingUpdated(rankingMap);
550 }
551
552 @Override
553 public void onEntryRemoved(NotificationEntry entry,
554 @NotifCollection.CancellationReason int reason) {
555 BubbleController.this.onEntryRemoved(entry);
556 }
557 });
558 }
559
Beverlyed8aea22020-01-22 16:52:47 -0500560 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800561 * Sets whether to perform inflation on the same thread as the caller. This method should only
562 * be used in tests, not in production.
563 */
564 @VisibleForTesting
565 void setInflateSynchronously(boolean inflateSynchronously) {
566 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800567 }
568
Lyn Hanb58c7562020-01-07 14:29:20 -0800569 void setOverflowCallback(Runnable updateOverflow) {
570 mOverflowCallback = updateOverflow;
571 }
572
573 /**
574 * @return Bubbles for updating overflow.
575 */
576 List<Bubble> getOverflowBubbles() {
577 return mBubbleData.getOverflowBubbles();
578 }
579
580
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400581 /**
582 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
583 * method initializes the stack view and adds it to the StatusBar just above the scrim.
584 */
585 private void ensureStackViewCreated() {
586 if (mStackView == null) {
587 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
wilsonshihe8321942019-10-18 18:39:46 +0800588 ViewGroup nsv = mNotificationShadeWindowController.getNotificationShadeView();
589 int bubbleScrimIndex = nsv.indexOfChild(nsv.findViewById(R.id.scrim_for_bubble));
Lyn Hanbde48202019-05-29 19:18:29 -0700590 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
wilsonshihe8321942019-10-18 18:39:46 +0800591 nsv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400592 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
593 if (mExpandListener != null) {
594 mStackView.setExpandListener(mExpandListener);
595 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400596 }
597 }
598
Mark Renoufc19b4732019-06-26 12:08:33 -0400599 /**
600 * Records the notification key for any active bubbles. These are used to restore active
601 * bubbles when the user returns to the foreground.
602 *
603 * @param userId the id of the user
604 */
605 private void saveBubbles(@UserIdInt int userId) {
606 // First clear any existing keys that might be stored.
607 mSavedBubbleKeysPerUser.remove(userId);
608 // Add in all active bubbles for the current user.
609 for (Bubble bubble: mBubbleData.getBubbles()) {
610 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
611 }
612 }
613
614 /**
615 * Promotes existing notifications to Bubbles if they were previously bubbles.
616 *
617 * @param userId the id of the user
618 */
619 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400620 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
621 if (savedBubbleKeys == null) {
622 // There were no bubbles saved for this used.
623 return;
624 }
Evan Laird181de622019-10-24 09:53:02 -0400625 for (NotificationEntry e :
626 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400627 if (savedBubbleKeys.contains(e.getKey())
Mark Renoufc19b4732019-06-26 12:08:33 -0400628 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
629 && canLaunchInActivityView(mContext, e)) {
630 updateBubble(e, /* suppressFlyout= */ true);
631 }
632 }
633 // Finally, remove the entries for this user now that bubbles are restored.
634 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
635 }
636
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700637 @Override
638 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800639 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700640 }
641
642 @Override
643 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800644 updateForThemeChanges();
645 }
646
647 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700648 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700649 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700650 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800651 mBubbleIconFactory = new BubbleIconFactory(mContext);
652 for (Bubble b: mBubbleData.getBubbles()) {
653 // Reload each bubble
654 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
655 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700656 }
657
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400658 @Override
659 public void onConfigChanged(Configuration newConfig) {
660 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400661 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700662 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400663 }
664 }
665
Mady Mellor5549dd22018-11-06 18:07:34 -0800666 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800667 * Set a listener to be notified when some states of the bubbles change.
668 */
669 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
670 mStateChangeListener = listener;
671 }
672
673 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800674 * Set a listener to be notified of bubble expand events.
675 */
676 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100677 mExpandListener = ((isExpanding, key) -> {
678 if (listener != null) {
679 listener.onBubbleExpandChanged(isExpanding, key);
680 }
wilsonshihe8321942019-10-18 18:39:46 +0800681 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100682 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800683 if (mStackView != null) {
684 mStackView.setExpandListener(mExpandListener);
685 }
686 }
687
688 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800689 * Whether or not there are bubbles present, regardless of them being visible on the
690 * screen (e.g. if on AOD).
691 */
692 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800693 if (mStackView == null) {
694 return false;
695 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400696 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800697 }
698
699 /**
700 * Whether the stack of bubbles is expanded or not.
701 */
702 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400703 return mBubbleData.isExpanded();
704 }
705
706 /**
707 * Tell the stack of bubbles to expand.
708 */
709 public void expandStack() {
710 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800711 }
712
713 /**
714 * Tell the stack of bubbles to collapse.
715 */
716 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400717 mBubbleData.setExpanded(false /* expanded */);
718 }
719
Mady Mellorce23c462019-06-17 17:30:07 -0700720 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700721 * True if either:
722 * (1) There is a bubble associated with the provided key and if its notification is hidden
723 * from the shade.
724 * (2) There is a group summary associated with the provided key that is hidden from the shade
725 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700726 *
Mady Mellore28fe102019-07-09 15:33:32 -0700727 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700728 */
Beverlyed8aea22020-01-22 16:52:47 -0500729 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
730 String key = entry.getKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700731 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800732 && !mBubbleData.getBubbleWithKey(key).showInShade();
Beverlyed8aea22020-01-22 16:52:47 -0500733
734 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700735 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700736 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Beverlyed8aea22020-01-22 16:52:47 -0500737
Mady Mellore4348272019-07-29 17:43:36 -0700738 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700739 }
740
Mark Renouf71a3af62019-04-08 15:02:54 -0400741 @VisibleForTesting
742 void selectBubble(String key) {
743 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellor247ca2c2019-12-02 16:18:59 -0800744 mBubbleData.setSelectedBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800745 }
746
Lyn Hanb58c7562020-01-07 14:29:20 -0800747 void promoteBubbleFromOverflow(Bubble bubble) {
Lyn Han1e19d7f2020-02-05 19:10:58 -0800748 bubble.setInflateSynchronously(mInflateSynchronously);
749 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
Lyn Hanb58c7562020-01-07 14:29:20 -0800750 }
751
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800752 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400753 * Request the stack expand if needed, then select the specified Bubble as current.
754 *
755 * @param notificationKey the notification key for the bubble to be selected
756 */
757 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400758 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
759 if (bubble != null) {
760 mBubbleData.setSelectedBubble(bubble);
761 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400762 }
763 }
764
765 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800766 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
767 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500768 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400769 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800770 }
771
772 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500773 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
774 * is forwarded a back key down/up pair.
775 */
776 public void performBackPressIfNeeded() {
777 if (mStackView != null) {
778 mStackView.performBackPressIfNeeded();
779 }
780 }
781
782 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800783 * Adds or updates a bubble associated with the provided notification entry.
784 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400785 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800786 */
Mark Renouff97ed462019-04-05 13:46:24 -0400787 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700788 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400789 }
790
791 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700792 updateBubble(notif, suppressFlyout, true /* showInShade */);
793 }
794
795 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800796 if (mStackView == null) {
797 // Lazy init stack view when a bubble is created
798 ensureStackViewCreated();
799 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700800 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400801 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700802 notif.setInterruption();
803 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800804 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
805 bubble.setInflateSynchronously(mInflateSynchronously);
806 bubble.inflate(
807 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
808 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700809 }
810
811 /**
812 * Called when a user has indicated that an active notification should be shown as a bubble.
813 * <p>
814 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
815 * the notification from appearing in the shade.
816 *
817 * @param entry the notification to show as a bubble.
818 */
819 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800820 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
821 Log.d(TAG, "onUserCreatedBubble: " + entry.getKey());
822 }
Heemin Seogba6337f2019-12-10 15:34:37 -0800823 mShadeController.collapsePanel(true);
Mady Mellor7f234902019-10-20 12:06:29 -0700824 entry.setFlagBubble(true);
825 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
Mady Mellorff076eb2019-11-13 10:12:06 -0800826 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800827 mUserBlockedBubbles.remove(entry.getKey());
Mady Mellor7f234902019-10-20 12:06:29 -0700828 }
829
830 /**
831 * Called when a user has indicated that an active notification appearing as a bubble should
832 * no longer be shown as a bubble.
833 *
834 * @param entry the notification to no longer show as a bubble.
835 */
836 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800837 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
838 Log.d(TAG, "onUserDemotedBubble: " + entry.getKey());
839 }
Mady Mellor7f234902019-10-20 12:06:29 -0700840 entry.setFlagBubble(false);
Beverlya53fb0d2020-01-29 15:26:13 -0500841 removeBubble(entry, DISMISS_BLOCKED);
Mady Mellorff076eb2019-11-13 10:12:06 -0800842 mUserCreatedBubbles.remove(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800843 if (BubbleExperimentConfig.isPackageWhitelistedToAutoBubble(
844 mContext, entry.getSbn().getPackageName())) {
845 // This package is whitelist but user demoted the bubble, let's save it so we don't
846 // auto-bubble for the whitelist again.
847 mUserBlockedBubbles.add(entry.getKey());
848 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800849 }
850
851 /**
852 * Whether this bubble was explicitly created by the user via a SysUI affordance.
853 */
854 boolean isUserCreatedBubble(String key) {
855 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800856 }
857
Beverlya53fb0d2020-01-29 15:26:13 -0500858 boolean isSummaryOfUserCreatedBubble(NotificationEntry entry) {
859 if (isSummaryOfBubbles(entry)) {
860 List<Bubble> bubbleChildren =
861 mBubbleData.getBubblesInGroup(entry.getSbn().getGroupKey());
862 for (int i = 0; i < bubbleChildren.size(); i++) {
863 // Check if any are user-created (i.e. experimental bubbles)
864 if (isUserCreatedBubble(bubbleChildren.get(i).getKey())) {
865 return true;
866 }
867 }
868 }
869 return false;
870 }
871
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800872 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500873 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500874 * <p>
875 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800876 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500877 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500878 void removeBubble(NotificationEntry entry, int reason) {
879 if (mBubbleData.hasBubbleWithKey(entry.getKey())) {
880 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800881 }
882 }
883
Beverlyed8aea22020-01-22 16:52:47 -0500884 private void onEntryAdded(NotificationEntry entry) {
885 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
886 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
887 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
888 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor22f2f072019-04-18 13:26:18 -0700889
Beverlyed8aea22020-01-22 16:52:47 -0500890 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
891 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
892 if (wasAdjusted && !previouslyUserCreated) {
893 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
894 mUserCreatedBubbles.add(entry.getKey());
Mady Mellorc2ff0112019-03-28 14:18:06 -0700895 }
Beverlyed8aea22020-01-22 16:52:47 -0500896 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700897 }
898 }
899
Beverlyed8aea22020-01-22 16:52:47 -0500900 private void onEntryUpdated(NotificationEntry entry) {
901 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
902 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
903 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
904 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700905
Beverlyed8aea22020-01-22 16:52:47 -0500906 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
907 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
908 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
909 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500910 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500911 } else if (shouldBubble) {
912 if (wasAdjusted && !previouslyUserCreated) {
913 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
914 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800915 }
Beverlyed8aea22020-01-22 16:52:47 -0500916 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800917 }
Beverlyed8aea22020-01-22 16:52:47 -0500918 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800919
Beverlya53fb0d2020-01-29 15:26:13 -0500920 private void onEntryRemoved(NotificationEntry entry) {
921 if (isSummaryOfBubbles(entry)) {
922 final String groupKey = entry.getSbn().getGroupKey();
923 mBubbleData.removeSuppressedSummary(groupKey);
924
925 // Remove any associated bubble children with the summary
926 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
927 for (int i = 0; i < bubbleChildren.size(); i++) {
928 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
929 }
930 } else {
931 removeBubble(entry, DISMISS_NOTIF_CANCEL);
932 }
933 }
934
Beverlyed8aea22020-01-22 16:52:47 -0500935 private void onRankingUpdated(RankingMap rankingMap) {
936 // Forward to BubbleData to block any bubbles which should no longer be shown
937 mBubbleData.notificationRankingUpdated(rankingMap);
938 }
Ned Burns01e38212019-01-03 16:32:52 -0500939
Mark Renouf71a3af62019-04-08 15:02:54 -0400940 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400941 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400942
Mark Renouf3bc5b362019-04-05 14:37:59 -0400943 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400944 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800945 // Update bubbles in overflow.
946 if (mOverflowCallback != null) {
947 mOverflowCallback.run();
948 }
949
Mark Renouf82a40e62019-05-23 16:16:24 -0400950 // Collapsing? Do this first before remaining steps.
951 if (update.expandedChanged && !update.expanded) {
952 mStackView.setExpanded(false);
953 }
954
955 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700956 ArrayList<Pair<Bubble, Integer>> removedBubbles =
957 new ArrayList<>(update.removedBubbles);
958 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400959 final Bubble bubble = removed.first;
960 @DismissReason final int reason = removed.second;
961 mStackView.removeBubble(bubble);
Mark Renoufc19b4732019-06-26 12:08:33 -0400962 // If the bubble is removed for user switching, leave the notification in place.
963 if (reason != DISMISS_USER_CHANGED) {
964 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800965 && !bubble.showInShade()) {
Beverlyed8aea22020-01-22 16:52:47 -0500966 // The bubble is now gone & the notification is hidden from the shade, so
967 // time to actually remove it
968 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -0500969 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -0500970 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400971 } else {
972 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400973 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700974
Mark Renoufc19b4732019-06-26 12:08:33 -0400975 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
976 // will get right result back
977 try {
978 mBarService.onNotificationBubbleChanged(bubble.getKey(),
979 false /* isBubble */);
980 } catch (RemoteException e) {
981 // Bad things have happened
982 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400983 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700984
Ned Burns00b4b2d2019-10-17 22:09:27 -0400985 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500986 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
987 // Time to potentially remove the summary
988 for (NotifCallback cb : mCallbacks) {
989 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -0700990 }
991 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700992 }
993 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400994
Lyn Hanc47e1712020-01-28 21:43:34 -0800995 if (update.addedBubble != null) {
996 mStackView.addBubble(update.addedBubble);
997 }
998
Mark Renouf82a40e62019-05-23 16:16:24 -0400999 if (update.updatedBubble != null) {
1000 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -04001001 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001002
Lyn Hanb58c7562020-01-07 14:29:20 -08001003 // At this point, the correct bubbles are inflated in the stack.
1004 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -04001005 if (update.orderChanged) {
1006 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -04001007 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001008
Mark Renouf82a40e62019-05-23 16:16:24 -04001009 if (update.selectionChanged) {
1010 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -07001011 if (update.selectedBubble != null) {
1012 mNotificationGroupManager.updateSuppression(
1013 update.selectedBubble.getEntry());
1014 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001015 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001016
Mark Renouf82a40e62019-05-23 16:16:24 -04001017 // Expanding? Apply this last.
1018 if (update.expandedChanged && update.expanded) {
1019 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001020 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001021
Beverlyed8aea22020-01-22 16:52:47 -05001022 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001023 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001024 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001025 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001026
Issei Suzukia8d07312019-06-07 12:56:19 +02001027 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001028 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001029 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001030 mBubbleData.getSelectedBubble()));
1031
1032 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001033 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001034 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001035 mStackView.getExpandedBubble()));
1036 }
Lyn Hanb58c7562020-01-07 14:29:20 -08001037 Log.d(TAG, "\n[BubbleData] overflow:");
1038 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
1039 null));
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001040 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001041 }
1042 };
1043
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001044 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001045 * We intercept notification entries (including group summaries) dismissed by the user when
1046 * there is an active bubble associated with it. We do this so that developers can still
1047 * cancel it (and hence the bubbles associated with it). However, these intercepted
1048 * notifications should then be hidden from the shade since the user has cancelled them, so we
1049 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1050 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001051 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001052 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001053 */
Beverlya53fb0d2020-01-29 15:26:13 -05001054 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001055 if (entry == null) {
1056 return false;
1057 }
Beverlyed8aea22020-01-22 16:52:47 -05001058
Beverlya53fb0d2020-01-29 15:26:13 -05001059 final boolean interceptBubbleDismissal = mBubbleData.hasBubbleWithKey(entry.getKey())
1060 && entry.isBubble();
1061 final boolean interceptSummaryDismissal = isSummaryOfBubbles(entry);
Beverlyed8aea22020-01-22 16:52:47 -05001062
Beverlya53fb0d2020-01-29 15:26:13 -05001063 if (interceptSummaryDismissal) {
1064 handleSummaryDismissalInterception(entry);
1065 } else if (interceptBubbleDismissal) {
1066 Bubble bubble = mBubbleData.getBubbleWithKey(entry.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001067 bubble.setSuppressNotification(true);
1068 bubble.setShowDot(false /* show */, true /* animate */);
Beverlya53fb0d2020-01-29 15:26:13 -05001069 } else {
Beverlyed8aea22020-01-22 16:52:47 -05001070 return false;
1071 }
Beverlya53fb0d2020-01-29 15:26:13 -05001072
1073 // Update the shade
1074 for (NotifCallback cb : mCallbacks) {
1075 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1076 }
1077 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001078 }
1079
Beverlya53fb0d2020-01-29 15:26:13 -05001080 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1081 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001082 return false;
1083 }
Beverlya53fb0d2020-01-29 15:26:13 -05001084
1085 String groupKey = entry.getSbn().getGroupKey();
1086 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1087 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1088 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1089 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1090 return (isSuppressedSummary || isSummary)
1091 && bubbleChildren != null
1092 && !bubbleChildren.isEmpty();
1093 }
1094
1095 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1096 // current children in the row:
1097 final List<NotificationEntry> children = summary.getChildren();
1098 if (children != null) {
1099 for (int i = 0; i < children.size(); i++) {
1100 NotificationEntry child = children.get(i);
1101 if (mBubbleData.hasBubbleWithKey(child.getKey())) {
1102 // Suppress the bubbled child
1103 // As far as group manager is concerned, once a child is no longer shown
1104 // in the shade, it is essentially removed.
1105 Bubble bubbleChild = mBubbleData.getBubbleWithKey(child.getKey());
1106 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1107 bubbleChild.setSuppressNotification(true);
1108 bubbleChild.setShowDot(false /* show */, true /* animate */);
1109 } else {
1110 // non-bubbled children can be removed
1111 for (NotifCallback cb : mCallbacks) {
1112 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1113 }
1114 }
1115 }
1116 }
1117
1118 // And since all children are removed, remove the summary.
1119 mNotificationGroupManager.onEntryRemoved(summary);
1120
1121 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1122 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1123 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001124 }
1125
1126 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001127 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001128 * Updates the visibility of the bubbles based on current state.
1129 * Does not un-bubble, just hides or un-hides. Notifies any
1130 * {@link BubbleStateChangeListener}s of visibility changes.
1131 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001132 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001133 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001134 if (mStackView == null) {
1135 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001136 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001137 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1138 // Bubbles only appear in unlocked shade
1139 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001140 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001141 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001142 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001143
Mady Mellor698d9e82019-08-01 23:11:53 +00001144 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001145 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001146 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001147 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001148 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1149 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1150 }
1151
1152 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001153 }
1154
1155 /**
1156 * Rect indicating the touchable region for the bubble stack / expanded stack.
1157 */
1158 public Rect getTouchableRegion() {
1159 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1160 return null;
1161 }
1162 mStackView.getBoundsOnScreen(mTempRect);
1163 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001164 }
1165
Mady Mellor390bff42019-04-05 15:09:01 -07001166 /**
1167 * The display id of the expanded view, if the stack is expanded and not occluded by the
1168 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1169 */
1170 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001171 final Bubble bubble = getExpandedBubble(context);
1172 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
1173 }
1174
1175 @Nullable
1176 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001177 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001178 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001179 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001180 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001181 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +02001182 final Bubble expandedBubble = mStackView.getExpandedBubble();
1183 if (defaultDisplay && expandedBubble != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001184 && !mNotificationShadeWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001185 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -07001186 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001187 return null;
Mady Mellor390bff42019-04-05 15:09:01 -07001188 }
1189
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001190 @VisibleForTesting
1191 BubbleStackView getStackView() {
1192 return mStackView;
1193 }
1194
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001195 /**
1196 * Description of current bubble state.
1197 */
1198 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1199 pw.println("BubbleController state:");
1200 mBubbleData.dump(fd, pw, args);
1201 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001202 if (mStackView != null) {
1203 mStackView.dump(fd, pw, args);
1204 }
1205 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001206 }
1207
Mady Mellore80930e2019-03-21 16:00:45 -07001208 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001209 * This task stack listener is responsible for responding to tasks moved to the front
1210 * which are on the default (main) display. When this happens, expanded bubbles must be
1211 * collapsed so the user may interact with the app which was just moved to the front.
1212 * <p>
1213 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1214 * these calls via a main thread Handler.
1215 */
1216 @MainThread
1217 private class BubbleTaskStackListener extends TaskStackChangeListener {
1218
Mark Renoufcecc77b2019-01-30 16:32:24 -05001219 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001220 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1221 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001222 if (!mStackView.isExpansionAnimating()) {
1223 mBubbleData.setExpanded(false);
1224 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001225 }
1226 }
1227
Mark Renoufcecc77b2019-01-30 16:32:24 -05001228 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001229 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001230 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001231 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001232 }
1233 }
Mark Renouf446251d2019-04-26 10:22:41 -04001234
1235 @Override
1236 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1237 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1238 mBubbleData.setExpanded(false);
1239 }
1240 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001241
1242 @Override
1243 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001244 if (mStackView == null) {
1245 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001246 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001247 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001248 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001249
1250 @Override
1251 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001252 final Bubble expandedBubble = mStackView != null
1253 ? mStackView.getExpandedBubble()
1254 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001255 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1256 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001257 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001258 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001259 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001260 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001261 }
1262
Mady Mellorca0c24c2019-05-16 16:14:32 -07001263 /**
1264 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1265 *
1266 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1267 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1268 *
1269 * @param context the context to use.
1270 * @param entry the entry to bubble.
1271 */
1272 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1273 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001274 ? entry.getBubbleMetadata().getBubbleIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001275 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001276 if (entry.getBubbleMetadata() != null
1277 && entry.getBubbleMetadata().getShortcutId() != null) {
1278 return true;
1279 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001280 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001281 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001282 return false;
1283 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001284 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1285 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001286 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001287 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001288 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001289 Log.w(TAG, "Unable to send as bubble, "
1290 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001291 + intent);
1292 return false;
1293 }
1294 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001295 Log.w(TAG, "Unable to send as bubble, "
1296 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001297 + intent);
1298 return false;
1299 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001300 return true;
1301 }
1302
Joshua Tsujia19515f2019-02-13 18:02:29 -05001303 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001304 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001305 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001306 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1307 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1308 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001309 }
1310 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001311 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001312}