blob: f36dca7fbaded4d646f2d99d23f0ded4d299e690 [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 Mellore8e07712019-01-23 12:45:33 -080073public class BubbleController implements BubbleExpandedViewContainer.OnBubbleBlockedListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080074 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 Mellore8e07712019-01-23 12:45:33 -0800269 mStackView.setOnBlockedListener(this);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800270 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800271 // It's new
272 BubbleView bubble = (BubbleView) mInflater.inflate(
273 R.layout.bubble_view, mStackView, false /* attachToRoot */);
274 bubble.setNotif(notif);
Mady Melloredd4ee12019-01-18 10:45:11 -0800275 PendingIntent bubbleIntent = getValidBubbleIntent(notif);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800276 if (bubbleIntent != null) {
277 bubble.setBubbleIntent(bubbleIntent);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800278 }
279 mBubbles.put(bubble.getKey(), bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800280 mStackView.addBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800281 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800282 updateVisibility();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800283 }
284
285 /**
286 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500287 * <p>
288 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800289 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500290 @MainThread
Ned Burns01e38212019-01-03 16:32:52 -0500291 void removeBubble(String key) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800292 BubbleView bv = mBubbles.remove(key);
Mady Mellord1c78b262018-11-06 18:04:40 -0800293 if (mStackView != null && bv != null) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800294 mStackView.removeBubble(bv);
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500295 bv.destroyActivityView(mStackView);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800296 }
Ned Burns01e38212019-01-03 16:32:52 -0500297
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800298 NotificationEntry entry = bv != null ? bv.getEntry() : null;
Ned Burns01e38212019-01-03 16:32:52 -0500299 if (entry != null) {
300 entry.setBubbleDismissed(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800301 mNotificationEntryManager.updateNotifications();
Mady Mellor5549dd22018-11-06 18:07:34 -0800302 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800303 updateVisibility();
Mady Mellord1c78b262018-11-06 18:04:40 -0800304 }
305
Mady Mellore8e07712019-01-23 12:45:33 -0800306 @Override
307 public void onBubbleBlocked(NotificationEntry entry) {
308 Object[] bubbles = mBubbles.values().toArray();
309 for (int i = 0; i < bubbles.length; i++) {
310 NotificationEntry e = ((BubbleView) bubbles[i]).getEntry();
311 boolean samePackage = entry.notification.getPackageName().equals(
312 e.notification.getPackageName());
313 if (samePackage) {
314 removeBubble(entry.key);
315 }
316 }
317 }
318
Ned Burns01e38212019-01-03 16:32:52 -0500319 @SuppressWarnings("FieldCanBeLocal")
320 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
321 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500322 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800323 if (!areBubblesEnabled(mContext)) {
324 return;
325 }
326 if (shouldAutoBubbleForFlags(mContext, entry) || shouldBubble(entry)) {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800327 // TODO: handle group summaries
328 // It's a new notif, it shows in the shade and as a bubble
Ned Burns01e38212019-01-03 16:32:52 -0500329 entry.setIsBubble(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800330 entry.setShowInShadeWhenBubble(true);
331 }
332 }
333
334 @Override
335 public void onEntryInflated(NotificationEntry entry,
336 @NotificationInflater.InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800337 if (!areBubblesEnabled(mContext)) {
338 return;
339 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800340 if (entry.isBubble() && mNotificationInterruptionStateProvider.shouldBubbleUp(entry)) {
341 updateBubble(entry, true /* updatePosition */);
342 }
343 }
344
345 @Override
346 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800347 if (!areBubblesEnabled(mContext)) {
348 return;
349 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800350 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
351 && alertAgain(entry, entry.notification.getNotification())) {
352 entry.setShowInShadeWhenBubble(true);
353 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
354 if (mBubbles.containsKey(entry.key)) {
355 mBubbles.get(entry.key).updateDotVisibility();
356 }
357 updateBubble(entry, true /* updatePosition */);
358 }
359 }
360
361 @Override
362 public void onEntryRemoved(NotificationEntry entry,
363 @Nullable NotificationVisibility visibility,
364 boolean removedByUser) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800365 if (!areBubblesEnabled(mContext)) {
366 return;
367 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800368 entry.setShowInShadeWhenBubble(false);
369 if (mBubbles.containsKey(entry.key)) {
370 mBubbles.get(entry.key).updateDotVisibility();
371 }
372 if (!removedByUser) {
373 // This was a cancel so we should remove the bubble
374 removeBubble(entry.key);
Ned Burns01e38212019-01-03 16:32:52 -0500375 }
376 }
377 };
378
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800379 /**
380 * Lets any listeners know if bubble state has changed.
381 */
Mady Mellord1c78b262018-11-06 18:04:40 -0800382 private void updateBubblesShowing() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800383 if (mStackView == null) {
384 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800385 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800386
Mady Mellord1c78b262018-11-06 18:04:40 -0800387 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800388 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
Mady Mellord1c78b262018-11-06 18:04:40 -0800389 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
Mady Mellord1c78b262018-11-06 18:04:40 -0800390 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
391 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
392 }
Mady Mellor5549dd22018-11-06 18:07:34 -0800393 }
394
395 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800396 * Updates the visibility of the bubbles based on current state.
397 * Does not un-bubble, just hides or un-hides. Will notify any
398 * {@link BubbleStateChangeListener}s if visibility changes.
Mady Mellor5549dd22018-11-06 18:07:34 -0800399 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800400 public void updateVisibility() {
401 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
402 // Bubbles only appear in unlocked shade
403 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
404 } else if (mStackView != null) {
405 mStackView.setVisibility(INVISIBLE);
406 collapseStack();
Mady Mellor5549dd22018-11-06 18:07:34 -0800407 }
Mady Mellord1c78b262018-11-06 18:04:40 -0800408 updateBubblesShowing();
409 }
410
411 /**
412 * Rect indicating the touchable region for the bubble stack / expanded stack.
413 */
414 public Rect getTouchableRegion() {
415 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
416 return null;
417 }
418 mStackView.getBoundsOnScreen(mTempRect);
419 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800420 }
421
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800422 @VisibleForTesting
423 BubbleStackView getStackView() {
424 return mStackView;
425 }
426
427 @Nullable
428 private PendingIntent getValidBubbleIntent(NotificationEntry notif) {
429 Notification notification = notif.notification.getNotification();
430 Notification.BubbleMetadata data = notif.getBubbleMetadata();
431 if (data != null && canLaunchInActivityView(data.getIntent())) {
432 return data.getIntent();
433 } else if (shouldUseContentIntent(mContext)
434 && canLaunchInActivityView(notification.contentIntent)) {
435 Log.d(TAG, "[addBubble " + notif.key
436 + "]: No appOverlayIntent, using contentIntent.");
437 return notification.contentIntent;
438 }
439 Log.d(TAG, "[addBubble " + notif.key + "]: No supported intent for ActivityView.");
440 return null;
441 }
442
443 /**
444 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
445 */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500446 private boolean canLaunchInActivityView(PendingIntent intent) {
447 if (intent == null) {
448 return false;
449 }
450 ActivityInfo info =
451 intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0);
452 return info != null
453 && ActivityInfo.isResizeableMode(info.resizeMode)
454 && (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0;
455 }
456
Mady Mellor5549dd22018-11-06 18:07:34 -0800457 /**
Mady Mellorb4991e62019-01-10 15:14:51 -0800458 * Whether the notification has been developer configured to bubble and is allowed by the user.
459 */
Mady Mellorc18ba962019-01-29 11:11:56 -0800460 @VisibleForTesting
461 protected boolean shouldBubble(NotificationEntry entry) {
Mady Mellorb4991e62019-01-10 15:14:51 -0800462 StatusBarNotification n = entry.notification;
463 boolean canAppOverlay = false;
464 try {
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800465 canAppOverlay = mNotificationManagerService.areBubblesAllowedForPackage(
Mady Mellorb4991e62019-01-10 15:14:51 -0800466 n.getPackageName(), n.getUid());
467 } catch (RemoteException e) {
468 Log.w(TAG, "Error calling NoMan to determine if app can overlay", e);
469 }
470
Mady Mellorc18ba962019-01-29 11:11:56 -0800471 NotificationChannel channel = mNotificationEntryManager.getNotificationData().getChannel(
472 entry.key);
473 boolean canChannelOverlay = channel != null && channel.canBubble();
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800474 boolean hasOverlayIntent = n.getNotification().getBubbleMetadata() != null
475 && n.getNotification().getBubbleMetadata().getIntent() != null;
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800476 return hasOverlayIntent && canChannelOverlay && canAppOverlay;
Mady Mellorb4991e62019-01-10 15:14:51 -0800477 }
478
479 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800480 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800481 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800482 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800483 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800484 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800485 return false;
486 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800487 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800488
489 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
490 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
491 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
492
Mady Mellor5549dd22018-11-06 18:07:34 -0800493 boolean hasRemoteInput = false;
494 if (n.getNotification().actions != null) {
495 for (Notification.Action action : n.getNotification().actions) {
496 if (action.getRemoteInputs() != null) {
497 hasRemoteInput = true;
498 break;
499 }
500 }
501 }
Mady Mellor711f9562018-12-05 14:53:46 -0800502 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
503 && n.isOngoing();
504 boolean isMusic = n.getNotification().hasMediaSession();
505 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800506
Mady Mellor5549dd22018-11-06 18:07:34 -0800507 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800508 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
509 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800510 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800511 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800512 || autoBubbleAll;
513 }
514
515 private static boolean shouldAutoBubbleMessages(Context context) {
516 return Settings.Secure.getInt(context.getContentResolver(),
517 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
518 }
519
520 private static boolean shouldAutoBubbleOngoing(Context context) {
521 return Settings.Secure.getInt(context.getContentResolver(),
522 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
523 }
524
525 private static boolean shouldAutoBubbleAll(Context context) {
526 return Settings.Secure.getInt(context.getContentResolver(),
527 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800528 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500529
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500530 private static boolean shouldUseContentIntent(Context context) {
531 return Settings.Secure.getInt(context.getContentResolver(),
532 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
533 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800534
535 private static boolean areBubblesEnabled(Context context) {
536 return Settings.Secure.getInt(context.getContentResolver(),
537 ENABLE_BUBBLES, 1) != 0;
538 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800539}