blob: 9f7bdd43fb118a5db32824d96e76088feec08e14 [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;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040050import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080051import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050052import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080053import android.os.ServiceManager;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040054import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040055import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040056import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040057import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040058import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040059import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050060import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080061import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080062import android.widget.FrameLayout;
63
Mark Renouf08bc42a2019-03-07 13:01:59 -050064import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050065import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040066import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050067
Mady Mellorebdbbb92018-11-15 14:36:48 -080068import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070069import com.android.internal.statusbar.IStatusBarService;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080070import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050071import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050072import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000073import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050074import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050075import com.android.systemui.shared.system.WindowManagerWrapper;
Mark Renoufc19b4732019-06-26 12:08:33 -040076import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070077import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050078import com.android.systemui.statusbar.notification.NotificationEntryListener;
79import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080080import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -040081import com.android.systemui.statusbar.notification.collection.NotificationData;
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 Mellorc3d6f7d2018-11-07 09:36:56 -080085import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070086import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040087import com.android.systemui.statusbar.policy.ZenModeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080088
Mady Mellor70cba7bb2019-07-02 15:06:07 -070089import java.io.FileDescriptor;
90import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -050091import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -040092import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -070093import java.util.ArrayList;
Mady Mellore80930e2019-03-21 16:00:45 -070094import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050095
Jason Monk27d01a622018-12-10 15:57:09 -050096import javax.inject.Inject;
97import javax.inject.Singleton;
98
Mady Mellor7f234902019-10-20 12:06:29 -070099import dagger.Lazy;
100
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800101/**
102 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
103 * Bubbles can be expanded to show more content.
104 *
105 * The controller manages addition, removal, and visible state of bubbles on screen.
106 */
Jason Monk27d01a622018-12-10 15:57:09 -0500107@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -0400108public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800109
Issei Suzukia8d07312019-06-07 12:56:19 +0200110 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800111
Mark Renouf08bc42a2019-03-07 13:01:59 -0500112 @Retention(SOURCE)
113 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400114 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700115 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400116 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500117 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700118
Mark Renouf08bc42a2019-03-07 13:01:59 -0500119 static final int DISMISS_USER_GESTURE = 1;
120 static final int DISMISS_AGED = 2;
121 static final int DISMISS_TASK_FINISHED = 3;
122 static final int DISMISS_BLOCKED = 4;
123 static final int DISMISS_NOTIF_CANCEL = 5;
124 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700125 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400126 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700127 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700128 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500129
Ned Burns01e38212019-01-03 16:32:52 -0500130 private final Context mContext;
131 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500132 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800133 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800134 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100135 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700136 private final NotificationGroupManager mNotificationGroupManager;
Mady Mellor7f234902019-10-20 12:06:29 -0700137 private final Lazy<ShadeController> mShadeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800138
Mady Mellor3dff9e62019-02-05 18:12:53 -0800139 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400140 @Nullable private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800141
Mark Renoufc19b4732019-06-26 12:08:33 -0400142 // Tracks the id of the current (foreground) user.
143 private int mCurrentUserId;
144 // Saves notification keys of active bubbles when users are switched.
145 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
146
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800147 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500148 private final StatusBarWindowController mStatusBarWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400149 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800150 private StatusBarStateListener mStatusBarStateListener;
151
Mady Melloraa8fef22019-04-11 13:36:40 -0700152 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700153 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800154
Mady Mellord1c78b262018-11-06 18:04:40 -0800155 // Used for determining view rect for touch interaction
156 private Rect mTempRect = new Rect();
157
Mark Renoufc19b4732019-06-26 12:08:33 -0400158 // Listens to user switch so bubbles can be saved and restored.
159 private final NotificationLockscreenUserManager mNotifUserManager;
160
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400161 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
162 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
163
Mady Mellor5549dd22018-11-06 18:07:34 -0800164 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800165 * Listener to be notified when some states of the bubbles change.
166 */
167 public interface BubbleStateChangeListener {
168 /**
169 * Called when the stack has bubbles or no longer has bubbles.
170 */
171 void onHasBubblesChanged(boolean hasBubbles);
172 }
173
Mady Mellorcd9b1302018-11-06 18:08:04 -0800174 /**
175 * Listener to find out about stack expansion / collapse events.
176 */
177 public interface BubbleExpandListener {
178 /**
179 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700180 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800181 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800182 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800183 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800184 void onBubbleExpandChanged(boolean isExpanding, String key);
185 }
186
187 /**
188 * Listens for the current state of the status bar and updates the visibility state
189 * of bubbles as needed.
190 */
191 private class StatusBarStateListener implements StatusBarStateController.StateListener {
192 private int mState;
193 /**
194 * Returns the current status bar state.
195 */
196 public int getCurrentState() {
197 return mState;
198 }
199
200 @Override
201 public void onStateChanged(int newState) {
202 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400203 boolean shouldCollapse = (mState != SHADE);
204 if (shouldCollapse) {
205 collapseStack();
206 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700207 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800208 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800209 }
210
Jason Monk27d01a622018-12-10 15:57:09 -0500211 @Inject
Mady Mellor7f234902019-10-20 12:06:29 -0700212 public BubbleController(Context context,
213 StatusBarWindowController statusBarWindowController,
214 StatusBarStateController statusBarStateController,
215 Lazy<ShadeController> shadeController,
216 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700217 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400218 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400219 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700220 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700221 NotificationGroupManager groupManager,
222 NotificationEntryManager entryManager) {
223 this(context, statusBarWindowController, statusBarStateController, shadeController,
224 data, null /* synchronizer */, configurationController, interruptionStateProvider,
225 zenModeController, notifUserManager, groupManager, entryManager);
226 }
227
228 public BubbleController(Context context,
229 StatusBarWindowController statusBarWindowController,
230 StatusBarStateController statusBarStateController,
231 Lazy<ShadeController> shadeController,
232 BubbleData data,
233 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
234 ConfigurationController configurationController,
235 NotificationInterruptionStateProvider interruptionStateProvider,
236 ZenModeController zenModeController,
237 NotificationLockscreenUserManager notifUserManager,
238 NotificationGroupManager groupManager,
239 NotificationEntryManager entryManager) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800240 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700241 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400242 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400243 mZenModeController = zenModeController;
244 mZenModeController.addCallback(new ZenModeController.Callback() {
245 @Override
246 public void onZenChanged(int zen) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700247 if (mStackView != null) {
248 mStackView.updateDots();
249 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400250 }
251
252 @Override
253 public void onConfigChanged(ZenModeConfig config) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700254 if (mStackView != null) {
255 mStackView.updateDots();
256 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400257 }
258 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700259
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700260 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800261
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400262 mBubbleData = data;
263 mBubbleData.setListener(mBubbleDataListener);
264
Mady Mellor7f234902019-10-20 12:06:29 -0700265 mNotificationEntryManager = entryManager;
Ned Burns01e38212019-01-03 16:32:52 -0500266 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700267 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellor22f2f072019-04-18 13:26:18 -0700268 mNotificationGroupManager = groupManager;
Mady Mellor740d85d2019-07-09 15:26:47 -0700269 mNotificationGroupManager.addOnGroupChangeListener(
270 new NotificationGroupManager.OnGroupChangeListener() {
271 @Override
272 public void onGroupSuppressionChanged(
273 NotificationGroupManager.NotificationGroup group,
274 boolean suppressed) {
275 // More notifications could be added causing summary to no longer
276 // be suppressed -- in this case need to remove the key.
277 final String groupKey = group.summary != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400278 ? group.summary.getSbn().getGroupKey()
Mady Mellor740d85d2019-07-09 15:26:47 -0700279 : null;
280 if (!suppressed && groupKey != null
281 && mBubbleData.isSummarySuppressed(groupKey)) {
282 mBubbleData.removeSuppressedSummary(groupKey);
283 }
284 }
285 });
Mady Mellorb4991e62019-01-10 15:14:51 -0800286
Mady Mellor7f234902019-10-20 12:06:29 -0700287 mShadeController = shadeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800288 mStatusBarWindowController = statusBarWindowController;
289 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700290 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500291
Mark Renoufcecc77b2019-01-30 16:32:24 -0500292 mTaskStackListener = new BubbleTaskStackListener();
293 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800294
Joshua Tsujia19515f2019-02-13 18:02:29 -0500295 try {
296 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
297 } catch (RemoteException e) {
298 e.printStackTrace();
299 }
Issei Suzukic0387542019-03-08 17:31:14 +0100300 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700301
302 mBarService = IStatusBarService.Stub.asInterface(
303 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400304
305 mSavedBubbleKeysPerUser = new SparseSetArray<>();
306 mCurrentUserId = mNotifUserManager.getCurrentUserId();
307 mNotifUserManager.addUserChangedListener(
308 newUserId -> {
309 saveBubbles(mCurrentUserId);
310 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
311 restoreBubbles(newUserId);
312 mCurrentUserId = newUserId;
313 });
Mady Mellor5549dd22018-11-06 18:07:34 -0800314 }
315
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400316 /**
317 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
318 * method initializes the stack view and adds it to the StatusBar just above the scrim.
319 */
320 private void ensureStackViewCreated() {
321 if (mStackView == null) {
322 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
323 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Lyn Hanbde48202019-05-29 19:18:29 -0700324 int bubbleScrimIndex = sbv.indexOfChild(sbv.findViewById(R.id.scrim_for_bubble));
325 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
326 sbv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400327 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
328 if (mExpandListener != null) {
329 mStackView.setExpandListener(mExpandListener);
330 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400331 }
332 }
333
Mark Renoufc19b4732019-06-26 12:08:33 -0400334 /**
335 * Records the notification key for any active bubbles. These are used to restore active
336 * bubbles when the user returns to the foreground.
337 *
338 * @param userId the id of the user
339 */
340 private void saveBubbles(@UserIdInt int userId) {
341 // First clear any existing keys that might be stored.
342 mSavedBubbleKeysPerUser.remove(userId);
343 // Add in all active bubbles for the current user.
344 for (Bubble bubble: mBubbleData.getBubbles()) {
345 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
346 }
347 }
348
349 /**
350 * Promotes existing notifications to Bubbles if they were previously bubbles.
351 *
352 * @param userId the id of the user
353 */
354 private void restoreBubbles(@UserIdInt int userId) {
355 NotificationData notificationData =
356 mNotificationEntryManager.getNotificationData();
357 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
358 if (savedBubbleKeys == null) {
359 // There were no bubbles saved for this used.
360 return;
361 }
362 for (NotificationEntry e : notificationData.getNotificationsForCurrentUser()) {
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();
Mady Mellore28fe102019-07-09 15:33:32 -0700461 NotificationEntry entry = mNotificationEntryManager.getNotificationData().get(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) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700574 NotificationEntry entry = mNotificationEntryManager.getNotificationData().get(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400575 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellor22f2f072019-04-18 13:26:18 -0700576 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
577
578 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
Mady Mellore28fe102019-07-09 15:33:32 -0700579 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
580 && mBubbleData.getSummaryKey(groupKey).equals(key));
Mady Mellor22f2f072019-04-18 13:26:18 -0700581 boolean isSummary = entry != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400582 && entry.getSbn().getNotification().isGroupSummary();
Mady Mellore28fe102019-07-09 15:33:32 -0700583 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
584 && bubbleChildren != null && !bubbleChildren.isEmpty();
Mady Mellor22f2f072019-04-18 13:26:18 -0700585
586 if (!inBubbleData && !isSummaryOfBubbles) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700587 return false;
588 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700589
590 final boolean isClearAll = reason == REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700591 final boolean isUserDimiss = reason == REASON_CANCEL || reason == REASON_CLICK;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700592 final boolean isAppCancel = reason == REASON_APP_CANCEL
593 || reason == REASON_APP_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700594 final boolean isSummaryCancel = reason == REASON_GROUP_SUMMARY_CANCELED;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700595
596 // Need to check for !appCancel here because the notification may have
597 // previously been dismissed & entry.isRowDismissed would still be true
Mady Mellorca184aae2019-09-17 16:07:12 -0700598 boolean userRemovedNotif = (entry != null && entry.isRowDismissed() && !isAppCancel)
Mady Mellor22f2f072019-04-18 13:26:18 -0700599 || isClearAll || isUserDimiss || isSummaryCancel;
600
601 if (isSummaryOfBubbles) {
602 return handleSummaryRemovalInterception(entry, userRemovedNotif);
603 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700604
605 // The bubble notification sticks around in the data as long as the bubble is
606 // not dismissed and the app hasn't cancelled the notification.
Mady Mellor22f2f072019-04-18 13:26:18 -0700607 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellorca184aae2019-09-17 16:07:12 -0700608 boolean bubbleExtended = entry != null && entry.isBubble() && userRemovedNotif;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700609 if (bubbleExtended) {
Mady Mellorce23c462019-06-17 17:30:07 -0700610 bubble.setShowInShadeWhenBubble(false);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700611 bubble.setShowBubbleDot(false);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700612 if (mStackView != null) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400613 mStackView.updateDotVisibility(entry.getKey());
Mady Mellorc2ff0112019-03-28 14:18:06 -0700614 }
Beverly85d4c192019-09-30 11:40:39 -0400615 mNotificationEntryManager.updateNotifications(
616 "BubbleController.onNotificationRemoveRequested");
Mady Mellorc2ff0112019-03-28 14:18:06 -0700617 return true;
Mady Mellor7f234902019-10-20 12:06:29 -0700618 } else if (!userRemovedNotif && entry != null && !bubble.isUserCreated()) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700619 // This wasn't a user removal so we should remove the bubble as well
620 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
621 return false;
622 }
623 return false;
624 }
625 };
626
Mady Mellor22f2f072019-04-18 13:26:18 -0700627 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
628 boolean userRemovedNotif) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400629 String groupKey = summary.getSbn().getGroupKey();
Mady Mellor22f2f072019-04-18 13:26:18 -0700630 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
631
632 if (userRemovedNotif) {
633 // If it's a user dismiss we mark the children to be hidden from the shade.
634 for (int i = 0; i < bubbleChildren.size(); i++) {
635 Bubble bubbleChild = bubbleChildren.get(i);
636 // As far as group manager is concerned, once a child is no longer shown
637 // in the shade, it is essentially removed.
638 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
639 bubbleChild.setShowInShadeWhenBubble(false);
640 bubbleChild.setShowBubbleDot(false);
641 if (mStackView != null) {
642 mStackView.updateDotVisibility(bubbleChild.getKey());
643 }
644 }
645 // And since all children are removed, remove the summary.
646 mNotificationGroupManager.onEntryRemoved(summary);
647
648 // If the summary was auto-generated we don't need to keep that notification around
649 // because apps can't cancel it; so we only intercept & suppress real summaries.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400650 boolean isAutogroupSummary = (summary.getSbn().getNotification().flags
Mady Mellor22f2f072019-04-18 13:26:18 -0700651 & FLAG_AUTOGROUP_SUMMARY) != 0;
Mady Mellore28fe102019-07-09 15:33:32 -0700652 if (!isAutogroupSummary) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400653 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
654 summary.getKey());
Mady Mellore28fe102019-07-09 15:33:32 -0700655 // Tell shade to update for the suppression
Beverly85d4c192019-09-30 11:40:39 -0400656 mNotificationEntryManager.updateNotifications(
657 "BubbleController.handleSummaryRemovalInterception");
Mady Mellore28fe102019-07-09 15:33:32 -0700658 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700659 return !isAutogroupSummary;
660 } else {
Mady Mellore28fe102019-07-09 15:33:32 -0700661 // If it's not a user dismiss it's a cancel.
662 mBubbleData.removeSuppressedSummary(groupKey);
663
Mady Mellor22f2f072019-04-18 13:26:18 -0700664 // Remove any associated bubble children.
665 for (int i = 0; i < bubbleChildren.size(); i++) {
666 Bubble bubbleChild = bubbleChildren.get(i);
667 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
668 DISMISS_GROUP_CANCELLED);
669 }
670 return false;
671 }
672 }
673
Mady Mellorc2ff0112019-03-28 14:18:06 -0700674 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500675 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
676 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500677 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellor7f234902019-10-20 12:06:29 -0700678 Bubble b = mBubbleData.getBubbleWithKey(entry.getKey());
679 BubbleExperimentConfig.adjustForExperiments(mContext, entry, b);
680
Mady Mellorca0c24c2019-05-16 16:14:32 -0700681 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
682 && canLaunchInActivityView(mContext, entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400683 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800684 }
685 }
686
687 @Override
688 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellor7f234902019-10-20 12:06:29 -0700689 Bubble b = mBubbleData.getBubbleWithKey(entry.getKey());
690 BubbleExperimentConfig.adjustForExperiments(mContext, entry, b);
691
Mady Mellorca0c24c2019-05-16 16:14:32 -0700692 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
693 && canLaunchInActivityView(mContext, entry);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400694 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
Mady Melloraa8fef22019-04-11 13:36:40 -0700695 // It was previously a bubble but no longer a bubble -- lets remove it
Ned Burns00b4b2d2019-10-17 22:09:27 -0400696 removeBubble(entry.getKey(), DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700697 } else if (shouldBubble) {
Mark Renouff97ed462019-04-05 13:46:24 -0400698 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800699 }
700 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400701
702 @Override
703 public void onNotificationRankingUpdated(RankingMap rankingMap) {
704 // Forward to BubbleData to block any bubbles which should no longer be shown
705 mBubbleData.notificationRankingUpdated(rankingMap);
706 }
Ned Burns01e38212019-01-03 16:32:52 -0500707 };
708
Mark Renouf71a3af62019-04-08 15:02:54 -0400709 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400710 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400711
Mark Renouf3bc5b362019-04-05 14:37:59 -0400712 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400713 public void applyUpdate(BubbleData.Update update) {
714 if (mStackView == null && update.addedBubble != null) {
715 // Lazy init stack view when the first bubble is added.
716 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400717 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400718
719 // If not yet initialized, ignore all other changes.
720 if (mStackView == null) {
721 return;
722 }
723
724 if (update.addedBubble != null) {
725 mStackView.addBubble(update.addedBubble);
726 }
727
728 // Collapsing? Do this first before remaining steps.
729 if (update.expandedChanged && !update.expanded) {
730 mStackView.setExpanded(false);
731 }
732
733 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700734 ArrayList<Pair<Bubble, Integer>> removedBubbles =
735 new ArrayList<>(update.removedBubbles);
736 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400737 final Bubble bubble = removed.first;
738 @DismissReason final int reason = removed.second;
739 mStackView.removeBubble(bubble);
740
Mark Renoufc19b4732019-06-26 12:08:33 -0400741 // If the bubble is removed for user switching, leave the notification in place.
742 if (reason != DISMISS_USER_CHANGED) {
743 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
744 && !bubble.showInShadeWhenBubble()) {
745 // The bubble is gone & the notification is gone, time to actually remove it
746 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400747 bubble.getEntry().getSbn(), UNDEFINED_DISMISS_REASON);
Mark Renoufc19b4732019-06-26 12:08:33 -0400748 } else {
749 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400750 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700751
Mark Renoufc19b4732019-06-26 12:08:33 -0400752 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
753 // will get right result back
754 try {
755 mBarService.onNotificationBubbleChanged(bubble.getKey(),
756 false /* isBubble */);
757 } catch (RemoteException e) {
758 // Bad things have happened
759 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400760 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700761
Mady Mellore28fe102019-07-09 15:33:32 -0700762 // Check if removed bubble has an associated suppressed group summary that needs
763 // to be removed now.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400764 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700765 if (mBubbleData.isSummarySuppressed(groupKey)
766 && mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
767 // Time to actually remove the summary.
768 String notifKey = mBubbleData.getSummaryKey(groupKey);
769 mBubbleData.removeSuppressedSummary(groupKey);
770 NotificationEntry entry =
771 mNotificationEntryManager.getNotificationData().get(notifKey);
Mady Mellore28fe102019-07-09 15:33:32 -0700772 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400773 entry.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellore28fe102019-07-09 15:33:32 -0700774 }
775
Mady Mellor22f2f072019-04-18 13:26:18 -0700776 // Check if summary should be removed from NoManGroup
777 NotificationEntry summary = mNotificationGroupManager.getLogicalGroupSummary(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400778 bubble.getEntry().getSbn());
Mady Mellor22f2f072019-04-18 13:26:18 -0700779 if (summary != null) {
780 ArrayList<NotificationEntry> summaryChildren =
Ned Burns00b4b2d2019-10-17 22:09:27 -0400781 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
782 boolean isSummaryThisNotif = summary.getKey().equals(
783 bubble.getEntry().getKey());
Mady Mellore4348272019-07-29 17:43:36 -0700784 if (!isSummaryThisNotif
785 && (summaryChildren == null || summaryChildren.isEmpty())) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700786 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400787 summary.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellor22f2f072019-04-18 13:26:18 -0700788 }
789 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700790 }
791 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400792
Mark Renouf82a40e62019-05-23 16:16:24 -0400793 if (update.updatedBubble != null) {
794 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400795 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400796
Mark Renouf82a40e62019-05-23 16:16:24 -0400797 if (update.orderChanged) {
798 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400799 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400800
Mark Renouf82a40e62019-05-23 16:16:24 -0400801 if (update.selectionChanged) {
802 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700803 if (update.selectedBubble != null) {
804 mNotificationGroupManager.updateSuppression(
805 update.selectedBubble.getEntry());
806 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400807 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400808
Mark Renouf82a40e62019-05-23 16:16:24 -0400809 // Expanding? Apply this last.
810 if (update.expandedChanged && update.expanded) {
811 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400812 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400813
Beverly85d4c192019-09-30 11:40:39 -0400814 mNotificationEntryManager.updateNotifications(
815 "BubbleData.Listener.applyUpdate");
Lyn Han6c40fe72019-05-08 14:06:33 -0700816 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400817
Issei Suzukia8d07312019-06-07 12:56:19 +0200818 if (DEBUG_BUBBLE_CONTROLLER) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400819 Log.d(TAG, "[BubbleData]");
820 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
821 mBubbleData.getSelectedBubble()));
822
823 if (mStackView != null) {
824 Log.d(TAG, "[BubbleStackView]");
825 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
826 mStackView.getExpandedBubble()));
827 }
828 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400829 }
830 };
831
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800832 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400833 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700834 * Updates the visibility of the bubbles based on current state.
835 * Does not un-bubble, just hides or un-hides. Notifies any
836 * {@link BubbleStateChangeListener}s of visibility changes.
837 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800838 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700839 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800840 if (mStackView == null) {
841 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800842 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800843 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
844 // Bubbles only appear in unlocked shade
845 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +0000846 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800847 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800848 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700849
Mady Mellor698d9e82019-08-01 23:11:53 +0000850 // Let listeners know if bubble state changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700851 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +0000852 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellor88552b82019-08-05 22:38:59 +0000853 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -0700854 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
855 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
856 }
857
858 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800859 }
860
861 /**
862 * Rect indicating the touchable region for the bubble stack / expanded stack.
863 */
864 public Rect getTouchableRegion() {
865 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
866 return null;
867 }
868 mStackView.getBoundsOnScreen(mTempRect);
869 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800870 }
871
Mady Mellor390bff42019-04-05 15:09:01 -0700872 /**
873 * The display id of the expanded view, if the stack is expanded and not occluded by the
874 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
875 */
876 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200877 final Bubble bubble = getExpandedBubble(context);
878 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
879 }
880
881 @Nullable
882 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700883 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200884 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700885 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200886 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700887 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200888 final Bubble expandedBubble = mStackView.getExpandedBubble();
889 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700890 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200891 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700892 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200893 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700894 }
895
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800896 @VisibleForTesting
897 BubbleStackView getStackView() {
898 return mStackView;
899 }
900
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700901 /**
902 * Description of current bubble state.
903 */
904 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
905 pw.println("BubbleController state:");
906 mBubbleData.dump(fd, pw, args);
907 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -0400908 if (mStackView != null) {
909 mStackView.dump(fd, pw, args);
910 }
911 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700912 }
913
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400914 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
915 StringBuilder sb = new StringBuilder();
916 for (Bubble bubble : bubbles) {
917 if (bubble == null) {
918 sb.append(" <null> !!!!!\n");
919 } else {
920 boolean isSelected = (bubble == selected);
921 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
922 ((isSelected) ? "->" : " "),
923 bubble.getLastActivity(),
924 (bubble.isOngoing() ? 1 : 0),
925 bubble.getKey()));
926 }
927 }
928 return sb.toString();
929 }
930
Mady Mellore80930e2019-03-21 16:00:45 -0700931 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -0500932 * This task stack listener is responsible for responding to tasks moved to the front
933 * which are on the default (main) display. When this happens, expanded bubbles must be
934 * collapsed so the user may interact with the app which was just moved to the front.
935 * <p>
936 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
937 * these calls via a main thread Handler.
938 */
939 @MainThread
940 private class BubbleTaskStackListener extends TaskStackChangeListener {
941
Mark Renoufcecc77b2019-01-30 16:32:24 -0500942 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500943 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
944 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -0700945 if (!mStackView.isExpansionAnimating()) {
946 mBubbleData.setExpanded(false);
947 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500948 }
949 }
950
Mark Renoufcecc77b2019-01-30 16:32:24 -0500951 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500952 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500953 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400954 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500955 }
956 }
Mark Renouf446251d2019-04-26 10:22:41 -0400957
958 @Override
959 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
960 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
961 mBubbleData.setExpanded(false);
962 }
963 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200964
965 @Override
966 public void onSingleTaskDisplayDrawn(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -0700967 final Bubble expandedBubble = mStackView != null
968 ? mStackView.getExpandedBubble()
969 : null;
Issei Suzukicac2a502019-04-16 16:52:50 +0200970 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
971 expandedBubble.setContentVisibility(true);
972 }
973 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200974
975 @Override
976 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -0700977 final Bubble expandedBubble = mStackView != null
978 ? mStackView.getExpandedBubble()
979 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -0700980 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
981 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +0200982 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +0200983 }
Mady Mellorca184aae2019-09-17 16:07:12 -0700984 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +0200985 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500986 }
987
Mady Mellorca0c24c2019-05-16 16:14:32 -0700988 /**
989 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
990 *
991 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
992 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
993 *
994 * @param context the context to use.
995 * @param entry the entry to bubble.
996 */
997 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
998 PendingIntent intent = entry.getBubbleMetadata() != null
999 ? entry.getBubbleMetadata().getIntent()
1000 : null;
Mady Mellor7f234902019-10-20 12:06:29 -07001001 return canLaunchIntentInActivityView(context, entry, intent);
1002 }
1003
1004 static boolean canLaunchIntentInActivityView(Context context, NotificationEntry entry,
1005 PendingIntent intent) {
Mady Mellorca0c24c2019-05-16 16:14:32 -07001006 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001007 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001008 return false;
1009 }
1010 ActivityInfo info =
1011 intent.getIntent().resolveActivityInfo(context.getPackageManager(), 0);
1012 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001013 Log.w(TAG, "Unable to send as bubble, "
1014 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001015 + intent);
1016 return false;
1017 }
1018 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001019 Log.w(TAG, "Unable to send as bubble, "
1020 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001021 + intent);
1022 return false;
1023 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001024 return true;
1025 }
1026
Joshua Tsujia19515f2019-02-13 18:02:29 -05001027 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001028 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001029 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001030 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1031 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1032 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001033 }
1034 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001035 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001036}