blob: e488cf271fdfe9be39cdd5654ea07ad7d4b07234 [file] [log] [blame]
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.bubbles;
18
Mady Mellor3a0a1b42019-05-23 06:40:21 -070019import static android.app.Notification.FLAG_BUBBLE;
Mady Mellor9adfe6a2020-03-30 17:23:26 -070020import static android.app.NotificationManager.BUBBLE_PREFERENCE_NONE;
21import static android.app.NotificationManager.BUBBLE_PREFERENCE_SELECTED;
Mady Mellorc2ff0112019-03-28 14:18:06 -070022import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL;
23import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL;
24import static android.service.notification.NotificationListenerService.REASON_CANCEL;
25import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -070026import static android.service.notification.NotificationListenerService.REASON_CLICK;
27import static android.service.notification.NotificationListenerService.REASON_GROUP_SUMMARY_CANCELED;
Mady Mellor390bff42019-04-05 15:09:01 -070028import static android.view.Display.DEFAULT_DISPLAY;
29import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080030import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080031import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080032import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080033
Issei Suzukia8d07312019-06-07 12:56:19 +020034import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_CONTROLLER;
35import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
36import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080037import static com.android.systemui.statusbar.StatusBarState.SHADE;
Mady Mellor1a4e86f2019-05-03 16:07:23 -070038import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080039
Mark Renoufba5ab512019-05-02 15:21:01 -040040import static java.lang.annotation.ElementType.FIELD;
41import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
42import static java.lang.annotation.ElementType.PARAMETER;
Mark Renouf08bc42a2019-03-07 13:01:59 -050043import static java.lang.annotation.RetentionPolicy.SOURCE;
44
Mark Renoufc19b4732019-06-26 12:08:33 -040045import android.annotation.UserIdInt;
Mark Renoufc808f062019-02-07 15:20:37 -050046import android.app.ActivityManager.RunningTaskInfo;
Mady Mellor9adfe6a2020-03-30 17:23:26 -070047import android.app.INotificationManager;
48import android.app.Notification;
49import android.app.NotificationChannel;
Mady Mellor66efd5e2019-05-15 13:38:11 -070050import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070051import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080052import android.content.Context;
Mady Mellorca0c24c2019-05-16 16:14:32 -070053import android.content.pm.ActivityInfo;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080054import android.content.pm.PackageManager;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040055import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080056import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050057import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080058import android.os.ServiceManager;
Mady Mellor56515c42020-02-18 17:58:36 -080059import android.service.notification.NotificationListenerService;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040060import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040061import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040062import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040063import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040064import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040065import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050066import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080067import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080068import android.widget.FrameLayout;
69
Mark Renouf08bc42a2019-03-07 13:01:59 -050070import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050071import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040072import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050073
Mady Mellorebdbbb92018-11-15 14:36:48 -080074import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070075import com.android.internal.statusbar.IStatusBarService;
Beverlya53fb0d2020-01-29 15:26:13 -050076import com.android.internal.statusbar.NotificationVisibility;
Beverlya53fb0d2020-01-29 15:26:13 -050077import com.android.systemui.Dumpable;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080078import com.android.systemui.R;
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +010079import com.android.systemui.bubbles.dagger.BubbleModule;
Ned Burnsaaeb44b2020-02-12 23:48:26 -050080import com.android.systemui.dump.DumpManager;
Joshua Tsujibe60a582020-03-23 17:17:26 -040081import com.android.systemui.model.SysUiState;
Beverly8fdb5332019-02-04 14:29:49 -050082import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050083import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000084import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050085import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050086import com.android.systemui.shared.system.WindowManagerWrapper;
Beverlya53fb0d2020-01-29 15:26:13 -050087import com.android.systemui.statusbar.FeatureFlags;
Mark Renoufc19b4732019-06-26 12:08:33 -040088import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070089import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Mady Mellor9adfe6a2020-03-30 17:23:26 -070090import com.android.systemui.statusbar.notification.NotificationChannelHelper;
Ned Burns01e38212019-01-03 16:32:52 -050091import com.android.systemui.statusbar.notification.NotificationEntryListener;
92import com.android.systemui.statusbar.notification.NotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -050093import com.android.systemui.statusbar.notification.collection.NotifCollection;
94import com.android.systemui.statusbar.notification.collection.NotifPipeline;
Ned Burnsf81c4c42019-01-07 14:10:43 -050095import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Beverlya53fb0d2020-01-29 15:26:13 -050096import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener;
Beverly Taid1e175c2020-03-10 16:37:04 +000097import com.android.systemui.statusbar.notification.interruption.NotificationInterruptStateProvider;
Mady Mellor22f2f072019-04-18 13:26:18 -070098import com.android.systemui.statusbar.phone.NotificationGroupManager;
wilsonshihe8321942019-10-18 18:39:46 +080099import com.android.systemui.statusbar.phone.NotificationShadeWindowController;
Mady Mellor7f234902019-10-20 12:06:29 -0700100import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorf3b9fab2019-11-13 17:27:32 -0800101import com.android.systemui.statusbar.phone.StatusBar;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700102import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400103import com.android.systemui.statusbar.policy.ZenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500104import com.android.systemui.util.FloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800105
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700106import java.io.FileDescriptor;
107import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500108import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -0400109import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -0700110import java.util.ArrayList;
Lyn Hanb58c7562020-01-07 14:29:20 -0800111import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500112
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800113/**
114 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
115 * Bubbles can be expanded to show more content.
116 *
117 * The controller manages addition, removal, and visible state of bubbles on screen.
118 */
Beverlya53fb0d2020-01-29 15:26:13 -0500119public class BubbleController implements ConfigurationController.ConfigurationListener, Dumpable {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800120
Issei Suzukia8d07312019-06-07 12:56:19 +0200121 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800122
Mark Renouf08bc42a2019-03-07 13:01:59 -0500123 @Retention(SOURCE)
124 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400125 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700126 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400127 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500128 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700129
Mark Renouf08bc42a2019-03-07 13:01:59 -0500130 static final int DISMISS_USER_GESTURE = 1;
131 static final int DISMISS_AGED = 2;
132 static final int DISMISS_TASK_FINISHED = 3;
133 static final int DISMISS_BLOCKED = 4;
134 static final int DISMISS_NOTIF_CANCEL = 5;
135 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700136 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400137 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700138 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700139 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500140
Ned Burns01e38212019-01-03 16:32:52 -0500141 private final Context mContext;
142 private final NotificationEntryManager mNotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500143 private final NotifPipeline mNotifPipeline;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500144 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800145 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800146 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100147 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700148 private final NotificationGroupManager mNotificationGroupManager;
Heemin Seogba6337f2019-12-10 15:34:37 -0800149 private final ShadeController mShadeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500150 private final FloatingContentCoordinator mFloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800151
Mady Mellor3dff9e62019-02-05 18:12:53 -0800152 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400153 @Nullable private BubbleStackView mStackView;
Mady Mellor247ca2c2019-12-02 16:18:59 -0800154 private BubbleIconFactory mBubbleIconFactory;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800155
Mark Renoufc19b4732019-06-26 12:08:33 -0400156 // Tracks the id of the current (foreground) user.
157 private int mCurrentUserId;
158 // Saves notification keys of active bubbles when users are switched.
159 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
160
Mady Mellor56515c42020-02-18 17:58:36 -0800161 // Used when ranking updates occur and we check if things should bubble / unbubble
162 private NotificationListenerService.Ranking mTmpRanking;
163
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800164 // Bubbles get added to the status bar view
wilsonshihe8321942019-10-18 18:39:46 +0800165 private final NotificationShadeWindowController mNotificationShadeWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400166 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800167 private StatusBarStateListener mStatusBarStateListener;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700168 private INotificationManager mINotificationManager;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500169
Lyn Hanb58c7562020-01-07 14:29:20 -0800170 // Callback that updates BubbleOverflowActivity on data change.
171 @Nullable private Runnable mOverflowCallback = null;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800172
Beverly Taid1e175c2020-03-10 16:37:04 +0000173 private final NotificationInterruptStateProvider mNotificationInterruptStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700174 private IStatusBarService mBarService;
Joshua Tsujibe60a582020-03-23 17:17:26 -0400175 private SysUiState mSysUiState;
Mady Mellorb4991e62019-01-10 15:14:51 -0800176
Mady Mellord1c78b262018-11-06 18:04:40 -0800177 // Used for determining view rect for touch interaction
178 private Rect mTempRect = new Rect();
179
Mark Renoufc19b4732019-06-26 12:08:33 -0400180 // Listens to user switch so bubbles can be saved and restored.
181 private final NotificationLockscreenUserManager mNotifUserManager;
182
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400183 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
184 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
185
Mady Mellor3df7ab02019-12-09 15:07:10 -0800186 private boolean mInflateSynchronously;
187
Beverlyed8aea22020-01-22 16:52:47 -0500188 // TODO (b/145659174): allow for multiple callbacks to support the "shadow" new notif pipeline
189 private final List<NotifCallback> mCallbacks = new ArrayList<>();
190
Mady Mellor5549dd22018-11-06 18:07:34 -0800191 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800192 * Listener to be notified when some states of the bubbles change.
193 */
194 public interface BubbleStateChangeListener {
195 /**
196 * Called when the stack has bubbles or no longer has bubbles.
197 */
198 void onHasBubblesChanged(boolean hasBubbles);
199 }
200
Mady Mellorcd9b1302018-11-06 18:08:04 -0800201 /**
202 * Listener to find out about stack expansion / collapse events.
203 */
204 public interface BubbleExpandListener {
205 /**
206 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700207 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800208 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800209 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800210 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800211 void onBubbleExpandChanged(boolean isExpanding, String key);
212 }
213
214 /**
Mady Mellorf44b6832020-01-14 13:26:14 -0800215 * Listener to be notified when a bubbles' notification suppression state changes.
216 */
217 public interface NotificationSuppressionChangedListener {
218 /**
219 * Called when the notification suppression state of a bubble changes.
220 */
221 void onBubbleNotificationSuppressionChange(Bubble bubble);
Beverlyed8aea22020-01-22 16:52:47 -0500222 }
Mady Mellorf44b6832020-01-14 13:26:14 -0800223
Beverlyed8aea22020-01-22 16:52:47 -0500224 /**
225 * Callback for when the BubbleController wants to interact with the notification pipeline to:
226 * - Remove a previously bubbled notification
227 * - Update the notification shade since bubbled notification should/shouldn't be showing
228 */
229 public interface NotifCallback {
230 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500231 * Called when a bubbled notification that was hidden from the shade is now being removed
232 * This can happen when an app cancels a bubbled notification or when the user dismisses a
233 * bubble.
Beverlyed8aea22020-01-22 16:52:47 -0500234 */
Beverlya53fb0d2020-01-29 15:26:13 -0500235 void removeNotification(NotificationEntry entry, int reason);
Beverlyed8aea22020-01-22 16:52:47 -0500236
237 /**
238 * Called when a bubbled notification has changed whether it should be
239 * filtered from the shade.
240 */
Beverlya53fb0d2020-01-29 15:26:13 -0500241 void invalidateNotifications(String reason);
Beverlyed8aea22020-01-22 16:52:47 -0500242
243 /**
244 * Called on a bubbled entry that has been removed when there are no longer
245 * bubbled entries in its group.
246 *
247 * Checks whether its group has any other (non-bubbled) children. If it doesn't,
248 * removes all remnants of the group's summary from the notification pipeline.
249 * TODO: (b/145659174) Only old pipeline needs this - delete post-migration.
250 */
251 void maybeCancelSummary(NotificationEntry entry);
Mady Mellorf44b6832020-01-14 13:26:14 -0800252 }
253
254 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800255 * Listens for the current state of the status bar and updates the visibility state
256 * of bubbles as needed.
257 */
258 private class StatusBarStateListener implements StatusBarStateController.StateListener {
259 private int mState;
260 /**
261 * Returns the current status bar state.
262 */
263 public int getCurrentState() {
264 return mState;
265 }
266
267 @Override
268 public void onStateChanged(int newState) {
269 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400270 boolean shouldCollapse = (mState != SHADE);
271 if (shouldCollapse) {
272 collapseStack();
273 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700274 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800275 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800276 }
277
Mady Mellor7f234902019-10-20 12:06:29 -0700278 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800279 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700280 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800281 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700282 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700283 ConfigurationController configurationController,
Beverly Taid1e175c2020-03-10 16:37:04 +0000284 NotificationInterruptStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400285 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700286 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700287 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500288 NotificationEntryManager entryManager,
289 NotifPipeline notifPipeline,
290 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500291 DumpManager dumpManager,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400292 FloatingContentCoordinator floatingContentCoordinator,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700293 SysUiState sysUiState,
294 INotificationManager notificationManager) {
wilsonshihe8321942019-10-18 18:39:46 +0800295 this(context, notificationShadeWindowController, statusBarStateController, shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700296 data, null /* synchronizer */, configurationController, interruptionStateProvider,
Beverlya53fb0d2020-01-29 15:26:13 -0500297 zenModeController, notifUserManager, groupManager, entryManager,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700298 notifPipeline, featureFlags, dumpManager, floatingContentCoordinator, sysUiState,
299 notificationManager);
Mady Mellor7f234902019-10-20 12:06:29 -0700300 }
301
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +0100302 /**
303 * Injected constructor. See {@link BubbleModule}.
304 */
Mady Mellor7f234902019-10-20 12:06:29 -0700305 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800306 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700307 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800308 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700309 BubbleData data,
310 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
311 ConfigurationController configurationController,
Beverly Taid1e175c2020-03-10 16:37:04 +0000312 NotificationInterruptStateProvider interruptionStateProvider,
Mady Mellor7f234902019-10-20 12:06:29 -0700313 ZenModeController zenModeController,
314 NotificationLockscreenUserManager notifUserManager,
315 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500316 NotificationEntryManager entryManager,
317 NotifPipeline notifPipeline,
318 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500319 DumpManager dumpManager,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400320 FloatingContentCoordinator floatingContentCoordinator,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700321 SysUiState sysUiState,
322 INotificationManager notificationManager) {
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500323 dumpManager.registerDumpable(TAG, this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800324 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800325 mShadeController = shadeController;
Beverly Taid1e175c2020-03-10 16:37:04 +0000326 mNotificationInterruptStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400327 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400328 mZenModeController = zenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500329 mFloatingContentCoordinator = floatingContentCoordinator;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700330 mINotificationManager = notificationManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400331 mZenModeController.addCallback(new ZenModeController.Callback() {
332 @Override
333 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800334 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400335 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700336 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400337 }
338
339 @Override
340 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800341 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400342 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700343 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400344 }
345 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700346
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700347 configurationController.addCallback(this /* configurationListener */);
Joshua Tsujibe60a582020-03-23 17:17:26 -0400348 mSysUiState = sysUiState;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800349
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400350 mBubbleData = data;
351 mBubbleData.setListener(mBubbleDataListener);
Mady Mellorf44b6832020-01-14 13:26:14 -0800352 mBubbleData.setSuppressionChangedListener(new NotificationSuppressionChangedListener() {
353 @Override
354 public void onBubbleNotificationSuppressionChange(Bubble bubble) {
355 // Make sure NoMan knows it's not showing in the shade anymore so anyone querying it
356 // can tell.
357 try {
358 mBarService.onBubbleNotificationSuppressionChanged(bubble.getKey(),
359 !bubble.showInShade());
360 } catch (RemoteException e) {
361 // Bad things have happened
362 }
363 }
364 });
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400365
Mady Mellor7f234902019-10-20 12:06:29 -0700366 mNotificationEntryManager = entryManager;
Mady Mellor22f2f072019-04-18 13:26:18 -0700367 mNotificationGroupManager = groupManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500368 mNotifPipeline = notifPipeline;
369
370 if (!featureFlags.isNewNotifPipelineRenderingEnabled()) {
371 setupNEM();
372 } else {
373 setupNotifPipeline();
374 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800375
wilsonshihe8321942019-10-18 18:39:46 +0800376 mNotificationShadeWindowController = notificationShadeWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800377 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700378 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500379
Mark Renoufcecc77b2019-01-30 16:32:24 -0500380 mTaskStackListener = new BubbleTaskStackListener();
381 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800382
Joshua Tsujia19515f2019-02-13 18:02:29 -0500383 try {
384 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
385 } catch (RemoteException e) {
386 e.printStackTrace();
387 }
Issei Suzukic0387542019-03-08 17:31:14 +0100388 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700389
390 mBarService = IStatusBarService.Stub.asInterface(
391 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400392
393 mSavedBubbleKeysPerUser = new SparseSetArray<>();
394 mCurrentUserId = mNotifUserManager.getCurrentUserId();
395 mNotifUserManager.addUserChangedListener(
Steve Elliottb47f1c72019-12-19 12:39:26 -0500396 new NotificationLockscreenUserManager.UserChangedListener() {
397 @Override
398 public void onUserChanged(int newUserId) {
399 BubbleController.this.saveBubbles(mCurrentUserId);
400 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
401 BubbleController.this.restoreBubbles(newUserId);
402 mCurrentUserId = newUserId;
403 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400404 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800405
Mady Mellor247ca2c2019-12-02 16:18:59 -0800406 mBubbleIconFactory = new BubbleIconFactory(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800407 }
408
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400409 /**
Beverlyed8aea22020-01-22 16:52:47 -0500410 * See {@link NotifCallback}.
411 */
412 public void addNotifCallback(NotifCallback callback) {
413 mCallbacks.add(callback);
414 }
415
416 private void setupNEM() {
417 mNotificationEntryManager.addNotificationEntryListener(
418 new NotificationEntryListener() {
419 @Override
Mady Mellorf9439ab2020-01-30 16:06:53 -0800420 public void onPendingEntryAdded(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -0500421 onEntryAdded(entry);
422 }
423
424 @Override
425 public void onPreEntryUpdated(NotificationEntry entry) {
426 onEntryUpdated(entry);
427 }
428
429 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500430 public void onEntryRemoved(
431 NotificationEntry entry,
432 @android.annotation.Nullable NotificationVisibility visibility,
Julia Reynolds138111f2020-02-26 11:17:39 -0500433 boolean removedByUser,
434 int reason) {
Beverlya53fb0d2020-01-29 15:26:13 -0500435 BubbleController.this.onEntryRemoved(entry);
436 }
437
438 @Override
Beverlyed8aea22020-01-22 16:52:47 -0500439 public void onNotificationRankingUpdated(RankingMap rankingMap) {
440 onRankingUpdated(rankingMap);
441 }
442 });
443
Evan Laird04373662020-01-24 17:37:39 -0500444 mNotificationEntryManager.addNotificationRemoveInterceptor(
Beverlyed8aea22020-01-22 16:52:47 -0500445 new NotificationRemoveInterceptor() {
446 @Override
Evan Laird04373662020-01-24 17:37:39 -0500447 public boolean onNotificationRemoveRequested(
Beverlya53fb0d2020-01-29 15:26:13 -0500448 String key,
449 NotificationEntry entry,
450 int dismissReason) {
451 final boolean isClearAll = dismissReason == REASON_CANCEL_ALL;
452 final boolean isUserDimiss = dismissReason == REASON_CANCEL
453 || dismissReason == REASON_CLICK;
454 final boolean isAppCancel = dismissReason == REASON_APP_CANCEL
455 || dismissReason == REASON_APP_CANCEL_ALL;
456 final boolean isSummaryCancel =
457 dismissReason == REASON_GROUP_SUMMARY_CANCELED;
458
459 // Need to check for !appCancel here because the notification may have
460 // previously been dismissed & entry.isRowDismissed would still be true
461 boolean userRemovedNotif =
462 (entry != null && entry.isRowDismissed() && !isAppCancel)
463 || isClearAll || isUserDimiss || isSummaryCancel;
464
Mady Mellordd6fe612020-04-15 11:47:55 -0700465 if (userRemovedNotif) {
Beverlya53fb0d2020-01-29 15:26:13 -0500466 return handleDismissalInterception(entry);
467 }
468
469 return false;
Beverlyed8aea22020-01-22 16:52:47 -0500470 }
471 });
472
473 mNotificationGroupManager.addOnGroupChangeListener(
474 new NotificationGroupManager.OnGroupChangeListener() {
475 @Override
476 public void onGroupSuppressionChanged(
477 NotificationGroupManager.NotificationGroup group,
478 boolean suppressed) {
479 // More notifications could be added causing summary to no longer
480 // be suppressed -- in this case need to remove the key.
481 final String groupKey = group.summary != null
482 ? group.summary.getSbn().getGroupKey()
483 : null;
484 if (!suppressed && groupKey != null
485 && mBubbleData.isSummarySuppressed(groupKey)) {
486 mBubbleData.removeSuppressedSummary(groupKey);
487 }
488 }
489 });
490
491 addNotifCallback(new NotifCallback() {
492 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500493 public void removeNotification(NotificationEntry entry, int reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500494 mNotificationEntryManager.performRemoveNotification(entry.getSbn(),
Beverlya53fb0d2020-01-29 15:26:13 -0500495 reason);
Beverlyed8aea22020-01-22 16:52:47 -0500496 }
497
498 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500499 public void invalidateNotifications(String reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500500 mNotificationEntryManager.updateNotifications(reason);
501 }
502
503 @Override
504 public void maybeCancelSummary(NotificationEntry entry) {
505 // Check if removed bubble has an associated suppressed group summary that needs
506 // to be removed now.
Beverlya53fb0d2020-01-29 15:26:13 -0500507 final String groupKey = entry.getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500508 if (mBubbleData.isSummarySuppressed(groupKey)) {
Beverlya53fb0d2020-01-29 15:26:13 -0500509 mBubbleData.removeSuppressedSummary(groupKey);
Beverlyed8aea22020-01-22 16:52:47 -0500510
511 final NotificationEntry summary =
512 mNotificationEntryManager.getActiveNotificationUnfiltered(
513 mBubbleData.getSummaryKey(groupKey));
Beverlya53fb0d2020-01-29 15:26:13 -0500514 if (summary != null) {
515 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
516 UNDEFINED_DISMISS_REASON);
517 }
Beverlyed8aea22020-01-22 16:52:47 -0500518 }
519
Beverlya53fb0d2020-01-29 15:26:13 -0500520 // Check if we still need to remove the summary from NoManGroup because the summary
521 // may not be in the mBubbleData.mSuppressedGroupKeys list and removed above.
522 // For example:
523 // 1. Bubbled notifications (group) is posted to shade and are visible bubbles
524 // 2. User expands bubbles so now their respective notifications in the shade are
525 // hidden, including the group summary
526 // 3. User removes all bubbles
527 // 4. We expect all the removed bubbles AND the summary (note: the summary was
528 // never added to the suppressedSummary list in BubbleData, so we add this check)
Beverlyed8aea22020-01-22 16:52:47 -0500529 NotificationEntry summary =
530 mNotificationGroupManager.getLogicalGroupSummary(entry.getSbn());
531 if (summary != null) {
532 ArrayList<NotificationEntry> summaryChildren =
533 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
534 boolean isSummaryThisNotif = summary.getKey().equals(entry.getKey());
535 if (!isSummaryThisNotif && (summaryChildren == null
536 || summaryChildren.isEmpty())) {
537 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
538 UNDEFINED_DISMISS_REASON);
539 }
540 }
541 }
542 });
543 }
544
Beverlya53fb0d2020-01-29 15:26:13 -0500545 private void setupNotifPipeline() {
546 mNotifPipeline.addCollectionListener(new NotifCollectionListener() {
547 @Override
548 public void onEntryAdded(NotificationEntry entry) {
549 BubbleController.this.onEntryAdded(entry);
550 }
551
552 @Override
553 public void onEntryUpdated(NotificationEntry entry) {
554 BubbleController.this.onEntryUpdated(entry);
555 }
556
557 @Override
558 public void onRankingUpdate(RankingMap rankingMap) {
559 onRankingUpdated(rankingMap);
560 }
561
562 @Override
563 public void onEntryRemoved(NotificationEntry entry,
564 @NotifCollection.CancellationReason int reason) {
565 BubbleController.this.onEntryRemoved(entry);
566 }
567 });
568 }
569
Beverlyed8aea22020-01-22 16:52:47 -0500570 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800571 * Sets whether to perform inflation on the same thread as the caller. This method should only
572 * be used in tests, not in production.
573 */
574 @VisibleForTesting
575 void setInflateSynchronously(boolean inflateSynchronously) {
576 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800577 }
578
Lyn Hanb58c7562020-01-07 14:29:20 -0800579 void setOverflowCallback(Runnable updateOverflow) {
580 mOverflowCallback = updateOverflow;
581 }
582
583 /**
584 * @return Bubbles for updating overflow.
585 */
586 List<Bubble> getOverflowBubbles() {
587 return mBubbleData.getOverflowBubbles();
588 }
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);
wilsonshihe8321942019-10-18 18:39:46 +0800600 ViewGroup nsv = mNotificationShadeWindowController.getNotificationShadeView();
601 int bubbleScrimIndex = nsv.indexOfChild(nsv.findViewById(R.id.scrim_for_bubble));
Lyn Hanbde48202019-05-29 19:18:29 -0700602 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
wilsonshihe8321942019-10-18 18:39:46 +0800603 nsv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400604 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
605 if (mExpandListener != null) {
606 mStackView.setExpandListener(mExpandListener);
607 }
Joshua Tsuji6855cab2020-04-16 01:05:39 -0400608
609 mStackView.setUnbubbleConversationCallback(notificationEntry ->
610 onUserChangedBubble(notificationEntry, false /* shouldBubble */));
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400611 }
612 }
613
Mark Renoufc19b4732019-06-26 12:08:33 -0400614 /**
615 * Records the notification key for any active bubbles. These are used to restore active
616 * bubbles when the user returns to the foreground.
617 *
618 * @param userId the id of the user
619 */
620 private void saveBubbles(@UserIdInt int userId) {
621 // First clear any existing keys that might be stored.
622 mSavedBubbleKeysPerUser.remove(userId);
623 // Add in all active bubbles for the current user.
624 for (Bubble bubble: mBubbleData.getBubbles()) {
625 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
626 }
627 }
628
629 /**
630 * Promotes existing notifications to Bubbles if they were previously bubbles.
631 *
632 * @param userId the id of the user
633 */
634 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400635 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
636 if (savedBubbleKeys == null) {
637 // There were no bubbles saved for this used.
638 return;
639 }
Evan Laird181de622019-10-24 09:53:02 -0400640 for (NotificationEntry e :
641 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400642 if (savedBubbleKeys.contains(e.getKey())
Beverly Taid1e175c2020-03-10 16:37:04 +0000643 && mNotificationInterruptStateProvider.shouldBubbleUp(e)
Mark Renoufc19b4732019-06-26 12:08:33 -0400644 && canLaunchInActivityView(mContext, e)) {
645 updateBubble(e, /* suppressFlyout= */ true);
646 }
647 }
648 // Finally, remove the entries for this user now that bubbles are restored.
649 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
650 }
651
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700652 @Override
653 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800654 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700655 }
656
657 @Override
658 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800659 updateForThemeChanges();
660 }
661
662 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700663 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700664 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700665 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800666 mBubbleIconFactory = new BubbleIconFactory(mContext);
667 for (Bubble b: mBubbleData.getBubbles()) {
668 // Reload each bubble
669 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
670 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700671 }
672
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400673 @Override
674 public void onConfigChanged(Configuration newConfig) {
675 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400676 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700677 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400678 }
679 }
680
Mady Mellor5549dd22018-11-06 18:07:34 -0800681 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800682 * Set a listener to be notified when some states of the bubbles change.
683 */
684 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
685 mStateChangeListener = listener;
686 }
687
688 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800689 * Set a listener to be notified of bubble expand events.
690 */
691 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100692 mExpandListener = ((isExpanding, key) -> {
693 if (listener != null) {
694 listener.onBubbleExpandChanged(isExpanding, key);
695 }
wilsonshihe8321942019-10-18 18:39:46 +0800696 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100697 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800698 if (mStackView != null) {
699 mStackView.setExpandListener(mExpandListener);
700 }
701 }
702
703 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800704 * Whether or not there are bubbles present, regardless of them being visible on the
705 * screen (e.g. if on AOD).
706 */
707 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800708 if (mStackView == null) {
709 return false;
710 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400711 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800712 }
713
714 /**
715 * Whether the stack of bubbles is expanded or not.
716 */
717 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400718 return mBubbleData.isExpanded();
719 }
720
721 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800722 * Tell the stack of bubbles to collapse.
723 */
724 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400725 mBubbleData.setExpanded(false /* expanded */);
726 }
727
Mady Mellorce23c462019-06-17 17:30:07 -0700728 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700729 * True if either:
730 * (1) There is a bubble associated with the provided key and if its notification is hidden
731 * from the shade.
732 * (2) There is a group summary associated with the provided key that is hidden from the shade
733 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700734 *
Mady Mellore28fe102019-07-09 15:33:32 -0700735 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700736 */
Beverlyed8aea22020-01-22 16:52:47 -0500737 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
738 String key = entry.getKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700739 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800740 && !mBubbleData.getBubbleWithKey(key).showInShade();
Beverlyed8aea22020-01-22 16:52:47 -0500741
742 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700743 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700744 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Beverlyed8aea22020-01-22 16:52:47 -0500745
Mady Mellore4348272019-07-29 17:43:36 -0700746 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700747 }
748
Lyn Hanb58c7562020-01-07 14:29:20 -0800749 void promoteBubbleFromOverflow(Bubble bubble) {
Lyn Han1e19d7f2020-02-05 19:10:58 -0800750 bubble.setInflateSynchronously(mInflateSynchronously);
751 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
Lyn Hanb58c7562020-01-07 14:29:20 -0800752 }
753
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800754 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400755 * Request the stack expand if needed, then select the specified Bubble as current.
756 *
757 * @param notificationKey the notification key for the bubble to be selected
758 */
759 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400760 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
761 if (bubble != null) {
762 mBubbleData.setSelectedBubble(bubble);
763 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400764 }
765 }
766
767 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500768 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
769 * is forwarded a back key down/up pair.
770 */
771 public void performBackPressIfNeeded() {
772 if (mStackView != null) {
773 mStackView.performBackPressIfNeeded();
774 }
775 }
776
777 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800778 * Adds or updates a bubble associated with the provided notification entry.
779 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400780 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800781 */
Mark Renouff97ed462019-04-05 13:46:24 -0400782 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700783 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400784 }
785
786 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700787 updateBubble(notif, suppressFlyout, true /* showInShade */);
788 }
789
790 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800791 if (mStackView == null) {
792 // Lazy init stack view when a bubble is created
793 ensureStackViewCreated();
794 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700795 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400796 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700797 notif.setInterruption();
798 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800799 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
800 bubble.setInflateSynchronously(mInflateSynchronously);
801 bubble.inflate(
802 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
803 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700804 }
805
806 /**
807 * Called when a user has indicated that an active notification should be shown as a bubble.
808 * <p>
809 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
810 * the notification from appearing in the shade.
811 *
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700812 * @param entry the notification to change bubble state for.
813 * @param shouldBubble whether the notification should show as a bubble or not.
Mady Mellor7f234902019-10-20 12:06:29 -0700814 */
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700815 public void onUserChangedBubble(NotificationEntry entry, boolean shouldBubble) {
816 NotificationChannel channel = entry.getChannel();
817 final String appPkg = entry.getSbn().getPackageName();
818 final int appUid = entry.getSbn().getUid();
819 if (channel == null || appPkg == null) {
820 return;
Mady Mellorff076eb2019-11-13 10:12:06 -0800821 }
Mady Mellor7f234902019-10-20 12:06:29 -0700822
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700823 // Update the state in NotificationManagerService
824 try {
825 int flags = Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION;
826 mBarService.onNotificationBubbleChanged(entry.getKey(), shouldBubble, flags);
827 } catch (RemoteException e) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800828 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700829
830 // Change the settings
831 channel = NotificationChannelHelper.createConversationChannelIfNeeded(mContext,
832 mINotificationManager, entry, channel);
833 channel.setAllowBubbles(shouldBubble);
834 try {
835 int currentPref = mINotificationManager.getBubblePreferenceForPackage(appPkg, appUid);
836 if (shouldBubble && currentPref == BUBBLE_PREFERENCE_NONE) {
837 mINotificationManager.setBubblesAllowed(appPkg, appUid, BUBBLE_PREFERENCE_SELECTED);
838 }
839 mINotificationManager.updateNotificationChannelForPackage(appPkg, appUid, channel);
840 } catch (RemoteException e) {
841 Log.e(TAG, e.getMessage());
842 }
843
844 if (shouldBubble) {
845 mShadeController.collapsePanel(true);
846 if (entry.getRow() != null) {
847 entry.getRow().updateBubbleButton();
848 }
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800849 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800850 }
851
852 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500853 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500854 * <p>
855 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800856 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500857 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500858 void removeBubble(NotificationEntry entry, int reason) {
859 if (mBubbleData.hasBubbleWithKey(entry.getKey())) {
860 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800861 }
862 }
863
Beverlyed8aea22020-01-22 16:52:47 -0500864 private void onEntryAdded(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000865 if (mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700866 && canLaunchInActivityView(mContext, entry)) {
Beverlyed8aea22020-01-22 16:52:47 -0500867 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700868 }
869 }
870
Beverlyed8aea22020-01-22 16:52:47 -0500871 private void onEntryUpdated(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000872 boolean shouldBubble = mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700873 && canLaunchInActivityView(mContext, entry);
Beverlyed8aea22020-01-22 16:52:47 -0500874 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
875 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500876 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500877 } else if (shouldBubble) {
Beverlyed8aea22020-01-22 16:52:47 -0500878 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800879 }
Beverlyed8aea22020-01-22 16:52:47 -0500880 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800881
Beverlya53fb0d2020-01-29 15:26:13 -0500882 private void onEntryRemoved(NotificationEntry entry) {
883 if (isSummaryOfBubbles(entry)) {
884 final String groupKey = entry.getSbn().getGroupKey();
885 mBubbleData.removeSuppressedSummary(groupKey);
886
887 // Remove any associated bubble children with the summary
888 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
889 for (int i = 0; i < bubbleChildren.size(); i++) {
890 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
891 }
892 } else {
893 removeBubble(entry, DISMISS_NOTIF_CANCEL);
894 }
895 }
896
Mady Mellor56515c42020-02-18 17:58:36 -0800897 /**
898 * Called when NotificationListener has received adjusted notification rank and reapplied
899 * filtering and sorting. This is used to dismiss or create bubbles based on changes in
900 * permissions on the notification channel or the global setting.
901 *
902 * @param rankingMap the updated ranking map from NotificationListenerService
903 */
Beverlyed8aea22020-01-22 16:52:47 -0500904 private void onRankingUpdated(RankingMap rankingMap) {
Mady Mellor56515c42020-02-18 17:58:36 -0800905 if (mTmpRanking == null) {
906 mTmpRanking = new NotificationListenerService.Ranking();
907 }
908 String[] orderedKeys = rankingMap.getOrderedKeys();
909 for (int i = 0; i < orderedKeys.length; i++) {
910 String key = orderedKeys[i];
911 NotificationEntry entry = mNotificationEntryManager.getPendingOrActiveNotif(key);
912 rankingMap.getRanking(key, mTmpRanking);
Mady Mellore45ff862020-03-24 15:54:50 -0700913 boolean isActiveBubble = mBubbleData.hasBubbleWithKey(key);
914 if (isActiveBubble && !mTmpRanking.canBubble()) {
Mady Mellor56515c42020-02-18 17:58:36 -0800915 mBubbleData.notificationEntryRemoved(entry, BubbleController.DISMISS_BLOCKED);
Mady Mellore45ff862020-03-24 15:54:50 -0700916 } else if (entry != null && mTmpRanking.isBubble() && !isActiveBubble) {
Mady Mellor56515c42020-02-18 17:58:36 -0800917 entry.setFlagBubble(true);
918 onEntryUpdated(entry);
919 }
920 }
Beverlyed8aea22020-01-22 16:52:47 -0500921 }
Ned Burns01e38212019-01-03 16:32:52 -0500922
Mark Renouf71a3af62019-04-08 15:02:54 -0400923 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400924 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400925
Mark Renouf3bc5b362019-04-05 14:37:59 -0400926 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400927 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800928 // Update bubbles in overflow.
929 if (mOverflowCallback != null) {
930 mOverflowCallback.run();
931 }
932
Mark Renouf82a40e62019-05-23 16:16:24 -0400933 // Collapsing? Do this first before remaining steps.
934 if (update.expandedChanged && !update.expanded) {
935 mStackView.setExpanded(false);
936 }
937
938 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700939 ArrayList<Pair<Bubble, Integer>> removedBubbles =
940 new ArrayList<>(update.removedBubbles);
941 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400942 final Bubble bubble = removed.first;
943 @DismissReason final int reason = removed.second;
944 mStackView.removeBubble(bubble);
Mark Renoufc19b4732019-06-26 12:08:33 -0400945 // If the bubble is removed for user switching, leave the notification in place.
946 if (reason != DISMISS_USER_CHANGED) {
947 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800948 && !bubble.showInShade()) {
Beverlyed8aea22020-01-22 16:52:47 -0500949 // The bubble is now gone & the notification is hidden from the shade, so
950 // time to actually remove it
951 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -0500952 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -0500953 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400954 } else {
955 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400956 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700957 if (bubble.getEntry().getRow() != null) {
958 bubble.getEntry().getRow().updateBubbleButton();
959 }
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700960
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700961 // Update the state in NotificationManagerService
Mark Renoufc19b4732019-06-26 12:08:33 -0400962 try {
963 mBarService.onNotificationBubbleChanged(bubble.getKey(),
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700964 false /* isBubble */, 0 /* flags */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400965 } catch (RemoteException e) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400966 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400967 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700968
Ned Burns00b4b2d2019-10-17 22:09:27 -0400969 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500970 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
971 // Time to potentially remove the summary
972 for (NotifCallback cb : mCallbacks) {
973 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -0700974 }
975 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700976 }
977 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400978
Lyn Hanc47e1712020-01-28 21:43:34 -0800979 if (update.addedBubble != null) {
980 mStackView.addBubble(update.addedBubble);
981 }
982
Mark Renouf82a40e62019-05-23 16:16:24 -0400983 if (update.updatedBubble != null) {
984 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400985 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400986
Lyn Hanb58c7562020-01-07 14:29:20 -0800987 // At this point, the correct bubbles are inflated in the stack.
988 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -0400989 if (update.orderChanged) {
990 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400991 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400992
Mark Renouf82a40e62019-05-23 16:16:24 -0400993 if (update.selectionChanged) {
994 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700995 if (update.selectedBubble != null) {
996 mNotificationGroupManager.updateSuppression(
997 update.selectedBubble.getEntry());
998 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400999 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001000
Mark Renouf82a40e62019-05-23 16:16:24 -04001001 // Expanding? Apply this last.
1002 if (update.expandedChanged && update.expanded) {
1003 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001004 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001005
Beverlyed8aea22020-01-22 16:52:47 -05001006 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001007 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001008 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001009 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001010
Issei Suzukia8d07312019-06-07 12:56:19 +02001011 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001012 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001013 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001014 mBubbleData.getSelectedBubble()));
1015
1016 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001017 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001018 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001019 mStackView.getExpandedBubble()));
1020 }
Lyn Hanb58c7562020-01-07 14:29:20 -08001021 Log.d(TAG, "\n[BubbleData] overflow:");
1022 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
1023 null));
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001024 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001025 }
1026 };
1027
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001028 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001029 * We intercept notification entries (including group summaries) dismissed by the user when
1030 * there is an active bubble associated with it. We do this so that developers can still
1031 * cancel it (and hence the bubbles associated with it). However, these intercepted
1032 * notifications should then be hidden from the shade since the user has cancelled them, so we
1033 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1034 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001035 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001036 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001037 */
Beverlya53fb0d2020-01-29 15:26:13 -05001038 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001039 if (entry == null) {
1040 return false;
1041 }
Beverlyed8aea22020-01-22 16:52:47 -05001042
Beverlya53fb0d2020-01-29 15:26:13 -05001043 final boolean interceptBubbleDismissal = mBubbleData.hasBubbleWithKey(entry.getKey())
1044 && entry.isBubble();
1045 final boolean interceptSummaryDismissal = isSummaryOfBubbles(entry);
Beverlyed8aea22020-01-22 16:52:47 -05001046
Beverlya53fb0d2020-01-29 15:26:13 -05001047 if (interceptSummaryDismissal) {
1048 handleSummaryDismissalInterception(entry);
1049 } else if (interceptBubbleDismissal) {
1050 Bubble bubble = mBubbleData.getBubbleWithKey(entry.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001051 bubble.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001052 bubble.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001053 } else {
Beverlyed8aea22020-01-22 16:52:47 -05001054 return false;
1055 }
Beverlya53fb0d2020-01-29 15:26:13 -05001056
1057 // Update the shade
1058 for (NotifCallback cb : mCallbacks) {
1059 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1060 }
1061 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001062 }
1063
Beverlya53fb0d2020-01-29 15:26:13 -05001064 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1065 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001066 return false;
1067 }
Beverlya53fb0d2020-01-29 15:26:13 -05001068
1069 String groupKey = entry.getSbn().getGroupKey();
1070 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1071 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1072 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1073 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1074 return (isSuppressedSummary || isSummary)
1075 && bubbleChildren != null
1076 && !bubbleChildren.isEmpty();
1077 }
1078
1079 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1080 // current children in the row:
Kevin Han43077f92020-02-28 12:51:53 -08001081 final List<NotificationEntry> children = summary.getAttachedNotifChildren();
Beverlya53fb0d2020-01-29 15:26:13 -05001082 if (children != null) {
1083 for (int i = 0; i < children.size(); i++) {
1084 NotificationEntry child = children.get(i);
1085 if (mBubbleData.hasBubbleWithKey(child.getKey())) {
1086 // Suppress the bubbled child
1087 // As far as group manager is concerned, once a child is no longer shown
1088 // in the shade, it is essentially removed.
1089 Bubble bubbleChild = mBubbleData.getBubbleWithKey(child.getKey());
1090 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1091 bubbleChild.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001092 bubbleChild.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001093 } else {
1094 // non-bubbled children can be removed
1095 for (NotifCallback cb : mCallbacks) {
1096 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1097 }
1098 }
1099 }
1100 }
1101
1102 // And since all children are removed, remove the summary.
1103 mNotificationGroupManager.onEntryRemoved(summary);
1104
1105 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1106 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1107 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001108 }
1109
1110 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001111 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001112 * Updates the visibility of the bubbles based on current state.
1113 * Does not un-bubble, just hides or un-hides. Notifies any
1114 * {@link BubbleStateChangeListener}s of visibility changes.
1115 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001116 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001117 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001118 if (mStackView == null) {
1119 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001120 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001121 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1122 // Bubbles only appear in unlocked shade
1123 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001124 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001125 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001126 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001127
Mady Mellor698d9e82019-08-01 23:11:53 +00001128 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001129 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001130 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001131 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001132 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1133 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1134 }
1135
1136 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001137 }
1138
1139 /**
1140 * Rect indicating the touchable region for the bubble stack / expanded stack.
1141 */
1142 public Rect getTouchableRegion() {
1143 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1144 return null;
1145 }
1146 mStackView.getBoundsOnScreen(mTempRect);
1147 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001148 }
1149
Mady Mellor390bff42019-04-05 15:09:01 -07001150 /**
1151 * The display id of the expanded view, if the stack is expanded and not occluded by the
1152 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1153 */
1154 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001155 if (mStackView == null) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001156 return INVALID_DISPLAY;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001157 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001158 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001159 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Lyn Han9f66c3b2020-03-05 23:59:29 -08001160 final BubbleViewProvider expandedViewProvider = mStackView.getExpandedBubble();
1161 if (defaultDisplay && expandedViewProvider != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001162 && !mNotificationShadeWindowController.getPanelExpanded()) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001163 return expandedViewProvider.getDisplayId();
Mady Mellor390bff42019-04-05 15:09:01 -07001164 }
Lyn Han9f66c3b2020-03-05 23:59:29 -08001165 return INVALID_DISPLAY;
Mady Mellor390bff42019-04-05 15:09:01 -07001166 }
1167
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001168 @VisibleForTesting
1169 BubbleStackView getStackView() {
1170 return mStackView;
1171 }
1172
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001173 /**
1174 * Description of current bubble state.
1175 */
1176 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1177 pw.println("BubbleController state:");
1178 mBubbleData.dump(fd, pw, args);
1179 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001180 if (mStackView != null) {
1181 mStackView.dump(fd, pw, args);
1182 }
1183 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001184 }
1185
Mady Mellore80930e2019-03-21 16:00:45 -07001186 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001187 * This task stack listener is responsible for responding to tasks moved to the front
1188 * which are on the default (main) display. When this happens, expanded bubbles must be
1189 * collapsed so the user may interact with the app which was just moved to the front.
1190 * <p>
1191 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1192 * these calls via a main thread Handler.
1193 */
1194 @MainThread
1195 private class BubbleTaskStackListener extends TaskStackChangeListener {
1196
Mark Renoufcecc77b2019-01-30 16:32:24 -05001197 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001198 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1199 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001200 if (!mStackView.isExpansionAnimating()) {
1201 mBubbleData.setExpanded(false);
1202 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001203 }
1204 }
1205
Mark Renoufcecc77b2019-01-30 16:32:24 -05001206 @Override
Winson Chunge789ff62020-02-24 14:40:23 -08001207 public void onActivityRestartAttempt(RunningTaskInfo task, boolean homeTaskVisible,
1208 boolean clearedTask) {
1209 for (Bubble b : mBubbleData.getBubbles()) {
1210 if (b.getDisplayId() == task.displayId) {
1211 expandStackAndSelectBubble(b.getKey());
1212 return;
1213 }
1214 }
1215 }
1216
1217 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001218 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001219 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001220 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001221 }
1222 }
Mark Renouf446251d2019-04-26 10:22:41 -04001223
1224 @Override
1225 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1226 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1227 mBubbleData.setExpanded(false);
1228 }
1229 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001230
1231 @Override
1232 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001233 if (mStackView == null) {
1234 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001235 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001236 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001237 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001238
1239 @Override
1240 public void onSingleTaskDisplayEmpty(int displayId) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001241 final BubbleViewProvider expandedBubble = mStackView != null
Mady Mellor5186b132019-09-16 17:55:48 -07001242 ? mStackView.getExpandedBubble()
1243 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001244 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1245 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001246 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001247 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001248 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001249 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001250 }
1251
Mady Mellorca0c24c2019-05-16 16:14:32 -07001252 /**
1253 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1254 *
1255 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1256 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1257 *
1258 * @param context the context to use.
1259 * @param entry the entry to bubble.
1260 */
1261 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1262 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Melloraa9ce172020-03-17 10:34:20 -07001263 ? entry.getBubbleMetadata().getIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001264 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001265 if (entry.getBubbleMetadata() != null
1266 && entry.getBubbleMetadata().getShortcutId() != null) {
1267 return true;
1268 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001269 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001270 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001271 return false;
1272 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001273 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1274 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001275 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001276 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001277 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001278 Log.w(TAG, "Unable to send as bubble, "
1279 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001280 + intent);
1281 return false;
1282 }
1283 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001284 Log.w(TAG, "Unable to send as bubble, "
1285 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001286 + intent);
1287 return false;
1288 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001289 return true;
1290 }
1291
Joshua Tsujia19515f2019-02-13 18:02:29 -05001292 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001293 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001294 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001295 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Joshua Tsujiff6b0f22020-03-09 14:55:19 -04001296 if (mStackView != null) {
Joshua Tsujid9422832019-03-05 13:32:37 -05001297 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001298 }
1299 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001300 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001301}