blob: 656d7577fd87730d0c0e75199cb66a735c81b8e5 [file] [log] [blame]
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.bubbles;
18
Mady Mellord1c78b262018-11-06 18:04:40 -080019import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080020import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080021import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080022
Mady Mellor3f2efdb2018-11-21 11:30:45 -080023import static com.android.systemui.statusbar.StatusBarState.SHADE;
24import static com.android.systemui.statusbar.notification.NotificationAlertingManager.alertAgain;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080025
Mady Mellorb4991e62019-01-10 15:14:51 -080026import android.annotation.Nullable;
Mark Renoufcecc77b2019-01-30 16:32:24 -050027import android.app.ActivityManager;
Mark Renoufc808f062019-02-07 15:20:37 -050028import android.app.ActivityManager.RunningTaskInfo;
Mark Renoufcecc77b2019-01-30 16:32:24 -050029import android.app.ActivityTaskManager;
30import android.app.IActivityTaskManager;
Mady Mellorb4991e62019-01-10 15:14:51 -080031import android.app.INotificationManager;
Mady Mellor5549dd22018-11-06 18:07:34 -080032import android.app.Notification;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080033import android.content.Context;
Joshua Tsujia19515f2019-02-13 18:02:29 -050034import android.content.pm.ParceledListSlice;
Mady Mellord1c78b262018-11-06 18:04:40 -080035import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050036import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080037import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080038import android.provider.Settings;
Mady Mellor5549dd22018-11-06 18:07:34 -080039import android.service.notification.StatusBarNotification;
Mark Renoufcecc77b2019-01-30 16:32:24 -050040import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050041import android.view.IPinnedStackController;
42import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080043import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080044import android.widget.FrameLayout;
45
Mark Renouf658c6bc2019-01-30 10:26:54 -050046import androidx.annotation.MainThread;
47
Mady Mellorebdbbb92018-11-15 14:36:48 -080048import com.android.internal.annotations.VisibleForTesting;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080049import com.android.internal.statusbar.NotificationVisibility;
Ned Burns01e38212019-01-03 16:32:52 -050050import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080051import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050052import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050053import com.android.systemui.shared.system.ActivityManagerWrapper;
54import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050055import com.android.systemui.shared.system.WindowManagerWrapper;
Ned Burns01e38212019-01-03 16:32:52 -050056import com.android.systemui.statusbar.notification.NotificationEntryListener;
57import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080058import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050059import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050060import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080061import com.android.systemui.statusbar.phone.StatusBarWindowController;
62
Mark Renoufcecc77b2019-01-30 16:32:24 -050063import java.util.List;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080064
Jason Monk27d01a622018-12-10 15:57:09 -050065import javax.inject.Inject;
66import javax.inject.Singleton;
67
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080068/**
69 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
70 * Bubbles can be expanded to show more content.
71 *
72 * The controller manages addition, removal, and visible state of bubbles on screen.
73 */
Jason Monk27d01a622018-12-10 15:57:09 -050074@Singleton
Mady Mellor3d82e682019-02-05 13:34:48 -080075public class BubbleController implements BubbleExpandedView.OnBubbleBlockedListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080076 private static final int MAX_BUBBLES = 5; // TODO: actually enforce this
77
78 private static final String TAG = "BubbleController";
79
Mady Mellor5549dd22018-11-06 18:07:34 -080080 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -050081 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -080082
Mady Mellor44ee2fe2019-01-30 17:51:16 -080083 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -080084 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Mady Mellor44ee2fe2019-01-30 17:51:16 -080085 /** Auto bubble flags set whether different notif types should be presented as a bubble */
Mady Mellorceced172018-11-27 11:18:39 -080086 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
87 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
88 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellor44ee2fe2019-01-30 17:51:16 -080089
90 /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */
Mark Renouf89b1a4a2018-12-04 14:59:45 -050091 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -080092
Mady Mellor44ee2fe2019-01-30 17:51:16 -080093 /** Whether the row of bubble circles are anchored to the top or bottom of the screen. */
94 private static final String ENABLE_BUBBLES_AT_TOP = "experiment_enable_top_bubbles";
95 /** Flag to position the header below the activity view */
96 private static final String ENABLE_BUBBLE_FOOTER = "experiment_enable_bubble_footer";
97
Ned Burns01e38212019-01-03 16:32:52 -050098 private final Context mContext;
99 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500100 private final IActivityTaskManager mActivityTaskManager;
101 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800102 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800103 private BubbleExpandListener mExpandListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800104
Mady Mellor3dff9e62019-02-05 18:12:53 -0800105 private BubbleData mBubbleData;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800106 private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800107
108 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500109 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800110 private StatusBarStateListener mStatusBarStateListener;
111
112 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider =
113 Dependency.get(NotificationInterruptionStateProvider.class);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800114
Mady Mellorb4991e62019-01-10 15:14:51 -0800115 private INotificationManager mNotificationManagerService;
116
Mady Mellord1c78b262018-11-06 18:04:40 -0800117 // Used for determining view rect for touch interaction
118 private Rect mTempRect = new Rect();
119
Mady Mellor5549dd22018-11-06 18:07:34 -0800120 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800121 * Listener to be notified when some states of the bubbles change.
122 */
123 public interface BubbleStateChangeListener {
124 /**
125 * Called when the stack has bubbles or no longer has bubbles.
126 */
127 void onHasBubblesChanged(boolean hasBubbles);
128 }
129
Mady Mellorcd9b1302018-11-06 18:08:04 -0800130 /**
131 * Listener to find out about stack expansion / collapse events.
132 */
133 public interface BubbleExpandListener {
134 /**
135 * Called when the expansion state of the bubble stack changes.
Mady Mellorcd9b1302018-11-06 18:08:04 -0800136 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800137 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800138 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800139 void onBubbleExpandChanged(boolean isExpanding, String key);
140 }
141
142 /**
143 * Listens for the current state of the status bar and updates the visibility state
144 * of bubbles as needed.
145 */
146 private class StatusBarStateListener implements StatusBarStateController.StateListener {
147 private int mState;
148 /**
149 * Returns the current status bar state.
150 */
151 public int getCurrentState() {
152 return mState;
153 }
154
155 @Override
156 public void onStateChanged(int newState) {
157 mState = newState;
158 updateVisibility();
159 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800160 }
161
Jason Monk27d01a622018-12-10 15:57:09 -0500162 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800163 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
164 BubbleData data) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800165 mContext = context;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800166
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800167 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500168 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800169
170 try {
171 mNotificationManagerService = INotificationManager.Stub.asInterface(
172 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
173 } catch (ServiceManager.ServiceNotFoundException e) {
174 e.printStackTrace();
175 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800176
177 mStatusBarWindowController = statusBarWindowController;
178 mStatusBarStateListener = new StatusBarStateListener();
179 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500180
181 mActivityTaskManager = ActivityTaskManager.getService();
182 mTaskStackListener = new BubbleTaskStackListener();
183 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800184
Joshua Tsujia19515f2019-02-13 18:02:29 -0500185 try {
186 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
187 } catch (RemoteException e) {
188 e.printStackTrace();
189 }
190
Mady Mellorcfd06c12019-02-13 14:32:12 -0800191 mBubbleData = data;
Mady Mellor5549dd22018-11-06 18:07:34 -0800192 }
193
194 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800195 * Set a listener to be notified when some states of the bubbles change.
196 */
197 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
198 mStateChangeListener = listener;
199 }
200
201 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800202 * Set a listener to be notified of bubble expand events.
203 */
204 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100205 mExpandListener = ((isExpanding, key) -> {
206 if (listener != null) {
207 listener.onBubbleExpandChanged(isExpanding, key);
208 }
209 mStatusBarWindowController.setBubbleExpanded(isExpanding);
210 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800211 if (mStackView != null) {
212 mStackView.setExpandListener(mExpandListener);
213 }
214 }
215
216 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800217 * Whether or not there are bubbles present, regardless of them being visible on the
218 * screen (e.g. if on AOD).
219 */
220 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800221 if (mStackView == null) {
222 return false;
223 }
224 for (Bubble bubble : mBubbleData.getBubbles()) {
225 if (!bubble.entry.isBubbleDismissed()) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800226 return true;
227 }
228 }
229 return false;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800230 }
231
232 /**
233 * Whether the stack of bubbles is expanded or not.
234 */
235 public boolean isStackExpanded() {
236 return mStackView != null && mStackView.isExpanded();
237 }
238
239 /**
240 * Tell the stack of bubbles to collapse.
241 */
242 public void collapseStack() {
243 if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800244 mStackView.collapseStack();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800245 }
246 }
247
248 /**
249 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
250 */
Ned Burns01e38212019-01-03 16:32:52 -0500251 void dismissStack() {
Mady Mellord1c78b262018-11-06 18:04:40 -0800252 if (mStackView == null) {
253 return;
254 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800255 mStackView.stackDismissed();
256
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800257 updateVisibility();
Ned Burns01e38212019-01-03 16:32:52 -0500258 mNotificationEntryManager.updateNotifications();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800259 }
260
261 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500262 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
263 * is forwarded a back key down/up pair.
264 */
265 public void performBackPressIfNeeded() {
266 if (mStackView != null) {
267 mStackView.performBackPressIfNeeded();
268 }
269 }
270
271 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800272 * Adds or updates a bubble associated with the provided notification entry.
273 *
274 * @param notif the notification associated with this bubble.
275 * @param updatePosition whether this update should promote the bubble to the top of the stack.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800276 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800277 public void updateBubble(NotificationEntry notif, boolean updatePosition) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800278 if (mStackView != null && mBubbleData.getBubble(notif.key) != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800279 // It's an update
Mady Mellor3dff9e62019-02-05 18:12:53 -0800280 mStackView.updateBubble(notif, updatePosition);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800281 } else {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800282 if (mStackView == null) {
Mady Mellorcfd06c12019-02-13 14:32:12 -0800283 mStackView = new BubbleStackView(mContext, mBubbleData);
Mady Mellord1c78b262018-11-06 18:04:40 -0800284 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800285 // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim
286 // between bubble and the shade
287 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
288 sbv.addView(mStackView, bubblePosition,
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800289 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
Mady Mellorcd9b1302018-11-06 18:08:04 -0800290 if (mExpandListener != null) {
291 mStackView.setExpandListener(mExpandListener);
292 }
Mady Mellore8e07712019-01-23 12:45:33 -0800293 mStackView.setOnBlockedListener(this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800294 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800295 // It's new
Mady Mellor3dff9e62019-02-05 18:12:53 -0800296 mStackView.addBubble(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800297 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800298 updateVisibility();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800299 }
300
301 /**
302 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500303 * <p>
304 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800305 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500306 @MainThread
Ned Burns01e38212019-01-03 16:32:52 -0500307 void removeBubble(String key) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800308 if (mStackView != null) {
309 mStackView.removeBubble(key);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800310 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800311 mNotificationEntryManager.updateNotifications();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800312 updateVisibility();
Mady Mellord1c78b262018-11-06 18:04:40 -0800313 }
314
Mady Mellore8e07712019-01-23 12:45:33 -0800315 @Override
316 public void onBubbleBlocked(NotificationEntry entry) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800317 Object[] bubbles = mBubbleData.getBubbles().toArray();
Mady Mellore8e07712019-01-23 12:45:33 -0800318 for (int i = 0; i < bubbles.length; i++) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800319 NotificationEntry e = ((Bubble) bubbles[i]).entry;
Mady Mellore8e07712019-01-23 12:45:33 -0800320 boolean samePackage = entry.notification.getPackageName().equals(
321 e.notification.getPackageName());
322 if (samePackage) {
323 removeBubble(entry.key);
324 }
325 }
326 }
327
Ned Burns01e38212019-01-03 16:32:52 -0500328 @SuppressWarnings("FieldCanBeLocal")
329 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
330 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500331 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800332 if (!areBubblesEnabled(mContext)) {
333 return;
334 }
335 if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800336 // TODO: handle group summaries
337 // It's a new notif, it shows in the shade and as a bubble
Ned Burns01e38212019-01-03 16:32:52 -0500338 entry.setIsBubble(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800339 entry.setShowInShadeWhenBubble(true);
340 }
341 }
342
343 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500344 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800345 if (!areBubblesEnabled(mContext)) {
346 return;
347 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800348 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
349 updateBubble(entry, true /* updatePosition */);
350 }
351 }
352
353 @Override
354 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800355 if (!areBubblesEnabled(mContext)) {
356 return;
357 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800358 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
359 && alertAgain(entry, entry.notification.getNotification())) {
360 entry.setShowInShadeWhenBubble(true);
361 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800362 updateBubble(entry, true /* updatePosition */);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800363 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800364 }
365 }
366
367 @Override
368 public void onEntryRemoved(NotificationEntry entry,
369 @Nullable NotificationVisibility visibility,
370 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800371 if (!areBubblesEnabled(mContext)) {
372 return;
373 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800374 entry.setShowInShadeWhenBubble(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800375 if (mStackView != null) {
376 mStackView.updateDotVisibility(entry.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800377 }
378 if (!removedByUser) {
379 // This was a cancel so we should remove the bubble
380 removeBubble(entry.key);
Ned Burns01e38212019-01-03 16:32:52 -0500381 }
382 }
383 };
384
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800385 /**
386 * Lets any listeners know if bubble state has changed.
387 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800388 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800389 if (mStackView == null) {
390 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800391 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800392
Mady Mellord1c78b262018-11-06 18:04:40 -0800393 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800394 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800395 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800396 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
397 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
398 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800399 }
400
401 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800402 * Updates the visibility of the bubbles based on current state.
403 * Does not un-bubble, just hides or un-hides. Will notify any
404 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800405 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800406 public void updateVisibility() {
407 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
408 // Bubbles only appear in unlocked shade
409 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
410 } else if (mStackView != null) {
411 mStackView.setVisibility(INVISIBLE);
412 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800413 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800414 updateBubblesShowing();
415 }
416
417 /**
418 * Rect indicating the touchable region for the bubble stack / expanded stack.
419 */
420 public Rect getTouchableRegion() {
421 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
422 return null;
423 }
424 mStackView.getBoundsOnScreen(mTempRect);
425 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800426 }
427
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800428 @VisibleForTesting
429 BubbleStackView getStackView() {
430 return mStackView;
431 }
432
Mady Mellor5549dd22018-11-06 18:07:34 -0800433 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800434 * Whether the notification has been developer configured to bubble and is allowed by the user.
435 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800436 @VisibleForTesting
437 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800438 StatusBarNotification n = entry.notification;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800439 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
440 && n.getNotification().getBubbleMetadata().getIntent() != null;
Julia Reynolds4509ce72019-01-31 13:12:43 -0500441 return hasOverlayIntent && entry.canBubble;
Mady Mellorb4991e62019-01-10 15:14:51 -0800442 }
443
444 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800445 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800446 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800447 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800448 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800449 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800450 return false;
451 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800452 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800453
454 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
455 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
456 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
457
Mady Mellor5549dd22018-11-06 18:07:34 -0800458 boolean hasRemoteInput = false;
459 if (n.getNotification().actions != null) {
460 for (Notification.Action action : n.getNotification().actions) {
461 if (action.getRemoteInputs() != null) {
462 hasRemoteInput = true;
463 break;
464 }
465 }
466 }
Mady Mellor711f9562018-12-05 14:53:46 -0800467 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
468 && n.isOngoing();
469 boolean isMusic = n.getNotification().hasMediaSession();
470 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800471
Mady Mellor5549dd22018-11-06 18:07:34 -0800472 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800473 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
474 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800475 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800476 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800477 || autoBubbleAll;
478 }
479
Mark Renoufcecc77b2019-01-30 16:32:24 -0500480 /**
481 * This task stack listener is responsible for responding to tasks moved to the front
482 * which are on the default (main) display. When this happens, expanded bubbles must be
483 * collapsed so the user may interact with the app which was just moved to the front.
484 * <p>
485 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
486 * these calls via a main thread Handler.
487 */
488 @MainThread
489 private class BubbleTaskStackListener extends TaskStackChangeListener {
490
491 @Nullable
492 private ActivityManager.StackInfo findStackInfo(int taskId) throws RemoteException {
493 final List<ActivityManager.StackInfo> stackInfoList =
494 mActivityTaskManager.getAllStackInfos();
495 // Iterate through stacks from top to bottom.
496 final int stackCount = stackInfoList.size();
497 for (int stackIndex = 0; stackIndex < stackCount; stackIndex++) {
498 final ActivityManager.StackInfo stackInfo = stackInfoList.get(stackIndex);
499 // Iterate through tasks from top to bottom.
500 for (int taskIndex = stackInfo.taskIds.length - 1; taskIndex >= 0; taskIndex--) {
501 if (stackInfo.taskIds[taskIndex] == taskId) {
502 return stackInfo;
503 }
504 }
505 }
506 return null;
507 }
508
509 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500510 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
511 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500512 mStackView.collapseStack();
513 }
514 }
515
516 /**
517 * This is a workaround for the case when the activity had to be created in a new task.
518 * Existing code in ActivityStackSupervisor checks the display where the activity
519 * ultimately ended up, displays an error message toast, and calls this method instead of
520 * onTaskMovedToFront.
521 */
Mark Renoufcecc77b2019-01-30 16:32:24 -0500522 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500523 public void onActivityLaunchOnSecondaryDisplayFailed(RunningTaskInfo taskInfo) {
524 // TODO(b/124058588): move to ActivityView.StateCallback, filter on virtualDisplay ID
Mark Renoufcecc77b2019-01-30 16:32:24 -0500525 if (mStackView != null) {
526 mStackView.collapseStack();
527 }
528 }
529 }
530
Mady Mellorceced172018-11-27 11:18:39 -0800531 private static boolean shouldAutoBubbleMessages(Context context) {
532 return Settings.Secure.getInt(context.getContentResolver(),
533 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
534 }
535
536 private static boolean shouldAutoBubbleOngoing(Context context) {
537 return Settings.Secure.getInt(context.getContentResolver(),
538 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
539 }
540
541 private static boolean shouldAutoBubbleAll(Context context) {
542 return Settings.Secure.getInt(context.getContentResolver(),
543 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800544 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500545
Mady Mellor3dff9e62019-02-05 18:12:53 -0800546 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500547 return Settings.Secure.getInt(context.getContentResolver(),
548 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
549 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800550
551 private static boolean areBubblesEnabled(Context context) {
552 return Settings.Secure.getInt(context.getContentResolver(),
553 ENABLE_BUBBLES, 1) != 0;
554 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500555
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800556 /**
557 * Whether bubbles should be positioned at the top of the screen or not.
558 */
559 public static boolean showBubblesAtTop(Context context) {
560 return Settings.Secure.getInt(context.getContentResolver(),
561 ENABLE_BUBBLES_AT_TOP, 0) != 0;
562 }
563
564 /**
565 * Whether the bubble chrome should display as a footer or not (in which case it's a header).
566 */
567 public static boolean useFooter(Context context) {
568 return Settings.Secure.getInt(context.getContentResolver(),
569 ENABLE_BUBBLE_FOOTER, 0) != 0;
570 }
571
Joshua Tsujia19515f2019-02-13 18:02:29 -0500572 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
573 private class BubblesImeListener extends IPinnedStackListener.Stub {
574
575 @Override
576 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
577 }
578
579 @Override
580 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
581 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
582 int displayRotation) throws RemoteException {}
583
584 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500585 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
586 if (mStackView != null && mStackView.getBubbleCount() > 0) {
587 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500588 }
589 }
590
591 @Override
592 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
593 throws RemoteException {}
594
595 @Override
596 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
597
598 @Override
599 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
600 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800601}