blob: 3aa9f73939acbd1b9a52a3fda9ee0154d9a1a44d [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 Mellord1c78b262018-11-06 18:04:40 -080019import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080020import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080021import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080022
Mady Mellor3f2efdb2018-11-21 11:30:45 -080023import static com.android.systemui.statusbar.StatusBarState.SHADE;
24import static com.android.systemui.statusbar.notification.NotificationAlertingManager.alertAgain;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080025
Mark Renouf08bc42a2019-03-07 13:01:59 -050026import static java.lang.annotation.RetentionPolicy.SOURCE;
27
Mady Mellorb4991e62019-01-10 15:14:51 -080028import android.annotation.Nullable;
Mady Mellore80930e2019-03-21 16:00:45 -070029import android.app.ActivityManager;
Mark Renoufc808f062019-02-07 15:20:37 -050030import android.app.ActivityManager.RunningTaskInfo;
Mark Renoufcecc77b2019-01-30 16:32:24 -050031import android.app.ActivityTaskManager;
32import android.app.IActivityTaskManager;
Mady Mellorb4991e62019-01-10 15:14:51 -080033import android.app.INotificationManager;
Mady Mellor5549dd22018-11-06 18:07:34 -080034import android.app.Notification;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080035import android.content.Context;
Joshua Tsujia19515f2019-02-13 18:02:29 -050036import android.content.pm.ParceledListSlice;
Mady Mellord1c78b262018-11-06 18:04:40 -080037import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050038import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080039import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080040import android.provider.Settings;
Mady Mellor5549dd22018-11-06 18:07:34 -080041import android.service.notification.StatusBarNotification;
Mark Renoufcecc77b2019-01-30 16:32:24 -050042import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050043import android.view.IPinnedStackController;
44import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080045import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080046import android.widget.FrameLayout;
47
Mark Renouf08bc42a2019-03-07 13:01:59 -050048import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050049import androidx.annotation.MainThread;
50
Mady Mellorebdbbb92018-11-15 14:36:48 -080051import com.android.internal.annotations.VisibleForTesting;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080052import com.android.internal.statusbar.NotificationVisibility;
Ned Burns01e38212019-01-03 16:32:52 -050053import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080054import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050055import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050056import com.android.systemui.shared.system.ActivityManagerWrapper;
57import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050058import com.android.systemui.shared.system.WindowManagerWrapper;
Ned Burns01e38212019-01-03 16:32:52 -050059import com.android.systemui.statusbar.notification.NotificationEntryListener;
60import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080061import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050062import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050063import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080064import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070065import com.android.systemui.statusbar.policy.ConfigurationController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080066
Mark Renouf08bc42a2019-03-07 13:01:59 -050067import java.lang.annotation.Retention;
Mady Mellore80930e2019-03-21 16:00:45 -070068import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050069
Jason Monk27d01a622018-12-10 15:57:09 -050070import javax.inject.Inject;
71import javax.inject.Singleton;
72
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080073/**
74 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
75 * Bubbles can be expanded to show more content.
76 *
77 * The controller manages addition, removal, and visible state of bubbles on screen.
78 */
Jason Monk27d01a622018-12-10 15:57:09 -050079@Singleton
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070080public class BubbleController implements BubbleExpandedView.OnBubbleBlockedListener,
81 ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080082
83 private static final String TAG = "BubbleController";
84
Mark Renouf08bc42a2019-03-07 13:01:59 -050085 @Retention(SOURCE)
86 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
87 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION})
88 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070089
Mark Renouf08bc42a2019-03-07 13:01:59 -050090 static final int DISMISS_USER_GESTURE = 1;
91 static final int DISMISS_AGED = 2;
92 static final int DISMISS_TASK_FINISHED = 3;
93 static final int DISMISS_BLOCKED = 4;
94 static final int DISMISS_NOTIF_CANCEL = 5;
95 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
96
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -040097 static final int MAX_BUBBLES = 5; // TODO: actually enforce this
98
Mady Mellor5549dd22018-11-06 18:07:34 -080099 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -0500100 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -0800101
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800102 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800103 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800104 /** Auto bubble flags set whether different notif types should be presented as a bubble */
Mady Mellorceced172018-11-27 11:18:39 -0800105 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
106 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
107 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800108
109 /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500110 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -0800111
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800112 /** Whether the row of bubble circles are anchored to the top or bottom of the screen. */
113 private static final String ENABLE_BUBBLES_AT_TOP = "experiment_enable_top_bubbles";
114 /** Flag to position the header below the activity view */
115 private static final String ENABLE_BUBBLE_FOOTER = "experiment_enable_bubble_footer";
116
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
135 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider =
136 Dependency.get(NotificationInterruptionStateProvider.class);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800137
Mady Mellorb4991e62019-01-10 15:14:51 -0800138 private INotificationManager mNotificationManagerService;
139
Mady Mellord1c78b262018-11-06 18:04:40 -0800140 // Used for determining view rect for touch interaction
141 private Rect mTempRect = new Rect();
142
Mady Mellor5549dd22018-11-06 18:07:34 -0800143 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800144 * Listener to be notified when some states of the bubbles change.
145 */
146 public interface BubbleStateChangeListener {
147 /**
148 * Called when the stack has bubbles or no longer has bubbles.
149 */
150 void onHasBubblesChanged(boolean hasBubbles);
151 }
152
Mady Mellorcd9b1302018-11-06 18:08:04 -0800153 /**
154 * Listener to find out about stack expansion / collapse events.
155 */
156 public interface BubbleExpandListener {
157 /**
158 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700159 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800160 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800161 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800162 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800163 void onBubbleExpandChanged(boolean isExpanding, String key);
164 }
165
166 /**
167 * Listens for the current state of the status bar and updates the visibility state
168 * of bubbles as needed.
169 */
170 private class StatusBarStateListener implements StatusBarStateController.StateListener {
171 private int mState;
172 /**
173 * Returns the current status bar state.
174 */
175 public int getCurrentState() {
176 return mState;
177 }
178
179 @Override
180 public void onStateChanged(int newState) {
181 mState = newState;
182 updateVisibility();
183 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800184 }
185
Jason Monk27d01a622018-12-10 15:57:09 -0500186 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800187 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700188 BubbleData data, ConfigurationController configurationController) {
189 this(context, statusBarWindowController, data, null /* synchronizer */,
190 configurationController);
Issei Suzukic0387542019-03-08 17:31:14 +0100191 }
192
193 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700194 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
195 ConfigurationController configurationController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800196 mContext = context;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700197 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800198
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800199 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500200 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800201
202 try {
203 mNotificationManagerService = INotificationManager.Stub.asInterface(
204 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
205 } catch (ServiceManager.ServiceNotFoundException e) {
206 e.printStackTrace();
207 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800208
209 mStatusBarWindowController = statusBarWindowController;
210 mStatusBarStateListener = new StatusBarStateListener();
211 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500212
213 mActivityTaskManager = ActivityTaskManager.getService();
214 mTaskStackListener = new BubbleTaskStackListener();
215 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800216
Joshua Tsujia19515f2019-02-13 18:02:29 -0500217 try {
218 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
219 } catch (RemoteException e) {
220 e.printStackTrace();
221 }
222
Mady Mellorcfd06c12019-02-13 14:32:12 -0800223 mBubbleData = data;
Issei Suzukic0387542019-03-08 17:31:14 +0100224 mSurfaceSynchronizer = synchronizer;
Mady Mellor5549dd22018-11-06 18:07:34 -0800225 }
226
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700227 @Override
228 public void onUiModeChanged() {
229 if (mStackView != null) {
230 mStackView.onConfigChanged();
231 }
232 }
233
234 @Override
235 public void onOverlayChanged() {
236 if (mStackView != null) {
237 mStackView.onConfigChanged();
238 }
239 }
240
Mady Mellor5549dd22018-11-06 18:07:34 -0800241 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800242 * Set a listener to be notified when some states of the bubbles change.
243 */
244 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
245 mStateChangeListener = listener;
246 }
247
248 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800249 * Set a listener to be notified of bubble expand events.
250 */
251 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100252 mExpandListener = ((isExpanding, key) -> {
253 if (listener != null) {
254 listener.onBubbleExpandChanged(isExpanding, key);
255 }
256 mStatusBarWindowController.setBubbleExpanded(isExpanding);
257 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800258 if (mStackView != null) {
259 mStackView.setExpandListener(mExpandListener);
260 }
261 }
262
263 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800264 * Whether or not there are bubbles present, regardless of them being visible on the
265 * screen (e.g. if on AOD).
266 */
267 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800268 if (mStackView == null) {
269 return false;
270 }
271 for (Bubble bubble : mBubbleData.getBubbles()) {
272 if (!bubble.entry.isBubbleDismissed()) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800273 return true;
274 }
275 }
276 return false;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800277 }
278
279 /**
280 * Whether the stack of bubbles is expanded or not.
281 */
282 public boolean isStackExpanded() {
283 return mStackView != null && mStackView.isExpanded();
284 }
285
286 /**
287 * Tell the stack of bubbles to collapse.
288 */
289 public void collapseStack() {
290 if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800291 mStackView.collapseStack();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800292 }
293 }
294
295 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400296 * Request the stack expand if needed, then select the specified Bubble as current.
297 *
298 * @param notificationKey the notification key for the bubble to be selected
299 */
300 public void expandStackAndSelectBubble(String notificationKey) {
301 if (mStackView != null && mBubbleData.getBubble(notificationKey) != null) {
302 mStackView.setExpandedBubble(notificationKey);
303 }
304 }
305
306 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800307 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
308 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500309 void dismissStack(@DismissReason int reason) {
Mady Mellord1c78b262018-11-06 18:04:40 -0800310 if (mStackView == null) {
311 return;
312 }
Mark Renouf08bc42a2019-03-07 13:01:59 -0500313 mStackView.stackDismissed(reason);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800314
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800315 updateVisibility();
Ned Burns01e38212019-01-03 16:32:52 -0500316 mNotificationEntryManager.updateNotifications();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800317 }
318
319 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500320 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
321 * is forwarded a back key down/up pair.
322 */
323 public void performBackPressIfNeeded() {
324 if (mStackView != null) {
325 mStackView.performBackPressIfNeeded();
326 }
327 }
328
329 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800330 * Adds or updates a bubble associated with the provided notification entry.
331 *
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700332 * @param notif the notification associated with this bubble.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800333 * @param updatePosition whether this update should promote the bubble to the top of the stack.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800334 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800335 public void updateBubble(NotificationEntry notif, boolean updatePosition) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800336 if (mStackView != null && mBubbleData.getBubble(notif.key) != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800337 // It's an update
Mady Mellor3dff9e62019-02-05 18:12:53 -0800338 mStackView.updateBubble(notif, updatePosition);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800339 } else {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800340 if (mStackView == null) {
Issei Suzukic0387542019-03-08 17:31:14 +0100341 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
Mady Mellord1c78b262018-11-06 18:04:40 -0800342 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800343 // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim
344 // between bubble and the shade
345 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
346 sbv.addView(mStackView, bubblePosition,
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800347 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
Mady Mellorcd9b1302018-11-06 18:08:04 -0800348 if (mExpandListener != null) {
349 mStackView.setExpandListener(mExpandListener);
350 }
Mady Mellore8e07712019-01-23 12:45:33 -0800351 mStackView.setOnBlockedListener(this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800352 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800353 // It's new
Mady Mellor3dff9e62019-02-05 18:12:53 -0800354 mStackView.addBubble(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800355 }
Mady Mellore80930e2019-03-21 16:00:45 -0700356 if (shouldAutoExpand(notif)) {
357 mStackView.setExpandedBubble(notif);
358 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800359 updateVisibility();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800360 }
361
362 /**
363 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500364 * <p>
365 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800366 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500367 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500368 void removeBubble(String key, int reason) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800369 if (mStackView != null) {
Mark Renouf08bc42a2019-03-07 13:01:59 -0500370 mStackView.removeBubble(key, reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800371 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800372 mNotificationEntryManager.updateNotifications();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800373 updateVisibility();
Mady Mellord1c78b262018-11-06 18:04:40 -0800374 }
375
Mady Mellore8e07712019-01-23 12:45:33 -0800376 @Override
377 public void onBubbleBlocked(NotificationEntry entry) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800378 Object[] bubbles = mBubbleData.getBubbles().toArray();
Mady Mellore8e07712019-01-23 12:45:33 -0800379 for (int i = 0; i < bubbles.length; i++) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800380 NotificationEntry e = ((Bubble) bubbles[i]).entry;
Mady Mellore8e07712019-01-23 12:45:33 -0800381 boolean samePackage = entry.notification.getPackageName().equals(
382 e.notification.getPackageName());
383 if (samePackage) {
Mark Renouf08bc42a2019-03-07 13:01:59 -0500384 removeBubble(entry.key, DISMISS_BLOCKED);
Mady Mellore8e07712019-01-23 12:45:33 -0800385 }
386 }
387 }
388
Ned Burns01e38212019-01-03 16:32:52 -0500389 @SuppressWarnings("FieldCanBeLocal")
390 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
391 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500392 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800393 if (!areBubblesEnabled(mContext)) {
394 return;
395 }
396 if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800397 // TODO: handle group summaries
Ned Burns01e38212019-01-03 16:32:52 -0500398 entry.setIsBubble(true);
Mady Mellor3ed46202019-03-26 20:22:35 -0700399 boolean suppressNotification = entry.getBubbleMetadata() != null
400 && entry.getBubbleMetadata().getSuppressInitialNotification()
401 && isForegroundApp(entry.notification.getPackageName());
402 entry.setShowInShadeWhenBubble(!suppressNotification);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800403 }
404 }
405
406 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500407 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800408 if (!areBubblesEnabled(mContext)) {
409 return;
410 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800411 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
412 updateBubble(entry, true /* updatePosition */);
413 }
414 }
415
416 @Override
417 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800418 if (!areBubblesEnabled(mContext)) {
419 return;
420 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800421 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
422 && alertAgain(entry, entry.notification.getNotification())) {
423 entry.setShowInShadeWhenBubble(true);
424 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800425 updateBubble(entry, true /* updatePosition */);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800426 mStackView.updateDotVisibility(entry.key);
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
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800448 /**
449 * Lets any listeners know if bubble state has changed.
450 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800451 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800452 if (mStackView == null) {
453 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800454 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800455
Mady Mellord1c78b262018-11-06 18:04:40 -0800456 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800457 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800458 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800459 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
460 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
461 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800462 }
463
464 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800465 * Updates the visibility of the bubbles based on current state.
466 * Does not un-bubble, just hides or un-hides. Will notify any
467 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800468 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800469 public void updateVisibility() {
470 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
471 // Bubbles only appear in unlocked shade
472 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
473 } else if (mStackView != null) {
474 mStackView.setVisibility(INVISIBLE);
475 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800476 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800477 updateBubblesShowing();
478 }
479
480 /**
481 * Rect indicating the touchable region for the bubble stack / expanded stack.
482 */
483 public Rect getTouchableRegion() {
484 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
485 return null;
486 }
487 mStackView.getBoundsOnScreen(mTempRect);
488 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800489 }
490
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800491 @VisibleForTesting
492 BubbleStackView getStackView() {
493 return mStackView;
494 }
495
Mady Mellor5549dd22018-11-06 18:07:34 -0800496 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800497 * Whether the notification has been developer configured to bubble and is allowed by the user.
498 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800499 @VisibleForTesting
500 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800501 StatusBarNotification n = entry.notification;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800502 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
503 && n.getNotification().getBubbleMetadata().getIntent() != null;
Julia Reynolds4509ce72019-01-31 13:12:43 -0500504 return hasOverlayIntent && entry.canBubble;
Mady Mellorb4991e62019-01-10 15:14:51 -0800505 }
506
507 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800508 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800509 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800510 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800511 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800512 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800513 return false;
514 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800515 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800516
517 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
518 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
519 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
520
Mady Mellor5549dd22018-11-06 18:07:34 -0800521 boolean hasRemoteInput = false;
522 if (n.getNotification().actions != null) {
523 for (Notification.Action action : n.getNotification().actions) {
524 if (action.getRemoteInputs() != null) {
525 hasRemoteInput = true;
526 break;
527 }
528 }
529 }
Mady Mellor711f9562018-12-05 14:53:46 -0800530 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
531 && n.isOngoing();
532 boolean isMusic = n.getNotification().hasMediaSession();
533 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800534
Mady Mellor5549dd22018-11-06 18:07:34 -0800535 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800536 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
537 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800538 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800539 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800540 || autoBubbleAll;
541 }
542
Mady Mellore80930e2019-03-21 16:00:45 -0700543 private boolean shouldAutoExpand(NotificationEntry entry) {
544 Notification.BubbleMetadata metadata = entry.getBubbleMetadata();
545 return metadata != null && metadata.getAutoExpandBubble()
546 && isForegroundApp(entry.notification.getPackageName());
547 }
548
549 /**
550 * Return true if the applications with the package name is running in foreground.
551 *
552 * @param pkgName application package name.
553 */
554 private boolean isForegroundApp(String pkgName) {
555 ActivityManager am = mContext.getSystemService(ActivityManager.class);
556 List<RunningTaskInfo> tasks = am.getRunningTasks(1 /* maxNum */);
557 return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
558 }
559
Mark Renoufcecc77b2019-01-30 16:32:24 -0500560 /**
561 * This task stack listener is responsible for responding to tasks moved to the front
562 * which are on the default (main) display. When this happens, expanded bubbles must be
563 * collapsed so the user may interact with the app which was just moved to the front.
564 * <p>
565 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
566 * these calls via a main thread Handler.
567 */
568 @MainThread
569 private class BubbleTaskStackListener extends TaskStackChangeListener {
570
Mark Renoufcecc77b2019-01-30 16:32:24 -0500571 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500572 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
573 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500574 mStackView.collapseStack();
575 }
576 }
577
Mark Renoufcecc77b2019-01-30 16:32:24 -0500578 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500579 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500580 if (mStackView != null) {
581 mStackView.collapseStack();
582 }
583 }
584 }
585
Mady Mellorceced172018-11-27 11:18:39 -0800586 private static boolean shouldAutoBubbleMessages(Context context) {
587 return Settings.Secure.getInt(context.getContentResolver(),
588 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
589 }
590
591 private static boolean shouldAutoBubbleOngoing(Context context) {
592 return Settings.Secure.getInt(context.getContentResolver(),
593 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
594 }
595
596 private static boolean shouldAutoBubbleAll(Context context) {
597 return Settings.Secure.getInt(context.getContentResolver(),
598 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800599 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500600
Mady Mellor3dff9e62019-02-05 18:12:53 -0800601 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500602 return Settings.Secure.getInt(context.getContentResolver(),
603 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
604 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800605
606 private static boolean areBubblesEnabled(Context context) {
607 return Settings.Secure.getInt(context.getContentResolver(),
608 ENABLE_BUBBLES, 1) != 0;
609 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500610
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800611 /**
612 * Whether bubbles should be positioned at the top of the screen or not.
613 */
614 public static boolean showBubblesAtTop(Context context) {
615 return Settings.Secure.getInt(context.getContentResolver(),
616 ENABLE_BUBBLES_AT_TOP, 0) != 0;
617 }
618
619 /**
620 * Whether the bubble chrome should display as a footer or not (in which case it's a header).
621 */
622 public static boolean useFooter(Context context) {
623 return Settings.Secure.getInt(context.getContentResolver(),
624 ENABLE_BUBBLE_FOOTER, 0) != 0;
625 }
626
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500627 /** Default stiffness to use for bubble physics animations. */
628 public static int getBubbleStiffness(Context context, int defaultStiffness) {
629 return Settings.Secure.getInt(
630 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
631 }
632
633 /** Default bounciness/damping ratio to use for bubble physics animations. */
634 public static float getBubbleBounciness(Context context, float defaultBounciness) {
635 return Settings.Secure.getInt(
636 context.getContentResolver(),
637 BUBBLE_BOUNCINESS,
638 (int) (defaultBounciness * 100)) / 100f;
639 }
640
Joshua Tsujia19515f2019-02-13 18:02:29 -0500641 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
642 private class BubblesImeListener extends IPinnedStackListener.Stub {
643
644 @Override
645 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
646 }
647
648 @Override
649 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
650 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
651 int displayRotation) throws RemoteException {}
652
653 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500654 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
655 if (mStackView != null && mStackView.getBubbleCount() > 0) {
656 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500657 }
658 }
659
660 @Override
661 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
662 throws RemoteException {}
663
664 @Override
665 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
666
667 @Override
668 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
669 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800670}