blob: e9ac8754f2e6cb23c71443c5748beb7394b9e6a0 [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
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040019import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE;
20import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST;
21import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
Mady Mellorca0c24c2019-05-16 16:14:32 -070022import static android.content.pm.ActivityInfo.DOCUMENT_LAUNCH_ALWAYS;
Mady Mellorc2ff0112019-03-28 14:18:06 -070023import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL;
24import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL;
25import static android.service.notification.NotificationListenerService.REASON_CANCEL;
26import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL;
Mady Mellor390bff42019-04-05 15:09:01 -070027import static android.view.Display.DEFAULT_DISPLAY;
28import static android.view.Display.INVALID_DISPLAY;
Mady Mellord1c78b262018-11-06 18:04:40 -080029import static android.view.View.INVISIBLE;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080030import static android.view.View.VISIBLE;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080031import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080032
Mady Mellor3f2efdb2018-11-21 11:30:45 -080033import static com.android.systemui.statusbar.StatusBarState.SHADE;
Mady Mellor1a4e86f2019-05-03 16:07:23 -070034import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080035
Mark Renoufba5ab512019-05-02 15:21:01 -040036import static java.lang.annotation.ElementType.FIELD;
37import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
38import static java.lang.annotation.ElementType.PARAMETER;
Mark Renouf08bc42a2019-03-07 13:01:59 -050039import static java.lang.annotation.RetentionPolicy.SOURCE;
40
Mady Mellorb4991e62019-01-10 15:14:51 -080041import android.annotation.Nullable;
Mady Mellore80930e2019-03-21 16:00:45 -070042import android.app.ActivityManager;
Mark Renoufc808f062019-02-07 15:20:37 -050043import android.app.ActivityManager.RunningTaskInfo;
Mark Renoufcecc77b2019-01-30 16:32:24 -050044import android.app.ActivityTaskManager;
45import android.app.IActivityTaskManager;
Mady Mellor5549dd22018-11-06 18:07:34 -080046import android.app.Notification;
Mady Mellor66efd5e2019-05-15 13:38:11 -070047import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070048import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080049import android.content.Context;
Mady Mellorca0c24c2019-05-16 16:14:32 -070050import android.content.pm.ActivityInfo;
Joshua Tsujia19515f2019-02-13 18:02:29 -050051import android.content.pm.ParceledListSlice;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040052import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080053import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050054import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080055import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080056import android.provider.Settings;
Mady Mellor5549dd22018-11-06 18:07:34 -080057import android.service.notification.StatusBarNotification;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040058import android.service.notification.ZenModeConfig;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040059import android.util.Log;
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;
68
Mady Mellorebdbbb92018-11-15 14:36:48 -080069import com.android.internal.annotations.VisibleForTesting;
Mady Mellora54e9fa2019-04-18 13:26:18 -070070import com.android.internal.statusbar.IStatusBarService;
Ned Burns01e38212019-01-03 16:32:52 -050071import com.android.systemui.Dependency;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080072import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050073import com.android.systemui.plugins.statusbar.StatusBarStateController;
Mark Renoufcecc77b2019-01-30 16:32:24 -050074import com.android.systemui.shared.system.ActivityManagerWrapper;
75import com.android.systemui.shared.system.TaskStackChangeListener;
Joshua Tsujia19515f2019-02-13 18:02:29 -050076import com.android.systemui.shared.system.WindowManagerWrapper;
Mady Mellorc2ff0112019-03-28 14:18:06 -070077import com.android.systemui.statusbar.NotificationRemoveInterceptor;
Ned Burns01e38212019-01-03 16:32:52 -050078import com.android.systemui.statusbar.notification.NotificationEntryListener;
79import com.android.systemui.statusbar.notification.NotificationEntryManager;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080080import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider;
Ned Burnsf81c4c42019-01-07 14:10:43 -050081import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050082import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080083import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lyn Hanf1c9b8b2019-03-14 16:49:48 -070084import com.android.systemui.statusbar.policy.ConfigurationController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040085import com.android.systemui.statusbar.policy.ZenModeController;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080086
Mark Renouf08bc42a2019-03-07 13:01:59 -050087import java.lang.annotation.Retention;
Mark Renoufba5ab512019-05-02 15:21:01 -040088import java.lang.annotation.Target;
Mady Mellore80930e2019-03-21 16:00:45 -070089import java.util.List;
Mark Renouf08bc42a2019-03-07 13:01:59 -050090
Jason Monk27d01a622018-12-10 15:57:09 -050091import javax.inject.Inject;
92import javax.inject.Singleton;
93
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080094/**
95 * Bubbles are a special type of content that can "float" on top of other apps or System UI.
96 * Bubbles can be expanded to show more content.
97 *
98 * The controller manages addition, removal, and visible state of bubbles on screen.
99 */
Jason Monk27d01a622018-12-10 15:57:09 -0500100@Singleton
Mark Renouf71a3af62019-04-08 15:02:54 -0400101public class BubbleController implements ConfigurationController.ConfigurationListener {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800102
103 private static final String TAG = "BubbleController";
Mark Renoufba5ab512019-05-02 15:21:01 -0400104 private static final boolean DEBUG = false;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800105
Mark Renouf08bc42a2019-03-07 13:01:59 -0500106 @Retention(SOURCE)
107 @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED,
Mady Melloraa8fef22019-04-11 13:36:40 -0700108 DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE})
Mark Renoufba5ab512019-05-02 15:21:01 -0400109 @Target({FIELD, LOCAL_VARIABLE, PARAMETER})
Mark Renouf08bc42a2019-03-07 13:01:59 -0500110 @interface DismissReason {}
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700111
Mark Renouf08bc42a2019-03-07 13:01:59 -0500112 static final int DISMISS_USER_GESTURE = 1;
113 static final int DISMISS_AGED = 2;
114 static final int DISMISS_TASK_FINISHED = 3;
115 static final int DISMISS_BLOCKED = 4;
116 static final int DISMISS_NOTIF_CANCEL = 5;
117 static final int DISMISS_ACCESSIBILITY_ACTION = 6;
Mady Melloraa8fef22019-04-11 13:36:40 -0700118 static final int DISMISS_NO_LONGER_BUBBLE = 7;
Mark Renouf08bc42a2019-03-07 13:01:59 -0500119
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400120 public static final int MAX_BUBBLES = 5; // TODO: actually enforce this
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -0400121
Mady Mellor5549dd22018-11-06 18:07:34 -0800122 // Enables some subset of notifs to automatically become bubbles
Mady Mellorca0c24c2019-05-16 16:14:32 -0700123 public static final boolean DEBUG_ENABLE_AUTO_BUBBLE = false;
Mady Mellor5549dd22018-11-06 18:07:34 -0800124
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800125 /** Flag to enable or disable the entire feature */
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800126 private static final String ENABLE_BUBBLES = "experiment_enable_bubbles";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800127 /** Auto bubble flags set whether different notif types should be presented as a bubble */
Mady Mellorceced172018-11-27 11:18:39 -0800128 private static final String ENABLE_AUTO_BUBBLE_MESSAGES = "experiment_autobubble_messaging";
129 private static final String ENABLE_AUTO_BUBBLE_ONGOING = "experiment_autobubble_ongoing";
130 private static final String ENABLE_AUTO_BUBBLE_ALL = "experiment_autobubble_all";
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800131
132 /** Use an activityView for an auto-bubbled notifs if it has an appropriate content intent */
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500133 private static final String ENABLE_BUBBLE_CONTENT_INTENT = "experiment_bubble_content_intent";
Mady Mellorceced172018-11-27 11:18:39 -0800134
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500135 private static final String BUBBLE_STIFFNESS = "experiment_bubble_stiffness";
136 private static final String BUBBLE_BOUNCINESS = "experiment_bubble_bounciness";
137
Ned Burns01e38212019-01-03 16:32:52 -0500138 private final Context mContext;
139 private final NotificationEntryManager mNotificationEntryManager;
Mark Renoufcecc77b2019-01-30 16:32:24 -0500140 private final IActivityTaskManager mActivityTaskManager;
141 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;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800147 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
252 mActivityTaskManager = ActivityTaskManager.getService();
253 mTaskStackListener = new BubbleTaskStackListener();
254 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800255
Joshua Tsujia19515f2019-02-13 18:02:29 -0500256 try {
257 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
258 } catch (RemoteException e) {
259 e.printStackTrace();
260 }
Issei Suzukic0387542019-03-08 17:31:14 +0100261 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700262
263 mBarService = IStatusBarService.Stub.asInterface(
264 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mady Mellor5549dd22018-11-06 18:07:34 -0800265 }
266
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400267 /**
268 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
269 * method initializes the stack view and adds it to the StatusBar just above the scrim.
270 */
271 private void ensureStackViewCreated() {
272 if (mStackView == null) {
273 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
274 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
275 // TODO(b/130237686): When you expand the shade on top of expanded bubble, there is no
276 // scrim between bubble and the shade
277 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
278 sbv.addView(mStackView, bubblePosition,
279 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
280 if (mExpandListener != null) {
281 mStackView.setExpandListener(mExpandListener);
282 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400283
284 updateStackViewForZenConfig();
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400285 }
286 }
287
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700288 @Override
289 public void onUiModeChanged() {
290 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700291 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700292 }
293 }
294
295 @Override
296 public void onOverlayChanged() {
297 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700298 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700299 }
300 }
301
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400302 @Override
303 public void onConfigChanged(Configuration newConfig) {
304 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
305 mStackView.onOrientationChanged();
306 mOrientation = newConfig.orientation;
307 }
308 }
309
Mady Mellor5549dd22018-11-06 18:07:34 -0800310 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800311 * Set a listener to be notified when some states of the bubbles change.
312 */
313 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
314 mStateChangeListener = listener;
315 }
316
317 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800318 * Set a listener to be notified of bubble expand events.
319 */
320 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100321 mExpandListener = ((isExpanding, key) -> {
322 if (listener != null) {
323 listener.onBubbleExpandChanged(isExpanding, key);
324 }
325 mStatusBarWindowController.setBubbleExpanded(isExpanding);
326 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800327 if (mStackView != null) {
328 mStackView.setExpandListener(mExpandListener);
329 }
330 }
331
332 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800333 * Whether or not there are bubbles present, regardless of them being visible on the
334 * screen (e.g. if on AOD).
335 */
336 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800337 if (mStackView == null) {
338 return false;
339 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400340 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800341 }
342
343 /**
344 * Whether the stack of bubbles is expanded or not.
345 */
346 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400347 return mBubbleData.isExpanded();
348 }
349
350 /**
351 * Tell the stack of bubbles to expand.
352 */
353 public void expandStack() {
354 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800355 }
356
357 /**
358 * Tell the stack of bubbles to collapse.
359 */
360 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400361 mBubbleData.setExpanded(false /* expanded */);
362 }
363
364 void selectBubble(Bubble bubble) {
365 mBubbleData.setSelectedBubble(bubble);
366 }
367
368 @VisibleForTesting
369 void selectBubble(String key) {
370 Bubble bubble = mBubbleData.getBubbleWithKey(key);
371 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800372 }
373
374 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400375 * Request the stack expand if needed, then select the specified Bubble as current.
376 *
377 * @param notificationKey the notification key for the bubble to be selected
378 */
379 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400380 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
381 if (bubble != null) {
382 mBubbleData.setSelectedBubble(bubble);
383 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400384 }
385 }
386
387 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800388 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
389 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500390 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400391 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800392 }
393
394 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500395 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
396 * is forwarded a back key down/up pair.
397 */
398 public void performBackPressIfNeeded() {
399 if (mStackView != null) {
400 mStackView.performBackPressIfNeeded();
401 }
402 }
403
404 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800405 * Adds or updates a bubble associated with the provided notification entry.
406 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400407 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800408 */
Mark Renouff97ed462019-04-05 13:46:24 -0400409 void updateBubble(NotificationEntry notif) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700410 // If this is an interruptive notif, mark that it's interrupted
411 if (notif.importance >= NotificationManager.IMPORTANCE_HIGH) {
412 notif.setInterruption();
413 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400414 mBubbleData.notificationEntryUpdated(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800415 }
416
417 /**
418 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500419 * <p>
420 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800421 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500422 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500423 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400424 // TEMP: refactor to change this to pass entry
425 Bubble bubble = mBubbleData.getBubbleWithKey(key);
426 if (bubble != null) {
427 mBubbleData.notificationEntryRemoved(bubble.entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800428 }
429 }
430
Ned Burns01e38212019-01-03 16:32:52 -0500431 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700432 private final NotificationRemoveInterceptor mRemoveInterceptor =
433 new NotificationRemoveInterceptor() {
434 @Override
435 public boolean onNotificationRemoveRequested(String key, int reason) {
436 if (!mBubbleData.hasBubbleWithKey(key)) {
437 return false;
438 }
439 NotificationEntry entry = mBubbleData.getBubbleWithKey(key).entry;
440
441 final boolean isClearAll = reason == REASON_CANCEL_ALL;
442 final boolean isUserDimiss = reason == REASON_CANCEL;
443 final boolean isAppCancel = reason == REASON_APP_CANCEL
444 || reason == REASON_APP_CANCEL_ALL;
445
446 // Need to check for !appCancel here because the notification may have
447 // previously been dismissed & entry.isRowDismissed would still be true
448 boolean userRemovedNotif = (entry.isRowDismissed() && !isAppCancel)
449 || isClearAll || isUserDimiss;
450
451 // The bubble notification sticks around in the data as long as the bubble is
452 // not dismissed and the app hasn't cancelled the notification.
453 boolean bubbleExtended = entry.isBubble() && !entry.isBubbleDismissed()
454 && userRemovedNotif;
455 if (bubbleExtended) {
456 entry.setShowInShadeWhenBubble(false);
457 if (mStackView != null) {
458 mStackView.updateDotVisibility(entry.key);
459 }
460 mNotificationEntryManager.updateNotifications();
461 return true;
462 } else if (!userRemovedNotif && !entry.isBubbleDismissed()) {
463 // This wasn't a user removal so we should remove the bubble as well
464 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
465 return false;
466 }
467 return false;
468 }
469 };
470
471 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500472 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
473 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500474 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800475 if (!areBubblesEnabled(mContext)) {
476 return;
477 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700478 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
479 && canLaunchInActivityView(mContext, entry)) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700480 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800481 }
482 }
483
484 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500485 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800486 if (!areBubblesEnabled(mContext)) {
487 return;
488 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700489 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
490 && canLaunchInActivityView(mContext, entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400491 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800492 }
493 }
494
495 @Override
496 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800497 if (!areBubblesEnabled(mContext)) {
498 return;
499 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700500 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
501 && canLaunchInActivityView(mContext, entry);
Mady Melloraa8fef22019-04-11 13:36:40 -0700502 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.key)) {
503 // It was previously a bubble but no longer a bubble -- lets remove it
504 removeBubble(entry.key, DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700505 } else if (shouldBubble) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700506 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800507 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mark Renouff97ed462019-04-05 13:46:24 -0400508 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800509 }
510 }
Ned Burns01e38212019-01-03 16:32:52 -0500511 };
512
Mark Renouf71a3af62019-04-08 15:02:54 -0400513 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400514 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400515
Mark Renouf3bc5b362019-04-05 14:37:59 -0400516 @Override
517 public void onBubbleAdded(Bubble bubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400518 ensureStackViewCreated();
519 mStackView.addBubble(bubble);
Mark Renouf3bc5b362019-04-05 14:37:59 -0400520 }
521
522 @Override
Mady Mellorc2ff0112019-03-28 14:18:06 -0700523 public void onBubbleRemoved(Bubble bubble, @DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400524 if (mStackView != null) {
525 mStackView.removeBubble(bubble);
526 }
Mady Mellorc2ff0112019-03-28 14:18:06 -0700527 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
528 && !bubble.entry.showInShadeWhenBubble()) {
529 // The bubble is gone & the notification is gone, time to actually remove it
530 mNotificationEntryManager.performRemoveNotification(bubble.entry.notification,
Mady Mellor1a4e86f2019-05-03 16:07:23 -0700531 UNDEFINED_DISMISS_REASON);
Mady Mellora54e9fa2019-04-18 13:26:18 -0700532 } else {
533 // The notification is still in the shade but we've removed the bubble so
534 // lets make sure NoMan knows it's not a bubble anymore
535 try {
536 mBarService.onNotificationBubbleChanged(bubble.getKey(), false /* isBubble */);
537 } catch (RemoteException e) {
538 // Bad things have happened
539 }
540 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400541 }
542
543 public void onBubbleUpdated(Bubble bubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400544 if (mStackView != null) {
545 mStackView.updateBubble(bubble);
546 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400547 }
548
549 @Override
550 public void onOrderChanged(List<Bubble> bubbles) {
Mark Renoufba5ab512019-05-02 15:21:01 -0400551 if (mStackView != null) {
552 mStackView.updateBubbleOrder(bubbles);
553 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400554 }
555
556 @Override
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400557 public void onSelectionChanged(@Nullable Bubble selectedBubble) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400558 if (mStackView != null) {
559 mStackView.setSelectedBubble(selectedBubble);
560 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400561 }
562
563 @Override
564 public void onExpandedChanged(boolean expanded) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400565 if (mStackView != null) {
566 mStackView.setExpanded(expanded);
567 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400568 }
569
Lyn Han6c40fe72019-05-08 14:06:33 -0700570 // Runs on state change.
Mark Renouf3bc5b362019-04-05 14:37:59 -0400571 @Override
Mark Renouf3bc5b362019-04-05 14:37:59 -0400572 public void apply() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400573 mNotificationEntryManager.updateNotifications();
Lyn Han6c40fe72019-05-08 14:06:33 -0700574 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400575
576 if (DEBUG) {
577 Log.d(TAG, "[BubbleData]");
578 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
579 mBubbleData.getSelectedBubble()));
580
581 if (mStackView != null) {
582 Log.d(TAG, "[BubbleStackView]");
583 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
584 mStackView.getExpandedBubble()));
585 }
586 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400587 }
588 };
589
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800590 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400591 * Updates the stack view's suppression flags from the latest config from the zen (do not
592 * disturb) controller.
593 */
594 private void updateStackViewForZenConfig() {
595 final ZenModeConfig zenModeConfig = mZenModeController.getConfig();
596
597 if (zenModeConfig == null || mStackView == null) {
598 return;
599 }
600
601 final int suppressedEffects = zenModeConfig.suppressedVisualEffects;
602 final boolean hideNotificationDotsSelected =
603 (suppressedEffects & SUPPRESSED_EFFECT_BADGE) != 0;
604 final boolean dontPopNotifsOnScreenSelected =
605 (suppressedEffects & SUPPRESSED_EFFECT_PEEK) != 0;
606 final boolean hideFromPullDownShadeSelected =
607 (suppressedEffects & SUPPRESSED_EFFECT_NOTIFICATION_LIST) != 0;
608
609 final boolean dndEnabled = mZenModeController.getZen() != Settings.Global.ZEN_MODE_OFF;
610
611 mStackView.setSuppressNewDot(
612 dndEnabled && hideNotificationDotsSelected);
613 mStackView.setSuppressFlyout(
614 dndEnabled && (dontPopNotifsOnScreenSelected
615 || hideFromPullDownShadeSelected));
616 }
617
618 /**
619 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700620 * Updates the visibility of the bubbles based on current state.
621 * Does not un-bubble, just hides or un-hides. Notifies any
622 * {@link BubbleStateChangeListener}s of visibility changes.
623 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800624 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700625 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800626 if (mStackView == null) {
627 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800628 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800629 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
630 // Bubbles only appear in unlocked shade
631 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
632 } else if (mStackView != null) {
633 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800634 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700635
636 // Let listeners know if bubble state changed.
637 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
638 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
639 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
640 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
641 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
642 }
643
644 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800645 }
646
647 /**
648 * Rect indicating the touchable region for the bubble stack / expanded stack.
649 */
650 public Rect getTouchableRegion() {
651 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
652 return null;
653 }
654 mStackView.getBoundsOnScreen(mTempRect);
655 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800656 }
657
Mady Mellor390bff42019-04-05 15:09:01 -0700658 /**
659 * The display id of the expanded view, if the stack is expanded and not occluded by the
660 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
661 */
662 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700663 if (mStackView == null) {
664 return INVALID_DISPLAY;
665 }
Mady Mellor390bff42019-04-05 15:09:01 -0700666 boolean defaultDisplay = context.getDisplay() != null
667 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
668 Bubble b = mStackView.getExpandedBubble();
669 if (defaultDisplay && b != null && isStackExpanded()
670 && !mStatusBarWindowController.getPanelExpanded()) {
671 return b.expandedView.getVirtualDisplayId();
672 }
673 return INVALID_DISPLAY;
674 }
675
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800676 @VisibleForTesting
677 BubbleStackView getStackView() {
678 return mStackView;
679 }
680
Mady Mellor5549dd22018-11-06 18:07:34 -0800681 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800682 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800683 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800684 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800685 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800686 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800687 return false;
688 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800689 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800690
691 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
692 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
693 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
694
Mady Mellor5549dd22018-11-06 18:07:34 -0800695 boolean hasRemoteInput = false;
696 if (n.getNotification().actions != null) {
697 for (Notification.Action action : n.getNotification().actions) {
698 if (action.getRemoteInputs() != null) {
699 hasRemoteInput = true;
700 break;
701 }
702 }
703 }
Mady Mellor711f9562018-12-05 14:53:46 -0800704 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
705 && n.isOngoing();
706 boolean isMusic = n.getNotification().hasMediaSession();
707 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800708
Mady Mellor5549dd22018-11-06 18:07:34 -0800709 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800710 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
711 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800712 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800713 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800714 || autoBubbleAll;
715 }
716
Mady Mellor8a1f0252019-04-01 11:31:34 -0700717 private void updateShowInShadeForSuppressNotification(NotificationEntry entry) {
718 boolean suppressNotification = entry.getBubbleMetadata() != null
Mady Mellorc529d6d2019-04-16 14:22:52 -0700719 && entry.getBubbleMetadata().isNotificationSuppressed()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400720 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellor8a1f0252019-04-01 11:31:34 -0700721 entry.setShowInShadeWhenBubble(!suppressNotification);
722 }
723
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400724 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
725 StringBuilder sb = new StringBuilder();
726 for (Bubble bubble : bubbles) {
727 if (bubble == null) {
728 sb.append(" <null> !!!!!\n");
729 } else {
730 boolean isSelected = (bubble == selected);
731 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
732 ((isSelected) ? "->" : " "),
733 bubble.getLastActivity(),
734 (bubble.isOngoing() ? 1 : 0),
735 bubble.getKey()));
736 }
737 }
738 return sb.toString();
739 }
740
Mady Mellore80930e2019-03-21 16:00:45 -0700741 /**
742 * Return true if the applications with the package name is running in foreground.
743 *
Steven Wu8ba8ca92019-04-11 10:47:42 -0400744 * @param context application context.
Mady Mellore80930e2019-03-21 16:00:45 -0700745 * @param pkgName application package name.
746 */
Steven Wu8ba8ca92019-04-11 10:47:42 -0400747 public static boolean isForegroundApp(Context context, String pkgName) {
748 ActivityManager am = context.getSystemService(ActivityManager.class);
Mady Mellore80930e2019-03-21 16:00:45 -0700749 List<RunningTaskInfo> tasks = am.getRunningTasks(1 /* maxNum */);
750 return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
751 }
752
Mark Renoufcecc77b2019-01-30 16:32:24 -0500753 /**
754 * This task stack listener is responsible for responding to tasks moved to the front
755 * which are on the default (main) display. When this happens, expanded bubbles must be
756 * collapsed so the user may interact with the app which was just moved to the front.
757 * <p>
758 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
759 * these calls via a main thread Handler.
760 */
761 @MainThread
762 private class BubbleTaskStackListener extends TaskStackChangeListener {
763
Mark Renoufcecc77b2019-01-30 16:32:24 -0500764 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500765 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
766 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400767 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500768 }
769 }
770
Mark Renoufcecc77b2019-01-30 16:32:24 -0500771 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500772 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500773 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400774 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500775 }
776 }
Mark Renouf446251d2019-04-26 10:22:41 -0400777
778 @Override
779 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
780 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
781 mBubbleData.setExpanded(false);
782 }
783 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500784 }
785
Mady Mellorceced172018-11-27 11:18:39 -0800786 private static boolean shouldAutoBubbleMessages(Context context) {
787 return Settings.Secure.getInt(context.getContentResolver(),
788 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
789 }
790
791 private static boolean shouldAutoBubbleOngoing(Context context) {
792 return Settings.Secure.getInt(context.getContentResolver(),
793 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
794 }
795
796 private static boolean shouldAutoBubbleAll(Context context) {
797 return Settings.Secure.getInt(context.getContentResolver(),
798 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800799 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500800
Mady Mellor3dff9e62019-02-05 18:12:53 -0800801 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500802 return Settings.Secure.getInt(context.getContentResolver(),
803 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
804 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800805
806 private static boolean areBubblesEnabled(Context context) {
807 return Settings.Secure.getInt(context.getContentResolver(),
808 ENABLE_BUBBLES, 1) != 0;
809 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500810
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500811 /** Default stiffness to use for bubble physics animations. */
812 public static int getBubbleStiffness(Context context, int defaultStiffness) {
813 return Settings.Secure.getInt(
814 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
815 }
816
817 /** Default bounciness/damping ratio to use for bubble physics animations. */
818 public static float getBubbleBounciness(Context context, float defaultBounciness) {
819 return Settings.Secure.getInt(
820 context.getContentResolver(),
821 BUBBLE_BOUNCINESS,
822 (int) (defaultBounciness * 100)) / 100f;
823 }
824
Mady Mellorca0c24c2019-05-16 16:14:32 -0700825 /**
826 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
827 *
828 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
829 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
830 *
831 * @param context the context to use.
832 * @param entry the entry to bubble.
833 */
834 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
835 PendingIntent intent = entry.getBubbleMetadata() != null
836 ? entry.getBubbleMetadata().getIntent()
837 : null;
838 if (intent == null) {
839 Log.w(TAG, "Unable to create bubble -- no intent");
840 return false;
841 }
842 ActivityInfo info =
843 intent.getIntent().resolveActivityInfo(context.getPackageManager(), 0);
844 if (info == null) {
845 Log.w(TAG, "Unable to send as bubble -- couldn't find activity info for intent: "
846 + intent);
847 return false;
848 }
849 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
850 Log.w(TAG, "Unable to send as bubble -- activity is not resizable for intent: "
851 + intent);
852 return false;
853 }
854 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
855 Log.w(TAG, "Unable to send as bubble -- activity is not documentLaunchMode=always "
856 + "for intent: " + intent);
857 return false;
858 }
859 if ((info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
860 Log.w(TAG, "Unable to send as bubble -- activity is not embeddable for intent: "
861 + intent);
862 return false;
863 }
864 return true;
865 }
866
Joshua Tsujia19515f2019-02-13 18:02:29 -0500867 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
868 private class BubblesImeListener extends IPinnedStackListener.Stub {
869
870 @Override
871 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
872 }
873
874 @Override
875 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
876 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
877 int displayRotation) throws RemoteException {}
878
879 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500880 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
881 if (mStackView != null && mStackView.getBubbleCount() > 0) {
882 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500883 }
884 }
885
886 @Override
887 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
888 throws RemoteException {}
889
890 @Override
891 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
892
893 @Override
894 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
895 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800896}