blob: b741673f5ae7c299d30ea53a11725f1fab5a6b32 [file] [log] [blame]
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.bubbles;
18
Mady Mellord1c78b262018-11-06 18:04:40 -080019import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080020import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080021import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080022
Mady Mellor3f2efdb2018-11-21 11:30:45 -080023import static com.android.systemui.statusbar.StatusBarState.SHADE;
24import static com.android.systemui.statusbar.notification.NotificationAlertingManager.alertAgain;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080025
Mady Mellorb4991e62019-01-10 15:14:51 -080026import android.annotation.Nullable;
27import android.app.INotificationManager;
Mady Mellor5549dd22018-11-06 18:07:34 -080028import android.app.Notification;
Mady Mellorc18ba962019-01-29 11:11:56 -080029import android.app.NotificationChannel;
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 Mellorf6e3ac02019-01-29 10:37:52 -080079 // Secure settings flags
80 // Feature level flag
81 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
82 // Auto bubble flags set whether different notification types should be presented as a bubble
Mady Mellorceced172018-11-27 11:18:39 -080083 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
84 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
85 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellorf6e3ac02019-01-29 10:37:52 -080086 // Use an activity view for an auto-bubbled notification if it has an appropriate content intent
Mark Renouf89b1a4a2018-12-04 14:59:45 -050087 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -080088
Ned Burns01e38212019-01-03 16:32:52 -050089 private final Context mContext;
90 private final NotificationEntryManager mNotificationEntryManager;
Mady Mellord1c78b262018-11-06 18:04:40 -080091 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -080092 private BubbleExpandListener mExpandListener;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080093 private LayoutInflater mInflater;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080094
Ned Burns01e38212019-01-03 16:32:52 -050095 private final Map<String, BubbleView> mBubbles = new HashMap<>();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080096 private BubbleStackView mStackView;
Ned Burns01e38212019-01-03 16:32:52 -050097 private final Point mDisplaySize;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080098
99 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500100 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800101 private StatusBarStateListener mStatusBarStateListener;
102
103 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider =
104 Dependency.get(NotificationInterruptionStateProvider.class);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800105
Mady Mellorb4991e62019-01-10 15:14:51 -0800106 private INotificationManager mNotificationManagerService;
107
Mady Mellord1c78b262018-11-06 18:04:40 -0800108 // Used for determining view rect for touch interaction
109 private Rect mTempRect = new Rect();
110
Mady Mellor5549dd22018-11-06 18:07:34 -0800111 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800112 * Listener to be notified when some states of the bubbles change.
113 */
114 public interface BubbleStateChangeListener {
115 /**
116 * Called when the stack has bubbles or no longer has bubbles.
117 */
118 void onHasBubblesChanged(boolean hasBubbles);
119 }
120
Mady Mellorcd9b1302018-11-06 18:08:04 -0800121 /**
122 * Listener to find out about stack expansion / collapse events.
123 */
124 public interface BubbleExpandListener {
125 /**
126 * Called when the expansion state of the bubble stack changes.
Mady Mellorcd9b1302018-11-06 18:08:04 -0800127 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800128 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800129 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800130 void onBubbleExpandChanged(boolean isExpanding, String key);
131 }
132
133 /**
134 * Listens for the current state of the status bar and updates the visibility state
135 * of bubbles as needed.
136 */
137 private class StatusBarStateListener implements StatusBarStateController.StateListener {
138 private int mState;
139 /**
140 * Returns the current status bar state.
141 */
142 public int getCurrentState() {
143 return mState;
144 }
145
146 @Override
147 public void onStateChanged(int newState) {
148 mState = newState;
149 updateVisibility();
150 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800151 }
152
Jason Monk27d01a622018-12-10 15:57:09 -0500153 @Inject
Jason Monk92d5c242018-12-21 14:37:34 -0500154 public BubbleController(Context context, StatusBarWindowController statusBarWindowController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800155 mContext = context;
156 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
157 mDisplaySize = new Point();
158 wm.getDefaultDisplay().getSize(mDisplaySize);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800159 mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800160
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800161 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500162 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800163
164 try {
165 mNotificationManagerService = INotificationManager.Stub.asInterface(
166 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
167 } catch (ServiceManager.ServiceNotFoundException e) {
168 e.printStackTrace();
169 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800170
171 mStatusBarWindowController = statusBarWindowController;
172 mStatusBarStateListener = new StatusBarStateListener();
173 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mady Mellor5549dd22018-11-06 18:07:34 -0800174 }
175
176 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800177 * Set a listener to be notified when some states of the bubbles change.
178 */
179 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
180 mStateChangeListener = listener;
181 }
182
183 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800184 * Set a listener to be notified of bubble expand events.
185 */
186 public void setExpandListener(BubbleExpandListener listener) {
187 mExpandListener = listener;
188 if (mStackView != null) {
189 mStackView.setExpandListener(mExpandListener);
190 }
191 }
192
193 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800194 * Whether or not there are bubbles present, regardless of them being visible on the
195 * screen (e.g. if on AOD).
196 */
197 public boolean hasBubbles() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800198 for (BubbleView bv : mBubbles.values()) {
199 if (!bv.getEntry().isBubbleDismissed()) {
200 return true;
201 }
202 }
203 return false;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800204 }
205
206 /**
207 * Whether the stack of bubbles is expanded or not.
208 */
209 public boolean isStackExpanded() {
210 return mStackView != null && mStackView.isExpanded();
211 }
212
213 /**
214 * Tell the stack of bubbles to collapse.
215 */
216 public void collapseStack() {
217 if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800218 mStackView.collapseStack();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800219 }
220 }
221
222 /**
223 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
224 */
Ned Burns01e38212019-01-03 16:32:52 -0500225 void dismissStack() {
Mady Mellord1c78b262018-11-06 18:04:40 -0800226 if (mStackView == null) {
227 return;
228 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800229 Set<String> keys = mBubbles.keySet();
230 for (String key: keys) {
231 mBubbles.get(key).getEntry().setBubbleDismissed(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800232 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800233 mStackView.stackDismissed();
234
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800235 updateVisibility();
Ned Burns01e38212019-01-03 16:32:52 -0500236 mNotificationEntryManager.updateNotifications();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800237 }
238
239 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800240 * Adds or updates a bubble associated with the provided notification entry.
241 *
242 * @param notif the notification associated with this bubble.
243 * @param updatePosition whether this update should promote the bubble to the top of the stack.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800244 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800245 public void updateBubble(NotificationEntry notif, boolean updatePosition) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800246 if (mBubbles.containsKey(notif.key)) {
247 // It's an update
248 BubbleView bubble = mBubbles.get(notif.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800249 mStackView.updateBubble(bubble, notif, updatePosition);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800250 } else {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800251 if (mStackView == null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800252 mStackView = new BubbleStackView(mContext);
Mady Mellord1c78b262018-11-06 18:04:40 -0800253 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800254 // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim
255 // between bubble and the shade
256 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
257 sbv.addView(mStackView, bubblePosition,
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800258 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
Mady Mellorcd9b1302018-11-06 18:08:04 -0800259 if (mExpandListener != null) {
260 mStackView.setExpandListener(mExpandListener);
261 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800262 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800263 // It's new
264 BubbleView bubble = (BubbleView) mInflater.inflate(
265 R.layout.bubble_view, mStackView, false /* attachToRoot */);
266 bubble.setNotif(notif);
Mady Melloredd4ee12019-01-18 10:45:11 -0800267 PendingIntent bubbleIntent = getValidBubbleIntent(notif);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800268 if (bubbleIntent != null) {
269 bubble.setBubbleIntent(bubbleIntent);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800270 }
271 mBubbles.put(bubble.getKey(), bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800272 mStackView.addBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800273 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800274 updateVisibility();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800275 }
276
277 /**
278 * Removes the bubble associated with the {@param uri}.
279 */
Ned Burns01e38212019-01-03 16:32:52 -0500280 void removeBubble(String key) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800281 BubbleView bv = mBubbles.remove(key);
Mady Mellord1c78b262018-11-06 18:04:40 -0800282 if (mStackView != null && bv != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800283 mStackView.removeBubble(bv);
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500284 bv.destroyActivityView(mStackView);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800285 }
Ned Burns01e38212019-01-03 16:32:52 -0500286
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800287 NotificationEntry entry = bv != null ? bv.getEntry() : null;
Ned Burns01e38212019-01-03 16:32:52 -0500288 if (entry != null) {
289 entry.setBubbleDismissed(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800290 mNotificationEntryManager.updateNotifications();
Mady Mellor5549dd22018-11-06 18:07:34 -0800291 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800292 updateVisibility();
Mady Mellord1c78b262018-11-06 18:04:40 -0800293 }
294
Ned Burns01e38212019-01-03 16:32:52 -0500295 @SuppressWarnings("FieldCanBeLocal")
296 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
297 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500298 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800299 if (!areBubblesEnabled(mContext)) {
300 return;
301 }
302 if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800303 // TODO: handle group summaries
304 // It's a new notif, it shows in the shade and as a bubble
Ned Burns01e38212019-01-03 16:32:52 -0500305 entry.setIsBubble(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800306 entry.setShowInShadeWhenBubble(true);
307 }
308 }
309
310 @Override
311 public void onEntryInflated(NotificationEntry entry,
312 @NotificationInflater.InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800313 if (!areBubblesEnabled(mContext)) {
314 return;
315 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800316 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
317 updateBubble(entry, true /* updatePosition */);
318 }
319 }
320
321 @Override
322 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800323 if (!areBubblesEnabled(mContext)) {
324 return;
325 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800326 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
327 && alertAgain(entry, entry.notification.getNotification())) {
328 entry.setShowInShadeWhenBubble(true);
329 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
330 if (mBubbles.containsKey(entry.key)) {
331 mBubbles.get(entry.key).updateDotVisibility();
332 }
333 updateBubble(entry, true /* updatePosition */);
334 }
335 }
336
337 @Override
338 public void onEntryRemoved(NotificationEntry entry,
339 @Nullable NotificationVisibility visibility,
340 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800341 if (!areBubblesEnabled(mContext)) {
342 return;
343 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800344 entry.setShowInShadeWhenBubble(false);
345 if (mBubbles.containsKey(entry.key)) {
346 mBubbles.get(entry.key).updateDotVisibility();
347 }
348 if (!removedByUser) {
349 // This was a cancel so we should remove the bubble
350 removeBubble(entry.key);
Ned Burns01e38212019-01-03 16:32:52 -0500351 }
352 }
353 };
354
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800355 /**
356 * Lets any listeners know if bubble state has changed.
357 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800358 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800359 if (mStackView == null) {
360 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800361 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800362
Mady Mellord1c78b262018-11-06 18:04:40 -0800363 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800364 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800365 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800366 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
367 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
368 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800369 }
370
371 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800372 * Updates the visibility of the bubbles based on current state.
373 * Does not un-bubble, just hides or un-hides. Will notify any
374 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800375 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800376 public void updateVisibility() {
377 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
378 // Bubbles only appear in unlocked shade
379 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
380 } else if (mStackView != null) {
381 mStackView.setVisibility(INVISIBLE);
382 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800383 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800384 updateBubblesShowing();
385 }
386
387 /**
388 * Rect indicating the touchable region for the bubble stack / expanded stack.
389 */
390 public Rect getTouchableRegion() {
391 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
392 return null;
393 }
394 mStackView.getBoundsOnScreen(mTempRect);
395 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800396 }
397
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800398 @VisibleForTesting
399 BubbleStackView getStackView() {
400 return mStackView;
401 }
402
403 @Nullable
404 private PendingIntent getValidBubbleIntent(NotificationEntry notif) {
405 Notification notification = notif.notification.getNotification();
406 Notification.BubbleMetadata data = notif.getBubbleMetadata();
407 if (data != null && canLaunchInActivityView(data.getIntent())) {
408 return data.getIntent();
409 } else if (shouldUseContentIntent(mContext)
410 && canLaunchInActivityView(notification.contentIntent)) {
411 Log.d(TAG, "[addBubble " + notif.key
412 + "]: No appOverlayIntent, using contentIntent.");
413 return notification.contentIntent;
414 }
415 Log.d(TAG, "[addBubble " + notif.key + "]: No supported intent for ActivityView.");
416 return null;
417 }
418
419 /**
420 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
421 */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500422 private boolean canLaunchInActivityView(PendingIntent intent) {
423 if (intent == null) {
424 return false;
425 }
426 ActivityInfo info =
427 intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0);
428 return info != null
429 && ActivityInfo.isResizeableMode(info.resizeMode)
430 && (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0;
431 }
432
Mady Mellor5549dd22018-11-06 18:07:34 -0800433 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800434 * Whether the notification has been developer configured to bubble and is allowed by the user.
435 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800436 @VisibleForTesting
437 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800438 StatusBarNotification n = entry.notification;
439 boolean canAppOverlay = false;
440 try {
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800441 canAppOverlay = mNotificationManagerService.areBubblesAllowedForPackage(
Mady Mellorb4991e62019-01-10 15:14:51 -0800442 n.getPackageName(), n.getUid());
443 } catch (RemoteException e) {
444 Log.w(TAG, "Error calling NoMan to determine if app can overlay", e);
445 }
446
Mady Mellorc18ba962019-01-29 11:11:56 -0800447 NotificationChannel channel = mNotificationEntryManager.getNotificationData().getChannel(
448 entry.key);
449 boolean canChannelOverlay = channel != null && channel.canBubble();
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800450 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
451 && n.getNotification().getBubbleMetadata().getIntent() != null;
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800452 return hasOverlayIntent && canChannelOverlay && canAppOverlay;
Mady Mellorb4991e62019-01-10 15:14:51 -0800453 }
454
455 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800456 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800457 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800458 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800459 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800460 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800461 return false;
462 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800463 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800464
465 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
466 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
467 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
468
Mady Mellor5549dd22018-11-06 18:07:34 -0800469 boolean hasRemoteInput = false;
470 if (n.getNotification().actions != null) {
471 for (Notification.Action action : n.getNotification().actions) {
472 if (action.getRemoteInputs() != null) {
473 hasRemoteInput = true;
474 break;
475 }
476 }
477 }
Mady Mellor711f9562018-12-05 14:53:46 -0800478 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
479 && n.isOngoing();
480 boolean isMusic = n.getNotification().hasMediaSession();
481 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800482
Mady Mellor5549dd22018-11-06 18:07:34 -0800483 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800484 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
485 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800486 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800487 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800488 || autoBubbleAll;
489 }
490
491 private static boolean shouldAutoBubbleMessages(Context context) {
492 return Settings.Secure.getInt(context.getContentResolver(),
493 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
494 }
495
496 private static boolean shouldAutoBubbleOngoing(Context context) {
497 return Settings.Secure.getInt(context.getContentResolver(),
498 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
499 }
500
501 private static boolean shouldAutoBubbleAll(Context context) {
502 return Settings.Secure.getInt(context.getContentResolver(),
503 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800504 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500505
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500506 private static boolean shouldUseContentIntent(Context context) {
507 return Settings.Secure.getInt(context.getContentResolver(),
508 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
509 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800510
511 private static boolean areBubblesEnabled(Context context) {
512 return Settings.Secure.getInt(context.getContentResolver(),
513 ENABLE_BUBBLES, 1) != 0;
514 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800515}