blob: 4a2731e5437a069e1058a66052d213a1e042b328 [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;
Mark Renoufc808f062019-02-07 15:20:37 -050029import android.app.ActivityManager.RunningTaskInfo;
Mark Renoufcecc77b2019-01-30 16:32:24 -050030import android.app.ActivityTaskManager;
31import android.app.IActivityTaskManager;
Mady Mellorb4991e62019-01-10 15:14:51 -080032import android.app.INotificationManager;
Mady Mellor5549dd22018-11-06 18:07:34 -080033import android.app.Notification;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080034import android.content.Context;
Joshua Tsujia19515f2019-02-13 18:02:29 -050035import android.content.pm.ParceledListSlice;
Mady Mellord1c78b262018-11-06 18:04:40 -080036import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050037import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080038import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080039import android.provider.Settings;
Mady Mellor5549dd22018-11-06 18:07:34 -080040import android.service.notification.StatusBarNotification;
Mark Renoufcecc77b2019-01-30 16:32:24 -050041import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050042import android.view.IPinnedStackController;
43import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080044import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080045import android.widget.FrameLayout;
46
Mark Renouf08bc42a2019-03-07 13:01:59 -050047import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050048import androidx.annotation.MainThread;
49
Mady Mellorebdbbb92018-11-15 14:36:48 -080050import com.android.internal.annotations.VisibleForTesting;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080051import com.android.internal.statusbar.NotificationVisibility;
Ned Burns01e38212019-01-03 16:32:52 -050052import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080053import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050054import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050055import com.android.systemui.shared.system.ActivityManagerWrapper;
56import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050057import com.android.systemui.shared.system.WindowManagerWrapper;
Ned Burns01e38212019-01-03 16:32:52 -050058import com.android.systemui.statusbar.notification.NotificationEntryListener;
59import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080060import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050061import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050062import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080063import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070064import com.android.systemui.statusbar.policy.ConfigurationController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080065
Mark Renouf08bc42a2019-03-07 13:01:59 -050066import java.lang.annotation.Retention;
67
Jason Monk27d01a622018-12-10 15:57:09 -050068import javax.inject.Inject;
69import javax.inject.Singleton;
70
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080071/**
72 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
73 * Bubbles can be expanded to show more content.
74 *
75 * The controller manages addition, removal, and visible state of bubbles on screen.
76 */
Jason Monk27d01a622018-12-10 15:57:09 -050077@Singleton
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070078public class BubbleController implements BubbleExpandedView.OnBubbleBlockedListener,
79 ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080080
81 private static final String TAG = "BubbleController";
82
Mark Renouf08bc42a2019-03-07 13:01:59 -050083 private static final int MAX_BUBBLES = 5; // TODO: actually enforce this
84
85 @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
Mady Mellor5549dd22018-11-06 18:07:34 -080097 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -050098 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -080099
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800100 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800101 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800102 /** Auto bubble flags set whether different notif types should be presented as a bubble */
Mady Mellorceced172018-11-27 11:18:39 -0800103 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
104 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
105 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800106
107 /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500108 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -0800109
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800110 /** Whether the row of bubble circles are anchored to the top or bottom of the screen. */
111 private static final String ENABLE_BUBBLES_AT_TOP = "experiment_enable_top_bubbles";
112 /** Flag to position the header below the activity view */
113 private static final String ENABLE_BUBBLE_FOOTER = "experiment_enable_bubble_footer";
114
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
133 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider =
134 Dependency.get(NotificationInterruptionStateProvider.class);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800135
Mady Mellorb4991e62019-01-10 15:14:51 -0800136 private INotificationManager mNotificationManagerService;
137
Mady Mellord1c78b262018-11-06 18:04:40 -0800138 // Used for determining view rect for touch interaction
139 private Rect mTempRect = new Rect();
140
Mady Mellor5549dd22018-11-06 18:07:34 -0800141 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800142 * Listener to be notified when some states of the bubbles change.
143 */
144 public interface BubbleStateChangeListener {
145 /**
146 * Called when the stack has bubbles or no longer has bubbles.
147 */
148 void onHasBubblesChanged(boolean hasBubbles);
149 }
150
Mady Mellorcd9b1302018-11-06 18:08:04 -0800151 /**
152 * Listener to find out about stack expansion / collapse events.
153 */
154 public interface BubbleExpandListener {
155 /**
156 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700157 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800158 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800159 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800160 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800161 void onBubbleExpandChanged(boolean isExpanding, String key);
162 }
163
164 /**
165 * Listens for the current state of the status bar and updates the visibility state
166 * of bubbles as needed.
167 */
168 private class StatusBarStateListener implements StatusBarStateController.StateListener {
169 private int mState;
170 /**
171 * Returns the current status bar state.
172 */
173 public int getCurrentState() {
174 return mState;
175 }
176
177 @Override
178 public void onStateChanged(int newState) {
179 mState = newState;
180 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;
Issei Suzukic0387542019-03-08 17:31:14 +0100222 mSurfaceSynchronizer = synchronizer;
Mady Mellor5549dd22018-11-06 18:07:34 -0800223 }
224
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700225 @Override
226 public void onUiModeChanged() {
227 if (mStackView != null) {
228 mStackView.onConfigChanged();
229 }
230 }
231
232 @Override
233 public void onOverlayChanged() {
234 if (mStackView != null) {
235 mStackView.onConfigChanged();
236 }
237 }
238
Mady Mellor5549dd22018-11-06 18:07:34 -0800239 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800240 * Set a listener to be notified when some states of the bubbles change.
241 */
242 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
243 mStateChangeListener = listener;
244 }
245
246 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800247 * Set a listener to be notified of bubble expand events.
248 */
249 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100250 mExpandListener = ((isExpanding, key) -> {
251 if (listener != null) {
252 listener.onBubbleExpandChanged(isExpanding, key);
253 }
254 mStatusBarWindowController.setBubbleExpanded(isExpanding);
255 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800256 if (mStackView != null) {
257 mStackView.setExpandListener(mExpandListener);
258 }
259 }
260
261 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800262 * Whether or not there are bubbles present, regardless of them being visible on the
263 * screen (e.g. if on AOD).
264 */
265 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800266 if (mStackView == null) {
267 return false;
268 }
269 for (Bubble bubble : mBubbleData.getBubbles()) {
270 if (!bubble.entry.isBubbleDismissed()) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800271 return true;
272 }
273 }
274 return false;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800275 }
276
277 /**
278 * Whether the stack of bubbles is expanded or not.
279 */
280 public boolean isStackExpanded() {
281 return mStackView != null && mStackView.isExpanded();
282 }
283
284 /**
285 * Tell the stack of bubbles to collapse.
286 */
287 public void collapseStack() {
288 if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800289 mStackView.collapseStack();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800290 }
291 }
292
293 /**
294 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
295 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500296 void dismissStack(@DismissReason int reason) {
Mady Mellord1c78b262018-11-06 18:04:40 -0800297 if (mStackView == null) {
298 return;
299 }
Mark Renouf08bc42a2019-03-07 13:01:59 -0500300 mStackView.stackDismissed(reason);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800301
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800302 updateVisibility();
Ned Burns01e38212019-01-03 16:32:52 -0500303 mNotificationEntryManager.updateNotifications();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800304 }
305
306 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500307 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
308 * is forwarded a back key down/up pair.
309 */
310 public void performBackPressIfNeeded() {
311 if (mStackView != null) {
312 mStackView.performBackPressIfNeeded();
313 }
314 }
315
316 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800317 * Adds or updates a bubble associated with the provided notification entry.
318 *
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700319 * @param notif the notification associated with this bubble.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800320 * @param updatePosition whether this update should promote the bubble to the top of the stack.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800321 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800322 public void updateBubble(NotificationEntry notif, boolean updatePosition) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800323 if (mStackView != null && mBubbleData.getBubble(notif.key) != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800324 // It's an update
Mady Mellor3dff9e62019-02-05 18:12:53 -0800325 mStackView.updateBubble(notif, updatePosition);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800326 } else {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800327 if (mStackView == null) {
Issei Suzukic0387542019-03-08 17:31:14 +0100328 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
Mady Mellord1c78b262018-11-06 18:04:40 -0800329 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800330 // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim
331 // between bubble and the shade
332 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
333 sbv.addView(mStackView, bubblePosition,
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800334 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
Mady Mellorcd9b1302018-11-06 18:08:04 -0800335 if (mExpandListener != null) {
336 mStackView.setExpandListener(mExpandListener);
337 }
Mady Mellore8e07712019-01-23 12:45:33 -0800338 mStackView.setOnBlockedListener(this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800339 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800340 // It's new
Mady Mellor3dff9e62019-02-05 18:12:53 -0800341 mStackView.addBubble(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800342 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800343 updateVisibility();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800344 }
345
346 /**
347 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500348 * <p>
349 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800350 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500351 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500352 void removeBubble(String key, int reason) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800353 if (mStackView != null) {
Mark Renouf08bc42a2019-03-07 13:01:59 -0500354 mStackView.removeBubble(key, reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800355 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800356 mNotificationEntryManager.updateNotifications();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800357 updateVisibility();
Mady Mellord1c78b262018-11-06 18:04:40 -0800358 }
359
Mady Mellore8e07712019-01-23 12:45:33 -0800360 @Override
361 public void onBubbleBlocked(NotificationEntry entry) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800362 Object[] bubbles = mBubbleData.getBubbles().toArray();
Mady Mellore8e07712019-01-23 12:45:33 -0800363 for (int i = 0; i < bubbles.length; i++) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800364 NotificationEntry e = ((Bubble) bubbles[i]).entry;
Mady Mellore8e07712019-01-23 12:45:33 -0800365 boolean samePackage = entry.notification.getPackageName().equals(
366 e.notification.getPackageName());
367 if (samePackage) {
Mark Renouf08bc42a2019-03-07 13:01:59 -0500368 removeBubble(entry.key, DISMISS_BLOCKED);
Mady Mellore8e07712019-01-23 12:45:33 -0800369 }
370 }
371 }
372
Ned Burns01e38212019-01-03 16:32:52 -0500373 @SuppressWarnings("FieldCanBeLocal")
374 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
375 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500376 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800377 if (!areBubblesEnabled(mContext)) {
378 return;
379 }
380 if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800381 // TODO: handle group summaries
382 // It's a new notif, it shows in the shade and as a bubble
Ned Burns01e38212019-01-03 16:32:52 -0500383 entry.setIsBubble(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800384 entry.setShowInShadeWhenBubble(true);
385 }
386 }
387
388 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500389 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800390 if (!areBubblesEnabled(mContext)) {
391 return;
392 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800393 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
394 updateBubble(entry, true /* updatePosition */);
395 }
396 }
397
398 @Override
399 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800400 if (!areBubblesEnabled(mContext)) {
401 return;
402 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800403 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
404 && alertAgain(entry, entry.notification.getNotification())) {
405 entry.setShowInShadeWhenBubble(true);
406 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800407 updateBubble(entry, true /* updatePosition */);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800408 mStackView.updateDotVisibility(entry.key);
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
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800430 /**
431 * Lets any listeners know if bubble state has changed.
432 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800433 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800434 if (mStackView == null) {
435 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800436 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800437
Mady Mellord1c78b262018-11-06 18:04:40 -0800438 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800439 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800440 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800441 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
442 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
443 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800444 }
445
446 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800447 * Updates the visibility of the bubbles based on current state.
448 * Does not un-bubble, just hides or un-hides. Will notify any
449 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800450 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800451 public void updateVisibility() {
452 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
453 // Bubbles only appear in unlocked shade
454 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
455 } else if (mStackView != null) {
456 mStackView.setVisibility(INVISIBLE);
457 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800458 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800459 updateBubblesShowing();
460 }
461
462 /**
463 * Rect indicating the touchable region for the bubble stack / expanded stack.
464 */
465 public Rect getTouchableRegion() {
466 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
467 return null;
468 }
469 mStackView.getBoundsOnScreen(mTempRect);
470 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800471 }
472
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800473 @VisibleForTesting
474 BubbleStackView getStackView() {
475 return mStackView;
476 }
477
Mady Mellor5549dd22018-11-06 18:07:34 -0800478 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800479 * Whether the notification has been developer configured to bubble and is allowed by the user.
480 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800481 @VisibleForTesting
482 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800483 StatusBarNotification n = entry.notification;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800484 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
485 && n.getNotification().getBubbleMetadata().getIntent() != null;
Julia Reynolds4509ce72019-01-31 13:12:43 -0500486 return hasOverlayIntent && entry.canBubble;
Mady Mellorb4991e62019-01-10 15:14:51 -0800487 }
488
489 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800490 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800491 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800492 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800493 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800494 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800495 return false;
496 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800497 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800498
499 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
500 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
501 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
502
Mady Mellor5549dd22018-11-06 18:07:34 -0800503 boolean hasRemoteInput = false;
504 if (n.getNotification().actions != null) {
505 for (Notification.Action action : n.getNotification().actions) {
506 if (action.getRemoteInputs() != null) {
507 hasRemoteInput = true;
508 break;
509 }
510 }
511 }
Mady Mellor711f9562018-12-05 14:53:46 -0800512 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
513 && n.isOngoing();
514 boolean isMusic = n.getNotification().hasMediaSession();
515 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800516
Mady Mellor5549dd22018-11-06 18:07:34 -0800517 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800518 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
519 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800520 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800521 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800522 || autoBubbleAll;
523 }
524
Mark Renoufcecc77b2019-01-30 16:32:24 -0500525 /**
526 * This task stack listener is responsible for responding to tasks moved to the front
527 * which are on the default (main) display. When this happens, expanded bubbles must be
528 * collapsed so the user may interact with the app which was just moved to the front.
529 * <p>
530 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
531 * these calls via a main thread Handler.
532 */
533 @MainThread
534 private class BubbleTaskStackListener extends TaskStackChangeListener {
535
Mark Renoufcecc77b2019-01-30 16:32:24 -0500536 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500537 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
538 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500539 mStackView.collapseStack();
540 }
541 }
542
Mark Renoufcecc77b2019-01-30 16:32:24 -0500543 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500544 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500545 if (mStackView != null) {
546 mStackView.collapseStack();
547 }
548 }
549 }
550
Mady Mellorceced172018-11-27 11:18:39 -0800551 private static boolean shouldAutoBubbleMessages(Context context) {
552 return Settings.Secure.getInt(context.getContentResolver(),
553 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
554 }
555
556 private static boolean shouldAutoBubbleOngoing(Context context) {
557 return Settings.Secure.getInt(context.getContentResolver(),
558 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
559 }
560
561 private static boolean shouldAutoBubbleAll(Context context) {
562 return Settings.Secure.getInt(context.getContentResolver(),
563 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800564 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500565
Mady Mellor3dff9e62019-02-05 18:12:53 -0800566 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500567 return Settings.Secure.getInt(context.getContentResolver(),
568 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
569 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800570
571 private static boolean areBubblesEnabled(Context context) {
572 return Settings.Secure.getInt(context.getContentResolver(),
573 ENABLE_BUBBLES, 1) != 0;
574 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500575
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800576 /**
577 * Whether bubbles should be positioned at the top of the screen or not.
578 */
579 public static boolean showBubblesAtTop(Context context) {
580 return Settings.Secure.getInt(context.getContentResolver(),
581 ENABLE_BUBBLES_AT_TOP, 0) != 0;
582 }
583
584 /**
585 * Whether the bubble chrome should display as a footer or not (in which case it's a header).
586 */
587 public static boolean useFooter(Context context) {
588 return Settings.Secure.getInt(context.getContentResolver(),
589 ENABLE_BUBBLE_FOOTER, 0) != 0;
590 }
591
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500592 /** Default stiffness to use for bubble physics animations. */
593 public static int getBubbleStiffness(Context context, int defaultStiffness) {
594 return Settings.Secure.getInt(
595 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
596 }
597
598 /** Default bounciness/damping ratio to use for bubble physics animations. */
599 public static float getBubbleBounciness(Context context, float defaultBounciness) {
600 return Settings.Secure.getInt(
601 context.getContentResolver(),
602 BUBBLE_BOUNCINESS,
603 (int) (defaultBounciness * 100)) / 100f;
604 }
605
Joshua Tsujia19515f2019-02-13 18:02:29 -0500606 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
607 private class BubblesImeListener extends IPinnedStackListener.Stub {
608
609 @Override
610 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
611 }
612
613 @Override
614 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
615 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
616 int displayRotation) throws RemoteException {}
617
618 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500619 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
620 if (mStackView != null && mStackView.getBubbleCount() > 0) {
621 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500622 }
623 }
624
625 @Override
626 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
627 throws RemoteException {}
628
629 @Override
630 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
631
632 @Override
633 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
634 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800635}