blob: f5abda23d66dd15b65f82d1aae6ecbc2da77d981 [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
Mady Mellorb4991e62019-01-10 15:14:51 -080026import android.annotation.Nullable;
Mark Renoufcecc77b2019-01-30 16:32:24 -050027import android.app.ActivityManager;
Mark Renoufc808f062019-02-07 15:20:37 -050028import android.app.ActivityManager.RunningTaskInfo;
Mark Renoufcecc77b2019-01-30 16:32:24 -050029import android.app.ActivityTaskManager;
30import android.app.IActivityTaskManager;
Mady Mellorb4991e62019-01-10 15:14:51 -080031import android.app.INotificationManager;
Mady Mellor5549dd22018-11-06 18:07:34 -080032import android.app.Notification;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080033import android.content.Context;
Joshua Tsujia19515f2019-02-13 18:02:29 -050034import android.content.pm.ParceledListSlice;
Mady Mellord1c78b262018-11-06 18:04:40 -080035import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050036import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080037import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080038import android.provider.Settings;
Mady Mellor5549dd22018-11-06 18:07:34 -080039import android.service.notification.StatusBarNotification;
Mark Renoufcecc77b2019-01-30 16:32:24 -050040import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050041import android.view.IPinnedStackController;
42import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080043import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080044import android.widget.FrameLayout;
45
Mark Renouf658c6bc2019-01-30 10:26:54 -050046import androidx.annotation.MainThread;
47
Mady Mellorebdbbb92018-11-15 14:36:48 -080048import com.android.internal.annotations.VisibleForTesting;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080049import com.android.internal.statusbar.NotificationVisibility;
Ned Burns01e38212019-01-03 16:32:52 -050050import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080051import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050052import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050053import com.android.systemui.shared.system.ActivityManagerWrapper;
54import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050055import com.android.systemui.shared.system.WindowManagerWrapper;
Ned Burns01e38212019-01-03 16:32:52 -050056import com.android.systemui.statusbar.notification.NotificationEntryListener;
57import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080058import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050059import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050060import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080061import com.android.systemui.statusbar.phone.StatusBarWindowController;
62
Mark Renoufcecc77b2019-01-30 16:32:24 -050063import java.util.List;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080064
Jason Monk27d01a622018-12-10 15:57:09 -050065import javax.inject.Inject;
66import javax.inject.Singleton;
67
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080068/**
69 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
70 * Bubbles can be expanded to show more content.
71 *
72 * The controller manages addition, removal, and visible state of bubbles on screen.
73 */
Jason Monk27d01a622018-12-10 15:57:09 -050074@Singleton
Mady Mellor3d82e682019-02-05 13:34:48 -080075public class BubbleController implements BubbleExpandedView.OnBubbleBlockedListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080076 private static final int MAX_BUBBLES = 5; // TODO: actually enforce this
77
78 private static final String TAG = "BubbleController";
79
Mady Mellor5549dd22018-11-06 18:07:34 -080080 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -050081 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -080082
Mady Mellor44ee2fe2019-01-30 17:51:16 -080083 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -080084 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Mady Mellor44ee2fe2019-01-30 17:51:16 -080085 /** Auto bubble flags set whether different notif types should be presented as a bubble */
Mady Mellorceced172018-11-27 11:18:39 -080086 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
87 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
88 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellor44ee2fe2019-01-30 17:51:16 -080089
90 /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */
Mark Renouf89b1a4a2018-12-04 14:59:45 -050091 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -080092
Mady Mellor44ee2fe2019-01-30 17:51:16 -080093 /** Whether the row of bubble circles are anchored to the top or bottom of the screen. */
94 private static final String ENABLE_BUBBLES_AT_TOP = "experiment_enable_top_bubbles";
95 /** Flag to position the header below the activity view */
96 private static final String ENABLE_BUBBLE_FOOTER = "experiment_enable_bubble_footer";
97
Joshua Tsuji010c2b12019-02-25 18:11:25 -050098 private static final String BUBBLE_STIFFNESS = "experiment_bubble_stiffness";
99 private static final String BUBBLE_BOUNCINESS = "experiment_bubble_bounciness";
100
Ned Burns01e38212019-01-03 16:32:52 -0500101 private final Context mContext;
102 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500103 private final IActivityTaskManager mActivityTaskManager;
104 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800105 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800106 private BubbleExpandListener mExpandListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800107
Mady Mellor3dff9e62019-02-05 18:12:53 -0800108 private BubbleData mBubbleData;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800109 private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800110
111 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500112 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800113 private StatusBarStateListener mStatusBarStateListener;
114
115 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider =
116 Dependency.get(NotificationInterruptionStateProvider.class);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800117
Mady Mellorb4991e62019-01-10 15:14:51 -0800118 private INotificationManager mNotificationManagerService;
119
Mady Mellord1c78b262018-11-06 18:04:40 -0800120 // Used for determining view rect for touch interaction
121 private Rect mTempRect = new Rect();
122
Mady Mellor5549dd22018-11-06 18:07:34 -0800123 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800124 * Listener to be notified when some states of the bubbles change.
125 */
126 public interface BubbleStateChangeListener {
127 /**
128 * Called when the stack has bubbles or no longer has bubbles.
129 */
130 void onHasBubblesChanged(boolean hasBubbles);
131 }
132
Mady Mellorcd9b1302018-11-06 18:08:04 -0800133 /**
134 * Listener to find out about stack expansion / collapse events.
135 */
136 public interface BubbleExpandListener {
137 /**
138 * Called when the expansion state of the bubble stack changes.
Mady Mellorcd9b1302018-11-06 18:08:04 -0800139 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800140 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800141 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800142 void onBubbleExpandChanged(boolean isExpanding, String key);
143 }
144
145 /**
146 * Listens for the current state of the status bar and updates the visibility state
147 * of bubbles as needed.
148 */
149 private class StatusBarStateListener implements StatusBarStateController.StateListener {
150 private int mState;
151 /**
152 * Returns the current status bar state.
153 */
154 public int getCurrentState() {
155 return mState;
156 }
157
158 @Override
159 public void onStateChanged(int newState) {
160 mState = newState;
161 updateVisibility();
162 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800163 }
164
Jason Monk27d01a622018-12-10 15:57:09 -0500165 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800166 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
167 BubbleData data) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800168 mContext = context;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800169
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800170 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500171 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800172
173 try {
174 mNotificationManagerService = INotificationManager.Stub.asInterface(
175 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
176 } catch (ServiceManager.ServiceNotFoundException e) {
177 e.printStackTrace();
178 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800179
180 mStatusBarWindowController = statusBarWindowController;
181 mStatusBarStateListener = new StatusBarStateListener();
182 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500183
184 mActivityTaskManager = ActivityTaskManager.getService();
185 mTaskStackListener = new BubbleTaskStackListener();
186 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800187
Joshua Tsujia19515f2019-02-13 18:02:29 -0500188 try {
189 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
190 } catch (RemoteException e) {
191 e.printStackTrace();
192 }
193
Mady Mellorcfd06c12019-02-13 14:32:12 -0800194 mBubbleData = data;
Mady Mellor5549dd22018-11-06 18:07:34 -0800195 }
196
197 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800198 * Set a listener to be notified when some states of the bubbles change.
199 */
200 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
201 mStateChangeListener = listener;
202 }
203
204 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800205 * Set a listener to be notified of bubble expand events.
206 */
207 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100208 mExpandListener = ((isExpanding, key) -> {
209 if (listener != null) {
210 listener.onBubbleExpandChanged(isExpanding, key);
211 }
212 mStatusBarWindowController.setBubbleExpanded(isExpanding);
213 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800214 if (mStackView != null) {
215 mStackView.setExpandListener(mExpandListener);
216 }
217 }
218
219 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800220 * Whether or not there are bubbles present, regardless of them being visible on the
221 * screen (e.g. if on AOD).
222 */
223 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800224 if (mStackView == null) {
225 return false;
226 }
227 for (Bubble bubble : mBubbleData.getBubbles()) {
228 if (!bubble.entry.isBubbleDismissed()) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800229 return true;
230 }
231 }
232 return false;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800233 }
234
235 /**
236 * Whether the stack of bubbles is expanded or not.
237 */
238 public boolean isStackExpanded() {
239 return mStackView != null && mStackView.isExpanded();
240 }
241
242 /**
243 * Tell the stack of bubbles to collapse.
244 */
245 public void collapseStack() {
246 if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800247 mStackView.collapseStack();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800248 }
249 }
250
251 /**
252 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
253 */
Ned Burns01e38212019-01-03 16:32:52 -0500254 void dismissStack() {
Mady Mellord1c78b262018-11-06 18:04:40 -0800255 if (mStackView == null) {
256 return;
257 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800258 mStackView.stackDismissed();
259
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800260 updateVisibility();
Ned Burns01e38212019-01-03 16:32:52 -0500261 mNotificationEntryManager.updateNotifications();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800262 }
263
264 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500265 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
266 * is forwarded a back key down/up pair.
267 */
268 public void performBackPressIfNeeded() {
269 if (mStackView != null) {
270 mStackView.performBackPressIfNeeded();
271 }
272 }
273
274 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800275 * Adds or updates a bubble associated with the provided notification entry.
276 *
277 * @param notif the notification associated with this bubble.
278 * @param updatePosition whether this update should promote the bubble to the top of the stack.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800279 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800280 public void updateBubble(NotificationEntry notif, boolean updatePosition) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800281 if (mStackView != null && mBubbleData.getBubble(notif.key) != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800282 // It's an update
Mady Mellor3dff9e62019-02-05 18:12:53 -0800283 mStackView.updateBubble(notif, updatePosition);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800284 } else {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800285 if (mStackView == null) {
Mady Mellorcfd06c12019-02-13 14:32:12 -0800286 mStackView = new BubbleStackView(mContext, mBubbleData);
Mady Mellord1c78b262018-11-06 18:04:40 -0800287 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800288 // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim
289 // between bubble and the shade
290 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
291 sbv.addView(mStackView, bubblePosition,
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800292 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
Mady Mellorcd9b1302018-11-06 18:08:04 -0800293 if (mExpandListener != null) {
294 mStackView.setExpandListener(mExpandListener);
295 }
Mady Mellore8e07712019-01-23 12:45:33 -0800296 mStackView.setOnBlockedListener(this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800297 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800298 // It's new
Mady Mellor3dff9e62019-02-05 18:12:53 -0800299 mStackView.addBubble(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800300 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800301 updateVisibility();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800302 }
303
304 /**
305 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500306 * <p>
307 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800308 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500309 @MainThread
Ned Burns01e38212019-01-03 16:32:52 -0500310 void removeBubble(String key) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800311 if (mStackView != null) {
312 mStackView.removeBubble(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800313 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800314 mNotificationEntryManager.updateNotifications();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800315 updateVisibility();
Mady Mellord1c78b262018-11-06 18:04:40 -0800316 }
317
Mady Mellore8e07712019-01-23 12:45:33 -0800318 @Override
319 public void onBubbleBlocked(NotificationEntry entry) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800320 Object[] bubbles = mBubbleData.getBubbles().toArray();
Mady Mellore8e07712019-01-23 12:45:33 -0800321 for (int i = 0; i < bubbles.length; i++) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800322 NotificationEntry e = ((Bubble) bubbles[i]).entry;
Mady Mellore8e07712019-01-23 12:45:33 -0800323 boolean samePackage = entry.notification.getPackageName().equals(
324 e.notification.getPackageName());
325 if (samePackage) {
326 removeBubble(entry.key);
327 }
328 }
329 }
330
Ned Burns01e38212019-01-03 16:32:52 -0500331 @SuppressWarnings("FieldCanBeLocal")
332 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
333 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500334 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800335 if (!areBubblesEnabled(mContext)) {
336 return;
337 }
338 if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800339 // TODO: handle group summaries
340 // It's a new notif, it shows in the shade and as a bubble
Ned Burns01e38212019-01-03 16:32:52 -0500341 entry.setIsBubble(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800342 entry.setShowInShadeWhenBubble(true);
343 }
344 }
345
346 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500347 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800348 if (!areBubblesEnabled(mContext)) {
349 return;
350 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800351 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
352 updateBubble(entry, true /* updatePosition */);
353 }
354 }
355
356 @Override
357 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800358 if (!areBubblesEnabled(mContext)) {
359 return;
360 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800361 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
362 && alertAgain(entry, entry.notification.getNotification())) {
363 entry.setShowInShadeWhenBubble(true);
364 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800365 updateBubble(entry, true /* updatePosition */);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800366 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800367 }
368 }
369
370 @Override
371 public void onEntryRemoved(NotificationEntry entry,
372 @Nullable NotificationVisibility visibility,
373 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800374 if (!areBubblesEnabled(mContext)) {
375 return;
376 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800377 entry.setShowInShadeWhenBubble(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800378 if (mStackView != null) {
379 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800380 }
381 if (!removedByUser) {
382 // This was a cancel so we should remove the bubble
383 removeBubble(entry.key);
Ned Burns01e38212019-01-03 16:32:52 -0500384 }
385 }
386 };
387
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800388 /**
389 * Lets any listeners know if bubble state has changed.
390 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800391 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800392 if (mStackView == null) {
393 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800394 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800395
Mady Mellord1c78b262018-11-06 18:04:40 -0800396 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800397 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800398 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800399 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
400 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
401 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800402 }
403
404 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800405 * Updates the visibility of the bubbles based on current state.
406 * Does not un-bubble, just hides or un-hides. Will notify any
407 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800408 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800409 public void updateVisibility() {
410 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
411 // Bubbles only appear in unlocked shade
412 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
413 } else if (mStackView != null) {
414 mStackView.setVisibility(INVISIBLE);
415 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800416 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800417 updateBubblesShowing();
418 }
419
420 /**
421 * Rect indicating the touchable region for the bubble stack / expanded stack.
422 */
423 public Rect getTouchableRegion() {
424 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
425 return null;
426 }
427 mStackView.getBoundsOnScreen(mTempRect);
428 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800429 }
430
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800431 @VisibleForTesting
432 BubbleStackView getStackView() {
433 return mStackView;
434 }
435
Mady Mellor5549dd22018-11-06 18:07:34 -0800436 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800437 * Whether the notification has been developer configured to bubble and is allowed by the user.
438 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800439 @VisibleForTesting
440 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800441 StatusBarNotification n = entry.notification;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800442 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
443 && n.getNotification().getBubbleMetadata().getIntent() != null;
Julia Reynolds4509ce72019-01-31 13:12:43 -0500444 return hasOverlayIntent && entry.canBubble;
Mady Mellorb4991e62019-01-10 15:14:51 -0800445 }
446
447 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800448 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800449 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800450 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800451 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800452 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800453 return false;
454 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800455 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800456
457 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
458 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
459 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
460
Mady Mellor5549dd22018-11-06 18:07:34 -0800461 boolean hasRemoteInput = false;
462 if (n.getNotification().actions != null) {
463 for (Notification.Action action : n.getNotification().actions) {
464 if (action.getRemoteInputs() != null) {
465 hasRemoteInput = true;
466 break;
467 }
468 }
469 }
Mady Mellor711f9562018-12-05 14:53:46 -0800470 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
471 && n.isOngoing();
472 boolean isMusic = n.getNotification().hasMediaSession();
473 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800474
Mady Mellor5549dd22018-11-06 18:07:34 -0800475 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800476 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
477 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800478 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800479 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800480 || autoBubbleAll;
481 }
482
Mark Renoufcecc77b2019-01-30 16:32:24 -0500483 /**
484 * This task stack listener is responsible for responding to tasks moved to the front
485 * which are on the default (main) display. When this happens, expanded bubbles must be
486 * collapsed so the user may interact with the app which was just moved to the front.
487 * <p>
488 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
489 * these calls via a main thread Handler.
490 */
491 @MainThread
492 private class BubbleTaskStackListener extends TaskStackChangeListener {
493
494 @Nullable
495 private ActivityManager.StackInfo findStackInfo(int taskId) throws RemoteException {
496 final List<ActivityManager.StackInfo> stackInfoList =
497 mActivityTaskManager.getAllStackInfos();
498 // Iterate through stacks from top to bottom.
499 final int stackCount = stackInfoList.size();
500 for (int stackIndex = 0; stackIndex < stackCount; stackIndex++) {
501 final ActivityManager.StackInfo stackInfo = stackInfoList.get(stackIndex);
502 // Iterate through tasks from top to bottom.
503 for (int taskIndex = stackInfo.taskIds.length - 1; taskIndex >= 0; taskIndex--) {
504 if (stackInfo.taskIds[taskIndex] == taskId) {
505 return stackInfo;
506 }
507 }
508 }
509 return null;
510 }
511
512 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500513 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
514 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500515 mStackView.collapseStack();
516 }
517 }
518
519 /**
520 * This is a workaround for the case when the activity had to be created in a new task.
521 * Existing code in ActivityStackSupervisor checks the display where the activity
522 * ultimately ended up, displays an error message toast, and calls this method instead of
523 * onTaskMovedToFront.
524 */
Mark Renoufcecc77b2019-01-30 16:32:24 -0500525 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500526 public void onActivityLaunchOnSecondaryDisplayFailed(RunningTaskInfo taskInfo) {
527 // TODO(b/124058588): move to ActivityView.StateCallback, filter on virtualDisplay ID
Mark Renoufcecc77b2019-01-30 16:32:24 -0500528 if (mStackView != null) {
529 mStackView.collapseStack();
530 }
531 }
532 }
533
Mady Mellorceced172018-11-27 11:18:39 -0800534 private static boolean shouldAutoBubbleMessages(Context context) {
535 return Settings.Secure.getInt(context.getContentResolver(),
536 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
537 }
538
539 private static boolean shouldAutoBubbleOngoing(Context context) {
540 return Settings.Secure.getInt(context.getContentResolver(),
541 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
542 }
543
544 private static boolean shouldAutoBubbleAll(Context context) {
545 return Settings.Secure.getInt(context.getContentResolver(),
546 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800547 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500548
Mady Mellor3dff9e62019-02-05 18:12:53 -0800549 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500550 return Settings.Secure.getInt(context.getContentResolver(),
551 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
552 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800553
554 private static boolean areBubblesEnabled(Context context) {
555 return Settings.Secure.getInt(context.getContentResolver(),
556 ENABLE_BUBBLES, 1) != 0;
557 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500558
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800559 /**
560 * Whether bubbles should be positioned at the top of the screen or not.
561 */
562 public static boolean showBubblesAtTop(Context context) {
563 return Settings.Secure.getInt(context.getContentResolver(),
564 ENABLE_BUBBLES_AT_TOP, 0) != 0;
565 }
566
567 /**
568 * Whether the bubble chrome should display as a footer or not (in which case it's a header).
569 */
570 public static boolean useFooter(Context context) {
571 return Settings.Secure.getInt(context.getContentResolver(),
572 ENABLE_BUBBLE_FOOTER, 0) != 0;
573 }
574
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500575 /** Default stiffness to use for bubble physics animations. */
576 public static int getBubbleStiffness(Context context, int defaultStiffness) {
577 return Settings.Secure.getInt(
578 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
579 }
580
581 /** Default bounciness/damping ratio to use for bubble physics animations. */
582 public static float getBubbleBounciness(Context context, float defaultBounciness) {
583 return Settings.Secure.getInt(
584 context.getContentResolver(),
585 BUBBLE_BOUNCINESS,
586 (int) (defaultBounciness * 100)) / 100f;
587 }
588
Joshua Tsujia19515f2019-02-13 18:02:29 -0500589 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
590 private class BubblesImeListener extends IPinnedStackListener.Stub {
591
592 @Override
593 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
594 }
595
596 @Override
597 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
598 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
599 int displayRotation) throws RemoteException {}
600
601 @Override
602 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight)
603 throws RemoteException {
604 if (mStackView != null) {
605 mStackView.post(() -> {
606 mStackView.onImeVisibilityChanged(imeVisible, imeHeight);
607 });
608 }
609 }
610
611 @Override
612 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
613 throws RemoteException {}
614
615 @Override
616 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
617
618 @Override
619 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
620 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800621}