blob: 2ed5b10fd5644ffec67e4fef5f6d3f87dd0b2cd2 [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
Mady Mellor5549dd22018-11-06 18:07:34 -0800193 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800194 * Listener to be notified when some states of the bubbles change.
195 */
196 public interface BubbleStateChangeListener {
197 /**
198 * Called when the stack has bubbles or no longer has bubbles.
199 */
200 void onHasBubblesChanged(boolean hasBubbles);
201 }
202
Mady Mellorcd9b1302018-11-06 18:08:04 -0800203 /**
204 * Listener to find out about stack expansion / collapse events.
205 */
206 public interface BubbleExpandListener {
207 /**
208 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700209 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800210 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800211 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800212 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800213 void onBubbleExpandChanged(boolean isExpanding, String key);
214 }
215
216 /**
Aran Inkaa4dfa72019-11-18 16:49:07 -0500217 * Listener for handling bubble screenshot events.
218 */
219 public interface BubbleScreenshotListener {
220 /**
221 * Called to trigger taking a screenshot and sending the result to a bubble.
222 */
223 void onBubbleScreenshot(Bubble bubble);
224 }
225
226 /**
Mady Mellorf44b6832020-01-14 13:26:14 -0800227 * Listener to be notified when a bubbles' notification suppression state changes.
228 */
229 public interface NotificationSuppressionChangedListener {
230 /**
231 * Called when the notification suppression state of a bubble changes.
232 */
233 void onBubbleNotificationSuppressionChange(Bubble bubble);
234
235 }
236
237 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800238 * Listens for the current state of the status bar and updates the visibility state
239 * of bubbles as needed.
240 */
241 private class StatusBarStateListener implements StatusBarStateController.StateListener {
242 private int mState;
243 /**
244 * Returns the current status bar state.
245 */
246 public int getCurrentState() {
247 return mState;
248 }
249
250 @Override
251 public void onStateChanged(int newState) {
252 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400253 boolean shouldCollapse = (mState != SHADE);
254 if (shouldCollapse) {
255 collapseStack();
256 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700257 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800258 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800259 }
260
Jason Monk27d01a622018-12-10 15:57:09 -0500261 @Inject
Mady Mellor7f234902019-10-20 12:06:29 -0700262 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800263 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700264 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800265 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700266 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700267 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400268 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400269 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700270 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700271 NotificationGroupManager groupManager,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500272 NotificationEntryManager entryManager,
273 RemoteInputUriController remoteInputUriController) {
wilsonshihe8321942019-10-18 18:39:46 +0800274 this(context, notificationShadeWindowController, statusBarStateController, shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700275 data, null /* synchronizer */, configurationController, interruptionStateProvider,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500276 zenModeController, notifUserManager, groupManager, entryManager,
277 remoteInputUriController);
Mady Mellor7f234902019-10-20 12:06:29 -0700278 }
279
280 public BubbleController(Context context,
wilsonshihe8321942019-10-18 18:39:46 +0800281 NotificationShadeWindowController notificationShadeWindowController,
Mady Mellor7f234902019-10-20 12:06:29 -0700282 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800283 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700284 BubbleData data,
285 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
286 ConfigurationController configurationController,
287 NotificationInterruptionStateProvider interruptionStateProvider,
288 ZenModeController zenModeController,
289 NotificationLockscreenUserManager notifUserManager,
290 NotificationGroupManager groupManager,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500291 NotificationEntryManager entryManager,
292 RemoteInputUriController remoteInputUriController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800293 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800294 mShadeController = shadeController;
Mady Melloraa8fef22019-04-11 13:36:40 -0700295 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400296 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400297 mZenModeController = zenModeController;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500298 mRemoteInputUriController = remoteInputUriController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400299 mZenModeController.addCallback(new ZenModeController.Callback() {
300 @Override
301 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800302 for (Bubble b : mBubbleData.getBubbles()) {
303 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700304 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400305 }
306
307 @Override
308 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800309 for (Bubble b : mBubbleData.getBubbles()) {
310 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700311 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400312 }
313 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700314
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700315 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800316
Mady Mellorf44b6832020-01-14 13:26:14 -0800317 mMaxBubbles = mContext.getResources().getInteger(R.integer.bubbles_max_rendered);
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400318 mBubbleData = data;
319 mBubbleData.setListener(mBubbleDataListener);
Mady Mellorf44b6832020-01-14 13:26:14 -0800320 mBubbleData.setSuppressionChangedListener(new NotificationSuppressionChangedListener() {
321 @Override
322 public void onBubbleNotificationSuppressionChange(Bubble bubble) {
323 // Make sure NoMan knows it's not showing in the shade anymore so anyone querying it
324 // can tell.
325 try {
326 mBarService.onBubbleNotificationSuppressionChanged(bubble.getKey(),
327 !bubble.showInShade());
328 } catch (RemoteException e) {
329 // Bad things have happened
330 }
331 }
332 });
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400333
Mady Mellor7f234902019-10-20 12:06:29 -0700334 mNotificationEntryManager = entryManager;
Ned Burns01e38212019-01-03 16:32:52 -0500335 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700336 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellor22f2f072019-04-18 13:26:18 -0700337 mNotificationGroupManager = groupManager;
Mady Mellor740d85d2019-07-09 15:26:47 -0700338 mNotificationGroupManager.addOnGroupChangeListener(
339 new NotificationGroupManager.OnGroupChangeListener() {
340 @Override
341 public void onGroupSuppressionChanged(
342 NotificationGroupManager.NotificationGroup group,
343 boolean suppressed) {
344 // More notifications could be added causing summary to no longer
345 // be suppressed -- in this case need to remove the key.
346 final String groupKey = group.summary != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400347 ? group.summary.getSbn().getGroupKey()
Mady Mellor740d85d2019-07-09 15:26:47 -0700348 : null;
349 if (!suppressed && groupKey != null
350 && mBubbleData.isSummarySuppressed(groupKey)) {
351 mBubbleData.removeSuppressedSummary(groupKey);
352 }
353 }
354 });
Mady Mellorb4991e62019-01-10 15:14:51 -0800355
wilsonshihe8321942019-10-18 18:39:46 +0800356 mNotificationShadeWindowController = notificationShadeWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800357 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700358 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500359
Mark Renoufcecc77b2019-01-30 16:32:24 -0500360 mTaskStackListener = new BubbleTaskStackListener();
361 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800362
Joshua Tsujia19515f2019-02-13 18:02:29 -0500363 try {
364 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
365 } catch (RemoteException e) {
366 e.printStackTrace();
367 }
Issei Suzukic0387542019-03-08 17:31:14 +0100368 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700369
370 mBarService = IStatusBarService.Stub.asInterface(
371 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400372
373 mSavedBubbleKeysPerUser = new SparseSetArray<>();
374 mCurrentUserId = mNotifUserManager.getCurrentUserId();
375 mNotifUserManager.addUserChangedListener(
Steve Elliottb47f1c72019-12-19 12:39:26 -0500376 new NotificationLockscreenUserManager.UserChangedListener() {
377 @Override
378 public void onUserChanged(int newUserId) {
379 BubbleController.this.saveBubbles(mCurrentUserId);
380 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
381 BubbleController.this.restoreBubbles(newUserId);
382 mCurrentUserId = newUserId;
383 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400384 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800385
386 mUserCreatedBubbles = new HashSet<>();
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800387 mUserBlockedBubbles = new HashSet<>();
Aran Inkaa4dfa72019-11-18 16:49:07 -0500388
389 mScreenshotHelper = new ScreenshotHelper(context);
Mady Mellor247ca2c2019-12-02 16:18:59 -0800390 mBubbleIconFactory = new BubbleIconFactory(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800391 }
392
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400393 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800394 * Sets whether to perform inflation on the same thread as the caller. This method should only
395 * be used in tests, not in production.
396 */
397 @VisibleForTesting
398 void setInflateSynchronously(boolean inflateSynchronously) {
399 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800400 }
401
Lyn Hanb58c7562020-01-07 14:29:20 -0800402 void setOverflowCallback(Runnable updateOverflow) {
403 mOverflowCallback = updateOverflow;
404 }
405
406 /**
407 * @return Bubbles for updating overflow.
408 */
409 List<Bubble> getOverflowBubbles() {
410 return mBubbleData.getOverflowBubbles();
411 }
412
413
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400414 /**
415 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
416 * method initializes the stack view and adds it to the StatusBar just above the scrim.
417 */
418 private void ensureStackViewCreated() {
419 if (mStackView == null) {
420 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
wilsonshihe8321942019-10-18 18:39:46 +0800421 ViewGroup nsv = mNotificationShadeWindowController.getNotificationShadeView();
422 int bubbleScrimIndex = nsv.indexOfChild(nsv.findViewById(R.id.scrim_for_bubble));
Lyn Hanbde48202019-05-29 19:18:29 -0700423 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
wilsonshihe8321942019-10-18 18:39:46 +0800424 nsv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400425 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
426 if (mExpandListener != null) {
427 mStackView.setExpandListener(mExpandListener);
428 }
Aran Inkaa4dfa72019-11-18 16:49:07 -0500429 if (mBubbleScreenshotListener != null) {
430 mStackView.setBubbleScreenshotListener(mBubbleScreenshotListener);
431 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400432 }
433 }
434
Mark Renoufc19b4732019-06-26 12:08:33 -0400435 /**
436 * Records the notification key for any active bubbles. These are used to restore active
437 * bubbles when the user returns to the foreground.
438 *
439 * @param userId the id of the user
440 */
441 private void saveBubbles(@UserIdInt int userId) {
442 // First clear any existing keys that might be stored.
443 mSavedBubbleKeysPerUser.remove(userId);
444 // Add in all active bubbles for the current user.
445 for (Bubble bubble: mBubbleData.getBubbles()) {
446 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
447 }
448 }
449
450 /**
451 * Promotes existing notifications to Bubbles if they were previously bubbles.
452 *
453 * @param userId the id of the user
454 */
455 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400456 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
457 if (savedBubbleKeys == null) {
458 // There were no bubbles saved for this used.
459 return;
460 }
Evan Laird181de622019-10-24 09:53:02 -0400461 for (NotificationEntry e :
462 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400463 if (savedBubbleKeys.contains(e.getKey())
Mark Renoufc19b4732019-06-26 12:08:33 -0400464 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
465 && canLaunchInActivityView(mContext, e)) {
466 updateBubble(e, /* suppressFlyout= */ true);
467 }
468 }
469 // Finally, remove the entries for this user now that bubbles are restored.
470 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
471 }
472
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700473 @Override
474 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800475 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700476 }
477
478 @Override
479 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800480 updateForThemeChanges();
481 }
482
483 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700484 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700485 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700486 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800487 mBubbleIconFactory = new BubbleIconFactory(mContext);
488 for (Bubble b: mBubbleData.getBubbles()) {
489 // Reload each bubble
490 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
491 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700492 }
493
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400494 @Override
495 public void onConfigChanged(Configuration newConfig) {
496 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400497 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700498 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400499 }
500 }
501
Mady Mellor5549dd22018-11-06 18:07:34 -0800502 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800503 * Set a listener to be notified when some states of the bubbles change.
504 */
505 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
506 mStateChangeListener = listener;
507 }
508
509 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800510 * Set a listener to be notified of bubble expand events.
511 */
512 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100513 mExpandListener = ((isExpanding, key) -> {
514 if (listener != null) {
515 listener.onBubbleExpandChanged(isExpanding, key);
516 }
wilsonshihe8321942019-10-18 18:39:46 +0800517 mNotificationShadeWindowController.setBubbleExpanded(isExpanding);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100518 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800519 if (mStackView != null) {
520 mStackView.setExpandListener(mExpandListener);
521 }
522 }
523
524 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800525 * Whether or not there are bubbles present, regardless of them being visible on the
526 * screen (e.g. if on AOD).
527 */
528 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800529 if (mStackView == null) {
530 return false;
531 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400532 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800533 }
534
535 /**
536 * Whether the stack of bubbles is expanded or not.
537 */
538 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400539 return mBubbleData.isExpanded();
540 }
541
542 /**
543 * Tell the stack of bubbles to expand.
544 */
545 public void expandStack() {
546 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800547 }
548
549 /**
550 * Tell the stack of bubbles to collapse.
551 */
552 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400553 mBubbleData.setExpanded(false /* expanded */);
554 }
555
Mady Mellorce23c462019-06-17 17:30:07 -0700556 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700557 * True if either:
558 * (1) There is a bubble associated with the provided key and if its notification is hidden
559 * from the shade.
560 * (2) There is a group summary associated with the provided key that is hidden from the shade
561 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700562 *
Mady Mellore28fe102019-07-09 15:33:32 -0700563 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700564 */
565 public boolean isBubbleNotificationSuppressedFromShade(String key) {
Mady Mellore28fe102019-07-09 15:33:32 -0700566 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800567 && !mBubbleData.getBubbleWithKey(key).showInShade();
Evan Laird181de622019-10-24 09:53:02 -0400568 NotificationEntry entry = mNotificationEntryManager.getActiveNotificationUnfiltered(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400569 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellore28fe102019-07-09 15:33:32 -0700570 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700571 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
572 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700573 }
574
Mark Renouf71a3af62019-04-08 15:02:54 -0400575 @VisibleForTesting
576 void selectBubble(String key) {
577 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellor247ca2c2019-12-02 16:18:59 -0800578 mBubbleData.setSelectedBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800579 }
580
Lyn Hanb58c7562020-01-07 14:29:20 -0800581 void promoteBubbleFromOverflow(Bubble bubble) {
582 mBubbleData.promoteBubbleFromOverflow(bubble);
583 }
584
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800585 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400586 * Request the stack expand if needed, then select the specified Bubble as current.
587 *
588 * @param notificationKey the notification key for the bubble to be selected
589 */
590 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400591 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
592 if (bubble != null) {
593 mBubbleData.setSelectedBubble(bubble);
594 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400595 }
596 }
597
598 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800599 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
600 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500601 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400602 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800603 }
604
605 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500606 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
607 * is forwarded a back key down/up pair.
608 */
609 public void performBackPressIfNeeded() {
610 if (mStackView != null) {
611 mStackView.performBackPressIfNeeded();
612 }
613 }
614
615 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800616 * Adds or updates a bubble associated with the provided notification entry.
617 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400618 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800619 */
Mark Renouff97ed462019-04-05 13:46:24 -0400620 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700621 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400622 }
623
624 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700625 updateBubble(notif, suppressFlyout, true /* showInShade */);
626 }
627
628 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800629 if (mStackView == null) {
630 // Lazy init stack view when a bubble is created
631 ensureStackViewCreated();
632 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700633 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400634 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700635 notif.setInterruption();
636 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800637 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
638 bubble.setInflateSynchronously(mInflateSynchronously);
639 bubble.inflate(
640 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
641 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700642 }
643
644 /**
645 * Called when a user has indicated that an active notification should be shown as a bubble.
646 * <p>
647 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
648 * the notification from appearing in the shade.
649 *
650 * @param entry the notification to show as a bubble.
651 */
652 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800653 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
654 Log.d(TAG, "onUserCreatedBubble: " + entry.getKey());
655 }
Heemin Seogba6337f2019-12-10 15:34:37 -0800656 mShadeController.collapsePanel(true);
Mady Mellor7f234902019-10-20 12:06:29 -0700657 entry.setFlagBubble(true);
658 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
Mady Mellorff076eb2019-11-13 10:12:06 -0800659 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800660 mUserBlockedBubbles.remove(entry.getKey());
Mady Mellor7f234902019-10-20 12:06:29 -0700661 }
662
663 /**
664 * Called when a user has indicated that an active notification appearing as a bubble should
665 * no longer be shown as a bubble.
666 *
667 * @param entry the notification to no longer show as a bubble.
668 */
669 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800670 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
671 Log.d(TAG, "onUserDemotedBubble: " + entry.getKey());
672 }
Mady Mellor7f234902019-10-20 12:06:29 -0700673 entry.setFlagBubble(false);
674 removeBubble(entry.getKey(), DISMISS_BLOCKED);
Mady Mellorff076eb2019-11-13 10:12:06 -0800675 mUserCreatedBubbles.remove(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800676 if (BubbleExperimentConfig.isPackageWhitelistedToAutoBubble(
677 mContext, entry.getSbn().getPackageName())) {
678 // This package is whitelist but user demoted the bubble, let's save it so we don't
679 // auto-bubble for the whitelist again.
680 mUserBlockedBubbles.add(entry.getKey());
681 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800682 }
683
684 /**
685 * Whether this bubble was explicitly created by the user via a SysUI affordance.
686 */
687 boolean isUserCreatedBubble(String key) {
688 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800689 }
690
691 /**
692 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500693 * <p>
694 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800695 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500696 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500697 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400698 // TEMP: refactor to change this to pass entry
699 Bubble bubble = mBubbleData.getBubbleWithKey(key);
700 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700701 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800702 }
703 }
704
Ned Burns01e38212019-01-03 16:32:52 -0500705 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700706 private final NotificationRemoveInterceptor mRemoveInterceptor =
707 new NotificationRemoveInterceptor() {
708 @Override
709 public boolean onNotificationRemoveRequested(String key, int reason) {
Evan Laird181de622019-10-24 09:53:02 -0400710 NotificationEntry entry =
711 mNotificationEntryManager.getActiveNotificationUnfiltered(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400712 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellor22f2f072019-04-18 13:26:18 -0700713 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
714
715 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
Mady Mellore28fe102019-07-09 15:33:32 -0700716 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
717 && mBubbleData.getSummaryKey(groupKey).equals(key));
Mady Mellor22f2f072019-04-18 13:26:18 -0700718 boolean isSummary = entry != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400719 && entry.getSbn().getNotification().isGroupSummary();
Mady Mellore28fe102019-07-09 15:33:32 -0700720 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
721 && bubbleChildren != null && !bubbleChildren.isEmpty();
Mady Mellor22f2f072019-04-18 13:26:18 -0700722
723 if (!inBubbleData && !isSummaryOfBubbles) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700724 return false;
725 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700726
727 final boolean isClearAll = reason == REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700728 final boolean isUserDimiss = reason == REASON_CANCEL || reason == REASON_CLICK;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700729 final boolean isAppCancel = reason == REASON_APP_CANCEL
730 || reason == REASON_APP_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700731 final boolean isSummaryCancel = reason == REASON_GROUP_SUMMARY_CANCELED;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700732
733 // Need to check for !appCancel here because the notification may have
734 // previously been dismissed & entry.isRowDismissed would still be true
Mady Mellorca184aae2019-09-17 16:07:12 -0700735 boolean userRemovedNotif = (entry != null && entry.isRowDismissed() && !isAppCancel)
Mady Mellor22f2f072019-04-18 13:26:18 -0700736 || isClearAll || isUserDimiss || isSummaryCancel;
737
738 if (isSummaryOfBubbles) {
739 return handleSummaryRemovalInterception(entry, userRemovedNotif);
740 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700741
742 // The bubble notification sticks around in the data as long as the bubble is
743 // not dismissed and the app hasn't cancelled the notification.
Mady Mellor22f2f072019-04-18 13:26:18 -0700744 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellorca184aae2019-09-17 16:07:12 -0700745 boolean bubbleExtended = entry != null && entry.isBubble() && userRemovedNotif;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700746 if (bubbleExtended) {
Mady Mellorf44b6832020-01-14 13:26:14 -0800747 bubble.setSuppressNotification(true);
Mady Mellorb8aaf972019-11-26 10:28:00 -0800748 bubble.setShowDot(false /* show */, true /* animate */);
Beverly85d4c192019-09-30 11:40:39 -0400749 mNotificationEntryManager.updateNotifications(
750 "BubbleController.onNotificationRemoveRequested");
Mady Mellorc2ff0112019-03-28 14:18:06 -0700751 return true;
Mady Mellorff076eb2019-11-13 10:12:06 -0800752 } else if (!userRemovedNotif && entry != null
753 && !isUserCreatedBubble(bubble.getKey())) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700754 // This wasn't a user removal so we should remove the bubble as well
755 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
756 return false;
757 }
758 return false;
759 }
760 };
761
Mady Mellor22f2f072019-04-18 13:26:18 -0700762 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
763 boolean userRemovedNotif) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400764 String groupKey = summary.getSbn().getGroupKey();
Mady Mellor22f2f072019-04-18 13:26:18 -0700765 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
766
767 if (userRemovedNotif) {
768 // If it's a user dismiss we mark the children to be hidden from the shade.
769 for (int i = 0; i < bubbleChildren.size(); i++) {
770 Bubble bubbleChild = bubbleChildren.get(i);
771 // As far as group manager is concerned, once a child is no longer shown
772 // in the shade, it is essentially removed.
773 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
Mady Mellorf44b6832020-01-14 13:26:14 -0800774 bubbleChild.setSuppressNotification(true);
Mady Mellorb8aaf972019-11-26 10:28:00 -0800775 bubbleChild.setShowDot(false /* show */, true /* animate */);
Mady Mellor22f2f072019-04-18 13:26:18 -0700776 }
777 // And since all children are removed, remove the summary.
778 mNotificationGroupManager.onEntryRemoved(summary);
779
780 // If the summary was auto-generated we don't need to keep that notification around
781 // because apps can't cancel it; so we only intercept & suppress real summaries.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400782 boolean isAutogroupSummary = (summary.getSbn().getNotification().flags
Mady Mellor22f2f072019-04-18 13:26:18 -0700783 & FLAG_AUTOGROUP_SUMMARY) != 0;
Mady Mellore28fe102019-07-09 15:33:32 -0700784 if (!isAutogroupSummary) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400785 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
786 summary.getKey());
Mady Mellore28fe102019-07-09 15:33:32 -0700787 // Tell shade to update for the suppression
Beverly85d4c192019-09-30 11:40:39 -0400788 mNotificationEntryManager.updateNotifications(
789 "BubbleController.handleSummaryRemovalInterception");
Mady Mellore28fe102019-07-09 15:33:32 -0700790 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700791 return !isAutogroupSummary;
792 } else {
Mady Mellore28fe102019-07-09 15:33:32 -0700793 // If it's not a user dismiss it's a cancel.
Mady Mellor84094222019-12-17 15:04:22 -0800794 for (int i = 0; i < bubbleChildren.size(); i++) {
795 // First check if any of these are user-created (i.e. experimental bubbles)
796 if (mUserCreatedBubbles.contains(bubbleChildren.get(i).getKey())) {
797 // Experimental bubble! Intercept the removal.
798 return true;
799 }
800 }
801 // Not an experimental bubble, safe to remove.
Mady Mellore28fe102019-07-09 15:33:32 -0700802 mBubbleData.removeSuppressedSummary(groupKey);
Mady Mellor84094222019-12-17 15:04:22 -0800803 // Remove any associated bubble children with the summary.
Mady Mellor22f2f072019-04-18 13:26:18 -0700804 for (int i = 0; i < bubbleChildren.size(); i++) {
805 Bubble bubbleChild = bubbleChildren.get(i);
806 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
807 DISMISS_GROUP_CANCELLED);
808 }
809 return false;
810 }
811 }
812
Mady Mellorc2ff0112019-03-28 14:18:06 -0700813 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500814 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
815 @Override
Mady Mellor58dc5192019-12-16 13:49:56 -0800816 public void onNotificationAdded(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800817 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800818 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
Mady Mellor30672942019-12-04 15:43:19 -0800819 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800820 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700821
Mady Mellorca0c24c2019-05-16 16:14:32 -0700822 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
Mady Mellor30672942019-12-04 15:43:19 -0800823 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
Mady Mellorea13b232019-12-05 15:55:46 -0800824 if (wasAdjusted && !previouslyUserCreated) {
825 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
826 mUserCreatedBubbles.add(entry.getKey());
827 }
Mark Renouff97ed462019-04-05 13:46:24 -0400828 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800829 }
830 }
831
832 @Override
833 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800834 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800835 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
Mady Mellor30672942019-12-04 15:43:19 -0800836 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800837 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700838
Mady Mellorca0c24c2019-05-16 16:14:32 -0700839 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
Mady Mellor30672942019-12-04 15:43:19 -0800840 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400841 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
Mady Melloraa8fef22019-04-11 13:36:40 -0700842 // It was previously a bubble but no longer a bubble -- lets remove it
Ned Burns00b4b2d2019-10-17 22:09:27 -0400843 removeBubble(entry.getKey(), DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700844 } else if (shouldBubble) {
Mady Mellorea13b232019-12-05 15:55:46 -0800845 if (wasAdjusted && !previouslyUserCreated) {
846 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
847 mUserCreatedBubbles.add(entry.getKey());
848 }
Mark Renouff97ed462019-04-05 13:46:24 -0400849 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800850 }
851 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400852
853 @Override
854 public void onNotificationRankingUpdated(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 };
859
Mark Renouf71a3af62019-04-08 15:02:54 -0400860 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400861 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400862
Mark Renouf3bc5b362019-04-05 14:37:59 -0400863 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400864 public void applyUpdate(BubbleData.Update update) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800865 // Update bubbles in overflow.
866 if (mOverflowCallback != null) {
867 mOverflowCallback.run();
868 }
869
Mark Renouf82a40e62019-05-23 16:16:24 -0400870 if (update.addedBubble != null) {
871 mStackView.addBubble(update.addedBubble);
872 }
873
874 // Collapsing? Do this first before remaining steps.
875 if (update.expandedChanged && !update.expanded) {
876 mStackView.setExpanded(false);
877 }
878
879 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700880 ArrayList<Pair<Bubble, Integer>> removedBubbles =
881 new ArrayList<>(update.removedBubbles);
882 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400883 final Bubble bubble = removed.first;
884 @DismissReason final int reason = removed.second;
885 mStackView.removeBubble(bubble);
886
Mark Renoufc19b4732019-06-26 12:08:33 -0400887 // If the bubble is removed for user switching, leave the notification in place.
888 if (reason != DISMISS_USER_CHANGED) {
889 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800890 && !bubble.showInShade()) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400891 // The bubble is gone & the notification is gone, time to actually remove it
892 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400893 bubble.getEntry().getSbn(), UNDEFINED_DISMISS_REASON);
Mark Renoufc19b4732019-06-26 12:08:33 -0400894 } else {
895 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400896 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700897
Mark Renoufc19b4732019-06-26 12:08:33 -0400898 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
899 // will get right result back
900 try {
901 mBarService.onNotificationBubbleChanged(bubble.getKey(),
902 false /* isBubble */);
903 } catch (RemoteException e) {
904 // Bad things have happened
905 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400906 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700907
Mady Mellore28fe102019-07-09 15:33:32 -0700908 // Check if removed bubble has an associated suppressed group summary that needs
909 // to be removed now.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400910 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700911 if (mBubbleData.isSummarySuppressed(groupKey)
912 && mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
913 // Time to actually remove the summary.
914 String notifKey = mBubbleData.getSummaryKey(groupKey);
915 mBubbleData.removeSuppressedSummary(groupKey);
916 NotificationEntry entry =
Evan Laird181de622019-10-24 09:53:02 -0400917 mNotificationEntryManager.getActiveNotificationUnfiltered(notifKey);
Mady Mellore28fe102019-07-09 15:33:32 -0700918 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400919 entry.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellore28fe102019-07-09 15:33:32 -0700920 }
921
Mady Mellor22f2f072019-04-18 13:26:18 -0700922 // Check if summary should be removed from NoManGroup
923 NotificationEntry summary = mNotificationGroupManager.getLogicalGroupSummary(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400924 bubble.getEntry().getSbn());
Mady Mellor22f2f072019-04-18 13:26:18 -0700925 if (summary != null) {
926 ArrayList<NotificationEntry> summaryChildren =
Ned Burns00b4b2d2019-10-17 22:09:27 -0400927 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
928 boolean isSummaryThisNotif = summary.getKey().equals(
929 bubble.getEntry().getKey());
Mady Mellore4348272019-07-29 17:43:36 -0700930 if (!isSummaryThisNotif
931 && (summaryChildren == null || summaryChildren.isEmpty())) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700932 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400933 summary.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellor22f2f072019-04-18 13:26:18 -0700934 }
935 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700936 }
937 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400938
Mark Renouf82a40e62019-05-23 16:16:24 -0400939 if (update.updatedBubble != null) {
940 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400941 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400942
Lyn Hanb58c7562020-01-07 14:29:20 -0800943 // At this point, the correct bubbles are inflated in the stack.
944 // Make sure the order in bubble data is reflected in bubble row.
Mark Renouf82a40e62019-05-23 16:16:24 -0400945 if (update.orderChanged) {
946 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400947 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400948
Mark Renouf82a40e62019-05-23 16:16:24 -0400949 if (update.selectionChanged) {
950 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700951 if (update.selectedBubble != null) {
952 mNotificationGroupManager.updateSuppression(
953 update.selectedBubble.getEntry());
954 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400955 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400956
Mark Renouf82a40e62019-05-23 16:16:24 -0400957 // Expanding? Apply this last.
958 if (update.expandedChanged && update.expanded) {
959 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400960 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400961
Beverly85d4c192019-09-30 11:40:39 -0400962 mNotificationEntryManager.updateNotifications(
963 "BubbleData.Listener.applyUpdate");
Lyn Han6c40fe72019-05-08 14:06:33 -0700964 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400965
Issei Suzukia8d07312019-06-07 12:56:19 +0200966 if (DEBUG_BUBBLE_CONTROLLER) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800967 Log.d(TAG, "\n[BubbleData] bubbles:");
Lyn Han767d70e2019-12-10 18:02:23 -0800968 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400969 mBubbleData.getSelectedBubble()));
970
971 if (mStackView != null) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800972 Log.d(TAG, "\n[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -0800973 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400974 mStackView.getExpandedBubble()));
975 }
Lyn Hanb58c7562020-01-07 14:29:20 -0800976 Log.d(TAG, "\n[BubbleData] overflow:");
977 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getOverflowBubbles(),
978 null));
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400979 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400980 }
981 };
982
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800983 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400984 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700985 * Updates the visibility of the bubbles based on current state.
986 * Does not un-bubble, just hides or un-hides. Notifies any
987 * {@link BubbleStateChangeListener}s of visibility changes.
988 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800989 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700990 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800991 if (mStackView == null) {
992 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800993 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800994 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
995 // Bubbles only appear in unlocked shade
996 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +0000997 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800998 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800999 }
Lyn Han6c40fe72019-05-08 14:06:33 -07001000
Mady Mellor698d9e82019-08-01 23:11:53 +00001001 // Let listeners know if bubble state changed.
wilsonshihe8321942019-10-18 18:39:46 +08001002 boolean hadBubbles = mNotificationShadeWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +00001003 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
wilsonshihe8321942019-10-18 18:39:46 +08001004 mNotificationShadeWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -07001005 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
1006 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
1007 }
1008
1009 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -08001010 }
1011
1012 /**
1013 * Rect indicating the touchable region for the bubble stack / expanded stack.
1014 */
1015 public Rect getTouchableRegion() {
1016 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
1017 return null;
1018 }
1019 mStackView.getBoundsOnScreen(mTempRect);
1020 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001021 }
1022
Mady Mellor390bff42019-04-05 15:09:01 -07001023 /**
1024 * The display id of the expanded view, if the stack is expanded and not occluded by the
1025 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
1026 */
1027 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001028 final Bubble bubble = getExpandedBubble(context);
1029 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
1030 }
1031
1032 @Nullable
1033 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -07001034 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001035 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -07001036 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001037 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -07001038 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +02001039 final Bubble expandedBubble = mStackView.getExpandedBubble();
1040 if (defaultDisplay && expandedBubble != null && isStackExpanded()
wilsonshihe8321942019-10-18 18:39:46 +08001041 && !mNotificationShadeWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +02001042 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -07001043 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001044 return null;
Mady Mellor390bff42019-04-05 15:09:01 -07001045 }
1046
Mady Mellorf6e3ac02019-01-29 10:37:52 -08001047 @VisibleForTesting
1048 BubbleStackView getStackView() {
1049 return mStackView;
1050 }
1051
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001052 /**
1053 * Description of current bubble state.
1054 */
1055 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1056 pw.println("BubbleController state:");
1057 mBubbleData.dump(fd, pw, args);
1058 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -04001059 if (mStackView != null) {
1060 mStackView.dump(fd, pw, args);
1061 }
1062 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001063 }
1064
Mady Mellore80930e2019-03-21 16:00:45 -07001065 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001066 * This task stack listener is responsible for responding to tasks moved to the front
1067 * which are on the default (main) display. When this happens, expanded bubbles must be
1068 * collapsed so the user may interact with the app which was just moved to the front.
1069 * <p>
1070 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1071 * these calls via a main thread Handler.
1072 */
1073 @MainThread
1074 private class BubbleTaskStackListener extends TaskStackChangeListener {
1075
Mark Renoufcecc77b2019-01-30 16:32:24 -05001076 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001077 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1078 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001079 if (!mStackView.isExpansionAnimating()) {
1080 mBubbleData.setExpanded(false);
1081 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001082 }
1083 }
1084
Mark Renoufcecc77b2019-01-30 16:32:24 -05001085 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001086 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001087 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001088 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001089 }
1090 }
Mark Renouf446251d2019-04-26 10:22:41 -04001091
1092 @Override
1093 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1094 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1095 mBubbleData.setExpanded(false);
1096 }
1097 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001098
1099 @Override
1100 public void onSingleTaskDisplayDrawn(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001101 final Bubble expandedBubble = mStackView != null
1102 ? mStackView.getExpandedBubble()
1103 : null;
Issei Suzukicac2a502019-04-16 16:52:50 +02001104 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
1105 expandedBubble.setContentVisibility(true);
1106 }
1107 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001108
1109 @Override
1110 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001111 final Bubble expandedBubble = mStackView != null
1112 ? mStackView.getExpandedBubble()
1113 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001114 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1115 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001116 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001117 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001118 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001119 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001120 }
1121
Mady Mellorca0c24c2019-05-16 16:14:32 -07001122 /**
1123 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1124 *
1125 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1126 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1127 *
1128 * @param context the context to use.
1129 * @param entry the entry to bubble.
1130 */
1131 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1132 PendingIntent intent = entry.getBubbleMetadata() != null
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001133 ? entry.getBubbleMetadata().getBubbleIntent()
Mady Mellorca0c24c2019-05-16 16:14:32 -07001134 : null;
Mady Mellor2ac2d3a2020-01-08 17:18:54 -08001135 if (entry.getBubbleMetadata() != null
1136 && entry.getBubbleMetadata().getShortcutId() != null) {
1137 return true;
1138 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001139 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001140 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001141 return false;
1142 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001143 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1144 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001145 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001146 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001147 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001148 Log.w(TAG, "Unable to send as bubble, "
1149 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001150 + intent);
1151 return false;
1152 }
1153 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001154 Log.w(TAG, "Unable to send as bubble, "
1155 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001156 + intent);
1157 return false;
1158 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001159 return true;
1160 }
1161
Joshua Tsujia19515f2019-02-13 18:02:29 -05001162 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001163 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001164 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001165 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1166 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1167 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001168 }
1169 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001170 }
Aran Inkaa4dfa72019-11-18 16:49:07 -05001171
1172 // TODO: Copied from RemoteInputView. Consolidate RemoteInput intent logic.
1173 private Intent prepareRemoteInputFromData(String contentType, Uri data,
1174 RemoteInput remoteInput, NotificationEntry entry) {
1175 HashMap<String, Uri> results = new HashMap<>();
1176 results.put(contentType, data);
1177 mRemoteInputUriController.grantInlineReplyUriPermission(entry.getSbn(), data);
1178 Intent fillInIntent = new Intent().addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
1179 RemoteInput.addDataResultToIntent(remoteInput, fillInIntent, results);
1180
1181 return fillInIntent;
1182 }
1183
1184 // TODO: Copied from RemoteInputView. Consolidate RemoteInput intent logic.
1185 private void sendRemoteInput(Intent intent, NotificationEntry entry,
1186 PendingIntent pendingIntent) {
1187 // Tell ShortcutManager that this package has been "activated". ShortcutManager
1188 // will reset the throttling for this package.
1189 // Strictly speaking, the intent receiver may be different from the notification publisher,
1190 // but that's an edge case, and also because we can't always know which package will receive
1191 // an intent, so we just reset for the publisher.
1192 mContext.getSystemService(ShortcutManager.class).onApplicationActive(
1193 entry.getSbn().getPackageName(),
1194 entry.getSbn().getUser().getIdentifier());
1195
1196 try {
1197 pendingIntent.send(mContext, 0, intent);
1198 } catch (PendingIntent.CanceledException e) {
1199 Log.i(TAG, "Unable to send remote input result", e);
1200 }
1201 }
1202
1203 private void sendScreenshotToBubble(Bubble bubble) {
Aran Ink141a8152019-12-12 13:31:23 -05001204 mScreenshotHelper.takeScreenshot(
1205 android.view.WindowManager.TAKE_SCREENSHOT_FULLSCREEN,
1206 true /* hasStatus */,
1207 true /* hasNav */,
1208 mHandler,
1209 new Consumer<Uri>() {
1210 @Override
1211 public void accept(Uri uri) {
1212 if (uri != null) {
1213 NotificationEntry entry = bubble.getEntry();
1214 Pair<RemoteInput, Notification.Action> pair = entry.getSbn()
1215 .getNotification().findRemoteInputActionPair(false);
1216 if (pair != null) {
1217 RemoteInput remoteInput = pair.first;
1218 Notification.Action action = pair.second;
1219 Intent dataIntent = prepareRemoteInputFromData("image/png", uri,
1220 remoteInput, entry);
1221 sendRemoteInput(dataIntent, entry, action.actionIntent);
1222 mBubbleData.setSelectedBubble(bubble);
1223 mBubbleData.setExpanded(true);
1224 } else {
1225 Log.w(TAG, "No RemoteInput found for notification: "
1226 + entry.getSbn().getKey());
Aran Inkaa4dfa72019-11-18 16:49:07 -05001227 }
Aran Ink141a8152019-12-12 13:31:23 -05001228 }
1229 }
1230 });
Aran Inkaa4dfa72019-11-18 16:49:07 -05001231 }
1232
1233 private final BubbleScreenshotListener mBubbleScreenshotListener =
1234 bubble -> sendScreenshotToBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001235}