blob: 9f5d6bc5d7924f2ee54e8ce5fef83d86689bac0c [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);
Lyn Handa9a6a22020-04-24 13:21:43 -0700667 // Reload each bubble
Mady Mellor3df7ab02019-12-09 15:07:10 -0800668 for (Bubble b: mBubbleData.getBubbles()) {
Lyn Handa9a6a22020-04-24 13:21:43 -0700669 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
670 }
671 for (Bubble b: mBubbleData.getOverflowBubbles()) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800672 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
673 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700674 }
675
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400676 @Override
677 public void onConfigChanged(Configuration newConfig) {
678 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400679 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700680 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400681 }
682 }
683
Mady Mellor5549dd22018-11-06 18:07:34 -0800684 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800685 * Set a listener to be notified when some states of the bubbles change.
686 */
687 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
688 mStateChangeListener = listener;
689 }
690
691 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800692 * Set a listener to be notified of bubble expand events.
693 */
694 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100695 mExpandListener = ((isExpanding, key) -> {
696 if (listener != null) {
697 listener.onBubbleExpandChanged(isExpanding, key);
698 }
wilsonshihe8321942019-10-18 18:39:46 +0800699 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100700 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800701 if (mStackView != null) {
702 mStackView.setExpandListener(mExpandListener);
703 }
704 }
705
706 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800707 * Whether or not there are bubbles present, regardless of them being visible on the
708 * screen (e.g. if on AOD).
709 */
710 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800711 if (mStackView == null) {
712 return false;
713 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400714 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800715 }
716
717 /**
718 * Whether the stack of bubbles is expanded or not.
719 */
720 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400721 return mBubbleData.isExpanded();
722 }
723
724 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800725 * Tell the stack of bubbles to collapse.
726 */
727 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400728 mBubbleData.setExpanded(false /* expanded */);
729 }
730
Mady Mellorce23c462019-06-17 17:30:07 -0700731 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700732 * True if either:
733 * (1) There is a bubble associated with the provided key and if its notification is hidden
734 * from the shade.
735 * (2) There is a group summary associated with the provided key that is hidden from the shade
736 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700737 *
Mady Mellore28fe102019-07-09 15:33:32 -0700738 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700739 */
Beverlyed8aea22020-01-22 16:52:47 -0500740 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
741 String key = entry.getKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700742 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800743 && !mBubbleData.getBubbleWithKey(key).showInShade();
Beverlyed8aea22020-01-22 16:52:47 -0500744
745 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700746 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700747 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Beverlyed8aea22020-01-22 16:52:47 -0500748
Mady Mellore4348272019-07-29 17:43:36 -0700749 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700750 }
751
Lyn Hanb58c7562020-01-07 14:29:20 -0800752 void promoteBubbleFromOverflow(Bubble bubble) {
Lyn Han1e19d7f2020-02-05 19:10:58 -0800753 bubble.setInflateSynchronously(mInflateSynchronously);
754 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
Lyn Hanb58c7562020-01-07 14:29:20 -0800755 }
756
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800757 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400758 * Request the stack expand if needed, then select the specified Bubble as current.
759 *
760 * @param notificationKey the notification key for the bubble to be selected
761 */
762 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400763 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
764 if (bubble != null) {
765 mBubbleData.setSelectedBubble(bubble);
766 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400767 }
768 }
769
770 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500771 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
772 * is forwarded a back key down/up pair.
773 */
774 public void performBackPressIfNeeded() {
775 if (mStackView != null) {
776 mStackView.performBackPressIfNeeded();
777 }
778 }
779
780 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800781 * Adds or updates a bubble associated with the provided notification entry.
782 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400783 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800784 */
Mark Renouff97ed462019-04-05 13:46:24 -0400785 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700786 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400787 }
788
789 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700790 updateBubble(notif, suppressFlyout, true /* showInShade */);
791 }
792
793 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800794 if (mStackView == null) {
795 // Lazy init stack view when a bubble is created
796 ensureStackViewCreated();
797 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700798 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400799 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700800 notif.setInterruption();
801 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800802 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
803 bubble.setInflateSynchronously(mInflateSynchronously);
804 bubble.inflate(
805 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
806 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700807 }
808
809 /**
810 * Called when a user has indicated that an active notification should be shown as a bubble.
811 * <p>
812 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
813 * the notification from appearing in the shade.
814 *
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700815 * @param entry the notification to change bubble state for.
816 * @param shouldBubble whether the notification should show as a bubble or not.
Mady Mellor7f234902019-10-20 12:06:29 -0700817 */
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700818 public void onUserChangedBubble(NotificationEntry entry, boolean shouldBubble) {
819 NotificationChannel channel = entry.getChannel();
820 final String appPkg = entry.getSbn().getPackageName();
821 final int appUid = entry.getSbn().getUid();
822 if (channel == null || appPkg == null) {
823 return;
Mady Mellorff076eb2019-11-13 10:12:06 -0800824 }
Mady Mellor7f234902019-10-20 12:06:29 -0700825
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700826 // Update the state in NotificationManagerService
827 try {
828 int flags = Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION;
829 mBarService.onNotificationBubbleChanged(entry.getKey(), shouldBubble, flags);
830 } catch (RemoteException e) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800831 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700832
833 // Change the settings
834 channel = NotificationChannelHelper.createConversationChannelIfNeeded(mContext,
835 mINotificationManager, entry, channel);
836 channel.setAllowBubbles(shouldBubble);
837 try {
838 int currentPref = mINotificationManager.getBubblePreferenceForPackage(appPkg, appUid);
839 if (shouldBubble && currentPref == BUBBLE_PREFERENCE_NONE) {
840 mINotificationManager.setBubblesAllowed(appPkg, appUid, BUBBLE_PREFERENCE_SELECTED);
841 }
842 mINotificationManager.updateNotificationChannelForPackage(appPkg, appUid, channel);
843 } catch (RemoteException e) {
844 Log.e(TAG, e.getMessage());
845 }
846
847 if (shouldBubble) {
848 mShadeController.collapsePanel(true);
849 if (entry.getRow() != null) {
850 entry.getRow().updateBubbleButton();
851 }
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800852 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800853 }
854
855 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500856 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500857 * <p>
858 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800859 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500860 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500861 void removeBubble(NotificationEntry entry, int reason) {
862 if (mBubbleData.hasBubbleWithKey(entry.getKey())) {
863 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800864 }
865 }
866
Beverlyed8aea22020-01-22 16:52:47 -0500867 private void onEntryAdded(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000868 if (mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700869 && canLaunchInActivityView(mContext, entry)) {
Beverlyed8aea22020-01-22 16:52:47 -0500870 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700871 }
872 }
873
Beverlyed8aea22020-01-22 16:52:47 -0500874 private void onEntryUpdated(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000875 boolean shouldBubble = mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700876 && canLaunchInActivityView(mContext, entry);
Beverlyed8aea22020-01-22 16:52:47 -0500877 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
878 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500879 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500880 } else if (shouldBubble) {
Beverlyed8aea22020-01-22 16:52:47 -0500881 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800882 }
Beverlyed8aea22020-01-22 16:52:47 -0500883 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800884
Beverlya53fb0d2020-01-29 15:26:13 -0500885 private void onEntryRemoved(NotificationEntry entry) {
886 if (isSummaryOfBubbles(entry)) {
887 final String groupKey = entry.getSbn().getGroupKey();
888 mBubbleData.removeSuppressedSummary(groupKey);
889
890 // Remove any associated bubble children with the summary
891 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
892 for (int i = 0; i < bubbleChildren.size(); i++) {
893 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
894 }
895 } else {
896 removeBubble(entry, DISMISS_NOTIF_CANCEL);
897 }
898 }
899
Mady Mellor56515c42020-02-18 17:58:36 -0800900 /**
901 * Called when NotificationListener has received adjusted notification rank and reapplied
902 * filtering and sorting. This is used to dismiss or create bubbles based on changes in
903 * permissions on the notification channel or the global setting.
904 *
905 * @param rankingMap the updated ranking map from NotificationListenerService
906 */
Beverlyed8aea22020-01-22 16:52:47 -0500907 private void onRankingUpdated(RankingMap rankingMap) {
Mady Mellor56515c42020-02-18 17:58:36 -0800908 if (mTmpRanking == null) {
909 mTmpRanking = new NotificationListenerService.Ranking();
910 }
911 String[] orderedKeys = rankingMap.getOrderedKeys();
912 for (int i = 0; i < orderedKeys.length; i++) {
913 String key = orderedKeys[i];
914 NotificationEntry entry = mNotificationEntryManager.getPendingOrActiveNotif(key);
915 rankingMap.getRanking(key, mTmpRanking);
Mady Mellore45ff862020-03-24 15:54:50 -0700916 boolean isActiveBubble = mBubbleData.hasBubbleWithKey(key);
917 if (isActiveBubble && !mTmpRanking.canBubble()) {
Mady Mellor56515c42020-02-18 17:58:36 -0800918 mBubbleData.notificationEntryRemoved(entry, BubbleController.DISMISS_BLOCKED);
Mady Mellore45ff862020-03-24 15:54:50 -0700919 } else if (entry != null && mTmpRanking.isBubble() && !isActiveBubble) {
Mady Mellor56515c42020-02-18 17:58:36 -0800920 entry.setFlagBubble(true);
921 onEntryUpdated(entry);
922 }
923 }
Beverlyed8aea22020-01-22 16:52:47 -0500924 }
Ned Burns01e38212019-01-03 16:32:52 -0500925
Mark Renouf71a3af62019-04-08 15:02:54 -0400926 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400927 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400928
Mark Renouf3bc5b362019-04-05 14:37:59 -0400929 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400930 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800931 // Update bubbles in overflow.
932 if (mOverflowCallback != null) {
933 mOverflowCallback.run();
934 }
935
Mark Renouf82a40e62019-05-23 16:16:24 -0400936 // Collapsing? Do this first before remaining steps.
937 if (update.expandedChanged && !update.expanded) {
938 mStackView.setExpanded(false);
939 }
940
941 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700942 ArrayList<Pair<Bubble, Integer>> removedBubbles =
943 new ArrayList<>(update.removedBubbles);
944 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400945 final Bubble bubble = removed.first;
946 @DismissReason final int reason = removed.second;
947 mStackView.removeBubble(bubble);
Mark Renoufc19b4732019-06-26 12:08:33 -0400948 // If the bubble is removed for user switching, leave the notification in place.
949 if (reason != DISMISS_USER_CHANGED) {
950 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800951 && !bubble.showInShade()) {
Beverlyed8aea22020-01-22 16:52:47 -0500952 // The bubble is now gone & the notification is hidden from the shade, so
953 // time to actually remove it
954 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -0500955 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -0500956 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400957 } else {
958 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400959 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700960 if (bubble.getEntry().getRow() != null) {
961 bubble.getEntry().getRow().updateBubbleButton();
962 }
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700963
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700964 // Update the state in NotificationManagerService
Mark Renoufc19b4732019-06-26 12:08:33 -0400965 try {
966 mBarService.onNotificationBubbleChanged(bubble.getKey(),
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700967 false /* isBubble */, 0 /* flags */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400968 } catch (RemoteException e) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400969 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400970 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700971
Ned Burns00b4b2d2019-10-17 22:09:27 -0400972 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500973 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
974 // Time to potentially remove the summary
975 for (NotifCallback cb : mCallbacks) {
976 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -0700977 }
978 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700979 }
980 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400981
Lyn Hanc47e1712020-01-28 21:43:34 -0800982 if (update.addedBubble != null) {
983 mStackView.addBubble(update.addedBubble);
984 }
985
Mark Renouf82a40e62019-05-23 16:16:24 -0400986 if (update.updatedBubble != null) {
987 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400988 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400989
Lyn Hanb58c7562020-01-07 14:29:20 -0800990 // At this point, the correct bubbles are inflated in the stack.
991 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -0400992 if (update.orderChanged) {
993 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400994 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400995
Mark Renouf82a40e62019-05-23 16:16:24 -0400996 if (update.selectionChanged) {
997 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700998 if (update.selectedBubble != null) {
999 mNotificationGroupManager.updateSuppression(
1000 update.selectedBubble.getEntry());
1001 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001002 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001003
Mark Renouf82a40e62019-05-23 16:16:24 -04001004 // Expanding? Apply this last.
1005 if (update.expandedChanged && update.expanded) {
1006 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001007 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001008
Beverlyed8aea22020-01-22 16:52:47 -05001009 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001010 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001011 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001012 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001013
Issei Suzukia8d07312019-06-07 12:56:19 +02001014 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001015 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001016 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001017 mBubbleData.getSelectedBubble()));
1018
1019 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001020 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001021 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001022 mStackView.getExpandedBubble()));
1023 }
Lyn Hanb58c7562020-01-07 14:29:20 -08001024 Log.d(TAG, "\n[BubbleData] overflow:");
1025 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
1026 null));
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001027 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001028 }
1029 };
1030
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001031 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001032 * We intercept notification entries (including group summaries) dismissed by the user when
1033 * there is an active bubble associated with it. We do this so that developers can still
1034 * cancel it (and hence the bubbles associated with it). However, these intercepted
1035 * notifications should then be hidden from the shade since the user has cancelled them, so we
1036 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1037 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001038 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001039 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001040 */
Beverlya53fb0d2020-01-29 15:26:13 -05001041 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001042 if (entry == null) {
1043 return false;
1044 }
Beverlyed8aea22020-01-22 16:52:47 -05001045
Beverlya53fb0d2020-01-29 15:26:13 -05001046 final boolean interceptBubbleDismissal = mBubbleData.hasBubbleWithKey(entry.getKey())
1047 && entry.isBubble();
1048 final boolean interceptSummaryDismissal = isSummaryOfBubbles(entry);
Beverlyed8aea22020-01-22 16:52:47 -05001049
Beverlya53fb0d2020-01-29 15:26:13 -05001050 if (interceptSummaryDismissal) {
1051 handleSummaryDismissalInterception(entry);
1052 } else if (interceptBubbleDismissal) {
1053 Bubble bubble = mBubbleData.getBubbleWithKey(entry.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001054 bubble.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001055 bubble.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001056 } else {
Beverlyed8aea22020-01-22 16:52:47 -05001057 return false;
1058 }
Beverlya53fb0d2020-01-29 15:26:13 -05001059
1060 // Update the shade
1061 for (NotifCallback cb : mCallbacks) {
1062 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1063 }
1064 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001065 }
1066
Beverlya53fb0d2020-01-29 15:26:13 -05001067 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1068 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001069 return false;
1070 }
Beverlya53fb0d2020-01-29 15:26:13 -05001071
1072 String groupKey = entry.getSbn().getGroupKey();
1073 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1074 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1075 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1076 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1077 return (isSuppressedSummary || isSummary)
1078 && bubbleChildren != null
1079 && !bubbleChildren.isEmpty();
1080 }
1081
1082 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1083 // current children in the row:
Kevin Han43077f92020-02-28 12:51:53 -08001084 final List<NotificationEntry> children = summary.getAttachedNotifChildren();
Beverlya53fb0d2020-01-29 15:26:13 -05001085 if (children != null) {
1086 for (int i = 0; i < children.size(); i++) {
1087 NotificationEntry child = children.get(i);
1088 if (mBubbleData.hasBubbleWithKey(child.getKey())) {
1089 // Suppress the bubbled child
1090 // As far as group manager is concerned, once a child is no longer shown
1091 // in the shade, it is essentially removed.
1092 Bubble bubbleChild = mBubbleData.getBubbleWithKey(child.getKey());
1093 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1094 bubbleChild.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001095 bubbleChild.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001096 } else {
1097 // non-bubbled children can be removed
1098 for (NotifCallback cb : mCallbacks) {
1099 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1100 }
1101 }
1102 }
1103 }
1104
1105 // And since all children are removed, remove the summary.
1106 mNotificationGroupManager.onEntryRemoved(summary);
1107
1108 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1109 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1110 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001111 }
1112
1113 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001114 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001115 * Updates the visibility of the bubbles based on current state.
1116 * Does not un-bubble, just hides or un-hides. Notifies any
1117 * {@link BubbleStateChangeListener}s of visibility changes.
1118 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001119 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001120 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001121 if (mStackView == null) {
1122 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001123 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001124 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1125 // Bubbles only appear in unlocked shade
1126 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001127 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001128 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001129 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001130
Mady Mellor698d9e82019-08-01 23:11:53 +00001131 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001132 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001133 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001134 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001135 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1136 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1137 }
1138
1139 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001140 }
1141
1142 /**
1143 * Rect indicating the touchable region for the bubble stack / expanded stack.
1144 */
1145 public Rect getTouchableRegion() {
1146 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1147 return null;
1148 }
1149 mStackView.getBoundsOnScreen(mTempRect);
1150 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001151 }
1152
Mady Mellor390bff42019-04-05 15:09:01 -07001153 /**
1154 * The display id of the expanded view, if the stack is expanded and not occluded by the
1155 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1156 */
1157 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001158 if (mStackView == null) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001159 return INVALID_DISPLAY;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001160 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001161 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001162 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Lyn Han9f66c3b2020-03-05 23:59:29 -08001163 final BubbleViewProvider expandedViewProvider = mStackView.getExpandedBubble();
1164 if (defaultDisplay && expandedViewProvider != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001165 && !mNotificationShadeWindowController.getPanelExpanded()) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001166 return expandedViewProvider.getDisplayId();
Mady Mellor390bff42019-04-05 15:09:01 -07001167 }
Lyn Han9f66c3b2020-03-05 23:59:29 -08001168 return INVALID_DISPLAY;
Mady Mellor390bff42019-04-05 15:09:01 -07001169 }
1170
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001171 @VisibleForTesting
1172 BubbleStackView getStackView() {
1173 return mStackView;
1174 }
1175
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001176 /**
1177 * Description of current bubble state.
1178 */
1179 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1180 pw.println("BubbleController state:");
1181 mBubbleData.dump(fd, pw, args);
1182 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001183 if (mStackView != null) {
1184 mStackView.dump(fd, pw, args);
1185 }
1186 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001187 }
1188
Mady Mellore80930e2019-03-21 16:00:45 -07001189 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001190 * This task stack listener is responsible for responding to tasks moved to the front
1191 * which are on the default (main) display. When this happens, expanded bubbles must be
1192 * collapsed so the user may interact with the app which was just moved to the front.
1193 * <p>
1194 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1195 * these calls via a main thread Handler.
1196 */
1197 @MainThread
1198 private class BubbleTaskStackListener extends TaskStackChangeListener {
1199
Mark Renoufcecc77b2019-01-30 16:32:24 -05001200 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001201 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1202 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001203 if (!mStackView.isExpansionAnimating()) {
1204 mBubbleData.setExpanded(false);
1205 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001206 }
1207 }
1208
Mark Renoufcecc77b2019-01-30 16:32:24 -05001209 @Override
Winson Chunge789ff62020-02-24 14:40:23 -08001210 public void onActivityRestartAttempt(RunningTaskInfo task, boolean homeTaskVisible,
1211 boolean clearedTask) {
1212 for (Bubble b : mBubbleData.getBubbles()) {
1213 if (b.getDisplayId() == task.displayId) {
1214 expandStackAndSelectBubble(b.getKey());
1215 return;
1216 }
1217 }
1218 }
1219
1220 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001221 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001222 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001223 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001224 }
1225 }
Mark Renouf446251d2019-04-26 10:22:41 -04001226
1227 @Override
1228 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1229 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1230 mBubbleData.setExpanded(false);
1231 }
1232 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001233
1234 @Override
1235 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001236 if (mStackView == null) {
1237 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001238 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001239 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001240 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001241
1242 @Override
1243 public void onSingleTaskDisplayEmpty(int displayId) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001244 final BubbleViewProvider expandedBubble = mStackView != null
Mady Mellor5186b132019-09-16 17:55:48 -07001245 ? mStackView.getExpandedBubble()
1246 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001247 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1248 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001249 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001250 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001251 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001252 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001253 }
1254
Mady Mellorca0c24c2019-05-16 16:14:32 -07001255 /**
1256 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1257 *
1258 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1259 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1260 *
1261 * @param context the context to use.
1262 * @param entry the entry to bubble.
1263 */
1264 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1265 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Melloraa9ce172020-03-17 10:34:20 -07001266 ? entry.getBubbleMetadata().getIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001267 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001268 if (entry.getBubbleMetadata() != null
1269 && entry.getBubbleMetadata().getShortcutId() != null) {
1270 return true;
1271 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001272 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001273 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001274 return false;
1275 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001276 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1277 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001278 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001279 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001280 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001281 Log.w(TAG, "Unable to send as bubble, "
1282 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001283 + intent);
1284 return false;
1285 }
1286 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001287 Log.w(TAG, "Unable to send as bubble, "
1288 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001289 + intent);
1290 return false;
1291 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001292 return true;
1293 }
1294
Joshua Tsujia19515f2019-02-13 18:02:29 -05001295 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001296 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001297 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001298 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Joshua Tsujiff6b0f22020-03-09 14:55:19 -04001299 if (mStackView != null) {
Joshua Tsujid9422832019-03-05 13:32:37 -05001300 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001301 }
1302 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001303 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001304}