blob: db1185fb96f9242776cee6fe2efaba2e69ef59d4 [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 Mellor22f2f072019-04-18 13:26:18 -070019import static android.app.Notification.FLAG_AUTOGROUP_SUMMARY;
Mady Mellor3a0a1b42019-05-23 06:40:21 -070020import static android.app.Notification.FLAG_BUBBLE;
Mady Mellorc2ff0112019-03-28 14:18:06 -070021import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL;
22import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL;
23import static android.service.notification.NotificationListenerService.REASON_CANCEL;
24import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -070025import static android.service.notification.NotificationListenerService.REASON_CLICK;
26import static android.service.notification.NotificationListenerService.REASON_GROUP_SUMMARY_CANCELED;
Mady Mellor390bff42019-04-05 15:09:01 -070027import static android.view.Display.DEFAULT_DISPLAY;
28import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080029import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080030import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080031import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080032
Issei Suzukia8d07312019-06-07 12:56:19 +020033import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_CONTROLLER;
34import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
35import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080036import static com.android.systemui.statusbar.StatusBarState.SHADE;
Mady Mellor1a4e86f2019-05-03 16:07:23 -070037import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080038
Mark Renoufba5ab512019-05-02 15:21:01 -040039import static java.lang.annotation.ElementType.FIELD;
40import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
41import static java.lang.annotation.ElementType.PARAMETER;
Mark Renouf08bc42a2019-03-07 13:01:59 -050042import static java.lang.annotation.RetentionPolicy.SOURCE;
43
Mark Renoufc19b4732019-06-26 12:08:33 -040044import android.annotation.UserIdInt;
Mark Renoufc808f062019-02-07 15:20:37 -050045import android.app.ActivityManager.RunningTaskInfo;
Mady Mellor66efd5e2019-05-15 13:38:11 -070046import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070047import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080048import android.content.Context;
Mady Mellorca0c24c2019-05-16 16:14:32 -070049import android.content.pm.ActivityInfo;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080050import android.content.pm.PackageManager;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040051import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080052import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050053import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080054import android.os.ServiceManager;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040055import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040056import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040057import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040058import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040059import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040060import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050061import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080062import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080063import android.widget.FrameLayout;
64
Mark Renouf08bc42a2019-03-07 13:01:59 -050065import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050066import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040067import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050068
Mady Mellorebdbbb92018-11-15 14:36:48 -080069import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070070import com.android.internal.statusbar.IStatusBarService;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080071import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050072import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050073import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000074import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050075import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050076import com.android.systemui.shared.system.WindowManagerWrapper;
Mark Renoufc19b4732019-06-26 12:08:33 -040077import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070078import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050079import com.android.systemui.statusbar.notification.NotificationEntryListener;
80import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080081import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050082import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellor22f2f072019-04-18 13:26:18 -070083import com.android.systemui.statusbar.phone.NotificationGroupManager;
Mady Mellor7f234902019-10-20 12:06:29 -070084import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080085import com.android.systemui.statusbar.phone.StatusBar;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080086import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070087import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040088import com.android.systemui.statusbar.policy.ZenModeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080089
Mady Mellor70cba7bb2019-07-02 15:06:07 -070090import java.io.FileDescriptor;
91import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -050092import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -040093import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -070094import java.util.ArrayList;
Mady Mellore80930e2019-03-21 16:00:45 -070095import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050096
Jason Monk27d01a622018-12-10 15:57:09 -050097import javax.inject.Inject;
98import javax.inject.Singleton;
99
Mady Mellor7f234902019-10-20 12:06:29 -0700100import dagger.Lazy;
101
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800102/**
103 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
104 * Bubbles can be expanded to show more content.
105 *
106 * The controller manages addition, removal, and visible state of bubbles on screen.
107 */
Jason Monk27d01a622018-12-10 15:57:09 -0500108@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -0400109public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800110
Issei Suzukia8d07312019-06-07 12:56:19 +0200111 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800112
Mark Renouf08bc42a2019-03-07 13:01:59 -0500113 @Retention(SOURCE)
114 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400115 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700116 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400117 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500118 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700119
Mark Renouf08bc42a2019-03-07 13:01:59 -0500120 static final int DISMISS_USER_GESTURE = 1;
121 static final int DISMISS_AGED = 2;
122 static final int DISMISS_TASK_FINISHED = 3;
123 static final int DISMISS_BLOCKED = 4;
124 static final int DISMISS_NOTIF_CANCEL = 5;
125 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700126 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400127 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700128 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700129 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500130
Ned Burns01e38212019-01-03 16:32:52 -0500131 private final Context mContext;
132 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500133 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800134 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800135 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100136 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700137 private final NotificationGroupManager mNotificationGroupManager;
Mady Mellor7f234902019-10-20 12:06:29 -0700138 private final Lazy<ShadeController> mShadeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800139
Mady Mellor3dff9e62019-02-05 18:12:53 -0800140 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400141 @Nullable private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800142
Mark Renoufc19b4732019-06-26 12:08:33 -0400143 // Tracks the id of the current (foreground) user.
144 private int mCurrentUserId;
145 // Saves notification keys of active bubbles when users are switched.
146 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
147
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800148 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500149 private final StatusBarWindowController mStatusBarWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400150 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800151 private StatusBarStateListener mStatusBarStateListener;
152
Mady Melloraa8fef22019-04-11 13:36:40 -0700153 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700154 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800155
Mady Mellord1c78b262018-11-06 18:04:40 -0800156 // Used for determining view rect for touch interaction
157 private Rect mTempRect = new Rect();
158
Mark Renoufc19b4732019-06-26 12:08:33 -0400159 // Listens to user switch so bubbles can be saved and restored.
160 private final NotificationLockscreenUserManager mNotifUserManager;
161
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400162 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
163 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
164
Mady Mellor5549dd22018-11-06 18:07:34 -0800165 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800166 * Listener to be notified when some states of the bubbles change.
167 */
168 public interface BubbleStateChangeListener {
169 /**
170 * Called when the stack has bubbles or no longer has bubbles.
171 */
172 void onHasBubblesChanged(boolean hasBubbles);
173 }
174
Mady Mellorcd9b1302018-11-06 18:08:04 -0800175 /**
176 * Listener to find out about stack expansion / collapse events.
177 */
178 public interface BubbleExpandListener {
179 /**
180 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700181 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800182 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800183 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800184 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800185 void onBubbleExpandChanged(boolean isExpanding, String key);
186 }
187
188 /**
189 * Listens for the current state of the status bar and updates the visibility state
190 * of bubbles as needed.
191 */
192 private class StatusBarStateListener implements StatusBarStateController.StateListener {
193 private int mState;
194 /**
195 * Returns the current status bar state.
196 */
197 public int getCurrentState() {
198 return mState;
199 }
200
201 @Override
202 public void onStateChanged(int newState) {
203 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400204 boolean shouldCollapse = (mState != SHADE);
205 if (shouldCollapse) {
206 collapseStack();
207 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700208 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800209 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800210 }
211
Jason Monk27d01a622018-12-10 15:57:09 -0500212 @Inject
Mady Mellor7f234902019-10-20 12:06:29 -0700213 public BubbleController(Context context,
214 StatusBarWindowController statusBarWindowController,
215 StatusBarStateController statusBarStateController,
216 Lazy<ShadeController> shadeController,
217 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700218 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400219 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400220 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700221 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700222 NotificationGroupManager groupManager,
223 NotificationEntryManager entryManager) {
224 this(context, statusBarWindowController, statusBarStateController, shadeController,
225 data, null /* synchronizer */, configurationController, interruptionStateProvider,
226 zenModeController, notifUserManager, groupManager, entryManager);
227 }
228
229 public BubbleController(Context context,
230 StatusBarWindowController statusBarWindowController,
231 StatusBarStateController statusBarStateController,
232 Lazy<ShadeController> shadeController,
233 BubbleData data,
234 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
235 ConfigurationController configurationController,
236 NotificationInterruptionStateProvider interruptionStateProvider,
237 ZenModeController zenModeController,
238 NotificationLockscreenUserManager notifUserManager,
239 NotificationGroupManager groupManager,
240 NotificationEntryManager entryManager) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800241 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700242 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400243 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400244 mZenModeController = zenModeController;
245 mZenModeController.addCallback(new ZenModeController.Callback() {
246 @Override
247 public void onZenChanged(int zen) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700248 if (mStackView != null) {
249 mStackView.updateDots();
250 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400251 }
252
253 @Override
254 public void onConfigChanged(ZenModeConfig config) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700255 if (mStackView != null) {
256 mStackView.updateDots();
257 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400258 }
259 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700260
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700261 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800262
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400263 mBubbleData = data;
264 mBubbleData.setListener(mBubbleDataListener);
265
Mady Mellor7f234902019-10-20 12:06:29 -0700266 mNotificationEntryManager = entryManager;
Ned Burns01e38212019-01-03 16:32:52 -0500267 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700268 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellor22f2f072019-04-18 13:26:18 -0700269 mNotificationGroupManager = groupManager;
Mady Mellor740d85d2019-07-09 15:26:47 -0700270 mNotificationGroupManager.addOnGroupChangeListener(
271 new NotificationGroupManager.OnGroupChangeListener() {
272 @Override
273 public void onGroupSuppressionChanged(
274 NotificationGroupManager.NotificationGroup group,
275 boolean suppressed) {
276 // More notifications could be added causing summary to no longer
277 // be suppressed -- in this case need to remove the key.
278 final String groupKey = group.summary != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400279 ? group.summary.getSbn().getGroupKey()
Mady Mellor740d85d2019-07-09 15:26:47 -0700280 : null;
281 if (!suppressed && groupKey != null
282 && mBubbleData.isSummarySuppressed(groupKey)) {
283 mBubbleData.removeSuppressedSummary(groupKey);
284 }
285 }
286 });
Mady Mellorb4991e62019-01-10 15:14:51 -0800287
Mady Mellor7f234902019-10-20 12:06:29 -0700288 mShadeController = shadeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800289 mStatusBarWindowController = statusBarWindowController;
290 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700291 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500292
Mark Renoufcecc77b2019-01-30 16:32:24 -0500293 mTaskStackListener = new BubbleTaskStackListener();
294 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800295
Joshua Tsujia19515f2019-02-13 18:02:29 -0500296 try {
297 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
298 } catch (RemoteException e) {
299 e.printStackTrace();
300 }
Issei Suzukic0387542019-03-08 17:31:14 +0100301 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700302
303 mBarService = IStatusBarService.Stub.asInterface(
304 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400305
306 mSavedBubbleKeysPerUser = new SparseSetArray<>();
307 mCurrentUserId = mNotifUserManager.getCurrentUserId();
308 mNotifUserManager.addUserChangedListener(
309 newUserId -> {
310 saveBubbles(mCurrentUserId);
311 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
312 restoreBubbles(newUserId);
313 mCurrentUserId = newUserId;
314 });
Mady Mellor5549dd22018-11-06 18:07:34 -0800315 }
316
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400317 /**
318 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
319 * method initializes the stack view and adds it to the StatusBar just above the scrim.
320 */
321 private void ensureStackViewCreated() {
322 if (mStackView == null) {
323 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
324 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Lyn Hanbde48202019-05-29 19:18:29 -0700325 int bubbleScrimIndex = sbv.indexOfChild(sbv.findViewById(R.id.scrim_for_bubble));
326 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
327 sbv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400328 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
329 if (mExpandListener != null) {
330 mStackView.setExpandListener(mExpandListener);
331 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400332 }
333 }
334
Mark Renoufc19b4732019-06-26 12:08:33 -0400335 /**
336 * Records the notification key for any active bubbles. These are used to restore active
337 * bubbles when the user returns to the foreground.
338 *
339 * @param userId the id of the user
340 */
341 private void saveBubbles(@UserIdInt int userId) {
342 // First clear any existing keys that might be stored.
343 mSavedBubbleKeysPerUser.remove(userId);
344 // Add in all active bubbles for the current user.
345 for (Bubble bubble: mBubbleData.getBubbles()) {
346 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
347 }
348 }
349
350 /**
351 * Promotes existing notifications to Bubbles if they were previously bubbles.
352 *
353 * @param userId the id of the user
354 */
355 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400356 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
357 if (savedBubbleKeys == null) {
358 // There were no bubbles saved for this used.
359 return;
360 }
Evan Laird181de622019-10-24 09:53:02 -0400361 for (NotificationEntry e :
362 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400363 if (savedBubbleKeys.contains(e.getKey())
Mark Renoufc19b4732019-06-26 12:08:33 -0400364 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
365 && canLaunchInActivityView(mContext, e)) {
366 updateBubble(e, /* suppressFlyout= */ true);
367 }
368 }
369 // Finally, remove the entries for this user now that bubbles are restored.
370 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
371 }
372
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700373 @Override
374 public void onUiModeChanged() {
375 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700376 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700377 }
378 }
379
380 @Override
381 public void onOverlayChanged() {
382 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700383 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700384 }
385 }
386
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400387 @Override
388 public void onConfigChanged(Configuration newConfig) {
389 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400390 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700391 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400392 }
393 }
394
Mady Mellor5549dd22018-11-06 18:07:34 -0800395 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800396 * Set a listener to be notified when some states of the bubbles change.
397 */
398 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
399 mStateChangeListener = listener;
400 }
401
402 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800403 * Set a listener to be notified of bubble expand events.
404 */
405 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100406 mExpandListener = ((isExpanding, key) -> {
407 if (listener != null) {
408 listener.onBubbleExpandChanged(isExpanding, key);
409 }
410 mStatusBarWindowController.setBubbleExpanded(isExpanding);
411 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800412 if (mStackView != null) {
413 mStackView.setExpandListener(mExpandListener);
414 }
415 }
416
417 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800418 * Whether or not there are bubbles present, regardless of them being visible on the
419 * screen (e.g. if on AOD).
420 */
421 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800422 if (mStackView == null) {
423 return false;
424 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400425 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800426 }
427
428 /**
429 * Whether the stack of bubbles is expanded or not.
430 */
431 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400432 return mBubbleData.isExpanded();
433 }
434
435 /**
436 * Tell the stack of bubbles to expand.
437 */
438 public void expandStack() {
439 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800440 }
441
442 /**
443 * Tell the stack of bubbles to collapse.
444 */
445 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400446 mBubbleData.setExpanded(false /* expanded */);
447 }
448
Mady Mellorce23c462019-06-17 17:30:07 -0700449 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700450 * True if either:
451 * (1) There is a bubble associated with the provided key and if its notification is hidden
452 * from the shade.
453 * (2) There is a group summary associated with the provided key that is hidden from the shade
454 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700455 *
Mady Mellore28fe102019-07-09 15:33:32 -0700456 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700457 */
458 public boolean isBubbleNotificationSuppressedFromShade(String key) {
Mady Mellore28fe102019-07-09 15:33:32 -0700459 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorce23c462019-06-17 17:30:07 -0700460 && !mBubbleData.getBubbleWithKey(key).showInShadeWhenBubble();
Evan Laird181de622019-10-24 09:53:02 -0400461 NotificationEntry entry = mNotificationEntryManager.getActiveNotificationUnfiltered(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400462 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellore28fe102019-07-09 15:33:32 -0700463 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700464 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
465 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700466 }
467
Mark Renouf71a3af62019-04-08 15:02:54 -0400468 void selectBubble(Bubble bubble) {
469 mBubbleData.setSelectedBubble(bubble);
470 }
471
472 @VisibleForTesting
473 void selectBubble(String key) {
474 Bubble bubble = mBubbleData.getBubbleWithKey(key);
475 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800476 }
477
478 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400479 * Request the stack expand if needed, then select the specified Bubble as current.
480 *
481 * @param notificationKey the notification key for the bubble to be selected
482 */
483 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400484 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
485 if (bubble != null) {
486 mBubbleData.setSelectedBubble(bubble);
487 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400488 }
489 }
490
491 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800492 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
493 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500494 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400495 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800496 }
497
498 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500499 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
500 * is forwarded a back key down/up pair.
501 */
502 public void performBackPressIfNeeded() {
503 if (mStackView != null) {
504 mStackView.performBackPressIfNeeded();
505 }
506 }
507
508 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800509 * Adds or updates a bubble associated with the provided notification entry.
510 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400511 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800512 */
Mark Renouff97ed462019-04-05 13:46:24 -0400513 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700514 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400515 }
516
517 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700518 updateBubble(notif, suppressFlyout, true /* showInShade */);
519 }
520
521 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700522 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400523 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700524 notif.setInterruption();
525 }
Mady Mellor7f234902019-10-20 12:06:29 -0700526 mBubbleData.notificationEntryUpdated(notif, suppressFlyout, showInShade);
527 }
528
529 /**
530 * Called when a user has indicated that an active notification should be shown as a bubble.
531 * <p>
532 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
533 * the notification from appearing in the shade.
534 *
535 * @param entry the notification to show as a bubble.
536 */
537 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
538 mShadeController.get().collapsePanel(true);
539 entry.setFlagBubble(true);
540 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
541 mBubbleData.getBubbleWithKey(entry.getKey()).setUserCreated(true);
542 }
543
544 /**
545 * Called when a user has indicated that an active notification appearing as a bubble should
546 * no longer be shown as a bubble.
547 *
548 * @param entry the notification to no longer show as a bubble.
549 */
550 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
551 entry.setFlagBubble(false);
552 removeBubble(entry.getKey(), DISMISS_BLOCKED);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800553 }
554
555 /**
556 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500557 * <p>
558 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800559 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500560 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500561 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400562 // TEMP: refactor to change this to pass entry
563 Bubble bubble = mBubbleData.getBubbleWithKey(key);
564 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700565 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800566 }
567 }
568
Ned Burns01e38212019-01-03 16:32:52 -0500569 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700570 private final NotificationRemoveInterceptor mRemoveInterceptor =
571 new NotificationRemoveInterceptor() {
572 @Override
573 public boolean onNotificationRemoveRequested(String key, int reason) {
Evan Laird181de622019-10-24 09:53:02 -0400574 NotificationEntry entry =
575 mNotificationEntryManager.getActiveNotificationUnfiltered(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400576 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellor22f2f072019-04-18 13:26:18 -0700577 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
578
579 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
Mady Mellore28fe102019-07-09 15:33:32 -0700580 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
581 && mBubbleData.getSummaryKey(groupKey).equals(key));
Mady Mellor22f2f072019-04-18 13:26:18 -0700582 boolean isSummary = entry != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400583 && entry.getSbn().getNotification().isGroupSummary();
Mady Mellore28fe102019-07-09 15:33:32 -0700584 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
585 && bubbleChildren != null && !bubbleChildren.isEmpty();
Mady Mellor22f2f072019-04-18 13:26:18 -0700586
587 if (!inBubbleData && !isSummaryOfBubbles) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700588 return false;
589 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700590
591 final boolean isClearAll = reason == REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700592 final boolean isUserDimiss = reason == REASON_CANCEL || reason == REASON_CLICK;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700593 final boolean isAppCancel = reason == REASON_APP_CANCEL
594 || reason == REASON_APP_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700595 final boolean isSummaryCancel = reason == REASON_GROUP_SUMMARY_CANCELED;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700596
597 // Need to check for !appCancel here because the notification may have
598 // previously been dismissed & entry.isRowDismissed would still be true
Mady Mellorca184aae2019-09-17 16:07:12 -0700599 boolean userRemovedNotif = (entry != null && entry.isRowDismissed() && !isAppCancel)
Mady Mellor22f2f072019-04-18 13:26:18 -0700600 || isClearAll || isUserDimiss || isSummaryCancel;
601
602 if (isSummaryOfBubbles) {
603 return handleSummaryRemovalInterception(entry, userRemovedNotif);
604 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700605
606 // The bubble notification sticks around in the data as long as the bubble is
607 // not dismissed and the app hasn't cancelled the notification.
Mady Mellor22f2f072019-04-18 13:26:18 -0700608 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellorca184aae2019-09-17 16:07:12 -0700609 boolean bubbleExtended = entry != null && entry.isBubble() && userRemovedNotif;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700610 if (bubbleExtended) {
Mady Mellorce23c462019-06-17 17:30:07 -0700611 bubble.setShowInShadeWhenBubble(false);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700612 bubble.setShowBubbleDot(false);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700613 if (mStackView != null) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400614 mStackView.updateDotVisibility(entry.getKey());
Mady Mellorc2ff0112019-03-28 14:18:06 -0700615 }
Beverly85d4c192019-09-30 11:40:39 -0400616 mNotificationEntryManager.updateNotifications(
617 "BubbleController.onNotificationRemoveRequested");
Mady Mellorc2ff0112019-03-28 14:18:06 -0700618 return true;
Mady Mellor7f234902019-10-20 12:06:29 -0700619 } else if (!userRemovedNotif && entry != null && !bubble.isUserCreated()) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700620 // This wasn't a user removal so we should remove the bubble as well
621 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
622 return false;
623 }
624 return false;
625 }
626 };
627
Mady Mellor22f2f072019-04-18 13:26:18 -0700628 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
629 boolean userRemovedNotif) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400630 String groupKey = summary.getSbn().getGroupKey();
Mady Mellor22f2f072019-04-18 13:26:18 -0700631 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
632
633 if (userRemovedNotif) {
634 // If it's a user dismiss we mark the children to be hidden from the shade.
635 for (int i = 0; i < bubbleChildren.size(); i++) {
636 Bubble bubbleChild = bubbleChildren.get(i);
637 // As far as group manager is concerned, once a child is no longer shown
638 // in the shade, it is essentially removed.
639 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
640 bubbleChild.setShowInShadeWhenBubble(false);
641 bubbleChild.setShowBubbleDot(false);
642 if (mStackView != null) {
643 mStackView.updateDotVisibility(bubbleChild.getKey());
644 }
645 }
646 // And since all children are removed, remove the summary.
647 mNotificationGroupManager.onEntryRemoved(summary);
648
649 // If the summary was auto-generated we don't need to keep that notification around
650 // because apps can't cancel it; so we only intercept & suppress real summaries.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400651 boolean isAutogroupSummary = (summary.getSbn().getNotification().flags
Mady Mellor22f2f072019-04-18 13:26:18 -0700652 & FLAG_AUTOGROUP_SUMMARY) != 0;
Mady Mellore28fe102019-07-09 15:33:32 -0700653 if (!isAutogroupSummary) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400654 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
655 summary.getKey());
Mady Mellore28fe102019-07-09 15:33:32 -0700656 // Tell shade to update for the suppression
Beverly85d4c192019-09-30 11:40:39 -0400657 mNotificationEntryManager.updateNotifications(
658 "BubbleController.handleSummaryRemovalInterception");
Mady Mellore28fe102019-07-09 15:33:32 -0700659 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700660 return !isAutogroupSummary;
661 } else {
Mady Mellore28fe102019-07-09 15:33:32 -0700662 // If it's not a user dismiss it's a cancel.
663 mBubbleData.removeSuppressedSummary(groupKey);
664
Mady Mellor22f2f072019-04-18 13:26:18 -0700665 // Remove any associated bubble children.
666 for (int i = 0; i < bubbleChildren.size(); i++) {
667 Bubble bubbleChild = bubbleChildren.get(i);
668 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
669 DISMISS_GROUP_CANCELLED);
670 }
671 return false;
672 }
673 }
674
Mady Mellorc2ff0112019-03-28 14:18:06 -0700675 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500676 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
677 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500678 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellor7f234902019-10-20 12:06:29 -0700679 Bubble b = mBubbleData.getBubbleWithKey(entry.getKey());
680 BubbleExperimentConfig.adjustForExperiments(mContext, entry, b);
681
Mady Mellorca0c24c2019-05-16 16:14:32 -0700682 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
683 && canLaunchInActivityView(mContext, entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400684 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800685 }
686 }
687
688 @Override
689 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellor7f234902019-10-20 12:06:29 -0700690 Bubble b = mBubbleData.getBubbleWithKey(entry.getKey());
691 BubbleExperimentConfig.adjustForExperiments(mContext, entry, b);
692
Mady Mellorca0c24c2019-05-16 16:14:32 -0700693 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
694 && canLaunchInActivityView(mContext, entry);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400695 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
Mady Melloraa8fef22019-04-11 13:36:40 -0700696 // It was previously a bubble but no longer a bubble -- lets remove it
Ned Burns00b4b2d2019-10-17 22:09:27 -0400697 removeBubble(entry.getKey(), DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700698 } else if (shouldBubble) {
Mark Renouff97ed462019-04-05 13:46:24 -0400699 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800700 }
701 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400702
703 @Override
704 public void onNotificationRankingUpdated(RankingMap rankingMap) {
705 // Forward to BubbleData to block any bubbles which should no longer be shown
706 mBubbleData.notificationRankingUpdated(rankingMap);
707 }
Ned Burns01e38212019-01-03 16:32:52 -0500708 };
709
Mark Renouf71a3af62019-04-08 15:02:54 -0400710 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400711 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400712
Mark Renouf3bc5b362019-04-05 14:37:59 -0400713 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400714 public void applyUpdate(BubbleData.Update update) {
715 if (mStackView == null && update.addedBubble != null) {
716 // Lazy init stack view when the first bubble is added.
717 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400718 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400719
720 // If not yet initialized, ignore all other changes.
721 if (mStackView == null) {
722 return;
723 }
724
725 if (update.addedBubble != null) {
726 mStackView.addBubble(update.addedBubble);
727 }
728
729 // Collapsing? Do this first before remaining steps.
730 if (update.expandedChanged && !update.expanded) {
731 mStackView.setExpanded(false);
732 }
733
734 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700735 ArrayList<Pair<Bubble, Integer>> removedBubbles =
736 new ArrayList<>(update.removedBubbles);
737 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400738 final Bubble bubble = removed.first;
739 @DismissReason final int reason = removed.second;
740 mStackView.removeBubble(bubble);
741
Mark Renoufc19b4732019-06-26 12:08:33 -0400742 // If the bubble is removed for user switching, leave the notification in place.
743 if (reason != DISMISS_USER_CHANGED) {
744 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
745 && !bubble.showInShadeWhenBubble()) {
746 // The bubble is gone & the notification is gone, time to actually remove it
747 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400748 bubble.getEntry().getSbn(), UNDEFINED_DISMISS_REASON);
Mark Renoufc19b4732019-06-26 12:08:33 -0400749 } else {
750 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400751 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700752
Mark Renoufc19b4732019-06-26 12:08:33 -0400753 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
754 // will get right result back
755 try {
756 mBarService.onNotificationBubbleChanged(bubble.getKey(),
757 false /* isBubble */);
758 } catch (RemoteException e) {
759 // Bad things have happened
760 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400761 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700762
Mady Mellore28fe102019-07-09 15:33:32 -0700763 // Check if removed bubble has an associated suppressed group summary that needs
764 // to be removed now.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400765 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700766 if (mBubbleData.isSummarySuppressed(groupKey)
767 && mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
768 // Time to actually remove the summary.
769 String notifKey = mBubbleData.getSummaryKey(groupKey);
770 mBubbleData.removeSuppressedSummary(groupKey);
771 NotificationEntry entry =
Evan Laird181de622019-10-24 09:53:02 -0400772 mNotificationEntryManager.getActiveNotificationUnfiltered(notifKey);
Mady Mellore28fe102019-07-09 15:33:32 -0700773 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400774 entry.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellore28fe102019-07-09 15:33:32 -0700775 }
776
Mady Mellor22f2f072019-04-18 13:26:18 -0700777 // Check if summary should be removed from NoManGroup
778 NotificationEntry summary = mNotificationGroupManager.getLogicalGroupSummary(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400779 bubble.getEntry().getSbn());
Mady Mellor22f2f072019-04-18 13:26:18 -0700780 if (summary != null) {
781 ArrayList<NotificationEntry> summaryChildren =
Ned Burns00b4b2d2019-10-17 22:09:27 -0400782 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
783 boolean isSummaryThisNotif = summary.getKey().equals(
784 bubble.getEntry().getKey());
Mady Mellore4348272019-07-29 17:43:36 -0700785 if (!isSummaryThisNotif
786 && (summaryChildren == null || summaryChildren.isEmpty())) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700787 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400788 summary.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellor22f2f072019-04-18 13:26:18 -0700789 }
790 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700791 }
792 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400793
Mark Renouf82a40e62019-05-23 16:16:24 -0400794 if (update.updatedBubble != null) {
795 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400796 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400797
Mark Renouf82a40e62019-05-23 16:16:24 -0400798 if (update.orderChanged) {
799 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400800 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400801
Mark Renouf82a40e62019-05-23 16:16:24 -0400802 if (update.selectionChanged) {
803 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700804 if (update.selectedBubble != null) {
805 mNotificationGroupManager.updateSuppression(
806 update.selectedBubble.getEntry());
807 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400808 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400809
Mark Renouf82a40e62019-05-23 16:16:24 -0400810 // Expanding? Apply this last.
811 if (update.expandedChanged && update.expanded) {
812 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400813 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400814
Beverly85d4c192019-09-30 11:40:39 -0400815 mNotificationEntryManager.updateNotifications(
816 "BubbleData.Listener.applyUpdate");
Lyn Han6c40fe72019-05-08 14:06:33 -0700817 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400818
Issei Suzukia8d07312019-06-07 12:56:19 +0200819 if (DEBUG_BUBBLE_CONTROLLER) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400820 Log.d(TAG, "[BubbleData]");
821 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
822 mBubbleData.getSelectedBubble()));
823
824 if (mStackView != null) {
825 Log.d(TAG, "[BubbleStackView]");
826 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
827 mStackView.getExpandedBubble()));
828 }
829 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400830 }
831 };
832
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800833 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400834 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700835 * Updates the visibility of the bubbles based on current state.
836 * Does not un-bubble, just hides or un-hides. Notifies any
837 * {@link BubbleStateChangeListener}s of visibility changes.
838 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800839 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700840 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800841 if (mStackView == null) {
842 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800843 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800844 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
845 // Bubbles only appear in unlocked shade
846 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +0000847 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800848 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800849 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700850
Mady Mellor698d9e82019-08-01 23:11:53 +0000851 // Let listeners know if bubble state changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700852 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +0000853 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellor88552b82019-08-05 22:38:59 +0000854 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -0700855 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
856 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
857 }
858
859 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800860 }
861
862 /**
863 * Rect indicating the touchable region for the bubble stack / expanded stack.
864 */
865 public Rect getTouchableRegion() {
866 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
867 return null;
868 }
869 mStackView.getBoundsOnScreen(mTempRect);
870 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800871 }
872
Mady Mellor390bff42019-04-05 15:09:01 -0700873 /**
874 * The display id of the expanded view, if the stack is expanded and not occluded by the
875 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
876 */
877 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200878 final Bubble bubble = getExpandedBubble(context);
879 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
880 }
881
882 @Nullable
883 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700884 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200885 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700886 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200887 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700888 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200889 final Bubble expandedBubble = mStackView.getExpandedBubble();
890 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700891 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200892 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700893 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200894 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700895 }
896
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800897 @VisibleForTesting
898 BubbleStackView getStackView() {
899 return mStackView;
900 }
901
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700902 /**
903 * Description of current bubble state.
904 */
905 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
906 pw.println("BubbleController state:");
907 mBubbleData.dump(fd, pw, args);
908 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -0400909 if (mStackView != null) {
910 mStackView.dump(fd, pw, args);
911 }
912 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700913 }
914
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400915 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
916 StringBuilder sb = new StringBuilder();
917 for (Bubble bubble : bubbles) {
918 if (bubble == null) {
919 sb.append(" <null> !!!!!\n");
920 } else {
921 boolean isSelected = (bubble == selected);
922 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
923 ((isSelected) ? "->" : " "),
924 bubble.getLastActivity(),
925 (bubble.isOngoing() ? 1 : 0),
926 bubble.getKey()));
927 }
928 }
929 return sb.toString();
930 }
931
Mady Mellore80930e2019-03-21 16:00:45 -0700932 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -0500933 * This task stack listener is responsible for responding to tasks moved to the front
934 * which are on the default (main) display. When this happens, expanded bubbles must be
935 * collapsed so the user may interact with the app which was just moved to the front.
936 * <p>
937 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
938 * these calls via a main thread Handler.
939 */
940 @MainThread
941 private class BubbleTaskStackListener extends TaskStackChangeListener {
942
Mark Renoufcecc77b2019-01-30 16:32:24 -0500943 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500944 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
945 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -0700946 if (!mStackView.isExpansionAnimating()) {
947 mBubbleData.setExpanded(false);
948 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500949 }
950 }
951
Mark Renoufcecc77b2019-01-30 16:32:24 -0500952 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500953 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500954 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400955 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500956 }
957 }
Mark Renouf446251d2019-04-26 10:22:41 -0400958
959 @Override
960 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
961 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
962 mBubbleData.setExpanded(false);
963 }
964 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200965
966 @Override
967 public void onSingleTaskDisplayDrawn(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -0700968 final Bubble expandedBubble = mStackView != null
969 ? mStackView.getExpandedBubble()
970 : null;
Issei Suzukicac2a502019-04-16 16:52:50 +0200971 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
972 expandedBubble.setContentVisibility(true);
973 }
974 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200975
976 @Override
977 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -0700978 final Bubble expandedBubble = mStackView != null
979 ? mStackView.getExpandedBubble()
980 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -0700981 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
982 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +0200983 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +0200984 }
Mady Mellorca184aae2019-09-17 16:07:12 -0700985 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +0200986 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500987 }
988
Mady Mellorca0c24c2019-05-16 16:14:32 -0700989 /**
990 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
991 *
992 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
993 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
994 *
995 * @param context the context to use.
996 * @param entry the entry to bubble.
997 */
998 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
999 PendingIntent intent = entry.getBubbleMetadata() != null
1000 ? entry.getBubbleMetadata().getIntent()
1001 : null;
Mady Mellor7f234902019-10-20 12:06:29 -07001002 return canLaunchIntentInActivityView(context, entry, intent);
1003 }
1004
1005 static boolean canLaunchIntentInActivityView(Context context, NotificationEntry entry,
1006 PendingIntent intent) {
Mady Mellorca0c24c2019-05-16 16:14:32 -07001007 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001008 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001009 return false;
1010 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001011 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1012 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001013 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001014 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001015 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001016 Log.w(TAG, "Unable to send as bubble, "
1017 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001018 + intent);
1019 return false;
1020 }
1021 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001022 Log.w(TAG, "Unable to send as bubble, "
1023 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001024 + intent);
1025 return false;
1026 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001027 return true;
1028 }
1029
Joshua Tsujia19515f2019-02-13 18:02:29 -05001030 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001031 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001032 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001033 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1034 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1035 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001036 }
1037 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001038 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001039}