blob: 957d772be730650a7d543249d64cba36f290fd31 [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;
21import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
22
23import static com.android.systemui.bubbles.BubbleMovementHelper.EDGE_OVERLAP;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080024import static com.android.systemui.statusbar.StatusBarState.SHADE;
25import static com.android.systemui.statusbar.notification.NotificationAlertingManager.alertAgain;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080026
Mady Mellorb4991e62019-01-10 15:14:51 -080027import android.annotation.Nullable;
28import android.app.INotificationManager;
Mady Mellor5549dd22018-11-06 18:07:34 -080029import android.app.Notification;
Mark Renouf89b1a4a2018-12-04 14:59:45 -050030import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080031import android.content.Context;
Mark Renouf89b1a4a2018-12-04 14:59:45 -050032import android.content.pm.ActivityInfo;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080033import android.graphics.Point;
Mady Mellord1c78b262018-11-06 18:04:40 -080034import android.graphics.Rect;
Mady Mellorb4991e62019-01-10 15:14:51 -080035import android.os.RemoteException;
36import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080037import android.provider.Settings;
Mady Mellor5549dd22018-11-06 18:07:34 -080038import android.service.notification.StatusBarNotification;
Mark Renouf89b1a4a2018-12-04 14:59:45 -050039import android.util.Log;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080040import android.view.LayoutInflater;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080041import android.view.ViewGroup;
42import android.view.WindowManager;
43import android.widget.FrameLayout;
44
Mady Mellorebdbbb92018-11-15 14:36:48 -080045import com.android.internal.annotations.VisibleForTesting;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080046import com.android.internal.statusbar.NotificationVisibility;
Ned Burns01e38212019-01-03 16:32:52 -050047import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080048import com.android.systemui.R;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080049import com.android.systemui.statusbar.StatusBarStateController;
Ned Burns01e38212019-01-03 16:32:52 -050050import com.android.systemui.statusbar.notification.NotificationEntryListener;
51import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080052import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050053import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080054import com.android.systemui.statusbar.notification.row.NotificationInflater;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080055import com.android.systemui.statusbar.phone.StatusBarWindowController;
56
57import java.util.HashMap;
58import java.util.Map;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080059import java.util.Set;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080060
Jason Monk27d01a622018-12-10 15:57:09 -050061import javax.inject.Inject;
62import javax.inject.Singleton;
63
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080064/**
65 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
66 * Bubbles can be expanded to show more content.
67 *
68 * The controller manages addition, removal, and visible state of bubbles on screen.
69 */
Jason Monk27d01a622018-12-10 15:57:09 -050070@Singleton
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080071public class BubbleController {
72 private static final int MAX_BUBBLES = 5; // TODO: actually enforce this
73
74 private static final String TAG = "BubbleController";
75
Mady Mellor5549dd22018-11-06 18:07:34 -080076 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -050077 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -080078
Mady Mellorceced172018-11-27 11:18:39 -080079 // Secure settings
80 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
81 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
82 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mark Renouf89b1a4a2018-12-04 14:59:45 -050083 private static final String ENABLE_BUBBLE_ACTIVITY_VIEW = "experiment_bubble_activity_view";
84 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -080085
Ned Burns01e38212019-01-03 16:32:52 -050086 private final Context mContext;
87 private final NotificationEntryManager mNotificationEntryManager;
Mady Mellord1c78b262018-11-06 18:04:40 -080088 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -080089 private BubbleExpandListener mExpandListener;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080090 private LayoutInflater mInflater;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080091
Ned Burns01e38212019-01-03 16:32:52 -050092 private final Map<String, BubbleView> mBubbles = new HashMap<>();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080093 private BubbleStackView mStackView;
Ned Burns01e38212019-01-03 16:32:52 -050094 private final Point mDisplaySize;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080095
96 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -050097 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080098 private StatusBarStateListener mStatusBarStateListener;
99
100 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider =
101 Dependency.get(NotificationInterruptionStateProvider.class);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800102
Mady Mellorb4991e62019-01-10 15:14:51 -0800103 private INotificationManager mNotificationManagerService;
104
Mady Mellord1c78b262018-11-06 18:04:40 -0800105 // Used for determining view rect for touch interaction
106 private Rect mTempRect = new Rect();
107
Mady Mellor5549dd22018-11-06 18:07:34 -0800108 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800109 * Listener to be notified when some states of the bubbles change.
110 */
111 public interface BubbleStateChangeListener {
112 /**
113 * Called when the stack has bubbles or no longer has bubbles.
114 */
115 void onHasBubblesChanged(boolean hasBubbles);
116 }
117
Mady Mellorcd9b1302018-11-06 18:08:04 -0800118 /**
119 * Listener to find out about stack expansion / collapse events.
120 */
121 public interface BubbleExpandListener {
122 /**
123 * Called when the expansion state of the bubble stack changes.
Mady Mellorcd9b1302018-11-06 18:08:04 -0800124 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800125 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800126 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800127 void onBubbleExpandChanged(boolean isExpanding, String key);
128 }
129
130 /**
131 * Listens for the current state of the status bar and updates the visibility state
132 * of bubbles as needed.
133 */
134 private class StatusBarStateListener implements StatusBarStateController.StateListener {
135 private int mState;
136 /**
137 * Returns the current status bar state.
138 */
139 public int getCurrentState() {
140 return mState;
141 }
142
143 @Override
144 public void onStateChanged(int newState) {
145 mState = newState;
146 updateVisibility();
147 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800148 }
149
Jason Monk27d01a622018-12-10 15:57:09 -0500150 @Inject
Jason Monk92d5c242018-12-21 14:37:34 -0500151 public BubbleController(Context context, StatusBarWindowController statusBarWindowController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800152 mContext = context;
153 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
154 mDisplaySize = new Point();
155 wm.getDefaultDisplay().getSize(mDisplaySize);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800156 mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800157
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800158 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500159 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800160
161 try {
162 mNotificationManagerService = INotificationManager.Stub.asInterface(
163 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
164 } catch (ServiceManager.ServiceNotFoundException e) {
165 e.printStackTrace();
166 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800167
168 mStatusBarWindowController = statusBarWindowController;
169 mStatusBarStateListener = new StatusBarStateListener();
170 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mady Mellor5549dd22018-11-06 18:07:34 -0800171 }
172
173 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800174 * Set a listener to be notified when some states of the bubbles change.
175 */
176 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
177 mStateChangeListener = listener;
178 }
179
180 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800181 * Set a listener to be notified of bubble expand events.
182 */
183 public void setExpandListener(BubbleExpandListener listener) {
184 mExpandListener = listener;
185 if (mStackView != null) {
186 mStackView.setExpandListener(mExpandListener);
187 }
188 }
189
190 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800191 * Whether or not there are bubbles present, regardless of them being visible on the
192 * screen (e.g. if on AOD).
193 */
194 public boolean hasBubbles() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800195 for (BubbleView bv : mBubbles.values()) {
196 if (!bv.getEntry().isBubbleDismissed()) {
197 return true;
198 }
199 }
200 return false;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800201 }
202
203 /**
204 * Whether the stack of bubbles is expanded or not.
205 */
206 public boolean isStackExpanded() {
207 return mStackView != null && mStackView.isExpanded();
208 }
209
210 /**
211 * Tell the stack of bubbles to collapse.
212 */
213 public void collapseStack() {
214 if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800215 mStackView.collapseStack();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800216 }
217 }
218
219 /**
220 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
221 */
Ned Burns01e38212019-01-03 16:32:52 -0500222 void dismissStack() {
Mady Mellord1c78b262018-11-06 18:04:40 -0800223 if (mStackView == null) {
224 return;
225 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800226 Set<String> keys = mBubbles.keySet();
227 for (String key: keys) {
228 mBubbles.get(key).getEntry().setBubbleDismissed(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800229 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800230 mStackView.stackDismissed();
231
232 // Reset the position of the stack (TODO - or should we save / respect last user position?)
233 Point startPoint = getStartPoint(mStackView.getStackWidth(), mDisplaySize);
234 mStackView.setPosition(startPoint.x, startPoint.y);
235
236 updateVisibility();
Ned Burns01e38212019-01-03 16:32:52 -0500237 mNotificationEntryManager.updateNotifications();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800238 }
239
240 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800241 * Adds or updates a bubble associated with the provided notification entry.
242 *
243 * @param notif the notification associated with this bubble.
244 * @param updatePosition whether this update should promote the bubble to the top of the stack.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800245 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800246 public void updateBubble(NotificationEntry notif, boolean updatePosition) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800247 if (mBubbles.containsKey(notif.key)) {
248 // It's an update
249 BubbleView bubble = mBubbles.get(notif.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800250 mStackView.updateBubble(bubble, notif, updatePosition);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800251 } else {
Mady Mellord1c78b262018-11-06 18:04:40 -0800252 boolean setPosition = mStackView != null && mStackView.getVisibility() != VISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800253 if (mStackView == null) {
254 setPosition = true;
255 mStackView = new BubbleStackView(mContext);
Mady Mellord1c78b262018-11-06 18:04:40 -0800256 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800257 // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim
258 // between bubble and the shade
259 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
260 sbv.addView(mStackView, bubblePosition,
261 new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
Mady Mellorcd9b1302018-11-06 18:08:04 -0800262 if (mExpandListener != null) {
263 mStackView.setExpandListener(mExpandListener);
264 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800265 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800266 // It's new
267 BubbleView bubble = (BubbleView) mInflater.inflate(
268 R.layout.bubble_view, mStackView, false /* attachToRoot */);
269 bubble.setNotif(notif);
270 if (shouldUseActivityView(mContext)) {
271 bubble.setAppOverlayIntent(getAppOverlayIntent(notif));
272 }
273 mBubbles.put(bubble.getKey(), bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800274 mStackView.addBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800275 if (setPosition) {
276 // Need to add the bubble to the stack before we can know the width
277 Point startPoint = getStartPoint(mStackView.getStackWidth(), mDisplaySize);
278 mStackView.setPosition(startPoint.x, startPoint.y);
279 }
280 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800281 updateVisibility();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800282 }
283
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500284 @Nullable
285 private PendingIntent getAppOverlayIntent(NotificationEntry notif) {
286 Notification notification = notif.notification.getNotification();
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800287 if (canLaunchInActivityView(notification.getBubbleMetadata() != null
288 ? notification.getBubbleMetadata().getIntent() : null)) {
289 return notification.getBubbleMetadata().getIntent();
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500290 } else if (shouldUseContentIntent(mContext)
291 && canLaunchInActivityView(notification.contentIntent)) {
292 Log.d(TAG, "[addBubble " + notif.key
293 + "]: No appOverlayIntent, using contentIntent.");
294 return notification.contentIntent;
295 }
296 Log.d(TAG, "[addBubble " + notif.key + "]: No supported intent for ActivityView.");
297 return null;
298 }
299
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800300 /**
301 * Removes the bubble associated with the {@param uri}.
302 */
Ned Burns01e38212019-01-03 16:32:52 -0500303 void removeBubble(String key) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800304 BubbleView bv = mBubbles.remove(key);
Mady Mellord1c78b262018-11-06 18:04:40 -0800305 if (mStackView != null && bv != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800306 mStackView.removeBubble(bv);
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500307 bv.destroyActivityView(mStackView);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800308 }
Ned Burns01e38212019-01-03 16:32:52 -0500309
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800310 NotificationEntry entry = bv != null ? bv.getEntry() : null;
Ned Burns01e38212019-01-03 16:32:52 -0500311 if (entry != null) {
312 entry.setBubbleDismissed(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800313 mNotificationEntryManager.updateNotifications();
Mady Mellor5549dd22018-11-06 18:07:34 -0800314 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800315 updateVisibility();
Mady Mellord1c78b262018-11-06 18:04:40 -0800316 }
317
Ned Burns01e38212019-01-03 16:32:52 -0500318 @SuppressWarnings("FieldCanBeLocal")
319 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
320 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500321 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800322 if (shouldAutoBubble(mContext, entry) || shouldBubble(entry)) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800323 // TODO: handle group summaries
324 // It's a new notif, it shows in the shade and as a bubble
Ned Burns01e38212019-01-03 16:32:52 -0500325 entry.setIsBubble(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800326 entry.setShowInShadeWhenBubble(true);
327 }
328 }
329
330 @Override
331 public void onEntryInflated(NotificationEntry entry,
332 @NotificationInflater.InflationFlag int inflatedFlags) {
333 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
334 updateBubble(entry, true /* updatePosition */);
335 }
336 }
337
338 @Override
339 public void onPreEntryUpdated(NotificationEntry entry) {
340 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
341 && alertAgain(entry, entry.notification.getNotification())) {
342 entry.setShowInShadeWhenBubble(true);
343 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
344 if (mBubbles.containsKey(entry.key)) {
345 mBubbles.get(entry.key).updateDotVisibility();
346 }
347 updateBubble(entry, true /* updatePosition */);
348 }
349 }
350
351 @Override
352 public void onEntryRemoved(NotificationEntry entry,
353 @Nullable NotificationVisibility visibility,
354 boolean removedByUser) {
355 entry.setShowInShadeWhenBubble(false);
356 if (mBubbles.containsKey(entry.key)) {
357 mBubbles.get(entry.key).updateDotVisibility();
358 }
359 if (!removedByUser) {
360 // This was a cancel so we should remove the bubble
361 removeBubble(entry.key);
Ned Burns01e38212019-01-03 16:32:52 -0500362 }
363 }
364 };
365
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800366 /**
367 * Lets any listeners know if bubble state has changed.
368 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800369 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800370 if (mStackView == null) {
371 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800372 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800373
Mady Mellord1c78b262018-11-06 18:04:40 -0800374 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800375 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800376 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800377 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
378 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
379 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800380 }
381
382 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800383 * Updates the visibility of the bubbles based on current state.
384 * Does not un-bubble, just hides or un-hides. Will notify any
385 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800386 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800387 public void updateVisibility() {
388 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
389 // Bubbles only appear in unlocked shade
390 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
391 } else if (mStackView != null) {
392 mStackView.setVisibility(INVISIBLE);
393 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800394 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800395 updateBubblesShowing();
396 }
397
398 /**
399 * Rect indicating the touchable region for the bubble stack / expanded stack.
400 */
401 public Rect getTouchableRegion() {
402 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
403 return null;
404 }
405 mStackView.getBoundsOnScreen(mTempRect);
406 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800407 }
408
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500409 private boolean canLaunchInActivityView(PendingIntent intent) {
410 if (intent == null) {
411 return false;
412 }
413 ActivityInfo info =
414 intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0);
415 return info != null
416 && ActivityInfo.isResizeableMode(info.resizeMode)
417 && (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0;
418 }
419
Mady Mellorebdbbb92018-11-15 14:36:48 -0800420 @VisibleForTesting
Ned Burns01e38212019-01-03 16:32:52 -0500421 BubbleStackView getStackView() {
Mady Mellorebdbbb92018-11-15 14:36:48 -0800422 return mStackView;
423 }
424
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800425 // TODO: factor in PIP location / maybe last place user had it
426 /**
427 * Gets an appropriate starting point to position the bubble stack.
428 */
Ned Burns01e38212019-01-03 16:32:52 -0500429 private static Point getStartPoint(int size, Point displaySize) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800430 final int x = displaySize.x - size + EDGE_OVERLAP;
431 final int y = displaySize.y / 4;
432 return new Point(x, y);
433 }
434
435 /**
436 * Gets an appropriate position for the bubble when the stack is expanded.
437 */
Ned Burns01e38212019-01-03 16:32:52 -0500438 static Point getExpandPoint(BubbleStackView view, int size, Point displaySize) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800439 // Same place for now..
440 return new Point(EDGE_OVERLAP, size);
441 }
442
Mady Mellor5549dd22018-11-06 18:07:34 -0800443 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800444 * Whether the notification has been developer configured to bubble and is allowed by the user.
445 */
446 private boolean shouldBubble(NotificationEntry entry) {
447 StatusBarNotification n = entry.notification;
448 boolean canAppOverlay = false;
449 try {
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800450 canAppOverlay = mNotificationManagerService.areBubblesAllowedForPackage(
Mady Mellorb4991e62019-01-10 15:14:51 -0800451 n.getPackageName(), n.getUid());
452 } catch (RemoteException e) {
453 Log.w(TAG, "Error calling NoMan to determine if app can overlay", e);
454 }
455
456 boolean canChannelOverlay = mNotificationEntryManager.getNotificationData().getChannel(
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800457 entry.key).canBubble();
458 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
459 && n.getNotification().getBubbleMetadata().getIntent() != null;
Mady Mellorb4991e62019-01-10 15:14:51 -0800460 return hasOverlayIntent && canChannelOverlay && canAppOverlay;
461 }
462
463 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800464 * Whether the notification should bubble or not. Gated by debug flag.
465 * <p>
466 * If a notification has been set to bubble via proper bubble APIs or if it is an important
467 * message-like notification.
468 * </p>
Mady Mellor5549dd22018-11-06 18:07:34 -0800469 */
Mady Mellorb4991e62019-01-10 15:14:51 -0800470 private boolean shouldAutoBubble(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800471 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800472 return false;
473 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800474 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800475
476 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
477 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
478 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
479
Mady Mellor5549dd22018-11-06 18:07:34 -0800480 boolean hasRemoteInput = false;
481 if (n.getNotification().actions != null) {
482 for (Notification.Action action : n.getNotification().actions) {
483 if (action.getRemoteInputs() != null) {
484 hasRemoteInput = true;
485 break;
486 }
487 }
488 }
Mady Mellor711f9562018-12-05 14:53:46 -0800489 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
490 && n.isOngoing();
491 boolean isMusic = n.getNotification().hasMediaSession();
492 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800493
Mady Mellor5549dd22018-11-06 18:07:34 -0800494 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800495 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
496 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
497 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800498 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800499 || autoBubbleAll;
500 }
501
502 private static boolean shouldAutoBubbleMessages(Context context) {
503 return Settings.Secure.getInt(context.getContentResolver(),
504 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
505 }
506
507 private static boolean shouldAutoBubbleOngoing(Context context) {
508 return Settings.Secure.getInt(context.getContentResolver(),
509 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
510 }
511
512 private static boolean shouldAutoBubbleAll(Context context) {
513 return Settings.Secure.getInt(context.getContentResolver(),
514 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800515 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500516
517 private static boolean shouldUseActivityView(Context context) {
518 return Settings.Secure.getInt(context.getContentResolver(),
519 ENABLE_BUBBLE_ACTIVITY_VIEW, 0) != 0;
520 }
521
522 private static boolean shouldUseContentIntent(Context context) {
523 return Settings.Secure.getInt(context.getContentResolver(),
524 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
525 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800526}