blob: e0d3aceab8bd28962b95a503780b538187adb843 [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;
28import android.app.ActivityTaskManager;
29import android.app.IActivityTaskManager;
Mady Mellorb4991e62019-01-10 15:14:51 -080030import android.app.INotificationManager;
Mady Mellor5549dd22018-11-06 18:07:34 -080031import android.app.Notification;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080032import android.content.Context;
Joshua Tsujia19515f2019-02-13 18:02:29 -050033import android.content.pm.ParceledListSlice;
Mady Mellord1c78b262018-11-06 18:04:40 -080034import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050035import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080036import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080037import android.provider.Settings;
Mady Mellor5549dd22018-11-06 18:07:34 -080038import android.service.notification.StatusBarNotification;
Mark Renoufcecc77b2019-01-30 16:32:24 -050039import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050040import android.view.IPinnedStackController;
41import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080042import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080043import android.widget.FrameLayout;
44
Mark Renouf658c6bc2019-01-30 10:26:54 -050045import androidx.annotation.MainThread;
46
Mady Mellorebdbbb92018-11-15 14:36:48 -080047import com.android.internal.annotations.VisibleForTesting;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080048import com.android.internal.statusbar.NotificationVisibility;
Ned Burns01e38212019-01-03 16:32:52 -050049import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080050import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050051import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050052import com.android.systemui.shared.system.ActivityManagerWrapper;
53import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050054import com.android.systemui.shared.system.WindowManagerWrapper;
Ned Burns01e38212019-01-03 16:32:52 -050055import com.android.systemui.statusbar.notification.NotificationEntryListener;
56import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080057import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050058import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050059import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080060import com.android.systemui.statusbar.phone.StatusBarWindowController;
61
Mark Renoufcecc77b2019-01-30 16:32:24 -050062import java.util.List;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080063
Jason Monk27d01a622018-12-10 15:57:09 -050064import javax.inject.Inject;
65import javax.inject.Singleton;
66
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080067/**
68 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
69 * Bubbles can be expanded to show more content.
70 *
71 * The controller manages addition, removal, and visible state of bubbles on screen.
72 */
Jason Monk27d01a622018-12-10 15:57:09 -050073@Singleton
Mady Mellor3d82e682019-02-05 13:34:48 -080074public class BubbleController implements BubbleExpandedView.OnBubbleBlockedListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080075 private static final int MAX_BUBBLES = 5; // TODO: actually enforce this
76
77 private static final String TAG = "BubbleController";
78
Mady Mellor5549dd22018-11-06 18:07:34 -080079 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -050080 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -080081
Mady Mellorf6e3ac02019-01-29 10:37:52 -080082 // Secure settings flags
83 // Feature level flag
84 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
85 // Auto bubble flags set whether different notification 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 Mellorf6e3ac02019-01-29 10:37:52 -080089 // Use an activity view for an auto-bubbled notification if it has an appropriate content intent
Mark Renouf89b1a4a2018-12-04 14:59:45 -050090 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -080091
Ned Burns01e38212019-01-03 16:32:52 -050092 private final Context mContext;
93 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -050094 private final IActivityTaskManager mActivityTaskManager;
95 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -080096 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -080097 private BubbleExpandListener mExpandListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080098
Mady Mellor3dff9e62019-02-05 18:12:53 -080099 private BubbleData mBubbleData;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800100 private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800101
102 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500103 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800104 private StatusBarStateListener mStatusBarStateListener;
105
106 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider =
107 Dependency.get(NotificationInterruptionStateProvider.class);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800108
Mady Mellorb4991e62019-01-10 15:14:51 -0800109 private INotificationManager mNotificationManagerService;
110
Mady Mellord1c78b262018-11-06 18:04:40 -0800111 // Used for determining view rect for touch interaction
112 private Rect mTempRect = new Rect();
113
Mady Mellor5549dd22018-11-06 18:07:34 -0800114 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800115 * Listener to be notified when some states of the bubbles change.
116 */
117 public interface BubbleStateChangeListener {
118 /**
119 * Called when the stack has bubbles or no longer has bubbles.
120 */
121 void onHasBubblesChanged(boolean hasBubbles);
122 }
123
Mady Mellorcd9b1302018-11-06 18:08:04 -0800124 /**
125 * Listener to find out about stack expansion / collapse events.
126 */
127 public interface BubbleExpandListener {
128 /**
129 * Called when the expansion state of the bubble stack changes.
Mady Mellorcd9b1302018-11-06 18:08:04 -0800130 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800131 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800132 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800133 void onBubbleExpandChanged(boolean isExpanding, String key);
134 }
135
136 /**
137 * Listens for the current state of the status bar and updates the visibility state
138 * of bubbles as needed.
139 */
140 private class StatusBarStateListener implements StatusBarStateController.StateListener {
141 private int mState;
142 /**
143 * Returns the current status bar state.
144 */
145 public int getCurrentState() {
146 return mState;
147 }
148
149 @Override
150 public void onStateChanged(int newState) {
151 mState = newState;
152 updateVisibility();
153 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800154 }
155
Jason Monk27d01a622018-12-10 15:57:09 -0500156 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800157 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
158 BubbleData data) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800159 mContext = context;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800160
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800161 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500162 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800163
164 try {
165 mNotificationManagerService = INotificationManager.Stub.asInterface(
166 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
167 } catch (ServiceManager.ServiceNotFoundException e) {
168 e.printStackTrace();
169 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800170
171 mStatusBarWindowController = statusBarWindowController;
172 mStatusBarStateListener = new StatusBarStateListener();
173 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500174
175 mActivityTaskManager = ActivityTaskManager.getService();
176 mTaskStackListener = new BubbleTaskStackListener();
177 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800178
Joshua Tsujia19515f2019-02-13 18:02:29 -0500179 try {
180 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
181 } catch (RemoteException e) {
182 e.printStackTrace();
183 }
184
Mady Mellorcfd06c12019-02-13 14:32:12 -0800185 mBubbleData = data;
Mady Mellor5549dd22018-11-06 18:07:34 -0800186 }
187
188 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800189 * Set a listener to be notified when some states of the bubbles change.
190 */
191 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
192 mStateChangeListener = listener;
193 }
194
195 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800196 * Set a listener to be notified of bubble expand events.
197 */
198 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100199 mExpandListener = ((isExpanding, key) -> {
200 if (listener != null) {
201 listener.onBubbleExpandChanged(isExpanding, key);
202 }
203 mStatusBarWindowController.setBubbleExpanded(isExpanding);
204 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800205 if (mStackView != null) {
206 mStackView.setExpandListener(mExpandListener);
207 }
208 }
209
210 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800211 * Whether or not there are bubbles present, regardless of them being visible on the
212 * screen (e.g. if on AOD).
213 */
214 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800215 if (mStackView == null) {
216 return false;
217 }
218 for (Bubble bubble : mBubbleData.getBubbles()) {
219 if (!bubble.entry.isBubbleDismissed()) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800220 return true;
221 }
222 }
223 return false;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800224 }
225
226 /**
227 * Whether the stack of bubbles is expanded or not.
228 */
229 public boolean isStackExpanded() {
230 return mStackView != null && mStackView.isExpanded();
231 }
232
233 /**
234 * Tell the stack of bubbles to collapse.
235 */
236 public void collapseStack() {
237 if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800238 mStackView.collapseStack();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800239 }
240 }
241
242 /**
243 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
244 */
Ned Burns01e38212019-01-03 16:32:52 -0500245 void dismissStack() {
Mady Mellord1c78b262018-11-06 18:04:40 -0800246 if (mStackView == null) {
247 return;
248 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800249 mStackView.stackDismissed();
250
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800251 updateVisibility();
Ned Burns01e38212019-01-03 16:32:52 -0500252 mNotificationEntryManager.updateNotifications();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800253 }
254
255 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500256 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
257 * is forwarded a back key down/up pair.
258 */
259 public void performBackPressIfNeeded() {
260 if (mStackView != null) {
261 mStackView.performBackPressIfNeeded();
262 }
263 }
264
265 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800266 * Adds or updates a bubble associated with the provided notification entry.
267 *
268 * @param notif the notification associated with this bubble.
269 * @param updatePosition whether this update should promote the bubble to the top of the stack.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800270 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800271 public void updateBubble(NotificationEntry notif, boolean updatePosition) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800272 if (mStackView != null && mBubbleData.getBubble(notif.key) != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800273 // It's an update
Mady Mellor3dff9e62019-02-05 18:12:53 -0800274 mStackView.updateBubble(notif, updatePosition);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800275 } else {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800276 if (mStackView == null) {
Mady Mellorcfd06c12019-02-13 14:32:12 -0800277 mStackView = new BubbleStackView(mContext, mBubbleData);
Mady Mellord1c78b262018-11-06 18:04:40 -0800278 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800279 // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim
280 // between bubble and the shade
281 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
282 sbv.addView(mStackView, bubblePosition,
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800283 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
Mady Mellorcd9b1302018-11-06 18:08:04 -0800284 if (mExpandListener != null) {
285 mStackView.setExpandListener(mExpandListener);
286 }
Mady Mellore8e07712019-01-23 12:45:33 -0800287 mStackView.setOnBlockedListener(this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800288 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800289 // It's new
Mady Mellor3dff9e62019-02-05 18:12:53 -0800290 mStackView.addBubble(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800291 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800292 updateVisibility();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800293 }
294
295 /**
296 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500297 * <p>
298 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800299 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500300 @MainThread
Ned Burns01e38212019-01-03 16:32:52 -0500301 void removeBubble(String key) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800302 if (mStackView != null) {
303 mStackView.removeBubble(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800304 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800305 mNotificationEntryManager.updateNotifications();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800306 updateVisibility();
Mady Mellord1c78b262018-11-06 18:04:40 -0800307 }
308
Mady Mellore8e07712019-01-23 12:45:33 -0800309 @Override
310 public void onBubbleBlocked(NotificationEntry entry) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800311 Object[] bubbles = mBubbleData.getBubbles().toArray();
Mady Mellore8e07712019-01-23 12:45:33 -0800312 for (int i = 0; i < bubbles.length; i++) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800313 NotificationEntry e = ((Bubble) bubbles[i]).entry;
Mady Mellore8e07712019-01-23 12:45:33 -0800314 boolean samePackage = entry.notification.getPackageName().equals(
315 e.notification.getPackageName());
316 if (samePackage) {
317 removeBubble(entry.key);
318 }
319 }
320 }
321
Ned Burns01e38212019-01-03 16:32:52 -0500322 @SuppressWarnings("FieldCanBeLocal")
323 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
324 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500325 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800326 if (!areBubblesEnabled(mContext)) {
327 return;
328 }
329 if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800330 // TODO: handle group summaries
331 // It's a new notif, it shows in the shade and as a bubble
Ned Burns01e38212019-01-03 16:32:52 -0500332 entry.setIsBubble(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800333 entry.setShowInShadeWhenBubble(true);
334 }
335 }
336
337 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500338 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800339 if (!areBubblesEnabled(mContext)) {
340 return;
341 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800342 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
343 updateBubble(entry, true /* updatePosition */);
344 }
345 }
346
347 @Override
348 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800349 if (!areBubblesEnabled(mContext)) {
350 return;
351 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800352 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
353 && alertAgain(entry, entry.notification.getNotification())) {
354 entry.setShowInShadeWhenBubble(true);
355 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800356 updateBubble(entry, true /* updatePosition */);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800357 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800358 }
359 }
360
361 @Override
362 public void onEntryRemoved(NotificationEntry entry,
363 @Nullable NotificationVisibility visibility,
364 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800365 if (!areBubblesEnabled(mContext)) {
366 return;
367 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800368 entry.setShowInShadeWhenBubble(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800369 if (mStackView != null) {
370 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800371 }
372 if (!removedByUser) {
373 // This was a cancel so we should remove the bubble
374 removeBubble(entry.key);
Ned Burns01e38212019-01-03 16:32:52 -0500375 }
376 }
377 };
378
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800379 /**
380 * Lets any listeners know if bubble state has changed.
381 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800382 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800383 if (mStackView == null) {
384 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800385 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800386
Mady Mellord1c78b262018-11-06 18:04:40 -0800387 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800388 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800389 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800390 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
391 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
392 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800393 }
394
395 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800396 * Updates the visibility of the bubbles based on current state.
397 * Does not un-bubble, just hides or un-hides. Will notify any
398 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800399 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800400 public void updateVisibility() {
401 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
402 // Bubbles only appear in unlocked shade
403 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
404 } else if (mStackView != null) {
405 mStackView.setVisibility(INVISIBLE);
406 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800407 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800408 updateBubblesShowing();
409 }
410
411 /**
412 * Rect indicating the touchable region for the bubble stack / expanded stack.
413 */
414 public Rect getTouchableRegion() {
415 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
416 return null;
417 }
418 mStackView.getBoundsOnScreen(mTempRect);
419 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800420 }
421
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800422 @VisibleForTesting
423 BubbleStackView getStackView() {
424 return mStackView;
425 }
426
Mady Mellor5549dd22018-11-06 18:07:34 -0800427 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800428 * Whether the notification has been developer configured to bubble and is allowed by the user.
429 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800430 @VisibleForTesting
431 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800432 StatusBarNotification n = entry.notification;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800433 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
434 && n.getNotification().getBubbleMetadata().getIntent() != null;
Julia Reynolds4509ce72019-01-31 13:12:43 -0500435 return hasOverlayIntent && entry.canBubble;
Mady Mellorb4991e62019-01-10 15:14:51 -0800436 }
437
438 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800439 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800440 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800441 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800442 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800443 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800444 return false;
445 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800446 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800447
448 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
449 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
450 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
451
Mady Mellor5549dd22018-11-06 18:07:34 -0800452 boolean hasRemoteInput = false;
453 if (n.getNotification().actions != null) {
454 for (Notification.Action action : n.getNotification().actions) {
455 if (action.getRemoteInputs() != null) {
456 hasRemoteInput = true;
457 break;
458 }
459 }
460 }
Mady Mellor711f9562018-12-05 14:53:46 -0800461 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
462 && n.isOngoing();
463 boolean isMusic = n.getNotification().hasMediaSession();
464 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800465
Mady Mellor5549dd22018-11-06 18:07:34 -0800466 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800467 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
468 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800469 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800470 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800471 || autoBubbleAll;
472 }
473
Mark Renoufcecc77b2019-01-30 16:32:24 -0500474 /**
475 * This task stack listener is responsible for responding to tasks moved to the front
476 * which are on the default (main) display. When this happens, expanded bubbles must be
477 * collapsed so the user may interact with the app which was just moved to the front.
478 * <p>
479 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
480 * these calls via a main thread Handler.
481 */
482 @MainThread
483 private class BubbleTaskStackListener extends TaskStackChangeListener {
484
485 @Nullable
486 private ActivityManager.StackInfo findStackInfo(int taskId) throws RemoteException {
487 final List<ActivityManager.StackInfo> stackInfoList =
488 mActivityTaskManager.getAllStackInfos();
489 // Iterate through stacks from top to bottom.
490 final int stackCount = stackInfoList.size();
491 for (int stackIndex = 0; stackIndex < stackCount; stackIndex++) {
492 final ActivityManager.StackInfo stackInfo = stackInfoList.get(stackIndex);
493 // Iterate through tasks from top to bottom.
494 for (int taskIndex = stackInfo.taskIds.length - 1; taskIndex >= 0; taskIndex--) {
495 if (stackInfo.taskIds[taskIndex] == taskId) {
496 return stackInfo;
497 }
498 }
499 }
500 return null;
501 }
502
503 @Override
504 public void onTaskMovedToFront(int taskId) {
505 ActivityManager.StackInfo stackInfo = null;
506 try {
507 stackInfo = findStackInfo(taskId);
508 } catch (RemoteException e) {
509 e.rethrowAsRuntimeException();
510 }
511 if (stackInfo != null && stackInfo.displayId == Display.DEFAULT_DISPLAY
512 && mStackView != null) {
513 mStackView.collapseStack();
514 }
515 }
516
517 /**
518 * This is a workaround for the case when the activity had to be created in a new task.
519 * Existing code in ActivityStackSupervisor checks the display where the activity
520 * ultimately ended up, displays an error message toast, and calls this method instead of
521 * onTaskMovedToFront.
522 */
523 // TODO(b/124058588): add requestedDisplayId to this callback, ignore unless matches
524 @Override
525 public void onActivityLaunchOnSecondaryDisplayFailed() {
526 if (mStackView != null) {
527 mStackView.collapseStack();
528 }
529 }
530 }
531
Mady Mellorceced172018-11-27 11:18:39 -0800532 private static boolean shouldAutoBubbleMessages(Context context) {
533 return Settings.Secure.getInt(context.getContentResolver(),
534 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
535 }
536
537 private static boolean shouldAutoBubbleOngoing(Context context) {
538 return Settings.Secure.getInt(context.getContentResolver(),
539 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
540 }
541
542 private static boolean shouldAutoBubbleAll(Context context) {
543 return Settings.Secure.getInt(context.getContentResolver(),
544 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800545 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500546
Mady Mellor3dff9e62019-02-05 18:12:53 -0800547 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500548 return Settings.Secure.getInt(context.getContentResolver(),
549 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
550 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800551
552 private static boolean areBubblesEnabled(Context context) {
553 return Settings.Secure.getInt(context.getContentResolver(),
554 ENABLE_BUBBLES, 1) != 0;
555 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500556
557 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
558 private class BubblesImeListener extends IPinnedStackListener.Stub {
559
560 @Override
561 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
562 }
563
564 @Override
565 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
566 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
567 int displayRotation) throws RemoteException {}
568
569 @Override
570 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight)
571 throws RemoteException {
572 if (mStackView != null) {
573 mStackView.post(() -> {
574 mStackView.onImeVisibilityChanged(imeVisible, imeHeight);
575 });
576 }
577 }
578
579 @Override
580 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
581 throws RemoteException {}
582
583 @Override
584 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
585
586 @Override
587 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
588 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800589}