blob: 0231b562ad041bf5a8016be1e217245f0d585b18 [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;
34import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
35import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080036import static com.android.systemui.statusbar.StatusBarState.SHADE;
Mady Mellor1a4e86f2019-05-03 16:07:23 -070037import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080038
Mark Renoufba5ab512019-05-02 15:21:01 -040039import static java.lang.annotation.ElementType.FIELD;
40import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
41import static java.lang.annotation.ElementType.PARAMETER;
Mark Renouf08bc42a2019-03-07 13:01:59 -050042import static java.lang.annotation.RetentionPolicy.SOURCE;
43
Mark Renoufc19b4732019-06-26 12:08:33 -040044import android.annotation.UserIdInt;
Mark Renoufc808f062019-02-07 15:20:37 -050045import android.app.ActivityManager.RunningTaskInfo;
Mady Mellor66efd5e2019-05-15 13:38:11 -070046import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070047import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080048import android.content.Context;
Mady Mellorca0c24c2019-05-16 16:14:32 -070049import android.content.pm.ActivityInfo;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040050import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080051import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050052import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080053import android.os.ServiceManager;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040054import android.service.notification.NotificationListenerService.RankingMap;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040055import android.service.notification.ZenModeConfig;
Mark Renoufc19b4732019-06-26 12:08:33 -040056import android.util.ArraySet;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040057import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040058import android.util.Pair;
Mark Renoufc19b4732019-06-26 12:08:33 -040059import android.util.SparseSetArray;
Mark Renoufcecc77b2019-01-30 16:32:24 -050060import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080061import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080062import android.widget.FrameLayout;
63
Mark Renouf08bc42a2019-03-07 13:01:59 -050064import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050065import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040066import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050067
Mady Mellorebdbbb92018-11-15 14:36:48 -080068import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070069import com.android.internal.statusbar.IStatusBarService;
Ned Burns01e38212019-01-03 16:32:52 -050070import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080071import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050072import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050073import com.android.systemui.shared.system.ActivityManagerWrapper;
Hongwei Wang43a752b2019-09-17 20:20:30 +000074import com.android.systemui.shared.system.PinnedStackListenerForwarder;
Mark Renoufcecc77b2019-01-30 16:32:24 -050075import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050076import com.android.systemui.shared.system.WindowManagerWrapper;
Mark Renoufc19b4732019-06-26 12:08:33 -040077import com.android.systemui.statusbar.NotificationLockscreenUserManager;
Mady Mellorc2ff0112019-03-28 14:18:06 -070078import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050079import com.android.systemui.statusbar.notification.NotificationEntryListener;
80import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080081import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -040082import com.android.systemui.statusbar.notification.collection.NotificationData;
Ned Burnsf81c4c42019-01-07 14:10:43 -050083import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellor22f2f072019-04-18 13:26:18 -070084import com.android.systemui.statusbar.phone.NotificationGroupManager;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080085import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070086import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040087import com.android.systemui.statusbar.policy.ZenModeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080088
Mady Mellor70cba7bb2019-07-02 15:06:07 -070089import java.io.FileDescriptor;
90import java.io.PrintWriter;
Mark Renouf08bc42a2019-03-07 13:01:59 -050091import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -040092import java.lang.annotation.Target;
Mady Mellor22f2f072019-04-18 13:26:18 -070093import java.util.ArrayList;
Mady Mellore80930e2019-03-21 16:00:45 -070094import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050095
Jason Monk27d01a622018-12-10 15:57:09 -050096import javax.inject.Inject;
97import javax.inject.Singleton;
98
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080099/**
100 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
101 * Bubbles can be expanded to show more content.
102 *
103 * The controller manages addition, removal, and visible state of bubbles on screen.
104 */
Jason Monk27d01a622018-12-10 15:57:09 -0500105@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -0400106public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800107
Issei Suzukia8d07312019-06-07 12:56:19 +0200108 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800109
Mark Renouf08bc42a2019-03-07 13:01:59 -0500110 @Retention(SOURCE)
111 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mark Renoufc19b4732019-06-26 12:08:33 -0400112 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE,
Mady Mellor8454ddf2019-08-15 11:16:23 -0700113 DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT})
Mark Renoufba5ab512019-05-02 15:21:01 -0400114 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500115 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700116
Mark Renouf08bc42a2019-03-07 13:01:59 -0500117 static final int DISMISS_USER_GESTURE = 1;
118 static final int DISMISS_AGED = 2;
119 static final int DISMISS_TASK_FINISHED = 3;
120 static final int DISMISS_BLOCKED = 4;
121 static final int DISMISS_NOTIF_CANCEL = 5;
122 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700123 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renoufc19b4732019-06-26 12:08:33 -0400124 static final int DISMISS_USER_CHANGED = 8;
Mady Mellor22f2f072019-04-18 13:26:18 -0700125 static final int DISMISS_GROUP_CANCELLED = 9;
Mady Mellor8454ddf2019-08-15 11:16:23 -0700126 static final int DISMISS_INVALID_INTENT = 10;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500127
Ned Burns01e38212019-01-03 16:32:52 -0500128 private final Context mContext;
129 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500130 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800131 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800132 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100133 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellor22f2f072019-04-18 13:26:18 -0700134 private final NotificationGroupManager mNotificationGroupManager;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800135
Mady Mellor3dff9e62019-02-05 18:12:53 -0800136 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400137 @Nullable private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800138
Mark Renoufc19b4732019-06-26 12:08:33 -0400139 // Tracks the id of the current (foreground) user.
140 private int mCurrentUserId;
141 // Saves notification keys of active bubbles when users are switched.
142 private final SparseSetArray<String> mSavedBubbleKeysPerUser;
143
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800144 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500145 private final StatusBarWindowController mStatusBarWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400146 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800147 private StatusBarStateListener mStatusBarStateListener;
148
Mady Melloraa8fef22019-04-11 13:36:40 -0700149 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700150 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800151
Mady Mellord1c78b262018-11-06 18:04:40 -0800152 // Used for determining view rect for touch interaction
153 private Rect mTempRect = new Rect();
154
Mark Renoufc19b4732019-06-26 12:08:33 -0400155 // Listens to user switch so bubbles can be saved and restored.
156 private final NotificationLockscreenUserManager mNotifUserManager;
157
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400158 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
159 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
160
Mady Mellor5549dd22018-11-06 18:07:34 -0800161 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800162 * Listener to be notified when some states of the bubbles change.
163 */
164 public interface BubbleStateChangeListener {
165 /**
166 * Called when the stack has bubbles or no longer has bubbles.
167 */
168 void onHasBubblesChanged(boolean hasBubbles);
169 }
170
Mady Mellorcd9b1302018-11-06 18:08:04 -0800171 /**
172 * Listener to find out about stack expansion / collapse events.
173 */
174 public interface BubbleExpandListener {
175 /**
176 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700177 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800178 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800179 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800180 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800181 void onBubbleExpandChanged(boolean isExpanding, String key);
182 }
183
184 /**
185 * Listens for the current state of the status bar and updates the visibility state
186 * of bubbles as needed.
187 */
188 private class StatusBarStateListener implements StatusBarStateController.StateListener {
189 private int mState;
190 /**
191 * Returns the current status bar state.
192 */
193 public int getCurrentState() {
194 return mState;
195 }
196
197 @Override
198 public void onStateChanged(int newState) {
199 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400200 boolean shouldCollapse = (mState != SHADE);
201 if (shouldCollapse) {
202 collapseStack();
203 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700204 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800205 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800206 }
207
Jason Monk27d01a622018-12-10 15:57:09 -0500208 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800209 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Mady Melloraa8fef22019-04-11 13:36:40 -0700210 BubbleData data, ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400211 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400212 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700213 NotificationLockscreenUserManager notifUserManager,
214 NotificationGroupManager groupManager) {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700215 this(context, statusBarWindowController, data, null /* synchronizer */,
Mark Renoufc19b4732019-06-26 12:08:33 -0400216 configurationController, interruptionStateProvider, zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700217 notifUserManager, groupManager);
Issei Suzukic0387542019-03-08 17:31:14 +0100218 }
219
220 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700221 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
Mady Melloraa8fef22019-04-11 13:36:40 -0700222 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400223 NotificationInterruptionStateProvider interruptionStateProvider,
Mark Renoufc19b4732019-06-26 12:08:33 -0400224 ZenModeController zenModeController,
Mady Mellor22f2f072019-04-18 13:26:18 -0700225 NotificationLockscreenUserManager notifUserManager,
226 NotificationGroupManager groupManager) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800227 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700228 mNotificationInterruptionStateProvider = interruptionStateProvider;
Mark Renoufc19b4732019-06-26 12:08:33 -0400229 mNotifUserManager = notifUserManager;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400230 mZenModeController = zenModeController;
231 mZenModeController.addCallback(new ZenModeController.Callback() {
232 @Override
233 public void onZenChanged(int zen) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700234 if (mStackView != null) {
235 mStackView.updateDots();
236 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400237 }
238
239 @Override
240 public void onConfigChanged(ZenModeConfig config) {
Mady Mellordf48d0a2019-06-25 18:26:46 -0700241 if (mStackView != null) {
242 mStackView.updateDots();
243 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400244 }
245 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700246
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700247 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800248
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400249 mBubbleData = data;
250 mBubbleData.setListener(mBubbleDataListener);
251
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800252 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500253 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700254 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellor22f2f072019-04-18 13:26:18 -0700255 mNotificationGroupManager = groupManager;
Mady Mellor740d85d2019-07-09 15:26:47 -0700256 mNotificationGroupManager.addOnGroupChangeListener(
257 new NotificationGroupManager.OnGroupChangeListener() {
258 @Override
259 public void onGroupSuppressionChanged(
260 NotificationGroupManager.NotificationGroup group,
261 boolean suppressed) {
262 // More notifications could be added causing summary to no longer
263 // be suppressed -- in this case need to remove the key.
264 final String groupKey = group.summary != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400265 ? group.summary.getSbn().getGroupKey()
Mady Mellor740d85d2019-07-09 15:26:47 -0700266 : null;
267 if (!suppressed && groupKey != null
268 && mBubbleData.isSummarySuppressed(groupKey)) {
269 mBubbleData.removeSuppressedSummary(groupKey);
270 }
271 }
272 });
Mady Mellorb4991e62019-01-10 15:14:51 -0800273
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800274 mStatusBarWindowController = statusBarWindowController;
275 mStatusBarStateListener = new StatusBarStateListener();
276 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500277
Mark Renoufcecc77b2019-01-30 16:32:24 -0500278 mTaskStackListener = new BubbleTaskStackListener();
279 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800280
Joshua Tsujia19515f2019-02-13 18:02:29 -0500281 try {
282 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
283 } catch (RemoteException e) {
284 e.printStackTrace();
285 }
Issei Suzukic0387542019-03-08 17:31:14 +0100286 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700287
288 mBarService = IStatusBarService.Stub.asInterface(
289 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mark Renoufc19b4732019-06-26 12:08:33 -0400290
291 mSavedBubbleKeysPerUser = new SparseSetArray<>();
292 mCurrentUserId = mNotifUserManager.getCurrentUserId();
293 mNotifUserManager.addUserChangedListener(
294 newUserId -> {
295 saveBubbles(mCurrentUserId);
296 mBubbleData.dismissAll(DISMISS_USER_CHANGED);
297 restoreBubbles(newUserId);
298 mCurrentUserId = newUserId;
299 });
Mady Mellor5549dd22018-11-06 18:07:34 -0800300 }
301
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400302 /**
303 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
304 * method initializes the stack view and adds it to the StatusBar just above the scrim.
305 */
306 private void ensureStackViewCreated() {
307 if (mStackView == null) {
308 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
309 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Lyn Hanbde48202019-05-29 19:18:29 -0700310 int bubbleScrimIndex = sbv.indexOfChild(sbv.findViewById(R.id.scrim_for_bubble));
311 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
312 sbv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400313 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
314 if (mExpandListener != null) {
315 mStackView.setExpandListener(mExpandListener);
316 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400317 }
318 }
319
Mark Renoufc19b4732019-06-26 12:08:33 -0400320 /**
321 * Records the notification key for any active bubbles. These are used to restore active
322 * bubbles when the user returns to the foreground.
323 *
324 * @param userId the id of the user
325 */
326 private void saveBubbles(@UserIdInt int userId) {
327 // First clear any existing keys that might be stored.
328 mSavedBubbleKeysPerUser.remove(userId);
329 // Add in all active bubbles for the current user.
330 for (Bubble bubble: mBubbleData.getBubbles()) {
331 mSavedBubbleKeysPerUser.add(userId, bubble.getKey());
332 }
333 }
334
335 /**
336 * Promotes existing notifications to Bubbles if they were previously bubbles.
337 *
338 * @param userId the id of the user
339 */
340 private void restoreBubbles(@UserIdInt int userId) {
341 NotificationData notificationData =
342 mNotificationEntryManager.getNotificationData();
343 ArraySet<String> savedBubbleKeys = mSavedBubbleKeysPerUser.get(userId);
344 if (savedBubbleKeys == null) {
345 // There were no bubbles saved for this used.
346 return;
347 }
348 for (NotificationEntry e : notificationData.getNotificationsForCurrentUser()) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400349 if (savedBubbleKeys.contains(e.getKey())
Mark Renoufc19b4732019-06-26 12:08:33 -0400350 && mNotificationInterruptionStateProvider.shouldBubbleUp(e)
351 && canLaunchInActivityView(mContext, e)) {
352 updateBubble(e, /* suppressFlyout= */ true);
353 }
354 }
355 // Finally, remove the entries for this user now that bubbles are restored.
356 mSavedBubbleKeysPerUser.remove(mCurrentUserId);
357 }
358
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700359 @Override
360 public void onUiModeChanged() {
361 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700362 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700363 }
364 }
365
366 @Override
367 public void onOverlayChanged() {
368 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700369 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700370 }
371 }
372
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400373 @Override
374 public void onConfigChanged(Configuration newConfig) {
375 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400376 mOrientation = newConfig.orientation;
Lyn Hanf4730312019-06-18 11:18:58 -0700377 mStackView.onOrientationChanged(newConfig.orientation);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400378 }
379 }
380
Mady Mellor5549dd22018-11-06 18:07:34 -0800381 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800382 * Set a listener to be notified when some states of the bubbles change.
383 */
384 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
385 mStateChangeListener = listener;
386 }
387
388 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800389 * Set a listener to be notified of bubble expand events.
390 */
391 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100392 mExpandListener = ((isExpanding, key) -> {
393 if (listener != null) {
394 listener.onBubbleExpandChanged(isExpanding, key);
395 }
396 mStatusBarWindowController.setBubbleExpanded(isExpanding);
397 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800398 if (mStackView != null) {
399 mStackView.setExpandListener(mExpandListener);
400 }
401 }
402
403 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800404 * Whether or not there are bubbles present, regardless of them being visible on the
405 * screen (e.g. if on AOD).
406 */
407 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800408 if (mStackView == null) {
409 return false;
410 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400411 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800412 }
413
414 /**
415 * Whether the stack of bubbles is expanded or not.
416 */
417 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400418 return mBubbleData.isExpanded();
419 }
420
421 /**
422 * Tell the stack of bubbles to expand.
423 */
424 public void expandStack() {
425 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800426 }
427
428 /**
429 * Tell the stack of bubbles to collapse.
430 */
431 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400432 mBubbleData.setExpanded(false /* expanded */);
433 }
434
Mady Mellorce23c462019-06-17 17:30:07 -0700435 /**
Mady Mellore28fe102019-07-09 15:33:32 -0700436 * True if either:
437 * (1) There is a bubble associated with the provided key and if its notification is hidden
438 * from the shade.
439 * (2) There is a group summary associated with the provided key that is hidden from the shade
440 * because it has been dismissed but still has child bubbles active.
Mady Mellorce23c462019-06-17 17:30:07 -0700441 *
Mady Mellore28fe102019-07-09 15:33:32 -0700442 * False otherwise.
Mady Mellorce23c462019-06-17 17:30:07 -0700443 */
444 public boolean isBubbleNotificationSuppressedFromShade(String key) {
Mady Mellore28fe102019-07-09 15:33:32 -0700445 boolean isBubbleAndSuppressed = mBubbleData.hasBubbleWithKey(key)
Mady Mellorce23c462019-06-17 17:30:07 -0700446 && !mBubbleData.getBubbleWithKey(key).showInShadeWhenBubble();
Mady Mellore28fe102019-07-09 15:33:32 -0700447 NotificationEntry entry = mNotificationEntryManager.getNotificationData().get(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400448 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellore28fe102019-07-09 15:33:32 -0700449 boolean isSuppressedSummary = mBubbleData.isSummarySuppressed(groupKey);
Mady Mellore4348272019-07-29 17:43:36 -0700450 boolean isSummary = key.equals(mBubbleData.getSummaryKey(groupKey));
451 return (isSummary && isSuppressedSummary) || isBubbleAndSuppressed;
Mady Mellorce23c462019-06-17 17:30:07 -0700452 }
453
Mark Renouf71a3af62019-04-08 15:02:54 -0400454 void selectBubble(Bubble bubble) {
455 mBubbleData.setSelectedBubble(bubble);
456 }
457
458 @VisibleForTesting
459 void selectBubble(String key) {
460 Bubble bubble = mBubbleData.getBubbleWithKey(key);
461 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800462 }
463
464 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400465 * Request the stack expand if needed, then select the specified Bubble as current.
466 *
467 * @param notificationKey the notification key for the bubble to be selected
468 */
469 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400470 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
471 if (bubble != null) {
472 mBubbleData.setSelectedBubble(bubble);
473 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400474 }
475 }
476
477 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800478 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
479 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500480 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400481 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800482 }
483
484 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500485 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
486 * is forwarded a back key down/up pair.
487 */
488 public void performBackPressIfNeeded() {
489 if (mStackView != null) {
490 mStackView.performBackPressIfNeeded();
491 }
492 }
493
494 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800495 * Adds or updates a bubble associated with the provided notification entry.
496 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400497 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800498 */
Mark Renouff97ed462019-04-05 13:46:24 -0400499 void updateBubble(NotificationEntry notif) {
Mark Renoufc19b4732019-06-26 12:08:33 -0400500 updateBubble(notif, /* supressFlyout */ false);
501 }
502
503 void updateBubble(NotificationEntry notif, boolean suppressFlyout) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700504 // If this is an interruptive notif, mark that it's interrupted
Ned Burns60e94592019-09-06 14:47:25 -0400505 if (notif.getImportance() >= NotificationManager.IMPORTANCE_HIGH) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700506 notif.setInterruption();
507 }
Mark Renoufc19b4732019-06-26 12:08:33 -0400508 mBubbleData.notificationEntryUpdated(notif, suppressFlyout);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800509 }
510
511 /**
512 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500513 * <p>
514 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800515 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500516 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500517 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400518 // TEMP: refactor to change this to pass entry
519 Bubble bubble = mBubbleData.getBubbleWithKey(key);
520 if (bubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700521 mBubbleData.notificationEntryRemoved(bubble.getEntry(), reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800522 }
523 }
524
Ned Burns01e38212019-01-03 16:32:52 -0500525 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700526 private final NotificationRemoveInterceptor mRemoveInterceptor =
527 new NotificationRemoveInterceptor() {
528 @Override
529 public boolean onNotificationRemoveRequested(String key, int reason) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700530 NotificationEntry entry = mNotificationEntryManager.getNotificationData().get(key);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400531 String groupKey = entry != null ? entry.getSbn().getGroupKey() : null;
Mady Mellor22f2f072019-04-18 13:26:18 -0700532 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
533
534 boolean inBubbleData = mBubbleData.hasBubbleWithKey(key);
Mady Mellore28fe102019-07-09 15:33:32 -0700535 boolean isSuppressedSummary = (mBubbleData.isSummarySuppressed(groupKey)
536 && mBubbleData.getSummaryKey(groupKey).equals(key));
Mady Mellor22f2f072019-04-18 13:26:18 -0700537 boolean isSummary = entry != null
Ned Burns00b4b2d2019-10-17 22:09:27 -0400538 && entry.getSbn().getNotification().isGroupSummary();
Mady Mellore28fe102019-07-09 15:33:32 -0700539 boolean isSummaryOfBubbles = (isSuppressedSummary || isSummary)
540 && bubbleChildren != null && !bubbleChildren.isEmpty();
Mady Mellor22f2f072019-04-18 13:26:18 -0700541
542 if (!inBubbleData && !isSummaryOfBubbles) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700543 return false;
544 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700545
546 final boolean isClearAll = reason == REASON_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700547 final boolean isUserDimiss = reason == REASON_CANCEL || reason == REASON_CLICK;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700548 final boolean isAppCancel = reason == REASON_APP_CANCEL
549 || reason == REASON_APP_CANCEL_ALL;
Mady Mellor22f2f072019-04-18 13:26:18 -0700550 final boolean isSummaryCancel = reason == REASON_GROUP_SUMMARY_CANCELED;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700551
552 // Need to check for !appCancel here because the notification may have
553 // previously been dismissed & entry.isRowDismissed would still be true
Mady Mellorca184aae2019-09-17 16:07:12 -0700554 boolean userRemovedNotif = (entry != null && entry.isRowDismissed() && !isAppCancel)
Mady Mellor22f2f072019-04-18 13:26:18 -0700555 || isClearAll || isUserDimiss || isSummaryCancel;
556
557 if (isSummaryOfBubbles) {
558 return handleSummaryRemovalInterception(entry, userRemovedNotif);
559 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700560
561 // The bubble notification sticks around in the data as long as the bubble is
562 // not dismissed and the app hasn't cancelled the notification.
Mady Mellor22f2f072019-04-18 13:26:18 -0700563 Bubble bubble = mBubbleData.getBubbleWithKey(key);
Mady Mellorca184aae2019-09-17 16:07:12 -0700564 boolean bubbleExtended = entry != null && entry.isBubble() && userRemovedNotif;
Mady Mellorc2ff0112019-03-28 14:18:06 -0700565 if (bubbleExtended) {
Mady Mellorce23c462019-06-17 17:30:07 -0700566 bubble.setShowInShadeWhenBubble(false);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700567 bubble.setShowBubbleDot(false);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700568 if (mStackView != null) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400569 mStackView.updateDotVisibility(entry.getKey());
Mady Mellorc2ff0112019-03-28 14:18:06 -0700570 }
Beverly85d4c192019-09-30 11:40:39 -0400571 mNotificationEntryManager.updateNotifications(
572 "BubbleController.onNotificationRemoveRequested");
Mady Mellorc2ff0112019-03-28 14:18:06 -0700573 return true;
Mady Mellorca184aae2019-09-17 16:07:12 -0700574 } else if (!userRemovedNotif && entry != null) {
Mady Mellorc2ff0112019-03-28 14:18:06 -0700575 // This wasn't a user removal so we should remove the bubble as well
576 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
577 return false;
578 }
579 return false;
580 }
581 };
582
Mady Mellor22f2f072019-04-18 13:26:18 -0700583 private boolean handleSummaryRemovalInterception(NotificationEntry summary,
584 boolean userRemovedNotif) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400585 String groupKey = summary.getSbn().getGroupKey();
Mady Mellor22f2f072019-04-18 13:26:18 -0700586 ArrayList<Bubble> bubbleChildren = mBubbleData.getBubblesInGroup(groupKey);
587
588 if (userRemovedNotif) {
589 // If it's a user dismiss we mark the children to be hidden from the shade.
590 for (int i = 0; i < bubbleChildren.size(); i++) {
591 Bubble bubbleChild = bubbleChildren.get(i);
592 // As far as group manager is concerned, once a child is no longer shown
593 // in the shade, it is essentially removed.
594 mNotificationGroupManager.onEntryRemoved(bubbleChild.getEntry());
595 bubbleChild.setShowInShadeWhenBubble(false);
596 bubbleChild.setShowBubbleDot(false);
597 if (mStackView != null) {
598 mStackView.updateDotVisibility(bubbleChild.getKey());
599 }
600 }
601 // And since all children are removed, remove the summary.
602 mNotificationGroupManager.onEntryRemoved(summary);
603
604 // If the summary was auto-generated we don't need to keep that notification around
605 // because apps can't cancel it; so we only intercept & suppress real summaries.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400606 boolean isAutogroupSummary = (summary.getSbn().getNotification().flags
Mady Mellor22f2f072019-04-18 13:26:18 -0700607 & FLAG_AUTOGROUP_SUMMARY) != 0;
Mady Mellore28fe102019-07-09 15:33:32 -0700608 if (!isAutogroupSummary) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400609 mBubbleData.addSummaryToSuppress(summary.getSbn().getGroupKey(),
610 summary.getKey());
Mady Mellore28fe102019-07-09 15:33:32 -0700611 // Tell shade to update for the suppression
Beverly85d4c192019-09-30 11:40:39 -0400612 mNotificationEntryManager.updateNotifications(
613 "BubbleController.handleSummaryRemovalInterception");
Mady Mellore28fe102019-07-09 15:33:32 -0700614 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700615 return !isAutogroupSummary;
616 } else {
Mady Mellore28fe102019-07-09 15:33:32 -0700617 // If it's not a user dismiss it's a cancel.
618 mBubbleData.removeSuppressedSummary(groupKey);
619
Mady Mellor22f2f072019-04-18 13:26:18 -0700620 // Remove any associated bubble children.
621 for (int i = 0; i < bubbleChildren.size(); i++) {
622 Bubble bubbleChild = bubbleChildren.get(i);
623 mBubbleData.notificationEntryRemoved(bubbleChild.getEntry(),
624 DISMISS_GROUP_CANCELLED);
625 }
626 return false;
627 }
628 }
629
Mady Mellorc2ff0112019-03-28 14:18:06 -0700630 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500631 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
632 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500633 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorca0c24c2019-05-16 16:14:32 -0700634 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
635 && canLaunchInActivityView(mContext, entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400636 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800637 }
638 }
639
640 @Override
641 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorca0c24c2019-05-16 16:14:32 -0700642 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
643 && canLaunchInActivityView(mContext, entry);
Ned Burns00b4b2d2019-10-17 22:09:27 -0400644 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.getKey())) {
Mady Melloraa8fef22019-04-11 13:36:40 -0700645 // It was previously a bubble but no longer a bubble -- lets remove it
Ned Burns00b4b2d2019-10-17 22:09:27 -0400646 removeBubble(entry.getKey(), DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700647 } else if (shouldBubble) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400648 Bubble b = mBubbleData.getBubbleWithKey(entry.getKey());
Mark Renouff97ed462019-04-05 13:46:24 -0400649 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800650 }
651 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400652
653 @Override
654 public void onNotificationRankingUpdated(RankingMap rankingMap) {
655 // Forward to BubbleData to block any bubbles which should no longer be shown
656 mBubbleData.notificationRankingUpdated(rankingMap);
657 }
Ned Burns01e38212019-01-03 16:32:52 -0500658 };
659
Mark Renouf71a3af62019-04-08 15:02:54 -0400660 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400661 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400662
Mark Renouf3bc5b362019-04-05 14:37:59 -0400663 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400664 public void applyUpdate(BubbleData.Update update) {
665 if (mStackView == null && update.addedBubble != null) {
666 // Lazy init stack view when the first bubble is added.
667 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400668 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400669
670 // If not yet initialized, ignore all other changes.
671 if (mStackView == null) {
672 return;
673 }
674
675 if (update.addedBubble != null) {
676 mStackView.addBubble(update.addedBubble);
677 }
678
679 // Collapsing? Do this first before remaining steps.
680 if (update.expandedChanged && !update.expanded) {
681 mStackView.setExpanded(false);
682 }
683
684 // Do removals, if any.
Mady Mellor22f2f072019-04-18 13:26:18 -0700685 ArrayList<Pair<Bubble, Integer>> removedBubbles =
686 new ArrayList<>(update.removedBubbles);
687 for (Pair<Bubble, Integer> removed : removedBubbles) {
Mark Renouf82a40e62019-05-23 16:16:24 -0400688 final Bubble bubble = removed.first;
689 @DismissReason final int reason = removed.second;
690 mStackView.removeBubble(bubble);
691
Mark Renoufc19b4732019-06-26 12:08:33 -0400692 // If the bubble is removed for user switching, leave the notification in place.
693 if (reason != DISMISS_USER_CHANGED) {
694 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
695 && !bubble.showInShadeWhenBubble()) {
696 // The bubble is gone & the notification is gone, time to actually remove it
697 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400698 bubble.getEntry().getSbn(), UNDEFINED_DISMISS_REASON);
Mark Renoufc19b4732019-06-26 12:08:33 -0400699 } else {
700 // Update the flag for SysUI
Ned Burns00b4b2d2019-10-17 22:09:27 -0400701 bubble.getEntry().getSbn().getNotification().flags &= ~FLAG_BUBBLE;
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700702
Mark Renoufc19b4732019-06-26 12:08:33 -0400703 // Make sure NoMan knows it's not a bubble anymore so anyone querying it
704 // will get right result back
705 try {
706 mBarService.onNotificationBubbleChanged(bubble.getKey(),
707 false /* isBubble */);
708 } catch (RemoteException e) {
709 // Bad things have happened
710 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400711 }
Mady Mellor22f2f072019-04-18 13:26:18 -0700712
Mady Mellore28fe102019-07-09 15:33:32 -0700713 // Check if removed bubble has an associated suppressed group summary that needs
714 // to be removed now.
Ned Burns00b4b2d2019-10-17 22:09:27 -0400715 final String groupKey = bubble.getEntry().getSbn().getGroupKey();
Mady Mellore28fe102019-07-09 15:33:32 -0700716 if (mBubbleData.isSummarySuppressed(groupKey)
717 && mBubbleData.getBubblesInGroup(groupKey).isEmpty()) {
718 // Time to actually remove the summary.
719 String notifKey = mBubbleData.getSummaryKey(groupKey);
720 mBubbleData.removeSuppressedSummary(groupKey);
721 NotificationEntry entry =
722 mNotificationEntryManager.getNotificationData().get(notifKey);
Mady Mellore28fe102019-07-09 15:33:32 -0700723 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400724 entry.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellore28fe102019-07-09 15:33:32 -0700725 }
726
Mady Mellor22f2f072019-04-18 13:26:18 -0700727 // Check if summary should be removed from NoManGroup
728 NotificationEntry summary = mNotificationGroupManager.getLogicalGroupSummary(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400729 bubble.getEntry().getSbn());
Mady Mellor22f2f072019-04-18 13:26:18 -0700730 if (summary != null) {
731 ArrayList<NotificationEntry> summaryChildren =
Ned Burns00b4b2d2019-10-17 22:09:27 -0400732 mNotificationGroupManager.getLogicalChildren(summary.getSbn());
733 boolean isSummaryThisNotif = summary.getKey().equals(
734 bubble.getEntry().getKey());
Mady Mellore4348272019-07-29 17:43:36 -0700735 if (!isSummaryThisNotif
736 && (summaryChildren == null || summaryChildren.isEmpty())) {
Mady Mellor22f2f072019-04-18 13:26:18 -0700737 mNotificationEntryManager.performRemoveNotification(
Ned Burns00b4b2d2019-10-17 22:09:27 -0400738 summary.getSbn(), UNDEFINED_DISMISS_REASON);
Mady Mellor22f2f072019-04-18 13:26:18 -0700739 }
740 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700741 }
742 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400743
Mark Renouf82a40e62019-05-23 16:16:24 -0400744 if (update.updatedBubble != null) {
745 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400746 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400747
Mark Renouf82a40e62019-05-23 16:16:24 -0400748 if (update.orderChanged) {
749 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400750 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400751
Mark Renouf82a40e62019-05-23 16:16:24 -0400752 if (update.selectionChanged) {
753 mStackView.setSelectedBubble(update.selectedBubble);
Mady Mellor740d85d2019-07-09 15:26:47 -0700754 if (update.selectedBubble != null) {
755 mNotificationGroupManager.updateSuppression(
756 update.selectedBubble.getEntry());
757 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400758 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400759
Mark Renouf82a40e62019-05-23 16:16:24 -0400760 // Expanding? Apply this last.
761 if (update.expandedChanged && update.expanded) {
762 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400763 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400764
Beverly85d4c192019-09-30 11:40:39 -0400765 mNotificationEntryManager.updateNotifications(
766 "BubbleData.Listener.applyUpdate");
Lyn Han6c40fe72019-05-08 14:06:33 -0700767 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400768
Issei Suzukia8d07312019-06-07 12:56:19 +0200769 if (DEBUG_BUBBLE_CONTROLLER) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400770 Log.d(TAG, "[BubbleData]");
771 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
772 mBubbleData.getSelectedBubble()));
773
774 if (mStackView != null) {
775 Log.d(TAG, "[BubbleStackView]");
776 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
777 mStackView.getExpandedBubble()));
778 }
779 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400780 }
781 };
782
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800783 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400784 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700785 * Updates the visibility of the bubbles based on current state.
786 * Does not un-bubble, just hides or un-hides. Notifies any
787 * {@link BubbleStateChangeListener}s of visibility changes.
788 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800789 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700790 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800791 if (mStackView == null) {
792 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800793 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800794 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
795 // Bubbles only appear in unlocked shade
796 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
Mady Mellor698d9e82019-08-01 23:11:53 +0000797 } else if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800798 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800799 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700800
Mady Mellor698d9e82019-08-01 23:11:53 +0000801 // Let listeners know if bubble state changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700802 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor698d9e82019-08-01 23:11:53 +0000803 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellor88552b82019-08-05 22:38:59 +0000804 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Lyn Han6c40fe72019-05-08 14:06:33 -0700805 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
806 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
807 }
808
809 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800810 }
811
812 /**
813 * Rect indicating the touchable region for the bubble stack / expanded stack.
814 */
815 public Rect getTouchableRegion() {
816 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
817 return null;
818 }
819 mStackView.getBoundsOnScreen(mTempRect);
820 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800821 }
822
Mady Mellor390bff42019-04-05 15:09:01 -0700823 /**
824 * The display id of the expanded view, if the stack is expanded and not occluded by the
825 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
826 */
827 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200828 final Bubble bubble = getExpandedBubble(context);
829 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
830 }
831
832 @Nullable
833 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700834 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200835 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700836 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200837 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700838 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200839 final Bubble expandedBubble = mStackView.getExpandedBubble();
840 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700841 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200842 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700843 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200844 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700845 }
846
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800847 @VisibleForTesting
848 BubbleStackView getStackView() {
849 return mStackView;
850 }
851
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700852 /**
853 * Description of current bubble state.
854 */
855 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
856 pw.println("BubbleController state:");
857 mBubbleData.dump(fd, pw, args);
858 pw.println();
Joshua Tsuji395bcfe2019-07-02 19:23:23 -0400859 if (mStackView != null) {
860 mStackView.dump(fd, pw, args);
861 }
862 pw.println();
Mady Mellor70cba7bb2019-07-02 15:06:07 -0700863 }
864
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400865 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
866 StringBuilder sb = new StringBuilder();
867 for (Bubble bubble : bubbles) {
868 if (bubble == null) {
869 sb.append(" <null> !!!!!\n");
870 } else {
871 boolean isSelected = (bubble == selected);
872 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
873 ((isSelected) ? "->" : " "),
874 bubble.getLastActivity(),
875 (bubble.isOngoing() ? 1 : 0),
876 bubble.getKey()));
877 }
878 }
879 return sb.toString();
880 }
881
Mady Mellore80930e2019-03-21 16:00:45 -0700882 /**
Mark Renoufcecc77b2019-01-30 16:32:24 -0500883 * This task stack listener is responsible for responding to tasks moved to the front
884 * which are on the default (main) display. When this happens, expanded bubbles must be
885 * collapsed so the user may interact with the app which was just moved to the front.
886 * <p>
887 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
888 * these calls via a main thread Handler.
889 */
890 @MainThread
891 private class BubbleTaskStackListener extends TaskStackChangeListener {
892
Mark Renoufcecc77b2019-01-30 16:32:24 -0500893 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500894 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
895 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mady Mellor047e24e2019-08-05 11:35:40 -0700896 if (!mStackView.isExpansionAnimating()) {
897 mBubbleData.setExpanded(false);
898 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500899 }
900 }
901
Mark Renoufcecc77b2019-01-30 16:32:24 -0500902 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500903 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500904 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400905 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500906 }
907 }
Mark Renouf446251d2019-04-26 10:22:41 -0400908
909 @Override
910 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
911 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
912 mBubbleData.setExpanded(false);
913 }
914 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200915
916 @Override
917 public void onSingleTaskDisplayDrawn(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -0700918 final Bubble expandedBubble = mStackView != null
919 ? mStackView.getExpandedBubble()
920 : null;
Issei Suzukicac2a502019-04-16 16:52:50 +0200921 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
922 expandedBubble.setContentVisibility(true);
923 }
924 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200925
926 @Override
927 public void onSingleTaskDisplayEmpty(int displayId) {
Mady Mellor5186b132019-09-16 17:55:48 -0700928 final Bubble expandedBubble = mStackView != null
929 ? mStackView.getExpandedBubble()
930 : null;
Mady Mellorca184aae2019-09-17 16:07:12 -0700931 int expandedId = expandedBubble != null ? expandedBubble.getDisplayId() : -1;
932 if (mStackView != null && mStackView.isExpanded() && expandedId == displayId) {
Issei Suzuki734bc942019-06-05 13:59:52 +0200933 mBubbleData.setExpanded(false);
Issei Suzuki734bc942019-06-05 13:59:52 +0200934 }
Mady Mellorca184aae2019-09-17 16:07:12 -0700935 mBubbleData.notifyDisplayEmpty(displayId);
Issei Suzuki734bc942019-06-05 13:59:52 +0200936 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500937 }
938
Mady Mellorca0c24c2019-05-16 16:14:32 -0700939 /**
940 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
941 *
942 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
943 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
944 *
945 * @param context the context to use.
946 * @param entry the entry to bubble.
947 */
948 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
949 PendingIntent intent = entry.getBubbleMetadata() != null
950 ? entry.getBubbleMetadata().getIntent()
951 : null;
952 if (intent == null) {
953 Log.w(TAG, "Unable to create bubble -- no intent");
954 return false;
955 }
956 ActivityInfo info =
957 intent.getIntent().resolveActivityInfo(context.getPackageManager(), 0);
958 if (info == null) {
959 Log.w(TAG, "Unable to send as bubble -- couldn't find activity info for intent: "
960 + intent);
961 return false;
962 }
963 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
964 Log.w(TAG, "Unable to send as bubble -- activity is not resizable for intent: "
965 + intent);
966 return false;
967 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700968 return true;
969 }
970
Joshua Tsujia19515f2019-02-13 18:02:29 -0500971 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
Hongwei Wang43a752b2019-09-17 20:20:30 +0000972 private class BubblesImeListener extends PinnedStackListenerForwarder.PinnedStackListener {
Joshua Tsujia19515f2019-02-13 18:02:29 -0500973 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500974 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
975 if (mStackView != null && mStackView.getBubbleCount() > 0) {
976 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500977 }
978 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500979 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800980}