blob: a67e1b7032c6516ec0a935b015b758d7dbdc30dd [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;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080055import com.android.systemui.statusbar.notification.row.NotificationInflater;
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
Jason Monk92d5c242018-12-21 14:37:34 -0500153 public BubbleController(Context context, StatusBarWindowController statusBarWindowController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800154 mContext = context;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800155
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800156 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500157 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800158
159 try {
160 mNotificationManagerService = INotificationManager.Stub.asInterface(
161 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
162 } catch (ServiceManager.ServiceNotFoundException e) {
163 e.printStackTrace();
164 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800165
166 mStatusBarWindowController = statusBarWindowController;
167 mStatusBarStateListener = new StatusBarStateListener();
168 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500169
170 mActivityTaskManager = ActivityTaskManager.getService();
171 mTaskStackListener = new BubbleTaskStackListener();
172 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800173
174 mBubbleData = BubbleData.getInstance();
Mady Mellor5549dd22018-11-06 18:07:34 -0800175 }
176
177 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800178 * Set a listener to be notified when some states of the bubbles change.
179 */
180 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
181 mStateChangeListener = listener;
182 }
183
184 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800185 * Set a listener to be notified of bubble expand events.
186 */
187 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100188 mExpandListener = ((isExpanding, key) -> {
189 if (listener != null) {
190 listener.onBubbleExpandChanged(isExpanding, key);
191 }
192 mStatusBarWindowController.setBubbleExpanded(isExpanding);
193 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800194 if (mStackView != null) {
195 mStackView.setExpandListener(mExpandListener);
196 }
197 }
198
199 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800200 * Whether or not there are bubbles present, regardless of them being visible on the
201 * screen (e.g. if on AOD).
202 */
203 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800204 if (mStackView == null) {
205 return false;
206 }
207 for (Bubble bubble : mBubbleData.getBubbles()) {
208 if (!bubble.entry.isBubbleDismissed()) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800209 return true;
210 }
211 }
212 return false;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800213 }
214
215 /**
216 * Whether the stack of bubbles is expanded or not.
217 */
218 public boolean isStackExpanded() {
219 return mStackView != null && mStackView.isExpanded();
220 }
221
222 /**
223 * Tell the stack of bubbles to collapse.
224 */
225 public void collapseStack() {
226 if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800227 mStackView.collapseStack();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800228 }
229 }
230
231 /**
232 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
233 */
Ned Burns01e38212019-01-03 16:32:52 -0500234 void dismissStack() {
Mady Mellord1c78b262018-11-06 18:04:40 -0800235 if (mStackView == null) {
236 return;
237 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800238 mStackView.stackDismissed();
239
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800240 updateVisibility();
Ned Burns01e38212019-01-03 16:32:52 -0500241 mNotificationEntryManager.updateNotifications();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800242 }
243
244 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800245 * Adds or updates a bubble associated with the provided notification entry.
246 *
247 * @param notif the notification associated with this bubble.
248 * @param updatePosition whether this update should promote the bubble to the top of the stack.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800249 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800250 public void updateBubble(NotificationEntry notif, boolean updatePosition) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800251 if (mStackView != null && mBubbleData.getBubble(notif.key) != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800252 // It's an update
Mady Mellor3dff9e62019-02-05 18:12:53 -0800253 mStackView.updateBubble(notif, updatePosition);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800254 } else {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800255 if (mStackView == null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800256 mStackView = new BubbleStackView(mContext);
Mady Mellord1c78b262018-11-06 18:04:40 -0800257 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800258 // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim
259 // between bubble and the shade
260 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
261 sbv.addView(mStackView, bubblePosition,
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800262 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
Mady Mellorcd9b1302018-11-06 18:08:04 -0800263 if (mExpandListener != null) {
264 mStackView.setExpandListener(mExpandListener);
265 }
Mady Mellore8e07712019-01-23 12:45:33 -0800266 mStackView.setOnBlockedListener(this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800267 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800268 // It's new
Mady Mellor3dff9e62019-02-05 18:12:53 -0800269 mStackView.addBubble(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800270 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800271 updateVisibility();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800272 }
273
274 /**
275 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500276 * <p>
277 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800278 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500279 @MainThread
Ned Burns01e38212019-01-03 16:32:52 -0500280 void removeBubble(String key) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800281 if (mStackView != null) {
282 mStackView.removeBubble(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800283 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800284 mNotificationEntryManager.updateNotifications();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800285 updateVisibility();
Mady Mellord1c78b262018-11-06 18:04:40 -0800286 }
287
Mady Mellore8e07712019-01-23 12:45:33 -0800288 @Override
289 public void onBubbleBlocked(NotificationEntry entry) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800290 Object[] bubbles = mBubbleData.getBubbles().toArray();
Mady Mellore8e07712019-01-23 12:45:33 -0800291 for (int i = 0; i < bubbles.length; i++) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800292 NotificationEntry e = ((Bubble) bubbles[i]).entry;
Mady Mellore8e07712019-01-23 12:45:33 -0800293 boolean samePackage = entry.notification.getPackageName().equals(
294 e.notification.getPackageName());
295 if (samePackage) {
296 removeBubble(entry.key);
297 }
298 }
299 }
300
Ned Burns01e38212019-01-03 16:32:52 -0500301 @SuppressWarnings("FieldCanBeLocal")
302 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
303 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500304 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800305 if (!areBubblesEnabled(mContext)) {
306 return;
307 }
308 if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800309 // TODO: handle group summaries
310 // It's a new notif, it shows in the shade and as a bubble
Ned Burns01e38212019-01-03 16:32:52 -0500311 entry.setIsBubble(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800312 entry.setShowInShadeWhenBubble(true);
313 }
314 }
315
316 @Override
317 public void onEntryInflated(NotificationEntry entry,
318 @NotificationInflater.InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800319 if (!areBubblesEnabled(mContext)) {
320 return;
321 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800322 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
323 updateBubble(entry, true /* updatePosition */);
324 }
325 }
326
327 @Override
328 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800329 if (!areBubblesEnabled(mContext)) {
330 return;
331 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800332 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
333 && alertAgain(entry, entry.notification.getNotification())) {
334 entry.setShowInShadeWhenBubble(true);
335 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800336 updateBubble(entry, true /* updatePosition */);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800337 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800338 }
339 }
340
341 @Override
342 public void onEntryRemoved(NotificationEntry entry,
343 @Nullable NotificationVisibility visibility,
344 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800345 if (!areBubblesEnabled(mContext)) {
346 return;
347 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800348 entry.setShowInShadeWhenBubble(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800349 if (mStackView != null) {
350 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800351 }
352 if (!removedByUser) {
353 // This was a cancel so we should remove the bubble
354 removeBubble(entry.key);
Ned Burns01e38212019-01-03 16:32:52 -0500355 }
356 }
357 };
358
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800359 /**
360 * Lets any listeners know if bubble state has changed.
361 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800362 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800363 if (mStackView == null) {
364 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800365 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800366
Mady Mellord1c78b262018-11-06 18:04:40 -0800367 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800368 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800369 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800370 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
371 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
372 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800373 }
374
375 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800376 * Updates the visibility of the bubbles based on current state.
377 * Does not un-bubble, just hides or un-hides. Will notify any
378 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800379 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800380 public void updateVisibility() {
381 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
382 // Bubbles only appear in unlocked shade
383 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
384 } else if (mStackView != null) {
385 mStackView.setVisibility(INVISIBLE);
386 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800387 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800388 updateBubblesShowing();
389 }
390
391 /**
392 * Rect indicating the touchable region for the bubble stack / expanded stack.
393 */
394 public Rect getTouchableRegion() {
395 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
396 return null;
397 }
398 mStackView.getBoundsOnScreen(mTempRect);
399 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800400 }
401
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800402 @VisibleForTesting
403 BubbleStackView getStackView() {
404 return mStackView;
405 }
406
Mady Mellor5549dd22018-11-06 18:07:34 -0800407 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800408 * Whether the notification has been developer configured to bubble and is allowed by the user.
409 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800410 @VisibleForTesting
411 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800412 StatusBarNotification n = entry.notification;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800413 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
414 && n.getNotification().getBubbleMetadata().getIntent() != null;
Julia Reynolds4509ce72019-01-31 13:12:43 -0500415 return hasOverlayIntent && entry.canBubble;
Mady Mellorb4991e62019-01-10 15:14:51 -0800416 }
417
418 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800419 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800420 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800421 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800422 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800423 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800424 return false;
425 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800426 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800427
428 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
429 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
430 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
431
Mady Mellor5549dd22018-11-06 18:07:34 -0800432 boolean hasRemoteInput = false;
433 if (n.getNotification().actions != null) {
434 for (Notification.Action action : n.getNotification().actions) {
435 if (action.getRemoteInputs() != null) {
436 hasRemoteInput = true;
437 break;
438 }
439 }
440 }
Mady Mellor711f9562018-12-05 14:53:46 -0800441 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
442 && n.isOngoing();
443 boolean isMusic = n.getNotification().hasMediaSession();
444 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800445
Mady Mellor5549dd22018-11-06 18:07:34 -0800446 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800447 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
448 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800449 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800450 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800451 || autoBubbleAll;
452 }
453
Mark Renoufcecc77b2019-01-30 16:32:24 -0500454 /**
455 * This task stack listener is responsible for responding to tasks moved to the front
456 * which are on the default (main) display. When this happens, expanded bubbles must be
457 * collapsed so the user may interact with the app which was just moved to the front.
458 * <p>
459 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
460 * these calls via a main thread Handler.
461 */
462 @MainThread
463 private class BubbleTaskStackListener extends TaskStackChangeListener {
464
465 @Nullable
466 private ActivityManager.StackInfo findStackInfo(int taskId) throws RemoteException {
467 final List<ActivityManager.StackInfo> stackInfoList =
468 mActivityTaskManager.getAllStackInfos();
469 // Iterate through stacks from top to bottom.
470 final int stackCount = stackInfoList.size();
471 for (int stackIndex = 0; stackIndex < stackCount; stackIndex++) {
472 final ActivityManager.StackInfo stackInfo = stackInfoList.get(stackIndex);
473 // Iterate through tasks from top to bottom.
474 for (int taskIndex = stackInfo.taskIds.length - 1; taskIndex >= 0; taskIndex--) {
475 if (stackInfo.taskIds[taskIndex] == taskId) {
476 return stackInfo;
477 }
478 }
479 }
480 return null;
481 }
482
483 @Override
484 public void onTaskMovedToFront(int taskId) {
485 ActivityManager.StackInfo stackInfo = null;
486 try {
487 stackInfo = findStackInfo(taskId);
488 } catch (RemoteException e) {
489 e.rethrowAsRuntimeException();
490 }
491 if (stackInfo != null && stackInfo.displayId == Display.DEFAULT_DISPLAY
492 && mStackView != null) {
493 mStackView.collapseStack();
494 }
495 }
496
497 /**
498 * This is a workaround for the case when the activity had to be created in a new task.
499 * Existing code in ActivityStackSupervisor checks the display where the activity
500 * ultimately ended up, displays an error message toast, and calls this method instead of
501 * onTaskMovedToFront.
502 */
503 // TODO(b/124058588): add requestedDisplayId to this callback, ignore unless matches
504 @Override
505 public void onActivityLaunchOnSecondaryDisplayFailed() {
506 if (mStackView != null) {
507 mStackView.collapseStack();
508 }
509 }
510 }
511
Mady Mellorceced172018-11-27 11:18:39 -0800512 private static boolean shouldAutoBubbleMessages(Context context) {
513 return Settings.Secure.getInt(context.getContentResolver(),
514 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
515 }
516
517 private static boolean shouldAutoBubbleOngoing(Context context) {
518 return Settings.Secure.getInt(context.getContentResolver(),
519 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
520 }
521
522 private static boolean shouldAutoBubbleAll(Context context) {
523 return Settings.Secure.getInt(context.getContentResolver(),
524 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800525 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500526
Mady Mellor3dff9e62019-02-05 18:12:53 -0800527 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500528 return Settings.Secure.getInt(context.getContentResolver(),
529 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
530 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800531
532 private static boolean areBubblesEnabled(Context context) {
533 return Settings.Secure.getInt(context.getContentResolver(),
534 ENABLE_BUBBLES, 1) != 0;
535 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800536}