blob: 13bdb99e7175ab2f7d353c081b57e7557687dcdc [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 Mellorb4991e62019-01-10 15:14:51 -080035import android.app.INotificationManager;
Mady Mellor5549dd22018-11-06 18:07:34 -080036import android.app.Notification;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080037import android.content.Context;
Joshua Tsujia19515f2019-02-13 18:02:29 -050038import android.content.pm.ParceledListSlice;
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 Renoufcecc77b2019-01-30 16:32:24 -050044import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050045import android.view.IPinnedStackController;
46import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080047import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080048import android.widget.FrameLayout;
49
Mark Renouf08bc42a2019-03-07 13:01:59 -050050import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050051import androidx.annotation.MainThread;
52
Mady Mellorebdbbb92018-11-15 14:36:48 -080053import com.android.internal.annotations.VisibleForTesting;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080054import com.android.internal.statusbar.NotificationVisibility;
Ned Burns01e38212019-01-03 16:32:52 -050055import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080056import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050057import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050058import com.android.systemui.shared.system.ActivityManagerWrapper;
59import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050060import com.android.systemui.shared.system.WindowManagerWrapper;
Ned Burns01e38212019-01-03 16:32:52 -050061import com.android.systemui.statusbar.notification.NotificationEntryListener;
62import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080063import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050064import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050065import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080066import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070067import com.android.systemui.statusbar.policy.ConfigurationController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080068
Mark Renouf08bc42a2019-03-07 13:01:59 -050069import java.lang.annotation.Retention;
Mady Mellore80930e2019-03-21 16:00:45 -070070import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050071
Jason Monk27d01a622018-12-10 15:57:09 -050072import javax.inject.Inject;
73import javax.inject.Singleton;
74
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080075/**
76 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
77 * Bubbles can be expanded to show more content.
78 *
79 * The controller manages addition, removal, and visible state of bubbles on screen.
80 */
Jason Monk27d01a622018-12-10 15:57:09 -050081@Singleton
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070082public class BubbleController implements BubbleExpandedView.OnBubbleBlockedListener,
83 ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080084
85 private static final String TAG = "BubbleController";
86
Mark Renouf08bc42a2019-03-07 13:01:59 -050087 @Retention(SOURCE)
88 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
89 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION})
90 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070091
Mark Renouf08bc42a2019-03-07 13:01:59 -050092 static final int DISMISS_USER_GESTURE = 1;
93 static final int DISMISS_AGED = 2;
94 static final int DISMISS_TASK_FINISHED = 3;
95 static final int DISMISS_BLOCKED = 4;
96 static final int DISMISS_NOTIF_CANCEL = 5;
97 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
98
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -040099 static final int MAX_BUBBLES = 5; // TODO: actually enforce this
100
Mady Mellor5549dd22018-11-06 18:07:34 -0800101 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -0500102 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -0800103
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800104 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800105 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800106 /** Auto bubble flags set whether different notif types should be presented as a bubble */
Mady Mellorceced172018-11-27 11:18:39 -0800107 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
108 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
109 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800110
111 /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500112 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -0800113
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500114 private static final String BUBBLE_STIFFNESS = "experiment_bubble_stiffness";
115 private static final String BUBBLE_BOUNCINESS = "experiment_bubble_bounciness";
116
Ned Burns01e38212019-01-03 16:32:52 -0500117 private final Context mContext;
118 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500119 private final IActivityTaskManager mActivityTaskManager;
120 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800121 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800122 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100123 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800124
Mady Mellor3dff9e62019-02-05 18:12:53 -0800125 private BubbleData mBubbleData;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800126 private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800127
128 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500129 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800130 private StatusBarStateListener mStatusBarStateListener;
131
132 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider =
133 Dependency.get(NotificationInterruptionStateProvider.class);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800134
Mady Mellorb4991e62019-01-10 15:14:51 -0800135 private INotificationManager mNotificationManagerService;
136
Mady Mellord1c78b262018-11-06 18:04:40 -0800137 // Used for determining view rect for touch interaction
138 private Rect mTempRect = new Rect();
139
Mady Mellor5549dd22018-11-06 18:07:34 -0800140 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800141 * Listener to be notified when some states of the bubbles change.
142 */
143 public interface BubbleStateChangeListener {
144 /**
145 * Called when the stack has bubbles or no longer has bubbles.
146 */
147 void onHasBubblesChanged(boolean hasBubbles);
148 }
149
Mady Mellorcd9b1302018-11-06 18:08:04 -0800150 /**
151 * Listener to find out about stack expansion / collapse events.
152 */
153 public interface BubbleExpandListener {
154 /**
155 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700156 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800157 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800158 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800159 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800160 void onBubbleExpandChanged(boolean isExpanding, String key);
161 }
162
163 /**
164 * Listens for the current state of the status bar and updates the visibility state
165 * of bubbles as needed.
166 */
167 private class StatusBarStateListener implements StatusBarStateController.StateListener {
168 private int mState;
169 /**
170 * Returns the current status bar state.
171 */
172 public int getCurrentState() {
173 return mState;
174 }
175
176 @Override
177 public void onStateChanged(int newState) {
178 mState = newState;
179 updateVisibility();
180 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800181 }
182
Jason Monk27d01a622018-12-10 15:57:09 -0500183 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800184 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700185 BubbleData data, ConfigurationController configurationController) {
186 this(context, statusBarWindowController, data, null /* synchronizer */,
187 configurationController);
Issei Suzukic0387542019-03-08 17:31:14 +0100188 }
189
190 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700191 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
192 ConfigurationController configurationController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800193 mContext = context;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700194 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800195
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800196 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500197 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800198
199 try {
200 mNotificationManagerService = INotificationManager.Stub.asInterface(
201 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
202 } catch (ServiceManager.ServiceNotFoundException e) {
203 e.printStackTrace();
204 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800205
206 mStatusBarWindowController = statusBarWindowController;
207 mStatusBarStateListener = new StatusBarStateListener();
208 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500209
210 mActivityTaskManager = ActivityTaskManager.getService();
211 mTaskStackListener = new BubbleTaskStackListener();
212 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800213
Joshua Tsujia19515f2019-02-13 18:02:29 -0500214 try {
215 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
216 } catch (RemoteException e) {
217 e.printStackTrace();
218 }
219
Mady Mellorcfd06c12019-02-13 14:32:12 -0800220 mBubbleData = data;
Mark Renouf3bc5b362019-04-05 14:37:59 -0400221 mBubbleData.setListener(mBubbleDataListener);
Issei Suzukic0387542019-03-08 17:31:14 +0100222 mSurfaceSynchronizer = synchronizer;
Mady Mellor5549dd22018-11-06 18:07:34 -0800223 }
224
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400225 /**
226 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
227 * method initializes the stack view and adds it to the StatusBar just above the scrim.
228 */
229 private void ensureStackViewCreated() {
230 if (mStackView == null) {
231 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
232 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
233 // TODO(b/130237686): When you expand the shade on top of expanded bubble, there is no
234 // scrim between bubble and the shade
235 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
236 sbv.addView(mStackView, bubblePosition,
237 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
238 if (mExpandListener != null) {
239 mStackView.setExpandListener(mExpandListener);
240 }
241 mStackView.setOnBlockedListener(this);
242 }
243 }
244
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700245 @Override
246 public void onUiModeChanged() {
247 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700248 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700249 }
250 }
251
252 @Override
253 public void onOverlayChanged() {
254 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700255 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700256 }
257 }
258
Mady Mellor5549dd22018-11-06 18:07:34 -0800259 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800260 * Set a listener to be notified when some states of the bubbles change.
261 */
262 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
263 mStateChangeListener = listener;
264 }
265
266 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800267 * Set a listener to be notified of bubble expand events.
268 */
269 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100270 mExpandListener = ((isExpanding, key) -> {
271 if (listener != null) {
272 listener.onBubbleExpandChanged(isExpanding, key);
273 }
274 mStatusBarWindowController.setBubbleExpanded(isExpanding);
275 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800276 if (mStackView != null) {
277 mStackView.setExpandListener(mExpandListener);
278 }
279 }
280
281 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800282 * Whether or not there are bubbles present, regardless of them being visible on the
283 * screen (e.g. if on AOD).
284 */
285 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800286 if (mStackView == null) {
287 return false;
288 }
289 for (Bubble bubble : mBubbleData.getBubbles()) {
290 if (!bubble.entry.isBubbleDismissed()) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800291 return true;
292 }
293 }
294 return false;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800295 }
296
297 /**
298 * Whether the stack of bubbles is expanded or not.
299 */
300 public boolean isStackExpanded() {
301 return mStackView != null && mStackView.isExpanded();
302 }
303
304 /**
305 * Tell the stack of bubbles to collapse.
306 */
307 public void collapseStack() {
308 if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800309 mStackView.collapseStack();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800310 }
311 }
312
313 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400314 * Request the stack expand if needed, then select the specified Bubble as current.
315 *
316 * @param notificationKey the notification key for the bubble to be selected
317 */
318 public void expandStackAndSelectBubble(String notificationKey) {
319 if (mStackView != null && mBubbleData.getBubble(notificationKey) != null) {
320 mStackView.setExpandedBubble(notificationKey);
321 }
322 }
323
324 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800325 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
326 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500327 void dismissStack(@DismissReason int reason) {
Mady Mellord1c78b262018-11-06 18:04:40 -0800328 if (mStackView == null) {
329 return;
330 }
Mark Renouf08bc42a2019-03-07 13:01:59 -0500331 mStackView.stackDismissed(reason);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800332
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800333 updateVisibility();
Ned Burns01e38212019-01-03 16:32:52 -0500334 mNotificationEntryManager.updateNotifications();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800335 }
336
337 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500338 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
339 * is forwarded a back key down/up pair.
340 */
341 public void performBackPressIfNeeded() {
342 if (mStackView != null) {
343 mStackView.performBackPressIfNeeded();
344 }
345 }
346
347 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800348 * Adds or updates a bubble associated with the provided notification entry.
349 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400350 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800351 */
Mark Renouff97ed462019-04-05 13:46:24 -0400352 void updateBubble(NotificationEntry notif) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800353 if (mStackView != null && mBubbleData.getBubble(notif.key) != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800354 // It's an update
Mark Renouff97ed462019-04-05 13:46:24 -0400355 mStackView.updateBubble(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800356 } else {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800357 // It's new
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400358 ensureStackViewCreated();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800359 mStackView.addBubble(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800360 }
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400361 Bubble bubble = mBubbleData.getBubble(notif.key);
Mady Mellore80930e2019-03-21 16:00:45 -0700362 if (shouldAutoExpand(notif)) {
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400363 mStackView.setSelectedBubble(bubble);
364 mStackView.setExpanded(true);
Mady Mellore80930e2019-03-21 16:00:45 -0700365 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800366 updateVisibility();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800367 }
368
369 /**
370 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500371 * <p>
372 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800373 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500374 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500375 void removeBubble(String key, int reason) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800376 if (mStackView != null) {
Mark Renouf08bc42a2019-03-07 13:01:59 -0500377 mStackView.removeBubble(key, reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800378 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800379 mNotificationEntryManager.updateNotifications();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800380 updateVisibility();
Mady Mellord1c78b262018-11-06 18:04:40 -0800381 }
382
Mady Mellore8e07712019-01-23 12:45:33 -0800383 @Override
384 public void onBubbleBlocked(NotificationEntry entry) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800385 Object[] bubbles = mBubbleData.getBubbles().toArray();
Mady Mellore8e07712019-01-23 12:45:33 -0800386 for (int i = 0; i < bubbles.length; i++) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800387 NotificationEntry e = ((Bubble) bubbles[i]).entry;
Mady Mellore8e07712019-01-23 12:45:33 -0800388 boolean samePackage = entry.notification.getPackageName().equals(
389 e.notification.getPackageName());
390 if (samePackage) {
Mark Renouf08bc42a2019-03-07 13:01:59 -0500391 removeBubble(entry.key, DISMISS_BLOCKED);
Mady Mellore8e07712019-01-23 12:45:33 -0800392 }
393 }
394 }
395
Ned Burns01e38212019-01-03 16:32:52 -0500396 @SuppressWarnings("FieldCanBeLocal")
397 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
398 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500399 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800400 if (!areBubblesEnabled(mContext)) {
401 return;
402 }
403 if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700404 // TODO: handle group summaries?
405 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800406 }
407 }
408
409 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500410 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800411 if (!areBubblesEnabled(mContext)) {
412 return;
413 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800414 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400415 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800416 }
417 }
418
419 @Override
420 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800421 if (!areBubblesEnabled(mContext)) {
422 return;
423 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800424 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
425 && alertAgain(entry, entry.notification.getNotification())) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700426 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800427 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mark Renouff97ed462019-04-05 13:46:24 -0400428 updateBubble(entry);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800429 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800430 }
431 }
432
433 @Override
434 public void onEntryRemoved(NotificationEntry entry,
435 @Nullable NotificationVisibility visibility,
436 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800437 if (!areBubblesEnabled(mContext)) {
438 return;
439 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800440 entry.setShowInShadeWhenBubble(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800441 if (mStackView != null) {
442 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800443 }
444 if (!removedByUser) {
445 // This was a cancel so we should remove the bubble
Mark Renouf08bc42a2019-03-07 13:01:59 -0500446 removeBubble(entry.key, DISMISS_NOTIF_CANCEL);
Ned Burns01e38212019-01-03 16:32:52 -0500447 }
448 }
449 };
450
Mark Renouf3bc5b362019-04-05 14:37:59 -0400451 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
452 @Override
453 public void onBubbleAdded(Bubble bubble) {
454
455 }
456
457 @Override
458 public void onBubbleRemoved(Bubble bubble, int reason) {
459
460 }
461
462 public void onBubbleUpdated(Bubble bubble) {
463
464 }
465
466 @Override
467 public void onOrderChanged(List<Bubble> bubbles) {
468
469 }
470
471 @Override
472 public void onSelectionChanged(Bubble selectedBubble) {
473
474 }
475
476 @Override
477 public void onExpandedChanged(boolean expanded) {
478
479 }
480
481 @Override
482 public void showFlyoutText(Bubble bubble, String text) {
483
484 }
485
486 @Override
487 public void apply() {
488
489 }
490 };
491
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800492 /**
493 * Lets any listeners know if bubble state has changed.
494 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800495 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800496 if (mStackView == null) {
497 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800498 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800499
Mady Mellord1c78b262018-11-06 18:04:40 -0800500 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800501 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800502 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800503 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
504 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
505 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800506 }
507
508 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800509 * Updates the visibility of the bubbles based on current state.
510 * Does not un-bubble, just hides or un-hides. Will notify any
511 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800512 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800513 public void updateVisibility() {
514 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
515 // Bubbles only appear in unlocked shade
516 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
517 } else if (mStackView != null) {
518 mStackView.setVisibility(INVISIBLE);
519 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800520 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800521 updateBubblesShowing();
522 }
523
524 /**
525 * Rect indicating the touchable region for the bubble stack / expanded stack.
526 */
527 public Rect getTouchableRegion() {
528 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
529 return null;
530 }
531 mStackView.getBoundsOnScreen(mTempRect);
532 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800533 }
534
Mady Mellor390bff42019-04-05 15:09:01 -0700535 /**
536 * The display id of the expanded view, if the stack is expanded and not occluded by the
537 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
538 */
539 public int getExpandedDisplayId(Context context) {
540 boolean defaultDisplay = context.getDisplay() != null
541 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
542 Bubble b = mStackView.getExpandedBubble();
543 if (defaultDisplay && b != null && isStackExpanded()
544 && !mStatusBarWindowController.getPanelExpanded()) {
545 return b.expandedView.getVirtualDisplayId();
546 }
547 return INVALID_DISPLAY;
548 }
549
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800550 @VisibleForTesting
551 BubbleStackView getStackView() {
552 return mStackView;
553 }
554
Mady Mellor5549dd22018-11-06 18:07:34 -0800555 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800556 * Whether the notification has been developer configured to bubble and is allowed by the user.
557 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800558 @VisibleForTesting
559 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800560 StatusBarNotification n = entry.notification;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800561 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
562 && n.getNotification().getBubbleMetadata().getIntent() != null;
Julia Reynolds4509ce72019-01-31 13:12:43 -0500563 return hasOverlayIntent && entry.canBubble;
Mady Mellorb4991e62019-01-10 15:14:51 -0800564 }
565
566 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800567 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800568 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800569 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800570 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800571 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800572 return false;
573 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800574 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800575
576 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
577 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
578 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
579
Mady Mellor5549dd22018-11-06 18:07:34 -0800580 boolean hasRemoteInput = false;
581 if (n.getNotification().actions != null) {
582 for (Notification.Action action : n.getNotification().actions) {
583 if (action.getRemoteInputs() != null) {
584 hasRemoteInput = true;
585 break;
586 }
587 }
588 }
Mady Mellor711f9562018-12-05 14:53:46 -0800589 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
590 && n.isOngoing();
591 boolean isMusic = n.getNotification().hasMediaSession();
592 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800593
Mady Mellor5549dd22018-11-06 18:07:34 -0800594 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800595 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
596 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800597 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800598 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800599 || autoBubbleAll;
600 }
601
Mady Mellore80930e2019-03-21 16:00:45 -0700602 private boolean shouldAutoExpand(NotificationEntry entry) {
603 Notification.BubbleMetadata metadata = entry.getBubbleMetadata();
604 return metadata != null && metadata.getAutoExpandBubble()
605 && isForegroundApp(entry.notification.getPackageName());
606 }
607
Mady Mellor8a1f0252019-04-01 11:31:34 -0700608 private void updateShowInShadeForSuppressNotification(NotificationEntry entry) {
609 boolean suppressNotification = entry.getBubbleMetadata() != null
610 && entry.getBubbleMetadata().getSuppressNotification()
611 && isForegroundApp(entry.notification.getPackageName());
612 entry.setShowInShadeWhenBubble(!suppressNotification);
613 }
614
Mady Mellore80930e2019-03-21 16:00:45 -0700615 /**
616 * Return true if the applications with the package name is running in foreground.
617 *
618 * @param pkgName application package name.
619 */
620 private boolean isForegroundApp(String pkgName) {
621 ActivityManager am = mContext.getSystemService(ActivityManager.class);
622 List<RunningTaskInfo> tasks = am.getRunningTasks(1 /* maxNum */);
623 return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
624 }
625
Mark Renoufcecc77b2019-01-30 16:32:24 -0500626 /**
627 * This task stack listener is responsible for responding to tasks moved to the front
628 * which are on the default (main) display. When this happens, expanded bubbles must be
629 * collapsed so the user may interact with the app which was just moved to the front.
630 * <p>
631 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
632 * these calls via a main thread Handler.
633 */
634 @MainThread
635 private class BubbleTaskStackListener extends TaskStackChangeListener {
636
Mark Renoufcecc77b2019-01-30 16:32:24 -0500637 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500638 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
639 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500640 mStackView.collapseStack();
641 }
642 }
643
Mark Renoufcecc77b2019-01-30 16:32:24 -0500644 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500645 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500646 if (mStackView != null) {
647 mStackView.collapseStack();
648 }
649 }
650 }
651
Mady Mellorceced172018-11-27 11:18:39 -0800652 private static boolean shouldAutoBubbleMessages(Context context) {
653 return Settings.Secure.getInt(context.getContentResolver(),
654 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
655 }
656
657 private static boolean shouldAutoBubbleOngoing(Context context) {
658 return Settings.Secure.getInt(context.getContentResolver(),
659 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
660 }
661
662 private static boolean shouldAutoBubbleAll(Context context) {
663 return Settings.Secure.getInt(context.getContentResolver(),
664 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800665 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500666
Mady Mellor3dff9e62019-02-05 18:12:53 -0800667 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500668 return Settings.Secure.getInt(context.getContentResolver(),
669 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
670 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800671
672 private static boolean areBubblesEnabled(Context context) {
673 return Settings.Secure.getInt(context.getContentResolver(),
674 ENABLE_BUBBLES, 1) != 0;
675 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500676
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500677 /** Default stiffness to use for bubble physics animations. */
678 public static int getBubbleStiffness(Context context, int defaultStiffness) {
679 return Settings.Secure.getInt(
680 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
681 }
682
683 /** Default bounciness/damping ratio to use for bubble physics animations. */
684 public static float getBubbleBounciness(Context context, float defaultBounciness) {
685 return Settings.Secure.getInt(
686 context.getContentResolver(),
687 BUBBLE_BOUNCINESS,
688 (int) (defaultBounciness * 100)) / 100f;
689 }
690
Joshua Tsujia19515f2019-02-13 18:02:29 -0500691 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
692 private class BubblesImeListener extends IPinnedStackListener.Stub {
693
694 @Override
695 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
696 }
697
698 @Override
699 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
700 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
701 int displayRotation) throws RemoteException {}
702
703 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500704 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
705 if (mStackView != null && mStackView.getBubbleCount() > 0) {
706 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500707 }
708 }
709
710 @Override
711 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
712 throws RemoteException {}
713
714 @Override
715 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
716
717 @Override
718 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
719 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800720}