blob: a23c99ef01fee48008bb5d4c27d3d1dad6cc5774 [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();
274 // TODO(b/130237686): When you expand the shade on top of expanded bubble, there is no
275 // scrim between bubble and the shade
276 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
277 sbv.addView(mStackView, bubblePosition,
278 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
279 if (mExpandListener != null) {
280 mStackView.setExpandListener(mExpandListener);
281 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400282
283 updateStackViewForZenConfig();
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400284 }
285 }
286
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700287 @Override
288 public void onUiModeChanged() {
289 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700290 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700291 }
292 }
293
294 @Override
295 public void onOverlayChanged() {
296 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700297 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700298 }
299 }
300
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400301 @Override
302 public void onConfigChanged(Configuration newConfig) {
303 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
304 mStackView.onOrientationChanged();
305 mOrientation = newConfig.orientation;
306 }
307 }
308
Mady Mellor5549dd22018-11-06 18:07:34 -0800309 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800310 * Set a listener to be notified when some states of the bubbles change.
311 */
312 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
313 mStateChangeListener = listener;
314 }
315
316 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800317 * Set a listener to be notified of bubble expand events.
318 */
319 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100320 mExpandListener = ((isExpanding, key) -> {
321 if (listener != null) {
322 listener.onBubbleExpandChanged(isExpanding, key);
323 }
324 mStatusBarWindowController.setBubbleExpanded(isExpanding);
325 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800326 if (mStackView != null) {
327 mStackView.setExpandListener(mExpandListener);
328 }
329 }
330
331 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800332 * Whether or not there are bubbles present, regardless of them being visible on the
333 * screen (e.g. if on AOD).
334 */
335 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800336 if (mStackView == null) {
337 return false;
338 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400339 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800340 }
341
342 /**
343 * Whether the stack of bubbles is expanded or not.
344 */
345 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400346 return mBubbleData.isExpanded();
347 }
348
349 /**
350 * Tell the stack of bubbles to expand.
351 */
352 public void expandStack() {
353 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800354 }
355
356 /**
357 * Tell the stack of bubbles to collapse.
358 */
359 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400360 mBubbleData.setExpanded(false /* expanded */);
361 }
362
363 void selectBubble(Bubble bubble) {
364 mBubbleData.setSelectedBubble(bubble);
365 }
366
367 @VisibleForTesting
368 void selectBubble(String key) {
369 Bubble bubble = mBubbleData.getBubbleWithKey(key);
370 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800371 }
372
373 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400374 * Request the stack expand if needed, then select the specified Bubble as current.
375 *
376 * @param notificationKey the notification key for the bubble to be selected
377 */
378 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400379 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
380 if (bubble != null) {
381 mBubbleData.setSelectedBubble(bubble);
382 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400383 }
384 }
385
386 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800387 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
388 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500389 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400390 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800391 }
392
393 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500394 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
395 * is forwarded a back key down/up pair.
396 */
397 public void performBackPressIfNeeded() {
398 if (mStackView != null) {
399 mStackView.performBackPressIfNeeded();
400 }
401 }
402
403 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800404 * Adds or updates a bubble associated with the provided notification entry.
405 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400406 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800407 */
Mark Renouff97ed462019-04-05 13:46:24 -0400408 void updateBubble(NotificationEntry notif) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700409 // If this is an interruptive notif, mark that it's interrupted
410 if (notif.importance >= NotificationManager.IMPORTANCE_HIGH) {
411 notif.setInterruption();
412 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400413 mBubbleData.notificationEntryUpdated(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800414 }
415
416 /**
417 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500418 * <p>
419 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800420 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500421 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500422 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400423 // TEMP: refactor to change this to pass entry
424 Bubble bubble = mBubbleData.getBubbleWithKey(key);
425 if (bubble != null) {
426 mBubbleData.notificationEntryRemoved(bubble.entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800427 }
428 }
429
Ned Burns01e38212019-01-03 16:32:52 -0500430 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700431 private final NotificationRemoveInterceptor mRemoveInterceptor =
432 new NotificationRemoveInterceptor() {
433 @Override
434 public boolean onNotificationRemoveRequested(String key, int reason) {
435 if (!mBubbleData.hasBubbleWithKey(key)) {
436 return false;
437 }
438 NotificationEntry entry = mBubbleData.getBubbleWithKey(key).entry;
439
440 final boolean isClearAll = reason == REASON_CANCEL_ALL;
441 final boolean isUserDimiss = reason == REASON_CANCEL;
442 final boolean isAppCancel = reason == REASON_APP_CANCEL
443 || reason == REASON_APP_CANCEL_ALL;
444
445 // Need to check for !appCancel here because the notification may have
446 // previously been dismissed & entry.isRowDismissed would still be true
447 boolean userRemovedNotif = (entry.isRowDismissed() && !isAppCancel)
448 || isClearAll || isUserDimiss;
449
450 // The bubble notification sticks around in the data as long as the bubble is
451 // not dismissed and the app hasn't cancelled the notification.
452 boolean bubbleExtended = entry.isBubble() && !entry.isBubbleDismissed()
453 && userRemovedNotif;
454 if (bubbleExtended) {
455 entry.setShowInShadeWhenBubble(false);
456 if (mStackView != null) {
457 mStackView.updateDotVisibility(entry.key);
458 }
459 mNotificationEntryManager.updateNotifications();
460 return true;
461 } else if (!userRemovedNotif && !entry.isBubbleDismissed()) {
462 // This wasn't a user removal so we should remove the bubble as well
463 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
464 return false;
465 }
466 return false;
467 }
468 };
469
470 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500471 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
472 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500473 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800474 if (!areBubblesEnabled(mContext)) {
475 return;
476 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700477 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
478 && canLaunchInActivityView(mContext, entry)) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700479 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800480 }
481 }
482
483 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500484 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800485 if (!areBubblesEnabled(mContext)) {
486 return;
487 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700488 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
489 && canLaunchInActivityView(mContext, entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400490 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800491 }
492 }
493
494 @Override
495 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800496 if (!areBubblesEnabled(mContext)) {
497 return;
498 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700499 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
500 && canLaunchInActivityView(mContext, entry);
Mady Melloraa8fef22019-04-11 13:36:40 -0700501 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.key)) {
502 // It was previously a bubble but no longer a bubble -- lets remove it
503 removeBubble(entry.key, DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700504 } else if (shouldBubble) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700505 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800506 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mark Renouff97ed462019-04-05 13:46:24 -0400507 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800508 }
509 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400510
511 @Override
512 public void onNotificationRankingUpdated(RankingMap rankingMap) {
513 // Forward to BubbleData to block any bubbles which should no longer be shown
514 mBubbleData.notificationRankingUpdated(rankingMap);
515 }
Ned Burns01e38212019-01-03 16:32:52 -0500516 };
517
Mark Renouf71a3af62019-04-08 15:02:54 -0400518 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400519 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400520
Mark Renouf3bc5b362019-04-05 14:37:59 -0400521 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400522 public void applyUpdate(BubbleData.Update update) {
523 if (mStackView == null && update.addedBubble != null) {
524 // Lazy init stack view when the first bubble is added.
525 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400526 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400527
528 // If not yet initialized, ignore all other changes.
529 if (mStackView == null) {
530 return;
531 }
532
533 if (update.addedBubble != null) {
534 mStackView.addBubble(update.addedBubble);
535 }
536
537 // Collapsing? Do this first before remaining steps.
538 if (update.expandedChanged && !update.expanded) {
539 mStackView.setExpanded(false);
540 }
541
542 // Do removals, if any.
543 for (Pair<Bubble, Integer> removed : update.removedBubbles) {
544 final Bubble bubble = removed.first;
545 @DismissReason final int reason = removed.second;
546 mStackView.removeBubble(bubble);
547
548 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
549 && !bubble.entry.showInShadeWhenBubble()) {
550 // The bubble is gone & the notification is gone, time to actually remove it
551 mNotificationEntryManager.performRemoveNotification(bubble.entry.notification,
552 UNDEFINED_DISMISS_REASON);
553 } else {
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700554 // Update the flag for SysUI
555 bubble.entry.notification.getNotification().flags &= ~FLAG_BUBBLE;
556
557 // Make sure NoMan knows it's not a bubble anymore so anyone querying it will
558 // get right result back
Mark Renouf82a40e62019-05-23 16:16:24 -0400559 try {
560 mBarService.onNotificationBubbleChanged(bubble.getKey(),
561 false /* isBubble */);
562 } catch (RemoteException e) {
563 // Bad things have happened
564 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700565 }
566 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400567
Mark Renouf82a40e62019-05-23 16:16:24 -0400568 if (update.updatedBubble != null) {
569 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400570 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400571
Mark Renouf82a40e62019-05-23 16:16:24 -0400572 if (update.orderChanged) {
573 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400574 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400575
Mark Renouf82a40e62019-05-23 16:16:24 -0400576 if (update.selectionChanged) {
577 mStackView.setSelectedBubble(update.selectedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400578 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400579
Mark Renouf82a40e62019-05-23 16:16:24 -0400580 // Expanding? Apply this last.
581 if (update.expandedChanged && update.expanded) {
582 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400583 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400584
Mark Renouf71a3af62019-04-08 15:02:54 -0400585 mNotificationEntryManager.updateNotifications();
Lyn Han6c40fe72019-05-08 14:06:33 -0700586 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400587
588 if (DEBUG) {
589 Log.d(TAG, "[BubbleData]");
590 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
591 mBubbleData.getSelectedBubble()));
592
593 if (mStackView != null) {
594 Log.d(TAG, "[BubbleStackView]");
595 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
596 mStackView.getExpandedBubble()));
597 }
598 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400599 }
600 };
601
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800602 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400603 * Updates the stack view's suppression flags from the latest config from the zen (do not
604 * disturb) controller.
605 */
606 private void updateStackViewForZenConfig() {
607 final ZenModeConfig zenModeConfig = mZenModeController.getConfig();
608
609 if (zenModeConfig == null || mStackView == null) {
610 return;
611 }
612
613 final int suppressedEffects = zenModeConfig.suppressedVisualEffects;
614 final boolean hideNotificationDotsSelected =
615 (suppressedEffects & SUPPRESSED_EFFECT_BADGE) != 0;
616 final boolean dontPopNotifsOnScreenSelected =
617 (suppressedEffects & SUPPRESSED_EFFECT_PEEK) != 0;
618 final boolean hideFromPullDownShadeSelected =
619 (suppressedEffects & SUPPRESSED_EFFECT_NOTIFICATION_LIST) != 0;
620
621 final boolean dndEnabled = mZenModeController.getZen() != Settings.Global.ZEN_MODE_OFF;
622
623 mStackView.setSuppressNewDot(
624 dndEnabled && hideNotificationDotsSelected);
625 mStackView.setSuppressFlyout(
626 dndEnabled && (dontPopNotifsOnScreenSelected
627 || hideFromPullDownShadeSelected));
628 }
629
630 /**
631 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700632 * Updates the visibility of the bubbles based on current state.
633 * Does not un-bubble, just hides or un-hides. Notifies any
634 * {@link BubbleStateChangeListener}s of visibility changes.
635 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800636 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700637 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800638 if (mStackView == null) {
639 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800640 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800641 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
642 // Bubbles only appear in unlocked shade
643 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
644 } else if (mStackView != null) {
645 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800646 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700647
648 // Let listeners know if bubble state changed.
649 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
650 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
651 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
652 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
653 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
654 }
655
656 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800657 }
658
659 /**
660 * Rect indicating the touchable region for the bubble stack / expanded stack.
661 */
662 public Rect getTouchableRegion() {
663 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
664 return null;
665 }
666 mStackView.getBoundsOnScreen(mTempRect);
667 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800668 }
669
Mady Mellor390bff42019-04-05 15:09:01 -0700670 /**
671 * The display id of the expanded view, if the stack is expanded and not occluded by the
672 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
673 */
674 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200675 final Bubble bubble = getExpandedBubble(context);
676 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
677 }
678
679 @Nullable
680 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700681 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200682 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700683 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200684 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700685 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200686 final Bubble expandedBubble = mStackView.getExpandedBubble();
687 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700688 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200689 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700690 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200691 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700692 }
693
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800694 @VisibleForTesting
695 BubbleStackView getStackView() {
696 return mStackView;
697 }
698
Mady Mellor5549dd22018-11-06 18:07:34 -0800699 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800700 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800701 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800702 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800703 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800704 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800705 return false;
706 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800707 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800708
709 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
710 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
711 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
712
Mady Mellor5549dd22018-11-06 18:07:34 -0800713 boolean hasRemoteInput = false;
714 if (n.getNotification().actions != null) {
715 for (Notification.Action action : n.getNotification().actions) {
716 if (action.getRemoteInputs() != null) {
717 hasRemoteInput = true;
718 break;
719 }
720 }
721 }
Mady Mellor711f9562018-12-05 14:53:46 -0800722 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
723 && n.isOngoing();
724 boolean isMusic = n.getNotification().hasMediaSession();
725 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800726
Mady Mellor5549dd22018-11-06 18:07:34 -0800727 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800728 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
729 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800730 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800731 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800732 || autoBubbleAll;
733 }
734
Mady Mellor8a1f0252019-04-01 11:31:34 -0700735 private void updateShowInShadeForSuppressNotification(NotificationEntry entry) {
736 boolean suppressNotification = entry.getBubbleMetadata() != null
Mady Mellorc529d6d2019-04-16 14:22:52 -0700737 && entry.getBubbleMetadata().isNotificationSuppressed()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400738 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellor8a1f0252019-04-01 11:31:34 -0700739 entry.setShowInShadeWhenBubble(!suppressNotification);
740 }
741
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400742 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
743 StringBuilder sb = new StringBuilder();
744 for (Bubble bubble : bubbles) {
745 if (bubble == null) {
746 sb.append(" <null> !!!!!\n");
747 } else {
748 boolean isSelected = (bubble == selected);
749 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
750 ((isSelected) ? "->" : " "),
751 bubble.getLastActivity(),
752 (bubble.isOngoing() ? 1 : 0),
753 bubble.getKey()));
754 }
755 }
756 return sb.toString();
757 }
758
Mady Mellore80930e2019-03-21 16:00:45 -0700759 /**
760 * Return true if the applications with the package name is running in foreground.
761 *
Steven Wu8ba8ca92019-04-11 10:47:42 -0400762 * @param context application context.
Mady Mellore80930e2019-03-21 16:00:45 -0700763 * @param pkgName application package name.
764 */
Steven Wu8ba8ca92019-04-11 10:47:42 -0400765 public static boolean isForegroundApp(Context context, String pkgName) {
766 ActivityManager am = context.getSystemService(ActivityManager.class);
Mady Mellore80930e2019-03-21 16:00:45 -0700767 List<RunningTaskInfo> tasks = am.getRunningTasks(1 /* maxNum */);
768 return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
769 }
770
Mark Renoufcecc77b2019-01-30 16:32:24 -0500771 /**
772 * This task stack listener is responsible for responding to tasks moved to the front
773 * which are on the default (main) display. When this happens, expanded bubbles must be
774 * collapsed so the user may interact with the app which was just moved to the front.
775 * <p>
776 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
777 * these calls via a main thread Handler.
778 */
779 @MainThread
780 private class BubbleTaskStackListener extends TaskStackChangeListener {
781
Mark Renoufcecc77b2019-01-30 16:32:24 -0500782 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500783 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
784 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400785 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500786 }
787 }
788
Mark Renoufcecc77b2019-01-30 16:32:24 -0500789 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500790 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500791 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400792 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500793 }
794 }
Mark Renouf446251d2019-04-26 10:22:41 -0400795
796 @Override
797 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
798 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
799 mBubbleData.setExpanded(false);
800 }
801 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200802
803 @Override
804 public void onSingleTaskDisplayDrawn(int displayId) {
805 final Bubble expandedBubble = getExpandedBubble(mContext);
806 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
807 expandedBubble.setContentVisibility(true);
808 }
809 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500810 }
811
Mady Mellorceced172018-11-27 11:18:39 -0800812 private static boolean shouldAutoBubbleMessages(Context context) {
813 return Settings.Secure.getInt(context.getContentResolver(),
814 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
815 }
816
817 private static boolean shouldAutoBubbleOngoing(Context context) {
818 return Settings.Secure.getInt(context.getContentResolver(),
819 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
820 }
821
822 private static boolean shouldAutoBubbleAll(Context context) {
823 return Settings.Secure.getInt(context.getContentResolver(),
824 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800825 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500826
Mady Mellor3dff9e62019-02-05 18:12:53 -0800827 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500828 return Settings.Secure.getInt(context.getContentResolver(),
829 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
830 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800831
832 private static boolean areBubblesEnabled(Context context) {
833 return Settings.Secure.getInt(context.getContentResolver(),
834 ENABLE_BUBBLES, 1) != 0;
835 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500836
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500837 /** Default stiffness to use for bubble physics animations. */
838 public static int getBubbleStiffness(Context context, int defaultStiffness) {
839 return Settings.Secure.getInt(
840 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
841 }
842
843 /** Default bounciness/damping ratio to use for bubble physics animations. */
844 public static float getBubbleBounciness(Context context, float defaultBounciness) {
845 return Settings.Secure.getInt(
846 context.getContentResolver(),
847 BUBBLE_BOUNCINESS,
848 (int) (defaultBounciness * 100)) / 100f;
849 }
850
Mady Mellorca0c24c2019-05-16 16:14:32 -0700851 /**
852 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
853 *
854 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
855 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
856 *
857 * @param context the context to use.
858 * @param entry the entry to bubble.
859 */
860 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
861 PendingIntent intent = entry.getBubbleMetadata() != null
862 ? entry.getBubbleMetadata().getIntent()
863 : null;
864 if (intent == null) {
865 Log.w(TAG, "Unable to create bubble -- no intent");
866 return false;
867 }
868 ActivityInfo info =
869 intent.getIntent().resolveActivityInfo(context.getPackageManager(), 0);
870 if (info == null) {
871 Log.w(TAG, "Unable to send as bubble -- couldn't find activity info for intent: "
872 + intent);
873 return false;
874 }
875 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
876 Log.w(TAG, "Unable to send as bubble -- activity is not resizable for intent: "
877 + intent);
878 return false;
879 }
880 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
881 Log.w(TAG, "Unable to send as bubble -- activity is not documentLaunchMode=always "
882 + "for intent: " + intent);
883 return false;
884 }
885 if ((info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
886 Log.w(TAG, "Unable to send as bubble -- activity is not embeddable for intent: "
887 + intent);
888 return false;
889 }
890 return true;
891 }
892
Joshua Tsujia19515f2019-02-13 18:02:29 -0500893 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
894 private class BubblesImeListener extends IPinnedStackListener.Stub {
895
896 @Override
897 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
898 }
899
900 @Override
901 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
902 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
903 int displayRotation) throws RemoteException {}
904
905 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500906 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
907 if (mStackView != null && mStackView.getBubbleCount() > 0) {
908 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500909 }
910 }
911
912 @Override
913 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
914 throws RemoteException {}
915
916 @Override
917 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
918
919 @Override
920 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
921 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800922}