blob: c8e9a687ad23f2067b15ad06300fe8018a2bcf59 [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 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400608 }
609 }
610
Mark Renoufc19b4732019-06-26 12:08:33 -0400611 /**
612 * Records the notification key for any active bubbles. These are used to restore active
613 * bubbles when the user returns to the foreground.
614 *
615 * @param userId the id of the user
616 */
617 private void saveBubbles(@UserIdInt int userId) {
618 // First clear any existing keys that might be stored.
619 mSavedBubbleKeysPerUser.remove(userId);
620 // Add in all active bubbles for the current user.
621 for (Bubble bubble: mBubbleData.getBubbles()) {
622 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
623 }
624 }
625
626 /**
627 * Promotes existing notifications to Bubbles if they were previously bubbles.
628 *
629 * @param userId the id of the user
630 */
631 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400632 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
633 if (savedBubbleKeys == null) {
634 // There were no bubbles saved for this used.
635 return;
636 }
Evan Laird181de622019-10-24 09:53:02 -0400637 for (NotificationEntry e :
638 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400639 if (savedBubbleKeys.contains(e.getKey())
Beverly Taid1e175c2020-03-10 16:37:04 +0000640 && mNotificationInterruptStateProvider.shouldBubbleUp(e)
Mark Renoufc19b4732019-06-26 12:08:33 -0400641 && canLaunchInActivityView(mContext, e)) {
642 updateBubble(e, /* suppressFlyout= */ true);
643 }
644 }
645 // Finally, remove the entries for this user now that bubbles are restored.
646 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
647 }
648
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700649 @Override
650 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800651 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700652 }
653
654 @Override
655 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800656 updateForThemeChanges();
657 }
658
659 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700660 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700661 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700662 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800663 mBubbleIconFactory = new BubbleIconFactory(mContext);
664 for (Bubble b: mBubbleData.getBubbles()) {
665 // Reload each bubble
666 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
667 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700668 }
669
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400670 @Override
671 public void onConfigChanged(Configuration newConfig) {
672 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400673 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700674 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400675 }
676 }
677
Mady Mellor5549dd22018-11-06 18:07:34 -0800678 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800679 * Set a listener to be notified when some states of the bubbles change.
680 */
681 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
682 mStateChangeListener = listener;
683 }
684
685 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800686 * Set a listener to be notified of bubble expand events.
687 */
688 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100689 mExpandListener = ((isExpanding, key) -> {
690 if (listener != null) {
691 listener.onBubbleExpandChanged(isExpanding, key);
692 }
wilsonshihe8321942019-10-18 18:39:46 +0800693 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100694 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800695 if (mStackView != null) {
696 mStackView.setExpandListener(mExpandListener);
697 }
698 }
699
700 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800701 * Whether or not there are bubbles present, regardless of them being visible on the
702 * screen (e.g. if on AOD).
703 */
704 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800705 if (mStackView == null) {
706 return false;
707 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400708 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800709 }
710
711 /**
712 * Whether the stack of bubbles is expanded or not.
713 */
714 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400715 return mBubbleData.isExpanded();
716 }
717
718 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800719 * Tell the stack of bubbles to collapse.
720 */
721 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400722 mBubbleData.setExpanded(false /* expanded */);
723 }
724
Mady Mellorce23c462019-06-17 17:30:07 -0700725 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700726 * True if either:
727 * (1) There is a bubble associated with the provided key and if its notification is hidden
728 * from the shade.
729 * (2) There is a group summary associated with the provided key that is hidden from the shade
730 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700731 *
Mady Mellore28fe102019-07-09 15:33:32 -0700732 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700733 */
Beverlyed8aea22020-01-22 16:52:47 -0500734 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
735 String key = entry.getKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700736 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800737 && !mBubbleData.getBubbleWithKey(key).showInShade();
Beverlyed8aea22020-01-22 16:52:47 -0500738
739 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700740 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700741 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Beverlyed8aea22020-01-22 16:52:47 -0500742
Mady Mellore4348272019-07-29 17:43:36 -0700743 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700744 }
745
Lyn Hanb58c7562020-01-07 14:29:20 -0800746 void promoteBubbleFromOverflow(Bubble bubble) {
Lyn Han1e19d7f2020-02-05 19:10:58 -0800747 bubble.setInflateSynchronously(mInflateSynchronously);
748 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
Lyn Hanb58c7562020-01-07 14:29:20 -0800749 }
750
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800751 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400752 * Request the stack expand if needed, then select the specified Bubble as current.
753 *
754 * @param notificationKey the notification key for the bubble to be selected
755 */
756 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400757 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
758 if (bubble != null) {
759 mBubbleData.setSelectedBubble(bubble);
760 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400761 }
762 }
763
764 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500765 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
766 * is forwarded a back key down/up pair.
767 */
768 public void performBackPressIfNeeded() {
769 if (mStackView != null) {
770 mStackView.performBackPressIfNeeded();
771 }
772 }
773
774 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800775 * Adds or updates a bubble associated with the provided notification entry.
776 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400777 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800778 */
Mark Renouff97ed462019-04-05 13:46:24 -0400779 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700780 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400781 }
782
783 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700784 updateBubble(notif, suppressFlyout, true /* showInShade */);
785 }
786
787 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800788 if (mStackView == null) {
789 // Lazy init stack view when a bubble is created
790 ensureStackViewCreated();
791 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700792 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400793 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700794 notif.setInterruption();
795 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800796 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
797 bubble.setInflateSynchronously(mInflateSynchronously);
798 bubble.inflate(
799 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
800 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700801 }
802
803 /**
804 * Called when a user has indicated that an active notification should be shown as a bubble.
805 * <p>
806 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
807 * the notification from appearing in the shade.
808 *
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700809 * @param entry the notification to change bubble state for.
810 * @param shouldBubble whether the notification should show as a bubble or not.
Mady Mellor7f234902019-10-20 12:06:29 -0700811 */
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700812 public void onUserChangedBubble(NotificationEntry entry, boolean shouldBubble) {
813 NotificationChannel channel = entry.getChannel();
814 final String appPkg = entry.getSbn().getPackageName();
815 final int appUid = entry.getSbn().getUid();
816 if (channel == null || appPkg == null) {
817 return;
Mady Mellorff076eb2019-11-13 10:12:06 -0800818 }
Mady Mellor7f234902019-10-20 12:06:29 -0700819
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700820 // Update the state in NotificationManagerService
821 try {
822 int flags = Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION;
823 mBarService.onNotificationBubbleChanged(entry.getKey(), shouldBubble, flags);
824 } catch (RemoteException e) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800825 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700826
827 // Change the settings
828 channel = NotificationChannelHelper.createConversationChannelIfNeeded(mContext,
829 mINotificationManager, entry, channel);
830 channel.setAllowBubbles(shouldBubble);
831 try {
832 int currentPref = mINotificationManager.getBubblePreferenceForPackage(appPkg, appUid);
833 if (shouldBubble && currentPref == BUBBLE_PREFERENCE_NONE) {
834 mINotificationManager.setBubblesAllowed(appPkg, appUid, BUBBLE_PREFERENCE_SELECTED);
835 }
836 mINotificationManager.updateNotificationChannelForPackage(appPkg, appUid, channel);
837 } catch (RemoteException e) {
838 Log.e(TAG, e.getMessage());
839 }
840
841 if (shouldBubble) {
842 mShadeController.collapsePanel(true);
843 if (entry.getRow() != null) {
844 entry.getRow().updateBubbleButton();
845 }
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800846 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800847 }
848
849 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500850 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500851 * <p>
852 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800853 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500854 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500855 void removeBubble(NotificationEntry entry, int reason) {
856 if (mBubbleData.hasBubbleWithKey(entry.getKey())) {
857 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800858 }
859 }
860
Beverlyed8aea22020-01-22 16:52:47 -0500861 private void onEntryAdded(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000862 if (mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700863 && canLaunchInActivityView(mContext, entry)) {
Beverlyed8aea22020-01-22 16:52:47 -0500864 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700865 }
866 }
867
Beverlyed8aea22020-01-22 16:52:47 -0500868 private void onEntryUpdated(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000869 boolean shouldBubble = mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700870 && canLaunchInActivityView(mContext, entry);
Beverlyed8aea22020-01-22 16:52:47 -0500871 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
872 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500873 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500874 } else if (shouldBubble) {
Beverlyed8aea22020-01-22 16:52:47 -0500875 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800876 }
Beverlyed8aea22020-01-22 16:52:47 -0500877 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800878
Beverlya53fb0d2020-01-29 15:26:13 -0500879 private void onEntryRemoved(NotificationEntry entry) {
880 if (isSummaryOfBubbles(entry)) {
881 final String groupKey = entry.getSbn().getGroupKey();
882 mBubbleData.removeSuppressedSummary(groupKey);
883
884 // Remove any associated bubble children with the summary
885 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
886 for (int i = 0; i < bubbleChildren.size(); i++) {
887 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
888 }
889 } else {
890 removeBubble(entry, DISMISS_NOTIF_CANCEL);
891 }
892 }
893
Mady Mellor56515c42020-02-18 17:58:36 -0800894 /**
895 * Called when NotificationListener has received adjusted notification rank and reapplied
896 * filtering and sorting. This is used to dismiss or create bubbles based on changes in
897 * permissions on the notification channel or the global setting.
898 *
899 * @param rankingMap the updated ranking map from NotificationListenerService
900 */
Beverlyed8aea22020-01-22 16:52:47 -0500901 private void onRankingUpdated(RankingMap rankingMap) {
Mady Mellor56515c42020-02-18 17:58:36 -0800902 if (mTmpRanking == null) {
903 mTmpRanking = new NotificationListenerService.Ranking();
904 }
905 String[] orderedKeys = rankingMap.getOrderedKeys();
906 for (int i = 0; i < orderedKeys.length; i++) {
907 String key = orderedKeys[i];
908 NotificationEntry entry = mNotificationEntryManager.getPendingOrActiveNotif(key);
909 rankingMap.getRanking(key, mTmpRanking);
Mady Mellore45ff862020-03-24 15:54:50 -0700910 boolean isActiveBubble = mBubbleData.hasBubbleWithKey(key);
911 if (isActiveBubble && !mTmpRanking.canBubble()) {
Mady Mellor56515c42020-02-18 17:58:36 -0800912 mBubbleData.notificationEntryRemoved(entry, BubbleController.DISMISS_BLOCKED);
Mady Mellore45ff862020-03-24 15:54:50 -0700913 } else if (entry != null && mTmpRanking.isBubble() && !isActiveBubble) {
Mady Mellor56515c42020-02-18 17:58:36 -0800914 entry.setFlagBubble(true);
915 onEntryUpdated(entry);
916 }
917 }
Beverlyed8aea22020-01-22 16:52:47 -0500918 }
Ned Burns01e38212019-01-03 16:32:52 -0500919
Mark Renouf71a3af62019-04-08 15:02:54 -0400920 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400921 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400922
Mark Renouf3bc5b362019-04-05 14:37:59 -0400923 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400924 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800925 // Update bubbles in overflow.
926 if (mOverflowCallback != null) {
927 mOverflowCallback.run();
928 }
929
Mark Renouf82a40e62019-05-23 16:16:24 -0400930 // Collapsing? Do this first before remaining steps.
931 if (update.expandedChanged && !update.expanded) {
932 mStackView.setExpanded(false);
933 }
934
935 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700936 ArrayList<Pair<Bubble, Integer>> removedBubbles =
937 new ArrayList<>(update.removedBubbles);
938 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400939 final Bubble bubble = removed.first;
940 @DismissReason final int reason = removed.second;
941 mStackView.removeBubble(bubble);
Mark Renoufc19b4732019-06-26 12:08:33 -0400942 // If the bubble is removed for user switching, leave the notification in place.
943 if (reason != DISMISS_USER_CHANGED) {
944 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800945 && !bubble.showInShade()) {
Beverlyed8aea22020-01-22 16:52:47 -0500946 // The bubble is now gone & the notification is hidden from the shade, so
947 // time to actually remove it
948 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -0500949 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -0500950 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400951 } else {
952 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400953 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700954 if (bubble.getEntry().getRow() != null) {
955 bubble.getEntry().getRow().updateBubbleButton();
956 }
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700957
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700958 // Update the state in NotificationManagerService
Mark Renoufc19b4732019-06-26 12:08:33 -0400959 try {
960 mBarService.onNotificationBubbleChanged(bubble.getKey(),
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700961 false /* isBubble */, 0 /* flags */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400962 } catch (RemoteException e) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400963 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400964 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700965
Ned Burns00b4b2d2019-10-17 22:09:27 -0400966 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500967 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
968 // Time to potentially remove the summary
969 for (NotifCallback cb : mCallbacks) {
970 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -0700971 }
972 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700973 }
974 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400975
Lyn Hanc47e1712020-01-28 21:43:34 -0800976 if (update.addedBubble != null) {
977 mStackView.addBubble(update.addedBubble);
978 }
979
Mark Renouf82a40e62019-05-23 16:16:24 -0400980 if (update.updatedBubble != null) {
981 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400982 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400983
Lyn Hanb58c7562020-01-07 14:29:20 -0800984 // At this point, the correct bubbles are inflated in the stack.
985 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -0400986 if (update.orderChanged) {
987 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400988 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400989
Mark Renouf82a40e62019-05-23 16:16:24 -0400990 if (update.selectionChanged) {
991 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700992 if (update.selectedBubble != null) {
993 mNotificationGroupManager.updateSuppression(
994 update.selectedBubble.getEntry());
995 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400996 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400997
Mark Renouf82a40e62019-05-23 16:16:24 -0400998 // Expanding? Apply this last.
999 if (update.expandedChanged && update.expanded) {
1000 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001001 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001002
Beverlyed8aea22020-01-22 16:52:47 -05001003 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001004 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001005 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001006 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001007
Issei Suzukia8d07312019-06-07 12:56:19 +02001008 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001009 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001010 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001011 mBubbleData.getSelectedBubble()));
1012
1013 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001014 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001015 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001016 mStackView.getExpandedBubble()));
1017 }
Lyn Hanb58c7562020-01-07 14:29:20 -08001018 Log.d(TAG, "\n[BubbleData] overflow:");
1019 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
1020 null));
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001021 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001022 }
1023 };
1024
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001025 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001026 * We intercept notification entries (including group summaries) dismissed by the user when
1027 * there is an active bubble associated with it. We do this so that developers can still
1028 * cancel it (and hence the bubbles associated with it). However, these intercepted
1029 * notifications should then be hidden from the shade since the user has cancelled them, so we
1030 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1031 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001032 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001033 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001034 */
Beverlya53fb0d2020-01-29 15:26:13 -05001035 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001036 if (entry == null) {
1037 return false;
1038 }
Beverlyed8aea22020-01-22 16:52:47 -05001039
Beverlya53fb0d2020-01-29 15:26:13 -05001040 final boolean interceptBubbleDismissal = mBubbleData.hasBubbleWithKey(entry.getKey())
1041 && entry.isBubble();
1042 final boolean interceptSummaryDismissal = isSummaryOfBubbles(entry);
Beverlyed8aea22020-01-22 16:52:47 -05001043
Beverlya53fb0d2020-01-29 15:26:13 -05001044 if (interceptSummaryDismissal) {
1045 handleSummaryDismissalInterception(entry);
1046 } else if (interceptBubbleDismissal) {
1047 Bubble bubble = mBubbleData.getBubbleWithKey(entry.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001048 bubble.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001049 bubble.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001050 } else {
Beverlyed8aea22020-01-22 16:52:47 -05001051 return false;
1052 }
Beverlya53fb0d2020-01-29 15:26:13 -05001053
1054 // Update the shade
1055 for (NotifCallback cb : mCallbacks) {
1056 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1057 }
1058 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001059 }
1060
Beverlya53fb0d2020-01-29 15:26:13 -05001061 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1062 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001063 return false;
1064 }
Beverlya53fb0d2020-01-29 15:26:13 -05001065
1066 String groupKey = entry.getSbn().getGroupKey();
1067 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1068 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1069 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1070 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1071 return (isSuppressedSummary || isSummary)
1072 && bubbleChildren != null
1073 && !bubbleChildren.isEmpty();
1074 }
1075
1076 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1077 // current children in the row:
1078 final List<NotificationEntry> children = summary.getChildren();
1079 if (children != null) {
1080 for (int i = 0; i < children.size(); i++) {
1081 NotificationEntry child = children.get(i);
1082 if (mBubbleData.hasBubbleWithKey(child.getKey())) {
1083 // Suppress the bubbled child
1084 // As far as group manager is concerned, once a child is no longer shown
1085 // in the shade, it is essentially removed.
1086 Bubble bubbleChild = mBubbleData.getBubbleWithKey(child.getKey());
1087 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1088 bubbleChild.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001089 bubbleChild.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001090 } else {
1091 // non-bubbled children can be removed
1092 for (NotifCallback cb : mCallbacks) {
1093 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1094 }
1095 }
1096 }
1097 }
1098
1099 // And since all children are removed, remove the summary.
1100 mNotificationGroupManager.onEntryRemoved(summary);
1101
1102 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1103 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1104 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001105 }
1106
1107 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001108 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001109 * Updates the visibility of the bubbles based on current state.
1110 * Does not un-bubble, just hides or un-hides. Notifies any
1111 * {@link BubbleStateChangeListener}s of visibility changes.
1112 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001113 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001114 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001115 if (mStackView == null) {
1116 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001117 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001118 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1119 // Bubbles only appear in unlocked shade
1120 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001121 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001122 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001123 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001124
Mady Mellor698d9e82019-08-01 23:11:53 +00001125 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001126 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001127 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001128 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001129 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1130 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1131 }
1132
1133 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001134 }
1135
1136 /**
1137 * Rect indicating the touchable region for the bubble stack / expanded stack.
1138 */
1139 public Rect getTouchableRegion() {
1140 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1141 return null;
1142 }
1143 mStackView.getBoundsOnScreen(mTempRect);
1144 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001145 }
1146
Mady Mellor390bff42019-04-05 15:09:01 -07001147 /**
1148 * The display id of the expanded view, if the stack is expanded and not occluded by the
1149 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1150 */
1151 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001152 if (mStackView == null) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001153 return INVALID_DISPLAY;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001154 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001155 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001156 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Lyn Han9f66c3b2020-03-05 23:59:29 -08001157 final BubbleViewProvider expandedViewProvider = mStackView.getExpandedBubble();
1158 if (defaultDisplay && expandedViewProvider != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001159 && !mNotificationShadeWindowController.getPanelExpanded()) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001160 return expandedViewProvider.getDisplayId();
Mady Mellor390bff42019-04-05 15:09:01 -07001161 }
Lyn Han9f66c3b2020-03-05 23:59:29 -08001162 return INVALID_DISPLAY;
Mady Mellor390bff42019-04-05 15:09:01 -07001163 }
1164
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001165 @VisibleForTesting
1166 BubbleStackView getStackView() {
1167 return mStackView;
1168 }
1169
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001170 /**
1171 * Description of current bubble state.
1172 */
1173 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1174 pw.println("BubbleController state:");
1175 mBubbleData.dump(fd, pw, args);
1176 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001177 if (mStackView != null) {
1178 mStackView.dump(fd, pw, args);
1179 }
1180 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001181 }
1182
Mady Mellore80930e2019-03-21 16:00:45 -07001183 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001184 * This task stack listener is responsible for responding to tasks moved to the front
1185 * which are on the default (main) display. When this happens, expanded bubbles must be
1186 * collapsed so the user may interact with the app which was just moved to the front.
1187 * <p>
1188 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1189 * these calls via a main thread Handler.
1190 */
1191 @MainThread
1192 private class BubbleTaskStackListener extends TaskStackChangeListener {
1193
Mark Renoufcecc77b2019-01-30 16:32:24 -05001194 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001195 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1196 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001197 if (!mStackView.isExpansionAnimating()) {
1198 mBubbleData.setExpanded(false);
1199 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001200 }
1201 }
1202
Mark Renoufcecc77b2019-01-30 16:32:24 -05001203 @Override
Winson Chunge789ff62020-02-24 14:40:23 -08001204 public void onActivityRestartAttempt(RunningTaskInfo task, boolean homeTaskVisible,
1205 boolean clearedTask) {
1206 for (Bubble b : mBubbleData.getBubbles()) {
1207 if (b.getDisplayId() == task.displayId) {
1208 expandStackAndSelectBubble(b.getKey());
1209 return;
1210 }
1211 }
1212 }
1213
1214 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001215 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001216 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001217 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001218 }
1219 }
Mark Renouf446251d2019-04-26 10:22:41 -04001220
1221 @Override
1222 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1223 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1224 mBubbleData.setExpanded(false);
1225 }
1226 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001227
1228 @Override
1229 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001230 if (mStackView == null) {
1231 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001232 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001233 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001234 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001235
1236 @Override
1237 public void onSingleTaskDisplayEmpty(int displayId) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001238 final BubbleViewProvider expandedBubble = mStackView != null
Mady Mellor5186b132019-09-16 17:55:48 -07001239 ? mStackView.getExpandedBubble()
1240 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001241 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1242 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001243 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001244 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001245 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001246 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001247 }
1248
Mady Mellorca0c24c2019-05-16 16:14:32 -07001249 /**
1250 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1251 *
1252 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1253 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1254 *
1255 * @param context the context to use.
1256 * @param entry the entry to bubble.
1257 */
1258 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1259 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Melloraa9ce172020-03-17 10:34:20 -07001260 ? entry.getBubbleMetadata().getIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001261 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001262 if (entry.getBubbleMetadata() != null
1263 && entry.getBubbleMetadata().getShortcutId() != null) {
1264 return true;
1265 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001266 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001267 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001268 return false;
1269 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001270 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1271 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001272 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001273 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001274 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001275 Log.w(TAG, "Unable to send as bubble, "
1276 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001277 + intent);
1278 return false;
1279 }
1280 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001281 Log.w(TAG, "Unable to send as bubble, "
1282 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001283 + intent);
1284 return false;
1285 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001286 return true;
1287 }
1288
Joshua Tsujia19515f2019-02-13 18:02:29 -05001289 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001290 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001291 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001292 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Joshua Tsujiff6b0f22020-03-09 14:55:19 -04001293 if (mStackView != null) {
Joshua Tsujid9422832019-03-05 13:32:37 -05001294 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001295 }
1296 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001297 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001298}