blob: 12f226a7eeb5d1e64c93194cd8dc5fd2b7b56ecf [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
Steven Wu552f63f2019-02-05 13:41:36 -050019import static android.content.pm.ActivityInfo.DOCUMENT_LAUNCH_ALWAYS;
20import static android.util.StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_MISSING;
21import static android.util.StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_NOT_RESIZABLE;
22import static android.util.StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__DOCUMENT_LAUNCH_NOT_ALWAYS;
Mady Mellord1c78b262018-11-06 18:04:40 -080023import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080024import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080025import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080026
Mady Mellor3f2efdb2018-11-21 11:30:45 -080027import static com.android.systemui.statusbar.StatusBarState.SHADE;
28import static com.android.systemui.statusbar.notification.NotificationAlertingManager.alertAgain;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080029
Mady Mellorb4991e62019-01-10 15:14:51 -080030import android.annotation.Nullable;
Mark Renoufcecc77b2019-01-30 16:32:24 -050031import android.app.ActivityManager;
32import android.app.ActivityTaskManager;
33import android.app.IActivityTaskManager;
Mady Mellorb4991e62019-01-10 15:14:51 -080034import android.app.INotificationManager;
Mady Mellor5549dd22018-11-06 18:07:34 -080035import android.app.Notification;
Mark Renouf89b1a4a2018-12-04 14:59:45 -050036import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080037import android.content.Context;
Mark Renouf89b1a4a2018-12-04 14:59:45 -050038import android.content.pm.ActivityInfo;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080039import android.graphics.Point;
Mady Mellord1c78b262018-11-06 18:04:40 -080040import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050041import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080042import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080043import android.provider.Settings;
Mady Mellor5549dd22018-11-06 18:07:34 -080044import android.service.notification.StatusBarNotification;
Mark Renouf89b1a4a2018-12-04 14:59:45 -050045import android.util.Log;
Steven Wu552f63f2019-02-05 13:41:36 -050046import android.util.StatsLog;
Mark Renoufcecc77b2019-01-30 16:32:24 -050047import android.view.Display;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080048import android.view.LayoutInflater;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080049import android.view.ViewGroup;
50import android.view.WindowManager;
51import android.widget.FrameLayout;
52
Mark Renouf658c6bc2019-01-30 10:26:54 -050053import androidx.annotation.MainThread;
54
Mady Mellorebdbbb92018-11-15 14:36:48 -080055import com.android.internal.annotations.VisibleForTesting;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080056import com.android.internal.statusbar.NotificationVisibility;
Ned Burns01e38212019-01-03 16:32:52 -050057import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080058import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050059import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050060import com.android.systemui.shared.system.ActivityManagerWrapper;
61import com.android.systemui.shared.system.TaskStackChangeListener;
Ned Burns01e38212019-01-03 16:32:52 -050062import com.android.systemui.statusbar.notification.NotificationEntryListener;
63import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080064import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050065import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050066import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080067import com.android.systemui.statusbar.phone.StatusBarWindowController;
68
69import java.util.HashMap;
Mark Renoufcecc77b2019-01-30 16:32:24 -050070import java.util.List;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080071import java.util.Map;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080072import java.util.Set;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080073
Jason Monk27d01a622018-12-10 15:57:09 -050074import javax.inject.Inject;
75import javax.inject.Singleton;
76
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080077/**
78 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
79 * Bubbles can be expanded to show more content.
80 *
81 * The controller manages addition, removal, and visible state of bubbles on screen.
82 */
Jason Monk27d01a622018-12-10 15:57:09 -050083@Singleton
Mady Mellor3d82e682019-02-05 13:34:48 -080084public class BubbleController implements BubbleExpandedView.OnBubbleBlockedListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080085 private static final int MAX_BUBBLES = 5; // TODO: actually enforce this
86
87 private static final String TAG = "BubbleController";
88
Mady Mellor5549dd22018-11-06 18:07:34 -080089 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -050090 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -080091
Mady Mellorf6e3ac02019-01-29 10:37:52 -080092 // Secure settings flags
93 // Feature level flag
94 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
95 // Auto bubble flags set whether different notification types should be presented as a bubble
Mady Mellorceced172018-11-27 11:18:39 -080096 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
97 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
98 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellorf6e3ac02019-01-29 10:37:52 -080099 // Use an activity view for an auto-bubbled notification if it has an appropriate content intent
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500100 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -0800101
Ned Burns01e38212019-01-03 16:32:52 -0500102 private final Context mContext;
103 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500104 private final IActivityTaskManager mActivityTaskManager;
105 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800106 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800107 private BubbleExpandListener mExpandListener;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800108 private LayoutInflater mInflater;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800109
Ned Burns01e38212019-01-03 16:32:52 -0500110 private final Map<String, BubbleView> mBubbles = new HashMap<>();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800111 private BubbleStackView mStackView;
Ned Burns01e38212019-01-03 16:32:52 -0500112 private final Point mDisplaySize;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800113
114 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500115 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800116 private StatusBarStateListener mStatusBarStateListener;
117
118 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider =
119 Dependency.get(NotificationInterruptionStateProvider.class);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800120
Mady Mellorb4991e62019-01-10 15:14:51 -0800121 private INotificationManager mNotificationManagerService;
122
Mady Mellord1c78b262018-11-06 18:04:40 -0800123 // Used for determining view rect for touch interaction
124 private Rect mTempRect = new Rect();
125
Mady Mellor5549dd22018-11-06 18:07:34 -0800126 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800127 * Listener to be notified when some states of the bubbles change.
128 */
129 public interface BubbleStateChangeListener {
130 /**
131 * Called when the stack has bubbles or no longer has bubbles.
132 */
133 void onHasBubblesChanged(boolean hasBubbles);
134 }
135
Mady Mellorcd9b1302018-11-06 18:08:04 -0800136 /**
137 * Listener to find out about stack expansion / collapse events.
138 */
139 public interface BubbleExpandListener {
140 /**
141 * Called when the expansion state of the bubble stack changes.
Mady Mellorcd9b1302018-11-06 18:08:04 -0800142 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800143 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800144 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800145 void onBubbleExpandChanged(boolean isExpanding, String key);
146 }
147
148 /**
149 * Listens for the current state of the status bar and updates the visibility state
150 * of bubbles as needed.
151 */
152 private class StatusBarStateListener implements StatusBarStateController.StateListener {
153 private int mState;
154 /**
155 * Returns the current status bar state.
156 */
157 public int getCurrentState() {
158 return mState;
159 }
160
161 @Override
162 public void onStateChanged(int newState) {
163 mState = newState;
164 updateVisibility();
165 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800166 }
167
Jason Monk27d01a622018-12-10 15:57:09 -0500168 @Inject
Jason Monk92d5c242018-12-21 14:37:34 -0500169 public BubbleController(Context context, StatusBarWindowController statusBarWindowController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800170 mContext = context;
171 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
172 mDisplaySize = new Point();
173 wm.getDefaultDisplay().getSize(mDisplaySize);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800174 mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800175
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800176 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500177 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800178
179 try {
180 mNotificationManagerService = INotificationManager.Stub.asInterface(
181 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
182 } catch (ServiceManager.ServiceNotFoundException e) {
183 e.printStackTrace();
184 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800185
186 mStatusBarWindowController = statusBarWindowController;
187 mStatusBarStateListener = new StatusBarStateListener();
188 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500189
190 mActivityTaskManager = ActivityTaskManager.getService();
191 mTaskStackListener = new BubbleTaskStackListener();
192 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor5549dd22018-11-06 18:07:34 -0800193 }
194
195 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800196 * Set a listener to be notified when some states of the bubbles change.
197 */
198 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
199 mStateChangeListener = listener;
200 }
201
202 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800203 * Set a listener to be notified of bubble expand events.
204 */
205 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100206 mExpandListener = ((isExpanding, key) -> {
207 if (listener != null) {
208 listener.onBubbleExpandChanged(isExpanding, key);
209 }
210 mStatusBarWindowController.setBubbleExpanded(isExpanding);
211 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800212 if (mStackView != null) {
213 mStackView.setExpandListener(mExpandListener);
214 }
215 }
216
217 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800218 * Whether or not there are bubbles present, regardless of them being visible on the
219 * screen (e.g. if on AOD).
220 */
221 public boolean hasBubbles() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800222 for (BubbleView bv : mBubbles.values()) {
223 if (!bv.getEntry().isBubbleDismissed()) {
224 return true;
225 }
226 }
227 return false;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800228 }
229
230 /**
231 * Whether the stack of bubbles is expanded or not.
232 */
233 public boolean isStackExpanded() {
234 return mStackView != null && mStackView.isExpanded();
235 }
236
237 /**
238 * Tell the stack of bubbles to collapse.
239 */
240 public void collapseStack() {
241 if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800242 mStackView.collapseStack();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800243 }
244 }
245
246 /**
247 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
248 */
Ned Burns01e38212019-01-03 16:32:52 -0500249 void dismissStack() {
Mady Mellord1c78b262018-11-06 18:04:40 -0800250 if (mStackView == null) {
251 return;
252 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800253 Set<String> keys = mBubbles.keySet();
254 for (String key: keys) {
255 mBubbles.get(key).getEntry().setBubbleDismissed(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800256 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800257 mStackView.stackDismissed();
258
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800259 updateVisibility();
Ned Burns01e38212019-01-03 16:32:52 -0500260 mNotificationEntryManager.updateNotifications();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800261 }
262
263 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800264 * Adds or updates a bubble associated with the provided notification entry.
265 *
266 * @param notif the notification associated with this bubble.
267 * @param updatePosition whether this update should promote the bubble to the top of the stack.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800268 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800269 public void updateBubble(NotificationEntry notif, boolean updatePosition) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800270 if (mBubbles.containsKey(notif.key)) {
271 // It's an update
272 BubbleView bubble = mBubbles.get(notif.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800273 mStackView.updateBubble(bubble, notif, updatePosition);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800274 } else {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800275 if (mStackView == null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800276 mStackView = new BubbleStackView(mContext);
Mady Mellord1c78b262018-11-06 18:04:40 -0800277 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800278 // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim
279 // between bubble and the shade
280 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
281 sbv.addView(mStackView, bubblePosition,
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800282 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
Mady Mellorcd9b1302018-11-06 18:08:04 -0800283 if (mExpandListener != null) {
284 mStackView.setExpandListener(mExpandListener);
285 }
Mady Mellore8e07712019-01-23 12:45:33 -0800286 mStackView.setOnBlockedListener(this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800287 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800288 // It's new
289 BubbleView bubble = (BubbleView) mInflater.inflate(
290 R.layout.bubble_view, mStackView, false /* attachToRoot */);
291 bubble.setNotif(notif);
Mady Melloredd4ee12019-01-18 10:45:11 -0800292 PendingIntent bubbleIntent = getValidBubbleIntent(notif);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800293 if (bubbleIntent != null) {
294 bubble.setBubbleIntent(bubbleIntent);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800295 }
296 mBubbles.put(bubble.getKey(), bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800297 mStackView.addBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800298 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800299 updateVisibility();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800300 }
301
302 /**
303 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500304 * <p>
305 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800306 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500307 @MainThread
Ned Burns01e38212019-01-03 16:32:52 -0500308 void removeBubble(String key) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800309 BubbleView bv = mBubbles.remove(key);
Mady Mellord1c78b262018-11-06 18:04:40 -0800310 if (mStackView != null && bv != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800311 mStackView.removeBubble(bv);
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500312 bv.destroyActivityView(mStackView);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800313 }
Ned Burns01e38212019-01-03 16:32:52 -0500314
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800315 NotificationEntry entry = bv != null ? bv.getEntry() : null;
Ned Burns01e38212019-01-03 16:32:52 -0500316 if (entry != null) {
317 entry.setBubbleDismissed(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800318 mNotificationEntryManager.updateNotifications();
Mady Mellor5549dd22018-11-06 18:07:34 -0800319 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800320 updateVisibility();
Mady Mellord1c78b262018-11-06 18:04:40 -0800321 }
322
Mady Mellore8e07712019-01-23 12:45:33 -0800323 @Override
324 public void onBubbleBlocked(NotificationEntry entry) {
325 Object[] bubbles = mBubbles.values().toArray();
326 for (int i = 0; i < bubbles.length; i++) {
327 NotificationEntry e = ((BubbleView) bubbles[i]).getEntry();
328 boolean samePackage = entry.notification.getPackageName().equals(
329 e.notification.getPackageName());
330 if (samePackage) {
331 removeBubble(entry.key);
332 }
333 }
334 }
335
Ned Burns01e38212019-01-03 16:32:52 -0500336 @SuppressWarnings("FieldCanBeLocal")
337 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
338 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500339 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800340 if (!areBubblesEnabled(mContext)) {
341 return;
342 }
343 if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800344 // TODO: handle group summaries
345 // It's a new notif, it shows in the shade and as a bubble
Ned Burns01e38212019-01-03 16:32:52 -0500346 entry.setIsBubble(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800347 entry.setShowInShadeWhenBubble(true);
348 }
349 }
350
351 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500352 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800353 if (!areBubblesEnabled(mContext)) {
354 return;
355 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800356 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
357 updateBubble(entry, true /* updatePosition */);
358 }
359 }
360
361 @Override
362 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800363 if (!areBubblesEnabled(mContext)) {
364 return;
365 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800366 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
367 && alertAgain(entry, entry.notification.getNotification())) {
368 entry.setShowInShadeWhenBubble(true);
369 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
370 if (mBubbles.containsKey(entry.key)) {
371 mBubbles.get(entry.key).updateDotVisibility();
372 }
373 updateBubble(entry, true /* updatePosition */);
374 }
375 }
376
377 @Override
378 public void onEntryRemoved(NotificationEntry entry,
379 @Nullable NotificationVisibility visibility,
380 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800381 if (!areBubblesEnabled(mContext)) {
382 return;
383 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800384 entry.setShowInShadeWhenBubble(false);
385 if (mBubbles.containsKey(entry.key)) {
386 mBubbles.get(entry.key).updateDotVisibility();
387 }
388 if (!removedByUser) {
389 // This was a cancel so we should remove the bubble
390 removeBubble(entry.key);
Ned Burns01e38212019-01-03 16:32:52 -0500391 }
392 }
393 };
394
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800395 /**
396 * Lets any listeners know if bubble state has changed.
397 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800398 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800399 if (mStackView == null) {
400 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800401 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800402
Mady Mellord1c78b262018-11-06 18:04:40 -0800403 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800404 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800405 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800406 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
407 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
408 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800409 }
410
411 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800412 * Updates the visibility of the bubbles based on current state.
413 * Does not un-bubble, just hides or un-hides. Will notify any
414 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800415 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800416 public void updateVisibility() {
417 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
418 // Bubbles only appear in unlocked shade
419 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
420 } else if (mStackView != null) {
421 mStackView.setVisibility(INVISIBLE);
422 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800423 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800424 updateBubblesShowing();
425 }
426
427 /**
428 * Rect indicating the touchable region for the bubble stack / expanded stack.
429 */
430 public Rect getTouchableRegion() {
431 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
432 return null;
433 }
434 mStackView.getBoundsOnScreen(mTempRect);
435 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800436 }
437
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800438 @VisibleForTesting
439 BubbleStackView getStackView() {
440 return mStackView;
441 }
442
443 @Nullable
444 private PendingIntent getValidBubbleIntent(NotificationEntry notif) {
445 Notification notification = notif.notification.getNotification();
Steven Wu552f63f2019-02-05 13:41:36 -0500446 String packageName = notif.notification.getPackageName();
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800447 Notification.BubbleMetadata data = notif.getBubbleMetadata();
Steven Wu552f63f2019-02-05 13:41:36 -0500448 if (data != null && canLaunchInActivityView(data.getIntent(),
449 true /* enable logging for bubbles */, packageName)) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800450 return data.getIntent();
Steven Wu552f63f2019-02-05 13:41:36 -0500451 }
452 if (shouldUseContentIntent(mContext)
453 && canLaunchInActivityView(notification.contentIntent,
454 false /* disable logging for notifications */, packageName)) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800455 Log.d(TAG, "[addBubble " + notif.key
456 + "]: No appOverlayIntent, using contentIntent.");
457 return notification.contentIntent;
458 }
459 Log.d(TAG, "[addBubble " + notif.key + "]: No supported intent for ActivityView.");
460 return null;
461 }
462
463 /**
464 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
Steven Wu552f63f2019-02-05 13:41:36 -0500465 *
466 * @param intent the pending intent of the bubble.
467 * @param enableLogging whether bubble developer error should be logged.
468 * @param packageName the notification package name for this bubble.
469 * @return
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800470 */
Steven Wu552f63f2019-02-05 13:41:36 -0500471 private boolean canLaunchInActivityView(PendingIntent intent, boolean enableLogging,
472 String packageName) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500473 if (intent == null) {
474 return false;
475 }
476 ActivityInfo info =
477 intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0);
Steven Wu552f63f2019-02-05 13:41:36 -0500478 if (info == null) {
479 if (enableLogging) {
480 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
481 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_MISSING);
482 }
483 return false;
484 }
485 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
486 if (enableLogging) {
487 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
488 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_NOT_RESIZABLE);
489 }
490 return false;
491 }
492 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
493 if (enableLogging) {
494 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
495 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__DOCUMENT_LAUNCH_NOT_ALWAYS);
496 }
497 return false;
498 }
499 return (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0;
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500500 }
501
Mady Mellor5549dd22018-11-06 18:07:34 -0800502 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800503 * Whether the notification has been developer configured to bubble and is allowed by the user.
504 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800505 @VisibleForTesting
506 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800507 StatusBarNotification n = entry.notification;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800508 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
509 && n.getNotification().getBubbleMetadata().getIntent() != null;
Julia Reynolds4509ce72019-01-31 13:12:43 -0500510 return hasOverlayIntent && entry.canBubble;
Mady Mellorb4991e62019-01-10 15:14:51 -0800511 }
512
513 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800514 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800515 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800516 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800517 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800518 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800519 return false;
520 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800521 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800522
523 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
524 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
525 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
526
Mady Mellor5549dd22018-11-06 18:07:34 -0800527 boolean hasRemoteInput = false;
528 if (n.getNotification().actions != null) {
529 for (Notification.Action action : n.getNotification().actions) {
530 if (action.getRemoteInputs() != null) {
531 hasRemoteInput = true;
532 break;
533 }
534 }
535 }
Mady Mellor711f9562018-12-05 14:53:46 -0800536 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
537 && n.isOngoing();
538 boolean isMusic = n.getNotification().hasMediaSession();
539 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800540
Mady Mellor5549dd22018-11-06 18:07:34 -0800541 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800542 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
543 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800544 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800545 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800546 || autoBubbleAll;
547 }
548
Mark Renoufcecc77b2019-01-30 16:32:24 -0500549 /**
550 * This task stack listener is responsible for responding to tasks moved to the front
551 * which are on the default (main) display. When this happens, expanded bubbles must be
552 * collapsed so the user may interact with the app which was just moved to the front.
553 * <p>
554 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
555 * these calls via a main thread Handler.
556 */
557 @MainThread
558 private class BubbleTaskStackListener extends TaskStackChangeListener {
559
560 @Nullable
561 private ActivityManager.StackInfo findStackInfo(int taskId) throws RemoteException {
562 final List<ActivityManager.StackInfo> stackInfoList =
563 mActivityTaskManager.getAllStackInfos();
564 // Iterate through stacks from top to bottom.
565 final int stackCount = stackInfoList.size();
566 for (int stackIndex = 0; stackIndex < stackCount; stackIndex++) {
567 final ActivityManager.StackInfo stackInfo = stackInfoList.get(stackIndex);
568 // Iterate through tasks from top to bottom.
569 for (int taskIndex = stackInfo.taskIds.length - 1; taskIndex >= 0; taskIndex--) {
570 if (stackInfo.taskIds[taskIndex] == taskId) {
571 return stackInfo;
572 }
573 }
574 }
575 return null;
576 }
577
578 @Override
579 public void onTaskMovedToFront(int taskId) {
580 ActivityManager.StackInfo stackInfo = null;
581 try {
582 stackInfo = findStackInfo(taskId);
583 } catch (RemoteException e) {
584 e.rethrowAsRuntimeException();
585 }
586 if (stackInfo != null && stackInfo.displayId == Display.DEFAULT_DISPLAY
587 && mStackView != null) {
588 mStackView.collapseStack();
589 }
590 }
591
592 /**
593 * This is a workaround for the case when the activity had to be created in a new task.
594 * Existing code in ActivityStackSupervisor checks the display where the activity
595 * ultimately ended up, displays an error message toast, and calls this method instead of
596 * onTaskMovedToFront.
597 */
598 // TODO(b/124058588): add requestedDisplayId to this callback, ignore unless matches
599 @Override
600 public void onActivityLaunchOnSecondaryDisplayFailed() {
601 if (mStackView != null) {
602 mStackView.collapseStack();
603 }
604 }
605 }
606
Mady Mellorceced172018-11-27 11:18:39 -0800607 private static boolean shouldAutoBubbleMessages(Context context) {
608 return Settings.Secure.getInt(context.getContentResolver(),
609 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
610 }
611
612 private static boolean shouldAutoBubbleOngoing(Context context) {
613 return Settings.Secure.getInt(context.getContentResolver(),
614 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
615 }
616
617 private static boolean shouldAutoBubbleAll(Context context) {
618 return Settings.Secure.getInt(context.getContentResolver(),
619 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800620 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500621
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500622 private static boolean shouldUseContentIntent(Context context) {
623 return Settings.Secure.getInt(context.getContentResolver(),
624 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
625 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800626
627 private static boolean areBubblesEnabled(Context context) {
628 return Settings.Secure.getInt(context.getContentResolver(),
629 ENABLE_BUBBLES, 1) != 0;
630 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800631}