blob: 9ecc6f448f17360b54f67eeb7c205d2cc9b809b3 [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 Mellorc2ff0112019-03-28 14:18:06 -070019import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL;
20import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL;
21import static android.service.notification.NotificationListenerService.REASON_CANCEL;
22import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL;
Mady Mellor390bff42019-04-05 15:09:01 -070023import static android.view.Display.DEFAULT_DISPLAY;
24import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080025import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080026import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080027import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080028
Mady Mellor3f2efdb2018-11-21 11:30:45 -080029import static com.android.systemui.statusbar.StatusBarState.SHADE;
30import static com.android.systemui.statusbar.notification.NotificationAlertingManager.alertAgain;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080031
Mark Renouf08bc42a2019-03-07 13:01:59 -050032import static java.lang.annotation.RetentionPolicy.SOURCE;
33
Mady Mellorb4991e62019-01-10 15:14:51 -080034import android.annotation.Nullable;
Mady Mellore80930e2019-03-21 16:00:45 -070035import android.app.ActivityManager;
Mark Renoufc808f062019-02-07 15:20:37 -050036import android.app.ActivityManager.RunningTaskInfo;
Mark Renoufcecc77b2019-01-30 16:32:24 -050037import android.app.ActivityTaskManager;
38import android.app.IActivityTaskManager;
Mady Mellor5549dd22018-11-06 18:07:34 -080039import android.app.Notification;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080040import android.content.Context;
Joshua Tsujia19515f2019-02-13 18:02:29 -050041import android.content.pm.ParceledListSlice;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040042import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080043import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050044import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080045import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080046import android.provider.Settings;
Mady Mellor5549dd22018-11-06 18:07:34 -080047import android.service.notification.StatusBarNotification;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040048import android.util.Log;
Mark Renoufcecc77b2019-01-30 16:32:24 -050049import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050050import android.view.IPinnedStackController;
51import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080052import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080053import android.widget.FrameLayout;
54
Mark Renouf08bc42a2019-03-07 13:01:59 -050055import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050056import androidx.annotation.MainThread;
57
Mady Mellorebdbbb92018-11-15 14:36:48 -080058import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070059import com.android.internal.statusbar.IStatusBarService;
Ned Burns01e38212019-01-03 16:32:52 -050060import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080061import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050062import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050063import com.android.systemui.shared.system.ActivityManagerWrapper;
64import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050065import com.android.systemui.shared.system.WindowManagerWrapper;
Mady Mellorc2ff0112019-03-28 14:18:06 -070066import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050067import com.android.systemui.statusbar.notification.NotificationEntryListener;
68import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080069import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050070import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050071import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080072import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070073import com.android.systemui.statusbar.policy.ConfigurationController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080074
Mark Renouf08bc42a2019-03-07 13:01:59 -050075import java.lang.annotation.Retention;
Mady Mellore80930e2019-03-21 16:00:45 -070076import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050077
Jason Monk27d01a622018-12-10 15:57:09 -050078import javax.inject.Inject;
79import javax.inject.Singleton;
80
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080081/**
82 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
83 * Bubbles can be expanded to show more content.
84 *
85 * The controller manages addition, removal, and visible state of bubbles on screen.
86 */
Jason Monk27d01a622018-12-10 15:57:09 -050087@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -040088public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080089
90 private static final String TAG = "BubbleController";
Mark Renouf9ba6cea2019-04-17 11:53:50 -040091 private static final boolean DEBUG = true;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080092
Mark Renouf08bc42a2019-03-07 13:01:59 -050093 @Retention(SOURCE)
94 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mady Melloraa8fef22019-04-11 13:36:40 -070095 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE})
Mark Renouf08bc42a2019-03-07 13:01:59 -050096 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070097
Mark Renouf08bc42a2019-03-07 13:01:59 -050098 static final int DISMISS_USER_GESTURE = 1;
99 static final int DISMISS_AGED = 2;
100 static final int DISMISS_TASK_FINISHED = 3;
101 static final int DISMISS_BLOCKED = 4;
102 static final int DISMISS_NOTIF_CANCEL = 5;
103 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700104 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500105
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -0400106 static final int MAX_BUBBLES = 5; // TODO: actually enforce this
107
Mady Mellor5549dd22018-11-06 18:07:34 -0800108 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -0500109 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -0800110
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800111 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800112 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800113 /** Auto bubble flags set whether different notif types should be presented as a bubble */
Mady Mellorceced172018-11-27 11:18:39 -0800114 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
115 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
116 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800117
118 /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500119 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -0800120
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500121 private static final String BUBBLE_STIFFNESS = "experiment_bubble_stiffness";
122 private static final String BUBBLE_BOUNCINESS = "experiment_bubble_bounciness";
123
Ned Burns01e38212019-01-03 16:32:52 -0500124 private final Context mContext;
125 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500126 private final IActivityTaskManager mActivityTaskManager;
127 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800128 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800129 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100130 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800131
Mady Mellor3dff9e62019-02-05 18:12:53 -0800132 private BubbleData mBubbleData;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800133 private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800134
135 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500136 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800137 private StatusBarStateListener mStatusBarStateListener;
138
Mady Melloraa8fef22019-04-11 13:36:40 -0700139 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700140 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800141
Mady Mellord1c78b262018-11-06 18:04:40 -0800142 // Used for determining view rect for touch interaction
143 private Rect mTempRect = new Rect();
144
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400145 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
146 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
147
Mady Mellor5549dd22018-11-06 18:07:34 -0800148 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800149 * Listener to be notified when some states of the bubbles change.
150 */
151 public interface BubbleStateChangeListener {
152 /**
153 * Called when the stack has bubbles or no longer has bubbles.
154 */
155 void onHasBubblesChanged(boolean hasBubbles);
156 }
157
Mady Mellorcd9b1302018-11-06 18:08:04 -0800158 /**
159 * Listener to find out about stack expansion / collapse events.
160 */
161 public interface BubbleExpandListener {
162 /**
163 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700164 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800165 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800166 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800167 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800168 void onBubbleExpandChanged(boolean isExpanding, String key);
169 }
170
171 /**
172 * Listens for the current state of the status bar and updates the visibility state
173 * of bubbles as needed.
174 */
175 private class StatusBarStateListener implements StatusBarStateController.StateListener {
176 private int mState;
177 /**
178 * Returns the current status bar state.
179 */
180 public int getCurrentState() {
181 return mState;
182 }
183
184 @Override
185 public void onStateChanged(int newState) {
186 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400187 boolean shouldCollapse = (mState != SHADE);
188 if (shouldCollapse) {
189 collapseStack();
190 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800191 updateVisibility();
192 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800193 }
194
Jason Monk27d01a622018-12-10 15:57:09 -0500195 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800196 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Mady Melloraa8fef22019-04-11 13:36:40 -0700197 BubbleData data, ConfigurationController configurationController,
198 NotificationInterruptionStateProvider interruptionStateProvider) {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700199 this(context, statusBarWindowController, data, null /* synchronizer */,
Mady Melloraa8fef22019-04-11 13:36:40 -0700200 configurationController, interruptionStateProvider);
Issei Suzukic0387542019-03-08 17:31:14 +0100201 }
202
203 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700204 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
Mady Melloraa8fef22019-04-11 13:36:40 -0700205 ConfigurationController configurationController,
206 NotificationInterruptionStateProvider interruptionStateProvider) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800207 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700208 mNotificationInterruptionStateProvider = interruptionStateProvider;
209
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700210 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800211
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400212 mBubbleData = data;
213 mBubbleData.setListener(mBubbleDataListener);
214
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800215 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500216 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700217 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellorb4991e62019-01-10 15:14:51 -0800218
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800219 mStatusBarWindowController = statusBarWindowController;
220 mStatusBarStateListener = new StatusBarStateListener();
221 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500222
223 mActivityTaskManager = ActivityTaskManager.getService();
224 mTaskStackListener = new BubbleTaskStackListener();
225 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800226
Joshua Tsujia19515f2019-02-13 18:02:29 -0500227 try {
228 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
229 } catch (RemoteException e) {
230 e.printStackTrace();
231 }
Issei Suzukic0387542019-03-08 17:31:14 +0100232 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700233
234 mBarService = IStatusBarService.Stub.asInterface(
235 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mady Mellor5549dd22018-11-06 18:07:34 -0800236 }
237
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400238 /**
239 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
240 * method initializes the stack view and adds it to the StatusBar just above the scrim.
241 */
242 private void ensureStackViewCreated() {
243 if (mStackView == null) {
244 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
245 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
246 // TODO(b/130237686): When you expand the shade on top of expanded bubble, there is no
247 // scrim between bubble and the shade
248 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
249 sbv.addView(mStackView, bubblePosition,
250 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
251 if (mExpandListener != null) {
252 mStackView.setExpandListener(mExpandListener);
253 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400254 }
255 }
256
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700257 @Override
258 public void onUiModeChanged() {
259 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700260 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700261 }
262 }
263
264 @Override
265 public void onOverlayChanged() {
266 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700267 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700268 }
269 }
270
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400271 @Override
272 public void onConfigChanged(Configuration newConfig) {
273 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
274 mStackView.onOrientationChanged();
275 mOrientation = newConfig.orientation;
276 }
277 }
278
Mady Mellor5549dd22018-11-06 18:07:34 -0800279 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800280 * Set a listener to be notified when some states of the bubbles change.
281 */
282 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
283 mStateChangeListener = listener;
284 }
285
286 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800287 * Set a listener to be notified of bubble expand events.
288 */
289 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100290 mExpandListener = ((isExpanding, key) -> {
291 if (listener != null) {
292 listener.onBubbleExpandChanged(isExpanding, key);
293 }
294 mStatusBarWindowController.setBubbleExpanded(isExpanding);
295 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800296 if (mStackView != null) {
297 mStackView.setExpandListener(mExpandListener);
298 }
299 }
300
301 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800302 * Whether or not there are bubbles present, regardless of them being visible on the
303 * screen (e.g. if on AOD).
304 */
305 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800306 if (mStackView == null) {
307 return false;
308 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400309 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800310 }
311
312 /**
313 * Whether the stack of bubbles is expanded or not.
314 */
315 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400316 return mBubbleData.isExpanded();
317 }
318
319 /**
320 * Tell the stack of bubbles to expand.
321 */
322 public void expandStack() {
323 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800324 }
325
326 /**
327 * Tell the stack of bubbles to collapse.
328 */
329 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400330 mBubbleData.setExpanded(false /* expanded */);
331 }
332
333 void selectBubble(Bubble bubble) {
334 mBubbleData.setSelectedBubble(bubble);
335 }
336
337 @VisibleForTesting
338 void selectBubble(String key) {
339 Bubble bubble = mBubbleData.getBubbleWithKey(key);
340 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800341 }
342
343 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400344 * Request the stack expand if needed, then select the specified Bubble as current.
345 *
346 * @param notificationKey the notification key for the bubble to be selected
347 */
348 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400349 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
350 if (bubble != null) {
351 mBubbleData.setSelectedBubble(bubble);
352 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400353 }
354 }
355
356 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800357 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
358 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500359 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400360 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800361 }
362
363 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500364 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
365 * is forwarded a back key down/up pair.
366 */
367 public void performBackPressIfNeeded() {
368 if (mStackView != null) {
369 mStackView.performBackPressIfNeeded();
370 }
371 }
372
373 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800374 * Adds or updates a bubble associated with the provided notification entry.
375 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400376 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800377 */
Mark Renouff97ed462019-04-05 13:46:24 -0400378 void updateBubble(NotificationEntry notif) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400379 mBubbleData.notificationEntryUpdated(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800380 }
381
382 /**
383 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500384 * <p>
385 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800386 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500387 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500388 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400389 // TEMP: refactor to change this to pass entry
390 Bubble bubble = mBubbleData.getBubbleWithKey(key);
391 if (bubble != null) {
392 mBubbleData.notificationEntryRemoved(bubble.entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800393 }
394 }
395
Ned Burns01e38212019-01-03 16:32:52 -0500396 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700397 private final NotificationRemoveInterceptor mRemoveInterceptor =
398 new NotificationRemoveInterceptor() {
399 @Override
400 public boolean onNotificationRemoveRequested(String key, int reason) {
401 if (!mBubbleData.hasBubbleWithKey(key)) {
402 return false;
403 }
404 NotificationEntry entry = mBubbleData.getBubbleWithKey(key).entry;
405
406 final boolean isClearAll = reason == REASON_CANCEL_ALL;
407 final boolean isUserDimiss = reason == REASON_CANCEL;
408 final boolean isAppCancel = reason == REASON_APP_CANCEL
409 || reason == REASON_APP_CANCEL_ALL;
410
411 // Need to check for !appCancel here because the notification may have
412 // previously been dismissed & entry.isRowDismissed would still be true
413 boolean userRemovedNotif = (entry.isRowDismissed() && !isAppCancel)
414 || isClearAll || isUserDimiss;
415
416 // The bubble notification sticks around in the data as long as the bubble is
417 // not dismissed and the app hasn't cancelled the notification.
418 boolean bubbleExtended = entry.isBubble() && !entry.isBubbleDismissed()
419 && userRemovedNotif;
420 if (bubbleExtended) {
421 entry.setShowInShadeWhenBubble(false);
422 if (mStackView != null) {
423 mStackView.updateDotVisibility(entry.key);
424 }
425 mNotificationEntryManager.updateNotifications();
426 return true;
427 } else if (!userRemovedNotif && !entry.isBubbleDismissed()) {
428 // This wasn't a user removal so we should remove the bubble as well
429 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
430 return false;
431 }
432 return false;
433 }
434 };
435
436 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500437 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
438 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500439 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800440 if (!areBubblesEnabled(mContext)) {
441 return;
442 }
Mady Melloraa8fef22019-04-11 13:36:40 -0700443 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700444 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800445 }
446 }
447
448 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500449 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800450 if (!areBubblesEnabled(mContext)) {
451 return;
452 }
Mady Melloraa8fef22019-04-11 13:36:40 -0700453 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400454 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800455 }
456 }
457
458 @Override
459 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800460 if (!areBubblesEnabled(mContext)) {
461 return;
462 }
Mady Melloraa8fef22019-04-11 13:36:40 -0700463 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry);
464 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.key)) {
465 // It was previously a bubble but no longer a bubble -- lets remove it
466 removeBubble(entry.key, DISMISS_NO_LONGER_BUBBLE);
467 } else if (shouldBubble && alertAgain(entry, entry.notification.getNotification())) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700468 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800469 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mark Renouff97ed462019-04-05 13:46:24 -0400470 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800471 }
472 }
Ned Burns01e38212019-01-03 16:32:52 -0500473 };
474
Mark Renouf71a3af62019-04-08 15:02:54 -0400475 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400476 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400477
Mark Renouf3bc5b362019-04-05 14:37:59 -0400478 @Override
479 public void onBubbleAdded(Bubble bubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400480 ensureStackViewCreated();
481 mStackView.addBubble(bubble);
Mark Renouf3bc5b362019-04-05 14:37:59 -0400482 }
483
484 @Override
Mady Mellorc2ff0112019-03-28 14:18:06 -0700485 public void onBubbleRemoved(Bubble bubble, @DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400486 if (mStackView != null) {
487 mStackView.removeBubble(bubble);
488 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700489 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
490 && !bubble.entry.showInShadeWhenBubble()) {
491 // The bubble is gone & the notification is gone, time to actually remove it
492 mNotificationEntryManager.performRemoveNotification(bubble.entry.notification,
493 0 /* reason */);
Mady Mellora54e9fa2019-04-18 13:26:18 -0700494 } else {
495 // The notification is still in the shade but we've removed the bubble so
496 // lets make sure NoMan knows it's not a bubble anymore
497 try {
498 mBarService.onNotificationBubbleChanged(bubble.getKey(), false /* isBubble */);
499 } catch (RemoteException e) {
500 // Bad things have happened
501 }
502 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400503 }
504
505 public void onBubbleUpdated(Bubble bubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400506 if (mStackView != null) {
507 mStackView.updateBubble(bubble);
508 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400509 }
510
511 @Override
512 public void onOrderChanged(List<Bubble> bubbles) {
Mark Renouf3bc5b362019-04-05 14:37:59 -0400513 }
514
515 @Override
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400516 public void onSelectionChanged(@Nullable Bubble selectedBubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400517 if (mStackView != null) {
518 mStackView.setSelectedBubble(selectedBubble);
519 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400520 }
521
522 @Override
523 public void onExpandedChanged(boolean expanded) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400524 if (mStackView != null) {
525 mStackView.setExpanded(expanded);
526 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400527 }
528
529 @Override
530 public void showFlyoutText(Bubble bubble, String text) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400531 if (mStackView != null) {
532 mStackView.animateInFlyoutForBubble(bubble);
533 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400534 }
535
536 @Override
537 public void apply() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400538 mNotificationEntryManager.updateNotifications();
539 updateVisibility();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400540
541 if (DEBUG) {
542 Log.d(TAG, "[BubbleData]");
543 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
544 mBubbleData.getSelectedBubble()));
545
546 if (mStackView != null) {
547 Log.d(TAG, "[BubbleStackView]");
548 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
549 mStackView.getExpandedBubble()));
550 }
551 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400552 }
553 };
554
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800555 /**
556 * Lets any listeners know if bubble state has changed.
557 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800558 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800559 if (mStackView == null) {
560 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800561 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800562
Mady Mellord1c78b262018-11-06 18:04:40 -0800563 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800564 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800565 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800566 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
567 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
568 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800569 }
570
571 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800572 * Updates the visibility of the bubbles based on current state.
573 * Does not un-bubble, just hides or un-hides. Will notify any
574 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800575 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800576 public void updateVisibility() {
577 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
578 // Bubbles only appear in unlocked shade
579 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
580 } else if (mStackView != null) {
581 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800582 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800583 updateBubblesShowing();
584 }
585
586 /**
587 * Rect indicating the touchable region for the bubble stack / expanded stack.
588 */
589 public Rect getTouchableRegion() {
590 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
591 return null;
592 }
593 mStackView.getBoundsOnScreen(mTempRect);
594 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800595 }
596
Mady Mellor390bff42019-04-05 15:09:01 -0700597 /**
598 * The display id of the expanded view, if the stack is expanded and not occluded by the
599 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
600 */
601 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700602 if (mStackView == null) {
603 return INVALID_DISPLAY;
604 }
Mady Mellor390bff42019-04-05 15:09:01 -0700605 boolean defaultDisplay = context.getDisplay() != null
606 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
607 Bubble b = mStackView.getExpandedBubble();
608 if (defaultDisplay && b != null && isStackExpanded()
609 && !mStatusBarWindowController.getPanelExpanded()) {
610 return b.expandedView.getVirtualDisplayId();
611 }
612 return INVALID_DISPLAY;
613 }
614
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800615 @VisibleForTesting
616 BubbleStackView getStackView() {
617 return mStackView;
618 }
619
Mady Mellor5549dd22018-11-06 18:07:34 -0800620 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800621 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800622 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800623 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800624 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800625 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800626 return false;
627 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800628 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800629
630 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
631 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
632 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
633
Mady Mellor5549dd22018-11-06 18:07:34 -0800634 boolean hasRemoteInput = false;
635 if (n.getNotification().actions != null) {
636 for (Notification.Action action : n.getNotification().actions) {
637 if (action.getRemoteInputs() != null) {
638 hasRemoteInput = true;
639 break;
640 }
641 }
642 }
Mady Mellor711f9562018-12-05 14:53:46 -0800643 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
644 && n.isOngoing();
645 boolean isMusic = n.getNotification().hasMediaSession();
646 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800647
Mady Mellor5549dd22018-11-06 18:07:34 -0800648 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800649 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
650 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800651 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800652 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800653 || autoBubbleAll;
654 }
655
Mady Mellore80930e2019-03-21 16:00:45 -0700656 private boolean shouldAutoExpand(NotificationEntry entry) {
657 Notification.BubbleMetadata metadata = entry.getBubbleMetadata();
658 return metadata != null && metadata.getAutoExpandBubble()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400659 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellore80930e2019-03-21 16:00:45 -0700660 }
661
Mady Mellor8a1f0252019-04-01 11:31:34 -0700662 private void updateShowInShadeForSuppressNotification(NotificationEntry entry) {
663 boolean suppressNotification = entry.getBubbleMetadata() != null
Mady Mellorc529d6d2019-04-16 14:22:52 -0700664 && entry.getBubbleMetadata().isNotificationSuppressed()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400665 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellor8a1f0252019-04-01 11:31:34 -0700666 entry.setShowInShadeWhenBubble(!suppressNotification);
667 }
668
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400669 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
670 StringBuilder sb = new StringBuilder();
671 for (Bubble bubble : bubbles) {
672 if (bubble == null) {
673 sb.append(" <null> !!!!!\n");
674 } else {
675 boolean isSelected = (bubble == selected);
676 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
677 ((isSelected) ? "->" : " "),
678 bubble.getLastActivity(),
679 (bubble.isOngoing() ? 1 : 0),
680 bubble.getKey()));
681 }
682 }
683 return sb.toString();
684 }
685
Mady Mellore80930e2019-03-21 16:00:45 -0700686 /**
687 * Return true if the applications with the package name is running in foreground.
688 *
Steven Wu8ba8ca92019-04-11 10:47:42 -0400689 * @param context application context.
Mady Mellore80930e2019-03-21 16:00:45 -0700690 * @param pkgName application package name.
691 */
Steven Wu8ba8ca92019-04-11 10:47:42 -0400692 public static boolean isForegroundApp(Context context, String pkgName) {
693 ActivityManager am = context.getSystemService(ActivityManager.class);
Mady Mellore80930e2019-03-21 16:00:45 -0700694 List<RunningTaskInfo> tasks = am.getRunningTasks(1 /* maxNum */);
695 return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
696 }
697
Mark Renoufcecc77b2019-01-30 16:32:24 -0500698 /**
699 * This task stack listener is responsible for responding to tasks moved to the front
700 * which are on the default (main) display. When this happens, expanded bubbles must be
701 * collapsed so the user may interact with the app which was just moved to the front.
702 * <p>
703 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
704 * these calls via a main thread Handler.
705 */
706 @MainThread
707 private class BubbleTaskStackListener extends TaskStackChangeListener {
708
Mark Renoufcecc77b2019-01-30 16:32:24 -0500709 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500710 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
711 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400712 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500713 }
714 }
715
Mark Renoufcecc77b2019-01-30 16:32:24 -0500716 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500717 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500718 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400719 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500720 }
721 }
722 }
723
Mady Mellorceced172018-11-27 11:18:39 -0800724 private static boolean shouldAutoBubbleMessages(Context context) {
725 return Settings.Secure.getInt(context.getContentResolver(),
726 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
727 }
728
729 private static boolean shouldAutoBubbleOngoing(Context context) {
730 return Settings.Secure.getInt(context.getContentResolver(),
731 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
732 }
733
734 private static boolean shouldAutoBubbleAll(Context context) {
735 return Settings.Secure.getInt(context.getContentResolver(),
736 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800737 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500738
Mady Mellor3dff9e62019-02-05 18:12:53 -0800739 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500740 return Settings.Secure.getInt(context.getContentResolver(),
741 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
742 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800743
744 private static boolean areBubblesEnabled(Context context) {
745 return Settings.Secure.getInt(context.getContentResolver(),
746 ENABLE_BUBBLES, 1) != 0;
747 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500748
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500749 /** Default stiffness to use for bubble physics animations. */
750 public static int getBubbleStiffness(Context context, int defaultStiffness) {
751 return Settings.Secure.getInt(
752 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
753 }
754
755 /** Default bounciness/damping ratio to use for bubble physics animations. */
756 public static float getBubbleBounciness(Context context, float defaultBounciness) {
757 return Settings.Secure.getInt(
758 context.getContentResolver(),
759 BUBBLE_BOUNCINESS,
760 (int) (defaultBounciness * 100)) / 100f;
761 }
762
Joshua Tsujia19515f2019-02-13 18:02:29 -0500763 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
764 private class BubblesImeListener extends IPinnedStackListener.Stub {
765
766 @Override
767 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
768 }
769
770 @Override
771 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
772 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
773 int displayRotation) throws RemoteException {}
774
775 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500776 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
777 if (mStackView != null && mStackView.getBubbleCount() > 0) {
778 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500779 }
780 }
781
782 @Override
783 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
784 throws RemoteException {}
785
786 @Override
787 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
788
789 @Override
790 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
791 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800792}