blob: c7492a233a2b147c112b43eae99518457ea4425b [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;
Mady Mellor7f234902019-10-20 12:06:29 -070092import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080093import com.android.systemui.statusbar.phone.StatusBar;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080094import com.android.systemui.statusbar.phone.StatusBarWindowController;
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;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500106import java.util.function.Consumer;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500107
Jason Monk27d01a622018-12-10 15:57:09 -0500108import javax.inject.Inject;
109import javax.inject.Singleton;
110
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800111/**
112 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
113 * Bubbles can be expanded to show more content.
114 *
115 * The controller manages addition, removal, and visible state of bubbles on screen.
116 */
Jason Monk27d01a622018-12-10 15:57:09 -0500117@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -0400118public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800119
Issei Suzukia8d07312019-06-07 12:56:19 +0200120 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800121
Mark Renouf08bc42a2019-03-07 13:01:59 -0500122 @Retention(SOURCE)
123 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400124 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700125 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400126 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500127 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700128
Mark Renouf08bc42a2019-03-07 13:01:59 -0500129 static final int DISMISS_USER_GESTURE = 1;
130 static final int DISMISS_AGED = 2;
131 static final int DISMISS_TASK_FINISHED = 3;
132 static final int DISMISS_BLOCKED = 4;
133 static final int DISMISS_NOTIF_CANCEL = 5;
134 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700135 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400136 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700137 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700138 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500139
Ned Burns01e38212019-01-03 16:32:52 -0500140 private final Context mContext;
141 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500142 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800143 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800144 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100145 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700146 private final NotificationGroupManager mNotificationGroupManager;
Heemin Seogba6337f2019-12-10 15:34:37 -0800147 private final ShadeController mShadeController;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500148 private final RemoteInputUriController mRemoteInputUriController;
149 private Handler mHandler = new Handler() {};
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800150
Mady Mellor3dff9e62019-02-05 18:12:53 -0800151 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400152 @Nullable private BubbleStackView mStackView;
Mady Mellor247ca2c2019-12-02 16:18:59 -0800153 private BubbleIconFactory mBubbleIconFactory;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800154
Mark Renoufc19b4732019-06-26 12:08:33 -0400155 // Tracks the id of the current (foreground) user.
156 private int mCurrentUserId;
157 // Saves notification keys of active bubbles when users are switched.
158 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
159
Mady Mellorff076eb2019-11-13 10:12:06 -0800160 // Saves notification keys of user created "fake" bubbles so that we can allow notifications
161 // like these to bubble by default. Doesn't persist across reboots, not a long-term solution.
162 private final HashSet<String> mUserCreatedBubbles;
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800163 // If we're auto-bubbling bubbles via a whitelist, we need to track which notifs from that app
164 // have been "demoted" back to a notification so that we don't auto-bubbles those again.
165 // Doesn't persist across reboots, not a long-term solution.
166 private final HashSet<String> mUserBlockedBubbles;
Mady Mellorff076eb2019-11-13 10:12:06 -0800167
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800168 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500169 private final StatusBarWindowController mStatusBarWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400170 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800171 private StatusBarStateListener mStatusBarStateListener;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500172 private final ScreenshotHelper mScreenshotHelper;
173
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800174
Mady Melloraa8fef22019-04-11 13:36:40 -0700175 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700176 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800177
Mady Mellord1c78b262018-11-06 18:04:40 -0800178 // Used for determining view rect for touch interaction
179 private Rect mTempRect = new Rect();
180
Mark Renoufc19b4732019-06-26 12:08:33 -0400181 // Listens to user switch so bubbles can be saved and restored.
182 private final NotificationLockscreenUserManager mNotifUserManager;
183
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400184 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
185 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
186
Mady Mellor3df7ab02019-12-09 15:07:10 -0800187 private boolean mInflateSynchronously;
188
Mady Mellor5549dd22018-11-06 18:07:34 -0800189 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800190 * Listener to be notified when some states of the bubbles change.
191 */
192 public interface BubbleStateChangeListener {
193 /**
194 * Called when the stack has bubbles or no longer has bubbles.
195 */
196 void onHasBubblesChanged(boolean hasBubbles);
197 }
198
Mady Mellorcd9b1302018-11-06 18:08:04 -0800199 /**
200 * Listener to find out about stack expansion / collapse events.
201 */
202 public interface BubbleExpandListener {
203 /**
204 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700205 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800206 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800207 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800208 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800209 void onBubbleExpandChanged(boolean isExpanding, String key);
210 }
211
212 /**
Aran Inkaa4dfa72019-11-18 16:49:07 -0500213 * Listener for handling bubble screenshot events.
214 */
215 public interface BubbleScreenshotListener {
216 /**
217 * Called to trigger taking a screenshot and sending the result to a bubble.
218 */
219 void onBubbleScreenshot(Bubble bubble);
220 }
221
222 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800223 * Listens for the current state of the status bar and updates the visibility state
224 * of bubbles as needed.
225 */
226 private class StatusBarStateListener implements StatusBarStateController.StateListener {
227 private int mState;
228 /**
229 * Returns the current status bar state.
230 */
231 public int getCurrentState() {
232 return mState;
233 }
234
235 @Override
236 public void onStateChanged(int newState) {
237 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400238 boolean shouldCollapse = (mState != SHADE);
239 if (shouldCollapse) {
240 collapseStack();
241 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700242 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800243 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800244 }
245
Jason Monk27d01a622018-12-10 15:57:09 -0500246 @Inject
Mady Mellor7f234902019-10-20 12:06:29 -0700247 public BubbleController(Context context,
248 StatusBarWindowController statusBarWindowController,
249 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800250 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700251 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700252 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400253 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400254 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700255 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700256 NotificationGroupManager groupManager,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500257 NotificationEntryManager entryManager,
258 RemoteInputUriController remoteInputUriController) {
Mady Mellor7f234902019-10-20 12:06:29 -0700259 this(context, statusBarWindowController, statusBarStateController, shadeController,
260 data, null /* synchronizer */, configurationController, interruptionStateProvider,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500261 zenModeController, notifUserManager, groupManager, entryManager,
262 remoteInputUriController);
Mady Mellor7f234902019-10-20 12:06:29 -0700263 }
264
265 public BubbleController(Context context,
266 StatusBarWindowController statusBarWindowController,
267 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800268 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700269 BubbleData data,
270 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
271 ConfigurationController configurationController,
272 NotificationInterruptionStateProvider interruptionStateProvider,
273 ZenModeController zenModeController,
274 NotificationLockscreenUserManager notifUserManager,
275 NotificationGroupManager groupManager,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500276 NotificationEntryManager entryManager,
277 RemoteInputUriController remoteInputUriController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800278 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800279 mShadeController = shadeController;
Mady Melloraa8fef22019-04-11 13:36:40 -0700280 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400281 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400282 mZenModeController = zenModeController;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500283 mRemoteInputUriController = remoteInputUriController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400284 mZenModeController.addCallback(new ZenModeController.Callback() {
285 @Override
286 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800287 for (Bubble b : mBubbleData.getBubbles()) {
288 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700289 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400290 }
291
292 @Override
293 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800294 for (Bubble b : mBubbleData.getBubbles()) {
295 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700296 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400297 }
298 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700299
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700300 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800301
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400302 mBubbleData = data;
303 mBubbleData.setListener(mBubbleDataListener);
304
Mady Mellor7f234902019-10-20 12:06:29 -0700305 mNotificationEntryManager = entryManager;
Ned Burns01e38212019-01-03 16:32:52 -0500306 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700307 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellor22f2f072019-04-18 13:26:18 -0700308 mNotificationGroupManager = groupManager;
Mady Mellor740d85d2019-07-09 15:26:47 -0700309 mNotificationGroupManager.addOnGroupChangeListener(
310 new NotificationGroupManager.OnGroupChangeListener() {
311 @Override
312 public void onGroupSuppressionChanged(
313 NotificationGroupManager.NotificationGroup group,
314 boolean suppressed) {
315 // More notifications could be added causing summary to no longer
316 // be suppressed -- in this case need to remove the key.
317 final String groupKey = group.summary != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400318 ? group.summary.getSbn().getGroupKey()
Mady Mellor740d85d2019-07-09 15:26:47 -0700319 : null;
320 if (!suppressed && groupKey != null
321 && mBubbleData.isSummarySuppressed(groupKey)) {
322 mBubbleData.removeSuppressedSummary(groupKey);
323 }
324 }
325 });
Mady Mellorb4991e62019-01-10 15:14:51 -0800326
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800327 mStatusBarWindowController = statusBarWindowController;
328 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700329 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500330
Mark Renoufcecc77b2019-01-30 16:32:24 -0500331 mTaskStackListener = new BubbleTaskStackListener();
332 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800333
Joshua Tsujia19515f2019-02-13 18:02:29 -0500334 try {
335 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
336 } catch (RemoteException e) {
337 e.printStackTrace();
338 }
Issei Suzukic0387542019-03-08 17:31:14 +0100339 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700340
341 mBarService = IStatusBarService.Stub.asInterface(
342 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400343
344 mSavedBubbleKeysPerUser = new SparseSetArray<>();
345 mCurrentUserId = mNotifUserManager.getCurrentUserId();
346 mNotifUserManager.addUserChangedListener(
Steve Elliottb47f1c72019-12-19 12:39:26 -0500347 new NotificationLockscreenUserManager.UserChangedListener() {
348 @Override
349 public void onUserChanged(int newUserId) {
350 BubbleController.this.saveBubbles(mCurrentUserId);
351 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
352 BubbleController.this.restoreBubbles(newUserId);
353 mCurrentUserId = newUserId;
354 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400355 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800356
357 mUserCreatedBubbles = new HashSet<>();
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800358 mUserBlockedBubbles = new HashSet<>();
Aran Inkaa4dfa72019-11-18 16:49:07 -0500359
360 mScreenshotHelper = new ScreenshotHelper(context);
Mady Mellor247ca2c2019-12-02 16:18:59 -0800361 mBubbleIconFactory = new BubbleIconFactory(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800362 }
363
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400364 /**
Mady Mellor3df7ab02019-12-09 15:07:10 -0800365 * Sets whether to perform inflation on the same thread as the caller. This method should only
366 * be used in tests, not in production.
367 */
368 @VisibleForTesting
369 void setInflateSynchronously(boolean inflateSynchronously) {
370 mInflateSynchronously = inflateSynchronously;
Mady Mellor5549dd22018-11-06 18:07:34 -0800371 }
372
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400373 /**
374 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
375 * method initializes the stack view and adds it to the StatusBar just above the scrim.
376 */
377 private void ensureStackViewCreated() {
378 if (mStackView == null) {
379 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
380 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Lyn Hanbde48202019-05-29 19:18:29 -0700381 int bubbleScrimIndex = sbv.indexOfChild(sbv.findViewById(R.id.scrim_for_bubble));
382 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
383 sbv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400384 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
385 if (mExpandListener != null) {
386 mStackView.setExpandListener(mExpandListener);
387 }
Aran Inkaa4dfa72019-11-18 16:49:07 -0500388 if (mBubbleScreenshotListener != null) {
389 mStackView.setBubbleScreenshotListener(mBubbleScreenshotListener);
390 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400391 }
392 }
393
Mark Renoufc19b4732019-06-26 12:08:33 -0400394 /**
395 * Records the notification key for any active bubbles. These are used to restore active
396 * bubbles when the user returns to the foreground.
397 *
398 * @param userId the id of the user
399 */
400 private void saveBubbles(@UserIdInt int userId) {
401 // First clear any existing keys that might be stored.
402 mSavedBubbleKeysPerUser.remove(userId);
403 // Add in all active bubbles for the current user.
404 for (Bubble bubble: mBubbleData.getBubbles()) {
405 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
406 }
407 }
408
409 /**
410 * Promotes existing notifications to Bubbles if they were previously bubbles.
411 *
412 * @param userId the id of the user
413 */
414 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400415 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
416 if (savedBubbleKeys == null) {
417 // There were no bubbles saved for this used.
418 return;
419 }
Evan Laird181de622019-10-24 09:53:02 -0400420 for (NotificationEntry e :
421 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400422 if (savedBubbleKeys.contains(e.getKey())
Mark Renoufc19b4732019-06-26 12:08:33 -0400423 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
424 && canLaunchInActivityView(mContext, e)) {
425 updateBubble(e, /* suppressFlyout= */ true);
426 }
427 }
428 // Finally, remove the entries for this user now that bubbles are restored.
429 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
430 }
431
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700432 @Override
433 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800434 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700435 }
436
437 @Override
438 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800439 updateForThemeChanges();
440 }
441
442 private void updateForThemeChanges() {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700443 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700444 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700445 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800446 mBubbleIconFactory = new BubbleIconFactory(mContext);
447 for (Bubble b: mBubbleData.getBubbles()) {
448 // Reload each bubble
449 b.inflate(null /* callback */, mContext, mStackView, mBubbleIconFactory);
450 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700451 }
452
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400453 @Override
454 public void onConfigChanged(Configuration newConfig) {
455 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400456 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700457 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400458 }
459 }
460
Mady Mellor5549dd22018-11-06 18:07:34 -0800461 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800462 * Set a listener to be notified when some states of the bubbles change.
463 */
464 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
465 mStateChangeListener = listener;
466 }
467
468 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800469 * Set a listener to be notified of bubble expand events.
470 */
471 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100472 mExpandListener = ((isExpanding, key) -> {
473 if (listener != null) {
474 listener.onBubbleExpandChanged(isExpanding, key);
475 }
476 mStatusBarWindowController.setBubbleExpanded(isExpanding);
477 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800478 if (mStackView != null) {
479 mStackView.setExpandListener(mExpandListener);
480 }
481 }
482
483 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800484 * Whether or not there are bubbles present, regardless of them being visible on the
485 * screen (e.g. if on AOD).
486 */
487 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800488 if (mStackView == null) {
489 return false;
490 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400491 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800492 }
493
494 /**
495 * Whether the stack of bubbles is expanded or not.
496 */
497 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400498 return mBubbleData.isExpanded();
499 }
500
501 /**
502 * Tell the stack of bubbles to expand.
503 */
504 public void expandStack() {
505 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800506 }
507
508 /**
509 * Tell the stack of bubbles to collapse.
510 */
511 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400512 mBubbleData.setExpanded(false /* expanded */);
513 }
514
Mady Mellorce23c462019-06-17 17:30:07 -0700515 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700516 * True if either:
517 * (1) There is a bubble associated with the provided key and if its notification is hidden
518 * from the shade.
519 * (2) There is a group summary associated with the provided key that is hidden from the shade
520 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700521 *
Mady Mellore28fe102019-07-09 15:33:32 -0700522 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700523 */
524 public boolean isBubbleNotificationSuppressedFromShade(String key) {
Mady Mellore28fe102019-07-09 15:33:32 -0700525 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800526 && !mBubbleData.getBubbleWithKey(key).showInShade();
Evan Laird181de622019-10-24 09:53:02 -0400527 NotificationEntry entry = mNotificationEntryManager.getActiveNotificationUnfiltered(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400528 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellore28fe102019-07-09 15:33:32 -0700529 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700530 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
531 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700532 }
533
Mark Renouf71a3af62019-04-08 15:02:54 -0400534 @VisibleForTesting
535 void selectBubble(String key) {
536 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellor247ca2c2019-12-02 16:18:59 -0800537 mBubbleData.setSelectedBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800538 }
539
540 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400541 * Request the stack expand if needed, then select the specified Bubble as current.
542 *
543 * @param notificationKey the notification key for the bubble to be selected
544 */
545 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400546 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
547 if (bubble != null) {
548 mBubbleData.setSelectedBubble(bubble);
549 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400550 }
551 }
552
553 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800554 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
555 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500556 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400557 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800558 }
559
560 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500561 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
562 * is forwarded a back key down/up pair.
563 */
564 public void performBackPressIfNeeded() {
565 if (mStackView != null) {
566 mStackView.performBackPressIfNeeded();
567 }
568 }
569
570 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800571 * Adds or updates a bubble associated with the provided notification entry.
572 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400573 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800574 */
Mark Renouff97ed462019-04-05 13:46:24 -0400575 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700576 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400577 }
578
579 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700580 updateBubble(notif, suppressFlyout, true /* showInShade */);
581 }
582
583 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor3df7ab02019-12-09 15:07:10 -0800584 if (mStackView == null) {
585 // Lazy init stack view when a bubble is created
586 ensureStackViewCreated();
587 }
Mady Mellor66efd5e2019-05-15 13:38:11 -0700588 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400589 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700590 notif.setInterruption();
591 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800592 Bubble bubble = mBubbleData.getOrCreateBubble(notif);
593 bubble.setInflateSynchronously(mInflateSynchronously);
594 bubble.inflate(
595 b -> mBubbleData.notificationEntryUpdated(b, suppressFlyout, showInShade),
596 mContext, mStackView, mBubbleIconFactory);
Mady Mellor7f234902019-10-20 12:06:29 -0700597 }
598
599 /**
600 * Called when a user has indicated that an active notification should be shown as a bubble.
601 * <p>
602 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
603 * the notification from appearing in the shade.
604 *
605 * @param entry the notification to show as a bubble.
606 */
607 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800608 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
609 Log.d(TAG, "onUserCreatedBubble: " + entry.getKey());
610 }
Heemin Seogba6337f2019-12-10 15:34:37 -0800611 mShadeController.collapsePanel(true);
Mady Mellor7f234902019-10-20 12:06:29 -0700612 entry.setFlagBubble(true);
613 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
Mady Mellorff076eb2019-11-13 10:12:06 -0800614 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800615 mUserBlockedBubbles.remove(entry.getKey());
Mady Mellor7f234902019-10-20 12:06:29 -0700616 }
617
618 /**
619 * Called when a user has indicated that an active notification appearing as a bubble should
620 * no longer be shown as a bubble.
621 *
622 * @param entry the notification to no longer show as a bubble.
623 */
624 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800625 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
626 Log.d(TAG, "onUserDemotedBubble: " + entry.getKey());
627 }
Mady Mellor7f234902019-10-20 12:06:29 -0700628 entry.setFlagBubble(false);
629 removeBubble(entry.getKey(), DISMISS_BLOCKED);
Mady Mellorff076eb2019-11-13 10:12:06 -0800630 mUserCreatedBubbles.remove(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800631 if (BubbleExperimentConfig.isPackageWhitelistedToAutoBubble(
632 mContext, entry.getSbn().getPackageName())) {
633 // This package is whitelist but user demoted the bubble, let's save it so we don't
634 // auto-bubble for the whitelist again.
635 mUserBlockedBubbles.add(entry.getKey());
636 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800637 }
638
639 /**
640 * Whether this bubble was explicitly created by the user via a SysUI affordance.
641 */
642 boolean isUserCreatedBubble(String key) {
643 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800644 }
645
646 /**
647 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500648 * <p>
649 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800650 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500651 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500652 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400653 // TEMP: refactor to change this to pass entry
654 Bubble bubble = mBubbleData.getBubbleWithKey(key);
655 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700656 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800657 }
658 }
659
Ned Burns01e38212019-01-03 16:32:52 -0500660 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700661 private final NotificationRemoveInterceptor mRemoveInterceptor =
662 new NotificationRemoveInterceptor() {
663 @Override
664 public boolean onNotificationRemoveRequested(String key, int reason) {
Evan Laird181de622019-10-24 09:53:02 -0400665 NotificationEntry entry =
666 mNotificationEntryManager.getActiveNotificationUnfiltered(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400667 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellor22f2f072019-04-18 13:26:18 -0700668 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
669
670 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
Mady Mellore28fe102019-07-09 15:33:32 -0700671 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
672 && mBubbleData.getSummaryKey(groupKey).equals(key));
Mady Mellor22f2f072019-04-18 13:26:18 -0700673 boolean isSummary = entry != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400674 && entry.getSbn().getNotification().isGroupSummary();
Mady Mellore28fe102019-07-09 15:33:32 -0700675 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
676 && bubbleChildren != null && !bubbleChildren.isEmpty();
Mady Mellor22f2f072019-04-18 13:26:18 -0700677
678 if (!inBubbleData && !isSummaryOfBubbles) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700679 return false;
680 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700681
682 final boolean isClearAll = reason == REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700683 final boolean isUserDimiss = reason == REASON_CANCEL || reason == REASON_CLICK;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700684 final boolean isAppCancel = reason == REASON_APP_CANCEL
685 || reason == REASON_APP_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700686 final boolean isSummaryCancel = reason == REASON_GROUP_SUMMARY_CANCELED;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700687
688 // Need to check for !appCancel here because the notification may have
689 // previously been dismissed & entry.isRowDismissed would still be true
Mady Mellorca184aae2019-09-17 16:07:12 -0700690 boolean userRemovedNotif = (entry != null && entry.isRowDismissed() && !isAppCancel)
Mady Mellor22f2f072019-04-18 13:26:18 -0700691 || isClearAll || isUserDimiss || isSummaryCancel;
692
693 if (isSummaryOfBubbles) {
694 return handleSummaryRemovalInterception(entry, userRemovedNotif);
695 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700696
697 // The bubble notification sticks around in the data as long as the bubble is
698 // not dismissed and the app hasn't cancelled the notification.
Mady Mellor22f2f072019-04-18 13:26:18 -0700699 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellorca184aae2019-09-17 16:07:12 -0700700 boolean bubbleExtended = entry != null && entry.isBubble() && userRemovedNotif;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700701 if (bubbleExtended) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800702 bubble.setShowInShade(false);
703 bubble.setShowDot(false /* show */, true /* animate */);
Beverly85d4c192019-09-30 11:40:39 -0400704 mNotificationEntryManager.updateNotifications(
705 "BubbleController.onNotificationRemoveRequested");
Mady Mellorc2ff0112019-03-28 14:18:06 -0700706 return true;
Mady Mellorff076eb2019-11-13 10:12:06 -0800707 } else if (!userRemovedNotif && entry != null
708 && !isUserCreatedBubble(bubble.getKey())) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700709 // This wasn't a user removal so we should remove the bubble as well
710 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
711 return false;
712 }
713 return false;
714 }
715 };
716
Mady Mellor22f2f072019-04-18 13:26:18 -0700717 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
718 boolean userRemovedNotif) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400719 String groupKey = summary.getSbn().getGroupKey();
Mady Mellor22f2f072019-04-18 13:26:18 -0700720 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
721
722 if (userRemovedNotif) {
723 // If it's a user dismiss we mark the children to be hidden from the shade.
724 for (int i = 0; i < bubbleChildren.size(); i++) {
725 Bubble bubbleChild = bubbleChildren.get(i);
726 // As far as group manager is concerned, once a child is no longer shown
727 // in the shade, it is essentially removed.
728 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
Mady Mellorb8aaf972019-11-26 10:28:00 -0800729 bubbleChild.setShowInShade(false);
730 bubbleChild.setShowDot(false /* show */, true /* animate */);
Mady Mellor22f2f072019-04-18 13:26:18 -0700731 }
732 // And since all children are removed, remove the summary.
733 mNotificationGroupManager.onEntryRemoved(summary);
734
735 // If the summary was auto-generated we don't need to keep that notification around
736 // because apps can't cancel it; so we only intercept & suppress real summaries.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400737 boolean isAutogroupSummary = (summary.getSbn().getNotification().flags
Mady Mellor22f2f072019-04-18 13:26:18 -0700738 & FLAG_AUTOGROUP_SUMMARY) != 0;
Mady Mellore28fe102019-07-09 15:33:32 -0700739 if (!isAutogroupSummary) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400740 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
741 summary.getKey());
Mady Mellore28fe102019-07-09 15:33:32 -0700742 // Tell shade to update for the suppression
Beverly85d4c192019-09-30 11:40:39 -0400743 mNotificationEntryManager.updateNotifications(
744 "BubbleController.handleSummaryRemovalInterception");
Mady Mellore28fe102019-07-09 15:33:32 -0700745 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700746 return !isAutogroupSummary;
747 } else {
Mady Mellore28fe102019-07-09 15:33:32 -0700748 // If it's not a user dismiss it's a cancel.
749 mBubbleData.removeSuppressedSummary(groupKey);
750
Mady Mellor22f2f072019-04-18 13:26:18 -0700751 // Remove any associated bubble children.
752 for (int i = 0; i < bubbleChildren.size(); i++) {
753 Bubble bubbleChild = bubbleChildren.get(i);
754 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
755 DISMISS_GROUP_CANCELLED);
756 }
757 return false;
758 }
759 }
760
Mady Mellorc2ff0112019-03-28 14:18:06 -0700761 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500762 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
763 @Override
Mady Mellor58dc5192019-12-16 13:49:56 -0800764 public void onNotificationAdded(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800765 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800766 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
Mady Mellor30672942019-12-04 15:43:19 -0800767 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800768 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700769
Mady Mellorca0c24c2019-05-16 16:14:32 -0700770 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
Mady Mellor30672942019-12-04 15:43:19 -0800771 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
Mady Mellorea13b232019-12-05 15:55:46 -0800772 if (wasAdjusted && !previouslyUserCreated) {
773 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
774 mUserCreatedBubbles.add(entry.getKey());
775 }
Mark Renouff97ed462019-04-05 13:46:24 -0400776 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800777 }
778 }
779
780 @Override
781 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800782 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800783 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
Mady Mellor30672942019-12-04 15:43:19 -0800784 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800785 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700786
Mady Mellorca0c24c2019-05-16 16:14:32 -0700787 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
Mady Mellor30672942019-12-04 15:43:19 -0800788 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400789 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
Mady Melloraa8fef22019-04-11 13:36:40 -0700790 // It was previously a bubble but no longer a bubble -- lets remove it
Ned Burns00b4b2d2019-10-17 22:09:27 -0400791 removeBubble(entry.getKey(), DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700792 } else if (shouldBubble) {
Mady Mellorea13b232019-12-05 15:55:46 -0800793 if (wasAdjusted && !previouslyUserCreated) {
794 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
795 mUserCreatedBubbles.add(entry.getKey());
796 }
Mark Renouff97ed462019-04-05 13:46:24 -0400797 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800798 }
799 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400800
801 @Override
802 public void onNotificationRankingUpdated(RankingMap rankingMap) {
803 // Forward to BubbleData to block any bubbles which should no longer be shown
804 mBubbleData.notificationRankingUpdated(rankingMap);
805 }
Ned Burns01e38212019-01-03 16:32:52 -0500806 };
807
Mark Renouf71a3af62019-04-08 15:02:54 -0400808 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400809 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400810
Mark Renouf3bc5b362019-04-05 14:37:59 -0400811 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400812 public void applyUpdate(BubbleData.Update update) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400813 if (update.addedBubble != null) {
814 mStackView.addBubble(update.addedBubble);
815 }
816
817 // Collapsing? Do this first before remaining steps.
818 if (update.expandedChanged && !update.expanded) {
819 mStackView.setExpanded(false);
820 }
821
822 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700823 ArrayList<Pair<Bubble, Integer>> removedBubbles =
824 new ArrayList<>(update.removedBubbles);
825 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400826 final Bubble bubble = removed.first;
827 @DismissReason final int reason = removed.second;
828 mStackView.removeBubble(bubble);
829
Mark Renoufc19b4732019-06-26 12:08:33 -0400830 // If the bubble is removed for user switching, leave the notification in place.
831 if (reason != DISMISS_USER_CHANGED) {
832 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800833 && !bubble.showInShade()) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400834 // The bubble is gone & the notification is gone, time to actually remove it
835 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400836 bubble.getEntry().getSbn(), UNDEFINED_DISMISS_REASON);
Mark Renoufc19b4732019-06-26 12:08:33 -0400837 } else {
838 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400839 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700840
Mark Renoufc19b4732019-06-26 12:08:33 -0400841 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
842 // will get right result back
843 try {
844 mBarService.onNotificationBubbleChanged(bubble.getKey(),
845 false /* isBubble */);
846 } catch (RemoteException e) {
847 // Bad things have happened
848 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400849 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700850
Mady Mellore28fe102019-07-09 15:33:32 -0700851 // Check if removed bubble has an associated suppressed group summary that needs
852 // to be removed now.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400853 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700854 if (mBubbleData.isSummarySuppressed(groupKey)
855 && mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
856 // Time to actually remove the summary.
857 String notifKey = mBubbleData.getSummaryKey(groupKey);
858 mBubbleData.removeSuppressedSummary(groupKey);
859 NotificationEntry entry =
Evan Laird181de622019-10-24 09:53:02 -0400860 mNotificationEntryManager.getActiveNotificationUnfiltered(notifKey);
Mady Mellore28fe102019-07-09 15:33:32 -0700861 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400862 entry.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellore28fe102019-07-09 15:33:32 -0700863 }
864
Mady Mellor22f2f072019-04-18 13:26:18 -0700865 // Check if summary should be removed from NoManGroup
866 NotificationEntry summary = mNotificationGroupManager.getLogicalGroupSummary(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400867 bubble.getEntry().getSbn());
Mady Mellor22f2f072019-04-18 13:26:18 -0700868 if (summary != null) {
869 ArrayList<NotificationEntry> summaryChildren =
Ned Burns00b4b2d2019-10-17 22:09:27 -0400870 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
871 boolean isSummaryThisNotif = summary.getKey().equals(
872 bubble.getEntry().getKey());
Mady Mellore4348272019-07-29 17:43:36 -0700873 if (!isSummaryThisNotif
874 && (summaryChildren == null || summaryChildren.isEmpty())) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700875 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400876 summary.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellor22f2f072019-04-18 13:26:18 -0700877 }
878 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700879 }
880 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400881
Mark Renouf82a40e62019-05-23 16:16:24 -0400882 if (update.updatedBubble != null) {
883 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400884 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400885
Mark Renouf82a40e62019-05-23 16:16:24 -0400886 if (update.orderChanged) {
887 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400888 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400889
Mark Renouf82a40e62019-05-23 16:16:24 -0400890 if (update.selectionChanged) {
891 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700892 if (update.selectedBubble != null) {
893 mNotificationGroupManager.updateSuppression(
894 update.selectedBubble.getEntry());
895 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400896 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400897
Mark Renouf82a40e62019-05-23 16:16:24 -0400898 // Expanding? Apply this last.
899 if (update.expandedChanged && update.expanded) {
900 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400901 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400902
Beverly85d4c192019-09-30 11:40:39 -0400903 mNotificationEntryManager.updateNotifications(
904 "BubbleData.Listener.applyUpdate");
Lyn Han6c40fe72019-05-08 14:06:33 -0700905 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400906
Issei Suzukia8d07312019-06-07 12:56:19 +0200907 if (DEBUG_BUBBLE_CONTROLLER) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400908 Log.d(TAG, "[BubbleData]");
Lyn Han767d70e2019-12-10 18:02:23 -0800909 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400910 mBubbleData.getSelectedBubble()));
911
912 if (mStackView != null) {
913 Log.d(TAG, "[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -0800914 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400915 mStackView.getExpandedBubble()));
916 }
917 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400918 }
919 };
920
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800921 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400922 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700923 * Updates the visibility of the bubbles based on current state.
924 * Does not un-bubble, just hides or un-hides. Notifies any
925 * {@link BubbleStateChangeListener}s of visibility changes.
926 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800927 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700928 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800929 if (mStackView == null) {
930 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800931 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800932 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
933 // Bubbles only appear in unlocked shade
934 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +0000935 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800936 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800937 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700938
Mady Mellor698d9e82019-08-01 23:11:53 +0000939 // Let listeners know if bubble state changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700940 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +0000941 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellor88552b82019-08-05 22:38:59 +0000942 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -0700943 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
944 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
945 }
946
947 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800948 }
949
950 /**
951 * Rect indicating the touchable region for the bubble stack / expanded stack.
952 */
953 public Rect getTouchableRegion() {
954 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
955 return null;
956 }
957 mStackView.getBoundsOnScreen(mTempRect);
958 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800959 }
960
Mady Mellor390bff42019-04-05 15:09:01 -0700961 /**
962 * The display id of the expanded view, if the stack is expanded and not occluded by the
963 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
964 */
965 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200966 final Bubble bubble = getExpandedBubble(context);
967 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
968 }
969
970 @Nullable
971 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700972 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200973 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700974 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200975 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700976 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200977 final Bubble expandedBubble = mStackView.getExpandedBubble();
978 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700979 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200980 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700981 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200982 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700983 }
984
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800985 @VisibleForTesting
986 BubbleStackView getStackView() {
987 return mStackView;
988 }
989
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700990 /**
991 * Description of current bubble state.
992 */
993 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
994 pw.println("BubbleController state:");
995 mBubbleData.dump(fd, pw, args);
996 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -0400997 if (mStackView != null) {
998 mStackView.dump(fd, pw, args);
999 }
1000 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -07001001 }
1002
Mady Mellore80930e2019-03-21 16:00:45 -07001003 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001004 * This task stack listener is responsible for responding to tasks moved to the front
1005 * which are on the default (main) display. When this happens, expanded bubbles must be
1006 * collapsed so the user may interact with the app which was just moved to the front.
1007 * <p>
1008 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1009 * these calls via a main thread Handler.
1010 */
1011 @MainThread
1012 private class BubbleTaskStackListener extends TaskStackChangeListener {
1013
Mark Renoufcecc77b2019-01-30 16:32:24 -05001014 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001015 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1016 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001017 if (!mStackView.isExpansionAnimating()) {
1018 mBubbleData.setExpanded(false);
1019 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001020 }
1021 }
1022
Mark Renoufcecc77b2019-01-30 16:32:24 -05001023 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001024 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001025 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001026 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001027 }
1028 }
Mark Renouf446251d2019-04-26 10:22:41 -04001029
1030 @Override
1031 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1032 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1033 mBubbleData.setExpanded(false);
1034 }
1035 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001036
1037 @Override
1038 public void onSingleTaskDisplayDrawn(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001039 final Bubble expandedBubble = mStackView != null
1040 ? mStackView.getExpandedBubble()
1041 : null;
Issei Suzukicac2a502019-04-16 16:52:50 +02001042 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
1043 expandedBubble.setContentVisibility(true);
1044 }
1045 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001046
1047 @Override
1048 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001049 final Bubble expandedBubble = mStackView != null
1050 ? mStackView.getExpandedBubble()
1051 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001052 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1053 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001054 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001055 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001056 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001057 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001058 }
1059
Mady Mellorca0c24c2019-05-16 16:14:32 -07001060 /**
1061 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1062 *
1063 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1064 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1065 *
1066 * @param context the context to use.
1067 * @param entry the entry to bubble.
1068 */
1069 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1070 PendingIntent intent = entry.getBubbleMetadata() != null
1071 ? entry.getBubbleMetadata().getIntent()
1072 : null;
1073 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001074 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001075 return false;
1076 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001077 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1078 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001079 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001080 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001081 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001082 Log.w(TAG, "Unable to send as bubble, "
1083 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001084 + intent);
1085 return false;
1086 }
1087 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001088 Log.w(TAG, "Unable to send as bubble, "
1089 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001090 + intent);
1091 return false;
1092 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001093 return true;
1094 }
1095
Joshua Tsujia19515f2019-02-13 18:02:29 -05001096 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001097 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001098 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001099 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1100 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1101 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001102 }
1103 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001104 }
Aran Inkaa4dfa72019-11-18 16:49:07 -05001105
1106 // TODO: Copied from RemoteInputView. Consolidate RemoteInput intent logic.
1107 private Intent prepareRemoteInputFromData(String contentType, Uri data,
1108 RemoteInput remoteInput, NotificationEntry entry) {
1109 HashMap<String, Uri> results = new HashMap<>();
1110 results.put(contentType, data);
1111 mRemoteInputUriController.grantInlineReplyUriPermission(entry.getSbn(), data);
1112 Intent fillInIntent = new Intent().addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
1113 RemoteInput.addDataResultToIntent(remoteInput, fillInIntent, results);
1114
1115 return fillInIntent;
1116 }
1117
1118 // TODO: Copied from RemoteInputView. Consolidate RemoteInput intent logic.
1119 private void sendRemoteInput(Intent intent, NotificationEntry entry,
1120 PendingIntent pendingIntent) {
1121 // Tell ShortcutManager that this package has been "activated". ShortcutManager
1122 // will reset the throttling for this package.
1123 // Strictly speaking, the intent receiver may be different from the notification publisher,
1124 // but that's an edge case, and also because we can't always know which package will receive
1125 // an intent, so we just reset for the publisher.
1126 mContext.getSystemService(ShortcutManager.class).onApplicationActive(
1127 entry.getSbn().getPackageName(),
1128 entry.getSbn().getUser().getIdentifier());
1129
1130 try {
1131 pendingIntent.send(mContext, 0, intent);
1132 } catch (PendingIntent.CanceledException e) {
1133 Log.i(TAG, "Unable to send remote input result", e);
1134 }
1135 }
1136
1137 private void sendScreenshotToBubble(Bubble bubble) {
Aran Ink141a8152019-12-12 13:31:23 -05001138 mScreenshotHelper.takeScreenshot(
1139 android.view.WindowManager.TAKE_SCREENSHOT_FULLSCREEN,
1140 true /* hasStatus */,
1141 true /* hasNav */,
1142 mHandler,
1143 new Consumer<Uri>() {
1144 @Override
1145 public void accept(Uri uri) {
1146 if (uri != null) {
1147 NotificationEntry entry = bubble.getEntry();
1148 Pair<RemoteInput, Notification.Action> pair = entry.getSbn()
1149 .getNotification().findRemoteInputActionPair(false);
1150 if (pair != null) {
1151 RemoteInput remoteInput = pair.first;
1152 Notification.Action action = pair.second;
1153 Intent dataIntent = prepareRemoteInputFromData("image/png", uri,
1154 remoteInput, entry);
1155 sendRemoteInput(dataIntent, entry, action.actionIntent);
1156 mBubbleData.setSelectedBubble(bubble);
1157 mBubbleData.setExpanded(true);
1158 } else {
1159 Log.w(TAG, "No RemoteInput found for notification: "
1160 + entry.getSbn().getKey());
Aran Inkaa4dfa72019-11-18 16:49:07 -05001161 }
Aran Ink141a8152019-12-12 13:31:23 -05001162 }
1163 }
1164 });
Aran Inkaa4dfa72019-11-18 16:49:07 -05001165 }
1166
1167 private final BubbleScreenshotListener mBubbleScreenshotListener =
1168 bubble -> sendScreenshotToBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001169}