blob: 792b9403dffd15379bb4c019609e191803328186 [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 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 Mellor22f2f072019-04-18 13:26:18 -070025import static android.service.notification.NotificationListenerService.REASON_CLICK;
26import static android.service.notification.NotificationListenerService.REASON_GROUP_SUMMARY_CANCELED;
Mady Mellor390bff42019-04-05 15:09:01 -070027import static android.view.Display.DEFAULT_DISPLAY;
28import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080029import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080030import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080031import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080032
Issei Suzukia8d07312019-06-07 12:56:19 +020033import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_CONTROLLER;
Mady Mellorff076eb2019-11-13 10:12:06 -080034import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_EXPERIMENTS;
Issei Suzukia8d07312019-06-07 12:56:19 +020035import 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;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080051import android.content.pm.PackageManager;
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;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040056import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040057import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040058import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040059import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040060import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040061import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050062import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080063import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080064import android.widget.FrameLayout;
65
Mark Renouf08bc42a2019-03-07 13:01:59 -050066import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050067import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040068import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050069
Mady Mellorebdbbb92018-11-15 14:36:48 -080070import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070071import com.android.internal.statusbar.IStatusBarService;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080072import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050073import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050074import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000075import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050076import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050077import com.android.systemui.shared.system.WindowManagerWrapper;
Mark Renoufc19b4732019-06-26 12:08:33 -040078import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070079import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050080import com.android.systemui.statusbar.notification.NotificationEntryListener;
81import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080082import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050083import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellor22f2f072019-04-18 13:26:18 -070084import com.android.systemui.statusbar.phone.NotificationGroupManager;
wilsonshihe8321942019-10-18 18:39:46 +080085import com.android.systemui.statusbar.phone.NotificationShadeWindowController;
Mady Mellor7f234902019-10-20 12:06:29 -070086import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080087import com.android.systemui.statusbar.phone.StatusBar;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070088import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040089import com.android.systemui.statusbar.policy.ZenModeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080090
Mady Mellor70cba7bb2019-07-02 15:06:07 -070091import java.io.FileDescriptor;
92import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -050093import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -040094import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -070095import java.util.ArrayList;
Mady Mellorff076eb2019-11-13 10:12:06 -080096import java.util.HashSet;
Lyn Hanb58c7562020-01-07 14:29:20 -080097import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050098
Jason Monk27d01a622018-12-10 15:57:09 -050099import javax.inject.Inject;
100import javax.inject.Singleton;
101
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800102/**
103 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
104 * Bubbles can be expanded to show more content.
105 *
106 * The controller manages addition, removal, and visible state of bubbles on screen.
107 */
Jason Monk27d01a622018-12-10 15:57:09 -0500108@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -0400109public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800110
Issei Suzukia8d07312019-06-07 12:56:19 +0200111 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800112
Mark Renouf08bc42a2019-03-07 13:01:59 -0500113 @Retention(SOURCE)
114 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400115 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700116 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400117 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500118 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700119
Mark Renouf08bc42a2019-03-07 13:01:59 -0500120 static final int DISMISS_USER_GESTURE = 1;
121 static final int DISMISS_AGED = 2;
122 static final int DISMISS_TASK_FINISHED = 3;
123 static final int DISMISS_BLOCKED = 4;
124 static final int DISMISS_NOTIF_CANCEL = 5;
125 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700126 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400127 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700128 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700129 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500130
Ned Burns01e38212019-01-03 16:32:52 -0500131 private final Context mContext;
132 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500133 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800134 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800135 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100136 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700137 private final NotificationGroupManager mNotificationGroupManager;
Heemin Seogba6337f2019-12-10 15:34:37 -0800138 private final ShadeController mShadeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800139
Mady Mellor3dff9e62019-02-05 18:12:53 -0800140 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400141 @Nullable private BubbleStackView mStackView;
Mady Mellor247ca2c2019-12-02 16:18:59 -0800142 private BubbleIconFactory mBubbleIconFactory;
Lyn Hanb58c7562020-01-07 14:29:20 -0800143 private int mMaxBubbles;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800144
Mark Renoufc19b4732019-06-26 12:08:33 -0400145 // Tracks the id of the current (foreground) user.
146 private int mCurrentUserId;
147 // Saves notification keys of active bubbles when users are switched.
148 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
149
Mady Mellorff076eb2019-11-13 10:12:06 -0800150 // Saves notification keys of user created "fake" bubbles so that we can allow notifications
151 // like these to bubble by default. Doesn't persist across reboots, not a long-term solution.
152 private final HashSet<String> mUserCreatedBubbles;
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800153 // If we're auto-bubbling bubbles via a whitelist, we need to track which notifs from that app
154 // have been "demoted" back to a notification so that we don't auto-bubbles those again.
155 // Doesn't persist across reboots, not a long-term solution.
156 private final HashSet<String> mUserBlockedBubbles;
Mady Mellorff076eb2019-11-13 10:12:06 -0800157
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800158 // Bubbles get added to the status bar view
wilsonshihe8321942019-10-18 18:39:46 +0800159 private final NotificationShadeWindowController mNotificationShadeWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400160 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800161 private StatusBarStateListener mStatusBarStateListener;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500162
Lyn Hanb58c7562020-01-07 14:29:20 -0800163 // Callback that updates BubbleOverflowActivity on data change.
164 @Nullable private Runnable mOverflowCallback = null;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800165
Mady Melloraa8fef22019-04-11 13:36:40 -0700166 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700167 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800168
Mady Mellord1c78b262018-11-06 18:04:40 -0800169 // Used for determining view rect for touch interaction
170 private Rect mTempRect = new Rect();
171
Mark Renoufc19b4732019-06-26 12:08:33 -0400172 // Listens to user switch so bubbles can be saved and restored.
173 private final NotificationLockscreenUserManager mNotifUserManager;
174
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400175 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
176 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
177
Mady Mellor3df7ab02019-12-09 15:07:10 -0800178 private boolean mInflateSynchronously;
179
Beverlyed8aea22020-01-22 16:52:47 -0500180 // TODO (b/145659174): allow for multiple callbacks to support the "shadow" new notif pipeline
181 private final List<NotifCallback> mCallbacks = new ArrayList<>();
182
Mady Mellor5549dd22018-11-06 18:07:34 -0800183 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800184 * Listener to be notified when some states of the bubbles change.
185 */
186 public interface BubbleStateChangeListener {
187 /**
188 * Called when the stack has bubbles or no longer has bubbles.
189 */
190 void onHasBubblesChanged(boolean hasBubbles);
191 }
192
Mady Mellorcd9b1302018-11-06 18:08:04 -0800193 /**
194 * Listener to find out about stack expansion / collapse events.
195 */
196 public interface BubbleExpandListener {
197 /**
198 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700199 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800200 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800201 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800202 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800203 void onBubbleExpandChanged(boolean isExpanding, String key);
204 }
205
206 /**
Mady Mellorf44b6832020-01-14 13:26:14 -0800207 * Listener to be notified when a bubbles' notification suppression state changes.
208 */
209 public interface NotificationSuppressionChangedListener {
210 /**
211 * Called when the notification suppression state of a bubble changes.
212 */
213 void onBubbleNotificationSuppressionChange(Bubble bubble);
Beverlyed8aea22020-01-22 16:52:47 -0500214 }
Mady Mellorf44b6832020-01-14 13:26:14 -0800215
Beverlyed8aea22020-01-22 16:52:47 -0500216 /**
217 * Callback for when the BubbleController wants to interact with the notification pipeline to:
218 * - Remove a previously bubbled notification
219 * - Update the notification shade since bubbled notification should/shouldn't be showing
220 */
221 public interface NotifCallback {
222 /**
223 * Called when the BubbleController wants to remove an entry that it was previously hiding
224 * from the shade. See {@link BubbleController#isBubbleNotificationSuppressedFromShade}.
225 */
226 void removeNotification(NotificationEntry entry);
227
228 /**
229 * Called when a bubbled notification has changed whether it should be
230 * filtered from the shade.
231 */
232 void invalidateNotificationFilter(String reason);
233
234 /**
235 * Called on a bubbled entry that has been removed when there are no longer
236 * bubbled entries in its group.
237 *
238 * Checks whether its group has any other (non-bubbled) children. If it doesn't,
239 * removes all remnants of the group's summary from the notification pipeline.
240 * TODO: (b/145659174) Only old pipeline needs this - delete post-migration.
241 */
242 void maybeCancelSummary(NotificationEntry entry);
Mady Mellorf44b6832020-01-14 13:26:14 -0800243 }
244
245 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800246 * Listens for the current state of the status bar and updates the visibility state
247 * of bubbles as needed.
248 */
249 private class StatusBarStateListener implements StatusBarStateController.StateListener {
250 private int mState;
251 /**
252 * Returns the current status bar state.
253 */
254 public int getCurrentState() {
255 return mState;
256 }
257
258 @Override
259 public void onStateChanged(int newState) {
260 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400261 boolean shouldCollapse = (mState != SHADE);
262 if (shouldCollapse) {
263 collapseStack();
264 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700265 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800266 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800267 }
268
Jason Monk27d01a622018-12-10 15:57:09 -0500269 @Inject
Mady Mellor7f234902019-10-20 12:06:29 -0700270 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800271 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700272 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800273 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700274 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700275 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400276 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400277 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700278 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700279 NotificationGroupManager groupManager,
Aran Inkc4381c42020-02-06 13:11:53 -0500280 NotificationEntryManager entryManager) {
wilsonshihe8321942019-10-18 18:39:46 +0800281 this(context, notificationShadeWindowController, statusBarStateController, shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700282 data, null /* synchronizer */, configurationController, interruptionStateProvider,
Aran Inkc4381c42020-02-06 13:11:53 -0500283 zenModeController, notifUserManager, groupManager, entryManager);
Mady Mellor7f234902019-10-20 12:06:29 -0700284 }
285
286 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800287 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700288 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800289 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700290 BubbleData data,
291 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
292 ConfigurationController configurationController,
293 NotificationInterruptionStateProvider interruptionStateProvider,
294 ZenModeController zenModeController,
295 NotificationLockscreenUserManager notifUserManager,
296 NotificationGroupManager groupManager,
Aran Inkc4381c42020-02-06 13:11:53 -0500297 NotificationEntryManager entryManager) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800298 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800299 mShadeController = shadeController;
Mady Melloraa8fef22019-04-11 13:36:40 -0700300 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400301 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400302 mZenModeController = zenModeController;
303 mZenModeController.addCallback(new ZenModeController.Callback() {
304 @Override
305 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800306 for (Bubble b : mBubbleData.getBubbles()) {
307 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700308 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400309 }
310
311 @Override
312 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800313 for (Bubble b : mBubbleData.getBubbles()) {
314 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700315 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400316 }
317 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700318
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700319 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800320
Mady Mellorf44b6832020-01-14 13:26:14 -0800321 mMaxBubbles = mContext.getResources().getInteger(R.integer.bubbles_max_rendered);
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400322 mBubbleData = data;
323 mBubbleData.setListener(mBubbleDataListener);
Mady Mellorf44b6832020-01-14 13:26:14 -0800324 mBubbleData.setSuppressionChangedListener(new NotificationSuppressionChangedListener() {
325 @Override
326 public void onBubbleNotificationSuppressionChange(Bubble bubble) {
327 // Make sure NoMan knows it's not showing in the shade anymore so anyone querying it
328 // can tell.
329 try {
330 mBarService.onBubbleNotificationSuppressionChanged(bubble.getKey(),
331 !bubble.showInShade());
332 } catch (RemoteException e) {
333 // Bad things have happened
334 }
335 }
336 });
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400337
Mady Mellor7f234902019-10-20 12:06:29 -0700338 mNotificationEntryManager = entryManager;
Mady Mellor22f2f072019-04-18 13:26:18 -0700339 mNotificationGroupManager = groupManager;
Beverlyed8aea22020-01-22 16:52:47 -0500340 setupNEM();
Mady Mellorb4991e62019-01-10 15:14:51 -0800341
wilsonshihe8321942019-10-18 18:39:46 +0800342 mNotificationShadeWindowController = notificationShadeWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800343 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700344 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500345
Mark Renoufcecc77b2019-01-30 16:32:24 -0500346 mTaskStackListener = new BubbleTaskStackListener();
347 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800348
Joshua Tsujia19515f2019-02-13 18:02:29 -0500349 try {
350 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
351 } catch (RemoteException e) {
352 e.printStackTrace();
353 }
Issei Suzukic0387542019-03-08 17:31:14 +0100354 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700355
356 mBarService = IStatusBarService.Stub.asInterface(
357 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400358
359 mSavedBubbleKeysPerUser = new SparseSetArray<>();
360 mCurrentUserId = mNotifUserManager.getCurrentUserId();
361 mNotifUserManager.addUserChangedListener(
Steve Elliottb47f1c72019-12-19 12:39:26 -0500362 new NotificationLockscreenUserManager.UserChangedListener() {
363 @Override
364 public void onUserChanged(int newUserId) {
365 BubbleController.this.saveBubbles(mCurrentUserId);
366 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
367 BubbleController.this.restoreBubbles(newUserId);
368 mCurrentUserId = newUserId;
369 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400370 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800371
372 mUserCreatedBubbles = new HashSet<>();
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800373 mUserBlockedBubbles = new HashSet<>();
Aran Inkaa4dfa72019-11-18 16:49:07 -0500374
Mady Mellor247ca2c2019-12-02 16:18:59 -0800375 mBubbleIconFactory = new BubbleIconFactory(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800376 }
377
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400378 /**
Beverlyed8aea22020-01-22 16:52:47 -0500379 * See {@link NotifCallback}.
380 */
381 public void addNotifCallback(NotifCallback callback) {
382 mCallbacks.add(callback);
383 }
384
385 private void setupNEM() {
386 mNotificationEntryManager.addNotificationEntryListener(
387 new NotificationEntryListener() {
388 @Override
Mady Mellorf9439ab2020-01-30 16:06:53 -0800389 public void onPendingEntryAdded(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -0500390 onEntryAdded(entry);
391 }
392
393 @Override
394 public void onPreEntryUpdated(NotificationEntry entry) {
395 onEntryUpdated(entry);
396 }
397
398 @Override
399 public void onNotificationRankingUpdated(RankingMap rankingMap) {
400 onRankingUpdated(rankingMap);
401 }
402 });
403
Evan Laird04373662020-01-24 17:37:39 -0500404 mNotificationEntryManager.addNotificationRemoveInterceptor(
Beverlyed8aea22020-01-22 16:52:47 -0500405 new NotificationRemoveInterceptor() {
406 @Override
Evan Laird04373662020-01-24 17:37:39 -0500407 public boolean onNotificationRemoveRequested(
408 String key, NotificationEntry entry, int reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500409 return shouldInterceptDismissal(entry, reason);
410 }
411 });
412
413 mNotificationGroupManager.addOnGroupChangeListener(
414 new NotificationGroupManager.OnGroupChangeListener() {
415 @Override
416 public void onGroupSuppressionChanged(
417 NotificationGroupManager.NotificationGroup group,
418 boolean suppressed) {
419 // More notifications could be added causing summary to no longer
420 // be suppressed -- in this case need to remove the key.
421 final String groupKey = group.summary != null
422 ? group.summary.getSbn().getGroupKey()
423 : null;
424 if (!suppressed && groupKey != null
425 && mBubbleData.isSummarySuppressed(groupKey)) {
426 mBubbleData.removeSuppressedSummary(groupKey);
427 }
428 }
429 });
430
431 addNotifCallback(new NotifCallback() {
432 @Override
433 public void removeNotification(NotificationEntry entry) {
434 mNotificationEntryManager.performRemoveNotification(entry.getSbn(),
435 UNDEFINED_DISMISS_REASON);
436 }
437
438 @Override
439 public void invalidateNotificationFilter(String reason) {
440 mNotificationEntryManager.updateNotifications(reason);
441 }
442
443 @Override
444 public void maybeCancelSummary(NotificationEntry entry) {
445 // Check if removed bubble has an associated suppressed group summary that needs
446 // to be removed now.
447 final String groupKey = entry.getSbn().getGroup();
448 if (mBubbleData.isSummarySuppressed(groupKey)) {
449 mBubbleData.removeSuppressedSummary(entry.getSbn().getGroupKey());
450
451 final NotificationEntry summary =
452 mNotificationEntryManager.getActiveNotificationUnfiltered(
453 mBubbleData.getSummaryKey(groupKey));
454 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
455 UNDEFINED_DISMISS_REASON);
456 }
457
458 // Check if summary should be removed from NoManGroup
459 NotificationEntry summary =
460 mNotificationGroupManager.getLogicalGroupSummary(entry.getSbn());
461 if (summary != null) {
462 ArrayList<NotificationEntry> summaryChildren =
463 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
464 boolean isSummaryThisNotif = summary.getKey().equals(entry.getKey());
465 if (!isSummaryThisNotif && (summaryChildren == null
466 || summaryChildren.isEmpty())) {
467 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
468 UNDEFINED_DISMISS_REASON);
469 }
470 }
471 }
472 });
473 }
474
475 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800476 * Sets whether to perform inflation on the same thread as the caller. This method should only
477 * be used in tests, not in production.
478 */
479 @VisibleForTesting
480 void setInflateSynchronously(boolean inflateSynchronously) {
481 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800482 }
483
Lyn Hanb58c7562020-01-07 14:29:20 -0800484 void setOverflowCallback(Runnable updateOverflow) {
485 mOverflowCallback = updateOverflow;
486 }
487
488 /**
489 * @return Bubbles for updating overflow.
490 */
491 List<Bubble> getOverflowBubbles() {
492 return mBubbleData.getOverflowBubbles();
493 }
494
495
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400496 /**
497 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
498 * method initializes the stack view and adds it to the StatusBar just above the scrim.
499 */
500 private void ensureStackViewCreated() {
501 if (mStackView == null) {
502 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
wilsonshihe8321942019-10-18 18:39:46 +0800503 ViewGroup nsv = mNotificationShadeWindowController.getNotificationShadeView();
504 int bubbleScrimIndex = nsv.indexOfChild(nsv.findViewById(R.id.scrim_for_bubble));
Lyn Hanbde48202019-05-29 19:18:29 -0700505 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
wilsonshihe8321942019-10-18 18:39:46 +0800506 nsv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400507 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
508 if (mExpandListener != null) {
509 mStackView.setExpandListener(mExpandListener);
510 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400511 }
512 }
513
Mark Renoufc19b4732019-06-26 12:08:33 -0400514 /**
515 * Records the notification key for any active bubbles. These are used to restore active
516 * bubbles when the user returns to the foreground.
517 *
518 * @param userId the id of the user
519 */
520 private void saveBubbles(@UserIdInt int userId) {
521 // First clear any existing keys that might be stored.
522 mSavedBubbleKeysPerUser.remove(userId);
523 // Add in all active bubbles for the current user.
524 for (Bubble bubble: mBubbleData.getBubbles()) {
525 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
526 }
527 }
528
529 /**
530 * Promotes existing notifications to Bubbles if they were previously bubbles.
531 *
532 * @param userId the id of the user
533 */
534 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400535 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
536 if (savedBubbleKeys == null) {
537 // There were no bubbles saved for this used.
538 return;
539 }
Evan Laird181de622019-10-24 09:53:02 -0400540 for (NotificationEntry e :
541 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400542 if (savedBubbleKeys.contains(e.getKey())
Mark Renoufc19b4732019-06-26 12:08:33 -0400543 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
544 && canLaunchInActivityView(mContext, e)) {
545 updateBubble(e, /* suppressFlyout= */ true);
546 }
547 }
548 // Finally, remove the entries for this user now that bubbles are restored.
549 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
550 }
551
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700552 @Override
553 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800554 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700555 }
556
557 @Override
558 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800559 updateForThemeChanges();
560 }
561
562 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700563 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700564 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700565 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800566 mBubbleIconFactory = new BubbleIconFactory(mContext);
567 for (Bubble b: mBubbleData.getBubbles()) {
568 // Reload each bubble
569 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
570 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700571 }
572
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400573 @Override
574 public void onConfigChanged(Configuration newConfig) {
575 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400576 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700577 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400578 }
579 }
580
Mady Mellor5549dd22018-11-06 18:07:34 -0800581 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800582 * Set a listener to be notified when some states of the bubbles change.
583 */
584 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
585 mStateChangeListener = listener;
586 }
587
588 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800589 * Set a listener to be notified of bubble expand events.
590 */
591 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100592 mExpandListener = ((isExpanding, key) -> {
593 if (listener != null) {
594 listener.onBubbleExpandChanged(isExpanding, key);
595 }
wilsonshihe8321942019-10-18 18:39:46 +0800596 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100597 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800598 if (mStackView != null) {
599 mStackView.setExpandListener(mExpandListener);
600 }
601 }
602
603 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800604 * Whether or not there are bubbles present, regardless of them being visible on the
605 * screen (e.g. if on AOD).
606 */
607 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800608 if (mStackView == null) {
609 return false;
610 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400611 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800612 }
613
614 /**
615 * Whether the stack of bubbles is expanded or not.
616 */
617 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400618 return mBubbleData.isExpanded();
619 }
620
621 /**
622 * Tell the stack of bubbles to expand.
623 */
624 public void expandStack() {
625 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800626 }
627
628 /**
629 * Tell the stack of bubbles to collapse.
630 */
631 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400632 mBubbleData.setExpanded(false /* expanded */);
633 }
634
Mady Mellorce23c462019-06-17 17:30:07 -0700635 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700636 * True if either:
637 * (1) There is a bubble associated with the provided key and if its notification is hidden
638 * from the shade.
639 * (2) There is a group summary associated with the provided key that is hidden from the shade
640 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700641 *
Mady Mellore28fe102019-07-09 15:33:32 -0700642 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700643 */
Beverlyed8aea22020-01-22 16:52:47 -0500644 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
645 String key = entry.getKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700646 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800647 && !mBubbleData.getBubbleWithKey(key).showInShade();
Beverlyed8aea22020-01-22 16:52:47 -0500648
649 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700650 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700651 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Beverlyed8aea22020-01-22 16:52:47 -0500652
Mady Mellore4348272019-07-29 17:43:36 -0700653 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700654 }
655
Mark Renouf71a3af62019-04-08 15:02:54 -0400656 @VisibleForTesting
657 void selectBubble(String key) {
658 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellor247ca2c2019-12-02 16:18:59 -0800659 mBubbleData.setSelectedBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800660 }
661
Lyn Hanb58c7562020-01-07 14:29:20 -0800662 void promoteBubbleFromOverflow(Bubble bubble) {
663 mBubbleData.promoteBubbleFromOverflow(bubble);
664 }
665
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800666 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400667 * Request the stack expand if needed, then select the specified Bubble as current.
668 *
669 * @param notificationKey the notification key for the bubble to be selected
670 */
671 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400672 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
673 if (bubble != null) {
674 mBubbleData.setSelectedBubble(bubble);
675 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400676 }
677 }
678
679 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800680 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
681 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500682 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400683 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800684 }
685
686 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500687 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
688 * is forwarded a back key down/up pair.
689 */
690 public void performBackPressIfNeeded() {
691 if (mStackView != null) {
692 mStackView.performBackPressIfNeeded();
693 }
694 }
695
696 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800697 * Adds or updates a bubble associated with the provided notification entry.
698 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400699 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800700 */
Mark Renouff97ed462019-04-05 13:46:24 -0400701 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700702 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400703 }
704
705 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700706 updateBubble(notif, suppressFlyout, true /* showInShade */);
707 }
708
709 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800710 if (mStackView == null) {
711 // Lazy init stack view when a bubble is created
712 ensureStackViewCreated();
713 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700714 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400715 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700716 notif.setInterruption();
717 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800718 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
719 bubble.setInflateSynchronously(mInflateSynchronously);
720 bubble.inflate(
721 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
722 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700723 }
724
725 /**
726 * Called when a user has indicated that an active notification should be shown as a bubble.
727 * <p>
728 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
729 * the notification from appearing in the shade.
730 *
731 * @param entry the notification to show as a bubble.
732 */
733 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800734 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
735 Log.d(TAG, "onUserCreatedBubble: " + entry.getKey());
736 }
Heemin Seogba6337f2019-12-10 15:34:37 -0800737 mShadeController.collapsePanel(true);
Mady Mellor7f234902019-10-20 12:06:29 -0700738 entry.setFlagBubble(true);
739 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
Mady Mellorff076eb2019-11-13 10:12:06 -0800740 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800741 mUserBlockedBubbles.remove(entry.getKey());
Mady Mellor7f234902019-10-20 12:06:29 -0700742 }
743
744 /**
745 * Called when a user has indicated that an active notification appearing as a bubble should
746 * no longer be shown as a bubble.
747 *
748 * @param entry the notification to no longer show as a bubble.
749 */
750 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800751 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
752 Log.d(TAG, "onUserDemotedBubble: " + entry.getKey());
753 }
Mady Mellor7f234902019-10-20 12:06:29 -0700754 entry.setFlagBubble(false);
755 removeBubble(entry.getKey(), DISMISS_BLOCKED);
Mady Mellorff076eb2019-11-13 10:12:06 -0800756 mUserCreatedBubbles.remove(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800757 if (BubbleExperimentConfig.isPackageWhitelistedToAutoBubble(
758 mContext, entry.getSbn().getPackageName())) {
759 // This package is whitelist but user demoted the bubble, let's save it so we don't
760 // auto-bubble for the whitelist again.
761 mUserBlockedBubbles.add(entry.getKey());
762 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800763 }
764
765 /**
766 * Whether this bubble was explicitly created by the user via a SysUI affordance.
767 */
768 boolean isUserCreatedBubble(String key) {
769 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800770 }
771
772 /**
773 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500774 * <p>
775 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800776 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500777 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500778 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400779 // TEMP: refactor to change this to pass entry
780 Bubble bubble = mBubbleData.getBubbleWithKey(key);
781 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700782 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800783 }
784 }
785
Beverlyed8aea22020-01-22 16:52:47 -0500786 private void onEntryAdded(NotificationEntry entry) {
787 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
788 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
789 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
790 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor22f2f072019-04-18 13:26:18 -0700791
Beverlyed8aea22020-01-22 16:52:47 -0500792 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
793 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
794 if (wasAdjusted && !previouslyUserCreated) {
795 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
796 mUserCreatedBubbles.add(entry.getKey());
Mady Mellorc2ff0112019-03-28 14:18:06 -0700797 }
Beverlyed8aea22020-01-22 16:52:47 -0500798 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700799 }
800 }
801
Beverlyed8aea22020-01-22 16:52:47 -0500802 private void onEntryUpdated(NotificationEntry entry) {
803 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
804 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
805 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
806 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700807
Beverlyed8aea22020-01-22 16:52:47 -0500808 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
809 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
810 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
811 // It was previously a bubble but no longer a bubble -- lets remove it
812 removeBubble(entry.getKey(), DISMISS_NO_LONGER_BUBBLE);
813 } else if (shouldBubble) {
814 if (wasAdjusted && !previouslyUserCreated) {
815 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
816 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800817 }
Beverlyed8aea22020-01-22 16:52:47 -0500818 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800819 }
Beverlyed8aea22020-01-22 16:52:47 -0500820 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800821
Beverlyed8aea22020-01-22 16:52:47 -0500822 private void onRankingUpdated(RankingMap rankingMap) {
823 // Forward to BubbleData to block any bubbles which should no longer be shown
824 mBubbleData.notificationRankingUpdated(rankingMap);
825 }
Ned Burns01e38212019-01-03 16:32:52 -0500826
Mark Renouf71a3af62019-04-08 15:02:54 -0400827 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400828 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400829
Mark Renouf3bc5b362019-04-05 14:37:59 -0400830 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400831 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800832 // Update bubbles in overflow.
833 if (mOverflowCallback != null) {
834 mOverflowCallback.run();
835 }
836
Mark Renouf82a40e62019-05-23 16:16:24 -0400837 // Collapsing? Do this first before remaining steps.
838 if (update.expandedChanged && !update.expanded) {
839 mStackView.setExpanded(false);
840 }
841
842 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700843 ArrayList<Pair<Bubble, Integer>> removedBubbles =
844 new ArrayList<>(update.removedBubbles);
845 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400846 final Bubble bubble = removed.first;
847 @DismissReason final int reason = removed.second;
848 mStackView.removeBubble(bubble);
849
Mark Renoufc19b4732019-06-26 12:08:33 -0400850 // If the bubble is removed for user switching, leave the notification in place.
851 if (reason != DISMISS_USER_CHANGED) {
852 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800853 && !bubble.showInShade()) {
Beverlyed8aea22020-01-22 16:52:47 -0500854 // The bubble is now gone & the notification is hidden from the shade, so
855 // time to actually remove it
856 for (NotifCallback cb : mCallbacks) {
857 cb.removeNotification(bubble.getEntry());
858 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400859 } else {
860 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400861 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700862
Mark Renoufc19b4732019-06-26 12:08:33 -0400863 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
864 // will get right result back
865 try {
866 mBarService.onNotificationBubbleChanged(bubble.getKey(),
867 false /* isBubble */);
868 } catch (RemoteException e) {
869 // Bad things have happened
870 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400871 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700872
Ned Burns00b4b2d2019-10-17 22:09:27 -0400873 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500874 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
875 // Time to potentially remove the summary
876 for (NotifCallback cb : mCallbacks) {
877 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -0700878 }
879 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700880 }
881 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400882
Lyn Hanc47e1712020-01-28 21:43:34 -0800883 if (update.addedBubble != null) {
884 mStackView.addBubble(update.addedBubble);
885 }
886
Mark Renouf82a40e62019-05-23 16:16:24 -0400887 if (update.updatedBubble != null) {
888 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400889 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400890
Lyn Hanb58c7562020-01-07 14:29:20 -0800891 // At this point, the correct bubbles are inflated in the stack.
892 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -0400893 if (update.orderChanged) {
894 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400895 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400896
Mark Renouf82a40e62019-05-23 16:16:24 -0400897 if (update.selectionChanged) {
898 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700899 if (update.selectedBubble != null) {
900 mNotificationGroupManager.updateSuppression(
901 update.selectedBubble.getEntry());
902 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400903 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400904
Mark Renouf82a40e62019-05-23 16:16:24 -0400905 // Expanding? Apply this last.
906 if (update.expandedChanged && update.expanded) {
907 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400908 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400909
Beverlyed8aea22020-01-22 16:52:47 -0500910 for (NotifCallback cb : mCallbacks) {
911 cb.invalidateNotificationFilter("BubbleData.Listener.applyUpdate");
912 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700913 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400914
Issei Suzukia8d07312019-06-07 12:56:19 +0200915 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800916 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -0800917 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400918 mBubbleData.getSelectedBubble()));
919
920 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800921 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -0800922 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400923 mStackView.getExpandedBubble()));
924 }
Lyn Hanb58c7562020-01-07 14:29:20 -0800925 Log.d(TAG, "\n[BubbleData] overflow:");
926 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
927 null));
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400928 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400929 }
930 };
931
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800932 /**
Beverlyed8aea22020-01-22 16:52:47 -0500933 * We intercept notification entries cancelled by the user (i.e. dismissed) when there is an
934 * active bubble associated with it. We do this so that developers can still cancel it
935 * (and hence the bubbles associated with it). However, these intercepted notifications
936 * should then be hidden from the shade since the user has cancelled them, so we update
937 * {@link Bubble#showInShade}.
938 *
939 * The cancellation of summaries with children associated with bubbles are also handled in this
940 * method. User-cancelled summaries are tracked by {@link BubbleData#addSummaryToSuppress}.
941 *
Mady Mellor91b31e62020-01-30 17:40:48 -0800942 * @return true if we want to intercept the dismissal of the entry, else false.
Beverlyed8aea22020-01-22 16:52:47 -0500943 */
944 public boolean shouldInterceptDismissal(NotificationEntry entry, int dismissReason) {
945 if (entry == null) {
946 return false;
947 }
948 String key = entry.getKey();
949 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
950 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
951
952 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
953 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
954 && mBubbleData.getSummaryKey(groupKey).equals(key));
955 boolean isSummary = entry != null
956 && entry.getSbn().getNotification().isGroupSummary();
957 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
958 && bubbleChildren != null && !bubbleChildren.isEmpty();
959
960 if (!inBubbleData && !isSummaryOfBubbles) {
961 return false;
962 }
963
964 final boolean isClearAll = dismissReason == REASON_CANCEL_ALL;
965 final boolean isUserDimiss = dismissReason == REASON_CANCEL
966 || dismissReason == REASON_CLICK;
967 final boolean isAppCancel = dismissReason == REASON_APP_CANCEL
968 || dismissReason == REASON_APP_CANCEL_ALL;
969 final boolean isSummaryCancel = dismissReason == REASON_GROUP_SUMMARY_CANCELED;
970
971 // Need to check for !appCancel here because the notification may have
972 // previously been dismissed & entry.isRowDismissed would still be true
973 boolean userRemovedNotif = (entry != null && entry.isRowDismissed() && !isAppCancel)
974 || isClearAll || isUserDimiss || isSummaryCancel;
975 if (isSummaryOfBubbles) {
976 return handleSummaryRemovalInterception(entry, userRemovedNotif);
977 }
978
979 // The bubble notification sticks around in the data as long as the bubble is
980 // not dismissed and the app hasn't cancelled the notification.
981 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellor91b31e62020-01-30 17:40:48 -0800982 boolean bubbleExtended = entry != null && entry.isBubble()
983 && (userRemovedNotif || isUserCreatedBubble(bubble.getKey()));
Beverlyed8aea22020-01-22 16:52:47 -0500984 if (bubbleExtended) {
985 bubble.setSuppressNotification(true);
986 bubble.setShowDot(false /* show */, true /* animate */);
987 for (NotifCallback cb : mCallbacks) {
988 cb.invalidateNotificationFilter("BubbleController"
989 + ".shouldInterceptDismissal");
990 }
991 return true;
Mady Mellor91b31e62020-01-30 17:40:48 -0800992 } else if (!userRemovedNotif && entry != null) {
Beverlyed8aea22020-01-22 16:52:47 -0500993 // This wasn't a user removal so we should remove the bubble as well
994 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
995 return false;
996 }
997 return false;
998 }
999
1000 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
1001 boolean userRemovedNotif) {
1002 String groupKey = summary.getSbn().getGroupKey();
1003 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1004
1005 if (userRemovedNotif) {
1006 // If it's a user dismiss we mark the children to be hidden from the shade.
1007 for (int i = 0; i < bubbleChildren.size(); i++) {
1008 Bubble bubbleChild = bubbleChildren.get(i);
1009 // As far as group manager is concerned, once a child is no longer shown
1010 // in the shade, it is essentially removed.
1011 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1012 bubbleChild.setSuppressNotification(true);
1013 bubbleChild.setShowDot(false /* show */, true /* animate */);
1014 }
1015 // And since all children are removed, remove the summary.
1016 mNotificationGroupManager.onEntryRemoved(summary);
1017
1018 // If the summary was auto-generated we don't need to keep that notification around
1019 // because apps can't cancel it; so we only intercept & suppress real summaries.
1020 boolean isAutogroupSummary = (summary.getSbn().getNotification().flags
1021 & FLAG_AUTOGROUP_SUMMARY) != 0;
1022 if (!isAutogroupSummary) {
1023 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1024 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1025 summary.getKey());
1026 // Tell shade to update for the suppression
1027 mNotificationEntryManager.updateNotifications("BubbleController"
1028 + ".handleSummaryRemovalInterception");
1029 }
1030 return !isAutogroupSummary;
1031 } else {
1032 // If it's not a user dismiss it's a cancel.
1033 for (int i = 0; i < bubbleChildren.size(); i++) {
1034 // First check if any of these are user-created (i.e. experimental bubbles)
1035 if (mUserCreatedBubbles.contains(bubbleChildren.get(i).getKey())) {
1036 // Experimental bubble! Intercept the removal.
1037 return true;
1038 }
1039 }
1040
1041 // Not an experimental bubble, safe to remove.
1042 mBubbleData.removeSuppressedSummary(groupKey);
1043 // Remove any associated bubble children with the summary.
1044 for (int i = 0; i < bubbleChildren.size(); i++) {
1045 Bubble bubbleChild = bubbleChildren.get(i);
1046 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
1047 DISMISS_GROUP_CANCELLED);
1048 }
1049 return false;
1050 }
1051 }
1052
1053 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001054 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001055 * Updates the visibility of the bubbles based on current state.
1056 * Does not un-bubble, just hides or un-hides. Notifies any
1057 * {@link BubbleStateChangeListener}s of visibility changes.
1058 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001059 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001060 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001061 if (mStackView == null) {
1062 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001063 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001064 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1065 // Bubbles only appear in unlocked shade
1066 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001067 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001068 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001069 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001070
Mady Mellor698d9e82019-08-01 23:11:53 +00001071 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001072 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001073 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001074 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001075 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1076 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1077 }
1078
1079 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001080 }
1081
1082 /**
1083 * Rect indicating the touchable region for the bubble stack / expanded stack.
1084 */
1085 public Rect getTouchableRegion() {
1086 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1087 return null;
1088 }
1089 mStackView.getBoundsOnScreen(mTempRect);
1090 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001091 }
1092
Mady Mellor390bff42019-04-05 15:09:01 -07001093 /**
1094 * The display id of the expanded view, if the stack is expanded and not occluded by the
1095 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1096 */
1097 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001098 final Bubble bubble = getExpandedBubble(context);
1099 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
1100 }
1101
1102 @Nullable
1103 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001104 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001105 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001106 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001107 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001108 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +02001109 final Bubble expandedBubble = mStackView.getExpandedBubble();
1110 if (defaultDisplay && expandedBubble != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001111 && !mNotificationShadeWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001112 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -07001113 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001114 return null;
Mady Mellor390bff42019-04-05 15:09:01 -07001115 }
1116
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001117 @VisibleForTesting
1118 BubbleStackView getStackView() {
1119 return mStackView;
1120 }
1121
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001122 /**
1123 * Description of current bubble state.
1124 */
1125 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1126 pw.println("BubbleController state:");
1127 mBubbleData.dump(fd, pw, args);
1128 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001129 if (mStackView != null) {
1130 mStackView.dump(fd, pw, args);
1131 }
1132 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001133 }
1134
Mady Mellore80930e2019-03-21 16:00:45 -07001135 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001136 * This task stack listener is responsible for responding to tasks moved to the front
1137 * which are on the default (main) display. When this happens, expanded bubbles must be
1138 * collapsed so the user may interact with the app which was just moved to the front.
1139 * <p>
1140 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1141 * these calls via a main thread Handler.
1142 */
1143 @MainThread
1144 private class BubbleTaskStackListener extends TaskStackChangeListener {
1145
Mark Renoufcecc77b2019-01-30 16:32:24 -05001146 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001147 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1148 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001149 if (!mStackView.isExpansionAnimating()) {
1150 mBubbleData.setExpanded(false);
1151 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001152 }
1153 }
1154
Mark Renoufcecc77b2019-01-30 16:32:24 -05001155 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001156 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001157 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001158 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001159 }
1160 }
Mark Renouf446251d2019-04-26 10:22:41 -04001161
1162 @Override
1163 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1164 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1165 mBubbleData.setExpanded(false);
1166 }
1167 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001168
1169 @Override
1170 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001171 if (mStackView == null) {
1172 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001173 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001174 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001175 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001176
1177 @Override
1178 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001179 final Bubble expandedBubble = mStackView != null
1180 ? mStackView.getExpandedBubble()
1181 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001182 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1183 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001184 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001185 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001186 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001187 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001188 }
1189
Mady Mellorca0c24c2019-05-16 16:14:32 -07001190 /**
1191 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1192 *
1193 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1194 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1195 *
1196 * @param context the context to use.
1197 * @param entry the entry to bubble.
1198 */
1199 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1200 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001201 ? entry.getBubbleMetadata().getBubbleIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001202 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001203 if (entry.getBubbleMetadata() != null
1204 && entry.getBubbleMetadata().getShortcutId() != null) {
1205 return true;
1206 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001207 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001208 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001209 return false;
1210 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001211 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1212 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001213 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001214 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001215 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001216 Log.w(TAG, "Unable to send as bubble, "
1217 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001218 + intent);
1219 return false;
1220 }
1221 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001222 Log.w(TAG, "Unable to send as bubble, "
1223 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001224 + intent);
1225 return false;
1226 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001227 return true;
1228 }
1229
Joshua Tsujia19515f2019-02-13 18:02:29 -05001230 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001231 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001232 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001233 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1234 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1235 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001236 }
1237 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001238 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001239}