blob: 4b503785b3f1c12f04b9322617455a52bf2f0e1b [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 Mellor9adfe6a2020-03-30 17:23:26 -070020import static android.app.NotificationManager.BUBBLE_PREFERENCE_NONE;
21import static android.app.NotificationManager.BUBBLE_PREFERENCE_SELECTED;
Mady Mellorc2ff0112019-03-28 14:18:06 -070022import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL;
23import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL;
24import static android.service.notification.NotificationListenerService.REASON_CANCEL;
25import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -070026import static android.service.notification.NotificationListenerService.REASON_CLICK;
27import static android.service.notification.NotificationListenerService.REASON_GROUP_SUMMARY_CANCELED;
Mady Mellor390bff42019-04-05 15:09:01 -070028import static android.view.Display.DEFAULT_DISPLAY;
29import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080030import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080031import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080032import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080033
Issei Suzukia8d07312019-06-07 12:56:19 +020034import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_CONTROLLER;
35import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
36import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080037import static com.android.systemui.statusbar.StatusBarState.SHADE;
Mady Mellor1a4e86f2019-05-03 16:07:23 -070038import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080039
Mark Renoufba5ab512019-05-02 15:21:01 -040040import static java.lang.annotation.ElementType.FIELD;
41import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
42import static java.lang.annotation.ElementType.PARAMETER;
Mark Renouf08bc42a2019-03-07 13:01:59 -050043import static java.lang.annotation.RetentionPolicy.SOURCE;
44
Mark Renoufc19b4732019-06-26 12:08:33 -040045import android.annotation.UserIdInt;
Mark Renoufc808f062019-02-07 15:20:37 -050046import android.app.ActivityManager.RunningTaskInfo;
Mady Mellor9adfe6a2020-03-30 17:23:26 -070047import android.app.INotificationManager;
48import android.app.Notification;
49import android.app.NotificationChannel;
Mady Mellor66efd5e2019-05-15 13:38:11 -070050import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070051import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080052import android.content.Context;
Mady Mellorca0c24c2019-05-16 16:14:32 -070053import android.content.pm.ActivityInfo;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080054import android.content.pm.PackageManager;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040055import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080056import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050057import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080058import android.os.ServiceManager;
Mady Mellor56515c42020-02-18 17:58:36 -080059import android.service.notification.NotificationListenerService;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040060import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040061import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040062import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040063import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040064import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040065import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050066import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080067import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080068import android.widget.FrameLayout;
69
Mark Renouf08bc42a2019-03-07 13:01:59 -050070import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050071import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040072import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050073
Mady Mellorebdbbb92018-11-15 14:36:48 -080074import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070075import com.android.internal.statusbar.IStatusBarService;
Beverlya53fb0d2020-01-29 15:26:13 -050076import com.android.internal.statusbar.NotificationVisibility;
Beverlya53fb0d2020-01-29 15:26:13 -050077import com.android.systemui.Dumpable;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080078import com.android.systemui.R;
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +010079import com.android.systemui.bubbles.dagger.BubbleModule;
Ned Burnsaaeb44b2020-02-12 23:48:26 -050080import com.android.systemui.dump.DumpManager;
Joshua Tsujibe60a582020-03-23 17:17:26 -040081import com.android.systemui.model.SysUiState;
Beverly8fdb5332019-02-04 14:29:49 -050082import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050083import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000084import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050085import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050086import com.android.systemui.shared.system.WindowManagerWrapper;
Beverlya53fb0d2020-01-29 15:26:13 -050087import com.android.systemui.statusbar.FeatureFlags;
Mark Renoufc19b4732019-06-26 12:08:33 -040088import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070089import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Mady Mellor9adfe6a2020-03-30 17:23:26 -070090import com.android.systemui.statusbar.notification.NotificationChannelHelper;
Ned Burns01e38212019-01-03 16:32:52 -050091import com.android.systemui.statusbar.notification.NotificationEntryListener;
92import com.android.systemui.statusbar.notification.NotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -050093import com.android.systemui.statusbar.notification.collection.NotifCollection;
94import com.android.systemui.statusbar.notification.collection.NotifPipeline;
Ned Burnsf81c4c42019-01-07 14:10:43 -050095import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Beverlya53fb0d2020-01-29 15:26:13 -050096import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener;
Beverly Taid1e175c2020-03-10 16:37:04 +000097import com.android.systemui.statusbar.notification.interruption.NotificationInterruptStateProvider;
Mady Mellor22f2f072019-04-18 13:26:18 -070098import com.android.systemui.statusbar.phone.NotificationGroupManager;
wilsonshihe8321942019-10-18 18:39:46 +080099import com.android.systemui.statusbar.phone.NotificationShadeWindowController;
Mady Mellor7f234902019-10-20 12:06:29 -0700100import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorf3b9fab2019-11-13 17:27:32 -0800101import com.android.systemui.statusbar.phone.StatusBar;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700102import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400103import com.android.systemui.statusbar.policy.ZenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500104import com.android.systemui.util.FloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800105
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700106import java.io.FileDescriptor;
107import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500108import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -0400109import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -0700110import java.util.ArrayList;
Lyn Hanb58c7562020-01-07 14:29:20 -0800111import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500112
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800113/**
114 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
115 * Bubbles can be expanded to show more content.
116 *
117 * The controller manages addition, removal, and visible state of bubbles on screen.
118 */
Beverlya53fb0d2020-01-29 15:26:13 -0500119public class BubbleController implements ConfigurationController.ConfigurationListener, Dumpable {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800120
Issei Suzukia8d07312019-06-07 12:56:19 +0200121 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800122
Mark Renouf08bc42a2019-03-07 13:01:59 -0500123 @Retention(SOURCE)
124 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400125 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Lyn Han2f6e89d2020-04-15 10:01:01 -0700126 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT,
127 DISMISS_OVERFLOW_MAX_REACHED})
Mark Renoufba5ab512019-05-02 15:21:01 -0400128 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500129 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700130
Mark Renouf08bc42a2019-03-07 13:01:59 -0500131 static final int DISMISS_USER_GESTURE = 1;
132 static final int DISMISS_AGED = 2;
133 static final int DISMISS_TASK_FINISHED = 3;
134 static final int DISMISS_BLOCKED = 4;
135 static final int DISMISS_NOTIF_CANCEL = 5;
136 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700137 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400138 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700139 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700140 static final int DISMISS_INVALID_INTENT = 10;
Lyn Han2f6e89d2020-04-15 10:01:01 -0700141 static final int DISMISS_OVERFLOW_MAX_REACHED = 11;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500142
Ned Burns01e38212019-01-03 16:32:52 -0500143 private final Context mContext;
144 private final NotificationEntryManager mNotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500145 private final NotifPipeline mNotifPipeline;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500146 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800147 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800148 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100149 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700150 private final NotificationGroupManager mNotificationGroupManager;
Heemin Seogba6337f2019-12-10 15:34:37 -0800151 private final ShadeController mShadeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500152 private final FloatingContentCoordinator mFloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800153
Mady Mellor3dff9e62019-02-05 18:12:53 -0800154 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400155 @Nullable private BubbleStackView mStackView;
Mady Mellor247ca2c2019-12-02 16:18:59 -0800156 private BubbleIconFactory mBubbleIconFactory;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800157
Mark Renoufc19b4732019-06-26 12:08:33 -0400158 // Tracks the id of the current (foreground) user.
159 private int mCurrentUserId;
160 // Saves notification keys of active bubbles when users are switched.
161 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
162
Mady Mellor56515c42020-02-18 17:58:36 -0800163 // Used when ranking updates occur and we check if things should bubble / unbubble
164 private NotificationListenerService.Ranking mTmpRanking;
165
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800166 // Bubbles get added to the status bar view
wilsonshihe8321942019-10-18 18:39:46 +0800167 private final NotificationShadeWindowController mNotificationShadeWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400168 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800169 private StatusBarStateListener mStatusBarStateListener;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700170 private INotificationManager mINotificationManager;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500171
Lyn Hanb58c7562020-01-07 14:29:20 -0800172 // Callback that updates BubbleOverflowActivity on data change.
173 @Nullable private Runnable mOverflowCallback = null;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800174
Beverly Taid1e175c2020-03-10 16:37:04 +0000175 private final NotificationInterruptStateProvider mNotificationInterruptStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700176 private IStatusBarService mBarService;
Joshua Tsujibe60a582020-03-23 17:17:26 -0400177 private SysUiState mSysUiState;
Mady Mellorb4991e62019-01-10 15:14:51 -0800178
Mady Mellord1c78b262018-11-06 18:04:40 -0800179 // Used for determining view rect for touch interaction
180 private Rect mTempRect = new Rect();
181
Mark Renoufc19b4732019-06-26 12:08:33 -0400182 // Listens to user switch so bubbles can be saved and restored.
183 private final NotificationLockscreenUserManager mNotifUserManager;
184
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400185 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
186 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
187
Mady Mellor3df7ab02019-12-09 15:07:10 -0800188 private boolean mInflateSynchronously;
189
Beverlyed8aea22020-01-22 16:52:47 -0500190 // TODO (b/145659174): allow for multiple callbacks to support the "shadow" new notif pipeline
191 private final List<NotifCallback> mCallbacks = new ArrayList<>();
192
Mady Mellor5549dd22018-11-06 18:07:34 -0800193 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800194 * Listener to be notified when some states of the bubbles change.
195 */
196 public interface BubbleStateChangeListener {
197 /**
198 * Called when the stack has bubbles or no longer has bubbles.
199 */
200 void onHasBubblesChanged(boolean hasBubbles);
201 }
202
Mady Mellorcd9b1302018-11-06 18:08:04 -0800203 /**
204 * Listener to find out about stack expansion / collapse events.
205 */
206 public interface BubbleExpandListener {
207 /**
208 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700209 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800210 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800211 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800212 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800213 void onBubbleExpandChanged(boolean isExpanding, String key);
214 }
215
216 /**
Mady Mellorf44b6832020-01-14 13:26:14 -0800217 * Listener to be notified when a bubbles' notification suppression state changes.
218 */
219 public interface NotificationSuppressionChangedListener {
220 /**
221 * Called when the notification suppression state of a bubble changes.
222 */
223 void onBubbleNotificationSuppressionChange(Bubble bubble);
Beverlyed8aea22020-01-22 16:52:47 -0500224 }
Mady Mellorf44b6832020-01-14 13:26:14 -0800225
Beverlyed8aea22020-01-22 16:52:47 -0500226 /**
227 * Callback for when the BubbleController wants to interact with the notification pipeline to:
228 * - Remove a previously bubbled notification
229 * - Update the notification shade since bubbled notification should/shouldn't be showing
230 */
231 public interface NotifCallback {
232 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500233 * Called when a bubbled notification that was hidden from the shade is now being removed
234 * This can happen when an app cancels a bubbled notification or when the user dismisses a
235 * bubble.
Beverlyed8aea22020-01-22 16:52:47 -0500236 */
Beverlya53fb0d2020-01-29 15:26:13 -0500237 void removeNotification(NotificationEntry entry, int reason);
Beverlyed8aea22020-01-22 16:52:47 -0500238
239 /**
240 * Called when a bubbled notification has changed whether it should be
241 * filtered from the shade.
242 */
Beverlya53fb0d2020-01-29 15:26:13 -0500243 void invalidateNotifications(String reason);
Beverlyed8aea22020-01-22 16:52:47 -0500244
245 /**
246 * Called on a bubbled entry that has been removed when there are no longer
247 * bubbled entries in its group.
248 *
249 * Checks whether its group has any other (non-bubbled) children. If it doesn't,
250 * removes all remnants of the group's summary from the notification pipeline.
251 * TODO: (b/145659174) Only old pipeline needs this - delete post-migration.
252 */
253 void maybeCancelSummary(NotificationEntry entry);
Mady Mellorf44b6832020-01-14 13:26:14 -0800254 }
255
256 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800257 * Listens for the current state of the status bar and updates the visibility state
258 * of bubbles as needed.
259 */
260 private class StatusBarStateListener implements StatusBarStateController.StateListener {
261 private int mState;
262 /**
263 * Returns the current status bar state.
264 */
265 public int getCurrentState() {
266 return mState;
267 }
268
269 @Override
270 public void onStateChanged(int newState) {
271 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400272 boolean shouldCollapse = (mState != SHADE);
273 if (shouldCollapse) {
274 collapseStack();
275 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700276 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800277 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800278 }
279
Mady Mellor7f234902019-10-20 12:06:29 -0700280 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800281 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700282 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800283 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700284 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700285 ConfigurationController configurationController,
Beverly Taid1e175c2020-03-10 16:37:04 +0000286 NotificationInterruptStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400287 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700288 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700289 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500290 NotificationEntryManager entryManager,
291 NotifPipeline notifPipeline,
292 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500293 DumpManager dumpManager,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400294 FloatingContentCoordinator floatingContentCoordinator,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700295 SysUiState sysUiState,
296 INotificationManager notificationManager) {
wilsonshihe8321942019-10-18 18:39:46 +0800297 this(context, notificationShadeWindowController, statusBarStateController, shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700298 data, null /* synchronizer */, configurationController, interruptionStateProvider,
Beverlya53fb0d2020-01-29 15:26:13 -0500299 zenModeController, notifUserManager, groupManager, entryManager,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700300 notifPipeline, featureFlags, dumpManager, floatingContentCoordinator, sysUiState,
301 notificationManager);
Mady Mellor7f234902019-10-20 12:06:29 -0700302 }
303
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +0100304 /**
305 * Injected constructor. See {@link BubbleModule}.
306 */
Mady Mellor7f234902019-10-20 12:06:29 -0700307 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800308 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700309 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800310 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700311 BubbleData data,
312 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
313 ConfigurationController configurationController,
Beverly Taid1e175c2020-03-10 16:37:04 +0000314 NotificationInterruptStateProvider interruptionStateProvider,
Mady Mellor7f234902019-10-20 12:06:29 -0700315 ZenModeController zenModeController,
316 NotificationLockscreenUserManager notifUserManager,
317 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500318 NotificationEntryManager entryManager,
319 NotifPipeline notifPipeline,
320 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500321 DumpManager dumpManager,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400322 FloatingContentCoordinator floatingContentCoordinator,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700323 SysUiState sysUiState,
324 INotificationManager notificationManager) {
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500325 dumpManager.registerDumpable(TAG, this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800326 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800327 mShadeController = shadeController;
Beverly Taid1e175c2020-03-10 16:37:04 +0000328 mNotificationInterruptStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400329 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400330 mZenModeController = zenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500331 mFloatingContentCoordinator = floatingContentCoordinator;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700332 mINotificationManager = notificationManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400333 mZenModeController.addCallback(new ZenModeController.Callback() {
334 @Override
335 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800336 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400337 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700338 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400339 }
340
341 @Override
342 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800343 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400344 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700345 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400346 }
347 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700348
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700349 configurationController.addCallback(this /* configurationListener */);
Joshua Tsujibe60a582020-03-23 17:17:26 -0400350 mSysUiState = sysUiState;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800351
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400352 mBubbleData = data;
353 mBubbleData.setListener(mBubbleDataListener);
Mady Mellorf44b6832020-01-14 13:26:14 -0800354 mBubbleData.setSuppressionChangedListener(new NotificationSuppressionChangedListener() {
355 @Override
356 public void onBubbleNotificationSuppressionChange(Bubble bubble) {
357 // Make sure NoMan knows it's not showing in the shade anymore so anyone querying it
358 // can tell.
359 try {
360 mBarService.onBubbleNotificationSuppressionChanged(bubble.getKey(),
361 !bubble.showInShade());
362 } catch (RemoteException e) {
363 // Bad things have happened
364 }
365 }
366 });
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400367
Mady Mellor7f234902019-10-20 12:06:29 -0700368 mNotificationEntryManager = entryManager;
Mady Mellor22f2f072019-04-18 13:26:18 -0700369 mNotificationGroupManager = groupManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500370 mNotifPipeline = notifPipeline;
371
372 if (!featureFlags.isNewNotifPipelineRenderingEnabled()) {
373 setupNEM();
374 } else {
375 setupNotifPipeline();
376 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800377
wilsonshihe8321942019-10-18 18:39:46 +0800378 mNotificationShadeWindowController = notificationShadeWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800379 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700380 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500381
Mark Renoufcecc77b2019-01-30 16:32:24 -0500382 mTaskStackListener = new BubbleTaskStackListener();
383 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800384
Joshua Tsujia19515f2019-02-13 18:02:29 -0500385 try {
386 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
387 } catch (RemoteException e) {
388 e.printStackTrace();
389 }
Issei Suzukic0387542019-03-08 17:31:14 +0100390 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700391
392 mBarService = IStatusBarService.Stub.asInterface(
393 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400394
395 mSavedBubbleKeysPerUser = new SparseSetArray<>();
396 mCurrentUserId = mNotifUserManager.getCurrentUserId();
397 mNotifUserManager.addUserChangedListener(
Steve Elliottb47f1c72019-12-19 12:39:26 -0500398 new NotificationLockscreenUserManager.UserChangedListener() {
399 @Override
400 public void onUserChanged(int newUserId) {
401 BubbleController.this.saveBubbles(mCurrentUserId);
402 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
403 BubbleController.this.restoreBubbles(newUserId);
404 mCurrentUserId = newUserId;
405 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400406 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800407
Mady Mellor247ca2c2019-12-02 16:18:59 -0800408 mBubbleIconFactory = new BubbleIconFactory(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800409 }
410
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400411 /**
Beverlyed8aea22020-01-22 16:52:47 -0500412 * See {@link NotifCallback}.
413 */
414 public void addNotifCallback(NotifCallback callback) {
415 mCallbacks.add(callback);
416 }
417
418 private void setupNEM() {
419 mNotificationEntryManager.addNotificationEntryListener(
420 new NotificationEntryListener() {
421 @Override
Mady Mellorf9439ab2020-01-30 16:06:53 -0800422 public void onPendingEntryAdded(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -0500423 onEntryAdded(entry);
424 }
425
426 @Override
427 public void onPreEntryUpdated(NotificationEntry entry) {
428 onEntryUpdated(entry);
429 }
430
431 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500432 public void onEntryRemoved(
433 NotificationEntry entry,
434 @android.annotation.Nullable NotificationVisibility visibility,
Julia Reynolds138111f2020-02-26 11:17:39 -0500435 boolean removedByUser,
436 int reason) {
Beverlya53fb0d2020-01-29 15:26:13 -0500437 BubbleController.this.onEntryRemoved(entry);
438 }
439
440 @Override
Beverlyed8aea22020-01-22 16:52:47 -0500441 public void onNotificationRankingUpdated(RankingMap rankingMap) {
442 onRankingUpdated(rankingMap);
443 }
444 });
445
Evan Laird04373662020-01-24 17:37:39 -0500446 mNotificationEntryManager.addNotificationRemoveInterceptor(
Beverlyed8aea22020-01-22 16:52:47 -0500447 new NotificationRemoveInterceptor() {
448 @Override
Evan Laird04373662020-01-24 17:37:39 -0500449 public boolean onNotificationRemoveRequested(
Beverlya53fb0d2020-01-29 15:26:13 -0500450 String key,
451 NotificationEntry entry,
452 int dismissReason) {
453 final boolean isClearAll = dismissReason == REASON_CANCEL_ALL;
454 final boolean isUserDimiss = dismissReason == REASON_CANCEL
455 || dismissReason == REASON_CLICK;
456 final boolean isAppCancel = dismissReason == REASON_APP_CANCEL
457 || dismissReason == REASON_APP_CANCEL_ALL;
458 final boolean isSummaryCancel =
459 dismissReason == REASON_GROUP_SUMMARY_CANCELED;
460
461 // Need to check for !appCancel here because the notification may have
462 // previously been dismissed & entry.isRowDismissed would still be true
463 boolean userRemovedNotif =
464 (entry != null && entry.isRowDismissed() && !isAppCancel)
465 || isClearAll || isUserDimiss || isSummaryCancel;
466
Mady Mellordd6fe612020-04-15 11:47:55 -0700467 if (userRemovedNotif) {
Beverlya53fb0d2020-01-29 15:26:13 -0500468 return handleDismissalInterception(entry);
469 }
Beverlya53fb0d2020-01-29 15:26:13 -0500470 return false;
Beverlyed8aea22020-01-22 16:52:47 -0500471 }
472 });
473
474 mNotificationGroupManager.addOnGroupChangeListener(
475 new NotificationGroupManager.OnGroupChangeListener() {
476 @Override
477 public void onGroupSuppressionChanged(
478 NotificationGroupManager.NotificationGroup group,
479 boolean suppressed) {
480 // More notifications could be added causing summary to no longer
481 // be suppressed -- in this case need to remove the key.
482 final String groupKey = group.summary != null
483 ? group.summary.getSbn().getGroupKey()
484 : null;
485 if (!suppressed && groupKey != null
486 && mBubbleData.isSummarySuppressed(groupKey)) {
487 mBubbleData.removeSuppressedSummary(groupKey);
488 }
489 }
490 });
491
492 addNotifCallback(new NotifCallback() {
493 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500494 public void removeNotification(NotificationEntry entry, int reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500495 mNotificationEntryManager.performRemoveNotification(entry.getSbn(),
Beverlya53fb0d2020-01-29 15:26:13 -0500496 reason);
Beverlyed8aea22020-01-22 16:52:47 -0500497 }
498
499 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500500 public void invalidateNotifications(String reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500501 mNotificationEntryManager.updateNotifications(reason);
502 }
503
504 @Override
505 public void maybeCancelSummary(NotificationEntry entry) {
506 // Check if removed bubble has an associated suppressed group summary that needs
507 // to be removed now.
Beverlya53fb0d2020-01-29 15:26:13 -0500508 final String groupKey = entry.getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500509 if (mBubbleData.isSummarySuppressed(groupKey)) {
Beverlya53fb0d2020-01-29 15:26:13 -0500510 mBubbleData.removeSuppressedSummary(groupKey);
Beverlyed8aea22020-01-22 16:52:47 -0500511
512 final NotificationEntry summary =
513 mNotificationEntryManager.getActiveNotificationUnfiltered(
514 mBubbleData.getSummaryKey(groupKey));
Beverlya53fb0d2020-01-29 15:26:13 -0500515 if (summary != null) {
516 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
517 UNDEFINED_DISMISS_REASON);
518 }
Beverlyed8aea22020-01-22 16:52:47 -0500519 }
520
Beverlya53fb0d2020-01-29 15:26:13 -0500521 // Check if we still need to remove the summary from NoManGroup because the summary
522 // may not be in the mBubbleData.mSuppressedGroupKeys list and removed above.
523 // For example:
524 // 1. Bubbled notifications (group) is posted to shade and are visible bubbles
525 // 2. User expands bubbles so now their respective notifications in the shade are
526 // hidden, including the group summary
527 // 3. User removes all bubbles
528 // 4. We expect all the removed bubbles AND the summary (note: the summary was
529 // never added to the suppressedSummary list in BubbleData, so we add this check)
Beverlyed8aea22020-01-22 16:52:47 -0500530 NotificationEntry summary =
531 mNotificationGroupManager.getLogicalGroupSummary(entry.getSbn());
532 if (summary != null) {
533 ArrayList<NotificationEntry> summaryChildren =
534 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
535 boolean isSummaryThisNotif = summary.getKey().equals(entry.getKey());
536 if (!isSummaryThisNotif && (summaryChildren == null
537 || summaryChildren.isEmpty())) {
538 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
539 UNDEFINED_DISMISS_REASON);
540 }
541 }
542 }
543 });
544 }
545
Beverlya53fb0d2020-01-29 15:26:13 -0500546 private void setupNotifPipeline() {
547 mNotifPipeline.addCollectionListener(new NotifCollectionListener() {
548 @Override
549 public void onEntryAdded(NotificationEntry entry) {
550 BubbleController.this.onEntryAdded(entry);
551 }
552
553 @Override
554 public void onEntryUpdated(NotificationEntry entry) {
555 BubbleController.this.onEntryUpdated(entry);
556 }
557
558 @Override
559 public void onRankingUpdate(RankingMap rankingMap) {
560 onRankingUpdated(rankingMap);
561 }
562
563 @Override
564 public void onEntryRemoved(NotificationEntry entry,
565 @NotifCollection.CancellationReason int reason) {
566 BubbleController.this.onEntryRemoved(entry);
567 }
568 });
569 }
570
Beverlyed8aea22020-01-22 16:52:47 -0500571 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800572 * Sets whether to perform inflation on the same thread as the caller. This method should only
573 * be used in tests, not in production.
574 */
575 @VisibleForTesting
576 void setInflateSynchronously(boolean inflateSynchronously) {
577 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800578 }
579
Lyn Hanb58c7562020-01-07 14:29:20 -0800580 void setOverflowCallback(Runnable updateOverflow) {
581 mOverflowCallback = updateOverflow;
582 }
583
584 /**
585 * @return Bubbles for updating overflow.
586 */
587 List<Bubble> getOverflowBubbles() {
588 return mBubbleData.getOverflowBubbles();
589 }
590
591
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400592 /**
593 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
594 * method initializes the stack view and adds it to the StatusBar just above the scrim.
595 */
596 private void ensureStackViewCreated() {
597 if (mStackView == null) {
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500598 mStackView = new BubbleStackView(
Joshua Tsujibe60a582020-03-23 17:17:26 -0400599 mContext, mBubbleData, mSurfaceSynchronizer, mFloatingContentCoordinator,
Joshua Tsujiba9fef02020-04-09 17:40:35 -0400600 mSysUiState, mNotificationShadeWindowController);
wilsonshihe8321942019-10-18 18:39:46 +0800601 ViewGroup nsv = mNotificationShadeWindowController.getNotificationShadeView();
602 int bubbleScrimIndex = nsv.indexOfChild(nsv.findViewById(R.id.scrim_for_bubble));
Lyn Hanbde48202019-05-29 19:18:29 -0700603 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
wilsonshihe8321942019-10-18 18:39:46 +0800604 nsv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400605 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
606 if (mExpandListener != null) {
607 mStackView.setExpandListener(mExpandListener);
608 }
Joshua Tsuji6855cab2020-04-16 01:05:39 -0400609
610 mStackView.setUnbubbleConversationCallback(notificationEntry ->
611 onUserChangedBubble(notificationEntry, false /* shouldBubble */));
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400612 }
613 }
614
Mark Renoufc19b4732019-06-26 12:08:33 -0400615 /**
616 * Records the notification key for any active bubbles. These are used to restore active
617 * bubbles when the user returns to the foreground.
618 *
619 * @param userId the id of the user
620 */
621 private void saveBubbles(@UserIdInt int userId) {
622 // First clear any existing keys that might be stored.
623 mSavedBubbleKeysPerUser.remove(userId);
624 // Add in all active bubbles for the current user.
625 for (Bubble bubble: mBubbleData.getBubbles()) {
626 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
627 }
628 }
629
630 /**
631 * Promotes existing notifications to Bubbles if they were previously bubbles.
632 *
633 * @param userId the id of the user
634 */
635 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400636 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
637 if (savedBubbleKeys == null) {
638 // There were no bubbles saved for this used.
639 return;
640 }
Evan Laird181de622019-10-24 09:53:02 -0400641 for (NotificationEntry e :
642 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400643 if (savedBubbleKeys.contains(e.getKey())
Beverly Taid1e175c2020-03-10 16:37:04 +0000644 && mNotificationInterruptStateProvider.shouldBubbleUp(e)
Mark Renoufc19b4732019-06-26 12:08:33 -0400645 && canLaunchInActivityView(mContext, e)) {
646 updateBubble(e, /* suppressFlyout= */ true);
647 }
648 }
649 // Finally, remove the entries for this user now that bubbles are restored.
650 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
651 }
652
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700653 @Override
654 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800655 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700656 }
657
658 @Override
659 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800660 updateForThemeChanges();
661 }
662
663 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700664 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700665 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700666 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800667 mBubbleIconFactory = new BubbleIconFactory(mContext);
668 for (Bubble b: mBubbleData.getBubbles()) {
669 // Reload each bubble
670 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
671 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700672 }
673
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400674 @Override
675 public void onConfigChanged(Configuration newConfig) {
676 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400677 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700678 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400679 }
680 }
681
Mady Mellor5549dd22018-11-06 18:07:34 -0800682 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800683 * Set a listener to be notified when some states of the bubbles change.
684 */
685 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
686 mStateChangeListener = listener;
687 }
688
689 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800690 * Set a listener to be notified of bubble expand events.
691 */
692 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100693 mExpandListener = ((isExpanding, key) -> {
694 if (listener != null) {
695 listener.onBubbleExpandChanged(isExpanding, key);
696 }
wilsonshihe8321942019-10-18 18:39:46 +0800697 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100698 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800699 if (mStackView != null) {
700 mStackView.setExpandListener(mExpandListener);
701 }
702 }
703
704 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800705 * Whether or not there are bubbles present, regardless of them being visible on the
706 * screen (e.g. if on AOD).
707 */
708 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800709 if (mStackView == null) {
710 return false;
711 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400712 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800713 }
714
715 /**
716 * Whether the stack of bubbles is expanded or not.
717 */
718 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400719 return mBubbleData.isExpanded();
720 }
721
722 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800723 * Tell the stack of bubbles to collapse.
724 */
725 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400726 mBubbleData.setExpanded(false /* expanded */);
727 }
728
Mady Mellorce23c462019-06-17 17:30:07 -0700729 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700730 * True if either:
731 * (1) There is a bubble associated with the provided key and if its notification is hidden
732 * from the shade.
733 * (2) There is a group summary associated with the provided key that is hidden from the shade
734 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700735 *
Mady Mellore28fe102019-07-09 15:33:32 -0700736 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700737 */
Beverlyed8aea22020-01-22 16:52:47 -0500738 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
739 String key = entry.getKey();
Lyn Han2f6e89d2020-04-15 10:01:01 -0700740 boolean isSuppressedBubble = (mBubbleData.hasAnyBubbleWithKey(key)
741 && !mBubbleData.getAnyBubbleWithkey(key).showInShade());
Beverlyed8aea22020-01-22 16:52:47 -0500742
743 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700744 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700745 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Lyn Han2f6e89d2020-04-15 10:01:01 -0700746 return (isSummary && isSuppressedSummary) || isSuppressedBubble;
Mady Mellorce23c462019-06-17 17:30:07 -0700747 }
748
Lyn Hanb58c7562020-01-07 14:29:20 -0800749 void promoteBubbleFromOverflow(Bubble bubble) {
Lyn Han1e19d7f2020-02-05 19:10:58 -0800750 bubble.setInflateSynchronously(mInflateSynchronously);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700751 setIsBubble(bubble, /* isBubble */ true);
Lyn Han1e19d7f2020-02-05 19:10:58 -0800752 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
Lyn Hanb58c7562020-01-07 14:29:20 -0800753 }
754
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800755 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400756 * Request the stack expand if needed, then select the specified Bubble as current.
757 *
758 * @param notificationKey the notification key for the bubble to be selected
759 */
760 public void expandStackAndSelectBubble(String notificationKey) {
Lyn Han2f6e89d2020-04-15 10:01:01 -0700761 Bubble bubble = mBubbleData.getBubbleInStackWithKey(notificationKey);
762 if (bubble == null) {
763 bubble = mBubbleData.getOverflowBubbleWithKey(notificationKey);
764 if (bubble != null) {
765 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
766 }
767 } else if (bubble.getEntry().isBubble()){
Mark Renouf71a3af62019-04-08 15:02:54 -0400768 mBubbleData.setSelectedBubble(bubble);
Mark Renouffec45da2019-03-13 13:24:27 -0400769 }
Lyn Han2f6e89d2020-04-15 10:01:01 -0700770 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400771 }
772
773 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500774 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
775 * is forwarded a back key down/up pair.
776 */
777 public void performBackPressIfNeeded() {
778 if (mStackView != null) {
779 mStackView.performBackPressIfNeeded();
780 }
781 }
782
783 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800784 * Adds or updates a bubble associated with the provided notification entry.
785 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400786 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800787 */
Mark Renouff97ed462019-04-05 13:46:24 -0400788 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700789 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400790 }
791
792 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700793 updateBubble(notif, suppressFlyout, true /* showInShade */);
794 }
795
796 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800797 if (mStackView == null) {
798 // Lazy init stack view when a bubble is created
799 ensureStackViewCreated();
800 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700801 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400802 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700803 notif.setInterruption();
804 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800805 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
806 bubble.setInflateSynchronously(mInflateSynchronously);
807 bubble.inflate(
808 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
809 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700810 }
811
812 /**
813 * Called when a user has indicated that an active notification should be shown as a bubble.
814 * <p>
815 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
816 * the notification from appearing in the shade.
817 *
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700818 * @param entry the notification to change bubble state for.
819 * @param shouldBubble whether the notification should show as a bubble or not.
Mady Mellor7f234902019-10-20 12:06:29 -0700820 */
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700821 public void onUserChangedBubble(NotificationEntry entry, boolean shouldBubble) {
822 NotificationChannel channel = entry.getChannel();
823 final String appPkg = entry.getSbn().getPackageName();
824 final int appUid = entry.getSbn().getUid();
825 if (channel == null || appPkg == null) {
826 return;
Mady Mellorff076eb2019-11-13 10:12:06 -0800827 }
Mady Mellor7f234902019-10-20 12:06:29 -0700828
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700829 // Update the state in NotificationManagerService
830 try {
831 int flags = Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION;
832 mBarService.onNotificationBubbleChanged(entry.getKey(), shouldBubble, flags);
833 } catch (RemoteException e) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800834 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700835
836 // Change the settings
837 channel = NotificationChannelHelper.createConversationChannelIfNeeded(mContext,
838 mINotificationManager, entry, channel);
839 channel.setAllowBubbles(shouldBubble);
840 try {
841 int currentPref = mINotificationManager.getBubblePreferenceForPackage(appPkg, appUid);
842 if (shouldBubble && currentPref == BUBBLE_PREFERENCE_NONE) {
843 mINotificationManager.setBubblesAllowed(appPkg, appUid, BUBBLE_PREFERENCE_SELECTED);
844 }
845 mINotificationManager.updateNotificationChannelForPackage(appPkg, appUid, channel);
846 } catch (RemoteException e) {
847 Log.e(TAG, e.getMessage());
848 }
849
850 if (shouldBubble) {
851 mShadeController.collapsePanel(true);
852 if (entry.getRow() != null) {
853 entry.getRow().updateBubbleButton();
854 }
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800855 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800856 }
857
858 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500859 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500860 * <p>
861 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800862 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500863 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500864 void removeBubble(NotificationEntry entry, int reason) {
Lyn Han2f6e89d2020-04-15 10:01:01 -0700865 if (mBubbleData.hasAnyBubbleWithKey(entry.getKey())) {
Beverlya53fb0d2020-01-29 15:26:13 -0500866 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800867 }
868 }
869
Beverlyed8aea22020-01-22 16:52:47 -0500870 private void onEntryAdded(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000871 if (mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700872 && canLaunchInActivityView(mContext, entry)) {
Beverlyed8aea22020-01-22 16:52:47 -0500873 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700874 }
875 }
876
Beverlyed8aea22020-01-22 16:52:47 -0500877 private void onEntryUpdated(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000878 boolean shouldBubble = mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700879 && canLaunchInActivityView(mContext, entry);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700880 if (!shouldBubble && mBubbleData.hasAnyBubbleWithKey(entry.getKey())) {
Beverlyed8aea22020-01-22 16:52:47 -0500881 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500882 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500883 } else if (shouldBubble) {
Beverlyed8aea22020-01-22 16:52:47 -0500884 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800885 }
Beverlyed8aea22020-01-22 16:52:47 -0500886 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800887
Beverlya53fb0d2020-01-29 15:26:13 -0500888 private void onEntryRemoved(NotificationEntry entry) {
889 if (isSummaryOfBubbles(entry)) {
890 final String groupKey = entry.getSbn().getGroupKey();
891 mBubbleData.removeSuppressedSummary(groupKey);
892
893 // Remove any associated bubble children with the summary
894 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
895 for (int i = 0; i < bubbleChildren.size(); i++) {
896 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
897 }
898 } else {
899 removeBubble(entry, DISMISS_NOTIF_CANCEL);
900 }
901 }
902
Mady Mellor56515c42020-02-18 17:58:36 -0800903 /**
904 * Called when NotificationListener has received adjusted notification rank and reapplied
905 * filtering and sorting. This is used to dismiss or create bubbles based on changes in
906 * permissions on the notification channel or the global setting.
907 *
908 * @param rankingMap the updated ranking map from NotificationListenerService
909 */
Beverlyed8aea22020-01-22 16:52:47 -0500910 private void onRankingUpdated(RankingMap rankingMap) {
Mady Mellor56515c42020-02-18 17:58:36 -0800911 if (mTmpRanking == null) {
912 mTmpRanking = new NotificationListenerService.Ranking();
913 }
914 String[] orderedKeys = rankingMap.getOrderedKeys();
915 for (int i = 0; i < orderedKeys.length; i++) {
916 String key = orderedKeys[i];
917 NotificationEntry entry = mNotificationEntryManager.getPendingOrActiveNotif(key);
918 rankingMap.getRanking(key, mTmpRanking);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700919 boolean isActiveBubble = mBubbleData.hasAnyBubbleWithKey(key);
Mady Mellore45ff862020-03-24 15:54:50 -0700920 if (isActiveBubble && !mTmpRanking.canBubble()) {
Mady Mellor56515c42020-02-18 17:58:36 -0800921 mBubbleData.notificationEntryRemoved(entry, BubbleController.DISMISS_BLOCKED);
Mady Mellore45ff862020-03-24 15:54:50 -0700922 } else if (entry != null && mTmpRanking.isBubble() && !isActiveBubble) {
Mady Mellor56515c42020-02-18 17:58:36 -0800923 entry.setFlagBubble(true);
924 onEntryUpdated(entry);
925 }
926 }
Beverlyed8aea22020-01-22 16:52:47 -0500927 }
Ned Burns01e38212019-01-03 16:32:52 -0500928
Lyn Han2f6e89d2020-04-15 10:01:01 -0700929 private void setIsBubble(Bubble b, boolean isBubble) {
930 if (isBubble) {
931 b.getEntry().getSbn().getNotification().flags |= FLAG_BUBBLE;
932 } else {
933 b.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
934 }
935 try {
936 mBarService.onNotificationBubbleChanged(b.getKey(), isBubble, 0);
937 } catch (RemoteException e) {
938 // Bad things have happened
939 }
940 }
941
Mark Renouf71a3af62019-04-08 15:02:54 -0400942 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400943 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400944
Mark Renouf3bc5b362019-04-05 14:37:59 -0400945 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400946 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800947 // Update bubbles in overflow.
948 if (mOverflowCallback != null) {
949 mOverflowCallback.run();
950 }
951
Mark Renouf82a40e62019-05-23 16:16:24 -0400952 // Collapsing? Do this first before remaining steps.
953 if (update.expandedChanged && !update.expanded) {
954 mStackView.setExpanded(false);
955 }
956
957 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700958 ArrayList<Pair<Bubble, Integer>> removedBubbles =
959 new ArrayList<>(update.removedBubbles);
960 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400961 final Bubble bubble = removed.first;
962 @DismissReason final int reason = removed.second;
963 mStackView.removeBubble(bubble);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700964
Mark Renoufc19b4732019-06-26 12:08:33 -0400965 // If the bubble is removed for user switching, leave the notification in place.
Lyn Han2f6e89d2020-04-15 10:01:01 -0700966 if (reason == DISMISS_USER_CHANGED) {
967 continue;
968 }
969 if (!mBubbleData.hasBubbleInStackWithKey(bubble.getKey())) {
970 if (!mBubbleData.hasOverflowBubbleWithKey(bubble.getKey())
971 && (!bubble.showInShade()
972 || reason == DISMISS_NOTIF_CANCEL
973 || reason == DISMISS_GROUP_CANCELLED)) {
Beverlyed8aea22020-01-22 16:52:47 -0500974 // The bubble is now gone & the notification is hidden from the shade, so
975 // time to actually remove it
976 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -0500977 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -0500978 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400979 } else {
Lyn Han2f6e89d2020-04-15 10:01:01 -0700980 if (bubble.getEntry().isBubble() && bubble.showInShade()) {
981 setIsBubble(bubble, /* isBubble */ false);
982 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700983 if (bubble.getEntry().getRow() != null) {
984 bubble.getEntry().getRow().updateBubbleButton();
985 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400986 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700987
Lyn Han2f6e89d2020-04-15 10:01:01 -0700988 }
989 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
990 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
991 // Time to potentially remove the summary
992 for (NotifCallback cb : mCallbacks) {
993 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -0700994 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700995 }
996 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400997
Lyn Hanc47e1712020-01-28 21:43:34 -0800998 if (update.addedBubble != null) {
999 mStackView.addBubble(update.addedBubble);
1000 }
1001
Mark Renouf82a40e62019-05-23 16:16:24 -04001002 if (update.updatedBubble != null) {
1003 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -04001004 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001005
Lyn Hanb58c7562020-01-07 14:29:20 -08001006 // At this point, the correct bubbles are inflated in the stack.
1007 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -04001008 if (update.orderChanged) {
1009 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -04001010 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001011
Mark Renouf82a40e62019-05-23 16:16:24 -04001012 if (update.selectionChanged) {
1013 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -07001014 if (update.selectedBubble != null) {
1015 mNotificationGroupManager.updateSuppression(
1016 update.selectedBubble.getEntry());
1017 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001018 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001019
Mark Renouf82a40e62019-05-23 16:16:24 -04001020 // Expanding? Apply this last.
1021 if (update.expandedChanged && update.expanded) {
1022 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001023 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001024
Beverlyed8aea22020-01-22 16:52:47 -05001025 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001026 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001027 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001028 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001029
Issei Suzukia8d07312019-06-07 12:56:19 +02001030 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001031 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001032 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001033 mBubbleData.getSelectedBubble()));
1034
1035 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001036 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001037 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001038 mStackView.getExpandedBubble()));
1039 }
Lyn Hanb58c7562020-01-07 14:29:20 -08001040 Log.d(TAG, "\n[BubbleData] overflow:");
1041 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
Lyn Han2f6e89d2020-04-15 10:01:01 -07001042 null) + "\n");
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001043 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001044 }
1045 };
1046
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001047 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001048 * We intercept notification entries (including group summaries) dismissed by the user when
1049 * there is an active bubble associated with it. We do this so that developers can still
1050 * cancel it (and hence the bubbles associated with it). However, these intercepted
1051 * notifications should then be hidden from the shade since the user has cancelled them, so we
1052 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1053 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001054 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001055 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001056 */
Beverlya53fb0d2020-01-29 15:26:13 -05001057 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001058 if (entry == null) {
1059 return false;
1060 }
Lyn Han2f6e89d2020-04-15 10:01:01 -07001061 if (isSummaryOfBubbles(entry)) {
Beverlya53fb0d2020-01-29 15:26:13 -05001062 handleSummaryDismissalInterception(entry);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001063 } else {
1064 Bubble bubble = mBubbleData.getBubbleInStackWithKey(entry.getKey());
1065 if (bubble == null || !entry.isBubble()) {
1066 bubble = mBubbleData.getOverflowBubbleWithKey(entry.getKey());
1067 }
1068 if (bubble == null) {
1069 return false;
1070 }
Beverlyed8aea22020-01-22 16:52:47 -05001071 bubble.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001072 bubble.setShowDot(false /* show */);
Beverlyed8aea22020-01-22 16:52:47 -05001073 }
Beverlya53fb0d2020-01-29 15:26:13 -05001074 // Update the shade
1075 for (NotifCallback cb : mCallbacks) {
1076 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1077 }
1078 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001079 }
1080
Beverlya53fb0d2020-01-29 15:26:13 -05001081 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1082 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001083 return false;
1084 }
Beverlya53fb0d2020-01-29 15:26:13 -05001085
1086 String groupKey = entry.getSbn().getGroupKey();
1087 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1088 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1089 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1090 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1091 return (isSuppressedSummary || isSummary)
1092 && bubbleChildren != null
1093 && !bubbleChildren.isEmpty();
1094 }
1095
1096 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1097 // current children in the row:
Kevin Han43077f92020-02-28 12:51:53 -08001098 final List<NotificationEntry> children = summary.getAttachedNotifChildren();
Beverlya53fb0d2020-01-29 15:26:13 -05001099 if (children != null) {
1100 for (int i = 0; i < children.size(); i++) {
1101 NotificationEntry child = children.get(i);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001102 if (mBubbleData.hasAnyBubbleWithKey(child.getKey())) {
Beverlya53fb0d2020-01-29 15:26:13 -05001103 // Suppress the bubbled child
1104 // As far as group manager is concerned, once a child is no longer shown
1105 // in the shade, it is essentially removed.
Lyn Han2f6e89d2020-04-15 10:01:01 -07001106 Bubble bubbleChild = mBubbleData.getAnyBubbleWithkey(child.getKey());
Beverlya53fb0d2020-01-29 15:26:13 -05001107 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1108 bubbleChild.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001109 bubbleChild.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001110 } else {
1111 // non-bubbled children can be removed
1112 for (NotifCallback cb : mCallbacks) {
1113 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1114 }
1115 }
1116 }
1117 }
1118
1119 // And since all children are removed, remove the summary.
1120 mNotificationGroupManager.onEntryRemoved(summary);
1121
1122 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1123 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1124 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001125 }
1126
1127 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001128 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001129 * Updates the visibility of the bubbles based on current state.
1130 * Does not un-bubble, just hides or un-hides. Notifies any
1131 * {@link BubbleStateChangeListener}s of visibility changes.
1132 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001133 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001134 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001135 if (mStackView == null) {
1136 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001137 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001138 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1139 // Bubbles only appear in unlocked shade
1140 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001141 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001142 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001143 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001144
Mady Mellor698d9e82019-08-01 23:11:53 +00001145 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001146 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001147 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001148 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001149 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1150 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1151 }
1152
1153 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001154 }
1155
1156 /**
1157 * Rect indicating the touchable region for the bubble stack / expanded stack.
1158 */
1159 public Rect getTouchableRegion() {
1160 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1161 return null;
1162 }
1163 mStackView.getBoundsOnScreen(mTempRect);
1164 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001165 }
1166
Mady Mellor390bff42019-04-05 15:09:01 -07001167 /**
1168 * The display id of the expanded view, if the stack is expanded and not occluded by the
1169 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1170 */
1171 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001172 if (mStackView == null) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001173 return INVALID_DISPLAY;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001174 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001175 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001176 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Lyn Han9f66c3b2020-03-05 23:59:29 -08001177 final BubbleViewProvider expandedViewProvider = mStackView.getExpandedBubble();
1178 if (defaultDisplay && expandedViewProvider != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001179 && !mNotificationShadeWindowController.getPanelExpanded()) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001180 return expandedViewProvider.getDisplayId();
Mady Mellor390bff42019-04-05 15:09:01 -07001181 }
Lyn Han9f66c3b2020-03-05 23:59:29 -08001182 return INVALID_DISPLAY;
Mady Mellor390bff42019-04-05 15:09:01 -07001183 }
1184
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001185 @VisibleForTesting
1186 BubbleStackView getStackView() {
1187 return mStackView;
1188 }
1189
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001190 /**
1191 * Description of current bubble state.
1192 */
1193 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1194 pw.println("BubbleController state:");
1195 mBubbleData.dump(fd, pw, args);
1196 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001197 if (mStackView != null) {
1198 mStackView.dump(fd, pw, args);
1199 }
1200 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001201 }
1202
Mady Mellore80930e2019-03-21 16:00:45 -07001203 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001204 * This task stack listener is responsible for responding to tasks moved to the front
1205 * which are on the default (main) display. When this happens, expanded bubbles must be
1206 * collapsed so the user may interact with the app which was just moved to the front.
1207 * <p>
1208 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1209 * these calls via a main thread Handler.
1210 */
1211 @MainThread
1212 private class BubbleTaskStackListener extends TaskStackChangeListener {
1213
Mark Renoufcecc77b2019-01-30 16:32:24 -05001214 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001215 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1216 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001217 if (!mStackView.isExpansionAnimating()) {
1218 mBubbleData.setExpanded(false);
1219 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001220 }
1221 }
1222
Mark Renoufcecc77b2019-01-30 16:32:24 -05001223 @Override
Winson Chunge789ff62020-02-24 14:40:23 -08001224 public void onActivityRestartAttempt(RunningTaskInfo task, boolean homeTaskVisible,
1225 boolean clearedTask) {
1226 for (Bubble b : mBubbleData.getBubbles()) {
1227 if (b.getDisplayId() == task.displayId) {
1228 expandStackAndSelectBubble(b.getKey());
1229 return;
1230 }
1231 }
1232 }
1233
1234 @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) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001258 final BubbleViewProvider expandedBubble = mStackView != null
Mady Mellor5186b132019-09-16 17:55:48 -07001259 ? 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 Melloraa9ce172020-03-17 10:34:20 -07001280 ? entry.getBubbleMetadata().getIntent()
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) {
Joshua Tsujiff6b0f22020-03-09 14:55:19 -04001313 if (mStackView != null) {
Joshua Tsujid9422832019-03-05 13:32:37 -05001314 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}