blob: 744f88d19ba35fb9b71117c8bfa4f026f6bacbd8 [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;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040039import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080040import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050041import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080042import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080043import android.provider.Settings;
Mady Mellor5549dd22018-11-06 18:07:34 -080044import android.service.notification.StatusBarNotification;
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 Mellor3f2efdb2018-11-21 11:30:45 -080055import com.android.internal.statusbar.NotificationVisibility;
Ned Burns01e38212019-01-03 16:32:52 -050056import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080057import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050058import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050059import com.android.systemui.shared.system.ActivityManagerWrapper;
60import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050061import com.android.systemui.shared.system.WindowManagerWrapper;
Ned Burns01e38212019-01-03 16:32:52 -050062import com.android.systemui.statusbar.notification.NotificationEntryListener;
63import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080064import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050065import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050066import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080067import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070068import com.android.systemui.statusbar.policy.ConfigurationController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080069
Mark Renouf08bc42a2019-03-07 13:01:59 -050070import java.lang.annotation.Retention;
Mady Mellore80930e2019-03-21 16:00:45 -070071import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050072
Jason Monk27d01a622018-12-10 15:57:09 -050073import javax.inject.Inject;
74import javax.inject.Singleton;
75
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080076/**
77 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
78 * Bubbles can be expanded to show more content.
79 *
80 * The controller manages addition, removal, and visible state of bubbles on screen.
81 */
Jason Monk27d01a622018-12-10 15:57:09 -050082@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -040083public class BubbleController implements 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,
Mady Melloraa8fef22019-04-11 13:36:40 -070089 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE})
Mark Renouf08bc42a2019-03-07 13:01:59 -050090 @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;
Mady Melloraa8fef22019-04-11 13:36:40 -070098 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renouf08bc42a2019-03-07 13:01:59 -050099
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -0400100 static final int MAX_BUBBLES = 5; // TODO: actually enforce this
101
Mady Mellor5549dd22018-11-06 18:07:34 -0800102 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -0500103 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -0800104
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800105 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800106 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800107 /** Auto bubble flags set whether different notif types should be presented as a bubble */
Mady Mellorceced172018-11-27 11:18:39 -0800108 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
109 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
110 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800111
112 /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500113 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -0800114
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500115 private static final String BUBBLE_STIFFNESS = "experiment_bubble_stiffness";
116 private static final String BUBBLE_BOUNCINESS = "experiment_bubble_bounciness";
117
Ned Burns01e38212019-01-03 16:32:52 -0500118 private final Context mContext;
119 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500120 private final IActivityTaskManager mActivityTaskManager;
121 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800122 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800123 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100124 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800125
Mady Mellor3dff9e62019-02-05 18:12:53 -0800126 private BubbleData mBubbleData;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800127 private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800128
129 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500130 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800131 private StatusBarStateListener mStatusBarStateListener;
132
Mady Melloraa8fef22019-04-11 13:36:40 -0700133 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
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
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400140 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
141 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
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;
Mark Renouf71a3af62019-04-08 15:02:54 -0400182 boolean shouldCollapse = (mState != SHADE);
183 if (shouldCollapse) {
184 collapseStack();
185 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800186 updateVisibility();
187 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800188 }
189
Jason Monk27d01a622018-12-10 15:57:09 -0500190 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800191 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Mady Melloraa8fef22019-04-11 13:36:40 -0700192 BubbleData data, ConfigurationController configurationController,
193 NotificationInterruptionStateProvider interruptionStateProvider) {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700194 this(context, statusBarWindowController, data, null /* synchronizer */,
Mady Melloraa8fef22019-04-11 13:36:40 -0700195 configurationController, interruptionStateProvider);
Issei Suzukic0387542019-03-08 17:31:14 +0100196 }
197
198 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700199 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
Mady Melloraa8fef22019-04-11 13:36:40 -0700200 ConfigurationController configurationController,
201 NotificationInterruptionStateProvider interruptionStateProvider) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800202 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700203 mNotificationInterruptionStateProvider = interruptionStateProvider;
204
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700205 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800206
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800207 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500208 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800209
210 try {
211 mNotificationManagerService = INotificationManager.Stub.asInterface(
212 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
213 } catch (ServiceManager.ServiceNotFoundException e) {
214 e.printStackTrace();
215 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800216
217 mStatusBarWindowController = statusBarWindowController;
218 mStatusBarStateListener = new StatusBarStateListener();
219 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500220
221 mActivityTaskManager = ActivityTaskManager.getService();
222 mTaskStackListener = new BubbleTaskStackListener();
223 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800224
Joshua Tsujia19515f2019-02-13 18:02:29 -0500225 try {
226 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
227 } catch (RemoteException e) {
228 e.printStackTrace();
229 }
230
Mady Mellorcfd06c12019-02-13 14:32:12 -0800231 mBubbleData = data;
Mark Renouf3bc5b362019-04-05 14:37:59 -0400232 mBubbleData.setListener(mBubbleDataListener);
Issei Suzukic0387542019-03-08 17:31:14 +0100233 mSurfaceSynchronizer = synchronizer;
Mady Mellor5549dd22018-11-06 18:07:34 -0800234 }
235
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400236 /**
237 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
238 * method initializes the stack view and adds it to the StatusBar just above the scrim.
239 */
240 private void ensureStackViewCreated() {
241 if (mStackView == null) {
242 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
243 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
244 // TODO(b/130237686): When you expand the shade on top of expanded bubble, there is no
245 // scrim between bubble and the shade
246 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
247 sbv.addView(mStackView, bubblePosition,
248 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
249 if (mExpandListener != null) {
250 mStackView.setExpandListener(mExpandListener);
251 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400252 }
253 }
254
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700255 @Override
256 public void onUiModeChanged() {
257 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700258 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700259 }
260 }
261
262 @Override
263 public void onOverlayChanged() {
264 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700265 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700266 }
267 }
268
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400269 @Override
270 public void onConfigChanged(Configuration newConfig) {
271 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
272 mStackView.onOrientationChanged();
273 mOrientation = newConfig.orientation;
274 }
275 }
276
Mady Mellor5549dd22018-11-06 18:07:34 -0800277 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800278 * Set a listener to be notified when some states of the bubbles change.
279 */
280 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
281 mStateChangeListener = listener;
282 }
283
284 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800285 * Set a listener to be notified of bubble expand events.
286 */
287 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100288 mExpandListener = ((isExpanding, key) -> {
289 if (listener != null) {
290 listener.onBubbleExpandChanged(isExpanding, key);
291 }
292 mStatusBarWindowController.setBubbleExpanded(isExpanding);
293 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800294 if (mStackView != null) {
295 mStackView.setExpandListener(mExpandListener);
296 }
297 }
298
299 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800300 * Whether or not there are bubbles present, regardless of them being visible on the
301 * screen (e.g. if on AOD).
302 */
303 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800304 if (mStackView == null) {
305 return false;
306 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400307 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800308 }
309
310 /**
311 * Whether the stack of bubbles is expanded or not.
312 */
313 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400314 return mBubbleData.isExpanded();
315 }
316
317 /**
318 * Tell the stack of bubbles to expand.
319 */
320 public void expandStack() {
321 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800322 }
323
324 /**
325 * Tell the stack of bubbles to collapse.
326 */
327 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400328 mBubbleData.setExpanded(false /* expanded */);
329 }
330
331 void selectBubble(Bubble bubble) {
332 mBubbleData.setSelectedBubble(bubble);
333 }
334
335 @VisibleForTesting
336 void selectBubble(String key) {
337 Bubble bubble = mBubbleData.getBubbleWithKey(key);
338 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800339 }
340
341 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400342 * Request the stack expand if needed, then select the specified Bubble as current.
343 *
344 * @param notificationKey the notification key for the bubble to be selected
345 */
346 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400347 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
348 if (bubble != null) {
349 mBubbleData.setSelectedBubble(bubble);
350 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400351 }
352 }
353
354 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800355 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
356 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500357 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400358 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800359 }
360
361 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500362 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
363 * is forwarded a back key down/up pair.
364 */
365 public void performBackPressIfNeeded() {
366 if (mStackView != null) {
367 mStackView.performBackPressIfNeeded();
368 }
369 }
370
371 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800372 * Adds or updates a bubble associated with the provided notification entry.
373 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400374 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800375 */
Mark Renouff97ed462019-04-05 13:46:24 -0400376 void updateBubble(NotificationEntry notif) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400377 mBubbleData.notificationEntryUpdated(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800378 }
379
380 /**
381 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500382 * <p>
383 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800384 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500385 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500386 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400387 // TEMP: refactor to change this to pass entry
388 Bubble bubble = mBubbleData.getBubbleWithKey(key);
389 if (bubble != null) {
390 mBubbleData.notificationEntryRemoved(bubble.entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800391 }
392 }
393
Ned Burns01e38212019-01-03 16:32:52 -0500394 @SuppressWarnings("FieldCanBeLocal")
395 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
396 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500397 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800398 if (!areBubblesEnabled(mContext)) {
399 return;
400 }
Mady Melloraa8fef22019-04-11 13:36:40 -0700401 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700402 // TODO: handle group summaries?
403 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800404 }
405 }
406
407 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500408 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800409 if (!areBubblesEnabled(mContext)) {
410 return;
411 }
Mady Melloraa8fef22019-04-11 13:36:40 -0700412 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400413 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800414 }
415 }
416
417 @Override
418 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800419 if (!areBubblesEnabled(mContext)) {
420 return;
421 }
Mady Melloraa8fef22019-04-11 13:36:40 -0700422 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry);
423 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.key)) {
424 // It was previously a bubble but no longer a bubble -- lets remove it
425 removeBubble(entry.key, DISMISS_NO_LONGER_BUBBLE);
426 } else if (shouldBubble && alertAgain(entry, entry.notification.getNotification())) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700427 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800428 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mark Renouff97ed462019-04-05 13:46:24 -0400429 updateBubble(entry);
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 Renouf71a3af62019-04-08 15:02:54 -0400451 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400452 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400453
Mark Renouf3bc5b362019-04-05 14:37:59 -0400454 @Override
455 public void onBubbleAdded(Bubble bubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400456 ensureStackViewCreated();
457 mStackView.addBubble(bubble);
Mark Renouf3bc5b362019-04-05 14:37:59 -0400458 }
459
460 @Override
461 public void onBubbleRemoved(Bubble bubble, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400462 if (mStackView != null) {
463 mStackView.removeBubble(bubble);
464 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400465 }
466
467 public void onBubbleUpdated(Bubble bubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400468 if (mStackView != null) {
469 mStackView.updateBubble(bubble);
470 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400471 }
472
473 @Override
474 public void onOrderChanged(List<Bubble> bubbles) {
Mark Renouf3bc5b362019-04-05 14:37:59 -0400475 }
476
477 @Override
478 public void onSelectionChanged(Bubble selectedBubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400479 if (mStackView != null) {
480 mStackView.setSelectedBubble(selectedBubble);
481 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400482 }
483
484 @Override
485 public void onExpandedChanged(boolean expanded) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400486 if (mStackView != null) {
487 mStackView.setExpanded(expanded);
488 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400489 }
490
491 @Override
492 public void showFlyoutText(Bubble bubble, String text) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400493 if (mStackView != null) {
494 mStackView.animateInFlyoutForBubble(bubble);
495 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400496 }
497
498 @Override
499 public void apply() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400500 mNotificationEntryManager.updateNotifications();
501 updateVisibility();
Mark Renouf3bc5b362019-04-05 14:37:59 -0400502 }
503 };
504
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800505 /**
506 * Lets any listeners know if bubble state has changed.
507 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800508 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800509 if (mStackView == null) {
510 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800511 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800512
Mady Mellord1c78b262018-11-06 18:04:40 -0800513 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800514 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800515 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800516 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
517 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
518 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800519 }
520
521 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800522 * Updates the visibility of the bubbles based on current state.
523 * Does not un-bubble, just hides or un-hides. Will notify any
524 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800525 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800526 public void updateVisibility() {
527 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
528 // Bubbles only appear in unlocked shade
529 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
530 } else if (mStackView != null) {
531 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800532 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800533 updateBubblesShowing();
534 }
535
536 /**
537 * Rect indicating the touchable region for the bubble stack / expanded stack.
538 */
539 public Rect getTouchableRegion() {
540 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
541 return null;
542 }
543 mStackView.getBoundsOnScreen(mTempRect);
544 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800545 }
546
Mady Mellor390bff42019-04-05 15:09:01 -0700547 /**
548 * The display id of the expanded view, if the stack is expanded and not occluded by the
549 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
550 */
551 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700552 if (mStackView == null) {
553 return INVALID_DISPLAY;
554 }
Mady Mellor390bff42019-04-05 15:09:01 -0700555 boolean defaultDisplay = context.getDisplay() != null
556 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
557 Bubble b = mStackView.getExpandedBubble();
558 if (defaultDisplay && b != null && isStackExpanded()
559 && !mStatusBarWindowController.getPanelExpanded()) {
560 return b.expandedView.getVirtualDisplayId();
561 }
562 return INVALID_DISPLAY;
563 }
564
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800565 @VisibleForTesting
566 BubbleStackView getStackView() {
567 return mStackView;
568 }
569
Mady Mellor5549dd22018-11-06 18:07:34 -0800570 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800571 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800572 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800573 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800574 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800575 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800576 return false;
577 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800578 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800579
580 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
581 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
582 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
583
Mady Mellor5549dd22018-11-06 18:07:34 -0800584 boolean hasRemoteInput = false;
585 if (n.getNotification().actions != null) {
586 for (Notification.Action action : n.getNotification().actions) {
587 if (action.getRemoteInputs() != null) {
588 hasRemoteInput = true;
589 break;
590 }
591 }
592 }
Mady Mellor711f9562018-12-05 14:53:46 -0800593 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
594 && n.isOngoing();
595 boolean isMusic = n.getNotification().hasMediaSession();
596 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800597
Mady Mellor5549dd22018-11-06 18:07:34 -0800598 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800599 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
600 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800601 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800602 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800603 || autoBubbleAll;
604 }
605
Mady Mellore80930e2019-03-21 16:00:45 -0700606 private boolean shouldAutoExpand(NotificationEntry entry) {
607 Notification.BubbleMetadata metadata = entry.getBubbleMetadata();
608 return metadata != null && metadata.getAutoExpandBubble()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400609 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellore80930e2019-03-21 16:00:45 -0700610 }
611
Mady Mellor8a1f0252019-04-01 11:31:34 -0700612 private void updateShowInShadeForSuppressNotification(NotificationEntry entry) {
613 boolean suppressNotification = entry.getBubbleMetadata() != null
Mady Mellorc529d6d2019-04-16 14:22:52 -0700614 && entry.getBubbleMetadata().isNotificationSuppressed()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400615 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellor8a1f0252019-04-01 11:31:34 -0700616 entry.setShowInShadeWhenBubble(!suppressNotification);
617 }
618
Mady Mellore80930e2019-03-21 16:00:45 -0700619 /**
620 * Return true if the applications with the package name is running in foreground.
621 *
Steven Wu8ba8ca92019-04-11 10:47:42 -0400622 * @param context application context.
Mady Mellore80930e2019-03-21 16:00:45 -0700623 * @param pkgName application package name.
624 */
Steven Wu8ba8ca92019-04-11 10:47:42 -0400625 public static boolean isForegroundApp(Context context, String pkgName) {
626 ActivityManager am = context.getSystemService(ActivityManager.class);
Mady Mellore80930e2019-03-21 16:00:45 -0700627 List<RunningTaskInfo> tasks = am.getRunningTasks(1 /* maxNum */);
628 return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
629 }
630
Mark Renoufcecc77b2019-01-30 16:32:24 -0500631 /**
632 * This task stack listener is responsible for responding to tasks moved to the front
633 * which are on the default (main) display. When this happens, expanded bubbles must be
634 * collapsed so the user may interact with the app which was just moved to the front.
635 * <p>
636 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
637 * these calls via a main thread Handler.
638 */
639 @MainThread
640 private class BubbleTaskStackListener extends TaskStackChangeListener {
641
Mark Renoufcecc77b2019-01-30 16:32:24 -0500642 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500643 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
644 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400645 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500646 }
647 }
648
Mark Renoufcecc77b2019-01-30 16:32:24 -0500649 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500650 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500651 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400652 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500653 }
654 }
655 }
656
Mady Mellorceced172018-11-27 11:18:39 -0800657 private static boolean shouldAutoBubbleMessages(Context context) {
658 return Settings.Secure.getInt(context.getContentResolver(),
659 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
660 }
661
662 private static boolean shouldAutoBubbleOngoing(Context context) {
663 return Settings.Secure.getInt(context.getContentResolver(),
664 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
665 }
666
667 private static boolean shouldAutoBubbleAll(Context context) {
668 return Settings.Secure.getInt(context.getContentResolver(),
669 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800670 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500671
Mady Mellor3dff9e62019-02-05 18:12:53 -0800672 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500673 return Settings.Secure.getInt(context.getContentResolver(),
674 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
675 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800676
677 private static boolean areBubblesEnabled(Context context) {
678 return Settings.Secure.getInt(context.getContentResolver(),
679 ENABLE_BUBBLES, 1) != 0;
680 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500681
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500682 /** Default stiffness to use for bubble physics animations. */
683 public static int getBubbleStiffness(Context context, int defaultStiffness) {
684 return Settings.Secure.getInt(
685 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
686 }
687
688 /** Default bounciness/damping ratio to use for bubble physics animations. */
689 public static float getBubbleBounciness(Context context, float defaultBounciness) {
690 return Settings.Secure.getInt(
691 context.getContentResolver(),
692 BUBBLE_BOUNCINESS,
693 (int) (defaultBounciness * 100)) / 100f;
694 }
695
Joshua Tsujia19515f2019-02-13 18:02:29 -0500696 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
697 private class BubblesImeListener extends IPinnedStackListener.Stub {
698
699 @Override
700 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
701 }
702
703 @Override
704 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
705 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
706 int displayRotation) throws RemoteException {}
707
708 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500709 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
710 if (mStackView != null && mStackView.getBubbleCount() > 0) {
711 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500712 }
713 }
714
715 @Override
716 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
717 throws RemoteException {}
718
719 @Override
720 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
721
722 @Override
723 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
724 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800725}