blob: 46139b81ac887d2e881709cc770d0fdd4f5c6c6b [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 Mellor22f2f072019-04-18 13:26:18 -0700117 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED})
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;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500130
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400131 public static final int MAX_BUBBLES = 5; // TODO: actually enforce this
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -0400132
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800133 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800134 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500135
Ned Burns01e38212019-01-03 16:32:52 -0500136 private final Context mContext;
137 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500138 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800139 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800140 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100141 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700142 private final NotificationGroupManager mNotificationGroupManager;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800143
Mady Mellor3dff9e62019-02-05 18:12:53 -0800144 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400145 @Nullable private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800146
Mark Renoufc19b4732019-06-26 12:08:33 -0400147 // Tracks the id of the current (foreground) user.
148 private int mCurrentUserId;
149 // Saves notification keys of active bubbles when users are switched.
150 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
151
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800152 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500153 private final StatusBarWindowController mStatusBarWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400154 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800155 private StatusBarStateListener mStatusBarStateListener;
156
Mady Melloraa8fef22019-04-11 13:36:40 -0700157 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700158 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800159
Mady Mellord1c78b262018-11-06 18:04:40 -0800160 // Used for determining view rect for touch interaction
161 private Rect mTempRect = new Rect();
162
Mark Renoufc19b4732019-06-26 12:08:33 -0400163 // Listens to user switch so bubbles can be saved and restored.
164 private final NotificationLockscreenUserManager mNotifUserManager;
165
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400166 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
167 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
168
Mady Mellor5549dd22018-11-06 18:07:34 -0800169 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800170 * Listener to be notified when some states of the bubbles change.
171 */
172 public interface BubbleStateChangeListener {
173 /**
174 * Called when the stack has bubbles or no longer has bubbles.
175 */
176 void onHasBubblesChanged(boolean hasBubbles);
177 }
178
Mady Mellorcd9b1302018-11-06 18:08:04 -0800179 /**
180 * Listener to find out about stack expansion / collapse events.
181 */
182 public interface BubbleExpandListener {
183 /**
184 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700185 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800186 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800187 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800188 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800189 void onBubbleExpandChanged(boolean isExpanding, String key);
190 }
191
192 /**
193 * Listens for the current state of the status bar and updates the visibility state
194 * of bubbles as needed.
195 */
196 private class StatusBarStateListener implements StatusBarStateController.StateListener {
197 private int mState;
198 /**
199 * Returns the current status bar state.
200 */
201 public int getCurrentState() {
202 return mState;
203 }
204
205 @Override
206 public void onStateChanged(int newState) {
207 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400208 boolean shouldCollapse = (mState != SHADE);
209 if (shouldCollapse) {
210 collapseStack();
211 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700212 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800213 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800214 }
215
Jason Monk27d01a622018-12-10 15:57:09 -0500216 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800217 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Mady Melloraa8fef22019-04-11 13:36:40 -0700218 BubbleData data, ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400219 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400220 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700221 NotificationLockscreenUserManager notifUserManager,
222 NotificationGroupManager groupManager) {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700223 this(context, statusBarWindowController, data, null /* synchronizer */,
Mark Renoufc19b4732019-06-26 12:08:33 -0400224 configurationController, interruptionStateProvider, zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700225 notifUserManager, groupManager);
Issei Suzukic0387542019-03-08 17:31:14 +0100226 }
227
228 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700229 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
Mady Melloraa8fef22019-04-11 13:36:40 -0700230 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400231 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400232 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700233 NotificationLockscreenUserManager notifUserManager,
234 NotificationGroupManager groupManager) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800235 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700236 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400237 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400238 mZenModeController = zenModeController;
239 mZenModeController.addCallback(new ZenModeController.Callback() {
240 @Override
241 public void onZenChanged(int zen) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700242 if (mStackView != null) {
243 mStackView.updateDots();
244 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400245 }
246
247 @Override
248 public void onConfigChanged(ZenModeConfig config) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700249 if (mStackView != null) {
250 mStackView.updateDots();
251 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400252 }
253 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700254
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700255 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800256
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400257 mBubbleData = data;
258 mBubbleData.setListener(mBubbleDataListener);
259
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800260 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500261 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700262 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellor22f2f072019-04-18 13:26:18 -0700263 mNotificationGroupManager = groupManager;
Mady Mellorb4991e62019-01-10 15:14:51 -0800264
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800265 mStatusBarWindowController = statusBarWindowController;
266 mStatusBarStateListener = new StatusBarStateListener();
267 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500268
Mark Renoufcecc77b2019-01-30 16:32:24 -0500269 mTaskStackListener = new BubbleTaskStackListener();
270 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800271
Joshua Tsujia19515f2019-02-13 18:02:29 -0500272 try {
273 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
274 } catch (RemoteException e) {
275 e.printStackTrace();
276 }
Issei Suzukic0387542019-03-08 17:31:14 +0100277 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700278
279 mBarService = IStatusBarService.Stub.asInterface(
280 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400281
282 mSavedBubbleKeysPerUser = new SparseSetArray<>();
283 mCurrentUserId = mNotifUserManager.getCurrentUserId();
284 mNotifUserManager.addUserChangedListener(
285 newUserId -> {
286 saveBubbles(mCurrentUserId);
287 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
288 restoreBubbles(newUserId);
289 mCurrentUserId = newUserId;
290 });
Mady Mellor5549dd22018-11-06 18:07:34 -0800291 }
292
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400293 /**
294 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
295 * method initializes the stack view and adds it to the StatusBar just above the scrim.
296 */
297 private void ensureStackViewCreated() {
298 if (mStackView == null) {
299 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
300 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Lyn Hanbde48202019-05-29 19:18:29 -0700301 int bubbleScrimIndex = sbv.indexOfChild(sbv.findViewById(R.id.scrim_for_bubble));
302 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
303 sbv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400304 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
305 if (mExpandListener != null) {
306 mStackView.setExpandListener(mExpandListener);
307 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400308 }
309 }
310
Mark Renoufc19b4732019-06-26 12:08:33 -0400311 /**
312 * Records the notification key for any active bubbles. These are used to restore active
313 * bubbles when the user returns to the foreground.
314 *
315 * @param userId the id of the user
316 */
317 private void saveBubbles(@UserIdInt int userId) {
318 // First clear any existing keys that might be stored.
319 mSavedBubbleKeysPerUser.remove(userId);
320 // Add in all active bubbles for the current user.
321 for (Bubble bubble: mBubbleData.getBubbles()) {
322 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
323 }
324 }
325
326 /**
327 * Promotes existing notifications to Bubbles if they were previously bubbles.
328 *
329 * @param userId the id of the user
330 */
331 private void restoreBubbles(@UserIdInt int userId) {
332 NotificationData notificationData =
333 mNotificationEntryManager.getNotificationData();
334 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
335 if (savedBubbleKeys == null) {
336 // There were no bubbles saved for this used.
337 return;
338 }
339 for (NotificationEntry e : notificationData.getNotificationsForCurrentUser()) {
340 if (savedBubbleKeys.contains(e.key)
341 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
342 && canLaunchInActivityView(mContext, e)) {
343 updateBubble(e, /* suppressFlyout= */ true);
344 }
345 }
346 // Finally, remove the entries for this user now that bubbles are restored.
347 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
348 }
349
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700350 @Override
351 public void onUiModeChanged() {
352 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700353 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700354 }
355 }
356
357 @Override
358 public void onOverlayChanged() {
359 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700360 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700361 }
362 }
363
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400364 @Override
365 public void onConfigChanged(Configuration newConfig) {
366 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400367 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700368 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400369 }
370 }
371
Mady Mellor5549dd22018-11-06 18:07:34 -0800372 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800373 * Set a listener to be notified when some states of the bubbles change.
374 */
375 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
376 mStateChangeListener = listener;
377 }
378
379 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800380 * Set a listener to be notified of bubble expand events.
381 */
382 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100383 mExpandListener = ((isExpanding, key) -> {
384 if (listener != null) {
385 listener.onBubbleExpandChanged(isExpanding, key);
386 }
387 mStatusBarWindowController.setBubbleExpanded(isExpanding);
388 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800389 if (mStackView != null) {
390 mStackView.setExpandListener(mExpandListener);
391 }
392 }
393
394 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800395 * Whether or not there are bubbles present, regardless of them being visible on the
396 * screen (e.g. if on AOD).
397 */
398 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800399 if (mStackView == null) {
400 return false;
401 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400402 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800403 }
404
405 /**
406 * Whether the stack of bubbles is expanded or not.
407 */
408 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400409 return mBubbleData.isExpanded();
410 }
411
412 /**
413 * Tell the stack of bubbles to expand.
414 */
415 public void expandStack() {
416 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800417 }
418
419 /**
420 * Tell the stack of bubbles to collapse.
421 */
422 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400423 mBubbleData.setExpanded(false /* expanded */);
424 }
425
Mady Mellorce23c462019-06-17 17:30:07 -0700426 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700427 * True if either:
428 * (1) There is a bubble associated with the provided key and if its notification is hidden
429 * from the shade.
430 * (2) There is a group summary associated with the provided key that is hidden from the shade
431 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700432 *
Mady Mellore28fe102019-07-09 15:33:32 -0700433 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700434 */
435 public boolean isBubbleNotificationSuppressedFromShade(String key) {
Mady Mellore28fe102019-07-09 15:33:32 -0700436 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorce23c462019-06-17 17:30:07 -0700437 && !mBubbleData.getBubbleWithKey(key).showInShadeWhenBubble();
Mady Mellore28fe102019-07-09 15:33:32 -0700438 NotificationEntry entry = mNotificationEntryManager.getNotificationData().get(key);
439 String groupKey = entry != null ? entry.notification.getGroupKey() : null;
440 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
441 return isSuppressedSummary || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700442 }
443
Mark Renouf71a3af62019-04-08 15:02:54 -0400444 void selectBubble(Bubble bubble) {
445 mBubbleData.setSelectedBubble(bubble);
446 }
447
448 @VisibleForTesting
449 void selectBubble(String key) {
450 Bubble bubble = mBubbleData.getBubbleWithKey(key);
451 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800452 }
453
454 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400455 * Request the stack expand if needed, then select the specified Bubble as current.
456 *
457 * @param notificationKey the notification key for the bubble to be selected
458 */
459 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400460 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
461 if (bubble != null) {
462 mBubbleData.setSelectedBubble(bubble);
463 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400464 }
465 }
466
467 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800468 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
469 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500470 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400471 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800472 }
473
474 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500475 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
476 * is forwarded a back key down/up pair.
477 */
478 public void performBackPressIfNeeded() {
479 if (mStackView != null) {
480 mStackView.performBackPressIfNeeded();
481 }
482 }
483
484 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800485 * Adds or updates a bubble associated with the provided notification entry.
486 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400487 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800488 */
Mark Renouff97ed462019-04-05 13:46:24 -0400489 void updateBubble(NotificationEntry notif) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400490 updateBubble(notif, /* supressFlyout */ false);
491 }
492
493 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700494 // If this is an interruptive notif, mark that it's interrupted
495 if (notif.importance >= NotificationManager.IMPORTANCE_HIGH) {
496 notif.setInterruption();
497 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400498 mBubbleData.notificationEntryUpdated(notif, suppressFlyout);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800499 }
500
501 /**
502 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500503 * <p>
504 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800505 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500506 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500507 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400508 // TEMP: refactor to change this to pass entry
509 Bubble bubble = mBubbleData.getBubbleWithKey(key);
510 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700511 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800512 }
513 }
514
Ned Burns01e38212019-01-03 16:32:52 -0500515 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700516 private final NotificationRemoveInterceptor mRemoveInterceptor =
517 new NotificationRemoveInterceptor() {
518 @Override
519 public boolean onNotificationRemoveRequested(String key, int reason) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700520 NotificationEntry entry = mNotificationEntryManager.getNotificationData().get(key);
521 String groupKey = entry != null ? entry.notification.getGroupKey() : null;
522 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
523
524 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
Mady Mellore28fe102019-07-09 15:33:32 -0700525 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
526 && mBubbleData.getSummaryKey(groupKey).equals(key));
Mady Mellor22f2f072019-04-18 13:26:18 -0700527 boolean isSummary = entry != null
528 && entry.notification.getNotification().isGroupSummary();
Mady Mellore28fe102019-07-09 15:33:32 -0700529 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
530 && bubbleChildren != null && !bubbleChildren.isEmpty();
Mady Mellor22f2f072019-04-18 13:26:18 -0700531
532 if (!inBubbleData && !isSummaryOfBubbles) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700533 return false;
534 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700535
536 final boolean isClearAll = reason == REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700537 final boolean isUserDimiss = reason == REASON_CANCEL || reason == REASON_CLICK;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700538 final boolean isAppCancel = reason == REASON_APP_CANCEL
539 || reason == REASON_APP_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700540 final boolean isSummaryCancel = reason == REASON_GROUP_SUMMARY_CANCELED;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700541
542 // Need to check for !appCancel here because the notification may have
543 // previously been dismissed & entry.isRowDismissed would still be true
544 boolean userRemovedNotif = (entry.isRowDismissed() && !isAppCancel)
Mady Mellor22f2f072019-04-18 13:26:18 -0700545 || isClearAll || isUserDimiss || isSummaryCancel;
546
547 if (isSummaryOfBubbles) {
548 return handleSummaryRemovalInterception(entry, userRemovedNotif);
549 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700550
551 // The bubble notification sticks around in the data as long as the bubble is
552 // not dismissed and the app hasn't cancelled the notification.
Mady Mellor22f2f072019-04-18 13:26:18 -0700553 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mark Renoufc19b4732019-06-26 12:08:33 -0400554 boolean bubbleExtended = entry.isBubble() && userRemovedNotif;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700555 if (bubbleExtended) {
Mady Mellorce23c462019-06-17 17:30:07 -0700556 bubble.setShowInShadeWhenBubble(false);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700557 bubble.setShowBubbleDot(false);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700558 if (mStackView != null) {
559 mStackView.updateDotVisibility(entry.key);
560 }
561 mNotificationEntryManager.updateNotifications();
562 return true;
Mark Renoufc19b4732019-06-26 12:08:33 -0400563 } else if (!userRemovedNotif) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700564 // This wasn't a user removal so we should remove the bubble as well
565 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
566 return false;
567 }
568 return false;
569 }
570 };
571
Mady Mellor22f2f072019-04-18 13:26:18 -0700572 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
573 boolean userRemovedNotif) {
574 String groupKey = summary.notification.getGroupKey();
575 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
576
577 if (userRemovedNotif) {
578 // If it's a user dismiss we mark the children to be hidden from the shade.
579 for (int i = 0; i < bubbleChildren.size(); i++) {
580 Bubble bubbleChild = bubbleChildren.get(i);
581 // As far as group manager is concerned, once a child is no longer shown
582 // in the shade, it is essentially removed.
583 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
584 bubbleChild.setShowInShadeWhenBubble(false);
585 bubbleChild.setShowBubbleDot(false);
586 if (mStackView != null) {
587 mStackView.updateDotVisibility(bubbleChild.getKey());
588 }
589 }
590 // And since all children are removed, remove the summary.
591 mNotificationGroupManager.onEntryRemoved(summary);
592
593 // If the summary was auto-generated we don't need to keep that notification around
594 // because apps can't cancel it; so we only intercept & suppress real summaries.
595 boolean isAutogroupSummary = (summary.notification.getNotification().flags
596 & FLAG_AUTOGROUP_SUMMARY) != 0;
Mady Mellore28fe102019-07-09 15:33:32 -0700597 if (!isAutogroupSummary) {
598 mBubbleData.addSummaryToSuppress(summary.notification.getGroupKey(),
599 summary.key);
600 // Tell shade to update for the suppression
601 mNotificationEntryManager.updateNotifications();
602 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700603 return !isAutogroupSummary;
604 } else {
Mady Mellore28fe102019-07-09 15:33:32 -0700605 // If it's not a user dismiss it's a cancel.
606 mBubbleData.removeSuppressedSummary(groupKey);
607
Mady Mellor22f2f072019-04-18 13:26:18 -0700608 // Remove any associated bubble children.
609 for (int i = 0; i < bubbleChildren.size(); i++) {
610 Bubble bubbleChild = bubbleChildren.get(i);
611 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
612 DISMISS_GROUP_CANCELLED);
613 }
614 return false;
615 }
616 }
617
Mady Mellorc2ff0112019-03-28 14:18:06 -0700618 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500619 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
620 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500621 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800622 if (!areBubblesEnabled(mContext)) {
623 return;
624 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700625 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
626 && canLaunchInActivityView(mContext, entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400627 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800628 }
629 }
630
631 @Override
632 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800633 if (!areBubblesEnabled(mContext)) {
634 return;
635 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700636 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
637 && canLaunchInActivityView(mContext, entry);
Mady Melloraa8fef22019-04-11 13:36:40 -0700638 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.key)) {
639 // It was previously a bubble but no longer a bubble -- lets remove it
640 removeBubble(entry.key, DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700641 } else if (shouldBubble) {
Mady Mellored99c272019-06-13 15:58:30 -0700642 Bubble b = mBubbleData.getBubbleWithKey(entry.key);
Mark Renouff97ed462019-04-05 13:46:24 -0400643 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800644 }
645 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400646
647 @Override
648 public void onNotificationRankingUpdated(RankingMap rankingMap) {
649 // Forward to BubbleData to block any bubbles which should no longer be shown
650 mBubbleData.notificationRankingUpdated(rankingMap);
651 }
Ned Burns01e38212019-01-03 16:32:52 -0500652 };
653
Mark Renouf71a3af62019-04-08 15:02:54 -0400654 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400655 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400656
Mark Renouf3bc5b362019-04-05 14:37:59 -0400657 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400658 public void applyUpdate(BubbleData.Update update) {
659 if (mStackView == null && update.addedBubble != null) {
660 // Lazy init stack view when the first bubble is added.
661 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400662 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400663
664 // If not yet initialized, ignore all other changes.
665 if (mStackView == null) {
666 return;
667 }
668
669 if (update.addedBubble != null) {
670 mStackView.addBubble(update.addedBubble);
671 }
672
673 // Collapsing? Do this first before remaining steps.
674 if (update.expandedChanged && !update.expanded) {
675 mStackView.setExpanded(false);
676 }
677
678 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700679 ArrayList<Pair<Bubble, Integer>> removedBubbles =
680 new ArrayList<>(update.removedBubbles);
681 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400682 final Bubble bubble = removed.first;
683 @DismissReason final int reason = removed.second;
684 mStackView.removeBubble(bubble);
685
Mark Renoufc19b4732019-06-26 12:08:33 -0400686 // If the bubble is removed for user switching, leave the notification in place.
687 if (reason != DISMISS_USER_CHANGED) {
688 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
689 && !bubble.showInShadeWhenBubble()) {
690 // The bubble is gone & the notification is gone, time to actually remove it
691 mNotificationEntryManager.performRemoveNotification(
692 bubble.getEntry().notification, UNDEFINED_DISMISS_REASON);
693 } else {
694 // Update the flag for SysUI
695 bubble.getEntry().notification.getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700696
Mark Renoufc19b4732019-06-26 12:08:33 -0400697 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
698 // will get right result back
699 try {
700 mBarService.onNotificationBubbleChanged(bubble.getKey(),
701 false /* isBubble */);
702 } catch (RemoteException e) {
703 // Bad things have happened
704 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400705 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700706
Mady Mellore28fe102019-07-09 15:33:32 -0700707 // Check if removed bubble has an associated suppressed group summary that needs
708 // to be removed now.
709 final String groupKey = bubble.getEntry().notification.getGroupKey();
710 if (mBubbleData.isSummarySuppressed(groupKey)
711 && mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
712 // Time to actually remove the summary.
713 String notifKey = mBubbleData.getSummaryKey(groupKey);
714 mBubbleData.removeSuppressedSummary(groupKey);
715 NotificationEntry entry =
716 mNotificationEntryManager.getNotificationData().get(notifKey);
717 if (entry == null) {
718 Log.w("mady", "WTF summary isn't in data... " + notifKey);
719 return;
720 }
721 mNotificationEntryManager.performRemoveNotification(
722 entry.notification, UNDEFINED_DISMISS_REASON);
723 }
724
Mady Mellor22f2f072019-04-18 13:26:18 -0700725 // Check if summary should be removed from NoManGroup
726 NotificationEntry summary = mNotificationGroupManager.getLogicalGroupSummary(
727 bubble.getEntry().notification);
728 if (summary != null) {
729 ArrayList<NotificationEntry> summaryChildren =
730 mNotificationGroupManager.getLogicalChildren(summary.notification);
731 if (summaryChildren == null || summaryChildren.isEmpty()) {
732 mNotificationEntryManager.performRemoveNotification(
733 summary.notification, UNDEFINED_DISMISS_REASON);
734 }
735 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700736 }
737 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400738
Mark Renouf82a40e62019-05-23 16:16:24 -0400739 if (update.updatedBubble != null) {
740 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400741 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400742
Mark Renouf82a40e62019-05-23 16:16:24 -0400743 if (update.orderChanged) {
744 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400745 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400746
Mark Renouf82a40e62019-05-23 16:16:24 -0400747 if (update.selectionChanged) {
748 mStackView.setSelectedBubble(update.selectedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400749 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400750
Mark Renouf82a40e62019-05-23 16:16:24 -0400751 // Expanding? Apply this last.
752 if (update.expandedChanged && update.expanded) {
753 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400754 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400755
Mark Renouf71a3af62019-04-08 15:02:54 -0400756 mNotificationEntryManager.updateNotifications();
Lyn Han6c40fe72019-05-08 14:06:33 -0700757 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400758
Issei Suzukia8d07312019-06-07 12:56:19 +0200759 if (DEBUG_BUBBLE_CONTROLLER) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400760 Log.d(TAG, "[BubbleData]");
761 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
762 mBubbleData.getSelectedBubble()));
763
764 if (mStackView != null) {
765 Log.d(TAG, "[BubbleStackView]");
766 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
767 mStackView.getExpandedBubble()));
768 }
769 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400770 }
771 };
772
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800773 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400774 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700775 * Updates the visibility of the bubbles based on current state.
776 * Does not un-bubble, just hides or un-hides. Notifies any
777 * {@link BubbleStateChangeListener}s of visibility changes.
778 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800779 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700780 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800781 if (mStackView == null) {
782 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800783 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800784 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
785 // Bubbles only appear in unlocked shade
786 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
787 } else if (mStackView != null) {
788 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800789 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700790
791 // Let listeners know if bubble state changed.
792 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
793 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
794 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
795 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
796 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
797 }
798
799 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800800 }
801
802 /**
803 * Rect indicating the touchable region for the bubble stack / expanded stack.
804 */
805 public Rect getTouchableRegion() {
806 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
807 return null;
808 }
809 mStackView.getBoundsOnScreen(mTempRect);
810 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800811 }
812
Mady Mellor390bff42019-04-05 15:09:01 -0700813 /**
814 * The display id of the expanded view, if the stack is expanded and not occluded by the
815 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
816 */
817 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200818 final Bubble bubble = getExpandedBubble(context);
819 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
820 }
821
822 @Nullable
823 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700824 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200825 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700826 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200827 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700828 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200829 final Bubble expandedBubble = mStackView.getExpandedBubble();
830 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700831 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200832 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700833 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200834 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700835 }
836
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800837 @VisibleForTesting
838 BubbleStackView getStackView() {
839 return mStackView;
840 }
841
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700842 /**
843 * Description of current bubble state.
844 */
845 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
846 pw.println("BubbleController state:");
847 mBubbleData.dump(fd, pw, args);
848 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -0400849 if (mStackView != null) {
850 mStackView.dump(fd, pw, args);
851 }
852 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700853 }
854
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400855 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
856 StringBuilder sb = new StringBuilder();
857 for (Bubble bubble : bubbles) {
858 if (bubble == null) {
859 sb.append(" <null> !!!!!\n");
860 } else {
861 boolean isSelected = (bubble == selected);
862 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
863 ((isSelected) ? "->" : " "),
864 bubble.getLastActivity(),
865 (bubble.isOngoing() ? 1 : 0),
866 bubble.getKey()));
867 }
868 }
869 return sb.toString();
870 }
871
Mady Mellore80930e2019-03-21 16:00:45 -0700872 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -0500873 * This task stack listener is responsible for responding to tasks moved to the front
874 * which are on the default (main) display. When this happens, expanded bubbles must be
875 * collapsed so the user may interact with the app which was just moved to the front.
876 * <p>
877 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
878 * these calls via a main thread Handler.
879 */
880 @MainThread
881 private class BubbleTaskStackListener extends TaskStackChangeListener {
882
Mark Renoufcecc77b2019-01-30 16:32:24 -0500883 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500884 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
885 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400886 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500887 }
888 }
889
Mark Renoufcecc77b2019-01-30 16:32:24 -0500890 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500891 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500892 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400893 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500894 }
895 }
Mark Renouf446251d2019-04-26 10:22:41 -0400896
897 @Override
898 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
899 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
900 mBubbleData.setExpanded(false);
901 }
902 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200903
904 @Override
905 public void onSingleTaskDisplayDrawn(int displayId) {
906 final Bubble expandedBubble = getExpandedBubble(mContext);
907 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
908 expandedBubble.setContentVisibility(true);
909 }
910 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200911
912 @Override
913 public void onSingleTaskDisplayEmpty(int displayId) {
914 final Bubble expandedBubble = getExpandedBubble(mContext);
915 if (expandedBubble == null) {
916 return;
917 }
918 if (expandedBubble.getDisplayId() == displayId) {
919 mBubbleData.setExpanded(false);
920 }
Mady Mellored99c272019-06-13 15:58:30 -0700921 if (expandedBubble.getExpandedView() != null) {
922 expandedBubble.getExpandedView().notifyDisplayEmpty();
Issei Suzuki734bc942019-06-05 13:59:52 +0200923 }
924 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500925 }
926
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800927 private static boolean areBubblesEnabled(Context context) {
928 return Settings.Secure.getInt(context.getContentResolver(),
929 ENABLE_BUBBLES, 1) != 0;
930 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500931
Mady Mellorca0c24c2019-05-16 16:14:32 -0700932 /**
933 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
934 *
935 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
936 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
937 *
938 * @param context the context to use.
939 * @param entry the entry to bubble.
940 */
941 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
942 PendingIntent intent = entry.getBubbleMetadata() != null
943 ? entry.getBubbleMetadata().getIntent()
944 : null;
945 if (intent == null) {
946 Log.w(TAG, "Unable to create bubble -- no intent");
947 return false;
948 }
949 ActivityInfo info =
950 intent.getIntent().resolveActivityInfo(context.getPackageManager(), 0);
951 if (info == null) {
952 Log.w(TAG, "Unable to send as bubble -- couldn't find activity info for intent: "
953 + intent);
954 return false;
955 }
956 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
957 Log.w(TAG, "Unable to send as bubble -- activity is not resizable for intent: "
958 + intent);
959 return false;
960 }
961 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
962 Log.w(TAG, "Unable to send as bubble -- activity is not documentLaunchMode=always "
963 + "for intent: " + intent);
964 return false;
965 }
966 if ((info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
967 Log.w(TAG, "Unable to send as bubble -- activity is not embeddable for intent: "
968 + intent);
969 return false;
970 }
971 return true;
972 }
973
Joshua Tsujia19515f2019-02-13 18:02:29 -0500974 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
975 private class BubblesImeListener extends IPinnedStackListener.Stub {
976
977 @Override
978 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
979 }
980
981 @Override
982 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
983 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
984 int displayRotation) throws RemoteException {}
985
986 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500987 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
988 if (mStackView != null && mStackView.getBubbleCount() > 0) {
989 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500990 }
991 }
992
993 @Override
994 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
995 throws RemoteException {}
996
997 @Override
998 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
999
1000 @Override
1001 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
1002 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001003}