blob: b5622432f919c96336e709a55749a356bae35252 [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;
165
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800166 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500167 private final StatusBarWindowController mStatusBarWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400168 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800169 private StatusBarStateListener mStatusBarStateListener;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500170 private final ScreenshotHelper mScreenshotHelper;
171
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800172
Mady Melloraa8fef22019-04-11 13:36:40 -0700173 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700174 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800175
Mady Mellord1c78b262018-11-06 18:04:40 -0800176 // Used for determining view rect for touch interaction
177 private Rect mTempRect = new Rect();
178
Mark Renoufc19b4732019-06-26 12:08:33 -0400179 // Listens to user switch so bubbles can be saved and restored.
180 private final NotificationLockscreenUserManager mNotifUserManager;
181
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400182 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
183 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
184
Mady Mellor5549dd22018-11-06 18:07:34 -0800185 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800186 * Listener to be notified when some states of the bubbles change.
187 */
188 public interface BubbleStateChangeListener {
189 /**
190 * Called when the stack has bubbles or no longer has bubbles.
191 */
192 void onHasBubblesChanged(boolean hasBubbles);
193 }
194
Mady Mellorcd9b1302018-11-06 18:08:04 -0800195 /**
196 * Listener to find out about stack expansion / collapse events.
197 */
198 public interface BubbleExpandListener {
199 /**
200 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700201 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800202 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800203 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800204 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800205 void onBubbleExpandChanged(boolean isExpanding, String key);
206 }
207
208 /**
Aran Inkaa4dfa72019-11-18 16:49:07 -0500209 * Listener for handling bubble screenshot events.
210 */
211 public interface BubbleScreenshotListener {
212 /**
213 * Called to trigger taking a screenshot and sending the result to a bubble.
214 */
215 void onBubbleScreenshot(Bubble bubble);
216 }
217
218 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800219 * Listens for the current state of the status bar and updates the visibility state
220 * of bubbles as needed.
221 */
222 private class StatusBarStateListener implements StatusBarStateController.StateListener {
223 private int mState;
224 /**
225 * Returns the current status bar state.
226 */
227 public int getCurrentState() {
228 return mState;
229 }
230
231 @Override
232 public void onStateChanged(int newState) {
233 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400234 boolean shouldCollapse = (mState != SHADE);
235 if (shouldCollapse) {
236 collapseStack();
237 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700238 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800239 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800240 }
241
Jason Monk27d01a622018-12-10 15:57:09 -0500242 @Inject
Mady Mellor7f234902019-10-20 12:06:29 -0700243 public BubbleController(Context context,
244 StatusBarWindowController statusBarWindowController,
245 StatusBarStateController statusBarStateController,
246 Lazy<ShadeController> shadeController,
247 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700248 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400249 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400250 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700251 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700252 NotificationGroupManager groupManager,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500253 NotificationEntryManager entryManager,
254 RemoteInputUriController remoteInputUriController) {
Mady Mellor7f234902019-10-20 12:06:29 -0700255 this(context, statusBarWindowController, statusBarStateController, shadeController,
256 data, null /* synchronizer */, configurationController, interruptionStateProvider,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500257 zenModeController, notifUserManager, groupManager, entryManager,
258 remoteInputUriController);
Mady Mellor7f234902019-10-20 12:06:29 -0700259 }
260
261 public BubbleController(Context context,
262 StatusBarWindowController statusBarWindowController,
263 StatusBarStateController statusBarStateController,
264 Lazy<ShadeController> shadeController,
265 BubbleData data,
266 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
267 ConfigurationController configurationController,
268 NotificationInterruptionStateProvider interruptionStateProvider,
269 ZenModeController zenModeController,
270 NotificationLockscreenUserManager notifUserManager,
271 NotificationGroupManager groupManager,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500272 NotificationEntryManager entryManager,
273 RemoteInputUriController remoteInputUriController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800274 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700275 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400276 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400277 mZenModeController = zenModeController;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500278 mRemoteInputUriController = remoteInputUriController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400279 mZenModeController.addCallback(new ZenModeController.Callback() {
280 @Override
281 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800282 for (Bubble b : mBubbleData.getBubbles()) {
283 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700284 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400285 }
286
287 @Override
288 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800289 for (Bubble b : mBubbleData.getBubbles()) {
290 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700291 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400292 }
293 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700294
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700295 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800296
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400297 mBubbleData = data;
298 mBubbleData.setListener(mBubbleDataListener);
299
Mady Mellor7f234902019-10-20 12:06:29 -0700300 mNotificationEntryManager = entryManager;
Ned Burns01e38212019-01-03 16:32:52 -0500301 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700302 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellor22f2f072019-04-18 13:26:18 -0700303 mNotificationGroupManager = groupManager;
Mady Mellor740d85d2019-07-09 15:26:47 -0700304 mNotificationGroupManager.addOnGroupChangeListener(
305 new NotificationGroupManager.OnGroupChangeListener() {
306 @Override
307 public void onGroupSuppressionChanged(
308 NotificationGroupManager.NotificationGroup group,
309 boolean suppressed) {
310 // More notifications could be added causing summary to no longer
311 // be suppressed -- in this case need to remove the key.
312 final String groupKey = group.summary != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400313 ? group.summary.getSbn().getGroupKey()
Mady Mellor740d85d2019-07-09 15:26:47 -0700314 : null;
315 if (!suppressed && groupKey != null
316 && mBubbleData.isSummarySuppressed(groupKey)) {
317 mBubbleData.removeSuppressedSummary(groupKey);
318 }
319 }
320 });
Mady Mellorb4991e62019-01-10 15:14:51 -0800321
Mady Mellor7f234902019-10-20 12:06:29 -0700322 mShadeController = shadeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800323 mStatusBarWindowController = statusBarWindowController;
324 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700325 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500326
Mark Renoufcecc77b2019-01-30 16:32:24 -0500327 mTaskStackListener = new BubbleTaskStackListener();
328 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800329
Joshua Tsujia19515f2019-02-13 18:02:29 -0500330 try {
331 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
332 } catch (RemoteException e) {
333 e.printStackTrace();
334 }
Issei Suzukic0387542019-03-08 17:31:14 +0100335 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700336
337 mBarService = IStatusBarService.Stub.asInterface(
338 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400339
340 mSavedBubbleKeysPerUser = new SparseSetArray<>();
341 mCurrentUserId = mNotifUserManager.getCurrentUserId();
342 mNotifUserManager.addUserChangedListener(
343 newUserId -> {
344 saveBubbles(mCurrentUserId);
345 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
346 restoreBubbles(newUserId);
347 mCurrentUserId = newUserId;
348 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800349
350 mUserCreatedBubbles = new HashSet<>();
Aran Inkaa4dfa72019-11-18 16:49:07 -0500351
352 mScreenshotHelper = new ScreenshotHelper(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800353 }
354
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400355 /**
356 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
357 * method initializes the stack view and adds it to the StatusBar just above the scrim.
358 */
359 private void ensureStackViewCreated() {
360 if (mStackView == null) {
361 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
362 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Lyn Hanbde48202019-05-29 19:18:29 -0700363 int bubbleScrimIndex = sbv.indexOfChild(sbv.findViewById(R.id.scrim_for_bubble));
364 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
365 sbv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400366 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
367 if (mExpandListener != null) {
368 mStackView.setExpandListener(mExpandListener);
369 }
Aran Inkaa4dfa72019-11-18 16:49:07 -0500370 if (mBubbleScreenshotListener != null) {
371 mStackView.setBubbleScreenshotListener(mBubbleScreenshotListener);
372 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400373 }
374 }
375
Mark Renoufc19b4732019-06-26 12:08:33 -0400376 /**
377 * Records the notification key for any active bubbles. These are used to restore active
378 * bubbles when the user returns to the foreground.
379 *
380 * @param userId the id of the user
381 */
382 private void saveBubbles(@UserIdInt int userId) {
383 // First clear any existing keys that might be stored.
384 mSavedBubbleKeysPerUser.remove(userId);
385 // Add in all active bubbles for the current user.
386 for (Bubble bubble: mBubbleData.getBubbles()) {
387 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
388 }
389 }
390
391 /**
392 * Promotes existing notifications to Bubbles if they were previously bubbles.
393 *
394 * @param userId the id of the user
395 */
396 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400397 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
398 if (savedBubbleKeys == null) {
399 // There were no bubbles saved for this used.
400 return;
401 }
Evan Laird181de622019-10-24 09:53:02 -0400402 for (NotificationEntry e :
403 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400404 if (savedBubbleKeys.contains(e.getKey())
Mark Renoufc19b4732019-06-26 12:08:33 -0400405 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
406 && canLaunchInActivityView(mContext, e)) {
407 updateBubble(e, /* suppressFlyout= */ true);
408 }
409 }
410 // Finally, remove the entries for this user now that bubbles are restored.
411 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
412 }
413
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700414 @Override
415 public void onUiModeChanged() {
416 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700417 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700418 }
419 }
420
421 @Override
422 public void onOverlayChanged() {
423 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700424 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700425 }
426 }
427
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400428 @Override
429 public void onConfigChanged(Configuration newConfig) {
430 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400431 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700432 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400433 }
434 }
435
Mady Mellor5549dd22018-11-06 18:07:34 -0800436 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800437 * Set a listener to be notified when some states of the bubbles change.
438 */
439 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
440 mStateChangeListener = listener;
441 }
442
443 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800444 * Set a listener to be notified of bubble expand events.
445 */
446 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100447 mExpandListener = ((isExpanding, key) -> {
448 if (listener != null) {
449 listener.onBubbleExpandChanged(isExpanding, key);
450 }
451 mStatusBarWindowController.setBubbleExpanded(isExpanding);
452 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800453 if (mStackView != null) {
454 mStackView.setExpandListener(mExpandListener);
455 }
456 }
457
458 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800459 * Whether or not there are bubbles present, regardless of them being visible on the
460 * screen (e.g. if on AOD).
461 */
462 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800463 if (mStackView == null) {
464 return false;
465 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400466 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800467 }
468
469 /**
470 * Whether the stack of bubbles is expanded or not.
471 */
472 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400473 return mBubbleData.isExpanded();
474 }
475
476 /**
477 * Tell the stack of bubbles to expand.
478 */
479 public void expandStack() {
480 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800481 }
482
483 /**
484 * Tell the stack of bubbles to collapse.
485 */
486 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400487 mBubbleData.setExpanded(false /* expanded */);
488 }
489
Mady Mellorce23c462019-06-17 17:30:07 -0700490 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700491 * True if either:
492 * (1) There is a bubble associated with the provided key and if its notification is hidden
493 * from the shade.
494 * (2) There is a group summary associated with the provided key that is hidden from the shade
495 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700496 *
Mady Mellore28fe102019-07-09 15:33:32 -0700497 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700498 */
499 public boolean isBubbleNotificationSuppressedFromShade(String key) {
Mady Mellore28fe102019-07-09 15:33:32 -0700500 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800501 && !mBubbleData.getBubbleWithKey(key).showInShade();
Evan Laird181de622019-10-24 09:53:02 -0400502 NotificationEntry entry = mNotificationEntryManager.getActiveNotificationUnfiltered(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400503 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellore28fe102019-07-09 15:33:32 -0700504 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700505 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
506 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700507 }
508
Mark Renouf71a3af62019-04-08 15:02:54 -0400509 void selectBubble(Bubble bubble) {
510 mBubbleData.setSelectedBubble(bubble);
511 }
512
513 @VisibleForTesting
514 void selectBubble(String key) {
515 Bubble bubble = mBubbleData.getBubbleWithKey(key);
516 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800517 }
518
519 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400520 * Request the stack expand if needed, then select the specified Bubble as current.
521 *
522 * @param notificationKey the notification key for the bubble to be selected
523 */
524 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400525 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
526 if (bubble != null) {
527 mBubbleData.setSelectedBubble(bubble);
528 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400529 }
530 }
531
532 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800533 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
534 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500535 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400536 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800537 }
538
539 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500540 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
541 * is forwarded a back key down/up pair.
542 */
543 public void performBackPressIfNeeded() {
544 if (mStackView != null) {
545 mStackView.performBackPressIfNeeded();
546 }
547 }
548
549 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800550 * Adds or updates a bubble associated with the provided notification entry.
551 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400552 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800553 */
Mark Renouff97ed462019-04-05 13:46:24 -0400554 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700555 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400556 }
557
558 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700559 updateBubble(notif, suppressFlyout, true /* showInShade */);
560 }
561
562 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700563 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400564 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700565 notif.setInterruption();
566 }
Mady Mellor7f234902019-10-20 12:06:29 -0700567 mBubbleData.notificationEntryUpdated(notif, suppressFlyout, showInShade);
568 }
569
570 /**
571 * Called when a user has indicated that an active notification should be shown as a bubble.
572 * <p>
573 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
574 * the notification from appearing in the shade.
575 *
576 * @param entry the notification to show as a bubble.
577 */
578 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800579 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
580 Log.d(TAG, "onUserCreatedBubble: " + entry.getKey());
581 }
Mady Mellor7f234902019-10-20 12:06:29 -0700582 mShadeController.get().collapsePanel(true);
583 entry.setFlagBubble(true);
584 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
Mady Mellorff076eb2019-11-13 10:12:06 -0800585 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor7f234902019-10-20 12:06:29 -0700586 }
587
588 /**
589 * Called when a user has indicated that an active notification appearing as a bubble should
590 * no longer be shown as a bubble.
591 *
592 * @param entry the notification to no longer show as a bubble.
593 */
594 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800595 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
596 Log.d(TAG, "onUserDemotedBubble: " + entry.getKey());
597 }
Mady Mellor7f234902019-10-20 12:06:29 -0700598 entry.setFlagBubble(false);
599 removeBubble(entry.getKey(), DISMISS_BLOCKED);
Mady Mellorff076eb2019-11-13 10:12:06 -0800600 mUserCreatedBubbles.remove(entry.getKey());
601 }
602
603 /**
604 * Whether this bubble was explicitly created by the user via a SysUI affordance.
605 */
606 boolean isUserCreatedBubble(String key) {
607 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800608 }
609
610 /**
611 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500612 * <p>
613 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800614 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500615 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500616 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400617 // TEMP: refactor to change this to pass entry
618 Bubble bubble = mBubbleData.getBubbleWithKey(key);
619 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700620 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800621 }
622 }
623
Ned Burns01e38212019-01-03 16:32:52 -0500624 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700625 private final NotificationRemoveInterceptor mRemoveInterceptor =
626 new NotificationRemoveInterceptor() {
627 @Override
628 public boolean onNotificationRemoveRequested(String key, int reason) {
Evan Laird181de622019-10-24 09:53:02 -0400629 NotificationEntry entry =
630 mNotificationEntryManager.getActiveNotificationUnfiltered(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400631 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellor22f2f072019-04-18 13:26:18 -0700632 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
633
634 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
Mady Mellore28fe102019-07-09 15:33:32 -0700635 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
636 && mBubbleData.getSummaryKey(groupKey).equals(key));
Mady Mellor22f2f072019-04-18 13:26:18 -0700637 boolean isSummary = entry != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400638 && entry.getSbn().getNotification().isGroupSummary();
Mady Mellore28fe102019-07-09 15:33:32 -0700639 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
640 && bubbleChildren != null && !bubbleChildren.isEmpty();
Mady Mellor22f2f072019-04-18 13:26:18 -0700641
642 if (!inBubbleData && !isSummaryOfBubbles) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700643 return false;
644 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700645
646 final boolean isClearAll = reason == REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700647 final boolean isUserDimiss = reason == REASON_CANCEL || reason == REASON_CLICK;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700648 final boolean isAppCancel = reason == REASON_APP_CANCEL
649 || reason == REASON_APP_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700650 final boolean isSummaryCancel = reason == REASON_GROUP_SUMMARY_CANCELED;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700651
652 // Need to check for !appCancel here because the notification may have
653 // previously been dismissed & entry.isRowDismissed would still be true
Mady Mellorca184aae2019-09-17 16:07:12 -0700654 boolean userRemovedNotif = (entry != null && entry.isRowDismissed() && !isAppCancel)
Mady Mellor22f2f072019-04-18 13:26:18 -0700655 || isClearAll || isUserDimiss || isSummaryCancel;
656
657 if (isSummaryOfBubbles) {
658 return handleSummaryRemovalInterception(entry, userRemovedNotif);
659 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700660
661 // The bubble notification sticks around in the data as long as the bubble is
662 // not dismissed and the app hasn't cancelled the notification.
Mady Mellor22f2f072019-04-18 13:26:18 -0700663 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellorca184aae2019-09-17 16:07:12 -0700664 boolean bubbleExtended = entry != null && entry.isBubble() && userRemovedNotif;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700665 if (bubbleExtended) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800666 bubble.setShowInShade(false);
667 bubble.setShowDot(false /* show */, true /* animate */);
Beverly85d4c192019-09-30 11:40:39 -0400668 mNotificationEntryManager.updateNotifications(
669 "BubbleController.onNotificationRemoveRequested");
Mady Mellorc2ff0112019-03-28 14:18:06 -0700670 return true;
Mady Mellorff076eb2019-11-13 10:12:06 -0800671 } else if (!userRemovedNotif && entry != null
672 && !isUserCreatedBubble(bubble.getKey())) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700673 // This wasn't a user removal so we should remove the bubble as well
674 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
675 return false;
676 }
677 return false;
678 }
679 };
680
Mady Mellor22f2f072019-04-18 13:26:18 -0700681 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
682 boolean userRemovedNotif) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400683 String groupKey = summary.getSbn().getGroupKey();
Mady Mellor22f2f072019-04-18 13:26:18 -0700684 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
685
686 if (userRemovedNotif) {
687 // If it's a user dismiss we mark the children to be hidden from the shade.
688 for (int i = 0; i < bubbleChildren.size(); i++) {
689 Bubble bubbleChild = bubbleChildren.get(i);
690 // As far as group manager is concerned, once a child is no longer shown
691 // in the shade, it is essentially removed.
692 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
Mady Mellorb8aaf972019-11-26 10:28:00 -0800693 bubbleChild.setShowInShade(false);
694 bubbleChild.setShowDot(false /* show */, true /* animate */);
Mady Mellor22f2f072019-04-18 13:26:18 -0700695 }
696 // And since all children are removed, remove the summary.
697 mNotificationGroupManager.onEntryRemoved(summary);
698
699 // If the summary was auto-generated we don't need to keep that notification around
700 // because apps can't cancel it; so we only intercept & suppress real summaries.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400701 boolean isAutogroupSummary = (summary.getSbn().getNotification().flags
Mady Mellor22f2f072019-04-18 13:26:18 -0700702 & FLAG_AUTOGROUP_SUMMARY) != 0;
Mady Mellore28fe102019-07-09 15:33:32 -0700703 if (!isAutogroupSummary) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400704 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
705 summary.getKey());
Mady Mellore28fe102019-07-09 15:33:32 -0700706 // Tell shade to update for the suppression
Beverly85d4c192019-09-30 11:40:39 -0400707 mNotificationEntryManager.updateNotifications(
708 "BubbleController.handleSummaryRemovalInterception");
Mady Mellore28fe102019-07-09 15:33:32 -0700709 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700710 return !isAutogroupSummary;
711 } else {
Mady Mellore28fe102019-07-09 15:33:32 -0700712 // If it's not a user dismiss it's a cancel.
713 mBubbleData.removeSuppressedSummary(groupKey);
714
Mady Mellor22f2f072019-04-18 13:26:18 -0700715 // Remove any associated bubble children.
716 for (int i = 0; i < bubbleChildren.size(); i++) {
717 Bubble bubbleChild = bubbleChildren.get(i);
718 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
719 DISMISS_GROUP_CANCELLED);
720 }
721 return false;
722 }
723 }
724
Mady Mellorc2ff0112019-03-28 14:18:06 -0700725 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500726 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
727 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500728 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800729 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
Mady Mellor30672942019-12-04 15:43:19 -0800730 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
731 mContext, entry, previouslyUserCreated);
Mady Mellor7f234902019-10-20 12:06:29 -0700732
Mady Mellorca0c24c2019-05-16 16:14:32 -0700733 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
Mady Mellor30672942019-12-04 15:43:19 -0800734 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
Mady Mellorea13b232019-12-05 15:55:46 -0800735 if (wasAdjusted && !previouslyUserCreated) {
736 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
737 mUserCreatedBubbles.add(entry.getKey());
738 }
Mark Renouff97ed462019-04-05 13:46:24 -0400739 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800740 }
741 }
742
743 @Override
744 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800745 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
Mady Mellor30672942019-12-04 15:43:19 -0800746 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
747 mContext, entry, previouslyUserCreated);
Mady Mellor7f234902019-10-20 12:06:29 -0700748
Mady Mellorca0c24c2019-05-16 16:14:32 -0700749 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
Mady Mellor30672942019-12-04 15:43:19 -0800750 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400751 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
Mady Melloraa8fef22019-04-11 13:36:40 -0700752 // It was previously a bubble but no longer a bubble -- lets remove it
Ned Burns00b4b2d2019-10-17 22:09:27 -0400753 removeBubble(entry.getKey(), DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700754 } else if (shouldBubble) {
Mady Mellorea13b232019-12-05 15:55:46 -0800755 if (wasAdjusted && !previouslyUserCreated) {
756 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
757 mUserCreatedBubbles.add(entry.getKey());
758 }
Mark Renouff97ed462019-04-05 13:46:24 -0400759 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800760 }
761 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400762
763 @Override
764 public void onNotificationRankingUpdated(RankingMap rankingMap) {
765 // Forward to BubbleData to block any bubbles which should no longer be shown
766 mBubbleData.notificationRankingUpdated(rankingMap);
767 }
Ned Burns01e38212019-01-03 16:32:52 -0500768 };
769
Mark Renouf71a3af62019-04-08 15:02:54 -0400770 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400771 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400772
Mark Renouf3bc5b362019-04-05 14:37:59 -0400773 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400774 public void applyUpdate(BubbleData.Update update) {
775 if (mStackView == null && update.addedBubble != null) {
776 // Lazy init stack view when the first bubble is added.
777 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400778 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400779
780 // If not yet initialized, ignore all other changes.
781 if (mStackView == null) {
782 return;
783 }
784
785 if (update.addedBubble != null) {
786 mStackView.addBubble(update.addedBubble);
787 }
788
789 // Collapsing? Do this first before remaining steps.
790 if (update.expandedChanged && !update.expanded) {
791 mStackView.setExpanded(false);
792 }
793
794 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700795 ArrayList<Pair<Bubble, Integer>> removedBubbles =
796 new ArrayList<>(update.removedBubbles);
797 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400798 final Bubble bubble = removed.first;
799 @DismissReason final int reason = removed.second;
800 mStackView.removeBubble(bubble);
801
Mark Renoufc19b4732019-06-26 12:08:33 -0400802 // If the bubble is removed for user switching, leave the notification in place.
803 if (reason != DISMISS_USER_CHANGED) {
804 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800805 && !bubble.showInShade()) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400806 // The bubble is gone & the notification is gone, time to actually remove it
807 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400808 bubble.getEntry().getSbn(), UNDEFINED_DISMISS_REASON);
Mark Renoufc19b4732019-06-26 12:08:33 -0400809 } else {
810 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400811 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700812
Mark Renoufc19b4732019-06-26 12:08:33 -0400813 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
814 // will get right result back
815 try {
816 mBarService.onNotificationBubbleChanged(bubble.getKey(),
817 false /* isBubble */);
818 } catch (RemoteException e) {
819 // Bad things have happened
820 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400821 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700822
Mady Mellore28fe102019-07-09 15:33:32 -0700823 // Check if removed bubble has an associated suppressed group summary that needs
824 // to be removed now.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400825 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700826 if (mBubbleData.isSummarySuppressed(groupKey)
827 && mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
828 // Time to actually remove the summary.
829 String notifKey = mBubbleData.getSummaryKey(groupKey);
830 mBubbleData.removeSuppressedSummary(groupKey);
831 NotificationEntry entry =
Evan Laird181de622019-10-24 09:53:02 -0400832 mNotificationEntryManager.getActiveNotificationUnfiltered(notifKey);
Mady Mellore28fe102019-07-09 15:33:32 -0700833 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400834 entry.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellore28fe102019-07-09 15:33:32 -0700835 }
836
Mady Mellor22f2f072019-04-18 13:26:18 -0700837 // Check if summary should be removed from NoManGroup
838 NotificationEntry summary = mNotificationGroupManager.getLogicalGroupSummary(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400839 bubble.getEntry().getSbn());
Mady Mellor22f2f072019-04-18 13:26:18 -0700840 if (summary != null) {
841 ArrayList<NotificationEntry> summaryChildren =
Ned Burns00b4b2d2019-10-17 22:09:27 -0400842 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
843 boolean isSummaryThisNotif = summary.getKey().equals(
844 bubble.getEntry().getKey());
Mady Mellore4348272019-07-29 17:43:36 -0700845 if (!isSummaryThisNotif
846 && (summaryChildren == null || summaryChildren.isEmpty())) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700847 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400848 summary.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellor22f2f072019-04-18 13:26:18 -0700849 }
850 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700851 }
852 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400853
Mark Renouf82a40e62019-05-23 16:16:24 -0400854 if (update.updatedBubble != null) {
855 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400856 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400857
Mark Renouf82a40e62019-05-23 16:16:24 -0400858 if (update.orderChanged) {
859 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400860 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400861
Mark Renouf82a40e62019-05-23 16:16:24 -0400862 if (update.selectionChanged) {
863 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700864 if (update.selectedBubble != null) {
865 mNotificationGroupManager.updateSuppression(
866 update.selectedBubble.getEntry());
867 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400868 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400869
Mark Renouf82a40e62019-05-23 16:16:24 -0400870 // Expanding? Apply this last.
871 if (update.expandedChanged && update.expanded) {
872 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400873 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400874
Beverly85d4c192019-09-30 11:40:39 -0400875 mNotificationEntryManager.updateNotifications(
876 "BubbleData.Listener.applyUpdate");
Lyn Han6c40fe72019-05-08 14:06:33 -0700877 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400878
Issei Suzukia8d07312019-06-07 12:56:19 +0200879 if (DEBUG_BUBBLE_CONTROLLER) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400880 Log.d(TAG, "[BubbleData]");
Lyn Han767d70e2019-12-10 18:02:23 -0800881 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400882 mBubbleData.getSelectedBubble()));
883
884 if (mStackView != null) {
885 Log.d(TAG, "[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -0800886 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400887 mStackView.getExpandedBubble()));
888 }
889 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400890 }
891 };
892
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800893 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400894 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700895 * Updates the visibility of the bubbles based on current state.
896 * Does not un-bubble, just hides or un-hides. Notifies any
897 * {@link BubbleStateChangeListener}s of visibility changes.
898 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800899 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700900 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800901 if (mStackView == null) {
902 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800903 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800904 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
905 // Bubbles only appear in unlocked shade
906 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +0000907 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800908 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800909 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700910
Mady Mellor698d9e82019-08-01 23:11:53 +0000911 // Let listeners know if bubble state changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700912 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +0000913 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellor88552b82019-08-05 22:38:59 +0000914 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -0700915 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
916 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
917 }
918
919 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800920 }
921
922 /**
923 * Rect indicating the touchable region for the bubble stack / expanded stack.
924 */
925 public Rect getTouchableRegion() {
926 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
927 return null;
928 }
929 mStackView.getBoundsOnScreen(mTempRect);
930 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800931 }
932
Mady Mellor390bff42019-04-05 15:09:01 -0700933 /**
934 * The display id of the expanded view, if the stack is expanded and not occluded by the
935 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
936 */
937 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200938 final Bubble bubble = getExpandedBubble(context);
939 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
940 }
941
942 @Nullable
943 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700944 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200945 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700946 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200947 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700948 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200949 final Bubble expandedBubble = mStackView.getExpandedBubble();
950 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700951 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200952 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700953 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200954 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700955 }
956
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800957 @VisibleForTesting
958 BubbleStackView getStackView() {
959 return mStackView;
960 }
961
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700962 /**
963 * Description of current bubble state.
964 */
965 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
966 pw.println("BubbleController state:");
967 mBubbleData.dump(fd, pw, args);
968 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -0400969 if (mStackView != null) {
970 mStackView.dump(fd, pw, args);
971 }
972 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700973 }
974
Mady Mellore80930e2019-03-21 16:00:45 -0700975 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -0500976 * This task stack listener is responsible for responding to tasks moved to the front
977 * which are on the default (main) display. When this happens, expanded bubbles must be
978 * collapsed so the user may interact with the app which was just moved to the front.
979 * <p>
980 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
981 * these calls via a main thread Handler.
982 */
983 @MainThread
984 private class BubbleTaskStackListener extends TaskStackChangeListener {
985
Mark Renoufcecc77b2019-01-30 16:32:24 -0500986 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500987 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
988 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -0700989 if (!mStackView.isExpansionAnimating()) {
990 mBubbleData.setExpanded(false);
991 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500992 }
993 }
994
Mark Renoufcecc77b2019-01-30 16:32:24 -0500995 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500996 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500997 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400998 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500999 }
1000 }
Mark Renouf446251d2019-04-26 10:22:41 -04001001
1002 @Override
1003 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1004 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1005 mBubbleData.setExpanded(false);
1006 }
1007 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001008
1009 @Override
1010 public void onSingleTaskDisplayDrawn(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001011 final Bubble expandedBubble = mStackView != null
1012 ? mStackView.getExpandedBubble()
1013 : null;
Issei Suzukicac2a502019-04-16 16:52:50 +02001014 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
1015 expandedBubble.setContentVisibility(true);
1016 }
1017 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001018
1019 @Override
1020 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001021 final Bubble expandedBubble = mStackView != null
1022 ? mStackView.getExpandedBubble()
1023 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001024 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1025 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001026 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001027 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001028 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001029 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001030 }
1031
Mady Mellorca0c24c2019-05-16 16:14:32 -07001032 /**
1033 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1034 *
1035 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1036 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1037 *
1038 * @param context the context to use.
1039 * @param entry the entry to bubble.
1040 */
1041 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1042 PendingIntent intent = entry.getBubbleMetadata() != null
1043 ? entry.getBubbleMetadata().getIntent()
1044 : null;
1045 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001046 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001047 return false;
1048 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001049 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1050 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001051 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001052 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001053 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001054 Log.w(TAG, "Unable to send as bubble, "
1055 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001056 + intent);
1057 return false;
1058 }
1059 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001060 Log.w(TAG, "Unable to send as bubble, "
1061 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001062 + intent);
1063 return false;
1064 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001065 return true;
1066 }
1067
Joshua Tsujia19515f2019-02-13 18:02:29 -05001068 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001069 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001070 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001071 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1072 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1073 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001074 }
1075 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001076 }
Aran Inkaa4dfa72019-11-18 16:49:07 -05001077
1078 // TODO: Copied from RemoteInputView. Consolidate RemoteInput intent logic.
1079 private Intent prepareRemoteInputFromData(String contentType, Uri data,
1080 RemoteInput remoteInput, NotificationEntry entry) {
1081 HashMap<String, Uri> results = new HashMap<>();
1082 results.put(contentType, data);
1083 mRemoteInputUriController.grantInlineReplyUriPermission(entry.getSbn(), data);
1084 Intent fillInIntent = new Intent().addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
1085 RemoteInput.addDataResultToIntent(remoteInput, fillInIntent, results);
1086
1087 return fillInIntent;
1088 }
1089
1090 // TODO: Copied from RemoteInputView. Consolidate RemoteInput intent logic.
1091 private void sendRemoteInput(Intent intent, NotificationEntry entry,
1092 PendingIntent pendingIntent) {
1093 // Tell ShortcutManager that this package has been "activated". ShortcutManager
1094 // will reset the throttling for this package.
1095 // Strictly speaking, the intent receiver may be different from the notification publisher,
1096 // but that's an edge case, and also because we can't always know which package will receive
1097 // an intent, so we just reset for the publisher.
1098 mContext.getSystemService(ShortcutManager.class).onApplicationActive(
1099 entry.getSbn().getPackageName(),
1100 entry.getSbn().getUser().getIdentifier());
1101
1102 try {
1103 pendingIntent.send(mContext, 0, intent);
1104 } catch (PendingIntent.CanceledException e) {
1105 Log.i(TAG, "Unable to send remote input result", e);
1106 }
1107 }
1108
1109 private void sendScreenshotToBubble(Bubble bubble) {
1110 // delay allows the bubble menu to disappear before the screenshot
1111 // done here because we already have a Handler to delay with.
1112 // TODO: Hide bubble + menu UI from screenshots entirely instead of just delaying.
1113 mHandler.postDelayed(new Runnable() {
1114 @Override
1115 public void run() {
1116 mScreenshotHelper.takeScreenshot(
1117 android.view.WindowManager.TAKE_SCREENSHOT_FULLSCREEN,
1118 true /* hasStatus */,
1119 true /* hasNav */,
1120 mHandler,
1121 new Consumer<Uri>() {
1122 @Override
1123 public void accept(Uri uri) {
1124 if (uri != null) {
1125 NotificationEntry entry = bubble.getEntry();
1126 Pair<RemoteInput, Notification.Action> pair = entry.getSbn()
1127 .getNotification().findRemoteInputActionPair(false);
1128 RemoteInput remoteInput = pair.first;
1129 Notification.Action action = pair.second;
1130 Intent dataIntent = prepareRemoteInputFromData("image/png", uri,
1131 remoteInput, entry);
1132 sendRemoteInput(dataIntent, entry, action.actionIntent);
1133 mBubbleData.setSelectedBubble(bubble);
1134 mBubbleData.setExpanded(true);
1135 }
1136 }
1137 });
1138 }
1139 }, 200);
1140 }
1141
1142 private final BubbleScreenshotListener mBubbleScreenshotListener =
1143 bubble -> sendScreenshotToBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001144}