blob: 7bdeb1cfa75d7b552ee573495ebc16355c37dec6 [file] [log] [blame]
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.bubbles;
18
Mady Mellor3a0a1b42019-05-23 06:40:21 -070019import static android.app.Notification.FLAG_BUBBLE;
Mady Mellorc2ff0112019-03-28 14:18:06 -070020import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL;
21import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL;
22import static android.service.notification.NotificationListenerService.REASON_CANCEL;
23import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -070024import static android.service.notification.NotificationListenerService.REASON_CLICK;
25import static android.service.notification.NotificationListenerService.REASON_GROUP_SUMMARY_CANCELED;
Mady Mellor390bff42019-04-05 15:09:01 -070026import static android.view.Display.DEFAULT_DISPLAY;
27import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080028import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080029import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080030import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080031
Issei Suzukia8d07312019-06-07 12:56:19 +020032import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_CONTROLLER;
Mady Mellorff076eb2019-11-13 10:12:06 -080033import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_EXPERIMENTS;
Issei Suzukia8d07312019-06-07 12:56:19 +020034import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
35import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080036import static com.android.systemui.statusbar.StatusBarState.SHADE;
Mady Mellor1a4e86f2019-05-03 16:07:23 -070037import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080038
Mark Renoufba5ab512019-05-02 15:21:01 -040039import static java.lang.annotation.ElementType.FIELD;
40import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
41import static java.lang.annotation.ElementType.PARAMETER;
Mark Renouf08bc42a2019-03-07 13:01:59 -050042import static java.lang.annotation.RetentionPolicy.SOURCE;
43
Mark Renoufc19b4732019-06-26 12:08:33 -040044import android.annotation.UserIdInt;
Mark Renoufc808f062019-02-07 15:20:37 -050045import android.app.ActivityManager.RunningTaskInfo;
Mady Mellor66efd5e2019-05-15 13:38:11 -070046import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070047import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080048import android.content.Context;
Mady Mellorca0c24c2019-05-16 16:14:32 -070049import android.content.pm.ActivityInfo;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080050import android.content.pm.PackageManager;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040051import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080052import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050053import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080054import android.os.ServiceManager;
Mady Mellor56515c42020-02-18 17:58:36 -080055import android.service.notification.NotificationListenerService;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040056import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040057import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040058import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040059import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040060import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040061import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050062import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080063import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080064import android.widget.FrameLayout;
65
Mark Renouf08bc42a2019-03-07 13:01:59 -050066import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050067import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040068import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050069
Mady Mellorebdbbb92018-11-15 14:36:48 -080070import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070071import com.android.internal.statusbar.IStatusBarService;
Beverlya53fb0d2020-01-29 15:26:13 -050072import com.android.internal.statusbar.NotificationVisibility;
Beverlya53fb0d2020-01-29 15:26:13 -050073import com.android.systemui.Dumpable;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080074import com.android.systemui.R;
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +010075import com.android.systemui.bubbles.dagger.BubbleModule;
Ned Burnsaaeb44b2020-02-12 23:48:26 -050076import com.android.systemui.dump.DumpManager;
Joshua Tsujibe60a582020-03-23 17:17:26 -040077import com.android.systemui.model.SysUiState;
Beverly8fdb5332019-02-04 14:29:49 -050078import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050079import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000080import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050081import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050082import com.android.systemui.shared.system.WindowManagerWrapper;
Beverlya53fb0d2020-01-29 15:26:13 -050083import com.android.systemui.statusbar.FeatureFlags;
Mark Renoufc19b4732019-06-26 12:08:33 -040084import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070085import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050086import com.android.systemui.statusbar.notification.NotificationEntryListener;
87import com.android.systemui.statusbar.notification.NotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -050088import com.android.systemui.statusbar.notification.collection.NotifCollection;
89import com.android.systemui.statusbar.notification.collection.NotifPipeline;
Ned Burnsf81c4c42019-01-07 14:10:43 -050090import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Beverlya53fb0d2020-01-29 15:26:13 -050091import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener;
Beverly Taid1e175c2020-03-10 16:37:04 +000092import com.android.systemui.statusbar.notification.interruption.NotificationInterruptStateProvider;
Mady Mellor22f2f072019-04-18 13:26:18 -070093import com.android.systemui.statusbar.phone.NotificationGroupManager;
wilsonshihe8321942019-10-18 18:39:46 +080094import com.android.systemui.statusbar.phone.NotificationShadeWindowController;
Mady Mellor7f234902019-10-20 12:06:29 -070095import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080096import com.android.systemui.statusbar.phone.StatusBar;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070097import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040098import com.android.systemui.statusbar.policy.ZenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -050099import com.android.systemui.util.FloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800100
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700101import java.io.FileDescriptor;
102import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500103import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -0400104import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -0700105import java.util.ArrayList;
Mady Mellorff076eb2019-11-13 10:12:06 -0800106import java.util.HashSet;
Lyn Hanb58c7562020-01-07 14:29:20 -0800107import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500108
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800109/**
110 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
111 * Bubbles can be expanded to show more content.
112 *
113 * The controller manages addition, removal, and visible state of bubbles on screen.
114 */
Beverlya53fb0d2020-01-29 15:26:13 -0500115public class BubbleController implements ConfigurationController.ConfigurationListener, Dumpable {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800116
Issei Suzukia8d07312019-06-07 12:56:19 +0200117 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800118
Mark Renouf08bc42a2019-03-07 13:01:59 -0500119 @Retention(SOURCE)
120 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400121 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700122 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400123 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500124 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700125
Mark Renouf08bc42a2019-03-07 13:01:59 -0500126 static final int DISMISS_USER_GESTURE = 1;
127 static final int DISMISS_AGED = 2;
128 static final int DISMISS_TASK_FINISHED = 3;
129 static final int DISMISS_BLOCKED = 4;
130 static final int DISMISS_NOTIF_CANCEL = 5;
131 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700132 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400133 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700134 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700135 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500136
Ned Burns01e38212019-01-03 16:32:52 -0500137 private final Context mContext;
138 private final NotificationEntryManager mNotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500139 private final NotifPipeline mNotifPipeline;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500140 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800141 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800142 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100143 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700144 private final NotificationGroupManager mNotificationGroupManager;
Heemin Seogba6337f2019-12-10 15:34:37 -0800145 private final ShadeController mShadeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500146 private final FloatingContentCoordinator mFloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800147
Mady Mellor3dff9e62019-02-05 18:12:53 -0800148 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400149 @Nullable private BubbleStackView mStackView;
Mady Mellor247ca2c2019-12-02 16:18:59 -0800150 private BubbleIconFactory mBubbleIconFactory;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800151
Mark Renoufc19b4732019-06-26 12:08:33 -0400152 // Tracks the id of the current (foreground) user.
153 private int mCurrentUserId;
154 // Saves notification keys of active bubbles when users are switched.
155 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
156
Mady Mellor56515c42020-02-18 17:58:36 -0800157 // Used when ranking updates occur and we check if things should bubble / unbubble
158 private NotificationListenerService.Ranking mTmpRanking;
159
Mady Mellorff076eb2019-11-13 10:12:06 -0800160 // Saves notification keys of user created "fake" bubbles so that we can allow notifications
161 // like these to bubble by default. Doesn't persist across reboots, not a long-term solution.
162 private final HashSet<String> mUserCreatedBubbles;
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800163 // If we're auto-bubbling bubbles via a whitelist, we need to track which notifs from that app
164 // have been "demoted" back to a notification so that we don't auto-bubbles those again.
165 // Doesn't persist across reboots, not a long-term solution.
166 private final HashSet<String> mUserBlockedBubbles;
Mady Mellorff076eb2019-11-13 10:12:06 -0800167
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800168 // Bubbles get added to the status bar view
wilsonshihe8321942019-10-18 18:39:46 +0800169 private final NotificationShadeWindowController mNotificationShadeWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400170 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800171 private StatusBarStateListener mStatusBarStateListener;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500172
Lyn Hanb58c7562020-01-07 14:29:20 -0800173 // Callback that updates BubbleOverflowActivity on data change.
174 @Nullable private Runnable mOverflowCallback = null;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800175
Beverly Taid1e175c2020-03-10 16:37:04 +0000176 private final NotificationInterruptStateProvider mNotificationInterruptStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700177 private IStatusBarService mBarService;
Joshua Tsujibe60a582020-03-23 17:17:26 -0400178 private SysUiState mSysUiState;
Mady Mellorb4991e62019-01-10 15:14:51 -0800179
Mady Mellord1c78b262018-11-06 18:04:40 -0800180 // Used for determining view rect for touch interaction
181 private Rect mTempRect = new Rect();
182
Mark Renoufc19b4732019-06-26 12:08:33 -0400183 // Listens to user switch so bubbles can be saved and restored.
184 private final NotificationLockscreenUserManager mNotifUserManager;
185
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400186 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
187 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
188
Mady Mellor3df7ab02019-12-09 15:07:10 -0800189 private boolean mInflateSynchronously;
190
Beverlyed8aea22020-01-22 16:52:47 -0500191 // TODO (b/145659174): allow for multiple callbacks to support the "shadow" new notif pipeline
192 private final List<NotifCallback> mCallbacks = new ArrayList<>();
193
Mady Mellor5549dd22018-11-06 18:07:34 -0800194 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800195 * Listener to be notified when some states of the bubbles change.
196 */
197 public interface BubbleStateChangeListener {
198 /**
199 * Called when the stack has bubbles or no longer has bubbles.
200 */
201 void onHasBubblesChanged(boolean hasBubbles);
202 }
203
Mady Mellorcd9b1302018-11-06 18:08:04 -0800204 /**
205 * Listener to find out about stack expansion / collapse events.
206 */
207 public interface BubbleExpandListener {
208 /**
209 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700210 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800211 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800212 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800213 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800214 void onBubbleExpandChanged(boolean isExpanding, String key);
215 }
216
217 /**
Mady Mellorf44b6832020-01-14 13:26:14 -0800218 * Listener to be notified when a bubbles' notification suppression state changes.
219 */
220 public interface NotificationSuppressionChangedListener {
221 /**
222 * Called when the notification suppression state of a bubble changes.
223 */
224 void onBubbleNotificationSuppressionChange(Bubble bubble);
Beverlyed8aea22020-01-22 16:52:47 -0500225 }
Mady Mellorf44b6832020-01-14 13:26:14 -0800226
Beverlyed8aea22020-01-22 16:52:47 -0500227 /**
228 * Callback for when the BubbleController wants to interact with the notification pipeline to:
229 * - Remove a previously bubbled notification
230 * - Update the notification shade since bubbled notification should/shouldn't be showing
231 */
232 public interface NotifCallback {
233 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500234 * Called when a bubbled notification that was hidden from the shade is now being removed
235 * This can happen when an app cancels a bubbled notification or when the user dismisses a
236 * bubble.
Beverlyed8aea22020-01-22 16:52:47 -0500237 */
Beverlya53fb0d2020-01-29 15:26:13 -0500238 void removeNotification(NotificationEntry entry, int reason);
Beverlyed8aea22020-01-22 16:52:47 -0500239
240 /**
241 * Called when a bubbled notification has changed whether it should be
242 * filtered from the shade.
243 */
Beverlya53fb0d2020-01-29 15:26:13 -0500244 void invalidateNotifications(String reason);
Beverlyed8aea22020-01-22 16:52:47 -0500245
246 /**
247 * Called on a bubbled entry that has been removed when there are no longer
248 * bubbled entries in its group.
249 *
250 * Checks whether its group has any other (non-bubbled) children. If it doesn't,
251 * removes all remnants of the group's summary from the notification pipeline.
252 * TODO: (b/145659174) Only old pipeline needs this - delete post-migration.
253 */
254 void maybeCancelSummary(NotificationEntry entry);
Mady Mellorf44b6832020-01-14 13:26:14 -0800255 }
256
257 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800258 * Listens for the current state of the status bar and updates the visibility state
259 * of bubbles as needed.
260 */
261 private class StatusBarStateListener implements StatusBarStateController.StateListener {
262 private int mState;
263 /**
264 * Returns the current status bar state.
265 */
266 public int getCurrentState() {
267 return mState;
268 }
269
270 @Override
271 public void onStateChanged(int newState) {
272 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400273 boolean shouldCollapse = (mState != SHADE);
274 if (shouldCollapse) {
275 collapseStack();
276 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700277 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800278 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800279 }
280
Mady Mellor7f234902019-10-20 12:06:29 -0700281 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800282 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700283 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800284 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700285 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700286 ConfigurationController configurationController,
Beverly Taid1e175c2020-03-10 16:37:04 +0000287 NotificationInterruptStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400288 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700289 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700290 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500291 NotificationEntryManager entryManager,
292 NotifPipeline notifPipeline,
293 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500294 DumpManager dumpManager,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400295 FloatingContentCoordinator floatingContentCoordinator,
296 SysUiState sysUiState) {
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,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400300 notifPipeline, featureFlags, dumpManager, floatingContentCoordinator, sysUiState);
Mady Mellor7f234902019-10-20 12:06:29 -0700301 }
302
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +0100303 /**
304 * Injected constructor. See {@link BubbleModule}.
305 */
Mady Mellor7f234902019-10-20 12:06:29 -0700306 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800307 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700308 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800309 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700310 BubbleData data,
311 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
312 ConfigurationController configurationController,
Beverly Taid1e175c2020-03-10 16:37:04 +0000313 NotificationInterruptStateProvider interruptionStateProvider,
Mady Mellor7f234902019-10-20 12:06:29 -0700314 ZenModeController zenModeController,
315 NotificationLockscreenUserManager notifUserManager,
316 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500317 NotificationEntryManager entryManager,
318 NotifPipeline notifPipeline,
319 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500320 DumpManager dumpManager,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400321 FloatingContentCoordinator floatingContentCoordinator,
322 SysUiState sysUiState) {
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500323 dumpManager.registerDumpable(TAG, this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800324 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800325 mShadeController = shadeController;
Beverly Taid1e175c2020-03-10 16:37:04 +0000326 mNotificationInterruptStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400327 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400328 mZenModeController = zenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500329 mFloatingContentCoordinator = floatingContentCoordinator;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400330 mZenModeController.addCallback(new ZenModeController.Callback() {
331 @Override
332 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800333 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400334 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700335 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400336 }
337
338 @Override
339 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800340 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400341 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700342 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400343 }
344 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700345
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700346 configurationController.addCallback(this /* configurationListener */);
Joshua Tsujibe60a582020-03-23 17:17:26 -0400347 mSysUiState = sysUiState;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800348
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400349 mBubbleData = data;
350 mBubbleData.setListener(mBubbleDataListener);
Mady Mellorf44b6832020-01-14 13:26:14 -0800351 mBubbleData.setSuppressionChangedListener(new NotificationSuppressionChangedListener() {
352 @Override
353 public void onBubbleNotificationSuppressionChange(Bubble bubble) {
354 // Make sure NoMan knows it's not showing in the shade anymore so anyone querying it
355 // can tell.
356 try {
357 mBarService.onBubbleNotificationSuppressionChanged(bubble.getKey(),
358 !bubble.showInShade());
359 } catch (RemoteException e) {
360 // Bad things have happened
361 }
362 }
363 });
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400364
Mady Mellor7f234902019-10-20 12:06:29 -0700365 mNotificationEntryManager = entryManager;
Mady Mellor22f2f072019-04-18 13:26:18 -0700366 mNotificationGroupManager = groupManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500367 mNotifPipeline = notifPipeline;
368
369 if (!featureFlags.isNewNotifPipelineRenderingEnabled()) {
370 setupNEM();
371 } else {
372 setupNotifPipeline();
373 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800374
wilsonshihe8321942019-10-18 18:39:46 +0800375 mNotificationShadeWindowController = notificationShadeWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800376 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700377 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500378
Mark Renoufcecc77b2019-01-30 16:32:24 -0500379 mTaskStackListener = new BubbleTaskStackListener();
380 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800381
Joshua Tsujia19515f2019-02-13 18:02:29 -0500382 try {
383 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
384 } catch (RemoteException e) {
385 e.printStackTrace();
386 }
Issei Suzukic0387542019-03-08 17:31:14 +0100387 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700388
389 mBarService = IStatusBarService.Stub.asInterface(
390 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400391
392 mSavedBubbleKeysPerUser = new SparseSetArray<>();
393 mCurrentUserId = mNotifUserManager.getCurrentUserId();
394 mNotifUserManager.addUserChangedListener(
Steve Elliottb47f1c72019-12-19 12:39:26 -0500395 new NotificationLockscreenUserManager.UserChangedListener() {
396 @Override
397 public void onUserChanged(int newUserId) {
398 BubbleController.this.saveBubbles(mCurrentUserId);
399 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
400 BubbleController.this.restoreBubbles(newUserId);
401 mCurrentUserId = newUserId;
402 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400403 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800404
405 mUserCreatedBubbles = new HashSet<>();
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800406 mUserBlockedBubbles = new HashSet<>();
Aran Inkaa4dfa72019-11-18 16:49:07 -0500407
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
467 if (userRemovedNotif || isUserCreatedBubble(key)
468 || isSummaryOfUserCreatedBubble(entry)) {
469 return handleDismissalInterception(entry);
470 }
471
472 return false;
Beverlyed8aea22020-01-22 16:52:47 -0500473 }
474 });
475
476 mNotificationGroupManager.addOnGroupChangeListener(
477 new NotificationGroupManager.OnGroupChangeListener() {
478 @Override
479 public void onGroupSuppressionChanged(
480 NotificationGroupManager.NotificationGroup group,
481 boolean suppressed) {
482 // More notifications could be added causing summary to no longer
483 // be suppressed -- in this case need to remove the key.
484 final String groupKey = group.summary != null
485 ? group.summary.getSbn().getGroupKey()
486 : null;
487 if (!suppressed && groupKey != null
488 && mBubbleData.isSummarySuppressed(groupKey)) {
489 mBubbleData.removeSuppressedSummary(groupKey);
490 }
491 }
492 });
493
494 addNotifCallback(new NotifCallback() {
495 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500496 public void removeNotification(NotificationEntry entry, int reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500497 mNotificationEntryManager.performRemoveNotification(entry.getSbn(),
Beverlya53fb0d2020-01-29 15:26:13 -0500498 reason);
Beverlyed8aea22020-01-22 16:52:47 -0500499 }
500
501 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500502 public void invalidateNotifications(String reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500503 mNotificationEntryManager.updateNotifications(reason);
504 }
505
506 @Override
507 public void maybeCancelSummary(NotificationEntry entry) {
508 // Check if removed bubble has an associated suppressed group summary that needs
509 // to be removed now.
Beverlya53fb0d2020-01-29 15:26:13 -0500510 final String groupKey = entry.getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500511 if (mBubbleData.isSummarySuppressed(groupKey)) {
Beverlya53fb0d2020-01-29 15:26:13 -0500512 mBubbleData.removeSuppressedSummary(groupKey);
Beverlyed8aea22020-01-22 16:52:47 -0500513
514 final NotificationEntry summary =
515 mNotificationEntryManager.getActiveNotificationUnfiltered(
516 mBubbleData.getSummaryKey(groupKey));
Beverlya53fb0d2020-01-29 15:26:13 -0500517 if (summary != null) {
518 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
519 UNDEFINED_DISMISS_REASON);
520 }
Beverlyed8aea22020-01-22 16:52:47 -0500521 }
522
Beverlya53fb0d2020-01-29 15:26:13 -0500523 // Check if we still need to remove the summary from NoManGroup because the summary
524 // may not be in the mBubbleData.mSuppressedGroupKeys list and removed above.
525 // For example:
526 // 1. Bubbled notifications (group) is posted to shade and are visible bubbles
527 // 2. User expands bubbles so now their respective notifications in the shade are
528 // hidden, including the group summary
529 // 3. User removes all bubbles
530 // 4. We expect all the removed bubbles AND the summary (note: the summary was
531 // never added to the suppressedSummary list in BubbleData, so we add this check)
Beverlyed8aea22020-01-22 16:52:47 -0500532 NotificationEntry summary =
533 mNotificationGroupManager.getLogicalGroupSummary(entry.getSbn());
534 if (summary != null) {
535 ArrayList<NotificationEntry> summaryChildren =
536 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
537 boolean isSummaryThisNotif = summary.getKey().equals(entry.getKey());
538 if (!isSummaryThisNotif && (summaryChildren == null
539 || summaryChildren.isEmpty())) {
540 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
541 UNDEFINED_DISMISS_REASON);
542 }
543 }
544 }
545 });
546 }
547
Beverlya53fb0d2020-01-29 15:26:13 -0500548 private void setupNotifPipeline() {
549 mNotifPipeline.addCollectionListener(new NotifCollectionListener() {
550 @Override
551 public void onEntryAdded(NotificationEntry entry) {
552 BubbleController.this.onEntryAdded(entry);
553 }
554
555 @Override
556 public void onEntryUpdated(NotificationEntry entry) {
557 BubbleController.this.onEntryUpdated(entry);
558 }
559
560 @Override
561 public void onRankingUpdate(RankingMap rankingMap) {
562 onRankingUpdated(rankingMap);
563 }
564
565 @Override
566 public void onEntryRemoved(NotificationEntry entry,
567 @NotifCollection.CancellationReason int reason) {
568 BubbleController.this.onEntryRemoved(entry);
569 }
570 });
571 }
572
Beverlyed8aea22020-01-22 16:52:47 -0500573 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800574 * Sets whether to perform inflation on the same thread as the caller. This method should only
575 * be used in tests, not in production.
576 */
577 @VisibleForTesting
578 void setInflateSynchronously(boolean inflateSynchronously) {
579 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800580 }
581
Lyn Hanb58c7562020-01-07 14:29:20 -0800582 void setOverflowCallback(Runnable updateOverflow) {
583 mOverflowCallback = updateOverflow;
584 }
585
586 /**
587 * @return Bubbles for updating overflow.
588 */
589 List<Bubble> getOverflowBubbles() {
590 return mBubbleData.getOverflowBubbles();
591 }
592
593
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400594 /**
595 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
596 * method initializes the stack view and adds it to the StatusBar just above the scrim.
597 */
598 private void ensureStackViewCreated() {
599 if (mStackView == null) {
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500600 mStackView = new BubbleStackView(
Joshua Tsujibe60a582020-03-23 17:17:26 -0400601 mContext, mBubbleData, mSurfaceSynchronizer, mFloatingContentCoordinator,
Joshua Tsujiba9fef02020-04-09 17:40:35 -0400602 mSysUiState, mNotificationShadeWindowController);
wilsonshihe8321942019-10-18 18:39:46 +0800603 ViewGroup nsv = mNotificationShadeWindowController.getNotificationShadeView();
604 int bubbleScrimIndex = nsv.indexOfChild(nsv.findViewById(R.id.scrim_for_bubble));
Lyn Hanbde48202019-05-29 19:18:29 -0700605 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
wilsonshihe8321942019-10-18 18:39:46 +0800606 nsv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400607 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
608 if (mExpandListener != null) {
609 mStackView.setExpandListener(mExpandListener);
610 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400611 }
612 }
613
Mark Renoufc19b4732019-06-26 12:08:33 -0400614 /**
615 * Records the notification key for any active bubbles. These are used to restore active
616 * bubbles when the user returns to the foreground.
617 *
618 * @param userId the id of the user
619 */
620 private void saveBubbles(@UserIdInt int userId) {
621 // First clear any existing keys that might be stored.
622 mSavedBubbleKeysPerUser.remove(userId);
623 // Add in all active bubbles for the current user.
624 for (Bubble bubble: mBubbleData.getBubbles()) {
625 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
626 }
627 }
628
629 /**
630 * Promotes existing notifications to Bubbles if they were previously bubbles.
631 *
632 * @param userId the id of the user
633 */
634 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400635 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
636 if (savedBubbleKeys == null) {
637 // There were no bubbles saved for this used.
638 return;
639 }
Evan Laird181de622019-10-24 09:53:02 -0400640 for (NotificationEntry e :
641 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400642 if (savedBubbleKeys.contains(e.getKey())
Beverly Taid1e175c2020-03-10 16:37:04 +0000643 && mNotificationInterruptStateProvider.shouldBubbleUp(e)
Mark Renoufc19b4732019-06-26 12:08:33 -0400644 && canLaunchInActivityView(mContext, e)) {
645 updateBubble(e, /* suppressFlyout= */ true);
646 }
647 }
648 // Finally, remove the entries for this user now that bubbles are restored.
649 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
650 }
651
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700652 @Override
653 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800654 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700655 }
656
657 @Override
658 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800659 updateForThemeChanges();
660 }
661
662 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700663 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700664 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700665 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800666 mBubbleIconFactory = new BubbleIconFactory(mContext);
667 for (Bubble b: mBubbleData.getBubbles()) {
668 // Reload each bubble
669 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
670 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700671 }
672
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400673 @Override
674 public void onConfigChanged(Configuration newConfig) {
675 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400676 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700677 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400678 }
679 }
680
Mady Mellor5549dd22018-11-06 18:07:34 -0800681 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800682 * Set a listener to be notified when some states of the bubbles change.
683 */
684 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
685 mStateChangeListener = listener;
686 }
687
688 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800689 * Set a listener to be notified of bubble expand events.
690 */
691 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100692 mExpandListener = ((isExpanding, key) -> {
693 if (listener != null) {
694 listener.onBubbleExpandChanged(isExpanding, key);
695 }
wilsonshihe8321942019-10-18 18:39:46 +0800696 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100697 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800698 if (mStackView != null) {
699 mStackView.setExpandListener(mExpandListener);
700 }
701 }
702
703 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800704 * Whether or not there are bubbles present, regardless of them being visible on the
705 * screen (e.g. if on AOD).
706 */
707 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800708 if (mStackView == null) {
709 return false;
710 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400711 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800712 }
713
714 /**
715 * Whether the stack of bubbles is expanded or not.
716 */
717 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400718 return mBubbleData.isExpanded();
719 }
720
721 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800722 * Tell the stack of bubbles to collapse.
723 */
724 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400725 mBubbleData.setExpanded(false /* expanded */);
726 }
727
Mady Mellorce23c462019-06-17 17:30:07 -0700728 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700729 * True if either:
730 * (1) There is a bubble associated with the provided key and if its notification is hidden
731 * from the shade.
732 * (2) There is a group summary associated with the provided key that is hidden from the shade
733 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700734 *
Mady Mellore28fe102019-07-09 15:33:32 -0700735 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700736 */
Beverlyed8aea22020-01-22 16:52:47 -0500737 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
738 String key = entry.getKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700739 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800740 && !mBubbleData.getBubbleWithKey(key).showInShade();
Beverlyed8aea22020-01-22 16:52:47 -0500741
742 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700743 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700744 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Beverlyed8aea22020-01-22 16:52:47 -0500745
Mady Mellore4348272019-07-29 17:43:36 -0700746 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700747 }
748
Lyn Hanb58c7562020-01-07 14:29:20 -0800749 void promoteBubbleFromOverflow(Bubble bubble) {
Lyn Han1e19d7f2020-02-05 19:10:58 -0800750 bubble.setInflateSynchronously(mInflateSynchronously);
751 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
Lyn Hanb58c7562020-01-07 14:29:20 -0800752 }
753
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800754 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400755 * Request the stack expand if needed, then select the specified Bubble as current.
756 *
757 * @param notificationKey the notification key for the bubble to be selected
758 */
759 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400760 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
761 if (bubble != null) {
762 mBubbleData.setSelectedBubble(bubble);
763 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400764 }
765 }
766
767 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500768 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
769 * is forwarded a back key down/up pair.
770 */
771 public void performBackPressIfNeeded() {
772 if (mStackView != null) {
773 mStackView.performBackPressIfNeeded();
774 }
775 }
776
777 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800778 * Adds or updates a bubble associated with the provided notification entry.
779 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400780 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800781 */
Mark Renouff97ed462019-04-05 13:46:24 -0400782 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700783 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400784 }
785
786 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700787 updateBubble(notif, suppressFlyout, true /* showInShade */);
788 }
789
790 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800791 if (mStackView == null) {
792 // Lazy init stack view when a bubble is created
793 ensureStackViewCreated();
794 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700795 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400796 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700797 notif.setInterruption();
798 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800799 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
800 bubble.setInflateSynchronously(mInflateSynchronously);
801 bubble.inflate(
802 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
803 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700804 }
805
806 /**
807 * Called when a user has indicated that an active notification should be shown as a bubble.
808 * <p>
809 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
810 * the notification from appearing in the shade.
811 *
812 * @param entry the notification to show as a bubble.
813 */
814 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800815 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
816 Log.d(TAG, "onUserCreatedBubble: " + entry.getKey());
817 }
Heemin Seogba6337f2019-12-10 15:34:37 -0800818 mShadeController.collapsePanel(true);
Mady Mellor7f234902019-10-20 12:06:29 -0700819 entry.setFlagBubble(true);
820 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
Mady Mellorff076eb2019-11-13 10:12:06 -0800821 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800822 mUserBlockedBubbles.remove(entry.getKey());
Mady Mellor7f234902019-10-20 12:06:29 -0700823 }
824
825 /**
826 * Called when a user has indicated that an active notification appearing as a bubble should
827 * no longer be shown as a bubble.
828 *
829 * @param entry the notification to no longer show as a bubble.
830 */
831 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800832 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
833 Log.d(TAG, "onUserDemotedBubble: " + entry.getKey());
834 }
Mady Mellor7f234902019-10-20 12:06:29 -0700835 entry.setFlagBubble(false);
Beverlya53fb0d2020-01-29 15:26:13 -0500836 removeBubble(entry, DISMISS_BLOCKED);
Mady Mellorff076eb2019-11-13 10:12:06 -0800837 mUserCreatedBubbles.remove(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800838 if (BubbleExperimentConfig.isPackageWhitelistedToAutoBubble(
839 mContext, entry.getSbn().getPackageName())) {
840 // This package is whitelist but user demoted the bubble, let's save it so we don't
841 // auto-bubble for the whitelist again.
842 mUserBlockedBubbles.add(entry.getKey());
843 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800844 }
845
846 /**
847 * Whether this bubble was explicitly created by the user via a SysUI affordance.
848 */
849 boolean isUserCreatedBubble(String key) {
850 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800851 }
852
Beverlya53fb0d2020-01-29 15:26:13 -0500853 boolean isSummaryOfUserCreatedBubble(NotificationEntry entry) {
854 if (isSummaryOfBubbles(entry)) {
855 List<Bubble> bubbleChildren =
856 mBubbleData.getBubblesInGroup(entry.getSbn().getGroupKey());
857 for (int i = 0; i < bubbleChildren.size(); i++) {
858 // Check if any are user-created (i.e. experimental bubbles)
859 if (isUserCreatedBubble(bubbleChildren.get(i).getKey())) {
860 return true;
861 }
862 }
863 }
864 return false;
865 }
866
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800867 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500868 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500869 * <p>
870 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800871 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500872 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500873 void removeBubble(NotificationEntry entry, int reason) {
874 if (mBubbleData.hasBubbleWithKey(entry.getKey())) {
875 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800876 }
877 }
878
Beverlyed8aea22020-01-22 16:52:47 -0500879 private void onEntryAdded(NotificationEntry entry) {
880 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
881 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
882 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
883 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor22f2f072019-04-18 13:26:18 -0700884
Beverly Taid1e175c2020-03-10 16:37:04 +0000885 if (mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Beverlyed8aea22020-01-22 16:52:47 -0500886 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
887 if (wasAdjusted && !previouslyUserCreated) {
888 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
889 mUserCreatedBubbles.add(entry.getKey());
Mady Mellorc2ff0112019-03-28 14:18:06 -0700890 }
Beverlyed8aea22020-01-22 16:52:47 -0500891 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700892 }
893 }
894
Beverlyed8aea22020-01-22 16:52:47 -0500895 private void onEntryUpdated(NotificationEntry entry) {
896 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
897 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
898 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
899 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700900
Beverly Taid1e175c2020-03-10 16:37:04 +0000901 boolean shouldBubble = mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Beverlyed8aea22020-01-22 16:52:47 -0500902 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
903 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
904 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500905 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500906 } else if (shouldBubble) {
907 if (wasAdjusted && !previouslyUserCreated) {
908 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
909 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800910 }
Beverlyed8aea22020-01-22 16:52:47 -0500911 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800912 }
Beverlyed8aea22020-01-22 16:52:47 -0500913 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800914
Beverlya53fb0d2020-01-29 15:26:13 -0500915 private void onEntryRemoved(NotificationEntry entry) {
916 if (isSummaryOfBubbles(entry)) {
917 final String groupKey = entry.getSbn().getGroupKey();
918 mBubbleData.removeSuppressedSummary(groupKey);
919
920 // Remove any associated bubble children with the summary
921 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
922 for (int i = 0; i < bubbleChildren.size(); i++) {
923 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
924 }
925 } else {
926 removeBubble(entry, DISMISS_NOTIF_CANCEL);
927 }
928 }
929
Mady Mellor56515c42020-02-18 17:58:36 -0800930 /**
931 * Called when NotificationListener has received adjusted notification rank and reapplied
932 * filtering and sorting. This is used to dismiss or create bubbles based on changes in
933 * permissions on the notification channel or the global setting.
934 *
935 * @param rankingMap the updated ranking map from NotificationListenerService
936 */
Beverlyed8aea22020-01-22 16:52:47 -0500937 private void onRankingUpdated(RankingMap rankingMap) {
Mady Mellor56515c42020-02-18 17:58:36 -0800938 if (mTmpRanking == null) {
939 mTmpRanking = new NotificationListenerService.Ranking();
940 }
941 String[] orderedKeys = rankingMap.getOrderedKeys();
942 for (int i = 0; i < orderedKeys.length; i++) {
943 String key = orderedKeys[i];
944 NotificationEntry entry = mNotificationEntryManager.getPendingOrActiveNotif(key);
945 rankingMap.getRanking(key, mTmpRanking);
Mady Mellore45ff862020-03-24 15:54:50 -0700946 boolean isActiveBubble = mBubbleData.hasBubbleWithKey(key);
947 if (isActiveBubble && !mTmpRanking.canBubble()) {
Mady Mellor56515c42020-02-18 17:58:36 -0800948 mBubbleData.notificationEntryRemoved(entry, BubbleController.DISMISS_BLOCKED);
Mady Mellore45ff862020-03-24 15:54:50 -0700949 } else if (entry != null && mTmpRanking.isBubble() && !isActiveBubble) {
Mady Mellor56515c42020-02-18 17:58:36 -0800950 entry.setFlagBubble(true);
951 onEntryUpdated(entry);
952 }
953 }
Beverlyed8aea22020-01-22 16:52:47 -0500954 }
Ned Burns01e38212019-01-03 16:32:52 -0500955
Mark Renouf71a3af62019-04-08 15:02:54 -0400956 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400957 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400958
Mark Renouf3bc5b362019-04-05 14:37:59 -0400959 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400960 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800961 // Update bubbles in overflow.
962 if (mOverflowCallback != null) {
963 mOverflowCallback.run();
964 }
965
Mark Renouf82a40e62019-05-23 16:16:24 -0400966 // Collapsing? Do this first before remaining steps.
967 if (update.expandedChanged && !update.expanded) {
968 mStackView.setExpanded(false);
969 }
970
971 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700972 ArrayList<Pair<Bubble, Integer>> removedBubbles =
973 new ArrayList<>(update.removedBubbles);
974 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400975 final Bubble bubble = removed.first;
976 @DismissReason final int reason = removed.second;
977 mStackView.removeBubble(bubble);
Mark Renoufc19b4732019-06-26 12:08:33 -0400978 // If the bubble is removed for user switching, leave the notification in place.
979 if (reason != DISMISS_USER_CHANGED) {
980 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800981 && !bubble.showInShade()) {
Beverlyed8aea22020-01-22 16:52:47 -0500982 // The bubble is now gone & the notification is hidden from the shade, so
983 // time to actually remove it
984 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -0500985 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -0500986 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400987 } else {
988 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400989 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700990
Mark Renoufc19b4732019-06-26 12:08:33 -0400991 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
992 // will get right result back
993 try {
994 mBarService.onNotificationBubbleChanged(bubble.getKey(),
995 false /* isBubble */);
996 } catch (RemoteException e) {
997 // Bad things have happened
998 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400999 }
Mady Mellor22f2f072019-04-18 13:26:18 -07001000
Ned Burns00b4b2d2019-10-17 22:09:27 -04001001 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -05001002 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
1003 // Time to potentially remove the summary
1004 for (NotifCallback cb : mCallbacks) {
1005 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -07001006 }
1007 }
Mady Mellora54e9fa2019-04-18 13:26:18 -07001008 }
1009 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001010
Lyn Hanc47e1712020-01-28 21:43:34 -08001011 if (update.addedBubble != null) {
1012 mStackView.addBubble(update.addedBubble);
1013 }
1014
Mark Renouf82a40e62019-05-23 16:16:24 -04001015 if (update.updatedBubble != null) {
1016 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -04001017 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001018
Lyn Hanb58c7562020-01-07 14:29:20 -08001019 // At this point, the correct bubbles are inflated in the stack.
1020 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -04001021 if (update.orderChanged) {
1022 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -04001023 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001024
Mark Renouf82a40e62019-05-23 16:16:24 -04001025 if (update.selectionChanged) {
1026 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -07001027 if (update.selectedBubble != null) {
1028 mNotificationGroupManager.updateSuppression(
1029 update.selectedBubble.getEntry());
1030 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001031 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001032
Mark Renouf82a40e62019-05-23 16:16:24 -04001033 // Expanding? Apply this last.
1034 if (update.expandedChanged && update.expanded) {
1035 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001036 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001037
Beverlyed8aea22020-01-22 16:52:47 -05001038 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001039 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001040 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001041 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001042
Issei Suzukia8d07312019-06-07 12:56:19 +02001043 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001044 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001045 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001046 mBubbleData.getSelectedBubble()));
1047
1048 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001049 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001050 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001051 mStackView.getExpandedBubble()));
1052 }
Lyn Hanb58c7562020-01-07 14:29:20 -08001053 Log.d(TAG, "\n[BubbleData] overflow:");
1054 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
1055 null));
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001056 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001057 }
1058 };
1059
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001060 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001061 * We intercept notification entries (including group summaries) dismissed by the user when
1062 * there is an active bubble associated with it. We do this so that developers can still
1063 * cancel it (and hence the bubbles associated with it). However, these intercepted
1064 * notifications should then be hidden from the shade since the user has cancelled them, so we
1065 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1066 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001067 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001068 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001069 */
Beverlya53fb0d2020-01-29 15:26:13 -05001070 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001071 if (entry == null) {
1072 return false;
1073 }
Beverlyed8aea22020-01-22 16:52:47 -05001074
Beverlya53fb0d2020-01-29 15:26:13 -05001075 final boolean interceptBubbleDismissal = mBubbleData.hasBubbleWithKey(entry.getKey())
1076 && entry.isBubble();
1077 final boolean interceptSummaryDismissal = isSummaryOfBubbles(entry);
Beverlyed8aea22020-01-22 16:52:47 -05001078
Beverlya53fb0d2020-01-29 15:26:13 -05001079 if (interceptSummaryDismissal) {
1080 handleSummaryDismissalInterception(entry);
1081 } else if (interceptBubbleDismissal) {
1082 Bubble bubble = mBubbleData.getBubbleWithKey(entry.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001083 bubble.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001084 bubble.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001085 } else {
Beverlyed8aea22020-01-22 16:52:47 -05001086 return false;
1087 }
Beverlya53fb0d2020-01-29 15:26:13 -05001088
1089 // Update the shade
1090 for (NotifCallback cb : mCallbacks) {
1091 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1092 }
1093 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001094 }
1095
Beverlya53fb0d2020-01-29 15:26:13 -05001096 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1097 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001098 return false;
1099 }
Beverlya53fb0d2020-01-29 15:26:13 -05001100
1101 String groupKey = entry.getSbn().getGroupKey();
1102 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1103 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1104 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1105 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1106 return (isSuppressedSummary || isSummary)
1107 && bubbleChildren != null
1108 && !bubbleChildren.isEmpty();
1109 }
1110
1111 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1112 // current children in the row:
Kevin Han43077f92020-02-28 12:51:53 -08001113 final List<NotificationEntry> children = summary.getAttachedNotifChildren();
Beverlya53fb0d2020-01-29 15:26:13 -05001114 if (children != null) {
1115 for (int i = 0; i < children.size(); i++) {
1116 NotificationEntry child = children.get(i);
1117 if (mBubbleData.hasBubbleWithKey(child.getKey())) {
1118 // Suppress the bubbled child
1119 // As far as group manager is concerned, once a child is no longer shown
1120 // in the shade, it is essentially removed.
1121 Bubble bubbleChild = mBubbleData.getBubbleWithKey(child.getKey());
1122 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1123 bubbleChild.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001124 bubbleChild.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001125 } else {
1126 // non-bubbled children can be removed
1127 for (NotifCallback cb : mCallbacks) {
1128 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1129 }
1130 }
1131 }
1132 }
1133
1134 // And since all children are removed, remove the summary.
1135 mNotificationGroupManager.onEntryRemoved(summary);
1136
1137 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1138 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1139 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001140 }
1141
1142 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001143 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001144 * Updates the visibility of the bubbles based on current state.
1145 * Does not un-bubble, just hides or un-hides. Notifies any
1146 * {@link BubbleStateChangeListener}s of visibility changes.
1147 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001148 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001149 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001150 if (mStackView == null) {
1151 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001152 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001153 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1154 // Bubbles only appear in unlocked shade
1155 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001156 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001157 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001158 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001159
Mady Mellor698d9e82019-08-01 23:11:53 +00001160 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001161 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001162 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001163 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001164 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1165 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1166 }
1167
1168 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001169 }
1170
1171 /**
1172 * Rect indicating the touchable region for the bubble stack / expanded stack.
1173 */
1174 public Rect getTouchableRegion() {
1175 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1176 return null;
1177 }
1178 mStackView.getBoundsOnScreen(mTempRect);
1179 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001180 }
1181
Mady Mellor390bff42019-04-05 15:09:01 -07001182 /**
1183 * The display id of the expanded view, if the stack is expanded and not occluded by the
1184 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1185 */
1186 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001187 if (mStackView == null) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001188 return INVALID_DISPLAY;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001189 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001190 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001191 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Lyn Han9f66c3b2020-03-05 23:59:29 -08001192 final BubbleViewProvider expandedViewProvider = mStackView.getExpandedBubble();
1193 if (defaultDisplay && expandedViewProvider != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001194 && !mNotificationShadeWindowController.getPanelExpanded()) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001195 return expandedViewProvider.getDisplayId();
Mady Mellor390bff42019-04-05 15:09:01 -07001196 }
Lyn Han9f66c3b2020-03-05 23:59:29 -08001197 return INVALID_DISPLAY;
Mady Mellor390bff42019-04-05 15:09:01 -07001198 }
1199
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001200 @VisibleForTesting
1201 BubbleStackView getStackView() {
1202 return mStackView;
1203 }
1204
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001205 /**
1206 * Description of current bubble state.
1207 */
1208 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1209 pw.println("BubbleController state:");
1210 mBubbleData.dump(fd, pw, args);
1211 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001212 if (mStackView != null) {
1213 mStackView.dump(fd, pw, args);
1214 }
1215 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001216 }
1217
Mady Mellore80930e2019-03-21 16:00:45 -07001218 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001219 * This task stack listener is responsible for responding to tasks moved to the front
1220 * which are on the default (main) display. When this happens, expanded bubbles must be
1221 * collapsed so the user may interact with the app which was just moved to the front.
1222 * <p>
1223 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1224 * these calls via a main thread Handler.
1225 */
1226 @MainThread
1227 private class BubbleTaskStackListener extends TaskStackChangeListener {
1228
Mark Renoufcecc77b2019-01-30 16:32:24 -05001229 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001230 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1231 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001232 if (!mStackView.isExpansionAnimating()) {
1233 mBubbleData.setExpanded(false);
1234 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001235 }
1236 }
1237
Mark Renoufcecc77b2019-01-30 16:32:24 -05001238 @Override
Winson Chunge789ff62020-02-24 14:40:23 -08001239 public void onActivityRestartAttempt(RunningTaskInfo task, boolean homeTaskVisible,
1240 boolean clearedTask) {
1241 for (Bubble b : mBubbleData.getBubbles()) {
1242 if (b.getDisplayId() == task.displayId) {
1243 expandStackAndSelectBubble(b.getKey());
1244 return;
1245 }
1246 }
1247 }
1248
1249 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001250 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001251 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001252 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001253 }
1254 }
Mark Renouf446251d2019-04-26 10:22:41 -04001255
1256 @Override
1257 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1258 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1259 mBubbleData.setExpanded(false);
1260 }
1261 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001262
1263 @Override
1264 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001265 if (mStackView == null) {
1266 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001267 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001268 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001269 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001270
1271 @Override
1272 public void onSingleTaskDisplayEmpty(int displayId) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001273 final BubbleViewProvider expandedBubble = mStackView != null
Mady Mellor5186b132019-09-16 17:55:48 -07001274 ? mStackView.getExpandedBubble()
1275 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001276 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1277 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001278 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001279 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001280 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001281 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001282 }
1283
Mady Mellorca0c24c2019-05-16 16:14:32 -07001284 /**
1285 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1286 *
1287 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1288 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1289 *
1290 * @param context the context to use.
1291 * @param entry the entry to bubble.
1292 */
1293 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1294 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Melloraa9ce172020-03-17 10:34:20 -07001295 ? entry.getBubbleMetadata().getIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001296 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001297 if (entry.getBubbleMetadata() != null
1298 && entry.getBubbleMetadata().getShortcutId() != null) {
1299 return true;
1300 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001301 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001302 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001303 return false;
1304 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001305 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1306 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001307 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001308 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001309 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001310 Log.w(TAG, "Unable to send as bubble, "
1311 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001312 + intent);
1313 return false;
1314 }
1315 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001316 Log.w(TAG, "Unable to send as bubble, "
1317 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001318 + intent);
1319 return false;
1320 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001321 return true;
1322 }
1323
Joshua Tsujia19515f2019-02-13 18:02:29 -05001324 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001325 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001326 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001327 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Joshua Tsujiff6b0f22020-03-09 14:55:19 -04001328 if (mStackView != null) {
Joshua Tsujid9422832019-03-05 13:32:37 -05001329 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001330 }
1331 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001332 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001333}