blob: 9b826e1e66f51f15bbd9333a3952dc8aff54541d [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;
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;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080040import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080041import android.widget.FrameLayout;
42
Mark Renouf658c6bc2019-01-30 10:26:54 -050043import androidx.annotation.MainThread;
44
Mady Mellorebdbbb92018-11-15 14:36:48 -080045import com.android.internal.annotations.VisibleForTesting;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080046import com.android.internal.statusbar.NotificationVisibility;
Ned Burns01e38212019-01-03 16:32:52 -050047import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080048import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050049import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050050import com.android.systemui.shared.system.ActivityManagerWrapper;
51import com.android.systemui.shared.system.TaskStackChangeListener;
Ned Burns01e38212019-01-03 16:32:52 -050052import com.android.systemui.statusbar.notification.NotificationEntryListener;
53import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080054import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050055import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080056import com.android.systemui.statusbar.notification.row.NotificationInflater;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080057import com.android.systemui.statusbar.phone.StatusBarWindowController;
58
Mark Renoufcecc77b2019-01-30 16:32:24 -050059import java.util.List;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080060
Jason Monk27d01a622018-12-10 15:57:09 -050061import javax.inject.Inject;
62import javax.inject.Singleton;
63
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080064/**
65 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
66 * Bubbles can be expanded to show more content.
67 *
68 * The controller manages addition, removal, and visible state of bubbles on screen.
69 */
Jason Monk27d01a622018-12-10 15:57:09 -050070@Singleton
Mady Mellor3d82e682019-02-05 13:34:48 -080071public class BubbleController implements BubbleExpandedView.OnBubbleBlockedListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080072 private static final int MAX_BUBBLES = 5; // TODO: actually enforce this
73
74 private static final String TAG = "BubbleController";
75
Mady Mellor5549dd22018-11-06 18:07:34 -080076 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -050077 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -080078
Mady Mellorf6e3ac02019-01-29 10:37:52 -080079 // Secure settings flags
80 // Feature level flag
81 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
82 // Auto bubble flags set whether different notification types should be presented as a bubble
Mady Mellorceced172018-11-27 11:18:39 -080083 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
84 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
85 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellorf6e3ac02019-01-29 10:37:52 -080086 // Use an activity view for an auto-bubbled notification if it has an appropriate content intent
Mark Renouf89b1a4a2018-12-04 14:59:45 -050087 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -080088
Ned Burns01e38212019-01-03 16:32:52 -050089 private final Context mContext;
90 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -050091 private final IActivityTaskManager mActivityTaskManager;
92 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -080093 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -080094 private BubbleExpandListener mExpandListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080095
Mady Mellor3dff9e62019-02-05 18:12:53 -080096 private BubbleData mBubbleData;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080097 private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080098
99 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500100 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800101 private StatusBarStateListener mStatusBarStateListener;
102
103 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider =
104 Dependency.get(NotificationInterruptionStateProvider.class);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800105
Mady Mellorb4991e62019-01-10 15:14:51 -0800106 private INotificationManager mNotificationManagerService;
107
Mady Mellord1c78b262018-11-06 18:04:40 -0800108 // Used for determining view rect for touch interaction
109 private Rect mTempRect = new Rect();
110
Mady Mellor5549dd22018-11-06 18:07:34 -0800111 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800112 * Listener to be notified when some states of the bubbles change.
113 */
114 public interface BubbleStateChangeListener {
115 /**
116 * Called when the stack has bubbles or no longer has bubbles.
117 */
118 void onHasBubblesChanged(boolean hasBubbles);
119 }
120
Mady Mellorcd9b1302018-11-06 18:08:04 -0800121 /**
122 * Listener to find out about stack expansion / collapse events.
123 */
124 public interface BubbleExpandListener {
125 /**
126 * Called when the expansion state of the bubble stack changes.
Mady Mellorcd9b1302018-11-06 18:08:04 -0800127 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800128 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800129 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800130 void onBubbleExpandChanged(boolean isExpanding, String key);
131 }
132
133 /**
134 * Listens for the current state of the status bar and updates the visibility state
135 * of bubbles as needed.
136 */
137 private class StatusBarStateListener implements StatusBarStateController.StateListener {
138 private int mState;
139 /**
140 * Returns the current status bar state.
141 */
142 public int getCurrentState() {
143 return mState;
144 }
145
146 @Override
147 public void onStateChanged(int newState) {
148 mState = newState;
149 updateVisibility();
150 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800151 }
152
Jason Monk27d01a622018-12-10 15:57:09 -0500153 @Inject
Jason Monk92d5c242018-12-21 14:37:34 -0500154 public BubbleController(Context context, StatusBarWindowController statusBarWindowController) {
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
175 mBubbleData = BubbleData.getInstance();
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 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800246 * Adds or updates a bubble associated with the provided notification entry.
247 *
248 * @param notif the notification associated with this bubble.
249 * @param updatePosition whether this update should promote the bubble to the top of the stack.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800250 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800251 public void updateBubble(NotificationEntry notif, boolean updatePosition) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800252 if (mStackView != null && mBubbleData.getBubble(notif.key) != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800253 // It's an update
Mady Mellor3dff9e62019-02-05 18:12:53 -0800254 mStackView.updateBubble(notif, updatePosition);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800255 } else {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800256 if (mStackView == null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800257 mStackView = new BubbleStackView(mContext);
Mady Mellord1c78b262018-11-06 18:04:40 -0800258 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800259 // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim
260 // between bubble and the shade
261 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
262 sbv.addView(mStackView, bubblePosition,
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800263 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
Mady Mellorcd9b1302018-11-06 18:08:04 -0800264 if (mExpandListener != null) {
265 mStackView.setExpandListener(mExpandListener);
266 }
Mady Mellore8e07712019-01-23 12:45:33 -0800267 mStackView.setOnBlockedListener(this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800268 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800269 // It's new
Mady Mellor3dff9e62019-02-05 18:12:53 -0800270 mStackView.addBubble(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800271 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800272 updateVisibility();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800273 }
274
275 /**
276 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500277 * <p>
278 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800279 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500280 @MainThread
Ned Burns01e38212019-01-03 16:32:52 -0500281 void removeBubble(String key) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800282 if (mStackView != null) {
283 mStackView.removeBubble(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800284 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800285 mNotificationEntryManager.updateNotifications();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800286 updateVisibility();
Mady Mellord1c78b262018-11-06 18:04:40 -0800287 }
288
Mady Mellore8e07712019-01-23 12:45:33 -0800289 @Override
290 public void onBubbleBlocked(NotificationEntry entry) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800291 Object[] bubbles = mBubbleData.getBubbles().toArray();
Mady Mellore8e07712019-01-23 12:45:33 -0800292 for (int i = 0; i < bubbles.length; i++) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800293 NotificationEntry e = ((Bubble) bubbles[i]).entry;
Mady Mellore8e07712019-01-23 12:45:33 -0800294 boolean samePackage = entry.notification.getPackageName().equals(
295 e.notification.getPackageName());
296 if (samePackage) {
297 removeBubble(entry.key);
298 }
299 }
300 }
301
Ned Burns01e38212019-01-03 16:32:52 -0500302 @SuppressWarnings("FieldCanBeLocal")
303 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
304 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500305 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800306 if (!areBubblesEnabled(mContext)) {
307 return;
308 }
309 if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800310 // TODO: handle group summaries
311 // It's a new notif, it shows in the shade and as a bubble
Ned Burns01e38212019-01-03 16:32:52 -0500312 entry.setIsBubble(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800313 entry.setShowInShadeWhenBubble(true);
314 }
315 }
316
317 @Override
318 public void onEntryInflated(NotificationEntry entry,
319 @NotificationInflater.InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800320 if (!areBubblesEnabled(mContext)) {
321 return;
322 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800323 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
324 updateBubble(entry, true /* updatePosition */);
325 }
326 }
327
328 @Override
329 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800330 if (!areBubblesEnabled(mContext)) {
331 return;
332 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800333 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
334 && alertAgain(entry, entry.notification.getNotification())) {
335 entry.setShowInShadeWhenBubble(true);
336 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800337 updateBubble(entry, true /* updatePosition */);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800338 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800339 }
340 }
341
342 @Override
343 public void onEntryRemoved(NotificationEntry entry,
344 @Nullable NotificationVisibility visibility,
345 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800346 if (!areBubblesEnabled(mContext)) {
347 return;
348 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800349 entry.setShowInShadeWhenBubble(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800350 if (mStackView != null) {
351 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800352 }
353 if (!removedByUser) {
354 // This was a cancel so we should remove the bubble
355 removeBubble(entry.key);
Ned Burns01e38212019-01-03 16:32:52 -0500356 }
357 }
358 };
359
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800360 /**
361 * Lets any listeners know if bubble state has changed.
362 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800363 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800364 if (mStackView == null) {
365 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800366 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800367
Mady Mellord1c78b262018-11-06 18:04:40 -0800368 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800369 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800370 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800371 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
372 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
373 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800374 }
375
376 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800377 * Updates the visibility of the bubbles based on current state.
378 * Does not un-bubble, just hides or un-hides. Will notify any
379 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800380 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800381 public void updateVisibility() {
382 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
383 // Bubbles only appear in unlocked shade
384 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
385 } else if (mStackView != null) {
386 mStackView.setVisibility(INVISIBLE);
387 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800388 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800389 updateBubblesShowing();
390 }
391
392 /**
393 * Rect indicating the touchable region for the bubble stack / expanded stack.
394 */
395 public Rect getTouchableRegion() {
396 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
397 return null;
398 }
399 mStackView.getBoundsOnScreen(mTempRect);
400 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800401 }
402
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800403 @VisibleForTesting
404 BubbleStackView getStackView() {
405 return mStackView;
406 }
407
Mady Mellor5549dd22018-11-06 18:07:34 -0800408 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800409 * Whether the notification has been developer configured to bubble and is allowed by the user.
410 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800411 @VisibleForTesting
412 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800413 StatusBarNotification n = entry.notification;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800414 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
415 && n.getNotification().getBubbleMetadata().getIntent() != null;
Julia Reynolds4509ce72019-01-31 13:12:43 -0500416 return hasOverlayIntent && entry.canBubble;
Mady Mellorb4991e62019-01-10 15:14:51 -0800417 }
418
419 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800420 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800421 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800422 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800423 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800424 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800425 return false;
426 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800427 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800428
429 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
430 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
431 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
432
Mady Mellor5549dd22018-11-06 18:07:34 -0800433 boolean hasRemoteInput = false;
434 if (n.getNotification().actions != null) {
435 for (Notification.Action action : n.getNotification().actions) {
436 if (action.getRemoteInputs() != null) {
437 hasRemoteInput = true;
438 break;
439 }
440 }
441 }
Mady Mellor711f9562018-12-05 14:53:46 -0800442 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
443 && n.isOngoing();
444 boolean isMusic = n.getNotification().hasMediaSession();
445 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800446
Mady Mellor5549dd22018-11-06 18:07:34 -0800447 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800448 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
449 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800450 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800451 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800452 || autoBubbleAll;
453 }
454
Mark Renoufcecc77b2019-01-30 16:32:24 -0500455 /**
456 * This task stack listener is responsible for responding to tasks moved to the front
457 * which are on the default (main) display. When this happens, expanded bubbles must be
458 * collapsed so the user may interact with the app which was just moved to the front.
459 * <p>
460 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
461 * these calls via a main thread Handler.
462 */
463 @MainThread
464 private class BubbleTaskStackListener extends TaskStackChangeListener {
465
466 @Nullable
467 private ActivityManager.StackInfo findStackInfo(int taskId) throws RemoteException {
468 final List<ActivityManager.StackInfo> stackInfoList =
469 mActivityTaskManager.getAllStackInfos();
470 // Iterate through stacks from top to bottom.
471 final int stackCount = stackInfoList.size();
472 for (int stackIndex = 0; stackIndex < stackCount; stackIndex++) {
473 final ActivityManager.StackInfo stackInfo = stackInfoList.get(stackIndex);
474 // Iterate through tasks from top to bottom.
475 for (int taskIndex = stackInfo.taskIds.length - 1; taskIndex >= 0; taskIndex--) {
476 if (stackInfo.taskIds[taskIndex] == taskId) {
477 return stackInfo;
478 }
479 }
480 }
481 return null;
482 }
483
484 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500485 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
486 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500487 mStackView.collapseStack();
488 }
489 }
490
491 /**
492 * This is a workaround for the case when the activity had to be created in a new task.
493 * Existing code in ActivityStackSupervisor checks the display where the activity
494 * ultimately ended up, displays an error message toast, and calls this method instead of
495 * onTaskMovedToFront.
496 */
Mark Renoufcecc77b2019-01-30 16:32:24 -0500497 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500498 public void onActivityLaunchOnSecondaryDisplayFailed(RunningTaskInfo taskInfo) {
499 // TODO(b/124058588): move to ActivityView.StateCallback, filter on virtualDisplay ID
Mark Renoufcecc77b2019-01-30 16:32:24 -0500500 if (mStackView != null) {
501 mStackView.collapseStack();
502 }
503 }
504 }
505
Mady Mellorceced172018-11-27 11:18:39 -0800506 private static boolean shouldAutoBubbleMessages(Context context) {
507 return Settings.Secure.getInt(context.getContentResolver(),
508 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
509 }
510
511 private static boolean shouldAutoBubbleOngoing(Context context) {
512 return Settings.Secure.getInt(context.getContentResolver(),
513 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
514 }
515
516 private static boolean shouldAutoBubbleAll(Context context) {
517 return Settings.Secure.getInt(context.getContentResolver(),
518 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800519 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500520
Mady Mellor3dff9e62019-02-05 18:12:53 -0800521 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500522 return Settings.Secure.getInt(context.getContentResolver(),
523 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
524 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800525
526 private static boolean areBubblesEnabled(Context context) {
527 return Settings.Secure.getInt(context.getContentResolver(),
528 ENABLE_BUBBLES, 1) != 0;
529 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800530}