blob: ed82082760ee3658cfa81fbf0c6b0ad8104e82b1 [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
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;
Joshua Tsujid9923e52020-04-29 15:33:01 -040056import android.graphics.PixelFormat;
Mady Mellord1c78b262018-11-06 18:04:40 -080057import android.graphics.Rect;
Joshua Tsujid9923e52020-04-29 15:33:01 -040058import android.os.Binder;
Lyn Han6cb4e5f2020-04-27 15:11:18 -070059import android.os.Handler;
Mark Renoufcecc77b2019-01-30 16:32:24 -050060import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080061import android.os.ServiceManager;
Mady Mellor56515c42020-02-18 17:58:36 -080062import android.service.notification.NotificationListenerService;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040063import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040064import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040065import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040066import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040067import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040068import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050069import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080070import android.view.ViewGroup;
Joshua Tsujid9923e52020-04-29 15:33:01 -040071import android.view.WindowManager;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080072
Mark Renouf08bc42a2019-03-07 13:01:59 -050073import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050074import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040075import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050076
Mady Mellorebdbbb92018-11-15 14:36:48 -080077import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070078import com.android.internal.statusbar.IStatusBarService;
Beverlya53fb0d2020-01-29 15:26:13 -050079import com.android.internal.statusbar.NotificationVisibility;
Beverlya53fb0d2020-01-29 15:26:13 -050080import com.android.systemui.Dumpable;
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +010081import com.android.systemui.bubbles.dagger.BubbleModule;
Ned Burnsaaeb44b2020-02-12 23:48:26 -050082import com.android.systemui.dump.DumpManager;
Joshua Tsujibe60a582020-03-23 17:17:26 -040083import com.android.systemui.model.SysUiState;
Beverly8fdb5332019-02-04 14:29:49 -050084import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050085import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000086import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050087import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050088import com.android.systemui.shared.system.WindowManagerWrapper;
Beverlya53fb0d2020-01-29 15:26:13 -050089import com.android.systemui.statusbar.FeatureFlags;
Mark Renoufc19b4732019-06-26 12:08:33 -040090import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070091import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Joshua Tsujid9923e52020-04-29 15:33:01 -040092import com.android.systemui.statusbar.ScrimView;
Mady Mellor9adfe6a2020-03-30 17:23:26 -070093import com.android.systemui.statusbar.notification.NotificationChannelHelper;
Ned Burns01e38212019-01-03 16:32:52 -050094import com.android.systemui.statusbar.notification.NotificationEntryListener;
95import com.android.systemui.statusbar.notification.NotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -050096import com.android.systemui.statusbar.notification.collection.NotifCollection;
97import com.android.systemui.statusbar.notification.collection.NotifPipeline;
Ned Burnsf81c4c42019-01-07 14:10:43 -050098import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Beverlya53fb0d2020-01-29 15:26:13 -050099import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener;
Beverly Taid1e175c2020-03-10 16:37:04 +0000100import com.android.systemui.statusbar.notification.interruption.NotificationInterruptStateProvider;
Mady Mellor22f2f072019-04-18 13:26:18 -0700101import com.android.systemui.statusbar.phone.NotificationGroupManager;
wilsonshihe8321942019-10-18 18:39:46 +0800102import com.android.systemui.statusbar.phone.NotificationShadeWindowController;
Joshua Tsujid9923e52020-04-29 15:33:01 -0400103import com.android.systemui.statusbar.phone.ScrimController;
Mady Mellor7f234902019-10-20 12:06:29 -0700104import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorf3b9fab2019-11-13 17:27:32 -0800105import com.android.systemui.statusbar.phone.StatusBar;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700106import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400107import com.android.systemui.statusbar.policy.ZenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500108import com.android.systemui.util.FloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800109
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700110import java.io.FileDescriptor;
111import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500112import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -0400113import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -0700114import java.util.ArrayList;
Lyn Hanb58c7562020-01-07 14:29:20 -0800115import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500116
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800117/**
118 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
119 * Bubbles can be expanded to show more content.
120 *
121 * The controller manages addition, removal, and visible state of bubbles on screen.
122 */
Beverlya53fb0d2020-01-29 15:26:13 -0500123public class BubbleController implements ConfigurationController.ConfigurationListener, Dumpable {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800124
Issei Suzukia8d07312019-06-07 12:56:19 +0200125 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800126
Mark Renouf08bc42a2019-03-07 13:01:59 -0500127 @Retention(SOURCE)
128 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400129 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Lyn Han2f6e89d2020-04-15 10:01:01 -0700130 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT,
131 DISMISS_OVERFLOW_MAX_REACHED})
Mark Renoufba5ab512019-05-02 15:21:01 -0400132 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500133 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700134
Mark Renouf08bc42a2019-03-07 13:01:59 -0500135 static final int DISMISS_USER_GESTURE = 1;
136 static final int DISMISS_AGED = 2;
137 static final int DISMISS_TASK_FINISHED = 3;
138 static final int DISMISS_BLOCKED = 4;
139 static final int DISMISS_NOTIF_CANCEL = 5;
140 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700141 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400142 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700143 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700144 static final int DISMISS_INVALID_INTENT = 10;
Lyn Han2f6e89d2020-04-15 10:01:01 -0700145 static final int DISMISS_OVERFLOW_MAX_REACHED = 11;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500146
Ned Burns01e38212019-01-03 16:32:52 -0500147 private final Context mContext;
148 private final NotificationEntryManager mNotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500149 private final NotifPipeline mNotifPipeline;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500150 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800151 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100152 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700153 private final NotificationGroupManager mNotificationGroupManager;
Heemin Seogba6337f2019-12-10 15:34:37 -0800154 private final ShadeController mShadeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500155 private final FloatingContentCoordinator mFloatingContentCoordinator;
Pinyao Tingee191b12020-04-29 18:35:39 -0700156 private final BubbleDataRepository mDataRepository;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800157
Mady Mellor3dff9e62019-02-05 18:12:53 -0800158 private BubbleData mBubbleData;
Joshua Tsujid9923e52020-04-29 15:33:01 -0400159 private ScrimView mBubbleScrim;
Joshua Tsujic650a142019-05-22 11:31:19 -0400160 @Nullable private BubbleStackView mStackView;
Mady Mellor247ca2c2019-12-02 16:18:59 -0800161 private BubbleIconFactory mBubbleIconFactory;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800162
Mark Renoufc19b4732019-06-26 12:08:33 -0400163 // Tracks the id of the current (foreground) user.
164 private int mCurrentUserId;
165 // Saves notification keys of active bubbles when users are switched.
166 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
167
Mady Mellor56515c42020-02-18 17:58:36 -0800168 // Used when ranking updates occur and we check if things should bubble / unbubble
169 private NotificationListenerService.Ranking mTmpRanking;
170
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800171 // Bubbles get added to the status bar view
wilsonshihe8321942019-10-18 18:39:46 +0800172 private final NotificationShadeWindowController mNotificationShadeWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400173 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800174 private StatusBarStateListener mStatusBarStateListener;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700175 private INotificationManager mINotificationManager;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500176
Lyn Hanb58c7562020-01-07 14:29:20 -0800177 // Callback that updates BubbleOverflowActivity on data change.
Lyn Hane4274be2020-04-24 17:55:36 -0700178 @Nullable private BubbleData.Listener mOverflowListener = null;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800179
Beverly Taid1e175c2020-03-10 16:37:04 +0000180 private final NotificationInterruptStateProvider mNotificationInterruptStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700181 private IStatusBarService mBarService;
Joshua Tsujid9923e52020-04-29 15:33:01 -0400182 private WindowManager mWindowManager;
Joshua Tsujibe60a582020-03-23 17:17:26 -0400183 private SysUiState mSysUiState;
Mady Mellorb4991e62019-01-10 15:14:51 -0800184
Lyn Han6cb4e5f2020-04-27 15:11:18 -0700185 // Used to post to main UI thread
186 private Handler mHandler = new Handler();
187
Joshua Tsujid9923e52020-04-29 15:33:01 -0400188 /** LayoutParams used to add the BubbleStackView to the window maanger. */
189 private WindowManager.LayoutParams mWmLayoutParams;
190
191
Mady Mellord1c78b262018-11-06 18:04:40 -0800192 // Used for determining view rect for touch interaction
193 private Rect mTempRect = new Rect();
194
Mark Renoufc19b4732019-06-26 12:08:33 -0400195 // Listens to user switch so bubbles can be saved and restored.
196 private final NotificationLockscreenUserManager mNotifUserManager;
197
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400198 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
199 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
200
Mady Mellor3df7ab02019-12-09 15:07:10 -0800201 private boolean mInflateSynchronously;
202
Beverlyed8aea22020-01-22 16:52:47 -0500203 // TODO (b/145659174): allow for multiple callbacks to support the "shadow" new notif pipeline
204 private final List<NotifCallback> mCallbacks = new ArrayList<>();
205
Mady Mellor5549dd22018-11-06 18:07:34 -0800206 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800207 * Listener to find out about stack expansion / collapse events.
208 */
209 public interface BubbleExpandListener {
210 /**
211 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700212 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800213 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800214 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800215 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800216 void onBubbleExpandChanged(boolean isExpanding, String key);
217 }
218
219 /**
Mady Mellorf44b6832020-01-14 13:26:14 -0800220 * Listener to be notified when a bubbles' notification suppression state changes.
221 */
222 public interface NotificationSuppressionChangedListener {
223 /**
224 * Called when the notification suppression state of a bubble changes.
225 */
226 void onBubbleNotificationSuppressionChange(Bubble bubble);
Beverlyed8aea22020-01-22 16:52:47 -0500227 }
Mady Mellorf44b6832020-01-14 13:26:14 -0800228
Beverlyed8aea22020-01-22 16:52:47 -0500229 /**
230 * Callback for when the BubbleController wants to interact with the notification pipeline to:
231 * - Remove a previously bubbled notification
232 * - Update the notification shade since bubbled notification should/shouldn't be showing
233 */
234 public interface NotifCallback {
235 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500236 * Called when a bubbled notification that was hidden from the shade is now being removed
237 * This can happen when an app cancels a bubbled notification or when the user dismisses a
238 * bubble.
Beverlyed8aea22020-01-22 16:52:47 -0500239 */
Beverlya53fb0d2020-01-29 15:26:13 -0500240 void removeNotification(NotificationEntry entry, int reason);
Beverlyed8aea22020-01-22 16:52:47 -0500241
242 /**
243 * Called when a bubbled notification has changed whether it should be
244 * filtered from the shade.
245 */
Beverlya53fb0d2020-01-29 15:26:13 -0500246 void invalidateNotifications(String reason);
Beverlyed8aea22020-01-22 16:52:47 -0500247
248 /**
249 * Called on a bubbled entry that has been removed when there are no longer
250 * bubbled entries in its group.
251 *
252 * Checks whether its group has any other (non-bubbled) children. If it doesn't,
253 * removes all remnants of the group's summary from the notification pipeline.
254 * TODO: (b/145659174) Only old pipeline needs this - delete post-migration.
255 */
256 void maybeCancelSummary(NotificationEntry entry);
Mady Mellorf44b6832020-01-14 13:26:14 -0800257 }
258
259 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800260 * Listens for the current state of the status bar and updates the visibility state
261 * of bubbles as needed.
262 */
263 private class StatusBarStateListener implements StatusBarStateController.StateListener {
264 private int mState;
265 /**
266 * Returns the current status bar state.
267 */
268 public int getCurrentState() {
269 return mState;
270 }
271
272 @Override
273 public void onStateChanged(int newState) {
274 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400275 boolean shouldCollapse = (mState != SHADE);
276 if (shouldCollapse) {
277 collapseStack();
278 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700279 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800280 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800281 }
282
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +0100283 /**
284 * Injected constructor. See {@link BubbleModule}.
285 */
Mady Mellor7f234902019-10-20 12:06:29 -0700286 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800287 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700288 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800289 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700290 BubbleData data,
291 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
292 ConfigurationController configurationController,
Beverly Taid1e175c2020-03-10 16:37:04 +0000293 NotificationInterruptStateProvider interruptionStateProvider,
Mady Mellor7f234902019-10-20 12:06:29 -0700294 ZenModeController zenModeController,
295 NotificationLockscreenUserManager notifUserManager,
296 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500297 NotificationEntryManager entryManager,
298 NotifPipeline notifPipeline,
299 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500300 DumpManager dumpManager,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400301 FloatingContentCoordinator floatingContentCoordinator,
Pinyao Tingee191b12020-04-29 18:35:39 -0700302 BubbleDataRepository dataRepository,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700303 SysUiState sysUiState,
Mady Mellor59a7b982020-05-11 15:19:59 -0700304 INotificationManager notificationManager,
305 WindowManager windowManager) {
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500306 dumpManager.registerDumpable(TAG, this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800307 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800308 mShadeController = shadeController;
Beverly Taid1e175c2020-03-10 16:37:04 +0000309 mNotificationInterruptStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400310 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400311 mZenModeController = zenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500312 mFloatingContentCoordinator = floatingContentCoordinator;
Pinyao Tingee191b12020-04-29 18:35:39 -0700313 mDataRepository = dataRepository;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700314 mINotificationManager = notificationManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400315 mZenModeController.addCallback(new ZenModeController.Callback() {
316 @Override
317 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800318 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400319 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700320 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400321 }
322
323 @Override
324 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800325 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400326 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700327 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400328 }
329 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700330
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700331 configurationController.addCallback(this /* configurationListener */);
Joshua Tsujibe60a582020-03-23 17:17:26 -0400332 mSysUiState = sysUiState;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800333
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400334 mBubbleData = data;
335 mBubbleData.setListener(mBubbleDataListener);
Mady Mellorf44b6832020-01-14 13:26:14 -0800336 mBubbleData.setSuppressionChangedListener(new NotificationSuppressionChangedListener() {
337 @Override
338 public void onBubbleNotificationSuppressionChange(Bubble bubble) {
339 // Make sure NoMan knows it's not showing in the shade anymore so anyone querying it
340 // can tell.
341 try {
342 mBarService.onBubbleNotificationSuppressionChanged(bubble.getKey(),
343 !bubble.showInShade());
344 } catch (RemoteException e) {
345 // Bad things have happened
346 }
347 }
348 });
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400349
Mady Mellor7f234902019-10-20 12:06:29 -0700350 mNotificationEntryManager = entryManager;
Mady Mellor22f2f072019-04-18 13:26:18 -0700351 mNotificationGroupManager = groupManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500352 mNotifPipeline = notifPipeline;
353
354 if (!featureFlags.isNewNotifPipelineRenderingEnabled()) {
355 setupNEM();
356 } else {
357 setupNotifPipeline();
358 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800359
wilsonshihe8321942019-10-18 18:39:46 +0800360 mNotificationShadeWindowController = notificationShadeWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800361 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700362 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500363
Mark Renoufcecc77b2019-01-30 16:32:24 -0500364 mTaskStackListener = new BubbleTaskStackListener();
365 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800366
Joshua Tsujia19515f2019-02-13 18:02:29 -0500367 try {
368 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
369 } catch (RemoteException e) {
370 e.printStackTrace();
371 }
Issei Suzukic0387542019-03-08 17:31:14 +0100372 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700373
Mady Mellor59a7b982020-05-11 15:19:59 -0700374 mWindowManager = windowManager;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700375 mBarService = IStatusBarService.Stub.asInterface(
376 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400377
Joshua Tsujid9923e52020-04-29 15:33:01 -0400378 mBubbleScrim = new ScrimView(mContext);
379
Mark Renoufc19b4732019-06-26 12:08:33 -0400380 mSavedBubbleKeysPerUser = new SparseSetArray<>();
381 mCurrentUserId = mNotifUserManager.getCurrentUserId();
382 mNotifUserManager.addUserChangedListener(
Steve Elliottb47f1c72019-12-19 12:39:26 -0500383 new NotificationLockscreenUserManager.UserChangedListener() {
384 @Override
385 public void onUserChanged(int newUserId) {
386 BubbleController.this.saveBubbles(mCurrentUserId);
387 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
388 BubbleController.this.restoreBubbles(newUserId);
389 mCurrentUserId = newUserId;
390 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400391 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800392
Mady Mellor247ca2c2019-12-02 16:18:59 -0800393 mBubbleIconFactory = new BubbleIconFactory(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800394 }
395
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400396 /**
Beverlyed8aea22020-01-22 16:52:47 -0500397 * See {@link NotifCallback}.
398 */
399 public void addNotifCallback(NotifCallback callback) {
400 mCallbacks.add(callback);
401 }
402
403 private void setupNEM() {
404 mNotificationEntryManager.addNotificationEntryListener(
405 new NotificationEntryListener() {
406 @Override
Mady Mellorf9439ab2020-01-30 16:06:53 -0800407 public void onPendingEntryAdded(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -0500408 onEntryAdded(entry);
409 }
410
411 @Override
412 public void onPreEntryUpdated(NotificationEntry entry) {
413 onEntryUpdated(entry);
414 }
415
416 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500417 public void onEntryRemoved(
418 NotificationEntry entry,
419 @android.annotation.Nullable NotificationVisibility visibility,
Julia Reynolds138111f2020-02-26 11:17:39 -0500420 boolean removedByUser,
421 int reason) {
Beverlya53fb0d2020-01-29 15:26:13 -0500422 BubbleController.this.onEntryRemoved(entry);
423 }
424
425 @Override
Beverlyed8aea22020-01-22 16:52:47 -0500426 public void onNotificationRankingUpdated(RankingMap rankingMap) {
427 onRankingUpdated(rankingMap);
428 }
429 });
430
Evan Laird04373662020-01-24 17:37:39 -0500431 mNotificationEntryManager.addNotificationRemoveInterceptor(
Beverlyed8aea22020-01-22 16:52:47 -0500432 new NotificationRemoveInterceptor() {
433 @Override
Evan Laird04373662020-01-24 17:37:39 -0500434 public boolean onNotificationRemoveRequested(
Beverlya53fb0d2020-01-29 15:26:13 -0500435 String key,
436 NotificationEntry entry,
437 int dismissReason) {
438 final boolean isClearAll = dismissReason == REASON_CANCEL_ALL;
439 final boolean isUserDimiss = dismissReason == REASON_CANCEL
440 || dismissReason == REASON_CLICK;
441 final boolean isAppCancel = dismissReason == REASON_APP_CANCEL
442 || dismissReason == REASON_APP_CANCEL_ALL;
443 final boolean isSummaryCancel =
444 dismissReason == REASON_GROUP_SUMMARY_CANCELED;
445
446 // Need to check for !appCancel here because the notification may have
447 // previously been dismissed & entry.isRowDismissed would still be true
448 boolean userRemovedNotif =
449 (entry != null && entry.isRowDismissed() && !isAppCancel)
450 || isClearAll || isUserDimiss || isSummaryCancel;
451
Mady Mellordd6fe612020-04-15 11:47:55 -0700452 if (userRemovedNotif) {
Beverlya53fb0d2020-01-29 15:26:13 -0500453 return handleDismissalInterception(entry);
454 }
Beverlya53fb0d2020-01-29 15:26:13 -0500455 return false;
Beverlyed8aea22020-01-22 16:52:47 -0500456 }
457 });
458
459 mNotificationGroupManager.addOnGroupChangeListener(
460 new NotificationGroupManager.OnGroupChangeListener() {
461 @Override
462 public void onGroupSuppressionChanged(
463 NotificationGroupManager.NotificationGroup group,
464 boolean suppressed) {
465 // More notifications could be added causing summary to no longer
466 // be suppressed -- in this case need to remove the key.
467 final String groupKey = group.summary != null
468 ? group.summary.getSbn().getGroupKey()
469 : null;
470 if (!suppressed && groupKey != null
471 && mBubbleData.isSummarySuppressed(groupKey)) {
472 mBubbleData.removeSuppressedSummary(groupKey);
473 }
474 }
475 });
476
477 addNotifCallback(new NotifCallback() {
478 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500479 public void removeNotification(NotificationEntry entry, int reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500480 mNotificationEntryManager.performRemoveNotification(entry.getSbn(),
Beverlya53fb0d2020-01-29 15:26:13 -0500481 reason);
Beverlyed8aea22020-01-22 16:52:47 -0500482 }
483
484 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500485 public void invalidateNotifications(String reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500486 mNotificationEntryManager.updateNotifications(reason);
487 }
488
489 @Override
490 public void maybeCancelSummary(NotificationEntry entry) {
491 // Check if removed bubble has an associated suppressed group summary that needs
492 // to be removed now.
Beverlya53fb0d2020-01-29 15:26:13 -0500493 final String groupKey = entry.getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500494 if (mBubbleData.isSummarySuppressed(groupKey)) {
Beverlya53fb0d2020-01-29 15:26:13 -0500495 mBubbleData.removeSuppressedSummary(groupKey);
Beverlyed8aea22020-01-22 16:52:47 -0500496
497 final NotificationEntry summary =
498 mNotificationEntryManager.getActiveNotificationUnfiltered(
499 mBubbleData.getSummaryKey(groupKey));
Beverlya53fb0d2020-01-29 15:26:13 -0500500 if (summary != null) {
501 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
502 UNDEFINED_DISMISS_REASON);
503 }
Beverlyed8aea22020-01-22 16:52:47 -0500504 }
505
Beverlya53fb0d2020-01-29 15:26:13 -0500506 // Check if we still need to remove the summary from NoManGroup because the summary
507 // may not be in the mBubbleData.mSuppressedGroupKeys list and removed above.
508 // For example:
509 // 1. Bubbled notifications (group) is posted to shade and are visible bubbles
510 // 2. User expands bubbles so now their respective notifications in the shade are
511 // hidden, including the group summary
512 // 3. User removes all bubbles
513 // 4. We expect all the removed bubbles AND the summary (note: the summary was
514 // never added to the suppressedSummary list in BubbleData, so we add this check)
Beverlyed8aea22020-01-22 16:52:47 -0500515 NotificationEntry summary =
516 mNotificationGroupManager.getLogicalGroupSummary(entry.getSbn());
517 if (summary != null) {
518 ArrayList<NotificationEntry> summaryChildren =
519 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
520 boolean isSummaryThisNotif = summary.getKey().equals(entry.getKey());
521 if (!isSummaryThisNotif && (summaryChildren == null
522 || summaryChildren.isEmpty())) {
523 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
524 UNDEFINED_DISMISS_REASON);
525 }
526 }
527 }
528 });
529 }
530
Beverlya53fb0d2020-01-29 15:26:13 -0500531 private void setupNotifPipeline() {
532 mNotifPipeline.addCollectionListener(new NotifCollectionListener() {
533 @Override
534 public void onEntryAdded(NotificationEntry entry) {
535 BubbleController.this.onEntryAdded(entry);
536 }
537
538 @Override
539 public void onEntryUpdated(NotificationEntry entry) {
540 BubbleController.this.onEntryUpdated(entry);
541 }
542
543 @Override
544 public void onRankingUpdate(RankingMap rankingMap) {
545 onRankingUpdated(rankingMap);
546 }
547
548 @Override
549 public void onEntryRemoved(NotificationEntry entry,
550 @NotifCollection.CancellationReason int reason) {
551 BubbleController.this.onEntryRemoved(entry);
552 }
553 });
554 }
555
Beverlyed8aea22020-01-22 16:52:47 -0500556 /**
Joshua Tsujid9923e52020-04-29 15:33:01 -0400557 * Returns the scrim drawn behind the bubble stack. This is managed by {@link ScrimController}
558 * since we want the scrim's appearance and behavior to be identical to that of the notification
559 * shade scrim.
560 */
561 public ScrimView getScrimForBubble() {
562 return mBubbleScrim;
563 }
564
565 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800566 * Sets whether to perform inflation on the same thread as the caller. This method should only
567 * be used in tests, not in production.
568 */
569 @VisibleForTesting
570 void setInflateSynchronously(boolean inflateSynchronously) {
571 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800572 }
573
Lyn Hane4274be2020-04-24 17:55:36 -0700574 void setOverflowListener(BubbleData.Listener listener) {
575 mOverflowListener = listener;
Lyn Hanb58c7562020-01-07 14:29:20 -0800576 }
577
578 /**
579 * @return Bubbles for updating overflow.
580 */
581 List<Bubble> getOverflowBubbles() {
582 return mBubbleData.getOverflowBubbles();
583 }
584
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400585 /**
586 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
587 * method initializes the stack view and adds it to the StatusBar just above the scrim.
588 */
589 private void ensureStackViewCreated() {
590 if (mStackView == null) {
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500591 mStackView = new BubbleStackView(
Joshua Tsujibe60a582020-03-23 17:17:26 -0400592 mContext, mBubbleData, mSurfaceSynchronizer, mFloatingContentCoordinator,
Joshua Tsujiba9fef02020-04-09 17:40:35 -0400593 mSysUiState, mNotificationShadeWindowController);
Joshua Tsujid9923e52020-04-29 15:33:01 -0400594 mStackView.addView(mBubbleScrim);
595 addToWindowManager();
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400596 if (mExpandListener != null) {
597 mStackView.setExpandListener(mExpandListener);
598 }
Joshua Tsuji6855cab2020-04-16 01:05:39 -0400599
600 mStackView.setUnbubbleConversationCallback(notificationEntry ->
601 onUserChangedBubble(notificationEntry, false /* shouldBubble */));
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400602 }
603 }
604
Joshua Tsujid9923e52020-04-29 15:33:01 -0400605 /** Adds the BubbleStackView to the WindowManager. */
606 private void addToWindowManager() {
607 mWmLayoutParams = new WindowManager.LayoutParams(
608 // Fill the screen so we can use translation animations to position the bubble
609 // stack. We'll use touchable regions to ignore touches that are not on the bubbles
610 // themselves.
611 ViewGroup.LayoutParams.MATCH_PARENT,
612 ViewGroup.LayoutParams.MATCH_PARENT,
Joshua Tsuji94d4c342020-05-04 14:00:24 -0400613 WindowManager.LayoutParams.TYPE_TRUSTED_APPLICATION_OVERLAY,
Joshua Tsujid9923e52020-04-29 15:33:01 -0400614 // Start not focusable - we'll become focusable when expanded so the ActivityView
615 // can use the IME.
616 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
617 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
618 PixelFormat.TRANSLUCENT);
619
620 mWmLayoutParams.setFitInsetsTypes(0);
621 mWmLayoutParams.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
622 mWmLayoutParams.token = new Binder();
623 mWmLayoutParams.setTitle("Bubbles!");
624 mWmLayoutParams.packageName = mContext.getPackageName();
625 mWmLayoutParams.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
626
627 mWindowManager.addView(mStackView, mWmLayoutParams);
628 }
629
630 private void updateWmFlags() {
631 if (isStackExpanded()) {
632 // If we're expanded, we want to be focusable so that the ActivityView can receive focus
633 // and show the IME.
634 mWmLayoutParams.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
635 } else {
636 // If we're collapsed, we don't want to be able to receive focus. Doing so would
637 // preclude applications from using the IME since we are always above them.
638 mWmLayoutParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
639 }
640
641 mWindowManager.updateViewLayout(mStackView, mWmLayoutParams);
642 }
643
Mark Renoufc19b4732019-06-26 12:08:33 -0400644 /**
645 * Records the notification key for any active bubbles. These are used to restore active
646 * bubbles when the user returns to the foreground.
647 *
648 * @param userId the id of the user
649 */
650 private void saveBubbles(@UserIdInt int userId) {
651 // First clear any existing keys that might be stored.
652 mSavedBubbleKeysPerUser.remove(userId);
653 // Add in all active bubbles for the current user.
654 for (Bubble bubble: mBubbleData.getBubbles()) {
655 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
656 }
657 }
658
659 /**
660 * Promotes existing notifications to Bubbles if they were previously bubbles.
661 *
662 * @param userId the id of the user
663 */
664 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400665 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
666 if (savedBubbleKeys == null) {
667 // There were no bubbles saved for this used.
668 return;
669 }
Evan Laird181de622019-10-24 09:53:02 -0400670 for (NotificationEntry e :
671 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400672 if (savedBubbleKeys.contains(e.getKey())
Beverly Taid1e175c2020-03-10 16:37:04 +0000673 && mNotificationInterruptStateProvider.shouldBubbleUp(e)
Mark Renoufc19b4732019-06-26 12:08:33 -0400674 && canLaunchInActivityView(mContext, e)) {
675 updateBubble(e, /* suppressFlyout= */ true);
676 }
677 }
678 // Finally, remove the entries for this user now that bubbles are restored.
679 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
680 }
681
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700682 @Override
683 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800684 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700685 }
686
687 @Override
688 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800689 updateForThemeChanges();
690 }
691
692 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700693 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700694 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700695 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800696 mBubbleIconFactory = new BubbleIconFactory(mContext);
Lyn Handa9a6a22020-04-24 13:21:43 -0700697 // Reload each bubble
Mady Mellor3df7ab02019-12-09 15:07:10 -0800698 for (Bubble b: mBubbleData.getBubbles()) {
Lyn Handa9a6a22020-04-24 13:21:43 -0700699 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
700 }
701 for (Bubble b: mBubbleData.getOverflowBubbles()) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800702 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
703 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700704 }
705
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400706 @Override
707 public void onConfigChanged(Configuration newConfig) {
708 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400709 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700710 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400711 }
712 }
713
Mady Mellor5549dd22018-11-06 18:07:34 -0800714 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800715 * Set a listener to be notified of bubble expand events.
716 */
717 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100718 mExpandListener = ((isExpanding, key) -> {
719 if (listener != null) {
720 listener.onBubbleExpandChanged(isExpanding, key);
721 }
Joshua Tsujid9923e52020-04-29 15:33:01 -0400722
723 updateWmFlags();
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100724 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800725 if (mStackView != null) {
726 mStackView.setExpandListener(mExpandListener);
727 }
728 }
729
730 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800731 * Whether or not there are bubbles present, regardless of them being visible on the
732 * screen (e.g. if on AOD).
733 */
Joshua Tsujid9923e52020-04-29 15:33:01 -0400734 @VisibleForTesting
735 boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800736 if (mStackView == null) {
737 return false;
738 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400739 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800740 }
741
742 /**
743 * Whether the stack of bubbles is expanded or not.
744 */
745 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400746 return mBubbleData.isExpanded();
747 }
748
749 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800750 * Tell the stack of bubbles to collapse.
751 */
752 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400753 mBubbleData.setExpanded(false /* expanded */);
754 }
755
Mady Mellorce23c462019-06-17 17:30:07 -0700756 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700757 * True if either:
758 * (1) There is a bubble associated with the provided key and if its notification is hidden
759 * from the shade.
760 * (2) There is a group summary associated with the provided key that is hidden from the shade
761 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700762 *
Mady Mellore28fe102019-07-09 15:33:32 -0700763 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700764 */
Beverlyed8aea22020-01-22 16:52:47 -0500765 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
766 String key = entry.getKey();
Lyn Han2f6e89d2020-04-15 10:01:01 -0700767 boolean isSuppressedBubble = (mBubbleData.hasAnyBubbleWithKey(key)
768 && !mBubbleData.getAnyBubbleWithkey(key).showInShade());
Beverlyed8aea22020-01-22 16:52:47 -0500769
770 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700771 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700772 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Lyn Han2f6e89d2020-04-15 10:01:01 -0700773 return (isSummary && isSuppressedSummary) || isSuppressedBubble;
Mady Mellorce23c462019-06-17 17:30:07 -0700774 }
775
Lyn Hanb58c7562020-01-07 14:29:20 -0800776 void promoteBubbleFromOverflow(Bubble bubble) {
Lyn Han1e19d7f2020-02-05 19:10:58 -0800777 bubble.setInflateSynchronously(mInflateSynchronously);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700778 setIsBubble(bubble, /* isBubble */ true);
Lyn Han1e19d7f2020-02-05 19:10:58 -0800779 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
Lyn Hanb58c7562020-01-07 14:29:20 -0800780 }
781
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800782 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400783 * Request the stack expand if needed, then select the specified Bubble as current.
784 *
785 * @param notificationKey the notification key for the bubble to be selected
786 */
787 public void expandStackAndSelectBubble(String notificationKey) {
Lyn Han2f6e89d2020-04-15 10:01:01 -0700788 Bubble bubble = mBubbleData.getBubbleInStackWithKey(notificationKey);
789 if (bubble == null) {
790 bubble = mBubbleData.getOverflowBubbleWithKey(notificationKey);
791 if (bubble != null) {
792 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
793 }
794 } else if (bubble.getEntry().isBubble()){
Mark Renouf71a3af62019-04-08 15:02:54 -0400795 mBubbleData.setSelectedBubble(bubble);
Mark Renouffec45da2019-03-13 13:24:27 -0400796 }
Lyn Han2f6e89d2020-04-15 10:01:01 -0700797 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400798 }
799
800 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500801 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
802 * is forwarded a back key down/up pair.
803 */
804 public void performBackPressIfNeeded() {
805 if (mStackView != null) {
806 mStackView.performBackPressIfNeeded();
807 }
808 }
809
810 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800811 * Adds or updates a bubble associated with the provided notification entry.
812 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400813 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800814 */
Mark Renouff97ed462019-04-05 13:46:24 -0400815 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700816 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400817 }
818
819 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700820 updateBubble(notif, suppressFlyout, true /* showInShade */);
821 }
822
823 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800824 if (mStackView == null) {
825 // Lazy init stack view when a bubble is created
826 ensureStackViewCreated();
827 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700828 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400829 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700830 notif.setInterruption();
831 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800832 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
833 bubble.setInflateSynchronously(mInflateSynchronously);
834 bubble.inflate(
Lyn Han6cb4e5f2020-04-27 15:11:18 -0700835 b -> {
836 mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade);
837 if (bubble.getBubbleIntent() == null) {
838 return;
839 }
840 bubble.getBubbleIntent().registerCancelListener(pendingIntent -> {
841 if (bubble.getWasAccessed()) {
842 bubble.setPendingIntentCanceled();
843 return;
844 }
845 mHandler.post(
846 () -> removeBubble(bubble.getEntry(),
847 BubbleController.DISMISS_INVALID_INTENT));
848 });
849 },
Mady Mellor3df7ab02019-12-09 15:07:10 -0800850 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700851 }
852
853 /**
854 * Called when a user has indicated that an active notification should be shown as a bubble.
855 * <p>
856 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
857 * the notification from appearing in the shade.
858 *
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700859 * @param entry the notification to change bubble state for.
860 * @param shouldBubble whether the notification should show as a bubble or not.
Mady Mellor7f234902019-10-20 12:06:29 -0700861 */
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700862 public void onUserChangedBubble(NotificationEntry entry, boolean shouldBubble) {
863 NotificationChannel channel = entry.getChannel();
864 final String appPkg = entry.getSbn().getPackageName();
865 final int appUid = entry.getSbn().getUid();
866 if (channel == null || appPkg == null) {
867 return;
Mady Mellorff076eb2019-11-13 10:12:06 -0800868 }
Mady Mellor7f234902019-10-20 12:06:29 -0700869
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700870 // Update the state in NotificationManagerService
871 try {
872 int flags = Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION;
873 mBarService.onNotificationBubbleChanged(entry.getKey(), shouldBubble, flags);
874 } catch (RemoteException e) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800875 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700876
877 // Change the settings
878 channel = NotificationChannelHelper.createConversationChannelIfNeeded(mContext,
879 mINotificationManager, entry, channel);
880 channel.setAllowBubbles(shouldBubble);
881 try {
882 int currentPref = mINotificationManager.getBubblePreferenceForPackage(appPkg, appUid);
883 if (shouldBubble && currentPref == BUBBLE_PREFERENCE_NONE) {
884 mINotificationManager.setBubblesAllowed(appPkg, appUid, BUBBLE_PREFERENCE_SELECTED);
885 }
886 mINotificationManager.updateNotificationChannelForPackage(appPkg, appUid, channel);
887 } catch (RemoteException e) {
888 Log.e(TAG, e.getMessage());
889 }
890
891 if (shouldBubble) {
892 mShadeController.collapsePanel(true);
893 if (entry.getRow() != null) {
894 entry.getRow().updateBubbleButton();
895 }
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800896 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800897 }
898
899 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500900 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500901 * <p>
902 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800903 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500904 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500905 void removeBubble(NotificationEntry entry, int reason) {
Lyn Han2f6e89d2020-04-15 10:01:01 -0700906 if (mBubbleData.hasAnyBubbleWithKey(entry.getKey())) {
Beverlya53fb0d2020-01-29 15:26:13 -0500907 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800908 }
909 }
910
Beverlyed8aea22020-01-22 16:52:47 -0500911 private void onEntryAdded(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000912 if (mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700913 && canLaunchInActivityView(mContext, entry)) {
Beverlyed8aea22020-01-22 16:52:47 -0500914 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700915 }
916 }
917
Beverlyed8aea22020-01-22 16:52:47 -0500918 private void onEntryUpdated(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000919 boolean shouldBubble = mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700920 && canLaunchInActivityView(mContext, entry);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700921 if (!shouldBubble && mBubbleData.hasAnyBubbleWithKey(entry.getKey())) {
Beverlyed8aea22020-01-22 16:52:47 -0500922 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500923 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500924 } else if (shouldBubble) {
Beverlyed8aea22020-01-22 16:52:47 -0500925 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800926 }
Beverlyed8aea22020-01-22 16:52:47 -0500927 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800928
Beverlya53fb0d2020-01-29 15:26:13 -0500929 private void onEntryRemoved(NotificationEntry entry) {
930 if (isSummaryOfBubbles(entry)) {
931 final String groupKey = entry.getSbn().getGroupKey();
932 mBubbleData.removeSuppressedSummary(groupKey);
933
934 // Remove any associated bubble children with the summary
935 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
936 for (int i = 0; i < bubbleChildren.size(); i++) {
937 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
938 }
939 } else {
940 removeBubble(entry, DISMISS_NOTIF_CANCEL);
941 }
942 }
943
Mady Mellor56515c42020-02-18 17:58:36 -0800944 /**
945 * Called when NotificationListener has received adjusted notification rank and reapplied
946 * filtering and sorting. This is used to dismiss or create bubbles based on changes in
947 * permissions on the notification channel or the global setting.
948 *
949 * @param rankingMap the updated ranking map from NotificationListenerService
950 */
Beverlyed8aea22020-01-22 16:52:47 -0500951 private void onRankingUpdated(RankingMap rankingMap) {
Mady Mellor56515c42020-02-18 17:58:36 -0800952 if (mTmpRanking == null) {
953 mTmpRanking = new NotificationListenerService.Ranking();
954 }
955 String[] orderedKeys = rankingMap.getOrderedKeys();
956 for (int i = 0; i < orderedKeys.length; i++) {
957 String key = orderedKeys[i];
958 NotificationEntry entry = mNotificationEntryManager.getPendingOrActiveNotif(key);
959 rankingMap.getRanking(key, mTmpRanking);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700960 boolean isActiveBubble = mBubbleData.hasAnyBubbleWithKey(key);
Mady Mellore45ff862020-03-24 15:54:50 -0700961 if (isActiveBubble && !mTmpRanking.canBubble()) {
Mady Mellor56515c42020-02-18 17:58:36 -0800962 mBubbleData.notificationEntryRemoved(entry, BubbleController.DISMISS_BLOCKED);
Mady Mellore45ff862020-03-24 15:54:50 -0700963 } else if (entry != null && mTmpRanking.isBubble() && !isActiveBubble) {
Mady Mellor56515c42020-02-18 17:58:36 -0800964 entry.setFlagBubble(true);
965 onEntryUpdated(entry);
966 }
967 }
Beverlyed8aea22020-01-22 16:52:47 -0500968 }
Ned Burns01e38212019-01-03 16:32:52 -0500969
Lyn Han2f6e89d2020-04-15 10:01:01 -0700970 private void setIsBubble(Bubble b, boolean isBubble) {
971 if (isBubble) {
972 b.getEntry().getSbn().getNotification().flags |= FLAG_BUBBLE;
973 } else {
974 b.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
975 }
976 try {
977 mBarService.onNotificationBubbleChanged(b.getKey(), isBubble, 0);
978 } catch (RemoteException e) {
979 // Bad things have happened
980 }
981 }
982
Mark Renouf71a3af62019-04-08 15:02:54 -0400983 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400984 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400985
Mark Renouf3bc5b362019-04-05 14:37:59 -0400986 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400987 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800988 // Update bubbles in overflow.
Lyn Hane4274be2020-04-24 17:55:36 -0700989 if (mOverflowListener != null) {
990 mOverflowListener.applyUpdate(update);
Lyn Hanb58c7562020-01-07 14:29:20 -0800991 }
992
Mark Renouf82a40e62019-05-23 16:16:24 -0400993 // Collapsing? Do this first before remaining steps.
994 if (update.expandedChanged && !update.expanded) {
995 mStackView.setExpanded(false);
996 }
997
998 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700999 ArrayList<Pair<Bubble, Integer>> removedBubbles =
1000 new ArrayList<>(update.removedBubbles);
Pinyao Tingee191b12020-04-29 18:35:39 -07001001 ArrayList<Bubble> bubblesToBeRemovedFromRepository = new ArrayList<>();
Mady Mellor22f2f072019-04-18 13:26:18 -07001002 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -04001003 final Bubble bubble = removed.first;
1004 @DismissReason final int reason = removed.second;
1005 mStackView.removeBubble(bubble);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001006
Mark Renoufc19b4732019-06-26 12:08:33 -04001007 // If the bubble is removed for user switching, leave the notification in place.
Lyn Han2f6e89d2020-04-15 10:01:01 -07001008 if (reason == DISMISS_USER_CHANGED) {
1009 continue;
1010 }
Pinyao Tingee191b12020-04-29 18:35:39 -07001011 if (reason == DISMISS_NOTIF_CANCEL) {
1012 bubblesToBeRemovedFromRepository.add(bubble);
1013 }
Lyn Han2f6e89d2020-04-15 10:01:01 -07001014 if (!mBubbleData.hasBubbleInStackWithKey(bubble.getKey())) {
1015 if (!mBubbleData.hasOverflowBubbleWithKey(bubble.getKey())
1016 && (!bubble.showInShade()
1017 || reason == DISMISS_NOTIF_CANCEL
Lyn Hane4274be2020-04-24 17:55:36 -07001018 || reason == DISMISS_GROUP_CANCELLED
1019 || reason == DISMISS_OVERFLOW_MAX_REACHED)) {
Beverlyed8aea22020-01-22 16:52:47 -05001020 // The bubble is now gone & the notification is hidden from the shade, so
1021 // time to actually remove it
1022 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001023 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -05001024 }
Mark Renoufc19b4732019-06-26 12:08:33 -04001025 } else {
Lyn Han2f6e89d2020-04-15 10:01:01 -07001026 if (bubble.getEntry().isBubble() && bubble.showInShade()) {
1027 setIsBubble(bubble, /* isBubble */ false);
1028 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -07001029 if (bubble.getEntry().getRow() != null) {
1030 bubble.getEntry().getRow().updateBubbleButton();
1031 }
Mark Renouf82a40e62019-05-23 16:16:24 -04001032 }
Mady Mellor22f2f072019-04-18 13:26:18 -07001033
Lyn Han2f6e89d2020-04-15 10:01:01 -07001034 }
1035 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
1036 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
1037 // Time to potentially remove the summary
1038 for (NotifCallback cb : mCallbacks) {
1039 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -07001040 }
Mady Mellora54e9fa2019-04-18 13:26:18 -07001041 }
1042 }
Pinyao Tingee191b12020-04-29 18:35:39 -07001043 mDataRepository.removeBubbles(mCurrentUserId, bubblesToBeRemovedFromRepository);
Mark Renouf3bc5b362019-04-05 14:37:59 -04001044
Lyn Hanc47e1712020-01-28 21:43:34 -08001045 if (update.addedBubble != null) {
Pinyao Tingee191b12020-04-29 18:35:39 -07001046 mDataRepository.addBubble(mCurrentUserId, update.addedBubble);
Lyn Hanc47e1712020-01-28 21:43:34 -08001047 mStackView.addBubble(update.addedBubble);
Pinyao Tingee191b12020-04-29 18:35:39 -07001048
Lyn Hanc47e1712020-01-28 21:43:34 -08001049 }
1050
Mark Renouf82a40e62019-05-23 16:16:24 -04001051 if (update.updatedBubble != null) {
1052 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -04001053 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001054
Lyn Hanb58c7562020-01-07 14:29:20 -08001055 // At this point, the correct bubbles are inflated in the stack.
1056 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -04001057 if (update.orderChanged) {
Pinyao Tingee191b12020-04-29 18:35:39 -07001058 mDataRepository.addBubbles(mCurrentUserId, update.bubbles);
Mark Renouf82a40e62019-05-23 16:16:24 -04001059 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -04001060 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001061
Mark Renouf82a40e62019-05-23 16:16:24 -04001062 if (update.selectionChanged) {
1063 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -07001064 if (update.selectedBubble != null) {
1065 mNotificationGroupManager.updateSuppression(
1066 update.selectedBubble.getEntry());
1067 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001068 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001069
Mark Renouf82a40e62019-05-23 16:16:24 -04001070 // Expanding? Apply this last.
1071 if (update.expandedChanged && update.expanded) {
1072 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001073 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001074
Beverlyed8aea22020-01-22 16:52:47 -05001075 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001076 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001077 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001078 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001079
Issei Suzukia8d07312019-06-07 12:56:19 +02001080 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001081 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001082 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001083 mBubbleData.getSelectedBubble()));
1084
1085 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001086 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001087 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001088 mStackView.getExpandedBubble()));
1089 }
1090 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001091 }
1092 };
1093
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001094 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001095 * We intercept notification entries (including group summaries) dismissed by the user when
1096 * there is an active bubble associated with it. We do this so that developers can still
1097 * cancel it (and hence the bubbles associated with it). However, these intercepted
1098 * notifications should then be hidden from the shade since the user has cancelled them, so we
1099 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1100 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001101 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001102 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001103 */
Beverlya53fb0d2020-01-29 15:26:13 -05001104 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001105 if (entry == null) {
1106 return false;
1107 }
Lyn Han2f6e89d2020-04-15 10:01:01 -07001108 if (isSummaryOfBubbles(entry)) {
Beverlya53fb0d2020-01-29 15:26:13 -05001109 handleSummaryDismissalInterception(entry);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001110 } else {
1111 Bubble bubble = mBubbleData.getBubbleInStackWithKey(entry.getKey());
1112 if (bubble == null || !entry.isBubble()) {
1113 bubble = mBubbleData.getOverflowBubbleWithKey(entry.getKey());
1114 }
1115 if (bubble == null) {
1116 return false;
1117 }
Beverlyed8aea22020-01-22 16:52:47 -05001118 bubble.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001119 bubble.setShowDot(false /* show */);
Beverlyed8aea22020-01-22 16:52:47 -05001120 }
Beverlya53fb0d2020-01-29 15:26:13 -05001121 // Update the shade
1122 for (NotifCallback cb : mCallbacks) {
1123 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1124 }
1125 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001126 }
1127
Beverlya53fb0d2020-01-29 15:26:13 -05001128 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1129 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001130 return false;
1131 }
Beverlya53fb0d2020-01-29 15:26:13 -05001132
1133 String groupKey = entry.getSbn().getGroupKey();
1134 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1135 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1136 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1137 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1138 return (isSuppressedSummary || isSummary)
1139 && bubbleChildren != null
1140 && !bubbleChildren.isEmpty();
1141 }
1142
1143 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1144 // current children in the row:
Kevin Han43077f92020-02-28 12:51:53 -08001145 final List<NotificationEntry> children = summary.getAttachedNotifChildren();
Beverlya53fb0d2020-01-29 15:26:13 -05001146 if (children != null) {
1147 for (int i = 0; i < children.size(); i++) {
1148 NotificationEntry child = children.get(i);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001149 if (mBubbleData.hasAnyBubbleWithKey(child.getKey())) {
Beverlya53fb0d2020-01-29 15:26:13 -05001150 // Suppress the bubbled child
1151 // As far as group manager is concerned, once a child is no longer shown
1152 // in the shade, it is essentially removed.
Lyn Han2f6e89d2020-04-15 10:01:01 -07001153 Bubble bubbleChild = mBubbleData.getAnyBubbleWithkey(child.getKey());
Beverlya53fb0d2020-01-29 15:26:13 -05001154 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1155 bubbleChild.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001156 bubbleChild.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001157 } else {
1158 // non-bubbled children can be removed
1159 for (NotifCallback cb : mCallbacks) {
1160 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1161 }
1162 }
1163 }
1164 }
1165
1166 // And since all children are removed, remove the summary.
1167 mNotificationGroupManager.onEntryRemoved(summary);
1168
1169 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1170 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1171 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001172 }
1173
1174 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001175 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001176 * Updates the visibility of the bubbles based on current state.
Joshua Tsujid9923e52020-04-29 15:33:01 -04001177 * Does not un-bubble, just hides or un-hides.
Lyn Han6c40fe72019-05-08 14:06:33 -07001178 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001179 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001180 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001181 if (mStackView == null) {
1182 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001183 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001184 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1185 // Bubbles only appear in unlocked shade
1186 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001187 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001188 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001189 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001190
Lyn Han6c40fe72019-05-08 14:06:33 -07001191 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001192 }
1193
1194 /**
Mady Mellor390bff42019-04-05 15:09:01 -07001195 * The display id of the expanded view, if the stack is expanded and not occluded by the
1196 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1197 */
1198 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001199 if (mStackView == null) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001200 return INVALID_DISPLAY;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001201 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001202 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001203 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Lyn Han9f66c3b2020-03-05 23:59:29 -08001204 final BubbleViewProvider expandedViewProvider = mStackView.getExpandedBubble();
1205 if (defaultDisplay && expandedViewProvider != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001206 && !mNotificationShadeWindowController.getPanelExpanded()) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001207 return expandedViewProvider.getDisplayId();
Mady Mellor390bff42019-04-05 15:09:01 -07001208 }
Lyn Han9f66c3b2020-03-05 23:59:29 -08001209 return INVALID_DISPLAY;
Mady Mellor390bff42019-04-05 15:09:01 -07001210 }
1211
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001212 @VisibleForTesting
1213 BubbleStackView getStackView() {
1214 return mStackView;
1215 }
1216
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001217 /**
1218 * Description of current bubble state.
1219 */
1220 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1221 pw.println("BubbleController state:");
1222 mBubbleData.dump(fd, pw, args);
1223 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001224 if (mStackView != null) {
1225 mStackView.dump(fd, pw, args);
1226 }
1227 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001228 }
1229
Mady Mellore80930e2019-03-21 16:00:45 -07001230 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001231 * This task stack listener is responsible for responding to tasks moved to the front
1232 * which are on the default (main) display. When this happens, expanded bubbles must be
1233 * collapsed so the user may interact with the app which was just moved to the front.
1234 * <p>
1235 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1236 * these calls via a main thread Handler.
1237 */
1238 @MainThread
1239 private class BubbleTaskStackListener extends TaskStackChangeListener {
1240
Mark Renoufcecc77b2019-01-30 16:32:24 -05001241 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001242 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1243 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001244 if (!mStackView.isExpansionAnimating()) {
1245 mBubbleData.setExpanded(false);
1246 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001247 }
1248 }
1249
Mark Renoufcecc77b2019-01-30 16:32:24 -05001250 @Override
Winson Chunge789ff62020-02-24 14:40:23 -08001251 public void onActivityRestartAttempt(RunningTaskInfo task, boolean homeTaskVisible,
Evan Rosky8d1c24e2020-04-23 09:21:16 -07001252 boolean clearedTask, boolean wasVisible) {
Winson Chunge789ff62020-02-24 14:40:23 -08001253 for (Bubble b : mBubbleData.getBubbles()) {
1254 if (b.getDisplayId() == task.displayId) {
1255 expandStackAndSelectBubble(b.getKey());
1256 return;
1257 }
1258 }
1259 }
1260
1261 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001262 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001263 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001264 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001265 }
1266 }
Mark Renouf446251d2019-04-26 10:22:41 -04001267
1268 @Override
1269 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1270 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1271 mBubbleData.setExpanded(false);
1272 }
1273 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001274
1275 @Override
1276 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001277 if (mStackView == null) {
1278 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001279 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001280 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001281 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001282
1283 @Override
1284 public void onSingleTaskDisplayEmpty(int displayId) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001285 final BubbleViewProvider expandedBubble = mStackView != null
Mady Mellor5186b132019-09-16 17:55:48 -07001286 ? mStackView.getExpandedBubble()
1287 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001288 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1289 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001290 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001291 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001292 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001293 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001294 }
1295
Mady Mellorca0c24c2019-05-16 16:14:32 -07001296 /**
1297 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1298 *
1299 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1300 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1301 *
1302 * @param context the context to use.
1303 * @param entry the entry to bubble.
1304 */
1305 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1306 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Melloraa9ce172020-03-17 10:34:20 -07001307 ? entry.getBubbleMetadata().getIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001308 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001309 if (entry.getBubbleMetadata() != null
1310 && entry.getBubbleMetadata().getShortcutId() != null) {
1311 return true;
1312 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001313 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001314 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001315 return false;
1316 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001317 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1318 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001319 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001320 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001321 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001322 Log.w(TAG, "Unable to send as bubble, "
1323 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001324 + intent);
1325 return false;
1326 }
1327 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001328 Log.w(TAG, "Unable to send as bubble, "
1329 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001330 + intent);
1331 return false;
1332 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001333 return true;
1334 }
1335
Joshua Tsujia19515f2019-02-13 18:02:29 -05001336 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001337 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001338 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001339 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Joshua Tsujiff6b0f22020-03-09 14:55:19 -04001340 if (mStackView != null) {
Joshua Tsujid9422832019-03-05 13:32:37 -05001341 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001342 }
1343 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001344 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001345}