blob: 7adeb56e0994999606ef4f30fffb9395bfbbbe77 [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()) {
334 b.setShowDot(b.showInShade(), true /* animate */);
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()) {
341 b.setShowDot(b.showInShade(), true /* animate */);
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,
602 mSysUiState);
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 /**
722 * Tell the stack of bubbles to expand.
723 */
724 public void expandStack() {
725 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800726 }
727
728 /**
729 * Tell the stack of bubbles to collapse.
730 */
731 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400732 mBubbleData.setExpanded(false /* expanded */);
733 }
734
Mady Mellorce23c462019-06-17 17:30:07 -0700735 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700736 * True if either:
737 * (1) There is a bubble associated with the provided key and if its notification is hidden
738 * from the shade.
739 * (2) There is a group summary associated with the provided key that is hidden from the shade
740 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700741 *
Mady Mellore28fe102019-07-09 15:33:32 -0700742 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700743 */
Beverlyed8aea22020-01-22 16:52:47 -0500744 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
745 String key = entry.getKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700746 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800747 && !mBubbleData.getBubbleWithKey(key).showInShade();
Beverlyed8aea22020-01-22 16:52:47 -0500748
749 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700750 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700751 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Beverlyed8aea22020-01-22 16:52:47 -0500752
Mady Mellore4348272019-07-29 17:43:36 -0700753 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700754 }
755
Mark Renouf71a3af62019-04-08 15:02:54 -0400756 @VisibleForTesting
757 void selectBubble(String key) {
758 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellor247ca2c2019-12-02 16:18:59 -0800759 mBubbleData.setSelectedBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800760 }
761
Lyn Hanb58c7562020-01-07 14:29:20 -0800762 void promoteBubbleFromOverflow(Bubble bubble) {
Lyn Han1e19d7f2020-02-05 19:10:58 -0800763 bubble.setInflateSynchronously(mInflateSynchronously);
764 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
Lyn Hanb58c7562020-01-07 14:29:20 -0800765 }
766
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800767 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400768 * Request the stack expand if needed, then select the specified Bubble as current.
769 *
770 * @param notificationKey the notification key for the bubble to be selected
771 */
772 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400773 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
774 if (bubble != null) {
775 mBubbleData.setSelectedBubble(bubble);
776 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400777 }
778 }
779
780 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800781 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
782 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500783 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400784 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800785 }
786
787 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500788 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
789 * is forwarded a back key down/up pair.
790 */
791 public void performBackPressIfNeeded() {
792 if (mStackView != null) {
793 mStackView.performBackPressIfNeeded();
794 }
795 }
796
797 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800798 * Adds or updates a bubble associated with the provided notification entry.
799 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400800 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800801 */
Mark Renouff97ed462019-04-05 13:46:24 -0400802 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700803 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400804 }
805
806 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700807 updateBubble(notif, suppressFlyout, true /* showInShade */);
808 }
809
810 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800811 if (mStackView == null) {
812 // Lazy init stack view when a bubble is created
813 ensureStackViewCreated();
814 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700815 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400816 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700817 notif.setInterruption();
818 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800819 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
820 bubble.setInflateSynchronously(mInflateSynchronously);
821 bubble.inflate(
822 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
823 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700824 }
825
826 /**
827 * Called when a user has indicated that an active notification should be shown as a bubble.
828 * <p>
829 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
830 * the notification from appearing in the shade.
831 *
832 * @param entry the notification to show as a bubble.
833 */
834 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800835 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
836 Log.d(TAG, "onUserCreatedBubble: " + entry.getKey());
837 }
Heemin Seogba6337f2019-12-10 15:34:37 -0800838 mShadeController.collapsePanel(true);
Mady Mellor7f234902019-10-20 12:06:29 -0700839 entry.setFlagBubble(true);
840 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
Mady Mellorff076eb2019-11-13 10:12:06 -0800841 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800842 mUserBlockedBubbles.remove(entry.getKey());
Mady Mellor7f234902019-10-20 12:06:29 -0700843 }
844
845 /**
846 * Called when a user has indicated that an active notification appearing as a bubble should
847 * no longer be shown as a bubble.
848 *
849 * @param entry the notification to no longer show as a bubble.
850 */
851 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800852 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
853 Log.d(TAG, "onUserDemotedBubble: " + entry.getKey());
854 }
Mady Mellor7f234902019-10-20 12:06:29 -0700855 entry.setFlagBubble(false);
Beverlya53fb0d2020-01-29 15:26:13 -0500856 removeBubble(entry, DISMISS_BLOCKED);
Mady Mellorff076eb2019-11-13 10:12:06 -0800857 mUserCreatedBubbles.remove(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800858 if (BubbleExperimentConfig.isPackageWhitelistedToAutoBubble(
859 mContext, entry.getSbn().getPackageName())) {
860 // This package is whitelist but user demoted the bubble, let's save it so we don't
861 // auto-bubble for the whitelist again.
862 mUserBlockedBubbles.add(entry.getKey());
863 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800864 }
865
866 /**
867 * Whether this bubble was explicitly created by the user via a SysUI affordance.
868 */
869 boolean isUserCreatedBubble(String key) {
870 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800871 }
872
Beverlya53fb0d2020-01-29 15:26:13 -0500873 boolean isSummaryOfUserCreatedBubble(NotificationEntry entry) {
874 if (isSummaryOfBubbles(entry)) {
875 List<Bubble> bubbleChildren =
876 mBubbleData.getBubblesInGroup(entry.getSbn().getGroupKey());
877 for (int i = 0; i < bubbleChildren.size(); i++) {
878 // Check if any are user-created (i.e. experimental bubbles)
879 if (isUserCreatedBubble(bubbleChildren.get(i).getKey())) {
880 return true;
881 }
882 }
883 }
884 return false;
885 }
886
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800887 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500888 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500889 * <p>
890 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800891 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500892 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500893 void removeBubble(NotificationEntry entry, int reason) {
894 if (mBubbleData.hasBubbleWithKey(entry.getKey())) {
895 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800896 }
897 }
898
Beverlyed8aea22020-01-22 16:52:47 -0500899 private void onEntryAdded(NotificationEntry entry) {
900 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
901 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
902 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
903 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor22f2f072019-04-18 13:26:18 -0700904
Beverly Taid1e175c2020-03-10 16:37:04 +0000905 if (mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Beverlyed8aea22020-01-22 16:52:47 -0500906 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
907 if (wasAdjusted && !previouslyUserCreated) {
908 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
909 mUserCreatedBubbles.add(entry.getKey());
Mady Mellorc2ff0112019-03-28 14:18:06 -0700910 }
Beverlyed8aea22020-01-22 16:52:47 -0500911 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700912 }
913 }
914
Beverlyed8aea22020-01-22 16:52:47 -0500915 private void onEntryUpdated(NotificationEntry entry) {
916 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
917 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
918 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
919 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700920
Beverly Taid1e175c2020-03-10 16:37:04 +0000921 boolean shouldBubble = mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Beverlyed8aea22020-01-22 16:52:47 -0500922 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
923 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
924 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500925 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500926 } else if (shouldBubble) {
927 if (wasAdjusted && !previouslyUserCreated) {
928 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
929 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800930 }
Beverlyed8aea22020-01-22 16:52:47 -0500931 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800932 }
Beverlyed8aea22020-01-22 16:52:47 -0500933 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800934
Beverlya53fb0d2020-01-29 15:26:13 -0500935 private void onEntryRemoved(NotificationEntry entry) {
936 if (isSummaryOfBubbles(entry)) {
937 final String groupKey = entry.getSbn().getGroupKey();
938 mBubbleData.removeSuppressedSummary(groupKey);
939
940 // Remove any associated bubble children with the summary
941 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
942 for (int i = 0; i < bubbleChildren.size(); i++) {
943 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
944 }
945 } else {
946 removeBubble(entry, DISMISS_NOTIF_CANCEL);
947 }
948 }
949
Mady Mellor56515c42020-02-18 17:58:36 -0800950 /**
951 * Called when NotificationListener has received adjusted notification rank and reapplied
952 * filtering and sorting. This is used to dismiss or create bubbles based on changes in
953 * permissions on the notification channel or the global setting.
954 *
955 * @param rankingMap the updated ranking map from NotificationListenerService
956 */
Beverlyed8aea22020-01-22 16:52:47 -0500957 private void onRankingUpdated(RankingMap rankingMap) {
Mady Mellor56515c42020-02-18 17:58:36 -0800958 if (mTmpRanking == null) {
959 mTmpRanking = new NotificationListenerService.Ranking();
960 }
961 String[] orderedKeys = rankingMap.getOrderedKeys();
962 for (int i = 0; i < orderedKeys.length; i++) {
963 String key = orderedKeys[i];
964 NotificationEntry entry = mNotificationEntryManager.getPendingOrActiveNotif(key);
965 rankingMap.getRanking(key, mTmpRanking);
966 if (mBubbleData.hasBubbleWithKey(key) && !mTmpRanking.canBubble()) {
967 mBubbleData.notificationEntryRemoved(entry, BubbleController.DISMISS_BLOCKED);
968 } else if (entry != null && mTmpRanking.isBubble()) {
969 entry.setFlagBubble(true);
970 onEntryUpdated(entry);
971 }
972 }
Beverlyed8aea22020-01-22 16:52:47 -0500973 }
Ned Burns01e38212019-01-03 16:32:52 -0500974
Mark Renouf71a3af62019-04-08 15:02:54 -0400975 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400976 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400977
Mark Renouf3bc5b362019-04-05 14:37:59 -0400978 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400979 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800980 // Update bubbles in overflow.
981 if (mOverflowCallback != null) {
982 mOverflowCallback.run();
983 }
984
Mark Renouf82a40e62019-05-23 16:16:24 -0400985 // Collapsing? Do this first before remaining steps.
986 if (update.expandedChanged && !update.expanded) {
987 mStackView.setExpanded(false);
988 }
989
990 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700991 ArrayList<Pair<Bubble, Integer>> removedBubbles =
992 new ArrayList<>(update.removedBubbles);
993 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400994 final Bubble bubble = removed.first;
995 @DismissReason final int reason = removed.second;
996 mStackView.removeBubble(bubble);
Mark Renoufc19b4732019-06-26 12:08:33 -0400997 // If the bubble is removed for user switching, leave the notification in place.
998 if (reason != DISMISS_USER_CHANGED) {
999 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -08001000 && !bubble.showInShade()) {
Beverlyed8aea22020-01-22 16:52:47 -05001001 // The bubble is now gone & the notification is hidden from the shade, so
1002 // time to actually remove it
1003 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001004 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -05001005 }
Mark Renoufc19b4732019-06-26 12:08:33 -04001006 } else {
1007 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -04001008 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -07001009
Mark Renoufc19b4732019-06-26 12:08:33 -04001010 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
1011 // will get right result back
1012 try {
1013 mBarService.onNotificationBubbleChanged(bubble.getKey(),
1014 false /* isBubble */);
1015 } catch (RemoteException e) {
1016 // Bad things have happened
1017 }
Mark Renouf82a40e62019-05-23 16:16:24 -04001018 }
Mady Mellor22f2f072019-04-18 13:26:18 -07001019
Ned Burns00b4b2d2019-10-17 22:09:27 -04001020 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -05001021 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
1022 // Time to potentially remove the summary
1023 for (NotifCallback cb : mCallbacks) {
1024 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -07001025 }
1026 }
Mady Mellora54e9fa2019-04-18 13:26:18 -07001027 }
1028 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001029
Lyn Hanc47e1712020-01-28 21:43:34 -08001030 if (update.addedBubble != null) {
1031 mStackView.addBubble(update.addedBubble);
1032 }
1033
Mark Renouf82a40e62019-05-23 16:16:24 -04001034 if (update.updatedBubble != null) {
1035 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -04001036 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001037
Lyn Hanb58c7562020-01-07 14:29:20 -08001038 // At this point, the correct bubbles are inflated in the stack.
1039 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -04001040 if (update.orderChanged) {
1041 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -04001042 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001043
Mark Renouf82a40e62019-05-23 16:16:24 -04001044 if (update.selectionChanged) {
1045 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -07001046 if (update.selectedBubble != null) {
1047 mNotificationGroupManager.updateSuppression(
1048 update.selectedBubble.getEntry());
1049 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001050 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001051
Mark Renouf82a40e62019-05-23 16:16:24 -04001052 // Expanding? Apply this last.
1053 if (update.expandedChanged && update.expanded) {
1054 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001055 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001056
Beverlyed8aea22020-01-22 16:52:47 -05001057 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001058 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001059 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001060 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001061
Issei Suzukia8d07312019-06-07 12:56:19 +02001062 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001063 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001064 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001065 mBubbleData.getSelectedBubble()));
1066
1067 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001068 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001069 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001070 mStackView.getExpandedBubble()));
1071 }
Lyn Hanb58c7562020-01-07 14:29:20 -08001072 Log.d(TAG, "\n[BubbleData] overflow:");
1073 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
1074 null));
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001075 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001076 }
1077 };
1078
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001079 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001080 * We intercept notification entries (including group summaries) dismissed by the user when
1081 * there is an active bubble associated with it. We do this so that developers can still
1082 * cancel it (and hence the bubbles associated with it). However, these intercepted
1083 * notifications should then be hidden from the shade since the user has cancelled them, so we
1084 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1085 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001086 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001087 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001088 */
Beverlya53fb0d2020-01-29 15:26:13 -05001089 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001090 if (entry == null) {
1091 return false;
1092 }
Beverlyed8aea22020-01-22 16:52:47 -05001093
Beverlya53fb0d2020-01-29 15:26:13 -05001094 final boolean interceptBubbleDismissal = mBubbleData.hasBubbleWithKey(entry.getKey())
1095 && entry.isBubble();
1096 final boolean interceptSummaryDismissal = isSummaryOfBubbles(entry);
Beverlyed8aea22020-01-22 16:52:47 -05001097
Beverlya53fb0d2020-01-29 15:26:13 -05001098 if (interceptSummaryDismissal) {
1099 handleSummaryDismissalInterception(entry);
1100 } else if (interceptBubbleDismissal) {
1101 Bubble bubble = mBubbleData.getBubbleWithKey(entry.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001102 bubble.setSuppressNotification(true);
1103 bubble.setShowDot(false /* show */, true /* animate */);
Beverlya53fb0d2020-01-29 15:26:13 -05001104 } else {
Beverlyed8aea22020-01-22 16:52:47 -05001105 return false;
1106 }
Beverlya53fb0d2020-01-29 15:26:13 -05001107
1108 // Update the shade
1109 for (NotifCallback cb : mCallbacks) {
1110 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1111 }
1112 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001113 }
1114
Beverlya53fb0d2020-01-29 15:26:13 -05001115 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1116 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001117 return false;
1118 }
Beverlya53fb0d2020-01-29 15:26:13 -05001119
1120 String groupKey = entry.getSbn().getGroupKey();
1121 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1122 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1123 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1124 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1125 return (isSuppressedSummary || isSummary)
1126 && bubbleChildren != null
1127 && !bubbleChildren.isEmpty();
1128 }
1129
1130 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1131 // current children in the row:
1132 final List<NotificationEntry> children = summary.getChildren();
1133 if (children != null) {
1134 for (int i = 0; i < children.size(); i++) {
1135 NotificationEntry child = children.get(i);
1136 if (mBubbleData.hasBubbleWithKey(child.getKey())) {
1137 // Suppress the bubbled child
1138 // As far as group manager is concerned, once a child is no longer shown
1139 // in the shade, it is essentially removed.
1140 Bubble bubbleChild = mBubbleData.getBubbleWithKey(child.getKey());
1141 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1142 bubbleChild.setSuppressNotification(true);
1143 bubbleChild.setShowDot(false /* show */, true /* animate */);
1144 } else {
1145 // non-bubbled children can be removed
1146 for (NotifCallback cb : mCallbacks) {
1147 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1148 }
1149 }
1150 }
1151 }
1152
1153 // And since all children are removed, remove the summary.
1154 mNotificationGroupManager.onEntryRemoved(summary);
1155
1156 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1157 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1158 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001159 }
1160
1161 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001162 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001163 * Updates the visibility of the bubbles based on current state.
1164 * Does not un-bubble, just hides or un-hides. Notifies any
1165 * {@link BubbleStateChangeListener}s of visibility changes.
1166 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001167 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001168 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001169 if (mStackView == null) {
1170 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001171 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001172 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1173 // Bubbles only appear in unlocked shade
1174 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001175 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001176 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001177 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001178
Mady Mellor698d9e82019-08-01 23:11:53 +00001179 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001180 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001181 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001182 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001183 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1184 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1185 }
1186
1187 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001188 }
1189
1190 /**
1191 * Rect indicating the touchable region for the bubble stack / expanded stack.
1192 */
1193 public Rect getTouchableRegion() {
1194 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1195 return null;
1196 }
1197 mStackView.getBoundsOnScreen(mTempRect);
1198 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001199 }
1200
Mady Mellor390bff42019-04-05 15:09:01 -07001201 /**
1202 * The display id of the expanded view, if the stack is expanded and not occluded by the
1203 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1204 */
1205 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001206 if (mStackView == null) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001207 return INVALID_DISPLAY;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001208 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001209 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001210 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Lyn Han9f66c3b2020-03-05 23:59:29 -08001211 final BubbleViewProvider expandedViewProvider = mStackView.getExpandedBubble();
1212 if (defaultDisplay && expandedViewProvider != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001213 && !mNotificationShadeWindowController.getPanelExpanded()) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001214 return expandedViewProvider.getDisplayId();
Mady Mellor390bff42019-04-05 15:09:01 -07001215 }
Lyn Han9f66c3b2020-03-05 23:59:29 -08001216 return INVALID_DISPLAY;
Mady Mellor390bff42019-04-05 15:09:01 -07001217 }
1218
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001219 @VisibleForTesting
1220 BubbleStackView getStackView() {
1221 return mStackView;
1222 }
1223
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001224 /**
1225 * Description of current bubble state.
1226 */
1227 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1228 pw.println("BubbleController state:");
1229 mBubbleData.dump(fd, pw, args);
1230 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001231 if (mStackView != null) {
1232 mStackView.dump(fd, pw, args);
1233 }
1234 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001235 }
1236
Mady Mellore80930e2019-03-21 16:00:45 -07001237 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001238 * This task stack listener is responsible for responding to tasks moved to the front
1239 * which are on the default (main) display. When this happens, expanded bubbles must be
1240 * collapsed so the user may interact with the app which was just moved to the front.
1241 * <p>
1242 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1243 * these calls via a main thread Handler.
1244 */
1245 @MainThread
1246 private class BubbleTaskStackListener extends TaskStackChangeListener {
1247
Mark Renoufcecc77b2019-01-30 16:32:24 -05001248 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001249 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1250 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001251 if (!mStackView.isExpansionAnimating()) {
1252 mBubbleData.setExpanded(false);
1253 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001254 }
1255 }
1256
Mark Renoufcecc77b2019-01-30 16:32:24 -05001257 @Override
Winson Chunge789ff62020-02-24 14:40:23 -08001258 public void onActivityRestartAttempt(RunningTaskInfo task, boolean homeTaskVisible,
1259 boolean clearedTask) {
1260 for (Bubble b : mBubbleData.getBubbles()) {
1261 if (b.getDisplayId() == task.displayId) {
1262 expandStackAndSelectBubble(b.getKey());
1263 return;
1264 }
1265 }
1266 }
1267
1268 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001269 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001270 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001271 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001272 }
1273 }
Mark Renouf446251d2019-04-26 10:22:41 -04001274
1275 @Override
1276 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1277 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1278 mBubbleData.setExpanded(false);
1279 }
1280 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001281
1282 @Override
1283 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001284 if (mStackView == null) {
1285 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001286 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001287 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001288 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001289
1290 @Override
1291 public void onSingleTaskDisplayEmpty(int displayId) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001292 final BubbleViewProvider expandedBubble = mStackView != null
Mady Mellor5186b132019-09-16 17:55:48 -07001293 ? mStackView.getExpandedBubble()
1294 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001295 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1296 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001297 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001298 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001299 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001300 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001301 }
1302
Mady Mellorca0c24c2019-05-16 16:14:32 -07001303 /**
1304 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1305 *
1306 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1307 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1308 *
1309 * @param context the context to use.
1310 * @param entry the entry to bubble.
1311 */
1312 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1313 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Melloraa9ce172020-03-17 10:34:20 -07001314 ? entry.getBubbleMetadata().getIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001315 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001316 if (entry.getBubbleMetadata() != null
1317 && entry.getBubbleMetadata().getShortcutId() != null) {
1318 return true;
1319 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001320 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001321 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001322 return false;
1323 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001324 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1325 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001326 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001327 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001328 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001329 Log.w(TAG, "Unable to send as bubble, "
1330 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001331 + intent);
1332 return false;
1333 }
1334 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001335 Log.w(TAG, "Unable to send as bubble, "
1336 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001337 + intent);
1338 return false;
1339 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001340 return true;
1341 }
1342
Joshua Tsujia19515f2019-02-13 18:02:29 -05001343 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001344 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001345 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001346 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Joshua Tsujiff6b0f22020-03-09 14:55:19 -04001347 if (mStackView != null) {
Joshua Tsujid9422832019-03-05 13:32:37 -05001348 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001349 }
1350 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001351 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001352}