blob: 1052a991dff81124a90164fb67494e3e3b1aad16 [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;
Ned Burnsf81c4c42019-01-07 14:10:43 -050081import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellor22f2f072019-04-18 13:26:18 -070082import com.android.systemui.statusbar.phone.NotificationGroupManager;
Mady Mellor7f234902019-10-20 12:06:29 -070083import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080084import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070085import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040086import com.android.systemui.statusbar.policy.ZenModeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080087
Mady Mellor70cba7bb2019-07-02 15:06:07 -070088import java.io.FileDescriptor;
89import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -050090import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -040091import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -070092import java.util.ArrayList;
Mady Mellore80930e2019-03-21 16:00:45 -070093import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050094
Jason Monk27d01a622018-12-10 15:57:09 -050095import javax.inject.Inject;
96import javax.inject.Singleton;
97
Mady Mellor7f234902019-10-20 12:06:29 -070098import dagger.Lazy;
99
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800100/**
101 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
102 * Bubbles can be expanded to show more content.
103 *
104 * The controller manages addition, removal, and visible state of bubbles on screen.
105 */
Jason Monk27d01a622018-12-10 15:57:09 -0500106@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -0400107public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800108
Issei Suzukia8d07312019-06-07 12:56:19 +0200109 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800110
Mark Renouf08bc42a2019-03-07 13:01:59 -0500111 @Retention(SOURCE)
112 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400113 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700114 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400115 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500116 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700117
Mark Renouf08bc42a2019-03-07 13:01:59 -0500118 static final int DISMISS_USER_GESTURE = 1;
119 static final int DISMISS_AGED = 2;
120 static final int DISMISS_TASK_FINISHED = 3;
121 static final int DISMISS_BLOCKED = 4;
122 static final int DISMISS_NOTIF_CANCEL = 5;
123 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700124 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400125 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700126 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700127 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500128
Ned Burns01e38212019-01-03 16:32:52 -0500129 private final Context mContext;
130 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500131 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800132 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800133 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100134 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700135 private final NotificationGroupManager mNotificationGroupManager;
Mady Mellor7f234902019-10-20 12:06:29 -0700136 private final Lazy<ShadeController> mShadeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800137
Mady Mellor3dff9e62019-02-05 18:12:53 -0800138 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400139 @Nullable private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800140
Mark Renoufc19b4732019-06-26 12:08:33 -0400141 // Tracks the id of the current (foreground) user.
142 private int mCurrentUserId;
143 // Saves notification keys of active bubbles when users are switched.
144 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
145
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800146 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500147 private final StatusBarWindowController mStatusBarWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400148 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800149 private StatusBarStateListener mStatusBarStateListener;
150
Mady Melloraa8fef22019-04-11 13:36:40 -0700151 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700152 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800153
Mady Mellord1c78b262018-11-06 18:04:40 -0800154 // Used for determining view rect for touch interaction
155 private Rect mTempRect = new Rect();
156
Mark Renoufc19b4732019-06-26 12:08:33 -0400157 // Listens to user switch so bubbles can be saved and restored.
158 private final NotificationLockscreenUserManager mNotifUserManager;
159
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400160 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
161 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
162
Mady Mellor5549dd22018-11-06 18:07:34 -0800163 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800164 * Listener to be notified when some states of the bubbles change.
165 */
166 public interface BubbleStateChangeListener {
167 /**
168 * Called when the stack has bubbles or no longer has bubbles.
169 */
170 void onHasBubblesChanged(boolean hasBubbles);
171 }
172
Mady Mellorcd9b1302018-11-06 18:08:04 -0800173 /**
174 * Listener to find out about stack expansion / collapse events.
175 */
176 public interface BubbleExpandListener {
177 /**
178 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700179 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800180 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800181 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800182 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800183 void onBubbleExpandChanged(boolean isExpanding, String key);
184 }
185
186 /**
187 * Listens for the current state of the status bar and updates the visibility state
188 * of bubbles as needed.
189 */
190 private class StatusBarStateListener implements StatusBarStateController.StateListener {
191 private int mState;
192 /**
193 * Returns the current status bar state.
194 */
195 public int getCurrentState() {
196 return mState;
197 }
198
199 @Override
200 public void onStateChanged(int newState) {
201 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400202 boolean shouldCollapse = (mState != SHADE);
203 if (shouldCollapse) {
204 collapseStack();
205 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700206 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800207 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800208 }
209
Jason Monk27d01a622018-12-10 15:57:09 -0500210 @Inject
Mady Mellor7f234902019-10-20 12:06:29 -0700211 public BubbleController(Context context,
212 StatusBarWindowController statusBarWindowController,
213 StatusBarStateController statusBarStateController,
214 Lazy<ShadeController> shadeController,
215 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700216 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400217 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400218 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700219 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700220 NotificationGroupManager groupManager,
221 NotificationEntryManager entryManager) {
222 this(context, statusBarWindowController, statusBarStateController, shadeController,
223 data, null /* synchronizer */, configurationController, interruptionStateProvider,
224 zenModeController, notifUserManager, groupManager, entryManager);
225 }
226
227 public BubbleController(Context context,
228 StatusBarWindowController statusBarWindowController,
229 StatusBarStateController statusBarStateController,
230 Lazy<ShadeController> shadeController,
231 BubbleData data,
232 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
233 ConfigurationController configurationController,
234 NotificationInterruptionStateProvider interruptionStateProvider,
235 ZenModeController zenModeController,
236 NotificationLockscreenUserManager notifUserManager,
237 NotificationGroupManager groupManager,
238 NotificationEntryManager entryManager) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800239 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700240 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400241 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400242 mZenModeController = zenModeController;
243 mZenModeController.addCallback(new ZenModeController.Callback() {
244 @Override
245 public void onZenChanged(int zen) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700246 if (mStackView != null) {
247 mStackView.updateDots();
248 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400249 }
250
251 @Override
252 public void onConfigChanged(ZenModeConfig config) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700253 if (mStackView != null) {
254 mStackView.updateDots();
255 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400256 }
257 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700258
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700259 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800260
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400261 mBubbleData = data;
262 mBubbleData.setListener(mBubbleDataListener);
263
Mady Mellor7f234902019-10-20 12:06:29 -0700264 mNotificationEntryManager = entryManager;
Ned Burns01e38212019-01-03 16:32:52 -0500265 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700266 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellor22f2f072019-04-18 13:26:18 -0700267 mNotificationGroupManager = groupManager;
Mady Mellor740d85d2019-07-09 15:26:47 -0700268 mNotificationGroupManager.addOnGroupChangeListener(
269 new NotificationGroupManager.OnGroupChangeListener() {
270 @Override
271 public void onGroupSuppressionChanged(
272 NotificationGroupManager.NotificationGroup group,
273 boolean suppressed) {
274 // More notifications could be added causing summary to no longer
275 // be suppressed -- in this case need to remove the key.
276 final String groupKey = group.summary != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400277 ? group.summary.getSbn().getGroupKey()
Mady Mellor740d85d2019-07-09 15:26:47 -0700278 : null;
279 if (!suppressed && groupKey != null
280 && mBubbleData.isSummarySuppressed(groupKey)) {
281 mBubbleData.removeSuppressedSummary(groupKey);
282 }
283 }
284 });
Mady Mellorb4991e62019-01-10 15:14:51 -0800285
Mady Mellor7f234902019-10-20 12:06:29 -0700286 mShadeController = shadeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800287 mStatusBarWindowController = statusBarWindowController;
288 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700289 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500290
Mark Renoufcecc77b2019-01-30 16:32:24 -0500291 mTaskStackListener = new BubbleTaskStackListener();
292 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800293
Joshua Tsujia19515f2019-02-13 18:02:29 -0500294 try {
295 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
296 } catch (RemoteException e) {
297 e.printStackTrace();
298 }
Issei Suzukic0387542019-03-08 17:31:14 +0100299 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700300
301 mBarService = IStatusBarService.Stub.asInterface(
302 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400303
304 mSavedBubbleKeysPerUser = new SparseSetArray<>();
305 mCurrentUserId = mNotifUserManager.getCurrentUserId();
306 mNotifUserManager.addUserChangedListener(
307 newUserId -> {
308 saveBubbles(mCurrentUserId);
309 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
310 restoreBubbles(newUserId);
311 mCurrentUserId = newUserId;
312 });
Mady Mellor5549dd22018-11-06 18:07:34 -0800313 }
314
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400315 /**
316 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
317 * method initializes the stack view and adds it to the StatusBar just above the scrim.
318 */
319 private void ensureStackViewCreated() {
320 if (mStackView == null) {
321 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
322 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Lyn Hanbde48202019-05-29 19:18:29 -0700323 int bubbleScrimIndex = sbv.indexOfChild(sbv.findViewById(R.id.scrim_for_bubble));
324 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
325 sbv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400326 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
327 if (mExpandListener != null) {
328 mStackView.setExpandListener(mExpandListener);
329 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400330 }
331 }
332
Mark Renoufc19b4732019-06-26 12:08:33 -0400333 /**
334 * Records the notification key for any active bubbles. These are used to restore active
335 * bubbles when the user returns to the foreground.
336 *
337 * @param userId the id of the user
338 */
339 private void saveBubbles(@UserIdInt int userId) {
340 // First clear any existing keys that might be stored.
341 mSavedBubbleKeysPerUser.remove(userId);
342 // Add in all active bubbles for the current user.
343 for (Bubble bubble: mBubbleData.getBubbles()) {
344 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
345 }
346 }
347
348 /**
349 * Promotes existing notifications to Bubbles if they were previously bubbles.
350 *
351 * @param userId the id of the user
352 */
353 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400354 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
355 if (savedBubbleKeys == null) {
356 // There were no bubbles saved for this used.
357 return;
358 }
Evan Laird181de622019-10-24 09:53:02 -0400359 for (NotificationEntry e :
360 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400361 if (savedBubbleKeys.contains(e.getKey())
Mark Renoufc19b4732019-06-26 12:08:33 -0400362 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
363 && canLaunchInActivityView(mContext, e)) {
364 updateBubble(e, /* suppressFlyout= */ true);
365 }
366 }
367 // Finally, remove the entries for this user now that bubbles are restored.
368 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
369 }
370
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700371 @Override
372 public void onUiModeChanged() {
373 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700374 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700375 }
376 }
377
378 @Override
379 public void onOverlayChanged() {
380 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700381 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700382 }
383 }
384
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400385 @Override
386 public void onConfigChanged(Configuration newConfig) {
387 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400388 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700389 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400390 }
391 }
392
Mady Mellor5549dd22018-11-06 18:07:34 -0800393 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800394 * Set a listener to be notified when some states of the bubbles change.
395 */
396 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
397 mStateChangeListener = listener;
398 }
399
400 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800401 * Set a listener to be notified of bubble expand events.
402 */
403 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100404 mExpandListener = ((isExpanding, key) -> {
405 if (listener != null) {
406 listener.onBubbleExpandChanged(isExpanding, key);
407 }
408 mStatusBarWindowController.setBubbleExpanded(isExpanding);
409 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800410 if (mStackView != null) {
411 mStackView.setExpandListener(mExpandListener);
412 }
413 }
414
415 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800416 * Whether or not there are bubbles present, regardless of them being visible on the
417 * screen (e.g. if on AOD).
418 */
419 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800420 if (mStackView == null) {
421 return false;
422 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400423 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800424 }
425
426 /**
427 * Whether the stack of bubbles is expanded or not.
428 */
429 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400430 return mBubbleData.isExpanded();
431 }
432
433 /**
434 * Tell the stack of bubbles to expand.
435 */
436 public void expandStack() {
437 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800438 }
439
440 /**
441 * Tell the stack of bubbles to collapse.
442 */
443 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400444 mBubbleData.setExpanded(false /* expanded */);
445 }
446
Mady Mellorce23c462019-06-17 17:30:07 -0700447 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700448 * True if either:
449 * (1) There is a bubble associated with the provided key and if its notification is hidden
450 * from the shade.
451 * (2) There is a group summary associated with the provided key that is hidden from the shade
452 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700453 *
Mady Mellore28fe102019-07-09 15:33:32 -0700454 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700455 */
456 public boolean isBubbleNotificationSuppressedFromShade(String key) {
Mady Mellore28fe102019-07-09 15:33:32 -0700457 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorce23c462019-06-17 17:30:07 -0700458 && !mBubbleData.getBubbleWithKey(key).showInShadeWhenBubble();
Evan Laird181de622019-10-24 09:53:02 -0400459 NotificationEntry entry = mNotificationEntryManager.getActiveNotificationUnfiltered(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400460 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellore28fe102019-07-09 15:33:32 -0700461 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700462 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
463 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700464 }
465
Mark Renouf71a3af62019-04-08 15:02:54 -0400466 void selectBubble(Bubble bubble) {
467 mBubbleData.setSelectedBubble(bubble);
468 }
469
470 @VisibleForTesting
471 void selectBubble(String key) {
472 Bubble bubble = mBubbleData.getBubbleWithKey(key);
473 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800474 }
475
476 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400477 * Request the stack expand if needed, then select the specified Bubble as current.
478 *
479 * @param notificationKey the notification key for the bubble to be selected
480 */
481 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400482 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
483 if (bubble != null) {
484 mBubbleData.setSelectedBubble(bubble);
485 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400486 }
487 }
488
489 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800490 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
491 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500492 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400493 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800494 }
495
496 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500497 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
498 * is forwarded a back key down/up pair.
499 */
500 public void performBackPressIfNeeded() {
501 if (mStackView != null) {
502 mStackView.performBackPressIfNeeded();
503 }
504 }
505
506 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800507 * Adds or updates a bubble associated with the provided notification entry.
508 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400509 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800510 */
Mark Renouff97ed462019-04-05 13:46:24 -0400511 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700512 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400513 }
514
515 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700516 updateBubble(notif, suppressFlyout, true /* showInShade */);
517 }
518
519 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700520 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400521 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700522 notif.setInterruption();
523 }
Mady Mellor7f234902019-10-20 12:06:29 -0700524 mBubbleData.notificationEntryUpdated(notif, suppressFlyout, showInShade);
525 }
526
527 /**
528 * Called when a user has indicated that an active notification should be shown as a bubble.
529 * <p>
530 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
531 * the notification from appearing in the shade.
532 *
533 * @param entry the notification to show as a bubble.
534 */
535 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
536 mShadeController.get().collapsePanel(true);
537 entry.setFlagBubble(true);
538 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
539 mBubbleData.getBubbleWithKey(entry.getKey()).setUserCreated(true);
540 }
541
542 /**
543 * Called when a user has indicated that an active notification appearing as a bubble should
544 * no longer be shown as a bubble.
545 *
546 * @param entry the notification to no longer show as a bubble.
547 */
548 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
549 entry.setFlagBubble(false);
550 removeBubble(entry.getKey(), DISMISS_BLOCKED);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800551 }
552
553 /**
554 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500555 * <p>
556 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800557 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500558 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500559 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400560 // TEMP: refactor to change this to pass entry
561 Bubble bubble = mBubbleData.getBubbleWithKey(key);
562 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700563 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800564 }
565 }
566
Ned Burns01e38212019-01-03 16:32:52 -0500567 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700568 private final NotificationRemoveInterceptor mRemoveInterceptor =
569 new NotificationRemoveInterceptor() {
570 @Override
571 public boolean onNotificationRemoveRequested(String key, int reason) {
Evan Laird181de622019-10-24 09:53:02 -0400572 NotificationEntry entry =
573 mNotificationEntryManager.getActiveNotificationUnfiltered(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400574 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellor22f2f072019-04-18 13:26:18 -0700575 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
576
577 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
Mady Mellore28fe102019-07-09 15:33:32 -0700578 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
579 && mBubbleData.getSummaryKey(groupKey).equals(key));
Mady Mellor22f2f072019-04-18 13:26:18 -0700580 boolean isSummary = entry != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400581 && entry.getSbn().getNotification().isGroupSummary();
Mady Mellore28fe102019-07-09 15:33:32 -0700582 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
583 && bubbleChildren != null && !bubbleChildren.isEmpty();
Mady Mellor22f2f072019-04-18 13:26:18 -0700584
585 if (!inBubbleData && !isSummaryOfBubbles) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700586 return false;
587 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700588
589 final boolean isClearAll = reason == REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700590 final boolean isUserDimiss = reason == REASON_CANCEL || reason == REASON_CLICK;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700591 final boolean isAppCancel = reason == REASON_APP_CANCEL
592 || reason == REASON_APP_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700593 final boolean isSummaryCancel = reason == REASON_GROUP_SUMMARY_CANCELED;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700594
595 // Need to check for !appCancel here because the notification may have
596 // previously been dismissed & entry.isRowDismissed would still be true
Mady Mellorca184aae2019-09-17 16:07:12 -0700597 boolean userRemovedNotif = (entry != null && entry.isRowDismissed() && !isAppCancel)
Mady Mellor22f2f072019-04-18 13:26:18 -0700598 || isClearAll || isUserDimiss || isSummaryCancel;
599
600 if (isSummaryOfBubbles) {
601 return handleSummaryRemovalInterception(entry, userRemovedNotif);
602 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700603
604 // The bubble notification sticks around in the data as long as the bubble is
605 // not dismissed and the app hasn't cancelled the notification.
Mady Mellor22f2f072019-04-18 13:26:18 -0700606 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellorca184aae2019-09-17 16:07:12 -0700607 boolean bubbleExtended = entry != null && entry.isBubble() && userRemovedNotif;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700608 if (bubbleExtended) {
Mady Mellorce23c462019-06-17 17:30:07 -0700609 bubble.setShowInShadeWhenBubble(false);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700610 bubble.setShowBubbleDot(false);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700611 if (mStackView != null) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400612 mStackView.updateDotVisibility(entry.getKey());
Mady Mellorc2ff0112019-03-28 14:18:06 -0700613 }
Beverly85d4c192019-09-30 11:40:39 -0400614 mNotificationEntryManager.updateNotifications(
615 "BubbleController.onNotificationRemoveRequested");
Mady Mellorc2ff0112019-03-28 14:18:06 -0700616 return true;
Mady Mellor7f234902019-10-20 12:06:29 -0700617 } else if (!userRemovedNotif && entry != null && !bubble.isUserCreated()) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700618 // This wasn't a user removal so we should remove the bubble as well
619 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
620 return false;
621 }
622 return false;
623 }
624 };
625
Mady Mellor22f2f072019-04-18 13:26:18 -0700626 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
627 boolean userRemovedNotif) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400628 String groupKey = summary.getSbn().getGroupKey();
Mady Mellor22f2f072019-04-18 13:26:18 -0700629 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
630
631 if (userRemovedNotif) {
632 // If it's a user dismiss we mark the children to be hidden from the shade.
633 for (int i = 0; i < bubbleChildren.size(); i++) {
634 Bubble bubbleChild = bubbleChildren.get(i);
635 // As far as group manager is concerned, once a child is no longer shown
636 // in the shade, it is essentially removed.
637 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
638 bubbleChild.setShowInShadeWhenBubble(false);
639 bubbleChild.setShowBubbleDot(false);
640 if (mStackView != null) {
641 mStackView.updateDotVisibility(bubbleChild.getKey());
642 }
643 }
644 // And since all children are removed, remove the summary.
645 mNotificationGroupManager.onEntryRemoved(summary);
646
647 // If the summary was auto-generated we don't need to keep that notification around
648 // because apps can't cancel it; so we only intercept & suppress real summaries.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400649 boolean isAutogroupSummary = (summary.getSbn().getNotification().flags
Mady Mellor22f2f072019-04-18 13:26:18 -0700650 & FLAG_AUTOGROUP_SUMMARY) != 0;
Mady Mellore28fe102019-07-09 15:33:32 -0700651 if (!isAutogroupSummary) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400652 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
653 summary.getKey());
Mady Mellore28fe102019-07-09 15:33:32 -0700654 // Tell shade to update for the suppression
Beverly85d4c192019-09-30 11:40:39 -0400655 mNotificationEntryManager.updateNotifications(
656 "BubbleController.handleSummaryRemovalInterception");
Mady Mellore28fe102019-07-09 15:33:32 -0700657 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700658 return !isAutogroupSummary;
659 } else {
Mady Mellore28fe102019-07-09 15:33:32 -0700660 // If it's not a user dismiss it's a cancel.
661 mBubbleData.removeSuppressedSummary(groupKey);
662
Mady Mellor22f2f072019-04-18 13:26:18 -0700663 // Remove any associated bubble children.
664 for (int i = 0; i < bubbleChildren.size(); i++) {
665 Bubble bubbleChild = bubbleChildren.get(i);
666 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
667 DISMISS_GROUP_CANCELLED);
668 }
669 return false;
670 }
671 }
672
Mady Mellorc2ff0112019-03-28 14:18:06 -0700673 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500674 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
675 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500676 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellor7f234902019-10-20 12:06:29 -0700677 Bubble b = mBubbleData.getBubbleWithKey(entry.getKey());
678 BubbleExperimentConfig.adjustForExperiments(mContext, entry, b);
679
Mady Mellorca0c24c2019-05-16 16:14:32 -0700680 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
681 && canLaunchInActivityView(mContext, entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400682 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800683 }
684 }
685
686 @Override
687 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellor7f234902019-10-20 12:06:29 -0700688 Bubble b = mBubbleData.getBubbleWithKey(entry.getKey());
689 BubbleExperimentConfig.adjustForExperiments(mContext, entry, b);
690
Mady Mellorca0c24c2019-05-16 16:14:32 -0700691 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
692 && canLaunchInActivityView(mContext, entry);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400693 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
Mady Melloraa8fef22019-04-11 13:36:40 -0700694 // It was previously a bubble but no longer a bubble -- lets remove it
Ned Burns00b4b2d2019-10-17 22:09:27 -0400695 removeBubble(entry.getKey(), DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700696 } else if (shouldBubble) {
Mark Renouff97ed462019-04-05 13:46:24 -0400697 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800698 }
699 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400700
701 @Override
702 public void onNotificationRankingUpdated(RankingMap rankingMap) {
703 // Forward to BubbleData to block any bubbles which should no longer be shown
704 mBubbleData.notificationRankingUpdated(rankingMap);
705 }
Ned Burns01e38212019-01-03 16:32:52 -0500706 };
707
Mark Renouf71a3af62019-04-08 15:02:54 -0400708 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400709 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400710
Mark Renouf3bc5b362019-04-05 14:37:59 -0400711 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400712 public void applyUpdate(BubbleData.Update update) {
713 if (mStackView == null && update.addedBubble != null) {
714 // Lazy init stack view when the first bubble is added.
715 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400716 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400717
718 // If not yet initialized, ignore all other changes.
719 if (mStackView == null) {
720 return;
721 }
722
723 if (update.addedBubble != null) {
724 mStackView.addBubble(update.addedBubble);
725 }
726
727 // Collapsing? Do this first before remaining steps.
728 if (update.expandedChanged && !update.expanded) {
729 mStackView.setExpanded(false);
730 }
731
732 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700733 ArrayList<Pair<Bubble, Integer>> removedBubbles =
734 new ArrayList<>(update.removedBubbles);
735 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400736 final Bubble bubble = removed.first;
737 @DismissReason final int reason = removed.second;
738 mStackView.removeBubble(bubble);
739
Mark Renoufc19b4732019-06-26 12:08:33 -0400740 // If the bubble is removed for user switching, leave the notification in place.
741 if (reason != DISMISS_USER_CHANGED) {
742 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
743 && !bubble.showInShadeWhenBubble()) {
744 // The bubble is gone & the notification is gone, time to actually remove it
745 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400746 bubble.getEntry().getSbn(), UNDEFINED_DISMISS_REASON);
Mark Renoufc19b4732019-06-26 12:08:33 -0400747 } else {
748 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400749 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700750
Mark Renoufc19b4732019-06-26 12:08:33 -0400751 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
752 // will get right result back
753 try {
754 mBarService.onNotificationBubbleChanged(bubble.getKey(),
755 false /* isBubble */);
756 } catch (RemoteException e) {
757 // Bad things have happened
758 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400759 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700760
Mady Mellore28fe102019-07-09 15:33:32 -0700761 // Check if removed bubble has an associated suppressed group summary that needs
762 // to be removed now.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400763 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700764 if (mBubbleData.isSummarySuppressed(groupKey)
765 && mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
766 // Time to actually remove the summary.
767 String notifKey = mBubbleData.getSummaryKey(groupKey);
768 mBubbleData.removeSuppressedSummary(groupKey);
769 NotificationEntry entry =
Evan Laird181de622019-10-24 09:53:02 -0400770 mNotificationEntryManager.getActiveNotificationUnfiltered(notifKey);
Mady Mellore28fe102019-07-09 15:33:32 -0700771 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400772 entry.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellore28fe102019-07-09 15:33:32 -0700773 }
774
Mady Mellor22f2f072019-04-18 13:26:18 -0700775 // Check if summary should be removed from NoManGroup
776 NotificationEntry summary = mNotificationGroupManager.getLogicalGroupSummary(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400777 bubble.getEntry().getSbn());
Mady Mellor22f2f072019-04-18 13:26:18 -0700778 if (summary != null) {
779 ArrayList<NotificationEntry> summaryChildren =
Ned Burns00b4b2d2019-10-17 22:09:27 -0400780 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
781 boolean isSummaryThisNotif = summary.getKey().equals(
782 bubble.getEntry().getKey());
Mady Mellore4348272019-07-29 17:43:36 -0700783 if (!isSummaryThisNotif
784 && (summaryChildren == null || summaryChildren.isEmpty())) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700785 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400786 summary.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellor22f2f072019-04-18 13:26:18 -0700787 }
788 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700789 }
790 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400791
Mark Renouf82a40e62019-05-23 16:16:24 -0400792 if (update.updatedBubble != null) {
793 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400794 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400795
Mark Renouf82a40e62019-05-23 16:16:24 -0400796 if (update.orderChanged) {
797 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400798 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400799
Mark Renouf82a40e62019-05-23 16:16:24 -0400800 if (update.selectionChanged) {
801 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700802 if (update.selectedBubble != null) {
803 mNotificationGroupManager.updateSuppression(
804 update.selectedBubble.getEntry());
805 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400806 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400807
Mark Renouf82a40e62019-05-23 16:16:24 -0400808 // Expanding? Apply this last.
809 if (update.expandedChanged && update.expanded) {
810 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400811 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400812
Beverly85d4c192019-09-30 11:40:39 -0400813 mNotificationEntryManager.updateNotifications(
814 "BubbleData.Listener.applyUpdate");
Lyn Han6c40fe72019-05-08 14:06:33 -0700815 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400816
Issei Suzukia8d07312019-06-07 12:56:19 +0200817 if (DEBUG_BUBBLE_CONTROLLER) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400818 Log.d(TAG, "[BubbleData]");
819 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
820 mBubbleData.getSelectedBubble()));
821
822 if (mStackView != null) {
823 Log.d(TAG, "[BubbleStackView]");
824 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
825 mStackView.getExpandedBubble()));
826 }
827 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400828 }
829 };
830
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800831 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400832 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700833 * Updates the visibility of the bubbles based on current state.
834 * Does not un-bubble, just hides or un-hides. Notifies any
835 * {@link BubbleStateChangeListener}s of visibility changes.
836 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800837 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700838 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800839 if (mStackView == null) {
840 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800841 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800842 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
843 // Bubbles only appear in unlocked shade
844 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +0000845 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800846 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800847 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700848
Mady Mellor698d9e82019-08-01 23:11:53 +0000849 // Let listeners know if bubble state changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700850 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +0000851 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellor88552b82019-08-05 22:38:59 +0000852 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -0700853 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
854 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
855 }
856
857 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800858 }
859
860 /**
861 * Rect indicating the touchable region for the bubble stack / expanded stack.
862 */
863 public Rect getTouchableRegion() {
864 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
865 return null;
866 }
867 mStackView.getBoundsOnScreen(mTempRect);
868 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800869 }
870
Mady Mellor390bff42019-04-05 15:09:01 -0700871 /**
872 * The display id of the expanded view, if the stack is expanded and not occluded by the
873 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
874 */
875 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200876 final Bubble bubble = getExpandedBubble(context);
877 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
878 }
879
880 @Nullable
881 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700882 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200883 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700884 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200885 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700886 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200887 final Bubble expandedBubble = mStackView.getExpandedBubble();
888 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700889 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200890 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700891 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200892 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700893 }
894
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800895 @VisibleForTesting
896 BubbleStackView getStackView() {
897 return mStackView;
898 }
899
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700900 /**
901 * Description of current bubble state.
902 */
903 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
904 pw.println("BubbleController state:");
905 mBubbleData.dump(fd, pw, args);
906 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -0400907 if (mStackView != null) {
908 mStackView.dump(fd, pw, args);
909 }
910 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700911 }
912
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400913 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
914 StringBuilder sb = new StringBuilder();
915 for (Bubble bubble : bubbles) {
916 if (bubble == null) {
917 sb.append(" <null> !!!!!\n");
918 } else {
919 boolean isSelected = (bubble == selected);
920 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
921 ((isSelected) ? "->" : " "),
922 bubble.getLastActivity(),
923 (bubble.isOngoing() ? 1 : 0),
924 bubble.getKey()));
925 }
926 }
927 return sb.toString();
928 }
929
Mady Mellore80930e2019-03-21 16:00:45 -0700930 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -0500931 * This task stack listener is responsible for responding to tasks moved to the front
932 * which are on the default (main) display. When this happens, expanded bubbles must be
933 * collapsed so the user may interact with the app which was just moved to the front.
934 * <p>
935 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
936 * these calls via a main thread Handler.
937 */
938 @MainThread
939 private class BubbleTaskStackListener extends TaskStackChangeListener {
940
Mark Renoufcecc77b2019-01-30 16:32:24 -0500941 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500942 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
943 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -0700944 if (!mStackView.isExpansionAnimating()) {
945 mBubbleData.setExpanded(false);
946 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500947 }
948 }
949
Mark Renoufcecc77b2019-01-30 16:32:24 -0500950 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500951 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500952 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400953 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500954 }
955 }
Mark Renouf446251d2019-04-26 10:22:41 -0400956
957 @Override
958 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
959 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
960 mBubbleData.setExpanded(false);
961 }
962 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200963
964 @Override
965 public void onSingleTaskDisplayDrawn(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -0700966 final Bubble expandedBubble = mStackView != null
967 ? mStackView.getExpandedBubble()
968 : null;
Issei Suzukicac2a502019-04-16 16:52:50 +0200969 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
970 expandedBubble.setContentVisibility(true);
971 }
972 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200973
974 @Override
975 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -0700976 final Bubble expandedBubble = mStackView != null
977 ? mStackView.getExpandedBubble()
978 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -0700979 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
980 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +0200981 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +0200982 }
Mady Mellorca184aae2019-09-17 16:07:12 -0700983 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +0200984 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500985 }
986
Mady Mellorca0c24c2019-05-16 16:14:32 -0700987 /**
988 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
989 *
990 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
991 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
992 *
993 * @param context the context to use.
994 * @param entry the entry to bubble.
995 */
996 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
997 PendingIntent intent = entry.getBubbleMetadata() != null
998 ? entry.getBubbleMetadata().getIntent()
999 : null;
Mady Mellor7f234902019-10-20 12:06:29 -07001000 return canLaunchIntentInActivityView(context, entry, intent);
1001 }
1002
1003 static boolean canLaunchIntentInActivityView(Context context, NotificationEntry entry,
1004 PendingIntent intent) {
Mady Mellorca0c24c2019-05-16 16:14:32 -07001005 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001006 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001007 return false;
1008 }
1009 ActivityInfo info =
1010 intent.getIntent().resolveActivityInfo(context.getPackageManager(), 0);
1011 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001012 Log.w(TAG, "Unable to send as bubble, "
1013 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001014 + intent);
1015 return false;
1016 }
1017 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001018 Log.w(TAG, "Unable to send as bubble, "
1019 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001020 + intent);
1021 return false;
1022 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001023 return true;
1024 }
1025
Joshua Tsujia19515f2019-02-13 18:02:29 -05001026 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001027 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001028 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001029 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1030 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1031 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001032 }
1033 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001034 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001035}