blob: 67299a67e276df633a29083f57a210cd7018b5ef [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;
Beverlya53fb0d2020-01-29 15:26:13 -050072import com.android.systemui.Dumpable;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080073import com.android.systemui.R;
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +010074import com.android.systemui.bubbles.dagger.BubbleModule;
Ned Burnsaaeb44b2020-02-12 23:48:26 -050075import com.android.systemui.dump.DumpManager;
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;
Beverlya53fb0d2020-01-29 15:26:13 -050086import com.android.systemui.statusbar.notification.collection.NotifCollection;
87import com.android.systemui.statusbar.notification.collection.NotifPipeline;
Ned Burnsf81c4c42019-01-07 14:10:43 -050088import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Beverlya53fb0d2020-01-29 15:26:13 -050089import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener;
Beverlye73aea22020-03-03 17:55:47 -050090import com.android.systemui.statusbar.notification.interruption.NotificationInterruptStateProvider;
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;
Joshua Tsuji7155bf12020-02-13 16:14:29 -050097import com.android.systemui.util.FloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080098
Mady Mellor70cba7bb2019-07-02 15:06:07 -070099import java.io.FileDescriptor;
100import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500101import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -0400102import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -0700103import java.util.ArrayList;
Mady Mellorff076eb2019-11-13 10:12:06 -0800104import java.util.HashSet;
Lyn Hanb58c7562020-01-07 14:29:20 -0800105import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500106
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800107/**
108 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
109 * Bubbles can be expanded to show more content.
110 *
111 * The controller manages addition, removal, and visible state of bubbles on screen.
112 */
Beverlya53fb0d2020-01-29 15:26:13 -0500113public class BubbleController implements ConfigurationController.ConfigurationListener, Dumpable {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800114
Issei Suzukia8d07312019-06-07 12:56:19 +0200115 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800116
Mark Renouf08bc42a2019-03-07 13:01:59 -0500117 @Retention(SOURCE)
118 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400119 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700120 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400121 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500122 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700123
Mark Renouf08bc42a2019-03-07 13:01:59 -0500124 static final int DISMISS_USER_GESTURE = 1;
125 static final int DISMISS_AGED = 2;
126 static final int DISMISS_TASK_FINISHED = 3;
127 static final int DISMISS_BLOCKED = 4;
128 static final int DISMISS_NOTIF_CANCEL = 5;
129 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700130 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400131 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700132 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700133 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500134
Ned Burns01e38212019-01-03 16:32:52 -0500135 private final Context mContext;
136 private final NotificationEntryManager mNotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500137 private final NotifPipeline mNotifPipeline;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500138 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800139 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800140 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100141 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700142 private final NotificationGroupManager mNotificationGroupManager;
Heemin Seogba6337f2019-12-10 15:34:37 -0800143 private final ShadeController mShadeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500144 private final FloatingContentCoordinator mFloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800145
Mady Mellor3dff9e62019-02-05 18:12:53 -0800146 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400147 @Nullable private BubbleStackView mStackView;
Mady Mellor247ca2c2019-12-02 16:18:59 -0800148 private BubbleIconFactory mBubbleIconFactory;
Lyn Hanb58c7562020-01-07 14:29:20 -0800149 private int mMaxBubbles;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800150
Mark Renoufc19b4732019-06-26 12:08:33 -0400151 // Tracks the id of the current (foreground) user.
152 private int mCurrentUserId;
153 // Saves notification keys of active bubbles when users are switched.
154 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
155
Mady Mellorff076eb2019-11-13 10:12:06 -0800156 // Saves notification keys of user created "fake" bubbles so that we can allow notifications
157 // like these to bubble by default. Doesn't persist across reboots, not a long-term solution.
158 private final HashSet<String> mUserCreatedBubbles;
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800159 // If we're auto-bubbling bubbles via a whitelist, we need to track which notifs from that app
160 // have been "demoted" back to a notification so that we don't auto-bubbles those again.
161 // Doesn't persist across reboots, not a long-term solution.
162 private final HashSet<String> mUserBlockedBubbles;
Mady Mellorff076eb2019-11-13 10:12:06 -0800163
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800164 // Bubbles get added to the status bar view
wilsonshihe8321942019-10-18 18:39:46 +0800165 private final NotificationShadeWindowController mNotificationShadeWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400166 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800167 private StatusBarStateListener mStatusBarStateListener;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500168
Lyn Hanb58c7562020-01-07 14:29:20 -0800169 // Callback that updates BubbleOverflowActivity on data change.
170 @Nullable private Runnable mOverflowCallback = null;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800171
Beverlye73aea22020-03-03 17:55:47 -0500172 private final NotificationInterruptStateProvider mNotificationInterruptStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700173 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800174
Mady Mellord1c78b262018-11-06 18:04:40 -0800175 // Used for determining view rect for touch interaction
176 private Rect mTempRect = new Rect();
177
Mark Renoufc19b4732019-06-26 12:08:33 -0400178 // Listens to user switch so bubbles can be saved and restored.
179 private final NotificationLockscreenUserManager mNotifUserManager;
180
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400181 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
182 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
183
Mady Mellor3df7ab02019-12-09 15:07:10 -0800184 private boolean mInflateSynchronously;
185
Beverlyed8aea22020-01-22 16:52:47 -0500186 // TODO (b/145659174): allow for multiple callbacks to support the "shadow" new notif pipeline
187 private final List<NotifCallback> mCallbacks = new ArrayList<>();
188
Mady Mellor5549dd22018-11-06 18:07:34 -0800189 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800190 * Listener to be notified when some states of the bubbles change.
191 */
192 public interface BubbleStateChangeListener {
193 /**
194 * Called when the stack has bubbles or no longer has bubbles.
195 */
196 void onHasBubblesChanged(boolean hasBubbles);
197 }
198
Mady Mellorcd9b1302018-11-06 18:08:04 -0800199 /**
200 * Listener to find out about stack expansion / collapse events.
201 */
202 public interface BubbleExpandListener {
203 /**
204 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700205 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800206 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800207 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800208 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800209 void onBubbleExpandChanged(boolean isExpanding, String key);
210 }
211
212 /**
Mady Mellorf44b6832020-01-14 13:26:14 -0800213 * Listener to be notified when a bubbles' notification suppression state changes.
214 */
215 public interface NotificationSuppressionChangedListener {
216 /**
217 * Called when the notification suppression state of a bubble changes.
218 */
219 void onBubbleNotificationSuppressionChange(Bubble bubble);
Beverlyed8aea22020-01-22 16:52:47 -0500220 }
Mady Mellorf44b6832020-01-14 13:26:14 -0800221
Beverlyed8aea22020-01-22 16:52:47 -0500222 /**
223 * Callback for when the BubbleController wants to interact with the notification pipeline to:
224 * - Remove a previously bubbled notification
225 * - Update the notification shade since bubbled notification should/shouldn't be showing
226 */
227 public interface NotifCallback {
228 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500229 * Called when a bubbled notification that was hidden from the shade is now being removed
230 * This can happen when an app cancels a bubbled notification or when the user dismisses a
231 * bubble.
Beverlyed8aea22020-01-22 16:52:47 -0500232 */
Beverlya53fb0d2020-01-29 15:26:13 -0500233 void removeNotification(NotificationEntry entry, int reason);
Beverlyed8aea22020-01-22 16:52:47 -0500234
235 /**
236 * Called when a bubbled notification has changed whether it should be
237 * filtered from the shade.
238 */
Beverlya53fb0d2020-01-29 15:26:13 -0500239 void invalidateNotifications(String reason);
Beverlyed8aea22020-01-22 16:52:47 -0500240
241 /**
242 * Called on a bubbled entry that has been removed when there are no longer
243 * bubbled entries in its group.
244 *
245 * Checks whether its group has any other (non-bubbled) children. If it doesn't,
246 * removes all remnants of the group's summary from the notification pipeline.
247 * TODO: (b/145659174) Only old pipeline needs this - delete post-migration.
248 */
249 void maybeCancelSummary(NotificationEntry entry);
Mady Mellorf44b6832020-01-14 13:26:14 -0800250 }
251
252 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800253 * Listens for the current state of the status bar and updates the visibility state
254 * of bubbles as needed.
255 */
256 private class StatusBarStateListener implements StatusBarStateController.StateListener {
257 private int mState;
258 /**
259 * Returns the current status bar state.
260 */
261 public int getCurrentState() {
262 return mState;
263 }
264
265 @Override
266 public void onStateChanged(int newState) {
267 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400268 boolean shouldCollapse = (mState != SHADE);
269 if (shouldCollapse) {
270 collapseStack();
271 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700272 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800273 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800274 }
275
Mady Mellor7f234902019-10-20 12:06:29 -0700276 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800277 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700278 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800279 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700280 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700281 ConfigurationController configurationController,
Beverlye73aea22020-03-03 17:55:47 -0500282 NotificationInterruptStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400283 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700284 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700285 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500286 NotificationEntryManager entryManager,
287 NotifPipeline notifPipeline,
288 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500289 DumpManager dumpManager,
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500290 FloatingContentCoordinator floatingContentCoordinator) {
wilsonshihe8321942019-10-18 18:39:46 +0800291 this(context, notificationShadeWindowController, statusBarStateController, shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700292 data, null /* synchronizer */, configurationController, interruptionStateProvider,
Beverlya53fb0d2020-01-29 15:26:13 -0500293 zenModeController, notifUserManager, groupManager, entryManager,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500294 notifPipeline, featureFlags, dumpManager, floatingContentCoordinator);
Mady Mellor7f234902019-10-20 12:06:29 -0700295 }
296
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +0100297 /**
298 * Injected constructor. See {@link BubbleModule}.
299 */
Mady Mellor7f234902019-10-20 12:06:29 -0700300 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800301 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700302 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800303 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700304 BubbleData data,
305 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
306 ConfigurationController configurationController,
Beverlye73aea22020-03-03 17:55:47 -0500307 NotificationInterruptStateProvider interruptionStateProvider,
Mady Mellor7f234902019-10-20 12:06:29 -0700308 ZenModeController zenModeController,
309 NotificationLockscreenUserManager notifUserManager,
310 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500311 NotificationEntryManager entryManager,
312 NotifPipeline notifPipeline,
313 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500314 DumpManager dumpManager,
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500315 FloatingContentCoordinator floatingContentCoordinator) {
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500316 dumpManager.registerDumpable(TAG, this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800317 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800318 mShadeController = shadeController;
Beverlye73aea22020-03-03 17:55:47 -0500319 mNotificationInterruptStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400320 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400321 mZenModeController = zenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500322 mFloatingContentCoordinator = floatingContentCoordinator;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400323 mZenModeController.addCallback(new ZenModeController.Callback() {
324 @Override
325 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800326 for (Bubble b : mBubbleData.getBubbles()) {
327 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700328 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400329 }
330
331 @Override
332 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800333 for (Bubble b : mBubbleData.getBubbles()) {
334 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700335 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400336 }
337 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700338
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700339 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800340
Mady Mellorf44b6832020-01-14 13:26:14 -0800341 mMaxBubbles = mContext.getResources().getInteger(R.integer.bubbles_max_rendered);
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400342 mBubbleData = data;
343 mBubbleData.setListener(mBubbleDataListener);
Mady Mellorf44b6832020-01-14 13:26:14 -0800344 mBubbleData.setSuppressionChangedListener(new NotificationSuppressionChangedListener() {
345 @Override
346 public void onBubbleNotificationSuppressionChange(Bubble bubble) {
347 // Make sure NoMan knows it's not showing in the shade anymore so anyone querying it
348 // can tell.
349 try {
350 mBarService.onBubbleNotificationSuppressionChanged(bubble.getKey(),
351 !bubble.showInShade());
352 } catch (RemoteException e) {
353 // Bad things have happened
354 }
355 }
356 });
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400357
Mady Mellor7f234902019-10-20 12:06:29 -0700358 mNotificationEntryManager = entryManager;
Mady Mellor22f2f072019-04-18 13:26:18 -0700359 mNotificationGroupManager = groupManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500360 mNotifPipeline = notifPipeline;
361
362 if (!featureFlags.isNewNotifPipelineRenderingEnabled()) {
363 setupNEM();
364 } else {
365 setupNotifPipeline();
366 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800367
wilsonshihe8321942019-10-18 18:39:46 +0800368 mNotificationShadeWindowController = notificationShadeWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800369 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700370 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500371
Mark Renoufcecc77b2019-01-30 16:32:24 -0500372 mTaskStackListener = new BubbleTaskStackListener();
373 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800374
Joshua Tsujia19515f2019-02-13 18:02:29 -0500375 try {
376 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
377 } catch (RemoteException e) {
378 e.printStackTrace();
379 }
Issei Suzukic0387542019-03-08 17:31:14 +0100380 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700381
382 mBarService = IStatusBarService.Stub.asInterface(
383 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400384
385 mSavedBubbleKeysPerUser = new SparseSetArray<>();
386 mCurrentUserId = mNotifUserManager.getCurrentUserId();
387 mNotifUserManager.addUserChangedListener(
Steve Elliottb47f1c72019-12-19 12:39:26 -0500388 new NotificationLockscreenUserManager.UserChangedListener() {
389 @Override
390 public void onUserChanged(int newUserId) {
391 BubbleController.this.saveBubbles(mCurrentUserId);
392 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
393 BubbleController.this.restoreBubbles(newUserId);
394 mCurrentUserId = newUserId;
395 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400396 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800397
398 mUserCreatedBubbles = new HashSet<>();
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800399 mUserBlockedBubbles = new HashSet<>();
Aran Inkaa4dfa72019-11-18 16:49:07 -0500400
Mady Mellor247ca2c2019-12-02 16:18:59 -0800401 mBubbleIconFactory = new BubbleIconFactory(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800402 }
403
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400404 /**
Beverlyed8aea22020-01-22 16:52:47 -0500405 * See {@link NotifCallback}.
406 */
407 public void addNotifCallback(NotifCallback callback) {
408 mCallbacks.add(callback);
409 }
410
411 private void setupNEM() {
412 mNotificationEntryManager.addNotificationEntryListener(
413 new NotificationEntryListener() {
414 @Override
Mady Mellorf9439ab2020-01-30 16:06:53 -0800415 public void onPendingEntryAdded(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -0500416 onEntryAdded(entry);
417 }
418
419 @Override
420 public void onPreEntryUpdated(NotificationEntry entry) {
421 onEntryUpdated(entry);
422 }
423
424 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500425 public void onEntryRemoved(
426 NotificationEntry entry,
427 @android.annotation.Nullable NotificationVisibility visibility,
Julia Reynolds138111f2020-02-26 11:17:39 -0500428 boolean removedByUser,
429 int reason) {
Beverlya53fb0d2020-01-29 15:26:13 -0500430 BubbleController.this.onEntryRemoved(entry);
431 }
432
433 @Override
Beverlyed8aea22020-01-22 16:52:47 -0500434 public void onNotificationRankingUpdated(RankingMap rankingMap) {
435 onRankingUpdated(rankingMap);
436 }
437 });
438
Evan Laird04373662020-01-24 17:37:39 -0500439 mNotificationEntryManager.addNotificationRemoveInterceptor(
Beverlyed8aea22020-01-22 16:52:47 -0500440 new NotificationRemoveInterceptor() {
441 @Override
Evan Laird04373662020-01-24 17:37:39 -0500442 public boolean onNotificationRemoveRequested(
Beverlya53fb0d2020-01-29 15:26:13 -0500443 String key,
444 NotificationEntry entry,
445 int dismissReason) {
446 final boolean isClearAll = dismissReason == REASON_CANCEL_ALL;
447 final boolean isUserDimiss = dismissReason == REASON_CANCEL
448 || dismissReason == REASON_CLICK;
449 final boolean isAppCancel = dismissReason == REASON_APP_CANCEL
450 || dismissReason == REASON_APP_CANCEL_ALL;
451 final boolean isSummaryCancel =
452 dismissReason == REASON_GROUP_SUMMARY_CANCELED;
453
454 // Need to check for !appCancel here because the notification may have
455 // previously been dismissed & entry.isRowDismissed would still be true
456 boolean userRemovedNotif =
457 (entry != null && entry.isRowDismissed() && !isAppCancel)
458 || isClearAll || isUserDimiss || isSummaryCancel;
459
460 if (userRemovedNotif || isUserCreatedBubble(key)
461 || isSummaryOfUserCreatedBubble(entry)) {
462 return handleDismissalInterception(entry);
463 }
464
465 return false;
Beverlyed8aea22020-01-22 16:52:47 -0500466 }
467 });
468
469 mNotificationGroupManager.addOnGroupChangeListener(
470 new NotificationGroupManager.OnGroupChangeListener() {
471 @Override
472 public void onGroupSuppressionChanged(
473 NotificationGroupManager.NotificationGroup group,
474 boolean suppressed) {
475 // More notifications could be added causing summary to no longer
476 // be suppressed -- in this case need to remove the key.
477 final String groupKey = group.summary != null
478 ? group.summary.getSbn().getGroupKey()
479 : null;
480 if (!suppressed && groupKey != null
481 && mBubbleData.isSummarySuppressed(groupKey)) {
482 mBubbleData.removeSuppressedSummary(groupKey);
483 }
484 }
485 });
486
487 addNotifCallback(new NotifCallback() {
488 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500489 public void removeNotification(NotificationEntry entry, int reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500490 mNotificationEntryManager.performRemoveNotification(entry.getSbn(),
Beverlya53fb0d2020-01-29 15:26:13 -0500491 reason);
Beverlyed8aea22020-01-22 16:52:47 -0500492 }
493
494 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500495 public void invalidateNotifications(String reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500496 mNotificationEntryManager.updateNotifications(reason);
497 }
498
499 @Override
500 public void maybeCancelSummary(NotificationEntry entry) {
501 // Check if removed bubble has an associated suppressed group summary that needs
502 // to be removed now.
Beverlya53fb0d2020-01-29 15:26:13 -0500503 final String groupKey = entry.getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500504 if (mBubbleData.isSummarySuppressed(groupKey)) {
Beverlya53fb0d2020-01-29 15:26:13 -0500505 mBubbleData.removeSuppressedSummary(groupKey);
Beverlyed8aea22020-01-22 16:52:47 -0500506
507 final NotificationEntry summary =
508 mNotificationEntryManager.getActiveNotificationUnfiltered(
509 mBubbleData.getSummaryKey(groupKey));
Beverlya53fb0d2020-01-29 15:26:13 -0500510 if (summary != null) {
511 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
512 UNDEFINED_DISMISS_REASON);
513 }
Beverlyed8aea22020-01-22 16:52:47 -0500514 }
515
Beverlya53fb0d2020-01-29 15:26:13 -0500516 // Check if we still need to remove the summary from NoManGroup because the summary
517 // may not be in the mBubbleData.mSuppressedGroupKeys list and removed above.
518 // For example:
519 // 1. Bubbled notifications (group) is posted to shade and are visible bubbles
520 // 2. User expands bubbles so now their respective notifications in the shade are
521 // hidden, including the group summary
522 // 3. User removes all bubbles
523 // 4. We expect all the removed bubbles AND the summary (note: the summary was
524 // never added to the suppressedSummary list in BubbleData, so we add this check)
Beverlyed8aea22020-01-22 16:52:47 -0500525 NotificationEntry summary =
526 mNotificationGroupManager.getLogicalGroupSummary(entry.getSbn());
527 if (summary != null) {
528 ArrayList<NotificationEntry> summaryChildren =
529 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
530 boolean isSummaryThisNotif = summary.getKey().equals(entry.getKey());
531 if (!isSummaryThisNotif && (summaryChildren == null
532 || summaryChildren.isEmpty())) {
533 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
534 UNDEFINED_DISMISS_REASON);
535 }
536 }
537 }
538 });
539 }
540
Beverlya53fb0d2020-01-29 15:26:13 -0500541 private void setupNotifPipeline() {
542 mNotifPipeline.addCollectionListener(new NotifCollectionListener() {
543 @Override
544 public void onEntryAdded(NotificationEntry entry) {
545 BubbleController.this.onEntryAdded(entry);
546 }
547
548 @Override
549 public void onEntryUpdated(NotificationEntry entry) {
550 BubbleController.this.onEntryUpdated(entry);
551 }
552
553 @Override
554 public void onRankingUpdate(RankingMap rankingMap) {
555 onRankingUpdated(rankingMap);
556 }
557
558 @Override
559 public void onEntryRemoved(NotificationEntry entry,
560 @NotifCollection.CancellationReason int reason) {
561 BubbleController.this.onEntryRemoved(entry);
562 }
563 });
564 }
565
Beverlyed8aea22020-01-22 16:52:47 -0500566 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800567 * Sets whether to perform inflation on the same thread as the caller. This method should only
568 * be used in tests, not in production.
569 */
570 @VisibleForTesting
571 void setInflateSynchronously(boolean inflateSynchronously) {
572 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800573 }
574
Lyn Hanb58c7562020-01-07 14:29:20 -0800575 void setOverflowCallback(Runnable updateOverflow) {
576 mOverflowCallback = updateOverflow;
577 }
578
579 /**
580 * @return Bubbles for updating overflow.
581 */
582 List<Bubble> getOverflowBubbles() {
583 return mBubbleData.getOverflowBubbles();
584 }
585
586
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400587 /**
588 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
589 * method initializes the stack view and adds it to the StatusBar just above the scrim.
590 */
591 private void ensureStackViewCreated() {
592 if (mStackView == null) {
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500593 mStackView = new BubbleStackView(
594 mContext, mBubbleData, mSurfaceSynchronizer, mFloatingContentCoordinator);
wilsonshihe8321942019-10-18 18:39:46 +0800595 ViewGroup nsv = mNotificationShadeWindowController.getNotificationShadeView();
596 int bubbleScrimIndex = nsv.indexOfChild(nsv.findViewById(R.id.scrim_for_bubble));
Lyn Hanbde48202019-05-29 19:18:29 -0700597 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
wilsonshihe8321942019-10-18 18:39:46 +0800598 nsv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400599 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
600 if (mExpandListener != null) {
601 mStackView.setExpandListener(mExpandListener);
602 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400603 }
604 }
605
Mark Renoufc19b4732019-06-26 12:08:33 -0400606 /**
607 * Records the notification key for any active bubbles. These are used to restore active
608 * bubbles when the user returns to the foreground.
609 *
610 * @param userId the id of the user
611 */
612 private void saveBubbles(@UserIdInt int userId) {
613 // First clear any existing keys that might be stored.
614 mSavedBubbleKeysPerUser.remove(userId);
615 // Add in all active bubbles for the current user.
616 for (Bubble bubble: mBubbleData.getBubbles()) {
617 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
618 }
619 }
620
621 /**
622 * Promotes existing notifications to Bubbles if they were previously bubbles.
623 *
624 * @param userId the id of the user
625 */
626 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400627 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
628 if (savedBubbleKeys == null) {
629 // There were no bubbles saved for this used.
630 return;
631 }
Evan Laird181de622019-10-24 09:53:02 -0400632 for (NotificationEntry e :
633 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400634 if (savedBubbleKeys.contains(e.getKey())
Beverlye73aea22020-03-03 17:55:47 -0500635 && mNotificationInterruptStateProvider.shouldBubbleUp(e)
Mark Renoufc19b4732019-06-26 12:08:33 -0400636 && canLaunchInActivityView(mContext, e)) {
637 updateBubble(e, /* suppressFlyout= */ true);
638 }
639 }
640 // Finally, remove the entries for this user now that bubbles are restored.
641 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
642 }
643
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700644 @Override
645 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800646 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700647 }
648
649 @Override
650 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800651 updateForThemeChanges();
652 }
653
654 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700655 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700656 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700657 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800658 mBubbleIconFactory = new BubbleIconFactory(mContext);
659 for (Bubble b: mBubbleData.getBubbles()) {
660 // Reload each bubble
661 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
662 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700663 }
664
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400665 @Override
666 public void onConfigChanged(Configuration newConfig) {
667 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400668 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700669 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400670 }
671 }
672
Mady Mellor5549dd22018-11-06 18:07:34 -0800673 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800674 * Set a listener to be notified when some states of the bubbles change.
675 */
676 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
677 mStateChangeListener = listener;
678 }
679
680 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800681 * Set a listener to be notified of bubble expand events.
682 */
683 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100684 mExpandListener = ((isExpanding, key) -> {
685 if (listener != null) {
686 listener.onBubbleExpandChanged(isExpanding, key);
687 }
wilsonshihe8321942019-10-18 18:39:46 +0800688 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100689 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800690 if (mStackView != null) {
691 mStackView.setExpandListener(mExpandListener);
692 }
693 }
694
695 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800696 * Whether or not there are bubbles present, regardless of them being visible on the
697 * screen (e.g. if on AOD).
698 */
699 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800700 if (mStackView == null) {
701 return false;
702 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400703 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800704 }
705
706 /**
707 * Whether the stack of bubbles is expanded or not.
708 */
709 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400710 return mBubbleData.isExpanded();
711 }
712
713 /**
714 * Tell the stack of bubbles to expand.
715 */
716 public void expandStack() {
717 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800718 }
719
720 /**
721 * Tell the stack of bubbles to collapse.
722 */
723 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400724 mBubbleData.setExpanded(false /* expanded */);
725 }
726
Mady Mellorce23c462019-06-17 17:30:07 -0700727 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700728 * True if either:
729 * (1) There is a bubble associated with the provided key and if its notification is hidden
730 * from the shade.
731 * (2) There is a group summary associated with the provided key that is hidden from the shade
732 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700733 *
Mady Mellore28fe102019-07-09 15:33:32 -0700734 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700735 */
Beverlyed8aea22020-01-22 16:52:47 -0500736 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
737 String key = entry.getKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700738 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800739 && !mBubbleData.getBubbleWithKey(key).showInShade();
Beverlyed8aea22020-01-22 16:52:47 -0500740
741 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700742 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700743 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Beverlyed8aea22020-01-22 16:52:47 -0500744
Mady Mellore4348272019-07-29 17:43:36 -0700745 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700746 }
747
Mark Renouf71a3af62019-04-08 15:02:54 -0400748 @VisibleForTesting
749 void selectBubble(String key) {
750 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellor247ca2c2019-12-02 16:18:59 -0800751 mBubbleData.setSelectedBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800752 }
753
Lyn Hanb58c7562020-01-07 14:29:20 -0800754 void promoteBubbleFromOverflow(Bubble bubble) {
Lyn Han1e19d7f2020-02-05 19:10:58 -0800755 bubble.setInflateSynchronously(mInflateSynchronously);
756 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
Lyn Hanb58c7562020-01-07 14:29:20 -0800757 }
758
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800759 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400760 * Request the stack expand if needed, then select the specified Bubble as current.
761 *
762 * @param notificationKey the notification key for the bubble to be selected
763 */
764 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400765 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
766 if (bubble != null) {
767 mBubbleData.setSelectedBubble(bubble);
768 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400769 }
770 }
771
772 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800773 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
774 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500775 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400776 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800777 }
778
779 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500780 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
781 * is forwarded a back key down/up pair.
782 */
783 public void performBackPressIfNeeded() {
784 if (mStackView != null) {
785 mStackView.performBackPressIfNeeded();
786 }
787 }
788
789 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800790 * Adds or updates a bubble associated with the provided notification entry.
791 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400792 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800793 */
Mark Renouff97ed462019-04-05 13:46:24 -0400794 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700795 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400796 }
797
798 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700799 updateBubble(notif, suppressFlyout, true /* showInShade */);
800 }
801
802 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800803 if (mStackView == null) {
804 // Lazy init stack view when a bubble is created
805 ensureStackViewCreated();
806 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700807 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400808 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700809 notif.setInterruption();
810 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800811 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
812 bubble.setInflateSynchronously(mInflateSynchronously);
813 bubble.inflate(
814 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
815 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700816 }
817
818 /**
819 * Called when a user has indicated that an active notification should be shown as a bubble.
820 * <p>
821 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
822 * the notification from appearing in the shade.
823 *
824 * @param entry the notification to show as a bubble.
825 */
826 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800827 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
828 Log.d(TAG, "onUserCreatedBubble: " + entry.getKey());
829 }
Heemin Seogba6337f2019-12-10 15:34:37 -0800830 mShadeController.collapsePanel(true);
Mady Mellor7f234902019-10-20 12:06:29 -0700831 entry.setFlagBubble(true);
832 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
Mady Mellorff076eb2019-11-13 10:12:06 -0800833 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800834 mUserBlockedBubbles.remove(entry.getKey());
Mady Mellor7f234902019-10-20 12:06:29 -0700835 }
836
837 /**
838 * Called when a user has indicated that an active notification appearing as a bubble should
839 * no longer be shown as a bubble.
840 *
841 * @param entry the notification to no longer show as a bubble.
842 */
843 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800844 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
845 Log.d(TAG, "onUserDemotedBubble: " + entry.getKey());
846 }
Mady Mellor7f234902019-10-20 12:06:29 -0700847 entry.setFlagBubble(false);
Beverlya53fb0d2020-01-29 15:26:13 -0500848 removeBubble(entry, DISMISS_BLOCKED);
Mady Mellorff076eb2019-11-13 10:12:06 -0800849 mUserCreatedBubbles.remove(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800850 if (BubbleExperimentConfig.isPackageWhitelistedToAutoBubble(
851 mContext, entry.getSbn().getPackageName())) {
852 // This package is whitelist but user demoted the bubble, let's save it so we don't
853 // auto-bubble for the whitelist again.
854 mUserBlockedBubbles.add(entry.getKey());
855 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800856 }
857
858 /**
859 * Whether this bubble was explicitly created by the user via a SysUI affordance.
860 */
861 boolean isUserCreatedBubble(String key) {
862 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800863 }
864
Beverlya53fb0d2020-01-29 15:26:13 -0500865 boolean isSummaryOfUserCreatedBubble(NotificationEntry entry) {
866 if (isSummaryOfBubbles(entry)) {
867 List<Bubble> bubbleChildren =
868 mBubbleData.getBubblesInGroup(entry.getSbn().getGroupKey());
869 for (int i = 0; i < bubbleChildren.size(); i++) {
870 // Check if any are user-created (i.e. experimental bubbles)
871 if (isUserCreatedBubble(bubbleChildren.get(i).getKey())) {
872 return true;
873 }
874 }
875 }
876 return false;
877 }
878
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800879 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500880 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500881 * <p>
882 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800883 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500884 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500885 void removeBubble(NotificationEntry entry, int reason) {
886 if (mBubbleData.hasBubbleWithKey(entry.getKey())) {
887 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800888 }
889 }
890
Beverlyed8aea22020-01-22 16:52:47 -0500891 private void onEntryAdded(NotificationEntry entry) {
892 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
893 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
894 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
895 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor22f2f072019-04-18 13:26:18 -0700896
Beverlye73aea22020-03-03 17:55:47 -0500897 if (mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Beverlyed8aea22020-01-22 16:52:47 -0500898 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
899 if (wasAdjusted && !previouslyUserCreated) {
900 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
901 mUserCreatedBubbles.add(entry.getKey());
Mady Mellorc2ff0112019-03-28 14:18:06 -0700902 }
Beverlyed8aea22020-01-22 16:52:47 -0500903 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700904 }
905 }
906
Beverlyed8aea22020-01-22 16:52:47 -0500907 private void onEntryUpdated(NotificationEntry entry) {
908 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
909 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
910 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
911 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700912
Beverlye73aea22020-03-03 17:55:47 -0500913 boolean shouldBubble = mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Beverlyed8aea22020-01-22 16:52:47 -0500914 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
915 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
916 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500917 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500918 } else if (shouldBubble) {
919 if (wasAdjusted && !previouslyUserCreated) {
920 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
921 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800922 }
Beverlyed8aea22020-01-22 16:52:47 -0500923 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800924 }
Beverlyed8aea22020-01-22 16:52:47 -0500925 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800926
Beverlya53fb0d2020-01-29 15:26:13 -0500927 private void onEntryRemoved(NotificationEntry entry) {
928 if (isSummaryOfBubbles(entry)) {
929 final String groupKey = entry.getSbn().getGroupKey();
930 mBubbleData.removeSuppressedSummary(groupKey);
931
932 // Remove any associated bubble children with the summary
933 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
934 for (int i = 0; i < bubbleChildren.size(); i++) {
935 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
936 }
937 } else {
938 removeBubble(entry, DISMISS_NOTIF_CANCEL);
939 }
940 }
941
Beverlyed8aea22020-01-22 16:52:47 -0500942 private void onRankingUpdated(RankingMap rankingMap) {
943 // Forward to BubbleData to block any bubbles which should no longer be shown
944 mBubbleData.notificationRankingUpdated(rankingMap);
945 }
Ned Burns01e38212019-01-03 16:32:52 -0500946
Mark Renouf71a3af62019-04-08 15:02:54 -0400947 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400948 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400949
Mark Renouf3bc5b362019-04-05 14:37:59 -0400950 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400951 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800952 // Update bubbles in overflow.
953 if (mOverflowCallback != null) {
954 mOverflowCallback.run();
955 }
956
Mark Renouf82a40e62019-05-23 16:16:24 -0400957 // Collapsing? Do this first before remaining steps.
958 if (update.expandedChanged && !update.expanded) {
959 mStackView.setExpanded(false);
960 }
961
962 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700963 ArrayList<Pair<Bubble, Integer>> removedBubbles =
964 new ArrayList<>(update.removedBubbles);
965 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400966 final Bubble bubble = removed.first;
967 @DismissReason final int reason = removed.second;
968 mStackView.removeBubble(bubble);
Mark Renoufc19b4732019-06-26 12:08:33 -0400969 // If the bubble is removed for user switching, leave the notification in place.
970 if (reason != DISMISS_USER_CHANGED) {
971 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800972 && !bubble.showInShade()) {
Beverlyed8aea22020-01-22 16:52:47 -0500973 // The bubble is now gone & the notification is hidden from the shade, so
974 // time to actually remove it
975 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -0500976 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -0500977 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400978 } else {
979 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400980 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700981
Mark Renoufc19b4732019-06-26 12:08:33 -0400982 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
983 // will get right result back
984 try {
985 mBarService.onNotificationBubbleChanged(bubble.getKey(),
986 false /* isBubble */);
987 } catch (RemoteException e) {
988 // Bad things have happened
989 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400990 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700991
Ned Burns00b4b2d2019-10-17 22:09:27 -0400992 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500993 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
994 // Time to potentially remove the summary
995 for (NotifCallback cb : mCallbacks) {
996 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -0700997 }
998 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700999 }
1000 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001001
Lyn Hanc47e1712020-01-28 21:43:34 -08001002 if (update.addedBubble != null) {
1003 mStackView.addBubble(update.addedBubble);
1004 }
1005
Mark Renouf82a40e62019-05-23 16:16:24 -04001006 if (update.updatedBubble != null) {
1007 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -04001008 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001009
Lyn Hanb58c7562020-01-07 14:29:20 -08001010 // At this point, the correct bubbles are inflated in the stack.
1011 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -04001012 if (update.orderChanged) {
1013 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -04001014 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001015
Mark Renouf82a40e62019-05-23 16:16:24 -04001016 if (update.selectionChanged) {
1017 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -07001018 if (update.selectedBubble != null) {
1019 mNotificationGroupManager.updateSuppression(
1020 update.selectedBubble.getEntry());
1021 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001022 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001023
Mark Renouf82a40e62019-05-23 16:16:24 -04001024 // Expanding? Apply this last.
1025 if (update.expandedChanged && update.expanded) {
1026 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001027 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001028
Beverlyed8aea22020-01-22 16:52:47 -05001029 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001030 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001031 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001032 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001033
Issei Suzukia8d07312019-06-07 12:56:19 +02001034 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001035 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001036 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001037 mBubbleData.getSelectedBubble()));
1038
1039 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001040 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001041 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001042 mStackView.getExpandedBubble()));
1043 }
Lyn Hanb58c7562020-01-07 14:29:20 -08001044 Log.d(TAG, "\n[BubbleData] overflow:");
1045 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
1046 null));
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001047 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001048 }
1049 };
1050
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001051 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001052 * We intercept notification entries (including group summaries) dismissed by the user when
1053 * there is an active bubble associated with it. We do this so that developers can still
1054 * cancel it (and hence the bubbles associated with it). However, these intercepted
1055 * notifications should then be hidden from the shade since the user has cancelled them, so we
1056 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1057 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001058 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001059 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001060 */
Beverlya53fb0d2020-01-29 15:26:13 -05001061 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001062 if (entry == null) {
1063 return false;
1064 }
Beverlyed8aea22020-01-22 16:52:47 -05001065
Beverlya53fb0d2020-01-29 15:26:13 -05001066 final boolean interceptBubbleDismissal = mBubbleData.hasBubbleWithKey(entry.getKey())
1067 && entry.isBubble();
1068 final boolean interceptSummaryDismissal = isSummaryOfBubbles(entry);
Beverlyed8aea22020-01-22 16:52:47 -05001069
Beverlya53fb0d2020-01-29 15:26:13 -05001070 if (interceptSummaryDismissal) {
1071 handleSummaryDismissalInterception(entry);
1072 } else if (interceptBubbleDismissal) {
1073 Bubble bubble = mBubbleData.getBubbleWithKey(entry.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001074 bubble.setSuppressNotification(true);
1075 bubble.setShowDot(false /* show */, true /* animate */);
Beverlya53fb0d2020-01-29 15:26:13 -05001076 } else {
Beverlyed8aea22020-01-22 16:52:47 -05001077 return false;
1078 }
Beverlya53fb0d2020-01-29 15:26:13 -05001079
1080 // Update the shade
1081 for (NotifCallback cb : mCallbacks) {
1082 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1083 }
1084 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001085 }
1086
Beverlya53fb0d2020-01-29 15:26:13 -05001087 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1088 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001089 return false;
1090 }
Beverlya53fb0d2020-01-29 15:26:13 -05001091
1092 String groupKey = entry.getSbn().getGroupKey();
1093 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1094 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1095 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1096 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1097 return (isSuppressedSummary || isSummary)
1098 && bubbleChildren != null
1099 && !bubbleChildren.isEmpty();
1100 }
1101
1102 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1103 // current children in the row:
1104 final List<NotificationEntry> children = summary.getChildren();
1105 if (children != null) {
1106 for (int i = 0; i < children.size(); i++) {
1107 NotificationEntry child = children.get(i);
1108 if (mBubbleData.hasBubbleWithKey(child.getKey())) {
1109 // Suppress the bubbled child
1110 // As far as group manager is concerned, once a child is no longer shown
1111 // in the shade, it is essentially removed.
1112 Bubble bubbleChild = mBubbleData.getBubbleWithKey(child.getKey());
1113 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1114 bubbleChild.setSuppressNotification(true);
1115 bubbleChild.setShowDot(false /* show */, true /* animate */);
1116 } else {
1117 // non-bubbled children can be removed
1118 for (NotifCallback cb : mCallbacks) {
1119 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1120 }
1121 }
1122 }
1123 }
1124
1125 // And since all children are removed, remove the summary.
1126 mNotificationGroupManager.onEntryRemoved(summary);
1127
1128 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1129 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1130 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001131 }
1132
1133 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001134 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001135 * Updates the visibility of the bubbles based on current state.
1136 * Does not un-bubble, just hides or un-hides. Notifies any
1137 * {@link BubbleStateChangeListener}s of visibility changes.
1138 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001139 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001140 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001141 if (mStackView == null) {
1142 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001143 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001144 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1145 // Bubbles only appear in unlocked shade
1146 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001147 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001148 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001149 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001150
Mady Mellor698d9e82019-08-01 23:11:53 +00001151 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001152 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001153 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001154 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001155 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1156 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1157 }
1158
1159 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001160 }
1161
1162 /**
1163 * Rect indicating the touchable region for the bubble stack / expanded stack.
1164 */
1165 public Rect getTouchableRegion() {
1166 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1167 return null;
1168 }
1169 mStackView.getBoundsOnScreen(mTempRect);
1170 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001171 }
1172
Mady Mellor390bff42019-04-05 15:09:01 -07001173 /**
1174 * The display id of the expanded view, if the stack is expanded and not occluded by the
1175 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1176 */
1177 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001178 if (mStackView == null) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001179 return INVALID_DISPLAY;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001180 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001181 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001182 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Lyn Han9f66c3b2020-03-05 23:59:29 -08001183 final BubbleViewProvider expandedViewProvider = mStackView.getExpandedBubble();
1184 if (defaultDisplay && expandedViewProvider != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001185 && !mNotificationShadeWindowController.getPanelExpanded()) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001186 return expandedViewProvider.getDisplayId();
Mady Mellor390bff42019-04-05 15:09:01 -07001187 }
Lyn Han9f66c3b2020-03-05 23:59:29 -08001188 return INVALID_DISPLAY;
Mady Mellor390bff42019-04-05 15:09:01 -07001189 }
1190
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001191 @VisibleForTesting
1192 BubbleStackView getStackView() {
1193 return mStackView;
1194 }
1195
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001196 /**
1197 * Description of current bubble state.
1198 */
1199 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1200 pw.println("BubbleController state:");
1201 mBubbleData.dump(fd, pw, args);
1202 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001203 if (mStackView != null) {
1204 mStackView.dump(fd, pw, args);
1205 }
1206 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001207 }
1208
Mady Mellore80930e2019-03-21 16:00:45 -07001209 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001210 * This task stack listener is responsible for responding to tasks moved to the front
1211 * which are on the default (main) display. When this happens, expanded bubbles must be
1212 * collapsed so the user may interact with the app which was just moved to the front.
1213 * <p>
1214 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1215 * these calls via a main thread Handler.
1216 */
1217 @MainThread
1218 private class BubbleTaskStackListener extends TaskStackChangeListener {
1219
Mark Renoufcecc77b2019-01-30 16:32:24 -05001220 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001221 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1222 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001223 if (!mStackView.isExpansionAnimating()) {
1224 mBubbleData.setExpanded(false);
1225 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001226 }
1227 }
1228
Mark Renoufcecc77b2019-01-30 16:32:24 -05001229 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001230 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001231 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001232 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001233 }
1234 }
Mark Renouf446251d2019-04-26 10:22:41 -04001235
1236 @Override
1237 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1238 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1239 mBubbleData.setExpanded(false);
1240 }
1241 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001242
1243 @Override
1244 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001245 if (mStackView == null) {
1246 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001247 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001248 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001249 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001250
1251 @Override
1252 public void onSingleTaskDisplayEmpty(int displayId) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001253 final BubbleViewProvider expandedBubble = mStackView != null
Mady Mellor5186b132019-09-16 17:55:48 -07001254 ? mStackView.getExpandedBubble()
1255 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001256 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1257 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001258 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001259 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001260 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001261 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001262 }
1263
Mady Mellorca0c24c2019-05-16 16:14:32 -07001264 /**
1265 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1266 *
1267 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1268 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1269 *
1270 * @param context the context to use.
1271 * @param entry the entry to bubble.
1272 */
1273 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1274 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001275 ? entry.getBubbleMetadata().getBubbleIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001276 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001277 if (entry.getBubbleMetadata() != null
1278 && entry.getBubbleMetadata().getShortcutId() != null) {
1279 return true;
1280 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001281 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001282 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001283 return false;
1284 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001285 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1286 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001287 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001288 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001289 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001290 Log.w(TAG, "Unable to send as bubble, "
1291 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001292 + intent);
1293 return false;
1294 }
1295 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001296 Log.w(TAG, "Unable to send as bubble, "
1297 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001298 + intent);
1299 return false;
1300 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001301 return true;
1302 }
1303
Joshua Tsujia19515f2019-02-13 18:02:29 -05001304 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001305 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001306 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001307 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1308 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1309 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001310 }
1311 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001312 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001313}