blob: 3c5c1028763b3903c4f30bea97ff95c2ecd1f68b [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 Mellorc2ff0112019-03-28 14:18:06 -070019import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL;
20import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL;
21import static android.service.notification.NotificationListenerService.REASON_CANCEL;
22import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL;
Mady Mellor390bff42019-04-05 15:09:01 -070023import static android.view.Display.DEFAULT_DISPLAY;
24import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080025import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080026import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080027import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080028
Mady Mellor3f2efdb2018-11-21 11:30:45 -080029import static com.android.systemui.statusbar.StatusBarState.SHADE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080030
Mark Renouf08bc42a2019-03-07 13:01:59 -050031import static java.lang.annotation.RetentionPolicy.SOURCE;
32
Mady Mellorb4991e62019-01-10 15:14:51 -080033import android.annotation.Nullable;
Mady Mellore80930e2019-03-21 16:00:45 -070034import android.app.ActivityManager;
Mark Renoufc808f062019-02-07 15:20:37 -050035import android.app.ActivityManager.RunningTaskInfo;
Mark Renoufcecc77b2019-01-30 16:32:24 -050036import android.app.ActivityTaskManager;
37import android.app.IActivityTaskManager;
Mady Mellor5549dd22018-11-06 18:07:34 -080038import android.app.Notification;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080039import android.content.Context;
Joshua Tsujia19515f2019-02-13 18:02:29 -050040import android.content.pm.ParceledListSlice;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040041import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080042import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050043import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080044import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080045import android.provider.Settings;
Mady Mellor5549dd22018-11-06 18:07:34 -080046import android.service.notification.StatusBarNotification;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040047import android.util.Log;
Mark Renoufcecc77b2019-01-30 16:32:24 -050048import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050049import android.view.IPinnedStackController;
50import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080051import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080052import android.widget.FrameLayout;
53
Mark Renouf08bc42a2019-03-07 13:01:59 -050054import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050055import androidx.annotation.MainThread;
56
Mady Mellorebdbbb92018-11-15 14:36:48 -080057import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070058import com.android.internal.statusbar.IStatusBarService;
Ned Burns01e38212019-01-03 16:32:52 -050059import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080060import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050061import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050062import com.android.systemui.shared.system.ActivityManagerWrapper;
63import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050064import com.android.systemui.shared.system.WindowManagerWrapper;
Mady Mellorc2ff0112019-03-28 14:18:06 -070065import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050066import com.android.systemui.statusbar.notification.NotificationEntryListener;
67import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080068import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050069import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050070import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080071import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070072import com.android.systemui.statusbar.policy.ConfigurationController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080073
Mark Renouf08bc42a2019-03-07 13:01:59 -050074import java.lang.annotation.Retention;
Mady Mellore80930e2019-03-21 16:00:45 -070075import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050076
Jason Monk27d01a622018-12-10 15:57:09 -050077import javax.inject.Inject;
78import javax.inject.Singleton;
79
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080080/**
81 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
82 * Bubbles can be expanded to show more content.
83 *
84 * The controller manages addition, removal, and visible state of bubbles on screen.
85 */
Jason Monk27d01a622018-12-10 15:57:09 -050086@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -040087public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080088
89 private static final String TAG = "BubbleController";
Mark Renouf9ba6cea2019-04-17 11:53:50 -040090 private static final boolean DEBUG = true;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080091
Mark Renouf08bc42a2019-03-07 13:01:59 -050092 @Retention(SOURCE)
93 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mady Melloraa8fef22019-04-11 13:36:40 -070094 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE})
Mark Renouf08bc42a2019-03-07 13:01:59 -050095 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070096
Mark Renouf08bc42a2019-03-07 13:01:59 -050097 static final int DISMISS_USER_GESTURE = 1;
98 static final int DISMISS_AGED = 2;
99 static final int DISMISS_TASK_FINISHED = 3;
100 static final int DISMISS_BLOCKED = 4;
101 static final int DISMISS_NOTIF_CANCEL = 5;
102 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700103 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500104
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -0400105 static final int MAX_BUBBLES = 5; // TODO: actually enforce this
106
Mady Mellor5549dd22018-11-06 18:07:34 -0800107 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -0500108 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -0800109
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800110 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800111 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800112 /** Auto bubble flags set whether different notif types should be presented as a bubble */
Mady Mellorceced172018-11-27 11:18:39 -0800113 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
114 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
115 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800116
117 /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500118 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -0800119
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500120 private static final String BUBBLE_STIFFNESS = "experiment_bubble_stiffness";
121 private static final String BUBBLE_BOUNCINESS = "experiment_bubble_bounciness";
122
Ned Burns01e38212019-01-03 16:32:52 -0500123 private final Context mContext;
124 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500125 private final IActivityTaskManager mActivityTaskManager;
126 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800127 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800128 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100129 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800130
Mady Mellor3dff9e62019-02-05 18:12:53 -0800131 private BubbleData mBubbleData;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800132 private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800133
134 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500135 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800136 private StatusBarStateListener mStatusBarStateListener;
137
Mady Melloraa8fef22019-04-11 13:36:40 -0700138 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700139 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800140
Mady Mellord1c78b262018-11-06 18:04:40 -0800141 // Used for determining view rect for touch interaction
142 private Rect mTempRect = new Rect();
143
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400144 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
145 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
146
Mady Mellor5549dd22018-11-06 18:07:34 -0800147 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800148 * Listener to be notified when some states of the bubbles change.
149 */
150 public interface BubbleStateChangeListener {
151 /**
152 * Called when the stack has bubbles or no longer has bubbles.
153 */
154 void onHasBubblesChanged(boolean hasBubbles);
155 }
156
Mady Mellorcd9b1302018-11-06 18:08:04 -0800157 /**
158 * Listener to find out about stack expansion / collapse events.
159 */
160 public interface BubbleExpandListener {
161 /**
162 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700163 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800164 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800165 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800166 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800167 void onBubbleExpandChanged(boolean isExpanding, String key);
168 }
169
170 /**
171 * Listens for the current state of the status bar and updates the visibility state
172 * of bubbles as needed.
173 */
174 private class StatusBarStateListener implements StatusBarStateController.StateListener {
175 private int mState;
176 /**
177 * Returns the current status bar state.
178 */
179 public int getCurrentState() {
180 return mState;
181 }
182
183 @Override
184 public void onStateChanged(int newState) {
185 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400186 boolean shouldCollapse = (mState != SHADE);
187 if (shouldCollapse) {
188 collapseStack();
189 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800190 updateVisibility();
191 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800192 }
193
Jason Monk27d01a622018-12-10 15:57:09 -0500194 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800195 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Mady Melloraa8fef22019-04-11 13:36:40 -0700196 BubbleData data, ConfigurationController configurationController,
197 NotificationInterruptionStateProvider interruptionStateProvider) {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700198 this(context, statusBarWindowController, data, null /* synchronizer */,
Mady Melloraa8fef22019-04-11 13:36:40 -0700199 configurationController, interruptionStateProvider);
Issei Suzukic0387542019-03-08 17:31:14 +0100200 }
201
202 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700203 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
Mady Melloraa8fef22019-04-11 13:36:40 -0700204 ConfigurationController configurationController,
205 NotificationInterruptionStateProvider interruptionStateProvider) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800206 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700207 mNotificationInterruptionStateProvider = interruptionStateProvider;
208
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700209 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800210
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400211 mBubbleData = data;
212 mBubbleData.setListener(mBubbleDataListener);
213
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800214 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500215 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700216 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellorb4991e62019-01-10 15:14:51 -0800217
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800218 mStatusBarWindowController = statusBarWindowController;
219 mStatusBarStateListener = new StatusBarStateListener();
220 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500221
222 mActivityTaskManager = ActivityTaskManager.getService();
223 mTaskStackListener = new BubbleTaskStackListener();
224 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800225
Joshua Tsujia19515f2019-02-13 18:02:29 -0500226 try {
227 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
228 } catch (RemoteException e) {
229 e.printStackTrace();
230 }
Issei Suzukic0387542019-03-08 17:31:14 +0100231 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700232
233 mBarService = IStatusBarService.Stub.asInterface(
234 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mady Mellor5549dd22018-11-06 18:07:34 -0800235 }
236
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400237 /**
238 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
239 * method initializes the stack view and adds it to the StatusBar just above the scrim.
240 */
241 private void ensureStackViewCreated() {
242 if (mStackView == null) {
243 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
244 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
245 // TODO(b/130237686): When you expand the shade on top of expanded bubble, there is no
246 // scrim between bubble and the shade
247 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
248 sbv.addView(mStackView, bubblePosition,
249 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
250 if (mExpandListener != null) {
251 mStackView.setExpandListener(mExpandListener);
252 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400253 }
254 }
255
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700256 @Override
257 public void onUiModeChanged() {
258 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700259 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700260 }
261 }
262
263 @Override
264 public void onOverlayChanged() {
265 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700266 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700267 }
268 }
269
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400270 @Override
271 public void onConfigChanged(Configuration newConfig) {
272 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
273 mStackView.onOrientationChanged();
274 mOrientation = newConfig.orientation;
275 }
276 }
277
Mady Mellor5549dd22018-11-06 18:07:34 -0800278 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800279 * Set a listener to be notified when some states of the bubbles change.
280 */
281 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
282 mStateChangeListener = listener;
283 }
284
285 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800286 * Set a listener to be notified of bubble expand events.
287 */
288 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100289 mExpandListener = ((isExpanding, key) -> {
290 if (listener != null) {
291 listener.onBubbleExpandChanged(isExpanding, key);
292 }
293 mStatusBarWindowController.setBubbleExpanded(isExpanding);
294 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800295 if (mStackView != null) {
296 mStackView.setExpandListener(mExpandListener);
297 }
298 }
299
300 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800301 * Whether or not there are bubbles present, regardless of them being visible on the
302 * screen (e.g. if on AOD).
303 */
304 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800305 if (mStackView == null) {
306 return false;
307 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400308 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800309 }
310
311 /**
312 * Whether the stack of bubbles is expanded or not.
313 */
314 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400315 return mBubbleData.isExpanded();
316 }
317
318 /**
319 * Tell the stack of bubbles to expand.
320 */
321 public void expandStack() {
322 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800323 }
324
325 /**
326 * Tell the stack of bubbles to collapse.
327 */
328 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400329 mBubbleData.setExpanded(false /* expanded */);
330 }
331
332 void selectBubble(Bubble bubble) {
333 mBubbleData.setSelectedBubble(bubble);
334 }
335
336 @VisibleForTesting
337 void selectBubble(String key) {
338 Bubble bubble = mBubbleData.getBubbleWithKey(key);
339 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800340 }
341
342 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400343 * Request the stack expand if needed, then select the specified Bubble as current.
344 *
345 * @param notificationKey the notification key for the bubble to be selected
346 */
347 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400348 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
349 if (bubble != null) {
350 mBubbleData.setSelectedBubble(bubble);
351 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400352 }
353 }
354
355 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800356 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
357 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500358 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400359 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800360 }
361
362 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500363 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
364 * is forwarded a back key down/up pair.
365 */
366 public void performBackPressIfNeeded() {
367 if (mStackView != null) {
368 mStackView.performBackPressIfNeeded();
369 }
370 }
371
372 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800373 * Adds or updates a bubble associated with the provided notification entry.
374 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400375 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800376 */
Mark Renouff97ed462019-04-05 13:46:24 -0400377 void updateBubble(NotificationEntry notif) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400378 mBubbleData.notificationEntryUpdated(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800379 }
380
381 /**
382 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500383 * <p>
384 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800385 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500386 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500387 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400388 // TEMP: refactor to change this to pass entry
389 Bubble bubble = mBubbleData.getBubbleWithKey(key);
390 if (bubble != null) {
391 mBubbleData.notificationEntryRemoved(bubble.entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800392 }
393 }
394
Ned Burns01e38212019-01-03 16:32:52 -0500395 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700396 private final NotificationRemoveInterceptor mRemoveInterceptor =
397 new NotificationRemoveInterceptor() {
398 @Override
399 public boolean onNotificationRemoveRequested(String key, int reason) {
400 if (!mBubbleData.hasBubbleWithKey(key)) {
401 return false;
402 }
403 NotificationEntry entry = mBubbleData.getBubbleWithKey(key).entry;
404
405 final boolean isClearAll = reason == REASON_CANCEL_ALL;
406 final boolean isUserDimiss = reason == REASON_CANCEL;
407 final boolean isAppCancel = reason == REASON_APP_CANCEL
408 || reason == REASON_APP_CANCEL_ALL;
409
410 // Need to check for !appCancel here because the notification may have
411 // previously been dismissed & entry.isRowDismissed would still be true
412 boolean userRemovedNotif = (entry.isRowDismissed() && !isAppCancel)
413 || isClearAll || isUserDimiss;
414
415 // The bubble notification sticks around in the data as long as the bubble is
416 // not dismissed and the app hasn't cancelled the notification.
417 boolean bubbleExtended = entry.isBubble() && !entry.isBubbleDismissed()
418 && userRemovedNotif;
419 if (bubbleExtended) {
420 entry.setShowInShadeWhenBubble(false);
421 if (mStackView != null) {
422 mStackView.updateDotVisibility(entry.key);
423 }
424 mNotificationEntryManager.updateNotifications();
425 return true;
426 } else if (!userRemovedNotif && !entry.isBubbleDismissed()) {
427 // This wasn't a user removal so we should remove the bubble as well
428 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
429 return false;
430 }
431 return false;
432 }
433 };
434
435 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500436 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
437 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500438 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800439 if (!areBubblesEnabled(mContext)) {
440 return;
441 }
Mady Melloraa8fef22019-04-11 13:36:40 -0700442 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700443 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800444 }
445 }
446
447 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500448 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800449 if (!areBubblesEnabled(mContext)) {
450 return;
451 }
Mady Melloraa8fef22019-04-11 13:36:40 -0700452 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400453 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800454 }
455 }
456
457 @Override
458 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800459 if (!areBubblesEnabled(mContext)) {
460 return;
461 }
Mady Melloraa8fef22019-04-11 13:36:40 -0700462 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry);
463 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.key)) {
464 // It was previously a bubble but no longer a bubble -- lets remove it
465 removeBubble(entry.key, DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700466 } else if (shouldBubble) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700467 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800468 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mark Renouff97ed462019-04-05 13:46:24 -0400469 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800470 }
471 }
Ned Burns01e38212019-01-03 16:32:52 -0500472 };
473
Mark Renouf71a3af62019-04-08 15:02:54 -0400474 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400475 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400476
Mark Renouf3bc5b362019-04-05 14:37:59 -0400477 @Override
478 public void onBubbleAdded(Bubble bubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400479 ensureStackViewCreated();
480 mStackView.addBubble(bubble);
Mark Renouf3bc5b362019-04-05 14:37:59 -0400481 }
482
483 @Override
Mady Mellorc2ff0112019-03-28 14:18:06 -0700484 public void onBubbleRemoved(Bubble bubble, @DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400485 if (mStackView != null) {
486 mStackView.removeBubble(bubble);
487 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700488 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
489 && !bubble.entry.showInShadeWhenBubble()) {
490 // The bubble is gone & the notification is gone, time to actually remove it
491 mNotificationEntryManager.performRemoveNotification(bubble.entry.notification,
492 0 /* reason */);
Mady Mellora54e9fa2019-04-18 13:26:18 -0700493 } else {
494 // The notification is still in the shade but we've removed the bubble so
495 // lets make sure NoMan knows it's not a bubble anymore
496 try {
497 mBarService.onNotificationBubbleChanged(bubble.getKey(), false /* isBubble */);
498 } catch (RemoteException e) {
499 // Bad things have happened
500 }
501 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400502 }
503
504 public void onBubbleUpdated(Bubble bubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400505 if (mStackView != null) {
506 mStackView.updateBubble(bubble);
507 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400508 }
509
510 @Override
511 public void onOrderChanged(List<Bubble> bubbles) {
Mark Renouf3bc5b362019-04-05 14:37:59 -0400512 }
513
514 @Override
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400515 public void onSelectionChanged(@Nullable Bubble selectedBubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400516 if (mStackView != null) {
517 mStackView.setSelectedBubble(selectedBubble);
518 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400519 }
520
521 @Override
522 public void onExpandedChanged(boolean expanded) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400523 if (mStackView != null) {
524 mStackView.setExpanded(expanded);
525 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400526 }
527
528 @Override
529 public void showFlyoutText(Bubble bubble, String text) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400530 if (mStackView != null) {
531 mStackView.animateInFlyoutForBubble(bubble);
532 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400533 }
534
535 @Override
536 public void apply() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400537 mNotificationEntryManager.updateNotifications();
538 updateVisibility();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400539
540 if (DEBUG) {
541 Log.d(TAG, "[BubbleData]");
542 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
543 mBubbleData.getSelectedBubble()));
544
545 if (mStackView != null) {
546 Log.d(TAG, "[BubbleStackView]");
547 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
548 mStackView.getExpandedBubble()));
549 }
550 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400551 }
552 };
553
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800554 /**
555 * Lets any listeners know if bubble state has changed.
556 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800557 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800558 if (mStackView == null) {
559 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800560 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800561
Mady Mellord1c78b262018-11-06 18:04:40 -0800562 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800563 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800564 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800565 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
566 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
567 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800568 }
569
570 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800571 * Updates the visibility of the bubbles based on current state.
572 * Does not un-bubble, just hides or un-hides. Will notify any
573 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800574 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800575 public void updateVisibility() {
576 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
577 // Bubbles only appear in unlocked shade
578 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
579 } else if (mStackView != null) {
580 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800581 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800582 updateBubblesShowing();
583 }
584
585 /**
586 * Rect indicating the touchable region for the bubble stack / expanded stack.
587 */
588 public Rect getTouchableRegion() {
589 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
590 return null;
591 }
592 mStackView.getBoundsOnScreen(mTempRect);
593 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800594 }
595
Mady Mellor390bff42019-04-05 15:09:01 -0700596 /**
597 * The display id of the expanded view, if the stack is expanded and not occluded by the
598 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
599 */
600 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700601 if (mStackView == null) {
602 return INVALID_DISPLAY;
603 }
Mady Mellor390bff42019-04-05 15:09:01 -0700604 boolean defaultDisplay = context.getDisplay() != null
605 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
606 Bubble b = mStackView.getExpandedBubble();
607 if (defaultDisplay && b != null && isStackExpanded()
608 && !mStatusBarWindowController.getPanelExpanded()) {
609 return b.expandedView.getVirtualDisplayId();
610 }
611 return INVALID_DISPLAY;
612 }
613
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800614 @VisibleForTesting
615 BubbleStackView getStackView() {
616 return mStackView;
617 }
618
Mady Mellor5549dd22018-11-06 18:07:34 -0800619 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800620 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800621 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800622 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800623 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800624 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800625 return false;
626 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800627 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800628
629 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
630 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
631 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
632
Mady Mellor5549dd22018-11-06 18:07:34 -0800633 boolean hasRemoteInput = false;
634 if (n.getNotification().actions != null) {
635 for (Notification.Action action : n.getNotification().actions) {
636 if (action.getRemoteInputs() != null) {
637 hasRemoteInput = true;
638 break;
639 }
640 }
641 }
Mady Mellor711f9562018-12-05 14:53:46 -0800642 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
643 && n.isOngoing();
644 boolean isMusic = n.getNotification().hasMediaSession();
645 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800646
Mady Mellor5549dd22018-11-06 18:07:34 -0800647 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800648 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
649 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800650 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800651 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800652 || autoBubbleAll;
653 }
654
Mady Mellore80930e2019-03-21 16:00:45 -0700655 private boolean shouldAutoExpand(NotificationEntry entry) {
656 Notification.BubbleMetadata metadata = entry.getBubbleMetadata();
657 return metadata != null && metadata.getAutoExpandBubble()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400658 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellore80930e2019-03-21 16:00:45 -0700659 }
660
Mady Mellor8a1f0252019-04-01 11:31:34 -0700661 private void updateShowInShadeForSuppressNotification(NotificationEntry entry) {
662 boolean suppressNotification = entry.getBubbleMetadata() != null
Mady Mellorc529d6d2019-04-16 14:22:52 -0700663 && entry.getBubbleMetadata().isNotificationSuppressed()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400664 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellor8a1f0252019-04-01 11:31:34 -0700665 entry.setShowInShadeWhenBubble(!suppressNotification);
666 }
667
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400668 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
669 StringBuilder sb = new StringBuilder();
670 for (Bubble bubble : bubbles) {
671 if (bubble == null) {
672 sb.append(" <null> !!!!!\n");
673 } else {
674 boolean isSelected = (bubble == selected);
675 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
676 ((isSelected) ? "->" : " "),
677 bubble.getLastActivity(),
678 (bubble.isOngoing() ? 1 : 0),
679 bubble.getKey()));
680 }
681 }
682 return sb.toString();
683 }
684
Mady Mellore80930e2019-03-21 16:00:45 -0700685 /**
686 * Return true if the applications with the package name is running in foreground.
687 *
Steven Wu8ba8ca92019-04-11 10:47:42 -0400688 * @param context application context.
Mady Mellore80930e2019-03-21 16:00:45 -0700689 * @param pkgName application package name.
690 */
Steven Wu8ba8ca92019-04-11 10:47:42 -0400691 public static boolean isForegroundApp(Context context, String pkgName) {
692 ActivityManager am = context.getSystemService(ActivityManager.class);
Mady Mellore80930e2019-03-21 16:00:45 -0700693 List<RunningTaskInfo> tasks = am.getRunningTasks(1 /* maxNum */);
694 return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
695 }
696
Mark Renoufcecc77b2019-01-30 16:32:24 -0500697 /**
698 * This task stack listener is responsible for responding to tasks moved to the front
699 * which are on the default (main) display. When this happens, expanded bubbles must be
700 * collapsed so the user may interact with the app which was just moved to the front.
701 * <p>
702 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
703 * these calls via a main thread Handler.
704 */
705 @MainThread
706 private class BubbleTaskStackListener extends TaskStackChangeListener {
707
Mark Renoufcecc77b2019-01-30 16:32:24 -0500708 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500709 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
710 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400711 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500712 }
713 }
714
Mark Renoufcecc77b2019-01-30 16:32:24 -0500715 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500716 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500717 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400718 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500719 }
720 }
721 }
722
Mady Mellorceced172018-11-27 11:18:39 -0800723 private static boolean shouldAutoBubbleMessages(Context context) {
724 return Settings.Secure.getInt(context.getContentResolver(),
725 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
726 }
727
728 private static boolean shouldAutoBubbleOngoing(Context context) {
729 return Settings.Secure.getInt(context.getContentResolver(),
730 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
731 }
732
733 private static boolean shouldAutoBubbleAll(Context context) {
734 return Settings.Secure.getInt(context.getContentResolver(),
735 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800736 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500737
Mady Mellor3dff9e62019-02-05 18:12:53 -0800738 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500739 return Settings.Secure.getInt(context.getContentResolver(),
740 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
741 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800742
743 private static boolean areBubblesEnabled(Context context) {
744 return Settings.Secure.getInt(context.getContentResolver(),
745 ENABLE_BUBBLES, 1) != 0;
746 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500747
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500748 /** Default stiffness to use for bubble physics animations. */
749 public static int getBubbleStiffness(Context context, int defaultStiffness) {
750 return Settings.Secure.getInt(
751 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
752 }
753
754 /** Default bounciness/damping ratio to use for bubble physics animations. */
755 public static float getBubbleBounciness(Context context, float defaultBounciness) {
756 return Settings.Secure.getInt(
757 context.getContentResolver(),
758 BUBBLE_BOUNCINESS,
759 (int) (defaultBounciness * 100)) / 100f;
760 }
761
Joshua Tsujia19515f2019-02-13 18:02:29 -0500762 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
763 private class BubblesImeListener extends IPinnedStackListener.Stub {
764
765 @Override
766 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
767 }
768
769 @Override
770 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
771 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
772 int displayRotation) throws RemoteException {}
773
774 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500775 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
776 if (mStackView != null && mStackView.getBubbleCount() > 0) {
777 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500778 }
779 }
780
781 @Override
782 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
783 throws RemoteException {}
784
785 @Override
786 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
787
788 @Override
789 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
790 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800791}