blob: 89c45f624b483307fbf106f7167777fa322078f9 [file] [log] [blame]
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.bubbles;
18
Mady Mellor3a0a1b42019-05-23 06:40:21 -070019import static android.app.Notification.FLAG_BUBBLE;
Mady Mellor9adfe6a2020-03-30 17:23:26 -070020import static android.app.NotificationManager.BUBBLE_PREFERENCE_NONE;
21import static android.app.NotificationManager.BUBBLE_PREFERENCE_SELECTED;
Mady Mellorc2ff0112019-03-28 14:18:06 -070022import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL;
23import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL;
24import static android.service.notification.NotificationListenerService.REASON_CANCEL;
25import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -070026import static android.service.notification.NotificationListenerService.REASON_CLICK;
27import static android.service.notification.NotificationListenerService.REASON_GROUP_SUMMARY_CANCELED;
Mady Mellor390bff42019-04-05 15:09:01 -070028import static android.view.Display.DEFAULT_DISPLAY;
29import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080030import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080031import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080032import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080033
Issei Suzukia8d07312019-06-07 12:56:19 +020034import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_CONTROLLER;
35import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
36import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080037import static com.android.systemui.statusbar.StatusBarState.SHADE;
Mady Mellor1a4e86f2019-05-03 16:07:23 -070038import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080039
Mark Renoufba5ab512019-05-02 15:21:01 -040040import static java.lang.annotation.ElementType.FIELD;
41import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
42import static java.lang.annotation.ElementType.PARAMETER;
Mark Renouf08bc42a2019-03-07 13:01:59 -050043import static java.lang.annotation.RetentionPolicy.SOURCE;
44
Mark Renoufc19b4732019-06-26 12:08:33 -040045import android.annotation.UserIdInt;
Mark Renoufc808f062019-02-07 15:20:37 -050046import android.app.ActivityManager.RunningTaskInfo;
Mady Mellor9adfe6a2020-03-30 17:23:26 -070047import android.app.INotificationManager;
48import android.app.Notification;
49import android.app.NotificationChannel;
Mady Mellor66efd5e2019-05-15 13:38:11 -070050import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070051import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080052import android.content.Context;
Mady Mellorca0c24c2019-05-16 16:14:32 -070053import android.content.pm.ActivityInfo;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080054import android.content.pm.PackageManager;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040055import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080056import android.graphics.Rect;
Lyn Han4cbc1422020-04-17 11:08:53 -070057import android.os.Handler;
Mark Renoufcecc77b2019-01-30 16:32:24 -050058import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080059import android.os.ServiceManager;
Mady Mellor56515c42020-02-18 17:58:36 -080060import android.service.notification.NotificationListenerService;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040061import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040062import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040063import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040064import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040065import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040066import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050067import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080068import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080069import android.widget.FrameLayout;
70
Mark Renouf08bc42a2019-03-07 13:01:59 -050071import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050072import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040073import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050074
Mady Mellorebdbbb92018-11-15 14:36:48 -080075import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070076import com.android.internal.statusbar.IStatusBarService;
Beverlya53fb0d2020-01-29 15:26:13 -050077import com.android.internal.statusbar.NotificationVisibility;
Beverlya53fb0d2020-01-29 15:26:13 -050078import com.android.systemui.Dumpable;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080079import com.android.systemui.R;
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +010080import com.android.systemui.bubbles.dagger.BubbleModule;
Ned Burnsaaeb44b2020-02-12 23:48:26 -050081import com.android.systemui.dump.DumpManager;
Joshua Tsujibe60a582020-03-23 17:17:26 -040082import com.android.systemui.model.SysUiState;
Beverly8fdb5332019-02-04 14:29:49 -050083import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050084import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000085import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050086import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050087import com.android.systemui.shared.system.WindowManagerWrapper;
Beverlya53fb0d2020-01-29 15:26:13 -050088import com.android.systemui.statusbar.FeatureFlags;
Mark Renoufc19b4732019-06-26 12:08:33 -040089import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070090import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Mady Mellor9adfe6a2020-03-30 17:23:26 -070091import com.android.systemui.statusbar.notification.NotificationChannelHelper;
Ned Burns01e38212019-01-03 16:32:52 -050092import com.android.systemui.statusbar.notification.NotificationEntryListener;
93import com.android.systemui.statusbar.notification.NotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -050094import com.android.systemui.statusbar.notification.collection.NotifCollection;
95import com.android.systemui.statusbar.notification.collection.NotifPipeline;
Ned Burnsf81c4c42019-01-07 14:10:43 -050096import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Beverlya53fb0d2020-01-29 15:26:13 -050097import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener;
Beverly Taid1e175c2020-03-10 16:37:04 +000098import com.android.systemui.statusbar.notification.interruption.NotificationInterruptStateProvider;
Mady Mellor22f2f072019-04-18 13:26:18 -070099import com.android.systemui.statusbar.phone.NotificationGroupManager;
wilsonshihe8321942019-10-18 18:39:46 +0800100import com.android.systemui.statusbar.phone.NotificationShadeWindowController;
Mady Mellor7f234902019-10-20 12:06:29 -0700101import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorf3b9fab2019-11-13 17:27:32 -0800102import com.android.systemui.statusbar.phone.StatusBar;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700103import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400104import com.android.systemui.statusbar.policy.ZenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500105import com.android.systemui.util.FloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800106
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700107import java.io.FileDescriptor;
108import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500109import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -0400110import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -0700111import java.util.ArrayList;
Lyn Hanb58c7562020-01-07 14:29:20 -0800112import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500113
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800114/**
115 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
116 * Bubbles can be expanded to show more content.
117 *
118 * The controller manages addition, removal, and visible state of bubbles on screen.
119 */
Beverlya53fb0d2020-01-29 15:26:13 -0500120public class BubbleController implements ConfigurationController.ConfigurationListener, Dumpable {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800121
Issei Suzukia8d07312019-06-07 12:56:19 +0200122 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800123
Mark Renouf08bc42a2019-03-07 13:01:59 -0500124 @Retention(SOURCE)
125 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400126 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Lyn Han2f6e89d2020-04-15 10:01:01 -0700127 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT,
128 DISMISS_OVERFLOW_MAX_REACHED})
Mark Renoufba5ab512019-05-02 15:21:01 -0400129 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500130 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700131
Mark Renouf08bc42a2019-03-07 13:01:59 -0500132 static final int DISMISS_USER_GESTURE = 1;
133 static final int DISMISS_AGED = 2;
134 static final int DISMISS_TASK_FINISHED = 3;
135 static final int DISMISS_BLOCKED = 4;
136 static final int DISMISS_NOTIF_CANCEL = 5;
137 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700138 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400139 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700140 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700141 static final int DISMISS_INVALID_INTENT = 10;
Lyn Han2f6e89d2020-04-15 10:01:01 -0700142 static final int DISMISS_OVERFLOW_MAX_REACHED = 11;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500143
Ned Burns01e38212019-01-03 16:32:52 -0500144 private final Context mContext;
145 private final NotificationEntryManager mNotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500146 private final NotifPipeline mNotifPipeline;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500147 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800148 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800149 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100150 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700151 private final NotificationGroupManager mNotificationGroupManager;
Heemin Seogba6337f2019-12-10 15:34:37 -0800152 private final ShadeController mShadeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500153 private final FloatingContentCoordinator mFloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800154
Mady Mellor3dff9e62019-02-05 18:12:53 -0800155 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400156 @Nullable private BubbleStackView mStackView;
Mady Mellor247ca2c2019-12-02 16:18:59 -0800157 private BubbleIconFactory mBubbleIconFactory;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800158
Mark Renoufc19b4732019-06-26 12:08:33 -0400159 // Tracks the id of the current (foreground) user.
160 private int mCurrentUserId;
161 // Saves notification keys of active bubbles when users are switched.
162 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
163
Mady Mellor56515c42020-02-18 17:58:36 -0800164 // Used when ranking updates occur and we check if things should bubble / unbubble
165 private NotificationListenerService.Ranking mTmpRanking;
166
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800167 // Bubbles get added to the status bar view
wilsonshihe8321942019-10-18 18:39:46 +0800168 private final NotificationShadeWindowController mNotificationShadeWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400169 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800170 private StatusBarStateListener mStatusBarStateListener;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700171 private INotificationManager mINotificationManager;
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
Lyn Han4cbc1422020-04-17 11:08:53 -0700180 // Used to post to main UI thread
181 private Handler mHandler = new Handler();
182
Mady Mellord1c78b262018-11-06 18:04:40 -0800183 // Used for determining view rect for touch interaction
184 private Rect mTempRect = new Rect();
185
Mark Renoufc19b4732019-06-26 12:08:33 -0400186 // Listens to user switch so bubbles can be saved and restored.
187 private final NotificationLockscreenUserManager mNotifUserManager;
188
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400189 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
190 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
191
Mady Mellor3df7ab02019-12-09 15:07:10 -0800192 private boolean mInflateSynchronously;
193
Beverlyed8aea22020-01-22 16:52:47 -0500194 // TODO (b/145659174): allow for multiple callbacks to support the "shadow" new notif pipeline
195 private final List<NotifCallback> mCallbacks = new ArrayList<>();
196
Mady Mellor5549dd22018-11-06 18:07:34 -0800197 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800198 * Listener to be notified when some states of the bubbles change.
199 */
200 public interface BubbleStateChangeListener {
201 /**
202 * Called when the stack has bubbles or no longer has bubbles.
203 */
204 void onHasBubblesChanged(boolean hasBubbles);
205 }
206
Mady Mellorcd9b1302018-11-06 18:08:04 -0800207 /**
208 * Listener to find out about stack expansion / collapse events.
209 */
210 public interface BubbleExpandListener {
211 /**
212 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700213 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800214 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800215 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800216 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800217 void onBubbleExpandChanged(boolean isExpanding, String key);
218 }
219
220 /**
Mady Mellorf44b6832020-01-14 13:26:14 -0800221 * Listener to be notified when a bubbles' notification suppression state changes.
222 */
223 public interface NotificationSuppressionChangedListener {
224 /**
225 * Called when the notification suppression state of a bubble changes.
226 */
227 void onBubbleNotificationSuppressionChange(Bubble bubble);
Beverlyed8aea22020-01-22 16:52:47 -0500228 }
Mady Mellorf44b6832020-01-14 13:26:14 -0800229
Beverlyed8aea22020-01-22 16:52:47 -0500230 /**
231 * Callback for when the BubbleController wants to interact with the notification pipeline to:
232 * - Remove a previously bubbled notification
233 * - Update the notification shade since bubbled notification should/shouldn't be showing
234 */
235 public interface NotifCallback {
236 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500237 * Called when a bubbled notification that was hidden from the shade is now being removed
238 * This can happen when an app cancels a bubbled notification or when the user dismisses a
239 * bubble.
Beverlyed8aea22020-01-22 16:52:47 -0500240 */
Beverlya53fb0d2020-01-29 15:26:13 -0500241 void removeNotification(NotificationEntry entry, int reason);
Beverlyed8aea22020-01-22 16:52:47 -0500242
243 /**
244 * Called when a bubbled notification has changed whether it should be
245 * filtered from the shade.
246 */
Beverlya53fb0d2020-01-29 15:26:13 -0500247 void invalidateNotifications(String reason);
Beverlyed8aea22020-01-22 16:52:47 -0500248
249 /**
250 * Called on a bubbled entry that has been removed when there are no longer
251 * bubbled entries in its group.
252 *
253 * Checks whether its group has any other (non-bubbled) children. If it doesn't,
254 * removes all remnants of the group's summary from the notification pipeline.
255 * TODO: (b/145659174) Only old pipeline needs this - delete post-migration.
256 */
257 void maybeCancelSummary(NotificationEntry entry);
Mady Mellorf44b6832020-01-14 13:26:14 -0800258 }
259
260 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800261 * Listens for the current state of the status bar and updates the visibility state
262 * of bubbles as needed.
263 */
264 private class StatusBarStateListener implements StatusBarStateController.StateListener {
265 private int mState;
266 /**
267 * Returns the current status bar state.
268 */
269 public int getCurrentState() {
270 return mState;
271 }
272
273 @Override
274 public void onStateChanged(int newState) {
275 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400276 boolean shouldCollapse = (mState != SHADE);
277 if (shouldCollapse) {
278 collapseStack();
279 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700280 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800281 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800282 }
283
Mady Mellor7f234902019-10-20 12:06:29 -0700284 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800285 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700286 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800287 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700288 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700289 ConfigurationController configurationController,
Beverly Taid1e175c2020-03-10 16:37:04 +0000290 NotificationInterruptStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400291 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700292 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700293 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500294 NotificationEntryManager entryManager,
295 NotifPipeline notifPipeline,
296 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500297 DumpManager dumpManager,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400298 FloatingContentCoordinator floatingContentCoordinator,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700299 SysUiState sysUiState,
300 INotificationManager notificationManager) {
wilsonshihe8321942019-10-18 18:39:46 +0800301 this(context, notificationShadeWindowController, statusBarStateController, shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700302 data, null /* synchronizer */, configurationController, interruptionStateProvider,
Beverlya53fb0d2020-01-29 15:26:13 -0500303 zenModeController, notifUserManager, groupManager, entryManager,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700304 notifPipeline, featureFlags, dumpManager, floatingContentCoordinator, sysUiState,
305 notificationManager);
Mady Mellor7f234902019-10-20 12:06:29 -0700306 }
307
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +0100308 /**
309 * Injected constructor. See {@link BubbleModule}.
310 */
Mady Mellor7f234902019-10-20 12:06:29 -0700311 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800312 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700313 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800314 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700315 BubbleData data,
316 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
317 ConfigurationController configurationController,
Beverly Taid1e175c2020-03-10 16:37:04 +0000318 NotificationInterruptStateProvider interruptionStateProvider,
Mady Mellor7f234902019-10-20 12:06:29 -0700319 ZenModeController zenModeController,
320 NotificationLockscreenUserManager notifUserManager,
321 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500322 NotificationEntryManager entryManager,
323 NotifPipeline notifPipeline,
324 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500325 DumpManager dumpManager,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400326 FloatingContentCoordinator floatingContentCoordinator,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700327 SysUiState sysUiState,
328 INotificationManager notificationManager) {
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500329 dumpManager.registerDumpable(TAG, this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800330 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800331 mShadeController = shadeController;
Beverly Taid1e175c2020-03-10 16:37:04 +0000332 mNotificationInterruptStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400333 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400334 mZenModeController = zenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500335 mFloatingContentCoordinator = floatingContentCoordinator;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700336 mINotificationManager = notificationManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400337 mZenModeController.addCallback(new ZenModeController.Callback() {
338 @Override
339 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800340 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400341 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700342 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400343 }
344
345 @Override
346 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800347 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400348 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700349 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400350 }
351 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700352
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700353 configurationController.addCallback(this /* configurationListener */);
Joshua Tsujibe60a582020-03-23 17:17:26 -0400354 mSysUiState = sysUiState;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800355
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400356 mBubbleData = data;
357 mBubbleData.setListener(mBubbleDataListener);
Mady Mellorf44b6832020-01-14 13:26:14 -0800358 mBubbleData.setSuppressionChangedListener(new NotificationSuppressionChangedListener() {
359 @Override
360 public void onBubbleNotificationSuppressionChange(Bubble bubble) {
361 // Make sure NoMan knows it's not showing in the shade anymore so anyone querying it
362 // can tell.
363 try {
364 mBarService.onBubbleNotificationSuppressionChanged(bubble.getKey(),
365 !bubble.showInShade());
366 } catch (RemoteException e) {
367 // Bad things have happened
368 }
369 }
370 });
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400371
Mady Mellor7f234902019-10-20 12:06:29 -0700372 mNotificationEntryManager = entryManager;
Mady Mellor22f2f072019-04-18 13:26:18 -0700373 mNotificationGroupManager = groupManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500374 mNotifPipeline = notifPipeline;
375
376 if (!featureFlags.isNewNotifPipelineRenderingEnabled()) {
377 setupNEM();
378 } else {
379 setupNotifPipeline();
380 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800381
wilsonshihe8321942019-10-18 18:39:46 +0800382 mNotificationShadeWindowController = notificationShadeWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800383 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700384 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500385
Mark Renoufcecc77b2019-01-30 16:32:24 -0500386 mTaskStackListener = new BubbleTaskStackListener();
387 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800388
Joshua Tsujia19515f2019-02-13 18:02:29 -0500389 try {
390 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
391 } catch (RemoteException e) {
392 e.printStackTrace();
393 }
Issei Suzukic0387542019-03-08 17:31:14 +0100394 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700395
396 mBarService = IStatusBarService.Stub.asInterface(
397 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400398
399 mSavedBubbleKeysPerUser = new SparseSetArray<>();
400 mCurrentUserId = mNotifUserManager.getCurrentUserId();
401 mNotifUserManager.addUserChangedListener(
Steve Elliottb47f1c72019-12-19 12:39:26 -0500402 new NotificationLockscreenUserManager.UserChangedListener() {
403 @Override
404 public void onUserChanged(int newUserId) {
405 BubbleController.this.saveBubbles(mCurrentUserId);
406 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
407 BubbleController.this.restoreBubbles(newUserId);
408 mCurrentUserId = newUserId;
409 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400410 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800411
Mady Mellor247ca2c2019-12-02 16:18:59 -0800412 mBubbleIconFactory = new BubbleIconFactory(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800413 }
414
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400415 /**
Beverlyed8aea22020-01-22 16:52:47 -0500416 * See {@link NotifCallback}.
417 */
418 public void addNotifCallback(NotifCallback callback) {
419 mCallbacks.add(callback);
420 }
421
422 private void setupNEM() {
423 mNotificationEntryManager.addNotificationEntryListener(
424 new NotificationEntryListener() {
425 @Override
Mady Mellorf9439ab2020-01-30 16:06:53 -0800426 public void onPendingEntryAdded(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -0500427 onEntryAdded(entry);
428 }
429
430 @Override
431 public void onPreEntryUpdated(NotificationEntry entry) {
432 onEntryUpdated(entry);
433 }
434
435 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500436 public void onEntryRemoved(
437 NotificationEntry entry,
438 @android.annotation.Nullable NotificationVisibility visibility,
Julia Reynolds138111f2020-02-26 11:17:39 -0500439 boolean removedByUser,
440 int reason) {
Beverlya53fb0d2020-01-29 15:26:13 -0500441 BubbleController.this.onEntryRemoved(entry);
442 }
443
444 @Override
Beverlyed8aea22020-01-22 16:52:47 -0500445 public void onNotificationRankingUpdated(RankingMap rankingMap) {
446 onRankingUpdated(rankingMap);
447 }
448 });
449
Evan Laird04373662020-01-24 17:37:39 -0500450 mNotificationEntryManager.addNotificationRemoveInterceptor(
Beverlyed8aea22020-01-22 16:52:47 -0500451 new NotificationRemoveInterceptor() {
452 @Override
Evan Laird04373662020-01-24 17:37:39 -0500453 public boolean onNotificationRemoveRequested(
Beverlya53fb0d2020-01-29 15:26:13 -0500454 String key,
455 NotificationEntry entry,
456 int dismissReason) {
457 final boolean isClearAll = dismissReason == REASON_CANCEL_ALL;
458 final boolean isUserDimiss = dismissReason == REASON_CANCEL
459 || dismissReason == REASON_CLICK;
460 final boolean isAppCancel = dismissReason == REASON_APP_CANCEL
461 || dismissReason == REASON_APP_CANCEL_ALL;
462 final boolean isSummaryCancel =
463 dismissReason == REASON_GROUP_SUMMARY_CANCELED;
464
465 // Need to check for !appCancel here because the notification may have
466 // previously been dismissed & entry.isRowDismissed would still be true
467 boolean userRemovedNotif =
468 (entry != null && entry.isRowDismissed() && !isAppCancel)
469 || isClearAll || isUserDimiss || isSummaryCancel;
470
Mady Mellordd6fe612020-04-15 11:47:55 -0700471 if (userRemovedNotif) {
Beverlya53fb0d2020-01-29 15:26:13 -0500472 return handleDismissalInterception(entry);
473 }
Beverlya53fb0d2020-01-29 15:26:13 -0500474 return false;
Beverlyed8aea22020-01-22 16:52:47 -0500475 }
476 });
477
478 mNotificationGroupManager.addOnGroupChangeListener(
479 new NotificationGroupManager.OnGroupChangeListener() {
480 @Override
481 public void onGroupSuppressionChanged(
482 NotificationGroupManager.NotificationGroup group,
483 boolean suppressed) {
484 // More notifications could be added causing summary to no longer
485 // be suppressed -- in this case need to remove the key.
486 final String groupKey = group.summary != null
487 ? group.summary.getSbn().getGroupKey()
488 : null;
489 if (!suppressed && groupKey != null
490 && mBubbleData.isSummarySuppressed(groupKey)) {
491 mBubbleData.removeSuppressedSummary(groupKey);
492 }
493 }
494 });
495
496 addNotifCallback(new NotifCallback() {
497 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500498 public void removeNotification(NotificationEntry entry, int reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500499 mNotificationEntryManager.performRemoveNotification(entry.getSbn(),
Beverlya53fb0d2020-01-29 15:26:13 -0500500 reason);
Beverlyed8aea22020-01-22 16:52:47 -0500501 }
502
503 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500504 public void invalidateNotifications(String reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500505 mNotificationEntryManager.updateNotifications(reason);
506 }
507
508 @Override
509 public void maybeCancelSummary(NotificationEntry entry) {
510 // Check if removed bubble has an associated suppressed group summary that needs
511 // to be removed now.
Beverlya53fb0d2020-01-29 15:26:13 -0500512 final String groupKey = entry.getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500513 if (mBubbleData.isSummarySuppressed(groupKey)) {
Beverlya53fb0d2020-01-29 15:26:13 -0500514 mBubbleData.removeSuppressedSummary(groupKey);
Beverlyed8aea22020-01-22 16:52:47 -0500515
516 final NotificationEntry summary =
517 mNotificationEntryManager.getActiveNotificationUnfiltered(
518 mBubbleData.getSummaryKey(groupKey));
Beverlya53fb0d2020-01-29 15:26:13 -0500519 if (summary != null) {
520 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
521 UNDEFINED_DISMISS_REASON);
522 }
Beverlyed8aea22020-01-22 16:52:47 -0500523 }
524
Beverlya53fb0d2020-01-29 15:26:13 -0500525 // Check if we still need to remove the summary from NoManGroup because the summary
526 // may not be in the mBubbleData.mSuppressedGroupKeys list and removed above.
527 // For example:
528 // 1. Bubbled notifications (group) is posted to shade and are visible bubbles
529 // 2. User expands bubbles so now their respective notifications in the shade are
530 // hidden, including the group summary
531 // 3. User removes all bubbles
532 // 4. We expect all the removed bubbles AND the summary (note: the summary was
533 // never added to the suppressedSummary list in BubbleData, so we add this check)
Beverlyed8aea22020-01-22 16:52:47 -0500534 NotificationEntry summary =
535 mNotificationGroupManager.getLogicalGroupSummary(entry.getSbn());
536 if (summary != null) {
537 ArrayList<NotificationEntry> summaryChildren =
538 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
539 boolean isSummaryThisNotif = summary.getKey().equals(entry.getKey());
540 if (!isSummaryThisNotif && (summaryChildren == null
541 || summaryChildren.isEmpty())) {
542 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
543 UNDEFINED_DISMISS_REASON);
544 }
545 }
546 }
547 });
548 }
549
Beverlya53fb0d2020-01-29 15:26:13 -0500550 private void setupNotifPipeline() {
551 mNotifPipeline.addCollectionListener(new NotifCollectionListener() {
552 @Override
553 public void onEntryAdded(NotificationEntry entry) {
554 BubbleController.this.onEntryAdded(entry);
555 }
556
557 @Override
558 public void onEntryUpdated(NotificationEntry entry) {
559 BubbleController.this.onEntryUpdated(entry);
560 }
561
562 @Override
563 public void onRankingUpdate(RankingMap rankingMap) {
564 onRankingUpdated(rankingMap);
565 }
566
567 @Override
568 public void onEntryRemoved(NotificationEntry entry,
569 @NotifCollection.CancellationReason int reason) {
570 BubbleController.this.onEntryRemoved(entry);
571 }
572 });
573 }
574
Beverlyed8aea22020-01-22 16:52:47 -0500575 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800576 * Sets whether to perform inflation on the same thread as the caller. This method should only
577 * be used in tests, not in production.
578 */
579 @VisibleForTesting
580 void setInflateSynchronously(boolean inflateSynchronously) {
581 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800582 }
583
Lyn Hanb58c7562020-01-07 14:29:20 -0800584 void setOverflowCallback(Runnable updateOverflow) {
585 mOverflowCallback = updateOverflow;
586 }
587
588 /**
589 * @return Bubbles for updating overflow.
590 */
591 List<Bubble> getOverflowBubbles() {
592 return mBubbleData.getOverflowBubbles();
593 }
594
595
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400596 /**
597 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
598 * method initializes the stack view and adds it to the StatusBar just above the scrim.
599 */
600 private void ensureStackViewCreated() {
601 if (mStackView == null) {
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500602 mStackView = new BubbleStackView(
Joshua Tsujibe60a582020-03-23 17:17:26 -0400603 mContext, mBubbleData, mSurfaceSynchronizer, mFloatingContentCoordinator,
Joshua Tsujiba9fef02020-04-09 17:40:35 -0400604 mSysUiState, mNotificationShadeWindowController);
wilsonshihe8321942019-10-18 18:39:46 +0800605 ViewGroup nsv = mNotificationShadeWindowController.getNotificationShadeView();
606 int bubbleScrimIndex = nsv.indexOfChild(nsv.findViewById(R.id.scrim_for_bubble));
Lyn Hanbde48202019-05-29 19:18:29 -0700607 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
wilsonshihe8321942019-10-18 18:39:46 +0800608 nsv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400609 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
610 if (mExpandListener != null) {
611 mStackView.setExpandListener(mExpandListener);
612 }
Joshua Tsuji6855cab2020-04-16 01:05:39 -0400613
614 mStackView.setUnbubbleConversationCallback(notificationEntry ->
615 onUserChangedBubble(notificationEntry, false /* shouldBubble */));
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400616 }
617 }
618
Mark Renoufc19b4732019-06-26 12:08:33 -0400619 /**
620 * Records the notification key for any active bubbles. These are used to restore active
621 * bubbles when the user returns to the foreground.
622 *
623 * @param userId the id of the user
624 */
625 private void saveBubbles(@UserIdInt int userId) {
626 // First clear any existing keys that might be stored.
627 mSavedBubbleKeysPerUser.remove(userId);
628 // Add in all active bubbles for the current user.
629 for (Bubble bubble: mBubbleData.getBubbles()) {
630 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
631 }
632 }
633
634 /**
635 * Promotes existing notifications to Bubbles if they were previously bubbles.
636 *
637 * @param userId the id of the user
638 */
639 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400640 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
641 if (savedBubbleKeys == null) {
642 // There were no bubbles saved for this used.
643 return;
644 }
Evan Laird181de622019-10-24 09:53:02 -0400645 for (NotificationEntry e :
646 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400647 if (savedBubbleKeys.contains(e.getKey())
Beverly Taid1e175c2020-03-10 16:37:04 +0000648 && mNotificationInterruptStateProvider.shouldBubbleUp(e)
Mark Renoufc19b4732019-06-26 12:08:33 -0400649 && canLaunchInActivityView(mContext, e)) {
650 updateBubble(e, /* suppressFlyout= */ true);
651 }
652 }
653 // Finally, remove the entries for this user now that bubbles are restored.
654 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
655 }
656
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700657 @Override
658 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800659 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700660 }
661
662 @Override
663 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800664 updateForThemeChanges();
665 }
666
667 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700668 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700669 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700670 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800671 mBubbleIconFactory = new BubbleIconFactory(mContext);
672 for (Bubble b: mBubbleData.getBubbles()) {
673 // Reload each bubble
674 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
675 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700676 }
677
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400678 @Override
679 public void onConfigChanged(Configuration newConfig) {
680 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400681 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700682 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400683 }
684 }
685
Mady Mellor5549dd22018-11-06 18:07:34 -0800686 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800687 * Set a listener to be notified when some states of the bubbles change.
688 */
689 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
690 mStateChangeListener = listener;
691 }
692
693 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800694 * Set a listener to be notified of bubble expand events.
695 */
696 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100697 mExpandListener = ((isExpanding, key) -> {
698 if (listener != null) {
699 listener.onBubbleExpandChanged(isExpanding, key);
700 }
wilsonshihe8321942019-10-18 18:39:46 +0800701 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100702 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800703 if (mStackView != null) {
704 mStackView.setExpandListener(mExpandListener);
705 }
706 }
707
708 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800709 * Whether or not there are bubbles present, regardless of them being visible on the
710 * screen (e.g. if on AOD).
711 */
712 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800713 if (mStackView == null) {
714 return false;
715 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400716 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800717 }
718
719 /**
720 * Whether the stack of bubbles is expanded or not.
721 */
722 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400723 return mBubbleData.isExpanded();
724 }
725
726 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800727 * Tell the stack of bubbles to collapse.
728 */
729 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400730 mBubbleData.setExpanded(false /* expanded */);
731 }
732
Mady Mellorce23c462019-06-17 17:30:07 -0700733 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700734 * True if either:
735 * (1) There is a bubble associated with the provided key and if its notification is hidden
736 * from the shade.
737 * (2) There is a group summary associated with the provided key that is hidden from the shade
738 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700739 *
Mady Mellore28fe102019-07-09 15:33:32 -0700740 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700741 */
Beverlyed8aea22020-01-22 16:52:47 -0500742 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
743 String key = entry.getKey();
Lyn Han2f6e89d2020-04-15 10:01:01 -0700744 boolean isSuppressedBubble = (mBubbleData.hasAnyBubbleWithKey(key)
745 && !mBubbleData.getAnyBubbleWithkey(key).showInShade());
Beverlyed8aea22020-01-22 16:52:47 -0500746
747 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700748 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700749 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Lyn Han2f6e89d2020-04-15 10:01:01 -0700750 return (isSummary && isSuppressedSummary) || isSuppressedBubble;
Mady Mellorce23c462019-06-17 17:30:07 -0700751 }
752
Lyn Hanb58c7562020-01-07 14:29:20 -0800753 void promoteBubbleFromOverflow(Bubble bubble) {
Lyn Han1e19d7f2020-02-05 19:10:58 -0800754 bubble.setInflateSynchronously(mInflateSynchronously);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700755 setIsBubble(bubble, /* isBubble */ true);
Lyn Han1e19d7f2020-02-05 19:10:58 -0800756 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
Lyn Hanb58c7562020-01-07 14:29:20 -0800757 }
758
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800759 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400760 * Request the stack expand if needed, then select the specified Bubble as current.
761 *
762 * @param notificationKey the notification key for the bubble to be selected
763 */
764 public void expandStackAndSelectBubble(String notificationKey) {
Lyn Han2f6e89d2020-04-15 10:01:01 -0700765 Bubble bubble = mBubbleData.getBubbleInStackWithKey(notificationKey);
766 if (bubble == null) {
767 bubble = mBubbleData.getOverflowBubbleWithKey(notificationKey);
768 if (bubble != null) {
769 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
770 }
771 } else if (bubble.getEntry().isBubble()){
Mark Renouf71a3af62019-04-08 15:02:54 -0400772 mBubbleData.setSelectedBubble(bubble);
Mark Renouffec45da2019-03-13 13:24:27 -0400773 }
Lyn Han2f6e89d2020-04-15 10:01:01 -0700774 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400775 }
776
777 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500778 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
779 * is forwarded a back key down/up pair.
780 */
781 public void performBackPressIfNeeded() {
782 if (mStackView != null) {
783 mStackView.performBackPressIfNeeded();
784 }
785 }
786
787 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800788 * Adds or updates a bubble associated with the provided notification entry.
789 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400790 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800791 */
Mark Renouff97ed462019-04-05 13:46:24 -0400792 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700793 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400794 }
795
796 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700797 updateBubble(notif, suppressFlyout, true /* showInShade */);
798 }
799
800 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800801 if (mStackView == null) {
802 // Lazy init stack view when a bubble is created
803 ensureStackViewCreated();
804 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700805 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400806 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700807 notif.setInterruption();
808 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800809 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
810 bubble.setInflateSynchronously(mInflateSynchronously);
811 bubble.inflate(
Lyn Han4cbc1422020-04-17 11:08:53 -0700812 b -> {
813 mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade);
814 if (bubble.getBubbleIntent() == null) {
815 return;
816 }
817 bubble.getBubbleIntent().registerCancelListener(pendingIntent -> {
818 mHandler.post(
819 () -> removeBubble(bubble.getEntry(),
820 BubbleController.DISMISS_INVALID_INTENT));
821 });
822 },
Mady Mellor3df7ab02019-12-09 15:07:10 -0800823 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 *
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700832 * @param entry the notification to change bubble state for.
833 * @param shouldBubble whether the notification should show as a bubble or not.
Mady Mellor7f234902019-10-20 12:06:29 -0700834 */
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700835 public void onUserChangedBubble(NotificationEntry entry, boolean shouldBubble) {
836 NotificationChannel channel = entry.getChannel();
837 final String appPkg = entry.getSbn().getPackageName();
838 final int appUid = entry.getSbn().getUid();
839 if (channel == null || appPkg == null) {
840 return;
Mady Mellorff076eb2019-11-13 10:12:06 -0800841 }
Mady Mellor7f234902019-10-20 12:06:29 -0700842
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700843 // Update the state in NotificationManagerService
844 try {
845 int flags = Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION;
846 mBarService.onNotificationBubbleChanged(entry.getKey(), shouldBubble, flags);
847 } catch (RemoteException e) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800848 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700849
850 // Change the settings
851 channel = NotificationChannelHelper.createConversationChannelIfNeeded(mContext,
852 mINotificationManager, entry, channel);
853 channel.setAllowBubbles(shouldBubble);
854 try {
855 int currentPref = mINotificationManager.getBubblePreferenceForPackage(appPkg, appUid);
856 if (shouldBubble && currentPref == BUBBLE_PREFERENCE_NONE) {
857 mINotificationManager.setBubblesAllowed(appPkg, appUid, BUBBLE_PREFERENCE_SELECTED);
858 }
859 mINotificationManager.updateNotificationChannelForPackage(appPkg, appUid, channel);
860 } catch (RemoteException e) {
861 Log.e(TAG, e.getMessage());
862 }
863
864 if (shouldBubble) {
865 mShadeController.collapsePanel(true);
866 if (entry.getRow() != null) {
867 entry.getRow().updateBubbleButton();
868 }
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800869 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800870 }
871
872 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500873 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500874 * <p>
875 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800876 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500877 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500878 void removeBubble(NotificationEntry entry, int reason) {
Lyn Han2f6e89d2020-04-15 10:01:01 -0700879 if (mBubbleData.hasAnyBubbleWithKey(entry.getKey())) {
Beverlya53fb0d2020-01-29 15:26:13 -0500880 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800881 }
882 }
883
Beverlyed8aea22020-01-22 16:52:47 -0500884 private void onEntryAdded(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000885 if (mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700886 && canLaunchInActivityView(mContext, entry)) {
Beverlyed8aea22020-01-22 16:52:47 -0500887 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700888 }
889 }
890
Beverlyed8aea22020-01-22 16:52:47 -0500891 private void onEntryUpdated(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000892 boolean shouldBubble = mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700893 && canLaunchInActivityView(mContext, entry);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700894 if (!shouldBubble && mBubbleData.hasAnyBubbleWithKey(entry.getKey())) {
Beverlyed8aea22020-01-22 16:52:47 -0500895 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500896 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500897 } else if (shouldBubble) {
Beverlyed8aea22020-01-22 16:52:47 -0500898 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800899 }
Beverlyed8aea22020-01-22 16:52:47 -0500900 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800901
Beverlya53fb0d2020-01-29 15:26:13 -0500902 private void onEntryRemoved(NotificationEntry entry) {
903 if (isSummaryOfBubbles(entry)) {
904 final String groupKey = entry.getSbn().getGroupKey();
905 mBubbleData.removeSuppressedSummary(groupKey);
906
907 // Remove any associated bubble children with the summary
908 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
909 for (int i = 0; i < bubbleChildren.size(); i++) {
910 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
911 }
912 } else {
913 removeBubble(entry, DISMISS_NOTIF_CANCEL);
914 }
915 }
916
Mady Mellor56515c42020-02-18 17:58:36 -0800917 /**
918 * Called when NotificationListener has received adjusted notification rank and reapplied
919 * filtering and sorting. This is used to dismiss or create bubbles based on changes in
920 * permissions on the notification channel or the global setting.
921 *
922 * @param rankingMap the updated ranking map from NotificationListenerService
923 */
Beverlyed8aea22020-01-22 16:52:47 -0500924 private void onRankingUpdated(RankingMap rankingMap) {
Mady Mellor56515c42020-02-18 17:58:36 -0800925 if (mTmpRanking == null) {
926 mTmpRanking = new NotificationListenerService.Ranking();
927 }
928 String[] orderedKeys = rankingMap.getOrderedKeys();
929 for (int i = 0; i < orderedKeys.length; i++) {
930 String key = orderedKeys[i];
931 NotificationEntry entry = mNotificationEntryManager.getPendingOrActiveNotif(key);
932 rankingMap.getRanking(key, mTmpRanking);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700933 boolean isActiveBubble = mBubbleData.hasAnyBubbleWithKey(key);
Mady Mellore45ff862020-03-24 15:54:50 -0700934 if (isActiveBubble && !mTmpRanking.canBubble()) {
Mady Mellor56515c42020-02-18 17:58:36 -0800935 mBubbleData.notificationEntryRemoved(entry, BubbleController.DISMISS_BLOCKED);
Mady Mellore45ff862020-03-24 15:54:50 -0700936 } else if (entry != null && mTmpRanking.isBubble() && !isActiveBubble) {
Mady Mellor56515c42020-02-18 17:58:36 -0800937 entry.setFlagBubble(true);
938 onEntryUpdated(entry);
939 }
940 }
Beverlyed8aea22020-01-22 16:52:47 -0500941 }
Ned Burns01e38212019-01-03 16:32:52 -0500942
Lyn Han2f6e89d2020-04-15 10:01:01 -0700943 private void setIsBubble(Bubble b, boolean isBubble) {
944 if (isBubble) {
945 b.getEntry().getSbn().getNotification().flags |= FLAG_BUBBLE;
946 } else {
947 b.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
948 }
949 try {
950 mBarService.onNotificationBubbleChanged(b.getKey(), isBubble, 0);
951 } catch (RemoteException e) {
952 // Bad things have happened
953 }
954 }
955
Mark Renouf71a3af62019-04-08 15:02:54 -0400956 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400957 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400958
Mark Renouf3bc5b362019-04-05 14:37:59 -0400959 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400960 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800961 // Update bubbles in overflow.
962 if (mOverflowCallback != null) {
963 mOverflowCallback.run();
964 }
965
Mark Renouf82a40e62019-05-23 16:16:24 -0400966 // Collapsing? Do this first before remaining steps.
967 if (update.expandedChanged && !update.expanded) {
968 mStackView.setExpanded(false);
969 }
970
971 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700972 ArrayList<Pair<Bubble, Integer>> removedBubbles =
973 new ArrayList<>(update.removedBubbles);
974 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400975 final Bubble bubble = removed.first;
976 @DismissReason final int reason = removed.second;
977 mStackView.removeBubble(bubble);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700978
Mark Renoufc19b4732019-06-26 12:08:33 -0400979 // If the bubble is removed for user switching, leave the notification in place.
Lyn Han2f6e89d2020-04-15 10:01:01 -0700980 if (reason == DISMISS_USER_CHANGED) {
981 continue;
982 }
983 if (!mBubbleData.hasBubbleInStackWithKey(bubble.getKey())) {
984 if (!mBubbleData.hasOverflowBubbleWithKey(bubble.getKey())
985 && (!bubble.showInShade()
986 || reason == DISMISS_NOTIF_CANCEL
987 || reason == DISMISS_GROUP_CANCELLED)) {
Beverlyed8aea22020-01-22 16:52:47 -0500988 // The bubble is now gone & the notification is hidden from the shade, so
989 // time to actually remove it
990 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -0500991 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -0500992 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400993 } else {
Lyn Han2f6e89d2020-04-15 10:01:01 -0700994 if (bubble.getEntry().isBubble() && bubble.showInShade()) {
995 setIsBubble(bubble, /* isBubble */ false);
996 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700997 if (bubble.getEntry().getRow() != null) {
998 bubble.getEntry().getRow().updateBubbleButton();
999 }
Mark Renouf82a40e62019-05-23 16:16:24 -04001000 }
Mady Mellor22f2f072019-04-18 13:26:18 -07001001
Lyn Han2f6e89d2020-04-15 10:01:01 -07001002 }
1003 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
1004 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
1005 // Time to potentially remove the summary
1006 for (NotifCallback cb : mCallbacks) {
1007 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -07001008 }
Mady Mellora54e9fa2019-04-18 13:26:18 -07001009 }
1010 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001011
Lyn Hanc47e1712020-01-28 21:43:34 -08001012 if (update.addedBubble != null) {
1013 mStackView.addBubble(update.addedBubble);
1014 }
1015
Mark Renouf82a40e62019-05-23 16:16:24 -04001016 if (update.updatedBubble != null) {
1017 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -04001018 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001019
Lyn Hanb58c7562020-01-07 14:29:20 -08001020 // At this point, the correct bubbles are inflated in the stack.
1021 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -04001022 if (update.orderChanged) {
1023 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -04001024 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001025
Mark Renouf82a40e62019-05-23 16:16:24 -04001026 if (update.selectionChanged) {
1027 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -07001028 if (update.selectedBubble != null) {
1029 mNotificationGroupManager.updateSuppression(
1030 update.selectedBubble.getEntry());
1031 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001032 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001033
Mark Renouf82a40e62019-05-23 16:16:24 -04001034 // Expanding? Apply this last.
1035 if (update.expandedChanged && update.expanded) {
1036 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001037 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001038
Beverlyed8aea22020-01-22 16:52:47 -05001039 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001040 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001041 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001042 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001043
Issei Suzukia8d07312019-06-07 12:56:19 +02001044 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001045 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001046 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001047 mBubbleData.getSelectedBubble()));
1048
1049 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001050 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001051 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001052 mStackView.getExpandedBubble()));
1053 }
Lyn Hanb58c7562020-01-07 14:29:20 -08001054 Log.d(TAG, "\n[BubbleData] overflow:");
1055 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
Lyn Han2f6e89d2020-04-15 10:01:01 -07001056 null) + "\n");
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001057 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001058 }
1059 };
1060
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001061 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001062 * We intercept notification entries (including group summaries) dismissed by the user when
1063 * there is an active bubble associated with it. We do this so that developers can still
1064 * cancel it (and hence the bubbles associated with it). However, these intercepted
1065 * notifications should then be hidden from the shade since the user has cancelled them, so we
1066 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1067 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001068 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001069 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001070 */
Beverlya53fb0d2020-01-29 15:26:13 -05001071 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001072 if (entry == null) {
1073 return false;
1074 }
Lyn Han2f6e89d2020-04-15 10:01:01 -07001075 if (isSummaryOfBubbles(entry)) {
Beverlya53fb0d2020-01-29 15:26:13 -05001076 handleSummaryDismissalInterception(entry);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001077 } else {
1078 Bubble bubble = mBubbleData.getBubbleInStackWithKey(entry.getKey());
1079 if (bubble == null || !entry.isBubble()) {
1080 bubble = mBubbleData.getOverflowBubbleWithKey(entry.getKey());
1081 }
1082 if (bubble == null) {
1083 return false;
1084 }
Beverlyed8aea22020-01-22 16:52:47 -05001085 bubble.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001086 bubble.setShowDot(false /* show */);
Beverlyed8aea22020-01-22 16:52:47 -05001087 }
Beverlya53fb0d2020-01-29 15:26:13 -05001088 // Update the shade
1089 for (NotifCallback cb : mCallbacks) {
1090 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1091 }
1092 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001093 }
1094
Beverlya53fb0d2020-01-29 15:26:13 -05001095 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1096 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001097 return false;
1098 }
Beverlya53fb0d2020-01-29 15:26:13 -05001099
1100 String groupKey = entry.getSbn().getGroupKey();
1101 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1102 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1103 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1104 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1105 return (isSuppressedSummary || isSummary)
1106 && bubbleChildren != null
1107 && !bubbleChildren.isEmpty();
1108 }
1109
1110 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1111 // current children in the row:
Kevin Han43077f92020-02-28 12:51:53 -08001112 final List<NotificationEntry> children = summary.getAttachedNotifChildren();
Beverlya53fb0d2020-01-29 15:26:13 -05001113 if (children != null) {
1114 for (int i = 0; i < children.size(); i++) {
1115 NotificationEntry child = children.get(i);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001116 if (mBubbleData.hasAnyBubbleWithKey(child.getKey())) {
Beverlya53fb0d2020-01-29 15:26:13 -05001117 // Suppress the bubbled child
1118 // As far as group manager is concerned, once a child is no longer shown
1119 // in the shade, it is essentially removed.
Lyn Han2f6e89d2020-04-15 10:01:01 -07001120 Bubble bubbleChild = mBubbleData.getAnyBubbleWithkey(child.getKey());
Beverlya53fb0d2020-01-29 15:26:13 -05001121 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1122 bubbleChild.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001123 bubbleChild.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001124 } else {
1125 // non-bubbled children can be removed
1126 for (NotifCallback cb : mCallbacks) {
1127 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1128 }
1129 }
1130 }
1131 }
1132
1133 // And since all children are removed, remove the summary.
1134 mNotificationGroupManager.onEntryRemoved(summary);
1135
1136 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1137 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1138 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001139 }
1140
1141 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001142 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001143 * Updates the visibility of the bubbles based on current state.
1144 * Does not un-bubble, just hides or un-hides. Notifies any
1145 * {@link BubbleStateChangeListener}s of visibility changes.
1146 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001147 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001148 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001149 if (mStackView == null) {
1150 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001151 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001152 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1153 // Bubbles only appear in unlocked shade
1154 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001155 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001156 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001157 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001158
Mady Mellor698d9e82019-08-01 23:11:53 +00001159 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001160 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001161 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001162 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001163 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1164 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1165 }
1166
1167 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001168 }
1169
1170 /**
1171 * Rect indicating the touchable region for the bubble stack / expanded stack.
1172 */
1173 public Rect getTouchableRegion() {
1174 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1175 return null;
1176 }
1177 mStackView.getBoundsOnScreen(mTempRect);
1178 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001179 }
1180
Mady Mellor390bff42019-04-05 15:09:01 -07001181 /**
1182 * The display id of the expanded view, if the stack is expanded and not occluded by the
1183 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1184 */
1185 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001186 if (mStackView == null) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001187 return INVALID_DISPLAY;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001188 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001189 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001190 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Lyn Han9f66c3b2020-03-05 23:59:29 -08001191 final BubbleViewProvider expandedViewProvider = mStackView.getExpandedBubble();
1192 if (defaultDisplay && expandedViewProvider != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001193 && !mNotificationShadeWindowController.getPanelExpanded()) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001194 return expandedViewProvider.getDisplayId();
Mady Mellor390bff42019-04-05 15:09:01 -07001195 }
Lyn Han9f66c3b2020-03-05 23:59:29 -08001196 return INVALID_DISPLAY;
Mady Mellor390bff42019-04-05 15:09:01 -07001197 }
1198
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001199 @VisibleForTesting
1200 BubbleStackView getStackView() {
1201 return mStackView;
1202 }
1203
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001204 /**
1205 * Description of current bubble state.
1206 */
1207 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1208 pw.println("BubbleController state:");
1209 mBubbleData.dump(fd, pw, args);
1210 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001211 if (mStackView != null) {
1212 mStackView.dump(fd, pw, args);
1213 }
1214 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001215 }
1216
Mady Mellore80930e2019-03-21 16:00:45 -07001217 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001218 * This task stack listener is responsible for responding to tasks moved to the front
1219 * which are on the default (main) display. When this happens, expanded bubbles must be
1220 * collapsed so the user may interact with the app which was just moved to the front.
1221 * <p>
1222 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1223 * these calls via a main thread Handler.
1224 */
1225 @MainThread
1226 private class BubbleTaskStackListener extends TaskStackChangeListener {
1227
Mark Renoufcecc77b2019-01-30 16:32:24 -05001228 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001229 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1230 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001231 if (!mStackView.isExpansionAnimating()) {
1232 mBubbleData.setExpanded(false);
1233 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001234 }
1235 }
1236
Mark Renoufcecc77b2019-01-30 16:32:24 -05001237 @Override
Winson Chunge789ff62020-02-24 14:40:23 -08001238 public void onActivityRestartAttempt(RunningTaskInfo task, boolean homeTaskVisible,
Evan Rosky8d1c24e2020-04-23 09:21:16 -07001239 boolean clearedTask, boolean wasVisible) {
Winson Chunge789ff62020-02-24 14:40:23 -08001240 for (Bubble b : mBubbleData.getBubbles()) {
1241 if (b.getDisplayId() == task.displayId) {
1242 expandStackAndSelectBubble(b.getKey());
1243 return;
1244 }
1245 }
1246 }
1247
1248 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001249 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001250 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001251 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001252 }
1253 }
Mark Renouf446251d2019-04-26 10:22:41 -04001254
1255 @Override
1256 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1257 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1258 mBubbleData.setExpanded(false);
1259 }
1260 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001261
1262 @Override
1263 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001264 if (mStackView == null) {
1265 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001266 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001267 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001268 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001269
1270 @Override
1271 public void onSingleTaskDisplayEmpty(int displayId) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001272 final BubbleViewProvider expandedBubble = mStackView != null
Mady Mellor5186b132019-09-16 17:55:48 -07001273 ? mStackView.getExpandedBubble()
1274 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001275 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1276 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001277 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001278 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001279 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001280 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001281 }
1282
Mady Mellorca0c24c2019-05-16 16:14:32 -07001283 /**
1284 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1285 *
1286 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1287 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1288 *
1289 * @param context the context to use.
1290 * @param entry the entry to bubble.
1291 */
1292 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1293 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Melloraa9ce172020-03-17 10:34:20 -07001294 ? entry.getBubbleMetadata().getIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001295 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001296 if (entry.getBubbleMetadata() != null
1297 && entry.getBubbleMetadata().getShortcutId() != null) {
1298 return true;
1299 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001300 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001301 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001302 return false;
1303 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001304 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1305 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001306 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001307 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001308 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001309 Log.w(TAG, "Unable to send as bubble, "
1310 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001311 + intent);
1312 return false;
1313 }
1314 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001315 Log.w(TAG, "Unable to send as bubble, "
1316 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001317 + intent);
1318 return false;
1319 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001320 return true;
1321 }
1322
Joshua Tsujia19515f2019-02-13 18:02:29 -05001323 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001324 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001325 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001326 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Joshua Tsujiff6b0f22020-03-09 14:55:19 -04001327 if (mStackView != null) {
Joshua Tsujid9422832019-03-05 13:32:37 -05001328 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001329 }
1330 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001331 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001332}