blob: a1fff9412eb2f1b8ab949b5b9865f693a4d022e7 [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;
Lyn Han4cbc1422020-04-17 11:08:53 -070057import android.os.Handler;
Mark Renoufcecc77b2019-01-30 16:32:24 -050058import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080059import android.os.ServiceManager;
Mady Mellor56515c42020-02-18 17:58:36 -080060import android.service.notification.NotificationListenerService;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040061import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040062import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040063import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040064import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040065import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040066import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050067import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080068import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080069import android.widget.FrameLayout;
70
Mark Renouf08bc42a2019-03-07 13:01:59 -050071import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050072import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040073import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050074
Mady Mellorebdbbb92018-11-15 14:36:48 -080075import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070076import com.android.internal.statusbar.IStatusBarService;
Beverlya53fb0d2020-01-29 15:26:13 -050077import com.android.internal.statusbar.NotificationVisibility;
Beverlya53fb0d2020-01-29 15:26:13 -050078import com.android.systemui.Dumpable;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080079import com.android.systemui.R;
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +010080import com.android.systemui.bubbles.dagger.BubbleModule;
Ned Burnsaaeb44b2020-02-12 23:48:26 -050081import com.android.systemui.dump.DumpManager;
Joshua Tsujibe60a582020-03-23 17:17:26 -040082import com.android.systemui.model.SysUiState;
Beverly8fdb5332019-02-04 14:29:49 -050083import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050084import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000085import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050086import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050087import com.android.systemui.shared.system.WindowManagerWrapper;
Beverlya53fb0d2020-01-29 15:26:13 -050088import com.android.systemui.statusbar.FeatureFlags;
Mark Renoufc19b4732019-06-26 12:08:33 -040089import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070090import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Mady Mellor9adfe6a2020-03-30 17:23:26 -070091import com.android.systemui.statusbar.notification.NotificationChannelHelper;
Ned Burns01e38212019-01-03 16:32:52 -050092import com.android.systemui.statusbar.notification.NotificationEntryListener;
93import com.android.systemui.statusbar.notification.NotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -050094import com.android.systemui.statusbar.notification.collection.NotifCollection;
95import com.android.systemui.statusbar.notification.collection.NotifPipeline;
Ned Burnsf81c4c42019-01-07 14:10:43 -050096import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Beverlya53fb0d2020-01-29 15:26:13 -050097import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener;
Beverly Taid1e175c2020-03-10 16:37:04 +000098import com.android.systemui.statusbar.notification.interruption.NotificationInterruptStateProvider;
Mady Mellor22f2f072019-04-18 13:26:18 -070099import com.android.systemui.statusbar.phone.NotificationGroupManager;
wilsonshihe8321942019-10-18 18:39:46 +0800100import com.android.systemui.statusbar.phone.NotificationShadeWindowController;
Mady Mellor7f234902019-10-20 12:06:29 -0700101import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorf3b9fab2019-11-13 17:27:32 -0800102import com.android.systemui.statusbar.phone.StatusBar;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700103import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400104import com.android.systemui.statusbar.policy.ZenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500105import com.android.systemui.util.FloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800106
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700107import java.io.FileDescriptor;
108import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500109import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -0400110import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -0700111import java.util.ArrayList;
Lyn Hanb58c7562020-01-07 14:29:20 -0800112import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500113
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800114/**
115 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
116 * Bubbles can be expanded to show more content.
117 *
118 * The controller manages addition, removal, and visible state of bubbles on screen.
119 */
Beverlya53fb0d2020-01-29 15:26:13 -0500120public class BubbleController implements ConfigurationController.ConfigurationListener, Dumpable {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800121
Issei Suzukia8d07312019-06-07 12:56:19 +0200122 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800123
Mark Renouf08bc42a2019-03-07 13:01:59 -0500124 @Retention(SOURCE)
125 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400126 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700127 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
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;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500141
Ned Burns01e38212019-01-03 16:32:52 -0500142 private final Context mContext;
143 private final NotificationEntryManager mNotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500144 private final NotifPipeline mNotifPipeline;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500145 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800146 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800147 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100148 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700149 private final NotificationGroupManager mNotificationGroupManager;
Heemin Seogba6337f2019-12-10 15:34:37 -0800150 private final ShadeController mShadeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500151 private final FloatingContentCoordinator mFloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800152
Mady Mellor3dff9e62019-02-05 18:12:53 -0800153 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400154 @Nullable private BubbleStackView mStackView;
Mady Mellor247ca2c2019-12-02 16:18:59 -0800155 private BubbleIconFactory mBubbleIconFactory;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800156
Mark Renoufc19b4732019-06-26 12:08:33 -0400157 // Tracks the id of the current (foreground) user.
158 private int mCurrentUserId;
159 // Saves notification keys of active bubbles when users are switched.
160 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
161
Mady Mellor56515c42020-02-18 17:58:36 -0800162 // Used when ranking updates occur and we check if things should bubble / unbubble
163 private NotificationListenerService.Ranking mTmpRanking;
164
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800165 // Bubbles get added to the status bar view
wilsonshihe8321942019-10-18 18:39:46 +0800166 private final NotificationShadeWindowController mNotificationShadeWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400167 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800168 private StatusBarStateListener mStatusBarStateListener;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700169 private INotificationManager mINotificationManager;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500170
Lyn Hanb58c7562020-01-07 14:29:20 -0800171 // Callback that updates BubbleOverflowActivity on data change.
172 @Nullable private Runnable mOverflowCallback = null;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800173
Beverly Taid1e175c2020-03-10 16:37:04 +0000174 private final NotificationInterruptStateProvider mNotificationInterruptStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700175 private IStatusBarService mBarService;
Joshua Tsujibe60a582020-03-23 17:17:26 -0400176 private SysUiState mSysUiState;
Mady Mellorb4991e62019-01-10 15:14:51 -0800177
Lyn Han4cbc1422020-04-17 11:08:53 -0700178 // Used to post to main UI thread
179 private Handler mHandler = new Handler();
180
Mady Mellord1c78b262018-11-06 18:04:40 -0800181 // Used for determining view rect for touch interaction
182 private Rect mTempRect = new Rect();
183
Mark Renoufc19b4732019-06-26 12:08:33 -0400184 // Listens to user switch so bubbles can be saved and restored.
185 private final NotificationLockscreenUserManager mNotifUserManager;
186
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400187 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
188 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
189
Mady Mellor3df7ab02019-12-09 15:07:10 -0800190 private boolean mInflateSynchronously;
191
Beverlyed8aea22020-01-22 16:52:47 -0500192 // TODO (b/145659174): allow for multiple callbacks to support the "shadow" new notif pipeline
193 private final List<NotifCallback> mCallbacks = new ArrayList<>();
194
Mady Mellor5549dd22018-11-06 18:07:34 -0800195 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800196 * Listener to be notified when some states of the bubbles change.
197 */
198 public interface BubbleStateChangeListener {
199 /**
200 * Called when the stack has bubbles or no longer has bubbles.
201 */
202 void onHasBubblesChanged(boolean hasBubbles);
203 }
204
Mady Mellorcd9b1302018-11-06 18:08:04 -0800205 /**
206 * Listener to find out about stack expansion / collapse events.
207 */
208 public interface BubbleExpandListener {
209 /**
210 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700211 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800212 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800213 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800214 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800215 void onBubbleExpandChanged(boolean isExpanding, String key);
216 }
217
218 /**
Mady Mellorf44b6832020-01-14 13:26:14 -0800219 * Listener to be notified when a bubbles' notification suppression state changes.
220 */
221 public interface NotificationSuppressionChangedListener {
222 /**
223 * Called when the notification suppression state of a bubble changes.
224 */
225 void onBubbleNotificationSuppressionChange(Bubble bubble);
Beverlyed8aea22020-01-22 16:52:47 -0500226 }
Mady Mellorf44b6832020-01-14 13:26:14 -0800227
Beverlyed8aea22020-01-22 16:52:47 -0500228 /**
229 * Callback for when the BubbleController wants to interact with the notification pipeline to:
230 * - Remove a previously bubbled notification
231 * - Update the notification shade since bubbled notification should/shouldn't be showing
232 */
233 public interface NotifCallback {
234 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500235 * Called when a bubbled notification that was hidden from the shade is now being removed
236 * This can happen when an app cancels a bubbled notification or when the user dismisses a
237 * bubble.
Beverlyed8aea22020-01-22 16:52:47 -0500238 */
Beverlya53fb0d2020-01-29 15:26:13 -0500239 void removeNotification(NotificationEntry entry, int reason);
Beverlyed8aea22020-01-22 16:52:47 -0500240
241 /**
242 * Called when a bubbled notification has changed whether it should be
243 * filtered from the shade.
244 */
Beverlya53fb0d2020-01-29 15:26:13 -0500245 void invalidateNotifications(String reason);
Beverlyed8aea22020-01-22 16:52:47 -0500246
247 /**
248 * Called on a bubbled entry that has been removed when there are no longer
249 * bubbled entries in its group.
250 *
251 * Checks whether its group has any other (non-bubbled) children. If it doesn't,
252 * removes all remnants of the group's summary from the notification pipeline.
253 * TODO: (b/145659174) Only old pipeline needs this - delete post-migration.
254 */
255 void maybeCancelSummary(NotificationEntry entry);
Mady Mellorf44b6832020-01-14 13:26:14 -0800256 }
257
258 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800259 * Listens for the current state of the status bar and updates the visibility state
260 * of bubbles as needed.
261 */
262 private class StatusBarStateListener implements StatusBarStateController.StateListener {
263 private int mState;
264 /**
265 * Returns the current status bar state.
266 */
267 public int getCurrentState() {
268 return mState;
269 }
270
271 @Override
272 public void onStateChanged(int newState) {
273 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400274 boolean shouldCollapse = (mState != SHADE);
275 if (shouldCollapse) {
276 collapseStack();
277 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700278 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800279 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800280 }
281
Mady Mellor7f234902019-10-20 12:06:29 -0700282 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800283 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700284 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800285 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700286 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700287 ConfigurationController configurationController,
Beverly Taid1e175c2020-03-10 16:37:04 +0000288 NotificationInterruptStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400289 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700290 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700291 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500292 NotificationEntryManager entryManager,
293 NotifPipeline notifPipeline,
294 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500295 DumpManager dumpManager,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400296 FloatingContentCoordinator floatingContentCoordinator,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700297 SysUiState sysUiState,
298 INotificationManager notificationManager) {
wilsonshihe8321942019-10-18 18:39:46 +0800299 this(context, notificationShadeWindowController, statusBarStateController, shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700300 data, null /* synchronizer */, configurationController, interruptionStateProvider,
Beverlya53fb0d2020-01-29 15:26:13 -0500301 zenModeController, notifUserManager, groupManager, entryManager,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700302 notifPipeline, featureFlags, dumpManager, floatingContentCoordinator, sysUiState,
303 notificationManager);
Mady Mellor7f234902019-10-20 12:06:29 -0700304 }
305
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +0100306 /**
307 * Injected constructor. See {@link BubbleModule}.
308 */
Mady Mellor7f234902019-10-20 12:06:29 -0700309 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800310 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700311 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800312 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700313 BubbleData data,
314 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
315 ConfigurationController configurationController,
Beverly Taid1e175c2020-03-10 16:37:04 +0000316 NotificationInterruptStateProvider interruptionStateProvider,
Mady Mellor7f234902019-10-20 12:06:29 -0700317 ZenModeController zenModeController,
318 NotificationLockscreenUserManager notifUserManager,
319 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500320 NotificationEntryManager entryManager,
321 NotifPipeline notifPipeline,
322 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500323 DumpManager dumpManager,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400324 FloatingContentCoordinator floatingContentCoordinator,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700325 SysUiState sysUiState,
326 INotificationManager notificationManager) {
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500327 dumpManager.registerDumpable(TAG, this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800328 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800329 mShadeController = shadeController;
Beverly Taid1e175c2020-03-10 16:37:04 +0000330 mNotificationInterruptStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400331 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400332 mZenModeController = zenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500333 mFloatingContentCoordinator = floatingContentCoordinator;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700334 mINotificationManager = notificationManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400335 mZenModeController.addCallback(new ZenModeController.Callback() {
336 @Override
337 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800338 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400339 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700340 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400341 }
342
343 @Override
344 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800345 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400346 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700347 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400348 }
349 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700350
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700351 configurationController.addCallback(this /* configurationListener */);
Joshua Tsujibe60a582020-03-23 17:17:26 -0400352 mSysUiState = sysUiState;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800353
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400354 mBubbleData = data;
355 mBubbleData.setListener(mBubbleDataListener);
Mady Mellorf44b6832020-01-14 13:26:14 -0800356 mBubbleData.setSuppressionChangedListener(new NotificationSuppressionChangedListener() {
357 @Override
358 public void onBubbleNotificationSuppressionChange(Bubble bubble) {
359 // Make sure NoMan knows it's not showing in the shade anymore so anyone querying it
360 // can tell.
361 try {
362 mBarService.onBubbleNotificationSuppressionChanged(bubble.getKey(),
363 !bubble.showInShade());
364 } catch (RemoteException e) {
365 // Bad things have happened
366 }
367 }
368 });
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400369
Mady Mellor7f234902019-10-20 12:06:29 -0700370 mNotificationEntryManager = entryManager;
Mady Mellor22f2f072019-04-18 13:26:18 -0700371 mNotificationGroupManager = groupManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500372 mNotifPipeline = notifPipeline;
373
374 if (!featureFlags.isNewNotifPipelineRenderingEnabled()) {
375 setupNEM();
376 } else {
377 setupNotifPipeline();
378 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800379
wilsonshihe8321942019-10-18 18:39:46 +0800380 mNotificationShadeWindowController = notificationShadeWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800381 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700382 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500383
Mark Renoufcecc77b2019-01-30 16:32:24 -0500384 mTaskStackListener = new BubbleTaskStackListener();
385 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800386
Joshua Tsujia19515f2019-02-13 18:02:29 -0500387 try {
388 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
389 } catch (RemoteException e) {
390 e.printStackTrace();
391 }
Issei Suzukic0387542019-03-08 17:31:14 +0100392 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700393
394 mBarService = IStatusBarService.Stub.asInterface(
395 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400396
397 mSavedBubbleKeysPerUser = new SparseSetArray<>();
398 mCurrentUserId = mNotifUserManager.getCurrentUserId();
399 mNotifUserManager.addUserChangedListener(
Steve Elliottb47f1c72019-12-19 12:39:26 -0500400 new NotificationLockscreenUserManager.UserChangedListener() {
401 @Override
402 public void onUserChanged(int newUserId) {
403 BubbleController.this.saveBubbles(mCurrentUserId);
404 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
405 BubbleController.this.restoreBubbles(newUserId);
406 mCurrentUserId = newUserId;
407 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400408 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800409
Mady Mellor247ca2c2019-12-02 16:18:59 -0800410 mBubbleIconFactory = new BubbleIconFactory(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800411 }
412
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400413 /**
Beverlyed8aea22020-01-22 16:52:47 -0500414 * See {@link NotifCallback}.
415 */
416 public void addNotifCallback(NotifCallback callback) {
417 mCallbacks.add(callback);
418 }
419
420 private void setupNEM() {
421 mNotificationEntryManager.addNotificationEntryListener(
422 new NotificationEntryListener() {
423 @Override
Mady Mellorf9439ab2020-01-30 16:06:53 -0800424 public void onPendingEntryAdded(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -0500425 onEntryAdded(entry);
426 }
427
428 @Override
429 public void onPreEntryUpdated(NotificationEntry entry) {
430 onEntryUpdated(entry);
431 }
432
433 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500434 public void onEntryRemoved(
435 NotificationEntry entry,
436 @android.annotation.Nullable NotificationVisibility visibility,
Julia Reynolds138111f2020-02-26 11:17:39 -0500437 boolean removedByUser,
438 int reason) {
Beverlya53fb0d2020-01-29 15:26:13 -0500439 BubbleController.this.onEntryRemoved(entry);
440 }
441
442 @Override
Beverlyed8aea22020-01-22 16:52:47 -0500443 public void onNotificationRankingUpdated(RankingMap rankingMap) {
444 onRankingUpdated(rankingMap);
445 }
446 });
447
Evan Laird04373662020-01-24 17:37:39 -0500448 mNotificationEntryManager.addNotificationRemoveInterceptor(
Beverlyed8aea22020-01-22 16:52:47 -0500449 new NotificationRemoveInterceptor() {
450 @Override
Evan Laird04373662020-01-24 17:37:39 -0500451 public boolean onNotificationRemoveRequested(
Beverlya53fb0d2020-01-29 15:26:13 -0500452 String key,
453 NotificationEntry entry,
454 int dismissReason) {
455 final boolean isClearAll = dismissReason == REASON_CANCEL_ALL;
456 final boolean isUserDimiss = dismissReason == REASON_CANCEL
457 || dismissReason == REASON_CLICK;
458 final boolean isAppCancel = dismissReason == REASON_APP_CANCEL
459 || dismissReason == REASON_APP_CANCEL_ALL;
460 final boolean isSummaryCancel =
461 dismissReason == REASON_GROUP_SUMMARY_CANCELED;
462
463 // Need to check for !appCancel here because the notification may have
464 // previously been dismissed & entry.isRowDismissed would still be true
465 boolean userRemovedNotif =
466 (entry != null && entry.isRowDismissed() && !isAppCancel)
467 || isClearAll || isUserDimiss || isSummaryCancel;
468
Mady Mellordd6fe612020-04-15 11:47:55 -0700469 if (userRemovedNotif) {
Beverlya53fb0d2020-01-29 15:26:13 -0500470 return handleDismissalInterception(entry);
471 }
472
473 return false;
Beverlyed8aea22020-01-22 16:52:47 -0500474 }
475 });
476
477 mNotificationGroupManager.addOnGroupChangeListener(
478 new NotificationGroupManager.OnGroupChangeListener() {
479 @Override
480 public void onGroupSuppressionChanged(
481 NotificationGroupManager.NotificationGroup group,
482 boolean suppressed) {
483 // More notifications could be added causing summary to no longer
484 // be suppressed -- in this case need to remove the key.
485 final String groupKey = group.summary != null
486 ? group.summary.getSbn().getGroupKey()
487 : null;
488 if (!suppressed && groupKey != null
489 && mBubbleData.isSummarySuppressed(groupKey)) {
490 mBubbleData.removeSuppressedSummary(groupKey);
491 }
492 }
493 });
494
495 addNotifCallback(new NotifCallback() {
496 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500497 public void removeNotification(NotificationEntry entry, int reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500498 mNotificationEntryManager.performRemoveNotification(entry.getSbn(),
Beverlya53fb0d2020-01-29 15:26:13 -0500499 reason);
Beverlyed8aea22020-01-22 16:52:47 -0500500 }
501
502 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500503 public void invalidateNotifications(String reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500504 mNotificationEntryManager.updateNotifications(reason);
505 }
506
507 @Override
508 public void maybeCancelSummary(NotificationEntry entry) {
509 // Check if removed bubble has an associated suppressed group summary that needs
510 // to be removed now.
Beverlya53fb0d2020-01-29 15:26:13 -0500511 final String groupKey = entry.getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500512 if (mBubbleData.isSummarySuppressed(groupKey)) {
Beverlya53fb0d2020-01-29 15:26:13 -0500513 mBubbleData.removeSuppressedSummary(groupKey);
Beverlyed8aea22020-01-22 16:52:47 -0500514
515 final NotificationEntry summary =
516 mNotificationEntryManager.getActiveNotificationUnfiltered(
517 mBubbleData.getSummaryKey(groupKey));
Beverlya53fb0d2020-01-29 15:26:13 -0500518 if (summary != null) {
519 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
520 UNDEFINED_DISMISS_REASON);
521 }
Beverlyed8aea22020-01-22 16:52:47 -0500522 }
523
Beverlya53fb0d2020-01-29 15:26:13 -0500524 // Check if we still need to remove the summary from NoManGroup because the summary
525 // may not be in the mBubbleData.mSuppressedGroupKeys list and removed above.
526 // For example:
527 // 1. Bubbled notifications (group) is posted to shade and are visible bubbles
528 // 2. User expands bubbles so now their respective notifications in the shade are
529 // hidden, including the group summary
530 // 3. User removes all bubbles
531 // 4. We expect all the removed bubbles AND the summary (note: the summary was
532 // never added to the suppressedSummary list in BubbleData, so we add this check)
Beverlyed8aea22020-01-22 16:52:47 -0500533 NotificationEntry summary =
534 mNotificationGroupManager.getLogicalGroupSummary(entry.getSbn());
535 if (summary != null) {
536 ArrayList<NotificationEntry> summaryChildren =
537 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
538 boolean isSummaryThisNotif = summary.getKey().equals(entry.getKey());
539 if (!isSummaryThisNotif && (summaryChildren == null
540 || summaryChildren.isEmpty())) {
541 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
542 UNDEFINED_DISMISS_REASON);
543 }
544 }
545 }
546 });
547 }
548
Beverlya53fb0d2020-01-29 15:26:13 -0500549 private void setupNotifPipeline() {
550 mNotifPipeline.addCollectionListener(new NotifCollectionListener() {
551 @Override
552 public void onEntryAdded(NotificationEntry entry) {
553 BubbleController.this.onEntryAdded(entry);
554 }
555
556 @Override
557 public void onEntryUpdated(NotificationEntry entry) {
558 BubbleController.this.onEntryUpdated(entry);
559 }
560
561 @Override
562 public void onRankingUpdate(RankingMap rankingMap) {
563 onRankingUpdated(rankingMap);
564 }
565
566 @Override
567 public void onEntryRemoved(NotificationEntry entry,
568 @NotifCollection.CancellationReason int reason) {
569 BubbleController.this.onEntryRemoved(entry);
570 }
571 });
572 }
573
Beverlyed8aea22020-01-22 16:52:47 -0500574 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800575 * Sets whether to perform inflation on the same thread as the caller. This method should only
576 * be used in tests, not in production.
577 */
578 @VisibleForTesting
579 void setInflateSynchronously(boolean inflateSynchronously) {
580 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800581 }
582
Lyn Hanb58c7562020-01-07 14:29:20 -0800583 void setOverflowCallback(Runnable updateOverflow) {
584 mOverflowCallback = updateOverflow;
585 }
586
587 /**
588 * @return Bubbles for updating overflow.
589 */
590 List<Bubble> getOverflowBubbles() {
591 return mBubbleData.getOverflowBubbles();
592 }
593
594
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400595 /**
596 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
597 * method initializes the stack view and adds it to the StatusBar just above the scrim.
598 */
599 private void ensureStackViewCreated() {
600 if (mStackView == null) {
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500601 mStackView = new BubbleStackView(
Joshua Tsujibe60a582020-03-23 17:17:26 -0400602 mContext, mBubbleData, mSurfaceSynchronizer, mFloatingContentCoordinator,
Joshua Tsujiba9fef02020-04-09 17:40:35 -0400603 mSysUiState, mNotificationShadeWindowController);
wilsonshihe8321942019-10-18 18:39:46 +0800604 ViewGroup nsv = mNotificationShadeWindowController.getNotificationShadeView();
605 int bubbleScrimIndex = nsv.indexOfChild(nsv.findViewById(R.id.scrim_for_bubble));
Lyn Hanbde48202019-05-29 19:18:29 -0700606 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
wilsonshihe8321942019-10-18 18:39:46 +0800607 nsv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400608 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
609 if (mExpandListener != null) {
610 mStackView.setExpandListener(mExpandListener);
611 }
Joshua Tsuji6855cab2020-04-16 01:05:39 -0400612
613 mStackView.setUnbubbleConversationCallback(notificationEntry ->
614 onUserChangedBubble(notificationEntry, false /* shouldBubble */));
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400615 }
616 }
617
Mark Renoufc19b4732019-06-26 12:08:33 -0400618 /**
619 * Records the notification key for any active bubbles. These are used to restore active
620 * bubbles when the user returns to the foreground.
621 *
622 * @param userId the id of the user
623 */
624 private void saveBubbles(@UserIdInt int userId) {
625 // First clear any existing keys that might be stored.
626 mSavedBubbleKeysPerUser.remove(userId);
627 // Add in all active bubbles for the current user.
628 for (Bubble bubble: mBubbleData.getBubbles()) {
629 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
630 }
631 }
632
633 /**
634 * Promotes existing notifications to Bubbles if they were previously bubbles.
635 *
636 * @param userId the id of the user
637 */
638 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400639 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
640 if (savedBubbleKeys == null) {
641 // There were no bubbles saved for this used.
642 return;
643 }
Evan Laird181de622019-10-24 09:53:02 -0400644 for (NotificationEntry e :
645 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400646 if (savedBubbleKeys.contains(e.getKey())
Beverly Taid1e175c2020-03-10 16:37:04 +0000647 && mNotificationInterruptStateProvider.shouldBubbleUp(e)
Mark Renoufc19b4732019-06-26 12:08:33 -0400648 && canLaunchInActivityView(mContext, e)) {
649 updateBubble(e, /* suppressFlyout= */ true);
650 }
651 }
652 // Finally, remove the entries for this user now that bubbles are restored.
653 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
654 }
655
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700656 @Override
657 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800658 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700659 }
660
661 @Override
662 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800663 updateForThemeChanges();
664 }
665
666 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700667 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700668 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700669 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800670 mBubbleIconFactory = new BubbleIconFactory(mContext);
671 for (Bubble b: mBubbleData.getBubbles()) {
672 // Reload each bubble
673 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
674 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700675 }
676
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400677 @Override
678 public void onConfigChanged(Configuration newConfig) {
679 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400680 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700681 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400682 }
683 }
684
Mady Mellor5549dd22018-11-06 18:07:34 -0800685 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800686 * Set a listener to be notified when some states of the bubbles change.
687 */
688 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
689 mStateChangeListener = listener;
690 }
691
692 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800693 * Set a listener to be notified of bubble expand events.
694 */
695 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100696 mExpandListener = ((isExpanding, key) -> {
697 if (listener != null) {
698 listener.onBubbleExpandChanged(isExpanding, key);
699 }
wilsonshihe8321942019-10-18 18:39:46 +0800700 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100701 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800702 if (mStackView != null) {
703 mStackView.setExpandListener(mExpandListener);
704 }
705 }
706
707 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800708 * Whether or not there are bubbles present, regardless of them being visible on the
709 * screen (e.g. if on AOD).
710 */
711 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800712 if (mStackView == null) {
713 return false;
714 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400715 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800716 }
717
718 /**
719 * Whether the stack of bubbles is expanded or not.
720 */
721 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400722 return mBubbleData.isExpanded();
723 }
724
725 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800726 * Tell the stack of bubbles to collapse.
727 */
728 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400729 mBubbleData.setExpanded(false /* expanded */);
730 }
731
Mady Mellorce23c462019-06-17 17:30:07 -0700732 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700733 * True if either:
734 * (1) There is a bubble associated with the provided key and if its notification is hidden
735 * from the shade.
736 * (2) There is a group summary associated with the provided key that is hidden from the shade
737 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700738 *
Mady Mellore28fe102019-07-09 15:33:32 -0700739 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700740 */
Beverlyed8aea22020-01-22 16:52:47 -0500741 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
742 String key = entry.getKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700743 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800744 && !mBubbleData.getBubbleWithKey(key).showInShade();
Beverlyed8aea22020-01-22 16:52:47 -0500745
746 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700747 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700748 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Beverlyed8aea22020-01-22 16:52:47 -0500749
Mady Mellore4348272019-07-29 17:43:36 -0700750 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700751 }
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 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500772 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
773 * is forwarded a back key down/up pair.
774 */
775 public void performBackPressIfNeeded() {
776 if (mStackView != null) {
777 mStackView.performBackPressIfNeeded();
778 }
779 }
780
781 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800782 * Adds or updates a bubble associated with the provided notification entry.
783 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400784 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800785 */
Mark Renouff97ed462019-04-05 13:46:24 -0400786 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700787 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400788 }
789
790 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700791 updateBubble(notif, suppressFlyout, true /* showInShade */);
792 }
793
794 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800795 if (mStackView == null) {
796 // Lazy init stack view when a bubble is created
797 ensureStackViewCreated();
798 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700799 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400800 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700801 notif.setInterruption();
802 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800803 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
804 bubble.setInflateSynchronously(mInflateSynchronously);
805 bubble.inflate(
Lyn Han4cbc1422020-04-17 11:08:53 -0700806 b -> {
807 mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade);
808 if (bubble.getBubbleIntent() == null) {
809 return;
810 }
811 bubble.getBubbleIntent().registerCancelListener(pendingIntent -> {
812 mHandler.post(
813 () -> removeBubble(bubble.getEntry(),
814 BubbleController.DISMISS_INVALID_INTENT));
815 });
816 },
Mady Mellor3df7ab02019-12-09 15:07:10 -0800817 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700818 }
819
820 /**
821 * Called when a user has indicated that an active notification should be shown as a bubble.
822 * <p>
823 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
824 * the notification from appearing in the shade.
825 *
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700826 * @param entry the notification to change bubble state for.
827 * @param shouldBubble whether the notification should show as a bubble or not.
Mady Mellor7f234902019-10-20 12:06:29 -0700828 */
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700829 public void onUserChangedBubble(NotificationEntry entry, boolean shouldBubble) {
830 NotificationChannel channel = entry.getChannel();
831 final String appPkg = entry.getSbn().getPackageName();
832 final int appUid = entry.getSbn().getUid();
833 if (channel == null || appPkg == null) {
834 return;
Mady Mellorff076eb2019-11-13 10:12:06 -0800835 }
Mady Mellor7f234902019-10-20 12:06:29 -0700836
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700837 // Update the state in NotificationManagerService
838 try {
839 int flags = Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION;
840 mBarService.onNotificationBubbleChanged(entry.getKey(), shouldBubble, flags);
841 } catch (RemoteException e) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800842 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700843
844 // Change the settings
845 channel = NotificationChannelHelper.createConversationChannelIfNeeded(mContext,
846 mINotificationManager, entry, channel);
847 channel.setAllowBubbles(shouldBubble);
848 try {
849 int currentPref = mINotificationManager.getBubblePreferenceForPackage(appPkg, appUid);
850 if (shouldBubble && currentPref == BUBBLE_PREFERENCE_NONE) {
851 mINotificationManager.setBubblesAllowed(appPkg, appUid, BUBBLE_PREFERENCE_SELECTED);
852 }
853 mINotificationManager.updateNotificationChannelForPackage(appPkg, appUid, channel);
854 } catch (RemoteException e) {
855 Log.e(TAG, e.getMessage());
856 }
857
858 if (shouldBubble) {
859 mShadeController.collapsePanel(true);
860 if (entry.getRow() != null) {
861 entry.getRow().updateBubbleButton();
862 }
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800863 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800864 }
865
866 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500867 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500868 * <p>
869 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800870 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500871 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500872 void removeBubble(NotificationEntry entry, int reason) {
873 if (mBubbleData.hasBubbleWithKey(entry.getKey())) {
874 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800875 }
876 }
877
Beverlyed8aea22020-01-22 16:52:47 -0500878 private void onEntryAdded(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000879 if (mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700880 && canLaunchInActivityView(mContext, entry)) {
Beverlyed8aea22020-01-22 16:52:47 -0500881 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700882 }
883 }
884
Beverlyed8aea22020-01-22 16:52:47 -0500885 private void onEntryUpdated(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000886 boolean shouldBubble = mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700887 && canLaunchInActivityView(mContext, entry);
Beverlyed8aea22020-01-22 16:52:47 -0500888 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
889 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500890 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500891 } else if (shouldBubble) {
Beverlyed8aea22020-01-22 16:52:47 -0500892 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800893 }
Beverlyed8aea22020-01-22 16:52:47 -0500894 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800895
Beverlya53fb0d2020-01-29 15:26:13 -0500896 private void onEntryRemoved(NotificationEntry entry) {
897 if (isSummaryOfBubbles(entry)) {
898 final String groupKey = entry.getSbn().getGroupKey();
899 mBubbleData.removeSuppressedSummary(groupKey);
900
901 // Remove any associated bubble children with the summary
902 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
903 for (int i = 0; i < bubbleChildren.size(); i++) {
904 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
905 }
906 } else {
907 removeBubble(entry, DISMISS_NOTIF_CANCEL);
908 }
909 }
910
Mady Mellor56515c42020-02-18 17:58:36 -0800911 /**
912 * Called when NotificationListener has received adjusted notification rank and reapplied
913 * filtering and sorting. This is used to dismiss or create bubbles based on changes in
914 * permissions on the notification channel or the global setting.
915 *
916 * @param rankingMap the updated ranking map from NotificationListenerService
917 */
Beverlyed8aea22020-01-22 16:52:47 -0500918 private void onRankingUpdated(RankingMap rankingMap) {
Mady Mellor56515c42020-02-18 17:58:36 -0800919 if (mTmpRanking == null) {
920 mTmpRanking = new NotificationListenerService.Ranking();
921 }
922 String[] orderedKeys = rankingMap.getOrderedKeys();
923 for (int i = 0; i < orderedKeys.length; i++) {
924 String key = orderedKeys[i];
925 NotificationEntry entry = mNotificationEntryManager.getPendingOrActiveNotif(key);
926 rankingMap.getRanking(key, mTmpRanking);
Mady Mellore45ff862020-03-24 15:54:50 -0700927 boolean isActiveBubble = mBubbleData.hasBubbleWithKey(key);
928 if (isActiveBubble && !mTmpRanking.canBubble()) {
Mady Mellor56515c42020-02-18 17:58:36 -0800929 mBubbleData.notificationEntryRemoved(entry, BubbleController.DISMISS_BLOCKED);
Mady Mellore45ff862020-03-24 15:54:50 -0700930 } else if (entry != null && mTmpRanking.isBubble() && !isActiveBubble) {
Mady Mellor56515c42020-02-18 17:58:36 -0800931 entry.setFlagBubble(true);
932 onEntryUpdated(entry);
933 }
934 }
Beverlyed8aea22020-01-22 16:52:47 -0500935 }
Ned Burns01e38212019-01-03 16:32:52 -0500936
Mark Renouf71a3af62019-04-08 15:02:54 -0400937 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400938 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400939
Mark Renouf3bc5b362019-04-05 14:37:59 -0400940 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400941 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800942 // Update bubbles in overflow.
943 if (mOverflowCallback != null) {
944 mOverflowCallback.run();
945 }
946
Mark Renouf82a40e62019-05-23 16:16:24 -0400947 // Collapsing? Do this first before remaining steps.
948 if (update.expandedChanged && !update.expanded) {
949 mStackView.setExpanded(false);
950 }
951
952 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700953 ArrayList<Pair<Bubble, Integer>> removedBubbles =
954 new ArrayList<>(update.removedBubbles);
955 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400956 final Bubble bubble = removed.first;
957 @DismissReason final int reason = removed.second;
958 mStackView.removeBubble(bubble);
Mark Renoufc19b4732019-06-26 12:08:33 -0400959 // If the bubble is removed for user switching, leave the notification in place.
960 if (reason != DISMISS_USER_CHANGED) {
961 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800962 && !bubble.showInShade()) {
Beverlyed8aea22020-01-22 16:52:47 -0500963 // The bubble is now gone & the notification is hidden from the shade, so
964 // time to actually remove it
965 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -0500966 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -0500967 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400968 } else {
969 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400970 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700971 if (bubble.getEntry().getRow() != null) {
972 bubble.getEntry().getRow().updateBubbleButton();
973 }
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700974
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700975 // Update the state in NotificationManagerService
Mark Renoufc19b4732019-06-26 12:08:33 -0400976 try {
977 mBarService.onNotificationBubbleChanged(bubble.getKey(),
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700978 false /* isBubble */, 0 /* flags */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400979 } catch (RemoteException e) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400980 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400981 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700982
Ned Burns00b4b2d2019-10-17 22:09:27 -0400983 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500984 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
985 // Time to potentially remove the summary
986 for (NotifCallback cb : mCallbacks) {
987 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -0700988 }
989 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700990 }
991 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400992
Lyn Hanc47e1712020-01-28 21:43:34 -0800993 if (update.addedBubble != null) {
994 mStackView.addBubble(update.addedBubble);
995 }
996
Mark Renouf82a40e62019-05-23 16:16:24 -0400997 if (update.updatedBubble != null) {
998 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400999 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001000
Lyn Hanb58c7562020-01-07 14:29:20 -08001001 // At this point, the correct bubbles are inflated in the stack.
1002 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -04001003 if (update.orderChanged) {
1004 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -04001005 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001006
Mark Renouf82a40e62019-05-23 16:16:24 -04001007 if (update.selectionChanged) {
1008 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -07001009 if (update.selectedBubble != null) {
1010 mNotificationGroupManager.updateSuppression(
1011 update.selectedBubble.getEntry());
1012 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001013 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001014
Mark Renouf82a40e62019-05-23 16:16:24 -04001015 // Expanding? Apply this last.
1016 if (update.expandedChanged && update.expanded) {
1017 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001018 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001019
Beverlyed8aea22020-01-22 16:52:47 -05001020 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001021 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001022 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001023 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001024
Issei Suzukia8d07312019-06-07 12:56:19 +02001025 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001026 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001027 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001028 mBubbleData.getSelectedBubble()));
1029
1030 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001031 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001032 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001033 mStackView.getExpandedBubble()));
1034 }
Lyn Hanb58c7562020-01-07 14:29:20 -08001035 Log.d(TAG, "\n[BubbleData] overflow:");
1036 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
1037 null));
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001038 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001039 }
1040 };
1041
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001042 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001043 * We intercept notification entries (including group summaries) dismissed by the user when
1044 * there is an active bubble associated with it. We do this so that developers can still
1045 * cancel it (and hence the bubbles associated with it). However, these intercepted
1046 * notifications should then be hidden from the shade since the user has cancelled them, so we
1047 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1048 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001049 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001050 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001051 */
Beverlya53fb0d2020-01-29 15:26:13 -05001052 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001053 if (entry == null) {
1054 return false;
1055 }
Beverlyed8aea22020-01-22 16:52:47 -05001056
Beverlya53fb0d2020-01-29 15:26:13 -05001057 final boolean interceptBubbleDismissal = mBubbleData.hasBubbleWithKey(entry.getKey())
1058 && entry.isBubble();
1059 final boolean interceptSummaryDismissal = isSummaryOfBubbles(entry);
Beverlyed8aea22020-01-22 16:52:47 -05001060
Beverlya53fb0d2020-01-29 15:26:13 -05001061 if (interceptSummaryDismissal) {
1062 handleSummaryDismissalInterception(entry);
1063 } else if (interceptBubbleDismissal) {
1064 Bubble bubble = mBubbleData.getBubbleWithKey(entry.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001065 bubble.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001066 bubble.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001067 } else {
Beverlyed8aea22020-01-22 16:52:47 -05001068 return false;
1069 }
Beverlya53fb0d2020-01-29 15:26:13 -05001070
1071 // Update the shade
1072 for (NotifCallback cb : mCallbacks) {
1073 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1074 }
1075 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001076 }
1077
Beverlya53fb0d2020-01-29 15:26:13 -05001078 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1079 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001080 return false;
1081 }
Beverlya53fb0d2020-01-29 15:26:13 -05001082
1083 String groupKey = entry.getSbn().getGroupKey();
1084 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1085 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1086 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1087 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1088 return (isSuppressedSummary || isSummary)
1089 && bubbleChildren != null
1090 && !bubbleChildren.isEmpty();
1091 }
1092
1093 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1094 // current children in the row:
Kevin Han43077f92020-02-28 12:51:53 -08001095 final List<NotificationEntry> children = summary.getAttachedNotifChildren();
Beverlya53fb0d2020-01-29 15:26:13 -05001096 if (children != null) {
1097 for (int i = 0; i < children.size(); i++) {
1098 NotificationEntry child = children.get(i);
1099 if (mBubbleData.hasBubbleWithKey(child.getKey())) {
1100 // Suppress the bubbled child
1101 // As far as group manager is concerned, once a child is no longer shown
1102 // in the shade, it is essentially removed.
1103 Bubble bubbleChild = mBubbleData.getBubbleWithKey(child.getKey());
1104 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1105 bubbleChild.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001106 bubbleChild.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001107 } else {
1108 // non-bubbled children can be removed
1109 for (NotifCallback cb : mCallbacks) {
1110 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1111 }
1112 }
1113 }
1114 }
1115
1116 // And since all children are removed, remove the summary.
1117 mNotificationGroupManager.onEntryRemoved(summary);
1118
1119 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1120 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1121 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001122 }
1123
1124 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001125 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001126 * Updates the visibility of the bubbles based on current state.
1127 * Does not un-bubble, just hides or un-hides. Notifies any
1128 * {@link BubbleStateChangeListener}s of visibility changes.
1129 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001130 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001131 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001132 if (mStackView == null) {
1133 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001134 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001135 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1136 // Bubbles only appear in unlocked shade
1137 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001138 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001139 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001140 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001141
Mady Mellor698d9e82019-08-01 23:11:53 +00001142 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001143 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001144 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001145 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001146 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1147 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1148 }
1149
1150 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001151 }
1152
1153 /**
1154 * Rect indicating the touchable region for the bubble stack / expanded stack.
1155 */
1156 public Rect getTouchableRegion() {
1157 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1158 return null;
1159 }
1160 mStackView.getBoundsOnScreen(mTempRect);
1161 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001162 }
1163
Mady Mellor390bff42019-04-05 15:09:01 -07001164 /**
1165 * The display id of the expanded view, if the stack is expanded and not occluded by the
1166 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1167 */
1168 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001169 if (mStackView == null) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001170 return INVALID_DISPLAY;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001171 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001172 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001173 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Lyn Han9f66c3b2020-03-05 23:59:29 -08001174 final BubbleViewProvider expandedViewProvider = mStackView.getExpandedBubble();
1175 if (defaultDisplay && expandedViewProvider != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001176 && !mNotificationShadeWindowController.getPanelExpanded()) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001177 return expandedViewProvider.getDisplayId();
Mady Mellor390bff42019-04-05 15:09:01 -07001178 }
Lyn Han9f66c3b2020-03-05 23:59:29 -08001179 return INVALID_DISPLAY;
Mady Mellor390bff42019-04-05 15:09:01 -07001180 }
1181
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001182 @VisibleForTesting
1183 BubbleStackView getStackView() {
1184 return mStackView;
1185 }
1186
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001187 /**
1188 * Description of current bubble state.
1189 */
1190 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1191 pw.println("BubbleController state:");
1192 mBubbleData.dump(fd, pw, args);
1193 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001194 if (mStackView != null) {
1195 mStackView.dump(fd, pw, args);
1196 }
1197 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001198 }
1199
Mady Mellore80930e2019-03-21 16:00:45 -07001200 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001201 * This task stack listener is responsible for responding to tasks moved to the front
1202 * which are on the default (main) display. When this happens, expanded bubbles must be
1203 * collapsed so the user may interact with the app which was just moved to the front.
1204 * <p>
1205 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1206 * these calls via a main thread Handler.
1207 */
1208 @MainThread
1209 private class BubbleTaskStackListener extends TaskStackChangeListener {
1210
Mark Renoufcecc77b2019-01-30 16:32:24 -05001211 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001212 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1213 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001214 if (!mStackView.isExpansionAnimating()) {
1215 mBubbleData.setExpanded(false);
1216 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001217 }
1218 }
1219
Mark Renoufcecc77b2019-01-30 16:32:24 -05001220 @Override
Winson Chunge789ff62020-02-24 14:40:23 -08001221 public void onActivityRestartAttempt(RunningTaskInfo task, boolean homeTaskVisible,
1222 boolean clearedTask) {
1223 for (Bubble b : mBubbleData.getBubbles()) {
1224 if (b.getDisplayId() == task.displayId) {
1225 expandStackAndSelectBubble(b.getKey());
1226 return;
1227 }
1228 }
1229 }
1230
1231 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001232 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001233 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001234 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001235 }
1236 }
Mark Renouf446251d2019-04-26 10:22:41 -04001237
1238 @Override
1239 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1240 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1241 mBubbleData.setExpanded(false);
1242 }
1243 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001244
1245 @Override
1246 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001247 if (mStackView == null) {
1248 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001249 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001250 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001251 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001252
1253 @Override
1254 public void onSingleTaskDisplayEmpty(int displayId) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001255 final BubbleViewProvider expandedBubble = mStackView != null
Mady Mellor5186b132019-09-16 17:55:48 -07001256 ? mStackView.getExpandedBubble()
1257 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001258 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1259 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001260 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001261 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001262 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001263 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001264 }
1265
Mady Mellorca0c24c2019-05-16 16:14:32 -07001266 /**
1267 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1268 *
1269 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1270 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1271 *
1272 * @param context the context to use.
1273 * @param entry the entry to bubble.
1274 */
1275 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1276 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Melloraa9ce172020-03-17 10:34:20 -07001277 ? entry.getBubbleMetadata().getIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001278 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001279 if (entry.getBubbleMetadata() != null
1280 && entry.getBubbleMetadata().getShortcutId() != null) {
1281 return true;
1282 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001283 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001284 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001285 return false;
1286 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001287 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1288 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001289 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001290 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001291 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001292 Log.w(TAG, "Unable to send as bubble, "
1293 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001294 + intent);
1295 return false;
1296 }
1297 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001298 Log.w(TAG, "Unable to send as bubble, "
1299 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001300 + intent);
1301 return false;
1302 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001303 return true;
1304 }
1305
Joshua Tsujia19515f2019-02-13 18:02:29 -05001306 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001307 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001308 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001309 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Joshua Tsujiff6b0f22020-03-09 14:55:19 -04001310 if (mStackView != null) {
Joshua Tsujid9422832019-03-05 13:32:37 -05001311 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001312 }
1313 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001314 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001315}