blob: 5239a37797a9f16d98d3048a9bf59d9bcc218cae [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.
Lyn Hane4274be2020-04-24 17:55:36 -0700173 @Nullable private BubbleData.Listener mOverflowListener = 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 Hane4274be2020-04-24 17:55:36 -0700580 void setOverflowListener(BubbleData.Listener listener) {
581 mOverflowListener = listener;
Lyn Hanb58c7562020-01-07 14:29:20 -0800582 }
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);
Lyn Handa9a6a22020-04-24 13:21:43 -0700668 // Reload each bubble
Mady Mellor3df7ab02019-12-09 15:07:10 -0800669 for (Bubble b: mBubbleData.getBubbles()) {
Lyn Handa9a6a22020-04-24 13:21:43 -0700670 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
671 }
672 for (Bubble b: mBubbleData.getOverflowBubbles()) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800673 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();
Lyn Han2f6e89d2020-04-15 10:01:01 -0700743 boolean isSuppressedBubble = (mBubbleData.hasAnyBubbleWithKey(key)
744 && !mBubbleData.getAnyBubbleWithkey(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));
Lyn Han2f6e89d2020-04-15 10:01:01 -0700749 return (isSummary && isSuppressedSummary) || isSuppressedBubble;
Mady Mellorce23c462019-06-17 17:30:07 -0700750 }
751
Lyn Hanb58c7562020-01-07 14:29:20 -0800752 void promoteBubbleFromOverflow(Bubble bubble) {
Lyn Han1e19d7f2020-02-05 19:10:58 -0800753 bubble.setInflateSynchronously(mInflateSynchronously);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700754 setIsBubble(bubble, /* isBubble */ true);
Lyn Han1e19d7f2020-02-05 19:10:58 -0800755 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) {
Lyn Han2f6e89d2020-04-15 10:01:01 -0700764 Bubble bubble = mBubbleData.getBubbleInStackWithKey(notificationKey);
765 if (bubble == null) {
766 bubble = mBubbleData.getOverflowBubbleWithKey(notificationKey);
767 if (bubble != null) {
768 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
769 }
770 } else if (bubble.getEntry().isBubble()){
Mark Renouf71a3af62019-04-08 15:02:54 -0400771 mBubbleData.setSelectedBubble(bubble);
Mark Renouffec45da2019-03-13 13:24:27 -0400772 }
Lyn Han2f6e89d2020-04-15 10:01:01 -0700773 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400774 }
775
776 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500777 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
778 * is forwarded a back key down/up pair.
779 */
780 public void performBackPressIfNeeded() {
781 if (mStackView != null) {
782 mStackView.performBackPressIfNeeded();
783 }
784 }
785
786 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800787 * Adds or updates a bubble associated with the provided notification entry.
788 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400789 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800790 */
Mark Renouff97ed462019-04-05 13:46:24 -0400791 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700792 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400793 }
794
795 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700796 updateBubble(notif, suppressFlyout, true /* showInShade */);
797 }
798
799 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800800 if (mStackView == null) {
801 // Lazy init stack view when a bubble is created
802 ensureStackViewCreated();
803 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700804 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400805 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700806 notif.setInterruption();
807 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800808 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
809 bubble.setInflateSynchronously(mInflateSynchronously);
810 bubble.inflate(
Mady Mellor2e09b5d2020-04-27 21:20:27 +0000811 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
Mady Mellor3df7ab02019-12-09 15:07:10 -0800812 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700813 }
814
815 /**
816 * Called when a user has indicated that an active notification should be shown as a bubble.
817 * <p>
818 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
819 * the notification from appearing in the shade.
820 *
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700821 * @param entry the notification to change bubble state for.
822 * @param shouldBubble whether the notification should show as a bubble or not.
Mady Mellor7f234902019-10-20 12:06:29 -0700823 */
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700824 public void onUserChangedBubble(NotificationEntry entry, boolean shouldBubble) {
825 NotificationChannel channel = entry.getChannel();
826 final String appPkg = entry.getSbn().getPackageName();
827 final int appUid = entry.getSbn().getUid();
828 if (channel == null || appPkg == null) {
829 return;
Mady Mellorff076eb2019-11-13 10:12:06 -0800830 }
Mady Mellor7f234902019-10-20 12:06:29 -0700831
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700832 // Update the state in NotificationManagerService
833 try {
834 int flags = Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION;
835 mBarService.onNotificationBubbleChanged(entry.getKey(), shouldBubble, flags);
836 } catch (RemoteException e) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800837 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700838
839 // Change the settings
840 channel = NotificationChannelHelper.createConversationChannelIfNeeded(mContext,
841 mINotificationManager, entry, channel);
842 channel.setAllowBubbles(shouldBubble);
843 try {
844 int currentPref = mINotificationManager.getBubblePreferenceForPackage(appPkg, appUid);
845 if (shouldBubble && currentPref == BUBBLE_PREFERENCE_NONE) {
846 mINotificationManager.setBubblesAllowed(appPkg, appUid, BUBBLE_PREFERENCE_SELECTED);
847 }
848 mINotificationManager.updateNotificationChannelForPackage(appPkg, appUid, channel);
849 } catch (RemoteException e) {
850 Log.e(TAG, e.getMessage());
851 }
852
853 if (shouldBubble) {
854 mShadeController.collapsePanel(true);
855 if (entry.getRow() != null) {
856 entry.getRow().updateBubbleButton();
857 }
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800858 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800859 }
860
861 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500862 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500863 * <p>
864 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800865 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500866 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500867 void removeBubble(NotificationEntry entry, int reason) {
Lyn Han2f6e89d2020-04-15 10:01:01 -0700868 if (mBubbleData.hasAnyBubbleWithKey(entry.getKey())) {
Beverlya53fb0d2020-01-29 15:26:13 -0500869 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800870 }
871 }
872
Beverlyed8aea22020-01-22 16:52:47 -0500873 private void onEntryAdded(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000874 if (mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700875 && canLaunchInActivityView(mContext, entry)) {
Beverlyed8aea22020-01-22 16:52:47 -0500876 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700877 }
878 }
879
Beverlyed8aea22020-01-22 16:52:47 -0500880 private void onEntryUpdated(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000881 boolean shouldBubble = mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700882 && canLaunchInActivityView(mContext, entry);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700883 if (!shouldBubble && mBubbleData.hasAnyBubbleWithKey(entry.getKey())) {
Beverlyed8aea22020-01-22 16:52:47 -0500884 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500885 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500886 } else if (shouldBubble) {
Beverlyed8aea22020-01-22 16:52:47 -0500887 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800888 }
Beverlyed8aea22020-01-22 16:52:47 -0500889 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800890
Beverlya53fb0d2020-01-29 15:26:13 -0500891 private void onEntryRemoved(NotificationEntry entry) {
892 if (isSummaryOfBubbles(entry)) {
893 final String groupKey = entry.getSbn().getGroupKey();
894 mBubbleData.removeSuppressedSummary(groupKey);
895
896 // Remove any associated bubble children with the summary
897 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
898 for (int i = 0; i < bubbleChildren.size(); i++) {
899 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
900 }
901 } else {
902 removeBubble(entry, DISMISS_NOTIF_CANCEL);
903 }
904 }
905
Mady Mellor56515c42020-02-18 17:58:36 -0800906 /**
907 * Called when NotificationListener has received adjusted notification rank and reapplied
908 * filtering and sorting. This is used to dismiss or create bubbles based on changes in
909 * permissions on the notification channel or the global setting.
910 *
911 * @param rankingMap the updated ranking map from NotificationListenerService
912 */
Beverlyed8aea22020-01-22 16:52:47 -0500913 private void onRankingUpdated(RankingMap rankingMap) {
Mady Mellor56515c42020-02-18 17:58:36 -0800914 if (mTmpRanking == null) {
915 mTmpRanking = new NotificationListenerService.Ranking();
916 }
917 String[] orderedKeys = rankingMap.getOrderedKeys();
918 for (int i = 0; i < orderedKeys.length; i++) {
919 String key = orderedKeys[i];
920 NotificationEntry entry = mNotificationEntryManager.getPendingOrActiveNotif(key);
921 rankingMap.getRanking(key, mTmpRanking);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700922 boolean isActiveBubble = mBubbleData.hasAnyBubbleWithKey(key);
Mady Mellore45ff862020-03-24 15:54:50 -0700923 if (isActiveBubble && !mTmpRanking.canBubble()) {
Mady Mellor56515c42020-02-18 17:58:36 -0800924 mBubbleData.notificationEntryRemoved(entry, BubbleController.DISMISS_BLOCKED);
Mady Mellore45ff862020-03-24 15:54:50 -0700925 } else if (entry != null && mTmpRanking.isBubble() && !isActiveBubble) {
Mady Mellor56515c42020-02-18 17:58:36 -0800926 entry.setFlagBubble(true);
927 onEntryUpdated(entry);
928 }
929 }
Beverlyed8aea22020-01-22 16:52:47 -0500930 }
Ned Burns01e38212019-01-03 16:32:52 -0500931
Lyn Han2f6e89d2020-04-15 10:01:01 -0700932 private void setIsBubble(Bubble b, boolean isBubble) {
933 if (isBubble) {
934 b.getEntry().getSbn().getNotification().flags |= FLAG_BUBBLE;
935 } else {
936 b.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
937 }
938 try {
939 mBarService.onNotificationBubbleChanged(b.getKey(), isBubble, 0);
940 } catch (RemoteException e) {
941 // Bad things have happened
942 }
943 }
944
Mark Renouf71a3af62019-04-08 15:02:54 -0400945 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400946 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400947
Mark Renouf3bc5b362019-04-05 14:37:59 -0400948 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400949 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800950 // Update bubbles in overflow.
Lyn Hane4274be2020-04-24 17:55:36 -0700951 if (mOverflowListener != null) {
952 mOverflowListener.applyUpdate(update);
Lyn Hanb58c7562020-01-07 14:29:20 -0800953 }
954
Mark Renouf82a40e62019-05-23 16:16:24 -0400955 // Collapsing? Do this first before remaining steps.
956 if (update.expandedChanged && !update.expanded) {
957 mStackView.setExpanded(false);
958 }
959
960 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700961 ArrayList<Pair<Bubble, Integer>> removedBubbles =
962 new ArrayList<>(update.removedBubbles);
963 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400964 final Bubble bubble = removed.first;
965 @DismissReason final int reason = removed.second;
966 mStackView.removeBubble(bubble);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700967
Mark Renoufc19b4732019-06-26 12:08:33 -0400968 // If the bubble is removed for user switching, leave the notification in place.
Lyn Han2f6e89d2020-04-15 10:01:01 -0700969 if (reason == DISMISS_USER_CHANGED) {
970 continue;
971 }
972 if (!mBubbleData.hasBubbleInStackWithKey(bubble.getKey())) {
973 if (!mBubbleData.hasOverflowBubbleWithKey(bubble.getKey())
974 && (!bubble.showInShade()
975 || reason == DISMISS_NOTIF_CANCEL
Lyn Hane4274be2020-04-24 17:55:36 -0700976 || reason == DISMISS_GROUP_CANCELLED
977 || reason == DISMISS_OVERFLOW_MAX_REACHED)) {
Beverlyed8aea22020-01-22 16:52:47 -0500978 // The bubble is now gone & the notification is hidden from the shade, so
979 // time to actually remove it
980 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -0500981 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -0500982 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400983 } else {
Lyn Han2f6e89d2020-04-15 10:01:01 -0700984 if (bubble.getEntry().isBubble() && bubble.showInShade()) {
985 setIsBubble(bubble, /* isBubble */ false);
986 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700987 if (bubble.getEntry().getRow() != null) {
988 bubble.getEntry().getRow().updateBubbleButton();
989 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400990 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700991
Lyn Han2f6e89d2020-04-15 10:01:01 -0700992 }
993 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
994 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
995 // Time to potentially remove the summary
996 for (NotifCallback cb : mCallbacks) {
997 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -0700998 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700999 }
1000 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001001
Lyn Hanc47e1712020-01-28 21:43:34 -08001002 if (update.addedBubble != null) {
1003 mStackView.addBubble(update.addedBubble);
1004 }
1005
Mark Renouf82a40e62019-05-23 16:16:24 -04001006 if (update.updatedBubble != null) {
1007 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -04001008 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001009
Lyn Hanb58c7562020-01-07 14:29:20 -08001010 // At this point, the correct bubbles are inflated in the stack.
1011 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -04001012 if (update.orderChanged) {
1013 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -04001014 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001015
Mark Renouf82a40e62019-05-23 16:16:24 -04001016 if (update.selectionChanged) {
1017 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -07001018 if (update.selectedBubble != null) {
1019 mNotificationGroupManager.updateSuppression(
1020 update.selectedBubble.getEntry());
1021 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001022 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001023
Mark Renouf82a40e62019-05-23 16:16:24 -04001024 // Expanding? Apply this last.
1025 if (update.expandedChanged && update.expanded) {
1026 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001027 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001028
Beverlyed8aea22020-01-22 16:52:47 -05001029 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001030 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001031 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001032 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001033
Issei Suzukia8d07312019-06-07 12:56:19 +02001034 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001035 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001036 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001037 mBubbleData.getSelectedBubble()));
1038
1039 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001040 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001041 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001042 mStackView.getExpandedBubble()));
1043 }
1044 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001045 }
1046 };
1047
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001048 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001049 * We intercept notification entries (including group summaries) dismissed by the user when
1050 * there is an active bubble associated with it. We do this so that developers can still
1051 * cancel it (and hence the bubbles associated with it). However, these intercepted
1052 * notifications should then be hidden from the shade since the user has cancelled them, so we
1053 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1054 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001055 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001056 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001057 */
Beverlya53fb0d2020-01-29 15:26:13 -05001058 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001059 if (entry == null) {
1060 return false;
1061 }
Lyn Han2f6e89d2020-04-15 10:01:01 -07001062 if (isSummaryOfBubbles(entry)) {
Beverlya53fb0d2020-01-29 15:26:13 -05001063 handleSummaryDismissalInterception(entry);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001064 } else {
1065 Bubble bubble = mBubbleData.getBubbleInStackWithKey(entry.getKey());
1066 if (bubble == null || !entry.isBubble()) {
1067 bubble = mBubbleData.getOverflowBubbleWithKey(entry.getKey());
1068 }
1069 if (bubble == null) {
1070 return false;
1071 }
Beverlyed8aea22020-01-22 16:52:47 -05001072 bubble.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001073 bubble.setShowDot(false /* show */);
Beverlyed8aea22020-01-22 16:52:47 -05001074 }
Beverlya53fb0d2020-01-29 15:26:13 -05001075 // Update the shade
1076 for (NotifCallback cb : mCallbacks) {
1077 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1078 }
1079 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001080 }
1081
Beverlya53fb0d2020-01-29 15:26:13 -05001082 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1083 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001084 return false;
1085 }
Beverlya53fb0d2020-01-29 15:26:13 -05001086
1087 String groupKey = entry.getSbn().getGroupKey();
1088 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1089 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1090 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1091 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1092 return (isSuppressedSummary || isSummary)
1093 && bubbleChildren != null
1094 && !bubbleChildren.isEmpty();
1095 }
1096
1097 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1098 // current children in the row:
Kevin Han43077f92020-02-28 12:51:53 -08001099 final List<NotificationEntry> children = summary.getAttachedNotifChildren();
Beverlya53fb0d2020-01-29 15:26:13 -05001100 if (children != null) {
1101 for (int i = 0; i < children.size(); i++) {
1102 NotificationEntry child = children.get(i);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001103 if (mBubbleData.hasAnyBubbleWithKey(child.getKey())) {
Beverlya53fb0d2020-01-29 15:26:13 -05001104 // Suppress the bubbled child
1105 // As far as group manager is concerned, once a child is no longer shown
1106 // in the shade, it is essentially removed.
Lyn Han2f6e89d2020-04-15 10:01:01 -07001107 Bubble bubbleChild = mBubbleData.getAnyBubbleWithkey(child.getKey());
Beverlya53fb0d2020-01-29 15:26:13 -05001108 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1109 bubbleChild.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001110 bubbleChild.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001111 } else {
1112 // non-bubbled children can be removed
1113 for (NotifCallback cb : mCallbacks) {
1114 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1115 }
1116 }
1117 }
1118 }
1119
1120 // And since all children are removed, remove the summary.
1121 mNotificationGroupManager.onEntryRemoved(summary);
1122
1123 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1124 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1125 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001126 }
1127
1128 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001129 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001130 * Updates the visibility of the bubbles based on current state.
1131 * Does not un-bubble, just hides or un-hides. Notifies any
1132 * {@link BubbleStateChangeListener}s of visibility changes.
1133 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001134 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001135 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001136 if (mStackView == null) {
1137 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001138 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001139 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1140 // Bubbles only appear in unlocked shade
1141 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001142 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001143 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001144 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001145
Mady Mellor698d9e82019-08-01 23:11:53 +00001146 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001147 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001148 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001149 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001150 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1151 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1152 }
1153
1154 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001155 }
1156
1157 /**
1158 * Rect indicating the touchable region for the bubble stack / expanded stack.
1159 */
1160 public Rect getTouchableRegion() {
1161 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1162 return null;
1163 }
1164 mStackView.getBoundsOnScreen(mTempRect);
1165 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001166 }
1167
Mady Mellor390bff42019-04-05 15:09:01 -07001168 /**
1169 * The display id of the expanded view, if the stack is expanded and not occluded by the
1170 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1171 */
1172 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001173 if (mStackView == null) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001174 return INVALID_DISPLAY;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001175 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001176 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001177 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Lyn Han9f66c3b2020-03-05 23:59:29 -08001178 final BubbleViewProvider expandedViewProvider = mStackView.getExpandedBubble();
1179 if (defaultDisplay && expandedViewProvider != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001180 && !mNotificationShadeWindowController.getPanelExpanded()) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001181 return expandedViewProvider.getDisplayId();
Mady Mellor390bff42019-04-05 15:09:01 -07001182 }
Lyn Han9f66c3b2020-03-05 23:59:29 -08001183 return INVALID_DISPLAY;
Mady Mellor390bff42019-04-05 15:09:01 -07001184 }
1185
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001186 @VisibleForTesting
1187 BubbleStackView getStackView() {
1188 return mStackView;
1189 }
1190
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001191 /**
1192 * Description of current bubble state.
1193 */
1194 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1195 pw.println("BubbleController state:");
1196 mBubbleData.dump(fd, pw, args);
1197 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001198 if (mStackView != null) {
1199 mStackView.dump(fd, pw, args);
1200 }
1201 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001202 }
1203
Mady Mellore80930e2019-03-21 16:00:45 -07001204 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001205 * This task stack listener is responsible for responding to tasks moved to the front
1206 * which are on the default (main) display. When this happens, expanded bubbles must be
1207 * collapsed so the user may interact with the app which was just moved to the front.
1208 * <p>
1209 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1210 * these calls via a main thread Handler.
1211 */
1212 @MainThread
1213 private class BubbleTaskStackListener extends TaskStackChangeListener {
1214
Mark Renoufcecc77b2019-01-30 16:32:24 -05001215 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001216 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1217 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001218 if (!mStackView.isExpansionAnimating()) {
1219 mBubbleData.setExpanded(false);
1220 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001221 }
1222 }
1223
Mark Renoufcecc77b2019-01-30 16:32:24 -05001224 @Override
Winson Chunge789ff62020-02-24 14:40:23 -08001225 public void onActivityRestartAttempt(RunningTaskInfo task, boolean homeTaskVisible,
Evan Rosky8d1c24e2020-04-23 09:21:16 -07001226 boolean clearedTask, boolean wasVisible) {
Winson Chunge789ff62020-02-24 14:40:23 -08001227 for (Bubble b : mBubbleData.getBubbles()) {
1228 if (b.getDisplayId() == task.displayId) {
1229 expandStackAndSelectBubble(b.getKey());
1230 return;
1231 }
1232 }
1233 }
1234
1235 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001236 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001237 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001238 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001239 }
1240 }
Mark Renouf446251d2019-04-26 10:22:41 -04001241
1242 @Override
1243 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1244 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1245 mBubbleData.setExpanded(false);
1246 }
1247 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001248
1249 @Override
1250 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001251 if (mStackView == null) {
1252 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001253 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001254 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001255 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001256
1257 @Override
1258 public void onSingleTaskDisplayEmpty(int displayId) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001259 final BubbleViewProvider expandedBubble = mStackView != null
Mady Mellor5186b132019-09-16 17:55:48 -07001260 ? mStackView.getExpandedBubble()
1261 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001262 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1263 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001264 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001265 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001266 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001267 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001268 }
1269
Mady Mellorca0c24c2019-05-16 16:14:32 -07001270 /**
1271 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1272 *
1273 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1274 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1275 *
1276 * @param context the context to use.
1277 * @param entry the entry to bubble.
1278 */
1279 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1280 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Melloraa9ce172020-03-17 10:34:20 -07001281 ? entry.getBubbleMetadata().getIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001282 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001283 if (entry.getBubbleMetadata() != null
1284 && entry.getBubbleMetadata().getShortcutId() != null) {
1285 return true;
1286 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001287 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001288 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001289 return false;
1290 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001291 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1292 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001293 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001294 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001295 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001296 Log.w(TAG, "Unable to send as bubble, "
1297 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001298 + intent);
1299 return false;
1300 }
1301 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001302 Log.w(TAG, "Unable to send as bubble, "
1303 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001304 + intent);
1305 return false;
1306 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001307 return true;
1308 }
1309
Joshua Tsujia19515f2019-02-13 18:02:29 -05001310 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001311 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001312 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001313 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Joshua Tsujiff6b0f22020-03-09 14:55:19 -04001314 if (mStackView != null) {
Joshua Tsujid9422832019-03-05 13:32:37 -05001315 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001316 }
1317 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001318 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001319}