blob: 0832296b7dab0d04acfdf471feac0a96ce2f27e9 [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;
Mady Mellord1c78b262018-11-06 18:04:40 -080033import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050034import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080035import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080036import android.provider.Settings;
Mady Mellor5549dd22018-11-06 18:07:34 -080037import android.service.notification.StatusBarNotification;
Mark Renoufcecc77b2019-01-30 16:32:24 -050038import android.view.Display;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080039import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080040import android.widget.FrameLayout;
41
Mark Renouf658c6bc2019-01-30 10:26:54 -050042import androidx.annotation.MainThread;
43
Mady Mellorebdbbb92018-11-15 14:36:48 -080044import com.android.internal.annotations.VisibleForTesting;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080045import com.android.internal.statusbar.NotificationVisibility;
Ned Burns01e38212019-01-03 16:32:52 -050046import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080047import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050048import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050049import com.android.systemui.shared.system.ActivityManagerWrapper;
50import com.android.systemui.shared.system.TaskStackChangeListener;
Ned Burns01e38212019-01-03 16:32:52 -050051import com.android.systemui.statusbar.notification.NotificationEntryListener;
52import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080053import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050054import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050055import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080056import com.android.systemui.statusbar.phone.StatusBarWindowController;
57
Mark Renoufcecc77b2019-01-30 16:32:24 -050058import java.util.List;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080059
Jason Monk27d01a622018-12-10 15:57:09 -050060import javax.inject.Inject;
61import javax.inject.Singleton;
62
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080063/**
64 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
65 * Bubbles can be expanded to show more content.
66 *
67 * The controller manages addition, removal, and visible state of bubbles on screen.
68 */
Jason Monk27d01a622018-12-10 15:57:09 -050069@Singleton
Mady Mellor3d82e682019-02-05 13:34:48 -080070public class BubbleController implements BubbleExpandedView.OnBubbleBlockedListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080071 private static final int MAX_BUBBLES = 5; // TODO: actually enforce this
72
73 private static final String TAG = "BubbleController";
74
Mady Mellor5549dd22018-11-06 18:07:34 -080075 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -050076 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -080077
Mady Mellorf6e3ac02019-01-29 10:37:52 -080078 // Secure settings flags
79 // Feature level flag
80 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
81 // Auto bubble flags set whether different notification types should be presented as a bubble
Mady Mellorceced172018-11-27 11:18:39 -080082 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
83 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
84 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellorf6e3ac02019-01-29 10:37:52 -080085 // Use an activity view for an auto-bubbled notification if it has an appropriate content intent
Mark Renouf89b1a4a2018-12-04 14:59:45 -050086 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -080087
Ned Burns01e38212019-01-03 16:32:52 -050088 private final Context mContext;
89 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -050090 private final IActivityTaskManager mActivityTaskManager;
91 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -080092 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -080093 private BubbleExpandListener mExpandListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080094
Mady Mellor3dff9e62019-02-05 18:12:53 -080095 private BubbleData mBubbleData;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080096 private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080097
98 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -050099 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800100 private StatusBarStateListener mStatusBarStateListener;
101
102 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider =
103 Dependency.get(NotificationInterruptionStateProvider.class);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800104
Mady Mellorb4991e62019-01-10 15:14:51 -0800105 private INotificationManager mNotificationManagerService;
106
Mady Mellord1c78b262018-11-06 18:04:40 -0800107 // Used for determining view rect for touch interaction
108 private Rect mTempRect = new Rect();
109
Mady Mellor5549dd22018-11-06 18:07:34 -0800110 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800111 * Listener to be notified when some states of the bubbles change.
112 */
113 public interface BubbleStateChangeListener {
114 /**
115 * Called when the stack has bubbles or no longer has bubbles.
116 */
117 void onHasBubblesChanged(boolean hasBubbles);
118 }
119
Mady Mellorcd9b1302018-11-06 18:08:04 -0800120 /**
121 * Listener to find out about stack expansion / collapse events.
122 */
123 public interface BubbleExpandListener {
124 /**
125 * Called when the expansion state of the bubble stack changes.
Mady Mellorcd9b1302018-11-06 18:08:04 -0800126 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800127 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800128 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800129 void onBubbleExpandChanged(boolean isExpanding, String key);
130 }
131
132 /**
133 * Listens for the current state of the status bar and updates the visibility state
134 * of bubbles as needed.
135 */
136 private class StatusBarStateListener implements StatusBarStateController.StateListener {
137 private int mState;
138 /**
139 * Returns the current status bar state.
140 */
141 public int getCurrentState() {
142 return mState;
143 }
144
145 @Override
146 public void onStateChanged(int newState) {
147 mState = newState;
148 updateVisibility();
149 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800150 }
151
Jason Monk27d01a622018-12-10 15:57:09 -0500152 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800153 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
154 BubbleData data) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800155 mContext = context;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800156
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800157 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500158 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800159
160 try {
161 mNotificationManagerService = INotificationManager.Stub.asInterface(
162 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
163 } catch (ServiceManager.ServiceNotFoundException e) {
164 e.printStackTrace();
165 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800166
167 mStatusBarWindowController = statusBarWindowController;
168 mStatusBarStateListener = new StatusBarStateListener();
169 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500170
171 mActivityTaskManager = ActivityTaskManager.getService();
172 mTaskStackListener = new BubbleTaskStackListener();
173 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800174
Mady Mellorcfd06c12019-02-13 14:32:12 -0800175 mBubbleData = data;
Mady Mellor5549dd22018-11-06 18:07:34 -0800176 }
177
178 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800179 * Set a listener to be notified when some states of the bubbles change.
180 */
181 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
182 mStateChangeListener = listener;
183 }
184
185 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800186 * Set a listener to be notified of bubble expand events.
187 */
188 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100189 mExpandListener = ((isExpanding, key) -> {
190 if (listener != null) {
191 listener.onBubbleExpandChanged(isExpanding, key);
192 }
193 mStatusBarWindowController.setBubbleExpanded(isExpanding);
194 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800195 if (mStackView != null) {
196 mStackView.setExpandListener(mExpandListener);
197 }
198 }
199
200 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800201 * Whether or not there are bubbles present, regardless of them being visible on the
202 * screen (e.g. if on AOD).
203 */
204 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800205 if (mStackView == null) {
206 return false;
207 }
208 for (Bubble bubble : mBubbleData.getBubbles()) {
209 if (!bubble.entry.isBubbleDismissed()) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800210 return true;
211 }
212 }
213 return false;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800214 }
215
216 /**
217 * Whether the stack of bubbles is expanded or not.
218 */
219 public boolean isStackExpanded() {
220 return mStackView != null && mStackView.isExpanded();
221 }
222
223 /**
224 * Tell the stack of bubbles to collapse.
225 */
226 public void collapseStack() {
227 if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800228 mStackView.collapseStack();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800229 }
230 }
231
232 /**
233 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
234 */
Ned Burns01e38212019-01-03 16:32:52 -0500235 void dismissStack() {
Mady Mellord1c78b262018-11-06 18:04:40 -0800236 if (mStackView == null) {
237 return;
238 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800239 mStackView.stackDismissed();
240
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800241 updateVisibility();
Ned Burns01e38212019-01-03 16:32:52 -0500242 mNotificationEntryManager.updateNotifications();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800243 }
244
245 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500246 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
247 * is forwarded a back key down/up pair.
248 */
249 public void performBackPressIfNeeded() {
250 if (mStackView != null) {
251 mStackView.performBackPressIfNeeded();
252 }
253 }
254
255 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800256 * Adds or updates a bubble associated with the provided notification entry.
257 *
258 * @param notif the notification associated with this bubble.
259 * @param updatePosition whether this update should promote the bubble to the top of the stack.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800260 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800261 public void updateBubble(NotificationEntry notif, boolean updatePosition) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800262 if (mStackView != null && mBubbleData.getBubble(notif.key) != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800263 // It's an update
Mady Mellor3dff9e62019-02-05 18:12:53 -0800264 mStackView.updateBubble(notif, updatePosition);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800265 } else {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800266 if (mStackView == null) {
Mady Mellorcfd06c12019-02-13 14:32:12 -0800267 mStackView = new BubbleStackView(mContext, mBubbleData);
Mady Mellord1c78b262018-11-06 18:04:40 -0800268 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800269 // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim
270 // between bubble and the shade
271 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
272 sbv.addView(mStackView, bubblePosition,
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800273 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
Mady Mellorcd9b1302018-11-06 18:08:04 -0800274 if (mExpandListener != null) {
275 mStackView.setExpandListener(mExpandListener);
276 }
Mady Mellore8e07712019-01-23 12:45:33 -0800277 mStackView.setOnBlockedListener(this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800278 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800279 // It's new
Mady Mellor3dff9e62019-02-05 18:12:53 -0800280 mStackView.addBubble(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800281 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800282 updateVisibility();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800283 }
284
285 /**
286 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500287 * <p>
288 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800289 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500290 @MainThread
Ned Burns01e38212019-01-03 16:32:52 -0500291 void removeBubble(String key) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800292 if (mStackView != null) {
293 mStackView.removeBubble(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800294 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800295 mNotificationEntryManager.updateNotifications();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800296 updateVisibility();
Mady Mellord1c78b262018-11-06 18:04:40 -0800297 }
298
Mady Mellore8e07712019-01-23 12:45:33 -0800299 @Override
300 public void onBubbleBlocked(NotificationEntry entry) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800301 Object[] bubbles = mBubbleData.getBubbles().toArray();
Mady Mellore8e07712019-01-23 12:45:33 -0800302 for (int i = 0; i < bubbles.length; i++) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800303 NotificationEntry e = ((Bubble) bubbles[i]).entry;
Mady Mellore8e07712019-01-23 12:45:33 -0800304 boolean samePackage = entry.notification.getPackageName().equals(
305 e.notification.getPackageName());
306 if (samePackage) {
307 removeBubble(entry.key);
308 }
309 }
310 }
311
Ned Burns01e38212019-01-03 16:32:52 -0500312 @SuppressWarnings("FieldCanBeLocal")
313 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
314 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500315 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800316 if (!areBubblesEnabled(mContext)) {
317 return;
318 }
319 if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800320 // TODO: handle group summaries
321 // It's a new notif, it shows in the shade and as a bubble
Ned Burns01e38212019-01-03 16:32:52 -0500322 entry.setIsBubble(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800323 entry.setShowInShadeWhenBubble(true);
324 }
325 }
326
327 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500328 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800329 if (!areBubblesEnabled(mContext)) {
330 return;
331 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800332 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
333 updateBubble(entry, true /* updatePosition */);
334 }
335 }
336
337 @Override
338 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800339 if (!areBubblesEnabled(mContext)) {
340 return;
341 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800342 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
343 && alertAgain(entry, entry.notification.getNotification())) {
344 entry.setShowInShadeWhenBubble(true);
345 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800346 updateBubble(entry, true /* updatePosition */);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800347 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800348 }
349 }
350
351 @Override
352 public void onEntryRemoved(NotificationEntry entry,
353 @Nullable NotificationVisibility visibility,
354 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800355 if (!areBubblesEnabled(mContext)) {
356 return;
357 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800358 entry.setShowInShadeWhenBubble(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800359 if (mStackView != null) {
360 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800361 }
362 if (!removedByUser) {
363 // This was a cancel so we should remove the bubble
364 removeBubble(entry.key);
Ned Burns01e38212019-01-03 16:32:52 -0500365 }
366 }
367 };
368
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800369 /**
370 * Lets any listeners know if bubble state has changed.
371 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800372 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800373 if (mStackView == null) {
374 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800375 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800376
Mady Mellord1c78b262018-11-06 18:04:40 -0800377 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800378 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800379 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800380 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
381 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
382 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800383 }
384
385 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800386 * Updates the visibility of the bubbles based on current state.
387 * Does not un-bubble, just hides or un-hides. Will notify any
388 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800389 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800390 public void updateVisibility() {
391 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
392 // Bubbles only appear in unlocked shade
393 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
394 } else if (mStackView != null) {
395 mStackView.setVisibility(INVISIBLE);
396 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800397 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800398 updateBubblesShowing();
399 }
400
401 /**
402 * Rect indicating the touchable region for the bubble stack / expanded stack.
403 */
404 public Rect getTouchableRegion() {
405 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
406 return null;
407 }
408 mStackView.getBoundsOnScreen(mTempRect);
409 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800410 }
411
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800412 @VisibleForTesting
413 BubbleStackView getStackView() {
414 return mStackView;
415 }
416
Mady Mellor5549dd22018-11-06 18:07:34 -0800417 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800418 * Whether the notification has been developer configured to bubble and is allowed by the user.
419 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800420 @VisibleForTesting
421 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800422 StatusBarNotification n = entry.notification;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800423 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
424 && n.getNotification().getBubbleMetadata().getIntent() != null;
Julia Reynolds4509ce72019-01-31 13:12:43 -0500425 return hasOverlayIntent && entry.canBubble;
Mady Mellorb4991e62019-01-10 15:14:51 -0800426 }
427
428 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800429 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800430 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800431 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800432 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800433 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800434 return false;
435 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800436 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800437
438 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
439 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
440 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
441
Mady Mellor5549dd22018-11-06 18:07:34 -0800442 boolean hasRemoteInput = false;
443 if (n.getNotification().actions != null) {
444 for (Notification.Action action : n.getNotification().actions) {
445 if (action.getRemoteInputs() != null) {
446 hasRemoteInput = true;
447 break;
448 }
449 }
450 }
Mady Mellor711f9562018-12-05 14:53:46 -0800451 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
452 && n.isOngoing();
453 boolean isMusic = n.getNotification().hasMediaSession();
454 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800455
Mady Mellor5549dd22018-11-06 18:07:34 -0800456 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800457 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
458 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800459 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800460 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800461 || autoBubbleAll;
462 }
463
Mark Renoufcecc77b2019-01-30 16:32:24 -0500464 /**
465 * This task stack listener is responsible for responding to tasks moved to the front
466 * which are on the default (main) display. When this happens, expanded bubbles must be
467 * collapsed so the user may interact with the app which was just moved to the front.
468 * <p>
469 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
470 * these calls via a main thread Handler.
471 */
472 @MainThread
473 private class BubbleTaskStackListener extends TaskStackChangeListener {
474
475 @Nullable
476 private ActivityManager.StackInfo findStackInfo(int taskId) throws RemoteException {
477 final List<ActivityManager.StackInfo> stackInfoList =
478 mActivityTaskManager.getAllStackInfos();
479 // Iterate through stacks from top to bottom.
480 final int stackCount = stackInfoList.size();
481 for (int stackIndex = 0; stackIndex < stackCount; stackIndex++) {
482 final ActivityManager.StackInfo stackInfo = stackInfoList.get(stackIndex);
483 // Iterate through tasks from top to bottom.
484 for (int taskIndex = stackInfo.taskIds.length - 1; taskIndex >= 0; taskIndex--) {
485 if (stackInfo.taskIds[taskIndex] == taskId) {
486 return stackInfo;
487 }
488 }
489 }
490 return null;
491 }
492
493 @Override
494 public void onTaskMovedToFront(int taskId) {
495 ActivityManager.StackInfo stackInfo = null;
496 try {
497 stackInfo = findStackInfo(taskId);
498 } catch (RemoteException e) {
499 e.rethrowAsRuntimeException();
500 }
501 if (stackInfo != null && stackInfo.displayId == Display.DEFAULT_DISPLAY
502 && mStackView != null) {
503 mStackView.collapseStack();
504 }
505 }
506
507 /**
508 * This is a workaround for the case when the activity had to be created in a new task.
509 * Existing code in ActivityStackSupervisor checks the display where the activity
510 * ultimately ended up, displays an error message toast, and calls this method instead of
511 * onTaskMovedToFront.
512 */
513 // TODO(b/124058588): add requestedDisplayId to this callback, ignore unless matches
514 @Override
515 public void onActivityLaunchOnSecondaryDisplayFailed() {
516 if (mStackView != null) {
517 mStackView.collapseStack();
518 }
519 }
520 }
521
Mady Mellorceced172018-11-27 11:18:39 -0800522 private static boolean shouldAutoBubbleMessages(Context context) {
523 return Settings.Secure.getInt(context.getContentResolver(),
524 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
525 }
526
527 private static boolean shouldAutoBubbleOngoing(Context context) {
528 return Settings.Secure.getInt(context.getContentResolver(),
529 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
530 }
531
532 private static boolean shouldAutoBubbleAll(Context context) {
533 return Settings.Secure.getInt(context.getContentResolver(),
534 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800535 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500536
Mady Mellor3dff9e62019-02-05 18:12:53 -0800537 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500538 return Settings.Secure.getInt(context.getContentResolver(),
539 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
540 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800541
542 private static boolean areBubblesEnabled(Context context) {
543 return Settings.Secure.getInt(context.getContentResolver(),
544 ENABLE_BUBBLES, 1) != 0;
545 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800546}