blob: 8e992fe55fd40520334518f0e9298adbd7a5d5f2 [file] [log] [blame]
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.bubbles;
18
Mady Mellor22f2f072019-04-18 13:26:18 -070019import static android.app.Notification.FLAG_AUTOGROUP_SUMMARY;
Mady Mellor3a0a1b42019-05-23 06:40:21 -070020import static android.app.Notification.FLAG_BUBBLE;
Mady Mellorc2ff0112019-03-28 14:18:06 -070021import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL;
22import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL;
23import static android.service.notification.NotificationListenerService.REASON_CANCEL;
24import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -070025import static android.service.notification.NotificationListenerService.REASON_CLICK;
26import static android.service.notification.NotificationListenerService.REASON_GROUP_SUMMARY_CANCELED;
Mady Mellor390bff42019-04-05 15:09:01 -070027import static android.view.Display.DEFAULT_DISPLAY;
28import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080029import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080030import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080031import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080032
Issei Suzukia8d07312019-06-07 12:56:19 +020033import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_CONTROLLER;
Mady Mellorff076eb2019-11-13 10:12:06 -080034import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_EXPERIMENTS;
Issei Suzukia8d07312019-06-07 12:56:19 +020035import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
36import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080037import static com.android.systemui.statusbar.StatusBarState.SHADE;
Mady Mellor1a4e86f2019-05-03 16:07:23 -070038import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080039
Mark Renoufba5ab512019-05-02 15:21:01 -040040import static java.lang.annotation.ElementType.FIELD;
41import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
42import static java.lang.annotation.ElementType.PARAMETER;
Mark Renouf08bc42a2019-03-07 13:01:59 -050043import static java.lang.annotation.RetentionPolicy.SOURCE;
44
Mark Renoufc19b4732019-06-26 12:08:33 -040045import android.annotation.UserIdInt;
Mark Renoufc808f062019-02-07 15:20:37 -050046import android.app.ActivityManager.RunningTaskInfo;
Aran Inkaa4dfa72019-11-18 16:49:07 -050047import android.app.Notification;
Mady Mellor66efd5e2019-05-15 13:38:11 -070048import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070049import android.app.PendingIntent;
Aran Inkaa4dfa72019-11-18 16:49:07 -050050import android.app.RemoteInput;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080051import android.content.Context;
Aran Inkaa4dfa72019-11-18 16:49:07 -050052import android.content.Intent;
Mady Mellorca0c24c2019-05-16 16:14:32 -070053import android.content.pm.ActivityInfo;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080054import android.content.pm.PackageManager;
Aran Inkaa4dfa72019-11-18 16:49:07 -050055import android.content.pm.ShortcutManager;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040056import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080057import android.graphics.Rect;
Aran Inkaa4dfa72019-11-18 16:49:07 -050058import android.net.Uri;
59import android.os.Handler;
Mark Renoufcecc77b2019-01-30 16:32:24 -050060import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080061import android.os.ServiceManager;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040062import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040063import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040064import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040065import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040066import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040067import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050068import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080069import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080070import android.widget.FrameLayout;
71
Mark Renouf08bc42a2019-03-07 13:01:59 -050072import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050073import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040074import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050075
Mady Mellorebdbbb92018-11-15 14:36:48 -080076import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070077import com.android.internal.statusbar.IStatusBarService;
Aran Inkaa4dfa72019-11-18 16:49:07 -050078import com.android.internal.util.ScreenshotHelper;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080079import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050080import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050081import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000082import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050083import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050084import com.android.systemui.shared.system.WindowManagerWrapper;
Mark Renoufc19b4732019-06-26 12:08:33 -040085import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070086import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050087import com.android.systemui.statusbar.notification.NotificationEntryListener;
88import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080089import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050090import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellor22f2f072019-04-18 13:26:18 -070091import com.android.systemui.statusbar.phone.NotificationGroupManager;
Mady Mellor7f234902019-10-20 12:06:29 -070092import com.android.systemui.statusbar.phone.ShadeController;
Mady Mellorf3b9fab2019-11-13 17:27:32 -080093import com.android.systemui.statusbar.phone.StatusBar;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080094import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070095import com.android.systemui.statusbar.policy.ConfigurationController;
Aran Inkaa4dfa72019-11-18 16:49:07 -050096import com.android.systemui.statusbar.policy.RemoteInputUriController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040097import com.android.systemui.statusbar.policy.ZenModeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080098
Mady Mellor70cba7bb2019-07-02 15:06:07 -070099import java.io.FileDescriptor;
100import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500101import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -0400102import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -0700103import java.util.ArrayList;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500104import java.util.HashMap;
Mady Mellorff076eb2019-11-13 10:12:06 -0800105import java.util.HashSet;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500106import java.util.function.Consumer;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500107
Jason Monk27d01a622018-12-10 15:57:09 -0500108import javax.inject.Inject;
109import javax.inject.Singleton;
110
Mady Mellor7f234902019-10-20 12:06:29 -0700111import dagger.Lazy;
112
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800113/**
114 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
115 * Bubbles can be expanded to show more content.
116 *
117 * The controller manages addition, removal, and visible state of bubbles on screen.
118 */
Jason Monk27d01a622018-12-10 15:57:09 -0500119@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -0400120public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800121
Issei Suzukia8d07312019-06-07 12:56:19 +0200122 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800123
Mark Renouf08bc42a2019-03-07 13:01:59 -0500124 @Retention(SOURCE)
125 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400126 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700127 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400128 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500129 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700130
Mark Renouf08bc42a2019-03-07 13:01:59 -0500131 static final int DISMISS_USER_GESTURE = 1;
132 static final int DISMISS_AGED = 2;
133 static final int DISMISS_TASK_FINISHED = 3;
134 static final int DISMISS_BLOCKED = 4;
135 static final int DISMISS_NOTIF_CANCEL = 5;
136 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700137 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400138 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700139 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700140 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500141
Ned Burns01e38212019-01-03 16:32:52 -0500142 private final Context mContext;
143 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500144 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800145 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800146 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100147 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700148 private final NotificationGroupManager mNotificationGroupManager;
Mady Mellor7f234902019-10-20 12:06:29 -0700149 private final Lazy<ShadeController> mShadeController;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500150 private final RemoteInputUriController mRemoteInputUriController;
151 private Handler mHandler = new Handler() {};
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800152
Mady Mellor3dff9e62019-02-05 18:12:53 -0800153 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400154 @Nullable private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800155
Mark Renoufc19b4732019-06-26 12:08:33 -0400156 // Tracks the id of the current (foreground) user.
157 private int mCurrentUserId;
158 // Saves notification keys of active bubbles when users are switched.
159 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
160
Mady Mellorff076eb2019-11-13 10:12:06 -0800161 // Saves notification keys of user created "fake" bubbles so that we can allow notifications
162 // like these to bubble by default. Doesn't persist across reboots, not a long-term solution.
163 private final HashSet<String> mUserCreatedBubbles;
164
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800165 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500166 private final StatusBarWindowController mStatusBarWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400167 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800168 private StatusBarStateListener mStatusBarStateListener;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500169 private final ScreenshotHelper mScreenshotHelper;
170
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800171
Mady Melloraa8fef22019-04-11 13:36:40 -0700172 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700173 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800174
Mady Mellord1c78b262018-11-06 18:04:40 -0800175 // Used for determining view rect for touch interaction
176 private Rect mTempRect = new Rect();
177
Mark Renoufc19b4732019-06-26 12:08:33 -0400178 // Listens to user switch so bubbles can be saved and restored.
179 private final NotificationLockscreenUserManager mNotifUserManager;
180
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400181 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
182 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
183
Mady Mellor5549dd22018-11-06 18:07:34 -0800184 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800185 * Listener to be notified when some states of the bubbles change.
186 */
187 public interface BubbleStateChangeListener {
188 /**
189 * Called when the stack has bubbles or no longer has bubbles.
190 */
191 void onHasBubblesChanged(boolean hasBubbles);
192 }
193
Mady Mellorcd9b1302018-11-06 18:08:04 -0800194 /**
195 * Listener to find out about stack expansion / collapse events.
196 */
197 public interface BubbleExpandListener {
198 /**
199 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700200 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800201 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800202 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800203 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800204 void onBubbleExpandChanged(boolean isExpanding, String key);
205 }
206
207 /**
Aran Inkaa4dfa72019-11-18 16:49:07 -0500208 * Listener for handling bubble screenshot events.
209 */
210 public interface BubbleScreenshotListener {
211 /**
212 * Called to trigger taking a screenshot and sending the result to a bubble.
213 */
214 void onBubbleScreenshot(Bubble bubble);
215 }
216
217 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800218 * Listens for the current state of the status bar and updates the visibility state
219 * of bubbles as needed.
220 */
221 private class StatusBarStateListener implements StatusBarStateController.StateListener {
222 private int mState;
223 /**
224 * Returns the current status bar state.
225 */
226 public int getCurrentState() {
227 return mState;
228 }
229
230 @Override
231 public void onStateChanged(int newState) {
232 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400233 boolean shouldCollapse = (mState != SHADE);
234 if (shouldCollapse) {
235 collapseStack();
236 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700237 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800238 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800239 }
240
Jason Monk27d01a622018-12-10 15:57:09 -0500241 @Inject
Mady Mellor7f234902019-10-20 12:06:29 -0700242 public BubbleController(Context context,
243 StatusBarWindowController statusBarWindowController,
244 StatusBarStateController statusBarStateController,
245 Lazy<ShadeController> shadeController,
246 BubbleData data,
Mady Melloraa8fef22019-04-11 13:36:40 -0700247 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400248 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400249 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700250 NotificationLockscreenUserManager notifUserManager,
Mady Mellor7f234902019-10-20 12:06:29 -0700251 NotificationGroupManager groupManager,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500252 NotificationEntryManager entryManager,
253 RemoteInputUriController remoteInputUriController) {
Mady Mellor7f234902019-10-20 12:06:29 -0700254 this(context, statusBarWindowController, statusBarStateController, shadeController,
255 data, null /* synchronizer */, configurationController, interruptionStateProvider,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500256 zenModeController, notifUserManager, groupManager, entryManager,
257 remoteInputUriController);
Mady Mellor7f234902019-10-20 12:06:29 -0700258 }
259
260 public BubbleController(Context context,
261 StatusBarWindowController statusBarWindowController,
262 StatusBarStateController statusBarStateController,
263 Lazy<ShadeController> shadeController,
264 BubbleData data,
265 @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
266 ConfigurationController configurationController,
267 NotificationInterruptionStateProvider interruptionStateProvider,
268 ZenModeController zenModeController,
269 NotificationLockscreenUserManager notifUserManager,
270 NotificationGroupManager groupManager,
Aran Inkaa4dfa72019-11-18 16:49:07 -0500271 NotificationEntryManager entryManager,
272 RemoteInputUriController remoteInputUriController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800273 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700274 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400275 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400276 mZenModeController = zenModeController;
Aran Inkaa4dfa72019-11-18 16:49:07 -0500277 mRemoteInputUriController = remoteInputUriController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400278 mZenModeController.addCallback(new ZenModeController.Callback() {
279 @Override
280 public void onZenChanged(int zen) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800281 for (Bubble b : mBubbleData.getBubbles()) {
282 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700283 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400284 }
285
286 @Override
287 public void onConfigChanged(ZenModeConfig config) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800288 for (Bubble b : mBubbleData.getBubbles()) {
289 b.setShowDot(b.showInShade(), true /* animate */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700290 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400291 }
292 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700293
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700294 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800295
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400296 mBubbleData = data;
297 mBubbleData.setListener(mBubbleDataListener);
298
Mady Mellor7f234902019-10-20 12:06:29 -0700299 mNotificationEntryManager = entryManager;
Ned Burns01e38212019-01-03 16:32:52 -0500300 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700301 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellor22f2f072019-04-18 13:26:18 -0700302 mNotificationGroupManager = groupManager;
Mady Mellor740d85d2019-07-09 15:26:47 -0700303 mNotificationGroupManager.addOnGroupChangeListener(
304 new NotificationGroupManager.OnGroupChangeListener() {
305 @Override
306 public void onGroupSuppressionChanged(
307 NotificationGroupManager.NotificationGroup group,
308 boolean suppressed) {
309 // More notifications could be added causing summary to no longer
310 // be suppressed -- in this case need to remove the key.
311 final String groupKey = group.summary != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400312 ? group.summary.getSbn().getGroupKey()
Mady Mellor740d85d2019-07-09 15:26:47 -0700313 : null;
314 if (!suppressed && groupKey != null
315 && mBubbleData.isSummarySuppressed(groupKey)) {
316 mBubbleData.removeSuppressedSummary(groupKey);
317 }
318 }
319 });
Mady Mellorb4991e62019-01-10 15:14:51 -0800320
Mady Mellor7f234902019-10-20 12:06:29 -0700321 mShadeController = shadeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800322 mStatusBarWindowController = statusBarWindowController;
323 mStatusBarStateListener = new StatusBarStateListener();
Mady Mellor7f234902019-10-20 12:06:29 -0700324 statusBarStateController.addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500325
Mark Renoufcecc77b2019-01-30 16:32:24 -0500326 mTaskStackListener = new BubbleTaskStackListener();
327 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800328
Joshua Tsujia19515f2019-02-13 18:02:29 -0500329 try {
330 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
331 } catch (RemoteException e) {
332 e.printStackTrace();
333 }
Issei Suzukic0387542019-03-08 17:31:14 +0100334 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700335
336 mBarService = IStatusBarService.Stub.asInterface(
337 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400338
339 mSavedBubbleKeysPerUser = new SparseSetArray<>();
340 mCurrentUserId = mNotifUserManager.getCurrentUserId();
341 mNotifUserManager.addUserChangedListener(
342 newUserId -> {
343 saveBubbles(mCurrentUserId);
344 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
345 restoreBubbles(newUserId);
346 mCurrentUserId = newUserId;
347 });
Mady Mellorff076eb2019-11-13 10:12:06 -0800348
349 mUserCreatedBubbles = new HashSet<>();
Aran Inkaa4dfa72019-11-18 16:49:07 -0500350
351 mScreenshotHelper = new ScreenshotHelper(context);
Mady Mellor5549dd22018-11-06 18:07:34 -0800352 }
353
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400354 /**
355 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
356 * method initializes the stack view and adds it to the StatusBar just above the scrim.
357 */
358 private void ensureStackViewCreated() {
359 if (mStackView == null) {
360 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
361 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Lyn Hanbde48202019-05-29 19:18:29 -0700362 int bubbleScrimIndex = sbv.indexOfChild(sbv.findViewById(R.id.scrim_for_bubble));
363 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
364 sbv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400365 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
366 if (mExpandListener != null) {
367 mStackView.setExpandListener(mExpandListener);
368 }
Aran Inkaa4dfa72019-11-18 16:49:07 -0500369 if (mBubbleScreenshotListener != null) {
370 mStackView.setBubbleScreenshotListener(mBubbleScreenshotListener);
371 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400372 }
373 }
374
Mark Renoufc19b4732019-06-26 12:08:33 -0400375 /**
376 * Records the notification key for any active bubbles. These are used to restore active
377 * bubbles when the user returns to the foreground.
378 *
379 * @param userId the id of the user
380 */
381 private void saveBubbles(@UserIdInt int userId) {
382 // First clear any existing keys that might be stored.
383 mSavedBubbleKeysPerUser.remove(userId);
384 // Add in all active bubbles for the current user.
385 for (Bubble bubble: mBubbleData.getBubbles()) {
386 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
387 }
388 }
389
390 /**
391 * Promotes existing notifications to Bubbles if they were previously bubbles.
392 *
393 * @param userId the id of the user
394 */
395 private void restoreBubbles(@UserIdInt int userId) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400396 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
397 if (savedBubbleKeys == null) {
398 // There were no bubbles saved for this used.
399 return;
400 }
Evan Laird181de622019-10-24 09:53:02 -0400401 for (NotificationEntry e :
402 mNotificationEntryManager.getActiveNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400403 if (savedBubbleKeys.contains(e.getKey())
Mark Renoufc19b4732019-06-26 12:08:33 -0400404 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
405 && canLaunchInActivityView(mContext, e)) {
406 updateBubble(e, /* suppressFlyout= */ true);
407 }
408 }
409 // Finally, remove the entries for this user now that bubbles are restored.
410 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
411 }
412
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700413 @Override
414 public void onUiModeChanged() {
415 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700416 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700417 }
418 }
419
420 @Override
421 public void onOverlayChanged() {
422 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700423 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700424 }
425 }
426
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400427 @Override
428 public void onConfigChanged(Configuration newConfig) {
429 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400430 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700431 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400432 }
433 }
434
Mady Mellor5549dd22018-11-06 18:07:34 -0800435 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800436 * Set a listener to be notified when some states of the bubbles change.
437 */
438 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
439 mStateChangeListener = listener;
440 }
441
442 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800443 * Set a listener to be notified of bubble expand events.
444 */
445 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100446 mExpandListener = ((isExpanding, key) -> {
447 if (listener != null) {
448 listener.onBubbleExpandChanged(isExpanding, key);
449 }
450 mStatusBarWindowController.setBubbleExpanded(isExpanding);
451 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800452 if (mStackView != null) {
453 mStackView.setExpandListener(mExpandListener);
454 }
455 }
456
457 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800458 * Whether or not there are bubbles present, regardless of them being visible on the
459 * screen (e.g. if on AOD).
460 */
461 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800462 if (mStackView == null) {
463 return false;
464 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400465 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800466 }
467
468 /**
469 * Whether the stack of bubbles is expanded or not.
470 */
471 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400472 return mBubbleData.isExpanded();
473 }
474
475 /**
476 * Tell the stack of bubbles to expand.
477 */
478 public void expandStack() {
479 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800480 }
481
482 /**
483 * Tell the stack of bubbles to collapse.
484 */
485 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400486 mBubbleData.setExpanded(false /* expanded */);
487 }
488
Mady Mellorce23c462019-06-17 17:30:07 -0700489 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700490 * True if either:
491 * (1) There is a bubble associated with the provided key and if its notification is hidden
492 * from the shade.
493 * (2) There is a group summary associated with the provided key that is hidden from the shade
494 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700495 *
Mady Mellore28fe102019-07-09 15:33:32 -0700496 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700497 */
498 public boolean isBubbleNotificationSuppressedFromShade(String key) {
Mady Mellore28fe102019-07-09 15:33:32 -0700499 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorb8aaf972019-11-26 10:28:00 -0800500 && !mBubbleData.getBubbleWithKey(key).showInShade();
Evan Laird181de622019-10-24 09:53:02 -0400501 NotificationEntry entry = mNotificationEntryManager.getActiveNotificationUnfiltered(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400502 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellore28fe102019-07-09 15:33:32 -0700503 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700504 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
505 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700506 }
507
Mark Renouf71a3af62019-04-08 15:02:54 -0400508 void selectBubble(Bubble bubble) {
509 mBubbleData.setSelectedBubble(bubble);
510 }
511
512 @VisibleForTesting
513 void selectBubble(String key) {
514 Bubble bubble = mBubbleData.getBubbleWithKey(key);
515 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800516 }
517
518 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400519 * Request the stack expand if needed, then select the specified Bubble as current.
520 *
521 * @param notificationKey the notification key for the bubble to be selected
522 */
523 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400524 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
525 if (bubble != null) {
526 mBubbleData.setSelectedBubble(bubble);
527 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400528 }
529 }
530
531 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800532 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
533 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500534 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400535 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800536 }
537
538 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500539 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
540 * is forwarded a back key down/up pair.
541 */
542 public void performBackPressIfNeeded() {
543 if (mStackView != null) {
544 mStackView.performBackPressIfNeeded();
545 }
546 }
547
548 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800549 * Adds or updates a bubble associated with the provided notification entry.
550 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400551 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800552 */
Mark Renouff97ed462019-04-05 13:46:24 -0400553 void updateBubble(NotificationEntry notif) {
Mady Mellor7f234902019-10-20 12:06:29 -0700554 updateBubble(notif, false /* suppressFlyout */);
Mark Renoufc19b4732019-06-26 12:08:33 -0400555 }
556
557 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor7f234902019-10-20 12:06:29 -0700558 updateBubble(notif, suppressFlyout, true /* showInShade */);
559 }
560
561 void updateBubble(NotificationEntry notif, boolean suppressFlyout, boolean showInShade) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700562 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400563 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700564 notif.setInterruption();
565 }
Mady Mellor7f234902019-10-20 12:06:29 -0700566 mBubbleData.notificationEntryUpdated(notif, suppressFlyout, showInShade);
567 }
568
569 /**
570 * Called when a user has indicated that an active notification should be shown as a bubble.
571 * <p>
572 * This method will collapse the shade, create the bubble without a flyout or dot, and suppress
573 * the notification from appearing in the shade.
574 *
575 * @param entry the notification to show as a bubble.
576 */
577 public void onUserCreatedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800578 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
579 Log.d(TAG, "onUserCreatedBubble: " + entry.getKey());
580 }
Mady Mellor7f234902019-10-20 12:06:29 -0700581 mShadeController.get().collapsePanel(true);
582 entry.setFlagBubble(true);
583 updateBubble(entry, true /* suppressFlyout */, false /* showInShade */);
Mady Mellorff076eb2019-11-13 10:12:06 -0800584 mUserCreatedBubbles.add(entry.getKey());
Mady Mellor7f234902019-10-20 12:06:29 -0700585 }
586
587 /**
588 * Called when a user has indicated that an active notification appearing as a bubble should
589 * no longer be shown as a bubble.
590 *
591 * @param entry the notification to no longer show as a bubble.
592 */
593 public void onUserDemotedBubbleFromNotification(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800594 if (DEBUG_EXPERIMENTS || DEBUG_BUBBLE_CONTROLLER) {
595 Log.d(TAG, "onUserDemotedBubble: " + entry.getKey());
596 }
Mady Mellor7f234902019-10-20 12:06:29 -0700597 entry.setFlagBubble(false);
598 removeBubble(entry.getKey(), DISMISS_BLOCKED);
Mady Mellorff076eb2019-11-13 10:12:06 -0800599 mUserCreatedBubbles.remove(entry.getKey());
600 }
601
602 /**
603 * Whether this bubble was explicitly created by the user via a SysUI affordance.
604 */
605 boolean isUserCreatedBubble(String key) {
606 return mUserCreatedBubbles.contains(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800607 }
608
609 /**
610 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500611 * <p>
612 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800613 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500614 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500615 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400616 // TEMP: refactor to change this to pass entry
617 Bubble bubble = mBubbleData.getBubbleWithKey(key);
618 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700619 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800620 }
621 }
622
Ned Burns01e38212019-01-03 16:32:52 -0500623 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700624 private final NotificationRemoveInterceptor mRemoveInterceptor =
625 new NotificationRemoveInterceptor() {
626 @Override
627 public boolean onNotificationRemoveRequested(String key, int reason) {
Evan Laird181de622019-10-24 09:53:02 -0400628 NotificationEntry entry =
629 mNotificationEntryManager.getActiveNotificationUnfiltered(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400630 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellor22f2f072019-04-18 13:26:18 -0700631 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
632
633 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
Mady Mellore28fe102019-07-09 15:33:32 -0700634 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
635 && mBubbleData.getSummaryKey(groupKey).equals(key));
Mady Mellor22f2f072019-04-18 13:26:18 -0700636 boolean isSummary = entry != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400637 && entry.getSbn().getNotification().isGroupSummary();
Mady Mellore28fe102019-07-09 15:33:32 -0700638 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
639 && bubbleChildren != null && !bubbleChildren.isEmpty();
Mady Mellor22f2f072019-04-18 13:26:18 -0700640
641 if (!inBubbleData && !isSummaryOfBubbles) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700642 return false;
643 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700644
645 final boolean isClearAll = reason == REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700646 final boolean isUserDimiss = reason == REASON_CANCEL || reason == REASON_CLICK;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700647 final boolean isAppCancel = reason == REASON_APP_CANCEL
648 || reason == REASON_APP_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700649 final boolean isSummaryCancel = reason == REASON_GROUP_SUMMARY_CANCELED;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700650
651 // Need to check for !appCancel here because the notification may have
652 // previously been dismissed & entry.isRowDismissed would still be true
Mady Mellorca184aae2019-09-17 16:07:12 -0700653 boolean userRemovedNotif = (entry != null && entry.isRowDismissed() && !isAppCancel)
Mady Mellor22f2f072019-04-18 13:26:18 -0700654 || isClearAll || isUserDimiss || isSummaryCancel;
655
656 if (isSummaryOfBubbles) {
657 return handleSummaryRemovalInterception(entry, userRemovedNotif);
658 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700659
660 // The bubble notification sticks around in the data as long as the bubble is
661 // not dismissed and the app hasn't cancelled the notification.
Mady Mellor22f2f072019-04-18 13:26:18 -0700662 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellorca184aae2019-09-17 16:07:12 -0700663 boolean bubbleExtended = entry != null && entry.isBubble() && userRemovedNotif;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700664 if (bubbleExtended) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800665 bubble.setShowInShade(false);
666 bubble.setShowDot(false /* show */, true /* animate */);
Beverly85d4c192019-09-30 11:40:39 -0400667 mNotificationEntryManager.updateNotifications(
668 "BubbleController.onNotificationRemoveRequested");
Mady Mellorc2ff0112019-03-28 14:18:06 -0700669 return true;
Mady Mellorff076eb2019-11-13 10:12:06 -0800670 } else if (!userRemovedNotif && entry != null
671 && !isUserCreatedBubble(bubble.getKey())) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700672 // This wasn't a user removal so we should remove the bubble as well
673 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
674 return false;
675 }
676 return false;
677 }
678 };
679
Mady Mellor22f2f072019-04-18 13:26:18 -0700680 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
681 boolean userRemovedNotif) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400682 String groupKey = summary.getSbn().getGroupKey();
Mady Mellor22f2f072019-04-18 13:26:18 -0700683 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
684
685 if (userRemovedNotif) {
686 // If it's a user dismiss we mark the children to be hidden from the shade.
687 for (int i = 0; i < bubbleChildren.size(); i++) {
688 Bubble bubbleChild = bubbleChildren.get(i);
689 // As far as group manager is concerned, once a child is no longer shown
690 // in the shade, it is essentially removed.
691 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
Mady Mellorb8aaf972019-11-26 10:28:00 -0800692 bubbleChild.setShowInShade(false);
693 bubbleChild.setShowDot(false /* show */, true /* animate */);
Mady Mellor22f2f072019-04-18 13:26:18 -0700694 }
695 // And since all children are removed, remove the summary.
696 mNotificationGroupManager.onEntryRemoved(summary);
697
698 // If the summary was auto-generated we don't need to keep that notification around
699 // because apps can't cancel it; so we only intercept & suppress real summaries.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400700 boolean isAutogroupSummary = (summary.getSbn().getNotification().flags
Mady Mellor22f2f072019-04-18 13:26:18 -0700701 & FLAG_AUTOGROUP_SUMMARY) != 0;
Mady Mellore28fe102019-07-09 15:33:32 -0700702 if (!isAutogroupSummary) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400703 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
704 summary.getKey());
Mady Mellore28fe102019-07-09 15:33:32 -0700705 // Tell shade to update for the suppression
Beverly85d4c192019-09-30 11:40:39 -0400706 mNotificationEntryManager.updateNotifications(
707 "BubbleController.handleSummaryRemovalInterception");
Mady Mellore28fe102019-07-09 15:33:32 -0700708 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700709 return !isAutogroupSummary;
710 } else {
Mady Mellore28fe102019-07-09 15:33:32 -0700711 // If it's not a user dismiss it's a cancel.
712 mBubbleData.removeSuppressedSummary(groupKey);
713
Mady Mellor22f2f072019-04-18 13:26:18 -0700714 // Remove any associated bubble children.
715 for (int i = 0; i < bubbleChildren.size(); i++) {
716 Bubble bubbleChild = bubbleChildren.get(i);
717 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
718 DISMISS_GROUP_CANCELLED);
719 }
720 return false;
721 }
722 }
723
Mady Mellorc2ff0112019-03-28 14:18:06 -0700724 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500725 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
726 @Override
Mady Mellor58dc5192019-12-16 13:49:56 -0800727 public void onNotificationAdded(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800728 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
Mady Mellor30672942019-12-04 15:43:19 -0800729 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
730 mContext, entry, previouslyUserCreated);
Mady Mellor7f234902019-10-20 12:06:29 -0700731
Mady Mellorca0c24c2019-05-16 16:14:32 -0700732 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
Mady Mellor30672942019-12-04 15:43:19 -0800733 && (canLaunchInActivityView(mContext, entry) || wasAdjusted)) {
Mady Mellorea13b232019-12-05 15:55:46 -0800734 if (wasAdjusted && !previouslyUserCreated) {
735 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
736 mUserCreatedBubbles.add(entry.getKey());
737 }
Mark Renouff97ed462019-04-05 13:46:24 -0400738 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800739 }
740 }
741
742 @Override
743 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800744 boolean previouslyUserCreated = mUserCreatedBubbles.contains(entry.getKey());
Mady Mellor30672942019-12-04 15:43:19 -0800745 boolean wasAdjusted = BubbleExperimentConfig.adjustForExperiments(
746 mContext, entry, previouslyUserCreated);
Mady Mellor7f234902019-10-20 12:06:29 -0700747
Mady Mellorca0c24c2019-05-16 16:14:32 -0700748 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
Mady Mellor30672942019-12-04 15:43:19 -0800749 && (canLaunchInActivityView(mContext, entry) || wasAdjusted);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400750 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
Mady Melloraa8fef22019-04-11 13:36:40 -0700751 // It was previously a bubble but no longer a bubble -- lets remove it
Ned Burns00b4b2d2019-10-17 22:09:27 -0400752 removeBubble(entry.getKey(), DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700753 } else if (shouldBubble) {
Mady Mellorea13b232019-12-05 15:55:46 -0800754 if (wasAdjusted && !previouslyUserCreated) {
755 // Gotta treat the auto-bubbled / whitelisted packaged bubbles as usercreated
756 mUserCreatedBubbles.add(entry.getKey());
757 }
Mark Renouff97ed462019-04-05 13:46:24 -0400758 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800759 }
760 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400761
762 @Override
763 public void onNotificationRankingUpdated(RankingMap rankingMap) {
764 // Forward to BubbleData to block any bubbles which should no longer be shown
765 mBubbleData.notificationRankingUpdated(rankingMap);
766 }
Ned Burns01e38212019-01-03 16:32:52 -0500767 };
768
Mark Renouf71a3af62019-04-08 15:02:54 -0400769 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400770 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400771
Mark Renouf3bc5b362019-04-05 14:37:59 -0400772 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400773 public void applyUpdate(BubbleData.Update update) {
774 if (mStackView == null && update.addedBubble != null) {
775 // Lazy init stack view when the first bubble is added.
776 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400777 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400778
779 // If not yet initialized, ignore all other changes.
780 if (mStackView == null) {
781 return;
782 }
783
784 if (update.addedBubble != null) {
785 mStackView.addBubble(update.addedBubble);
786 }
787
788 // Collapsing? Do this first before remaining steps.
789 if (update.expandedChanged && !update.expanded) {
790 mStackView.setExpanded(false);
791 }
792
793 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700794 ArrayList<Pair<Bubble, Integer>> removedBubbles =
795 new ArrayList<>(update.removedBubbles);
796 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400797 final Bubble bubble = removed.first;
798 @DismissReason final int reason = removed.second;
799 mStackView.removeBubble(bubble);
800
Mark Renoufc19b4732019-06-26 12:08:33 -0400801 // If the bubble is removed for user switching, leave the notification in place.
802 if (reason != DISMISS_USER_CHANGED) {
803 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
Mady Mellorb8aaf972019-11-26 10:28:00 -0800804 && !bubble.showInShade()) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400805 // The bubble is gone & the notification is gone, time to actually remove it
806 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400807 bubble.getEntry().getSbn(), UNDEFINED_DISMISS_REASON);
Mark Renoufc19b4732019-06-26 12:08:33 -0400808 } else {
809 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400810 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700811
Mark Renoufc19b4732019-06-26 12:08:33 -0400812 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
813 // will get right result back
814 try {
815 mBarService.onNotificationBubbleChanged(bubble.getKey(),
816 false /* isBubble */);
817 } catch (RemoteException e) {
818 // Bad things have happened
819 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400820 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700821
Mady Mellore28fe102019-07-09 15:33:32 -0700822 // Check if removed bubble has an associated suppressed group summary that needs
823 // to be removed now.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400824 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700825 if (mBubbleData.isSummarySuppressed(groupKey)
826 && mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
827 // Time to actually remove the summary.
828 String notifKey = mBubbleData.getSummaryKey(groupKey);
829 mBubbleData.removeSuppressedSummary(groupKey);
830 NotificationEntry entry =
Evan Laird181de622019-10-24 09:53:02 -0400831 mNotificationEntryManager.getActiveNotificationUnfiltered(notifKey);
Mady Mellore28fe102019-07-09 15:33:32 -0700832 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400833 entry.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellore28fe102019-07-09 15:33:32 -0700834 }
835
Mady Mellor22f2f072019-04-18 13:26:18 -0700836 // Check if summary should be removed from NoManGroup
837 NotificationEntry summary = mNotificationGroupManager.getLogicalGroupSummary(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400838 bubble.getEntry().getSbn());
Mady Mellor22f2f072019-04-18 13:26:18 -0700839 if (summary != null) {
840 ArrayList<NotificationEntry> summaryChildren =
Ned Burns00b4b2d2019-10-17 22:09:27 -0400841 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
842 boolean isSummaryThisNotif = summary.getKey().equals(
843 bubble.getEntry().getKey());
Mady Mellore4348272019-07-29 17:43:36 -0700844 if (!isSummaryThisNotif
845 && (summaryChildren == null || summaryChildren.isEmpty())) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700846 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400847 summary.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellor22f2f072019-04-18 13:26:18 -0700848 }
849 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700850 }
851 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400852
Mark Renouf82a40e62019-05-23 16:16:24 -0400853 if (update.updatedBubble != null) {
854 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400855 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400856
Mark Renouf82a40e62019-05-23 16:16:24 -0400857 if (update.orderChanged) {
858 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400859 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400860
Mark Renouf82a40e62019-05-23 16:16:24 -0400861 if (update.selectionChanged) {
862 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700863 if (update.selectedBubble != null) {
864 mNotificationGroupManager.updateSuppression(
865 update.selectedBubble.getEntry());
866 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400867 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400868
Mark Renouf82a40e62019-05-23 16:16:24 -0400869 // Expanding? Apply this last.
870 if (update.expandedChanged && update.expanded) {
871 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400872 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400873
Beverly85d4c192019-09-30 11:40:39 -0400874 mNotificationEntryManager.updateNotifications(
875 "BubbleData.Listener.applyUpdate");
Lyn Han6c40fe72019-05-08 14:06:33 -0700876 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400877
Issei Suzukia8d07312019-06-07 12:56:19 +0200878 if (DEBUG_BUBBLE_CONTROLLER) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400879 Log.d(TAG, "[BubbleData]");
Lyn Han767d70e2019-12-10 18:02:23 -0800880 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mBubbleData.getBubbles(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400881 mBubbleData.getSelectedBubble()));
882
883 if (mStackView != null) {
884 Log.d(TAG, "[BubbleStackView]");
Lyn Han767d70e2019-12-10 18:02:23 -0800885 Log.d(TAG, BubbleDebugConfig.formatBubblesString(mStackView.getBubblesOnScreen(),
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400886 mStackView.getExpandedBubble()));
887 }
888 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400889 }
890 };
891
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800892 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400893 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700894 * Updates the visibility of the bubbles based on current state.
895 * Does not un-bubble, just hides or un-hides. Notifies any
896 * {@link BubbleStateChangeListener}s of visibility changes.
897 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800898 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700899 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800900 if (mStackView == null) {
901 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800902 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800903 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
904 // Bubbles only appear in unlocked shade
905 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +0000906 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800907 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800908 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700909
Mady Mellor698d9e82019-08-01 23:11:53 +0000910 // Let listeners know if bubble state changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700911 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +0000912 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellor88552b82019-08-05 22:38:59 +0000913 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -0700914 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
915 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
916 }
917
918 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800919 }
920
921 /**
922 * Rect indicating the touchable region for the bubble stack / expanded stack.
923 */
924 public Rect getTouchableRegion() {
925 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
926 return null;
927 }
928 mStackView.getBoundsOnScreen(mTempRect);
929 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800930 }
931
Mady Mellor390bff42019-04-05 15:09:01 -0700932 /**
933 * The display id of the expanded view, if the stack is expanded and not occluded by the
934 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
935 */
936 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200937 final Bubble bubble = getExpandedBubble(context);
938 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
939 }
940
941 @Nullable
942 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700943 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200944 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700945 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200946 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700947 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200948 final Bubble expandedBubble = mStackView.getExpandedBubble();
949 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700950 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200951 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700952 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200953 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700954 }
955
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800956 @VisibleForTesting
957 BubbleStackView getStackView() {
958 return mStackView;
959 }
960
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700961 /**
962 * Description of current bubble state.
963 */
964 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
965 pw.println("BubbleController state:");
966 mBubbleData.dump(fd, pw, args);
967 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -0400968 if (mStackView != null) {
969 mStackView.dump(fd, pw, args);
970 }
971 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700972 }
973
Mady Mellore80930e2019-03-21 16:00:45 -0700974 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -0500975 * This task stack listener is responsible for responding to tasks moved to the front
976 * which are on the default (main) display. When this happens, expanded bubbles must be
977 * collapsed so the user may interact with the app which was just moved to the front.
978 * <p>
979 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
980 * these calls via a main thread Handler.
981 */
982 @MainThread
983 private class BubbleTaskStackListener extends TaskStackChangeListener {
984
Mark Renoufcecc77b2019-01-30 16:32:24 -0500985 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500986 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
987 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -0700988 if (!mStackView.isExpansionAnimating()) {
989 mBubbleData.setExpanded(false);
990 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500991 }
992 }
993
Mark Renoufcecc77b2019-01-30 16:32:24 -0500994 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500995 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500996 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400997 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500998 }
999 }
Mark Renouf446251d2019-04-26 10:22:41 -04001000
1001 @Override
1002 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
1003 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
1004 mBubbleData.setExpanded(false);
1005 }
1006 }
Issei Suzukicac2a502019-04-16 16:52:50 +02001007
1008 @Override
1009 public void onSingleTaskDisplayDrawn(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001010 final Bubble expandedBubble = mStackView != null
1011 ? mStackView.getExpandedBubble()
1012 : null;
Issei Suzukicac2a502019-04-16 16:52:50 +02001013 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
1014 expandedBubble.setContentVisibility(true);
1015 }
1016 }
Issei Suzuki734bc942019-06-05 13:59:52 +02001017
1018 @Override
1019 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -07001020 final Bubble expandedBubble = mStackView != null
1021 ? mStackView.getExpandedBubble()
1022 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -07001023 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
1024 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +02001025 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +02001026 }
Mady Mellorca184aae2019-09-17 16:07:12 -07001027 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +02001028 }
Mark Renoufcecc77b2019-01-30 16:32:24 -05001029 }
1030
Mady Mellorca0c24c2019-05-16 16:14:32 -07001031 /**
1032 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
1033 *
1034 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
1035 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
1036 *
1037 * @param context the context to use.
1038 * @param entry the entry to bubble.
1039 */
1040 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
1041 PendingIntent intent = entry.getBubbleMetadata() != null
1042 ? entry.getBubbleMetadata().getIntent()
1043 : null;
1044 if (intent == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001045 Log.w(TAG, "Unable to create bubble -- no intent: " + entry.getKey());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001046 return false;
1047 }
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001048 PackageManager packageManager = StatusBar.getPackageManagerForUser(
1049 context, entry.getSbn().getUser().getIdentifier());
Mady Mellorca0c24c2019-05-16 16:14:32 -07001050 ActivityInfo info =
Mady Mellorf3b9fab2019-11-13 17:27:32 -08001051 intent.getIntent().resolveActivityInfo(packageManager, 0);
Mady Mellorca0c24c2019-05-16 16:14:32 -07001052 if (info == null) {
Mady Mellor7f234902019-10-20 12:06:29 -07001053 Log.w(TAG, "Unable to send as bubble, "
1054 + entry.getKey() + " couldn't find activity info for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001055 + intent);
1056 return false;
1057 }
1058 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
Mady Mellor7f234902019-10-20 12:06:29 -07001059 Log.w(TAG, "Unable to send as bubble, "
1060 + entry.getKey() + " activity is not resizable for intent: "
Mady Mellorca0c24c2019-05-16 16:14:32 -07001061 + intent);
1062 return false;
1063 }
Mady Mellorca0c24c2019-05-16 16:14:32 -07001064 return true;
1065 }
1066
Joshua Tsujia19515f2019-02-13 18:02:29 -05001067 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +00001068 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -05001069 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -05001070 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
1071 if (mStackView != null && mStackView.getBubbleCount() > 0) {
1072 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -05001073 }
1074 }
Joshua Tsujia19515f2019-02-13 18:02:29 -05001075 }
Aran Inkaa4dfa72019-11-18 16:49:07 -05001076
1077 // TODO: Copied from RemoteInputView. Consolidate RemoteInput intent logic.
1078 private Intent prepareRemoteInputFromData(String contentType, Uri data,
1079 RemoteInput remoteInput, NotificationEntry entry) {
1080 HashMap<String, Uri> results = new HashMap<>();
1081 results.put(contentType, data);
1082 mRemoteInputUriController.grantInlineReplyUriPermission(entry.getSbn(), data);
1083 Intent fillInIntent = new Intent().addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
1084 RemoteInput.addDataResultToIntent(remoteInput, fillInIntent, results);
1085
1086 return fillInIntent;
1087 }
1088
1089 // TODO: Copied from RemoteInputView. Consolidate RemoteInput intent logic.
1090 private void sendRemoteInput(Intent intent, NotificationEntry entry,
1091 PendingIntent pendingIntent) {
1092 // Tell ShortcutManager that this package has been "activated". ShortcutManager
1093 // will reset the throttling for this package.
1094 // Strictly speaking, the intent receiver may be different from the notification publisher,
1095 // but that's an edge case, and also because we can't always know which package will receive
1096 // an intent, so we just reset for the publisher.
1097 mContext.getSystemService(ShortcutManager.class).onApplicationActive(
1098 entry.getSbn().getPackageName(),
1099 entry.getSbn().getUser().getIdentifier());
1100
1101 try {
1102 pendingIntent.send(mContext, 0, intent);
1103 } catch (PendingIntent.CanceledException e) {
1104 Log.i(TAG, "Unable to send remote input result", e);
1105 }
1106 }
1107
1108 private void sendScreenshotToBubble(Bubble bubble) {
1109 // delay allows the bubble menu to disappear before the screenshot
1110 // done here because we already have a Handler to delay with.
1111 // TODO: Hide bubble + menu UI from screenshots entirely instead of just delaying.
1112 mHandler.postDelayed(new Runnable() {
1113 @Override
1114 public void run() {
1115 mScreenshotHelper.takeScreenshot(
1116 android.view.WindowManager.TAKE_SCREENSHOT_FULLSCREEN,
1117 true /* hasStatus */,
1118 true /* hasNav */,
1119 mHandler,
1120 new Consumer<Uri>() {
1121 @Override
1122 public void accept(Uri uri) {
1123 if (uri != null) {
1124 NotificationEntry entry = bubble.getEntry();
1125 Pair<RemoteInput, Notification.Action> pair = entry.getSbn()
1126 .getNotification().findRemoteInputActionPair(false);
1127 RemoteInput remoteInput = pair.first;
1128 Notification.Action action = pair.second;
1129 Intent dataIntent = prepareRemoteInputFromData("image/png", uri,
1130 remoteInput, entry);
1131 sendRemoteInput(dataIntent, entry, action.actionIntent);
1132 mBubbleData.setSelectedBubble(bubble);
1133 mBubbleData.setExpanded(true);
1134 }
1135 }
1136 });
1137 }
1138 }, 200);
1139 }
1140
1141 private final BubbleScreenshotListener mBubbleScreenshotListener =
1142 bubble -> sendScreenshotToBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001143}