blob: 9a9a52f4022703d1fb9b06157131b11a52916beb [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
Mark Renouf658c6bc2019-01-30 10:26:54 -050045import androidx.annotation.MainThread;
46
Mady Mellorebdbbb92018-11-15 14:36:48 -080047import com.android.internal.annotations.VisibleForTesting;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080048import com.android.internal.statusbar.NotificationVisibility;
Ned Burns01e38212019-01-03 16:32:52 -050049import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080050import com.android.systemui.R;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080051import com.android.systemui.statusbar.StatusBarStateController;
Ned Burns01e38212019-01-03 16:32:52 -050052import com.android.systemui.statusbar.notification.NotificationEntryListener;
53import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080054import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050055import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080056import com.android.systemui.statusbar.notification.row.NotificationInflater;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080057import com.android.systemui.statusbar.phone.StatusBarWindowController;
58
59import java.util.HashMap;
60import java.util.Map;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080061import java.util.Set;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080062
Jason Monk27d01a622018-12-10 15:57:09 -050063import javax.inject.Inject;
64import javax.inject.Singleton;
65
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080066/**
67 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
68 * Bubbles can be expanded to show more content.
69 *
70 * The controller manages addition, removal, and visible state of bubbles on screen.
71 */
Jason Monk27d01a622018-12-10 15:57:09 -050072@Singleton
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080073public class BubbleController {
74 private static final int MAX_BUBBLES = 5; // TODO: actually enforce this
75
76 private static final String TAG = "BubbleController";
77
Mady Mellor5549dd22018-11-06 18:07:34 -080078 // Enables some subset of notifs to automatically become bubbles
Ned Burns01e38212019-01-03 16:32:52 -050079 private static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -080080
Mady Mellorf6e3ac02019-01-29 10:37:52 -080081 // Secure settings flags
82 // Feature level flag
83 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
84 // Auto bubble flags set whether different notification types should be presented as a bubble
Mady Mellorceced172018-11-27 11:18:39 -080085 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
86 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
87 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellorf6e3ac02019-01-29 10:37:52 -080088 // Use an activity view for an auto-bubbled notification if it has an appropriate content intent
Mark Renouf89b1a4a2018-12-04 14:59:45 -050089 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -080090
Ned Burns01e38212019-01-03 16:32:52 -050091 private final Context mContext;
92 private final NotificationEntryManager mNotificationEntryManager;
Mady Mellord1c78b262018-11-06 18:04:40 -080093 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -080094 private BubbleExpandListener mExpandListener;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080095 private LayoutInflater mInflater;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080096
Ned Burns01e38212019-01-03 16:32:52 -050097 private final Map<String, BubbleView> mBubbles = new HashMap<>();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080098 private BubbleStackView mStackView;
Ned Burns01e38212019-01-03 16:32:52 -050099 private final Point mDisplaySize;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800100
101 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500102 private final StatusBarWindowController mStatusBarWindowController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800103 private StatusBarStateListener mStatusBarStateListener;
104
105 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider =
106 Dependency.get(NotificationInterruptionStateProvider.class);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800107
Mady Mellorb4991e62019-01-10 15:14:51 -0800108 private INotificationManager mNotificationManagerService;
109
Mady Mellord1c78b262018-11-06 18:04:40 -0800110 // Used for determining view rect for touch interaction
111 private Rect mTempRect = new Rect();
112
Mady Mellor5549dd22018-11-06 18:07:34 -0800113 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800114 * Listener to be notified when some states of the bubbles change.
115 */
116 public interface BubbleStateChangeListener {
117 /**
118 * Called when the stack has bubbles or no longer has bubbles.
119 */
120 void onHasBubblesChanged(boolean hasBubbles);
121 }
122
Mady Mellorcd9b1302018-11-06 18:08:04 -0800123 /**
124 * Listener to find out about stack expansion / collapse events.
125 */
126 public interface BubbleExpandListener {
127 /**
128 * Called when the expansion state of the bubble stack changes.
Mady Mellorcd9b1302018-11-06 18:08:04 -0800129 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800130 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800131 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800132 void onBubbleExpandChanged(boolean isExpanding, String key);
133 }
134
135 /**
136 * Listens for the current state of the status bar and updates the visibility state
137 * of bubbles as needed.
138 */
139 private class StatusBarStateListener implements StatusBarStateController.StateListener {
140 private int mState;
141 /**
142 * Returns the current status bar state.
143 */
144 public int getCurrentState() {
145 return mState;
146 }
147
148 @Override
149 public void onStateChanged(int newState) {
150 mState = newState;
151 updateVisibility();
152 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800153 }
154
Jason Monk27d01a622018-12-10 15:57:09 -0500155 @Inject
Jason Monk92d5c242018-12-21 14:37:34 -0500156 public BubbleController(Context context, StatusBarWindowController statusBarWindowController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800157 mContext = context;
158 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
159 mDisplaySize = new Point();
160 wm.getDefaultDisplay().getSize(mDisplaySize);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800161 mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800162
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800163 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500164 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorb4991e62019-01-10 15:14:51 -0800165
166 try {
167 mNotificationManagerService = INotificationManager.Stub.asInterface(
168 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
169 } catch (ServiceManager.ServiceNotFoundException e) {
170 e.printStackTrace();
171 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800172
173 mStatusBarWindowController = statusBarWindowController;
174 mStatusBarStateListener = new StatusBarStateListener();
175 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mady Mellor5549dd22018-11-06 18:07:34 -0800176 }
177
178 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800179 * Set a listener to be notified when some states of the bubbles change.
180 */
181 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
182 mStateChangeListener = listener;
183 }
184
185 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800186 * Set a listener to be notified of bubble expand events.
187 */
188 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100189 mExpandListener = ((isExpanding, key) -> {
190 if (listener != null) {
191 listener.onBubbleExpandChanged(isExpanding, key);
192 }
193 mStatusBarWindowController.setBubbleExpanded(isExpanding);
194 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800195 if (mStackView != null) {
196 mStackView.setExpandListener(mExpandListener);
197 }
198 }
199
200 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800201 * Whether or not there are bubbles present, regardless of them being visible on the
202 * screen (e.g. if on AOD).
203 */
204 public boolean hasBubbles() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800205 for (BubbleView bv : mBubbles.values()) {
206 if (!bv.getEntry().isBubbleDismissed()) {
207 return true;
208 }
209 }
210 return false;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800211 }
212
213 /**
214 * Whether the stack of bubbles is expanded or not.
215 */
216 public boolean isStackExpanded() {
217 return mStackView != null && mStackView.isExpanded();
218 }
219
220 /**
221 * Tell the stack of bubbles to collapse.
222 */
223 public void collapseStack() {
224 if (mStackView != null) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800225 mStackView.collapseStack();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800226 }
227 }
228
229 /**
230 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
231 */
Ned Burns01e38212019-01-03 16:32:52 -0500232 void dismissStack() {
Mady Mellord1c78b262018-11-06 18:04:40 -0800233 if (mStackView == null) {
234 return;
235 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800236 Set<String> keys = mBubbles.keySet();
237 for (String key: keys) {
238 mBubbles.get(key).getEntry().setBubbleDismissed(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800239 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800240 mStackView.stackDismissed();
241
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800242 updateVisibility();
Ned Burns01e38212019-01-03 16:32:52 -0500243 mNotificationEntryManager.updateNotifications();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800244 }
245
246 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800247 * Adds or updates a bubble associated with the provided notification entry.
248 *
249 * @param notif the notification associated with this bubble.
250 * @param updatePosition whether this update should promote the bubble to the top of the stack.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800251 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800252 public void updateBubble(NotificationEntry notif, boolean updatePosition) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800253 if (mBubbles.containsKey(notif.key)) {
254 // It's an update
255 BubbleView bubble = mBubbles.get(notif.key);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800256 mStackView.updateBubble(bubble, notif, updatePosition);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800257 } else {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800258 if (mStackView == null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800259 mStackView = new BubbleStackView(mContext);
Mady Mellord1c78b262018-11-06 18:04:40 -0800260 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800261 // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim
262 // between bubble and the shade
263 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
264 sbv.addView(mStackView, bubblePosition,
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800265 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
Mady Mellorcd9b1302018-11-06 18:08:04 -0800266 if (mExpandListener != null) {
267 mStackView.setExpandListener(mExpandListener);
268 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800269 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800270 // It's new
271 BubbleView bubble = (BubbleView) mInflater.inflate(
272 R.layout.bubble_view, mStackView, false /* attachToRoot */);
273 bubble.setNotif(notif);
Mady Melloredd4ee12019-01-18 10:45:11 -0800274 PendingIntent bubbleIntent = getValidBubbleIntent(notif);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800275 if (bubbleIntent != null) {
276 bubble.setBubbleIntent(bubbleIntent);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800277 }
278 mBubbles.put(bubble.getKey(), bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800279 mStackView.addBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800280 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800281 updateVisibility();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800282 }
283
284 /**
285 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500286 * <p>
287 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800288 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500289 @MainThread
Ned Burns01e38212019-01-03 16:32:52 -0500290 void removeBubble(String key) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800291 BubbleView bv = mBubbles.remove(key);
Mady Mellord1c78b262018-11-06 18:04:40 -0800292 if (mStackView != null && bv != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800293 mStackView.removeBubble(bv);
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500294 bv.destroyActivityView(mStackView);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800295 }
Ned Burns01e38212019-01-03 16:32:52 -0500296
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800297 NotificationEntry entry = bv != null ? bv.getEntry() : null;
Ned Burns01e38212019-01-03 16:32:52 -0500298 if (entry != null) {
299 entry.setBubbleDismissed(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800300 mNotificationEntryManager.updateNotifications();
Mady Mellor5549dd22018-11-06 18:07:34 -0800301 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800302 updateVisibility();
Mady Mellord1c78b262018-11-06 18:04:40 -0800303 }
304
Ned Burns01e38212019-01-03 16:32:52 -0500305 @SuppressWarnings("FieldCanBeLocal")
306 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
307 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500308 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800309 if (!areBubblesEnabled(mContext)) {
310 return;
311 }
312 if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800313 // TODO: handle group summaries
314 // It's a new notif, it shows in the shade and as a bubble
Ned Burns01e38212019-01-03 16:32:52 -0500315 entry.setIsBubble(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800316 entry.setShowInShadeWhenBubble(true);
317 }
318 }
319
320 @Override
321 public void onEntryInflated(NotificationEntry entry,
322 @NotificationInflater.InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800323 if (!areBubblesEnabled(mContext)) {
324 return;
325 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800326 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
327 updateBubble(entry, true /* updatePosition */);
328 }
329 }
330
331 @Override
332 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800333 if (!areBubblesEnabled(mContext)) {
334 return;
335 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800336 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
337 && alertAgain(entry, entry.notification.getNotification())) {
338 entry.setShowInShadeWhenBubble(true);
339 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
340 if (mBubbles.containsKey(entry.key)) {
341 mBubbles.get(entry.key).updateDotVisibility();
342 }
343 updateBubble(entry, true /* updatePosition */);
344 }
345 }
346
347 @Override
348 public void onEntryRemoved(NotificationEntry entry,
349 @Nullable NotificationVisibility visibility,
350 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800351 if (!areBubblesEnabled(mContext)) {
352 return;
353 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800354 entry.setShowInShadeWhenBubble(false);
355 if (mBubbles.containsKey(entry.key)) {
356 mBubbles.get(entry.key).updateDotVisibility();
357 }
358 if (!removedByUser) {
359 // This was a cancel so we should remove the bubble
360 removeBubble(entry.key);
Ned Burns01e38212019-01-03 16:32:52 -0500361 }
362 }
363 };
364
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800365 /**
366 * Lets any listeners know if bubble state has changed.
367 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800368 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800369 if (mStackView == null) {
370 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800371 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800372
Mady Mellord1c78b262018-11-06 18:04:40 -0800373 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800374 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800375 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800376 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
377 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
378 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800379 }
380
381 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800382 * Updates the visibility of the bubbles based on current state.
383 * Does not un-bubble, just hides or un-hides. Will notify any
384 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800385 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800386 public void updateVisibility() {
387 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
388 // Bubbles only appear in unlocked shade
389 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
390 } else if (mStackView != null) {
391 mStackView.setVisibility(INVISIBLE);
392 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800393 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800394 updateBubblesShowing();
395 }
396
397 /**
398 * Rect indicating the touchable region for the bubble stack / expanded stack.
399 */
400 public Rect getTouchableRegion() {
401 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
402 return null;
403 }
404 mStackView.getBoundsOnScreen(mTempRect);
405 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800406 }
407
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800408 @VisibleForTesting
409 BubbleStackView getStackView() {
410 return mStackView;
411 }
412
413 @Nullable
414 private PendingIntent getValidBubbleIntent(NotificationEntry notif) {
415 Notification notification = notif.notification.getNotification();
416 Notification.BubbleMetadata data = notif.getBubbleMetadata();
417 if (data != null && canLaunchInActivityView(data.getIntent())) {
418 return data.getIntent();
419 } else if (shouldUseContentIntent(mContext)
420 && canLaunchInActivityView(notification.contentIntent)) {
421 Log.d(TAG, "[addBubble " + notif.key
422 + "]: No appOverlayIntent, using contentIntent.");
423 return notification.contentIntent;
424 }
425 Log.d(TAG, "[addBubble " + notif.key + "]: No supported intent for ActivityView.");
426 return null;
427 }
428
429 /**
430 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
431 */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500432 private boolean canLaunchInActivityView(PendingIntent intent) {
433 if (intent == null) {
434 return false;
435 }
436 ActivityInfo info =
437 intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0);
438 return info != null
439 && ActivityInfo.isResizeableMode(info.resizeMode)
440 && (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0;
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 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800446 @VisibleForTesting
447 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800448 StatusBarNotification n = entry.notification;
449 boolean canAppOverlay = false;
450 try {
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800451 canAppOverlay = mNotificationManagerService.areBubblesAllowedForPackage(
Mady Mellorb4991e62019-01-10 15:14:51 -0800452 n.getPackageName(), n.getUid());
453 } catch (RemoteException e) {
454 Log.w(TAG, "Error calling NoMan to determine if app can overlay", e);
455 }
456
Mady Mellorc18ba962019-01-29 11:11:56 -0800457 NotificationChannel channel = mNotificationEntryManager.getNotificationData().getChannel(
458 entry.key);
459 boolean canChannelOverlay = channel != null && channel.canBubble();
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800460 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
461 && n.getNotification().getBubbleMetadata().getIntent() != null;
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800462 return hasOverlayIntent && canChannelOverlay && canAppOverlay;
Mady Mellorb4991e62019-01-10 15:14:51 -0800463 }
464
465 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800466 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800467 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800468 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800469 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800470 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800471 return false;
472 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800473 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800474
475 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
476 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
477 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
478
Mady Mellor5549dd22018-11-06 18:07:34 -0800479 boolean hasRemoteInput = false;
480 if (n.getNotification().actions != null) {
481 for (Notification.Action action : n.getNotification().actions) {
482 if (action.getRemoteInputs() != null) {
483 hasRemoteInput = true;
484 break;
485 }
486 }
487 }
Mady Mellor711f9562018-12-05 14:53:46 -0800488 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
489 && n.isOngoing();
490 boolean isMusic = n.getNotification().hasMediaSession();
491 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800492
Mady Mellor5549dd22018-11-06 18:07:34 -0800493 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800494 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
495 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800496 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800497 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800498 || autoBubbleAll;
499 }
500
501 private static boolean shouldAutoBubbleMessages(Context context) {
502 return Settings.Secure.getInt(context.getContentResolver(),
503 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
504 }
505
506 private static boolean shouldAutoBubbleOngoing(Context context) {
507 return Settings.Secure.getInt(context.getContentResolver(),
508 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
509 }
510
511 private static boolean shouldAutoBubbleAll(Context context) {
512 return Settings.Secure.getInt(context.getContentResolver(),
513 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800514 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500515
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500516 private static boolean shouldUseContentIntent(Context context) {
517 return Settings.Secure.getInt(context.getContentResolver(),
518 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
519 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800520
521 private static boolean areBubblesEnabled(Context context) {
522 return Settings.Secure.getInt(context.getContentResolver(),
523 ENABLE_BUBBLES, 1) != 0;
524 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800525}