blob: 1fc5c39ff0fa07ff724b05ce95ce2a4151001664 [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 Mellor3a0a1b42019-05-23 06:40:21 -070019import static android.app.Notification.FLAG_BUBBLE;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040020import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE;
21import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST;
22import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
Mady Mellorca0c24c2019-05-16 16:14:32 -070023import static android.content.pm.ActivityInfo.DOCUMENT_LAUNCH_ALWAYS;
Mady Mellorc2ff0112019-03-28 14:18:06 -070024import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL;
25import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL;
26import static android.service.notification.NotificationListenerService.REASON_CANCEL;
27import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL;
Mady Mellor390bff42019-04-05 15:09:01 -070028import static android.view.Display.DEFAULT_DISPLAY;
29import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080030import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080031import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080032import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080033
Mady Mellor3f2efdb2018-11-21 11:30:45 -080034import static com.android.systemui.statusbar.StatusBarState.SHADE;
Mady Mellor1a4e86f2019-05-03 16:07:23 -070035import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080036
Mark Renoufba5ab512019-05-02 15:21:01 -040037import static java.lang.annotation.ElementType.FIELD;
38import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
39import static java.lang.annotation.ElementType.PARAMETER;
Mark Renouf08bc42a2019-03-07 13:01:59 -050040import static java.lang.annotation.RetentionPolicy.SOURCE;
41
Mady Mellore80930e2019-03-21 16:00:45 -070042import android.app.ActivityManager;
Mark Renoufc808f062019-02-07 15:20:37 -050043import android.app.ActivityManager.RunningTaskInfo;
Mady Mellor5549dd22018-11-06 18:07:34 -080044import android.app.Notification;
Mady Mellor66efd5e2019-05-15 13:38:11 -070045import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070046import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080047import android.content.Context;
Mady Mellorca0c24c2019-05-16 16:14:32 -070048import android.content.pm.ActivityInfo;
Joshua Tsujia19515f2019-02-13 18:02:29 -050049import android.content.pm.ParceledListSlice;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040050import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080051import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050052import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080053import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080054import android.provider.Settings;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040055import android.service.notification.NotificationListenerService.RankingMap;
Mady Mellor5549dd22018-11-06 18:07:34 -080056import android.service.notification.StatusBarNotification;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040057import android.service.notification.ZenModeConfig;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040058import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040059import android.util.Pair;
Mark Renoufcecc77b2019-01-30 16:32:24 -050060import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050061import android.view.IPinnedStackController;
62import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080063import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080064import android.widget.FrameLayout;
65
Mark Renouf08bc42a2019-03-07 13:01:59 -050066import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050067import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040068import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050069
Mady Mellorebdbbb92018-11-15 14:36:48 -080070import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070071import com.android.internal.statusbar.IStatusBarService;
Ned Burns01e38212019-01-03 16:32:52 -050072import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080073import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050074import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050075import com.android.systemui.shared.system.ActivityManagerWrapper;
76import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050077import com.android.systemui.shared.system.WindowManagerWrapper;
Mady Mellorc2ff0112019-03-28 14:18:06 -070078import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050079import com.android.systemui.statusbar.notification.NotificationEntryListener;
80import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080081import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050082import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050083import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080084import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070085import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040086import com.android.systemui.statusbar.policy.ZenModeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080087
Mark Renouf08bc42a2019-03-07 13:01:59 -050088import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -040089import java.lang.annotation.Target;
Mady Mellore80930e2019-03-21 16:00:45 -070090import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050091
Jason Monk27d01a622018-12-10 15:57:09 -050092import javax.inject.Inject;
93import javax.inject.Singleton;
94
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080095/**
96 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
97 * Bubbles can be expanded to show more content.
98 *
99 * The controller manages addition, removal, and visible state of bubbles on screen.
100 */
Jason Monk27d01a622018-12-10 15:57:09 -0500101@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -0400102public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800103
104 private static final String TAG = "BubbleController";
Mark Renoufba5ab512019-05-02 15:21:01 -0400105 private static final boolean DEBUG = false;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800106
Mark Renouf08bc42a2019-03-07 13:01:59 -0500107 @Retention(SOURCE)
108 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mady Melloraa8fef22019-04-11 13:36:40 -0700109 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE})
Mark Renoufba5ab512019-05-02 15:21:01 -0400110 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500111 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700112
Mark Renouf08bc42a2019-03-07 13:01:59 -0500113 static final int DISMISS_USER_GESTURE = 1;
114 static final int DISMISS_AGED = 2;
115 static final int DISMISS_TASK_FINISHED = 3;
116 static final int DISMISS_BLOCKED = 4;
117 static final int DISMISS_NOTIF_CANCEL = 5;
118 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700119 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500120
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400121 public static final int MAX_BUBBLES = 5; // TODO: actually enforce this
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -0400122
Mady Mellor5549dd22018-11-06 18:07:34 -0800123 // Enables some subset of notifs to automatically become bubbles
Mady Mellorca0c24c2019-05-16 16:14:32 -0700124 public static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -0800125
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800126 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800127 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800128 /** Auto bubble flags set whether different notif types should be presented as a bubble */
Mady Mellorceced172018-11-27 11:18:39 -0800129 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
130 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
131 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800132
133 /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500134 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -0800135
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500136 private static final String BUBBLE_STIFFNESS = "experiment_bubble_stiffness";
137 private static final String BUBBLE_BOUNCINESS = "experiment_bubble_bounciness";
138
Ned Burns01e38212019-01-03 16:32:52 -0500139 private final Context mContext;
140 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500141 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800142 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800143 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100144 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800145
Mady Mellor3dff9e62019-02-05 18:12:53 -0800146 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400147 @Nullable private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800148
149 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500150 private final StatusBarWindowController mStatusBarWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400151 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800152 private StatusBarStateListener mStatusBarStateListener;
153
Mady Melloraa8fef22019-04-11 13:36:40 -0700154 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700155 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800156
Mady Mellord1c78b262018-11-06 18:04:40 -0800157 // Used for determining view rect for touch interaction
158 private Rect mTempRect = new Rect();
159
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400160 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
161 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
162
Mady Mellor5549dd22018-11-06 18:07:34 -0800163 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800164 * Listener to be notified when some states of the bubbles change.
165 */
166 public interface BubbleStateChangeListener {
167 /**
168 * Called when the stack has bubbles or no longer has bubbles.
169 */
170 void onHasBubblesChanged(boolean hasBubbles);
171 }
172
Mady Mellorcd9b1302018-11-06 18:08:04 -0800173 /**
174 * Listener to find out about stack expansion / collapse events.
175 */
176 public interface BubbleExpandListener {
177 /**
178 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700179 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800180 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800181 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800182 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800183 void onBubbleExpandChanged(boolean isExpanding, String key);
184 }
185
186 /**
187 * Listens for the current state of the status bar and updates the visibility state
188 * of bubbles as needed.
189 */
190 private class StatusBarStateListener implements StatusBarStateController.StateListener {
191 private int mState;
192 /**
193 * Returns the current status bar state.
194 */
195 public int getCurrentState() {
196 return mState;
197 }
198
199 @Override
200 public void onStateChanged(int newState) {
201 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400202 boolean shouldCollapse = (mState != SHADE);
203 if (shouldCollapse) {
204 collapseStack();
205 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700206 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800207 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800208 }
209
Jason Monk27d01a622018-12-10 15:57:09 -0500210 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800211 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Mady Melloraa8fef22019-04-11 13:36:40 -0700212 BubbleData data, ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400213 NotificationInterruptionStateProvider interruptionStateProvider,
214 ZenModeController zenModeController) {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700215 this(context, statusBarWindowController, data, null /* synchronizer */,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400216 configurationController, interruptionStateProvider, zenModeController);
Issei Suzukic0387542019-03-08 17:31:14 +0100217 }
218
219 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700220 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
Mady Melloraa8fef22019-04-11 13:36:40 -0700221 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400222 NotificationInterruptionStateProvider interruptionStateProvider,
223 ZenModeController zenModeController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800224 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700225 mNotificationInterruptionStateProvider = interruptionStateProvider;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400226 mZenModeController = zenModeController;
227 mZenModeController.addCallback(new ZenModeController.Callback() {
228 @Override
229 public void onZenChanged(int zen) {
230 updateStackViewForZenConfig();
231 }
232
233 @Override
234 public void onConfigChanged(ZenModeConfig config) {
235 updateStackViewForZenConfig();
236 }
237 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700238
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700239 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800240
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400241 mBubbleData = data;
242 mBubbleData.setListener(mBubbleDataListener);
243
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800244 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500245 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700246 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellorb4991e62019-01-10 15:14:51 -0800247
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800248 mStatusBarWindowController = statusBarWindowController;
249 mStatusBarStateListener = new StatusBarStateListener();
250 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500251
Mark Renoufcecc77b2019-01-30 16:32:24 -0500252 mTaskStackListener = new BubbleTaskStackListener();
253 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800254
Joshua Tsujia19515f2019-02-13 18:02:29 -0500255 try {
256 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
257 } catch (RemoteException e) {
258 e.printStackTrace();
259 }
Issei Suzukic0387542019-03-08 17:31:14 +0100260 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700261
262 mBarService = IStatusBarService.Stub.asInterface(
263 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mady Mellor5549dd22018-11-06 18:07:34 -0800264 }
265
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400266 /**
267 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
268 * method initializes the stack view and adds it to the StatusBar just above the scrim.
269 */
270 private void ensureStackViewCreated() {
271 if (mStackView == null) {
272 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
273 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
Lyn Hanbde48202019-05-29 19:18:29 -0700274 int bubbleScrimIndex = sbv.indexOfChild(sbv.findViewById(R.id.scrim_for_bubble));
275 int stackIndex = bubbleScrimIndex + 1; // Show stack above bubble scrim.
276 sbv.addView(mStackView, stackIndex,
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400277 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
278 if (mExpandListener != null) {
279 mStackView.setExpandListener(mExpandListener);
280 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400281
282 updateStackViewForZenConfig();
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400283 }
284 }
285
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700286 @Override
287 public void onUiModeChanged() {
288 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700289 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700290 }
291 }
292
293 @Override
294 public void onOverlayChanged() {
295 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700296 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700297 }
298 }
299
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400300 @Override
301 public void onConfigChanged(Configuration newConfig) {
302 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
303 mStackView.onOrientationChanged();
304 mOrientation = newConfig.orientation;
305 }
306 }
307
Mady Mellor5549dd22018-11-06 18:07:34 -0800308 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800309 * Set a listener to be notified when some states of the bubbles change.
310 */
311 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
312 mStateChangeListener = listener;
313 }
314
315 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800316 * Set a listener to be notified of bubble expand events.
317 */
318 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100319 mExpandListener = ((isExpanding, key) -> {
320 if (listener != null) {
321 listener.onBubbleExpandChanged(isExpanding, key);
322 }
323 mStatusBarWindowController.setBubbleExpanded(isExpanding);
324 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800325 if (mStackView != null) {
326 mStackView.setExpandListener(mExpandListener);
327 }
328 }
329
330 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800331 * Whether or not there are bubbles present, regardless of them being visible on the
332 * screen (e.g. if on AOD).
333 */
334 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800335 if (mStackView == null) {
336 return false;
337 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400338 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800339 }
340
341 /**
342 * Whether the stack of bubbles is expanded or not.
343 */
344 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400345 return mBubbleData.isExpanded();
346 }
347
348 /**
349 * Tell the stack of bubbles to expand.
350 */
351 public void expandStack() {
352 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800353 }
354
355 /**
356 * Tell the stack of bubbles to collapse.
357 */
358 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400359 mBubbleData.setExpanded(false /* expanded */);
360 }
361
362 void selectBubble(Bubble bubble) {
363 mBubbleData.setSelectedBubble(bubble);
364 }
365
366 @VisibleForTesting
367 void selectBubble(String key) {
368 Bubble bubble = mBubbleData.getBubbleWithKey(key);
369 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800370 }
371
372 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400373 * Request the stack expand if needed, then select the specified Bubble as current.
374 *
375 * @param notificationKey the notification key for the bubble to be selected
376 */
377 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400378 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
379 if (bubble != null) {
380 mBubbleData.setSelectedBubble(bubble);
381 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400382 }
383 }
384
385 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800386 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
387 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500388 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400389 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800390 }
391
392 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500393 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
394 * is forwarded a back key down/up pair.
395 */
396 public void performBackPressIfNeeded() {
397 if (mStackView != null) {
398 mStackView.performBackPressIfNeeded();
399 }
400 }
401
402 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800403 * Adds or updates a bubble associated with the provided notification entry.
404 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400405 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800406 */
Mark Renouff97ed462019-04-05 13:46:24 -0400407 void updateBubble(NotificationEntry notif) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700408 // If this is an interruptive notif, mark that it's interrupted
409 if (notif.importance >= NotificationManager.IMPORTANCE_HIGH) {
410 notif.setInterruption();
411 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400412 mBubbleData.notificationEntryUpdated(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800413 }
414
415 /**
416 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500417 * <p>
418 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800419 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500420 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500421 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400422 // TEMP: refactor to change this to pass entry
423 Bubble bubble = mBubbleData.getBubbleWithKey(key);
424 if (bubble != null) {
425 mBubbleData.notificationEntryRemoved(bubble.entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800426 }
427 }
428
Ned Burns01e38212019-01-03 16:32:52 -0500429 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700430 private final NotificationRemoveInterceptor mRemoveInterceptor =
431 new NotificationRemoveInterceptor() {
432 @Override
433 public boolean onNotificationRemoveRequested(String key, int reason) {
434 if (!mBubbleData.hasBubbleWithKey(key)) {
435 return false;
436 }
437 NotificationEntry entry = mBubbleData.getBubbleWithKey(key).entry;
438
439 final boolean isClearAll = reason == REASON_CANCEL_ALL;
440 final boolean isUserDimiss = reason == REASON_CANCEL;
441 final boolean isAppCancel = reason == REASON_APP_CANCEL
442 || reason == REASON_APP_CANCEL_ALL;
443
444 // Need to check for !appCancel here because the notification may have
445 // previously been dismissed & entry.isRowDismissed would still be true
446 boolean userRemovedNotif = (entry.isRowDismissed() && !isAppCancel)
447 || isClearAll || isUserDimiss;
448
449 // The bubble notification sticks around in the data as long as the bubble is
450 // not dismissed and the app hasn't cancelled the notification.
451 boolean bubbleExtended = entry.isBubble() && !entry.isBubbleDismissed()
452 && userRemovedNotif;
453 if (bubbleExtended) {
454 entry.setShowInShadeWhenBubble(false);
455 if (mStackView != null) {
456 mStackView.updateDotVisibility(entry.key);
457 }
458 mNotificationEntryManager.updateNotifications();
459 return true;
460 } else if (!userRemovedNotif && !entry.isBubbleDismissed()) {
461 // This wasn't a user removal so we should remove the bubble as well
462 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
463 return false;
464 }
465 return false;
466 }
467 };
468
469 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500470 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
471 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500472 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800473 if (!areBubblesEnabled(mContext)) {
474 return;
475 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700476 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
477 && canLaunchInActivityView(mContext, entry)) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700478 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800479 }
480 }
481
482 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500483 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800484 if (!areBubblesEnabled(mContext)) {
485 return;
486 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700487 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
488 && canLaunchInActivityView(mContext, entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400489 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800490 }
491 }
492
493 @Override
494 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800495 if (!areBubblesEnabled(mContext)) {
496 return;
497 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700498 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
499 && canLaunchInActivityView(mContext, entry);
Mady Melloraa8fef22019-04-11 13:36:40 -0700500 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.key)) {
501 // It was previously a bubble but no longer a bubble -- lets remove it
502 removeBubble(entry.key, DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700503 } else if (shouldBubble) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700504 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800505 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mark Renouff97ed462019-04-05 13:46:24 -0400506 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800507 }
508 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400509
510 @Override
511 public void onNotificationRankingUpdated(RankingMap rankingMap) {
512 // Forward to BubbleData to block any bubbles which should no longer be shown
513 mBubbleData.notificationRankingUpdated(rankingMap);
514 }
Ned Burns01e38212019-01-03 16:32:52 -0500515 };
516
Mark Renouf71a3af62019-04-08 15:02:54 -0400517 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400518 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400519
Mark Renouf3bc5b362019-04-05 14:37:59 -0400520 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400521 public void applyUpdate(BubbleData.Update update) {
522 if (mStackView == null && update.addedBubble != null) {
523 // Lazy init stack view when the first bubble is added.
524 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400525 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400526
527 // If not yet initialized, ignore all other changes.
528 if (mStackView == null) {
529 return;
530 }
531
532 if (update.addedBubble != null) {
533 mStackView.addBubble(update.addedBubble);
534 }
535
536 // Collapsing? Do this first before remaining steps.
537 if (update.expandedChanged && !update.expanded) {
538 mStackView.setExpanded(false);
539 }
540
541 // Do removals, if any.
542 for (Pair<Bubble, Integer> removed : update.removedBubbles) {
543 final Bubble bubble = removed.first;
544 @DismissReason final int reason = removed.second;
545 mStackView.removeBubble(bubble);
546
547 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
548 && !bubble.entry.showInShadeWhenBubble()) {
549 // The bubble is gone & the notification is gone, time to actually remove it
550 mNotificationEntryManager.performRemoveNotification(bubble.entry.notification,
551 UNDEFINED_DISMISS_REASON);
552 } else {
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700553 // Update the flag for SysUI
554 bubble.entry.notification.getNotification().flags &= ~FLAG_BUBBLE;
555
556 // Make sure NoMan knows it's not a bubble anymore so anyone querying it will
557 // get right result back
Mark Renouf82a40e62019-05-23 16:16:24 -0400558 try {
559 mBarService.onNotificationBubbleChanged(bubble.getKey(),
560 false /* isBubble */);
561 } catch (RemoteException e) {
562 // Bad things have happened
563 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700564 }
565 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400566
Mark Renouf82a40e62019-05-23 16:16:24 -0400567 if (update.updatedBubble != null) {
568 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400569 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400570
Mark Renouf82a40e62019-05-23 16:16:24 -0400571 if (update.orderChanged) {
572 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400573 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400574
Mark Renouf82a40e62019-05-23 16:16:24 -0400575 if (update.selectionChanged) {
576 mStackView.setSelectedBubble(update.selectedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400577 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400578
Mark Renouf82a40e62019-05-23 16:16:24 -0400579 // Expanding? Apply this last.
580 if (update.expandedChanged && update.expanded) {
581 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400582 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400583
Mark Renouf71a3af62019-04-08 15:02:54 -0400584 mNotificationEntryManager.updateNotifications();
Lyn Han6c40fe72019-05-08 14:06:33 -0700585 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400586
587 if (DEBUG) {
588 Log.d(TAG, "[BubbleData]");
589 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
590 mBubbleData.getSelectedBubble()));
591
592 if (mStackView != null) {
593 Log.d(TAG, "[BubbleStackView]");
594 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
595 mStackView.getExpandedBubble()));
596 }
597 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400598 }
599 };
600
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800601 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400602 * Updates the stack view's suppression flags from the latest config from the zen (do not
603 * disturb) controller.
604 */
605 private void updateStackViewForZenConfig() {
606 final ZenModeConfig zenModeConfig = mZenModeController.getConfig();
607
608 if (zenModeConfig == null || mStackView == null) {
609 return;
610 }
611
612 final int suppressedEffects = zenModeConfig.suppressedVisualEffects;
613 final boolean hideNotificationDotsSelected =
614 (suppressedEffects & SUPPRESSED_EFFECT_BADGE) != 0;
615 final boolean dontPopNotifsOnScreenSelected =
616 (suppressedEffects & SUPPRESSED_EFFECT_PEEK) != 0;
617 final boolean hideFromPullDownShadeSelected =
618 (suppressedEffects & SUPPRESSED_EFFECT_NOTIFICATION_LIST) != 0;
619
620 final boolean dndEnabled = mZenModeController.getZen() != Settings.Global.ZEN_MODE_OFF;
621
622 mStackView.setSuppressNewDot(
623 dndEnabled && hideNotificationDotsSelected);
624 mStackView.setSuppressFlyout(
625 dndEnabled && (dontPopNotifsOnScreenSelected
626 || hideFromPullDownShadeSelected));
627 }
628
629 /**
630 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700631 * Updates the visibility of the bubbles based on current state.
632 * Does not un-bubble, just hides or un-hides. Notifies any
633 * {@link BubbleStateChangeListener}s of visibility changes.
634 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800635 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700636 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800637 if (mStackView == null) {
638 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800639 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800640 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
641 // Bubbles only appear in unlocked shade
642 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
643 } else if (mStackView != null) {
644 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800645 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700646
647 // Let listeners know if bubble state changed.
648 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
649 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
650 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
651 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
652 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
653 }
654
655 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800656 }
657
658 /**
659 * Rect indicating the touchable region for the bubble stack / expanded stack.
660 */
661 public Rect getTouchableRegion() {
662 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
663 return null;
664 }
665 mStackView.getBoundsOnScreen(mTempRect);
666 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800667 }
668
Mady Mellor390bff42019-04-05 15:09:01 -0700669 /**
670 * The display id of the expanded view, if the stack is expanded and not occluded by the
671 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
672 */
673 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200674 final Bubble bubble = getExpandedBubble(context);
675 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
676 }
677
678 @Nullable
679 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700680 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200681 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700682 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200683 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700684 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200685 final Bubble expandedBubble = mStackView.getExpandedBubble();
686 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700687 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200688 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700689 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200690 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700691 }
692
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800693 @VisibleForTesting
694 BubbleStackView getStackView() {
695 return mStackView;
696 }
697
Mady Mellor5549dd22018-11-06 18:07:34 -0800698 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800699 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800700 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800701 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800702 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800703 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800704 return false;
705 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800706 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800707
708 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
709 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
710 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
711
Mady Mellor5549dd22018-11-06 18:07:34 -0800712 boolean hasRemoteInput = false;
713 if (n.getNotification().actions != null) {
714 for (Notification.Action action : n.getNotification().actions) {
715 if (action.getRemoteInputs() != null) {
716 hasRemoteInput = true;
717 break;
718 }
719 }
720 }
Mady Mellor711f9562018-12-05 14:53:46 -0800721 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
722 && n.isOngoing();
723 boolean isMusic = n.getNotification().hasMediaSession();
724 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800725
Mady Mellor5549dd22018-11-06 18:07:34 -0800726 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800727 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
728 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800729 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800730 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800731 || autoBubbleAll;
732 }
733
Mady Mellor8a1f0252019-04-01 11:31:34 -0700734 private void updateShowInShadeForSuppressNotification(NotificationEntry entry) {
735 boolean suppressNotification = entry.getBubbleMetadata() != null
Mady Mellorc529d6d2019-04-16 14:22:52 -0700736 && entry.getBubbleMetadata().isNotificationSuppressed()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400737 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellor8a1f0252019-04-01 11:31:34 -0700738 entry.setShowInShadeWhenBubble(!suppressNotification);
739 }
740
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400741 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
742 StringBuilder sb = new StringBuilder();
743 for (Bubble bubble : bubbles) {
744 if (bubble == null) {
745 sb.append(" <null> !!!!!\n");
746 } else {
747 boolean isSelected = (bubble == selected);
748 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
749 ((isSelected) ? "->" : " "),
750 bubble.getLastActivity(),
751 (bubble.isOngoing() ? 1 : 0),
752 bubble.getKey()));
753 }
754 }
755 return sb.toString();
756 }
757
Mady Mellore80930e2019-03-21 16:00:45 -0700758 /**
759 * Return true if the applications with the package name is running in foreground.
760 *
Steven Wu8ba8ca92019-04-11 10:47:42 -0400761 * @param context application context.
Mady Mellore80930e2019-03-21 16:00:45 -0700762 * @param pkgName application package name.
763 */
Steven Wu8ba8ca92019-04-11 10:47:42 -0400764 public static boolean isForegroundApp(Context context, String pkgName) {
765 ActivityManager am = context.getSystemService(ActivityManager.class);
Mady Mellore80930e2019-03-21 16:00:45 -0700766 List<RunningTaskInfo> tasks = am.getRunningTasks(1 /* maxNum */);
767 return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
768 }
769
Mark Renoufcecc77b2019-01-30 16:32:24 -0500770 /**
771 * This task stack listener is responsible for responding to tasks moved to the front
772 * which are on the default (main) display. When this happens, expanded bubbles must be
773 * collapsed so the user may interact with the app which was just moved to the front.
774 * <p>
775 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
776 * these calls via a main thread Handler.
777 */
778 @MainThread
779 private class BubbleTaskStackListener extends TaskStackChangeListener {
780
Mark Renoufcecc77b2019-01-30 16:32:24 -0500781 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500782 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
783 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400784 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500785 }
786 }
787
Mark Renoufcecc77b2019-01-30 16:32:24 -0500788 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500789 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500790 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400791 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500792 }
793 }
Mark Renouf446251d2019-04-26 10:22:41 -0400794
795 @Override
796 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
797 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
798 mBubbleData.setExpanded(false);
799 }
800 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200801
802 @Override
803 public void onSingleTaskDisplayDrawn(int displayId) {
804 final Bubble expandedBubble = getExpandedBubble(mContext);
805 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
806 expandedBubble.setContentVisibility(true);
807 }
808 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500809 }
810
Mady Mellorceced172018-11-27 11:18:39 -0800811 private static boolean shouldAutoBubbleMessages(Context context) {
812 return Settings.Secure.getInt(context.getContentResolver(),
813 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
814 }
815
816 private static boolean shouldAutoBubbleOngoing(Context context) {
817 return Settings.Secure.getInt(context.getContentResolver(),
818 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
819 }
820
821 private static boolean shouldAutoBubbleAll(Context context) {
822 return Settings.Secure.getInt(context.getContentResolver(),
823 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800824 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500825
Mady Mellor3dff9e62019-02-05 18:12:53 -0800826 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500827 return Settings.Secure.getInt(context.getContentResolver(),
828 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
829 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800830
831 private static boolean areBubblesEnabled(Context context) {
832 return Settings.Secure.getInt(context.getContentResolver(),
833 ENABLE_BUBBLES, 1) != 0;
834 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500835
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500836 /** Default stiffness to use for bubble physics animations. */
837 public static int getBubbleStiffness(Context context, int defaultStiffness) {
838 return Settings.Secure.getInt(
839 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
840 }
841
842 /** Default bounciness/damping ratio to use for bubble physics animations. */
843 public static float getBubbleBounciness(Context context, float defaultBounciness) {
844 return Settings.Secure.getInt(
845 context.getContentResolver(),
846 BUBBLE_BOUNCINESS,
847 (int) (defaultBounciness * 100)) / 100f;
848 }
849
Mady Mellorca0c24c2019-05-16 16:14:32 -0700850 /**
851 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
852 *
853 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
854 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
855 *
856 * @param context the context to use.
857 * @param entry the entry to bubble.
858 */
859 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
860 PendingIntent intent = entry.getBubbleMetadata() != null
861 ? entry.getBubbleMetadata().getIntent()
862 : null;
863 if (intent == null) {
864 Log.w(TAG, "Unable to create bubble -- no intent");
865 return false;
866 }
867 ActivityInfo info =
868 intent.getIntent().resolveActivityInfo(context.getPackageManager(), 0);
869 if (info == null) {
870 Log.w(TAG, "Unable to send as bubble -- couldn't find activity info for intent: "
871 + intent);
872 return false;
873 }
874 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
875 Log.w(TAG, "Unable to send as bubble -- activity is not resizable for intent: "
876 + intent);
877 return false;
878 }
879 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
880 Log.w(TAG, "Unable to send as bubble -- activity is not documentLaunchMode=always "
881 + "for intent: " + intent);
882 return false;
883 }
884 if ((info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
885 Log.w(TAG, "Unable to send as bubble -- activity is not embeddable for intent: "
886 + intent);
887 return false;
888 }
889 return true;
890 }
891
Joshua Tsujia19515f2019-02-13 18:02:29 -0500892 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
893 private class BubblesImeListener extends IPinnedStackListener.Stub {
894
895 @Override
896 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
897 }
898
899 @Override
900 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
901 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
902 int displayRotation) throws RemoteException {}
903
904 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500905 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
906 if (mStackView != null && mStackView.getBubbleCount() > 0) {
907 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500908 }
909 }
910
911 @Override
912 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
913 throws RemoteException {}
914
915 @Override
916 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
917
918 @Override
919 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
920 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800921}