blob: 7c07c9d8100027fe3288f839eef8c5ee396e2253 [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;
Aran Inkaa4dfa72019-11-18 16:49:07 -050047import android.app.Notification;
Mady Mellor66efd5e2019-05-15 13:38:11 -070048import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070049import android.app.PendingIntent;
Aran Inkaa4dfa72019-11-18 16:49:07 -050050import android.app.RemoteInput;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080051import android.content.Context;
Aran Inkaa4dfa72019-11-18 16:49:07 -050052import android.content.Intent;
Mady Mellorca0c24c2019-05-16 16:14:32 -070053import android.content.pm.ActivityInfo;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080054import android.content.pm.PackageManager;
Aran Inkaa4dfa72019-11-18 16:49:07 -050055import android.content.pm.ShortcutManager;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040056import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080057import android.graphics.Rect;
Aran Inkaa4dfa72019-11-18 16:49:07 -050058import android.net.Uri;
59import android.os.Handler;
Mark Renoufcecc77b2019-01-30 16:32:24 -050060import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080061import android.os.ServiceManager;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040062import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040063import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040064import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040065import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040066import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040067import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050068import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080069import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080070import android.widget.FrameLayout;
71
Mark Renouf08bc42a2019-03-07 13:01:59 -050072import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050073import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040074import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050075
Mady Mellorebdbbb92018-11-15 14:36:48 -080076import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070077import com.android.internal.statusbar.IStatusBarService;
Aran Inkaa4dfa72019-11-18 16:49:07 -050078import com.android.internal.util.ScreenshotHelper;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080079import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050080import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050081import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000082import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050083import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050084import com.android.systemui.shared.system.WindowManagerWrapper;
Mark Renoufc19b4732019-06-26 12:08:33 -040085import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070086import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050087import com.android.systemui.statusbar.notification.NotificationEntryListener;
88import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080089import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050090import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellor22f2f072019-04-18 13:26:18 -070091import com.android.systemui.statusbar.phone.NotificationGroupManager;
wilsonshihe8321942019-10-18 18:39:46 +080092import com.android.systemui.statusbar.phone.NotificationShadeWindowController;
Mady Mellor7f234902019-10-20 12:06:29 -070093import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080094import com.android.systemui.statusbar.phone.StatusBar;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070095import com.android.systemui.statusbar.policy.ConfigurationController;
Aran Inkaa4dfa72019-11-18 16:49:07 -050096import com.android.systemui.statusbar.policy.RemoteInputUriController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040097import com.android.systemui.statusbar.policy.ZenModeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080098
Mady Mellor70cba7bb2019-07-02 15:06:07 -070099import java.io.FileDescriptor;
100import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500101import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -0400102import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -0700103import java.util.ArrayList;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500104import java.util.HashMap;
Mady Mellorff076eb2019-11-13 10:12:06 -0800105import java.util.HashSet;
Lyn Hanb58c7562020-01-07 14:29:20 -0800106import java.util.List;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500107import java.util.function.Consumer;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500108
Jason Monk27d01a622018-12-10 15:57:09 -0500109import javax.inject.Inject;
110import javax.inject.Singleton;
111
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800112/**
113 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
114 * Bubbles can be expanded to show more content.
115 *
116 * The controller manages addition, removal, and visible state of bubbles on screen.
117 */
Jason Monk27d01a622018-12-10 15:57:09 -0500118@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -0400119public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800120
Issei Suzukia8d07312019-06-07 12:56:19 +0200121 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800122
Mark Renouf08bc42a2019-03-07 13:01:59 -0500123 @Retention(SOURCE)
124 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400125 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700126 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400127 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500128 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700129
Mark Renouf08bc42a2019-03-07 13:01:59 -0500130 static final int DISMISS_USER_GESTURE = 1;
131 static final int DISMISS_AGED = 2;
132 static final int DISMISS_TASK_FINISHED = 3;
133 static final int DISMISS_BLOCKED = 4;
134 static final int DISMISS_NOTIF_CANCEL = 5;
135 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700136 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400137 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700138 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700139 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500140
Ned Burns01e38212019-01-03 16:32:52 -0500141 private final Context mContext;
142 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500143 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800144 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800145 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100146 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700147 private final NotificationGroupManager mNotificationGroupManager;
Heemin Seogba6337f2019-12-10 15:34:37 -0800148 private final ShadeController mShadeController;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500149 private final RemoteInputUriController mRemoteInputUriController;
150 private Handler mHandler = new Handler() {};
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800151
Mady Mellor3dff9e62019-02-05 18:12:53 -0800152 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400153 @Nullable private BubbleStackView mStackView;
Mady Mellor247ca2c2019-12-02 16:18:59 -0800154 private BubbleIconFactory mBubbleIconFactory;
Lyn Hanb58c7562020-01-07 14:29:20 -0800155 private int mMaxBubbles;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800156
Mark Renoufc19b4732019-06-26 12:08:33 -0400157 // Tracks the id of the current (foreground) user.
158 private int mCurrentUserId;
159 // Saves notification keys of active bubbles when users are switched.
160 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
161
Mady Mellorff076eb2019-11-13 10:12:06 -0800162 // Saves notification keys of user created "fake" bubbles so that we can allow notifications
163 // like these to bubble by default. Doesn't persist across reboots, not a long-term solution.
164 private final HashSet<String> mUserCreatedBubbles;
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800165 // If we're auto-bubbling bubbles via a whitelist, we need to track which notifs from that app
166 // have been "demoted" back to a notification so that we don't auto-bubbles those again.
167 // Doesn't persist across reboots, not a long-term solution.
168 private final HashSet<String> mUserBlockedBubbles;
Mady Mellorff076eb2019-11-13 10:12:06 -0800169
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800170 // Bubbles get added to the status bar view
wilsonshihe8321942019-10-18 18:39:46 +0800171 private final NotificationShadeWindowController mNotificationShadeWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400172 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800173 private StatusBarStateListener mStatusBarStateListener;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500174 private final ScreenshotHelper mScreenshotHelper;
175
Lyn Hanb58c7562020-01-07 14:29:20 -0800176 // Callback that updates BubbleOverflowActivity on data change.
177 @Nullable private Runnable mOverflowCallback = null;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800178
Mady Melloraa8fef22019-04-11 13:36:40 -0700179 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700180 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800181
Mady Mellord1c78b262018-11-06 18:04:40 -0800182 // Used for determining view rect for touch interaction
183 private Rect mTempRect = new Rect();
184
Mark Renoufc19b4732019-06-26 12:08:33 -0400185 // Listens to user switch so bubbles can be saved and restored.
186 private final NotificationLockscreenUserManager mNotifUserManager;
187
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400188 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
189 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
190
Mady Mellor3df7ab02019-12-09 15:07:10 -0800191 private boolean mInflateSynchronously;
192
Beverlyed8aea22020-01-22 16:52:47 -0500193 // TODO (b/145659174): allow for multiple callbacks to support the "shadow" new notif pipeline
194 private final List<NotifCallback> mCallbacks = new ArrayList<>();
195
Mady Mellor5549dd22018-11-06 18:07:34 -0800196 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800197 * Listener to be notified when some states of the bubbles change.
198 */
199 public interface BubbleStateChangeListener {
200 /**
201 * Called when the stack has bubbles or no longer has bubbles.
202 */
203 void onHasBubblesChanged(boolean hasBubbles);
204 }
205
Mady Mellorcd9b1302018-11-06 18:08:04 -0800206 /**
207 * Listener to find out about stack expansion / collapse events.
208 */
209 public interface BubbleExpandListener {
210 /**
211 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700212 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800213 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800214 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800215 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800216 void onBubbleExpandChanged(boolean isExpanding, String key);
217 }
218
219 /**
Aran Inkaa4dfa72019-11-18 16:49:07 -0500220 * Listener for handling bubble screenshot events.
221 */
222 public interface BubbleScreenshotListener {
223 /**
224 * Called to trigger taking a screenshot and sending the result to a bubble.
225 */
226 void onBubbleScreenshot(Bubble bubble);
227 }
228
229 /**
Mady Mellorf44b6832020-01-14 13:26:14 -0800230 * Listener to be notified when a bubbles' notification suppression state changes.
231 */
232 public interface NotificationSuppressionChangedListener {
233 /**
234 * Called when the notification suppression state of a bubble changes.
235 */
236 void onBubbleNotificationSuppressionChange(Bubble bubble);
Beverlyed8aea22020-01-22 16:52:47 -0500237 }
Mady Mellorf44b6832020-01-14 13:26:14 -0800238
Beverlyed8aea22020-01-22 16:52:47 -0500239 /**
240 * Callback for when the BubbleController wants to interact with the notification pipeline to:
241 * - Remove a previously bubbled notification
242 * - Update the notification shade since bubbled notification should/shouldn't be showing
243 */
244 public interface NotifCallback {
245 /**
246 * Called when the BubbleController wants to remove an entry that it was previously hiding
247 * from the shade. See {@link BubbleController#isBubbleNotificationSuppressedFromShade}.
248 */
249 void removeNotification(NotificationEntry entry);
250
251 /**
252 * Called when a bubbled notification has changed whether it should be
253 * filtered from the shade.
254 */
255 void invalidateNotificationFilter(String reason);
256
257 /**
258 * Called on a bubbled entry that has been removed when there are no longer
259 * bubbled entries in its group.
260 *
261 * Checks whether its group has any other (non-bubbled) children. If it doesn't,
262 * removes all remnants of the group's summary from the notification pipeline.
263 * TODO: (b/145659174) Only old pipeline needs this - delete post-migration.
264 */
265 void maybeCancelSummary(NotificationEntry entry);
Mady Mellorf44b6832020-01-14 13:26:14 -0800266 }
267
268 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800269 * Listens for the current state of the status bar and updates the visibility state
270 * of bubbles as needed.
271 */
272 private class StatusBarStateListener implements StatusBarStateController.StateListener {
273 private int mState;
274 /**
275 * Returns the current status bar state.
276 */
277 public int getCurrentState() {
278 return mState;
279 }
280
281 @Override
282 public void onStateChanged(int newState) {
283 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400284 boolean shouldCollapse = (mState != SHADE);
285 if (shouldCollapse) {
286 collapseStack();
287 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700288 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800289 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800290 }
291
Jason Monk27d01a622018-12-10 15:57:09 -0500292 @Inject
Mady Mellor7f234902019-10-20 12:06:29 -0700293 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800294 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700295 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800296 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700297 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700298 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400299 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400300 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700301 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700302 NotificationGroupManager groupManager,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500303 NotificationEntryManager entryManager,
304 RemoteInputUriController remoteInputUriController) {
wilsonshihe8321942019-10-18 18:39:46 +0800305 this(context, notificationShadeWindowController, statusBarStateController, shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700306 data, null /* synchronizer */, configurationController, interruptionStateProvider,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500307 zenModeController, notifUserManager, groupManager, entryManager,
308 remoteInputUriController);
Mady Mellor7f234902019-10-20 12:06:29 -0700309 }
310
311 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800312 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700313 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800314 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700315 BubbleData data,
316 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
317 ConfigurationController configurationController,
318 NotificationInterruptionStateProvider interruptionStateProvider,
319 ZenModeController zenModeController,
320 NotificationLockscreenUserManager notifUserManager,
321 NotificationGroupManager groupManager,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500322 NotificationEntryManager entryManager,
323 RemoteInputUriController remoteInputUriController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800324 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800325 mShadeController = shadeController;
Mady Melloraa8fef22019-04-11 13:36:40 -0700326 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400327 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400328 mZenModeController = zenModeController;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500329 mRemoteInputUriController = remoteInputUriController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400330 mZenModeController.addCallback(new ZenModeController.Callback() {
331 @Override
332 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800333 for (Bubble b : mBubbleData.getBubbles()) {
334 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700335 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400336 }
337
338 @Override
339 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800340 for (Bubble b : mBubbleData.getBubbles()) {
341 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700342 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400343 }
344 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700345
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700346 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800347
Mady Mellorf44b6832020-01-14 13:26:14 -0800348 mMaxBubbles = mContext.getResources().getInteger(R.integer.bubbles_max_rendered);
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400349 mBubbleData = data;
350 mBubbleData.setListener(mBubbleDataListener);
Mady Mellorf44b6832020-01-14 13:26:14 -0800351 mBubbleData.setSuppressionChangedListener(new NotificationSuppressionChangedListener() {
352 @Override
353 public void onBubbleNotificationSuppressionChange(Bubble bubble) {
354 // Make sure NoMan knows it's not showing in the shade anymore so anyone querying it
355 // can tell.
356 try {
357 mBarService.onBubbleNotificationSuppressionChanged(bubble.getKey(),
358 !bubble.showInShade());
359 } catch (RemoteException e) {
360 // Bad things have happened
361 }
362 }
363 });
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400364
Mady Mellor7f234902019-10-20 12:06:29 -0700365 mNotificationEntryManager = entryManager;
Mady Mellor22f2f072019-04-18 13:26:18 -0700366 mNotificationGroupManager = groupManager;
Beverlyed8aea22020-01-22 16:52:47 -0500367 setupNEM();
Mady Mellorb4991e62019-01-10 15:14:51 -0800368
wilsonshihe8321942019-10-18 18:39:46 +0800369 mNotificationShadeWindowController = notificationShadeWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800370 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700371 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500372
Mark Renoufcecc77b2019-01-30 16:32:24 -0500373 mTaskStackListener = new BubbleTaskStackListener();
374 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800375
Joshua Tsujia19515f2019-02-13 18:02:29 -0500376 try {
377 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
378 } catch (RemoteException e) {
379 e.printStackTrace();
380 }
Issei Suzukic0387542019-03-08 17:31:14 +0100381 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700382
383 mBarService = IStatusBarService.Stub.asInterface(
384 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400385
386 mSavedBubbleKeysPerUser = new SparseSetArray<>();
387 mCurrentUserId = mNotifUserManager.getCurrentUserId();
388 mNotifUserManager.addUserChangedListener(
Steve Elliottb47f1c72019-12-19 12:39:26 -0500389 new NotificationLockscreenUserManager.UserChangedListener() {
390 @Override
391 public void onUserChanged(int newUserId) {
392 BubbleController.this.saveBubbles(mCurrentUserId);
393 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
394 BubbleController.this.restoreBubbles(newUserId);
395 mCurrentUserId = newUserId;
396 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400397 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800398
399 mUserCreatedBubbles = new HashSet<>();
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800400 mUserBlockedBubbles = new HashSet<>();
Aran Inkaa4dfa72019-11-18 16:49:07 -0500401
402 mScreenshotHelper = new ScreenshotHelper(context);
Mady Mellor247ca2c2019-12-02 16:18:59 -0800403 mBubbleIconFactory = new BubbleIconFactory(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800404 }
405
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400406 /**
Beverlyed8aea22020-01-22 16:52:47 -0500407 * See {@link NotifCallback}.
408 */
409 public void addNotifCallback(NotifCallback callback) {
410 mCallbacks.add(callback);
411 }
412
413 private void setupNEM() {
414 mNotificationEntryManager.addNotificationEntryListener(
415 new NotificationEntryListener() {
416 @Override
Mady Mellorf9439ab2020-01-30 16:06:53 -0800417 public void onPendingEntryAdded(NotificationEntry entry) {
Beverlyed8aea22020-01-22 16:52:47 -0500418 onEntryAdded(entry);
419 }
420
421 @Override
422 public void onPreEntryUpdated(NotificationEntry entry) {
423 onEntryUpdated(entry);
424 }
425
426 @Override
427 public void onNotificationRankingUpdated(RankingMap rankingMap) {
428 onRankingUpdated(rankingMap);
429 }
430 });
431
Evan Laird04373662020-01-24 17:37:39 -0500432 mNotificationEntryManager.addNotificationRemoveInterceptor(
Beverlyed8aea22020-01-22 16:52:47 -0500433 new NotificationRemoveInterceptor() {
434 @Override
Evan Laird04373662020-01-24 17:37:39 -0500435 public boolean onNotificationRemoveRequested(
436 String key, NotificationEntry entry, int reason) {
Beverlyed8aea22020-01-22 16:52:47 -0500437 return shouldInterceptDismissal(entry, reason);
438 }
439 });
440
441 mNotificationGroupManager.addOnGroupChangeListener(
442 new NotificationGroupManager.OnGroupChangeListener() {
443 @Override
444 public void onGroupSuppressionChanged(
445 NotificationGroupManager.NotificationGroup group,
446 boolean suppressed) {
447 // More notifications could be added causing summary to no longer
448 // be suppressed -- in this case need to remove the key.
449 final String groupKey = group.summary != null
450 ? group.summary.getSbn().getGroupKey()
451 : null;
452 if (!suppressed && groupKey != null
453 && mBubbleData.isSummarySuppressed(groupKey)) {
454 mBubbleData.removeSuppressedSummary(groupKey);
455 }
456 }
457 });
458
459 addNotifCallback(new NotifCallback() {
460 @Override
461 public void removeNotification(NotificationEntry entry) {
462 mNotificationEntryManager.performRemoveNotification(entry.getSbn(),
463 UNDEFINED_DISMISS_REASON);
464 }
465
466 @Override
467 public void invalidateNotificationFilter(String reason) {
468 mNotificationEntryManager.updateNotifications(reason);
469 }
470
471 @Override
472 public void maybeCancelSummary(NotificationEntry entry) {
473 // Check if removed bubble has an associated suppressed group summary that needs
474 // to be removed now.
475 final String groupKey = entry.getSbn().getGroup();
476 if (mBubbleData.isSummarySuppressed(groupKey)) {
477 mBubbleData.removeSuppressedSummary(entry.getSbn().getGroupKey());
478
479 final NotificationEntry summary =
480 mNotificationEntryManager.getActiveNotificationUnfiltered(
481 mBubbleData.getSummaryKey(groupKey));
482 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
483 UNDEFINED_DISMISS_REASON);
484 }
485
486 // Check if summary should be removed from NoManGroup
487 NotificationEntry summary =
488 mNotificationGroupManager.getLogicalGroupSummary(entry.getSbn());
489 if (summary != null) {
490 ArrayList<NotificationEntry> summaryChildren =
491 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
492 boolean isSummaryThisNotif = summary.getKey().equals(entry.getKey());
493 if (!isSummaryThisNotif && (summaryChildren == null
494 || summaryChildren.isEmpty())) {
495 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
496 UNDEFINED_DISMISS_REASON);
497 }
498 }
499 }
500 });
501 }
502
503 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800504 * Sets whether to perform inflation on the same thread as the caller. This method should only
505 * be used in tests, not in production.
506 */
507 @VisibleForTesting
508 void setInflateSynchronously(boolean inflateSynchronously) {
509 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800510 }
511
Lyn Hanb58c7562020-01-07 14:29:20 -0800512 void setOverflowCallback(Runnable updateOverflow) {
513 mOverflowCallback = updateOverflow;
514 }
515
516 /**
517 * @return Bubbles for updating overflow.
518 */
519 List<Bubble> getOverflowBubbles() {
520 return mBubbleData.getOverflowBubbles();
521 }
522
523
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400524 /**
525 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
526 * method initializes the stack view and adds it to the StatusBar just above the scrim.
527 */
528 private void ensureStackViewCreated() {
529 if (mStackView == null) {
530 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
wilsonshihe8321942019-10-18 18:39:46 +0800531 ViewGroup nsv = mNotificationShadeWindowController.getNotificationShadeView();
532 int bubbleScrimIndex = nsv.indexOfChild(nsv.findViewById(R.id.scrim_for_bubble));
Lyn Hanbde48202019-05-29 19:18:29 -0700533 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
wilsonshihe8321942019-10-18 18:39:46 +0800534 nsv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400535 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
536 if (mExpandListener != null) {
537 mStackView.setExpandListener(mExpandListener);
538 }
Aran Inkaa4dfa72019-11-18 16:49:07 -0500539 if (mBubbleScreenshotListener != null) {
540 mStackView.setBubbleScreenshotListener(mBubbleScreenshotListener);
541 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400542 }
543 }
544
Mark Renoufc19b4732019-06-26 12:08:33 -0400545 /**
546 * Records the notification key for any active bubbles. These are used to restore active
547 * bubbles when the user returns to the foreground.
548 *
549 * @param userId the id of the user
550 */
551 private void saveBubbles(@UserIdInt int userId) {
552 // First clear any existing keys that might be stored.
553 mSavedBubbleKeysPerUser.remove(userId);
554 // Add in all active bubbles for the current user.
555 for (Bubble bubble: mBubbleData.getBubbles()) {
556 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
557 }
558 }
559
560 /**
561 * Promotes existing notifications to Bubbles if they were previously bubbles.
562 *
563 * @param userId the id of the user
564 */
565 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400566 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
567 if (savedBubbleKeys == null) {
568 // There were no bubbles saved for this used.
569 return;
570 }
Evan Laird181de622019-10-24 09:53:02 -0400571 for (NotificationEntry e :
572 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400573 if (savedBubbleKeys.contains(e.getKey())
Mark Renoufc19b4732019-06-26 12:08:33 -0400574 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
575 && canLaunchInActivityView(mContext, e)) {
576 updateBubble(e, /* suppressFlyout= */ true);
577 }
578 }
579 // Finally, remove the entries for this user now that bubbles are restored.
580 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
581 }
582
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700583 @Override
584 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800585 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700586 }
587
588 @Override
589 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800590 updateForThemeChanges();
591 }
592
593 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700594 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700595 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700596 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800597 mBubbleIconFactory = new BubbleIconFactory(mContext);
598 for (Bubble b: mBubbleData.getBubbles()) {
599 // Reload each bubble
600 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
601 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700602 }
603
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400604 @Override
605 public void onConfigChanged(Configuration newConfig) {
606 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400607 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700608 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400609 }
610 }
611
Mady Mellor5549dd22018-11-06 18:07:34 -0800612 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800613 * Set a listener to be notified when some states of the bubbles change.
614 */
615 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
616 mStateChangeListener = listener;
617 }
618
619 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800620 * Set a listener to be notified of bubble expand events.
621 */
622 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100623 mExpandListener = ((isExpanding, key) -> {
624 if (listener != null) {
625 listener.onBubbleExpandChanged(isExpanding, key);
626 }
wilsonshihe8321942019-10-18 18:39:46 +0800627 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100628 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800629 if (mStackView != null) {
630 mStackView.setExpandListener(mExpandListener);
631 }
632 }
633
634 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800635 * Whether or not there are bubbles present, regardless of them being visible on the
636 * screen (e.g. if on AOD).
637 */
638 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800639 if (mStackView == null) {
640 return false;
641 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400642 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800643 }
644
645 /**
646 * Whether the stack of bubbles is expanded or not.
647 */
648 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400649 return mBubbleData.isExpanded();
650 }
651
652 /**
653 * Tell the stack of bubbles to expand.
654 */
655 public void expandStack() {
656 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800657 }
658
659 /**
660 * Tell the stack of bubbles to collapse.
661 */
662 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400663 mBubbleData.setExpanded(false /* expanded */);
664 }
665
Mady Mellorce23c462019-06-17 17:30:07 -0700666 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700667 * True if either:
668 * (1) There is a bubble associated with the provided key and if its notification is hidden
669 * from the shade.
670 * (2) There is a group summary associated with the provided key that is hidden from the shade
671 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700672 *
Mady Mellore28fe102019-07-09 15:33:32 -0700673 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700674 */
Beverlyed8aea22020-01-22 16:52:47 -0500675 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
676 String key = entry.getKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700677 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800678 && !mBubbleData.getBubbleWithKey(key).showInShade();
Beverlyed8aea22020-01-22 16:52:47 -0500679
680 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700681 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700682 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Beverlyed8aea22020-01-22 16:52:47 -0500683
Mady Mellore4348272019-07-29 17:43:36 -0700684 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700685 }
686
Mark Renouf71a3af62019-04-08 15:02:54 -0400687 @VisibleForTesting
688 void selectBubble(String key) {
689 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellor247ca2c2019-12-02 16:18:59 -0800690 mBubbleData.setSelectedBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800691 }
692
Lyn Hanb58c7562020-01-07 14:29:20 -0800693 void promoteBubbleFromOverflow(Bubble bubble) {
694 mBubbleData.promoteBubbleFromOverflow(bubble);
695 }
696
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800697 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400698 * Request the stack expand if needed, then select the specified Bubble as current.
699 *
700 * @param notificationKey the notification key for the bubble to be selected
701 */
702 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400703 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
704 if (bubble != null) {
705 mBubbleData.setSelectedBubble(bubble);
706 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400707 }
708 }
709
710 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800711 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
712 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500713 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400714 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800715 }
716
717 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500718 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
719 * is forwarded a back key down/up pair.
720 */
721 public void performBackPressIfNeeded() {
722 if (mStackView != null) {
723 mStackView.performBackPressIfNeeded();
724 }
725 }
726
727 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800728 * Adds or updates a bubble associated with the provided notification entry.
729 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400730 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800731 */
Mark Renouff97ed462019-04-05 13:46:24 -0400732 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700733 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400734 }
735
736 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700737 updateBubble(notif, suppressFlyout, true /* showInShade */);
738 }
739
740 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800741 if (mStackView == null) {
742 // Lazy init stack view when a bubble is created
743 ensureStackViewCreated();
744 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700745 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400746 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700747 notif.setInterruption();
748 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800749 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
750 bubble.setInflateSynchronously(mInflateSynchronously);
751 bubble.inflate(
752 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
753 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700754 }
755
756 /**
757 * Called when a user has indicated that an active notification should be shown as a bubble.
758 * <p>
759 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
760 * the notification from appearing in the shade.
761 *
762 * @param entry the notification to show as a bubble.
763 */
764 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800765 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
766 Log.d(TAG, "onUserCreatedBubble: " + entry.getKey());
767 }
Heemin Seogba6337f2019-12-10 15:34:37 -0800768 mShadeController.collapsePanel(true);
Mady Mellor7f234902019-10-20 12:06:29 -0700769 entry.setFlagBubble(true);
770 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
Mady Mellorff076eb2019-11-13 10:12:06 -0800771 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800772 mUserBlockedBubbles.remove(entry.getKey());
Mady Mellor7f234902019-10-20 12:06:29 -0700773 }
774
775 /**
776 * Called when a user has indicated that an active notification appearing as a bubble should
777 * no longer be shown as a bubble.
778 *
779 * @param entry the notification to no longer show as a bubble.
780 */
781 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800782 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
783 Log.d(TAG, "onUserDemotedBubble: " + entry.getKey());
784 }
Mady Mellor7f234902019-10-20 12:06:29 -0700785 entry.setFlagBubble(false);
786 removeBubble(entry.getKey(), DISMISS_BLOCKED);
Mady Mellorff076eb2019-11-13 10:12:06 -0800787 mUserCreatedBubbles.remove(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800788 if (BubbleExperimentConfig.isPackageWhitelistedToAutoBubble(
789 mContext, entry.getSbn().getPackageName())) {
790 // This package is whitelist but user demoted the bubble, let's save it so we don't
791 // auto-bubble for the whitelist again.
792 mUserBlockedBubbles.add(entry.getKey());
793 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800794 }
795
796 /**
797 * Whether this bubble was explicitly created by the user via a SysUI affordance.
798 */
799 boolean isUserCreatedBubble(String key) {
800 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800801 }
802
803 /**
804 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500805 * <p>
806 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800807 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500808 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500809 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400810 // TEMP: refactor to change this to pass entry
811 Bubble bubble = mBubbleData.getBubbleWithKey(key);
812 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700813 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800814 }
815 }
816
Beverlyed8aea22020-01-22 16:52:47 -0500817 private void onEntryAdded(NotificationEntry entry) {
818 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
819 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
820 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
821 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor22f2f072019-04-18 13:26:18 -0700822
Beverlyed8aea22020-01-22 16:52:47 -0500823 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
824 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
825 if (wasAdjusted && !previouslyUserCreated) {
826 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
827 mUserCreatedBubbles.add(entry.getKey());
Mady Mellorc2ff0112019-03-28 14:18:06 -0700828 }
Beverlyed8aea22020-01-22 16:52:47 -0500829 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700830 }
831 }
832
Beverlyed8aea22020-01-22 16:52:47 -0500833 private void onEntryUpdated(NotificationEntry entry) {
834 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
835 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
836 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
837 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700838
Beverlyed8aea22020-01-22 16:52:47 -0500839 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
840 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
841 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
842 // It was previously a bubble but no longer a bubble -- lets remove it
843 removeBubble(entry.getKey(), DISMISS_NO_LONGER_BUBBLE);
844 } else if (shouldBubble) {
845 if (wasAdjusted && !previouslyUserCreated) {
846 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
847 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800848 }
Beverlyed8aea22020-01-22 16:52:47 -0500849 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800850 }
Beverlyed8aea22020-01-22 16:52:47 -0500851 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800852
Beverlyed8aea22020-01-22 16:52:47 -0500853 private void onRankingUpdated(RankingMap rankingMap) {
854 // Forward to BubbleData to block any bubbles which should no longer be shown
855 mBubbleData.notificationRankingUpdated(rankingMap);
856 }
Ned Burns01e38212019-01-03 16:32:52 -0500857
Mark Renouf71a3af62019-04-08 15:02:54 -0400858 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400859 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400860
Mark Renouf3bc5b362019-04-05 14:37:59 -0400861 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400862 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800863 // Update bubbles in overflow.
864 if (mOverflowCallback != null) {
865 mOverflowCallback.run();
866 }
867
Mark Renouf82a40e62019-05-23 16:16:24 -0400868 // Collapsing? Do this first before remaining steps.
869 if (update.expandedChanged && !update.expanded) {
870 mStackView.setExpanded(false);
871 }
872
873 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700874 ArrayList<Pair<Bubble, Integer>> removedBubbles =
875 new ArrayList<>(update.removedBubbles);
876 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400877 final Bubble bubble = removed.first;
878 @DismissReason final int reason = removed.second;
879 mStackView.removeBubble(bubble);
880
Mark Renoufc19b4732019-06-26 12:08:33 -0400881 // If the bubble is removed for user switching, leave the notification in place.
882 if (reason != DISMISS_USER_CHANGED) {
883 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800884 && !bubble.showInShade()) {
Beverlyed8aea22020-01-22 16:52:47 -0500885 // The bubble is now gone & the notification is hidden from the shade, so
886 // time to actually remove it
887 for (NotifCallback cb : mCallbacks) {
888 cb.removeNotification(bubble.getEntry());
889 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400890 } else {
891 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400892 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700893
Mark Renoufc19b4732019-06-26 12:08:33 -0400894 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
895 // will get right result back
896 try {
897 mBarService.onNotificationBubbleChanged(bubble.getKey(),
898 false /* isBubble */);
899 } catch (RemoteException e) {
900 // Bad things have happened
901 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400902 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700903
Ned Burns00b4b2d2019-10-17 22:09:27 -0400904 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500905 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
906 // Time to potentially remove the summary
907 for (NotifCallback cb : mCallbacks) {
908 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -0700909 }
910 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700911 }
912 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400913
Lyn Hanc47e1712020-01-28 21:43:34 -0800914 if (update.addedBubble != null) {
915 mStackView.addBubble(update.addedBubble);
916 }
917
Mark Renouf82a40e62019-05-23 16:16:24 -0400918 if (update.updatedBubble != null) {
919 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400920 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400921
Lyn Hanb58c7562020-01-07 14:29:20 -0800922 // At this point, the correct bubbles are inflated in the stack.
923 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -0400924 if (update.orderChanged) {
925 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400926 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400927
Mark Renouf82a40e62019-05-23 16:16:24 -0400928 if (update.selectionChanged) {
929 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700930 if (update.selectedBubble != null) {
931 mNotificationGroupManager.updateSuppression(
932 update.selectedBubble.getEntry());
933 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400934 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400935
Mark Renouf82a40e62019-05-23 16:16:24 -0400936 // Expanding? Apply this last.
937 if (update.expandedChanged && update.expanded) {
938 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400939 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400940
Beverlyed8aea22020-01-22 16:52:47 -0500941 for (NotifCallback cb : mCallbacks) {
942 cb.invalidateNotificationFilter("BubbleData.Listener.applyUpdate");
943 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700944 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400945
Issei Suzukia8d07312019-06-07 12:56:19 +0200946 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800947 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -0800948 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400949 mBubbleData.getSelectedBubble()));
950
951 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800952 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -0800953 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400954 mStackView.getExpandedBubble()));
955 }
Lyn Hanb58c7562020-01-07 14:29:20 -0800956 Log.d(TAG, "\n[BubbleData] overflow:");
957 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
958 null));
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400959 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400960 }
961 };
962
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800963 /**
Beverlyed8aea22020-01-22 16:52:47 -0500964 * We intercept notification entries cancelled by the user (i.e. dismissed) when there is an
965 * active bubble associated with it. We do this so that developers can still cancel it
966 * (and hence the bubbles associated with it). However, these intercepted notifications
967 * should then be hidden from the shade since the user has cancelled them, so we update
968 * {@link Bubble#showInShade}.
969 *
970 * The cancellation of summaries with children associated with bubbles are also handled in this
971 * method. User-cancelled summaries are tracked by {@link BubbleData#addSummaryToSuppress}.
972 *
973 * @return true if we want to intercept the dismissal of the entry, else false
974 */
975 public boolean shouldInterceptDismissal(NotificationEntry entry, int dismissReason) {
976 if (entry == null) {
977 return false;
978 }
979 String key = entry.getKey();
980 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
981 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
982
983 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
984 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
985 && mBubbleData.getSummaryKey(groupKey).equals(key));
986 boolean isSummary = entry != null
987 && entry.getSbn().getNotification().isGroupSummary();
988 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
989 && bubbleChildren != null && !bubbleChildren.isEmpty();
990
991 if (!inBubbleData && !isSummaryOfBubbles) {
992 return false;
993 }
994
995 final boolean isClearAll = dismissReason == REASON_CANCEL_ALL;
996 final boolean isUserDimiss = dismissReason == REASON_CANCEL
997 || dismissReason == REASON_CLICK;
998 final boolean isAppCancel = dismissReason == REASON_APP_CANCEL
999 || dismissReason == REASON_APP_CANCEL_ALL;
1000 final boolean isSummaryCancel = dismissReason == REASON_GROUP_SUMMARY_CANCELED;
1001
1002 // Need to check for !appCancel here because the notification may have
1003 // previously been dismissed & entry.isRowDismissed would still be true
1004 boolean userRemovedNotif = (entry != null && entry.isRowDismissed() && !isAppCancel)
1005 || isClearAll || isUserDimiss || isSummaryCancel;
1006 if (isSummaryOfBubbles) {
1007 return handleSummaryRemovalInterception(entry, userRemovedNotif);
1008 }
1009
1010 // The bubble notification sticks around in the data as long as the bubble is
1011 // not dismissed and the app hasn't cancelled the notification.
1012 Bubble bubble = mBubbleData.getBubbleWithKey(key);
1013 boolean bubbleExtended = entry != null && entry.isBubble() && userRemovedNotif;
1014 if (bubbleExtended) {
1015 bubble.setSuppressNotification(true);
1016 bubble.setShowDot(false /* show */, true /* animate */);
1017 for (NotifCallback cb : mCallbacks) {
1018 cb.invalidateNotificationFilter("BubbleController"
1019 + ".shouldInterceptDismissal");
1020 }
1021 return true;
1022 } else if (!userRemovedNotif && entry != null
1023 && !isUserCreatedBubble(bubble.getKey())) {
1024 // This wasn't a user removal so we should remove the bubble as well
1025 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
1026 return false;
1027 }
1028 return false;
1029 }
1030
1031 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
1032 boolean userRemovedNotif) {
1033 String groupKey = summary.getSbn().getGroupKey();
1034 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1035
1036 if (userRemovedNotif) {
1037 // If it's a user dismiss we mark the children to be hidden from the shade.
1038 for (int i = 0; i < bubbleChildren.size(); i++) {
1039 Bubble bubbleChild = bubbleChildren.get(i);
1040 // As far as group manager is concerned, once a child is no longer shown
1041 // in the shade, it is essentially removed.
1042 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1043 bubbleChild.setSuppressNotification(true);
1044 bubbleChild.setShowDot(false /* show */, true /* animate */);
1045 }
1046 // And since all children are removed, remove the summary.
1047 mNotificationGroupManager.onEntryRemoved(summary);
1048
1049 // If the summary was auto-generated we don't need to keep that notification around
1050 // because apps can't cancel it; so we only intercept & suppress real summaries.
1051 boolean isAutogroupSummary = (summary.getSbn().getNotification().flags
1052 & FLAG_AUTOGROUP_SUMMARY) != 0;
1053 if (!isAutogroupSummary) {
1054 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1055 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1056 summary.getKey());
1057 // Tell shade to update for the suppression
1058 mNotificationEntryManager.updateNotifications("BubbleController"
1059 + ".handleSummaryRemovalInterception");
1060 }
1061 return !isAutogroupSummary;
1062 } else {
1063 // If it's not a user dismiss it's a cancel.
1064 for (int i = 0; i < bubbleChildren.size(); i++) {
1065 // First check if any of these are user-created (i.e. experimental bubbles)
1066 if (mUserCreatedBubbles.contains(bubbleChildren.get(i).getKey())) {
1067 // Experimental bubble! Intercept the removal.
1068 return true;
1069 }
1070 }
1071
1072 // Not an experimental bubble, safe to remove.
1073 mBubbleData.removeSuppressedSummary(groupKey);
1074 // Remove any associated bubble children with the summary.
1075 for (int i = 0; i < bubbleChildren.size(); i++) {
1076 Bubble bubbleChild = bubbleChildren.get(i);
1077 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
1078 DISMISS_GROUP_CANCELLED);
1079 }
1080 return false;
1081 }
1082 }
1083
1084 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001085 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001086 * Updates the visibility of the bubbles based on current state.
1087 * Does not un-bubble, just hides or un-hides. Notifies any
1088 * {@link BubbleStateChangeListener}s of visibility changes.
1089 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001090 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001091 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001092 if (mStackView == null) {
1093 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001094 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001095 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1096 // Bubbles only appear in unlocked shade
1097 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001098 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001099 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001100 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001101
Mady Mellor698d9e82019-08-01 23:11:53 +00001102 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001103 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001104 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001105 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001106 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1107 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1108 }
1109
1110 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001111 }
1112
1113 /**
1114 * Rect indicating the touchable region for the bubble stack / expanded stack.
1115 */
1116 public Rect getTouchableRegion() {
1117 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1118 return null;
1119 }
1120 mStackView.getBoundsOnScreen(mTempRect);
1121 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001122 }
1123
Mady Mellor390bff42019-04-05 15:09:01 -07001124 /**
1125 * The display id of the expanded view, if the stack is expanded and not occluded by the
1126 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1127 */
1128 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001129 final Bubble bubble = getExpandedBubble(context);
1130 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
1131 }
1132
1133 @Nullable
1134 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001135 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001136 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001137 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001138 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001139 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +02001140 final Bubble expandedBubble = mStackView.getExpandedBubble();
1141 if (defaultDisplay && expandedBubble != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001142 && !mNotificationShadeWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001143 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -07001144 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001145 return null;
Mady Mellor390bff42019-04-05 15:09:01 -07001146 }
1147
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001148 @VisibleForTesting
1149 BubbleStackView getStackView() {
1150 return mStackView;
1151 }
1152
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001153 /**
1154 * Description of current bubble state.
1155 */
1156 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1157 pw.println("BubbleController state:");
1158 mBubbleData.dump(fd, pw, args);
1159 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001160 if (mStackView != null) {
1161 mStackView.dump(fd, pw, args);
1162 }
1163 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001164 }
1165
Mady Mellore80930e2019-03-21 16:00:45 -07001166 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001167 * This task stack listener is responsible for responding to tasks moved to the front
1168 * which are on the default (main) display. When this happens, expanded bubbles must be
1169 * collapsed so the user may interact with the app which was just moved to the front.
1170 * <p>
1171 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1172 * these calls via a main thread Handler.
1173 */
1174 @MainThread
1175 private class BubbleTaskStackListener extends TaskStackChangeListener {
1176
Mark Renoufcecc77b2019-01-30 16:32:24 -05001177 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001178 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1179 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001180 if (!mStackView.isExpansionAnimating()) {
1181 mBubbleData.setExpanded(false);
1182 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001183 }
1184 }
1185
Mark Renoufcecc77b2019-01-30 16:32:24 -05001186 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001187 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001188 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001189 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001190 }
1191 }
Mark Renouf446251d2019-04-26 10:22:41 -04001192
1193 @Override
1194 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1195 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1196 mBubbleData.setExpanded(false);
1197 }
1198 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001199
1200 @Override
1201 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001202 if (mStackView == null) {
1203 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001204 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001205 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001206 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001207
1208 @Override
1209 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001210 final Bubble expandedBubble = mStackView != null
1211 ? mStackView.getExpandedBubble()
1212 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001213 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1214 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001215 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001216 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001217 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001218 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001219 }
1220
Mady Mellorca0c24c2019-05-16 16:14:32 -07001221 /**
1222 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1223 *
1224 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1225 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1226 *
1227 * @param context the context to use.
1228 * @param entry the entry to bubble.
1229 */
1230 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1231 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001232 ? entry.getBubbleMetadata().getBubbleIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001233 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001234 if (entry.getBubbleMetadata() != null
1235 && entry.getBubbleMetadata().getShortcutId() != null) {
1236 return true;
1237 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001238 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001239 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001240 return false;
1241 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001242 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1243 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001244 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001245 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001246 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001247 Log.w(TAG, "Unable to send as bubble, "
1248 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001249 + intent);
1250 return false;
1251 }
1252 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001253 Log.w(TAG, "Unable to send as bubble, "
1254 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001255 + intent);
1256 return false;
1257 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001258 return true;
1259 }
1260
Joshua Tsujia19515f2019-02-13 18:02:29 -05001261 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001262 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001263 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001264 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1265 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1266 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001267 }
1268 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001269 }
Aran Inkaa4dfa72019-11-18 16:49:07 -05001270
1271 // TODO: Copied from RemoteInputView. Consolidate RemoteInput intent logic.
1272 private Intent prepareRemoteInputFromData(String contentType, Uri data,
1273 RemoteInput remoteInput, NotificationEntry entry) {
1274 HashMap<String, Uri> results = new HashMap<>();
1275 results.put(contentType, data);
1276 mRemoteInputUriController.grantInlineReplyUriPermission(entry.getSbn(), data);
1277 Intent fillInIntent = new Intent().addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
1278 RemoteInput.addDataResultToIntent(remoteInput, fillInIntent, results);
1279
1280 return fillInIntent;
1281 }
1282
1283 // TODO: Copied from RemoteInputView. Consolidate RemoteInput intent logic.
1284 private void sendRemoteInput(Intent intent, NotificationEntry entry,
1285 PendingIntent pendingIntent) {
1286 // Tell ShortcutManager that this package has been "activated". ShortcutManager
1287 // will reset the throttling for this package.
1288 // Strictly speaking, the intent receiver may be different from the notification publisher,
1289 // but that's an edge case, and also because we can't always know which package will receive
1290 // an intent, so we just reset for the publisher.
1291 mContext.getSystemService(ShortcutManager.class).onApplicationActive(
1292 entry.getSbn().getPackageName(),
1293 entry.getSbn().getUser().getIdentifier());
1294
1295 try {
1296 pendingIntent.send(mContext, 0, intent);
1297 } catch (PendingIntent.CanceledException e) {
1298 Log.i(TAG, "Unable to send remote input result", e);
1299 }
1300 }
1301
1302 private void sendScreenshotToBubble(Bubble bubble) {
Aran Ink141a8152019-12-12 13:31:23 -05001303 mScreenshotHelper.takeScreenshot(
1304 android.view.WindowManager.TAKE_SCREENSHOT_FULLSCREEN,
1305 true /* hasStatus */,
1306 true /* hasNav */,
1307 mHandler,
1308 new Consumer<Uri>() {
1309 @Override
1310 public void accept(Uri uri) {
1311 if (uri != null) {
1312 NotificationEntry entry = bubble.getEntry();
1313 Pair<RemoteInput, Notification.Action> pair = entry.getSbn()
1314 .getNotification().findRemoteInputActionPair(false);
1315 if (pair != null) {
1316 RemoteInput remoteInput = pair.first;
1317 Notification.Action action = pair.second;
1318 Intent dataIntent = prepareRemoteInputFromData("image/png", uri,
1319 remoteInput, entry);
1320 sendRemoteInput(dataIntent, entry, action.actionIntent);
1321 mBubbleData.setSelectedBubble(bubble);
1322 mBubbleData.setExpanded(true);
1323 } else {
1324 Log.w(TAG, "No RemoteInput found for notification: "
1325 + entry.getSbn().getKey());
Aran Inkaa4dfa72019-11-18 16:49:07 -05001326 }
Aran Ink141a8152019-12-12 13:31:23 -05001327 }
1328 }
1329 });
Aran Inkaa4dfa72019-11-18 16:49:07 -05001330 }
1331
1332 private final BubbleScreenshotListener mBubbleScreenshotListener =
1333 bubble -> sendScreenshotToBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001334}