blob: a220dac0af4131b5df628bda5fa94ae6b27f99a6 [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;
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;
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
Mady Melloraa8fef22019-04-11 13:36:40 -0700172 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
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,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400282 NotificationInterruptionStateProvider 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,
307 NotificationInterruptionStateProvider interruptionStateProvider,
308 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;
Mady Melloraa8fef22019-04-11 13:36:40 -0700319 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400320 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400321 mZenModeController = zenModeController;
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,
428 boolean removedByUser) {
429 BubbleController.this.onEntryRemoved(entry);
430 }
431
432 @Override
Beverlyed8aea22020-01-22 16:52:47 -0500433 public void onNotificationRankingUpdated(RankingMap rankingMap) {
434 onRankingUpdated(rankingMap);
435 }
436 });
437
Evan Laird04373662020-01-24 17:37:39 -0500438 mNotificationEntryManager.addNotificationRemoveInterceptor(
Beverlyed8aea22020-01-22 16:52:47 -0500439 new NotificationRemoveInterceptor() {
440 @Override
Evan Laird04373662020-01-24 17:37:39 -0500441 public boolean onNotificationRemoveRequested(
Beverlya53fb0d2020-01-29 15:26:13 -0500442 String key,
443 NotificationEntry entry,
444 int dismissReason) {
445 final boolean isClearAll = dismissReason == REASON_CANCEL_ALL;
446 final boolean isUserDimiss = dismissReason == REASON_CANCEL
447 || dismissReason == REASON_CLICK;
448 final boolean isAppCancel = dismissReason == REASON_APP_CANCEL
449 || dismissReason == REASON_APP_CANCEL_ALL;
450 final boolean isSummaryCancel =
451 dismissReason == REASON_GROUP_SUMMARY_CANCELED;
452
453 // Need to check for !appCancel here because the notification may have
454 // previously been dismissed & entry.isRowDismissed would still be true
455 boolean userRemovedNotif =
456 (entry != null && entry.isRowDismissed() && !isAppCancel)
457 || isClearAll || isUserDimiss || isSummaryCancel;
458
459 if (userRemovedNotif || isUserCreatedBubble(key)
460 || isSummaryOfUserCreatedBubble(entry)) {
461 return handleDismissalInterception(entry);
462 }
463
464 return false;
Beverlyed8aea22020-01-22 16:52:47 -0500465 }
466 });
467
468 mNotificationGroupManager.addOnGroupChangeListener(
469 new NotificationGroupManager.OnGroupChangeListener() {
470 @Override
471 public void onGroupSuppressionChanged(
472 NotificationGroupManager.NotificationGroup group,
473 boolean suppressed) {
474 // More notifications could be added causing summary to no longer
475 // be suppressed -- in this case need to remove the key.
476 final String groupKey = group.summary != null
477 ? group.summary.getSbn().getGroupKey()
478 : null;
479 if (!suppressed && groupKey != null
480 && mBubbleData.isSummarySuppressed(groupKey)) {
481 mBubbleData.removeSuppressedSummary(groupKey);
482 }
483 }
484 });
485
486 addNotifCallback(new NotifCallback() {
487 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500488 public void removeNotification(NotificationEntry entry, int reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500489 mNotificationEntryManager.performRemoveNotification(entry.getSbn(),
Beverlya53fb0d2020-01-29 15:26:13 -0500490 reason);
Beverlyed8aea22020-01-22 16:52:47 -0500491 }
492
493 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500494 public void invalidateNotifications(String reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500495 mNotificationEntryManager.updateNotifications(reason);
496 }
497
498 @Override
499 public void maybeCancelSummary(NotificationEntry entry) {
500 // Check if removed bubble has an associated suppressed group summary that needs
501 // to be removed now.
Beverlya53fb0d2020-01-29 15:26:13 -0500502 final String groupKey = entry.getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500503 if (mBubbleData.isSummarySuppressed(groupKey)) {
Beverlya53fb0d2020-01-29 15:26:13 -0500504 mBubbleData.removeSuppressedSummary(groupKey);
Beverlyed8aea22020-01-22 16:52:47 -0500505
506 final NotificationEntry summary =
507 mNotificationEntryManager.getActiveNotificationUnfiltered(
508 mBubbleData.getSummaryKey(groupKey));
Beverlya53fb0d2020-01-29 15:26:13 -0500509 if (summary != null) {
510 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
511 UNDEFINED_DISMISS_REASON);
512 }
Beverlyed8aea22020-01-22 16:52:47 -0500513 }
514
Beverlya53fb0d2020-01-29 15:26:13 -0500515 // Check if we still need to remove the summary from NoManGroup because the summary
516 // may not be in the mBubbleData.mSuppressedGroupKeys list and removed above.
517 // For example:
518 // 1. Bubbled notifications (group) is posted to shade and are visible bubbles
519 // 2. User expands bubbles so now their respective notifications in the shade are
520 // hidden, including the group summary
521 // 3. User removes all bubbles
522 // 4. We expect all the removed bubbles AND the summary (note: the summary was
523 // never added to the suppressedSummary list in BubbleData, so we add this check)
Beverlyed8aea22020-01-22 16:52:47 -0500524 NotificationEntry summary =
525 mNotificationGroupManager.getLogicalGroupSummary(entry.getSbn());
526 if (summary != null) {
527 ArrayList<NotificationEntry> summaryChildren =
528 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
529 boolean isSummaryThisNotif = summary.getKey().equals(entry.getKey());
530 if (!isSummaryThisNotif && (summaryChildren == null
531 || summaryChildren.isEmpty())) {
532 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
533 UNDEFINED_DISMISS_REASON);
534 }
535 }
536 }
537 });
538 }
539
Beverlya53fb0d2020-01-29 15:26:13 -0500540 private void setupNotifPipeline() {
541 mNotifPipeline.addCollectionListener(new NotifCollectionListener() {
542 @Override
543 public void onEntryAdded(NotificationEntry entry) {
544 BubbleController.this.onEntryAdded(entry);
545 }
546
547 @Override
548 public void onEntryUpdated(NotificationEntry entry) {
549 BubbleController.this.onEntryUpdated(entry);
550 }
551
552 @Override
553 public void onRankingUpdate(RankingMap rankingMap) {
554 onRankingUpdated(rankingMap);
555 }
556
557 @Override
558 public void onEntryRemoved(NotificationEntry entry,
559 @NotifCollection.CancellationReason int reason) {
560 BubbleController.this.onEntryRemoved(entry);
561 }
562 });
563 }
564
Beverlyed8aea22020-01-22 16:52:47 -0500565 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800566 * Sets whether to perform inflation on the same thread as the caller. This method should only
567 * be used in tests, not in production.
568 */
569 @VisibleForTesting
570 void setInflateSynchronously(boolean inflateSynchronously) {
571 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800572 }
573
Lyn Hanb58c7562020-01-07 14:29:20 -0800574 void setOverflowCallback(Runnable updateOverflow) {
575 mOverflowCallback = updateOverflow;
576 }
577
578 /**
579 * @return Bubbles for updating overflow.
580 */
581 List<Bubble> getOverflowBubbles() {
582 return mBubbleData.getOverflowBubbles();
583 }
584
585
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400586 /**
587 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
588 * method initializes the stack view and adds it to the StatusBar just above the scrim.
589 */
590 private void ensureStackViewCreated() {
591 if (mStackView == null) {
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500592 mStackView = new BubbleStackView(
593 mContext, mBubbleData, mSurfaceSynchronizer, mFloatingContentCoordinator);
wilsonshihe8321942019-10-18 18:39:46 +0800594 ViewGroup nsv = mNotificationShadeWindowController.getNotificationShadeView();
595 int bubbleScrimIndex = nsv.indexOfChild(nsv.findViewById(R.id.scrim_for_bubble));
Lyn Hanbde48202019-05-29 19:18:29 -0700596 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
wilsonshihe8321942019-10-18 18:39:46 +0800597 nsv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400598 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
599 if (mExpandListener != null) {
600 mStackView.setExpandListener(mExpandListener);
601 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400602 }
603 }
604
Mark Renoufc19b4732019-06-26 12:08:33 -0400605 /**
606 * Records the notification key for any active bubbles. These are used to restore active
607 * bubbles when the user returns to the foreground.
608 *
609 * @param userId the id of the user
610 */
611 private void saveBubbles(@UserIdInt int userId) {
612 // First clear any existing keys that might be stored.
613 mSavedBubbleKeysPerUser.remove(userId);
614 // Add in all active bubbles for the current user.
615 for (Bubble bubble: mBubbleData.getBubbles()) {
616 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
617 }
618 }
619
620 /**
621 * Promotes existing notifications to Bubbles if they were previously bubbles.
622 *
623 * @param userId the id of the user
624 */
625 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400626 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
627 if (savedBubbleKeys == null) {
628 // There were no bubbles saved for this used.
629 return;
630 }
Evan Laird181de622019-10-24 09:53:02 -0400631 for (NotificationEntry e :
632 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400633 if (savedBubbleKeys.contains(e.getKey())
Mark Renoufc19b4732019-06-26 12:08:33 -0400634 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
635 && canLaunchInActivityView(mContext, e)) {
636 updateBubble(e, /* suppressFlyout= */ true);
637 }
638 }
639 // Finally, remove the entries for this user now that bubbles are restored.
640 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
641 }
642
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700643 @Override
644 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800645 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700646 }
647
648 @Override
649 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800650 updateForThemeChanges();
651 }
652
653 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700654 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700655 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700656 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800657 mBubbleIconFactory = new BubbleIconFactory(mContext);
658 for (Bubble b: mBubbleData.getBubbles()) {
659 // Reload each bubble
660 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
661 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700662 }
663
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400664 @Override
665 public void onConfigChanged(Configuration newConfig) {
666 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400667 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700668 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400669 }
670 }
671
Mady Mellor5549dd22018-11-06 18:07:34 -0800672 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800673 * Set a listener to be notified when some states of the bubbles change.
674 */
675 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
676 mStateChangeListener = listener;
677 }
678
679 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800680 * Set a listener to be notified of bubble expand events.
681 */
682 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100683 mExpandListener = ((isExpanding, key) -> {
684 if (listener != null) {
685 listener.onBubbleExpandChanged(isExpanding, key);
686 }
wilsonshihe8321942019-10-18 18:39:46 +0800687 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100688 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800689 if (mStackView != null) {
690 mStackView.setExpandListener(mExpandListener);
691 }
692 }
693
694 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800695 * Whether or not there are bubbles present, regardless of them being visible on the
696 * screen (e.g. if on AOD).
697 */
698 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800699 if (mStackView == null) {
700 return false;
701 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400702 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800703 }
704
705 /**
706 * Whether the stack of bubbles is expanded or not.
707 */
708 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400709 return mBubbleData.isExpanded();
710 }
711
712 /**
713 * Tell the stack of bubbles to expand.
714 */
715 public void expandStack() {
716 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800717 }
718
719 /**
720 * Tell the stack of bubbles to collapse.
721 */
722 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400723 mBubbleData.setExpanded(false /* expanded */);
724 }
725
Mady Mellorce23c462019-06-17 17:30:07 -0700726 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700727 * True if either:
728 * (1) There is a bubble associated with the provided key and if its notification is hidden
729 * from the shade.
730 * (2) There is a group summary associated with the provided key that is hidden from the shade
731 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700732 *
Mady Mellore28fe102019-07-09 15:33:32 -0700733 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700734 */
Beverlyed8aea22020-01-22 16:52:47 -0500735 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
736 String key = entry.getKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700737 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800738 && !mBubbleData.getBubbleWithKey(key).showInShade();
Beverlyed8aea22020-01-22 16:52:47 -0500739
740 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700741 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700742 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Beverlyed8aea22020-01-22 16:52:47 -0500743
Mady Mellore4348272019-07-29 17:43:36 -0700744 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700745 }
746
Mark Renouf71a3af62019-04-08 15:02:54 -0400747 @VisibleForTesting
748 void selectBubble(String key) {
749 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellor247ca2c2019-12-02 16:18:59 -0800750 mBubbleData.setSelectedBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800751 }
752
Lyn Hanb58c7562020-01-07 14:29:20 -0800753 void promoteBubbleFromOverflow(Bubble bubble) {
Lyn Han1e19d7f2020-02-05 19:10:58 -0800754 bubble.setInflateSynchronously(mInflateSynchronously);
755 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
Lyn Hanb58c7562020-01-07 14:29:20 -0800756 }
757
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800758 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400759 * Request the stack expand if needed, then select the specified Bubble as current.
760 *
761 * @param notificationKey the notification key for the bubble to be selected
762 */
763 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400764 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
765 if (bubble != null) {
766 mBubbleData.setSelectedBubble(bubble);
767 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400768 }
769 }
770
771 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800772 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
773 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500774 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400775 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800776 }
777
778 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500779 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
780 * is forwarded a back key down/up pair.
781 */
782 public void performBackPressIfNeeded() {
783 if (mStackView != null) {
784 mStackView.performBackPressIfNeeded();
785 }
786 }
787
788 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800789 * Adds or updates a bubble associated with the provided notification entry.
790 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400791 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800792 */
Mark Renouff97ed462019-04-05 13:46:24 -0400793 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700794 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400795 }
796
797 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700798 updateBubble(notif, suppressFlyout, true /* showInShade */);
799 }
800
801 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800802 if (mStackView == null) {
803 // Lazy init stack view when a bubble is created
804 ensureStackViewCreated();
805 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700806 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400807 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700808 notif.setInterruption();
809 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800810 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
811 bubble.setInflateSynchronously(mInflateSynchronously);
812 bubble.inflate(
813 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
814 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700815 }
816
817 /**
818 * Called when a user has indicated that an active notification should be shown as a bubble.
819 * <p>
820 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
821 * the notification from appearing in the shade.
822 *
823 * @param entry the notification to show as a bubble.
824 */
825 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800826 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
827 Log.d(TAG, "onUserCreatedBubble: " + entry.getKey());
828 }
Heemin Seogba6337f2019-12-10 15:34:37 -0800829 mShadeController.collapsePanel(true);
Mady Mellor7f234902019-10-20 12:06:29 -0700830 entry.setFlagBubble(true);
831 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
Mady Mellorff076eb2019-11-13 10:12:06 -0800832 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800833 mUserBlockedBubbles.remove(entry.getKey());
Mady Mellor7f234902019-10-20 12:06:29 -0700834 }
835
836 /**
837 * Called when a user has indicated that an active notification appearing as a bubble should
838 * no longer be shown as a bubble.
839 *
840 * @param entry the notification to no longer show as a bubble.
841 */
842 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800843 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
844 Log.d(TAG, "onUserDemotedBubble: " + entry.getKey());
845 }
Mady Mellor7f234902019-10-20 12:06:29 -0700846 entry.setFlagBubble(false);
Beverlya53fb0d2020-01-29 15:26:13 -0500847 removeBubble(entry, DISMISS_BLOCKED);
Mady Mellorff076eb2019-11-13 10:12:06 -0800848 mUserCreatedBubbles.remove(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800849 if (BubbleExperimentConfig.isPackageWhitelistedToAutoBubble(
850 mContext, entry.getSbn().getPackageName())) {
851 // This package is whitelist but user demoted the bubble, let's save it so we don't
852 // auto-bubble for the whitelist again.
853 mUserBlockedBubbles.add(entry.getKey());
854 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800855 }
856
857 /**
858 * Whether this bubble was explicitly created by the user via a SysUI affordance.
859 */
860 boolean isUserCreatedBubble(String key) {
861 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800862 }
863
Beverlya53fb0d2020-01-29 15:26:13 -0500864 boolean isSummaryOfUserCreatedBubble(NotificationEntry entry) {
865 if (isSummaryOfBubbles(entry)) {
866 List<Bubble> bubbleChildren =
867 mBubbleData.getBubblesInGroup(entry.getSbn().getGroupKey());
868 for (int i = 0; i < bubbleChildren.size(); i++) {
869 // Check if any are user-created (i.e. experimental bubbles)
870 if (isUserCreatedBubble(bubbleChildren.get(i).getKey())) {
871 return true;
872 }
873 }
874 }
875 return false;
876 }
877
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800878 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500879 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500880 * <p>
881 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800882 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500883 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500884 void removeBubble(NotificationEntry entry, int reason) {
885 if (mBubbleData.hasBubbleWithKey(entry.getKey())) {
886 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800887 }
888 }
889
Beverlyed8aea22020-01-22 16:52:47 -0500890 private void onEntryAdded(NotificationEntry entry) {
891 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
892 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
893 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
894 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor22f2f072019-04-18 13:26:18 -0700895
Beverlyed8aea22020-01-22 16:52:47 -0500896 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
897 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
898 if (wasAdjusted && !previouslyUserCreated) {
899 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
900 mUserCreatedBubbles.add(entry.getKey());
Mady Mellorc2ff0112019-03-28 14:18:06 -0700901 }
Beverlyed8aea22020-01-22 16:52:47 -0500902 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700903 }
904 }
905
Beverlyed8aea22020-01-22 16:52:47 -0500906 private void onEntryUpdated(NotificationEntry entry) {
907 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
908 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
909 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
910 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700911
Beverlyed8aea22020-01-22 16:52:47 -0500912 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
913 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
914 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
915 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500916 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500917 } else if (shouldBubble) {
918 if (wasAdjusted && !previouslyUserCreated) {
919 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
920 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800921 }
Beverlyed8aea22020-01-22 16:52:47 -0500922 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800923 }
Beverlyed8aea22020-01-22 16:52:47 -0500924 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800925
Beverlya53fb0d2020-01-29 15:26:13 -0500926 private void onEntryRemoved(NotificationEntry entry) {
927 if (isSummaryOfBubbles(entry)) {
928 final String groupKey = entry.getSbn().getGroupKey();
929 mBubbleData.removeSuppressedSummary(groupKey);
930
931 // Remove any associated bubble children with the summary
932 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
933 for (int i = 0; i < bubbleChildren.size(); i++) {
934 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
935 }
936 } else {
937 removeBubble(entry, DISMISS_NOTIF_CANCEL);
938 }
939 }
940
Beverlyed8aea22020-01-22 16:52:47 -0500941 private void onRankingUpdated(RankingMap rankingMap) {
942 // Forward to BubbleData to block any bubbles which should no longer be shown
943 mBubbleData.notificationRankingUpdated(rankingMap);
944 }
Ned Burns01e38212019-01-03 16:32:52 -0500945
Mark Renouf71a3af62019-04-08 15:02:54 -0400946 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400947 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400948
Mark Renouf3bc5b362019-04-05 14:37:59 -0400949 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400950 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800951 // Update bubbles in overflow.
952 if (mOverflowCallback != null) {
953 mOverflowCallback.run();
954 }
955
Mark Renouf82a40e62019-05-23 16:16:24 -0400956 // Collapsing? Do this first before remaining steps.
957 if (update.expandedChanged && !update.expanded) {
958 mStackView.setExpanded(false);
959 }
960
961 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700962 ArrayList<Pair<Bubble, Integer>> removedBubbles =
963 new ArrayList<>(update.removedBubbles);
964 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400965 final Bubble bubble = removed.first;
966 @DismissReason final int reason = removed.second;
967 mStackView.removeBubble(bubble);
Mark Renoufc19b4732019-06-26 12:08:33 -0400968 // If the bubble is removed for user switching, leave the notification in place.
969 if (reason != DISMISS_USER_CHANGED) {
970 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800971 && !bubble.showInShade()) {
Beverlyed8aea22020-01-22 16:52:47 -0500972 // The bubble is now gone & the notification is hidden from the shade, so
973 // time to actually remove it
974 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -0500975 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -0500976 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400977 } else {
978 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400979 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700980
Mark Renoufc19b4732019-06-26 12:08:33 -0400981 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
982 // will get right result back
983 try {
984 mBarService.onNotificationBubbleChanged(bubble.getKey(),
985 false /* isBubble */);
986 } catch (RemoteException e) {
987 // Bad things have happened
988 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400989 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700990
Ned Burns00b4b2d2019-10-17 22:09:27 -0400991 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500992 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
993 // Time to potentially remove the summary
994 for (NotifCallback cb : mCallbacks) {
995 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -0700996 }
997 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700998 }
999 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001000
Lyn Hanc47e1712020-01-28 21:43:34 -08001001 if (update.addedBubble != null) {
1002 mStackView.addBubble(update.addedBubble);
1003 }
1004
Mark Renouf82a40e62019-05-23 16:16:24 -04001005 if (update.updatedBubble != null) {
1006 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -04001007 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001008
Lyn Hanb58c7562020-01-07 14:29:20 -08001009 // At this point, the correct bubbles are inflated in the stack.
1010 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -04001011 if (update.orderChanged) {
1012 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -04001013 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001014
Mark Renouf82a40e62019-05-23 16:16:24 -04001015 if (update.selectionChanged) {
1016 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -07001017 if (update.selectedBubble != null) {
1018 mNotificationGroupManager.updateSuppression(
1019 update.selectedBubble.getEntry());
1020 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001021 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001022
Mark Renouf82a40e62019-05-23 16:16:24 -04001023 // Expanding? Apply this last.
1024 if (update.expandedChanged && update.expanded) {
1025 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001026 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001027
Beverlyed8aea22020-01-22 16:52:47 -05001028 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001029 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001030 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001031 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001032
Issei Suzukia8d07312019-06-07 12:56:19 +02001033 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001034 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001035 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001036 mBubbleData.getSelectedBubble()));
1037
1038 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001039 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001040 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001041 mStackView.getExpandedBubble()));
1042 }
Lyn Hanb58c7562020-01-07 14:29:20 -08001043 Log.d(TAG, "\n[BubbleData] overflow:");
1044 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
1045 null));
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001046 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001047 }
1048 };
1049
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001050 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001051 * We intercept notification entries (including group summaries) dismissed by the user when
1052 * there is an active bubble associated with it. We do this so that developers can still
1053 * cancel it (and hence the bubbles associated with it). However, these intercepted
1054 * notifications should then be hidden from the shade since the user has cancelled them, so we
1055 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1056 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001057 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001058 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001059 */
Beverlya53fb0d2020-01-29 15:26:13 -05001060 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001061 if (entry == null) {
1062 return false;
1063 }
Beverlyed8aea22020-01-22 16:52:47 -05001064
Beverlya53fb0d2020-01-29 15:26:13 -05001065 final boolean interceptBubbleDismissal = mBubbleData.hasBubbleWithKey(entry.getKey())
1066 && entry.isBubble();
1067 final boolean interceptSummaryDismissal = isSummaryOfBubbles(entry);
Beverlyed8aea22020-01-22 16:52:47 -05001068
Beverlya53fb0d2020-01-29 15:26:13 -05001069 if (interceptSummaryDismissal) {
1070 handleSummaryDismissalInterception(entry);
1071 } else if (interceptBubbleDismissal) {
1072 Bubble bubble = mBubbleData.getBubbleWithKey(entry.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001073 bubble.setSuppressNotification(true);
1074 bubble.setShowDot(false /* show */, true /* animate */);
Beverlya53fb0d2020-01-29 15:26:13 -05001075 } else {
Beverlyed8aea22020-01-22 16:52:47 -05001076 return false;
1077 }
Beverlya53fb0d2020-01-29 15:26:13 -05001078
1079 // Update the shade
1080 for (NotifCallback cb : mCallbacks) {
1081 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1082 }
1083 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001084 }
1085
Beverlya53fb0d2020-01-29 15:26:13 -05001086 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1087 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001088 return false;
1089 }
Beverlya53fb0d2020-01-29 15:26:13 -05001090
1091 String groupKey = entry.getSbn().getGroupKey();
1092 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1093 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1094 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1095 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1096 return (isSuppressedSummary || isSummary)
1097 && bubbleChildren != null
1098 && !bubbleChildren.isEmpty();
1099 }
1100
1101 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1102 // current children in the row:
1103 final List<NotificationEntry> children = summary.getChildren();
1104 if (children != null) {
1105 for (int i = 0; i < children.size(); i++) {
1106 NotificationEntry child = children.get(i);
1107 if (mBubbleData.hasBubbleWithKey(child.getKey())) {
1108 // Suppress the bubbled child
1109 // As far as group manager is concerned, once a child is no longer shown
1110 // in the shade, it is essentially removed.
1111 Bubble bubbleChild = mBubbleData.getBubbleWithKey(child.getKey());
1112 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1113 bubbleChild.setSuppressNotification(true);
1114 bubbleChild.setShowDot(false /* show */, true /* animate */);
1115 } else {
1116 // non-bubbled children can be removed
1117 for (NotifCallback cb : mCallbacks) {
1118 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1119 }
1120 }
1121 }
1122 }
1123
1124 // And since all children are removed, remove the summary.
1125 mNotificationGroupManager.onEntryRemoved(summary);
1126
1127 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1128 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1129 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001130 }
1131
1132 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001133 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001134 * Updates the visibility of the bubbles based on current state.
1135 * Does not un-bubble, just hides or un-hides. Notifies any
1136 * {@link BubbleStateChangeListener}s of visibility changes.
1137 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001138 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001139 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001140 if (mStackView == null) {
1141 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001142 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001143 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1144 // Bubbles only appear in unlocked shade
1145 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001146 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001147 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001148 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001149
Mady Mellor698d9e82019-08-01 23:11:53 +00001150 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001151 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001152 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001153 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001154 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1155 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1156 }
1157
1158 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001159 }
1160
1161 /**
1162 * Rect indicating the touchable region for the bubble stack / expanded stack.
1163 */
1164 public Rect getTouchableRegion() {
1165 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1166 return null;
1167 }
1168 mStackView.getBoundsOnScreen(mTempRect);
1169 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001170 }
1171
Mady Mellor390bff42019-04-05 15:09:01 -07001172 /**
1173 * The display id of the expanded view, if the stack is expanded and not occluded by the
1174 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1175 */
1176 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001177 final Bubble bubble = getExpandedBubble(context);
1178 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
1179 }
1180
1181 @Nullable
1182 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001183 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001184 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001185 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001186 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001187 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +02001188 final Bubble expandedBubble = mStackView.getExpandedBubble();
1189 if (defaultDisplay && expandedBubble != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001190 && !mNotificationShadeWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001191 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -07001192 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001193 return null;
Mady Mellor390bff42019-04-05 15:09:01 -07001194 }
1195
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001196 @VisibleForTesting
1197 BubbleStackView getStackView() {
1198 return mStackView;
1199 }
1200
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001201 /**
1202 * Description of current bubble state.
1203 */
1204 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1205 pw.println("BubbleController state:");
1206 mBubbleData.dump(fd, pw, args);
1207 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001208 if (mStackView != null) {
1209 mStackView.dump(fd, pw, args);
1210 }
1211 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001212 }
1213
Mady Mellore80930e2019-03-21 16:00:45 -07001214 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001215 * This task stack listener is responsible for responding to tasks moved to the front
1216 * which are on the default (main) display. When this happens, expanded bubbles must be
1217 * collapsed so the user may interact with the app which was just moved to the front.
1218 * <p>
1219 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1220 * these calls via a main thread Handler.
1221 */
1222 @MainThread
1223 private class BubbleTaskStackListener extends TaskStackChangeListener {
1224
Mark Renoufcecc77b2019-01-30 16:32:24 -05001225 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001226 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1227 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001228 if (!mStackView.isExpansionAnimating()) {
1229 mBubbleData.setExpanded(false);
1230 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001231 }
1232 }
1233
Mark Renoufcecc77b2019-01-30 16:32:24 -05001234 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001235 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001236 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001237 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001238 }
1239 }
Mark Renouf446251d2019-04-26 10:22:41 -04001240
1241 @Override
1242 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1243 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1244 mBubbleData.setExpanded(false);
1245 }
1246 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001247
1248 @Override
1249 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001250 if (mStackView == null) {
1251 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001252 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001253 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001254 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001255
1256 @Override
1257 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001258 final Bubble expandedBubble = mStackView != null
1259 ? mStackView.getExpandedBubble()
1260 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001261 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1262 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001263 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001264 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001265 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001266 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001267 }
1268
Mady Mellorca0c24c2019-05-16 16:14:32 -07001269 /**
1270 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1271 *
1272 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1273 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1274 *
1275 * @param context the context to use.
1276 * @param entry the entry to bubble.
1277 */
1278 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1279 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001280 ? entry.getBubbleMetadata().getBubbleIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001281 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001282 if (entry.getBubbleMetadata() != null
1283 && entry.getBubbleMetadata().getShortcutId() != null) {
1284 return true;
1285 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001286 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001287 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001288 return false;
1289 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001290 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1291 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001292 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001293 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001294 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001295 Log.w(TAG, "Unable to send as bubble, "
1296 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001297 + intent);
1298 return false;
1299 }
1300 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001301 Log.w(TAG, "Unable to send as bubble, "
1302 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001303 + intent);
1304 return false;
1305 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001306 return true;
1307 }
1308
Joshua Tsujia19515f2019-02-13 18:02:29 -05001309 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001310 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001311 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001312 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1313 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1314 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001315 }
1316 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001317 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001318}