blob: 41bc1b29075c94be84ed62c1ffe3fcd93271d55a [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;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080066import com.android.systemui.statusbar.notification.row.NotificationInflater;
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
352 public void onEntryInflated(NotificationEntry entry,
353 @NotificationInflater.InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800354 if (!areBubblesEnabled(mContext)) {
355 return;
356 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800357 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
358 updateBubble(entry, true /* updatePosition */);
359 }
360 }
361
362 @Override
363 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800364 if (!areBubblesEnabled(mContext)) {
365 return;
366 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800367 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
368 && alertAgain(entry, entry.notification.getNotification())) {
369 entry.setShowInShadeWhenBubble(true);
370 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
371 if (mBubbles.containsKey(entry.key)) {
372 mBubbles.get(entry.key).updateDotVisibility();
373 }
374 updateBubble(entry, true /* updatePosition */);
375 }
376 }
377
378 @Override
379 public void onEntryRemoved(NotificationEntry entry,
380 @Nullable NotificationVisibility visibility,
381 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800382 if (!areBubblesEnabled(mContext)) {
383 return;
384 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800385 entry.setShowInShadeWhenBubble(false);
386 if (mBubbles.containsKey(entry.key)) {
387 mBubbles.get(entry.key).updateDotVisibility();
388 }
389 if (!removedByUser) {
390 // This was a cancel so we should remove the bubble
391 removeBubble(entry.key);
Ned Burns01e38212019-01-03 16:32:52 -0500392 }
393 }
394 };
395
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800396 /**
397 * Lets any listeners know if bubble state has changed.
398 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800399 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800400 if (mStackView == null) {
401 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800402 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800403
Mady Mellord1c78b262018-11-06 18:04:40 -0800404 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800405 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800406 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800407 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
408 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
409 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800410 }
411
412 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800413 * Updates the visibility of the bubbles based on current state.
414 * Does not un-bubble, just hides or un-hides. Will notify any
415 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800416 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800417 public void updateVisibility() {
418 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
419 // Bubbles only appear in unlocked shade
420 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
421 } else if (mStackView != null) {
422 mStackView.setVisibility(INVISIBLE);
423 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800424 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800425 updateBubblesShowing();
426 }
427
428 /**
429 * Rect indicating the touchable region for the bubble stack / expanded stack.
430 */
431 public Rect getTouchableRegion() {
432 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
433 return null;
434 }
435 mStackView.getBoundsOnScreen(mTempRect);
436 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800437 }
438
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800439 @VisibleForTesting
440 BubbleStackView getStackView() {
441 return mStackView;
442 }
443
444 @Nullable
445 private PendingIntent getValidBubbleIntent(NotificationEntry notif) {
446 Notification notification = notif.notification.getNotification();
Steven Wu552f63f2019-02-05 13:41:36 -0500447 String packageName = notif.notification.getPackageName();
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800448 Notification.BubbleMetadata data = notif.getBubbleMetadata();
Steven Wu552f63f2019-02-05 13:41:36 -0500449 if (data != null && canLaunchInActivityView(data.getIntent(),
450 true /* enable logging for bubbles */, packageName)) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800451 return data.getIntent();
Steven Wu552f63f2019-02-05 13:41:36 -0500452 }
453 if (shouldUseContentIntent(mContext)
454 && canLaunchInActivityView(notification.contentIntent,
455 false /* disable logging for notifications */, packageName)) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800456 Log.d(TAG, "[addBubble " + notif.key
457 + "]: No appOverlayIntent, using contentIntent.");
458 return notification.contentIntent;
459 }
460 Log.d(TAG, "[addBubble " + notif.key + "]: No supported intent for ActivityView.");
461 return null;
462 }
463
464 /**
465 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
Steven Wu552f63f2019-02-05 13:41:36 -0500466 *
467 * @param intent the pending intent of the bubble.
468 * @param enableLogging whether bubble developer error should be logged.
469 * @param packageName the notification package name for this bubble.
470 * @return
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800471 */
Steven Wu552f63f2019-02-05 13:41:36 -0500472 private boolean canLaunchInActivityView(PendingIntent intent, boolean enableLogging,
473 String packageName) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500474 if (intent == null) {
475 return false;
476 }
477 ActivityInfo info =
478 intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0);
Steven Wu552f63f2019-02-05 13:41:36 -0500479 if (info == null) {
480 if (enableLogging) {
481 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
482 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_MISSING);
483 }
484 return false;
485 }
486 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
487 if (enableLogging) {
488 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
489 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_NOT_RESIZABLE);
490 }
491 return false;
492 }
493 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
494 if (enableLogging) {
495 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
496 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__DOCUMENT_LAUNCH_NOT_ALWAYS);
497 }
498 return false;
499 }
500 return (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0;
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500501 }
502
Mady Mellor5549dd22018-11-06 18:07:34 -0800503 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800504 * Whether the notification has been developer configured to bubble and is allowed by the user.
505 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800506 @VisibleForTesting
507 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800508 StatusBarNotification n = entry.notification;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800509 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
510 && n.getNotification().getBubbleMetadata().getIntent() != null;
Julia Reynolds4509ce72019-01-31 13:12:43 -0500511 return hasOverlayIntent && entry.canBubble;
Mady Mellorb4991e62019-01-10 15:14:51 -0800512 }
513
514 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800515 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800516 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800517 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800518 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800519 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800520 return false;
521 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800522 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800523
524 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
525 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
526 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
527
Mady Mellor5549dd22018-11-06 18:07:34 -0800528 boolean hasRemoteInput = false;
529 if (n.getNotification().actions != null) {
530 for (Notification.Action action : n.getNotification().actions) {
531 if (action.getRemoteInputs() != null) {
532 hasRemoteInput = true;
533 break;
534 }
535 }
536 }
Mady Mellor711f9562018-12-05 14:53:46 -0800537 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
538 && n.isOngoing();
539 boolean isMusic = n.getNotification().hasMediaSession();
540 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800541
Mady Mellor5549dd22018-11-06 18:07:34 -0800542 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800543 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
544 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800545 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800546 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800547 || autoBubbleAll;
548 }
549
Mark Renoufcecc77b2019-01-30 16:32:24 -0500550 /**
551 * This task stack listener is responsible for responding to tasks moved to the front
552 * which are on the default (main) display. When this happens, expanded bubbles must be
553 * collapsed so the user may interact with the app which was just moved to the front.
554 * <p>
555 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
556 * these calls via a main thread Handler.
557 */
558 @MainThread
559 private class BubbleTaskStackListener extends TaskStackChangeListener {
560
561 @Nullable
562 private ActivityManager.StackInfo findStackInfo(int taskId) throws RemoteException {
563 final List<ActivityManager.StackInfo> stackInfoList =
564 mActivityTaskManager.getAllStackInfos();
565 // Iterate through stacks from top to bottom.
566 final int stackCount = stackInfoList.size();
567 for (int stackIndex = 0; stackIndex < stackCount; stackIndex++) {
568 final ActivityManager.StackInfo stackInfo = stackInfoList.get(stackIndex);
569 // Iterate through tasks from top to bottom.
570 for (int taskIndex = stackInfo.taskIds.length - 1; taskIndex >= 0; taskIndex--) {
571 if (stackInfo.taskIds[taskIndex] == taskId) {
572 return stackInfo;
573 }
574 }
575 }
576 return null;
577 }
578
579 @Override
580 public void onTaskMovedToFront(int taskId) {
581 ActivityManager.StackInfo stackInfo = null;
582 try {
583 stackInfo = findStackInfo(taskId);
584 } catch (RemoteException e) {
585 e.rethrowAsRuntimeException();
586 }
587 if (stackInfo != null && stackInfo.displayId == Display.DEFAULT_DISPLAY
588 && mStackView != null) {
589 mStackView.collapseStack();
590 }
591 }
592
593 /**
594 * This is a workaround for the case when the activity had to be created in a new task.
595 * Existing code in ActivityStackSupervisor checks the display where the activity
596 * ultimately ended up, displays an error message toast, and calls this method instead of
597 * onTaskMovedToFront.
598 */
599 // TODO(b/124058588): add requestedDisplayId to this callback, ignore unless matches
600 @Override
601 public void onActivityLaunchOnSecondaryDisplayFailed() {
602 if (mStackView != null) {
603 mStackView.collapseStack();
604 }
605 }
606 }
607
Mady Mellorceced172018-11-27 11:18:39 -0800608 private static boolean shouldAutoBubbleMessages(Context context) {
609 return Settings.Secure.getInt(context.getContentResolver(),
610 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
611 }
612
613 private static boolean shouldAutoBubbleOngoing(Context context) {
614 return Settings.Secure.getInt(context.getContentResolver(),
615 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
616 }
617
618 private static boolean shouldAutoBubbleAll(Context context) {
619 return Settings.Secure.getInt(context.getContentResolver(),
620 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800621 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500622
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500623 private static boolean shouldUseContentIntent(Context context) {
624 return Settings.Secure.getInt(context.getContentResolver(),
625 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
626 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800627
628 private static boolean areBubblesEnabled(Context context) {
629 return Settings.Secure.getInt(context.getContentResolver(),
630 ENABLE_BUBBLES, 1) != 0;
631 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800632}