blob: 43576a4e21effcaeb0ea478f2d3d3b096d839455 [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;
Mady Mellorceced172018-11-27 11:18:39 -080055import android.provider.Settings;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040056import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040057import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040058import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040059import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040060import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040061import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050062import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080063import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080064import android.widget.FrameLayout;
65
Mark Renouf08bc42a2019-03-07 13:01:59 -050066import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050067import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040068import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050069
Mady Mellorebdbbb92018-11-15 14:36:48 -080070import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070071import com.android.internal.statusbar.IStatusBarService;
Ned Burns01e38212019-01-03 16:32:52 -050072import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080073import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050074import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050075import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wange3ff1392019-08-06 14:24:43 -070076import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050077import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050078import com.android.systemui.shared.system.WindowManagerWrapper;
Mark Renoufc19b4732019-06-26 12:08:33 -040079import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070080import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050081import com.android.systemui.statusbar.notification.NotificationEntryListener;
82import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080083import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -040084import com.android.systemui.statusbar.notification.collection.NotificationData;
Ned Burnsf81c4c42019-01-07 14:10:43 -050085import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellor22f2f072019-04-18 13:26:18 -070086import com.android.systemui.statusbar.phone.NotificationGroupManager;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080087import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070088import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040089import com.android.systemui.statusbar.policy.ZenModeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080090
Mady Mellor70cba7bb2019-07-02 15:06:07 -070091import java.io.FileDescriptor;
92import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -050093import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -040094import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -070095import java.util.ArrayList;
Mady Mellore80930e2019-03-21 16:00:45 -070096import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050097
Jason Monk27d01a622018-12-10 15:57:09 -050098import javax.inject.Inject;
99import javax.inject.Singleton;
100
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800101/**
102 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
103 * Bubbles can be expanded to show more content.
104 *
105 * The controller manages addition, removal, and visible state of bubbles on screen.
106 */
Jason Monk27d01a622018-12-10 15:57:09 -0500107@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -0400108public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800109
Issei Suzukia8d07312019-06-07 12:56:19 +0200110 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800111
Mark Renouf08bc42a2019-03-07 13:01:59 -0500112 @Retention(SOURCE)
113 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400114 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700115 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400116 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500117 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700118
Mark Renouf08bc42a2019-03-07 13:01:59 -0500119 static final int DISMISS_USER_GESTURE = 1;
120 static final int DISMISS_AGED = 2;
121 static final int DISMISS_TASK_FINISHED = 3;
122 static final int DISMISS_BLOCKED = 4;
123 static final int DISMISS_NOTIF_CANCEL = 5;
124 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700125 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400126 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700127 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700128 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500129
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400130 public static final int MAX_BUBBLES = 5; // TODO: actually enforce this
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -0400131
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800132 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800133 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500134
Ned Burns01e38212019-01-03 16:32:52 -0500135 private final Context mContext;
136 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500137 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800138 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800139 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100140 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700141 private final NotificationGroupManager mNotificationGroupManager;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800142
Mady Mellor3dff9e62019-02-05 18:12:53 -0800143 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400144 @Nullable private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800145
Mark Renoufc19b4732019-06-26 12:08:33 -0400146 // Tracks the id of the current (foreground) user.
147 private int mCurrentUserId;
148 // Saves notification keys of active bubbles when users are switched.
149 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
150
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800151 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500152 private final StatusBarWindowController mStatusBarWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400153 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800154 private StatusBarStateListener mStatusBarStateListener;
155
Mady Melloraa8fef22019-04-11 13:36:40 -0700156 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700157 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800158
Mady Mellord1c78b262018-11-06 18:04:40 -0800159 // Used for determining view rect for touch interaction
160 private Rect mTempRect = new Rect();
161
Mark Renoufc19b4732019-06-26 12:08:33 -0400162 // Listens to user switch so bubbles can be saved and restored.
163 private final NotificationLockscreenUserManager mNotifUserManager;
164
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400165 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
166 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
167
Mady Mellor5549dd22018-11-06 18:07:34 -0800168 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800169 * Listener to be notified when some states of the bubbles change.
170 */
171 public interface BubbleStateChangeListener {
172 /**
173 * Called when the stack has bubbles or no longer has bubbles.
174 */
175 void onHasBubblesChanged(boolean hasBubbles);
176 }
177
Mady Mellorcd9b1302018-11-06 18:08:04 -0800178 /**
179 * Listener to find out about stack expansion / collapse events.
180 */
181 public interface BubbleExpandListener {
182 /**
183 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700184 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800185 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800186 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800187 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800188 void onBubbleExpandChanged(boolean isExpanding, String key);
189 }
190
191 /**
192 * Listens for the current state of the status bar and updates the visibility state
193 * of bubbles as needed.
194 */
195 private class StatusBarStateListener implements StatusBarStateController.StateListener {
196 private int mState;
197 /**
198 * Returns the current status bar state.
199 */
200 public int getCurrentState() {
201 return mState;
202 }
203
204 @Override
205 public void onStateChanged(int newState) {
206 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400207 boolean shouldCollapse = (mState != SHADE);
208 if (shouldCollapse) {
209 collapseStack();
210 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700211 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800212 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800213 }
214
Jason Monk27d01a622018-12-10 15:57:09 -0500215 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800216 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Mady Melloraa8fef22019-04-11 13:36:40 -0700217 BubbleData data, ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400218 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400219 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700220 NotificationLockscreenUserManager notifUserManager,
221 NotificationGroupManager groupManager) {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700222 this(context, statusBarWindowController, data, null /* synchronizer */,
Mark Renoufc19b4732019-06-26 12:08:33 -0400223 configurationController, interruptionStateProvider, zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700224 notifUserManager, groupManager);
Issei Suzukic0387542019-03-08 17:31:14 +0100225 }
226
227 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700228 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
Mady Melloraa8fef22019-04-11 13:36:40 -0700229 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400230 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400231 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700232 NotificationLockscreenUserManager notifUserManager,
233 NotificationGroupManager groupManager) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800234 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700235 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400236 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400237 mZenModeController = zenModeController;
238 mZenModeController.addCallback(new ZenModeController.Callback() {
239 @Override
240 public void onZenChanged(int zen) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700241 if (mStackView != null) {
242 mStackView.updateDots();
243 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400244 }
245
246 @Override
247 public void onConfigChanged(ZenModeConfig config) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700248 if (mStackView != null) {
249 mStackView.updateDots();
250 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400251 }
252 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700253
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700254 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800255
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400256 mBubbleData = data;
257 mBubbleData.setListener(mBubbleDataListener);
258
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800259 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500260 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700261 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellor22f2f072019-04-18 13:26:18 -0700262 mNotificationGroupManager = groupManager;
Mady Mellor740d85d2019-07-09 15:26:47 -0700263 mNotificationGroupManager.addOnGroupChangeListener(
264 new NotificationGroupManager.OnGroupChangeListener() {
265 @Override
266 public void onGroupSuppressionChanged(
267 NotificationGroupManager.NotificationGroup group,
268 boolean suppressed) {
269 // More notifications could be added causing summary to no longer
270 // be suppressed -- in this case need to remove the key.
271 final String groupKey = group.summary != null
272 ? group.summary.notification.getGroupKey()
273 : null;
274 if (!suppressed && groupKey != null
275 && mBubbleData.isSummarySuppressed(groupKey)) {
276 mBubbleData.removeSuppressedSummary(groupKey);
277 }
278 }
279 });
Mady Mellorb4991e62019-01-10 15:14:51 -0800280
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800281 mStatusBarWindowController = statusBarWindowController;
282 mStatusBarStateListener = new StatusBarStateListener();
283 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500284
Mark Renoufcecc77b2019-01-30 16:32:24 -0500285 mTaskStackListener = new BubbleTaskStackListener();
286 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800287
Joshua Tsujia19515f2019-02-13 18:02:29 -0500288 try {
289 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
290 } catch (RemoteException e) {
291 e.printStackTrace();
292 }
Issei Suzukic0387542019-03-08 17:31:14 +0100293 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700294
295 mBarService = IStatusBarService.Stub.asInterface(
296 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400297
298 mSavedBubbleKeysPerUser = new SparseSetArray<>();
299 mCurrentUserId = mNotifUserManager.getCurrentUserId();
300 mNotifUserManager.addUserChangedListener(
301 newUserId -> {
302 saveBubbles(mCurrentUserId);
303 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
304 restoreBubbles(newUserId);
305 mCurrentUserId = newUserId;
306 });
Mady Mellor5549dd22018-11-06 18:07:34 -0800307 }
308
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400309 /**
310 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
311 * method initializes the stack view and adds it to the StatusBar just above the scrim.
312 */
313 private void ensureStackViewCreated() {
314 if (mStackView == null) {
315 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
316 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Lyn Hanbde48202019-05-29 19:18:29 -0700317 int bubbleScrimIndex = sbv.indexOfChild(sbv.findViewById(R.id.scrim_for_bubble));
318 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
319 sbv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400320 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
321 if (mExpandListener != null) {
322 mStackView.setExpandListener(mExpandListener);
323 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400324 }
325 }
326
Mark Renoufc19b4732019-06-26 12:08:33 -0400327 /**
328 * Records the notification key for any active bubbles. These are used to restore active
329 * bubbles when the user returns to the foreground.
330 *
331 * @param userId the id of the user
332 */
333 private void saveBubbles(@UserIdInt int userId) {
334 // First clear any existing keys that might be stored.
335 mSavedBubbleKeysPerUser.remove(userId);
336 // Add in all active bubbles for the current user.
337 for (Bubble bubble: mBubbleData.getBubbles()) {
338 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
339 }
340 }
341
342 /**
343 * Promotes existing notifications to Bubbles if they were previously bubbles.
344 *
345 * @param userId the id of the user
346 */
347 private void restoreBubbles(@UserIdInt int userId) {
348 NotificationData notificationData =
349 mNotificationEntryManager.getNotificationData();
350 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
351 if (savedBubbleKeys == null) {
352 // There were no bubbles saved for this used.
353 return;
354 }
355 for (NotificationEntry e : notificationData.getNotificationsForCurrentUser()) {
356 if (savedBubbleKeys.contains(e.key)
357 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
358 && canLaunchInActivityView(mContext, e)) {
359 updateBubble(e, /* suppressFlyout= */ true);
360 }
361 }
362 // Finally, remove the entries for this user now that bubbles are restored.
363 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
364 }
365
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700366 @Override
367 public void onUiModeChanged() {
368 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700369 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700370 }
371 }
372
373 @Override
374 public void onOverlayChanged() {
375 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700376 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700377 }
378 }
379
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400380 @Override
381 public void onConfigChanged(Configuration newConfig) {
382 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400383 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700384 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400385 }
386 }
387
Mady Mellor5549dd22018-11-06 18:07:34 -0800388 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800389 * Set a listener to be notified when some states of the bubbles change.
390 */
391 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
392 mStateChangeListener = listener;
393 }
394
395 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800396 * Set a listener to be notified of bubble expand events.
397 */
398 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100399 mExpandListener = ((isExpanding, key) -> {
400 if (listener != null) {
401 listener.onBubbleExpandChanged(isExpanding, key);
402 }
403 mStatusBarWindowController.setBubbleExpanded(isExpanding);
404 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800405 if (mStackView != null) {
406 mStackView.setExpandListener(mExpandListener);
407 }
408 }
409
410 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800411 * Whether or not there are bubbles present, regardless of them being visible on the
412 * screen (e.g. if on AOD).
413 */
414 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800415 if (mStackView == null) {
416 return false;
417 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400418 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800419 }
420
421 /**
422 * Whether the stack of bubbles is expanded or not.
423 */
424 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400425 return mBubbleData.isExpanded();
426 }
427
428 /**
429 * Tell the stack of bubbles to expand.
430 */
431 public void expandStack() {
432 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800433 }
434
435 /**
436 * Tell the stack of bubbles to collapse.
437 */
438 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400439 mBubbleData.setExpanded(false /* expanded */);
440 }
441
Mady Mellorce23c462019-06-17 17:30:07 -0700442 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700443 * True if either:
444 * (1) There is a bubble associated with the provided key and if its notification is hidden
445 * from the shade.
446 * (2) There is a group summary associated with the provided key that is hidden from the shade
447 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700448 *
Mady Mellore28fe102019-07-09 15:33:32 -0700449 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700450 */
451 public boolean isBubbleNotificationSuppressedFromShade(String key) {
Mady Mellore28fe102019-07-09 15:33:32 -0700452 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorce23c462019-06-17 17:30:07 -0700453 && !mBubbleData.getBubbleWithKey(key).showInShadeWhenBubble();
Mady Mellore28fe102019-07-09 15:33:32 -0700454 NotificationEntry entry = mNotificationEntryManager.getNotificationData().get(key);
455 String groupKey = entry != null ? entry.notification.getGroupKey() : null;
456 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700457 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
458 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700459 }
460
Mark Renouf71a3af62019-04-08 15:02:54 -0400461 void selectBubble(Bubble bubble) {
462 mBubbleData.setSelectedBubble(bubble);
463 }
464
465 @VisibleForTesting
466 void selectBubble(String key) {
467 Bubble bubble = mBubbleData.getBubbleWithKey(key);
468 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800469 }
470
471 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400472 * Request the stack expand if needed, then select the specified Bubble as current.
473 *
474 * @param notificationKey the notification key for the bubble to be selected
475 */
476 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400477 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
478 if (bubble != null) {
479 mBubbleData.setSelectedBubble(bubble);
480 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400481 }
482 }
483
484 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800485 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
486 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500487 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400488 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800489 }
490
491 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500492 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
493 * is forwarded a back key down/up pair.
494 */
495 public void performBackPressIfNeeded() {
496 if (mStackView != null) {
497 mStackView.performBackPressIfNeeded();
498 }
499 }
500
501 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800502 * Adds or updates a bubble associated with the provided notification entry.
503 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400504 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800505 */
Mark Renouff97ed462019-04-05 13:46:24 -0400506 void updateBubble(NotificationEntry notif) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400507 updateBubble(notif, /* supressFlyout */ false);
508 }
509
510 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700511 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400512 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700513 notif.setInterruption();
514 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400515 mBubbleData.notificationEntryUpdated(notif, suppressFlyout);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800516 }
517
518 /**
519 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500520 * <p>
521 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800522 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500523 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500524 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400525 // TEMP: refactor to change this to pass entry
526 Bubble bubble = mBubbleData.getBubbleWithKey(key);
527 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700528 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800529 }
530 }
531
Ned Burns01e38212019-01-03 16:32:52 -0500532 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700533 private final NotificationRemoveInterceptor mRemoveInterceptor =
534 new NotificationRemoveInterceptor() {
535 @Override
536 public boolean onNotificationRemoveRequested(String key, int reason) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700537 NotificationEntry entry = mNotificationEntryManager.getNotificationData().get(key);
538 String groupKey = entry != null ? entry.notification.getGroupKey() : null;
539 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
540
541 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
Mady Mellore28fe102019-07-09 15:33:32 -0700542 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
543 && mBubbleData.getSummaryKey(groupKey).equals(key));
Mady Mellor22f2f072019-04-18 13:26:18 -0700544 boolean isSummary = entry != null
545 && entry.notification.getNotification().isGroupSummary();
Mady Mellore28fe102019-07-09 15:33:32 -0700546 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
547 && bubbleChildren != null && !bubbleChildren.isEmpty();
Mady Mellor22f2f072019-04-18 13:26:18 -0700548
549 if (!inBubbleData && !isSummaryOfBubbles) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700550 return false;
551 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700552
553 final boolean isClearAll = reason == REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700554 final boolean isUserDimiss = reason == REASON_CANCEL || reason == REASON_CLICK;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700555 final boolean isAppCancel = reason == REASON_APP_CANCEL
556 || reason == REASON_APP_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700557 final boolean isSummaryCancel = reason == REASON_GROUP_SUMMARY_CANCELED;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700558
559 // Need to check for !appCancel here because the notification may have
560 // previously been dismissed & entry.isRowDismissed would still be true
561 boolean userRemovedNotif = (entry.isRowDismissed() && !isAppCancel)
Mady Mellor22f2f072019-04-18 13:26:18 -0700562 || isClearAll || isUserDimiss || isSummaryCancel;
563
564 if (isSummaryOfBubbles) {
565 return handleSummaryRemovalInterception(entry, userRemovedNotif);
566 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700567
568 // The bubble notification sticks around in the data as long as the bubble is
569 // not dismissed and the app hasn't cancelled the notification.
Mady Mellor22f2f072019-04-18 13:26:18 -0700570 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mark Renoufc19b4732019-06-26 12:08:33 -0400571 boolean bubbleExtended = entry.isBubble() && userRemovedNotif;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700572 if (bubbleExtended) {
Mady Mellorce23c462019-06-17 17:30:07 -0700573 bubble.setShowInShadeWhenBubble(false);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700574 bubble.setShowBubbleDot(false);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700575 if (mStackView != null) {
576 mStackView.updateDotVisibility(entry.key);
577 }
578 mNotificationEntryManager.updateNotifications();
579 return true;
Mark Renoufc19b4732019-06-26 12:08:33 -0400580 } else if (!userRemovedNotif) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700581 // This wasn't a user removal so we should remove the bubble as well
582 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
583 return false;
584 }
585 return false;
586 }
587 };
588
Mady Mellor22f2f072019-04-18 13:26:18 -0700589 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
590 boolean userRemovedNotif) {
591 String groupKey = summary.notification.getGroupKey();
592 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
593
594 if (userRemovedNotif) {
595 // If it's a user dismiss we mark the children to be hidden from the shade.
596 for (int i = 0; i < bubbleChildren.size(); i++) {
597 Bubble bubbleChild = bubbleChildren.get(i);
598 // As far as group manager is concerned, once a child is no longer shown
599 // in the shade, it is essentially removed.
600 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
601 bubbleChild.setShowInShadeWhenBubble(false);
602 bubbleChild.setShowBubbleDot(false);
603 if (mStackView != null) {
604 mStackView.updateDotVisibility(bubbleChild.getKey());
605 }
606 }
607 // And since all children are removed, remove the summary.
608 mNotificationGroupManager.onEntryRemoved(summary);
609
610 // If the summary was auto-generated we don't need to keep that notification around
611 // because apps can't cancel it; so we only intercept & suppress real summaries.
612 boolean isAutogroupSummary = (summary.notification.getNotification().flags
613 & FLAG_AUTOGROUP_SUMMARY) != 0;
Mady Mellore28fe102019-07-09 15:33:32 -0700614 if (!isAutogroupSummary) {
615 mBubbleData.addSummaryToSuppress(summary.notification.getGroupKey(),
616 summary.key);
617 // Tell shade to update for the suppression
618 mNotificationEntryManager.updateNotifications();
619 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700620 return !isAutogroupSummary;
621 } else {
Mady Mellore28fe102019-07-09 15:33:32 -0700622 // If it's not a user dismiss it's a cancel.
623 mBubbleData.removeSuppressedSummary(groupKey);
624
Mady Mellor22f2f072019-04-18 13:26:18 -0700625 // Remove any associated bubble children.
626 for (int i = 0; i < bubbleChildren.size(); i++) {
627 Bubble bubbleChild = bubbleChildren.get(i);
628 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
629 DISMISS_GROUP_CANCELLED);
630 }
631 return false;
632 }
633 }
634
Mady Mellorc2ff0112019-03-28 14:18:06 -0700635 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500636 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
637 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500638 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800639 if (!areBubblesEnabled(mContext)) {
640 return;
641 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700642 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
643 && canLaunchInActivityView(mContext, entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400644 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800645 }
646 }
647
648 @Override
649 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800650 if (!areBubblesEnabled(mContext)) {
651 return;
652 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700653 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
654 && canLaunchInActivityView(mContext, entry);
Mady Melloraa8fef22019-04-11 13:36:40 -0700655 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.key)) {
656 // It was previously a bubble but no longer a bubble -- lets remove it
657 removeBubble(entry.key, DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700658 } else if (shouldBubble) {
Mady Mellored99c272019-06-13 15:58:30 -0700659 Bubble b = mBubbleData.getBubbleWithKey(entry.key);
Mark Renouff97ed462019-04-05 13:46:24 -0400660 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800661 }
662 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400663
664 @Override
665 public void onNotificationRankingUpdated(RankingMap rankingMap) {
666 // Forward to BubbleData to block any bubbles which should no longer be shown
667 mBubbleData.notificationRankingUpdated(rankingMap);
668 }
Ned Burns01e38212019-01-03 16:32:52 -0500669 };
670
Mark Renouf71a3af62019-04-08 15:02:54 -0400671 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400672 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400673
Mark Renouf3bc5b362019-04-05 14:37:59 -0400674 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400675 public void applyUpdate(BubbleData.Update update) {
676 if (mStackView == null && update.addedBubble != null) {
677 // Lazy init stack view when the first bubble is added.
678 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400679 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400680
681 // If not yet initialized, ignore all other changes.
682 if (mStackView == null) {
683 return;
684 }
685
686 if (update.addedBubble != null) {
687 mStackView.addBubble(update.addedBubble);
688 }
689
690 // Collapsing? Do this first before remaining steps.
691 if (update.expandedChanged && !update.expanded) {
692 mStackView.setExpanded(false);
693 }
694
695 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700696 ArrayList<Pair<Bubble, Integer>> removedBubbles =
697 new ArrayList<>(update.removedBubbles);
698 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400699 final Bubble bubble = removed.first;
700 @DismissReason final int reason = removed.second;
701 mStackView.removeBubble(bubble);
702
Mark Renoufc19b4732019-06-26 12:08:33 -0400703 // If the bubble is removed for user switching, leave the notification in place.
704 if (reason != DISMISS_USER_CHANGED) {
705 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
706 && !bubble.showInShadeWhenBubble()) {
707 // The bubble is gone & the notification is gone, time to actually remove it
708 mNotificationEntryManager.performRemoveNotification(
709 bubble.getEntry().notification, UNDEFINED_DISMISS_REASON);
710 } else {
711 // Update the flag for SysUI
712 bubble.getEntry().notification.getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700713
Mark Renoufc19b4732019-06-26 12:08:33 -0400714 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
715 // will get right result back
716 try {
717 mBarService.onNotificationBubbleChanged(bubble.getKey(),
718 false /* isBubble */);
719 } catch (RemoteException e) {
720 // Bad things have happened
721 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400722 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700723
Mady Mellore28fe102019-07-09 15:33:32 -0700724 // Check if removed bubble has an associated suppressed group summary that needs
725 // to be removed now.
726 final String groupKey = bubble.getEntry().notification.getGroupKey();
727 if (mBubbleData.isSummarySuppressed(groupKey)
728 && mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
729 // Time to actually remove the summary.
730 String notifKey = mBubbleData.getSummaryKey(groupKey);
731 mBubbleData.removeSuppressedSummary(groupKey);
732 NotificationEntry entry =
733 mNotificationEntryManager.getNotificationData().get(notifKey);
Mady Mellore28fe102019-07-09 15:33:32 -0700734 mNotificationEntryManager.performRemoveNotification(
735 entry.notification, UNDEFINED_DISMISS_REASON);
736 }
737
Mady Mellor22f2f072019-04-18 13:26:18 -0700738 // Check if summary should be removed from NoManGroup
739 NotificationEntry summary = mNotificationGroupManager.getLogicalGroupSummary(
740 bubble.getEntry().notification);
741 if (summary != null) {
742 ArrayList<NotificationEntry> summaryChildren =
743 mNotificationGroupManager.getLogicalChildren(summary.notification);
Mady Mellore4348272019-07-29 17:43:36 -0700744 boolean isSummaryThisNotif = summary.key.equals(bubble.getEntry().key);
745 if (!isSummaryThisNotif
746 && (summaryChildren == null || summaryChildren.isEmpty())) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700747 mNotificationEntryManager.performRemoveNotification(
748 summary.notification, UNDEFINED_DISMISS_REASON);
749 }
750 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700751 }
752 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400753
Mark Renouf82a40e62019-05-23 16:16:24 -0400754 if (update.updatedBubble != null) {
755 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400756 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400757
Mark Renouf82a40e62019-05-23 16:16:24 -0400758 if (update.orderChanged) {
759 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400760 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400761
Mark Renouf82a40e62019-05-23 16:16:24 -0400762 if (update.selectionChanged) {
763 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700764 if (update.selectedBubble != null) {
765 mNotificationGroupManager.updateSuppression(
766 update.selectedBubble.getEntry());
767 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400768 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400769
Mark Renouf82a40e62019-05-23 16:16:24 -0400770 // Expanding? Apply this last.
771 if (update.expandedChanged && update.expanded) {
772 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400773 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400774
Mark Renouf71a3af62019-04-08 15:02:54 -0400775 mNotificationEntryManager.updateNotifications();
Lyn Han6c40fe72019-05-08 14:06:33 -0700776 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400777
Issei Suzukia8d07312019-06-07 12:56:19 +0200778 if (DEBUG_BUBBLE_CONTROLLER) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400779 Log.d(TAG, "[BubbleData]");
780 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
781 mBubbleData.getSelectedBubble()));
782
783 if (mStackView != null) {
784 Log.d(TAG, "[BubbleStackView]");
785 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
786 mStackView.getExpandedBubble()));
787 }
788 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400789 }
790 };
791
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800792 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400793 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700794 * Updates the visibility of the bubbles based on current state.
795 * Does not un-bubble, just hides or un-hides. Notifies any
796 * {@link BubbleStateChangeListener}s of visibility changes.
797 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800798 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700799 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800800 if (mStackView == null) {
801 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800802 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800803 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
804 // Bubbles only appear in unlocked shade
805 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +0000806 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800807 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800808 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700809
Mady Mellor698d9e82019-08-01 23:11:53 +0000810 // Let listeners know if bubble state changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700811 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +0000812 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellor88552b82019-08-05 22:38:59 +0000813 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -0700814 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
815 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
816 }
817
818 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800819 }
820
821 /**
822 * Rect indicating the touchable region for the bubble stack / expanded stack.
823 */
824 public Rect getTouchableRegion() {
825 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
826 return null;
827 }
828 mStackView.getBoundsOnScreen(mTempRect);
829 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800830 }
831
Mady Mellor390bff42019-04-05 15:09:01 -0700832 /**
833 * The display id of the expanded view, if the stack is expanded and not occluded by the
834 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
835 */
836 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200837 final Bubble bubble = getExpandedBubble(context);
838 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
839 }
840
841 @Nullable
842 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700843 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200844 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700845 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200846 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700847 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200848 final Bubble expandedBubble = mStackView.getExpandedBubble();
849 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700850 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200851 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700852 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200853 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700854 }
855
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800856 @VisibleForTesting
857 BubbleStackView getStackView() {
858 return mStackView;
859 }
860
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700861 /**
862 * Description of current bubble state.
863 */
864 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
865 pw.println("BubbleController state:");
866 mBubbleData.dump(fd, pw, args);
867 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -0400868 if (mStackView != null) {
869 mStackView.dump(fd, pw, args);
870 }
871 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700872 }
873
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400874 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
875 StringBuilder sb = new StringBuilder();
876 for (Bubble bubble : bubbles) {
877 if (bubble == null) {
878 sb.append(" <null> !!!!!\n");
879 } else {
880 boolean isSelected = (bubble == selected);
881 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
882 ((isSelected) ? "->" : " "),
883 bubble.getLastActivity(),
884 (bubble.isOngoing() ? 1 : 0),
885 bubble.getKey()));
886 }
887 }
888 return sb.toString();
889 }
890
Mady Mellore80930e2019-03-21 16:00:45 -0700891 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -0500892 * This task stack listener is responsible for responding to tasks moved to the front
893 * which are on the default (main) display. When this happens, expanded bubbles must be
894 * collapsed so the user may interact with the app which was just moved to the front.
895 * <p>
896 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
897 * these calls via a main thread Handler.
898 */
899 @MainThread
900 private class BubbleTaskStackListener extends TaskStackChangeListener {
901
Mark Renoufcecc77b2019-01-30 16:32:24 -0500902 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500903 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
904 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -0700905 if (!mStackView.isExpansionAnimating()) {
906 mBubbleData.setExpanded(false);
907 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500908 }
909 }
910
Mark Renoufcecc77b2019-01-30 16:32:24 -0500911 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500912 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500913 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400914 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500915 }
916 }
Mark Renouf446251d2019-04-26 10:22:41 -0400917
918 @Override
919 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
920 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
921 mBubbleData.setExpanded(false);
922 }
923 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200924
925 @Override
926 public void onSingleTaskDisplayDrawn(int displayId) {
927 final Bubble expandedBubble = getExpandedBubble(mContext);
928 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
929 expandedBubble.setContentVisibility(true);
930 }
931 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200932
933 @Override
934 public void onSingleTaskDisplayEmpty(int displayId) {
935 final Bubble expandedBubble = getExpandedBubble(mContext);
936 if (expandedBubble == null) {
937 return;
938 }
939 if (expandedBubble.getDisplayId() == displayId) {
940 mBubbleData.setExpanded(false);
Mady Mellored99c272019-06-13 15:58:30 -0700941 expandedBubble.getExpandedView().notifyDisplayEmpty();
Issei Suzuki734bc942019-06-05 13:59:52 +0200942 }
943 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500944 }
945
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800946 private static boolean areBubblesEnabled(Context context) {
947 return Settings.Secure.getInt(context.getContentResolver(),
948 ENABLE_BUBBLES, 1) != 0;
949 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500950
Mady Mellorca0c24c2019-05-16 16:14:32 -0700951 /**
952 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
953 *
954 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
955 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
956 *
957 * @param context the context to use.
958 * @param entry the entry to bubble.
959 */
960 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
961 PendingIntent intent = entry.getBubbleMetadata() != null
962 ? entry.getBubbleMetadata().getIntent()
963 : null;
964 if (intent == null) {
965 Log.w(TAG, "Unable to create bubble -- no intent");
966 return false;
967 }
968 ActivityInfo info =
969 intent.getIntent().resolveActivityInfo(context.getPackageManager(), 0);
970 if (info == null) {
971 Log.w(TAG, "Unable to send as bubble -- couldn't find activity info for intent: "
972 + intent);
973 return false;
974 }
975 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
976 Log.w(TAG, "Unable to send as bubble -- activity is not resizable for intent: "
977 + intent);
978 return false;
979 }
980 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
981 Log.w(TAG, "Unable to send as bubble -- activity is not documentLaunchMode=always "
982 + "for intent: " + intent);
983 return false;
984 }
985 if ((info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
986 Log.w(TAG, "Unable to send as bubble -- activity is not embeddable for intent: "
987 + intent);
988 return false;
989 }
990 return true;
991 }
992
Joshua Tsujia19515f2019-02-13 18:02:29 -0500993 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wange3ff1392019-08-06 14:24:43 -0700994 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -0500995 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500996 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
997 if (mStackView != null && mStackView.getBubbleCount() > 0) {
998 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500999 }
1000 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001001 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001002}