blob: cc548d0c7f8ef13e0be501269d67b2d3d08e3ace [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 Mellorca0c24c2019-05-16 16:14:32 -070021import static android.content.pm.ActivityInfo.DOCUMENT_LAUNCH_ALWAYS;
Mady Mellorc2ff0112019-03-28 14:18:06 -070022import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL;
23import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL;
24import static android.service.notification.NotificationListenerService.REASON_CANCEL;
25import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -070026import static android.service.notification.NotificationListenerService.REASON_CLICK;
27import static android.service.notification.NotificationListenerService.REASON_GROUP_SUMMARY_CANCELED;
Mady Mellor390bff42019-04-05 15:09:01 -070028import static android.view.Display.DEFAULT_DISPLAY;
29import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080030import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080031import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080032import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080033
Issei Suzukia8d07312019-06-07 12:56:19 +020034import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_CONTROLLER;
35import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
36import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080037import static com.android.systemui.statusbar.StatusBarState.SHADE;
Mady Mellor1a4e86f2019-05-03 16:07:23 -070038import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080039
Mark Renoufba5ab512019-05-02 15:21:01 -040040import static java.lang.annotation.ElementType.FIELD;
41import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
42import static java.lang.annotation.ElementType.PARAMETER;
Mark Renouf08bc42a2019-03-07 13:01:59 -050043import static java.lang.annotation.RetentionPolicy.SOURCE;
44
Mark Renoufc19b4732019-06-26 12:08:33 -040045import android.annotation.UserIdInt;
Mark Renoufc808f062019-02-07 15:20:37 -050046import android.app.ActivityManager.RunningTaskInfo;
Mady Mellor66efd5e2019-05-15 13:38:11 -070047import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070048import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080049import android.content.Context;
Mady Mellorca0c24c2019-05-16 16:14:32 -070050import android.content.pm.ActivityInfo;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040051import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080052import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050053import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080054import android.os.ServiceManager;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040055import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040056import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040057import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040058import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040059import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040060import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050061import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080062import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080063import android.widget.FrameLayout;
64
Mark Renouf08bc42a2019-03-07 13:01:59 -050065import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050066import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040067import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050068
Mady Mellorebdbbb92018-11-15 14:36:48 -080069import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070070import com.android.internal.statusbar.IStatusBarService;
Ned Burns01e38212019-01-03 16:32:52 -050071import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080072import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050073import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050074import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000075import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050076import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050077import com.android.systemui.shared.system.WindowManagerWrapper;
Mark Renoufc19b4732019-06-26 12:08:33 -040078import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070079import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050080import com.android.systemui.statusbar.notification.NotificationEntryListener;
81import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080082import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -040083import com.android.systemui.statusbar.notification.collection.NotificationData;
Ned Burnsf81c4c42019-01-07 14:10:43 -050084import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellor22f2f072019-04-18 13:26:18 -070085import com.android.systemui.statusbar.phone.NotificationGroupManager;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080086import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070087import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040088import com.android.systemui.statusbar.policy.ZenModeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080089
Mady Mellor70cba7bb2019-07-02 15:06:07 -070090import java.io.FileDescriptor;
91import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -050092import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -040093import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -070094import java.util.ArrayList;
Mady Mellore80930e2019-03-21 16:00:45 -070095import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050096
Jason Monk27d01a622018-12-10 15:57:09 -050097import javax.inject.Inject;
98import javax.inject.Singleton;
99
Mady 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 Mellorc3d6f7d2018-11-07 09:36:56 -0800136
Mady Mellor3dff9e62019-02-05 18:12:53 -0800137 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400138 @Nullable private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800139
Mark Renoufc19b4732019-06-26 12:08:33 -0400140 // Tracks the id of the current (foreground) user.
141 private int mCurrentUserId;
142 // Saves notification keys of active bubbles when users are switched.
143 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
144
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800145 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500146 private final StatusBarWindowController mStatusBarWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400147 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800148 private StatusBarStateListener mStatusBarStateListener;
149
Mady Melloraa8fef22019-04-11 13:36:40 -0700150 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700151 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800152
Mady Mellord1c78b262018-11-06 18:04:40 -0800153 // Used for determining view rect for touch interaction
154 private Rect mTempRect = new Rect();
155
Mark Renoufc19b4732019-06-26 12:08:33 -0400156 // Listens to user switch so bubbles can be saved and restored.
157 private final NotificationLockscreenUserManager mNotifUserManager;
158
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400159 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
160 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
161
Mady Mellor5549dd22018-11-06 18:07:34 -0800162 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800163 * Listener to be notified when some states of the bubbles change.
164 */
165 public interface BubbleStateChangeListener {
166 /**
167 * Called when the stack has bubbles or no longer has bubbles.
168 */
169 void onHasBubblesChanged(boolean hasBubbles);
170 }
171
Mady Mellorcd9b1302018-11-06 18:08:04 -0800172 /**
173 * Listener to find out about stack expansion / collapse events.
174 */
175 public interface BubbleExpandListener {
176 /**
177 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700178 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800179 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800180 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800181 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800182 void onBubbleExpandChanged(boolean isExpanding, String key);
183 }
184
185 /**
186 * Listens for the current state of the status bar and updates the visibility state
187 * of bubbles as needed.
188 */
189 private class StatusBarStateListener implements StatusBarStateController.StateListener {
190 private int mState;
191 /**
192 * Returns the current status bar state.
193 */
194 public int getCurrentState() {
195 return mState;
196 }
197
198 @Override
199 public void onStateChanged(int newState) {
200 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400201 boolean shouldCollapse = (mState != SHADE);
202 if (shouldCollapse) {
203 collapseStack();
204 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700205 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800206 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800207 }
208
Jason Monk27d01a622018-12-10 15:57:09 -0500209 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800210 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Mady Melloraa8fef22019-04-11 13:36:40 -0700211 BubbleData data, ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400212 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400213 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700214 NotificationLockscreenUserManager notifUserManager,
215 NotificationGroupManager groupManager) {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700216 this(context, statusBarWindowController, data, null /* synchronizer */,
Mark Renoufc19b4732019-06-26 12:08:33 -0400217 configurationController, interruptionStateProvider, zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700218 notifUserManager, groupManager);
Issei Suzukic0387542019-03-08 17:31:14 +0100219 }
220
221 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700222 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
Mady Melloraa8fef22019-04-11 13:36:40 -0700223 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400224 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400225 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700226 NotificationLockscreenUserManager notifUserManager,
227 NotificationGroupManager groupManager) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800228 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700229 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400230 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400231 mZenModeController = zenModeController;
232 mZenModeController.addCallback(new ZenModeController.Callback() {
233 @Override
234 public void onZenChanged(int zen) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700235 if (mStackView != null) {
236 mStackView.updateDots();
237 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400238 }
239
240 @Override
241 public void onConfigChanged(ZenModeConfig config) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700242 if (mStackView != null) {
243 mStackView.updateDots();
244 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400245 }
246 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700247
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700248 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800249
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400250 mBubbleData = data;
251 mBubbleData.setListener(mBubbleDataListener);
252
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800253 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500254 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700255 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellor22f2f072019-04-18 13:26:18 -0700256 mNotificationGroupManager = groupManager;
Mady Mellor740d85d2019-07-09 15:26:47 -0700257 mNotificationGroupManager.addOnGroupChangeListener(
258 new NotificationGroupManager.OnGroupChangeListener() {
259 @Override
260 public void onGroupSuppressionChanged(
261 NotificationGroupManager.NotificationGroup group,
262 boolean suppressed) {
263 // More notifications could be added causing summary to no longer
264 // be suppressed -- in this case need to remove the key.
265 final String groupKey = group.summary != null
266 ? group.summary.notification.getGroupKey()
267 : null;
268 if (!suppressed && groupKey != null
269 && mBubbleData.isSummarySuppressed(groupKey)) {
270 mBubbleData.removeSuppressedSummary(groupKey);
271 }
272 }
273 });
Mady Mellorb4991e62019-01-10 15:14:51 -0800274
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800275 mStatusBarWindowController = statusBarWindowController;
276 mStatusBarStateListener = new StatusBarStateListener();
277 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500278
Mark Renoufcecc77b2019-01-30 16:32:24 -0500279 mTaskStackListener = new BubbleTaskStackListener();
280 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800281
Joshua Tsujia19515f2019-02-13 18:02:29 -0500282 try {
283 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
284 } catch (RemoteException e) {
285 e.printStackTrace();
286 }
Issei Suzukic0387542019-03-08 17:31:14 +0100287 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700288
289 mBarService = IStatusBarService.Stub.asInterface(
290 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400291
292 mSavedBubbleKeysPerUser = new SparseSetArray<>();
293 mCurrentUserId = mNotifUserManager.getCurrentUserId();
294 mNotifUserManager.addUserChangedListener(
295 newUserId -> {
296 saveBubbles(mCurrentUserId);
297 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
298 restoreBubbles(newUserId);
299 mCurrentUserId = newUserId;
300 });
Mady Mellor5549dd22018-11-06 18:07:34 -0800301 }
302
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400303 /**
304 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
305 * method initializes the stack view and adds it to the StatusBar just above the scrim.
306 */
307 private void ensureStackViewCreated() {
308 if (mStackView == null) {
309 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
310 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Lyn Hanbde48202019-05-29 19:18:29 -0700311 int bubbleScrimIndex = sbv.indexOfChild(sbv.findViewById(R.id.scrim_for_bubble));
312 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
313 sbv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400314 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
315 if (mExpandListener != null) {
316 mStackView.setExpandListener(mExpandListener);
317 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400318 }
319 }
320
Mark Renoufc19b4732019-06-26 12:08:33 -0400321 /**
322 * Records the notification key for any active bubbles. These are used to restore active
323 * bubbles when the user returns to the foreground.
324 *
325 * @param userId the id of the user
326 */
327 private void saveBubbles(@UserIdInt int userId) {
328 // First clear any existing keys that might be stored.
329 mSavedBubbleKeysPerUser.remove(userId);
330 // Add in all active bubbles for the current user.
331 for (Bubble bubble: mBubbleData.getBubbles()) {
332 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
333 }
334 }
335
336 /**
337 * Promotes existing notifications to Bubbles if they were previously bubbles.
338 *
339 * @param userId the id of the user
340 */
341 private void restoreBubbles(@UserIdInt int userId) {
342 NotificationData notificationData =
343 mNotificationEntryManager.getNotificationData();
344 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
345 if (savedBubbleKeys == null) {
346 // There were no bubbles saved for this used.
347 return;
348 }
349 for (NotificationEntry e : notificationData.getNotificationsForCurrentUser()) {
350 if (savedBubbleKeys.contains(e.key)
351 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
352 && canLaunchInActivityView(mContext, e)) {
353 updateBubble(e, /* suppressFlyout= */ true);
354 }
355 }
356 // Finally, remove the entries for this user now that bubbles are restored.
357 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
358 }
359
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700360 @Override
361 public void onUiModeChanged() {
362 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700363 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700364 }
365 }
366
367 @Override
368 public void onOverlayChanged() {
369 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700370 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700371 }
372 }
373
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400374 @Override
375 public void onConfigChanged(Configuration newConfig) {
376 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400377 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700378 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400379 }
380 }
381
Mady Mellor5549dd22018-11-06 18:07:34 -0800382 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800383 * Set a listener to be notified when some states of the bubbles change.
384 */
385 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
386 mStateChangeListener = listener;
387 }
388
389 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800390 * Set a listener to be notified of bubble expand events.
391 */
392 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100393 mExpandListener = ((isExpanding, key) -> {
394 if (listener != null) {
395 listener.onBubbleExpandChanged(isExpanding, key);
396 }
397 mStatusBarWindowController.setBubbleExpanded(isExpanding);
398 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800399 if (mStackView != null) {
400 mStackView.setExpandListener(mExpandListener);
401 }
402 }
403
404 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800405 * Whether or not there are bubbles present, regardless of them being visible on the
406 * screen (e.g. if on AOD).
407 */
408 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800409 if (mStackView == null) {
410 return false;
411 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400412 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800413 }
414
415 /**
416 * Whether the stack of bubbles is expanded or not.
417 */
418 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400419 return mBubbleData.isExpanded();
420 }
421
422 /**
423 * Tell the stack of bubbles to expand.
424 */
425 public void expandStack() {
426 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800427 }
428
429 /**
430 * Tell the stack of bubbles to collapse.
431 */
432 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400433 mBubbleData.setExpanded(false /* expanded */);
434 }
435
Mady Mellorce23c462019-06-17 17:30:07 -0700436 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700437 * True if either:
438 * (1) There is a bubble associated with the provided key and if its notification is hidden
439 * from the shade.
440 * (2) There is a group summary associated with the provided key that is hidden from the shade
441 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700442 *
Mady Mellore28fe102019-07-09 15:33:32 -0700443 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700444 */
445 public boolean isBubbleNotificationSuppressedFromShade(String key) {
Mady Mellore28fe102019-07-09 15:33:32 -0700446 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorce23c462019-06-17 17:30:07 -0700447 && !mBubbleData.getBubbleWithKey(key).showInShadeWhenBubble();
Mady Mellore28fe102019-07-09 15:33:32 -0700448 NotificationEntry entry = mNotificationEntryManager.getNotificationData().get(key);
449 String groupKey = entry != null ? entry.notification.getGroupKey() : null;
450 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700451 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
452 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700453 }
454
Mark Renouf71a3af62019-04-08 15:02:54 -0400455 void selectBubble(Bubble bubble) {
456 mBubbleData.setSelectedBubble(bubble);
457 }
458
459 @VisibleForTesting
460 void selectBubble(String key) {
461 Bubble bubble = mBubbleData.getBubbleWithKey(key);
462 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800463 }
464
465 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400466 * Request the stack expand if needed, then select the specified Bubble as current.
467 *
468 * @param notificationKey the notification key for the bubble to be selected
469 */
470 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400471 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
472 if (bubble != null) {
473 mBubbleData.setSelectedBubble(bubble);
474 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400475 }
476 }
477
478 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800479 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
480 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500481 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400482 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800483 }
484
485 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500486 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
487 * is forwarded a back key down/up pair.
488 */
489 public void performBackPressIfNeeded() {
490 if (mStackView != null) {
491 mStackView.performBackPressIfNeeded();
492 }
493 }
494
495 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800496 * Adds or updates a bubble associated with the provided notification entry.
497 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400498 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800499 */
Mark Renouff97ed462019-04-05 13:46:24 -0400500 void updateBubble(NotificationEntry notif) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400501 updateBubble(notif, /* supressFlyout */ false);
502 }
503
504 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700505 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400506 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700507 notif.setInterruption();
508 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400509 mBubbleData.notificationEntryUpdated(notif, suppressFlyout);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800510 }
511
512 /**
513 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500514 * <p>
515 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800516 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500517 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500518 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400519 // TEMP: refactor to change this to pass entry
520 Bubble bubble = mBubbleData.getBubbleWithKey(key);
521 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700522 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800523 }
524 }
525
Ned Burns01e38212019-01-03 16:32:52 -0500526 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700527 private final NotificationRemoveInterceptor mRemoveInterceptor =
528 new NotificationRemoveInterceptor() {
529 @Override
530 public boolean onNotificationRemoveRequested(String key, int reason) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700531 NotificationEntry entry = mNotificationEntryManager.getNotificationData().get(key);
532 String groupKey = entry != null ? entry.notification.getGroupKey() : null;
533 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
534
535 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
Mady Mellore28fe102019-07-09 15:33:32 -0700536 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
537 && mBubbleData.getSummaryKey(groupKey).equals(key));
Mady Mellor22f2f072019-04-18 13:26:18 -0700538 boolean isSummary = entry != null
539 && entry.notification.getNotification().isGroupSummary();
Mady Mellore28fe102019-07-09 15:33:32 -0700540 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
541 && bubbleChildren != null && !bubbleChildren.isEmpty();
Mady Mellor22f2f072019-04-18 13:26:18 -0700542
543 if (!inBubbleData && !isSummaryOfBubbles) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700544 return false;
545 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700546
547 final boolean isClearAll = reason == REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700548 final boolean isUserDimiss = reason == REASON_CANCEL || reason == REASON_CLICK;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700549 final boolean isAppCancel = reason == REASON_APP_CANCEL
550 || reason == REASON_APP_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700551 final boolean isSummaryCancel = reason == REASON_GROUP_SUMMARY_CANCELED;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700552
553 // Need to check for !appCancel here because the notification may have
554 // previously been dismissed & entry.isRowDismissed would still be true
Mady Mellorca184aae2019-09-17 16:07:12 -0700555 boolean userRemovedNotif = (entry != null && entry.isRowDismissed() && !isAppCancel)
Mady Mellor22f2f072019-04-18 13:26:18 -0700556 || isClearAll || isUserDimiss || isSummaryCancel;
557
558 if (isSummaryOfBubbles) {
559 return handleSummaryRemovalInterception(entry, userRemovedNotif);
560 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700561
562 // The bubble notification sticks around in the data as long as the bubble is
563 // not dismissed and the app hasn't cancelled the notification.
Mady Mellor22f2f072019-04-18 13:26:18 -0700564 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellorca184aae2019-09-17 16:07:12 -0700565 boolean bubbleExtended = entry != null && entry.isBubble() && userRemovedNotif;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700566 if (bubbleExtended) {
Mady Mellorce23c462019-06-17 17:30:07 -0700567 bubble.setShowInShadeWhenBubble(false);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700568 bubble.setShowBubbleDot(false);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700569 if (mStackView != null) {
570 mStackView.updateDotVisibility(entry.key);
571 }
Beverly85d4c192019-09-30 11:40:39 -0400572 mNotificationEntryManager.updateNotifications(
573 "BubbleController.onNotificationRemoveRequested");
Mady Mellorc2ff0112019-03-28 14:18:06 -0700574 return true;
Mady Mellorca184aae2019-09-17 16:07:12 -0700575 } else if (!userRemovedNotif && entry != null) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700576 // This wasn't a user removal so we should remove the bubble as well
577 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
578 return false;
579 }
580 return false;
581 }
582 };
583
Mady Mellor22f2f072019-04-18 13:26:18 -0700584 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
585 boolean userRemovedNotif) {
586 String groupKey = summary.notification.getGroupKey();
587 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
588
589 if (userRemovedNotif) {
590 // If it's a user dismiss we mark the children to be hidden from the shade.
591 for (int i = 0; i < bubbleChildren.size(); i++) {
592 Bubble bubbleChild = bubbleChildren.get(i);
593 // As far as group manager is concerned, once a child is no longer shown
594 // in the shade, it is essentially removed.
595 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
596 bubbleChild.setShowInShadeWhenBubble(false);
597 bubbleChild.setShowBubbleDot(false);
598 if (mStackView != null) {
599 mStackView.updateDotVisibility(bubbleChild.getKey());
600 }
601 }
602 // And since all children are removed, remove the summary.
603 mNotificationGroupManager.onEntryRemoved(summary);
604
605 // If the summary was auto-generated we don't need to keep that notification around
606 // because apps can't cancel it; so we only intercept & suppress real summaries.
607 boolean isAutogroupSummary = (summary.notification.getNotification().flags
608 & FLAG_AUTOGROUP_SUMMARY) != 0;
Mady Mellore28fe102019-07-09 15:33:32 -0700609 if (!isAutogroupSummary) {
610 mBubbleData.addSummaryToSuppress(summary.notification.getGroupKey(),
611 summary.key);
612 // Tell shade to update for the suppression
Beverly85d4c192019-09-30 11:40:39 -0400613 mNotificationEntryManager.updateNotifications(
614 "BubbleController.handleSummaryRemovalInterception");
Mady Mellore28fe102019-07-09 15:33:32 -0700615 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700616 return !isAutogroupSummary;
617 } else {
Mady Mellore28fe102019-07-09 15:33:32 -0700618 // If it's not a user dismiss it's a cancel.
619 mBubbleData.removeSuppressedSummary(groupKey);
620
Mady Mellor22f2f072019-04-18 13:26:18 -0700621 // Remove any associated bubble children.
622 for (int i = 0; i < bubbleChildren.size(); i++) {
623 Bubble bubbleChild = bubbleChildren.get(i);
624 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
625 DISMISS_GROUP_CANCELLED);
626 }
627 return false;
628 }
629 }
630
Mady Mellorc2ff0112019-03-28 14:18:06 -0700631 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500632 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
633 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500634 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorca0c24c2019-05-16 16:14:32 -0700635 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
636 && canLaunchInActivityView(mContext, entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400637 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800638 }
639 }
640
641 @Override
642 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorca0c24c2019-05-16 16:14:32 -0700643 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
644 && canLaunchInActivityView(mContext, entry);
Mady Melloraa8fef22019-04-11 13:36:40 -0700645 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.key)) {
646 // It was previously a bubble but no longer a bubble -- lets remove it
647 removeBubble(entry.key, DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700648 } else if (shouldBubble) {
Mady Mellored99c272019-06-13 15:58:30 -0700649 Bubble b = mBubbleData.getBubbleWithKey(entry.key);
Mark Renouff97ed462019-04-05 13:46:24 -0400650 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800651 }
652 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400653
654 @Override
655 public void onNotificationRankingUpdated(RankingMap rankingMap) {
656 // Forward to BubbleData to block any bubbles which should no longer be shown
657 mBubbleData.notificationRankingUpdated(rankingMap);
658 }
Ned Burns01e38212019-01-03 16:32:52 -0500659 };
660
Mark Renouf71a3af62019-04-08 15:02:54 -0400661 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400662 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400663
Mark Renouf3bc5b362019-04-05 14:37:59 -0400664 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400665 public void applyUpdate(BubbleData.Update update) {
666 if (mStackView == null && update.addedBubble != null) {
667 // Lazy init stack view when the first bubble is added.
668 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400669 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400670
671 // If not yet initialized, ignore all other changes.
672 if (mStackView == null) {
673 return;
674 }
675
676 if (update.addedBubble != null) {
677 mStackView.addBubble(update.addedBubble);
678 }
679
680 // Collapsing? Do this first before remaining steps.
681 if (update.expandedChanged && !update.expanded) {
682 mStackView.setExpanded(false);
683 }
684
685 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700686 ArrayList<Pair<Bubble, Integer>> removedBubbles =
687 new ArrayList<>(update.removedBubbles);
688 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400689 final Bubble bubble = removed.first;
690 @DismissReason final int reason = removed.second;
691 mStackView.removeBubble(bubble);
692
Mark Renoufc19b4732019-06-26 12:08:33 -0400693 // If the bubble is removed for user switching, leave the notification in place.
694 if (reason != DISMISS_USER_CHANGED) {
695 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
696 && !bubble.showInShadeWhenBubble()) {
697 // The bubble is gone & the notification is gone, time to actually remove it
698 mNotificationEntryManager.performRemoveNotification(
699 bubble.getEntry().notification, UNDEFINED_DISMISS_REASON);
700 } else {
701 // Update the flag for SysUI
702 bubble.getEntry().notification.getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700703
Mark Renoufc19b4732019-06-26 12:08:33 -0400704 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
705 // will get right result back
706 try {
707 mBarService.onNotificationBubbleChanged(bubble.getKey(),
708 false /* isBubble */);
709 } catch (RemoteException e) {
710 // Bad things have happened
711 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400712 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700713
Mady Mellore28fe102019-07-09 15:33:32 -0700714 // Check if removed bubble has an associated suppressed group summary that needs
715 // to be removed now.
716 final String groupKey = bubble.getEntry().notification.getGroupKey();
717 if (mBubbleData.isSummarySuppressed(groupKey)
718 && mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
719 // Time to actually remove the summary.
720 String notifKey = mBubbleData.getSummaryKey(groupKey);
721 mBubbleData.removeSuppressedSummary(groupKey);
722 NotificationEntry entry =
723 mNotificationEntryManager.getNotificationData().get(notifKey);
Mady Mellore28fe102019-07-09 15:33:32 -0700724 mNotificationEntryManager.performRemoveNotification(
725 entry.notification, UNDEFINED_DISMISS_REASON);
726 }
727
Mady Mellor22f2f072019-04-18 13:26:18 -0700728 // Check if summary should be removed from NoManGroup
729 NotificationEntry summary = mNotificationGroupManager.getLogicalGroupSummary(
730 bubble.getEntry().notification);
731 if (summary != null) {
732 ArrayList<NotificationEntry> summaryChildren =
733 mNotificationGroupManager.getLogicalChildren(summary.notification);
Mady Mellore4348272019-07-29 17:43:36 -0700734 boolean isSummaryThisNotif = summary.key.equals(bubble.getEntry().key);
735 if (!isSummaryThisNotif
736 && (summaryChildren == null || summaryChildren.isEmpty())) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700737 mNotificationEntryManager.performRemoveNotification(
738 summary.notification, UNDEFINED_DISMISS_REASON);
739 }
740 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700741 }
742 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400743
Mark Renouf82a40e62019-05-23 16:16:24 -0400744 if (update.updatedBubble != null) {
745 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400746 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400747
Mark Renouf82a40e62019-05-23 16:16:24 -0400748 if (update.orderChanged) {
749 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400750 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400751
Mark Renouf82a40e62019-05-23 16:16:24 -0400752 if (update.selectionChanged) {
753 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700754 if (update.selectedBubble != null) {
755 mNotificationGroupManager.updateSuppression(
756 update.selectedBubble.getEntry());
757 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400758 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400759
Mark Renouf82a40e62019-05-23 16:16:24 -0400760 // Expanding? Apply this last.
761 if (update.expandedChanged && update.expanded) {
762 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400763 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400764
Beverly85d4c192019-09-30 11:40:39 -0400765 mNotificationEntryManager.updateNotifications(
766 "BubbleData.Listener.applyUpdate");
Lyn Han6c40fe72019-05-08 14:06:33 -0700767 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400768
Issei Suzukia8d07312019-06-07 12:56:19 +0200769 if (DEBUG_BUBBLE_CONTROLLER) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400770 Log.d(TAG, "[BubbleData]");
771 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
772 mBubbleData.getSelectedBubble()));
773
774 if (mStackView != null) {
775 Log.d(TAG, "[BubbleStackView]");
776 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
777 mStackView.getExpandedBubble()));
778 }
779 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400780 }
781 };
782
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800783 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400784 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700785 * Updates the visibility of the bubbles based on current state.
786 * Does not un-bubble, just hides or un-hides. Notifies any
787 * {@link BubbleStateChangeListener}s of visibility changes.
788 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800789 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700790 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800791 if (mStackView == null) {
792 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800793 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800794 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
795 // Bubbles only appear in unlocked shade
796 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +0000797 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800798 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800799 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700800
Mady Mellor698d9e82019-08-01 23:11:53 +0000801 // Let listeners know if bubble state changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700802 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +0000803 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellor88552b82019-08-05 22:38:59 +0000804 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -0700805 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
806 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
807 }
808
809 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800810 }
811
812 /**
813 * Rect indicating the touchable region for the bubble stack / expanded stack.
814 */
815 public Rect getTouchableRegion() {
816 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
817 return null;
818 }
819 mStackView.getBoundsOnScreen(mTempRect);
820 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800821 }
822
Mady Mellor390bff42019-04-05 15:09:01 -0700823 /**
824 * The display id of the expanded view, if the stack is expanded and not occluded by the
825 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
826 */
827 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200828 final Bubble bubble = getExpandedBubble(context);
829 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
830 }
831
832 @Nullable
833 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700834 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200835 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700836 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200837 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700838 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200839 final Bubble expandedBubble = mStackView.getExpandedBubble();
840 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700841 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200842 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700843 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200844 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700845 }
846
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800847 @VisibleForTesting
848 BubbleStackView getStackView() {
849 return mStackView;
850 }
851
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700852 /**
853 * Description of current bubble state.
854 */
855 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
856 pw.println("BubbleController state:");
857 mBubbleData.dump(fd, pw, args);
858 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -0400859 if (mStackView != null) {
860 mStackView.dump(fd, pw, args);
861 }
862 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700863 }
864
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400865 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
866 StringBuilder sb = new StringBuilder();
867 for (Bubble bubble : bubbles) {
868 if (bubble == null) {
869 sb.append(" <null> !!!!!\n");
870 } else {
871 boolean isSelected = (bubble == selected);
872 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
873 ((isSelected) ? "->" : " "),
874 bubble.getLastActivity(),
875 (bubble.isOngoing() ? 1 : 0),
876 bubble.getKey()));
877 }
878 }
879 return sb.toString();
880 }
881
Mady Mellore80930e2019-03-21 16:00:45 -0700882 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -0500883 * This task stack listener is responsible for responding to tasks moved to the front
884 * which are on the default (main) display. When this happens, expanded bubbles must be
885 * collapsed so the user may interact with the app which was just moved to the front.
886 * <p>
887 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
888 * these calls via a main thread Handler.
889 */
890 @MainThread
891 private class BubbleTaskStackListener extends TaskStackChangeListener {
892
Mark Renoufcecc77b2019-01-30 16:32:24 -0500893 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500894 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
895 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -0700896 if (!mStackView.isExpansionAnimating()) {
897 mBubbleData.setExpanded(false);
898 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500899 }
900 }
901
Mark Renoufcecc77b2019-01-30 16:32:24 -0500902 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500903 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500904 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400905 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500906 }
907 }
Mark Renouf446251d2019-04-26 10:22:41 -0400908
909 @Override
910 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
911 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
912 mBubbleData.setExpanded(false);
913 }
914 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200915
916 @Override
917 public void onSingleTaskDisplayDrawn(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -0700918 final Bubble expandedBubble = mStackView != null
919 ? mStackView.getExpandedBubble()
920 : null;
Issei Suzukicac2a502019-04-16 16:52:50 +0200921 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
922 expandedBubble.setContentVisibility(true);
923 }
924 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200925
926 @Override
927 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -0700928 final Bubble expandedBubble = mStackView != null
929 ? mStackView.getExpandedBubble()
930 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -0700931 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
932 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +0200933 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +0200934 }
Mady Mellorca184aae2019-09-17 16:07:12 -0700935 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +0200936 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500937 }
938
Mady Mellorca0c24c2019-05-16 16:14:32 -0700939 /**
940 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
941 *
942 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
943 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
944 *
945 * @param context the context to use.
946 * @param entry the entry to bubble.
947 */
948 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
949 PendingIntent intent = entry.getBubbleMetadata() != null
950 ? entry.getBubbleMetadata().getIntent()
951 : null;
952 if (intent == null) {
953 Log.w(TAG, "Unable to create bubble -- no intent");
954 return false;
955 }
956 ActivityInfo info =
957 intent.getIntent().resolveActivityInfo(context.getPackageManager(), 0);
958 if (info == null) {
959 Log.w(TAG, "Unable to send as bubble -- couldn't find activity info for intent: "
960 + intent);
961 return false;
962 }
963 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
964 Log.w(TAG, "Unable to send as bubble -- activity is not resizable for intent: "
965 + intent);
966 return false;
967 }
968 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
969 Log.w(TAG, "Unable to send as bubble -- activity is not documentLaunchMode=always "
970 + "for intent: " + intent);
971 return false;
972 }
973 if ((info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
974 Log.w(TAG, "Unable to send as bubble -- activity is not embeddable for intent: "
975 + intent);
976 return false;
977 }
978 return true;
979 }
980
Joshua Tsujia19515f2019-02-13 18:02:29 -0500981 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +0000982 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -0500983 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500984 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
985 if (mStackView != null && mStackView.getBubbleCount() > 0) {
986 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500987 }
988 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500989 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800990}