blob: 013f22203fbc868df1aef54729b50eb21d95cd19 [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;
Lyn Han6cb4e5f2020-04-27 15:11:18 -070057import android.os.Handler;
Mark Renoufcecc77b2019-01-30 16:32:24 -050058import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080059import android.os.ServiceManager;
Mady Mellor56515c42020-02-18 17:58:36 -080060import android.service.notification.NotificationListenerService;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040061import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040062import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040063import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040064import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040065import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040066import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050067import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080068import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080069import android.widget.FrameLayout;
70
Mark Renouf08bc42a2019-03-07 13:01:59 -050071import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050072import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040073import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050074
Mady Mellorebdbbb92018-11-15 14:36:48 -080075import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070076import com.android.internal.statusbar.IStatusBarService;
Beverlya53fb0d2020-01-29 15:26:13 -050077import com.android.internal.statusbar.NotificationVisibility;
Beverlya53fb0d2020-01-29 15:26:13 -050078import com.android.systemui.Dumpable;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080079import com.android.systemui.R;
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +010080import com.android.systemui.bubbles.dagger.BubbleModule;
Ned Burnsaaeb44b2020-02-12 23:48:26 -050081import com.android.systemui.dump.DumpManager;
Joshua Tsujibe60a582020-03-23 17:17:26 -040082import com.android.systemui.model.SysUiState;
Beverly8fdb5332019-02-04 14:29:49 -050083import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050084import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000085import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050086import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050087import com.android.systemui.shared.system.WindowManagerWrapper;
Beverlya53fb0d2020-01-29 15:26:13 -050088import com.android.systemui.statusbar.FeatureFlags;
Mark Renoufc19b4732019-06-26 12:08:33 -040089import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070090import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Mady Mellor9adfe6a2020-03-30 17:23:26 -070091import com.android.systemui.statusbar.notification.NotificationChannelHelper;
Ned Burns01e38212019-01-03 16:32:52 -050092import com.android.systemui.statusbar.notification.NotificationEntryListener;
93import com.android.systemui.statusbar.notification.NotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -050094import com.android.systemui.statusbar.notification.collection.NotifCollection;
95import com.android.systemui.statusbar.notification.collection.NotifPipeline;
Ned Burnsf81c4c42019-01-07 14:10:43 -050096import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Beverlya53fb0d2020-01-29 15:26:13 -050097import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener;
Beverly Taid1e175c2020-03-10 16:37:04 +000098import com.android.systemui.statusbar.notification.interruption.NotificationInterruptStateProvider;
Mady Mellor22f2f072019-04-18 13:26:18 -070099import com.android.systemui.statusbar.phone.NotificationGroupManager;
wilsonshihe8321942019-10-18 18:39:46 +0800100import com.android.systemui.statusbar.phone.NotificationShadeWindowController;
Mady Mellor7f234902019-10-20 12:06:29 -0700101import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorf3b9fab2019-11-13 17:27:32 -0800102import com.android.systemui.statusbar.phone.StatusBar;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700103import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400104import com.android.systemui.statusbar.policy.ZenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500105import com.android.systemui.util.FloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800106
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700107import java.io.FileDescriptor;
108import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500109import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -0400110import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -0700111import java.util.ArrayList;
Lyn Hanb58c7562020-01-07 14:29:20 -0800112import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500113
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800114/**
115 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
116 * Bubbles can be expanded to show more content.
117 *
118 * The controller manages addition, removal, and visible state of bubbles on screen.
119 */
Beverlya53fb0d2020-01-29 15:26:13 -0500120public class BubbleController implements ConfigurationController.ConfigurationListener, Dumpable {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800121
Issei Suzukia8d07312019-06-07 12:56:19 +0200122 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800123
Mark Renouf08bc42a2019-03-07 13:01:59 -0500124 @Retention(SOURCE)
125 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400126 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Lyn Han2f6e89d2020-04-15 10:01:01 -0700127 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT,
128 DISMISS_OVERFLOW_MAX_REACHED})
Mark Renoufba5ab512019-05-02 15:21:01 -0400129 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500130 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700131
Mark Renouf08bc42a2019-03-07 13:01:59 -0500132 static final int DISMISS_USER_GESTURE = 1;
133 static final int DISMISS_AGED = 2;
134 static final int DISMISS_TASK_FINISHED = 3;
135 static final int DISMISS_BLOCKED = 4;
136 static final int DISMISS_NOTIF_CANCEL = 5;
137 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700138 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400139 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700140 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700141 static final int DISMISS_INVALID_INTENT = 10;
Lyn Han2f6e89d2020-04-15 10:01:01 -0700142 static final int DISMISS_OVERFLOW_MAX_REACHED = 11;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500143
Ned Burns01e38212019-01-03 16:32:52 -0500144 private final Context mContext;
145 private final NotificationEntryManager mNotificationEntryManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500146 private final NotifPipeline mNotifPipeline;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500147 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800148 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800149 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100150 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700151 private final NotificationGroupManager mNotificationGroupManager;
Heemin Seogba6337f2019-12-10 15:34:37 -0800152 private final ShadeController mShadeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500153 private final FloatingContentCoordinator mFloatingContentCoordinator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800154
Mady Mellor3dff9e62019-02-05 18:12:53 -0800155 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400156 @Nullable private BubbleStackView mStackView;
Mady Mellor247ca2c2019-12-02 16:18:59 -0800157 private BubbleIconFactory mBubbleIconFactory;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800158
Mark Renoufc19b4732019-06-26 12:08:33 -0400159 // Tracks the id of the current (foreground) user.
160 private int mCurrentUserId;
161 // Saves notification keys of active bubbles when users are switched.
162 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
163
Mady Mellor56515c42020-02-18 17:58:36 -0800164 // Used when ranking updates occur and we check if things should bubble / unbubble
165 private NotificationListenerService.Ranking mTmpRanking;
166
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800167 // Bubbles get added to the status bar view
wilsonshihe8321942019-10-18 18:39:46 +0800168 private final NotificationShadeWindowController mNotificationShadeWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400169 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800170 private StatusBarStateListener mStatusBarStateListener;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700171 private INotificationManager mINotificationManager;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500172
Lyn Hanb58c7562020-01-07 14:29:20 -0800173 // Callback that updates BubbleOverflowActivity on data change.
Lyn Hane4274be2020-04-24 17:55:36 -0700174 @Nullable private BubbleData.Listener mOverflowListener = null;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800175
Beverly Taid1e175c2020-03-10 16:37:04 +0000176 private final NotificationInterruptStateProvider mNotificationInterruptStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700177 private IStatusBarService mBarService;
Joshua Tsujibe60a582020-03-23 17:17:26 -0400178 private SysUiState mSysUiState;
Mady Mellorb4991e62019-01-10 15:14:51 -0800179
Lyn Han6cb4e5f2020-04-27 15:11:18 -0700180 // Used to post to main UI thread
181 private Handler mHandler = new Handler();
182
Mady Mellord1c78b262018-11-06 18:04:40 -0800183 // Used for determining view rect for touch interaction
184 private Rect mTempRect = new Rect();
185
Mark Renoufc19b4732019-06-26 12:08:33 -0400186 // Listens to user switch so bubbles can be saved and restored.
187 private final NotificationLockscreenUserManager mNotifUserManager;
188
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400189 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
190 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
191
Mady Mellor3df7ab02019-12-09 15:07:10 -0800192 private boolean mInflateSynchronously;
193
Beverlyed8aea22020-01-22 16:52:47 -0500194 // TODO (b/145659174): allow for multiple callbacks to support the "shadow" new notif pipeline
195 private final List<NotifCallback> mCallbacks = new ArrayList<>();
196
Mady Mellor5549dd22018-11-06 18:07:34 -0800197 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800198 * Listener to be notified when some states of the bubbles change.
199 */
200 public interface BubbleStateChangeListener {
201 /**
202 * Called when the stack has bubbles or no longer has bubbles.
203 */
204 void onHasBubblesChanged(boolean hasBubbles);
205 }
206
Mady Mellorcd9b1302018-11-06 18:08:04 -0800207 /**
208 * Listener to find out about stack expansion / collapse events.
209 */
210 public interface BubbleExpandListener {
211 /**
212 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700213 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800214 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800215 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800216 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800217 void onBubbleExpandChanged(boolean isExpanding, String key);
218 }
219
220 /**
Mady Mellorf44b6832020-01-14 13:26:14 -0800221 * Listener to be notified when a bubbles' notification suppression state changes.
222 */
223 public interface NotificationSuppressionChangedListener {
224 /**
225 * Called when the notification suppression state of a bubble changes.
226 */
227 void onBubbleNotificationSuppressionChange(Bubble bubble);
Beverlyed8aea22020-01-22 16:52:47 -0500228 }
Mady Mellorf44b6832020-01-14 13:26:14 -0800229
Beverlyed8aea22020-01-22 16:52:47 -0500230 /**
231 * Callback for when the BubbleController wants to interact with the notification pipeline to:
232 * - Remove a previously bubbled notification
233 * - Update the notification shade since bubbled notification should/shouldn't be showing
234 */
235 public interface NotifCallback {
236 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500237 * Called when a bubbled notification that was hidden from the shade is now being removed
238 * This can happen when an app cancels a bubbled notification or when the user dismisses a
239 * bubble.
Beverlyed8aea22020-01-22 16:52:47 -0500240 */
Beverlya53fb0d2020-01-29 15:26:13 -0500241 void removeNotification(NotificationEntry entry, int reason);
Beverlyed8aea22020-01-22 16:52:47 -0500242
243 /**
244 * Called when a bubbled notification has changed whether it should be
245 * filtered from the shade.
246 */
Beverlya53fb0d2020-01-29 15:26:13 -0500247 void invalidateNotifications(String reason);
Beverlyed8aea22020-01-22 16:52:47 -0500248
249 /**
250 * Called on a bubbled entry that has been removed when there are no longer
251 * bubbled entries in its group.
252 *
253 * Checks whether its group has any other (non-bubbled) children. If it doesn't,
254 * removes all remnants of the group's summary from the notification pipeline.
255 * TODO: (b/145659174) Only old pipeline needs this - delete post-migration.
256 */
257 void maybeCancelSummary(NotificationEntry entry);
Mady Mellorf44b6832020-01-14 13:26:14 -0800258 }
259
260 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800261 * Listens for the current state of the status bar and updates the visibility state
262 * of bubbles as needed.
263 */
264 private class StatusBarStateListener implements StatusBarStateController.StateListener {
265 private int mState;
266 /**
267 * Returns the current status bar state.
268 */
269 public int getCurrentState() {
270 return mState;
271 }
272
273 @Override
274 public void onStateChanged(int newState) {
275 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400276 boolean shouldCollapse = (mState != SHADE);
277 if (shouldCollapse) {
278 collapseStack();
279 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700280 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800281 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800282 }
283
Mady Mellor7f234902019-10-20 12:06:29 -0700284 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800285 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700286 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800287 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700288 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700289 ConfigurationController configurationController,
Beverly Taid1e175c2020-03-10 16:37:04 +0000290 NotificationInterruptStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400291 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700292 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700293 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500294 NotificationEntryManager entryManager,
295 NotifPipeline notifPipeline,
296 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500297 DumpManager dumpManager,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400298 FloatingContentCoordinator floatingContentCoordinator,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700299 SysUiState sysUiState,
300 INotificationManager notificationManager) {
wilsonshihe8321942019-10-18 18:39:46 +0800301 this(context, notificationShadeWindowController, statusBarStateController, shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700302 data, null /* synchronizer */, configurationController, interruptionStateProvider,
Beverlya53fb0d2020-01-29 15:26:13 -0500303 zenModeController, notifUserManager, groupManager, entryManager,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700304 notifPipeline, featureFlags, dumpManager, floatingContentCoordinator, sysUiState,
305 notificationManager);
Mady Mellor7f234902019-10-20 12:06:29 -0700306 }
307
Sergey Nikolaienkov5cb6e522020-02-10 17:33:00 +0100308 /**
309 * Injected constructor. See {@link BubbleModule}.
310 */
Mady Mellor7f234902019-10-20 12:06:29 -0700311 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800312 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700313 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800314 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700315 BubbleData data,
316 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
317 ConfigurationController configurationController,
Beverly Taid1e175c2020-03-10 16:37:04 +0000318 NotificationInterruptStateProvider interruptionStateProvider,
Mady Mellor7f234902019-10-20 12:06:29 -0700319 ZenModeController zenModeController,
320 NotificationLockscreenUserManager notifUserManager,
321 NotificationGroupManager groupManager,
Beverlya53fb0d2020-01-29 15:26:13 -0500322 NotificationEntryManager entryManager,
323 NotifPipeline notifPipeline,
324 FeatureFlags featureFlags,
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500325 DumpManager dumpManager,
Joshua Tsujibe60a582020-03-23 17:17:26 -0400326 FloatingContentCoordinator floatingContentCoordinator,
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700327 SysUiState sysUiState,
328 INotificationManager notificationManager) {
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500329 dumpManager.registerDumpable(TAG, this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800330 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800331 mShadeController = shadeController;
Beverly Taid1e175c2020-03-10 16:37:04 +0000332 mNotificationInterruptStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400333 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400334 mZenModeController = zenModeController;
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500335 mFloatingContentCoordinator = floatingContentCoordinator;
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700336 mINotificationManager = notificationManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400337 mZenModeController.addCallback(new ZenModeController.Callback() {
338 @Override
339 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800340 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400341 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700342 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400343 }
344
345 @Override
346 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800347 for (Bubble b : mBubbleData.getBubbles()) {
Joshua Tsuji2ed260e2020-03-26 14:26:01 -0400348 b.setShowDot(b.showInShade());
Mady Mellordf48d0a2019-06-25 18:26:46 -0700349 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400350 }
351 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700352
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700353 configurationController.addCallback(this /* configurationListener */);
Joshua Tsujibe60a582020-03-23 17:17:26 -0400354 mSysUiState = sysUiState;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800355
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400356 mBubbleData = data;
357 mBubbleData.setListener(mBubbleDataListener);
Mady Mellorf44b6832020-01-14 13:26:14 -0800358 mBubbleData.setSuppressionChangedListener(new NotificationSuppressionChangedListener() {
359 @Override
360 public void onBubbleNotificationSuppressionChange(Bubble bubble) {
361 // Make sure NoMan knows it's not showing in the shade anymore so anyone querying it
362 // can tell.
363 try {
364 mBarService.onBubbleNotificationSuppressionChanged(bubble.getKey(),
365 !bubble.showInShade());
366 } catch (RemoteException e) {
367 // Bad things have happened
368 }
369 }
370 });
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400371
Mady Mellor7f234902019-10-20 12:06:29 -0700372 mNotificationEntryManager = entryManager;
Mady Mellor22f2f072019-04-18 13:26:18 -0700373 mNotificationGroupManager = groupManager;
Beverlya53fb0d2020-01-29 15:26:13 -0500374 mNotifPipeline = notifPipeline;
375
376 if (!featureFlags.isNewNotifPipelineRenderingEnabled()) {
377 setupNEM();
378 } else {
379 setupNotifPipeline();
380 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800381
wilsonshihe8321942019-10-18 18:39:46 +0800382 mNotificationShadeWindowController = notificationShadeWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800383 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700384 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500385
Mark Renoufcecc77b2019-01-30 16:32:24 -0500386 mTaskStackListener = new BubbleTaskStackListener();
387 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800388
Joshua Tsujia19515f2019-02-13 18:02:29 -0500389 try {
390 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
391 } catch (RemoteException e) {
392 e.printStackTrace();
393 }
Issei Suzukic0387542019-03-08 17:31:14 +0100394 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700395
396 mBarService = IStatusBarService.Stub.asInterface(
397 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400398
399 mSavedBubbleKeysPerUser = new SparseSetArray<>();
400 mCurrentUserId = mNotifUserManager.getCurrentUserId();
401 mNotifUserManager.addUserChangedListener(
Steve Elliottb47f1c72019-12-19 12:39:26 -0500402 new NotificationLockscreenUserManager.UserChangedListener() {
403 @Override
404 public void onUserChanged(int newUserId) {
405 BubbleController.this.saveBubbles(mCurrentUserId);
406 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
407 BubbleController.this.restoreBubbles(newUserId);
408 mCurrentUserId = newUserId;
409 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400410 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800411
Mady Mellor247ca2c2019-12-02 16:18:59 -0800412 mBubbleIconFactory = new BubbleIconFactory(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800413 }
414
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400415 /**
Beverlyed8aea22020-01-22 16:52:47 -0500416 * See {@link NotifCallback}.
417 */
418 public void addNotifCallback(NotifCallback callback) {
419 mCallbacks.add(callback);
420 }
421
422 private void setupNEM() {
423 mNotificationEntryManager.addNotificationEntryListener(
424 new NotificationEntryListener() {
425 @Override
Mady Mellorf9439ab2020-01-30 16:06:53 -0800426 public void onPendingEntryAdded(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -0500427 onEntryAdded(entry);
428 }
429
430 @Override
431 public void onPreEntryUpdated(NotificationEntry entry) {
432 onEntryUpdated(entry);
433 }
434
435 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500436 public void onEntryRemoved(
437 NotificationEntry entry,
438 @android.annotation.Nullable NotificationVisibility visibility,
Julia Reynolds138111f2020-02-26 11:17:39 -0500439 boolean removedByUser,
440 int reason) {
Beverlya53fb0d2020-01-29 15:26:13 -0500441 BubbleController.this.onEntryRemoved(entry);
442 }
443
444 @Override
Beverlyed8aea22020-01-22 16:52:47 -0500445 public void onNotificationRankingUpdated(RankingMap rankingMap) {
446 onRankingUpdated(rankingMap);
447 }
448 });
449
Evan Laird04373662020-01-24 17:37:39 -0500450 mNotificationEntryManager.addNotificationRemoveInterceptor(
Beverlyed8aea22020-01-22 16:52:47 -0500451 new NotificationRemoveInterceptor() {
452 @Override
Evan Laird04373662020-01-24 17:37:39 -0500453 public boolean onNotificationRemoveRequested(
Beverlya53fb0d2020-01-29 15:26:13 -0500454 String key,
455 NotificationEntry entry,
456 int dismissReason) {
457 final boolean isClearAll = dismissReason == REASON_CANCEL_ALL;
458 final boolean isUserDimiss = dismissReason == REASON_CANCEL
459 || dismissReason == REASON_CLICK;
460 final boolean isAppCancel = dismissReason == REASON_APP_CANCEL
461 || dismissReason == REASON_APP_CANCEL_ALL;
462 final boolean isSummaryCancel =
463 dismissReason == REASON_GROUP_SUMMARY_CANCELED;
464
465 // Need to check for !appCancel here because the notification may have
466 // previously been dismissed & entry.isRowDismissed would still be true
467 boolean userRemovedNotif =
468 (entry != null && entry.isRowDismissed() && !isAppCancel)
469 || isClearAll || isUserDimiss || isSummaryCancel;
470
Mady Mellordd6fe612020-04-15 11:47:55 -0700471 if (userRemovedNotif) {
Beverlya53fb0d2020-01-29 15:26:13 -0500472 return handleDismissalInterception(entry);
473 }
Beverlya53fb0d2020-01-29 15:26:13 -0500474 return false;
Beverlyed8aea22020-01-22 16:52:47 -0500475 }
476 });
477
478 mNotificationGroupManager.addOnGroupChangeListener(
479 new NotificationGroupManager.OnGroupChangeListener() {
480 @Override
481 public void onGroupSuppressionChanged(
482 NotificationGroupManager.NotificationGroup group,
483 boolean suppressed) {
484 // More notifications could be added causing summary to no longer
485 // be suppressed -- in this case need to remove the key.
486 final String groupKey = group.summary != null
487 ? group.summary.getSbn().getGroupKey()
488 : null;
489 if (!suppressed && groupKey != null
490 && mBubbleData.isSummarySuppressed(groupKey)) {
491 mBubbleData.removeSuppressedSummary(groupKey);
492 }
493 }
494 });
495
496 addNotifCallback(new NotifCallback() {
497 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500498 public void removeNotification(NotificationEntry entry, int reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500499 mNotificationEntryManager.performRemoveNotification(entry.getSbn(),
Beverlya53fb0d2020-01-29 15:26:13 -0500500 reason);
Beverlyed8aea22020-01-22 16:52:47 -0500501 }
502
503 @Override
Beverlya53fb0d2020-01-29 15:26:13 -0500504 public void invalidateNotifications(String reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500505 mNotificationEntryManager.updateNotifications(reason);
506 }
507
508 @Override
509 public void maybeCancelSummary(NotificationEntry entry) {
510 // Check if removed bubble has an associated suppressed group summary that needs
511 // to be removed now.
Beverlya53fb0d2020-01-29 15:26:13 -0500512 final String groupKey = entry.getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500513 if (mBubbleData.isSummarySuppressed(groupKey)) {
Beverlya53fb0d2020-01-29 15:26:13 -0500514 mBubbleData.removeSuppressedSummary(groupKey);
Beverlyed8aea22020-01-22 16:52:47 -0500515
516 final NotificationEntry summary =
517 mNotificationEntryManager.getActiveNotificationUnfiltered(
518 mBubbleData.getSummaryKey(groupKey));
Beverlya53fb0d2020-01-29 15:26:13 -0500519 if (summary != null) {
520 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
521 UNDEFINED_DISMISS_REASON);
522 }
Beverlyed8aea22020-01-22 16:52:47 -0500523 }
524
Beverlya53fb0d2020-01-29 15:26:13 -0500525 // Check if we still need to remove the summary from NoManGroup because the summary
526 // may not be in the mBubbleData.mSuppressedGroupKeys list and removed above.
527 // For example:
528 // 1. Bubbled notifications (group) is posted to shade and are visible bubbles
529 // 2. User expands bubbles so now their respective notifications in the shade are
530 // hidden, including the group summary
531 // 3. User removes all bubbles
532 // 4. We expect all the removed bubbles AND the summary (note: the summary was
533 // never added to the suppressedSummary list in BubbleData, so we add this check)
Beverlyed8aea22020-01-22 16:52:47 -0500534 NotificationEntry summary =
535 mNotificationGroupManager.getLogicalGroupSummary(entry.getSbn());
536 if (summary != null) {
537 ArrayList<NotificationEntry> summaryChildren =
538 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
539 boolean isSummaryThisNotif = summary.getKey().equals(entry.getKey());
540 if (!isSummaryThisNotif && (summaryChildren == null
541 || summaryChildren.isEmpty())) {
542 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
543 UNDEFINED_DISMISS_REASON);
544 }
545 }
546 }
547 });
548 }
549
Beverlya53fb0d2020-01-29 15:26:13 -0500550 private void setupNotifPipeline() {
551 mNotifPipeline.addCollectionListener(new NotifCollectionListener() {
552 @Override
553 public void onEntryAdded(NotificationEntry entry) {
554 BubbleController.this.onEntryAdded(entry);
555 }
556
557 @Override
558 public void onEntryUpdated(NotificationEntry entry) {
559 BubbleController.this.onEntryUpdated(entry);
560 }
561
562 @Override
563 public void onRankingUpdate(RankingMap rankingMap) {
564 onRankingUpdated(rankingMap);
565 }
566
567 @Override
568 public void onEntryRemoved(NotificationEntry entry,
569 @NotifCollection.CancellationReason int reason) {
570 BubbleController.this.onEntryRemoved(entry);
571 }
572 });
573 }
574
Beverlyed8aea22020-01-22 16:52:47 -0500575 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800576 * Sets whether to perform inflation on the same thread as the caller. This method should only
577 * be used in tests, not in production.
578 */
579 @VisibleForTesting
580 void setInflateSynchronously(boolean inflateSynchronously) {
581 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800582 }
583
Lyn Hane4274be2020-04-24 17:55:36 -0700584 void setOverflowListener(BubbleData.Listener listener) {
585 mOverflowListener = listener;
Lyn Hanb58c7562020-01-07 14:29:20 -0800586 }
587
588 /**
589 * @return Bubbles for updating overflow.
590 */
591 List<Bubble> getOverflowBubbles() {
592 return mBubbleData.getOverflowBubbles();
593 }
594
595
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400596 /**
597 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
598 * method initializes the stack view and adds it to the StatusBar just above the scrim.
599 */
600 private void ensureStackViewCreated() {
601 if (mStackView == null) {
Joshua Tsuji7155bf12020-02-13 16:14:29 -0500602 mStackView = new BubbleStackView(
Joshua Tsujibe60a582020-03-23 17:17:26 -0400603 mContext, mBubbleData, mSurfaceSynchronizer, mFloatingContentCoordinator,
Joshua Tsujiba9fef02020-04-09 17:40:35 -0400604 mSysUiState, mNotificationShadeWindowController);
wilsonshihe8321942019-10-18 18:39:46 +0800605 ViewGroup nsv = mNotificationShadeWindowController.getNotificationShadeView();
606 int bubbleScrimIndex = nsv.indexOfChild(nsv.findViewById(R.id.scrim_for_bubble));
Lyn Hanbde48202019-05-29 19:18:29 -0700607 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
wilsonshihe8321942019-10-18 18:39:46 +0800608 nsv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400609 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
610 if (mExpandListener != null) {
611 mStackView.setExpandListener(mExpandListener);
612 }
Joshua Tsuji6855cab2020-04-16 01:05:39 -0400613
614 mStackView.setUnbubbleConversationCallback(notificationEntry ->
615 onUserChangedBubble(notificationEntry, false /* shouldBubble */));
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400616 }
617 }
618
Mark Renoufc19b4732019-06-26 12:08:33 -0400619 /**
620 * Records the notification key for any active bubbles. These are used to restore active
621 * bubbles when the user returns to the foreground.
622 *
623 * @param userId the id of the user
624 */
625 private void saveBubbles(@UserIdInt int userId) {
626 // First clear any existing keys that might be stored.
627 mSavedBubbleKeysPerUser.remove(userId);
628 // Add in all active bubbles for the current user.
629 for (Bubble bubble: mBubbleData.getBubbles()) {
630 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
631 }
632 }
633
634 /**
635 * Promotes existing notifications to Bubbles if they were previously bubbles.
636 *
637 * @param userId the id of the user
638 */
639 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400640 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
641 if (savedBubbleKeys == null) {
642 // There were no bubbles saved for this used.
643 return;
644 }
Evan Laird181de622019-10-24 09:53:02 -0400645 for (NotificationEntry e :
646 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400647 if (savedBubbleKeys.contains(e.getKey())
Beverly Taid1e175c2020-03-10 16:37:04 +0000648 && mNotificationInterruptStateProvider.shouldBubbleUp(e)
Mark Renoufc19b4732019-06-26 12:08:33 -0400649 && canLaunchInActivityView(mContext, e)) {
650 updateBubble(e, /* suppressFlyout= */ true);
651 }
652 }
653 // Finally, remove the entries for this user now that bubbles are restored.
654 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
655 }
656
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700657 @Override
658 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800659 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700660 }
661
662 @Override
663 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800664 updateForThemeChanges();
665 }
666
667 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700668 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700669 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700670 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800671 mBubbleIconFactory = new BubbleIconFactory(mContext);
Lyn Handa9a6a22020-04-24 13:21:43 -0700672 // Reload each bubble
Mady Mellor3df7ab02019-12-09 15:07:10 -0800673 for (Bubble b: mBubbleData.getBubbles()) {
Lyn Handa9a6a22020-04-24 13:21:43 -0700674 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
675 }
676 for (Bubble b: mBubbleData.getOverflowBubbles()) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800677 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
678 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700679 }
680
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400681 @Override
682 public void onConfigChanged(Configuration newConfig) {
683 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400684 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700685 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400686 }
687 }
688
Mady Mellor5549dd22018-11-06 18:07:34 -0800689 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800690 * Set a listener to be notified when some states of the bubbles change.
691 */
692 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
693 mStateChangeListener = listener;
694 }
695
696 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800697 * Set a listener to be notified of bubble expand events.
698 */
699 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100700 mExpandListener = ((isExpanding, key) -> {
701 if (listener != null) {
702 listener.onBubbleExpandChanged(isExpanding, key);
703 }
wilsonshihe8321942019-10-18 18:39:46 +0800704 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100705 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800706 if (mStackView != null) {
707 mStackView.setExpandListener(mExpandListener);
708 }
709 }
710
711 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800712 * Whether or not there are bubbles present, regardless of them being visible on the
713 * screen (e.g. if on AOD).
714 */
715 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800716 if (mStackView == null) {
717 return false;
718 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400719 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800720 }
721
722 /**
723 * Whether the stack of bubbles is expanded or not.
724 */
725 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400726 return mBubbleData.isExpanded();
727 }
728
729 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800730 * Tell the stack of bubbles to collapse.
731 */
732 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400733 mBubbleData.setExpanded(false /* expanded */);
734 }
735
Mady Mellorce23c462019-06-17 17:30:07 -0700736 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700737 * True if either:
738 * (1) There is a bubble associated with the provided key and if its notification is hidden
739 * from the shade.
740 * (2) There is a group summary associated with the provided key that is hidden from the shade
741 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700742 *
Mady Mellore28fe102019-07-09 15:33:32 -0700743 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700744 */
Beverlyed8aea22020-01-22 16:52:47 -0500745 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
746 String key = entry.getKey();
Lyn Han2f6e89d2020-04-15 10:01:01 -0700747 boolean isSuppressedBubble = (mBubbleData.hasAnyBubbleWithKey(key)
748 && !mBubbleData.getAnyBubbleWithkey(key).showInShade());
Beverlyed8aea22020-01-22 16:52:47 -0500749
750 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700751 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700752 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Lyn Han2f6e89d2020-04-15 10:01:01 -0700753 return (isSummary && isSuppressedSummary) || isSuppressedBubble;
Mady Mellorce23c462019-06-17 17:30:07 -0700754 }
755
Lyn Hanb58c7562020-01-07 14:29:20 -0800756 void promoteBubbleFromOverflow(Bubble bubble) {
Lyn Han1e19d7f2020-02-05 19:10:58 -0800757 bubble.setInflateSynchronously(mInflateSynchronously);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700758 setIsBubble(bubble, /* isBubble */ true);
Lyn Han1e19d7f2020-02-05 19:10:58 -0800759 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
Lyn Hanb58c7562020-01-07 14:29:20 -0800760 }
761
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800762 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400763 * Request the stack expand if needed, then select the specified Bubble as current.
764 *
765 * @param notificationKey the notification key for the bubble to be selected
766 */
767 public void expandStackAndSelectBubble(String notificationKey) {
Lyn Han2f6e89d2020-04-15 10:01:01 -0700768 Bubble bubble = mBubbleData.getBubbleInStackWithKey(notificationKey);
769 if (bubble == null) {
770 bubble = mBubbleData.getOverflowBubbleWithKey(notificationKey);
771 if (bubble != null) {
772 mBubbleData.promoteBubbleFromOverflow(bubble, mStackView, mBubbleIconFactory);
773 }
774 } else if (bubble.getEntry().isBubble()){
Mark Renouf71a3af62019-04-08 15:02:54 -0400775 mBubbleData.setSelectedBubble(bubble);
Mark Renouffec45da2019-03-13 13:24:27 -0400776 }
Lyn Han2f6e89d2020-04-15 10:01:01 -0700777 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400778 }
779
780 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500781 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
782 * is forwarded a back key down/up pair.
783 */
784 public void performBackPressIfNeeded() {
785 if (mStackView != null) {
786 mStackView.performBackPressIfNeeded();
787 }
788 }
789
790 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800791 * Adds or updates a bubble associated with the provided notification entry.
792 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400793 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800794 */
Mark Renouff97ed462019-04-05 13:46:24 -0400795 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700796 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400797 }
798
799 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700800 updateBubble(notif, suppressFlyout, true /* showInShade */);
801 }
802
803 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800804 if (mStackView == null) {
805 // Lazy init stack view when a bubble is created
806 ensureStackViewCreated();
807 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700808 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400809 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700810 notif.setInterruption();
811 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800812 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
813 bubble.setInflateSynchronously(mInflateSynchronously);
814 bubble.inflate(
Lyn Han6cb4e5f2020-04-27 15:11:18 -0700815 b -> {
816 mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade);
817 if (bubble.getBubbleIntent() == null) {
818 return;
819 }
820 bubble.getBubbleIntent().registerCancelListener(pendingIntent -> {
821 if (bubble.getWasAccessed()) {
822 bubble.setPendingIntentCanceled();
823 return;
824 }
825 mHandler.post(
826 () -> removeBubble(bubble.getEntry(),
827 BubbleController.DISMISS_INVALID_INTENT));
828 });
829 },
Mady Mellor3df7ab02019-12-09 15:07:10 -0800830 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700831 }
832
833 /**
834 * Called when a user has indicated that an active notification should be shown as a bubble.
835 * <p>
836 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
837 * the notification from appearing in the shade.
838 *
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700839 * @param entry the notification to change bubble state for.
840 * @param shouldBubble whether the notification should show as a bubble or not.
Mady Mellor7f234902019-10-20 12:06:29 -0700841 */
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700842 public void onUserChangedBubble(NotificationEntry entry, boolean shouldBubble) {
843 NotificationChannel channel = entry.getChannel();
844 final String appPkg = entry.getSbn().getPackageName();
845 final int appUid = entry.getSbn().getUid();
846 if (channel == null || appPkg == null) {
847 return;
Mady Mellorff076eb2019-11-13 10:12:06 -0800848 }
Mady Mellor7f234902019-10-20 12:06:29 -0700849
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700850 // Update the state in NotificationManagerService
851 try {
852 int flags = Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION;
853 mBarService.onNotificationBubbleChanged(entry.getKey(), shouldBubble, flags);
854 } catch (RemoteException e) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800855 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -0700856
857 // Change the settings
858 channel = NotificationChannelHelper.createConversationChannelIfNeeded(mContext,
859 mINotificationManager, entry, channel);
860 channel.setAllowBubbles(shouldBubble);
861 try {
862 int currentPref = mINotificationManager.getBubblePreferenceForPackage(appPkg, appUid);
863 if (shouldBubble && currentPref == BUBBLE_PREFERENCE_NONE) {
864 mINotificationManager.setBubblesAllowed(appPkg, appUid, BUBBLE_PREFERENCE_SELECTED);
865 }
866 mINotificationManager.updateNotificationChannelForPackage(appPkg, appUid, channel);
867 } catch (RemoteException e) {
868 Log.e(TAG, e.getMessage());
869 }
870
871 if (shouldBubble) {
872 mShadeController.collapsePanel(true);
873 if (entry.getRow() != null) {
874 entry.getRow().updateBubbleButton();
875 }
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800876 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800877 }
878
879 /**
Beverlya53fb0d2020-01-29 15:26:13 -0500880 * Removes the bubble with the given NotificationEntry.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500881 * <p>
882 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800883 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500884 @MainThread
Beverlya53fb0d2020-01-29 15:26:13 -0500885 void removeBubble(NotificationEntry entry, int reason) {
Lyn Han2f6e89d2020-04-15 10:01:01 -0700886 if (mBubbleData.hasAnyBubbleWithKey(entry.getKey())) {
Beverlya53fb0d2020-01-29 15:26:13 -0500887 mBubbleData.notificationEntryRemoved(entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800888 }
889 }
890
Beverlyed8aea22020-01-22 16:52:47 -0500891 private void onEntryAdded(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000892 if (mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700893 && canLaunchInActivityView(mContext, entry)) {
Beverlyed8aea22020-01-22 16:52:47 -0500894 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700895 }
896 }
897
Beverlyed8aea22020-01-22 16:52:47 -0500898 private void onEntryUpdated(NotificationEntry entry) {
Beverly Taid1e175c2020-03-10 16:37:04 +0000899 boolean shouldBubble = mNotificationInterruptStateProvider.shouldBubbleUp(entry)
Mady Mellordd6fe612020-04-15 11:47:55 -0700900 && canLaunchInActivityView(mContext, entry);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700901 if (!shouldBubble && mBubbleData.hasAnyBubbleWithKey(entry.getKey())) {
Beverlyed8aea22020-01-22 16:52:47 -0500902 // It was previously a bubble but no longer a bubble -- lets remove it
Beverlya53fb0d2020-01-29 15:26:13 -0500903 removeBubble(entry, DISMISS_NO_LONGER_BUBBLE);
Beverlyed8aea22020-01-22 16:52:47 -0500904 } else if (shouldBubble) {
Beverlyed8aea22020-01-22 16:52:47 -0500905 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800906 }
Beverlyed8aea22020-01-22 16:52:47 -0500907 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800908
Beverlya53fb0d2020-01-29 15:26:13 -0500909 private void onEntryRemoved(NotificationEntry entry) {
910 if (isSummaryOfBubbles(entry)) {
911 final String groupKey = entry.getSbn().getGroupKey();
912 mBubbleData.removeSuppressedSummary(groupKey);
913
914 // Remove any associated bubble children with the summary
915 final List<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
916 for (int i = 0; i < bubbleChildren.size(); i++) {
917 removeBubble(bubbleChildren.get(i).getEntry(), DISMISS_GROUP_CANCELLED);
918 }
919 } else {
920 removeBubble(entry, DISMISS_NOTIF_CANCEL);
921 }
922 }
923
Mady Mellor56515c42020-02-18 17:58:36 -0800924 /**
925 * Called when NotificationListener has received adjusted notification rank and reapplied
926 * filtering and sorting. This is used to dismiss or create bubbles based on changes in
927 * permissions on the notification channel or the global setting.
928 *
929 * @param rankingMap the updated ranking map from NotificationListenerService
930 */
Beverlyed8aea22020-01-22 16:52:47 -0500931 private void onRankingUpdated(RankingMap rankingMap) {
Mady Mellor56515c42020-02-18 17:58:36 -0800932 if (mTmpRanking == null) {
933 mTmpRanking = new NotificationListenerService.Ranking();
934 }
935 String[] orderedKeys = rankingMap.getOrderedKeys();
936 for (int i = 0; i < orderedKeys.length; i++) {
937 String key = orderedKeys[i];
938 NotificationEntry entry = mNotificationEntryManager.getPendingOrActiveNotif(key);
939 rankingMap.getRanking(key, mTmpRanking);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700940 boolean isActiveBubble = mBubbleData.hasAnyBubbleWithKey(key);
Mady Mellore45ff862020-03-24 15:54:50 -0700941 if (isActiveBubble && !mTmpRanking.canBubble()) {
Mady Mellor56515c42020-02-18 17:58:36 -0800942 mBubbleData.notificationEntryRemoved(entry, BubbleController.DISMISS_BLOCKED);
Mady Mellore45ff862020-03-24 15:54:50 -0700943 } else if (entry != null && mTmpRanking.isBubble() && !isActiveBubble) {
Mady Mellor56515c42020-02-18 17:58:36 -0800944 entry.setFlagBubble(true);
945 onEntryUpdated(entry);
946 }
947 }
Beverlyed8aea22020-01-22 16:52:47 -0500948 }
Ned Burns01e38212019-01-03 16:32:52 -0500949
Lyn Han2f6e89d2020-04-15 10:01:01 -0700950 private void setIsBubble(Bubble b, boolean isBubble) {
951 if (isBubble) {
952 b.getEntry().getSbn().getNotification().flags |= FLAG_BUBBLE;
953 } else {
954 b.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
955 }
956 try {
957 mBarService.onNotificationBubbleChanged(b.getKey(), isBubble, 0);
958 } catch (RemoteException e) {
959 // Bad things have happened
960 }
961 }
962
Mark Renouf71a3af62019-04-08 15:02:54 -0400963 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400964 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400965
Mark Renouf3bc5b362019-04-05 14:37:59 -0400966 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400967 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800968 // Update bubbles in overflow.
Lyn Hane4274be2020-04-24 17:55:36 -0700969 if (mOverflowListener != null) {
970 mOverflowListener.applyUpdate(update);
Lyn Hanb58c7562020-01-07 14:29:20 -0800971 }
972
Mark Renouf82a40e62019-05-23 16:16:24 -0400973 // Collapsing? Do this first before remaining steps.
974 if (update.expandedChanged && !update.expanded) {
975 mStackView.setExpanded(false);
976 }
977
978 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700979 ArrayList<Pair<Bubble, Integer>> removedBubbles =
980 new ArrayList<>(update.removedBubbles);
981 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400982 final Bubble bubble = removed.first;
983 @DismissReason final int reason = removed.second;
984 mStackView.removeBubble(bubble);
Lyn Han2f6e89d2020-04-15 10:01:01 -0700985
Mark Renoufc19b4732019-06-26 12:08:33 -0400986 // If the bubble is removed for user switching, leave the notification in place.
Lyn Han2f6e89d2020-04-15 10:01:01 -0700987 if (reason == DISMISS_USER_CHANGED) {
988 continue;
989 }
990 if (!mBubbleData.hasBubbleInStackWithKey(bubble.getKey())) {
991 if (!mBubbleData.hasOverflowBubbleWithKey(bubble.getKey())
992 && (!bubble.showInShade()
993 || reason == DISMISS_NOTIF_CANCEL
Lyn Hane4274be2020-04-24 17:55:36 -0700994 || reason == DISMISS_GROUP_CANCELLED
995 || reason == DISMISS_OVERFLOW_MAX_REACHED)) {
Beverlyed8aea22020-01-22 16:52:47 -0500996 // The bubble is now gone & the notification is hidden from the shade, so
997 // time to actually remove it
998 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -0500999 cb.removeNotification(bubble.getEntry(), REASON_CANCEL);
Beverlyed8aea22020-01-22 16:52:47 -05001000 }
Mark Renoufc19b4732019-06-26 12:08:33 -04001001 } else {
Lyn Han2f6e89d2020-04-15 10:01:01 -07001002 if (bubble.getEntry().isBubble() && bubble.showInShade()) {
1003 setIsBubble(bubble, /* isBubble */ false);
1004 }
Mady Mellor9adfe6a2020-03-30 17:23:26 -07001005 if (bubble.getEntry().getRow() != null) {
1006 bubble.getEntry().getRow().updateBubbleButton();
1007 }
Mark Renouf82a40e62019-05-23 16:16:24 -04001008 }
Mady Mellor22f2f072019-04-18 13:26:18 -07001009
Lyn Han2f6e89d2020-04-15 10:01:01 -07001010 }
1011 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
1012 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
1013 // Time to potentially remove the summary
1014 for (NotifCallback cb : mCallbacks) {
1015 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -07001016 }
Mady Mellora54e9fa2019-04-18 13:26:18 -07001017 }
1018 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001019
Lyn Hanc47e1712020-01-28 21:43:34 -08001020 if (update.addedBubble != null) {
1021 mStackView.addBubble(update.addedBubble);
1022 }
1023
Mark Renouf82a40e62019-05-23 16:16:24 -04001024 if (update.updatedBubble != null) {
1025 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -04001026 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001027
Lyn Hanb58c7562020-01-07 14:29:20 -08001028 // At this point, the correct bubbles are inflated in the stack.
1029 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -04001030 if (update.orderChanged) {
1031 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -04001032 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001033
Mark Renouf82a40e62019-05-23 16:16:24 -04001034 if (update.selectionChanged) {
1035 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -07001036 if (update.selectedBubble != null) {
1037 mNotificationGroupManager.updateSuppression(
1038 update.selectedBubble.getEntry());
1039 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001040 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001041
Mark Renouf82a40e62019-05-23 16:16:24 -04001042 // Expanding? Apply this last.
1043 if (update.expandedChanged && update.expanded) {
1044 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -04001045 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001046
Beverlyed8aea22020-01-22 16:52:47 -05001047 for (NotifCallback cb : mCallbacks) {
Beverlya53fb0d2020-01-29 15:26:13 -05001048 cb.invalidateNotifications("BubbleData.Listener.applyUpdate");
Beverlyed8aea22020-01-22 16:52:47 -05001049 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001050 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001051
Issei Suzukia8d07312019-06-07 12:56:19 +02001052 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001053 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -08001054 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001055 mBubbleData.getSelectedBubble()));
1056
1057 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001058 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -08001059 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001060 mStackView.getExpandedBubble()));
1061 }
1062 }
Mark Renouf3bc5b362019-04-05 14:37:59 -04001063 }
1064 };
1065
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001066 /**
Beverlya53fb0d2020-01-29 15:26:13 -05001067 * We intercept notification entries (including group summaries) dismissed by the user when
1068 * there is an active bubble associated with it. We do this so that developers can still
1069 * cancel it (and hence the bubbles associated with it). However, these intercepted
1070 * notifications should then be hidden from the shade since the user has cancelled them, so we
1071 * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add
1072 * {@link BubbleData#addSummaryToSuppress}.
Beverlyed8aea22020-01-22 16:52:47 -05001073 *
Mady Mellor91b31e62020-01-30 17:40:48 -08001074 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -05001075 */
Beverlya53fb0d2020-01-29 15:26:13 -05001076 public boolean handleDismissalInterception(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -05001077 if (entry == null) {
1078 return false;
1079 }
Lyn Han2f6e89d2020-04-15 10:01:01 -07001080 if (isSummaryOfBubbles(entry)) {
Beverlya53fb0d2020-01-29 15:26:13 -05001081 handleSummaryDismissalInterception(entry);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001082 } else {
1083 Bubble bubble = mBubbleData.getBubbleInStackWithKey(entry.getKey());
1084 if (bubble == null || !entry.isBubble()) {
1085 bubble = mBubbleData.getOverflowBubbleWithKey(entry.getKey());
1086 }
1087 if (bubble == null) {
1088 return false;
1089 }
Beverlyed8aea22020-01-22 16:52:47 -05001090 bubble.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001091 bubble.setShowDot(false /* show */);
Beverlyed8aea22020-01-22 16:52:47 -05001092 }
Beverlya53fb0d2020-01-29 15:26:13 -05001093 // Update the shade
1094 for (NotifCallback cb : mCallbacks) {
1095 cb.invalidateNotifications("BubbleController.handleDismissalInterception");
1096 }
1097 return true;
Beverlyed8aea22020-01-22 16:52:47 -05001098 }
1099
Beverlya53fb0d2020-01-29 15:26:13 -05001100 private boolean isSummaryOfBubbles(NotificationEntry entry) {
1101 if (entry == null) {
Beverlyed8aea22020-01-22 16:52:47 -05001102 return false;
1103 }
Beverlya53fb0d2020-01-29 15:26:13 -05001104
1105 String groupKey = entry.getSbn().getGroupKey();
1106 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1107 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
1108 && mBubbleData.getSummaryKey(groupKey).equals(entry.getKey()));
1109 boolean isSummary = entry.getSbn().getNotification().isGroupSummary();
1110 return (isSuppressedSummary || isSummary)
1111 && bubbleChildren != null
1112 && !bubbleChildren.isEmpty();
1113 }
1114
1115 private void handleSummaryDismissalInterception(NotificationEntry summary) {
1116 // current children in the row:
Kevin Han43077f92020-02-28 12:51:53 -08001117 final List<NotificationEntry> children = summary.getAttachedNotifChildren();
Beverlya53fb0d2020-01-29 15:26:13 -05001118 if (children != null) {
1119 for (int i = 0; i < children.size(); i++) {
1120 NotificationEntry child = children.get(i);
Lyn Han2f6e89d2020-04-15 10:01:01 -07001121 if (mBubbleData.hasAnyBubbleWithKey(child.getKey())) {
Beverlya53fb0d2020-01-29 15:26:13 -05001122 // Suppress the bubbled child
1123 // As far as group manager is concerned, once a child is no longer shown
1124 // in the shade, it is essentially removed.
Lyn Han2f6e89d2020-04-15 10:01:01 -07001125 Bubble bubbleChild = mBubbleData.getAnyBubbleWithkey(child.getKey());
Beverlya53fb0d2020-01-29 15:26:13 -05001126 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1127 bubbleChild.setSuppressNotification(true);
Joshua Tsuji2ed260e2020-03-26 14:26:01 -04001128 bubbleChild.setShowDot(false /* show */);
Beverlya53fb0d2020-01-29 15:26:13 -05001129 } else {
1130 // non-bubbled children can be removed
1131 for (NotifCallback cb : mCallbacks) {
1132 cb.removeNotification(child, REASON_GROUP_SUMMARY_CANCELED);
1133 }
1134 }
1135 }
1136 }
1137
1138 // And since all children are removed, remove the summary.
1139 mNotificationGroupManager.onEntryRemoved(summary);
1140
1141 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1142 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1143 summary.getKey());
Beverlyed8aea22020-01-22 16:52:47 -05001144 }
1145
1146 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001147 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001148 * Updates the visibility of the bubbles based on current state.
1149 * Does not un-bubble, just hides or un-hides. Notifies any
1150 * {@link BubbleStateChangeListener}s of visibility changes.
1151 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001152 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001153 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001154 if (mStackView == null) {
1155 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001156 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001157 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1158 // Bubbles only appear in unlocked shade
1159 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001160 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001161 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001162 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001163
Mady Mellor698d9e82019-08-01 23:11:53 +00001164 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001165 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001166 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001167 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001168 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1169 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1170 }
1171
1172 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001173 }
1174
1175 /**
1176 * Rect indicating the touchable region for the bubble stack / expanded stack.
1177 */
1178 public Rect getTouchableRegion() {
1179 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1180 return null;
1181 }
1182 mStackView.getBoundsOnScreen(mTempRect);
1183 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001184 }
1185
Mady Mellor390bff42019-04-05 15:09:01 -07001186 /**
1187 * The display id of the expanded view, if the stack is expanded and not occluded by the
1188 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1189 */
1190 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001191 if (mStackView == null) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001192 return INVALID_DISPLAY;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001193 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001194 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001195 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Lyn Han9f66c3b2020-03-05 23:59:29 -08001196 final BubbleViewProvider expandedViewProvider = mStackView.getExpandedBubble();
1197 if (defaultDisplay && expandedViewProvider != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001198 && !mNotificationShadeWindowController.getPanelExpanded()) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001199 return expandedViewProvider.getDisplayId();
Mady Mellor390bff42019-04-05 15:09:01 -07001200 }
Lyn Han9f66c3b2020-03-05 23:59:29 -08001201 return INVALID_DISPLAY;
Mady Mellor390bff42019-04-05 15:09:01 -07001202 }
1203
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001204 @VisibleForTesting
1205 BubbleStackView getStackView() {
1206 return mStackView;
1207 }
1208
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001209 /**
1210 * Description of current bubble state.
1211 */
1212 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1213 pw.println("BubbleController state:");
1214 mBubbleData.dump(fd, pw, args);
1215 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001216 if (mStackView != null) {
1217 mStackView.dump(fd, pw, args);
1218 }
1219 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001220 }
1221
Mady Mellore80930e2019-03-21 16:00:45 -07001222 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001223 * This task stack listener is responsible for responding to tasks moved to the front
1224 * which are on the default (main) display. When this happens, expanded bubbles must be
1225 * collapsed so the user may interact with the app which was just moved to the front.
1226 * <p>
1227 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1228 * these calls via a main thread Handler.
1229 */
1230 @MainThread
1231 private class BubbleTaskStackListener extends TaskStackChangeListener {
1232
Mark Renoufcecc77b2019-01-30 16:32:24 -05001233 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001234 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1235 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001236 if (!mStackView.isExpansionAnimating()) {
1237 mBubbleData.setExpanded(false);
1238 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001239 }
1240 }
1241
Mark Renoufcecc77b2019-01-30 16:32:24 -05001242 @Override
Winson Chunge789ff62020-02-24 14:40:23 -08001243 public void onActivityRestartAttempt(RunningTaskInfo task, boolean homeTaskVisible,
Evan Rosky8d1c24e2020-04-23 09:21:16 -07001244 boolean clearedTask, boolean wasVisible) {
Winson Chunge789ff62020-02-24 14:40:23 -08001245 for (Bubble b : mBubbleData.getBubbles()) {
1246 if (b.getDisplayId() == task.displayId) {
1247 expandStackAndSelectBubble(b.getKey());
1248 return;
1249 }
1250 }
1251 }
1252
1253 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001254 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001255 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001256 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001257 }
1258 }
Mark Renouf446251d2019-04-26 10:22:41 -04001259
1260 @Override
1261 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1262 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1263 mBubbleData.setExpanded(false);
1264 }
1265 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001266
1267 @Override
1268 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001269 if (mStackView == null) {
1270 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001271 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001272 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001273 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001274
1275 @Override
1276 public void onSingleTaskDisplayEmpty(int displayId) {
Lyn Han9f66c3b2020-03-05 23:59:29 -08001277 final BubbleViewProvider expandedBubble = mStackView != null
Mady Mellor5186b132019-09-16 17:55:48 -07001278 ? mStackView.getExpandedBubble()
1279 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001280 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1281 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001282 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001283 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001284 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001285 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001286 }
1287
Mady Mellorca0c24c2019-05-16 16:14:32 -07001288 /**
1289 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1290 *
1291 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1292 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1293 *
1294 * @param context the context to use.
1295 * @param entry the entry to bubble.
1296 */
1297 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1298 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Melloraa9ce172020-03-17 10:34:20 -07001299 ? entry.getBubbleMetadata().getIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001300 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001301 if (entry.getBubbleMetadata() != null
1302 && entry.getBubbleMetadata().getShortcutId() != null) {
1303 return true;
1304 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001305 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001306 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001307 return false;
1308 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001309 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1310 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001311 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001312 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001313 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001314 Log.w(TAG, "Unable to send as bubble, "
1315 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001316 + intent);
1317 return false;
1318 }
1319 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001320 Log.w(TAG, "Unable to send as bubble, "
1321 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001322 + intent);
1323 return false;
1324 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001325 return true;
1326 }
1327
Joshua Tsujia19515f2019-02-13 18:02:29 -05001328 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001329 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001330 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001331 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Joshua Tsujiff6b0f22020-03-09 14:55:19 -04001332 if (mStackView != null) {
Joshua Tsujid9422832019-03-05 13:32:37 -05001333 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001334 }
1335 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001336 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001337}