blob: ea5f2bfc0d3cc12199a516dd20a972a91390d9a0 [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 Mellore80930e2019-03-21 16:00:45 -070041import android.app.ActivityManager;
Mark Renoufc808f062019-02-07 15:20:37 -050042import android.app.ActivityManager.RunningTaskInfo;
Mady Mellor5549dd22018-11-06 18:07:34 -080043import android.app.Notification;
Mady Mellor66efd5e2019-05-15 13:38:11 -070044import android.app.NotificationManager;
Mady Mellorca0c24c2019-05-16 16:14:32 -070045import android.app.PendingIntent;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080046import android.content.Context;
Mady Mellorca0c24c2019-05-16 16:14:32 -070047import android.content.pm.ActivityInfo;
Joshua Tsujia19515f2019-02-13 18:02:29 -050048import android.content.pm.ParceledListSlice;
Joshua Tsujif418f9e2019-04-04 17:09:53 -040049import android.content.res.Configuration;
Mady Mellord1c78b262018-11-06 18:04:40 -080050import android.graphics.Rect;
Mark Renoufcecc77b2019-01-30 16:32:24 -050051import android.os.RemoteException;
Mady Mellorb4991e62019-01-10 15:14:51 -080052import android.os.ServiceManager;
Mady Mellorceced172018-11-27 11:18:39 -080053import android.provider.Settings;
Mark Renoufbbcf07f2019-05-09 10:42:43 -040054import android.service.notification.NotificationListenerService.RankingMap;
Mady Mellor5549dd22018-11-06 18:07:34 -080055import android.service.notification.StatusBarNotification;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -040056import android.service.notification.ZenModeConfig;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040057import android.util.Log;
Mark Renouf82a40e62019-05-23 16:16:24 -040058import android.util.Pair;
Mark Renoufcecc77b2019-01-30 16:32:24 -050059import android.view.Display;
Joshua Tsujia19515f2019-02-13 18:02:29 -050060import android.view.IPinnedStackController;
61import android.view.IPinnedStackListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080062import android.view.ViewGroup;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080063import android.widget.FrameLayout;
64
Mark Renouf08bc42a2019-03-07 13:01:59 -050065import androidx.annotation.IntDef;
Mark Renouf658c6bc2019-01-30 10:26:54 -050066import androidx.annotation.MainThread;
Joshua Tsujic650a142019-05-22 11:31:19 -040067import androidx.annotation.Nullable;
Mark Renouf658c6bc2019-01-30 10:26:54 -050068
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 BubbleTaskStackListener mTaskStackListener;
Mady Mellord1c78b262018-11-06 18:04:40 -0800141 private BubbleStateChangeListener mStateChangeListener;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800142 private BubbleExpandListener mExpandListener;
Issei Suzukic0387542019-03-08 17:31:14 +0100143 @Nullable private BubbleStackView.SurfaceSynchronizer mSurfaceSynchronizer;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800144
Mady Mellor3dff9e62019-02-05 18:12:53 -0800145 private BubbleData mBubbleData;
Joshua Tsujic650a142019-05-22 11:31:19 -0400146 @Nullable private BubbleStackView mStackView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800147
148 // Bubbles get added to the status bar view
Ned Burns01e38212019-01-03 16:32:52 -0500149 private final StatusBarWindowController mStatusBarWindowController;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400150 private final ZenModeController mZenModeController;
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800151 private StatusBarStateListener mStatusBarStateListener;
152
Mady Melloraa8fef22019-04-11 13:36:40 -0700153 private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700154 private IStatusBarService mBarService;
Mady Mellorb4991e62019-01-10 15:14:51 -0800155
Mady Mellord1c78b262018-11-06 18:04:40 -0800156 // Used for determining view rect for touch interaction
157 private Rect mTempRect = new Rect();
158
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400159 /** Last known orientation, used to detect orientation changes in {@link #onConfigChanged}. */
160 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
161
Mady Mellor5549dd22018-11-06 18:07:34 -0800162 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800163 * Listener to be notified when some states of the bubbles change.
164 */
165 public interface BubbleStateChangeListener {
166 /**
167 * Called when the stack has bubbles or no longer has bubbles.
168 */
169 void onHasBubblesChanged(boolean hasBubbles);
170 }
171
Mady Mellorcd9b1302018-11-06 18:08:04 -0800172 /**
173 * Listener to find out about stack expansion / collapse events.
174 */
175 public interface BubbleExpandListener {
176 /**
177 * Called when the expansion state of the bubble stack changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700178 *
Mady Mellorcd9b1302018-11-06 18:08:04 -0800179 * @param isExpanding whether it's expanding or collapsing
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800180 * @param key the notification key associated with bubble being expanded
Mady Mellorcd9b1302018-11-06 18:08:04 -0800181 */
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800182 void onBubbleExpandChanged(boolean isExpanding, String key);
183 }
184
185 /**
186 * Listens for the current state of the status bar and updates the visibility state
187 * of bubbles as needed.
188 */
189 private class StatusBarStateListener implements StatusBarStateController.StateListener {
190 private int mState;
191 /**
192 * Returns the current status bar state.
193 */
194 public int getCurrentState() {
195 return mState;
196 }
197
198 @Override
199 public void onStateChanged(int newState) {
200 mState = newState;
Mark Renouf71a3af62019-04-08 15:02:54 -0400201 boolean shouldCollapse = (mState != SHADE);
202 if (shouldCollapse) {
203 collapseStack();
204 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700205 updateStack();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800206 }
Mady Mellorcd9b1302018-11-06 18:08:04 -0800207 }
208
Jason Monk27d01a622018-12-10 15:57:09 -0500209 @Inject
Mady Mellorcfd06c12019-02-13 14:32:12 -0800210 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Mady Melloraa8fef22019-04-11 13:36:40 -0700211 BubbleData data, ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400212 NotificationInterruptionStateProvider interruptionStateProvider,
213 ZenModeController zenModeController) {
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700214 this(context, statusBarWindowController, data, null /* synchronizer */,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400215 configurationController, interruptionStateProvider, zenModeController);
Issei Suzukic0387542019-03-08 17:31:14 +0100216 }
217
218 public BubbleController(Context context, StatusBarWindowController statusBarWindowController,
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700219 BubbleData data, @Nullable BubbleStackView.SurfaceSynchronizer synchronizer,
Mady Melloraa8fef22019-04-11 13:36:40 -0700220 ConfigurationController configurationController,
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400221 NotificationInterruptionStateProvider interruptionStateProvider,
222 ZenModeController zenModeController) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800223 mContext = context;
Mady Melloraa8fef22019-04-11 13:36:40 -0700224 mNotificationInterruptionStateProvider = interruptionStateProvider;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400225 mZenModeController = zenModeController;
226 mZenModeController.addCallback(new ZenModeController.Callback() {
227 @Override
228 public void onZenChanged(int zen) {
229 updateStackViewForZenConfig();
230 }
231
232 @Override
233 public void onConfigChanged(ZenModeConfig config) {
234 updateStackViewForZenConfig();
235 }
236 });
Mady Melloraa8fef22019-04-11 13:36:40 -0700237
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700238 configurationController.addCallback(this /* configurationListener */);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800239
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400240 mBubbleData = data;
241 mBubbleData.setListener(mBubbleDataListener);
242
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800243 mNotificationEntryManager = Dependency.get(NotificationEntryManager.class);
Ned Burns01e38212019-01-03 16:32:52 -0500244 mNotificationEntryManager.addNotificationEntryListener(mEntryListener);
Mady Mellorc2ff0112019-03-28 14:18:06 -0700245 mNotificationEntryManager.setNotificationRemoveInterceptor(mRemoveInterceptor);
Mady Mellorb4991e62019-01-10 15:14:51 -0800246
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800247 mStatusBarWindowController = statusBarWindowController;
248 mStatusBarStateListener = new StatusBarStateListener();
249 Dependency.get(StatusBarStateController.class).addCallback(mStatusBarStateListener);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500250
Mark Renoufcecc77b2019-01-30 16:32:24 -0500251 mTaskStackListener = new BubbleTaskStackListener();
252 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800253
Joshua Tsujia19515f2019-02-13 18:02:29 -0500254 try {
255 WindowManagerWrapper.getInstance().addPinnedStackListener(new BubblesImeListener());
256 } catch (RemoteException e) {
257 e.printStackTrace();
258 }
Issei Suzukic0387542019-03-08 17:31:14 +0100259 mSurfaceSynchronizer = synchronizer;
Mady Mellora54e9fa2019-04-18 13:26:18 -0700260
261 mBarService = IStatusBarService.Stub.asInterface(
262 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
Mady Mellor5549dd22018-11-06 18:07:34 -0800263 }
264
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400265 /**
266 * BubbleStackView is lazily created by this method the first time a Bubble is added. This
267 * method initializes the stack view and adds it to the StatusBar just above the scrim.
268 */
269 private void ensureStackViewCreated() {
270 if (mStackView == null) {
271 mStackView = new BubbleStackView(mContext, mBubbleData, mSurfaceSynchronizer);
272 ViewGroup sbv = mStatusBarWindowController.getStatusBarView();
273 // TODO(b/130237686): When you expand the shade on top of expanded bubble, there is no
274 // scrim between bubble and the shade
275 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
276 sbv.addView(mStackView, bubblePosition,
277 new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
278 if (mExpandListener != null) {
279 mStackView.setExpandListener(mExpandListener);
280 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400281
282 updateStackViewForZenConfig();
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400283 }
284 }
285
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700286 @Override
287 public void onUiModeChanged() {
288 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700289 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700290 }
291 }
292
293 @Override
294 public void onOverlayChanged() {
295 if (mStackView != null) {
Lyn Han02cca812019-04-02 16:27:32 -0700296 mStackView.onThemeChanged();
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700297 }
298 }
299
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400300 @Override
301 public void onConfigChanged(Configuration newConfig) {
302 if (mStackView != null && newConfig != null && newConfig.orientation != mOrientation) {
303 mStackView.onOrientationChanged();
304 mOrientation = newConfig.orientation;
305 }
306 }
307
Mady Mellor5549dd22018-11-06 18:07:34 -0800308 /**
Mady Mellord1c78b262018-11-06 18:04:40 -0800309 * Set a listener to be notified when some states of the bubbles change.
310 */
311 public void setBubbleStateChangeListener(BubbleStateChangeListener listener) {
312 mStateChangeListener = listener;
313 }
314
315 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800316 * Set a listener to be notified of bubble expand events.
317 */
318 public void setExpandListener(BubbleExpandListener listener) {
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100319 mExpandListener = ((isExpanding, key) -> {
320 if (listener != null) {
321 listener.onBubbleExpandChanged(isExpanding, key);
322 }
323 mStatusBarWindowController.setBubbleExpanded(isExpanding);
324 });
Mady Mellorcd9b1302018-11-06 18:08:04 -0800325 if (mStackView != null) {
326 mStackView.setExpandListener(mExpandListener);
327 }
328 }
329
330 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800331 * Whether or not there are bubbles present, regardless of them being visible on the
332 * screen (e.g. if on AOD).
333 */
334 public boolean hasBubbles() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800335 if (mStackView == null) {
336 return false;
337 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400338 return mBubbleData.hasBubbles();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800339 }
340
341 /**
342 * Whether the stack of bubbles is expanded or not.
343 */
344 public boolean isStackExpanded() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400345 return mBubbleData.isExpanded();
346 }
347
348 /**
349 * Tell the stack of bubbles to expand.
350 */
351 public void expandStack() {
352 mBubbleData.setExpanded(true);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800353 }
354
355 /**
356 * Tell the stack of bubbles to collapse.
357 */
358 public void collapseStack() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400359 mBubbleData.setExpanded(false /* expanded */);
360 }
361
362 void selectBubble(Bubble bubble) {
363 mBubbleData.setSelectedBubble(bubble);
364 }
365
366 @VisibleForTesting
367 void selectBubble(String key) {
368 Bubble bubble = mBubbleData.getBubbleWithKey(key);
369 selectBubble(bubble);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800370 }
371
372 /**
Mark Renouffec45da2019-03-13 13:24:27 -0400373 * Request the stack expand if needed, then select the specified Bubble as current.
374 *
375 * @param notificationKey the notification key for the bubble to be selected
376 */
377 public void expandStackAndSelectBubble(String notificationKey) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400378 Bubble bubble = mBubbleData.getBubbleWithKey(notificationKey);
379 if (bubble != null) {
380 mBubbleData.setSelectedBubble(bubble);
381 mBubbleData.setExpanded(true);
Mark Renouffec45da2019-03-13 13:24:27 -0400382 }
383 }
384
385 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800386 * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack.
387 */
Mark Renouf08bc42a2019-03-07 13:01:59 -0500388 void dismissStack(@DismissReason int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400389 mBubbleData.dismissAll(reason);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800390 }
391
392 /**
Mark Renouf041d7262019-02-06 12:09:41 -0500393 * Directs a back gesture at the bubble stack. When opened, the current expanded bubble
394 * is forwarded a back key down/up pair.
395 */
396 public void performBackPressIfNeeded() {
397 if (mStackView != null) {
398 mStackView.performBackPressIfNeeded();
399 }
400 }
401
402 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800403 * Adds or updates a bubble associated with the provided notification entry.
404 *
Mark Renouf8b6a3c62019-04-09 10:17:40 -0400405 * @param notif the notification associated with this bubble.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800406 */
Mark Renouff97ed462019-04-05 13:46:24 -0400407 void updateBubble(NotificationEntry notif) {
Mady Mellor66efd5e2019-05-15 13:38:11 -0700408 // If this is an interruptive notif, mark that it's interrupted
409 if (notif.importance >= NotificationManager.IMPORTANCE_HIGH) {
410 notif.setInterruption();
411 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400412 mBubbleData.notificationEntryUpdated(notif);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800413 }
414
415 /**
416 * Removes the bubble associated with the {@param uri}.
Mark Renouf658c6bc2019-01-30 10:26:54 -0500417 * <p>
418 * Must be called from the main thread.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800419 */
Mark Renouf658c6bc2019-01-30 10:26:54 -0500420 @MainThread
Mark Renouf08bc42a2019-03-07 13:01:59 -0500421 void removeBubble(String key, int reason) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400422 // TEMP: refactor to change this to pass entry
423 Bubble bubble = mBubbleData.getBubbleWithKey(key);
424 if (bubble != null) {
425 mBubbleData.notificationEntryRemoved(bubble.entry, reason);
Mady Mellore8e07712019-01-23 12:45:33 -0800426 }
427 }
428
Ned Burns01e38212019-01-03 16:32:52 -0500429 @SuppressWarnings("FieldCanBeLocal")
Mady Mellorc2ff0112019-03-28 14:18:06 -0700430 private final NotificationRemoveInterceptor mRemoveInterceptor =
431 new NotificationRemoveInterceptor() {
432 @Override
433 public boolean onNotificationRemoveRequested(String key, int reason) {
434 if (!mBubbleData.hasBubbleWithKey(key)) {
435 return false;
436 }
437 NotificationEntry entry = mBubbleData.getBubbleWithKey(key).entry;
438
439 final boolean isClearAll = reason == REASON_CANCEL_ALL;
440 final boolean isUserDimiss = reason == REASON_CANCEL;
441 final boolean isAppCancel = reason == REASON_APP_CANCEL
442 || reason == REASON_APP_CANCEL_ALL;
443
444 // Need to check for !appCancel here because the notification may have
445 // previously been dismissed & entry.isRowDismissed would still be true
446 boolean userRemovedNotif = (entry.isRowDismissed() && !isAppCancel)
447 || isClearAll || isUserDimiss;
448
449 // The bubble notification sticks around in the data as long as the bubble is
450 // not dismissed and the app hasn't cancelled the notification.
451 boolean bubbleExtended = entry.isBubble() && !entry.isBubbleDismissed()
452 && userRemovedNotif;
453 if (bubbleExtended) {
454 entry.setShowInShadeWhenBubble(false);
455 if (mStackView != null) {
456 mStackView.updateDotVisibility(entry.key);
457 }
458 mNotificationEntryManager.updateNotifications();
459 return true;
460 } else if (!userRemovedNotif && !entry.isBubbleDismissed()) {
461 // This wasn't a user removal so we should remove the bubble as well
462 mBubbleData.notificationEntryRemoved(entry, DISMISS_NOTIF_CANCEL);
463 return false;
464 }
465 return false;
466 }
467 };
468
469 @SuppressWarnings("FieldCanBeLocal")
Ned Burns01e38212019-01-03 16:32:52 -0500470 private final NotificationEntryListener mEntryListener = new NotificationEntryListener() {
471 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500472 public void onPendingEntryAdded(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800473 if (!areBubblesEnabled(mContext)) {
474 return;
475 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700476 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
477 && canLaunchInActivityView(mContext, entry)) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700478 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800479 }
480 }
481
482 @Override
Ned Burns1a5e22f2019-02-14 15:11:52 -0500483 public void onEntryInflated(NotificationEntry entry, @InflationFlag int inflatedFlags) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800484 if (!areBubblesEnabled(mContext)) {
485 return;
486 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700487 if (mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
488 && canLaunchInActivityView(mContext, entry)) {
Mark Renouff97ed462019-04-05 13:46:24 -0400489 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800490 }
491 }
492
493 @Override
494 public void onPreEntryUpdated(NotificationEntry entry) {
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800495 if (!areBubblesEnabled(mContext)) {
496 return;
497 }
Mady Mellorca0c24c2019-05-16 16:14:32 -0700498 boolean shouldBubble = mNotificationInterruptionStateProvider.shouldBubbleUp(entry)
499 && canLaunchInActivityView(mContext, entry);
Mady Melloraa8fef22019-04-11 13:36:40 -0700500 if (!shouldBubble && mBubbleData.hasBubbleWithKey(entry.key)) {
501 // It was previously a bubble but no longer a bubble -- lets remove it
502 removeBubble(entry.key, DISMISS_NO_LONGER_BUBBLE);
Mady Mellorff40e012019-05-03 15:34:41 -0700503 } else if (shouldBubble) {
Mady Mellor8a1f0252019-04-01 11:31:34 -0700504 updateShowInShadeForSuppressNotification(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800505 entry.setBubbleDismissed(false); // updates come back as bubbles even if dismissed
Mark Renouff97ed462019-04-05 13:46:24 -0400506 updateBubble(entry);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800507 }
508 }
Mark Renoufbbcf07f2019-05-09 10:42:43 -0400509
510 @Override
511 public void onNotificationRankingUpdated(RankingMap rankingMap) {
512 // Forward to BubbleData to block any bubbles which should no longer be shown
513 mBubbleData.notificationRankingUpdated(rankingMap);
514 }
Ned Burns01e38212019-01-03 16:32:52 -0500515 };
516
Mark Renouf71a3af62019-04-08 15:02:54 -0400517 @SuppressWarnings("FieldCanBeLocal")
Mark Renouf3bc5b362019-04-05 14:37:59 -0400518 private final BubbleData.Listener mBubbleDataListener = new BubbleData.Listener() {
Mark Renouf71a3af62019-04-08 15:02:54 -0400519
Mark Renouf3bc5b362019-04-05 14:37:59 -0400520 @Override
Mark Renouf82a40e62019-05-23 16:16:24 -0400521 public void applyUpdate(BubbleData.Update update) {
522 if (mStackView == null && update.addedBubble != null) {
523 // Lazy init stack view when the first bubble is added.
524 ensureStackViewCreated();
Mark Renouf71a3af62019-04-08 15:02:54 -0400525 }
Mark Renouf82a40e62019-05-23 16:16:24 -0400526
527 // If not yet initialized, ignore all other changes.
528 if (mStackView == null) {
529 return;
530 }
531
532 if (update.addedBubble != null) {
533 mStackView.addBubble(update.addedBubble);
534 }
535
536 // Collapsing? Do this first before remaining steps.
537 if (update.expandedChanged && !update.expanded) {
538 mStackView.setExpanded(false);
539 }
540
541 // Do removals, if any.
542 for (Pair<Bubble, Integer> removed : update.removedBubbles) {
543 final Bubble bubble = removed.first;
544 @DismissReason final int reason = removed.second;
545 mStackView.removeBubble(bubble);
546
547 if (!mBubbleData.hasBubbleWithKey(bubble.getKey())
548 && !bubble.entry.showInShadeWhenBubble()) {
549 // The bubble is gone & the notification is gone, time to actually remove it
550 mNotificationEntryManager.performRemoveNotification(bubble.entry.notification,
551 UNDEFINED_DISMISS_REASON);
552 } else {
553 // The notification is still in the shade but we've removed the bubble so
554 // lets make sure NoMan knows it's not a bubble anymore
555 try {
556 mBarService.onNotificationBubbleChanged(bubble.getKey(),
557 false /* isBubble */);
558 } catch (RemoteException e) {
559 // Bad things have happened
560 }
Mady Mellora54e9fa2019-04-18 13:26:18 -0700561 }
562 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400563
Mark Renouf82a40e62019-05-23 16:16:24 -0400564 if (update.updatedBubble != null) {
565 mStackView.updateBubble(update.updatedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400566 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400567
Mark Renouf82a40e62019-05-23 16:16:24 -0400568 if (update.orderChanged) {
569 mStackView.updateBubbleOrder(update.bubbles);
Mark Renoufba5ab512019-05-02 15:21:01 -0400570 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400571
Mark Renouf82a40e62019-05-23 16:16:24 -0400572 if (update.selectionChanged) {
573 mStackView.setSelectedBubble(update.selectedBubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400574 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400575
Mark Renouf82a40e62019-05-23 16:16:24 -0400576 // Expanding? Apply this last.
577 if (update.expandedChanged && update.expanded) {
578 mStackView.setExpanded(true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400579 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400580
Mark Renouf71a3af62019-04-08 15:02:54 -0400581 mNotificationEntryManager.updateNotifications();
Lyn Han6c40fe72019-05-08 14:06:33 -0700582 updateStack();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400583
584 if (DEBUG) {
585 Log.d(TAG, "[BubbleData]");
586 Log.d(TAG, formatBubblesString(mBubbleData.getBubbles(),
587 mBubbleData.getSelectedBubble()));
588
589 if (mStackView != null) {
590 Log.d(TAG, "[BubbleStackView]");
591 Log.d(TAG, formatBubblesString(mStackView.getBubblesOnScreen(),
592 mStackView.getExpandedBubble()));
593 }
594 }
Mark Renouf3bc5b362019-04-05 14:37:59 -0400595 }
596 };
597
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800598 /**
Joshua Tsujidd4d9f92019-05-13 13:57:38 -0400599 * Updates the stack view's suppression flags from the latest config from the zen (do not
600 * disturb) controller.
601 */
602 private void updateStackViewForZenConfig() {
603 final ZenModeConfig zenModeConfig = mZenModeController.getConfig();
604
605 if (zenModeConfig == null || mStackView == null) {
606 return;
607 }
608
609 final int suppressedEffects = zenModeConfig.suppressedVisualEffects;
610 final boolean hideNotificationDotsSelected =
611 (suppressedEffects & SUPPRESSED_EFFECT_BADGE) != 0;
612 final boolean dontPopNotifsOnScreenSelected =
613 (suppressedEffects & SUPPRESSED_EFFECT_PEEK) != 0;
614 final boolean hideFromPullDownShadeSelected =
615 (suppressedEffects & SUPPRESSED_EFFECT_NOTIFICATION_LIST) != 0;
616
617 final boolean dndEnabled = mZenModeController.getZen() != Settings.Global.ZEN_MODE_OFF;
618
619 mStackView.setSuppressNewDot(
620 dndEnabled && hideNotificationDotsSelected);
621 mStackView.setSuppressFlyout(
622 dndEnabled && (dontPopNotifsOnScreenSelected
623 || hideFromPullDownShadeSelected));
624 }
625
626 /**
627 * Lets any listeners know if bubble state has changed.
Lyn Han6c40fe72019-05-08 14:06:33 -0700628 * Updates the visibility of the bubbles based on current state.
629 * Does not un-bubble, just hides or un-hides. Notifies any
630 * {@link BubbleStateChangeListener}s of visibility changes.
631 * Updates stack description for TalkBack focus.
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800632 */
Lyn Han6c40fe72019-05-08 14:06:33 -0700633 public void updateStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800634 if (mStackView == null) {
635 return;
Mady Mellord1c78b262018-11-06 18:04:40 -0800636 }
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800637 if (mStatusBarStateListener.getCurrentState() == SHADE && hasBubbles()) {
638 // Bubbles only appear in unlocked shade
639 mStackView.setVisibility(hasBubbles() ? VISIBLE : INVISIBLE);
640 } else if (mStackView != null) {
641 mStackView.setVisibility(INVISIBLE);
Mady Mellor5549dd22018-11-06 18:07:34 -0800642 }
Lyn Han6c40fe72019-05-08 14:06:33 -0700643
644 // Let listeners know if bubble state changed.
645 boolean hadBubbles = mStatusBarWindowController.getBubblesShowing();
646 boolean hasBubblesShowing = hasBubbles() && mStackView.getVisibility() == VISIBLE;
647 mStatusBarWindowController.setBubblesShowing(hasBubblesShowing);
648 if (mStateChangeListener != null && hadBubbles != hasBubblesShowing) {
649 mStateChangeListener.onHasBubblesChanged(hasBubblesShowing);
650 }
651
652 mStackView.updateContentDescription();
Mady Mellord1c78b262018-11-06 18:04:40 -0800653 }
654
655 /**
656 * Rect indicating the touchable region for the bubble stack / expanded stack.
657 */
658 public Rect getTouchableRegion() {
659 if (mStackView == null || mStackView.getVisibility() != VISIBLE) {
660 return null;
661 }
662 mStackView.getBoundsOnScreen(mTempRect);
663 return mTempRect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800664 }
665
Mady Mellor390bff42019-04-05 15:09:01 -0700666 /**
667 * The display id of the expanded view, if the stack is expanded and not occluded by the
668 * status bar, otherwise returns {@link Display#INVALID_DISPLAY}.
669 */
670 public int getExpandedDisplayId(Context context) {
Joel Galenson4071ddb2019-04-18 13:30:45 -0700671 if (mStackView == null) {
672 return INVALID_DISPLAY;
673 }
Mady Mellor390bff42019-04-05 15:09:01 -0700674 boolean defaultDisplay = context.getDisplay() != null
675 && context.getDisplay().getDisplayId() == DEFAULT_DISPLAY;
676 Bubble b = mStackView.getExpandedBubble();
677 if (defaultDisplay && b != null && isStackExpanded()
678 && !mStatusBarWindowController.getPanelExpanded()) {
679 return b.expandedView.getVirtualDisplayId();
680 }
681 return INVALID_DISPLAY;
682 }
683
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800684 @VisibleForTesting
685 BubbleStackView getStackView() {
686 return mStackView;
687 }
688
Mady Mellor5549dd22018-11-06 18:07:34 -0800689 /**
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800690 * Whether the notification should automatically bubble or not. Gated by secure settings flags.
Mady Mellor5549dd22018-11-06 18:07:34 -0800691 */
Mady Mellor9bad2242019-01-28 11:21:51 -0800692 @VisibleForTesting
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800693 protected boolean shouldAutoBubbleForFlags(Context context, NotificationEntry entry) {
Mady Mellorceced172018-11-27 11:18:39 -0800694 if (entry.isBubbleDismissed()) {
Mady Mellor5549dd22018-11-06 18:07:34 -0800695 return false;
696 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800697 StatusBarNotification n = entry.notification;
Mady Mellorceced172018-11-27 11:18:39 -0800698
699 boolean autoBubbleMessages = shouldAutoBubbleMessages(context) || DEBUG_ENABLE_AUTO_BUBBLE;
700 boolean autoBubbleOngoing = shouldAutoBubbleOngoing(context) || DEBUG_ENABLE_AUTO_BUBBLE;
701 boolean autoBubbleAll = shouldAutoBubbleAll(context) || DEBUG_ENABLE_AUTO_BUBBLE;
702
Mady Mellor5549dd22018-11-06 18:07:34 -0800703 boolean hasRemoteInput = false;
704 if (n.getNotification().actions != null) {
705 for (Notification.Action action : n.getNotification().actions) {
706 if (action.getRemoteInputs() != null) {
707 hasRemoteInput = true;
708 break;
709 }
710 }
711 }
Mady Mellor711f9562018-12-05 14:53:46 -0800712 boolean isCall = Notification.CATEGORY_CALL.equals(n.getNotification().category)
713 && n.isOngoing();
714 boolean isMusic = n.getNotification().hasMediaSession();
715 boolean isImportantOngoing = isMusic || isCall;
Mady Mellorceced172018-11-27 11:18:39 -0800716
Mady Mellor5549dd22018-11-06 18:07:34 -0800717 Class<? extends Notification.Style> style = n.getNotification().getNotificationStyle();
Mady Mellore3175372018-12-04 17:05:11 -0800718 boolean isMessageType = Notification.CATEGORY_MESSAGE.equals(n.getNotification().category);
719 boolean isMessageStyle = Notification.MessagingStyle.class.equals(style);
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800720 return (((isMessageType && hasRemoteInput) || isMessageStyle) && autoBubbleMessages)
Mady Mellor711f9562018-12-05 14:53:46 -0800721 || (isImportantOngoing && autoBubbleOngoing)
Mady Mellorceced172018-11-27 11:18:39 -0800722 || autoBubbleAll;
723 }
724
Mady Mellor8a1f0252019-04-01 11:31:34 -0700725 private void updateShowInShadeForSuppressNotification(NotificationEntry entry) {
726 boolean suppressNotification = entry.getBubbleMetadata() != null
Mady Mellorc529d6d2019-04-16 14:22:52 -0700727 && entry.getBubbleMetadata().isNotificationSuppressed()
Steven Wu8ba8ca92019-04-11 10:47:42 -0400728 && isForegroundApp(mContext, entry.notification.getPackageName());
Mady Mellor8a1f0252019-04-01 11:31:34 -0700729 entry.setShowInShadeWhenBubble(!suppressNotification);
730 }
731
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400732 static String formatBubblesString(List<Bubble> bubbles, Bubble selected) {
733 StringBuilder sb = new StringBuilder();
734 for (Bubble bubble : bubbles) {
735 if (bubble == null) {
736 sb.append(" <null> !!!!!\n");
737 } else {
738 boolean isSelected = (bubble == selected);
739 sb.append(String.format("%s Bubble{act=%12d, ongoing=%d, key=%s}\n",
740 ((isSelected) ? "->" : " "),
741 bubble.getLastActivity(),
742 (bubble.isOngoing() ? 1 : 0),
743 bubble.getKey()));
744 }
745 }
746 return sb.toString();
747 }
748
Mady Mellore80930e2019-03-21 16:00:45 -0700749 /**
750 * Return true if the applications with the package name is running in foreground.
751 *
Steven Wu8ba8ca92019-04-11 10:47:42 -0400752 * @param context application context.
Mady Mellore80930e2019-03-21 16:00:45 -0700753 * @param pkgName application package name.
754 */
Steven Wu8ba8ca92019-04-11 10:47:42 -0400755 public static boolean isForegroundApp(Context context, String pkgName) {
756 ActivityManager am = context.getSystemService(ActivityManager.class);
Mady Mellore80930e2019-03-21 16:00:45 -0700757 List<RunningTaskInfo> tasks = am.getRunningTasks(1 /* maxNum */);
758 return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
759 }
760
Mark Renoufcecc77b2019-01-30 16:32:24 -0500761 /**
762 * This task stack listener is responsible for responding to tasks moved to the front
763 * which are on the default (main) display. When this happens, expanded bubbles must be
764 * collapsed so the user may interact with the app which was just moved to the front.
765 * <p>
766 * This listener is registered with SystemUI's ActivityManagerWrapper which dispatches
767 * these calls via a main thread Handler.
768 */
769 @MainThread
770 private class BubbleTaskStackListener extends TaskStackChangeListener {
771
Mark Renoufcecc77b2019-01-30 16:32:24 -0500772 @Override
Mark Renoufc808f062019-02-07 15:20:37 -0500773 public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
774 if (mStackView != null && taskInfo.displayId == Display.DEFAULT_DISPLAY) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400775 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500776 }
777 }
778
Mark Renoufcecc77b2019-01-30 16:32:24 -0500779 @Override
Mark Renoufa12e8762019-03-07 15:43:01 -0500780 public void onActivityLaunchOnSecondaryDisplayRerouted() {
Mark Renoufcecc77b2019-01-30 16:32:24 -0500781 if (mStackView != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400782 mBubbleData.setExpanded(false);
Mark Renoufcecc77b2019-01-30 16:32:24 -0500783 }
784 }
Mark Renouf446251d2019-04-26 10:22:41 -0400785
786 @Override
787 public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) {
788 if (mStackView != null && taskInfo.displayId == getExpandedDisplayId(mContext)) {
789 mBubbleData.setExpanded(false);
790 }
791 }
Mark Renoufcecc77b2019-01-30 16:32:24 -0500792 }
793
Mady Mellorceced172018-11-27 11:18:39 -0800794 private static boolean shouldAutoBubbleMessages(Context context) {
795 return Settings.Secure.getInt(context.getContentResolver(),
796 ENABLE_AUTO_BUBBLE_MESSAGES, 0) != 0;
797 }
798
799 private static boolean shouldAutoBubbleOngoing(Context context) {
800 return Settings.Secure.getInt(context.getContentResolver(),
801 ENABLE_AUTO_BUBBLE_ONGOING, 0) != 0;
802 }
803
804 private static boolean shouldAutoBubbleAll(Context context) {
805 return Settings.Secure.getInt(context.getContentResolver(),
806 ENABLE_AUTO_BUBBLE_ALL, 0) != 0;
Mady Mellor5549dd22018-11-06 18:07:34 -0800807 }
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500808
Mady Mellor3dff9e62019-02-05 18:12:53 -0800809 static boolean shouldUseContentIntent(Context context) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500810 return Settings.Secure.getInt(context.getContentResolver(),
811 ENABLE_BUBBLE_CONTENT_INTENT, 0) != 0;
812 }
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800813
814 private static boolean areBubblesEnabled(Context context) {
815 return Settings.Secure.getInt(context.getContentResolver(),
816 ENABLE_BUBBLES, 1) != 0;
817 }
Joshua Tsujia19515f2019-02-13 18:02:29 -0500818
Joshua Tsuji010c2b12019-02-25 18:11:25 -0500819 /** Default stiffness to use for bubble physics animations. */
820 public static int getBubbleStiffness(Context context, int defaultStiffness) {
821 return Settings.Secure.getInt(
822 context.getContentResolver(), BUBBLE_STIFFNESS, defaultStiffness);
823 }
824
825 /** Default bounciness/damping ratio to use for bubble physics animations. */
826 public static float getBubbleBounciness(Context context, float defaultBounciness) {
827 return Settings.Secure.getInt(
828 context.getContentResolver(),
829 BUBBLE_BOUNCINESS,
830 (int) (defaultBounciness * 100)) / 100f;
831 }
832
Mady Mellorca0c24c2019-05-16 16:14:32 -0700833 /**
834 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
835 *
836 * Keep checks in sync with NotificationManagerService#canLaunchInActivityView. Typically
837 * that should filter out any invalid bubbles, but should protect SysUI side just in case.
838 *
839 * @param context the context to use.
840 * @param entry the entry to bubble.
841 */
842 static boolean canLaunchInActivityView(Context context, NotificationEntry entry) {
843 PendingIntent intent = entry.getBubbleMetadata() != null
844 ? entry.getBubbleMetadata().getIntent()
845 : null;
846 if (intent == null) {
847 Log.w(TAG, "Unable to create bubble -- no intent");
848 return false;
849 }
850 ActivityInfo info =
851 intent.getIntent().resolveActivityInfo(context.getPackageManager(), 0);
852 if (info == null) {
853 Log.w(TAG, "Unable to send as bubble -- couldn't find activity info for intent: "
854 + intent);
855 return false;
856 }
857 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
858 Log.w(TAG, "Unable to send as bubble -- activity is not resizable for intent: "
859 + intent);
860 return false;
861 }
862 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
863 Log.w(TAG, "Unable to send as bubble -- activity is not documentLaunchMode=always "
864 + "for intent: " + intent);
865 return false;
866 }
867 if ((info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
868 Log.w(TAG, "Unable to send as bubble -- activity is not embeddable for intent: "
869 + intent);
870 return false;
871 }
872 return true;
873 }
874
Joshua Tsujia19515f2019-02-13 18:02:29 -0500875 /** PinnedStackListener that dispatches IME visibility updates to the stack. */
876 private class BubblesImeListener extends IPinnedStackListener.Stub {
877
878 @Override
879 public void onListenerRegistered(IPinnedStackController controller) throws RemoteException {
880 }
881
882 @Override
883 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds,
884 Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment,
885 int displayRotation) throws RemoteException {}
886
887 @Override
Joshua Tsujid9422832019-03-05 13:32:37 -0500888 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
889 if (mStackView != null && mStackView.getBubbleCount() > 0) {
890 mStackView.post(() -> mStackView.onImeVisibilityChanged(imeVisible, imeHeight));
Joshua Tsujia19515f2019-02-13 18:02:29 -0500891 }
892 }
893
894 @Override
895 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight)
896 throws RemoteException {}
897
898 @Override
899 public void onMinimizedStateChanged(boolean isMinimized) throws RemoteException {}
900
901 @Override
902 public void onActionsChanged(ParceledListSlice actions) throws RemoteException {}
903 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800904}