blob: a26cce0bfc0c490036b7a6cecb63784e7f32a78c [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
432 mNotificationEntryManager.setNotificationRemoveInterceptor(
433 new NotificationRemoveInterceptor() {
434 @Override
435 public boolean onNotificationRemoveRequested(String key, int reason) {
436 NotificationEntry entry =
437 mNotificationEntryManager.getActiveNotificationUnfiltered(key);
438 return shouldInterceptDismissal(entry, reason);
439 }
440 });
441
442 mNotificationGroupManager.addOnGroupChangeListener(
443 new NotificationGroupManager.OnGroupChangeListener() {
444 @Override
445 public void onGroupSuppressionChanged(
446 NotificationGroupManager.NotificationGroup group,
447 boolean suppressed) {
448 // More notifications could be added causing summary to no longer
449 // be suppressed -- in this case need to remove the key.
450 final String groupKey = group.summary != null
451 ? group.summary.getSbn().getGroupKey()
452 : null;
453 if (!suppressed && groupKey != null
454 && mBubbleData.isSummarySuppressed(groupKey)) {
455 mBubbleData.removeSuppressedSummary(groupKey);
456 }
457 }
458 });
459
460 addNotifCallback(new NotifCallback() {
461 @Override
462 public void removeNotification(NotificationEntry entry) {
463 mNotificationEntryManager.performRemoveNotification(entry.getSbn(),
464 UNDEFINED_DISMISS_REASON);
465 }
466
467 @Override
468 public void invalidateNotificationFilter(String reason) {
469 mNotificationEntryManager.updateNotifications(reason);
470 }
471
472 @Override
473 public void maybeCancelSummary(NotificationEntry entry) {
474 // Check if removed bubble has an associated suppressed group summary that needs
475 // to be removed now.
476 final String groupKey = entry.getSbn().getGroup();
477 if (mBubbleData.isSummarySuppressed(groupKey)) {
478 mBubbleData.removeSuppressedSummary(entry.getSbn().getGroupKey());
479
480 final NotificationEntry summary =
481 mNotificationEntryManager.getActiveNotificationUnfiltered(
482 mBubbleData.getSummaryKey(groupKey));
483 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
484 UNDEFINED_DISMISS_REASON);
485 }
486
487 // Check if summary should be removed from NoManGroup
488 NotificationEntry summary =
489 mNotificationGroupManager.getLogicalGroupSummary(entry.getSbn());
490 if (summary != null) {
491 ArrayList<NotificationEntry> summaryChildren =
492 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
493 boolean isSummaryThisNotif = summary.getKey().equals(entry.getKey());
494 if (!isSummaryThisNotif && (summaryChildren == null
495 || summaryChildren.isEmpty())) {
496 mNotificationEntryManager.performRemoveNotification(summary.getSbn(),
497 UNDEFINED_DISMISS_REASON);
498 }
499 }
500 }
501 });
502 }
503
504 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800505 * Sets whether to perform inflation on the same thread as the caller. This method should only
506 * be used in tests, not in production.
507 */
508 @VisibleForTesting
509 void setInflateSynchronously(boolean inflateSynchronously) {
510 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800511 }
512
Lyn Hanb58c7562020-01-07 14:29:20 -0800513 void setOverflowCallback(Runnable updateOverflow) {
514 mOverflowCallback = updateOverflow;
515 }
516
517 /**
518 * @return Bubbles for updating overflow.
519 */
520 List<Bubble> getOverflowBubbles() {
521 return mBubbleData.getOverflowBubbles();
522 }
523
524
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400525 /**
526 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
527 * method initializes the stack view and adds it to the StatusBar just above the scrim.
528 */
529 private void ensureStackViewCreated() {
530 if (mStackView == null) {
531 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
wilsonshihe8321942019-10-18 18:39:46 +0800532 ViewGroup nsv = mNotificationShadeWindowController.getNotificationShadeView();
533 int bubbleScrimIndex = nsv.indexOfChild(nsv.findViewById(R.id.scrim_for_bubble));
Lyn Hanbde48202019-05-29 19:18:29 -0700534 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
wilsonshihe8321942019-10-18 18:39:46 +0800535 nsv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400536 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
537 if (mExpandListener != null) {
538 mStackView.setExpandListener(mExpandListener);
539 }
Aran Inkaa4dfa72019-11-18 16:49:07 -0500540 if (mBubbleScreenshotListener != null) {
541 mStackView.setBubbleScreenshotListener(mBubbleScreenshotListener);
542 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400543 }
544 }
545
Mark Renoufc19b4732019-06-26 12:08:33 -0400546 /**
547 * Records the notification key for any active bubbles. These are used to restore active
548 * bubbles when the user returns to the foreground.
549 *
550 * @param userId the id of the user
551 */
552 private void saveBubbles(@UserIdInt int userId) {
553 // First clear any existing keys that might be stored.
554 mSavedBubbleKeysPerUser.remove(userId);
555 // Add in all active bubbles for the current user.
556 for (Bubble bubble: mBubbleData.getBubbles()) {
557 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
558 }
559 }
560
561 /**
562 * Promotes existing notifications to Bubbles if they were previously bubbles.
563 *
564 * @param userId the id of the user
565 */
566 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400567 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
568 if (savedBubbleKeys == null) {
569 // There were no bubbles saved for this used.
570 return;
571 }
Evan Laird181de622019-10-24 09:53:02 -0400572 for (NotificationEntry e :
573 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400574 if (savedBubbleKeys.contains(e.getKey())
Mark Renoufc19b4732019-06-26 12:08:33 -0400575 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
576 && canLaunchInActivityView(mContext, e)) {
577 updateBubble(e, /* suppressFlyout= */ true);
578 }
579 }
580 // Finally, remove the entries for this user now that bubbles are restored.
581 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
582 }
583
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700584 @Override
585 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800586 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700587 }
588
589 @Override
590 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800591 updateForThemeChanges();
592 }
593
594 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700595 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700596 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700597 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800598 mBubbleIconFactory = new BubbleIconFactory(mContext);
599 for (Bubble b: mBubbleData.getBubbles()) {
600 // Reload each bubble
601 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
602 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700603 }
604
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400605 @Override
606 public void onConfigChanged(Configuration newConfig) {
607 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400608 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700609 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400610 }
611 }
612
Mady Mellor5549dd22018-11-06 18:07:34 -0800613 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800614 * Set a listener to be notified when some states of the bubbles change.
615 */
616 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
617 mStateChangeListener = listener;
618 }
619
620 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800621 * Set a listener to be notified of bubble expand events.
622 */
623 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100624 mExpandListener = ((isExpanding, key) -> {
625 if (listener != null) {
626 listener.onBubbleExpandChanged(isExpanding, key);
627 }
wilsonshihe8321942019-10-18 18:39:46 +0800628 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100629 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800630 if (mStackView != null) {
631 mStackView.setExpandListener(mExpandListener);
632 }
633 }
634
635 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800636 * Whether or not there are bubbles present, regardless of them being visible on the
637 * screen (e.g. if on AOD).
638 */
639 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800640 if (mStackView == null) {
641 return false;
642 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400643 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800644 }
645
646 /**
647 * Whether the stack of bubbles is expanded or not.
648 */
649 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400650 return mBubbleData.isExpanded();
651 }
652
653 /**
654 * Tell the stack of bubbles to expand.
655 */
656 public void expandStack() {
657 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800658 }
659
660 /**
661 * Tell the stack of bubbles to collapse.
662 */
663 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400664 mBubbleData.setExpanded(false /* expanded */);
665 }
666
Mady Mellorce23c462019-06-17 17:30:07 -0700667 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700668 * True if either:
669 * (1) There is a bubble associated with the provided key and if its notification is hidden
670 * from the shade.
671 * (2) There is a group summary associated with the provided key that is hidden from the shade
672 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700673 *
Mady Mellore28fe102019-07-09 15:33:32 -0700674 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700675 */
Beverlyed8aea22020-01-22 16:52:47 -0500676 public boolean isBubbleNotificationSuppressedFromShade(NotificationEntry entry) {
677 String key = entry.getKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700678 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800679 && !mBubbleData.getBubbleWithKey(key).showInShade();
Beverlyed8aea22020-01-22 16:52:47 -0500680
681 String groupKey = entry.getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700682 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700683 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
Beverlyed8aea22020-01-22 16:52:47 -0500684
Mady Mellore4348272019-07-29 17:43:36 -0700685 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700686 }
687
Mark Renouf71a3af62019-04-08 15:02:54 -0400688 @VisibleForTesting
689 void selectBubble(String key) {
690 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellor247ca2c2019-12-02 16:18:59 -0800691 mBubbleData.setSelectedBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800692 }
693
Lyn Hanb58c7562020-01-07 14:29:20 -0800694 void promoteBubbleFromOverflow(Bubble bubble) {
695 mBubbleData.promoteBubbleFromOverflow(bubble);
696 }
697
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800698 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400699 * Request the stack expand if needed, then select the specified Bubble as current.
700 *
701 * @param notificationKey the notification key for the bubble to be selected
702 */
703 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400704 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
705 if (bubble != null) {
706 mBubbleData.setSelectedBubble(bubble);
707 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400708 }
709 }
710
711 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800712 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
713 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500714 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400715 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800716 }
717
718 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500719 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
720 * is forwarded a back key down/up pair.
721 */
722 public void performBackPressIfNeeded() {
723 if (mStackView != null) {
724 mStackView.performBackPressIfNeeded();
725 }
726 }
727
728 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800729 * Adds or updates a bubble associated with the provided notification entry.
730 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400731 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800732 */
Mark Renouff97ed462019-04-05 13:46:24 -0400733 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700734 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400735 }
736
737 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700738 updateBubble(notif, suppressFlyout, true /* showInShade */);
739 }
740
741 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800742 if (mStackView == null) {
743 // Lazy init stack view when a bubble is created
744 ensureStackViewCreated();
745 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700746 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400747 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700748 notif.setInterruption();
749 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800750 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
751 bubble.setInflateSynchronously(mInflateSynchronously);
752 bubble.inflate(
753 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
754 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700755 }
756
757 /**
758 * Called when a user has indicated that an active notification should be shown as a bubble.
759 * <p>
760 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
761 * the notification from appearing in the shade.
762 *
763 * @param entry the notification to show as a bubble.
764 */
765 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800766 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
767 Log.d(TAG, "onUserCreatedBubble: " + entry.getKey());
768 }
Heemin Seogba6337f2019-12-10 15:34:37 -0800769 mShadeController.collapsePanel(true);
Mady Mellor7f234902019-10-20 12:06:29 -0700770 entry.setFlagBubble(true);
771 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
Mady Mellorff076eb2019-11-13 10:12:06 -0800772 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800773 mUserBlockedBubbles.remove(entry.getKey());
Mady Mellor7f234902019-10-20 12:06:29 -0700774 }
775
776 /**
777 * Called when a user has indicated that an active notification appearing as a bubble should
778 * no longer be shown as a bubble.
779 *
780 * @param entry the notification to no longer show as a bubble.
781 */
782 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800783 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
784 Log.d(TAG, "onUserDemotedBubble: " + entry.getKey());
785 }
Mady Mellor7f234902019-10-20 12:06:29 -0700786 entry.setFlagBubble(false);
787 removeBubble(entry.getKey(), DISMISS_BLOCKED);
Mady Mellorff076eb2019-11-13 10:12:06 -0800788 mUserCreatedBubbles.remove(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800789 if (BubbleExperimentConfig.isPackageWhitelistedToAutoBubble(
790 mContext, entry.getSbn().getPackageName())) {
791 // This package is whitelist but user demoted the bubble, let's save it so we don't
792 // auto-bubble for the whitelist again.
793 mUserBlockedBubbles.add(entry.getKey());
794 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800795 }
796
797 /**
798 * Whether this bubble was explicitly created by the user via a SysUI affordance.
799 */
800 boolean isUserCreatedBubble(String key) {
801 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800802 }
803
804 /**
805 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500806 * <p>
807 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800808 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500809 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500810 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400811 // TEMP: refactor to change this to pass entry
812 Bubble bubble = mBubbleData.getBubbleWithKey(key);
813 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700814 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800815 }
816 }
817
Beverlyed8aea22020-01-22 16:52:47 -0500818 private void onEntryAdded(NotificationEntry entry) {
819 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
820 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
821 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
822 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor22f2f072019-04-18 13:26:18 -0700823
Beverlyed8aea22020-01-22 16:52:47 -0500824 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
825 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
826 if (wasAdjusted && !previouslyUserCreated) {
827 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
828 mUserCreatedBubbles.add(entry.getKey());
Mady Mellorc2ff0112019-03-28 14:18:06 -0700829 }
Beverlyed8aea22020-01-22 16:52:47 -0500830 updateBubble(entry);
Mady Mellor22f2f072019-04-18 13:26:18 -0700831 }
832 }
833
Beverlyed8aea22020-01-22 16:52:47 -0500834 private void onEntryUpdated(NotificationEntry entry) {
835 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
836 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
837 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
838 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700839
Beverlyed8aea22020-01-22 16:52:47 -0500840 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
841 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
842 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
843 // It was previously a bubble but no longer a bubble -- lets remove it
844 removeBubble(entry.getKey(), DISMISS_NO_LONGER_BUBBLE);
845 } else if (shouldBubble) {
846 if (wasAdjusted && !previouslyUserCreated) {
847 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
848 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800849 }
Beverlyed8aea22020-01-22 16:52:47 -0500850 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800851 }
Beverlyed8aea22020-01-22 16:52:47 -0500852 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800853
Beverlyed8aea22020-01-22 16:52:47 -0500854 private void onRankingUpdated(RankingMap rankingMap) {
855 // Forward to BubbleData to block any bubbles which should no longer be shown
856 mBubbleData.notificationRankingUpdated(rankingMap);
857 }
Ned Burns01e38212019-01-03 16:32:52 -0500858
Mark Renouf71a3af62019-04-08 15:02:54 -0400859 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400860 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400861
Mark Renouf3bc5b362019-04-05 14:37:59 -0400862 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400863 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800864 // Update bubbles in overflow.
865 if (mOverflowCallback != null) {
866 mOverflowCallback.run();
867 }
868
Mark Renouf82a40e62019-05-23 16:16:24 -0400869 // Collapsing? Do this first before remaining steps.
870 if (update.expandedChanged && !update.expanded) {
871 mStackView.setExpanded(false);
872 }
873
874 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700875 ArrayList<Pair<Bubble, Integer>> removedBubbles =
876 new ArrayList<>(update.removedBubbles);
877 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400878 final Bubble bubble = removed.first;
879 @DismissReason final int reason = removed.second;
880 mStackView.removeBubble(bubble);
881
Mark Renoufc19b4732019-06-26 12:08:33 -0400882 // If the bubble is removed for user switching, leave the notification in place.
883 if (reason != DISMISS_USER_CHANGED) {
884 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800885 && !bubble.showInShade()) {
Beverlyed8aea22020-01-22 16:52:47 -0500886 // The bubble is now gone & the notification is hidden from the shade, so
887 // time to actually remove it
888 for (NotifCallback cb : mCallbacks) {
889 cb.removeNotification(bubble.getEntry());
890 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400891 } else {
892 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400893 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700894
Mark Renoufc19b4732019-06-26 12:08:33 -0400895 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
896 // will get right result back
897 try {
898 mBarService.onNotificationBubbleChanged(bubble.getKey(),
899 false /* isBubble */);
900 } catch (RemoteException e) {
901 // Bad things have happened
902 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400903 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700904
Ned Burns00b4b2d2019-10-17 22:09:27 -0400905 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Beverlyed8aea22020-01-22 16:52:47 -0500906 if (mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
907 // Time to potentially remove the summary
908 for (NotifCallback cb : mCallbacks) {
909 cb.maybeCancelSummary(bubble.getEntry());
Mady Mellor22f2f072019-04-18 13:26:18 -0700910 }
911 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700912 }
913 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400914
Lyn Hanc47e1712020-01-28 21:43:34 -0800915 if (update.addedBubble != null) {
916 mStackView.addBubble(update.addedBubble);
917 }
918
Mark Renouf82a40e62019-05-23 16:16:24 -0400919 if (update.updatedBubble != null) {
920 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400921 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400922
Lyn Hanb58c7562020-01-07 14:29:20 -0800923 // At this point, the correct bubbles are inflated in the stack.
924 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -0400925 if (update.orderChanged) {
926 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400927 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400928
Mark Renouf82a40e62019-05-23 16:16:24 -0400929 if (update.selectionChanged) {
930 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700931 if (update.selectedBubble != null) {
932 mNotificationGroupManager.updateSuppression(
933 update.selectedBubble.getEntry());
934 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400935 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400936
Mark Renouf82a40e62019-05-23 16:16:24 -0400937 // Expanding? Apply this last.
938 if (update.expandedChanged && update.expanded) {
939 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400940 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400941
Beverlyed8aea22020-01-22 16:52:47 -0500942 for (NotifCallback cb : mCallbacks) {
943 cb.invalidateNotificationFilter("BubbleData.Listener.applyUpdate");
944 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700945 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400946
Issei Suzukia8d07312019-06-07 12:56:19 +0200947 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800948 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -0800949 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400950 mBubbleData.getSelectedBubble()));
951
952 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800953 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -0800954 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400955 mStackView.getExpandedBubble()));
956 }
Lyn Hanb58c7562020-01-07 14:29:20 -0800957 Log.d(TAG, "\n[BubbleData] overflow:");
958 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
959 null));
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400960 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400961 }
962 };
963
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800964 /**
Beverlyed8aea22020-01-22 16:52:47 -0500965 * We intercept notification entries cancelled by the user (i.e. dismissed) when there is an
966 * active bubble associated with it. We do this so that developers can still cancel it
967 * (and hence the bubbles associated with it). However, these intercepted notifications
968 * should then be hidden from the shade since the user has cancelled them, so we update
969 * {@link Bubble#showInShade}.
970 *
971 * The cancellation of summaries with children associated with bubbles are also handled in this
972 * method. User-cancelled summaries are tracked by {@link BubbleData#addSummaryToSuppress}.
973 *
974 * @return true if we want to intercept the dismissal of the entry, else false
975 */
976 public boolean shouldInterceptDismissal(NotificationEntry entry, int dismissReason) {
977 if (entry == null) {
978 return false;
979 }
980 String key = entry.getKey();
981 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
982 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
983
984 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
985 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
986 && mBubbleData.getSummaryKey(groupKey).equals(key));
987 boolean isSummary = entry != null
988 && entry.getSbn().getNotification().isGroupSummary();
989 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
990 && bubbleChildren != null && !bubbleChildren.isEmpty();
991
992 if (!inBubbleData && !isSummaryOfBubbles) {
993 return false;
994 }
995
996 final boolean isClearAll = dismissReason == REASON_CANCEL_ALL;
997 final boolean isUserDimiss = dismissReason == REASON_CANCEL
998 || dismissReason == REASON_CLICK;
999 final boolean isAppCancel = dismissReason == REASON_APP_CANCEL
1000 || dismissReason == REASON_APP_CANCEL_ALL;
1001 final boolean isSummaryCancel = dismissReason == REASON_GROUP_SUMMARY_CANCELED;
1002
1003 // Need to check for !appCancel here because the notification may have
1004 // previously been dismissed & entry.isRowDismissed would still be true
1005 boolean userRemovedNotif = (entry != null && entry.isRowDismissed() && !isAppCancel)
1006 || isClearAll || isUserDimiss || isSummaryCancel;
1007 if (isSummaryOfBubbles) {
1008 return handleSummaryRemovalInterception(entry, userRemovedNotif);
1009 }
1010
1011 // The bubble notification sticks around in the data as long as the bubble is
1012 // not dismissed and the app hasn't cancelled the notification.
1013 Bubble bubble = mBubbleData.getBubbleWithKey(key);
1014 boolean bubbleExtended = entry != null && entry.isBubble() && userRemovedNotif;
1015 if (bubbleExtended) {
1016 bubble.setSuppressNotification(true);
1017 bubble.setShowDot(false /* show */, true /* animate */);
1018 for (NotifCallback cb : mCallbacks) {
1019 cb.invalidateNotificationFilter("BubbleController"
1020 + ".shouldInterceptDismissal");
1021 }
1022 return true;
1023 } else if (!userRemovedNotif && entry != null
1024 && !isUserCreatedBubble(bubble.getKey())) {
1025 // This wasn't a user removal so we should remove the bubble as well
1026 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
1027 return false;
1028 }
1029 return false;
1030 }
1031
1032 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
1033 boolean userRemovedNotif) {
1034 String groupKey = summary.getSbn().getGroupKey();
1035 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
1036
1037 if (userRemovedNotif) {
1038 // If it's a user dismiss we mark the children to be hidden from the shade.
1039 for (int i = 0; i < bubbleChildren.size(); i++) {
1040 Bubble bubbleChild = bubbleChildren.get(i);
1041 // As far as group manager is concerned, once a child is no longer shown
1042 // in the shade, it is essentially removed.
1043 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
1044 bubbleChild.setSuppressNotification(true);
1045 bubbleChild.setShowDot(false /* show */, true /* animate */);
1046 }
1047 // And since all children are removed, remove the summary.
1048 mNotificationGroupManager.onEntryRemoved(summary);
1049
1050 // If the summary was auto-generated we don't need to keep that notification around
1051 // because apps can't cancel it; so we only intercept & suppress real summaries.
1052 boolean isAutogroupSummary = (summary.getSbn().getNotification().flags
1053 & FLAG_AUTOGROUP_SUMMARY) != 0;
1054 if (!isAutogroupSummary) {
1055 // TODO: (b/145659174) remove references to mSuppressedGroupKeys once fully migrated
1056 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
1057 summary.getKey());
1058 // Tell shade to update for the suppression
1059 mNotificationEntryManager.updateNotifications("BubbleController"
1060 + ".handleSummaryRemovalInterception");
1061 }
1062 return !isAutogroupSummary;
1063 } else {
1064 // If it's not a user dismiss it's a cancel.
1065 for (int i = 0; i < bubbleChildren.size(); i++) {
1066 // First check if any of these are user-created (i.e. experimental bubbles)
1067 if (mUserCreatedBubbles.contains(bubbleChildren.get(i).getKey())) {
1068 // Experimental bubble! Intercept the removal.
1069 return true;
1070 }
1071 }
1072
1073 // Not an experimental bubble, safe to remove.
1074 mBubbleData.removeSuppressedSummary(groupKey);
1075 // Remove any associated bubble children with the summary.
1076 for (int i = 0; i < bubbleChildren.size(); i++) {
1077 Bubble bubbleChild = bubbleChildren.get(i);
1078 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
1079 DISMISS_GROUP_CANCELLED);
1080 }
1081 return false;
1082 }
1083 }
1084
1085 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001086 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -07001087 * Updates the visibility of the bubbles based on current state.
1088 * Does not un-bubble, just hides or un-hides. Notifies any
1089 * {@link BubbleStateChangeListener}s of visibility changes.
1090 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001091 */
Lyn Han6c40fe72019-05-08 14:06:33 -07001092 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001093 if (mStackView == null) {
1094 return;
Mady Mellord1c78b262018-11-06 18:04:40 -08001095 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001096 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
1097 // Bubbles only appear in unlocked shade
1098 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +00001099 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001100 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -08001101 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001102
Mady Mellor698d9e82019-08-01 23:11:53 +00001103 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001104 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001105 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001106 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001107 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1108 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1109 }
1110
1111 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001112 }
1113
1114 /**
1115 * Rect indicating the touchable region for the bubble stack / expanded stack.
1116 */
1117 public Rect getTouchableRegion() {
1118 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1119 return null;
1120 }
1121 mStackView.getBoundsOnScreen(mTempRect);
1122 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001123 }
1124
Mady Mellor390bff42019-04-05 15:09:01 -07001125 /**
1126 * The display id of the expanded view, if the stack is expanded and not occluded by the
1127 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1128 */
1129 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001130 final Bubble bubble = getExpandedBubble(context);
1131 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
1132 }
1133
1134 @Nullable
1135 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001136 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001137 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001138 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001139 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001140 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +02001141 final Bubble expandedBubble = mStackView.getExpandedBubble();
1142 if (defaultDisplay && expandedBubble != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001143 && !mNotificationShadeWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001144 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -07001145 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001146 return null;
Mady Mellor390bff42019-04-05 15:09:01 -07001147 }
1148
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001149 @VisibleForTesting
1150 BubbleStackView getStackView() {
1151 return mStackView;
1152 }
1153
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001154 /**
1155 * Description of current bubble state.
1156 */
1157 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1158 pw.println("BubbleController state:");
1159 mBubbleData.dump(fd, pw, args);
1160 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001161 if (mStackView != null) {
1162 mStackView.dump(fd, pw, args);
1163 }
1164 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001165 }
1166
Mady Mellore80930e2019-03-21 16:00:45 -07001167 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001168 * This task stack listener is responsible for responding to tasks moved to the front
1169 * which are on the default (main) display. When this happens, expanded bubbles must be
1170 * collapsed so the user may interact with the app which was just moved to the front.
1171 * <p>
1172 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1173 * these calls via a main thread Handler.
1174 */
1175 @MainThread
1176 private class BubbleTaskStackListener extends TaskStackChangeListener {
1177
Mark Renoufcecc77b2019-01-30 16:32:24 -05001178 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001179 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1180 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001181 if (!mStackView.isExpansionAnimating()) {
1182 mBubbleData.setExpanded(false);
1183 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001184 }
1185 }
1186
Mark Renoufcecc77b2019-01-30 16:32:24 -05001187 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001188 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001189 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001190 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001191 }
1192 }
Mark Renouf446251d2019-04-26 10:22:41 -04001193
1194 @Override
1195 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1196 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1197 mBubbleData.setExpanded(false);
1198 }
1199 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001200
1201 @Override
1202 public void onSingleTaskDisplayDrawn(int displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -08001203 if (mStackView == null) {
1204 return;
Issei Suzukicac2a502019-04-16 16:52:50 +02001205 }
Lyn Hana0bb02e2020-01-28 17:57:27 -08001206 mStackView.showExpandedViewContents(displayId);
Issei Suzukicac2a502019-04-16 16:52:50 +02001207 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001208
1209 @Override
1210 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001211 final Bubble expandedBubble = mStackView != null
1212 ? mStackView.getExpandedBubble()
1213 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001214 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1215 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001216 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001217 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001218 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001219 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001220 }
1221
Mady Mellorca0c24c2019-05-16 16:14:32 -07001222 /**
1223 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1224 *
1225 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1226 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1227 *
1228 * @param context the context to use.
1229 * @param entry the entry to bubble.
1230 */
1231 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1232 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001233 ? entry.getBubbleMetadata().getBubbleIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001234 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001235 if (entry.getBubbleMetadata() != null
1236 && entry.getBubbleMetadata().getShortcutId() != null) {
1237 return true;
1238 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001239 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001240 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001241 return false;
1242 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001243 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1244 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001245 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001246 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001247 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001248 Log.w(TAG, "Unable to send as bubble, "
1249 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001250 + intent);
1251 return false;
1252 }
1253 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001254 Log.w(TAG, "Unable to send as bubble, "
1255 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001256 + intent);
1257 return false;
1258 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001259 return true;
1260 }
1261
Joshua Tsujia19515f2019-02-13 18:02:29 -05001262 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001263 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001264 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001265 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1266 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1267 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001268 }
1269 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001270 }
Aran Inkaa4dfa72019-11-18 16:49:07 -05001271
1272 // TODO: Copied from RemoteInputView. Consolidate RemoteInput intent logic.
1273 private Intent prepareRemoteInputFromData(String contentType, Uri data,
1274 RemoteInput remoteInput, NotificationEntry entry) {
1275 HashMap<String, Uri> results = new HashMap<>();
1276 results.put(contentType, data);
1277 mRemoteInputUriController.grantInlineReplyUriPermission(entry.getSbn(), data);
1278 Intent fillInIntent = new Intent().addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
1279 RemoteInput.addDataResultToIntent(remoteInput, fillInIntent, results);
1280
1281 return fillInIntent;
1282 }
1283
1284 // TODO: Copied from RemoteInputView. Consolidate RemoteInput intent logic.
1285 private void sendRemoteInput(Intent intent, NotificationEntry entry,
1286 PendingIntent pendingIntent) {
1287 // Tell ShortcutManager that this package has been "activated". ShortcutManager
1288 // will reset the throttling for this package.
1289 // Strictly speaking, the intent receiver may be different from the notification publisher,
1290 // but that's an edge case, and also because we can't always know which package will receive
1291 // an intent, so we just reset for the publisher.
1292 mContext.getSystemService(ShortcutManager.class).onApplicationActive(
1293 entry.getSbn().getPackageName(),
1294 entry.getSbn().getUser().getIdentifier());
1295
1296 try {
1297 pendingIntent.send(mContext, 0, intent);
1298 } catch (PendingIntent.CanceledException e) {
1299 Log.i(TAG, "Unable to send remote input result", e);
1300 }
1301 }
1302
1303 private void sendScreenshotToBubble(Bubble bubble) {
Aran Ink141a8152019-12-12 13:31:23 -05001304 mScreenshotHelper.takeScreenshot(
1305 android.view.WindowManager.TAKE_SCREENSHOT_FULLSCREEN,
1306 true /* hasStatus */,
1307 true /* hasNav */,
1308 mHandler,
1309 new Consumer<Uri>() {
1310 @Override
1311 public void accept(Uri uri) {
1312 if (uri != null) {
1313 NotificationEntry entry = bubble.getEntry();
1314 Pair<RemoteInput, Notification.Action> pair = entry.getSbn()
1315 .getNotification().findRemoteInputActionPair(false);
1316 if (pair != null) {
1317 RemoteInput remoteInput = pair.first;
1318 Notification.Action action = pair.second;
1319 Intent dataIntent = prepareRemoteInputFromData("image/png", uri,
1320 remoteInput, entry);
1321 sendRemoteInput(dataIntent, entry, action.actionIntent);
1322 mBubbleData.setSelectedBubble(bubble);
1323 mBubbleData.setExpanded(true);
1324 } else {
1325 Log.w(TAG, "No RemoteInput found for notification: "
1326 + entry.getSbn().getKey());
Aran Inkaa4dfa72019-11-18 16:49:07 -05001327 }
Aran Ink141a8152019-12-12 13:31:23 -05001328 }
1329 }
1330 });
Aran Inkaa4dfa72019-11-18 16:49:07 -05001331 }
1332
1333 private final BubbleScreenshotListener mBubbleScreenshotListener =
1334 bubble -> sendScreenshotToBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001335}