blob: d0713636c540aa0ee30b1ddc5a2203701d146de6 [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 Mellor390bff42019-04-05 15:09:01 -070019import static android.view.Display.DEFAULT_DISPLAY;
20import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080021import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080022import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080023import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080024
Mady Mellor3f2efdb2018-11-21 11:30:45 -080025import static com.android.systemui.statusbar.StatusBarState.SHADE;
26import static com.android.systemui.statusbar.notification.NotificationAlertingManager.alertAgain;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080027
Mark Renouf08bc42a2019-03-07 13:01:59 -050028import static java.lang.annotation.RetentionPolicy.SOURCE;
29
Mady Mellorb4991e62019-01-10 15:14:51 -080030import android.annotation.Nullable;
Mady Mellore80930e2019-03-21 16:00:45 -070031import android.app.ActivityManager;
Mark Renoufc808f062019-02-07 15:20:37 -050032import android.app.ActivityManager.RunningTaskInfo;
Mark Renoufcecc77b2019-01-30 16:32:24 -050033import android.app.ActivityTaskManager;
34import android.app.IActivityTaskManager;
Mady Mellor5549dd22018-11-06 18:07:34 -080035import android.app.Notification;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080036import android.content.Context;
Joshua Tsujia19515f2019-02-13 18:02:29 -050037import android.content.pm.ParceledListSlice;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040038import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080039import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050040import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080041import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080042import android.provider.Settings;
Mady Mellor5549dd22018-11-06 18:07:34 -080043import android.service.notification.StatusBarNotification;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040044import android.util.Log;
Mark Renoufcecc77b2019-01-30 16:32:24 -050045import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050046import android.view.IPinnedStackController;
47import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080048import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080049import android.widget.FrameLayout;
50
Mark Renouf08bc42a2019-03-07 13:01:59 -050051import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050052import androidx.annotation.MainThread;
53
Mady Mellorebdbbb92018-11-15 14:36:48 -080054import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070055import com.android.internal.statusbar.IStatusBarService;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080056import com.android.internal.statusbar.NotificationVisibility;
Ned Burns01e38212019-01-03 16:32:52 -050057import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080058import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050059import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050060import com.android.systemui.shared.system.ActivityManagerWrapper;
61import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050062import com.android.systemui.shared.system.WindowManagerWrapper;
Ned Burns01e38212019-01-03 16:32:52 -050063import com.android.systemui.statusbar.notification.NotificationEntryListener;
64import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080065import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050066import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050067import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080068import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070069import com.android.systemui.statusbar.policy.ConfigurationController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080070
Mark Renouf08bc42a2019-03-07 13:01:59 -050071import java.lang.annotation.Retention;
Mady Mellore80930e2019-03-21 16:00:45 -070072import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050073
Jason Monk27d01a622018-12-10 15:57:09 -050074import javax.inject.Inject;
75import javax.inject.Singleton;
76
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080077/**
78 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
79 * Bubbles can be expanded to show more content.
80 *
81 * The controller manages addition, removal, and visible state of bubbles on screen.
82 */
Jason Monk27d01a622018-12-10 15:57:09 -050083@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -040084public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080085
86 private static final String TAG = "BubbleController";
Mark Renouf9ba6cea2019-04-17 11:53:50 -040087 private static final boolean DEBUG = true;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080088
Mark Renouf08bc42a2019-03-07 13:01:59 -050089 @Retention(SOURCE)
90 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mady Melloraa8fef22019-04-11 13:36:40 -070091 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE})
Mark Renouf08bc42a2019-03-07 13:01:59 -050092 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070093
Mark Renouf08bc42a2019-03-07 13:01:59 -050094 static final int DISMISS_USER_GESTURE = 1;
95 static final int DISMISS_AGED = 2;
96 static final int DISMISS_TASK_FINISHED = 3;
97 static final int DISMISS_BLOCKED = 4;
98 static final int DISMISS_NOTIF_CANCEL = 5;
99 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700100 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500101
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -0400102 static final int MAX_BUBBLES = 5; // TODO: actually enforce this
103
Mady Mellor5549dd22018-11-06 18:07:34 -0800104 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -0500105 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -0800106
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800107 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800108 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800109 /** Auto bubble flags set whether different notif types should be presented as a bubble */
Mady Mellorceced172018-11-27 11:18:39 -0800110 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
111 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
112 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800113
114 /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500115 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -0800116
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500117 private static final String BUBBLE_STIFFNESS = "experiment_bubble_stiffness";
118 private static final String BUBBLE_BOUNCINESS = "experiment_bubble_bounciness";
119
Ned Burns01e38212019-01-03 16:32:52 -0500120 private final Context mContext;
121 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500122 private final IActivityTaskManager mActivityTaskManager;
123 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800124 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800125 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100126 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800127
Mady Mellor3dff9e62019-02-05 18:12:53 -0800128 private BubbleData mBubbleData;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800129 private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800130
131 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500132 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800133 private StatusBarStateListener mStatusBarStateListener;
134
Mady Melloraa8fef22019-04-11 13:36:40 -0700135 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700136 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800137
Mady Mellord1c78b262018-11-06 18:04:40 -0800138 // Used for determining view rect for touch interaction
139 private Rect mTempRect = new Rect();
140
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400141 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
142 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
143
Mady Mellor5549dd22018-11-06 18:07:34 -0800144 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800145 * Listener to be notified when some states of the bubbles change.
146 */
147 public interface BubbleStateChangeListener {
148 /**
149 * Called when the stack has bubbles or no longer has bubbles.
150 */
151 void onHasBubblesChanged(boolean hasBubbles);
152 }
153
Mady Mellorcd9b1302018-11-06 18:08:04 -0800154 /**
155 * Listener to find out about stack expansion / collapse events.
156 */
157 public interface BubbleExpandListener {
158 /**
159 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700160 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800161 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800162 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800163 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800164 void onBubbleExpandChanged(boolean isExpanding, String key);
165 }
166
167 /**
168 * Listens for the current state of the status bar and updates the visibility state
169 * of bubbles as needed.
170 */
171 private class StatusBarStateListener implements StatusBarStateController.StateListener {
172 private int mState;
173 /**
174 * Returns the current status bar state.
175 */
176 public int getCurrentState() {
177 return mState;
178 }
179
180 @Override
181 public void onStateChanged(int newState) {
182 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400183 boolean shouldCollapse = (mState != SHADE);
184 if (shouldCollapse) {
185 collapseStack();
186 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800187 updateVisibility();
188 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800189 }
190
Jason Monk27d01a622018-12-10 15:57:09 -0500191 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800192 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Mady Melloraa8fef22019-04-11 13:36:40 -0700193 BubbleData data, ConfigurationController configurationController,
194 NotificationInterruptionStateProvider interruptionStateProvider) {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700195 this(context, statusBarWindowController, data, null /* synchronizer */,
Mady Melloraa8fef22019-04-11 13:36:40 -0700196 configurationController, interruptionStateProvider);
Issei Suzukic0387542019-03-08 17:31:14 +0100197 }
198
199 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700200 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
Mady Melloraa8fef22019-04-11 13:36:40 -0700201 ConfigurationController configurationController,
202 NotificationInterruptionStateProvider interruptionStateProvider) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800203 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700204 mNotificationInterruptionStateProvider = interruptionStateProvider;
205
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700206 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800207
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400208 mBubbleData = data;
209 mBubbleData.setListener(mBubbleDataListener);
210
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800211 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500212 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800213
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800214 mStatusBarWindowController = statusBarWindowController;
215 mStatusBarStateListener = new StatusBarStateListener();
216 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500217
218 mActivityTaskManager = ActivityTaskManager.getService();
219 mTaskStackListener = new BubbleTaskStackListener();
220 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800221
Joshua Tsujia19515f2019-02-13 18:02:29 -0500222 try {
223 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
224 } catch (RemoteException e) {
225 e.printStackTrace();
226 }
Issei Suzukic0387542019-03-08 17:31:14 +0100227 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700228
229 mBarService = IStatusBarService.Stub.asInterface(
230 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mady Mellor5549dd22018-11-06 18:07:34 -0800231 }
232
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400233 /**
234 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
235 * method initializes the stack view and adds it to the StatusBar just above the scrim.
236 */
237 private void ensureStackViewCreated() {
238 if (mStackView == null) {
239 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
240 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
241 // TODO(b/130237686): When you expand the shade on top of expanded bubble, there is no
242 // scrim between bubble and the shade
243 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
244 sbv.addView(mStackView, bubblePosition,
245 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
246 if (mExpandListener != null) {
247 mStackView.setExpandListener(mExpandListener);
248 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400249 }
250 }
251
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700252 @Override
253 public void onUiModeChanged() {
254 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700255 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700256 }
257 }
258
259 @Override
260 public void onOverlayChanged() {
261 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700262 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700263 }
264 }
265
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400266 @Override
267 public void onConfigChanged(Configuration newConfig) {
268 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
269 mStackView.onOrientationChanged();
270 mOrientation = newConfig.orientation;
271 }
272 }
273
Mady Mellor5549dd22018-11-06 18:07:34 -0800274 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800275 * Set a listener to be notified when some states of the bubbles change.
276 */
277 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
278 mStateChangeListener = listener;
279 }
280
281 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800282 * Set a listener to be notified of bubble expand events.
283 */
284 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100285 mExpandListener = ((isExpanding, key) -> {
286 if (listener != null) {
287 listener.onBubbleExpandChanged(isExpanding, key);
288 }
289 mStatusBarWindowController.setBubbleExpanded(isExpanding);
290 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800291 if (mStackView != null) {
292 mStackView.setExpandListener(mExpandListener);
293 }
294 }
295
296 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800297 * Whether or not there are bubbles present, regardless of them being visible on the
298 * screen (e.g. if on AOD).
299 */
300 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800301 if (mStackView == null) {
302 return false;
303 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400304 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800305 }
306
307 /**
308 * Whether the stack of bubbles is expanded or not.
309 */
310 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400311 return mBubbleData.isExpanded();
312 }
313
314 /**
315 * Tell the stack of bubbles to expand.
316 */
317 public void expandStack() {
318 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800319 }
320
321 /**
322 * Tell the stack of bubbles to collapse.
323 */
324 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400325 mBubbleData.setExpanded(false /* expanded */);
326 }
327
328 void selectBubble(Bubble bubble) {
329 mBubbleData.setSelectedBubble(bubble);
330 }
331
332 @VisibleForTesting
333 void selectBubble(String key) {
334 Bubble bubble = mBubbleData.getBubbleWithKey(key);
335 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800336 }
337
338 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400339 * Request the stack expand if needed, then select the specified Bubble as current.
340 *
341 * @param notificationKey the notification key for the bubble to be selected
342 */
343 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400344 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
345 if (bubble != null) {
346 mBubbleData.setSelectedBubble(bubble);
347 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400348 }
349 }
350
351 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800352 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
353 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500354 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400355 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800356 }
357
358 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500359 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
360 * is forwarded a back key down/up pair.
361 */
362 public void performBackPressIfNeeded() {
363 if (mStackView != null) {
364 mStackView.performBackPressIfNeeded();
365 }
366 }
367
368 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800369 * Adds or updates a bubble associated with the provided notification entry.
370 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400371 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800372 */
Mark Renouff97ed462019-04-05 13:46:24 -0400373 void updateBubble(NotificationEntry notif) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400374 mBubbleData.notificationEntryUpdated(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800375 }
376
377 /**
378 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500379 * <p>
380 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800381 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500382 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500383 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400384 // TEMP: refactor to change this to pass entry
385 Bubble bubble = mBubbleData.getBubbleWithKey(key);
386 if (bubble != null) {
387 mBubbleData.notificationEntryRemoved(bubble.entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800388 }
389 }
390
Ned Burns01e38212019-01-03 16:32:52 -0500391 @SuppressWarnings("FieldCanBeLocal")
392 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
393 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500394 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800395 if (!areBubblesEnabled(mContext)) {
396 return;
397 }
Mady Melloraa8fef22019-04-11 13:36:40 -0700398 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700399 // TODO: handle group summaries?
400 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800401 }
402 }
403
404 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500405 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800406 if (!areBubblesEnabled(mContext)) {
407 return;
408 }
Mady Melloraa8fef22019-04-11 13:36:40 -0700409 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400410 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800411 }
412 }
413
414 @Override
415 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800416 if (!areBubblesEnabled(mContext)) {
417 return;
418 }
Mady Melloraa8fef22019-04-11 13:36:40 -0700419 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry);
420 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.key)) {
421 // It was previously a bubble but no longer a bubble -- lets remove it
422 removeBubble(entry.key, DISMISS_NO_LONGER_BUBBLE);
423 } else if (shouldBubble && alertAgain(entry, entry.notification.getNotification())) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700424 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800425 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mark Renouff97ed462019-04-05 13:46:24 -0400426 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800427 }
428 }
429
430 @Override
431 public void onEntryRemoved(NotificationEntry entry,
432 @Nullable NotificationVisibility visibility,
433 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800434 if (!areBubblesEnabled(mContext)) {
435 return;
436 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800437 entry.setShowInShadeWhenBubble(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800438 if (mStackView != null) {
439 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800440 }
441 if (!removedByUser) {
442 // This was a cancel so we should remove the bubble
Mark Renouf08bc42a2019-03-07 13:01:59 -0500443 removeBubble(entry.key, DISMISS_NOTIF_CANCEL);
Ned Burns01e38212019-01-03 16:32:52 -0500444 }
445 }
446 };
447
Mark Renouf71a3af62019-04-08 15:02:54 -0400448 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400449 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400450
Mark Renouf3bc5b362019-04-05 14:37:59 -0400451 @Override
452 public void onBubbleAdded(Bubble bubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400453 ensureStackViewCreated();
454 mStackView.addBubble(bubble);
Mark Renouf3bc5b362019-04-05 14:37:59 -0400455 }
456
457 @Override
458 public void onBubbleRemoved(Bubble bubble, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400459 if (mStackView != null) {
460 mStackView.removeBubble(bubble);
461 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700462 if (!bubble.entry.showInShadeWhenBubble()) {
463 // The notification is gone & bubble is gone, time to actually remove it
464 mNotificationEntryManager.performRemoveNotification(bubble.entry.notification);
465 } else {
466 // The notification is still in the shade but we've removed the bubble so
467 // lets make sure NoMan knows it's not a bubble anymore
468 try {
469 mBarService.onNotificationBubbleChanged(bubble.getKey(), false /* isBubble */);
470 } catch (RemoteException e) {
471 // Bad things have happened
472 }
473 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400474 }
475
476 public void onBubbleUpdated(Bubble bubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400477 if (mStackView != null) {
478 mStackView.updateBubble(bubble);
479 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400480 }
481
482 @Override
483 public void onOrderChanged(List<Bubble> bubbles) {
Mark Renouf3bc5b362019-04-05 14:37:59 -0400484 }
485
486 @Override
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400487 public void onSelectionChanged(@Nullable Bubble selectedBubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400488 if (mStackView != null) {
489 mStackView.setSelectedBubble(selectedBubble);
490 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400491 }
492
493 @Override
494 public void onExpandedChanged(boolean expanded) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400495 if (mStackView != null) {
496 mStackView.setExpanded(expanded);
497 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400498 }
499
500 @Override
501 public void showFlyoutText(Bubble bubble, String text) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400502 if (mStackView != null) {
503 mStackView.animateInFlyoutForBubble(bubble);
504 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400505 }
506
507 @Override
508 public void apply() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400509 mNotificationEntryManager.updateNotifications();
510 updateVisibility();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400511
512 if (DEBUG) {
513 Log.d(TAG, "[BubbleData]");
514 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
515 mBubbleData.getSelectedBubble()));
516
517 if (mStackView != null) {
518 Log.d(TAG, "[BubbleStackView]");
519 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
520 mStackView.getExpandedBubble()));
521 }
522 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400523 }
524 };
525
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800526 /**
527 * Lets any listeners know if bubble state has changed.
528 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800529 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800530 if (mStackView == null) {
531 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800532 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800533
Mady Mellord1c78b262018-11-06 18:04:40 -0800534 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800535 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800536 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800537 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
538 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
539 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800540 }
541
542 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800543 * Updates the visibility of the bubbles based on current state.
544 * Does not un-bubble, just hides or un-hides. Will notify any
545 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800546 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800547 public void updateVisibility() {
548 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
549 // Bubbles only appear in unlocked shade
550 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
551 } else if (mStackView != null) {
552 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800553 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800554 updateBubblesShowing();
555 }
556
557 /**
558 * Rect indicating the touchable region for the bubble stack / expanded stack.
559 */
560 public Rect getTouchableRegion() {
561 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
562 return null;
563 }
564 mStackView.getBoundsOnScreen(mTempRect);
565 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800566 }
567
Mady Mellor390bff42019-04-05 15:09:01 -0700568 /**
569 * The display id of the expanded view, if the stack is expanded and not occluded by the
570 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
571 */
572 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700573 if (mStackView == null) {
574 return INVALID_DISPLAY;
575 }
Mady Mellor390bff42019-04-05 15:09:01 -0700576 boolean defaultDisplay = context.getDisplay() != null
577 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
578 Bubble b = mStackView.getExpandedBubble();
579 if (defaultDisplay && b != null && isStackExpanded()
580 && !mStatusBarWindowController.getPanelExpanded()) {
581 return b.expandedView.getVirtualDisplayId();
582 }
583 return INVALID_DISPLAY;
584 }
585
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800586 @VisibleForTesting
587 BubbleStackView getStackView() {
588 return mStackView;
589 }
590
Mady Mellor5549dd22018-11-06 18:07:34 -0800591 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800592 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800593 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800594 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800595 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800596 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800597 return false;
598 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800599 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800600
601 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
602 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
603 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
604
Mady Mellor5549dd22018-11-06 18:07:34 -0800605 boolean hasRemoteInput = false;
606 if (n.getNotification().actions != null) {
607 for (Notification.Action action : n.getNotification().actions) {
608 if (action.getRemoteInputs() != null) {
609 hasRemoteInput = true;
610 break;
611 }
612 }
613 }
Mady Mellor711f9562018-12-05 14:53:46 -0800614 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
615 && n.isOngoing();
616 boolean isMusic = n.getNotification().hasMediaSession();
617 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800618
Mady Mellor5549dd22018-11-06 18:07:34 -0800619 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800620 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
621 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800622 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800623 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800624 || autoBubbleAll;
625 }
626
Mady Mellore80930e2019-03-21 16:00:45 -0700627 private boolean shouldAutoExpand(NotificationEntry entry) {
628 Notification.BubbleMetadata metadata = entry.getBubbleMetadata();
629 return metadata != null && metadata.getAutoExpandBubble()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400630 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellore80930e2019-03-21 16:00:45 -0700631 }
632
Mady Mellor8a1f0252019-04-01 11:31:34 -0700633 private void updateShowInShadeForSuppressNotification(NotificationEntry entry) {
634 boolean suppressNotification = entry.getBubbleMetadata() != null
Mady Mellorc529d6d2019-04-16 14:22:52 -0700635 && entry.getBubbleMetadata().isNotificationSuppressed()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400636 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellor8a1f0252019-04-01 11:31:34 -0700637 entry.setShowInShadeWhenBubble(!suppressNotification);
638 }
639
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400640 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
641 StringBuilder sb = new StringBuilder();
642 for (Bubble bubble : bubbles) {
643 if (bubble == null) {
644 sb.append(" <null> !!!!!\n");
645 } else {
646 boolean isSelected = (bubble == selected);
647 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
648 ((isSelected) ? "->" : " "),
649 bubble.getLastActivity(),
650 (bubble.isOngoing() ? 1 : 0),
651 bubble.getKey()));
652 }
653 }
654 return sb.toString();
655 }
656
Mady Mellore80930e2019-03-21 16:00:45 -0700657 /**
658 * Return true if the applications with the package name is running in foreground.
659 *
Steven Wu8ba8ca92019-04-11 10:47:42 -0400660 * @param context application context.
Mady Mellore80930e2019-03-21 16:00:45 -0700661 * @param pkgName application package name.
662 */
Steven Wu8ba8ca92019-04-11 10:47:42 -0400663 public static boolean isForegroundApp(Context context, String pkgName) {
664 ActivityManager am = context.getSystemService(ActivityManager.class);
Mady Mellore80930e2019-03-21 16:00:45 -0700665 List<RunningTaskInfo> tasks = am.getRunningTasks(1 /* maxNum */);
666 return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
667 }
668
Mark Renoufcecc77b2019-01-30 16:32:24 -0500669 /**
670 * This task stack listener is responsible for responding to tasks moved to the front
671 * which are on the default (main) display. When this happens, expanded bubbles must be
672 * collapsed so the user may interact with the app which was just moved to the front.
673 * <p>
674 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
675 * these calls via a main thread Handler.
676 */
677 @MainThread
678 private class BubbleTaskStackListener extends TaskStackChangeListener {
679
Mark Renoufcecc77b2019-01-30 16:32:24 -0500680 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500681 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
682 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400683 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500684 }
685 }
686
Mark Renoufcecc77b2019-01-30 16:32:24 -0500687 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500688 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500689 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400690 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500691 }
692 }
693 }
694
Mady Mellorceced172018-11-27 11:18:39 -0800695 private static boolean shouldAutoBubbleMessages(Context context) {
696 return Settings.Secure.getInt(context.getContentResolver(),
697 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
698 }
699
700 private static boolean shouldAutoBubbleOngoing(Context context) {
701 return Settings.Secure.getInt(context.getContentResolver(),
702 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
703 }
704
705 private static boolean shouldAutoBubbleAll(Context context) {
706 return Settings.Secure.getInt(context.getContentResolver(),
707 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800708 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500709
Mady Mellor3dff9e62019-02-05 18:12:53 -0800710 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500711 return Settings.Secure.getInt(context.getContentResolver(),
712 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
713 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800714
715 private static boolean areBubblesEnabled(Context context) {
716 return Settings.Secure.getInt(context.getContentResolver(),
717 ENABLE_BUBBLES, 1) != 0;
718 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500719
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500720 /** Default stiffness to use for bubble physics animations. */
721 public static int getBubbleStiffness(Context context, int defaultStiffness) {
722 return Settings.Secure.getInt(
723 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
724 }
725
726 /** Default bounciness/damping ratio to use for bubble physics animations. */
727 public static float getBubbleBounciness(Context context, float defaultBounciness) {
728 return Settings.Secure.getInt(
729 context.getContentResolver(),
730 BUBBLE_BOUNCINESS,
731 (int) (defaultBounciness * 100)) / 100f;
732 }
733
Joshua Tsujia19515f2019-02-13 18:02:29 -0500734 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
735 private class BubblesImeListener extends IPinnedStackListener.Stub {
736
737 @Override
738 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
739 }
740
741 @Override
742 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
743 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
744 int displayRotation) throws RemoteException {}
745
746 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500747 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
748 if (mStackView != null && mStackView.getBubbleCount() > 0) {
749 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500750 }
751 }
752
753 @Override
754 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
755 throws RemoteException {}
756
757 @Override
758 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
759
760 @Override
761 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
762 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800763}