blob: acdcfb2ea688d6491a19e25929cb30ccb07ae961 [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
Mark Renouf71a3af62019-04-08 15:02:54 -040080public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080081
82 private static final String TAG = "BubbleController";
83
Mark Renouf08bc42a2019-03-07 13:01:59 -050084 @Retention(SOURCE)
85 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
86 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION})
87 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070088
Mark Renouf08bc42a2019-03-07 13:01:59 -050089 static final int DISMISS_USER_GESTURE = 1;
90 static final int DISMISS_AGED = 2;
91 static final int DISMISS_TASK_FINISHED = 3;
92 static final int DISMISS_BLOCKED = 4;
93 static final int DISMISS_NOTIF_CANCEL = 5;
94 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
95
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -040096 static final int MAX_BUBBLES = 5; // TODO: actually enforce this
97
Mady Mellor5549dd22018-11-06 18:07:34 -080098 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -050099 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -0800100
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800101 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800102 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800103 /** Auto bubble flags set whether different notif types should be presented as a bubble */
Mady Mellorceced172018-11-27 11:18:39 -0800104 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
105 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
106 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800107
108 /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500109 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -0800110
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500111 private static final String BUBBLE_STIFFNESS = "experiment_bubble_stiffness";
112 private static final String BUBBLE_BOUNCINESS = "experiment_bubble_bounciness";
113
Ned Burns01e38212019-01-03 16:32:52 -0500114 private final Context mContext;
115 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500116 private final IActivityTaskManager mActivityTaskManager;
117 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800118 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800119 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100120 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800121
Mady Mellor3dff9e62019-02-05 18:12:53 -0800122 private BubbleData mBubbleData;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800123 private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800124
125 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500126 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800127 private StatusBarStateListener mStatusBarStateListener;
128
129 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider =
130 Dependency.get(NotificationInterruptionStateProvider.class);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800131
Mady Mellorb4991e62019-01-10 15:14:51 -0800132 private INotificationManager mNotificationManagerService;
133
Mady Mellord1c78b262018-11-06 18:04:40 -0800134 // Used for determining view rect for touch interaction
135 private Rect mTempRect = new Rect();
136
Mady Mellor5549dd22018-11-06 18:07:34 -0800137 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800138 * Listener to be notified when some states of the bubbles change.
139 */
140 public interface BubbleStateChangeListener {
141 /**
142 * Called when the stack has bubbles or no longer has bubbles.
143 */
144 void onHasBubblesChanged(boolean hasBubbles);
145 }
146
Mady Mellorcd9b1302018-11-06 18:08:04 -0800147 /**
148 * Listener to find out about stack expansion / collapse events.
149 */
150 public interface BubbleExpandListener {
151 /**
152 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700153 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800154 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800155 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800156 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800157 void onBubbleExpandChanged(boolean isExpanding, String key);
158 }
159
160 /**
161 * Listens for the current state of the status bar and updates the visibility state
162 * of bubbles as needed.
163 */
164 private class StatusBarStateListener implements StatusBarStateController.StateListener {
165 private int mState;
166 /**
167 * Returns the current status bar state.
168 */
169 public int getCurrentState() {
170 return mState;
171 }
172
173 @Override
174 public void onStateChanged(int newState) {
175 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400176 boolean shouldCollapse = (mState != SHADE);
177 if (shouldCollapse) {
178 collapseStack();
179 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800180 updateVisibility();
181 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800182 }
183
Jason Monk27d01a622018-12-10 15:57:09 -0500184 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800185 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700186 BubbleData data, ConfigurationController configurationController) {
187 this(context, statusBarWindowController, data, null /* synchronizer */,
188 configurationController);
Issei Suzukic0387542019-03-08 17:31:14 +0100189 }
190
191 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700192 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
193 ConfigurationController configurationController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800194 mContext = context;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700195 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800196
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800197 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500198 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800199
200 try {
201 mNotificationManagerService = INotificationManager.Stub.asInterface(
202 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
203 } catch (ServiceManager.ServiceNotFoundException e) {
204 e.printStackTrace();
205 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800206
207 mStatusBarWindowController = statusBarWindowController;
208 mStatusBarStateListener = new StatusBarStateListener();
209 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500210
211 mActivityTaskManager = ActivityTaskManager.getService();
212 mTaskStackListener = new BubbleTaskStackListener();
213 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800214
Joshua Tsujia19515f2019-02-13 18:02:29 -0500215 try {
216 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
217 } catch (RemoteException e) {
218 e.printStackTrace();
219 }
220
Mady Mellorcfd06c12019-02-13 14:32:12 -0800221 mBubbleData = data;
Mark Renouf3bc5b362019-04-05 14:37:59 -0400222 mBubbleData.setListener(mBubbleDataListener);
Issei Suzukic0387542019-03-08 17:31:14 +0100223 mSurfaceSynchronizer = synchronizer;
Mady Mellor5549dd22018-11-06 18:07:34 -0800224 }
225
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400226 /**
227 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
228 * method initializes the stack view and adds it to the StatusBar just above the scrim.
229 */
230 private void ensureStackViewCreated() {
231 if (mStackView == null) {
232 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
233 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
234 // TODO(b/130237686): When you expand the shade on top of expanded bubble, there is no
235 // scrim between bubble and the shade
236 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
237 sbv.addView(mStackView, bubblePosition,
238 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
239 if (mExpandListener != null) {
240 mStackView.setExpandListener(mExpandListener);
241 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400242 }
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 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400289 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800290 }
291
292 /**
293 * Whether the stack of bubbles is expanded or not.
294 */
295 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400296 return mBubbleData.isExpanded();
297 }
298
299 /**
300 * Tell the stack of bubbles to expand.
301 */
302 public void expandStack() {
303 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800304 }
305
306 /**
307 * Tell the stack of bubbles to collapse.
308 */
309 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400310 mBubbleData.setExpanded(false /* expanded */);
311 }
312
313 void selectBubble(Bubble bubble) {
314 mBubbleData.setSelectedBubble(bubble);
315 }
316
317 @VisibleForTesting
318 void selectBubble(String key) {
319 Bubble bubble = mBubbleData.getBubbleWithKey(key);
320 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800321 }
322
323 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400324 * Request the stack expand if needed, then select the specified Bubble as current.
325 *
326 * @param notificationKey the notification key for the bubble to be selected
327 */
328 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400329 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
330 if (bubble != null) {
331 mBubbleData.setSelectedBubble(bubble);
332 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400333 }
334 }
335
336 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800337 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
338 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500339 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400340 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800341 }
342
343 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500344 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
345 * is forwarded a back key down/up pair.
346 */
347 public void performBackPressIfNeeded() {
348 if (mStackView != null) {
349 mStackView.performBackPressIfNeeded();
350 }
351 }
352
353 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800354 * Adds or updates a bubble associated with the provided notification entry.
355 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400356 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800357 */
Mark Renouff97ed462019-04-05 13:46:24 -0400358 void updateBubble(NotificationEntry notif) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400359 mBubbleData.notificationEntryUpdated(notif);
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) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400369 // TEMP: refactor to change this to pass entry
370 Bubble bubble = mBubbleData.getBubbleWithKey(key);
371 if (bubble != null) {
372 mBubbleData.notificationEntryRemoved(bubble.entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800373 }
374 }
375
Ned Burns01e38212019-01-03 16:32:52 -0500376 @SuppressWarnings("FieldCanBeLocal")
377 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
378 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500379 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800380 if (!areBubblesEnabled(mContext)) {
381 return;
382 }
383 if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700384 // TODO: handle group summaries?
385 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800386 }
387 }
388
389 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500390 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800391 if (!areBubblesEnabled(mContext)) {
392 return;
393 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800394 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400395 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800396 }
397 }
398
399 @Override
400 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800401 if (!areBubblesEnabled(mContext)) {
402 return;
403 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800404 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
405 && alertAgain(entry, entry.notification.getNotification())) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700406 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800407 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mark Renouff97ed462019-04-05 13:46:24 -0400408 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800409 }
410 }
411
412 @Override
413 public void onEntryRemoved(NotificationEntry entry,
414 @Nullable NotificationVisibility visibility,
415 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800416 if (!areBubblesEnabled(mContext)) {
417 return;
418 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800419 entry.setShowInShadeWhenBubble(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800420 if (mStackView != null) {
421 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800422 }
423 if (!removedByUser) {
424 // This was a cancel so we should remove the bubble
Mark Renouf08bc42a2019-03-07 13:01:59 -0500425 removeBubble(entry.key, DISMISS_NOTIF_CANCEL);
Ned Burns01e38212019-01-03 16:32:52 -0500426 }
427 }
428 };
429
Mark Renouf71a3af62019-04-08 15:02:54 -0400430 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400431 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400432
Mark Renouf3bc5b362019-04-05 14:37:59 -0400433 @Override
434 public void onBubbleAdded(Bubble bubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400435 ensureStackViewCreated();
436 mStackView.addBubble(bubble);
Mark Renouf3bc5b362019-04-05 14:37:59 -0400437 }
438
439 @Override
440 public void onBubbleRemoved(Bubble bubble, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400441 if (mStackView != null) {
442 mStackView.removeBubble(bubble);
443 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400444 }
445
446 public void onBubbleUpdated(Bubble bubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400447 if (mStackView != null) {
448 mStackView.updateBubble(bubble);
449 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400450 }
451
452 @Override
453 public void onOrderChanged(List<Bubble> bubbles) {
Mark Renouf3bc5b362019-04-05 14:37:59 -0400454 }
455
456 @Override
457 public void onSelectionChanged(Bubble selectedBubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400458 if (mStackView != null) {
459 mStackView.setSelectedBubble(selectedBubble);
460 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400461 }
462
463 @Override
464 public void onExpandedChanged(boolean expanded) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400465 if (mStackView != null) {
466 mStackView.setExpanded(expanded);
467 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400468 }
469
470 @Override
471 public void showFlyoutText(Bubble bubble, String text) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400472 if (mStackView != null) {
473 mStackView.animateInFlyoutForBubble(bubble);
474 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400475 }
476
477 @Override
478 public void apply() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400479 mNotificationEntryManager.updateNotifications();
480 updateVisibility();
Mark Renouf3bc5b362019-04-05 14:37:59 -0400481 }
482 };
483
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800484 /**
485 * Lets any listeners know if bubble state has changed.
486 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800487 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800488 if (mStackView == null) {
489 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800490 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800491
Mady Mellord1c78b262018-11-06 18:04:40 -0800492 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800493 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800494 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800495 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
496 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
497 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800498 }
499
500 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800501 * Updates the visibility of the bubbles based on current state.
502 * Does not un-bubble, just hides or un-hides. Will notify any
503 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800504 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800505 public void updateVisibility() {
506 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
507 // Bubbles only appear in unlocked shade
508 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
509 } else if (mStackView != null) {
510 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800511 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800512 updateBubblesShowing();
513 }
514
515 /**
516 * Rect indicating the touchable region for the bubble stack / expanded stack.
517 */
518 public Rect getTouchableRegion() {
519 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
520 return null;
521 }
522 mStackView.getBoundsOnScreen(mTempRect);
523 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800524 }
525
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800526 @VisibleForTesting
527 BubbleStackView getStackView() {
528 return mStackView;
529 }
530
Mady Mellor5549dd22018-11-06 18:07:34 -0800531 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800532 * Whether the notification has been developer configured to bubble and is allowed by the user.
533 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800534 @VisibleForTesting
535 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800536 StatusBarNotification n = entry.notification;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800537 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
538 && n.getNotification().getBubbleMetadata().getIntent() != null;
Julia Reynolds4509ce72019-01-31 13:12:43 -0500539 return hasOverlayIntent && entry.canBubble;
Mady Mellorb4991e62019-01-10 15:14:51 -0800540 }
541
542 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800543 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800544 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800545 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800546 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800547 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800548 return false;
549 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800550 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800551
552 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
553 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
554 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
555
Mady Mellor5549dd22018-11-06 18:07:34 -0800556 boolean hasRemoteInput = false;
557 if (n.getNotification().actions != null) {
558 for (Notification.Action action : n.getNotification().actions) {
559 if (action.getRemoteInputs() != null) {
560 hasRemoteInput = true;
561 break;
562 }
563 }
564 }
Mady Mellor711f9562018-12-05 14:53:46 -0800565 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
566 && n.isOngoing();
567 boolean isMusic = n.getNotification().hasMediaSession();
568 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800569
Mady Mellor5549dd22018-11-06 18:07:34 -0800570 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800571 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
572 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800573 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800574 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800575 || autoBubbleAll;
576 }
577
Mady Mellore80930e2019-03-21 16:00:45 -0700578 private boolean shouldAutoExpand(NotificationEntry entry) {
579 Notification.BubbleMetadata metadata = entry.getBubbleMetadata();
580 return metadata != null && metadata.getAutoExpandBubble()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400581 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellore80930e2019-03-21 16:00:45 -0700582 }
583
Mady Mellor8a1f0252019-04-01 11:31:34 -0700584 private void updateShowInShadeForSuppressNotification(NotificationEntry entry) {
585 boolean suppressNotification = entry.getBubbleMetadata() != null
586 && entry.getBubbleMetadata().getSuppressNotification()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400587 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellor8a1f0252019-04-01 11:31:34 -0700588 entry.setShowInShadeWhenBubble(!suppressNotification);
589 }
590
Mady Mellore80930e2019-03-21 16:00:45 -0700591 /**
592 * Return true if the applications with the package name is running in foreground.
593 *
Steven Wu8ba8ca92019-04-11 10:47:42 -0400594 * @param context application context.
Mady Mellore80930e2019-03-21 16:00:45 -0700595 * @param pkgName application package name.
596 */
Steven Wu8ba8ca92019-04-11 10:47:42 -0400597 public static boolean isForegroundApp(Context context, String pkgName) {
598 ActivityManager am = context.getSystemService(ActivityManager.class);
Mady Mellore80930e2019-03-21 16:00:45 -0700599 List<RunningTaskInfo> tasks = am.getRunningTasks(1 /* maxNum */);
600 return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
601 }
602
Mark Renoufcecc77b2019-01-30 16:32:24 -0500603 /**
604 * This task stack listener is responsible for responding to tasks moved to the front
605 * which are on the default (main) display. When this happens, expanded bubbles must be
606 * collapsed so the user may interact with the app which was just moved to the front.
607 * <p>
608 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
609 * these calls via a main thread Handler.
610 */
611 @MainThread
612 private class BubbleTaskStackListener extends TaskStackChangeListener {
613
Mark Renoufcecc77b2019-01-30 16:32:24 -0500614 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500615 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
616 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400617 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500618 }
619 }
620
Mark Renoufcecc77b2019-01-30 16:32:24 -0500621 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500622 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500623 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400624 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500625 }
626 }
627 }
628
Mady Mellorceced172018-11-27 11:18:39 -0800629 private static boolean shouldAutoBubbleMessages(Context context) {
630 return Settings.Secure.getInt(context.getContentResolver(),
631 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
632 }
633
634 private static boolean shouldAutoBubbleOngoing(Context context) {
635 return Settings.Secure.getInt(context.getContentResolver(),
636 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
637 }
638
639 private static boolean shouldAutoBubbleAll(Context context) {
640 return Settings.Secure.getInt(context.getContentResolver(),
641 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800642 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500643
Mady Mellor3dff9e62019-02-05 18:12:53 -0800644 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500645 return Settings.Secure.getInt(context.getContentResolver(),
646 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
647 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800648
649 private static boolean areBubblesEnabled(Context context) {
650 return Settings.Secure.getInt(context.getContentResolver(),
651 ENABLE_BUBBLES, 1) != 0;
652 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500653
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500654 /** Default stiffness to use for bubble physics animations. */
655 public static int getBubbleStiffness(Context context, int defaultStiffness) {
656 return Settings.Secure.getInt(
657 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
658 }
659
660 /** Default bounciness/damping ratio to use for bubble physics animations. */
661 public static float getBubbleBounciness(Context context, float defaultBounciness) {
662 return Settings.Secure.getInt(
663 context.getContentResolver(),
664 BUBBLE_BOUNCINESS,
665 (int) (defaultBounciness * 100)) / 100f;
666 }
667
Joshua Tsujia19515f2019-02-13 18:02:29 -0500668 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
669 private class BubblesImeListener extends IPinnedStackListener.Stub {
670
671 @Override
672 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
673 }
674
675 @Override
676 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
677 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
678 int displayRotation) throws RemoteException {}
679
680 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500681 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
682 if (mStackView != null && mStackView.getBubbleCount() > 0) {
683 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500684 }
685 }
686
687 @Override
688 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
689 throws RemoteException {}
690
691 @Override
692 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
693
694 @Override
695 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
696 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800697}