blob: 4b75696fa256b114595aef164b4997f096d846a2 [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;
Mady Mellore80930e2019-03-21 16:00:45 -0700106import java.util.List;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500107import java.util.function.Consumer;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500108
Jason Monk27d01a622018-12-10 15:57:09 -0500109import javax.inject.Inject;
110import javax.inject.Singleton;
111
Mady Mellor7f234902019-10-20 12:06:29 -0700112import dagger.Lazy;
113
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800114/**
115 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
116 * Bubbles can be expanded to show more content.
117 *
118 * The controller manages addition, removal, and visible state of bubbles on screen.
119 */
Jason Monk27d01a622018-12-10 15:57:09 -0500120@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -0400121public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800122
Issei Suzukia8d07312019-06-07 12:56:19 +0200123 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800124
Mark Renouf08bc42a2019-03-07 13:01:59 -0500125 @Retention(SOURCE)
126 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400127 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700128 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400129 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500130 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700131
Mark Renouf08bc42a2019-03-07 13:01:59 -0500132 static final int DISMISS_USER_GESTURE = 1;
133 static final int DISMISS_AGED = 2;
134 static final int DISMISS_TASK_FINISHED = 3;
135 static final int DISMISS_BLOCKED = 4;
136 static final int DISMISS_NOTIF_CANCEL = 5;
137 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700138 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400139 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700140 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700141 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500142
Ned Burns01e38212019-01-03 16:32:52 -0500143 private final Context mContext;
144 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500145 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800146 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800147 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100148 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700149 private final NotificationGroupManager mNotificationGroupManager;
Mady Mellor7f234902019-10-20 12:06:29 -0700150 private final Lazy<ShadeController> mShadeController;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500151 private final RemoteInputUriController mRemoteInputUriController;
152 private Handler mHandler = new Handler() {};
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800153
Mady Mellor3dff9e62019-02-05 18:12:53 -0800154 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400155 @Nullable private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800156
Mark Renoufc19b4732019-06-26 12:08:33 -0400157 // Tracks the id of the current (foreground) user.
158 private int mCurrentUserId;
159 // Saves notification keys of active bubbles when users are switched.
160 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
161
Mady Mellorff076eb2019-11-13 10:12:06 -0800162 // Saves notification keys of user created "fake" bubbles so that we can allow notifications
163 // like these to bubble by default. Doesn't persist across reboots, not a long-term solution.
164 private final HashSet<String> mUserCreatedBubbles;
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800165 // If we're auto-bubbling bubbles via a whitelist, we need to track which notifs from that app
166 // have been "demoted" back to a notification so that we don't auto-bubbles those again.
167 // Doesn't persist across reboots, not a long-term solution.
168 private final HashSet<String> mUserBlockedBubbles;
Mady Mellorff076eb2019-11-13 10:12:06 -0800169
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800170 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500171 private final StatusBarWindowController mStatusBarWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400172 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800173 private StatusBarStateListener mStatusBarStateListener;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500174 private final ScreenshotHelper mScreenshotHelper;
175
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800176
Mady Melloraa8fef22019-04-11 13:36:40 -0700177 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700178 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800179
Mady Mellord1c78b262018-11-06 18:04:40 -0800180 // Used for determining view rect for touch interaction
181 private Rect mTempRect = new Rect();
182
Mark Renoufc19b4732019-06-26 12:08:33 -0400183 // Listens to user switch so bubbles can be saved and restored.
184 private final NotificationLockscreenUserManager mNotifUserManager;
185
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400186 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
187 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
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,
250 Lazy<ShadeController> shadeController,
251 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,
268 Lazy<ShadeController> shadeController,
269 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;
Mady Melloraa8fef22019-04-11 13:36:40 -0700279 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400280 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400281 mZenModeController = zenModeController;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500282 mRemoteInputUriController = remoteInputUriController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400283 mZenModeController.addCallback(new ZenModeController.Callback() {
284 @Override
285 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800286 for (Bubble b : mBubbleData.getBubbles()) {
287 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700288 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400289 }
290
291 @Override
292 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800293 for (Bubble b : mBubbleData.getBubbles()) {
294 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700295 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400296 }
297 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700298
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700299 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800300
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400301 mBubbleData = data;
302 mBubbleData.setListener(mBubbleDataListener);
303
Mady Mellor7f234902019-10-20 12:06:29 -0700304 mNotificationEntryManager = entryManager;
Ned Burns01e38212019-01-03 16:32:52 -0500305 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700306 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellor22f2f072019-04-18 13:26:18 -0700307 mNotificationGroupManager = groupManager;
Mady Mellor740d85d2019-07-09 15:26:47 -0700308 mNotificationGroupManager.addOnGroupChangeListener(
309 new NotificationGroupManager.OnGroupChangeListener() {
310 @Override
311 public void onGroupSuppressionChanged(
312 NotificationGroupManager.NotificationGroup group,
313 boolean suppressed) {
314 // More notifications could be added causing summary to no longer
315 // be suppressed -- in this case need to remove the key.
316 final String groupKey = group.summary != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400317 ? group.summary.getSbn().getGroupKey()
Mady Mellor740d85d2019-07-09 15:26:47 -0700318 : null;
319 if (!suppressed && groupKey != null
320 && mBubbleData.isSummarySuppressed(groupKey)) {
321 mBubbleData.removeSuppressedSummary(groupKey);
322 }
323 }
324 });
Mady Mellorb4991e62019-01-10 15:14:51 -0800325
Mady Mellor7f234902019-10-20 12:06:29 -0700326 mShadeController = shadeController;
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(
347 newUserId -> {
348 saveBubbles(mCurrentUserId);
349 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
350 restoreBubbles(newUserId);
351 mCurrentUserId = newUserId;
352 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800353
354 mUserCreatedBubbles = new HashSet<>();
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800355 mUserBlockedBubbles = new HashSet<>();
Aran Inkaa4dfa72019-11-18 16:49:07 -0500356
357 mScreenshotHelper = new ScreenshotHelper(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800358 }
359
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400360 /**
361 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
362 * method initializes the stack view and adds it to the StatusBar just above the scrim.
363 */
364 private void ensureStackViewCreated() {
365 if (mStackView == null) {
366 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
367 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Lyn Hanbde48202019-05-29 19:18:29 -0700368 int bubbleScrimIndex = sbv.indexOfChild(sbv.findViewById(R.id.scrim_for_bubble));
369 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
370 sbv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400371 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
372 if (mExpandListener != null) {
373 mStackView.setExpandListener(mExpandListener);
374 }
Aran Inkaa4dfa72019-11-18 16:49:07 -0500375 if (mBubbleScreenshotListener != null) {
376 mStackView.setBubbleScreenshotListener(mBubbleScreenshotListener);
377 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400378 }
379 }
380
Mark Renoufc19b4732019-06-26 12:08:33 -0400381 /**
382 * Records the notification key for any active bubbles. These are used to restore active
383 * bubbles when the user returns to the foreground.
384 *
385 * @param userId the id of the user
386 */
387 private void saveBubbles(@UserIdInt int userId) {
388 // First clear any existing keys that might be stored.
389 mSavedBubbleKeysPerUser.remove(userId);
390 // Add in all active bubbles for the current user.
391 for (Bubble bubble: mBubbleData.getBubbles()) {
392 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
393 }
394 }
395
396 /**
397 * Promotes existing notifications to Bubbles if they were previously bubbles.
398 *
399 * @param userId the id of the user
400 */
401 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400402 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
403 if (savedBubbleKeys == null) {
404 // There were no bubbles saved for this used.
405 return;
406 }
Evan Laird181de622019-10-24 09:53:02 -0400407 for (NotificationEntry e :
408 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400409 if (savedBubbleKeys.contains(e.getKey())
Mark Renoufc19b4732019-06-26 12:08:33 -0400410 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
411 && canLaunchInActivityView(mContext, e)) {
412 updateBubble(e, /* suppressFlyout= */ true);
413 }
414 }
415 // Finally, remove the entries for this user now that bubbles are restored.
416 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
417 }
418
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700419 @Override
420 public void onUiModeChanged() {
421 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700422 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700423 }
424 }
425
426 @Override
427 public void onOverlayChanged() {
428 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700429 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700430 }
431 }
432
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400433 @Override
434 public void onConfigChanged(Configuration newConfig) {
435 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400436 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700437 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400438 }
439 }
440
Mady Mellor5549dd22018-11-06 18:07:34 -0800441 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800442 * Set a listener to be notified when some states of the bubbles change.
443 */
444 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
445 mStateChangeListener = listener;
446 }
447
448 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800449 * Set a listener to be notified of bubble expand events.
450 */
451 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100452 mExpandListener = ((isExpanding, key) -> {
453 if (listener != null) {
454 listener.onBubbleExpandChanged(isExpanding, key);
455 }
456 mStatusBarWindowController.setBubbleExpanded(isExpanding);
457 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800458 if (mStackView != null) {
459 mStackView.setExpandListener(mExpandListener);
460 }
461 }
462
463 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800464 * Whether or not there are bubbles present, regardless of them being visible on the
465 * screen (e.g. if on AOD).
466 */
467 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800468 if (mStackView == null) {
469 return false;
470 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400471 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800472 }
473
474 /**
475 * Whether the stack of bubbles is expanded or not.
476 */
477 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400478 return mBubbleData.isExpanded();
479 }
480
481 /**
482 * Tell the stack of bubbles to expand.
483 */
484 public void expandStack() {
485 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800486 }
487
488 /**
489 * Tell the stack of bubbles to collapse.
490 */
491 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400492 mBubbleData.setExpanded(false /* expanded */);
493 }
494
Mady Mellorce23c462019-06-17 17:30:07 -0700495 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700496 * True if either:
497 * (1) There is a bubble associated with the provided key and if its notification is hidden
498 * from the shade.
499 * (2) There is a group summary associated with the provided key that is hidden from the shade
500 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700501 *
Mady Mellore28fe102019-07-09 15:33:32 -0700502 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700503 */
504 public boolean isBubbleNotificationSuppressedFromShade(String key) {
Mady Mellore28fe102019-07-09 15:33:32 -0700505 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800506 && !mBubbleData.getBubbleWithKey(key).showInShade();
Evan Laird181de622019-10-24 09:53:02 -0400507 NotificationEntry entry = mNotificationEntryManager.getActiveNotificationUnfiltered(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400508 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellore28fe102019-07-09 15:33:32 -0700509 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700510 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
511 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700512 }
513
Mark Renouf71a3af62019-04-08 15:02:54 -0400514 void selectBubble(Bubble bubble) {
515 mBubbleData.setSelectedBubble(bubble);
516 }
517
518 @VisibleForTesting
519 void selectBubble(String key) {
520 Bubble bubble = mBubbleData.getBubbleWithKey(key);
521 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800522 }
523
524 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400525 * Request the stack expand if needed, then select the specified Bubble as current.
526 *
527 * @param notificationKey the notification key for the bubble to be selected
528 */
529 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400530 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
531 if (bubble != null) {
532 mBubbleData.setSelectedBubble(bubble);
533 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400534 }
535 }
536
537 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800538 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
539 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500540 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400541 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800542 }
543
544 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500545 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
546 * is forwarded a back key down/up pair.
547 */
548 public void performBackPressIfNeeded() {
549 if (mStackView != null) {
550 mStackView.performBackPressIfNeeded();
551 }
552 }
553
554 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800555 * Adds or updates a bubble associated with the provided notification entry.
556 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400557 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800558 */
Mark Renouff97ed462019-04-05 13:46:24 -0400559 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700560 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400561 }
562
563 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700564 updateBubble(notif, suppressFlyout, true /* showInShade */);
565 }
566
567 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700568 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400569 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700570 notif.setInterruption();
571 }
Mady Mellor7f234902019-10-20 12:06:29 -0700572 mBubbleData.notificationEntryUpdated(notif, suppressFlyout, showInShade);
573 }
574
575 /**
576 * Called when a user has indicated that an active notification should be shown as a bubble.
577 * <p>
578 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
579 * the notification from appearing in the shade.
580 *
581 * @param entry the notification to show as a bubble.
582 */
583 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800584 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
585 Log.d(TAG, "onUserCreatedBubble: " + entry.getKey());
586 }
Mady Mellor7f234902019-10-20 12:06:29 -0700587 mShadeController.get().collapsePanel(true);
588 entry.setFlagBubble(true);
589 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
Mady Mellorff076eb2019-11-13 10:12:06 -0800590 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800591 mUserBlockedBubbles.remove(entry.getKey());
Mady Mellor7f234902019-10-20 12:06:29 -0700592 }
593
594 /**
595 * Called when a user has indicated that an active notification appearing as a bubble should
596 * no longer be shown as a bubble.
597 *
598 * @param entry the notification to no longer show as a bubble.
599 */
600 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800601 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
602 Log.d(TAG, "onUserDemotedBubble: " + entry.getKey());
603 }
Mady Mellor7f234902019-10-20 12:06:29 -0700604 entry.setFlagBubble(false);
605 removeBubble(entry.getKey(), DISMISS_BLOCKED);
Mady Mellorff076eb2019-11-13 10:12:06 -0800606 mUserCreatedBubbles.remove(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800607 if (BubbleExperimentConfig.isPackageWhitelistedToAutoBubble(
608 mContext, entry.getSbn().getPackageName())) {
609 // This package is whitelist but user demoted the bubble, let's save it so we don't
610 // auto-bubble for the whitelist again.
611 mUserBlockedBubbles.add(entry.getKey());
612 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800613 }
614
615 /**
616 * Whether this bubble was explicitly created by the user via a SysUI affordance.
617 */
618 boolean isUserCreatedBubble(String key) {
619 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800620 }
621
622 /**
623 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500624 * <p>
625 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800626 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500627 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500628 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400629 // TEMP: refactor to change this to pass entry
630 Bubble bubble = mBubbleData.getBubbleWithKey(key);
631 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700632 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800633 }
634 }
635
Ned Burns01e38212019-01-03 16:32:52 -0500636 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700637 private final NotificationRemoveInterceptor mRemoveInterceptor =
638 new NotificationRemoveInterceptor() {
639 @Override
640 public boolean onNotificationRemoveRequested(String key, int reason) {
Evan Laird181de622019-10-24 09:53:02 -0400641 NotificationEntry entry =
642 mNotificationEntryManager.getActiveNotificationUnfiltered(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400643 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellor22f2f072019-04-18 13:26:18 -0700644 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
645
646 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
Mady Mellore28fe102019-07-09 15:33:32 -0700647 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
648 && mBubbleData.getSummaryKey(groupKey).equals(key));
Mady Mellor22f2f072019-04-18 13:26:18 -0700649 boolean isSummary = entry != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400650 && entry.getSbn().getNotification().isGroupSummary();
Mady Mellore28fe102019-07-09 15:33:32 -0700651 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
652 && bubbleChildren != null && !bubbleChildren.isEmpty();
Mady Mellor22f2f072019-04-18 13:26:18 -0700653
654 if (!inBubbleData && !isSummaryOfBubbles) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700655 return false;
656 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700657
658 final boolean isClearAll = reason == REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700659 final boolean isUserDimiss = reason == REASON_CANCEL || reason == REASON_CLICK;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700660 final boolean isAppCancel = reason == REASON_APP_CANCEL
661 || reason == REASON_APP_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700662 final boolean isSummaryCancel = reason == REASON_GROUP_SUMMARY_CANCELED;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700663
664 // Need to check for !appCancel here because the notification may have
665 // previously been dismissed & entry.isRowDismissed would still be true
Mady Mellorca184aae2019-09-17 16:07:12 -0700666 boolean userRemovedNotif = (entry != null && entry.isRowDismissed() && !isAppCancel)
Mady Mellor22f2f072019-04-18 13:26:18 -0700667 || isClearAll || isUserDimiss || isSummaryCancel;
668
669 if (isSummaryOfBubbles) {
670 return handleSummaryRemovalInterception(entry, userRemovedNotif);
671 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700672
673 // The bubble notification sticks around in the data as long as the bubble is
674 // not dismissed and the app hasn't cancelled the notification.
Mady Mellor22f2f072019-04-18 13:26:18 -0700675 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellorca184aae2019-09-17 16:07:12 -0700676 boolean bubbleExtended = entry != null && entry.isBubble() && userRemovedNotif;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700677 if (bubbleExtended) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800678 bubble.setShowInShade(false);
679 bubble.setShowDot(false /* show */, true /* animate */);
Beverly85d4c192019-09-30 11:40:39 -0400680 mNotificationEntryManager.updateNotifications(
681 "BubbleController.onNotificationRemoveRequested");
Mady Mellorc2ff0112019-03-28 14:18:06 -0700682 return true;
Mady Mellorff076eb2019-11-13 10:12:06 -0800683 } else if (!userRemovedNotif && entry != null
684 && !isUserCreatedBubble(bubble.getKey())) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700685 // This wasn't a user removal so we should remove the bubble as well
686 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
687 return false;
688 }
689 return false;
690 }
691 };
692
Mady Mellor22f2f072019-04-18 13:26:18 -0700693 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
694 boolean userRemovedNotif) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400695 String groupKey = summary.getSbn().getGroupKey();
Mady Mellor22f2f072019-04-18 13:26:18 -0700696 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
697
698 if (userRemovedNotif) {
699 // If it's a user dismiss we mark the children to be hidden from the shade.
700 for (int i = 0; i < bubbleChildren.size(); i++) {
701 Bubble bubbleChild = bubbleChildren.get(i);
702 // As far as group manager is concerned, once a child is no longer shown
703 // in the shade, it is essentially removed.
704 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
Mady Mellorb8aaf972019-11-26 10:28:00 -0800705 bubbleChild.setShowInShade(false);
706 bubbleChild.setShowDot(false /* show */, true /* animate */);
Mady Mellor22f2f072019-04-18 13:26:18 -0700707 }
708 // And since all children are removed, remove the summary.
709 mNotificationGroupManager.onEntryRemoved(summary);
710
711 // If the summary was auto-generated we don't need to keep that notification around
712 // because apps can't cancel it; so we only intercept & suppress real summaries.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400713 boolean isAutogroupSummary = (summary.getSbn().getNotification().flags
Mady Mellor22f2f072019-04-18 13:26:18 -0700714 & FLAG_AUTOGROUP_SUMMARY) != 0;
Mady Mellore28fe102019-07-09 15:33:32 -0700715 if (!isAutogroupSummary) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400716 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
717 summary.getKey());
Mady Mellore28fe102019-07-09 15:33:32 -0700718 // Tell shade to update for the suppression
Beverly85d4c192019-09-30 11:40:39 -0400719 mNotificationEntryManager.updateNotifications(
720 "BubbleController.handleSummaryRemovalInterception");
Mady Mellore28fe102019-07-09 15:33:32 -0700721 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700722 return !isAutogroupSummary;
723 } else {
Mady Mellore28fe102019-07-09 15:33:32 -0700724 // If it's not a user dismiss it's a cancel.
725 mBubbleData.removeSuppressedSummary(groupKey);
726
Mady Mellor22f2f072019-04-18 13:26:18 -0700727 // Remove any associated bubble children.
728 for (int i = 0; i < bubbleChildren.size(); i++) {
729 Bubble bubbleChild = bubbleChildren.get(i);
730 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
731 DISMISS_GROUP_CANCELLED);
732 }
733 return false;
734 }
735 }
736
Mady Mellorc2ff0112019-03-28 14:18:06 -0700737 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500738 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
739 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500740 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800741 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800742 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
Mady Mellor30672942019-12-04 15:43:19 -0800743 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800744 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700745
Mady Mellorca0c24c2019-05-16 16:14:32 -0700746 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
Mady Mellor30672942019-12-04 15:43:19 -0800747 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
Mady Mellorea13b232019-12-05 15:55:46 -0800748 if (wasAdjusted && !previouslyUserCreated) {
749 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
750 mUserCreatedBubbles.add(entry.getKey());
751 }
Mark Renouff97ed462019-04-05 13:46:24 -0400752 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800753 }
754 }
755
756 @Override
757 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800758 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800759 boolean userBlocked = mUserBlockedBubbles.contains(entry.getKey());
Mady Mellor30672942019-12-04 15:43:19 -0800760 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
Mady Mellor3b86a4f2019-12-11 13:15:41 -0800761 mContext, entry, previouslyUserCreated, userBlocked);
Mady Mellor7f234902019-10-20 12:06:29 -0700762
Mady Mellorca0c24c2019-05-16 16:14:32 -0700763 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
Mady Mellor30672942019-12-04 15:43:19 -0800764 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400765 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
Mady Melloraa8fef22019-04-11 13:36:40 -0700766 // It was previously a bubble but no longer a bubble -- lets remove it
Ned Burns00b4b2d2019-10-17 22:09:27 -0400767 removeBubble(entry.getKey(), DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700768 } else if (shouldBubble) {
Mady Mellorea13b232019-12-05 15:55:46 -0800769 if (wasAdjusted && !previouslyUserCreated) {
770 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
771 mUserCreatedBubbles.add(entry.getKey());
772 }
Mark Renouff97ed462019-04-05 13:46:24 -0400773 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800774 }
775 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400776
777 @Override
778 public void onNotificationRankingUpdated(RankingMap rankingMap) {
779 // Forward to BubbleData to block any bubbles which should no longer be shown
780 mBubbleData.notificationRankingUpdated(rankingMap);
781 }
Ned Burns01e38212019-01-03 16:32:52 -0500782 };
783
Mark Renouf71a3af62019-04-08 15:02:54 -0400784 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400785 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400786
Mark Renouf3bc5b362019-04-05 14:37:59 -0400787 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400788 public void applyUpdate(BubbleData.Update update) {
789 if (mStackView == null && update.addedBubble != null) {
790 // Lazy init stack view when the first bubble is added.
791 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400792 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400793
794 // If not yet initialized, ignore all other changes.
795 if (mStackView == null) {
796 return;
797 }
798
799 if (update.addedBubble != null) {
800 mStackView.addBubble(update.addedBubble);
801 }
802
803 // Collapsing? Do this first before remaining steps.
804 if (update.expandedChanged && !update.expanded) {
805 mStackView.setExpanded(false);
806 }
807
808 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700809 ArrayList<Pair<Bubble, Integer>> removedBubbles =
810 new ArrayList<>(update.removedBubbles);
811 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400812 final Bubble bubble = removed.first;
813 @DismissReason final int reason = removed.second;
814 mStackView.removeBubble(bubble);
815
Mark Renoufc19b4732019-06-26 12:08:33 -0400816 // If the bubble is removed for user switching, leave the notification in place.
817 if (reason != DISMISS_USER_CHANGED) {
818 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800819 && !bubble.showInShade()) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400820 // The bubble is gone & the notification is gone, time to actually remove it
821 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400822 bubble.getEntry().getSbn(), UNDEFINED_DISMISS_REASON);
Mark Renoufc19b4732019-06-26 12:08:33 -0400823 } else {
824 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400825 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700826
Mark Renoufc19b4732019-06-26 12:08:33 -0400827 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
828 // will get right result back
829 try {
830 mBarService.onNotificationBubbleChanged(bubble.getKey(),
831 false /* isBubble */);
832 } catch (RemoteException e) {
833 // Bad things have happened
834 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400835 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700836
Mady Mellore28fe102019-07-09 15:33:32 -0700837 // Check if removed bubble has an associated suppressed group summary that needs
838 // to be removed now.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400839 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700840 if (mBubbleData.isSummarySuppressed(groupKey)
841 && mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
842 // Time to actually remove the summary.
843 String notifKey = mBubbleData.getSummaryKey(groupKey);
844 mBubbleData.removeSuppressedSummary(groupKey);
845 NotificationEntry entry =
Evan Laird181de622019-10-24 09:53:02 -0400846 mNotificationEntryManager.getActiveNotificationUnfiltered(notifKey);
Mady Mellore28fe102019-07-09 15:33:32 -0700847 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400848 entry.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellore28fe102019-07-09 15:33:32 -0700849 }
850
Mady Mellor22f2f072019-04-18 13:26:18 -0700851 // Check if summary should be removed from NoManGroup
852 NotificationEntry summary = mNotificationGroupManager.getLogicalGroupSummary(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400853 bubble.getEntry().getSbn());
Mady Mellor22f2f072019-04-18 13:26:18 -0700854 if (summary != null) {
855 ArrayList<NotificationEntry> summaryChildren =
Ned Burns00b4b2d2019-10-17 22:09:27 -0400856 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
857 boolean isSummaryThisNotif = summary.getKey().equals(
858 bubble.getEntry().getKey());
Mady Mellore4348272019-07-29 17:43:36 -0700859 if (!isSummaryThisNotif
860 && (summaryChildren == null || summaryChildren.isEmpty())) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700861 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400862 summary.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellor22f2f072019-04-18 13:26:18 -0700863 }
864 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700865 }
866 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400867
Mark Renouf82a40e62019-05-23 16:16:24 -0400868 if (update.updatedBubble != null) {
869 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400870 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400871
Mark Renouf82a40e62019-05-23 16:16:24 -0400872 if (update.orderChanged) {
873 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400874 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400875
Mark Renouf82a40e62019-05-23 16:16:24 -0400876 if (update.selectionChanged) {
877 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700878 if (update.selectedBubble != null) {
879 mNotificationGroupManager.updateSuppression(
880 update.selectedBubble.getEntry());
881 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400882 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400883
Mark Renouf82a40e62019-05-23 16:16:24 -0400884 // Expanding? Apply this last.
885 if (update.expandedChanged && update.expanded) {
886 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400887 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400888
Beverly85d4c192019-09-30 11:40:39 -0400889 mNotificationEntryManager.updateNotifications(
890 "BubbleData.Listener.applyUpdate");
Lyn Han6c40fe72019-05-08 14:06:33 -0700891 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400892
Issei Suzukia8d07312019-06-07 12:56:19 +0200893 if (DEBUG_BUBBLE_CONTROLLER) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400894 Log.d(TAG, "[BubbleData]");
895 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
896 mBubbleData.getSelectedBubble()));
897
898 if (mStackView != null) {
899 Log.d(TAG, "[BubbleStackView]");
900 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
901 mStackView.getExpandedBubble()));
902 }
903 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400904 }
905 };
906
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800907 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400908 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700909 * Updates the visibility of the bubbles based on current state.
910 * Does not un-bubble, just hides or un-hides. Notifies any
911 * {@link BubbleStateChangeListener}s of visibility changes.
912 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800913 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700914 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800915 if (mStackView == null) {
916 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800917 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800918 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
919 // Bubbles only appear in unlocked shade
920 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +0000921 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800922 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800923 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700924
Mady Mellor698d9e82019-08-01 23:11:53 +0000925 // Let listeners know if bubble state changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700926 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +0000927 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellor88552b82019-08-05 22:38:59 +0000928 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -0700929 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
930 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
931 }
932
933 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800934 }
935
936 /**
937 * Rect indicating the touchable region for the bubble stack / expanded stack.
938 */
939 public Rect getTouchableRegion() {
940 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
941 return null;
942 }
943 mStackView.getBoundsOnScreen(mTempRect);
944 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800945 }
946
Mady Mellor390bff42019-04-05 15:09:01 -0700947 /**
948 * The display id of the expanded view, if the stack is expanded and not occluded by the
949 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
950 */
951 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200952 final Bubble bubble = getExpandedBubble(context);
953 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
954 }
955
956 @Nullable
957 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700958 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200959 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700960 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200961 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700962 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200963 final Bubble expandedBubble = mStackView.getExpandedBubble();
964 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700965 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200966 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700967 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200968 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700969 }
970
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800971 @VisibleForTesting
972 BubbleStackView getStackView() {
973 return mStackView;
974 }
975
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700976 /**
977 * Description of current bubble state.
978 */
979 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
980 pw.println("BubbleController state:");
981 mBubbleData.dump(fd, pw, args);
982 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -0400983 if (mStackView != null) {
984 mStackView.dump(fd, pw, args);
985 }
986 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700987 }
988
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400989 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
990 StringBuilder sb = new StringBuilder();
991 for (Bubble bubble : bubbles) {
992 if (bubble == null) {
993 sb.append(" <null> !!!!!\n");
994 } else {
995 boolean isSelected = (bubble == selected);
996 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
997 ((isSelected) ? "->" : " "),
998 bubble.getLastActivity(),
999 (bubble.isOngoing() ? 1 : 0),
1000 bubble.getKey()));
1001 }
1002 }
1003 return sb.toString();
1004 }
1005
Mady Mellore80930e2019-03-21 16:00:45 -07001006 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -05001007 * This task stack listener is responsible for responding to tasks moved to the front
1008 * which are on the default (main) display. When this happens, expanded bubbles must be
1009 * collapsed so the user may interact with the app which was just moved to the front.
1010 * <p>
1011 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
1012 * these calls via a main thread Handler.
1013 */
1014 @MainThread
1015 private class BubbleTaskStackListener extends TaskStackChangeListener {
1016
Mark Renoufcecc77b2019-01-30 16:32:24 -05001017 @Override
Mark Renoufc808f062019-02-07 15:20:37 -05001018 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
1019 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -07001020 if (!mStackView.isExpansionAnimating()) {
1021 mBubbleData.setExpanded(false);
1022 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001023 }
1024 }
1025
Mark Renoufcecc77b2019-01-30 16:32:24 -05001026 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -05001027 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -05001028 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -04001029 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -05001030 }
1031 }
Mark Renouf446251d2019-04-26 10:22:41 -04001032
1033 @Override
1034 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1035 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1036 mBubbleData.setExpanded(false);
1037 }
1038 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001039
1040 @Override
1041 public void onSingleTaskDisplayDrawn(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001042 final Bubble expandedBubble = mStackView != null
1043 ? mStackView.getExpandedBubble()
1044 : null;
Issei Suzukicac2a502019-04-16 16:52:50 +02001045 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
1046 expandedBubble.setContentVisibility(true);
1047 }
1048 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001049
1050 @Override
1051 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001052 final Bubble expandedBubble = mStackView != null
1053 ? mStackView.getExpandedBubble()
1054 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001055 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1056 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001057 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001058 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001059 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001060 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001061 }
1062
Mady Mellorca0c24c2019-05-16 16:14:32 -07001063 /**
1064 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1065 *
1066 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1067 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1068 *
1069 * @param context the context to use.
1070 * @param entry the entry to bubble.
1071 */
1072 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1073 PendingIntent intent = entry.getBubbleMetadata() != null
1074 ? entry.getBubbleMetadata().getIntent()
1075 : null;
1076 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001077 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001078 return false;
1079 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001080 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1081 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001082 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001083 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001084 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001085 Log.w(TAG, "Unable to send as bubble, "
1086 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001087 + intent);
1088 return false;
1089 }
1090 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001091 Log.w(TAG, "Unable to send as bubble, "
1092 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001093 + intent);
1094 return false;
1095 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001096 return true;
1097 }
1098
Joshua Tsujia19515f2019-02-13 18:02:29 -05001099 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001100 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001101 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001102 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1103 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1104 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001105 }
1106 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001107 }
Aran Inkaa4dfa72019-11-18 16:49:07 -05001108
1109 // TODO: Copied from RemoteInputView. Consolidate RemoteInput intent logic.
1110 private Intent prepareRemoteInputFromData(String contentType, Uri data,
1111 RemoteInput remoteInput, NotificationEntry entry) {
1112 HashMap<String, Uri> results = new HashMap<>();
1113 results.put(contentType, data);
1114 mRemoteInputUriController.grantInlineReplyUriPermission(entry.getSbn(), data);
1115 Intent fillInIntent = new Intent().addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
1116 RemoteInput.addDataResultToIntent(remoteInput, fillInIntent, results);
1117
1118 return fillInIntent;
1119 }
1120
1121 // TODO: Copied from RemoteInputView. Consolidate RemoteInput intent logic.
1122 private void sendRemoteInput(Intent intent, NotificationEntry entry,
1123 PendingIntent pendingIntent) {
1124 // Tell ShortcutManager that this package has been "activated". ShortcutManager
1125 // will reset the throttling for this package.
1126 // Strictly speaking, the intent receiver may be different from the notification publisher,
1127 // but that's an edge case, and also because we can't always know which package will receive
1128 // an intent, so we just reset for the publisher.
1129 mContext.getSystemService(ShortcutManager.class).onApplicationActive(
1130 entry.getSbn().getPackageName(),
1131 entry.getSbn().getUser().getIdentifier());
1132
1133 try {
1134 pendingIntent.send(mContext, 0, intent);
1135 } catch (PendingIntent.CanceledException e) {
1136 Log.i(TAG, "Unable to send remote input result", e);
1137 }
1138 }
1139
1140 private void sendScreenshotToBubble(Bubble bubble) {
1141 // delay allows the bubble menu to disappear before the screenshot
1142 // done here because we already have a Handler to delay with.
1143 // TODO: Hide bubble + menu UI from screenshots entirely instead of just delaying.
1144 mHandler.postDelayed(new Runnable() {
1145 @Override
1146 public void run() {
1147 mScreenshotHelper.takeScreenshot(
1148 android.view.WindowManager.TAKE_SCREENSHOT_FULLSCREEN,
1149 true /* hasStatus */,
1150 true /* hasNav */,
1151 mHandler,
1152 new Consumer<Uri>() {
1153 @Override
1154 public void accept(Uri uri) {
1155 if (uri != null) {
1156 NotificationEntry entry = bubble.getEntry();
1157 Pair<RemoteInput, Notification.Action> pair = entry.getSbn()
1158 .getNotification().findRemoteInputActionPair(false);
1159 RemoteInput remoteInput = pair.first;
1160 Notification.Action action = pair.second;
1161 Intent dataIntent = prepareRemoteInputFromData("image/png", uri,
1162 remoteInput, entry);
1163 sendRemoteInput(dataIntent, entry, action.actionIntent);
1164 mBubbleData.setSelectedBubble(bubble);
1165 mBubbleData.setExpanded(true);
1166 }
1167 }
1168 });
1169 }
1170 }, 200);
1171 }
1172
1173 private final BubbleScreenshotListener mBubbleScreenshotListener =
1174 bubble -> sendScreenshotToBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001175}