blob: ef383add644ed0a9f44e55fcbdb746be3ed4a26c [file] [log] [blame]
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.bubbles;
18
Mady Mellor390bff42019-04-05 15:09:01 -070019import static android.view.Display.DEFAULT_DISPLAY;
20import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080021import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080022import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080023import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080024
Mady Mellor3f2efdb2018-11-21 11:30:45 -080025import static com.android.systemui.statusbar.StatusBarState.SHADE;
26import static com.android.systemui.statusbar.notification.NotificationAlertingManager.alertAgain;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080027
Mark Renouf08bc42a2019-03-07 13:01:59 -050028import static java.lang.annotation.RetentionPolicy.SOURCE;
29
Mady Mellorb4991e62019-01-10 15:14:51 -080030import android.annotation.Nullable;
Mady Mellore80930e2019-03-21 16:00:45 -070031import android.app.ActivityManager;
Mark Renoufc808f062019-02-07 15:20:37 -050032import android.app.ActivityManager.RunningTaskInfo;
Mark Renoufcecc77b2019-01-30 16:32:24 -050033import android.app.ActivityTaskManager;
34import android.app.IActivityTaskManager;
Mady Mellor5549dd22018-11-06 18:07:34 -080035import android.app.Notification;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080036import android.content.Context;
Joshua Tsujia19515f2019-02-13 18:02:29 -050037import android.content.pm.ParceledListSlice;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040038import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080039import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050040import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080041import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080042import android.provider.Settings;
Mady Mellor5549dd22018-11-06 18:07:34 -080043import android.service.notification.StatusBarNotification;
Mark Renoufcecc77b2019-01-30 16:32:24 -050044import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050045import android.view.IPinnedStackController;
46import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080047import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080048import android.widget.FrameLayout;
49
Mark Renouf08bc42a2019-03-07 13:01:59 -050050import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050051import androidx.annotation.MainThread;
52
Mady Mellorebdbbb92018-11-15 14:36:48 -080053import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070054import com.android.internal.statusbar.IStatusBarService;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080055import com.android.internal.statusbar.NotificationVisibility;
Ned Burns01e38212019-01-03 16:32:52 -050056import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080057import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050058import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050059import com.android.systemui.shared.system.ActivityManagerWrapper;
60import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050061import com.android.systemui.shared.system.WindowManagerWrapper;
Ned Burns01e38212019-01-03 16:32:52 -050062import com.android.systemui.statusbar.notification.NotificationEntryListener;
63import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080064import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050065import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050066import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080067import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070068import com.android.systemui.statusbar.policy.ConfigurationController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080069
Mark Renouf08bc42a2019-03-07 13:01:59 -050070import java.lang.annotation.Retention;
Mady Mellore80930e2019-03-21 16:00:45 -070071import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050072
Jason Monk27d01a622018-12-10 15:57:09 -050073import javax.inject.Inject;
74import javax.inject.Singleton;
75
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080076/**
77 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
78 * Bubbles can be expanded to show more content.
79 *
80 * The controller manages addition, removal, and visible state of bubbles on screen.
81 */
Jason Monk27d01a622018-12-10 15:57:09 -050082@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -040083public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080084
85 private static final String TAG = "BubbleController";
86
Mark Renouf08bc42a2019-03-07 13:01:59 -050087 @Retention(SOURCE)
88 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mady Melloraa8fef22019-04-11 13:36:40 -070089 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE})
Mark Renouf08bc42a2019-03-07 13:01:59 -050090 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070091
Mark Renouf08bc42a2019-03-07 13:01:59 -050092 static final int DISMISS_USER_GESTURE = 1;
93 static final int DISMISS_AGED = 2;
94 static final int DISMISS_TASK_FINISHED = 3;
95 static final int DISMISS_BLOCKED = 4;
96 static final int DISMISS_NOTIF_CANCEL = 5;
97 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -070098 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renouf08bc42a2019-03-07 13:01:59 -050099
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -0400100 static final int MAX_BUBBLES = 5; // TODO: actually enforce this
101
Mady Mellor5549dd22018-11-06 18:07:34 -0800102 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -0500103 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -0800104
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800105 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800106 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800107 /** Auto bubble flags set whether different notif types should be presented as a bubble */
Mady Mellorceced172018-11-27 11:18:39 -0800108 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
109 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
110 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800111
112 /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500113 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -0800114
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500115 private static final String BUBBLE_STIFFNESS = "experiment_bubble_stiffness";
116 private static final String BUBBLE_BOUNCINESS = "experiment_bubble_bounciness";
117
Ned Burns01e38212019-01-03 16:32:52 -0500118 private final Context mContext;
119 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500120 private final IActivityTaskManager mActivityTaskManager;
121 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800122 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800123 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100124 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800125
Mady Mellor3dff9e62019-02-05 18:12:53 -0800126 private BubbleData mBubbleData;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800127 private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800128
129 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500130 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800131 private StatusBarStateListener mStatusBarStateListener;
132
Mady Melloraa8fef22019-04-11 13:36:40 -0700133 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700134 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800135
Mady Mellord1c78b262018-11-06 18:04:40 -0800136 // Used for determining view rect for touch interaction
137 private Rect mTempRect = new Rect();
138
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400139 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
140 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
141
Mady Mellor5549dd22018-11-06 18:07:34 -0800142 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800143 * Listener to be notified when some states of the bubbles change.
144 */
145 public interface BubbleStateChangeListener {
146 /**
147 * Called when the stack has bubbles or no longer has bubbles.
148 */
149 void onHasBubblesChanged(boolean hasBubbles);
150 }
151
Mady Mellorcd9b1302018-11-06 18:08:04 -0800152 /**
153 * Listener to find out about stack expansion / collapse events.
154 */
155 public interface BubbleExpandListener {
156 /**
157 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700158 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800159 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800160 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800161 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800162 void onBubbleExpandChanged(boolean isExpanding, String key);
163 }
164
165 /**
166 * Listens for the current state of the status bar and updates the visibility state
167 * of bubbles as needed.
168 */
169 private class StatusBarStateListener implements StatusBarStateController.StateListener {
170 private int mState;
171 /**
172 * Returns the current status bar state.
173 */
174 public int getCurrentState() {
175 return mState;
176 }
177
178 @Override
179 public void onStateChanged(int newState) {
180 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400181 boolean shouldCollapse = (mState != SHADE);
182 if (shouldCollapse) {
183 collapseStack();
184 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800185 updateVisibility();
186 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800187 }
188
Jason Monk27d01a622018-12-10 15:57:09 -0500189 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800190 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Mady Melloraa8fef22019-04-11 13:36:40 -0700191 BubbleData data, ConfigurationController configurationController,
192 NotificationInterruptionStateProvider interruptionStateProvider) {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700193 this(context, statusBarWindowController, data, null /* synchronizer */,
Mady Melloraa8fef22019-04-11 13:36:40 -0700194 configurationController, interruptionStateProvider);
Issei Suzukic0387542019-03-08 17:31:14 +0100195 }
196
197 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700198 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
Mady Melloraa8fef22019-04-11 13:36:40 -0700199 ConfigurationController configurationController,
200 NotificationInterruptionStateProvider interruptionStateProvider) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800201 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700202 mNotificationInterruptionStateProvider = interruptionStateProvider;
203
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700204 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800205
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800206 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500207 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800208
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800209 mStatusBarWindowController = statusBarWindowController;
210 mStatusBarStateListener = new StatusBarStateListener();
211 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500212
213 mActivityTaskManager = ActivityTaskManager.getService();
214 mTaskStackListener = new BubbleTaskStackListener();
215 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800216
Joshua Tsujia19515f2019-02-13 18:02:29 -0500217 try {
218 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
219 } catch (RemoteException e) {
220 e.printStackTrace();
221 }
222
Mady Mellorcfd06c12019-02-13 14:32:12 -0800223 mBubbleData = data;
Mark Renouf3bc5b362019-04-05 14:37:59 -0400224 mBubbleData.setListener(mBubbleDataListener);
Issei Suzukic0387542019-03-08 17:31:14 +0100225 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700226
227 mBarService = IStatusBarService.Stub.asInterface(
228 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mady Mellor5549dd22018-11-06 18:07:34 -0800229 }
230
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400231 /**
232 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
233 * method initializes the stack view and adds it to the StatusBar just above the scrim.
234 */
235 private void ensureStackViewCreated() {
236 if (mStackView == null) {
237 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
238 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
239 // TODO(b/130237686): When you expand the shade on top of expanded bubble, there is no
240 // scrim between bubble and the shade
241 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
242 sbv.addView(mStackView, bubblePosition,
243 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
244 if (mExpandListener != null) {
245 mStackView.setExpandListener(mExpandListener);
246 }
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400247 }
248 }
249
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700250 @Override
251 public void onUiModeChanged() {
252 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700253 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700254 }
255 }
256
257 @Override
258 public void onOverlayChanged() {
259 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700260 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700261 }
262 }
263
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400264 @Override
265 public void onConfigChanged(Configuration newConfig) {
266 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
267 mStackView.onOrientationChanged();
268 mOrientation = newConfig.orientation;
269 }
270 }
271
Mady Mellor5549dd22018-11-06 18:07:34 -0800272 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800273 * Set a listener to be notified when some states of the bubbles change.
274 */
275 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
276 mStateChangeListener = listener;
277 }
278
279 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800280 * Set a listener to be notified of bubble expand events.
281 */
282 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100283 mExpandListener = ((isExpanding, key) -> {
284 if (listener != null) {
285 listener.onBubbleExpandChanged(isExpanding, key);
286 }
287 mStatusBarWindowController.setBubbleExpanded(isExpanding);
288 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800289 if (mStackView != null) {
290 mStackView.setExpandListener(mExpandListener);
291 }
292 }
293
294 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800295 * Whether or not there are bubbles present, regardless of them being visible on the
296 * screen (e.g. if on AOD).
297 */
298 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800299 if (mStackView == null) {
300 return false;
301 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400302 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800303 }
304
305 /**
306 * Whether the stack of bubbles is expanded or not.
307 */
308 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400309 return mBubbleData.isExpanded();
310 }
311
312 /**
313 * Tell the stack of bubbles to expand.
314 */
315 public void expandStack() {
316 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800317 }
318
319 /**
320 * Tell the stack of bubbles to collapse.
321 */
322 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400323 mBubbleData.setExpanded(false /* expanded */);
324 }
325
326 void selectBubble(Bubble bubble) {
327 mBubbleData.setSelectedBubble(bubble);
328 }
329
330 @VisibleForTesting
331 void selectBubble(String key) {
332 Bubble bubble = mBubbleData.getBubbleWithKey(key);
333 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800334 }
335
336 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400337 * Request the stack expand if needed, then select the specified Bubble as current.
338 *
339 * @param notificationKey the notification key for the bubble to be selected
340 */
341 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400342 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
343 if (bubble != null) {
344 mBubbleData.setSelectedBubble(bubble);
345 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400346 }
347 }
348
349 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800350 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
351 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500352 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400353 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800354 }
355
356 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500357 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
358 * is forwarded a back key down/up pair.
359 */
360 public void performBackPressIfNeeded() {
361 if (mStackView != null) {
362 mStackView.performBackPressIfNeeded();
363 }
364 }
365
366 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800367 * Adds or updates a bubble associated with the provided notification entry.
368 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400369 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800370 */
Mark Renouff97ed462019-04-05 13:46:24 -0400371 void updateBubble(NotificationEntry notif) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400372 mBubbleData.notificationEntryUpdated(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800373 }
374
375 /**
376 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500377 * <p>
378 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800379 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500380 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500381 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400382 // TEMP: refactor to change this to pass entry
383 Bubble bubble = mBubbleData.getBubbleWithKey(key);
384 if (bubble != null) {
385 mBubbleData.notificationEntryRemoved(bubble.entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800386 }
387 }
388
Ned Burns01e38212019-01-03 16:32:52 -0500389 @SuppressWarnings("FieldCanBeLocal")
390 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
391 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500392 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800393 if (!areBubblesEnabled(mContext)) {
394 return;
395 }
Mady Melloraa8fef22019-04-11 13:36:40 -0700396 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700397 // TODO: handle group summaries?
398 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800399 }
400 }
401
402 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500403 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800404 if (!areBubblesEnabled(mContext)) {
405 return;
406 }
Mady Melloraa8fef22019-04-11 13:36:40 -0700407 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
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 onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800414 if (!areBubblesEnabled(mContext)) {
415 return;
416 }
Mady Melloraa8fef22019-04-11 13:36:40 -0700417 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry);
418 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.key)) {
419 // It was previously a bubble but no longer a bubble -- lets remove it
420 removeBubble(entry.key, DISMISS_NO_LONGER_BUBBLE);
421 } else if (shouldBubble && alertAgain(entry, entry.notification.getNotification())) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700422 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800423 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mark Renouff97ed462019-04-05 13:46:24 -0400424 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800425 }
426 }
427
428 @Override
429 public void onEntryRemoved(NotificationEntry entry,
430 @Nullable NotificationVisibility visibility,
431 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800432 if (!areBubblesEnabled(mContext)) {
433 return;
434 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800435 entry.setShowInShadeWhenBubble(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800436 if (mStackView != null) {
437 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800438 }
439 if (!removedByUser) {
440 // This was a cancel so we should remove the bubble
Mark Renouf08bc42a2019-03-07 13:01:59 -0500441 removeBubble(entry.key, DISMISS_NOTIF_CANCEL);
Ned Burns01e38212019-01-03 16:32:52 -0500442 }
443 }
444 };
445
Mark Renouf71a3af62019-04-08 15:02:54 -0400446 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400447 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400448
Mark Renouf3bc5b362019-04-05 14:37:59 -0400449 @Override
450 public void onBubbleAdded(Bubble bubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400451 ensureStackViewCreated();
452 mStackView.addBubble(bubble);
Mark Renouf3bc5b362019-04-05 14:37:59 -0400453 }
454
455 @Override
456 public void onBubbleRemoved(Bubble bubble, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400457 if (mStackView != null) {
458 mStackView.removeBubble(bubble);
459 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700460 if (!bubble.entry.showInShadeWhenBubble()) {
461 // The notification is gone & bubble is gone, time to actually remove it
462 mNotificationEntryManager.performRemoveNotification(bubble.entry.notification);
463 } else {
464 // The notification is still in the shade but we've removed the bubble so
465 // lets make sure NoMan knows it's not a bubble anymore
466 try {
467 mBarService.onNotificationBubbleChanged(bubble.getKey(), false /* isBubble */);
468 } catch (RemoteException e) {
469 // Bad things have happened
470 }
471 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400472 }
473
474 public void onBubbleUpdated(Bubble bubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400475 if (mStackView != null) {
476 mStackView.updateBubble(bubble);
477 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400478 }
479
480 @Override
481 public void onOrderChanged(List<Bubble> bubbles) {
Mark Renouf3bc5b362019-04-05 14:37:59 -0400482 }
483
484 @Override
485 public void onSelectionChanged(Bubble selectedBubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400486 if (mStackView != null) {
487 mStackView.setSelectedBubble(selectedBubble);
488 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400489 }
490
491 @Override
492 public void onExpandedChanged(boolean expanded) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400493 if (mStackView != null) {
494 mStackView.setExpanded(expanded);
495 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400496 }
497
498 @Override
499 public void showFlyoutText(Bubble bubble, String text) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400500 if (mStackView != null) {
501 mStackView.animateInFlyoutForBubble(bubble);
502 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400503 }
504
505 @Override
506 public void apply() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400507 mNotificationEntryManager.updateNotifications();
508 updateVisibility();
Mark Renouf3bc5b362019-04-05 14:37:59 -0400509 }
510 };
511
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800512 /**
513 * Lets any listeners know if bubble state has changed.
514 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800515 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800516 if (mStackView == null) {
517 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800518 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800519
Mady Mellord1c78b262018-11-06 18:04:40 -0800520 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800521 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800522 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800523 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
524 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
525 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800526 }
527
528 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800529 * Updates the visibility of the bubbles based on current state.
530 * Does not un-bubble, just hides or un-hides. Will notify any
531 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800532 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800533 public void updateVisibility() {
534 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
535 // Bubbles only appear in unlocked shade
536 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
537 } else if (mStackView != null) {
538 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800539 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800540 updateBubblesShowing();
541 }
542
543 /**
544 * Rect indicating the touchable region for the bubble stack / expanded stack.
545 */
546 public Rect getTouchableRegion() {
547 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
548 return null;
549 }
550 mStackView.getBoundsOnScreen(mTempRect);
551 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800552 }
553
Mady Mellor390bff42019-04-05 15:09:01 -0700554 /**
555 * The display id of the expanded view, if the stack is expanded and not occluded by the
556 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
557 */
558 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700559 if (mStackView == null) {
560 return INVALID_DISPLAY;
561 }
Mady Mellor390bff42019-04-05 15:09:01 -0700562 boolean defaultDisplay = context.getDisplay() != null
563 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
564 Bubble b = mStackView.getExpandedBubble();
565 if (defaultDisplay && b != null && isStackExpanded()
566 && !mStatusBarWindowController.getPanelExpanded()) {
567 return b.expandedView.getVirtualDisplayId();
568 }
569 return INVALID_DISPLAY;
570 }
571
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800572 @VisibleForTesting
573 BubbleStackView getStackView() {
574 return mStackView;
575 }
576
Mady Mellor5549dd22018-11-06 18:07:34 -0800577 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800578 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800579 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800580 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800581 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800582 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800583 return false;
584 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800585 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800586
587 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
588 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
589 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
590
Mady Mellor5549dd22018-11-06 18:07:34 -0800591 boolean hasRemoteInput = false;
592 if (n.getNotification().actions != null) {
593 for (Notification.Action action : n.getNotification().actions) {
594 if (action.getRemoteInputs() != null) {
595 hasRemoteInput = true;
596 break;
597 }
598 }
599 }
Mady Mellor711f9562018-12-05 14:53:46 -0800600 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
601 && n.isOngoing();
602 boolean isMusic = n.getNotification().hasMediaSession();
603 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800604
Mady Mellor5549dd22018-11-06 18:07:34 -0800605 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800606 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
607 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800608 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800609 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800610 || autoBubbleAll;
611 }
612
Mady Mellore80930e2019-03-21 16:00:45 -0700613 private boolean shouldAutoExpand(NotificationEntry entry) {
614 Notification.BubbleMetadata metadata = entry.getBubbleMetadata();
615 return metadata != null && metadata.getAutoExpandBubble()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400616 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellore80930e2019-03-21 16:00:45 -0700617 }
618
Mady Mellor8a1f0252019-04-01 11:31:34 -0700619 private void updateShowInShadeForSuppressNotification(NotificationEntry entry) {
620 boolean suppressNotification = entry.getBubbleMetadata() != null
Mady Mellorc529d6d2019-04-16 14:22:52 -0700621 && entry.getBubbleMetadata().isNotificationSuppressed()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400622 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellor8a1f0252019-04-01 11:31:34 -0700623 entry.setShowInShadeWhenBubble(!suppressNotification);
624 }
625
Mady Mellore80930e2019-03-21 16:00:45 -0700626 /**
627 * Return true if the applications with the package name is running in foreground.
628 *
Steven Wu8ba8ca92019-04-11 10:47:42 -0400629 * @param context application context.
Mady Mellore80930e2019-03-21 16:00:45 -0700630 * @param pkgName application package name.
631 */
Steven Wu8ba8ca92019-04-11 10:47:42 -0400632 public static boolean isForegroundApp(Context context, String pkgName) {
633 ActivityManager am = context.getSystemService(ActivityManager.class);
Mady Mellore80930e2019-03-21 16:00:45 -0700634 List<RunningTaskInfo> tasks = am.getRunningTasks(1 /* maxNum */);
635 return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
636 }
637
Mark Renoufcecc77b2019-01-30 16:32:24 -0500638 /**
639 * This task stack listener is responsible for responding to tasks moved to the front
640 * which are on the default (main) display. When this happens, expanded bubbles must be
641 * collapsed so the user may interact with the app which was just moved to the front.
642 * <p>
643 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
644 * these calls via a main thread Handler.
645 */
646 @MainThread
647 private class BubbleTaskStackListener extends TaskStackChangeListener {
648
Mark Renoufcecc77b2019-01-30 16:32:24 -0500649 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500650 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
651 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400652 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500653 }
654 }
655
Mark Renoufcecc77b2019-01-30 16:32:24 -0500656 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500657 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500658 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400659 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500660 }
661 }
662 }
663
Mady Mellorceced172018-11-27 11:18:39 -0800664 private static boolean shouldAutoBubbleMessages(Context context) {
665 return Settings.Secure.getInt(context.getContentResolver(),
666 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
667 }
668
669 private static boolean shouldAutoBubbleOngoing(Context context) {
670 return Settings.Secure.getInt(context.getContentResolver(),
671 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
672 }
673
674 private static boolean shouldAutoBubbleAll(Context context) {
675 return Settings.Secure.getInt(context.getContentResolver(),
676 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800677 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500678
Mady Mellor3dff9e62019-02-05 18:12:53 -0800679 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500680 return Settings.Secure.getInt(context.getContentResolver(),
681 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
682 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800683
684 private static boolean areBubblesEnabled(Context context) {
685 return Settings.Secure.getInt(context.getContentResolver(),
686 ENABLE_BUBBLES, 1) != 0;
687 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500688
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500689 /** Default stiffness to use for bubble physics animations. */
690 public static int getBubbleStiffness(Context context, int defaultStiffness) {
691 return Settings.Secure.getInt(
692 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
693 }
694
695 /** Default bounciness/damping ratio to use for bubble physics animations. */
696 public static float getBubbleBounciness(Context context, float defaultBounciness) {
697 return Settings.Secure.getInt(
698 context.getContentResolver(),
699 BUBBLE_BOUNCINESS,
700 (int) (defaultBounciness * 100)) / 100f;
701 }
702
Joshua Tsujia19515f2019-02-13 18:02:29 -0500703 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
704 private class BubblesImeListener extends IPinnedStackListener.Stub {
705
706 @Override
707 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
708 }
709
710 @Override
711 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
712 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
713 int displayRotation) throws RemoteException {}
714
715 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500716 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
717 if (mStackView != null && mStackView.getBubbleCount() > 0) {
718 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500719 }
720 }
721
722 @Override
723 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
724 throws RemoteException {}
725
726 @Override
727 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
728
729 @Override
730 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
731 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800732}