blob: bde1eb09de49e9a446e4cdb35b85f4a46c8f0858 [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
Issei Suzukia8d07312019-06-07 12:56:19 +020034import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_CONTROLLER;
35import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
36import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080037import static com.android.systemui.statusbar.StatusBarState.SHADE;
Mady Mellor1a4e86f2019-05-03 16:07:23 -070038import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080039
Mark Renoufba5ab512019-05-02 15:21:01 -040040import static java.lang.annotation.ElementType.FIELD;
41import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
42import static java.lang.annotation.ElementType.PARAMETER;
Mark Renouf08bc42a2019-03-07 13:01:59 -050043import static java.lang.annotation.RetentionPolicy.SOURCE;
44
Mady Mellore80930e2019-03-21 16:00:45 -070045import android.app.ActivityManager;
Mark Renoufc808f062019-02-07 15:20:37 -050046import android.app.ActivityManager.RunningTaskInfo;
Mady Mellor5549dd22018-11-06 18:07:34 -080047import android.app.Notification;
Mady Mellor66efd5e2019-05-15 13:38:11 -070048import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070049import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080050import android.content.Context;
Mady Mellorca0c24c2019-05-16 16:14:32 -070051import android.content.pm.ActivityInfo;
Joshua Tsujia19515f2019-02-13 18:02:29 -050052import android.content.pm.ParceledListSlice;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040053import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080054import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050055import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080056import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080057import android.provider.Settings;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040058import android.service.notification.NotificationListenerService.RankingMap;
Mady Mellor5549dd22018-11-06 18:07:34 -080059import android.service.notification.StatusBarNotification;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040060import android.service.notification.ZenModeConfig;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040061import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040062import android.util.Pair;
Mark Renoufcecc77b2019-01-30 16:32:24 -050063import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050064import android.view.IPinnedStackController;
65import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080066import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080067import android.widget.FrameLayout;
68
Mark Renouf08bc42a2019-03-07 13:01:59 -050069import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050070import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040071import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050072
Mady Mellorebdbbb92018-11-15 14:36:48 -080073import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070074import com.android.internal.statusbar.IStatusBarService;
Ned Burns01e38212019-01-03 16:32:52 -050075import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080076import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050077import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050078import com.android.systemui.shared.system.ActivityManagerWrapper;
79import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050080import com.android.systemui.shared.system.WindowManagerWrapper;
Mady Mellorc2ff0112019-03-28 14:18:06 -070081import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050082import com.android.systemui.statusbar.notification.NotificationEntryListener;
83import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080084import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050085import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050086import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080087import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070088import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040089import com.android.systemui.statusbar.policy.ZenModeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080090
Mark Renouf08bc42a2019-03-07 13:01:59 -050091import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -040092import java.lang.annotation.Target;
Mady Mellore80930e2019-03-21 16:00:45 -070093import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050094
Jason Monk27d01a622018-12-10 15:57:09 -050095import javax.inject.Inject;
96import javax.inject.Singleton;
97
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080098/**
99 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
100 * Bubbles can be expanded to show more content.
101 *
102 * The controller manages addition, removal, and visible state of bubbles on screen.
103 */
Jason Monk27d01a622018-12-10 15:57:09 -0500104@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -0400105public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800106
Issei Suzukia8d07312019-06-07 12:56:19 +0200107 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleController" : TAG_BUBBLES;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800108
Mark Renouf08bc42a2019-03-07 13:01:59 -0500109 @Retention(SOURCE)
110 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mady Melloraa8fef22019-04-11 13:36:40 -0700111 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE})
Mark Renoufba5ab512019-05-02 15:21:01 -0400112 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500113 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700114
Mark Renouf08bc42a2019-03-07 13:01:59 -0500115 static final int DISMISS_USER_GESTURE = 1;
116 static final int DISMISS_AGED = 2;
117 static final int DISMISS_TASK_FINISHED = 3;
118 static final int DISMISS_BLOCKED = 4;
119 static final int DISMISS_NOTIF_CANCEL = 5;
120 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700121 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500122
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400123 public static final int MAX_BUBBLES = 5; // TODO: actually enforce this
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -0400124
Mady Mellor5549dd22018-11-06 18:07:34 -0800125 // Enables some subset of notifs to automatically become bubbles
Mady Mellorca0c24c2019-05-16 16:14:32 -0700126 public static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -0800127
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800128 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800129 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800130 /** Auto bubble flags set whether different notif types should be presented as a bubble */
Mady Mellorceced172018-11-27 11:18:39 -0800131 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
132 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
133 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800134
135 /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500136 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -0800137
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500138 private static final String BUBBLE_STIFFNESS = "experiment_bubble_stiffness";
139 private static final String BUBBLE_BOUNCINESS = "experiment_bubble_bounciness";
140
Ned Burns01e38212019-01-03 16:32:52 -0500141 private final Context mContext;
142 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500143 private final BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800144 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800145 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100146 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800147
Mady Mellor3dff9e62019-02-05 18:12:53 -0800148 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400149 @Nullable private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800150
151 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500152 private final StatusBarWindowController mStatusBarWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400153 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800154 private StatusBarStateListener mStatusBarStateListener;
155
Mady Melloraa8fef22019-04-11 13:36:40 -0700156 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700157 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800158
Mady Mellord1c78b262018-11-06 18:04:40 -0800159 // Used for determining view rect for touch interaction
160 private Rect mTempRect = new Rect();
161
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400162 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
163 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
164
Mady Mellor5549dd22018-11-06 18:07:34 -0800165 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800166 * Listener to be notified when some states of the bubbles change.
167 */
168 public interface BubbleStateChangeListener {
169 /**
170 * Called when the stack has bubbles or no longer has bubbles.
171 */
172 void onHasBubblesChanged(boolean hasBubbles);
173 }
174
Mady Mellorcd9b1302018-11-06 18:08:04 -0800175 /**
176 * Listener to find out about stack expansion / collapse events.
177 */
178 public interface BubbleExpandListener {
179 /**
180 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700181 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800182 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800183 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800184 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800185 void onBubbleExpandChanged(boolean isExpanding, String key);
186 }
187
188 /**
189 * Listens for the current state of the status bar and updates the visibility state
190 * of bubbles as needed.
191 */
192 private class StatusBarStateListener implements StatusBarStateController.StateListener {
193 private int mState;
194 /**
195 * Returns the current status bar state.
196 */
197 public int getCurrentState() {
198 return mState;
199 }
200
201 @Override
202 public void onStateChanged(int newState) {
203 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400204 boolean shouldCollapse = (mState != SHADE);
205 if (shouldCollapse) {
206 collapseStack();
207 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700208 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800209 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800210 }
211
Jason Monk27d01a622018-12-10 15:57:09 -0500212 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800213 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Mady Melloraa8fef22019-04-11 13:36:40 -0700214 BubbleData data, ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400215 NotificationInterruptionStateProvider interruptionStateProvider,
216 ZenModeController zenModeController) {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700217 this(context, statusBarWindowController, data, null /* synchronizer */,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400218 configurationController, interruptionStateProvider, zenModeController);
Issei Suzukic0387542019-03-08 17:31:14 +0100219 }
220
221 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700222 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
Mady Melloraa8fef22019-04-11 13:36:40 -0700223 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400224 NotificationInterruptionStateProvider interruptionStateProvider,
225 ZenModeController zenModeController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800226 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700227 mNotificationInterruptionStateProvider = interruptionStateProvider;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400228 mZenModeController = zenModeController;
229 mZenModeController.addCallback(new ZenModeController.Callback() {
230 @Override
231 public void onZenChanged(int zen) {
232 updateStackViewForZenConfig();
233 }
234
235 @Override
236 public void onConfigChanged(ZenModeConfig config) {
237 updateStackViewForZenConfig();
238 }
239 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700240
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700241 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800242
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400243 mBubbleData = data;
244 mBubbleData.setListener(mBubbleDataListener);
245
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800246 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500247 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700248 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellorb4991e62019-01-10 15:14:51 -0800249
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800250 mStatusBarWindowController = statusBarWindowController;
251 mStatusBarStateListener = new StatusBarStateListener();
252 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500253
Mark Renoufcecc77b2019-01-30 16:32:24 -0500254 mTaskStackListener = new BubbleTaskStackListener();
255 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800256
Joshua Tsujia19515f2019-02-13 18:02:29 -0500257 try {
258 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
259 } catch (RemoteException e) {
260 e.printStackTrace();
261 }
Issei Suzukic0387542019-03-08 17:31:14 +0100262 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700263
264 mBarService = IStatusBarService.Stub.asInterface(
265 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mady Mellor5549dd22018-11-06 18:07:34 -0800266 }
267
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400268 /**
269 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
270 * method initializes the stack view and adds it to the StatusBar just above the scrim.
271 */
272 private void ensureStackViewCreated() {
273 if (mStackView == null) {
274 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
275 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
276 // TODO(b/130237686): When you expand the shade on top of expanded bubble, there is no
277 // scrim between bubble and the shade
278 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
279 sbv.addView(mStackView, bubblePosition,
280 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
281 if (mExpandListener != null) {
282 mStackView.setExpandListener(mExpandListener);
283 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400284
285 updateStackViewForZenConfig();
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400286 }
287 }
288
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700289 @Override
290 public void onUiModeChanged() {
291 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700292 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700293 }
294 }
295
296 @Override
297 public void onOverlayChanged() {
298 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700299 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700300 }
301 }
302
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400303 @Override
304 public void onConfigChanged(Configuration newConfig) {
305 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
306 mStackView.onOrientationChanged();
307 mOrientation = newConfig.orientation;
308 }
309 }
310
Mady Mellor5549dd22018-11-06 18:07:34 -0800311 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800312 * Set a listener to be notified when some states of the bubbles change.
313 */
314 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
315 mStateChangeListener = listener;
316 }
317
318 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800319 * Set a listener to be notified of bubble expand events.
320 */
321 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100322 mExpandListener = ((isExpanding, key) -> {
323 if (listener != null) {
324 listener.onBubbleExpandChanged(isExpanding, key);
325 }
326 mStatusBarWindowController.setBubbleExpanded(isExpanding);
327 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800328 if (mStackView != null) {
329 mStackView.setExpandListener(mExpandListener);
330 }
331 }
332
333 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800334 * Whether or not there are bubbles present, regardless of them being visible on the
335 * screen (e.g. if on AOD).
336 */
337 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800338 if (mStackView == null) {
339 return false;
340 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400341 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800342 }
343
344 /**
345 * Whether the stack of bubbles is expanded or not.
346 */
347 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400348 return mBubbleData.isExpanded();
349 }
350
351 /**
352 * Tell the stack of bubbles to expand.
353 */
354 public void expandStack() {
355 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800356 }
357
358 /**
359 * Tell the stack of bubbles to collapse.
360 */
361 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400362 mBubbleData.setExpanded(false /* expanded */);
363 }
364
365 void selectBubble(Bubble bubble) {
366 mBubbleData.setSelectedBubble(bubble);
367 }
368
369 @VisibleForTesting
370 void selectBubble(String key) {
371 Bubble bubble = mBubbleData.getBubbleWithKey(key);
372 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800373 }
374
375 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400376 * Request the stack expand if needed, then select the specified Bubble as current.
377 *
378 * @param notificationKey the notification key for the bubble to be selected
379 */
380 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400381 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
382 if (bubble != null) {
383 mBubbleData.setSelectedBubble(bubble);
384 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400385 }
386 }
387
388 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800389 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
390 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500391 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400392 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800393 }
394
395 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500396 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
397 * is forwarded a back key down/up pair.
398 */
399 public void performBackPressIfNeeded() {
400 if (mStackView != null) {
401 mStackView.performBackPressIfNeeded();
402 }
403 }
404
405 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800406 * Adds or updates a bubble associated with the provided notification entry.
407 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400408 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800409 */
Mark Renouff97ed462019-04-05 13:46:24 -0400410 void updateBubble(NotificationEntry notif) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700411 // If this is an interruptive notif, mark that it's interrupted
412 if (notif.importance >= NotificationManager.IMPORTANCE_HIGH) {
413 notif.setInterruption();
414 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400415 mBubbleData.notificationEntryUpdated(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800416 }
417
418 /**
419 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500420 * <p>
421 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800422 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500423 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500424 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400425 // TEMP: refactor to change this to pass entry
426 Bubble bubble = mBubbleData.getBubbleWithKey(key);
427 if (bubble != null) {
428 mBubbleData.notificationEntryRemoved(bubble.entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800429 }
430 }
431
Ned Burns01e38212019-01-03 16:32:52 -0500432 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700433 private final NotificationRemoveInterceptor mRemoveInterceptor =
434 new NotificationRemoveInterceptor() {
435 @Override
436 public boolean onNotificationRemoveRequested(String key, int reason) {
437 if (!mBubbleData.hasBubbleWithKey(key)) {
438 return false;
439 }
440 NotificationEntry entry = mBubbleData.getBubbleWithKey(key).entry;
441
442 final boolean isClearAll = reason == REASON_CANCEL_ALL;
443 final boolean isUserDimiss = reason == REASON_CANCEL;
444 final boolean isAppCancel = reason == REASON_APP_CANCEL
445 || reason == REASON_APP_CANCEL_ALL;
446
447 // Need to check for !appCancel here because the notification may have
448 // previously been dismissed & entry.isRowDismissed would still be true
449 boolean userRemovedNotif = (entry.isRowDismissed() && !isAppCancel)
450 || isClearAll || isUserDimiss;
451
452 // The bubble notification sticks around in the data as long as the bubble is
453 // not dismissed and the app hasn't cancelled the notification.
454 boolean bubbleExtended = entry.isBubble() && !entry.isBubbleDismissed()
455 && userRemovedNotif;
456 if (bubbleExtended) {
457 entry.setShowInShadeWhenBubble(false);
458 if (mStackView != null) {
459 mStackView.updateDotVisibility(entry.key);
460 }
461 mNotificationEntryManager.updateNotifications();
462 return true;
463 } else if (!userRemovedNotif && !entry.isBubbleDismissed()) {
464 // This wasn't a user removal so we should remove the bubble as well
465 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
466 return false;
467 }
468 return false;
469 }
470 };
471
472 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500473 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
474 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500475 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800476 if (!areBubblesEnabled(mContext)) {
477 return;
478 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700479 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
480 && canLaunchInActivityView(mContext, entry)) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700481 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800482 }
483 }
484
485 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500486 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800487 if (!areBubblesEnabled(mContext)) {
488 return;
489 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700490 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
491 && canLaunchInActivityView(mContext, entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400492 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800493 }
494 }
495
496 @Override
497 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800498 if (!areBubblesEnabled(mContext)) {
499 return;
500 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700501 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
502 && canLaunchInActivityView(mContext, entry);
Mady Melloraa8fef22019-04-11 13:36:40 -0700503 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.key)) {
504 // It was previously a bubble but no longer a bubble -- lets remove it
505 removeBubble(entry.key, DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700506 } else if (shouldBubble) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700507 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800508 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mark Renouff97ed462019-04-05 13:46:24 -0400509 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800510 }
511 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400512
513 @Override
514 public void onNotificationRankingUpdated(RankingMap rankingMap) {
515 // Forward to BubbleData to block any bubbles which should no longer be shown
516 mBubbleData.notificationRankingUpdated(rankingMap);
517 }
Ned Burns01e38212019-01-03 16:32:52 -0500518 };
519
Mark Renouf71a3af62019-04-08 15:02:54 -0400520 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400521 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400522
Mark Renouf3bc5b362019-04-05 14:37:59 -0400523 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400524 public void applyUpdate(BubbleData.Update update) {
525 if (mStackView == null && update.addedBubble != null) {
526 // Lazy init stack view when the first bubble is added.
527 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400528 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400529
530 // If not yet initialized, ignore all other changes.
531 if (mStackView == null) {
532 return;
533 }
534
535 if (update.addedBubble != null) {
536 mStackView.addBubble(update.addedBubble);
537 }
538
539 // Collapsing? Do this first before remaining steps.
540 if (update.expandedChanged && !update.expanded) {
541 mStackView.setExpanded(false);
542 }
543
544 // Do removals, if any.
545 for (Pair<Bubble, Integer> removed : update.removedBubbles) {
546 final Bubble bubble = removed.first;
547 @DismissReason final int reason = removed.second;
548 mStackView.removeBubble(bubble);
549
550 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
551 && !bubble.entry.showInShadeWhenBubble()) {
552 // The bubble is gone & the notification is gone, time to actually remove it
553 mNotificationEntryManager.performRemoveNotification(bubble.entry.notification,
554 UNDEFINED_DISMISS_REASON);
555 } else {
Mady Mellor3a0a1b42019-05-23 06:40:21 -0700556 // Update the flag for SysUI
557 bubble.entry.notification.getNotification().flags &= ~FLAG_BUBBLE;
558
559 // Make sure NoMan knows it's not a bubble anymore so anyone querying it will
560 // get right result back
Mark Renouf82a40e62019-05-23 16:16:24 -0400561 try {
562 mBarService.onNotificationBubbleChanged(bubble.getKey(),
563 false /* isBubble */);
564 } catch (RemoteException e) {
565 // Bad things have happened
566 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700567 }
568 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400569
Mark Renouf82a40e62019-05-23 16:16:24 -0400570 if (update.updatedBubble != null) {
571 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400572 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400573
Mark Renouf82a40e62019-05-23 16:16:24 -0400574 if (update.orderChanged) {
575 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400576 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400577
Mark Renouf82a40e62019-05-23 16:16:24 -0400578 if (update.selectionChanged) {
579 mStackView.setSelectedBubble(update.selectedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400580 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400581
Mark Renouf82a40e62019-05-23 16:16:24 -0400582 // Expanding? Apply this last.
583 if (update.expandedChanged && update.expanded) {
584 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400585 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400586
Mark Renouf71a3af62019-04-08 15:02:54 -0400587 mNotificationEntryManager.updateNotifications();
Lyn Han6c40fe72019-05-08 14:06:33 -0700588 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400589
Issei Suzukia8d07312019-06-07 12:56:19 +0200590 if (DEBUG_BUBBLE_CONTROLLER) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400591 Log.d(TAG, "[BubbleData]");
592 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
593 mBubbleData.getSelectedBubble()));
594
595 if (mStackView != null) {
596 Log.d(TAG, "[BubbleStackView]");
597 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
598 mStackView.getExpandedBubble()));
599 }
600 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400601 }
602 };
603
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800604 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400605 * Updates the stack view's suppression flags from the latest config from the zen (do not
606 * disturb) controller.
607 */
608 private void updateStackViewForZenConfig() {
609 final ZenModeConfig zenModeConfig = mZenModeController.getConfig();
610
611 if (zenModeConfig == null || mStackView == null) {
612 return;
613 }
614
615 final int suppressedEffects = zenModeConfig.suppressedVisualEffects;
616 final boolean hideNotificationDotsSelected =
617 (suppressedEffects & SUPPRESSED_EFFECT_BADGE) != 0;
618 final boolean dontPopNotifsOnScreenSelected =
619 (suppressedEffects & SUPPRESSED_EFFECT_PEEK) != 0;
620 final boolean hideFromPullDownShadeSelected =
621 (suppressedEffects & SUPPRESSED_EFFECT_NOTIFICATION_LIST) != 0;
622
623 final boolean dndEnabled = mZenModeController.getZen() != Settings.Global.ZEN_MODE_OFF;
624
625 mStackView.setSuppressNewDot(
626 dndEnabled && hideNotificationDotsSelected);
627 mStackView.setSuppressFlyout(
628 dndEnabled && (dontPopNotifsOnScreenSelected
629 || hideFromPullDownShadeSelected));
630 }
631
632 /**
633 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700634 * Updates the visibility of the bubbles based on current state.
635 * Does not un-bubble, just hides or un-hides. Notifies any
636 * {@link BubbleStateChangeListener}s of visibility changes.
637 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800638 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700639 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800640 if (mStackView == null) {
641 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800642 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800643 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
644 // Bubbles only appear in unlocked shade
645 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
646 } else if (mStackView != null) {
647 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800648 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700649
650 // Let listeners know if bubble state changed.
651 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
652 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
653 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
654 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
655 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
656 }
657
658 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800659 }
660
661 /**
662 * Rect indicating the touchable region for the bubble stack / expanded stack.
663 */
664 public Rect getTouchableRegion() {
665 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
666 return null;
667 }
668 mStackView.getBoundsOnScreen(mTempRect);
669 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800670 }
671
Mady Mellor390bff42019-04-05 15:09:01 -0700672 /**
673 * The display id of the expanded view, if the stack is expanded and not occluded by the
674 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
675 */
676 public int getExpandedDisplayId(Context context) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200677 final Bubble bubble = getExpandedBubble(context);
678 return bubble != null ? bubble.getDisplayId() : INVALID_DISPLAY;
679 }
680
681 @Nullable
682 private Bubble getExpandedBubble(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700683 if (mStackView == null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200684 return null;
Joel Galenson4071ddb2019-04-18 13:30:45 -0700685 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200686 final boolean defaultDisplay = context.getDisplay() != null
Mady Mellor390bff42019-04-05 15:09:01 -0700687 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
Issei Suzukicac2a502019-04-16 16:52:50 +0200688 final Bubble expandedBubble = mStackView.getExpandedBubble();
689 if (defaultDisplay && expandedBubble != null && isStackExpanded()
Mady Mellor390bff42019-04-05 15:09:01 -0700690 && !mStatusBarWindowController.getPanelExpanded()) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200691 return expandedBubble;
Mady Mellor390bff42019-04-05 15:09:01 -0700692 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200693 return null;
Mady Mellor390bff42019-04-05 15:09:01 -0700694 }
695
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800696 @VisibleForTesting
697 BubbleStackView getStackView() {
698 return mStackView;
699 }
700
Mady Mellor5549dd22018-11-06 18:07:34 -0800701 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800702 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800703 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800704 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800705 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800706 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800707 return false;
708 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800709 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800710
711 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
712 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
713 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
714
Mady Mellor5549dd22018-11-06 18:07:34 -0800715 boolean hasRemoteInput = false;
716 if (n.getNotification().actions != null) {
717 for (Notification.Action action : n.getNotification().actions) {
718 if (action.getRemoteInputs() != null) {
719 hasRemoteInput = true;
720 break;
721 }
722 }
723 }
Mady Mellor711f9562018-12-05 14:53:46 -0800724 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
725 && n.isOngoing();
726 boolean isMusic = n.getNotification().hasMediaSession();
727 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800728
Mady Mellor5549dd22018-11-06 18:07:34 -0800729 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800730 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
731 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800732 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800733 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800734 || autoBubbleAll;
735 }
736
Mady Mellor8a1f0252019-04-01 11:31:34 -0700737 private void updateShowInShadeForSuppressNotification(NotificationEntry entry) {
738 boolean suppressNotification = entry.getBubbleMetadata() != null
Mady Mellorc529d6d2019-04-16 14:22:52 -0700739 && entry.getBubbleMetadata().isNotificationSuppressed()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400740 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellor8a1f0252019-04-01 11:31:34 -0700741 entry.setShowInShadeWhenBubble(!suppressNotification);
742 }
743
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400744 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
745 StringBuilder sb = new StringBuilder();
746 for (Bubble bubble : bubbles) {
747 if (bubble == null) {
748 sb.append(" <null> !!!!!\n");
749 } else {
750 boolean isSelected = (bubble == selected);
751 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
752 ((isSelected) ? "->" : " "),
753 bubble.getLastActivity(),
754 (bubble.isOngoing() ? 1 : 0),
755 bubble.getKey()));
756 }
757 }
758 return sb.toString();
759 }
760
Mady Mellore80930e2019-03-21 16:00:45 -0700761 /**
762 * Return true if the applications with the package name is running in foreground.
763 *
Steven Wu8ba8ca92019-04-11 10:47:42 -0400764 * @param context application context.
Mady Mellore80930e2019-03-21 16:00:45 -0700765 * @param pkgName application package name.
766 */
Steven Wu8ba8ca92019-04-11 10:47:42 -0400767 public static boolean isForegroundApp(Context context, String pkgName) {
768 ActivityManager am = context.getSystemService(ActivityManager.class);
Mady Mellore80930e2019-03-21 16:00:45 -0700769 List<RunningTaskInfo> tasks = am.getRunningTasks(1 /* maxNum */);
770 return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
771 }
772
Mark Renoufcecc77b2019-01-30 16:32:24 -0500773 /**
774 * This task stack listener is responsible for responding to tasks moved to the front
775 * which are on the default (main) display. When this happens, expanded bubbles must be
776 * collapsed so the user may interact with the app which was just moved to the front.
777 * <p>
778 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
779 * these calls via a main thread Handler.
780 */
781 @MainThread
782 private class BubbleTaskStackListener extends TaskStackChangeListener {
783
Mark Renoufcecc77b2019-01-30 16:32:24 -0500784 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500785 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
786 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400787 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500788 }
789 }
790
Mark Renoufcecc77b2019-01-30 16:32:24 -0500791 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500792 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500793 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400794 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500795 }
796 }
Mark Renouf446251d2019-04-26 10:22:41 -0400797
798 @Override
799 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
800 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
801 mBubbleData.setExpanded(false);
802 }
803 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200804
805 @Override
806 public void onSingleTaskDisplayDrawn(int displayId) {
807 final Bubble expandedBubble = getExpandedBubble(mContext);
808 if (expandedBubble != null && expandedBubble.getDisplayId() == displayId) {
809 expandedBubble.setContentVisibility(true);
810 }
811 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500812 }
813
Mady Mellorceced172018-11-27 11:18:39 -0800814 private static boolean shouldAutoBubbleMessages(Context context) {
815 return Settings.Secure.getInt(context.getContentResolver(),
816 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
817 }
818
819 private static boolean shouldAutoBubbleOngoing(Context context) {
820 return Settings.Secure.getInt(context.getContentResolver(),
821 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
822 }
823
824 private static boolean shouldAutoBubbleAll(Context context) {
825 return Settings.Secure.getInt(context.getContentResolver(),
826 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800827 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500828
Mady Mellor3dff9e62019-02-05 18:12:53 -0800829 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500830 return Settings.Secure.getInt(context.getContentResolver(),
831 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
832 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800833
834 private static boolean areBubblesEnabled(Context context) {
835 return Settings.Secure.getInt(context.getContentResolver(),
836 ENABLE_BUBBLES, 1) != 0;
837 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500838
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500839 /** Default stiffness to use for bubble physics animations. */
840 public static int getBubbleStiffness(Context context, int defaultStiffness) {
841 return Settings.Secure.getInt(
842 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
843 }
844
845 /** Default bounciness/damping ratio to use for bubble physics animations. */
846 public static float getBubbleBounciness(Context context, float defaultBounciness) {
847 return Settings.Secure.getInt(
848 context.getContentResolver(),
849 BUBBLE_BOUNCINESS,
850 (int) (defaultBounciness * 100)) / 100f;
851 }
852
Mady Mellorca0c24c2019-05-16 16:14:32 -0700853 /**
854 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
855 *
856 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
857 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
858 *
859 * @param context the context to use.
860 * @param entry the entry to bubble.
861 */
862 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
863 PendingIntent intent = entry.getBubbleMetadata() != null
864 ? entry.getBubbleMetadata().getIntent()
865 : null;
866 if (intent == null) {
867 Log.w(TAG, "Unable to create bubble -- no intent");
868 return false;
869 }
870 ActivityInfo info =
871 intent.getIntent().resolveActivityInfo(context.getPackageManager(), 0);
872 if (info == null) {
873 Log.w(TAG, "Unable to send as bubble -- couldn't find activity info for intent: "
874 + intent);
875 return false;
876 }
877 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
878 Log.w(TAG, "Unable to send as bubble -- activity is not resizable for intent: "
879 + intent);
880 return false;
881 }
882 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
883 Log.w(TAG, "Unable to send as bubble -- activity is not documentLaunchMode=always "
884 + "for intent: " + intent);
885 return false;
886 }
887 if ((info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
888 Log.w(TAG, "Unable to send as bubble -- activity is not embeddable for intent: "
889 + intent);
890 return false;
891 }
892 return true;
893 }
894
Joshua Tsujia19515f2019-02-13 18:02:29 -0500895 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
896 private class BubblesImeListener extends IPinnedStackListener.Stub {
897
898 @Override
899 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
900 }
901
902 @Override
903 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
904 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
905 int displayRotation) throws RemoteException {}
906
907 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500908 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
909 if (mStackView != null && mStackView.getBubbleCount() > 0) {
910 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500911 }
912 }
913
914 @Override
915 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
916 throws RemoteException {}
917
918 @Override
919 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
920
921 @Override
922 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
923 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800924}