blob: 7ef22c206dd37320ca369425b530872027ae5aa3 [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 Mellor3a0a1b42019-05-23 06:40:21 -070019import static android.app.Notification.FLAG_BUBBLE;
Mady Mellorca0c24c2019-05-16 16:14:32 -070020import static android.content.pm.ActivityInfo.DOCUMENT_LAUNCH_ALWAYS;
Mady Mellorc2ff0112019-03-28 14:18:06 -070021import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL;
22import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL;
23import static android.service.notification.NotificationListenerService.REASON_CANCEL;
24import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL;
Mady Mellor390bff42019-04-05 15:09:01 -070025import static android.view.Display.DEFAULT_DISPLAY;
26import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080027import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080028import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080029import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080030
Issei Suzukia8d07312019-06-07 12:56:19 +020031import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_CONTROLLER;
32import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
33import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080034import static com.android.systemui.statusbar.StatusBarState.SHADE;
Mady Mellor1a4e86f2019-05-03 16:07:23 -070035import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080036
Mark Renoufba5ab512019-05-02 15:21:01 -040037import static java.lang.annotation.ElementType.FIELD;
38import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
39import static java.lang.annotation.ElementType.PARAMETER;
Mark Renouf08bc42a2019-03-07 13:01:59 -050040import static java.lang.annotation.RetentionPolicy.SOURCE;
41
Mark Renoufc19b4732019-06-26 12:08:33 -040042import android.annotation.UserIdInt;
Mark Renoufc808f062019-02-07 15:20:37 -050043import android.app.ActivityManager.RunningTaskInfo;
Mady Mellor66efd5e2019-05-15 13:38:11 -070044import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070045import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080046import android.content.Context;
Mady Mellorca0c24c2019-05-16 16:14:32 -070047import android.content.pm.ActivityInfo;
Joshua Tsujia19515f2019-02-13 18:02:29 -050048import android.content.pm.ParceledListSlice;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040049import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080050import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050051import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080052import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080053import android.provider.Settings;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040054import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040055import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040056import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040057import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040058import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040059import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050060import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050061import android.view.IPinnedStackController;
62import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080063import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080064import android.widget.FrameLayout;
65
Mark Renouf08bc42a2019-03-07 13:01:59 -050066import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050067import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040068import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050069
Mady Mellorebdbbb92018-11-15 14:36:48 -080070import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070071import com.android.internal.statusbar.IStatusBarService;
Ned Burns01e38212019-01-03 16:32:52 -050072import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080073import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050074import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050075import com.android.systemui.shared.system.ActivityManagerWrapper;
76import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050077import com.android.systemui.shared.system.WindowManagerWrapper;
Mark Renoufc19b4732019-06-26 12:08:33 -040078import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070079import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050080import com.android.systemui.statusbar.notification.NotificationEntryListener;
81import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080082import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -040083import com.android.systemui.statusbar.notification.collection.NotificationData;
Ned Burnsf81c4c42019-01-07 14:10:43 -050084import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080085import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070086import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040087import com.android.systemui.statusbar.policy.ZenModeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080088
Mady Mellor70cba7bb2019-07-02 15:06:07 -070089import java.io.FileDescriptor;
90import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -050091import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -040092import java.lang.annotation.Target;
Mady Mellore80930e2019-03-21 16:00:45 -070093import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050094
Jason Monk27d01a622018-12-10 15:57:09 -050095import javax.inject.Inject;
96import javax.inject.Singleton;
97
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080098/**
99 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
100 * Bubbles can be expanded to show more content.
101 *
102 * The controller manages addition, removal, and visible state of bubbles on screen.
103 */
Jason Monk27d01a622018-12-10 15:57:09 -0500104@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -0400105public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800106
Issei Suzukia8d07312019-06-07 12:56:19 +0200107 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800108
Mark Renouf08bc42a2019-03-07 13:01:59 -0500109 @Retention(SOURCE)
110 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400111 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
112 DISMISS_USER_CHANGED})
Mark Renoufba5ab512019-05-02 15:21:01 -0400113 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500114 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700115
Mark Renouf08bc42a2019-03-07 13:01:59 -0500116 static final int DISMISS_USER_GESTURE = 1;
117 static final int DISMISS_AGED = 2;
118 static final int DISMISS_TASK_FINISHED = 3;
119 static final int DISMISS_BLOCKED = 4;
120 static final int DISMISS_NOTIF_CANCEL = 5;
121 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700122 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400123 static final int DISMISS_USER_CHANGED = 8;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500124
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400125 public static final int MAX_BUBBLES = 5; // TODO: actually enforce this
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -0400126
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800127 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800128 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500129
Ned Burns01e38212019-01-03 16:32:52 -0500130 private final Context mContext;
131 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500132 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800133 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800134 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100135 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800136
Mady Mellor3dff9e62019-02-05 18:12:53 -0800137 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400138 @Nullable private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800139
Mark Renoufc19b4732019-06-26 12:08:33 -0400140 // Tracks the id of the current (foreground) user.
141 private int mCurrentUserId;
142 // Saves notification keys of active bubbles when users are switched.
143 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
144
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800145 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500146 private final StatusBarWindowController mStatusBarWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400147 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800148 private StatusBarStateListener mStatusBarStateListener;
149
Mady Melloraa8fef22019-04-11 13:36:40 -0700150 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700151 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800152
Mady Mellord1c78b262018-11-06 18:04:40 -0800153 // Used for determining view rect for touch interaction
154 private Rect mTempRect = new Rect();
155
Mark Renoufc19b4732019-06-26 12:08:33 -0400156 // Listens to user switch so bubbles can be saved and restored.
157 private final NotificationLockscreenUserManager mNotifUserManager;
158
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400159 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
160 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
161
Mady Mellor5549dd22018-11-06 18:07:34 -0800162 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800163 * Listener to be notified when some states of the bubbles change.
164 */
165 public interface BubbleStateChangeListener {
166 /**
167 * Called when the stack has bubbles or no longer has bubbles.
168 */
169 void onHasBubblesChanged(boolean hasBubbles);
170 }
171
Mady Mellorcd9b1302018-11-06 18:08:04 -0800172 /**
173 * Listener to find out about stack expansion / collapse events.
174 */
175 public interface BubbleExpandListener {
176 /**
177 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700178 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800179 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800180 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800181 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800182 void onBubbleExpandChanged(boolean isExpanding, String key);
183 }
184
185 /**
186 * Listens for the current state of the status bar and updates the visibility state
187 * of bubbles as needed.
188 */
189 private class StatusBarStateListener implements StatusBarStateController.StateListener {
190 private int mState;
191 /**
192 * Returns the current status bar state.
193 */
194 public int getCurrentState() {
195 return mState;
196 }
197
198 @Override
199 public void onStateChanged(int newState) {
200 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400201 boolean shouldCollapse = (mState != SHADE);
202 if (shouldCollapse) {
203 collapseStack();
204 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700205 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800206 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800207 }
208
Jason Monk27d01a622018-12-10 15:57:09 -0500209 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800210 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Mady Melloraa8fef22019-04-11 13:36:40 -0700211 BubbleData data, ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400212 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400213 ZenModeController zenModeController,
214 NotificationLockscreenUserManager notifUserManager) {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700215 this(context, statusBarWindowController, data, null /* synchronizer */,
Mark Renoufc19b4732019-06-26 12:08:33 -0400216 configurationController, interruptionStateProvider, zenModeController,
217 notifUserManager);
Issei Suzukic0387542019-03-08 17:31:14 +0100218 }
219
220 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700221 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
Mady Melloraa8fef22019-04-11 13:36:40 -0700222 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400223 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400224 ZenModeController zenModeController,
225 NotificationLockscreenUserManager notifUserManager) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800226 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700227 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400228 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400229 mZenModeController = zenModeController;
230 mZenModeController.addCallback(new ZenModeController.Callback() {
231 @Override
232 public void onZenChanged(int zen) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700233 if (mStackView != null) {
234 mStackView.updateDots();
235 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400236 }
237
238 @Override
239 public void onConfigChanged(ZenModeConfig config) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700240 if (mStackView != null) {
241 mStackView.updateDots();
242 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400243 }
244 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700245
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700246 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800247
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400248 mBubbleData = data;
249 mBubbleData.setListener(mBubbleDataListener);
250
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800251 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500252 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700253 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellorb4991e62019-01-10 15:14:51 -0800254
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800255 mStatusBarWindowController = statusBarWindowController;
256 mStatusBarStateListener = new StatusBarStateListener();
257 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500258
Mark Renoufcecc77b2019-01-30 16:32:24 -0500259 mTaskStackListener = new BubbleTaskStackListener();
260 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800261
Joshua Tsujia19515f2019-02-13 18:02:29 -0500262 try {
263 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
264 } catch (RemoteException e) {
265 e.printStackTrace();
266 }
Issei Suzukic0387542019-03-08 17:31:14 +0100267 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700268
269 mBarService = IStatusBarService.Stub.asInterface(
270 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400271
272 mSavedBubbleKeysPerUser = new SparseSetArray<>();
273 mCurrentUserId = mNotifUserManager.getCurrentUserId();
274 mNotifUserManager.addUserChangedListener(
275 newUserId -> {
276 saveBubbles(mCurrentUserId);
277 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
278 restoreBubbles(newUserId);
279 mCurrentUserId = newUserId;
280 });
Mady Mellor5549dd22018-11-06 18:07:34 -0800281 }
282
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400283 /**
284 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
285 * method initializes the stack view and adds it to the StatusBar just above the scrim.
286 */
287 private void ensureStackViewCreated() {
288 if (mStackView == null) {
289 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
290 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Lyn Hanbde48202019-05-29 19:18:29 -0700291 int bubbleScrimIndex = sbv.indexOfChild(sbv.findViewById(R.id.scrim_for_bubble));
292 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
293 sbv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400294 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
295 if (mExpandListener != null) {
296 mStackView.setExpandListener(mExpandListener);
297 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400298 }
299 }
300
Mark Renoufc19b4732019-06-26 12:08:33 -0400301 /**
302 * Records the notification key for any active bubbles. These are used to restore active
303 * bubbles when the user returns to the foreground.
304 *
305 * @param userId the id of the user
306 */
307 private void saveBubbles(@UserIdInt int userId) {
308 // First clear any existing keys that might be stored.
309 mSavedBubbleKeysPerUser.remove(userId);
310 // Add in all active bubbles for the current user.
311 for (Bubble bubble: mBubbleData.getBubbles()) {
312 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
313 }
314 }
315
316 /**
317 * Promotes existing notifications to Bubbles if they were previously bubbles.
318 *
319 * @param userId the id of the user
320 */
321 private void restoreBubbles(@UserIdInt int userId) {
322 NotificationData notificationData =
323 mNotificationEntryManager.getNotificationData();
324 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
325 if (savedBubbleKeys == null) {
326 // There were no bubbles saved for this used.
327 return;
328 }
329 for (NotificationEntry e : notificationData.getNotificationsForCurrentUser()) {
330 if (savedBubbleKeys.contains(e.key)
331 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
332 && canLaunchInActivityView(mContext, e)) {
333 updateBubble(e, /* suppressFlyout= */ true);
334 }
335 }
336 // Finally, remove the entries for this user now that bubbles are restored.
337 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
338 }
339
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700340 @Override
341 public void onUiModeChanged() {
342 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700343 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700344 }
345 }
346
347 @Override
348 public void onOverlayChanged() {
349 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700350 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700351 }
352 }
353
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400354 @Override
355 public void onConfigChanged(Configuration newConfig) {
356 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400357 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700358 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400359 }
360 }
361
Mady Mellor5549dd22018-11-06 18:07:34 -0800362 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800363 * Set a listener to be notified when some states of the bubbles change.
364 */
365 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
366 mStateChangeListener = listener;
367 }
368
369 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800370 * Set a listener to be notified of bubble expand events.
371 */
372 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100373 mExpandListener = ((isExpanding, key) -> {
374 if (listener != null) {
375 listener.onBubbleExpandChanged(isExpanding, key);
376 }
377 mStatusBarWindowController.setBubbleExpanded(isExpanding);
378 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800379 if (mStackView != null) {
380 mStackView.setExpandListener(mExpandListener);
381 }
382 }
383
384 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800385 * Whether or not there are bubbles present, regardless of them being visible on the
386 * screen (e.g. if on AOD).
387 */
388 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800389 if (mStackView == null) {
390 return false;
391 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400392 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800393 }
394
395 /**
396 * Whether the stack of bubbles is expanded or not.
397 */
398 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400399 return mBubbleData.isExpanded();
400 }
401
402 /**
403 * Tell the stack of bubbles to expand.
404 */
405 public void expandStack() {
406 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800407 }
408
409 /**
410 * Tell the stack of bubbles to collapse.
411 */
412 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400413 mBubbleData.setExpanded(false /* expanded */);
414 }
415
Mady Mellorce23c462019-06-17 17:30:07 -0700416 /**
417 * Whether (1) there is a bubble associated with the provided key and
418 * (2) if the notification for that bubble is hidden from the shade.
419 *
420 * False if there isn't a bubble or if the notification for that bubble appears in the shade.
421 */
422 public boolean isBubbleNotificationSuppressedFromShade(String key) {
423 return mBubbleData.hasBubbleWithKey(key)
424 && !mBubbleData.getBubbleWithKey(key).showInShadeWhenBubble();
425 }
426
Mark Renouf71a3af62019-04-08 15:02:54 -0400427 void selectBubble(Bubble bubble) {
428 mBubbleData.setSelectedBubble(bubble);
429 }
430
431 @VisibleForTesting
432 void selectBubble(String key) {
433 Bubble bubble = mBubbleData.getBubbleWithKey(key);
434 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800435 }
436
437 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400438 * Request the stack expand if needed, then select the specified Bubble as current.
439 *
440 * @param notificationKey the notification key for the bubble to be selected
441 */
442 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400443 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
444 if (bubble != null) {
445 mBubbleData.setSelectedBubble(bubble);
446 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400447 }
448 }
449
450 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800451 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
452 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500453 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400454 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800455 }
456
457 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500458 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
459 * is forwarded a back key down/up pair.
460 */
461 public void performBackPressIfNeeded() {
462 if (mStackView != null) {
463 mStackView.performBackPressIfNeeded();
464 }
465 }
466
467 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800468 * Adds or updates a bubble associated with the provided notification entry.
469 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400470 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800471 */
Mark Renouff97ed462019-04-05 13:46:24 -0400472 void updateBubble(NotificationEntry notif) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400473 updateBubble(notif, /* supressFlyout */ false);
474 }
475
476 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700477 // If this is an interruptive notif, mark that it's interrupted
478 if (notif.importance >= NotificationManager.IMPORTANCE_HIGH) {
479 notif.setInterruption();
480 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400481 mBubbleData.notificationEntryUpdated(notif, suppressFlyout);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800482 }
483
484 /**
485 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500486 * <p>
487 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800488 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500489 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500490 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400491 // TEMP: refactor to change this to pass entry
492 Bubble bubble = mBubbleData.getBubbleWithKey(key);
493 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700494 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800495 }
496 }
497
Ned Burns01e38212019-01-03 16:32:52 -0500498 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700499 private final NotificationRemoveInterceptor mRemoveInterceptor =
500 new NotificationRemoveInterceptor() {
501 @Override
502 public boolean onNotificationRemoveRequested(String key, int reason) {
503 if (!mBubbleData.hasBubbleWithKey(key)) {
504 return false;
505 }
Mady Mellored99c272019-06-13 15:58:30 -0700506 Bubble bubble = mBubbleData.getBubbleWithKey(key);
507 NotificationEntry entry = bubble.getEntry();
Mady Mellorc2ff0112019-03-28 14:18:06 -0700508
509 final boolean isClearAll = reason == REASON_CANCEL_ALL;
510 final boolean isUserDimiss = reason == REASON_CANCEL;
511 final boolean isAppCancel = reason == REASON_APP_CANCEL
512 || reason == REASON_APP_CANCEL_ALL;
513
514 // Need to check for !appCancel here because the notification may have
515 // previously been dismissed & entry.isRowDismissed would still be true
516 boolean userRemovedNotif = (entry.isRowDismissed() && !isAppCancel)
517 || isClearAll || isUserDimiss;
518
519 // The bubble notification sticks around in the data as long as the bubble is
520 // not dismissed and the app hasn't cancelled the notification.
Mark Renoufc19b4732019-06-26 12:08:33 -0400521 boolean bubbleExtended = entry.isBubble() && userRemovedNotif;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700522 if (bubbleExtended) {
Mady Mellorce23c462019-06-17 17:30:07 -0700523 bubble.setShowInShadeWhenBubble(false);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700524 bubble.setShowBubbleDot(false);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700525 if (mStackView != null) {
526 mStackView.updateDotVisibility(entry.key);
527 }
528 mNotificationEntryManager.updateNotifications();
529 return true;
Mark Renoufc19b4732019-06-26 12:08:33 -0400530 } else if (!userRemovedNotif) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700531 // This wasn't a user removal so we should remove the bubble as well
532 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
533 return false;
534 }
535 return false;
536 }
537 };
538
539 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500540 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
541 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500542 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800543 if (!areBubblesEnabled(mContext)) {
544 return;
545 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700546 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
547 && canLaunchInActivityView(mContext, entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400548 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800549 }
550 }
551
552 @Override
553 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800554 if (!areBubblesEnabled(mContext)) {
555 return;
556 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700557 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
558 && canLaunchInActivityView(mContext, entry);
Mady Melloraa8fef22019-04-11 13:36:40 -0700559 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.key)) {
560 // It was previously a bubble but no longer a bubble -- lets remove it
561 removeBubble(entry.key, DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700562 } else if (shouldBubble) {
Mady Mellored99c272019-06-13 15:58:30 -0700563 Bubble b = mBubbleData.getBubbleWithKey(entry.key);
Mark Renouff97ed462019-04-05 13:46:24 -0400564 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800565 }
566 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400567
568 @Override
569 public void onNotificationRankingUpdated(RankingMap rankingMap) {
570 // Forward to BubbleData to block any bubbles which should no longer be shown
571 mBubbleData.notificationRankingUpdated(rankingMap);
572 }
Ned Burns01e38212019-01-03 16:32:52 -0500573 };
574
Mark Renouf71a3af62019-04-08 15:02:54 -0400575 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400576 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400577
Mark Renouf3bc5b362019-04-05 14:37:59 -0400578 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400579 public void applyUpdate(BubbleData.Update update) {
580 if (mStackView == null && update.addedBubble != null) {
581 // Lazy init stack view when the first bubble is added.
582 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400583 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400584
585 // If not yet initialized, ignore all other changes.
586 if (mStackView == null) {
587 return;
588 }
589
590 if (update.addedBubble != null) {
591 mStackView.addBubble(update.addedBubble);
592 }
593
594 // Collapsing? Do this first before remaining steps.
595 if (update.expandedChanged && !update.expanded) {
596 mStackView.setExpanded(false);
597 }
598
599 // Do removals, if any.
600 for (Pair<Bubble, Integer> removed : update.removedBubbles) {
601 final Bubble bubble = removed.first;
602 @DismissReason final int reason = removed.second;
603 mStackView.removeBubble(bubble);
604
Mark Renoufc19b4732019-06-26 12:08:33 -0400605 // If the bubble is removed for user switching, leave the notification in place.
606 if (reason != DISMISS_USER_CHANGED) {
607 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
608 && !bubble.showInShadeWhenBubble()) {
609 // The bubble is gone & the notification is gone, time to actually remove it
610 mNotificationEntryManager.performRemoveNotification(
611 bubble.getEntry().notification, UNDEFINED_DISMISS_REASON);
612 } else {
613 // Update the flag for SysUI
614 bubble.getEntry().notification.getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700615
Mark Renoufc19b4732019-06-26 12:08:33 -0400616 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
617 // will get right result back
618 try {
619 mBarService.onNotificationBubbleChanged(bubble.getKey(),
620 false /* isBubble */);
621 } catch (RemoteException e) {
622 // Bad things have happened
623 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400624 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700625 }
626 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400627
Mark Renouf82a40e62019-05-23 16:16:24 -0400628 if (update.updatedBubble != null) {
629 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400630 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400631
Mark Renouf82a40e62019-05-23 16:16:24 -0400632 if (update.orderChanged) {
633 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400634 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400635
Mark Renouf82a40e62019-05-23 16:16:24 -0400636 if (update.selectionChanged) {
637 mStackView.setSelectedBubble(update.selectedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400638 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400639
Mark Renouf82a40e62019-05-23 16:16:24 -0400640 // Expanding? Apply this last.
641 if (update.expandedChanged && update.expanded) {
642 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400643 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400644
Mark Renouf71a3af62019-04-08 15:02:54 -0400645 mNotificationEntryManager.updateNotifications();
Lyn Han6c40fe72019-05-08 14:06:33 -0700646 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400647
Issei Suzukia8d07312019-06-07 12:56:19 +0200648 if (DEBUG_BUBBLE_CONTROLLER) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400649 Log.d(TAG, "[BubbleData]");
650 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
651 mBubbleData.getSelectedBubble()));
652
653 if (mStackView != null) {
654 Log.d(TAG, "[BubbleStackView]");
655 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
656 mStackView.getExpandedBubble()));
657 }
658 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400659 }
660 };
661
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800662 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400663 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700664 * Updates the visibility of the bubbles based on current state.
665 * Does not un-bubble, just hides or un-hides. Notifies any
666 * {@link BubbleStateChangeListener}s of visibility changes.
667 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800668 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700669 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800670 if (mStackView == null) {
671 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800672 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800673 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
674 // Bubbles only appear in unlocked shade
675 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
676 } else if (mStackView != null) {
677 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800678 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700679
680 // Let listeners know if bubble state changed.
681 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
682 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
683 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
684 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
685 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
686 }
687
688 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800689 }
690
691 /**
692 * Rect indicating the touchable region for the bubble stack / expanded stack.
693 */
694 public Rect getTouchableRegion() {
695 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
696 return null;
697 }
698 mStackView.getBoundsOnScreen(mTempRect);
699 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800700 }
701
Mady Mellor390bff42019-04-05 15:09:01 -0700702 /**
703 * The display id of the expanded view, if the stack is expanded and not occluded by the
704 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
705 */
706 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200707 final Bubble bubble = getExpandedBubble(context);
708 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
709 }
710
711 @Nullable
712 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700713 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200714 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700715 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200716 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700717 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200718 final Bubble expandedBubble = mStackView.getExpandedBubble();
719 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700720 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200721 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700722 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200723 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700724 }
725
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800726 @VisibleForTesting
727 BubbleStackView getStackView() {
728 return mStackView;
729 }
730
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700731 /**
732 * Description of current bubble state.
733 */
734 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
735 pw.println("BubbleController state:");
736 mBubbleData.dump(fd, pw, args);
737 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -0400738 if (mStackView != null) {
739 mStackView.dump(fd, pw, args);
740 }
741 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700742 }
743
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400744 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
745 StringBuilder sb = new StringBuilder();
746 for (Bubble bubble : bubbles) {
747 if (bubble == null) {
748 sb.append(" <null> !!!!!\n");
749 } else {
750 boolean isSelected = (bubble == selected);
751 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
752 ((isSelected) ? "->" : " "),
753 bubble.getLastActivity(),
754 (bubble.isOngoing() ? 1 : 0),
755 bubble.getKey()));
756 }
757 }
758 return sb.toString();
759 }
760
Mady Mellore80930e2019-03-21 16:00:45 -0700761 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -0500762 * This task stack listener is responsible for responding to tasks moved to the front
763 * which are on the default (main) display. When this happens, expanded bubbles must be
764 * collapsed so the user may interact with the app which was just moved to the front.
765 * <p>
766 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
767 * these calls via a main thread Handler.
768 */
769 @MainThread
770 private class BubbleTaskStackListener extends TaskStackChangeListener {
771
Mark Renoufcecc77b2019-01-30 16:32:24 -0500772 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500773 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
774 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400775 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500776 }
777 }
778
Mark Renoufcecc77b2019-01-30 16:32:24 -0500779 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500780 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500781 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400782 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500783 }
784 }
Mark Renouf446251d2019-04-26 10:22:41 -0400785
786 @Override
787 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
788 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
789 mBubbleData.setExpanded(false);
790 }
791 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200792
793 @Override
794 public void onSingleTaskDisplayDrawn(int displayId) {
795 final Bubble expandedBubble = getExpandedBubble(mContext);
796 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
797 expandedBubble.setContentVisibility(true);
798 }
799 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200800
801 @Override
802 public void onSingleTaskDisplayEmpty(int displayId) {
803 final Bubble expandedBubble = getExpandedBubble(mContext);
804 if (expandedBubble == null) {
805 return;
806 }
807 if (expandedBubble.getDisplayId() == displayId) {
808 mBubbleData.setExpanded(false);
809 }
Mady Mellored99c272019-06-13 15:58:30 -0700810 if (expandedBubble.getExpandedView() != null) {
811 expandedBubble.getExpandedView().notifyDisplayEmpty();
Issei Suzuki734bc942019-06-05 13:59:52 +0200812 }
813 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500814 }
815
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800816 private static boolean areBubblesEnabled(Context context) {
817 return Settings.Secure.getInt(context.getContentResolver(),
818 ENABLE_BUBBLES, 1) != 0;
819 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500820
Mady Mellorca0c24c2019-05-16 16:14:32 -0700821 /**
822 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
823 *
824 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
825 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
826 *
827 * @param context the context to use.
828 * @param entry the entry to bubble.
829 */
830 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
831 PendingIntent intent = entry.getBubbleMetadata() != null
832 ? entry.getBubbleMetadata().getIntent()
833 : null;
834 if (intent == null) {
835 Log.w(TAG, "Unable to create bubble -- no intent");
836 return false;
837 }
838 ActivityInfo info =
839 intent.getIntent().resolveActivityInfo(context.getPackageManager(), 0);
840 if (info == null) {
841 Log.w(TAG, "Unable to send as bubble -- couldn't find activity info for intent: "
842 + intent);
843 return false;
844 }
845 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
846 Log.w(TAG, "Unable to send as bubble -- activity is not resizable for intent: "
847 + intent);
848 return false;
849 }
850 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
851 Log.w(TAG, "Unable to send as bubble -- activity is not documentLaunchMode=always "
852 + "for intent: " + intent);
853 return false;
854 }
855 if ((info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
856 Log.w(TAG, "Unable to send as bubble -- activity is not embeddable for intent: "
857 + intent);
858 return false;
859 }
860 return true;
861 }
862
Joshua Tsujia19515f2019-02-13 18:02:29 -0500863 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
864 private class BubblesImeListener extends IPinnedStackListener.Stub {
865
866 @Override
867 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
868 }
869
870 @Override
871 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
872 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
873 int displayRotation) throws RemoteException {}
874
875 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500876 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
877 if (mStackView != null && mStackView.getBubbleCount() > 0) {
878 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500879 }
880 }
881
882 @Override
883 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
884 throws RemoteException {}
885
886 @Override
887 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
888
889 @Override
890 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
891 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800892}