blob: 85a395917e48bf102a6c26962d1b83a0369d6731 [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 Mellor5549dd22018-11-06 18:07:34 -0800187 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800188 * Listener to be notified when some states of the bubbles change.
189 */
190 public interface BubbleStateChangeListener {
191 /**
192 * Called when the stack has bubbles or no longer has bubbles.
193 */
194 void onHasBubblesChanged(boolean hasBubbles);
195 }
196
Mady Mellorcd9b1302018-11-06 18:08:04 -0800197 /**
198 * Listener to find out about stack expansion / collapse events.
199 */
200 public interface BubbleExpandListener {
201 /**
202 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700203 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800204 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800205 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800206 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800207 void onBubbleExpandChanged(boolean isExpanding, String key);
208 }
209
210 /**
Aran Inkaa4dfa72019-11-18 16:49:07 -0500211 * Listener for handling bubble screenshot events.
212 */
213 public interface BubbleScreenshotListener {
214 /**
215 * Called to trigger taking a screenshot and sending the result to a bubble.
216 */
217 void onBubbleScreenshot(Bubble bubble);
218 }
219
220 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800221 * Listens for the current state of the status bar and updates the visibility state
222 * of bubbles as needed.
223 */
224 private class StatusBarStateListener implements StatusBarStateController.StateListener {
225 private int mState;
226 /**
227 * Returns the current status bar state.
228 */
229 public int getCurrentState() {
230 return mState;
231 }
232
233 @Override
234 public void onStateChanged(int newState) {
235 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400236 boolean shouldCollapse = (mState != SHADE);
237 if (shouldCollapse) {
238 collapseStack();
239 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700240 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800241 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800242 }
243
Jason Monk27d01a622018-12-10 15:57:09 -0500244 @Inject
Mady Mellor7f234902019-10-20 12:06:29 -0700245 public BubbleController(Context context,
246 StatusBarWindowController statusBarWindowController,
247 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800248 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700249 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700250 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400251 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400252 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700253 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700254 NotificationGroupManager groupManager,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500255 NotificationEntryManager entryManager,
256 RemoteInputUriController remoteInputUriController) {
Mady Mellor7f234902019-10-20 12:06:29 -0700257 this(context, statusBarWindowController, statusBarStateController, shadeController,
258 data, null /* synchronizer */, configurationController, interruptionStateProvider,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500259 zenModeController, notifUserManager, groupManager, entryManager,
260 remoteInputUriController);
Mady Mellor7f234902019-10-20 12:06:29 -0700261 }
262
263 public BubbleController(Context context,
264 StatusBarWindowController statusBarWindowController,
265 StatusBarStateController statusBarStateController,
Heemin Seogba6337f2019-12-10 15:34:37 -0800266 ShadeController shadeController,
Mady Mellor7f234902019-10-20 12:06:29 -0700267 BubbleData data,
268 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
269 ConfigurationController configurationController,
270 NotificationInterruptionStateProvider interruptionStateProvider,
271 ZenModeController zenModeController,
272 NotificationLockscreenUserManager notifUserManager,
273 NotificationGroupManager groupManager,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500274 NotificationEntryManager entryManager,
275 RemoteInputUriController remoteInputUriController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800276 mContext = context;
Heemin Seogba6337f2019-12-10 15:34:37 -0800277 mShadeController = shadeController;
Mady Melloraa8fef22019-04-11 13:36:40 -0700278 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400279 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400280 mZenModeController = zenModeController;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500281 mRemoteInputUriController = remoteInputUriController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400282 mZenModeController.addCallback(new ZenModeController.Callback() {
283 @Override
284 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800285 for (Bubble b : mBubbleData.getBubbles()) {
286 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700287 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400288 }
289
290 @Override
291 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800292 for (Bubble b : mBubbleData.getBubbles()) {
293 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700294 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400295 }
296 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700297
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700298 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800299
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400300 mBubbleData = data;
301 mBubbleData.setListener(mBubbleDataListener);
302
Mady Mellor7f234902019-10-20 12:06:29 -0700303 mNotificationEntryManager = entryManager;
Ned Burns01e38212019-01-03 16:32:52 -0500304 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700305 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellor22f2f072019-04-18 13:26:18 -0700306 mNotificationGroupManager = groupManager;
Mady Mellor740d85d2019-07-09 15:26:47 -0700307 mNotificationGroupManager.addOnGroupChangeListener(
308 new NotificationGroupManager.OnGroupChangeListener() {
309 @Override
310 public void onGroupSuppressionChanged(
311 NotificationGroupManager.NotificationGroup group,
312 boolean suppressed) {
313 // More notifications could be added causing summary to no longer
314 // be suppressed -- in this case need to remove the key.
315 final String groupKey = group.summary != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400316 ? group.summary.getSbn().getGroupKey()
Mady Mellor740d85d2019-07-09 15:26:47 -0700317 : null;
318 if (!suppressed && groupKey != null
319 && mBubbleData.isSummarySuppressed(groupKey)) {
320 mBubbleData.removeSuppressedSummary(groupKey);
321 }
322 }
323 });
Mady Mellorb4991e62019-01-10 15:14:51 -0800324
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800325 mStatusBarWindowController = statusBarWindowController;
326 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700327 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500328
Mark Renoufcecc77b2019-01-30 16:32:24 -0500329 mTaskStackListener = new BubbleTaskStackListener();
330 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800331
Joshua Tsujia19515f2019-02-13 18:02:29 -0500332 try {
333 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
334 } catch (RemoteException e) {
335 e.printStackTrace();
336 }
Issei Suzukic0387542019-03-08 17:31:14 +0100337 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700338
339 mBarService = IStatusBarService.Stub.asInterface(
340 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400341
342 mSavedBubbleKeysPerUser = new SparseSetArray<>();
343 mCurrentUserId = mNotifUserManager.getCurrentUserId();
344 mNotifUserManager.addUserChangedListener(
345 newUserId -> {
346 saveBubbles(mCurrentUserId);
347 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
348 restoreBubbles(newUserId);
349 mCurrentUserId = newUserId;
350 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800351
352 mUserCreatedBubbles = new HashSet<>();
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800353 mUserBlockedBubbles = new HashSet<>();
Aran Inkaa4dfa72019-11-18 16:49:07 -0500354
355 mScreenshotHelper = new ScreenshotHelper(context);
Mady Mellor247ca2c2019-12-02 16:18:59 -0800356 mBubbleIconFactory = new BubbleIconFactory(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800357 }
358
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400359 /**
360 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
361 * method initializes the stack view and adds it to the StatusBar just above the scrim.
362 */
363 private void ensureStackViewCreated() {
364 if (mStackView == null) {
365 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
366 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Lyn Hanbde48202019-05-29 19:18:29 -0700367 int bubbleScrimIndex = sbv.indexOfChild(sbv.findViewById(R.id.scrim_for_bubble));
368 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
369 sbv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400370 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
371 if (mExpandListener != null) {
372 mStackView.setExpandListener(mExpandListener);
373 }
Aran Inkaa4dfa72019-11-18 16:49:07 -0500374 if (mBubbleScreenshotListener != null) {
375 mStackView.setBubbleScreenshotListener(mBubbleScreenshotListener);
376 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400377 }
378 }
379
Mark Renoufc19b4732019-06-26 12:08:33 -0400380 /**
381 * Records the notification key for any active bubbles. These are used to restore active
382 * bubbles when the user returns to the foreground.
383 *
384 * @param userId the id of the user
385 */
386 private void saveBubbles(@UserIdInt int userId) {
387 // First clear any existing keys that might be stored.
388 mSavedBubbleKeysPerUser.remove(userId);
389 // Add in all active bubbles for the current user.
390 for (Bubble bubble: mBubbleData.getBubbles()) {
391 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
392 }
393 }
394
395 /**
396 * Promotes existing notifications to Bubbles if they were previously bubbles.
397 *
398 * @param userId the id of the user
399 */
400 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400401 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
402 if (savedBubbleKeys == null) {
403 // There were no bubbles saved for this used.
404 return;
405 }
Evan Laird181de622019-10-24 09:53:02 -0400406 for (NotificationEntry e :
407 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400408 if (savedBubbleKeys.contains(e.getKey())
Mark Renoufc19b4732019-06-26 12:08:33 -0400409 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
410 && canLaunchInActivityView(mContext, e)) {
411 updateBubble(e, /* suppressFlyout= */ true);
412 }
413 }
414 // Finally, remove the entries for this user now that bubbles are restored.
415 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
416 }
417
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700418 @Override
419 public void onUiModeChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800420 updateForThemeChanges();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700421 }
422
423 @Override
424 public void onOverlayChanged() {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800425 updateForThemeChanges();
426 }
427
428 private void updateForThemeChanges() {
429 mBubbleIconFactory = new BubbleIconFactory(mContext);
430 for (Bubble b: mBubbleData.getBubbles()) {
431 b.getIconView().setBubbleIconFactory(mBubbleIconFactory);
432 b.getIconView().updateViews();
433 b.getExpandedView().applyThemeAttrs();
434 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700435 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700436 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700437 }
438 }
439
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400440 @Override
441 public void onConfigChanged(Configuration newConfig) {
442 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400443 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700444 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400445 }
446 }
447
Mady Mellor5549dd22018-11-06 18:07:34 -0800448 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800449 * Set a listener to be notified when some states of the bubbles change.
450 */
451 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
452 mStateChangeListener = listener;
453 }
454
455 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800456 * Set a listener to be notified of bubble expand events.
457 */
458 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100459 mExpandListener = ((isExpanding, key) -> {
460 if (listener != null) {
461 listener.onBubbleExpandChanged(isExpanding, key);
462 }
463 mStatusBarWindowController.setBubbleExpanded(isExpanding);
464 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800465 if (mStackView != null) {
466 mStackView.setExpandListener(mExpandListener);
467 }
468 }
469
470 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800471 * Whether or not there are bubbles present, regardless of them being visible on the
472 * screen (e.g. if on AOD).
473 */
474 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800475 if (mStackView == null) {
476 return false;
477 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400478 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800479 }
480
481 /**
482 * Whether the stack of bubbles is expanded or not.
483 */
484 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400485 return mBubbleData.isExpanded();
486 }
487
488 /**
489 * Tell the stack of bubbles to expand.
490 */
491 public void expandStack() {
492 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800493 }
494
495 /**
496 * Tell the stack of bubbles to collapse.
497 */
498 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400499 mBubbleData.setExpanded(false /* expanded */);
500 }
501
Mady Mellorce23c462019-06-17 17:30:07 -0700502 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700503 * True if either:
504 * (1) There is a bubble associated with the provided key and if its notification is hidden
505 * from the shade.
506 * (2) There is a group summary associated with the provided key that is hidden from the shade
507 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700508 *
Mady Mellore28fe102019-07-09 15:33:32 -0700509 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700510 */
511 public boolean isBubbleNotificationSuppressedFromShade(String key) {
Mady Mellore28fe102019-07-09 15:33:32 -0700512 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800513 && !mBubbleData.getBubbleWithKey(key).showInShade();
Evan Laird181de622019-10-24 09:53:02 -0400514 NotificationEntry entry = mNotificationEntryManager.getActiveNotificationUnfiltered(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400515 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellore28fe102019-07-09 15:33:32 -0700516 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700517 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
518 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700519 }
520
Mark Renouf71a3af62019-04-08 15:02:54 -0400521 @VisibleForTesting
522 void selectBubble(String key) {
523 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellor247ca2c2019-12-02 16:18:59 -0800524 mBubbleData.setSelectedBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800525 }
526
527 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400528 * Request the stack expand if needed, then select the specified Bubble as current.
529 *
530 * @param notificationKey the notification key for the bubble to be selected
531 */
532 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400533 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
534 if (bubble != null) {
535 mBubbleData.setSelectedBubble(bubble);
536 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400537 }
538 }
539
540 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800541 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
542 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500543 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400544 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800545 }
546
547 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500548 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
549 * is forwarded a back key down/up pair.
550 */
551 public void performBackPressIfNeeded() {
552 if (mStackView != null) {
553 mStackView.performBackPressIfNeeded();
554 }
555 }
556
557 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800558 * Adds or updates a bubble associated with the provided notification entry.
559 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400560 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800561 */
Mark Renouff97ed462019-04-05 13:46:24 -0400562 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700563 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400564 }
565
566 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700567 updateBubble(notif, suppressFlyout, true /* showInShade */);
568 }
569
570 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700571 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400572 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700573 notif.setInterruption();
574 }
Mady Mellor7f234902019-10-20 12:06:29 -0700575 mBubbleData.notificationEntryUpdated(notif, suppressFlyout, showInShade);
576 }
577
578 /**
579 * Called when a user has indicated that an active notification should be shown as a bubble.
580 * <p>
581 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
582 * the notification from appearing in the shade.
583 *
584 * @param entry the notification to show as a bubble.
585 */
586 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800587 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
588 Log.d(TAG, "onUserCreatedBubble: " + entry.getKey());
589 }
Heemin Seogba6337f2019-12-10 15:34:37 -0800590 mShadeController.collapsePanel(true);
Mady Mellor7f234902019-10-20 12:06:29 -0700591 entry.setFlagBubble(true);
592 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
Mady Mellorff076eb2019-11-13 10:12:06 -0800593 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800594 mUserBlockedBubbles.remove(entry.getKey());
Mady Mellor7f234902019-10-20 12:06:29 -0700595 }
596
597 /**
598 * Called when a user has indicated that an active notification appearing as a bubble should
599 * no longer be shown as a bubble.
600 *
601 * @param entry the notification to no longer show as a bubble.
602 */
603 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800604 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
605 Log.d(TAG, "onUserDemotedBubble: " + entry.getKey());
606 }
Mady Mellor7f234902019-10-20 12:06:29 -0700607 entry.setFlagBubble(false);
608 removeBubble(entry.getKey(), DISMISS_BLOCKED);
Mady Mellorff076eb2019-11-13 10:12:06 -0800609 mUserCreatedBubbles.remove(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800610 if (BubbleExperimentConfig.isPackageWhitelistedToAutoBubble(
611 mContext, entry.getSbn().getPackageName())) {
612 // This package is whitelist but user demoted the bubble, let's save it so we don't
613 // auto-bubble for the whitelist again.
614 mUserBlockedBubbles.add(entry.getKey());
615 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800616 }
617
618 /**
619 * Whether this bubble was explicitly created by the user via a SysUI affordance.
620 */
621 boolean isUserCreatedBubble(String key) {
622 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800623 }
624
625 /**
626 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500627 * <p>
628 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800629 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500630 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500631 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400632 // TEMP: refactor to change this to pass entry
633 Bubble bubble = mBubbleData.getBubbleWithKey(key);
634 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700635 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800636 }
637 }
638
Ned Burns01e38212019-01-03 16:32:52 -0500639 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700640 private final NotificationRemoveInterceptor mRemoveInterceptor =
641 new NotificationRemoveInterceptor() {
642 @Override
643 public boolean onNotificationRemoveRequested(String key, int reason) {
Evan Laird181de622019-10-24 09:53:02 -0400644 NotificationEntry entry =
645 mNotificationEntryManager.getActiveNotificationUnfiltered(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400646 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellor22f2f072019-04-18 13:26:18 -0700647 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
648
649 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
Mady Mellore28fe102019-07-09 15:33:32 -0700650 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
651 && mBubbleData.getSummaryKey(groupKey).equals(key));
Mady Mellor22f2f072019-04-18 13:26:18 -0700652 boolean isSummary = entry != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400653 && entry.getSbn().getNotification().isGroupSummary();
Mady Mellore28fe102019-07-09 15:33:32 -0700654 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
655 && bubbleChildren != null && !bubbleChildren.isEmpty();
Mady Mellor22f2f072019-04-18 13:26:18 -0700656
657 if (!inBubbleData && !isSummaryOfBubbles) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700658 return false;
659 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700660
661 final boolean isClearAll = reason == REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700662 final boolean isUserDimiss = reason == REASON_CANCEL || reason == REASON_CLICK;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700663 final boolean isAppCancel = reason == REASON_APP_CANCEL
664 || reason == REASON_APP_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700665 final boolean isSummaryCancel = reason == REASON_GROUP_SUMMARY_CANCELED;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700666
667 // Need to check for !appCancel here because the notification may have
668 // previously been dismissed & entry.isRowDismissed would still be true
Mady Mellorca184aae2019-09-17 16:07:12 -0700669 boolean userRemovedNotif = (entry != null && entry.isRowDismissed() && !isAppCancel)
Mady Mellor22f2f072019-04-18 13:26:18 -0700670 || isClearAll || isUserDimiss || isSummaryCancel;
671
672 if (isSummaryOfBubbles) {
673 return handleSummaryRemovalInterception(entry, userRemovedNotif);
674 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700675
676 // The bubble notification sticks around in the data as long as the bubble is
677 // not dismissed and the app hasn't cancelled the notification.
Mady Mellor22f2f072019-04-18 13:26:18 -0700678 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellorca184aae2019-09-17 16:07:12 -0700679 boolean bubbleExtended = entry != null && entry.isBubble() && userRemovedNotif;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700680 if (bubbleExtended) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800681 bubble.setShowInShade(false);
682 bubble.setShowDot(false /* show */, true /* animate */);
Beverly85d4c192019-09-30 11:40:39 -0400683 mNotificationEntryManager.updateNotifications(
684 "BubbleController.onNotificationRemoveRequested");
Mady Mellorc2ff0112019-03-28 14:18:06 -0700685 return true;
Mady Mellorff076eb2019-11-13 10:12:06 -0800686 } else if (!userRemovedNotif && entry != null
687 && !isUserCreatedBubble(bubble.getKey())) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700688 // This wasn't a user removal so we should remove the bubble as well
689 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
690 return false;
691 }
692 return false;
693 }
694 };
695
Mady Mellor22f2f072019-04-18 13:26:18 -0700696 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
697 boolean userRemovedNotif) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400698 String groupKey = summary.getSbn().getGroupKey();
Mady Mellor22f2f072019-04-18 13:26:18 -0700699 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
700
701 if (userRemovedNotif) {
702 // If it's a user dismiss we mark the children to be hidden from the shade.
703 for (int i = 0; i < bubbleChildren.size(); i++) {
704 Bubble bubbleChild = bubbleChildren.get(i);
705 // As far as group manager is concerned, once a child is no longer shown
706 // in the shade, it is essentially removed.
707 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
Mady Mellorb8aaf972019-11-26 10:28:00 -0800708 bubbleChild.setShowInShade(false);
709 bubbleChild.setShowDot(false /* show */, true /* animate */);
Mady Mellor22f2f072019-04-18 13:26:18 -0700710 }
711 // And since all children are removed, remove the summary.
712 mNotificationGroupManager.onEntryRemoved(summary);
713
714 // If the summary was auto-generated we don't need to keep that notification around
715 // because apps can't cancel it; so we only intercept & suppress real summaries.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400716 boolean isAutogroupSummary = (summary.getSbn().getNotification().flags
Mady Mellor22f2f072019-04-18 13:26:18 -0700717 & FLAG_AUTOGROUP_SUMMARY) != 0;
Mady Mellore28fe102019-07-09 15:33:32 -0700718 if (!isAutogroupSummary) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400719 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
720 summary.getKey());
Mady Mellore28fe102019-07-09 15:33:32 -0700721 // Tell shade to update for the suppression
Beverly85d4c192019-09-30 11:40:39 -0400722 mNotificationEntryManager.updateNotifications(
723 "BubbleController.handleSummaryRemovalInterception");
Mady Mellore28fe102019-07-09 15:33:32 -0700724 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700725 return !isAutogroupSummary;
726 } else {
Mady Mellore28fe102019-07-09 15:33:32 -0700727 // If it's not a user dismiss it's a cancel.
728 mBubbleData.removeSuppressedSummary(groupKey);
729
Mady Mellor22f2f072019-04-18 13:26:18 -0700730 // Remove any associated bubble children.
731 for (int i = 0; i < bubbleChildren.size(); i++) {
732 Bubble bubbleChild = bubbleChildren.get(i);
733 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
734 DISMISS_GROUP_CANCELLED);
735 }
736 return false;
737 }
738 }
739
Mady Mellorc2ff0112019-03-28 14:18:06 -0700740 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500741 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
742 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500743 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800744 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800745 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
Mady Mellor30672942019-12-04 15:43:19 -0800746 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800747 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700748
Mady Mellorca0c24c2019-05-16 16:14:32 -0700749 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
Mady Mellor30672942019-12-04 15:43:19 -0800750 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
Mady Mellorea13b232019-12-05 15:55:46 -0800751 if (wasAdjusted && !previouslyUserCreated) {
752 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
753 mUserCreatedBubbles.add(entry.getKey());
754 }
Mark Renouff97ed462019-04-05 13:46:24 -0400755 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800756 }
757 }
758
759 @Override
760 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800761 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800762 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
Mady Mellor30672942019-12-04 15:43:19 -0800763 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800764 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700765
Mady Mellorca0c24c2019-05-16 16:14:32 -0700766 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
Mady Mellor30672942019-12-04 15:43:19 -0800767 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400768 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
Mady Melloraa8fef22019-04-11 13:36:40 -0700769 // It was previously a bubble but no longer a bubble -- lets remove it
Ned Burns00b4b2d2019-10-17 22:09:27 -0400770 removeBubble(entry.getKey(), DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700771 } else if (shouldBubble) {
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 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400779
780 @Override
781 public void onNotificationRankingUpdated(RankingMap rankingMap) {
782 // Forward to BubbleData to block any bubbles which should no longer be shown
783 mBubbleData.notificationRankingUpdated(rankingMap);
784 }
Ned Burns01e38212019-01-03 16:32:52 -0500785 };
786
Mark Renouf71a3af62019-04-08 15:02:54 -0400787 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400788 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400789
Mark Renouf3bc5b362019-04-05 14:37:59 -0400790 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400791 public void applyUpdate(BubbleData.Update update) {
792 if (mStackView == null && update.addedBubble != null) {
793 // Lazy init stack view when the first bubble is added.
794 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400795 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400796
797 // If not yet initialized, ignore all other changes.
798 if (mStackView == null) {
799 return;
800 }
801
802 if (update.addedBubble != null) {
803 mStackView.addBubble(update.addedBubble);
804 }
805
806 // Collapsing? Do this first before remaining steps.
807 if (update.expandedChanged && !update.expanded) {
808 mStackView.setExpanded(false);
809 }
810
811 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700812 ArrayList<Pair<Bubble, Integer>> removedBubbles =
813 new ArrayList<>(update.removedBubbles);
814 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400815 final Bubble bubble = removed.first;
816 @DismissReason final int reason = removed.second;
817 mStackView.removeBubble(bubble);
818
Mark Renoufc19b4732019-06-26 12:08:33 -0400819 // If the bubble is removed for user switching, leave the notification in place.
820 if (reason != DISMISS_USER_CHANGED) {
821 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800822 && !bubble.showInShade()) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400823 // The bubble is gone & the notification is gone, time to actually remove it
824 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400825 bubble.getEntry().getSbn(), UNDEFINED_DISMISS_REASON);
Mark Renoufc19b4732019-06-26 12:08:33 -0400826 } else {
827 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400828 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700829
Mark Renoufc19b4732019-06-26 12:08:33 -0400830 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
831 // will get right result back
832 try {
833 mBarService.onNotificationBubbleChanged(bubble.getKey(),
834 false /* isBubble */);
835 } catch (RemoteException e) {
836 // Bad things have happened
837 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400838 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700839
Mady Mellore28fe102019-07-09 15:33:32 -0700840 // Check if removed bubble has an associated suppressed group summary that needs
841 // to be removed now.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400842 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700843 if (mBubbleData.isSummarySuppressed(groupKey)
844 && mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
845 // Time to actually remove the summary.
846 String notifKey = mBubbleData.getSummaryKey(groupKey);
847 mBubbleData.removeSuppressedSummary(groupKey);
848 NotificationEntry entry =
Evan Laird181de622019-10-24 09:53:02 -0400849 mNotificationEntryManager.getActiveNotificationUnfiltered(notifKey);
Mady Mellore28fe102019-07-09 15:33:32 -0700850 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400851 entry.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellore28fe102019-07-09 15:33:32 -0700852 }
853
Mady Mellor22f2f072019-04-18 13:26:18 -0700854 // Check if summary should be removed from NoManGroup
855 NotificationEntry summary = mNotificationGroupManager.getLogicalGroupSummary(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400856 bubble.getEntry().getSbn());
Mady Mellor22f2f072019-04-18 13:26:18 -0700857 if (summary != null) {
858 ArrayList<NotificationEntry> summaryChildren =
Ned Burns00b4b2d2019-10-17 22:09:27 -0400859 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
860 boolean isSummaryThisNotif = summary.getKey().equals(
861 bubble.getEntry().getKey());
Mady Mellore4348272019-07-29 17:43:36 -0700862 if (!isSummaryThisNotif
863 && (summaryChildren == null || summaryChildren.isEmpty())) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700864 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400865 summary.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellor22f2f072019-04-18 13:26:18 -0700866 }
867 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700868 }
869 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400870
Mark Renouf82a40e62019-05-23 16:16:24 -0400871 if (update.updatedBubble != null) {
872 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400873 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400874
Mark Renouf82a40e62019-05-23 16:16:24 -0400875 if (update.orderChanged) {
876 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400877 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400878
Mark Renouf82a40e62019-05-23 16:16:24 -0400879 if (update.selectionChanged) {
880 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700881 if (update.selectedBubble != null) {
882 mNotificationGroupManager.updateSuppression(
883 update.selectedBubble.getEntry());
884 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400885 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400886
Mark Renouf82a40e62019-05-23 16:16:24 -0400887 // Expanding? Apply this last.
888 if (update.expandedChanged && update.expanded) {
889 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400890 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400891
Beverly85d4c192019-09-30 11:40:39 -0400892 mNotificationEntryManager.updateNotifications(
893 "BubbleData.Listener.applyUpdate");
Lyn Han6c40fe72019-05-08 14:06:33 -0700894 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400895
Issei Suzukia8d07312019-06-07 12:56:19 +0200896 if (DEBUG_BUBBLE_CONTROLLER) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400897 Log.d(TAG, "[BubbleData]");
Lyn Han767d70e2019-12-10 18:02:23 -0800898 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400899 mBubbleData.getSelectedBubble()));
900
901 if (mStackView != null) {
902 Log.d(TAG, "[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -0800903 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400904 mStackView.getExpandedBubble()));
905 }
906 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400907 }
908 };
909
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800910 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400911 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700912 * Updates the visibility of the bubbles based on current state.
913 * Does not un-bubble, just hides or un-hides. Notifies any
914 * {@link BubbleStateChangeListener}s of visibility changes.
915 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800916 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700917 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800918 if (mStackView == null) {
919 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800920 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800921 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
922 // Bubbles only appear in unlocked shade
923 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +0000924 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800925 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800926 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700927
Mady Mellor698d9e82019-08-01 23:11:53 +0000928 // Let listeners know if bubble state changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700929 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +0000930 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellor88552b82019-08-05 22:38:59 +0000931 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -0700932 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
933 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
934 }
935
936 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800937 }
938
939 /**
940 * Rect indicating the touchable region for the bubble stack / expanded stack.
941 */
942 public Rect getTouchableRegion() {
943 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
944 return null;
945 }
946 mStackView.getBoundsOnScreen(mTempRect);
947 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800948 }
949
Mady Mellor390bff42019-04-05 15:09:01 -0700950 /**
951 * The display id of the expanded view, if the stack is expanded and not occluded by the
952 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
953 */
954 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200955 final Bubble bubble = getExpandedBubble(context);
956 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
957 }
958
959 @Nullable
960 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700961 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200962 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700963 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200964 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700965 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200966 final Bubble expandedBubble = mStackView.getExpandedBubble();
967 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700968 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200969 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700970 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200971 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700972 }
973
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800974 @VisibleForTesting
975 BubbleStackView getStackView() {
976 return mStackView;
977 }
978
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700979 /**
980 * Description of current bubble state.
981 */
982 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
983 pw.println("BubbleController state:");
984 mBubbleData.dump(fd, pw, args);
985 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -0400986 if (mStackView != null) {
987 mStackView.dump(fd, pw, args);
988 }
989 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700990 }
991
Mady Mellore80930e2019-03-21 16:00:45 -0700992 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -0500993 * This task stack listener is responsible for responding to tasks moved to the front
994 * which are on the default (main) display. When this happens, expanded bubbles must be
995 * collapsed so the user may interact with the app which was just moved to the front.
996 * <p>
997 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
998 * these calls via a main thread Handler.
999 */
1000 @MainThread
1001 private class BubbleTaskStackListener extends TaskStackChangeListener {
1002
Mark Renoufcecc77b2019-01-30 16:32:24 -05001003 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001004 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1005 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001006 if (!mStackView.isExpansionAnimating()) {
1007 mBubbleData.setExpanded(false);
1008 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001009 }
1010 }
1011
Mark Renoufcecc77b2019-01-30 16:32:24 -05001012 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001013 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001014 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001015 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001016 }
1017 }
Mark Renouf446251d2019-04-26 10:22:41 -04001018
1019 @Override
1020 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1021 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1022 mBubbleData.setExpanded(false);
1023 }
1024 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001025
1026 @Override
1027 public void onSingleTaskDisplayDrawn(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001028 final Bubble expandedBubble = mStackView != null
1029 ? mStackView.getExpandedBubble()
1030 : null;
Issei Suzukicac2a502019-04-16 16:52:50 +02001031 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
1032 expandedBubble.setContentVisibility(true);
1033 }
1034 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001035
1036 @Override
1037 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001038 final Bubble expandedBubble = mStackView != null
1039 ? mStackView.getExpandedBubble()
1040 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001041 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1042 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001043 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001044 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001045 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001046 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001047 }
1048
Mady Mellorca0c24c2019-05-16 16:14:32 -07001049 /**
1050 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1051 *
1052 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1053 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1054 *
1055 * @param context the context to use.
1056 * @param entry the entry to bubble.
1057 */
1058 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1059 PendingIntent intent = entry.getBubbleMetadata() != null
1060 ? entry.getBubbleMetadata().getIntent()
1061 : null;
1062 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001063 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001064 return false;
1065 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001066 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1067 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001068 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001069 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001070 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001071 Log.w(TAG, "Unable to send as bubble, "
1072 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001073 + intent);
1074 return false;
1075 }
1076 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001077 Log.w(TAG, "Unable to send as bubble, "
1078 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001079 + intent);
1080 return false;
1081 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001082 return true;
1083 }
1084
Joshua Tsujia19515f2019-02-13 18:02:29 -05001085 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001086 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001087 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001088 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1089 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1090 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001091 }
1092 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001093 }
Aran Inkaa4dfa72019-11-18 16:49:07 -05001094
1095 // TODO: Copied from RemoteInputView. Consolidate RemoteInput intent logic.
1096 private Intent prepareRemoteInputFromData(String contentType, Uri data,
1097 RemoteInput remoteInput, NotificationEntry entry) {
1098 HashMap<String, Uri> results = new HashMap<>();
1099 results.put(contentType, data);
1100 mRemoteInputUriController.grantInlineReplyUriPermission(entry.getSbn(), data);
1101 Intent fillInIntent = new Intent().addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
1102 RemoteInput.addDataResultToIntent(remoteInput, fillInIntent, results);
1103
1104 return fillInIntent;
1105 }
1106
1107 // TODO: Copied from RemoteInputView. Consolidate RemoteInput intent logic.
1108 private void sendRemoteInput(Intent intent, NotificationEntry entry,
1109 PendingIntent pendingIntent) {
1110 // Tell ShortcutManager that this package has been "activated". ShortcutManager
1111 // will reset the throttling for this package.
1112 // Strictly speaking, the intent receiver may be different from the notification publisher,
1113 // but that's an edge case, and also because we can't always know which package will receive
1114 // an intent, so we just reset for the publisher.
1115 mContext.getSystemService(ShortcutManager.class).onApplicationActive(
1116 entry.getSbn().getPackageName(),
1117 entry.getSbn().getUser().getIdentifier());
1118
1119 try {
1120 pendingIntent.send(mContext, 0, intent);
1121 } catch (PendingIntent.CanceledException e) {
1122 Log.i(TAG, "Unable to send remote input result", e);
1123 }
1124 }
1125
1126 private void sendScreenshotToBubble(Bubble bubble) {
Aran Ink141a8152019-12-12 13:31:23 -05001127 mScreenshotHelper.takeScreenshot(
1128 android.view.WindowManager.TAKE_SCREENSHOT_FULLSCREEN,
1129 true /* hasStatus */,
1130 true /* hasNav */,
1131 mHandler,
1132 new Consumer<Uri>() {
1133 @Override
1134 public void accept(Uri uri) {
1135 if (uri != null) {
1136 NotificationEntry entry = bubble.getEntry();
1137 Pair<RemoteInput, Notification.Action> pair = entry.getSbn()
1138 .getNotification().findRemoteInputActionPair(false);
1139 if (pair != null) {
1140 RemoteInput remoteInput = pair.first;
1141 Notification.Action action = pair.second;
1142 Intent dataIntent = prepareRemoteInputFromData("image/png", uri,
1143 remoteInput, entry);
1144 sendRemoteInput(dataIntent, entry, action.actionIntent);
1145 mBubbleData.setSelectedBubble(bubble);
1146 mBubbleData.setExpanded(true);
1147 } else {
1148 Log.w(TAG, "No RemoteInput found for notification: "
1149 + entry.getSbn().getKey());
Aran Inkaa4dfa72019-11-18 16:49:07 -05001150 }
Aran Ink141a8152019-12-12 13:31:23 -05001151 }
1152 }
1153 });
Aran Inkaa4dfa72019-11-18 16:49:07 -05001154 }
1155
1156 private final BubbleScreenshotListener mBubbleScreenshotListener =
1157 bubble -> sendScreenshotToBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001158}