blob: 25bc7959a6cd9c3ec82f6af47c66e31f62827608 [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 Tsujid9923e52020-04-29 15:33:01 -040032import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
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
Pinyao Ting293b83d2020-05-06 17:10:56 -070045import android.annotation.NonNull;
Mark Renoufc19b4732019-06-26 12:08:33 -040046import android.annotation.UserIdInt;
Mark Renoufc808f062019-02-07 15:20:37 -050047import android.app.ActivityManager.RunningTaskInfo;
Mady Mellor9adfe6a2020-03-30 17:23:26 -070048import android.app.INotificationManager;
49import android.app.Notification;
50import android.app.NotificationChannel;
Mady Mellor66efd5e2019-05-15 13:38:11 -070051import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070052import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080053import android.content.Context;
Mady Mellorca0c24c2019-05-16 16:14:32 -070054import android.content.pm.ActivityInfo;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080055import android.content.pm.PackageManager;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040056import android.content.res.Configuration;
Joshua Tsujid9923e52020-04-29 15:33:01 -040057import android.graphics.PixelFormat;
Mady Mellord1c78b262018-11-06 18:04:40 -080058import android.graphics.Rect;
Joshua Tsujid9923e52020-04-29 15:33:01 -040059import android.os.Binder;
Lyn Han6cb4e5f2020-04-27 15:11:18 -070060import android.os.Handler;
Mark Renoufcecc77b2019-01-30 16:32:24 -050061import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080062import android.os.ServiceManager;
Mady Mellor56515c42020-02-18 17:58:36 -080063import android.service.notification.NotificationListenerService;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040064import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040065import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040066import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040067import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040068import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040069import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050070import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080071import android.view.ViewGroup;
Joshua Tsujid9923e52020-04-29 15:33:01 -040072import android.view.WindowManager;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080073
Mark Renouf08bc42a2019-03-07 13:01:59 -050074import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050075import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040076import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050077
Mady Mellorebdbbb92018-11-15 14:36:48 -080078import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070079import com.android.internal.statusbar.IStatusBarService;
Beverlya53fb0d2020-01-29 15:26:13 -050080import com.android.internal.statusbar.NotificationVisibility;
Beverlya53fb0d2020-01-29 15:26:13 -050081import com.android.systemui.Dumpable;
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +010082import com.android.systemui.bubbles.dagger.BubbleModule;
Ned Burnsaaeb44b2020-02-12 23:48:26 -050083import com.android.systemui.dump.DumpManager;
Joshua Tsujibe60a582020-03-23 17:17:26 -040084import com.android.systemui.model.SysUiState;
Beverly8fdb5332019-02-04 14:29:49 -050085import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050086import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000087import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050088import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050089import com.android.systemui.shared.system.WindowManagerWrapper;
Beverlya53fb0d2020-01-29 15:26:13 -050090import com.android.systemui.statusbar.FeatureFlags;
Mark Renoufc19b4732019-06-26 12:08:33 -040091import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070092import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Joshua Tsujid9923e52020-04-29 15:33:01 -040093import com.android.systemui.statusbar.ScrimView;
Mady Mellor9adfe6a2020-03-30 17:23:26 -070094import com.android.systemui.statusbar.notification.NotificationChannelHelper;
Ned Burns01e38212019-01-03 16:32:52 -050095import com.android.systemui.statusbar.notification.NotificationEntryListener;
96import com.android.systemui.statusbar.notification.NotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -050097import com.android.systemui.statusbar.notification.collection.NotifCollection;
98import com.android.systemui.statusbar.notification.collection.NotifPipeline;
Ned Burnsf81c4c42019-01-07 14:10:43 -050099import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Beverlya53fb0d2020-01-29 15:26:13 -0500100import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener;
Beverly Taid1e175c2020-03-10 16:37:04 +0000101import com.android.systemui.statusbar.notification.interruption.NotificationInterruptStateProvider;
Mady Mellor22f2f072019-04-18 13:26:18 -0700102import com.android.systemui.statusbar.phone.NotificationGroupManager;
wilsonshihe8321942019-10-18 18:39:46 +0800103import com.android.systemui.statusbar.phone.NotificationShadeWindowController;
Joshua Tsujid9923e52020-04-29 15:33:01 -0400104import com.android.systemui.statusbar.phone.ScrimController;
Mady Mellor7f234902019-10-20 12:06:29 -0700105import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorf3b9fab2019-11-13 17:27:32 -0800106import com.android.systemui.statusbar.phone.StatusBar;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700107import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400108import com.android.systemui.statusbar.policy.ZenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500109import com.android.systemui.util.FloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800110
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700111import java.io.FileDescriptor;
112import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500113import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -0400114import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -0700115import java.util.ArrayList;
Lyn Hanb58c7562020-01-07 14:29:20 -0800116import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500117
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800118/**
119 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
120 * Bubbles can be expanded to show more content.
121 *
122 * The controller manages addition, removal, and visible state of bubbles on screen.
123 */
Beverlya53fb0d2020-01-29 15:26:13 -0500124public class BubbleController implements ConfigurationController.ConfigurationListener, Dumpable {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800125
Issei Suzukia8d07312019-06-07 12:56:19 +0200126 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800127
Mark Renouf08bc42a2019-03-07 13:01:59 -0500128 @Retention(SOURCE)
129 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400130 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Lyn Han2f6e89d2020-04-15 10:01:01 -0700131 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT,
132 DISMISS_OVERFLOW_MAX_REACHED})
Mark Renoufba5ab512019-05-02 15:21:01 -0400133 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500134 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700135
Mark Renouf08bc42a2019-03-07 13:01:59 -0500136 static final int DISMISS_USER_GESTURE = 1;
137 static final int DISMISS_AGED = 2;
138 static final int DISMISS_TASK_FINISHED = 3;
139 static final int DISMISS_BLOCKED = 4;
140 static final int DISMISS_NOTIF_CANCEL = 5;
141 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700142 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400143 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700144 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700145 static final int DISMISS_INVALID_INTENT = 10;
Lyn Han2f6e89d2020-04-15 10:01:01 -0700146 static final int DISMISS_OVERFLOW_MAX_REACHED = 11;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500147
Ned Burns01e38212019-01-03 16:32:52 -0500148 private final Context mContext;
149 private final NotificationEntryManager mNotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500150 private final NotifPipeline mNotifPipeline;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500151 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800152 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100153 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700154 private final NotificationGroupManager mNotificationGroupManager;
Heemin Seogba6337f2019-12-10 15:34:37 -0800155 private final ShadeController mShadeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500156 private final FloatingContentCoordinator mFloatingContentCoordinator;
Pinyao Tingee191b12020-04-29 18:35:39 -0700157 private final BubbleDataRepository mDataRepository;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800158
Mady Mellor3dff9e62019-02-05 18:12:53 -0800159 private BubbleData mBubbleData;
Joshua Tsujid9923e52020-04-29 15:33:01 -0400160 private ScrimView mBubbleScrim;
Joshua Tsujic650a142019-05-22 11:31:19 -0400161 @Nullable private BubbleStackView mStackView;
Mady Mellor247ca2c2019-12-02 16:18:59 -0800162 private BubbleIconFactory mBubbleIconFactory;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800163
Mark Renoufc19b4732019-06-26 12:08:33 -0400164 // Tracks the id of the current (foreground) user.
165 private int mCurrentUserId;
166 // Saves notification keys of active bubbles when users are switched.
167 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
168
Mady Mellor56515c42020-02-18 17:58:36 -0800169 // Used when ranking updates occur and we check if things should bubble / unbubble
170 private NotificationListenerService.Ranking mTmpRanking;
171
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800172 // Bubbles get added to the status bar view
wilsonshihe8321942019-10-18 18:39:46 +0800173 private final NotificationShadeWindowController mNotificationShadeWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400174 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800175 private StatusBarStateListener mStatusBarStateListener;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700176 private INotificationManager mINotificationManager;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500177
Lyn Hanb58c7562020-01-07 14:29:20 -0800178 // Callback that updates BubbleOverflowActivity on data change.
Mady Mellor1d082022020-05-12 16:35:39 +0000179 @Nullable private Runnable mOverflowCallback = null;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800180
Beverly Taid1e175c2020-03-10 16:37:04 +0000181 private final NotificationInterruptStateProvider mNotificationInterruptStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700182 private IStatusBarService mBarService;
Joshua Tsujid9923e52020-04-29 15:33:01 -0400183 private WindowManager mWindowManager;
Joshua Tsujibe60a582020-03-23 17:17:26 -0400184 private SysUiState mSysUiState;
Mady Mellorb4991e62019-01-10 15:14:51 -0800185
Lyn Han6cb4e5f2020-04-27 15:11:18 -0700186 // Used to post to main UI thread
187 private Handler mHandler = new Handler();
188
Joshua Tsujid9923e52020-04-29 15:33:01 -0400189 /** LayoutParams used to add the BubbleStackView to the window maanger. */
190 private WindowManager.LayoutParams mWmLayoutParams;
191
192
Mady Mellord1c78b262018-11-06 18:04:40 -0800193 // Used for determining view rect for touch interaction
194 private Rect mTempRect = new Rect();
195
Mark Renoufc19b4732019-06-26 12:08:33 -0400196 // Listens to user switch so bubbles can be saved and restored.
197 private final NotificationLockscreenUserManager mNotifUserManager;
198
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400199 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
200 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
201
Lyn Hanb4b06132020-05-11 09:25:20 -0700202 /**
203 * Last known screen density, used to detect display size changes in {@link #onConfigChanged}.
204 */
205 private int mDensityDpi = Configuration.DENSITY_DPI_UNDEFINED;
206
Mady Mellor3df7ab02019-12-09 15:07:10 -0800207 private boolean mInflateSynchronously;
208
Beverlyed8aea22020-01-22 16:52:47 -0500209 // TODO (b/145659174): allow for multiple callbacks to support the "shadow" new notif pipeline
210 private final List<NotifCallback> mCallbacks = new ArrayList<>();
211
Mady Mellor5549dd22018-11-06 18:07:34 -0800212 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800213 * Listener to find out about stack expansion / collapse events.
214 */
215 public interface BubbleExpandListener {
216 /**
217 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700218 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800219 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800220 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800221 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800222 void onBubbleExpandChanged(boolean isExpanding, String key);
223 }
224
225 /**
Mady Mellorf44b6832020-01-14 13:26:14 -0800226 * Listener to be notified when a bubbles' notification suppression state changes.
227 */
228 public interface NotificationSuppressionChangedListener {
229 /**
230 * Called when the notification suppression state of a bubble changes.
231 */
232 void onBubbleNotificationSuppressionChange(Bubble bubble);
Beverlyed8aea22020-01-22 16:52:47 -0500233 }
Mady Mellorf44b6832020-01-14 13:26:14 -0800234
Beverlyed8aea22020-01-22 16:52:47 -0500235 /**
236 * Callback for when the BubbleController wants to interact with the notification pipeline to:
237 * - Remove a previously bubbled notification
238 * - Update the notification shade since bubbled notification should/shouldn't be showing
239 */
240 public interface NotifCallback {
241 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500242 * Called when a bubbled notification that was hidden from the shade is now being removed
243 * This can happen when an app cancels a bubbled notification or when the user dismisses a
244 * bubble.
Beverlyed8aea22020-01-22 16:52:47 -0500245 */
Pinyao Ting293b83d2020-05-06 17:10:56 -0700246 void removeNotification(@NonNull NotificationEntry entry, int reason);
Beverlyed8aea22020-01-22 16:52:47 -0500247
248 /**
249 * Called when a bubbled notification has changed whether it should be
250 * filtered from the shade.
251 */
Beverlya53fb0d2020-01-29 15:26:13 -0500252 void invalidateNotifications(String reason);
Beverlyed8aea22020-01-22 16:52:47 -0500253
254 /**
255 * Called on a bubbled entry that has been removed when there are no longer
256 * bubbled entries in its group.
257 *
258 * Checks whether its group has any other (non-bubbled) children. If it doesn't,
259 * removes all remnants of the group's summary from the notification pipeline.
260 * TODO: (b/145659174) Only old pipeline needs this - delete post-migration.
261 */
Pinyao Ting293b83d2020-05-06 17:10:56 -0700262 void maybeCancelSummary(@NonNull NotificationEntry entry);
Mady Mellorf44b6832020-01-14 13:26:14 -0800263 }
264
265 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800266 * Listens for the current state of the status bar and updates the visibility state
267 * of bubbles as needed.
268 */
269 private class StatusBarStateListener implements StatusBarStateController.StateListener {
270 private int mState;
271 /**
272 * Returns the current status bar state.
273 */
274 public int getCurrentState() {
275 return mState;
276 }
277
278 @Override
279 public void onStateChanged(int newState) {
280 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400281 boolean shouldCollapse = (mState != SHADE);
282 if (shouldCollapse) {
283 collapseStack();
284 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700285 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800286 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800287 }
288
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +0100289 /**
290 * Injected constructor. See {@link BubbleModule}.
291 */
Mady Mellor7f234902019-10-20 12:06:29 -0700292 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800293 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700294 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800295 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700296 BubbleData data,
297 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
298 ConfigurationController configurationController,
Beverly Taid1e175c2020-03-10 16:37:04 +0000299 NotificationInterruptStateProvider interruptionStateProvider,
Mady Mellor7f234902019-10-20 12:06:29 -0700300 ZenModeController zenModeController,
301 NotificationLockscreenUserManager notifUserManager,
302 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500303 NotificationEntryManager entryManager,
304 NotifPipeline notifPipeline,
305 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500306 DumpManager dumpManager,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400307 FloatingContentCoordinator floatingContentCoordinator,
Pinyao Tingee191b12020-04-29 18:35:39 -0700308 BubbleDataRepository dataRepository,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700309 SysUiState sysUiState,
Mady Mellor59a7b982020-05-11 15:19:59 -0700310 INotificationManager notificationManager,
311 WindowManager windowManager) {
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500312 dumpManager.registerDumpable(TAG, this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800313 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800314 mShadeController = shadeController;
Beverly Taid1e175c2020-03-10 16:37:04 +0000315 mNotificationInterruptStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400316 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400317 mZenModeController = zenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500318 mFloatingContentCoordinator = floatingContentCoordinator;
Pinyao Tingee191b12020-04-29 18:35:39 -0700319 mDataRepository = dataRepository;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700320 mINotificationManager = notificationManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400321 mZenModeController.addCallback(new ZenModeController.Callback() {
322 @Override
323 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800324 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400325 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700326 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400327 }
328
329 @Override
330 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800331 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400332 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700333 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400334 }
335 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700336
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700337 configurationController.addCallback(this /* configurationListener */);
Joshua Tsujibe60a582020-03-23 17:17:26 -0400338 mSysUiState = sysUiState;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800339
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400340 mBubbleData = data;
341 mBubbleData.setListener(mBubbleDataListener);
Mady Mellorf44b6832020-01-14 13:26:14 -0800342 mBubbleData.setSuppressionChangedListener(new NotificationSuppressionChangedListener() {
343 @Override
344 public void onBubbleNotificationSuppressionChange(Bubble bubble) {
345 // Make sure NoMan knows it's not showing in the shade anymore so anyone querying it
346 // can tell.
347 try {
348 mBarService.onBubbleNotificationSuppressionChanged(bubble.getKey(),
349 !bubble.showInShade());
350 } catch (RemoteException e) {
351 // Bad things have happened
352 }
353 }
354 });
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400355
Mady Mellor7f234902019-10-20 12:06:29 -0700356 mNotificationEntryManager = entryManager;
Mady Mellor22f2f072019-04-18 13:26:18 -0700357 mNotificationGroupManager = groupManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500358 mNotifPipeline = notifPipeline;
359
360 if (!featureFlags.isNewNotifPipelineRenderingEnabled()) {
361 setupNEM();
362 } else {
363 setupNotifPipeline();
364 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800365
wilsonshihe8321942019-10-18 18:39:46 +0800366 mNotificationShadeWindowController = notificationShadeWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800367 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700368 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500369
Mark Renoufcecc77b2019-01-30 16:32:24 -0500370 mTaskStackListener = new BubbleTaskStackListener();
371 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800372
Joshua Tsujia19515f2019-02-13 18:02:29 -0500373 try {
374 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
375 } catch (RemoteException e) {
376 e.printStackTrace();
377 }
Issei Suzukic0387542019-03-08 17:31:14 +0100378 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700379
Mady Mellor59a7b982020-05-11 15:19:59 -0700380 mWindowManager = windowManager;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700381 mBarService = IStatusBarService.Stub.asInterface(
382 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400383
Joshua Tsujid9923e52020-04-29 15:33:01 -0400384 mBubbleScrim = new ScrimView(mContext);
385
Mark Renoufc19b4732019-06-26 12:08:33 -0400386 mSavedBubbleKeysPerUser = new SparseSetArray<>();
387 mCurrentUserId = mNotifUserManager.getCurrentUserId();
388 mNotifUserManager.addUserChangedListener(
Steve Elliottb47f1c72019-12-19 12:39:26 -0500389 new NotificationLockscreenUserManager.UserChangedListener() {
390 @Override
391 public void onUserChanged(int newUserId) {
392 BubbleController.this.saveBubbles(mCurrentUserId);
393 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
394 BubbleController.this.restoreBubbles(newUserId);
395 mCurrentUserId = newUserId;
396 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400397 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800398
Mady Mellor247ca2c2019-12-02 16:18:59 -0800399 mBubbleIconFactory = new BubbleIconFactory(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800400 }
401
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400402 /**
Beverlyed8aea22020-01-22 16:52:47 -0500403 * See {@link NotifCallback}.
404 */
405 public void addNotifCallback(NotifCallback callback) {
406 mCallbacks.add(callback);
407 }
408
409 private void setupNEM() {
410 mNotificationEntryManager.addNotificationEntryListener(
411 new NotificationEntryListener() {
412 @Override
Mady Mellorf9439ab2020-01-30 16:06:53 -0800413 public void onPendingEntryAdded(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -0500414 onEntryAdded(entry);
415 }
416
417 @Override
418 public void onPreEntryUpdated(NotificationEntry entry) {
419 onEntryUpdated(entry);
420 }
421
422 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500423 public void onEntryRemoved(
424 NotificationEntry entry,
425 @android.annotation.Nullable NotificationVisibility visibility,
Julia Reynolds138111f2020-02-26 11:17:39 -0500426 boolean removedByUser,
427 int reason) {
Beverlya53fb0d2020-01-29 15:26:13 -0500428 BubbleController.this.onEntryRemoved(entry);
429 }
430
431 @Override
Beverlyed8aea22020-01-22 16:52:47 -0500432 public void onNotificationRankingUpdated(RankingMap rankingMap) {
433 onRankingUpdated(rankingMap);
434 }
435 });
436
Evan Laird04373662020-01-24 17:37:39 -0500437 mNotificationEntryManager.addNotificationRemoveInterceptor(
Beverlyed8aea22020-01-22 16:52:47 -0500438 new NotificationRemoveInterceptor() {
439 @Override
Evan Laird04373662020-01-24 17:37:39 -0500440 public boolean onNotificationRemoveRequested(
Beverlya53fb0d2020-01-29 15:26:13 -0500441 String key,
442 NotificationEntry entry,
443 int dismissReason) {
444 final boolean isClearAll = dismissReason == REASON_CANCEL_ALL;
445 final boolean isUserDimiss = dismissReason == REASON_CANCEL
446 || dismissReason == REASON_CLICK;
447 final boolean isAppCancel = dismissReason == REASON_APP_CANCEL
448 || dismissReason == REASON_APP_CANCEL_ALL;
449 final boolean isSummaryCancel =
450 dismissReason == REASON_GROUP_SUMMARY_CANCELED;
451
452 // Need to check for !appCancel here because the notification may have
453 // previously been dismissed & entry.isRowDismissed would still be true
454 boolean userRemovedNotif =
455 (entry != null && entry.isRowDismissed() && !isAppCancel)
456 || isClearAll || isUserDimiss || isSummaryCancel;
457
Mady Mellordd6fe612020-04-15 11:47:55 -0700458 if (userRemovedNotif) {
Beverlya53fb0d2020-01-29 15:26:13 -0500459 return handleDismissalInterception(entry);
460 }
Beverlya53fb0d2020-01-29 15:26:13 -0500461 return false;
Beverlyed8aea22020-01-22 16:52:47 -0500462 }
463 });
464
465 mNotificationGroupManager.addOnGroupChangeListener(
466 new NotificationGroupManager.OnGroupChangeListener() {
467 @Override
468 public void onGroupSuppressionChanged(
469 NotificationGroupManager.NotificationGroup group,
470 boolean suppressed) {
471 // More notifications could be added causing summary to no longer
472 // be suppressed -- in this case need to remove the key.
473 final String groupKey = group.summary != null
474 ? group.summary.getSbn().getGroupKey()
475 : null;
476 if (!suppressed && groupKey != null
477 && mBubbleData.isSummarySuppressed(groupKey)) {
478 mBubbleData.removeSuppressedSummary(groupKey);
479 }
480 }
481 });
482
483 addNotifCallback(new NotifCallback() {
484 @Override
Pinyao Ting293b83d2020-05-06 17:10:56 -0700485 public void removeNotification(@NonNull final NotificationEntry entry, int reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500486 mNotificationEntryManager.performRemoveNotification(entry.getSbn(),
Beverlya53fb0d2020-01-29 15:26:13 -0500487 reason);
Beverlyed8aea22020-01-22 16:52:47 -0500488 }
489
490 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500491 public void invalidateNotifications(String reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500492 mNotificationEntryManager.updateNotifications(reason);
493 }
494
495 @Override
Pinyao Ting293b83d2020-05-06 17:10:56 -0700496 public void maybeCancelSummary(@NonNull final NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -0500497 // Check if removed bubble has an associated suppressed group summary that needs
498 // to be removed now.
Beverlya53fb0d2020-01-29 15:26:13 -0500499 final String groupKey = entry.getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500500 if (mBubbleData.isSummarySuppressed(groupKey)) {
Beverlya53fb0d2020-01-29 15:26:13 -0500501 mBubbleData.removeSuppressedSummary(groupKey);
Beverlyed8aea22020-01-22 16:52:47 -0500502
503 final NotificationEntry summary =
504 mNotificationEntryManager.getActiveNotificationUnfiltered(
505 mBubbleData.getSummaryKey(groupKey));
Beverlya53fb0d2020-01-29 15:26:13 -0500506 if (summary != null) {
507 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
508 UNDEFINED_DISMISS_REASON);
509 }
Beverlyed8aea22020-01-22 16:52:47 -0500510 }
511
Beverlya53fb0d2020-01-29 15:26:13 -0500512 // Check if we still need to remove the summary from NoManGroup because the summary
513 // may not be in the mBubbleData.mSuppressedGroupKeys list and removed above.
514 // For example:
515 // 1. Bubbled notifications (group) is posted to shade and are visible bubbles
516 // 2. User expands bubbles so now their respective notifications in the shade are
517 // hidden, including the group summary
518 // 3. User removes all bubbles
519 // 4. We expect all the removed bubbles AND the summary (note: the summary was
520 // never added to the suppressedSummary list in BubbleData, so we add this check)
Beverlyed8aea22020-01-22 16:52:47 -0500521 NotificationEntry summary =
522 mNotificationGroupManager.getLogicalGroupSummary(entry.getSbn());
523 if (summary != null) {
524 ArrayList<NotificationEntry> summaryChildren =
525 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
526 boolean isSummaryThisNotif = summary.getKey().equals(entry.getKey());
527 if (!isSummaryThisNotif && (summaryChildren == null
528 || summaryChildren.isEmpty())) {
529 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
530 UNDEFINED_DISMISS_REASON);
531 }
532 }
533 }
534 });
535 }
536
Beverlya53fb0d2020-01-29 15:26:13 -0500537 private void setupNotifPipeline() {
538 mNotifPipeline.addCollectionListener(new NotifCollectionListener() {
539 @Override
540 public void onEntryAdded(NotificationEntry entry) {
541 BubbleController.this.onEntryAdded(entry);
542 }
543
544 @Override
545 public void onEntryUpdated(NotificationEntry entry) {
546 BubbleController.this.onEntryUpdated(entry);
547 }
548
549 @Override
550 public void onRankingUpdate(RankingMap rankingMap) {
551 onRankingUpdated(rankingMap);
552 }
553
554 @Override
555 public void onEntryRemoved(NotificationEntry entry,
556 @NotifCollection.CancellationReason int reason) {
557 BubbleController.this.onEntryRemoved(entry);
558 }
559 });
560 }
561
Beverlyed8aea22020-01-22 16:52:47 -0500562 /**
Joshua Tsujid9923e52020-04-29 15:33:01 -0400563 * Returns the scrim drawn behind the bubble stack. This is managed by {@link ScrimController}
564 * since we want the scrim's appearance and behavior to be identical to that of the notification
565 * shade scrim.
566 */
567 public ScrimView getScrimForBubble() {
568 return mBubbleScrim;
569 }
570
571 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800572 * Sets whether to perform inflation on the same thread as the caller. This method should only
573 * be used in tests, not in production.
574 */
575 @VisibleForTesting
576 void setInflateSynchronously(boolean inflateSynchronously) {
577 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800578 }
579
Mady Mellor1d082022020-05-12 16:35:39 +0000580 void setOverflowCallback(Runnable updateOverflow) {
581 mOverflowCallback = updateOverflow;
Lyn Hanb58c7562020-01-07 14:29:20 -0800582 }
583
584 /**
585 * @return Bubbles for updating overflow.
586 */
587 List<Bubble> getOverflowBubbles() {
588 return mBubbleData.getOverflowBubbles();
589 }
590
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400591 /**
592 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
593 * method initializes the stack view and adds it to the StatusBar just above the scrim.
594 */
595 private void ensureStackViewCreated() {
596 if (mStackView == null) {
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500597 mStackView = new BubbleStackView(
Joshua Tsujibe60a582020-03-23 17:17:26 -0400598 mContext, mBubbleData, mSurfaceSynchronizer, mFloatingContentCoordinator,
Joshua Tsujiba9fef02020-04-09 17:40:35 -0400599 mSysUiState, mNotificationShadeWindowController);
Joshua Tsujid9923e52020-04-29 15:33:01 -0400600 mStackView.addView(mBubbleScrim);
601 addToWindowManager();
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400602 if (mExpandListener != null) {
603 mStackView.setExpandListener(mExpandListener);
604 }
Joshua Tsuji6855cab2020-04-16 01:05:39 -0400605
606 mStackView.setUnbubbleConversationCallback(notificationEntry ->
607 onUserChangedBubble(notificationEntry, false /* shouldBubble */));
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400608 }
609 }
610
Joshua Tsujid9923e52020-04-29 15:33:01 -0400611 /** Adds the BubbleStackView to the WindowManager. */
612 private void addToWindowManager() {
613 mWmLayoutParams = new WindowManager.LayoutParams(
614 // Fill the screen so we can use translation animations to position the bubble
615 // stack. We'll use touchable regions to ignore touches that are not on the bubbles
616 // themselves.
617 ViewGroup.LayoutParams.MATCH_PARENT,
618 ViewGroup.LayoutParams.MATCH_PARENT,
Joshua Tsuji94d4c342020-05-04 14:00:24 -0400619 WindowManager.LayoutParams.TYPE_TRUSTED_APPLICATION_OVERLAY,
Joshua Tsujid9923e52020-04-29 15:33:01 -0400620 // Start not focusable - we'll become focusable when expanded so the ActivityView
621 // can use the IME.
622 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
623 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
624 PixelFormat.TRANSLUCENT);
625
626 mWmLayoutParams.setFitInsetsTypes(0);
627 mWmLayoutParams.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
628 mWmLayoutParams.token = new Binder();
629 mWmLayoutParams.setTitle("Bubbles!");
630 mWmLayoutParams.packageName = mContext.getPackageName();
631 mWmLayoutParams.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
632
633 mWindowManager.addView(mStackView, mWmLayoutParams);
634 }
635
636 private void updateWmFlags() {
637 if (isStackExpanded()) {
638 // If we're expanded, we want to be focusable so that the ActivityView can receive focus
639 // and show the IME.
640 mWmLayoutParams.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
641 } else {
642 // If we're collapsed, we don't want to be able to receive focus. Doing so would
643 // preclude applications from using the IME since we are always above them.
644 mWmLayoutParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
645 }
646
647 mWindowManager.updateViewLayout(mStackView, mWmLayoutParams);
648 }
649
Mark Renoufc19b4732019-06-26 12:08:33 -0400650 /**
651 * Records the notification key for any active bubbles. These are used to restore active
652 * bubbles when the user returns to the foreground.
653 *
654 * @param userId the id of the user
655 */
656 private void saveBubbles(@UserIdInt int userId) {
657 // First clear any existing keys that might be stored.
658 mSavedBubbleKeysPerUser.remove(userId);
659 // Add in all active bubbles for the current user.
660 for (Bubble bubble: mBubbleData.getBubbles()) {
661 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
662 }
663 }
664
665 /**
666 * Promotes existing notifications to Bubbles if they were previously bubbles.
667 *
668 * @param userId the id of the user
669 */
670 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400671 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
672 if (savedBubbleKeys == null) {
673 // There were no bubbles saved for this used.
674 return;
675 }
Evan Laird181de622019-10-24 09:53:02 -0400676 for (NotificationEntry e :
677 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400678 if (savedBubbleKeys.contains(e.getKey())
Beverly Taid1e175c2020-03-10 16:37:04 +0000679 && mNotificationInterruptStateProvider.shouldBubbleUp(e)
Mark Renoufc19b4732019-06-26 12:08:33 -0400680 && canLaunchInActivityView(mContext, e)) {
681 updateBubble(e, /* suppressFlyout= */ true);
682 }
683 }
684 // Finally, remove the entries for this user now that bubbles are restored.
685 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
686 }
687
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700688 @Override
689 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800690 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700691 }
692
693 @Override
694 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800695 updateForThemeChanges();
696 }
697
698 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700699 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700700 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700701 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800702 mBubbleIconFactory = new BubbleIconFactory(mContext);
Lyn Handa9a6a22020-04-24 13:21:43 -0700703 // Reload each bubble
Mady Mellor3df7ab02019-12-09 15:07:10 -0800704 for (Bubble b: mBubbleData.getBubbles()) {
Pinyao Ting293b83d2020-05-06 17:10:56 -0700705 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory,
706 false /* skipInflation */);
Lyn Handa9a6a22020-04-24 13:21:43 -0700707 }
708 for (Bubble b: mBubbleData.getOverflowBubbles()) {
Pinyao Ting293b83d2020-05-06 17:10:56 -0700709 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory,
710 false /* skipInflation */);
Mady Mellor3df7ab02019-12-09 15:07:10 -0800711 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700712 }
713
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400714 @Override
715 public void onConfigChanged(Configuration newConfig) {
Lyn Hanb4b06132020-05-11 09:25:20 -0700716 if (mStackView != null && newConfig != null) {
717 if (newConfig.orientation != mOrientation) {
718 mOrientation = newConfig.orientation;
719 mStackView.onOrientationChanged(newConfig.orientation);
720 }
721 if (newConfig.densityDpi != mDensityDpi) {
722 mDensityDpi = newConfig.densityDpi;
723 mBubbleIconFactory = new BubbleIconFactory(mContext);
724 mStackView.onDisplaySizeChanged();
725 }
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400726 }
727 }
728
Mady Mellor5549dd22018-11-06 18:07:34 -0800729 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800730 * Set a listener to be notified of bubble expand events.
731 */
732 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100733 mExpandListener = ((isExpanding, key) -> {
734 if (listener != null) {
735 listener.onBubbleExpandChanged(isExpanding, key);
736 }
Joshua Tsujid9923e52020-04-29 15:33:01 -0400737
738 updateWmFlags();
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100739 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800740 if (mStackView != null) {
741 mStackView.setExpandListener(mExpandListener);
742 }
743 }
744
745 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800746 * Whether or not there are bubbles present, regardless of them being visible on the
747 * screen (e.g. if on AOD).
748 */
Joshua Tsujid9923e52020-04-29 15:33:01 -0400749 @VisibleForTesting
750 boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800751 if (mStackView == null) {
752 return false;
753 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400754 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800755 }
756
757 /**
758 * Whether the stack of bubbles is expanded or not.
759 */
760 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400761 return mBubbleData.isExpanded();
762 }
763
764 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800765 * Tell the stack of bubbles to collapse.
766 */
767 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400768 mBubbleData.setExpanded(false /* expanded */);
769 }
770
Mady Mellorce23c462019-06-17 17:30:07 -0700771 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700772 * True if either:
773 * (1) There is a bubble associated with the provided key and if its notification is hidden
774 * from the shade.
775 * (2) There is a group summary associated with the provided key that is hidden from the shade
776 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700777 *
Mady Mellore28fe102019-07-09 15:33:32 -0700778 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700779 */
Beverlyed8aea22020-01-22 16:52:47 -0500780 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
781 String key = entry.getKey();
Lyn Han2f6e89d2020-04-15 10:01:01 -0700782 boolean isSuppressedBubble = (mBubbleData.hasAnyBubbleWithKey(key)
783 && !mBubbleData.getAnyBubbleWithkey(key).showInShade());
Beverlyed8aea22020-01-22 16:52:47 -0500784
785 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700786 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700787 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Lyn Han2f6e89d2020-04-15 10:01:01 -0700788 return (isSummary && isSuppressedSummary) || isSuppressedBubble;
Mady Mellorce23c462019-06-17 17:30:07 -0700789 }
790
Lyn Hanb58c7562020-01-07 14:29:20 -0800791 void promoteBubbleFromOverflow(Bubble bubble) {
Lyn Han1e19d7f2020-02-05 19:10:58 -0800792 bubble.setInflateSynchronously(mInflateSynchronously);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700793 setIsBubble(bubble, /* isBubble */ true);
Lyn Han1e19d7f2020-02-05 19:10:58 -0800794 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
Lyn Hanb58c7562020-01-07 14:29:20 -0800795 }
796
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800797 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400798 * Request the stack expand if needed, then select the specified Bubble as current.
799 *
800 * @param notificationKey the notification key for the bubble to be selected
801 */
802 public void expandStackAndSelectBubble(String notificationKey) {
Lyn Han2f6e89d2020-04-15 10:01:01 -0700803 Bubble bubble = mBubbleData.getBubbleInStackWithKey(notificationKey);
804 if (bubble == null) {
805 bubble = mBubbleData.getOverflowBubbleWithKey(notificationKey);
806 if (bubble != null) {
807 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
808 }
Pinyao Ting293b83d2020-05-06 17:10:56 -0700809 } else if (bubble.isBubble()) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400810 mBubbleData.setSelectedBubble(bubble);
Mark Renouffec45da2019-03-13 13:24:27 -0400811 }
Lyn Han2f6e89d2020-04-15 10:01:01 -0700812 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400813 }
814
815 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500816 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
817 * is forwarded a back key down/up pair.
818 */
819 public void performBackPressIfNeeded() {
820 if (mStackView != null) {
821 mStackView.performBackPressIfNeeded();
822 }
823 }
824
825 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800826 * Adds or updates a bubble associated with the provided notification entry.
827 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400828 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800829 */
Mark Renouff97ed462019-04-05 13:46:24 -0400830 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700831 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400832 }
833
834 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700835 updateBubble(notif, suppressFlyout, true /* showInShade */);
836 }
837
Pinyao Ting293b83d2020-05-06 17:10:56 -0700838 /**
839 * Fills the overflow bubbles by loading them from disk.
840 */
841 void loadOverflowBubblesFromDisk() {
842 if (!mBubbleData.getOverflowBubbles().isEmpty()) {
843 // we don't need to load overflow bubbles from disk if it is already in memory
844 return;
845 }
846 mDataRepository.loadBubbles((bubbles) -> {
847 bubbles.forEach(bubble -> {
848 if (mBubbleData.getBubbles().contains(bubble)) {
849 // if the bubble is already active, there's no need to push it to overflow
850 return;
851 }
852 bubble.inflate((b) -> mBubbleData.overflowBubble(DISMISS_AGED, bubble),
853 mContext, mStackView, mBubbleIconFactory, true /* skipInflation */);
854 });
855 return null;
856 });
857 }
858
Mady Mellor7f234902019-10-20 12:06:29 -0700859 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800860 if (mStackView == null) {
861 // Lazy init stack view when a bubble is created
862 ensureStackViewCreated();
Pinyao Ting293b83d2020-05-06 17:10:56 -0700863 // Lazy load overflow bubbles from disk
864 loadOverflowBubblesFromDisk();
Mady Mellor3df7ab02019-12-09 15:07:10 -0800865 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700866 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400867 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700868 notif.setInterruption();
869 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800870 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
871 bubble.setInflateSynchronously(mInflateSynchronously);
872 bubble.inflate(
Lyn Han6cb4e5f2020-04-27 15:11:18 -0700873 b -> {
874 mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade);
875 if (bubble.getBubbleIntent() == null) {
876 return;
877 }
878 bubble.getBubbleIntent().registerCancelListener(pendingIntent -> {
879 if (bubble.getWasAccessed()) {
880 bubble.setPendingIntentCanceled();
881 return;
882 }
883 mHandler.post(
Pinyao Ting293b83d2020-05-06 17:10:56 -0700884 () -> removeBubble(bubble.getKey(),
Lyn Han6cb4e5f2020-04-27 15:11:18 -0700885 BubbleController.DISMISS_INVALID_INTENT));
886 });
887 },
Pinyao Ting293b83d2020-05-06 17:10:56 -0700888 mContext, mStackView, mBubbleIconFactory, false /* skipInflation */);
Mady Mellor7f234902019-10-20 12:06:29 -0700889 }
890
891 /**
892 * Called when a user has indicated that an active notification should be shown as a bubble.
893 * <p>
894 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
895 * the notification from appearing in the shade.
896 *
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700897 * @param entry the notification to change bubble state for.
898 * @param shouldBubble whether the notification should show as a bubble or not.
Mady Mellor7f234902019-10-20 12:06:29 -0700899 */
Pinyao Ting293b83d2020-05-06 17:10:56 -0700900 public void onUserChangedBubble(@Nullable final NotificationEntry entry, boolean shouldBubble) {
901 if (entry == null) {
902 return;
903 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700904 NotificationChannel channel = entry.getChannel();
905 final String appPkg = entry.getSbn().getPackageName();
906 final int appUid = entry.getSbn().getUid();
907 if (channel == null || appPkg == null) {
908 return;
Mady Mellorff076eb2019-11-13 10:12:06 -0800909 }
Mady Mellor7f234902019-10-20 12:06:29 -0700910
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700911 // Update the state in NotificationManagerService
912 try {
913 int flags = Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION;
914 mBarService.onNotificationBubbleChanged(entry.getKey(), shouldBubble, flags);
915 } catch (RemoteException e) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800916 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700917
918 // Change the settings
919 channel = NotificationChannelHelper.createConversationChannelIfNeeded(mContext,
920 mINotificationManager, entry, channel);
921 channel.setAllowBubbles(shouldBubble);
922 try {
923 int currentPref = mINotificationManager.getBubblePreferenceForPackage(appPkg, appUid);
924 if (shouldBubble && currentPref == BUBBLE_PREFERENCE_NONE) {
925 mINotificationManager.setBubblesAllowed(appPkg, appUid, BUBBLE_PREFERENCE_SELECTED);
926 }
927 mINotificationManager.updateNotificationChannelForPackage(appPkg, appUid, channel);
928 } catch (RemoteException e) {
929 Log.e(TAG, e.getMessage());
930 }
931
932 if (shouldBubble) {
933 mShadeController.collapsePanel(true);
934 if (entry.getRow() != null) {
935 entry.getRow().updateBubbleButton();
936 }
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800937 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800938 }
939
940 /**
Pinyao Ting293b83d2020-05-06 17:10:56 -0700941 * Removes the bubble with the given key.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500942 * <p>
943 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800944 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500945 @MainThread
Pinyao Ting293b83d2020-05-06 17:10:56 -0700946 void removeBubble(String key, int reason) {
947 if (mBubbleData.hasAnyBubbleWithKey(key)) {
948 mBubbleData.notificationEntryRemoved(key, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800949 }
950 }
951
Beverlyed8aea22020-01-22 16:52:47 -0500952 private void onEntryAdded(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000953 if (mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700954 && canLaunchInActivityView(mContext, entry)) {
Beverlyed8aea22020-01-22 16:52:47 -0500955 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700956 }
957 }
958
Beverlyed8aea22020-01-22 16:52:47 -0500959 private void onEntryUpdated(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000960 boolean shouldBubble = mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700961 && canLaunchInActivityView(mContext, entry);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700962 if (!shouldBubble && mBubbleData.hasAnyBubbleWithKey(entry.getKey())) {
Beverlyed8aea22020-01-22 16:52:47 -0500963 // It was previously a bubble but no longer a bubble -- lets remove it
Pinyao Ting293b83d2020-05-06 17:10:56 -0700964 removeBubble(entry.getKey(), DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500965 } else if (shouldBubble) {
Beverlyed8aea22020-01-22 16:52:47 -0500966 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800967 }
Beverlyed8aea22020-01-22 16:52:47 -0500968 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800969
Beverlya53fb0d2020-01-29 15:26:13 -0500970 private void onEntryRemoved(NotificationEntry entry) {
971 if (isSummaryOfBubbles(entry)) {
972 final String groupKey = entry.getSbn().getGroupKey();
973 mBubbleData.removeSuppressedSummary(groupKey);
974
975 // Remove any associated bubble children with the summary
976 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
977 for (int i = 0; i < bubbleChildren.size(); i++) {
Pinyao Ting293b83d2020-05-06 17:10:56 -0700978 removeBubble(bubbleChildren.get(i).getKey(), DISMISS_GROUP_CANCELLED);
Beverlya53fb0d2020-01-29 15:26:13 -0500979 }
980 } else {
Pinyao Ting293b83d2020-05-06 17:10:56 -0700981 removeBubble(entry.getKey(), DISMISS_NOTIF_CANCEL);
Beverlya53fb0d2020-01-29 15:26:13 -0500982 }
983 }
984
Mady Mellor56515c42020-02-18 17:58:36 -0800985 /**
986 * Called when NotificationListener has received adjusted notification rank and reapplied
987 * filtering and sorting. This is used to dismiss or create bubbles based on changes in
988 * permissions on the notification channel or the global setting.
989 *
990 * @param rankingMap the updated ranking map from NotificationListenerService
991 */
Beverlyed8aea22020-01-22 16:52:47 -0500992 private void onRankingUpdated(RankingMap rankingMap) {
Mady Mellor56515c42020-02-18 17:58:36 -0800993 if (mTmpRanking == null) {
994 mTmpRanking = new NotificationListenerService.Ranking();
995 }
996 String[] orderedKeys = rankingMap.getOrderedKeys();
997 for (int i = 0; i < orderedKeys.length; i++) {
998 String key = orderedKeys[i];
999 NotificationEntry entry = mNotificationEntryManager.getPendingOrActiveNotif(key);
1000 rankingMap.getRanking(key, mTmpRanking);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001001 boolean isActiveBubble = mBubbleData.hasAnyBubbleWithKey(key);
Mady Mellore45ff862020-03-24 15:54:50 -07001002 if (isActiveBubble && !mTmpRanking.canBubble()) {
Pinyao Ting293b83d2020-05-06 17:10:56 -07001003 mBubbleData.notificationEntryRemoved(entry.getKey(),
1004 BubbleController.DISMISS_BLOCKED);
Mady Mellore45ff862020-03-24 15:54:50 -07001005 } else if (entry != null && mTmpRanking.isBubble() && !isActiveBubble) {
Mady Mellor56515c42020-02-18 17:58:36 -08001006 entry.setFlagBubble(true);
1007 onEntryUpdated(entry);
1008 }
1009 }
Beverlyed8aea22020-01-22 16:52:47 -05001010 }
Ned Burns01e38212019-01-03 16:32:52 -05001011
Lyn Han2f6e89d2020-04-15 10:01:01 -07001012 private void setIsBubble(Bubble b, boolean isBubble) {
1013 if (isBubble) {
Pinyao Ting293b83d2020-05-06 17:10:56 -07001014 if (b.getEntry() != null) {
1015 b.getEntry().getSbn().getNotification().flags |= FLAG_BUBBLE;
1016 }
1017 b.enable(FLAG_BUBBLE);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001018 } else {
Pinyao Ting293b83d2020-05-06 17:10:56 -07001019 if (b.getEntry() != null) {
1020 b.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
1021 }
1022 b.disable(FLAG_BUBBLE);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001023 }
1024 try {
1025 mBarService.onNotificationBubbleChanged(b.getKey(), isBubble, 0);
1026 } catch (RemoteException e) {
1027 // Bad things have happened
1028 }
1029 }
1030
Mark Renouf71a3af62019-04-08 15:02:54 -04001031 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -04001032 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -04001033
Mark Renouf3bc5b362019-04-05 14:37:59 -04001034 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -04001035 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001036 // Update bubbles in overflow.
Mady Mellor1d082022020-05-12 16:35:39 +00001037 if (mOverflowCallback != null) {
1038 mOverflowCallback.run();
Lyn Hanb58c7562020-01-07 14:29:20 -08001039 }
1040
Mark Renouf82a40e62019-05-23 16:16:24 -04001041 // Collapsing? Do this first before remaining steps.
1042 if (update.expandedChanged && !update.expanded) {
1043 mStackView.setExpanded(false);
1044 }
1045
1046 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -07001047 ArrayList<Pair<Bubble, Integer>> removedBubbles =
1048 new ArrayList<>(update.removedBubbles);
Pinyao Tingee191b12020-04-29 18:35:39 -07001049 ArrayList<Bubble> bubblesToBeRemovedFromRepository = new ArrayList<>();
Mady Mellor22f2f072019-04-18 13:26:18 -07001050 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -04001051 final Bubble bubble = removed.first;
1052 @DismissReason final int reason = removed.second;
1053 mStackView.removeBubble(bubble);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001054
Mark Renoufc19b4732019-06-26 12:08:33 -04001055 // If the bubble is removed for user switching, leave the notification in place.
Lyn Han2f6e89d2020-04-15 10:01:01 -07001056 if (reason == DISMISS_USER_CHANGED) {
1057 continue;
1058 }
Pinyao Tingee191b12020-04-29 18:35:39 -07001059 if (reason == DISMISS_NOTIF_CANCEL) {
1060 bubblesToBeRemovedFromRepository.add(bubble);
1061 }
Lyn Han2f6e89d2020-04-15 10:01:01 -07001062 if (!mBubbleData.hasBubbleInStackWithKey(bubble.getKey())) {
1063 if (!mBubbleData.hasOverflowBubbleWithKey(bubble.getKey())
1064 && (!bubble.showInShade()
1065 || reason == DISMISS_NOTIF_CANCEL
Mady Mellor1d082022020-05-12 16:35:39 +00001066 || reason == DISMISS_GROUP_CANCELLED)) {
Beverlyed8aea22020-01-22 16:52:47 -05001067 // The bubble is now gone & the notification is hidden from the shade, so
1068 // time to actually remove it
1069 for (NotifCallback cb : mCallbacks) {
Pinyao Ting293b83d2020-05-06 17:10:56 -07001070 if (bubble.getEntry() != null) {
1071 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
1072 }
Beverlyed8aea22020-01-22 16:52:47 -05001073 }
Mark Renoufc19b4732019-06-26 12:08:33 -04001074 } else {
Pinyao Ting293b83d2020-05-06 17:10:56 -07001075 if (bubble.isBubble() && bubble.showInShade()) {
Lyn Han2f6e89d2020-04-15 10:01:01 -07001076 setIsBubble(bubble, /* isBubble */ false);
1077 }
Pinyao Ting293b83d2020-05-06 17:10:56 -07001078 if (bubble.getEntry() != null && bubble.getEntry().getRow() != null) {
Mady Mellor9adfe6a2020-03-30 17:23:26 -07001079 bubble.getEntry().getRow().updateBubbleButton();
1080 }
Mark Renouf82a40e62019-05-23 16:16:24 -04001081 }
Mady Mellor22f2f072019-04-18 13:26:18 -07001082
Lyn Han2f6e89d2020-04-15 10:01:01 -07001083 }
Pinyao Ting293b83d2020-05-06 17:10:56 -07001084 if (bubble.getEntry() != null) {
1085 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
1086 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
1087 // Time to potentially remove the summary
1088 for (NotifCallback cb : mCallbacks) {
1089 cb.maybeCancelSummary(bubble.getEntry());
1090 }
Mady Mellor22f2f072019-04-18 13:26:18 -07001091 }
Mady Mellora54e9fa2019-04-18 13:26:18 -07001092 }
1093 }
Pinyao Tingee191b12020-04-29 18:35:39 -07001094 mDataRepository.removeBubbles(mCurrentUserId, bubblesToBeRemovedFromRepository);
Mark Renouf3bc5b362019-04-05 14:37:59 -04001095
Lyn Hanc47e1712020-01-28 21:43:34 -08001096 if (update.addedBubble != null) {
Pinyao Tingee191b12020-04-29 18:35:39 -07001097 mDataRepository.addBubble(mCurrentUserId, update.addedBubble);
Lyn Hanc47e1712020-01-28 21:43:34 -08001098 mStackView.addBubble(update.addedBubble);
Pinyao Tingee191b12020-04-29 18:35:39 -07001099
Lyn Hanc47e1712020-01-28 21:43:34 -08001100 }
1101
Mark Renouf82a40e62019-05-23 16:16:24 -04001102 if (update.updatedBubble != null) {
1103 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -04001104 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001105
Lyn Hanb58c7562020-01-07 14:29:20 -08001106 // At this point, the correct bubbles are inflated in the stack.
1107 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -04001108 if (update.orderChanged) {
Pinyao Tingee191b12020-04-29 18:35:39 -07001109 mDataRepository.addBubbles(mCurrentUserId, update.bubbles);
Mark Renouf82a40e62019-05-23 16:16:24 -04001110 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -04001111 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001112
Mark Renouf82a40e62019-05-23 16:16:24 -04001113 if (update.selectionChanged) {
1114 mStackView.setSelectedBubble(update.selectedBubble);
Pinyao Ting293b83d2020-05-06 17:10:56 -07001115 if (update.selectedBubble != null && update.selectedBubble.getEntry() != null) {
Mady Mellor740d85d2019-07-09 15:26:47 -07001116 mNotificationGroupManager.updateSuppression(
1117 update.selectedBubble.getEntry());
1118 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001119 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001120
Mark Renouf82a40e62019-05-23 16:16:24 -04001121 // Expanding? Apply this last.
1122 if (update.expandedChanged && update.expanded) {
1123 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001124 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001125
Beverlyed8aea22020-01-22 16:52:47 -05001126 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001127 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001128 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001129 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001130
Issei Suzukia8d07312019-06-07 12:56:19 +02001131 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001132 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001133 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001134 mBubbleData.getSelectedBubble()));
1135
1136 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001137 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001138 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001139 mStackView.getExpandedBubble()));
1140 }
Mady Mellor1d082022020-05-12 16:35:39 +00001141 Log.d(TAG, "\n[BubbleData] overflow:");
1142 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
1143 null) + "\n");
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001144 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001145 }
1146 };
1147
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001148 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001149 * We intercept notification entries (including group summaries) dismissed by the user when
1150 * there is an active bubble associated with it. We do this so that developers can still
1151 * cancel it (and hence the bubbles associated with it). However, these intercepted
1152 * notifications should then be hidden from the shade since the user has cancelled them, so we
1153 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1154 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001155 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001156 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001157 */
Beverlya53fb0d2020-01-29 15:26:13 -05001158 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001159 if (entry == null) {
1160 return false;
1161 }
Lyn Han2f6e89d2020-04-15 10:01:01 -07001162 if (isSummaryOfBubbles(entry)) {
Beverlya53fb0d2020-01-29 15:26:13 -05001163 handleSummaryDismissalInterception(entry);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001164 } else {
1165 Bubble bubble = mBubbleData.getBubbleInStackWithKey(entry.getKey());
1166 if (bubble == null || !entry.isBubble()) {
1167 bubble = mBubbleData.getOverflowBubbleWithKey(entry.getKey());
1168 }
1169 if (bubble == null) {
1170 return false;
1171 }
Beverlyed8aea22020-01-22 16:52:47 -05001172 bubble.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001173 bubble.setShowDot(false /* show */);
Beverlyed8aea22020-01-22 16:52:47 -05001174 }
Beverlya53fb0d2020-01-29 15:26:13 -05001175 // Update the shade
1176 for (NotifCallback cb : mCallbacks) {
1177 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1178 }
1179 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001180 }
1181
Beverlya53fb0d2020-01-29 15:26:13 -05001182 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1183 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001184 return false;
1185 }
Beverlya53fb0d2020-01-29 15:26:13 -05001186
1187 String groupKey = entry.getSbn().getGroupKey();
1188 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1189 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1190 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1191 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1192 return (isSuppressedSummary || isSummary)
1193 && bubbleChildren != null
1194 && !bubbleChildren.isEmpty();
1195 }
1196
1197 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1198 // current children in the row:
Kevin Han43077f92020-02-28 12:51:53 -08001199 final List<NotificationEntry> children = summary.getAttachedNotifChildren();
Beverlya53fb0d2020-01-29 15:26:13 -05001200 if (children != null) {
1201 for (int i = 0; i < children.size(); i++) {
1202 NotificationEntry child = children.get(i);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001203 if (mBubbleData.hasAnyBubbleWithKey(child.getKey())) {
Beverlya53fb0d2020-01-29 15:26:13 -05001204 // Suppress the bubbled child
1205 // As far as group manager is concerned, once a child is no longer shown
1206 // in the shade, it is essentially removed.
Lyn Han2f6e89d2020-04-15 10:01:01 -07001207 Bubble bubbleChild = mBubbleData.getAnyBubbleWithkey(child.getKey());
Beverlya53fb0d2020-01-29 15:26:13 -05001208 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1209 bubbleChild.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001210 bubbleChild.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001211 } else {
1212 // non-bubbled children can be removed
1213 for (NotifCallback cb : mCallbacks) {
1214 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1215 }
1216 }
1217 }
1218 }
1219
1220 // And since all children are removed, remove the summary.
1221 mNotificationGroupManager.onEntryRemoved(summary);
1222
1223 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1224 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1225 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001226 }
1227
1228 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001229 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001230 * Updates the visibility of the bubbles based on current state.
Joshua Tsujid9923e52020-04-29 15:33:01 -04001231 * Does not un-bubble, just hides or un-hides.
Lyn Han6c40fe72019-05-08 14:06:33 -07001232 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001233 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001234 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001235 if (mStackView == null) {
1236 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001237 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001238 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1239 // Bubbles only appear in unlocked shade
1240 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001241 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001242 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001243 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001244
Lyn Han6c40fe72019-05-08 14:06:33 -07001245 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001246 }
1247
1248 /**
Mady Mellor390bff42019-04-05 15:09:01 -07001249 * The display id of the expanded view, if the stack is expanded and not occluded by the
1250 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1251 */
1252 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001253 if (mStackView == null) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001254 return INVALID_DISPLAY;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001255 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001256 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001257 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Lyn Han9f66c3b2020-03-05 23:59:29 -08001258 final BubbleViewProvider expandedViewProvider = mStackView.getExpandedBubble();
1259 if (defaultDisplay && expandedViewProvider != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001260 && !mNotificationShadeWindowController.getPanelExpanded()) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001261 return expandedViewProvider.getDisplayId();
Mady Mellor390bff42019-04-05 15:09:01 -07001262 }
Lyn Han9f66c3b2020-03-05 23:59:29 -08001263 return INVALID_DISPLAY;
Mady Mellor390bff42019-04-05 15:09:01 -07001264 }
1265
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001266 @VisibleForTesting
1267 BubbleStackView getStackView() {
1268 return mStackView;
1269 }
1270
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001271 /**
1272 * Description of current bubble state.
1273 */
1274 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1275 pw.println("BubbleController state:");
1276 mBubbleData.dump(fd, pw, args);
1277 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001278 if (mStackView != null) {
1279 mStackView.dump(fd, pw, args);
1280 }
1281 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001282 }
1283
Mady Mellore80930e2019-03-21 16:00:45 -07001284 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001285 * This task stack listener is responsible for responding to tasks moved to the front
1286 * which are on the default (main) display. When this happens, expanded bubbles must be
1287 * collapsed so the user may interact with the app which was just moved to the front.
1288 * <p>
1289 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1290 * these calls via a main thread Handler.
1291 */
1292 @MainThread
1293 private class BubbleTaskStackListener extends TaskStackChangeListener {
1294
Mark Renoufcecc77b2019-01-30 16:32:24 -05001295 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001296 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1297 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001298 if (!mStackView.isExpansionAnimating()) {
1299 mBubbleData.setExpanded(false);
1300 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001301 }
1302 }
1303
Mark Renoufcecc77b2019-01-30 16:32:24 -05001304 @Override
Winson Chunge789ff62020-02-24 14:40:23 -08001305 public void onActivityRestartAttempt(RunningTaskInfo task, boolean homeTaskVisible,
Evan Rosky8d1c24e2020-04-23 09:21:16 -07001306 boolean clearedTask, boolean wasVisible) {
Winson Chunge789ff62020-02-24 14:40:23 -08001307 for (Bubble b : mBubbleData.getBubbles()) {
1308 if (b.getDisplayId() == task.displayId) {
1309 expandStackAndSelectBubble(b.getKey());
1310 return;
1311 }
1312 }
1313 }
1314
1315 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001316 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001317 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001318 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001319 }
1320 }
Mark Renouf446251d2019-04-26 10:22:41 -04001321
1322 @Override
1323 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1324 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1325 mBubbleData.setExpanded(false);
1326 }
1327 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001328
1329 @Override
1330 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001331 if (mStackView == null) {
1332 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001333 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001334 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001335 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001336
1337 @Override
1338 public void onSingleTaskDisplayEmpty(int displayId) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001339 final BubbleViewProvider expandedBubble = mStackView != null
Mady Mellor5186b132019-09-16 17:55:48 -07001340 ? mStackView.getExpandedBubble()
1341 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001342 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1343 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001344 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001345 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001346 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001347 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001348 }
1349
Mady Mellorca0c24c2019-05-16 16:14:32 -07001350 /**
1351 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1352 *
1353 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1354 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1355 *
1356 * @param context the context to use.
1357 * @param entry the entry to bubble.
1358 */
1359 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1360 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Melloraa9ce172020-03-17 10:34:20 -07001361 ? entry.getBubbleMetadata().getIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001362 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001363 if (entry.getBubbleMetadata() != null
1364 && entry.getBubbleMetadata().getShortcutId() != null) {
1365 return true;
1366 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001367 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001368 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001369 return false;
1370 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001371 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1372 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001373 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001374 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001375 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001376 Log.w(TAG, "Unable to send as bubble, "
1377 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001378 + intent);
1379 return false;
1380 }
1381 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001382 Log.w(TAG, "Unable to send as bubble, "
1383 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001384 + intent);
1385 return false;
1386 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001387 return true;
1388 }
1389
Joshua Tsujia19515f2019-02-13 18:02:29 -05001390 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001391 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001392 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001393 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Joshua Tsujiff6b0f22020-03-09 14:55:19 -04001394 if (mStackView != null) {
Joshua Tsujid9422832019-03-05 13:32:37 -05001395 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001396 }
1397 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001398 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001399}