blob: 3cc91de4343851d4db1517b65d299f74c768ef17 [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 Tsujia19515f2019-02-13 18:02:29 -050051import android.content.pm.ParceledListSlice;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040052import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080053import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050054import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080055import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080056import android.provider.Settings;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040057import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040058import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040059import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040060import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040061import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040062import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050063import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050064import android.view.IPinnedStackController;
65import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080066import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080067import android.widget.FrameLayout;
68
Mark Renouf08bc42a2019-03-07 13:01:59 -050069import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050070import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040071import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050072
Mady Mellorebdbbb92018-11-15 14:36:48 -080073import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070074import com.android.internal.statusbar.IStatusBarService;
Ned Burns01e38212019-01-03 16:32:52 -050075import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080076import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050077import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050078import com.android.systemui.shared.system.ActivityManagerWrapper;
79import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050080import com.android.systemui.shared.system.WindowManagerWrapper;
Mark Renoufc19b4732019-06-26 12:08:33 -040081import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070082import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050083import com.android.systemui.statusbar.notification.NotificationEntryListener;
84import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080085import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -040086import com.android.systemui.statusbar.notification.collection.NotificationData;
Ned Burnsf81c4c42019-01-07 14:10:43 -050087import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellor22f2f072019-04-18 13:26:18 -070088import com.android.systemui.statusbar.phone.NotificationGroupManager;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080089import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070090import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040091import com.android.systemui.statusbar.policy.ZenModeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080092
Mady Mellor70cba7bb2019-07-02 15:06:07 -070093import java.io.FileDescriptor;
94import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -050095import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -040096import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -070097import java.util.ArrayList;
Mady Mellore80930e2019-03-21 16:00:45 -070098import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050099
Jason Monk27d01a622018-12-10 15:57:09 -0500100import javax.inject.Inject;
101import javax.inject.Singleton;
102
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800103/**
104 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
105 * Bubbles can be expanded to show more content.
106 *
107 * The controller manages addition, removal, and visible state of bubbles on screen.
108 */
Jason Monk27d01a622018-12-10 15:57:09 -0500109@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -0400110public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800111
Issei Suzukia8d07312019-06-07 12:56:19 +0200112 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800113
Mark Renouf08bc42a2019-03-07 13:01:59 -0500114 @Retention(SOURCE)
115 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400116 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700117 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400118 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500119 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700120
Mark Renouf08bc42a2019-03-07 13:01:59 -0500121 static final int DISMISS_USER_GESTURE = 1;
122 static final int DISMISS_AGED = 2;
123 static final int DISMISS_TASK_FINISHED = 3;
124 static final int DISMISS_BLOCKED = 4;
125 static final int DISMISS_NOTIF_CANCEL = 5;
126 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700127 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400128 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700129 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700130 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500131
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400132 public static final int MAX_BUBBLES = 5; // TODO: actually enforce this
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -0400133
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800134 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800135 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500136
Ned Burns01e38212019-01-03 16:32:52 -0500137 private final Context mContext;
138 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500139 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800140 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800141 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100142 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700143 private final NotificationGroupManager mNotificationGroupManager;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800144
Mady Mellor3dff9e62019-02-05 18:12:53 -0800145 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400146 @Nullable private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800147
Mark Renoufc19b4732019-06-26 12:08:33 -0400148 // Tracks the id of the current (foreground) user.
149 private int mCurrentUserId;
150 // Saves notification keys of active bubbles when users are switched.
151 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
152
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800153 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500154 private final StatusBarWindowController mStatusBarWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400155 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800156 private StatusBarStateListener mStatusBarStateListener;
157
Mady Melloraa8fef22019-04-11 13:36:40 -0700158 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700159 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800160
Mady Mellord1c78b262018-11-06 18:04:40 -0800161 // Used for determining view rect for touch interaction
162 private Rect mTempRect = new Rect();
163
Mark Renoufc19b4732019-06-26 12:08:33 -0400164 // Listens to user switch so bubbles can be saved and restored.
165 private final NotificationLockscreenUserManager mNotifUserManager;
166
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400167 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
168 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
169
Mady Mellor5549dd22018-11-06 18:07:34 -0800170 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800171 * Listener to be notified when some states of the bubbles change.
172 */
173 public interface BubbleStateChangeListener {
174 /**
175 * Called when the stack has bubbles or no longer has bubbles.
176 */
177 void onHasBubblesChanged(boolean hasBubbles);
178 }
179
Mady Mellorcd9b1302018-11-06 18:08:04 -0800180 /**
181 * Listener to find out about stack expansion / collapse events.
182 */
183 public interface BubbleExpandListener {
184 /**
185 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700186 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800187 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800188 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800189 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800190 void onBubbleExpandChanged(boolean isExpanding, String key);
191 }
192
193 /**
194 * Listens for the current state of the status bar and updates the visibility state
195 * of bubbles as needed.
196 */
197 private class StatusBarStateListener implements StatusBarStateController.StateListener {
198 private int mState;
199 /**
200 * Returns the current status bar state.
201 */
202 public int getCurrentState() {
203 return mState;
204 }
205
206 @Override
207 public void onStateChanged(int newState) {
208 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400209 boolean shouldCollapse = (mState != SHADE);
210 if (shouldCollapse) {
211 collapseStack();
212 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700213 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800214 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800215 }
216
Jason Monk27d01a622018-12-10 15:57:09 -0500217 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800218 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Mady Melloraa8fef22019-04-11 13:36:40 -0700219 BubbleData data, ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400220 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400221 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700222 NotificationLockscreenUserManager notifUserManager,
223 NotificationGroupManager groupManager) {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700224 this(context, statusBarWindowController, data, null /* synchronizer */,
Mark Renoufc19b4732019-06-26 12:08:33 -0400225 configurationController, interruptionStateProvider, zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700226 notifUserManager, groupManager);
Issei Suzukic0387542019-03-08 17:31:14 +0100227 }
228
229 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700230 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
Mady Melloraa8fef22019-04-11 13:36:40 -0700231 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400232 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400233 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700234 NotificationLockscreenUserManager notifUserManager,
235 NotificationGroupManager groupManager) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800236 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700237 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400238 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400239 mZenModeController = zenModeController;
240 mZenModeController.addCallback(new ZenModeController.Callback() {
241 @Override
242 public void onZenChanged(int zen) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700243 if (mStackView != null) {
244 mStackView.updateDots();
245 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400246 }
247
248 @Override
249 public void onConfigChanged(ZenModeConfig config) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700250 if (mStackView != null) {
251 mStackView.updateDots();
252 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400253 }
254 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700255
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700256 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800257
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400258 mBubbleData = data;
259 mBubbleData.setListener(mBubbleDataListener);
260
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800261 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500262 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700263 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellor22f2f072019-04-18 13:26:18 -0700264 mNotificationGroupManager = groupManager;
Mady Mellor740d85d2019-07-09 15:26:47 -0700265 mNotificationGroupManager.addOnGroupChangeListener(
266 new NotificationGroupManager.OnGroupChangeListener() {
267 @Override
268 public void onGroupSuppressionChanged(
269 NotificationGroupManager.NotificationGroup group,
270 boolean suppressed) {
271 // More notifications could be added causing summary to no longer
272 // be suppressed -- in this case need to remove the key.
273 final String groupKey = group.summary != null
274 ? group.summary.notification.getGroupKey()
275 : null;
276 if (!suppressed && groupKey != null
277 && mBubbleData.isSummarySuppressed(groupKey)) {
278 mBubbleData.removeSuppressedSummary(groupKey);
279 }
280 }
281 });
Mady Mellorb4991e62019-01-10 15:14:51 -0800282
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800283 mStatusBarWindowController = statusBarWindowController;
284 mStatusBarStateListener = new StatusBarStateListener();
285 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500286
Mark Renoufcecc77b2019-01-30 16:32:24 -0500287 mTaskStackListener = new BubbleTaskStackListener();
288 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800289
Joshua Tsujia19515f2019-02-13 18:02:29 -0500290 try {
291 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
292 } catch (RemoteException e) {
293 e.printStackTrace();
294 }
Issei Suzukic0387542019-03-08 17:31:14 +0100295 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700296
297 mBarService = IStatusBarService.Stub.asInterface(
298 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400299
300 mSavedBubbleKeysPerUser = new SparseSetArray<>();
301 mCurrentUserId = mNotifUserManager.getCurrentUserId();
302 mNotifUserManager.addUserChangedListener(
303 newUserId -> {
304 saveBubbles(mCurrentUserId);
305 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
306 restoreBubbles(newUserId);
307 mCurrentUserId = newUserId;
308 });
Mady Mellor5549dd22018-11-06 18:07:34 -0800309 }
310
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400311 /**
312 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
313 * method initializes the stack view and adds it to the StatusBar just above the scrim.
314 */
315 private void ensureStackViewCreated() {
316 if (mStackView == null) {
317 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
318 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Lyn Hanbde48202019-05-29 19:18:29 -0700319 int bubbleScrimIndex = sbv.indexOfChild(sbv.findViewById(R.id.scrim_for_bubble));
320 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
321 sbv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400322 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
323 if (mExpandListener != null) {
324 mStackView.setExpandListener(mExpandListener);
325 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400326 }
327 }
328
Mark Renoufc19b4732019-06-26 12:08:33 -0400329 /**
330 * Records the notification key for any active bubbles. These are used to restore active
331 * bubbles when the user returns to the foreground.
332 *
333 * @param userId the id of the user
334 */
335 private void saveBubbles(@UserIdInt int userId) {
336 // First clear any existing keys that might be stored.
337 mSavedBubbleKeysPerUser.remove(userId);
338 // Add in all active bubbles for the current user.
339 for (Bubble bubble: mBubbleData.getBubbles()) {
340 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
341 }
342 }
343
344 /**
345 * Promotes existing notifications to Bubbles if they were previously bubbles.
346 *
347 * @param userId the id of the user
348 */
349 private void restoreBubbles(@UserIdInt int userId) {
350 NotificationData notificationData =
351 mNotificationEntryManager.getNotificationData();
352 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
353 if (savedBubbleKeys == null) {
354 // There were no bubbles saved for this used.
355 return;
356 }
357 for (NotificationEntry e : notificationData.getNotificationsForCurrentUser()) {
358 if (savedBubbleKeys.contains(e.key)
359 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
360 && canLaunchInActivityView(mContext, e)) {
361 updateBubble(e, /* suppressFlyout= */ true);
362 }
363 }
364 // Finally, remove the entries for this user now that bubbles are restored.
365 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
366 }
367
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700368 @Override
369 public void onUiModeChanged() {
370 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700371 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700372 }
373 }
374
375 @Override
376 public void onOverlayChanged() {
377 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700378 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700379 }
380 }
381
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400382 @Override
383 public void onConfigChanged(Configuration newConfig) {
384 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400385 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700386 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400387 }
388 }
389
Mady Mellor5549dd22018-11-06 18:07:34 -0800390 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800391 * Set a listener to be notified when some states of the bubbles change.
392 */
393 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
394 mStateChangeListener = listener;
395 }
396
397 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800398 * Set a listener to be notified of bubble expand events.
399 */
400 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100401 mExpandListener = ((isExpanding, key) -> {
402 if (listener != null) {
403 listener.onBubbleExpandChanged(isExpanding, key);
404 }
405 mStatusBarWindowController.setBubbleExpanded(isExpanding);
406 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800407 if (mStackView != null) {
408 mStackView.setExpandListener(mExpandListener);
409 }
410 }
411
412 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800413 * Whether or not there are bubbles present, regardless of them being visible on the
414 * screen (e.g. if on AOD).
415 */
416 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800417 if (mStackView == null) {
418 return false;
419 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400420 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800421 }
422
423 /**
424 * Whether the stack of bubbles is expanded or not.
425 */
426 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400427 return mBubbleData.isExpanded();
428 }
429
430 /**
431 * Tell the stack of bubbles to expand.
432 */
433 public void expandStack() {
434 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800435 }
436
437 /**
438 * Tell the stack of bubbles to collapse.
439 */
440 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400441 mBubbleData.setExpanded(false /* expanded */);
442 }
443
Mady Mellorce23c462019-06-17 17:30:07 -0700444 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700445 * True if either:
446 * (1) There is a bubble associated with the provided key and if its notification is hidden
447 * from the shade.
448 * (2) There is a group summary associated with the provided key that is hidden from the shade
449 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700450 *
Mady Mellore28fe102019-07-09 15:33:32 -0700451 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700452 */
453 public boolean isBubbleNotificationSuppressedFromShade(String key) {
Mady Mellore28fe102019-07-09 15:33:32 -0700454 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorce23c462019-06-17 17:30:07 -0700455 && !mBubbleData.getBubbleWithKey(key).showInShadeWhenBubble();
Mady Mellore28fe102019-07-09 15:33:32 -0700456 NotificationEntry entry = mNotificationEntryManager.getNotificationData().get(key);
457 String groupKey = entry != null ? entry.notification.getGroupKey() : null;
458 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700459 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
460 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700461 }
462
Mark Renouf71a3af62019-04-08 15:02:54 -0400463 void selectBubble(Bubble bubble) {
464 mBubbleData.setSelectedBubble(bubble);
465 }
466
467 @VisibleForTesting
468 void selectBubble(String key) {
469 Bubble bubble = mBubbleData.getBubbleWithKey(key);
470 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800471 }
472
473 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400474 * Request the stack expand if needed, then select the specified Bubble as current.
475 *
476 * @param notificationKey the notification key for the bubble to be selected
477 */
478 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400479 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
480 if (bubble != null) {
481 mBubbleData.setSelectedBubble(bubble);
482 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400483 }
484 }
485
486 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800487 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
488 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500489 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400490 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800491 }
492
493 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500494 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
495 * is forwarded a back key down/up pair.
496 */
497 public void performBackPressIfNeeded() {
498 if (mStackView != null) {
499 mStackView.performBackPressIfNeeded();
500 }
501 }
502
503 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800504 * Adds or updates a bubble associated with the provided notification entry.
505 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400506 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800507 */
Mark Renouff97ed462019-04-05 13:46:24 -0400508 void updateBubble(NotificationEntry notif) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400509 updateBubble(notif, /* supressFlyout */ false);
510 }
511
512 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700513 // If this is an interruptive notif, mark that it's interrupted
514 if (notif.importance >= NotificationManager.IMPORTANCE_HIGH) {
515 notif.setInterruption();
516 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400517 mBubbleData.notificationEntryUpdated(notif, suppressFlyout);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800518 }
519
520 /**
521 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500522 * <p>
523 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800524 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500525 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500526 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400527 // TEMP: refactor to change this to pass entry
528 Bubble bubble = mBubbleData.getBubbleWithKey(key);
529 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700530 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800531 }
532 }
533
Ned Burns01e38212019-01-03 16:32:52 -0500534 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700535 private final NotificationRemoveInterceptor mRemoveInterceptor =
536 new NotificationRemoveInterceptor() {
537 @Override
538 public boolean onNotificationRemoveRequested(String key, int reason) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700539 NotificationEntry entry = mNotificationEntryManager.getNotificationData().get(key);
540 String groupKey = entry != null ? entry.notification.getGroupKey() : null;
541 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
542
543 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
Mady Mellore28fe102019-07-09 15:33:32 -0700544 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
545 && mBubbleData.getSummaryKey(groupKey).equals(key));
Mady Mellor22f2f072019-04-18 13:26:18 -0700546 boolean isSummary = entry != null
547 && entry.notification.getNotification().isGroupSummary();
Mady Mellore28fe102019-07-09 15:33:32 -0700548 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
549 && bubbleChildren != null && !bubbleChildren.isEmpty();
Mady Mellor22f2f072019-04-18 13:26:18 -0700550
551 if (!inBubbleData && !isSummaryOfBubbles) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700552 return false;
553 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700554
555 final boolean isClearAll = reason == REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700556 final boolean isUserDimiss = reason == REASON_CANCEL || reason == REASON_CLICK;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700557 final boolean isAppCancel = reason == REASON_APP_CANCEL
558 || reason == REASON_APP_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700559 final boolean isSummaryCancel = reason == REASON_GROUP_SUMMARY_CANCELED;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700560
561 // Need to check for !appCancel here because the notification may have
562 // previously been dismissed & entry.isRowDismissed would still be true
Mady Mellorca184aae2019-09-17 16:07:12 -0700563 boolean userRemovedNotif = (entry != null && entry.isRowDismissed() && !isAppCancel)
Mady Mellor22f2f072019-04-18 13:26:18 -0700564 || isClearAll || isUserDimiss || isSummaryCancel;
565
566 if (isSummaryOfBubbles) {
567 return handleSummaryRemovalInterception(entry, userRemovedNotif);
568 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700569
570 // The bubble notification sticks around in the data as long as the bubble is
571 // not dismissed and the app hasn't cancelled the notification.
Mady Mellor22f2f072019-04-18 13:26:18 -0700572 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellorca184aae2019-09-17 16:07:12 -0700573 boolean bubbleExtended = entry != null && entry.isBubble() && userRemovedNotif;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700574 if (bubbleExtended) {
Mady Mellorce23c462019-06-17 17:30:07 -0700575 bubble.setShowInShadeWhenBubble(false);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700576 bubble.setShowBubbleDot(false);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700577 if (mStackView != null) {
578 mStackView.updateDotVisibility(entry.key);
579 }
580 mNotificationEntryManager.updateNotifications();
581 return true;
Mady Mellorca184aae2019-09-17 16:07:12 -0700582 } else if (!userRemovedNotif && entry != null) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700583 // This wasn't a user removal so we should remove the bubble as well
584 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
585 return false;
586 }
587 return false;
588 }
589 };
590
Mady Mellor22f2f072019-04-18 13:26:18 -0700591 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
592 boolean userRemovedNotif) {
593 String groupKey = summary.notification.getGroupKey();
594 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
595
596 if (userRemovedNotif) {
597 // If it's a user dismiss we mark the children to be hidden from the shade.
598 for (int i = 0; i < bubbleChildren.size(); i++) {
599 Bubble bubbleChild = bubbleChildren.get(i);
600 // As far as group manager is concerned, once a child is no longer shown
601 // in the shade, it is essentially removed.
602 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
603 bubbleChild.setShowInShadeWhenBubble(false);
604 bubbleChild.setShowBubbleDot(false);
605 if (mStackView != null) {
606 mStackView.updateDotVisibility(bubbleChild.getKey());
607 }
608 }
609 // And since all children are removed, remove the summary.
610 mNotificationGroupManager.onEntryRemoved(summary);
611
612 // If the summary was auto-generated we don't need to keep that notification around
613 // because apps can't cancel it; so we only intercept & suppress real summaries.
614 boolean isAutogroupSummary = (summary.notification.getNotification().flags
615 & FLAG_AUTOGROUP_SUMMARY) != 0;
Mady Mellore28fe102019-07-09 15:33:32 -0700616 if (!isAutogroupSummary) {
617 mBubbleData.addSummaryToSuppress(summary.notification.getGroupKey(),
618 summary.key);
619 // Tell shade to update for the suppression
620 mNotificationEntryManager.updateNotifications();
621 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700622 return !isAutogroupSummary;
623 } else {
Mady Mellore28fe102019-07-09 15:33:32 -0700624 // If it's not a user dismiss it's a cancel.
625 mBubbleData.removeSuppressedSummary(groupKey);
626
Mady Mellor22f2f072019-04-18 13:26:18 -0700627 // Remove any associated bubble children.
628 for (int i = 0; i < bubbleChildren.size(); i++) {
629 Bubble bubbleChild = bubbleChildren.get(i);
630 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
631 DISMISS_GROUP_CANCELLED);
632 }
633 return false;
634 }
635 }
636
Mady Mellorc2ff0112019-03-28 14:18:06 -0700637 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500638 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
639 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500640 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800641 if (!areBubblesEnabled(mContext)) {
642 return;
643 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700644 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
645 && canLaunchInActivityView(mContext, entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400646 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800647 }
648 }
649
650 @Override
651 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800652 if (!areBubblesEnabled(mContext)) {
653 return;
654 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700655 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
656 && canLaunchInActivityView(mContext, entry);
Mady Melloraa8fef22019-04-11 13:36:40 -0700657 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.key)) {
658 // It was previously a bubble but no longer a bubble -- lets remove it
659 removeBubble(entry.key, DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700660 } else if (shouldBubble) {
Mady Mellored99c272019-06-13 15:58:30 -0700661 Bubble b = mBubbleData.getBubbleWithKey(entry.key);
Mark Renouff97ed462019-04-05 13:46:24 -0400662 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800663 }
664 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400665
666 @Override
667 public void onNotificationRankingUpdated(RankingMap rankingMap) {
668 // Forward to BubbleData to block any bubbles which should no longer be shown
669 mBubbleData.notificationRankingUpdated(rankingMap);
670 }
Ned Burns01e38212019-01-03 16:32:52 -0500671 };
672
Mark Renouf71a3af62019-04-08 15:02:54 -0400673 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400674 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400675
Mark Renouf3bc5b362019-04-05 14:37:59 -0400676 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400677 public void applyUpdate(BubbleData.Update update) {
678 if (mStackView == null && update.addedBubble != null) {
679 // Lazy init stack view when the first bubble is added.
680 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400681 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400682
683 // If not yet initialized, ignore all other changes.
684 if (mStackView == null) {
685 return;
686 }
687
688 if (update.addedBubble != null) {
689 mStackView.addBubble(update.addedBubble);
690 }
691
692 // Collapsing? Do this first before remaining steps.
693 if (update.expandedChanged && !update.expanded) {
694 mStackView.setExpanded(false);
695 }
696
697 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700698 ArrayList<Pair<Bubble, Integer>> removedBubbles =
699 new ArrayList<>(update.removedBubbles);
700 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400701 final Bubble bubble = removed.first;
702 @DismissReason final int reason = removed.second;
703 mStackView.removeBubble(bubble);
704
Mark Renoufc19b4732019-06-26 12:08:33 -0400705 // If the bubble is removed for user switching, leave the notification in place.
706 if (reason != DISMISS_USER_CHANGED) {
707 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
708 && !bubble.showInShadeWhenBubble()) {
709 // The bubble is gone & the notification is gone, time to actually remove it
710 mNotificationEntryManager.performRemoveNotification(
711 bubble.getEntry().notification, UNDEFINED_DISMISS_REASON);
712 } else {
713 // Update the flag for SysUI
714 bubble.getEntry().notification.getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700715
Mark Renoufc19b4732019-06-26 12:08:33 -0400716 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
717 // will get right result back
718 try {
719 mBarService.onNotificationBubbleChanged(bubble.getKey(),
720 false /* isBubble */);
721 } catch (RemoteException e) {
722 // Bad things have happened
723 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400724 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700725
Mady Mellore28fe102019-07-09 15:33:32 -0700726 // Check if removed bubble has an associated suppressed group summary that needs
727 // to be removed now.
728 final String groupKey = bubble.getEntry().notification.getGroupKey();
729 if (mBubbleData.isSummarySuppressed(groupKey)
730 && mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
731 // Time to actually remove the summary.
732 String notifKey = mBubbleData.getSummaryKey(groupKey);
733 mBubbleData.removeSuppressedSummary(groupKey);
734 NotificationEntry entry =
735 mNotificationEntryManager.getNotificationData().get(notifKey);
Mady Mellore28fe102019-07-09 15:33:32 -0700736 mNotificationEntryManager.performRemoveNotification(
737 entry.notification, UNDEFINED_DISMISS_REASON);
738 }
739
Mady Mellor22f2f072019-04-18 13:26:18 -0700740 // Check if summary should be removed from NoManGroup
741 NotificationEntry summary = mNotificationGroupManager.getLogicalGroupSummary(
742 bubble.getEntry().notification);
743 if (summary != null) {
744 ArrayList<NotificationEntry> summaryChildren =
745 mNotificationGroupManager.getLogicalChildren(summary.notification);
Mady Mellore4348272019-07-29 17:43:36 -0700746 boolean isSummaryThisNotif = summary.key.equals(bubble.getEntry().key);
747 if (!isSummaryThisNotif
748 && (summaryChildren == null || summaryChildren.isEmpty())) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700749 mNotificationEntryManager.performRemoveNotification(
750 summary.notification, UNDEFINED_DISMISS_REASON);
751 }
752 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700753 }
754 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400755
Mark Renouf82a40e62019-05-23 16:16:24 -0400756 if (update.updatedBubble != null) {
757 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400758 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400759
Mark Renouf82a40e62019-05-23 16:16:24 -0400760 if (update.orderChanged) {
761 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400762 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400763
Mark Renouf82a40e62019-05-23 16:16:24 -0400764 if (update.selectionChanged) {
765 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700766 if (update.selectedBubble != null) {
767 mNotificationGroupManager.updateSuppression(
768 update.selectedBubble.getEntry());
769 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400770 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400771
Mark Renouf82a40e62019-05-23 16:16:24 -0400772 // Expanding? Apply this last.
773 if (update.expandedChanged && update.expanded) {
774 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400775 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400776
Mark Renouf71a3af62019-04-08 15:02:54 -0400777 mNotificationEntryManager.updateNotifications();
Lyn Han6c40fe72019-05-08 14:06:33 -0700778 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400779
Issei Suzukia8d07312019-06-07 12:56:19 +0200780 if (DEBUG_BUBBLE_CONTROLLER) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400781 Log.d(TAG, "[BubbleData]");
782 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
783 mBubbleData.getSelectedBubble()));
784
785 if (mStackView != null) {
786 Log.d(TAG, "[BubbleStackView]");
787 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
788 mStackView.getExpandedBubble()));
789 }
790 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400791 }
792 };
793
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800794 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400795 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700796 * Updates the visibility of the bubbles based on current state.
797 * Does not un-bubble, just hides or un-hides. Notifies any
798 * {@link BubbleStateChangeListener}s of visibility changes.
799 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800800 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700801 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800802 if (mStackView == null) {
803 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800804 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800805 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
806 // Bubbles only appear in unlocked shade
807 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +0000808 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800809 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800810 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700811
Mady Mellor698d9e82019-08-01 23:11:53 +0000812 // Let listeners know if bubble state changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700813 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +0000814 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellor88552b82019-08-05 22:38:59 +0000815 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -0700816 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
817 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
818 }
819
820 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800821 }
822
823 /**
824 * Rect indicating the touchable region for the bubble stack / expanded stack.
825 */
826 public Rect getTouchableRegion() {
827 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
828 return null;
829 }
830 mStackView.getBoundsOnScreen(mTempRect);
831 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800832 }
833
Mady Mellor390bff42019-04-05 15:09:01 -0700834 /**
835 * The display id of the expanded view, if the stack is expanded and not occluded by the
836 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
837 */
838 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200839 final Bubble bubble = getExpandedBubble(context);
840 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
841 }
842
843 @Nullable
844 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700845 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200846 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700847 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200848 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700849 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200850 final Bubble expandedBubble = mStackView.getExpandedBubble();
851 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700852 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200853 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700854 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200855 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700856 }
857
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800858 @VisibleForTesting
859 BubbleStackView getStackView() {
860 return mStackView;
861 }
862
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700863 /**
864 * Description of current bubble state.
865 */
866 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
867 pw.println("BubbleController state:");
868 mBubbleData.dump(fd, pw, args);
869 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -0400870 if (mStackView != null) {
871 mStackView.dump(fd, pw, args);
872 }
873 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700874 }
875
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400876 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
877 StringBuilder sb = new StringBuilder();
878 for (Bubble bubble : bubbles) {
879 if (bubble == null) {
880 sb.append(" <null> !!!!!\n");
881 } else {
882 boolean isSelected = (bubble == selected);
883 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
884 ((isSelected) ? "->" : " "),
885 bubble.getLastActivity(),
886 (bubble.isOngoing() ? 1 : 0),
887 bubble.getKey()));
888 }
889 }
890 return sb.toString();
891 }
892
Mady Mellore80930e2019-03-21 16:00:45 -0700893 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -0500894 * This task stack listener is responsible for responding to tasks moved to the front
895 * which are on the default (main) display. When this happens, expanded bubbles must be
896 * collapsed so the user may interact with the app which was just moved to the front.
897 * <p>
898 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
899 * these calls via a main thread Handler.
900 */
901 @MainThread
902 private class BubbleTaskStackListener extends TaskStackChangeListener {
903
Mark Renoufcecc77b2019-01-30 16:32:24 -0500904 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500905 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
906 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -0700907 if (!mStackView.isExpansionAnimating()) {
908 mBubbleData.setExpanded(false);
909 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500910 }
911 }
912
Mark Renoufcecc77b2019-01-30 16:32:24 -0500913 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500914 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500915 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400916 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500917 }
918 }
Mark Renouf446251d2019-04-26 10:22:41 -0400919
920 @Override
921 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
922 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
923 mBubbleData.setExpanded(false);
924 }
925 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200926
927 @Override
928 public void onSingleTaskDisplayDrawn(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -0700929 final Bubble expandedBubble = mStackView != null
930 ? mStackView.getExpandedBubble()
931 : null;
Issei Suzukicac2a502019-04-16 16:52:50 +0200932 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
933 expandedBubble.setContentVisibility(true);
934 }
935 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200936
937 @Override
938 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -0700939 final Bubble expandedBubble = mStackView != null
940 ? mStackView.getExpandedBubble()
941 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -0700942 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
943 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +0200944 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +0200945 }
Mady Mellorca184aae2019-09-17 16:07:12 -0700946 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +0200947 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500948 }
949
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800950 private static boolean areBubblesEnabled(Context context) {
951 return Settings.Secure.getInt(context.getContentResolver(),
952 ENABLE_BUBBLES, 1) != 0;
953 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500954
Mady Mellorca0c24c2019-05-16 16:14:32 -0700955 /**
956 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
957 *
958 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
959 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
960 *
961 * @param context the context to use.
962 * @param entry the entry to bubble.
963 */
964 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
965 PendingIntent intent = entry.getBubbleMetadata() != null
966 ? entry.getBubbleMetadata().getIntent()
967 : null;
968 if (intent == null) {
969 Log.w(TAG, "Unable to create bubble -- no intent");
970 return false;
971 }
972 ActivityInfo info =
973 intent.getIntent().resolveActivityInfo(context.getPackageManager(), 0);
974 if (info == null) {
975 Log.w(TAG, "Unable to send as bubble -- couldn't find activity info for intent: "
976 + intent);
977 return false;
978 }
979 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
980 Log.w(TAG, "Unable to send as bubble -- activity is not resizable for intent: "
981 + intent);
982 return false;
983 }
984 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
985 Log.w(TAG, "Unable to send as bubble -- activity is not documentLaunchMode=always "
986 + "for intent: " + intent);
987 return false;
988 }
989 if ((info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
990 Log.w(TAG, "Unable to send as bubble -- activity is not embeddable for intent: "
991 + intent);
992 return false;
993 }
994 return true;
995 }
996
Joshua Tsujia19515f2019-02-13 18:02:29 -0500997 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
998 private class BubblesImeListener extends IPinnedStackListener.Stub {
999
1000 @Override
1001 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
1002 }
1003
1004 @Override
1005 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
1006 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
1007 int displayRotation) throws RemoteException {}
1008
1009 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001010 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1011 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1012 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001013 }
1014 }
1015
1016 @Override
1017 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
1018 throws RemoteException {}
1019
1020 @Override
1021 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
1022
1023 @Override
1024 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
1025 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001026}