blob: 878a88452f43885a7e4ab06f064647340e5266ae [file] [log] [blame]
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.bubbles;
18
Mady Mellor3a0a1b42019-05-23 06:40:21 -070019import static android.app.Notification.FLAG_BUBBLE;
Mady Mellor9adfe6a2020-03-30 17:23:26 -070020import static android.app.NotificationManager.BUBBLE_PREFERENCE_NONE;
21import static android.app.NotificationManager.BUBBLE_PREFERENCE_SELECTED;
Mady Mellorc2ff0112019-03-28 14:18:06 -070022import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL;
23import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL;
24import static android.service.notification.NotificationListenerService.REASON_CANCEL;
25import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -070026import static android.service.notification.NotificationListenerService.REASON_CLICK;
27import static android.service.notification.NotificationListenerService.REASON_GROUP_SUMMARY_CANCELED;
Mady Mellor390bff42019-04-05 15:09:01 -070028import static android.view.Display.DEFAULT_DISPLAY;
29import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080030import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080031import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080032import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080033
Issei Suzukia8d07312019-06-07 12:56:19 +020034import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_CONTROLLER;
35import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
36import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080037import static com.android.systemui.statusbar.StatusBarState.SHADE;
Mady Mellor1a4e86f2019-05-03 16:07:23 -070038import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080039
Mark Renoufba5ab512019-05-02 15:21:01 -040040import static java.lang.annotation.ElementType.FIELD;
41import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
42import static java.lang.annotation.ElementType.PARAMETER;
Mark Renouf08bc42a2019-03-07 13:01:59 -050043import static java.lang.annotation.RetentionPolicy.SOURCE;
44
Mark Renoufc19b4732019-06-26 12:08:33 -040045import android.annotation.UserIdInt;
Mark Renoufc808f062019-02-07 15:20:37 -050046import android.app.ActivityManager.RunningTaskInfo;
Mady Mellor9adfe6a2020-03-30 17:23:26 -070047import android.app.INotificationManager;
48import android.app.Notification;
49import android.app.NotificationChannel;
Mady Mellor66efd5e2019-05-15 13:38:11 -070050import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070051import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080052import android.content.Context;
Mady Mellorca0c24c2019-05-16 16:14:32 -070053import android.content.pm.ActivityInfo;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080054import android.content.pm.PackageManager;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040055import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080056import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050057import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080058import android.os.ServiceManager;
Mady Mellor56515c42020-02-18 17:58:36 -080059import android.service.notification.NotificationListenerService;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040060import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040061import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040062import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040063import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040064import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040065import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050066import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080067import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080068import android.widget.FrameLayout;
69
Mark Renouf08bc42a2019-03-07 13:01:59 -050070import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050071import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040072import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050073
Mady Mellorebdbbb92018-11-15 14:36:48 -080074import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070075import com.android.internal.statusbar.IStatusBarService;
Beverlya53fb0d2020-01-29 15:26:13 -050076import com.android.internal.statusbar.NotificationVisibility;
Beverlya53fb0d2020-01-29 15:26:13 -050077import com.android.systemui.Dumpable;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080078import com.android.systemui.R;
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +010079import com.android.systemui.bubbles.dagger.BubbleModule;
Ned Burnsaaeb44b2020-02-12 23:48:26 -050080import com.android.systemui.dump.DumpManager;
Joshua Tsujibe60a582020-03-23 17:17:26 -040081import com.android.systemui.model.SysUiState;
Beverly8fdb5332019-02-04 14:29:49 -050082import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050083import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000084import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050085import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050086import com.android.systemui.shared.system.WindowManagerWrapper;
Beverlya53fb0d2020-01-29 15:26:13 -050087import com.android.systemui.statusbar.FeatureFlags;
Mark Renoufc19b4732019-06-26 12:08:33 -040088import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070089import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Mady Mellor9adfe6a2020-03-30 17:23:26 -070090import com.android.systemui.statusbar.notification.NotificationChannelHelper;
Ned Burns01e38212019-01-03 16:32:52 -050091import com.android.systemui.statusbar.notification.NotificationEntryListener;
92import com.android.systemui.statusbar.notification.NotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -050093import com.android.systemui.statusbar.notification.collection.NotifCollection;
94import com.android.systemui.statusbar.notification.collection.NotifPipeline;
Ned Burnsf81c4c42019-01-07 14:10:43 -050095import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Beverlya53fb0d2020-01-29 15:26:13 -050096import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener;
Beverly Taid1e175c2020-03-10 16:37:04 +000097import com.android.systemui.statusbar.notification.interruption.NotificationInterruptStateProvider;
Mady Mellor22f2f072019-04-18 13:26:18 -070098import com.android.systemui.statusbar.phone.NotificationGroupManager;
wilsonshihe8321942019-10-18 18:39:46 +080099import com.android.systemui.statusbar.phone.NotificationShadeWindowController;
Mady Mellor7f234902019-10-20 12:06:29 -0700100import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorf3b9fab2019-11-13 17:27:32 -0800101import com.android.systemui.statusbar.phone.StatusBar;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700102import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400103import com.android.systemui.statusbar.policy.ZenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500104import com.android.systemui.util.FloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800105
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700106import java.io.FileDescriptor;
107import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500108import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -0400109import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -0700110import java.util.ArrayList;
Mady Mellorff076eb2019-11-13 10:12:06 -0800111import java.util.HashSet;
Lyn Hanb58c7562020-01-07 14:29:20 -0800112import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500113
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800114/**
115 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
116 * Bubbles can be expanded to show more content.
117 *
118 * The controller manages addition, removal, and visible state of bubbles on screen.
119 */
Beverlya53fb0d2020-01-29 15:26:13 -0500120public class BubbleController implements ConfigurationController.ConfigurationListener, Dumpable {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800121
Issei Suzukia8d07312019-06-07 12:56:19 +0200122 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800123
Mark Renouf08bc42a2019-03-07 13:01:59 -0500124 @Retention(SOURCE)
125 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400126 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700127 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400128 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500129 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700130
Mark Renouf08bc42a2019-03-07 13:01:59 -0500131 static final int DISMISS_USER_GESTURE = 1;
132 static final int DISMISS_AGED = 2;
133 static final int DISMISS_TASK_FINISHED = 3;
134 static final int DISMISS_BLOCKED = 4;
135 static final int DISMISS_NOTIF_CANCEL = 5;
136 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700137 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400138 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700139 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700140 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500141
Ned Burns01e38212019-01-03 16:32:52 -0500142 private final Context mContext;
143 private final NotificationEntryManager mNotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500144 private final NotifPipeline mNotifPipeline;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500145 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800146 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800147 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100148 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700149 private final NotificationGroupManager mNotificationGroupManager;
Heemin Seogba6337f2019-12-10 15:34:37 -0800150 private final ShadeController mShadeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500151 private final FloatingContentCoordinator mFloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800152
Mady Mellor3dff9e62019-02-05 18:12:53 -0800153 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400154 @Nullable private BubbleStackView mStackView;
Mady Mellor247ca2c2019-12-02 16:18:59 -0800155 private BubbleIconFactory mBubbleIconFactory;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800156
Mark Renoufc19b4732019-06-26 12:08:33 -0400157 // Tracks the id of the current (foreground) user.
158 private int mCurrentUserId;
159 // Saves notification keys of active bubbles when users are switched.
160 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
161
Mady Mellor56515c42020-02-18 17:58:36 -0800162 // Used when ranking updates occur and we check if things should bubble / unbubble
163 private NotificationListenerService.Ranking mTmpRanking;
164
Mady Mellorff076eb2019-11-13 10:12:06 -0800165 // Saves notification keys of user created "fake" bubbles so that we can allow notifications
166 // like these to bubble by default. Doesn't persist across reboots, not a long-term solution.
167 private final HashSet<String> mUserCreatedBubbles;
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800168 // If we're auto-bubbling bubbles via a whitelist, we need to track which notifs from that app
169 // have been "demoted" back to a notification so that we don't auto-bubbles those again.
170 // Doesn't persist across reboots, not a long-term solution.
171 private final HashSet<String> mUserBlockedBubbles;
Mady Mellorff076eb2019-11-13 10:12:06 -0800172
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800173 // Bubbles get added to the status bar view
wilsonshihe8321942019-10-18 18:39:46 +0800174 private final NotificationShadeWindowController mNotificationShadeWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400175 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800176 private StatusBarStateListener mStatusBarStateListener;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700177 private INotificationManager mINotificationManager;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500178
Lyn Hanb58c7562020-01-07 14:29:20 -0800179 // Callback that updates BubbleOverflowActivity on data change.
180 @Nullable private Runnable mOverflowCallback = null;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800181
Beverly Taid1e175c2020-03-10 16:37:04 +0000182 private final NotificationInterruptStateProvider mNotificationInterruptStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700183 private IStatusBarService mBarService;
Joshua Tsujibe60a582020-03-23 17:17:26 -0400184 private SysUiState mSysUiState;
Mady Mellorb4991e62019-01-10 15:14:51 -0800185
Mady Mellord1c78b262018-11-06 18:04:40 -0800186 // Used for determining view rect for touch interaction
187 private Rect mTempRect = new Rect();
188
Mark Renoufc19b4732019-06-26 12:08:33 -0400189 // Listens to user switch so bubbles can be saved and restored.
190 private final NotificationLockscreenUserManager mNotifUserManager;
191
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400192 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
193 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
194
Mady Mellor3df7ab02019-12-09 15:07:10 -0800195 private boolean mInflateSynchronously;
196
Beverlyed8aea22020-01-22 16:52:47 -0500197 // TODO (b/145659174): allow for multiple callbacks to support the "shadow" new notif pipeline
198 private final List<NotifCallback> mCallbacks = new ArrayList<>();
199
Mady Mellor5549dd22018-11-06 18:07:34 -0800200 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800201 * Listener to be notified when some states of the bubbles change.
202 */
203 public interface BubbleStateChangeListener {
204 /**
205 * Called when the stack has bubbles or no longer has bubbles.
206 */
207 void onHasBubblesChanged(boolean hasBubbles);
208 }
209
Mady Mellorcd9b1302018-11-06 18:08:04 -0800210 /**
211 * Listener to find out about stack expansion / collapse events.
212 */
213 public interface BubbleExpandListener {
214 /**
215 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700216 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800217 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800218 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800219 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800220 void onBubbleExpandChanged(boolean isExpanding, String key);
221 }
222
223 /**
Mady Mellorf44b6832020-01-14 13:26:14 -0800224 * Listener to be notified when a bubbles' notification suppression state changes.
225 */
226 public interface NotificationSuppressionChangedListener {
227 /**
228 * Called when the notification suppression state of a bubble changes.
229 */
230 void onBubbleNotificationSuppressionChange(Bubble bubble);
Beverlyed8aea22020-01-22 16:52:47 -0500231 }
Mady Mellorf44b6832020-01-14 13:26:14 -0800232
Beverlyed8aea22020-01-22 16:52:47 -0500233 /**
234 * Callback for when the BubbleController wants to interact with the notification pipeline to:
235 * - Remove a previously bubbled notification
236 * - Update the notification shade since bubbled notification should/shouldn't be showing
237 */
238 public interface NotifCallback {
239 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500240 * Called when a bubbled notification that was hidden from the shade is now being removed
241 * This can happen when an app cancels a bubbled notification or when the user dismisses a
242 * bubble.
Beverlyed8aea22020-01-22 16:52:47 -0500243 */
Beverlya53fb0d2020-01-29 15:26:13 -0500244 void removeNotification(NotificationEntry entry, int reason);
Beverlyed8aea22020-01-22 16:52:47 -0500245
246 /**
247 * Called when a bubbled notification has changed whether it should be
248 * filtered from the shade.
249 */
Beverlya53fb0d2020-01-29 15:26:13 -0500250 void invalidateNotifications(String reason);
Beverlyed8aea22020-01-22 16:52:47 -0500251
252 /**
253 * Called on a bubbled entry that has been removed when there are no longer
254 * bubbled entries in its group.
255 *
256 * Checks whether its group has any other (non-bubbled) children. If it doesn't,
257 * removes all remnants of the group's summary from the notification pipeline.
258 * TODO: (b/145659174) Only old pipeline needs this - delete post-migration.
259 */
260 void maybeCancelSummary(NotificationEntry entry);
Mady Mellorf44b6832020-01-14 13:26:14 -0800261 }
262
263 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800264 * Listens for the current state of the status bar and updates the visibility state
265 * of bubbles as needed.
266 */
267 private class StatusBarStateListener implements StatusBarStateController.StateListener {
268 private int mState;
269 /**
270 * Returns the current status bar state.
271 */
272 public int getCurrentState() {
273 return mState;
274 }
275
276 @Override
277 public void onStateChanged(int newState) {
278 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400279 boolean shouldCollapse = (mState != SHADE);
280 if (shouldCollapse) {
281 collapseStack();
282 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700283 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800284 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800285 }
286
Mady Mellor7f234902019-10-20 12:06:29 -0700287 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800288 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700289 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800290 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700291 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700292 ConfigurationController configurationController,
Beverly Taid1e175c2020-03-10 16:37:04 +0000293 NotificationInterruptStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400294 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700295 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700296 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500297 NotificationEntryManager entryManager,
298 NotifPipeline notifPipeline,
299 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500300 DumpManager dumpManager,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400301 FloatingContentCoordinator floatingContentCoordinator,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700302 SysUiState sysUiState,
303 INotificationManager notificationManager) {
wilsonshihe8321942019-10-18 18:39:46 +0800304 this(context, notificationShadeWindowController, statusBarStateController, shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700305 data, null /* synchronizer */, configurationController, interruptionStateProvider,
Beverlya53fb0d2020-01-29 15:26:13 -0500306 zenModeController, notifUserManager, groupManager, entryManager,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700307 notifPipeline, featureFlags, dumpManager, floatingContentCoordinator, sysUiState,
308 notificationManager);
Mady Mellor7f234902019-10-20 12:06:29 -0700309 }
310
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +0100311 /**
312 * Injected constructor. See {@link BubbleModule}.
313 */
Mady Mellor7f234902019-10-20 12:06:29 -0700314 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800315 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700316 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800317 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700318 BubbleData data,
319 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
320 ConfigurationController configurationController,
Beverly Taid1e175c2020-03-10 16:37:04 +0000321 NotificationInterruptStateProvider interruptionStateProvider,
Mady Mellor7f234902019-10-20 12:06:29 -0700322 ZenModeController zenModeController,
323 NotificationLockscreenUserManager notifUserManager,
324 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500325 NotificationEntryManager entryManager,
326 NotifPipeline notifPipeline,
327 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500328 DumpManager dumpManager,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400329 FloatingContentCoordinator floatingContentCoordinator,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700330 SysUiState sysUiState,
331 INotificationManager notificationManager) {
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500332 dumpManager.registerDumpable(TAG, this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800333 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800334 mShadeController = shadeController;
Beverly Taid1e175c2020-03-10 16:37:04 +0000335 mNotificationInterruptStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400336 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400337 mZenModeController = zenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500338 mFloatingContentCoordinator = floatingContentCoordinator;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700339 mINotificationManager = notificationManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400340 mZenModeController.addCallback(new ZenModeController.Callback() {
341 @Override
342 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800343 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400344 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700345 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400346 }
347
348 @Override
349 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800350 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400351 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700352 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400353 }
354 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700355
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700356 configurationController.addCallback(this /* configurationListener */);
Joshua Tsujibe60a582020-03-23 17:17:26 -0400357 mSysUiState = sysUiState;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800358
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400359 mBubbleData = data;
360 mBubbleData.setListener(mBubbleDataListener);
Mady Mellorf44b6832020-01-14 13:26:14 -0800361 mBubbleData.setSuppressionChangedListener(new NotificationSuppressionChangedListener() {
362 @Override
363 public void onBubbleNotificationSuppressionChange(Bubble bubble) {
364 // Make sure NoMan knows it's not showing in the shade anymore so anyone querying it
365 // can tell.
366 try {
367 mBarService.onBubbleNotificationSuppressionChanged(bubble.getKey(),
368 !bubble.showInShade());
369 } catch (RemoteException e) {
370 // Bad things have happened
371 }
372 }
373 });
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400374
Mady Mellor7f234902019-10-20 12:06:29 -0700375 mNotificationEntryManager = entryManager;
Mady Mellor22f2f072019-04-18 13:26:18 -0700376 mNotificationGroupManager = groupManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500377 mNotifPipeline = notifPipeline;
378
379 if (!featureFlags.isNewNotifPipelineRenderingEnabled()) {
380 setupNEM();
381 } else {
382 setupNotifPipeline();
383 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800384
wilsonshihe8321942019-10-18 18:39:46 +0800385 mNotificationShadeWindowController = notificationShadeWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800386 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700387 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500388
Mark Renoufcecc77b2019-01-30 16:32:24 -0500389 mTaskStackListener = new BubbleTaskStackListener();
390 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800391
Joshua Tsujia19515f2019-02-13 18:02:29 -0500392 try {
393 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
394 } catch (RemoteException e) {
395 e.printStackTrace();
396 }
Issei Suzukic0387542019-03-08 17:31:14 +0100397 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700398
399 mBarService = IStatusBarService.Stub.asInterface(
400 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400401
402 mSavedBubbleKeysPerUser = new SparseSetArray<>();
403 mCurrentUserId = mNotifUserManager.getCurrentUserId();
404 mNotifUserManager.addUserChangedListener(
Steve Elliottb47f1c72019-12-19 12:39:26 -0500405 new NotificationLockscreenUserManager.UserChangedListener() {
406 @Override
407 public void onUserChanged(int newUserId) {
408 BubbleController.this.saveBubbles(mCurrentUserId);
409 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
410 BubbleController.this.restoreBubbles(newUserId);
411 mCurrentUserId = newUserId;
412 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400413 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800414
415 mUserCreatedBubbles = new HashSet<>();
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800416 mUserBlockedBubbles = new HashSet<>();
Aran Inkaa4dfa72019-11-18 16:49:07 -0500417
Mady Mellor247ca2c2019-12-02 16:18:59 -0800418 mBubbleIconFactory = new BubbleIconFactory(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800419 }
420
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400421 /**
Beverlyed8aea22020-01-22 16:52:47 -0500422 * See {@link NotifCallback}.
423 */
424 public void addNotifCallback(NotifCallback callback) {
425 mCallbacks.add(callback);
426 }
427
428 private void setupNEM() {
429 mNotificationEntryManager.addNotificationEntryListener(
430 new NotificationEntryListener() {
431 @Override
Mady Mellorf9439ab2020-01-30 16:06:53 -0800432 public void onPendingEntryAdded(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -0500433 onEntryAdded(entry);
434 }
435
436 @Override
437 public void onPreEntryUpdated(NotificationEntry entry) {
438 onEntryUpdated(entry);
439 }
440
441 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500442 public void onEntryRemoved(
443 NotificationEntry entry,
444 @android.annotation.Nullable NotificationVisibility visibility,
Julia Reynolds138111f2020-02-26 11:17:39 -0500445 boolean removedByUser,
446 int reason) {
Beverlya53fb0d2020-01-29 15:26:13 -0500447 BubbleController.this.onEntryRemoved(entry);
448 }
449
450 @Override
Beverlyed8aea22020-01-22 16:52:47 -0500451 public void onNotificationRankingUpdated(RankingMap rankingMap) {
452 onRankingUpdated(rankingMap);
453 }
454 });
455
Evan Laird04373662020-01-24 17:37:39 -0500456 mNotificationEntryManager.addNotificationRemoveInterceptor(
Beverlyed8aea22020-01-22 16:52:47 -0500457 new NotificationRemoveInterceptor() {
458 @Override
Evan Laird04373662020-01-24 17:37:39 -0500459 public boolean onNotificationRemoveRequested(
Beverlya53fb0d2020-01-29 15:26:13 -0500460 String key,
461 NotificationEntry entry,
462 int dismissReason) {
463 final boolean isClearAll = dismissReason == REASON_CANCEL_ALL;
464 final boolean isUserDimiss = dismissReason == REASON_CANCEL
465 || dismissReason == REASON_CLICK;
466 final boolean isAppCancel = dismissReason == REASON_APP_CANCEL
467 || dismissReason == REASON_APP_CANCEL_ALL;
468 final boolean isSummaryCancel =
469 dismissReason == REASON_GROUP_SUMMARY_CANCELED;
470
471 // Need to check for !appCancel here because the notification may have
472 // previously been dismissed & entry.isRowDismissed would still be true
473 boolean userRemovedNotif =
474 (entry != null && entry.isRowDismissed() && !isAppCancel)
475 || isClearAll || isUserDimiss || isSummaryCancel;
476
477 if (userRemovedNotif || isUserCreatedBubble(key)
478 || isSummaryOfUserCreatedBubble(entry)) {
479 return handleDismissalInterception(entry);
480 }
481
482 return false;
Beverlyed8aea22020-01-22 16:52:47 -0500483 }
484 });
485
486 mNotificationGroupManager.addOnGroupChangeListener(
487 new NotificationGroupManager.OnGroupChangeListener() {
488 @Override
489 public void onGroupSuppressionChanged(
490 NotificationGroupManager.NotificationGroup group,
491 boolean suppressed) {
492 // More notifications could be added causing summary to no longer
493 // be suppressed -- in this case need to remove the key.
494 final String groupKey = group.summary != null
495 ? group.summary.getSbn().getGroupKey()
496 : null;
497 if (!suppressed && groupKey != null
498 && mBubbleData.isSummarySuppressed(groupKey)) {
499 mBubbleData.removeSuppressedSummary(groupKey);
500 }
501 }
502 });
503
504 addNotifCallback(new NotifCallback() {
505 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500506 public void removeNotification(NotificationEntry entry, int reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500507 mNotificationEntryManager.performRemoveNotification(entry.getSbn(),
Beverlya53fb0d2020-01-29 15:26:13 -0500508 reason);
Beverlyed8aea22020-01-22 16:52:47 -0500509 }
510
511 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500512 public void invalidateNotifications(String reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500513 mNotificationEntryManager.updateNotifications(reason);
514 }
515
516 @Override
517 public void maybeCancelSummary(NotificationEntry entry) {
518 // Check if removed bubble has an associated suppressed group summary that needs
519 // to be removed now.
Beverlya53fb0d2020-01-29 15:26:13 -0500520 final String groupKey = entry.getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500521 if (mBubbleData.isSummarySuppressed(groupKey)) {
Beverlya53fb0d2020-01-29 15:26:13 -0500522 mBubbleData.removeSuppressedSummary(groupKey);
Beverlyed8aea22020-01-22 16:52:47 -0500523
524 final NotificationEntry summary =
525 mNotificationEntryManager.getActiveNotificationUnfiltered(
526 mBubbleData.getSummaryKey(groupKey));
Beverlya53fb0d2020-01-29 15:26:13 -0500527 if (summary != null) {
528 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
529 UNDEFINED_DISMISS_REASON);
530 }
Beverlyed8aea22020-01-22 16:52:47 -0500531 }
532
Beverlya53fb0d2020-01-29 15:26:13 -0500533 // Check if we still need to remove the summary from NoManGroup because the summary
534 // may not be in the mBubbleData.mSuppressedGroupKeys list and removed above.
535 // For example:
536 // 1. Bubbled notifications (group) is posted to shade and are visible bubbles
537 // 2. User expands bubbles so now their respective notifications in the shade are
538 // hidden, including the group summary
539 // 3. User removes all bubbles
540 // 4. We expect all the removed bubbles AND the summary (note: the summary was
541 // never added to the suppressedSummary list in BubbleData, so we add this check)
Beverlyed8aea22020-01-22 16:52:47 -0500542 NotificationEntry summary =
543 mNotificationGroupManager.getLogicalGroupSummary(entry.getSbn());
544 if (summary != null) {
545 ArrayList<NotificationEntry> summaryChildren =
546 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
547 boolean isSummaryThisNotif = summary.getKey().equals(entry.getKey());
548 if (!isSummaryThisNotif && (summaryChildren == null
549 || summaryChildren.isEmpty())) {
550 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
551 UNDEFINED_DISMISS_REASON);
552 }
553 }
554 }
555 });
556 }
557
Beverlya53fb0d2020-01-29 15:26:13 -0500558 private void setupNotifPipeline() {
559 mNotifPipeline.addCollectionListener(new NotifCollectionListener() {
560 @Override
561 public void onEntryAdded(NotificationEntry entry) {
562 BubbleController.this.onEntryAdded(entry);
563 }
564
565 @Override
566 public void onEntryUpdated(NotificationEntry entry) {
567 BubbleController.this.onEntryUpdated(entry);
568 }
569
570 @Override
571 public void onRankingUpdate(RankingMap rankingMap) {
572 onRankingUpdated(rankingMap);
573 }
574
575 @Override
576 public void onEntryRemoved(NotificationEntry entry,
577 @NotifCollection.CancellationReason int reason) {
578 BubbleController.this.onEntryRemoved(entry);
579 }
580 });
581 }
582
Beverlyed8aea22020-01-22 16:52:47 -0500583 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800584 * Sets whether to perform inflation on the same thread as the caller. This method should only
585 * be used in tests, not in production.
586 */
587 @VisibleForTesting
588 void setInflateSynchronously(boolean inflateSynchronously) {
589 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800590 }
591
Lyn Hanb58c7562020-01-07 14:29:20 -0800592 void setOverflowCallback(Runnable updateOverflow) {
593 mOverflowCallback = updateOverflow;
594 }
595
596 /**
597 * @return Bubbles for updating overflow.
598 */
599 List<Bubble> getOverflowBubbles() {
600 return mBubbleData.getOverflowBubbles();
601 }
602
603
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400604 /**
605 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
606 * method initializes the stack view and adds it to the StatusBar just above the scrim.
607 */
608 private void ensureStackViewCreated() {
609 if (mStackView == null) {
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500610 mStackView = new BubbleStackView(
Joshua Tsujibe60a582020-03-23 17:17:26 -0400611 mContext, mBubbleData, mSurfaceSynchronizer, mFloatingContentCoordinator,
612 mSysUiState);
wilsonshihe8321942019-10-18 18:39:46 +0800613 ViewGroup nsv = mNotificationShadeWindowController.getNotificationShadeView();
614 int bubbleScrimIndex = nsv.indexOfChild(nsv.findViewById(R.id.scrim_for_bubble));
Lyn Hanbde48202019-05-29 19:18:29 -0700615 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
wilsonshihe8321942019-10-18 18:39:46 +0800616 nsv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400617 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
618 if (mExpandListener != null) {
619 mStackView.setExpandListener(mExpandListener);
620 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400621 }
622 }
623
Mark Renoufc19b4732019-06-26 12:08:33 -0400624 /**
625 * Records the notification key for any active bubbles. These are used to restore active
626 * bubbles when the user returns to the foreground.
627 *
628 * @param userId the id of the user
629 */
630 private void saveBubbles(@UserIdInt int userId) {
631 // First clear any existing keys that might be stored.
632 mSavedBubbleKeysPerUser.remove(userId);
633 // Add in all active bubbles for the current user.
634 for (Bubble bubble: mBubbleData.getBubbles()) {
635 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
636 }
637 }
638
639 /**
640 * Promotes existing notifications to Bubbles if they were previously bubbles.
641 *
642 * @param userId the id of the user
643 */
644 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400645 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
646 if (savedBubbleKeys == null) {
647 // There were no bubbles saved for this used.
648 return;
649 }
Evan Laird181de622019-10-24 09:53:02 -0400650 for (NotificationEntry e :
651 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400652 if (savedBubbleKeys.contains(e.getKey())
Beverly Taid1e175c2020-03-10 16:37:04 +0000653 && mNotificationInterruptStateProvider.shouldBubbleUp(e)
Mark Renoufc19b4732019-06-26 12:08:33 -0400654 && canLaunchInActivityView(mContext, e)) {
655 updateBubble(e, /* suppressFlyout= */ true);
656 }
657 }
658 // Finally, remove the entries for this user now that bubbles are restored.
659 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
660 }
661
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700662 @Override
663 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800664 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700665 }
666
667 @Override
668 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800669 updateForThemeChanges();
670 }
671
672 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700673 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700674 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700675 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800676 mBubbleIconFactory = new BubbleIconFactory(mContext);
677 for (Bubble b: mBubbleData.getBubbles()) {
678 // Reload each bubble
679 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
680 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700681 }
682
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400683 @Override
684 public void onConfigChanged(Configuration newConfig) {
685 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400686 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700687 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400688 }
689 }
690
Mady Mellor5549dd22018-11-06 18:07:34 -0800691 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800692 * Set a listener to be notified when some states of the bubbles change.
693 */
694 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
695 mStateChangeListener = listener;
696 }
697
698 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800699 * Set a listener to be notified of bubble expand events.
700 */
701 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100702 mExpandListener = ((isExpanding, key) -> {
703 if (listener != null) {
704 listener.onBubbleExpandChanged(isExpanding, key);
705 }
wilsonshihe8321942019-10-18 18:39:46 +0800706 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100707 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800708 if (mStackView != null) {
709 mStackView.setExpandListener(mExpandListener);
710 }
711 }
712
713 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800714 * Whether or not there are bubbles present, regardless of them being visible on the
715 * screen (e.g. if on AOD).
716 */
717 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800718 if (mStackView == null) {
719 return false;
720 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400721 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800722 }
723
724 /**
725 * Whether the stack of bubbles is expanded or not.
726 */
727 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400728 return mBubbleData.isExpanded();
729 }
730
731 /**
732 * Tell the stack of bubbles to expand.
733 */
734 public void expandStack() {
735 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800736 }
737
738 /**
739 * Tell the stack of bubbles to collapse.
740 */
741 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400742 mBubbleData.setExpanded(false /* expanded */);
743 }
744
Mady Mellorce23c462019-06-17 17:30:07 -0700745 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700746 * True if either:
747 * (1) There is a bubble associated with the provided key and if its notification is hidden
748 * from the shade.
749 * (2) There is a group summary associated with the provided key that is hidden from the shade
750 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700751 *
Mady Mellore28fe102019-07-09 15:33:32 -0700752 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700753 */
Beverlyed8aea22020-01-22 16:52:47 -0500754 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
755 String key = entry.getKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700756 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800757 && !mBubbleData.getBubbleWithKey(key).showInShade();
Beverlyed8aea22020-01-22 16:52:47 -0500758
759 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700760 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700761 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Beverlyed8aea22020-01-22 16:52:47 -0500762
Mady Mellore4348272019-07-29 17:43:36 -0700763 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700764 }
765
Mark Renouf71a3af62019-04-08 15:02:54 -0400766 @VisibleForTesting
767 void selectBubble(String key) {
768 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellor247ca2c2019-12-02 16:18:59 -0800769 mBubbleData.setSelectedBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800770 }
771
Lyn Hanb58c7562020-01-07 14:29:20 -0800772 void promoteBubbleFromOverflow(Bubble bubble) {
Lyn Han1e19d7f2020-02-05 19:10:58 -0800773 bubble.setInflateSynchronously(mInflateSynchronously);
774 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
Lyn Hanb58c7562020-01-07 14:29:20 -0800775 }
776
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800777 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400778 * Request the stack expand if needed, then select the specified Bubble as current.
779 *
780 * @param notificationKey the notification key for the bubble to be selected
781 */
782 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400783 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
784 if (bubble != null) {
785 mBubbleData.setSelectedBubble(bubble);
786 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400787 }
788 }
789
790 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800791 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
792 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500793 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400794 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800795 }
796
797 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500798 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
799 * is forwarded a back key down/up pair.
800 */
801 public void performBackPressIfNeeded() {
802 if (mStackView != null) {
803 mStackView.performBackPressIfNeeded();
804 }
805 }
806
807 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800808 * Adds or updates a bubble associated with the provided notification entry.
809 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400810 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800811 */
Mark Renouff97ed462019-04-05 13:46:24 -0400812 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700813 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400814 }
815
816 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700817 updateBubble(notif, suppressFlyout, true /* showInShade */);
818 }
819
820 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800821 if (mStackView == null) {
822 // Lazy init stack view when a bubble is created
823 ensureStackViewCreated();
824 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700825 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400826 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700827 notif.setInterruption();
828 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800829 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
830 bubble.setInflateSynchronously(mInflateSynchronously);
831 bubble.inflate(
832 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
833 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700834 }
835
836 /**
837 * Called when a user has indicated that an active notification should be shown as a bubble.
838 * <p>
839 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
840 * the notification from appearing in the shade.
841 *
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700842 * @param entry the notification to change bubble state for.
843 * @param shouldBubble whether the notification should show as a bubble or not.
Mady Mellor7f234902019-10-20 12:06:29 -0700844 */
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700845 public void onUserChangedBubble(NotificationEntry entry, boolean shouldBubble) {
846 NotificationChannel channel = entry.getChannel();
847 final String appPkg = entry.getSbn().getPackageName();
848 final int appUid = entry.getSbn().getUid();
849 if (channel == null || appPkg == null) {
850 return;
Mady Mellorff076eb2019-11-13 10:12:06 -0800851 }
Mady Mellor7f234902019-10-20 12:06:29 -0700852
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700853 // Update the state in NotificationManagerService
854 try {
855 int flags = Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION;
856 mBarService.onNotificationBubbleChanged(entry.getKey(), shouldBubble, flags);
857 } catch (RemoteException e) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800858 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700859
860 // Change the settings
861 channel = NotificationChannelHelper.createConversationChannelIfNeeded(mContext,
862 mINotificationManager, entry, channel);
863 channel.setAllowBubbles(shouldBubble);
864 try {
865 int currentPref = mINotificationManager.getBubblePreferenceForPackage(appPkg, appUid);
866 if (shouldBubble && currentPref == BUBBLE_PREFERENCE_NONE) {
867 mINotificationManager.setBubblesAllowed(appPkg, appUid, BUBBLE_PREFERENCE_SELECTED);
868 }
869 mINotificationManager.updateNotificationChannelForPackage(appPkg, appUid, channel);
870 } catch (RemoteException e) {
871 Log.e(TAG, e.getMessage());
872 }
873
874 if (shouldBubble) {
875 mShadeController.collapsePanel(true);
876 if (entry.getRow() != null) {
877 entry.getRow().updateBubbleButton();
878 }
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800879 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800880 }
881
882 /**
883 * Whether this bubble was explicitly created by the user via a SysUI affordance.
884 */
885 boolean isUserCreatedBubble(String key) {
886 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800887 }
888
Beverlya53fb0d2020-01-29 15:26:13 -0500889 boolean isSummaryOfUserCreatedBubble(NotificationEntry entry) {
890 if (isSummaryOfBubbles(entry)) {
891 List<Bubble> bubbleChildren =
892 mBubbleData.getBubblesInGroup(entry.getSbn().getGroupKey());
893 for (int i = 0; i < bubbleChildren.size(); i++) {
894 // Check if any are user-created (i.e. experimental bubbles)
895 if (isUserCreatedBubble(bubbleChildren.get(i).getKey())) {
896 return true;
897 }
898 }
899 }
900 return false;
901 }
902
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800903 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500904 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500905 * <p>
906 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800907 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500908 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500909 void removeBubble(NotificationEntry entry, int reason) {
910 if (mBubbleData.hasBubbleWithKey(entry.getKey())) {
911 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800912 }
913 }
914
Beverlyed8aea22020-01-22 16:52:47 -0500915 private void onEntryAdded(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 Mellor22f2f072019-04-18 13:26:18 -0700920
Beverly Taid1e175c2020-03-10 16:37:04 +0000921 if (mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Beverlyed8aea22020-01-22 16:52:47 -0500922 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
923 if (wasAdjusted && !previouslyUserCreated) {
924 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
925 mUserCreatedBubbles.add(entry.getKey());
Mady Mellorc2ff0112019-03-28 14:18:06 -0700926 }
Beverlyed8aea22020-01-22 16:52:47 -0500927 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700928 }
929 }
930
Beverlyed8aea22020-01-22 16:52:47 -0500931 private void onEntryUpdated(NotificationEntry entry) {
932 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
933 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
934 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
935 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700936
Beverly Taid1e175c2020-03-10 16:37:04 +0000937 boolean shouldBubble = mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Beverlyed8aea22020-01-22 16:52:47 -0500938 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
939 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
940 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500941 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500942 } else if (shouldBubble) {
943 if (wasAdjusted && !previouslyUserCreated) {
944 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
945 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800946 }
Beverlyed8aea22020-01-22 16:52:47 -0500947 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800948 }
Beverlyed8aea22020-01-22 16:52:47 -0500949 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800950
Beverlya53fb0d2020-01-29 15:26:13 -0500951 private void onEntryRemoved(NotificationEntry entry) {
952 if (isSummaryOfBubbles(entry)) {
953 final String groupKey = entry.getSbn().getGroupKey();
954 mBubbleData.removeSuppressedSummary(groupKey);
955
956 // Remove any associated bubble children with the summary
957 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
958 for (int i = 0; i < bubbleChildren.size(); i++) {
959 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
960 }
961 } else {
962 removeBubble(entry, DISMISS_NOTIF_CANCEL);
963 }
964 }
965
Mady Mellor56515c42020-02-18 17:58:36 -0800966 /**
967 * Called when NotificationListener has received adjusted notification rank and reapplied
968 * filtering and sorting. This is used to dismiss or create bubbles based on changes in
969 * permissions on the notification channel or the global setting.
970 *
971 * @param rankingMap the updated ranking map from NotificationListenerService
972 */
Beverlyed8aea22020-01-22 16:52:47 -0500973 private void onRankingUpdated(RankingMap rankingMap) {
Mady Mellor56515c42020-02-18 17:58:36 -0800974 if (mTmpRanking == null) {
975 mTmpRanking = new NotificationListenerService.Ranking();
976 }
977 String[] orderedKeys = rankingMap.getOrderedKeys();
978 for (int i = 0; i < orderedKeys.length; i++) {
979 String key = orderedKeys[i];
980 NotificationEntry entry = mNotificationEntryManager.getPendingOrActiveNotif(key);
981 rankingMap.getRanking(key, mTmpRanking);
Mady Mellore45ff862020-03-24 15:54:50 -0700982 boolean isActiveBubble = mBubbleData.hasBubbleWithKey(key);
983 if (isActiveBubble && !mTmpRanking.canBubble()) {
Mady Mellor56515c42020-02-18 17:58:36 -0800984 mBubbleData.notificationEntryRemoved(entry, BubbleController.DISMISS_BLOCKED);
Mady Mellore45ff862020-03-24 15:54:50 -0700985 } else if (entry != null && mTmpRanking.isBubble() && !isActiveBubble) {
Mady Mellor56515c42020-02-18 17:58:36 -0800986 entry.setFlagBubble(true);
987 onEntryUpdated(entry);
988 }
989 }
Beverlyed8aea22020-01-22 16:52:47 -0500990 }
Ned Burns01e38212019-01-03 16:32:52 -0500991
Mark Renouf71a3af62019-04-08 15:02:54 -0400992 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400993 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400994
Mark Renouf3bc5b362019-04-05 14:37:59 -0400995 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400996 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800997 // Update bubbles in overflow.
998 if (mOverflowCallback != null) {
999 mOverflowCallback.run();
1000 }
1001
Mark Renouf82a40e62019-05-23 16:16:24 -04001002 // Collapsing? Do this first before remaining steps.
1003 if (update.expandedChanged && !update.expanded) {
1004 mStackView.setExpanded(false);
1005 }
1006
1007 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -07001008 ArrayList<Pair<Bubble, Integer>> removedBubbles =
1009 new ArrayList<>(update.removedBubbles);
1010 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -04001011 final Bubble bubble = removed.first;
1012 @DismissReason final int reason = removed.second;
1013 mStackView.removeBubble(bubble);
Mark Renoufc19b4732019-06-26 12:08:33 -04001014 // If the bubble is removed for user switching, leave the notification in place.
1015 if (reason != DISMISS_USER_CHANGED) {
1016 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -08001017 && !bubble.showInShade()) {
Beverlyed8aea22020-01-22 16:52:47 -05001018 // The bubble is now gone & the notification is hidden from the shade, so
1019 // time to actually remove it
1020 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001021 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -05001022 }
Mark Renoufc19b4732019-06-26 12:08:33 -04001023 } else {
1024 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -04001025 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor9adfe6a2020-03-30 17:23:26 -07001026 if (bubble.getEntry().getRow() != null) {
1027 bubble.getEntry().getRow().updateBubbleButton();
1028 }
Mady Mellor3a0a1b42019-05-23 06:40:21 -07001029
Mady Mellor9adfe6a2020-03-30 17:23:26 -07001030 // Update the state in NotificationManagerService
Mark Renoufc19b4732019-06-26 12:08:33 -04001031 try {
1032 mBarService.onNotificationBubbleChanged(bubble.getKey(),
Mady Mellor9adfe6a2020-03-30 17:23:26 -07001033 false /* isBubble */, 0 /* flags */);
Mark Renoufc19b4732019-06-26 12:08:33 -04001034 } catch (RemoteException e) {
Mark Renoufc19b4732019-06-26 12:08:33 -04001035 }
Mark Renouf82a40e62019-05-23 16:16:24 -04001036 }
Mady Mellor22f2f072019-04-18 13:26:18 -07001037
Ned Burns00b4b2d2019-10-17 22:09:27 -04001038 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -05001039 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
1040 // Time to potentially remove the summary
1041 for (NotifCallback cb : mCallbacks) {
1042 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -07001043 }
1044 }
Mady Mellora54e9fa2019-04-18 13:26:18 -07001045 }
1046 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001047
Lyn Hanc47e1712020-01-28 21:43:34 -08001048 if (update.addedBubble != null) {
1049 mStackView.addBubble(update.addedBubble);
1050 }
1051
Mark Renouf82a40e62019-05-23 16:16:24 -04001052 if (update.updatedBubble != null) {
1053 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -04001054 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001055
Lyn Hanb58c7562020-01-07 14:29:20 -08001056 // At this point, the correct bubbles are inflated in the stack.
1057 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -04001058 if (update.orderChanged) {
1059 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -04001060 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001061
Mark Renouf82a40e62019-05-23 16:16:24 -04001062 if (update.selectionChanged) {
1063 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -07001064 if (update.selectedBubble != null) {
1065 mNotificationGroupManager.updateSuppression(
1066 update.selectedBubble.getEntry());
1067 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001068 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001069
Mark Renouf82a40e62019-05-23 16:16:24 -04001070 // Expanding? Apply this last.
1071 if (update.expandedChanged && update.expanded) {
1072 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001073 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001074
Beverlyed8aea22020-01-22 16:52:47 -05001075 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001076 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001077 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001078 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001079
Issei Suzukia8d07312019-06-07 12:56:19 +02001080 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001081 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001082 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001083 mBubbleData.getSelectedBubble()));
1084
1085 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001086 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001087 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001088 mStackView.getExpandedBubble()));
1089 }
Lyn Hanb58c7562020-01-07 14:29:20 -08001090 Log.d(TAG, "\n[BubbleData] overflow:");
1091 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
1092 null));
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001093 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001094 }
1095 };
1096
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001097 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001098 * We intercept notification entries (including group summaries) dismissed by the user when
1099 * there is an active bubble associated with it. We do this so that developers can still
1100 * cancel it (and hence the bubbles associated with it). However, these intercepted
1101 * notifications should then be hidden from the shade since the user has cancelled them, so we
1102 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1103 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001104 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001105 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001106 */
Beverlya53fb0d2020-01-29 15:26:13 -05001107 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001108 if (entry == null) {
1109 return false;
1110 }
Beverlyed8aea22020-01-22 16:52:47 -05001111
Beverlya53fb0d2020-01-29 15:26:13 -05001112 final boolean interceptBubbleDismissal = mBubbleData.hasBubbleWithKey(entry.getKey())
1113 && entry.isBubble();
1114 final boolean interceptSummaryDismissal = isSummaryOfBubbles(entry);
Beverlyed8aea22020-01-22 16:52:47 -05001115
Beverlya53fb0d2020-01-29 15:26:13 -05001116 if (interceptSummaryDismissal) {
1117 handleSummaryDismissalInterception(entry);
1118 } else if (interceptBubbleDismissal) {
1119 Bubble bubble = mBubbleData.getBubbleWithKey(entry.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001120 bubble.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001121 bubble.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001122 } else {
Beverlyed8aea22020-01-22 16:52:47 -05001123 return false;
1124 }
Beverlya53fb0d2020-01-29 15:26:13 -05001125
1126 // Update the shade
1127 for (NotifCallback cb : mCallbacks) {
1128 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1129 }
1130 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001131 }
1132
Beverlya53fb0d2020-01-29 15:26:13 -05001133 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1134 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001135 return false;
1136 }
Beverlya53fb0d2020-01-29 15:26:13 -05001137
1138 String groupKey = entry.getSbn().getGroupKey();
1139 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1140 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1141 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1142 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1143 return (isSuppressedSummary || isSummary)
1144 && bubbleChildren != null
1145 && !bubbleChildren.isEmpty();
1146 }
1147
1148 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1149 // current children in the row:
1150 final List<NotificationEntry> children = summary.getChildren();
1151 if (children != null) {
1152 for (int i = 0; i < children.size(); i++) {
1153 NotificationEntry child = children.get(i);
1154 if (mBubbleData.hasBubbleWithKey(child.getKey())) {
1155 // Suppress the bubbled child
1156 // As far as group manager is concerned, once a child is no longer shown
1157 // in the shade, it is essentially removed.
1158 Bubble bubbleChild = mBubbleData.getBubbleWithKey(child.getKey());
1159 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1160 bubbleChild.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001161 bubbleChild.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001162 } else {
1163 // non-bubbled children can be removed
1164 for (NotifCallback cb : mCallbacks) {
1165 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1166 }
1167 }
1168 }
1169 }
1170
1171 // And since all children are removed, remove the summary.
1172 mNotificationGroupManager.onEntryRemoved(summary);
1173
1174 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1175 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1176 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001177 }
1178
1179 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001180 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001181 * Updates the visibility of the bubbles based on current state.
1182 * Does not un-bubble, just hides or un-hides. Notifies any
1183 * {@link BubbleStateChangeListener}s of visibility changes.
1184 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001185 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001186 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001187 if (mStackView == null) {
1188 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001189 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001190 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1191 // Bubbles only appear in unlocked shade
1192 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001193 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001194 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001195 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001196
Mady Mellor698d9e82019-08-01 23:11:53 +00001197 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001198 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001199 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001200 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001201 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1202 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1203 }
1204
1205 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001206 }
1207
1208 /**
1209 * Rect indicating the touchable region for the bubble stack / expanded stack.
1210 */
1211 public Rect getTouchableRegion() {
1212 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1213 return null;
1214 }
1215 mStackView.getBoundsOnScreen(mTempRect);
1216 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001217 }
1218
Mady Mellor390bff42019-04-05 15:09:01 -07001219 /**
1220 * The display id of the expanded view, if the stack is expanded and not occluded by the
1221 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1222 */
1223 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001224 if (mStackView == null) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001225 return INVALID_DISPLAY;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001226 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001227 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001228 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Lyn Han9f66c3b2020-03-05 23:59:29 -08001229 final BubbleViewProvider expandedViewProvider = mStackView.getExpandedBubble();
1230 if (defaultDisplay && expandedViewProvider != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001231 && !mNotificationShadeWindowController.getPanelExpanded()) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001232 return expandedViewProvider.getDisplayId();
Mady Mellor390bff42019-04-05 15:09:01 -07001233 }
Lyn Han9f66c3b2020-03-05 23:59:29 -08001234 return INVALID_DISPLAY;
Mady Mellor390bff42019-04-05 15:09:01 -07001235 }
1236
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001237 @VisibleForTesting
1238 BubbleStackView getStackView() {
1239 return mStackView;
1240 }
1241
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001242 /**
1243 * Description of current bubble state.
1244 */
1245 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1246 pw.println("BubbleController state:");
1247 mBubbleData.dump(fd, pw, args);
1248 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001249 if (mStackView != null) {
1250 mStackView.dump(fd, pw, args);
1251 }
1252 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001253 }
1254
Mady Mellore80930e2019-03-21 16:00:45 -07001255 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001256 * This task stack listener is responsible for responding to tasks moved to the front
1257 * which are on the default (main) display. When this happens, expanded bubbles must be
1258 * collapsed so the user may interact with the app which was just moved to the front.
1259 * <p>
1260 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1261 * these calls via a main thread Handler.
1262 */
1263 @MainThread
1264 private class BubbleTaskStackListener extends TaskStackChangeListener {
1265
Mark Renoufcecc77b2019-01-30 16:32:24 -05001266 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001267 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1268 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001269 if (!mStackView.isExpansionAnimating()) {
1270 mBubbleData.setExpanded(false);
1271 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001272 }
1273 }
1274
Mark Renoufcecc77b2019-01-30 16:32:24 -05001275 @Override
Winson Chunge789ff62020-02-24 14:40:23 -08001276 public void onActivityRestartAttempt(RunningTaskInfo task, boolean homeTaskVisible,
1277 boolean clearedTask) {
1278 for (Bubble b : mBubbleData.getBubbles()) {
1279 if (b.getDisplayId() == task.displayId) {
1280 expandStackAndSelectBubble(b.getKey());
1281 return;
1282 }
1283 }
1284 }
1285
1286 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001287 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001288 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001289 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001290 }
1291 }
Mark Renouf446251d2019-04-26 10:22:41 -04001292
1293 @Override
1294 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1295 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1296 mBubbleData.setExpanded(false);
1297 }
1298 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001299
1300 @Override
1301 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001302 if (mStackView == null) {
1303 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001304 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001305 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001306 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001307
1308 @Override
1309 public void onSingleTaskDisplayEmpty(int displayId) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001310 final BubbleViewProvider expandedBubble = mStackView != null
Mady Mellor5186b132019-09-16 17:55:48 -07001311 ? mStackView.getExpandedBubble()
1312 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001313 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1314 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001315 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001316 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001317 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001318 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001319 }
1320
Mady Mellorca0c24c2019-05-16 16:14:32 -07001321 /**
1322 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1323 *
1324 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1325 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1326 *
1327 * @param context the context to use.
1328 * @param entry the entry to bubble.
1329 */
1330 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1331 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Melloraa9ce172020-03-17 10:34:20 -07001332 ? entry.getBubbleMetadata().getIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001333 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001334 if (entry.getBubbleMetadata() != null
1335 && entry.getBubbleMetadata().getShortcutId() != null) {
1336 return true;
1337 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001338 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001339 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001340 return false;
1341 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001342 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1343 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001344 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001345 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001346 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001347 Log.w(TAG, "Unable to send as bubble, "
1348 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001349 + intent);
1350 return false;
1351 }
1352 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001353 Log.w(TAG, "Unable to send as bubble, "
1354 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001355 + intent);
1356 return false;
1357 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001358 return true;
1359 }
1360
Joshua Tsujia19515f2019-02-13 18:02:29 -05001361 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001362 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001363 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001364 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Joshua Tsujiff6b0f22020-03-09 14:55:19 -04001365 if (mStackView != null) {
Joshua Tsujid9422832019-03-05 13:32:37 -05001366 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001367 }
1368 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001369 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001370}