blob: 557d1bb0ae52d6644d6236f23df0b4f627a61248 [file] [log] [blame]
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001/*
2 * Copyright (C) 2012 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 Tsujib1a796b2019-01-16 15:43:12 -080019import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080020import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
21
Mady Mellorb8aaf972019-11-26 10:28:00 -080022import static com.android.systemui.bubbles.BadgedImageView.DOT_STATE_DEFAULT;
23import static com.android.systemui.bubbles.BadgedImageView.DOT_STATE_SUPPRESSED_FOR_FLYOUT;
Issei Suzukia8d07312019-06-07 12:56:19 +020024import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_STACK_VIEW;
25import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
26import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
27
Joshua Tsuji4accf5982019-04-22 17:36:11 -040028import android.animation.Animator;
29import android.animation.AnimatorListenerAdapter;
30import android.animation.ValueAnimator;
Issei Suzukic0387542019-03-08 17:31:14 +010031import android.annotation.NonNull;
Lyn Han6c40fe72019-05-08 14:06:33 -070032import android.app.Notification;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080033import android.content.Context;
Lyn Hanf4730312019-06-18 11:18:58 -070034import android.content.res.Configuration;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080035import android.content.res.Resources;
Lyn Hanb58c7562020-01-07 14:29:20 -080036import android.content.res.TypedArray;
37import android.graphics.Color;
Joshua Tsuji4accf5982019-04-22 17:36:11 -040038import android.graphics.ColorMatrix;
39import android.graphics.ColorMatrixColorFilter;
Joshua Tsuji4accf5982019-04-22 17:36:11 -040040import android.graphics.Paint;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080041import android.graphics.Point;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080042import android.graphics.PointF;
43import android.graphics.Rect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080044import android.graphics.RectF;
Lyn Hanb58c7562020-01-07 14:29:20 -080045import android.graphics.drawable.AdaptiveIconDrawable;
46import android.graphics.drawable.ColorDrawable;
47import android.graphics.drawable.InsetDrawable;
Mady Mellor217b2e92019-02-27 11:44:16 -080048import android.os.Bundle;
Joshua Tsuji4accf5982019-04-22 17:36:11 -040049import android.os.VibrationEffect;
50import android.os.Vibrator;
Steven Wua254dab2019-01-29 11:30:39 -050051import android.service.notification.StatusBarNotification;
Mark Renouf89b1a4a2018-12-04 14:59:45 -050052import android.util.Log;
Issei Suzukic0387542019-03-08 17:31:14 +010053import android.view.Choreographer;
Mady Mellor9be3bed2019-08-21 17:26:26 -070054import android.view.DisplayCutout;
Joshua Tsuji36b1b2c2019-04-18 16:27:35 -040055import android.view.Gravity;
Mady Mellordea7ecf2018-12-10 15:47:40 -080056import android.view.LayoutInflater;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080057import android.view.MotionEvent;
58import android.view.View;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080059import android.view.ViewTreeObserver;
Joshua Tsuji0fee7682019-01-25 11:37:49 -050060import android.view.WindowInsets;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080061import android.view.WindowManager;
Mady Mellor217b2e92019-02-27 11:44:16 -080062import android.view.accessibility.AccessibilityNodeInfo;
Lyn Hane68d0912019-05-02 18:28:01 -070063import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
Joshua Tsuji614b1df2019-03-26 13:57:05 -040064import android.view.animation.AccelerateDecelerateInterpolator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080065import android.widget.FrameLayout;
Lyn Hanb58c7562020-01-07 14:29:20 -080066import android.widget.ImageView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080067
Mark Renoufcecc77b2019-01-30 16:32:24 -050068import androidx.annotation.MainThread;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080069import androidx.annotation.Nullable;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080070import androidx.dynamicanimation.animation.DynamicAnimation;
Joshua Tsuji6549e702019-05-02 13:13:16 -040071import androidx.dynamicanimation.animation.FloatPropertyCompat;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080072import androidx.dynamicanimation.animation.SpringAnimation;
73import androidx.dynamicanimation.animation.SpringForce;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080074
Mady Melloredd4ee12019-01-18 10:45:11 -080075import com.android.internal.annotations.VisibleForTesting;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080076import com.android.internal.widget.ViewClippingUtil;
77import com.android.systemui.R;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080078import com.android.systemui.bubbles.animation.ExpandedAnimationController;
79import com.android.systemui.bubbles.animation.PhysicsAnimationLayout;
80import com.android.systemui.bubbles.animation.StackAnimationController;
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -080081import com.android.systemui.shared.system.SysUiStatsLog;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080082
Joshua Tsuji395bcfe2019-07-02 19:23:23 -040083import java.io.FileDescriptor;
84import java.io.PrintWriter;
Steven Wua254dab2019-01-29 11:30:39 -050085import java.math.BigDecimal;
86import java.math.RoundingMode;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040087import java.util.ArrayList;
Mark Renouf821e6782019-04-01 14:17:37 -040088import java.util.Collections;
89import java.util.List;
Steven Wua254dab2019-01-29 11:30:39 -050090
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080091/**
92 * Renders bubbles in a stack and handles animating expanded and collapsed states.
93 */
Joshua Tsuji442b6272019-02-08 13:23:43 -050094public class BubbleStackView extends FrameLayout {
Issei Suzukia8d07312019-06-07 12:56:19 +020095 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleStackView" : TAG_BUBBLES;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080096
Joshua Tsuji6549e702019-05-02 13:13:16 -040097 /** How far the flyout needs to be dragged before it's dismissed regardless of velocity. */
98 static final float FLYOUT_DRAG_PERCENT_DISMISS = 0.25f;
99
100 /** Velocity required to dismiss the flyout via drag. */
101 private static final float FLYOUT_DISMISS_VELOCITY = 2000f;
102
103 /**
104 * Factor for attenuating translation when the flyout is overscrolled (8f = flyout moves 1 pixel
105 * for every 8 pixels overscrolled).
106 */
107 private static final float FLYOUT_OVERSCROLL_ATTENUATION_FACTOR = 8f;
108
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400109 /** Duration of the flyout alpha animations. */
110 private static final int FLYOUT_ALPHA_ANIMATION_DURATION = 100;
111
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400112 /** Percent to darken the bubbles when they're in the dismiss target. */
113 private static final float DARKEN_PERCENT = 0.3f;
114
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400115 /** How long to wait, in milliseconds, before hiding the flyout. */
116 @VisibleForTesting
117 static final int FLYOUT_HIDE_AFTER = 5000;
118
Issei Suzukic0387542019-03-08 17:31:14 +0100119 /**
120 * Interface to synchronize {@link View} state and the screen.
121 *
122 * {@hide}
123 */
124 interface SurfaceSynchronizer {
125 /**
126 * Wait until requested change on a {@link View} is reflected on the screen.
127 *
128 * @param callback callback to run after the change is reflected on the screen.
129 */
130 void syncSurfaceAndRun(Runnable callback);
131 }
132
133 private static final SurfaceSynchronizer DEFAULT_SURFACE_SYNCHRONIZER =
134 new SurfaceSynchronizer() {
135 @Override
136 public void syncSurfaceAndRun(Runnable callback) {
137 Choreographer.getInstance().postFrameCallback(new Choreographer.FrameCallback() {
138 // Just wait 2 frames. There is no guarantee, but this is usually enough time that
139 // the requested change is reflected on the screen.
140 // TODO: Once SurfaceFlinger provide APIs to sync the state of {@code View} and
141 // surfaces, rewrite this logic with them.
142 private int mFrameWait = 2;
143
144 @Override
145 public void doFrame(long frameTimeNanos) {
146 if (--mFrameWait > 0) {
147 Choreographer.getInstance().postFrameCallback(this);
148 } else {
149 callback.run();
150 }
151 }
152 });
153 }
154 };
155
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800156 private Point mDisplaySize;
157
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800158 private final SpringAnimation mExpandedViewXAnim;
159 private final SpringAnimation mExpandedViewYAnim;
Mady Mellorcfd06c12019-02-13 14:32:12 -0800160 private final BubbleData mBubbleData;
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800161
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400162 private final Vibrator mVibrator;
163 private final ValueAnimator mDesaturateAndDarkenAnimator;
164 private final Paint mDesaturateAndDarkenPaint = new Paint();
165
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800166 private PhysicsAnimationLayout mBubbleContainer;
167 private StackAnimationController mStackAnimationController;
168 private ExpandedAnimationController mExpandedAnimationController;
169
Mady Mellor3dff9e62019-02-05 18:12:53 -0800170 private FrameLayout mExpandedViewContainer;
171
Joshua Tsuji6549e702019-05-02 13:13:16 -0400172 private BubbleFlyoutView mFlyout;
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400173 /** Runnable that fades out the flyout and then sets it to GONE. */
Joshua Tsuji6549e702019-05-02 13:13:16 -0400174 private Runnable mHideFlyout = () -> animateFlyoutCollapsed(true, 0 /* velX */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700175 /**
176 * Callback to run after the flyout hides. Also called if a new flyout is shown before the
177 * previous one animates out.
178 */
Mady Mellorb8aaf972019-11-26 10:28:00 -0800179 private Runnable mAfterFlyoutHidden;
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400180
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400181 /** Layout change listener that moves the stack to the nearest valid position on rotation. */
Lyn Hanf4730312019-06-18 11:18:58 -0700182 private OnLayoutChangeListener mOrientationChangedListener;
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400183 /** Whether the stack was on the left side of the screen prior to rotation. */
184 private boolean mWasOnLeftBeforeRotation = false;
185 /**
186 * How far down the screen the stack was before rotation, in terms of percentage of the way down
187 * the allowable region. Defaults to -1 if not set.
188 */
189 private float mVerticalPosPercentBeforeRotation = -1;
190
Mady Mellor70958542019-09-24 17:12:46 -0700191 private int mMaxBubbles;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800192 private int mBubbleSize;
Mady Mellor70958542019-09-24 17:12:46 -0700193 private int mBubbleElevation;
Lyn Han4a8efe32019-05-30 09:43:27 -0700194 private int mBubblePaddingTop;
Mady Mellore9371bc2019-07-10 18:50:59 -0700195 private int mBubbleTouchPadding;
Lyn Han6f6b3ae2019-05-16 14:17:30 -0700196 private int mExpandedViewPadding;
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800197 private int mExpandedAnimateXDistance;
198 private int mExpandedAnimateYDistance;
Lyn Han5aa27e22019-05-15 10:55:07 -0700199 private int mPointerHeight;
Joshua Tsujif44347f2019-02-12 14:28:06 -0500200 private int mStatusBarHeight;
Joshua Tsujia19515f2019-02-13 18:02:29 -0500201 private int mImeOffset;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800202 private Bubble mExpandedBubble;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800203 private boolean mIsExpanded;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800204
Joshua Tsuji6549e702019-05-02 13:13:16 -0400205 /** Whether the stack is currently on the left side of the screen, or animating there. */
206 private boolean mStackOnLeftOrWillBe = false;
207
208 /** Whether a touch gesture, such as a stack/bubble drag or flyout drag, is in progress. */
209 private boolean mIsGestureInProgress = false;
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400210
Joshua Tsuji395bcfe2019-07-02 19:23:23 -0400211 /** Description of current animation controller state. */
212 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
213 pw.println("Stack view state:");
214 pw.print(" gestureInProgress: "); pw.println(mIsGestureInProgress);
215 pw.print(" showingDismiss: "); pw.println(mShowingDismiss);
216 pw.print(" isExpansionAnimating: "); pw.println(mIsExpansionAnimating);
217 pw.print(" draggingInDismiss: "); pw.println(mDraggingInDismissTarget);
218 pw.print(" animatingMagnet: "); pw.println(mAnimatingMagnet);
219 mStackAnimationController.dump(fd, pw, args);
220 mExpandedAnimationController.dump(fd, pw, args);
221 }
222
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800223 private BubbleTouchHandler mTouchHandler;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800224 private BubbleController.BubbleExpandListener mExpandListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800225
226 private boolean mViewUpdatedRequested = false;
Mady Mellorbc078c22019-03-26 17:10:34 -0700227 private boolean mIsExpansionAnimating = false;
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400228 private boolean mShowingDismiss = false;
229
230 /**
231 * Whether the user is currently dragging their finger within the dismiss target. In this state
232 * the stack will be magnetized to the center of the target, so we shouldn't move it until the
233 * touch exits the dismiss target area.
234 */
235 private boolean mDraggingInDismissTarget = false;
236
237 /** Whether the stack is magneting towards the dismiss target. */
238 private boolean mAnimatingMagnet = false;
239
240 /** The view to desaturate/darken when magneted to the dismiss target. */
241 private View mDesaturateAndDarkenTargetView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800242
Mady Mellor3dff9e62019-02-05 18:12:53 -0800243 private LayoutInflater mInflater;
244
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800245 // Used for determining view / touch intersection
246 int[] mTempLoc = new int[2];
247 RectF mTempRect = new RectF();
248
Mark Renouf821e6782019-04-01 14:17:37 -0400249 private final List<Rect> mSystemGestureExclusionRects = Collections.singletonList(new Rect());
250
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800251 private ViewTreeObserver.OnPreDrawListener mViewUpdater =
252 new ViewTreeObserver.OnPreDrawListener() {
253 @Override
254 public boolean onPreDraw() {
255 getViewTreeObserver().removeOnPreDrawListener(mViewUpdater);
Lyn Han285ad302019-05-29 19:01:39 -0700256 updateExpandedView();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800257 mViewUpdatedRequested = false;
258 return true;
259 }
260 };
261
Mark Renouf821e6782019-04-01 14:17:37 -0400262 private ViewTreeObserver.OnDrawListener mSystemGestureExcludeUpdater =
263 this::updateSystemGestureExcludeRects;
264
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800265 private ViewClippingUtil.ClippingParameters mClippingParameters =
266 new ViewClippingUtil.ClippingParameters() {
267
Lyn Han522e9ff2019-05-17 13:26:13 -0700268 @Override
269 public boolean shouldFinish(View view) {
270 return false;
271 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800272
Lyn Han522e9ff2019-05-17 13:26:13 -0700273 @Override
274 public boolean isClippingEnablingAllowed(View view) {
275 return !mIsExpanded;
276 }
277 };
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800278
Joshua Tsuji6549e702019-05-02 13:13:16 -0400279 /** Float property that 'drags' the flyout. */
280 private final FloatPropertyCompat mFlyoutCollapseProperty =
281 new FloatPropertyCompat("FlyoutCollapseSpring") {
282 @Override
283 public float getValue(Object o) {
284 return mFlyoutDragDeltaX;
285 }
286
287 @Override
288 public void setValue(Object o, float v) {
289 onFlyoutDragged(v);
290 }
291 };
292
293 /** SpringAnimation that springs the flyout collapsed via onFlyoutDragged. */
294 private final SpringAnimation mFlyoutTransitionSpring =
295 new SpringAnimation(this, mFlyoutCollapseProperty);
296
297 /** Distance the flyout has been dragged in the X axis. */
298 private float mFlyoutDragDeltaX = 0f;
299
300 /**
Joshua Tsuji14e68552019-06-06 17:17:08 -0400301 * Runnable that animates in the flyout. This reference is needed to cancel delayed postings.
302 */
303 private Runnable mAnimateInFlyout;
304
305 /**
Joshua Tsuji6549e702019-05-02 13:13:16 -0400306 * End listener for the flyout spring that either posts a runnable to hide the flyout, or hides
307 * it immediately.
308 */
309 private final DynamicAnimation.OnAnimationEndListener mAfterFlyoutTransitionSpring =
310 (dynamicAnimation, b, v, v1) -> {
311 if (mFlyoutDragDeltaX == 0) {
312 mFlyout.postDelayed(mHideFlyout, FLYOUT_HIDE_AFTER);
313 } else {
314 mFlyout.hideFlyout();
315 }
316 };
317
Lyn Han1b4f25e2019-06-11 13:56:34 -0700318 @NonNull
319 private final SurfaceSynchronizer mSurfaceSynchronizer;
Issei Suzukic0387542019-03-08 17:31:14 +0100320
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400321 private BubbleDismissView mDismissContainer;
322 private Runnable mAfterMagnet;
Issei Suzukic0387542019-03-08 17:31:14 +0100323
Lyn Hanf4730312019-06-18 11:18:58 -0700324 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
Lyn Hanb58c7562020-01-07 14:29:20 -0800325 private BubbleExpandedView mOverflowExpandedView;
326 private ImageView mOverflowBtn;
Lyn Hanf4730312019-06-18 11:18:58 -0700327
Issei Suzukic0387542019-03-08 17:31:14 +0100328 public BubbleStackView(Context context, BubbleData data,
Lyn Han522e9ff2019-05-17 13:26:13 -0700329 @Nullable SurfaceSynchronizer synchronizer) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800330 super(context);
331
Mady Mellorcfd06c12019-02-13 14:32:12 -0800332 mBubbleData = data;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800333 mInflater = LayoutInflater.from(context);
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400334 mTouchHandler = new BubbleTouchHandler(this, data, context);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800335 setOnTouchListener(mTouchHandler);
Joshua Tsuji442b6272019-02-08 13:23:43 -0500336 mInflater = LayoutInflater.from(context);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800337
338 Resources res = getResources();
Mady Mellor70958542019-09-24 17:12:46 -0700339 mMaxBubbles = res.getInteger(R.integer.bubbles_max_rendered);
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800340 mBubbleSize = res.getDimensionPixelSize(R.dimen.individual_bubble_size);
Mady Mellor70958542019-09-24 17:12:46 -0700341 mBubbleElevation = res.getDimensionPixelSize(R.dimen.bubble_elevation);
Lyn Han4a8efe32019-05-30 09:43:27 -0700342 mBubblePaddingTop = res.getDimensionPixelSize(R.dimen.bubble_padding_top);
Mady Mellore9371bc2019-07-10 18:50:59 -0700343 mBubbleTouchPadding = res.getDimensionPixelSize(R.dimen.bubble_touch_padding);
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800344 mExpandedAnimateXDistance =
345 res.getDimensionPixelSize(R.dimen.bubble_expanded_animate_x_distance);
346 mExpandedAnimateYDistance =
347 res.getDimensionPixelSize(R.dimen.bubble_expanded_animate_y_distance);
Lyn Han5aa27e22019-05-15 10:55:07 -0700348 mPointerHeight = res.getDimensionPixelSize(R.dimen.bubble_pointer_height);
349
Joshua Tsujif44347f2019-02-12 14:28:06 -0500350 mStatusBarHeight =
351 res.getDimensionPixelSize(com.android.internal.R.dimen.status_bar_height);
Joshua Tsujia19515f2019-02-13 18:02:29 -0500352 mImeOffset = res.getDimensionPixelSize(R.dimen.pip_ime_offset);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800353
354 mDisplaySize = new Point();
355 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Mady Mellor9be3bed2019-08-21 17:26:26 -0700356 // We use the real size & subtract screen decorations / window insets ourselves when needed
Mady Mellore19353d2019-08-21 17:25:02 -0700357 wm.getDefaultDisplay().getRealSize(mDisplaySize);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800358
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400359 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
360
Lyn Han6f6b3ae2019-05-16 14:17:30 -0700361 mExpandedViewPadding = res.getDimensionPixelSize(R.dimen.bubble_expanded_view_padding);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800362 int elevation = res.getDimensionPixelSize(R.dimen.bubble_elevation);
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800363
364 mStackAnimationController = new StackAnimationController();
Lyn Hanf4730312019-06-18 11:18:58 -0700365
Lyn Han6f6b3ae2019-05-16 14:17:30 -0700366 mExpandedAnimationController = new ExpandedAnimationController(
Lyn Hanf4730312019-06-18 11:18:58 -0700367 mDisplaySize, mExpandedViewPadding, res.getConfiguration().orientation);
Issei Suzukic0387542019-03-08 17:31:14 +0100368 mSurfaceSynchronizer = synchronizer != null ? synchronizer : DEFAULT_SURFACE_SYNCHRONIZER;
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800369
370 mBubbleContainer = new PhysicsAnimationLayout(context);
Joshua Tsujic36ee6f2019-05-28 17:00:16 -0400371 mBubbleContainer.setActiveController(mStackAnimationController);
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800372 mBubbleContainer.setElevation(elevation);
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800373 mBubbleContainer.setClipChildren(false);
374 addView(mBubbleContainer, new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
375
Mady Mellor3dff9e62019-02-05 18:12:53 -0800376 mExpandedViewContainer = new FrameLayout(context);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800377 mExpandedViewContainer.setElevation(elevation);
Lyn Han6f6b3ae2019-05-16 14:17:30 -0700378 mExpandedViewContainer.setPadding(mExpandedViewPadding, mExpandedViewPadding,
379 mExpandedViewPadding, mExpandedViewPadding);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800380 mExpandedViewContainer.setClipChildren(false);
381 addView(mExpandedViewContainer);
382
Mady Mellor8bfe5412019-07-31 14:56:44 -0700383 setUpFlyout();
Joshua Tsuji6549e702019-05-02 13:13:16 -0400384 mFlyoutTransitionSpring.setSpring(new SpringForce()
Joshua Tsuji14e68552019-06-06 17:17:08 -0400385 .setStiffness(SpringForce.STIFFNESS_LOW)
Joshua Tsuji6549e702019-05-02 13:13:16 -0400386 .setDampingRatio(SpringForce.DAMPING_RATIO_LOW_BOUNCY));
387 mFlyoutTransitionSpring.addEndListener(mAfterFlyoutTransitionSpring);
388
389 mDismissContainer = new BubbleDismissView(mContext);
390 mDismissContainer.setLayoutParams(new FrameLayout.LayoutParams(
391 MATCH_PARENT,
392 getResources().getDimensionPixelSize(R.dimen.pip_dismiss_gradient_height),
393 Gravity.BOTTOM));
394 addView(mDismissContainer);
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400395
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800396 mExpandedViewXAnim =
397 new SpringAnimation(mExpandedViewContainer, DynamicAnimation.TRANSLATION_X);
398 mExpandedViewXAnim.setSpring(
399 new SpringForce()
400 .setStiffness(SpringForce.STIFFNESS_LOW)
401 .setDampingRatio(SpringForce.DAMPING_RATIO_LOW_BOUNCY));
402
403 mExpandedViewYAnim =
404 new SpringAnimation(mExpandedViewContainer, DynamicAnimation.TRANSLATION_Y);
405 mExpandedViewYAnim.setSpring(
406 new SpringForce()
407 .setStiffness(SpringForce.STIFFNESS_LOW)
408 .setDampingRatio(SpringForce.DAMPING_RATIO_LOW_BOUNCY));
Mady Mellorbc078c22019-03-26 17:10:34 -0700409 mExpandedViewYAnim.addEndListener((anim, cancelled, value, velocity) -> {
Lyn Hana0bb02e2020-01-28 17:57:27 -0800410 if (mIsExpanded) {
411 if (mExpandedBubble == null) {
412 mOverflowExpandedView.updateView();
413 } else {
414 mExpandedBubble.getExpandedView().updateView();
415 }
Mady Mellorbc078c22019-03-26 17:10:34 -0700416 }
417 });
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800418
419 setClipChildren(false);
Mady Mellor217b2e92019-02-27 11:44:16 -0800420 setFocusable(true);
Joshua Tsuji442b6272019-02-08 13:23:43 -0500421 mBubbleContainer.bringToFront();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800422
Lyn Hanb58c7562020-01-07 14:29:20 -0800423 if (BubbleExperimentConfig.allowBubbleOverflow(mContext)) {
424 setUpOverflow();
425 }
426
Mady Mellor5d8f1402019-02-21 18:23:52 -0800427 setOnApplyWindowInsetsListener((View view, WindowInsets insets) -> {
Mady Mellorbc078c22019-03-26 17:10:34 -0700428 if (!mIsExpanded || mIsExpansionAnimating) {
Mady Mellor5d8f1402019-02-21 18:23:52 -0800429 return view.onApplyWindowInsets(insets);
430 }
Mady Mellor5d8f1402019-02-21 18:23:52 -0800431 mExpandedAnimationController.updateYPosition(
432 // Update the insets after we're done translating otherwise position
433 // calculation for them won't be correct.
Lyn Hanb58c7562020-01-07 14:29:20 -0800434 () -> {
435 if (mExpandedBubble == null) {
436 mOverflowExpandedView.updateInsets(insets);
437 } else {
438 mExpandedBubble.getExpandedView().updateInsets(insets);
439 }
440 });
Mady Mellor5d8f1402019-02-21 18:23:52 -0800441 return view.onApplyWindowInsets(insets);
442 });
Mark Renouf821e6782019-04-01 14:17:37 -0400443
Lyn Hanf4730312019-06-18 11:18:58 -0700444 mOrientationChangedListener =
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400445 (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
Mady Mellor9be3bed2019-08-21 17:26:26 -0700446 mExpandedAnimationController.updateOrientation(mOrientation, mDisplaySize);
447 mStackAnimationController.updateOrientation(mOrientation);
448
449 // Reposition & adjust the height for new orientation
450 if (mIsExpanded) {
451 mExpandedViewContainer.setTranslationY(getExpandedViewY());
Lyn Hanb58c7562020-01-07 14:29:20 -0800452 if (mExpandedBubble == null) {
453 mOverflowExpandedView.updateView();
454 } else {
455 mExpandedBubble.getExpandedView().updateView();
456 }
Mady Mellor9be3bed2019-08-21 17:26:26 -0700457 }
458
459 // Need to update the padding around the view
460 WindowInsets insets = getRootWindowInsets();
461 int leftPadding = mExpandedViewPadding;
462 int rightPadding = mExpandedViewPadding;
463 if (insets != null) {
464 // Can't have the expanded view overlaying notches
465 int cutoutLeft = 0;
466 int cutoutRight = 0;
467 DisplayCutout cutout = insets.getDisplayCutout();
468 if (cutout != null) {
469 cutoutLeft = cutout.getSafeInsetLeft();
470 cutoutRight = cutout.getSafeInsetRight();
471 }
472 // Or overlaying nav or status bar
473 leftPadding += Math.max(cutoutLeft, insets.getStableInsetLeft());
474 rightPadding += Math.max(cutoutRight, insets.getStableInsetRight());
475 }
476 mExpandedViewContainer.setPadding(leftPadding, mExpandedViewPadding,
477 rightPadding, mExpandedViewPadding);
478
Lyn Hanf4730312019-06-18 11:18:58 -0700479 if (mIsExpanded) {
480 // Re-draw bubble row and pointer for new orientation.
481 mExpandedAnimationController.expandFromStack(() -> {
482 updatePointerPosition();
483 } /* after */);
484 }
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400485 if (mVerticalPosPercentBeforeRotation >= 0) {
486 mStackAnimationController.moveStackToSimilarPositionAfterRotation(
487 mWasOnLeftBeforeRotation, mVerticalPosPercentBeforeRotation);
488 }
Lyn Hanf4730312019-06-18 11:18:58 -0700489 removeOnLayoutChangeListener(mOrientationChangedListener);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400490 };
491
Mark Renouf821e6782019-04-01 14:17:37 -0400492 // This must be a separate OnDrawListener since it should be called for every draw.
493 getViewTreeObserver().addOnDrawListener(mSystemGestureExcludeUpdater);
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400494
495 final ColorMatrix animatedMatrix = new ColorMatrix();
496 final ColorMatrix darkenMatrix = new ColorMatrix();
497
498 mDesaturateAndDarkenAnimator = ValueAnimator.ofFloat(1f, 0f);
499 mDesaturateAndDarkenAnimator.addUpdateListener(animation -> {
500 final float animatedValue = (float) animation.getAnimatedValue();
501 animatedMatrix.setSaturation(animatedValue);
502
503 final float animatedDarkenValue = (1f - animatedValue) * DARKEN_PERCENT;
504 darkenMatrix.setScale(
505 1f - animatedDarkenValue /* red */,
506 1f - animatedDarkenValue /* green */,
507 1f - animatedDarkenValue /* blue */,
508 1f /* alpha */);
509
510 // Concat the matrices so that the animatedMatrix both desaturates and darkens.
511 animatedMatrix.postConcat(darkenMatrix);
512
513 // Update the paint and apply it to the bubble container.
514 mDesaturateAndDarkenPaint.setColorFilter(new ColorMatrixColorFilter(animatedMatrix));
515 mDesaturateAndDarkenTargetView.setLayerPaint(mDesaturateAndDarkenPaint);
516 });
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800517 }
518
Lyn Hanb58c7562020-01-07 14:29:20 -0800519 private void setUpOverflow() {
520 mOverflowExpandedView = (BubbleExpandedView) mInflater.inflate(
521 R.layout.bubble_expanded_view, this /* root */, false /* attachToRoot */);
522 mOverflowExpandedView.setOverflow(true);
523
Lyn Hanc47e1712020-01-28 21:43:34 -0800524 mOverflowBtn = (ImageView) mInflater.inflate(R.layout.bubble_overflow_button,
525 this /* root */,
526 false /* attachToRoot */);
527
528 mBubbleContainer.addView(mOverflowBtn, 0,
529 new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
530
Lyn Hanedb55e22020-02-12 18:55:39 -0800531 setOverflowBtnTheme();
532 mOverflowBtn.setVisibility(GONE);
533 }
534
535 // TODO(b/149146374) Propagate theme change to bubbles in overflow.
536 private void setOverflowBtnTheme() {
Lyn Hanb58c7562020-01-07 14:29:20 -0800537 TypedArray ta = mContext.obtainStyledAttributes(
538 new int[]{android.R.attr.colorBackgroundFloating});
539 int bgColor = ta.getColor(0, Color.WHITE /* default */);
540 ta.recycle();
541
542 InsetDrawable fg = new InsetDrawable(mOverflowBtn.getDrawable(), 28);
543 ColorDrawable bg = new ColorDrawable(bgColor);
544 AdaptiveIconDrawable adaptiveIcon = new AdaptiveIconDrawable(bg, fg);
545 mOverflowBtn.setImageDrawable(adaptiveIcon);
Lyn Hanb58c7562020-01-07 14:29:20 -0800546 }
547
Lyn Hana0bb02e2020-01-28 17:57:27 -0800548 void showExpandedViewContents(int displayId) {
Lyn Hanc47e1712020-01-28 21:43:34 -0800549 if (mOverflowExpandedView != null
550 && mOverflowExpandedView.getVirtualDisplayId() == displayId) {
Lyn Hana0bb02e2020-01-28 17:57:27 -0800551 mOverflowExpandedView.setContentVisibility(true);
552 } else if (mExpandedBubble != null
553 && mExpandedBubble.getExpandedView().getVirtualDisplayId() == displayId) {
554 mExpandedBubble.setContentVisibility(true);
Lyn Hanb58c7562020-01-07 14:29:20 -0800555 }
Lyn Hanb58c7562020-01-07 14:29:20 -0800556 }
557
Mady Mellor8bfe5412019-07-31 14:56:44 -0700558 private void setUpFlyout() {
559 if (mFlyout != null) {
560 removeView(mFlyout);
561 }
562 mFlyout = new BubbleFlyoutView(getContext());
563 mFlyout.setVisibility(GONE);
564 mFlyout.animate()
565 .setDuration(FLYOUT_ALPHA_ANIMATION_DURATION)
566 .setInterpolator(new AccelerateDecelerateInterpolator());
567 addView(mFlyout, new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
568 }
569
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700570 /**
Lyn Han02cca812019-04-02 16:27:32 -0700571 * Handle theme changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700572 */
Lyn Han02cca812019-04-02 16:27:32 -0700573 public void onThemeChanged() {
Mady Mellor8bfe5412019-07-31 14:56:44 -0700574 setUpFlyout();
Lyn Hanedb55e22020-02-12 18:55:39 -0800575 if (BubbleExperimentConfig.allowBubbleOverflow(mContext)) {
576 setOverflowBtnTheme();
577 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700578 }
579
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400580 /** Respond to the phone being rotated by repositioning the stack and hiding any flyouts. */
Lyn Hanf4730312019-06-18 11:18:58 -0700581 public void onOrientationChanged(int orientation) {
582 mOrientation = orientation;
583
Mady Mellore19353d2019-08-21 17:25:02 -0700584 // Display size is based on the rotation device was in when requested, we should update it
Mady Mellor9be3bed2019-08-21 17:26:26 -0700585 // We use the real size & subtract screen decorations / window insets ourselves when needed
Mady Mellore19353d2019-08-21 17:25:02 -0700586 WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
587 wm.getDefaultDisplay().getRealSize(mDisplaySize);
588
Mady Mellor818eef02019-08-16 16:12:29 -0700589 // Some resources change depending on orientation
590 Resources res = getContext().getResources();
591 mStatusBarHeight = res.getDimensionPixelSize(
592 com.android.internal.R.dimen.status_bar_height);
593 mBubblePaddingTop = res.getDimensionPixelSize(R.dimen.bubble_padding_top);
594
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400595 final RectF allowablePos = mStackAnimationController.getAllowableStackPositionRegion();
596 mWasOnLeftBeforeRotation = mStackAnimationController.isStackOnLeftSide();
597 mVerticalPosPercentBeforeRotation =
598 (mStackAnimationController.getStackPosition().y - allowablePos.top)
599 / (allowablePos.bottom - allowablePos.top);
Lyn Hanf4730312019-06-18 11:18:58 -0700600 addOnLayoutChangeListener(mOrientationChangedListener);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400601 hideFlyoutImmediate();
602 }
603
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800604 @Override
Mady Mellor217b2e92019-02-27 11:44:16 -0800605 public void getBoundsOnScreen(Rect outRect, boolean clipToParent) {
606 getBoundsOnScreen(outRect);
607 }
608
609 @Override
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800610 protected void onDetachedFromWindow() {
611 super.onDetachedFromWindow();
612 getViewTreeObserver().removeOnPreDrawListener(mViewUpdater);
613 }
614
615 @Override
Mady Mellor217b2e92019-02-27 11:44:16 -0800616 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
617 super.onInitializeAccessibilityNodeInfoInternal(info);
Lyn Hane68d0912019-05-02 18:28:01 -0700618
619 // Custom actions.
620 AccessibilityAction moveTopLeft = new AccessibilityAction(R.id.action_move_top_left,
621 getContext().getResources()
622 .getString(R.string.bubble_accessibility_action_move_top_left));
623 info.addAction(moveTopLeft);
624
625 AccessibilityAction moveTopRight = new AccessibilityAction(R.id.action_move_top_right,
626 getContext().getResources()
627 .getString(R.string.bubble_accessibility_action_move_top_right));
628 info.addAction(moveTopRight);
629
630 AccessibilityAction moveBottomLeft = new AccessibilityAction(R.id.action_move_bottom_left,
631 getContext().getResources()
632 .getString(R.string.bubble_accessibility_action_move_bottom_left));
633 info.addAction(moveBottomLeft);
634
635 AccessibilityAction moveBottomRight = new AccessibilityAction(R.id.action_move_bottom_right,
636 getContext().getResources()
637 .getString(R.string.bubble_accessibility_action_move_bottom_right));
638 info.addAction(moveBottomRight);
639
640 // Default actions.
641 info.addAction(AccessibilityAction.ACTION_DISMISS);
Mady Mellor217b2e92019-02-27 11:44:16 -0800642 if (mIsExpanded) {
Lyn Hane68d0912019-05-02 18:28:01 -0700643 info.addAction(AccessibilityAction.ACTION_COLLAPSE);
Mady Mellor217b2e92019-02-27 11:44:16 -0800644 } else {
Lyn Hane68d0912019-05-02 18:28:01 -0700645 info.addAction(AccessibilityAction.ACTION_EXPAND);
Mady Mellor217b2e92019-02-27 11:44:16 -0800646 }
647 }
648
649 @Override
650 public boolean performAccessibilityActionInternal(int action, Bundle arguments) {
651 if (super.performAccessibilityActionInternal(action, arguments)) {
652 return true;
653 }
Lyn Hane68d0912019-05-02 18:28:01 -0700654 final RectF stackBounds = mStackAnimationController.getAllowableStackPositionRegion();
655
656 // R constants are not final so we cannot use switch-case here.
657 if (action == AccessibilityNodeInfo.ACTION_DISMISS) {
658 mBubbleData.dismissAll(BubbleController.DISMISS_ACCESSIBILITY_ACTION);
659 return true;
660 } else if (action == AccessibilityNodeInfo.ACTION_COLLAPSE) {
661 mBubbleData.setExpanded(false);
662 return true;
663 } else if (action == AccessibilityNodeInfo.ACTION_EXPAND) {
664 mBubbleData.setExpanded(true);
665 return true;
666 } else if (action == R.id.action_move_top_left) {
667 mStackAnimationController.springStack(stackBounds.left, stackBounds.top);
668 return true;
669 } else if (action == R.id.action_move_top_right) {
670 mStackAnimationController.springStack(stackBounds.right, stackBounds.top);
671 return true;
672 } else if (action == R.id.action_move_bottom_left) {
673 mStackAnimationController.springStack(stackBounds.left, stackBounds.bottom);
674 return true;
675 } else if (action == R.id.action_move_bottom_right) {
676 mStackAnimationController.springStack(stackBounds.right, stackBounds.bottom);
677 return true;
Mady Mellor217b2e92019-02-27 11:44:16 -0800678 }
679 return false;
680 }
681
Lyn Han6c40fe72019-05-08 14:06:33 -0700682 /**
683 * Update content description for a11y TalkBack.
684 */
685 public void updateContentDescription() {
686 if (mBubbleData.getBubbles().isEmpty()) {
687 return;
688 }
689 Bubble topBubble = mBubbleData.getBubbles().get(0);
690 String appName = topBubble.getAppName();
Ned Burns00b4b2d2019-10-17 22:09:27 -0400691 Notification notification = topBubble.getEntry().getSbn().getNotification();
Lyn Han6c40fe72019-05-08 14:06:33 -0700692 CharSequence titleCharSeq = notification.extras.getCharSequence(Notification.EXTRA_TITLE);
693 String titleStr = getResources().getString(R.string.stream_notification);
694 if (titleCharSeq != null) {
695 titleStr = titleCharSeq.toString();
696 }
697 int moreCount = mBubbleContainer.getChildCount() - 1;
698
699 // Example: Title from app name.
700 String singleDescription = getResources().getString(
701 R.string.bubble_content_description_single, titleStr, appName);
702
703 // Example: Title from app name and 4 more.
704 String stackDescription = getResources().getString(
705 R.string.bubble_content_description_stack, titleStr, appName, moreCount);
706
707 if (mIsExpanded) {
708 // TODO(b/129522932) - update content description for each bubble in expanded view.
709 } else {
710 // Collapsed stack.
711 if (moreCount > 0) {
712 mBubbleContainer.setContentDescription(stackDescription);
713 } else {
714 mBubbleContainer.setContentDescription(singleDescription);
715 }
716 }
717 }
718
Mark Renouf821e6782019-04-01 14:17:37 -0400719 private void updateSystemGestureExcludeRects() {
720 // Exclude the region occupied by the first BubbleView in the stack
721 Rect excludeZone = mSystemGestureExclusionRects.get(0);
Lyn Hanc47e1712020-01-28 21:43:34 -0800722 if (getBubbleCount() > 0) {
Mark Renouf821e6782019-04-01 14:17:37 -0400723 View firstBubble = mBubbleContainer.getChildAt(0);
724 excludeZone.set(firstBubble.getLeft(), firstBubble.getTop(), firstBubble.getRight(),
725 firstBubble.getBottom());
726 excludeZone.offset((int) (firstBubble.getTranslationX() + 0.5f),
727 (int) (firstBubble.getTranslationY() + 0.5f));
728 mBubbleContainer.setSystemGestureExclusionRects(mSystemGestureExclusionRects);
729 } else {
730 excludeZone.setEmpty();
731 mBubbleContainer.setSystemGestureExclusionRects(Collections.emptyList());
732 }
733 }
734
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800735 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800736 * Sets the listener to notify when the bubble stack is expanded.
737 */
738 public void setExpandListener(BubbleController.BubbleExpandListener listener) {
739 mExpandListener = listener;
740 }
741
742 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800743 * Whether the stack of bubbles is expanded or not.
744 */
745 public boolean isExpanded() {
746 return mIsExpanded;
747 }
748
749 /**
Mady Mellor047e24e2019-08-05 11:35:40 -0700750 * Whether the stack of bubbles is animating to or from expansion.
751 */
752 public boolean isExpansionAnimating() {
753 return mIsExpansionAnimating;
754 }
755
756 /**
Mady Mellorb8aaf972019-11-26 10:28:00 -0800757 * The {@link BadgedImageView} that is expanded, null if one does not exist.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800758 */
Mady Mellorb8aaf972019-11-26 10:28:00 -0800759 BadgedImageView getExpandedBubbleView() {
Mady Mellored99c272019-06-13 15:58:30 -0700760 return mExpandedBubble != null ? mExpandedBubble.getIconView() : null;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800761 }
762
763 /**
764 * The {@link Bubble} that is expanded, null if one does not exist.
765 */
766 Bubble getExpandedBubble() {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800767 return mExpandedBubble;
768 }
769
Mark Renouf71a3af62019-04-08 15:02:54 -0400770 // via BubbleData.Listener
771 void addBubble(Bubble bubble) {
Issei Suzukia8d07312019-06-07 12:56:19 +0200772 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400773 Log.d(TAG, "addBubble: " + bubble);
774 }
Joshua Tsujib35f5912019-07-24 16:15:21 -0400775
Lyn Hanc47e1712020-01-28 21:43:34 -0800776 if (getBubbleCount() == 0) {
Joshua Tsujib35f5912019-07-24 16:15:21 -0400777 mStackOnLeftOrWillBe = mStackAnimationController.isStackOnLeftSide();
778 }
779
Joshua Tsujib35f5912019-07-24 16:15:21 -0400780 // Set the dot position to the opposite of the side the stack is resting on, since the stack
781 // resting slightly off-screen would result in the dot also being off-screen.
782 bubble.getIconView().setDotPosition(
783 !mStackOnLeftOrWillBe /* onLeft */, false /* animate */);
784
Mady Mellored99c272019-06-13 15:58:30 -0700785 mBubbleContainer.addView(bubble.getIconView(), 0,
Mark Renouf71a3af62019-04-08 15:02:54 -0400786 new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
Mady Mellored99c272019-06-13 15:58:30 -0700787 ViewClippingUtil.setClippingDeactivated(bubble.getIconView(), true, mClippingParameters);
Mark Renoufba5ab512019-05-02 15:21:01 -0400788 animateInFlyoutForBubble(bubble);
Lyn Hanb58c7562020-01-07 14:29:20 -0800789 updatePointerPosition();
790 updateOverflowBtnVisibility( /*apply */ true);
Mark Renouf71a3af62019-04-08 15:02:54 -0400791 requestUpdate();
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -0800792 logBubbleEvent(bubble, SysUiStatsLog.BUBBLE_UICHANGED__ACTION__POSTED);
Mark Renouf71a3af62019-04-08 15:02:54 -0400793 }
794
795 // via BubbleData.Listener
796 void removeBubble(Bubble bubble) {
Issei Suzukia8d07312019-06-07 12:56:19 +0200797 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400798 Log.d(TAG, "removeBubble: " + bubble);
799 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400800 // Remove it from the views
Mady Mellored99c272019-06-13 15:58:30 -0700801 int removedIndex = mBubbleContainer.indexOfChild(bubble.getIconView());
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400802 if (removedIndex >= 0) {
803 mBubbleContainer.removeViewAt(removedIndex);
Mark Renoufc19b4732019-06-26 12:08:33 -0400804 bubble.cleanupExpandedState();
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -0800805 logBubbleEvent(bubble, SysUiStatsLog.BUBBLE_UICHANGED__ACTION__DISMISSED);
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400806 } else {
807 Log.d(TAG, "was asked to remove Bubble, but didn't find the view! " + bubble);
808 }
Lyn Hanb58c7562020-01-07 14:29:20 -0800809 updateOverflowBtnVisibility(/* apply */ true);
810 }
811
812 private void updateOverflowBtnVisibility(boolean apply) {
813 if (!BubbleExperimentConfig.allowBubbleOverflow(mContext)) {
814 return;
815 }
816 if (mIsExpanded) {
817 if (DEBUG_BUBBLE_STACK_VIEW) {
Lyn Hanc47e1712020-01-28 21:43:34 -0800818 Log.d(TAG, "Show overflow button.");
Lyn Hanb58c7562020-01-07 14:29:20 -0800819 }
Lyn Hanb58c7562020-01-07 14:29:20 -0800820 mOverflowBtn.setVisibility(VISIBLE);
Lyn Hanb58c7562020-01-07 14:29:20 -0800821 if (apply) {
Lyn Hanc47e1712020-01-28 21:43:34 -0800822 mExpandedAnimationController.expandFromStack(() -> {
823 updatePointerPosition();
824 } /* after */);
Lyn Hanb58c7562020-01-07 14:29:20 -0800825 }
826 } else {
827 if (DEBUG_BUBBLE_STACK_VIEW) {
828 Log.d(TAG, "Collapsed. Hide overflow button.");
829 }
830 mOverflowBtn.setVisibility(GONE);
831 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400832 }
833
834 // via BubbleData.Listener
835 void updateBubble(Bubble bubble) {
Mark Renoufba5ab512019-05-02 15:21:01 -0400836 animateInFlyoutForBubble(bubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400837 requestUpdate();
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -0800838 logBubbleEvent(bubble, SysUiStatsLog.BUBBLE_UICHANGED__ACTION__UPDATED);
Mark Renouf71a3af62019-04-08 15:02:54 -0400839 }
840
Mark Renoufba5ab512019-05-02 15:21:01 -0400841 public void updateBubbleOrder(List<Bubble> bubbles) {
842 for (int i = 0; i < bubbles.size(); i++) {
843 Bubble bubble = bubbles.get(i);
Mady Mellored99c272019-06-13 15:58:30 -0700844 mBubbleContainer.reorderView(bubble.getIconView(), i);
Mark Renoufba5ab512019-05-02 15:21:01 -0400845 }
Joshua Tsuji2862f2e2019-07-29 12:32:33 -0400846
847 updateBubbleZOrdersAndDotPosition(false /* animate */);
Mark Renoufba5ab512019-05-02 15:21:01 -0400848 }
849
Lyn Han99e457f2020-01-31 10:14:19 -0800850 void showOverflow() {
851 setSelectedBubble(null);
852 }
853
Mady Melloredd4ee12019-01-18 10:45:11 -0800854 /**
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400855 * Changes the currently selected bubble. If the stack is already expanded, the newly selected
856 * bubble will be shown immediately. This does not change the expanded state or change the
857 * position of any bubble.
858 */
Mark Renouf71a3af62019-04-08 15:02:54 -0400859 // via BubbleData.Listener
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400860 public void setSelectedBubble(@Nullable Bubble bubbleToSelect) {
Issei Suzukia8d07312019-06-07 12:56:19 +0200861 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400862 Log.d(TAG, "setSelectedBubble: " + bubbleToSelect);
863 }
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400864 if (mExpandedBubble != null && mExpandedBubble.equals(bubbleToSelect)) {
865 return;
866 }
867 final Bubble previouslySelected = mExpandedBubble;
868 mExpandedBubble = bubbleToSelect;
Issei Suzukicac2a502019-04-16 16:52:50 +0200869
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400870 if (mIsExpanded) {
871 // Make the container of the expanded view transparent before removing the expanded view
872 // from it. Otherwise a punch hole created by {@link android.view.SurfaceView} in the
873 // expanded view becomes visible on the screen. See b/126856255
874 mExpandedViewContainer.setAlpha(0.0f);
875 mSurfaceSynchronizer.syncSurfaceAndRun(() -> {
Lyn Hana0bb02e2020-01-28 17:57:27 -0800876 if (previouslySelected == null) {
877 mOverflowExpandedView.setContentVisibility(false);
878 } else {
Issei Suzukicac2a502019-04-16 16:52:50 +0200879 previouslySelected.setContentVisibility(false);
880 }
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400881 updateExpandedBubble();
882 updatePointerPosition();
883 requestUpdate();
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -0800884 logBubbleEvent(previouslySelected,
885 SysUiStatsLog.BUBBLE_UICHANGED__ACTION__COLLAPSED);
886 logBubbleEvent(bubbleToSelect, SysUiStatsLog.BUBBLE_UICHANGED__ACTION__EXPANDED);
Mady Mellor99a302602019-06-14 11:39:56 -0700887 notifyExpansionChanged(previouslySelected, false /* expanded */);
888 notifyExpansionChanged(bubbleToSelect, true /* expanded */);
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400889 });
890 }
891 }
892
893 /**
894 * Changes the expanded state of the stack.
895 *
Mark Renouf71a3af62019-04-08 15:02:54 -0400896 * @param shouldExpand whether the bubble stack should appear expanded
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400897 */
Mark Renouf71a3af62019-04-08 15:02:54 -0400898 // via BubbleData.Listener
899 public void setExpanded(boolean shouldExpand) {
Issei Suzukia8d07312019-06-07 12:56:19 +0200900 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400901 Log.d(TAG, "setExpanded: " + shouldExpand);
902 }
Lyn Han285ad302019-05-29 19:01:39 -0700903 if (shouldExpand == mIsExpanded) {
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400904 return;
905 }
Lyn Han285ad302019-05-29 19:01:39 -0700906 if (mIsExpanded) {
907 animateCollapse();
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -0800908 logBubbleEvent(mExpandedBubble, SysUiStatsLog.BUBBLE_UICHANGED__ACTION__COLLAPSED);
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400909 } else {
Lyn Han285ad302019-05-29 19:01:39 -0700910 animateExpansion();
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400911 // TODO: move next line to BubbleData
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -0800912 logBubbleEvent(mExpandedBubble, SysUiStatsLog.BUBBLE_UICHANGED__ACTION__EXPANDED);
913 logBubbleEvent(mExpandedBubble, SysUiStatsLog.BUBBLE_UICHANGED__ACTION__STACK_EXPANDED);
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400914 }
Mady Mellor99a302602019-06-14 11:39:56 -0700915 notifyExpansionChanged(mExpandedBubble, mIsExpanded);
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400916 }
917
918 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800919 * Dismiss the stack of bubbles.
Lyn Han1b4f25e2019-06-11 13:56:34 -0700920 *
Mark Renouf71a3af62019-04-08 15:02:54 -0400921 * @deprecated
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800922 */
Mark Renouf71a3af62019-04-08 15:02:54 -0400923 @Deprecated
Mady Mellorc3d7d062019-03-28 16:13:05 -0700924 void stackDismissed(int reason) {
Issei Suzukia8d07312019-06-07 12:56:19 +0200925 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400926 Log.d(TAG, "stackDismissed: reason=" + reason);
927 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400928 mBubbleData.dismissAll(reason);
Steven Wua254dab2019-01-29 11:30:39 -0500929 logBubbleEvent(null /* no bubble associated with bubble stack dismiss */,
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -0800930 SysUiStatsLog.BUBBLE_UICHANGED__ACTION__STACK_DISMISSED);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800931 }
932
933 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800934 * @return the view the touch event is on
935 */
936 @Nullable
937 public View getTargetView(MotionEvent event) {
938 float x = event.getRawX();
939 float y = event.getRawY();
940 if (mIsExpanded) {
941 if (isIntersecting(mBubbleContainer, x, y)) {
Lyn Han99e457f2020-01-31 10:14:19 -0800942 if (BubbleExperimentConfig.allowBubbleOverflow(mContext)
943 && isIntersecting(mOverflowBtn, x, y)) {
944 return mOverflowBtn;
945 }
Mady Mellor47b11e32019-07-11 19:06:21 -0700946 // Could be tapping or dragging a bubble while expanded
Lyn Hanc47e1712020-01-28 21:43:34 -0800947 for (int i = 0; i < getBubbleCount(); i++) {
Mady Mellorb8aaf972019-11-26 10:28:00 -0800948 BadgedImageView view = (BadgedImageView) mBubbleContainer.getChildAt(i);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800949 if (isIntersecting(view, x, y)) {
950 return view;
951 }
952 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800953 }
Mady Mellor47b11e32019-07-11 19:06:21 -0700954 BubbleExpandedView bev = (BubbleExpandedView) mExpandedViewContainer.getChildAt(0);
955 if (bev.intersectingTouchableContent((int) x, (int) y)) {
956 return bev;
957 }
958 // Outside of the parts we care about.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800959 return null;
Joshua Tsuji6549e702019-05-02 13:13:16 -0400960 } else if (mFlyout.getVisibility() == VISIBLE && isIntersecting(mFlyout, x, y)) {
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400961 return mFlyout;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800962 }
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400963 // If it wasn't an individual bubble in the expanded state, or the flyout, it's the stack.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800964 return this;
965 }
966
Mark Renouf71a3af62019-04-08 15:02:54 -0400967 View getFlyoutView() {
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400968 return mFlyout;
969 }
970
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800971 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800972 * Collapses the stack of bubbles.
Mark Renoufcecc77b2019-01-30 16:32:24 -0500973 * <p>
974 * Must be called from the main thread.
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400975 *
976 * @deprecated use {@link #setExpanded(boolean)} and {@link #setSelectedBubble(Bubble)}
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800977 */
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400978 @Deprecated
Mark Renoufcecc77b2019-01-30 16:32:24 -0500979 @MainThread
Mark Renouf71a3af62019-04-08 15:02:54 -0400980 void collapseStack() {
Issei Suzukia8d07312019-06-07 12:56:19 +0200981 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400982 Log.d(TAG, "collapseStack()");
983 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400984 mBubbleData.setExpanded(false);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800985 }
986
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400987 /**
988 * @deprecated use {@link #setExpanded(boolean)} and {@link #setSelectedBubble(Bubble)}
989 */
990 @Deprecated
991 @MainThread
Mady Mellor9801e852019-01-22 14:50:28 -0800992 void collapseStack(Runnable endRunnable) {
Issei Suzukia8d07312019-06-07 12:56:19 +0200993 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400994 Log.d(TAG, "collapseStack(endRunnable)");
995 }
Mady Mellor9801e852019-01-22 14:50:28 -0800996 collapseStack();
997 // TODO - use the runnable at end of animation
998 endRunnable.run();
999 }
1000
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001001 /**
Mady Mellor94d94a72019-03-05 18:16:59 -08001002 * Expands the stack of bubbles.
Mark Renoufcecc77b2019-01-30 16:32:24 -05001003 * <p>
1004 * Must be called from the main thread.
Mark Renoufc6ab73d2019-04-09 16:42:22 -04001005 *
1006 * @deprecated use {@link #setExpanded(boolean)} and {@link #setSelectedBubble(Bubble)}
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001007 */
Mark Renoufc6ab73d2019-04-09 16:42:22 -04001008 @Deprecated
Mark Renoufcecc77b2019-01-30 16:32:24 -05001009 @MainThread
Mark Renouf71a3af62019-04-08 15:02:54 -04001010 void expandStack() {
Issei Suzukia8d07312019-06-07 12:56:19 +02001011 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001012 Log.d(TAG, "expandStack()");
1013 }
Mark Renouf71a3af62019-04-08 15:02:54 -04001014 mBubbleData.setExpanded(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001015 }
1016
Lyn Han285ad302019-05-29 19:01:39 -07001017 private void beforeExpandedViewAnimation() {
1018 hideFlyoutImmediate();
1019 updateExpandedBubble();
1020 updateExpandedView();
1021 mIsExpansionAnimating = true;
1022 }
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001023
Lyn Han285ad302019-05-29 19:01:39 -07001024 private void afterExpandedViewAnimation() {
1025 updateExpandedView();
1026 mIsExpansionAnimating = false;
1027 requestUpdate();
1028 }
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001029
Lyn Han285ad302019-05-29 19:01:39 -07001030 private void animateCollapse() {
1031 mIsExpanded = false;
Issei Suzuki71f7ae42019-07-11 14:19:50 +02001032 final Bubble previouslySelected = mExpandedBubble;
Lyn Han285ad302019-05-29 19:01:39 -07001033 beforeExpandedViewAnimation();
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001034
Lyn Hanb58c7562020-01-07 14:29:20 -08001035 if (DEBUG_BUBBLE_STACK_VIEW) {
1036 Log.d(TAG, "animateCollapse");
1037 Log.d(TAG, BubbleDebugConfig.formatBubblesString(this.getBubblesOnScreen(),
1038 this.getExpandedBubble()));
1039 }
1040 updateOverflowBtnVisibility(/* apply */ false);
Lyn Han285ad302019-05-29 19:01:39 -07001041 mBubbleContainer.cancelAllAnimations();
1042 mExpandedAnimationController.collapseBackToStack(
Joshua Tsuji61b38f52019-05-31 16:20:22 -04001043 mStackAnimationController.getStackPositionAlongNearestHorizontalEdge()
1044 /* collapseTo */,
Lyn Han285ad302019-05-29 19:01:39 -07001045 () -> {
Joshua Tsuji61b38f52019-05-31 16:20:22 -04001046 mBubbleContainer.setActiveController(mStackAnimationController);
Lyn Han285ad302019-05-29 19:01:39 -07001047 afterExpandedViewAnimation();
Lyn Hana0bb02e2020-01-28 17:57:27 -08001048 if (previouslySelected == null) {
1049 mOverflowExpandedView.setContentVisibility(false);
1050 } else {
Lyn Hanb58c7562020-01-07 14:29:20 -08001051 previouslySelected.setContentVisibility(false);
1052 }
Lyn Han285ad302019-05-29 19:01:39 -07001053 });
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001054
Lyn Han285ad302019-05-29 19:01:39 -07001055 mExpandedViewXAnim.animateToFinalPosition(getCollapsedX());
1056 mExpandedViewYAnim.animateToFinalPosition(getCollapsedY());
1057 mExpandedViewContainer.animate()
1058 .setDuration(100)
1059 .alpha(0f);
1060 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001061
Lyn Han285ad302019-05-29 19:01:39 -07001062 private void animateExpansion() {
1063 mIsExpanded = true;
1064 beforeExpandedViewAnimation();
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001065
Joshua Tsuji61b38f52019-05-31 16:20:22 -04001066 mBubbleContainer.setActiveController(mExpandedAnimationController);
Lyn Hanb58c7562020-01-07 14:29:20 -08001067 updateOverflowBtnVisibility(/* apply */ false);
Joshua Tsuji61b38f52019-05-31 16:20:22 -04001068 mExpandedAnimationController.expandFromStack(() -> {
1069 updatePointerPosition();
1070 afterExpandedViewAnimation();
1071 } /* after */);
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001072
Lyn Han285ad302019-05-29 19:01:39 -07001073 mExpandedViewContainer.setTranslationX(getCollapsedX());
1074 mExpandedViewContainer.setTranslationY(getCollapsedY());
1075 mExpandedViewContainer.setAlpha(0f);
1076
1077 mExpandedViewXAnim.animateToFinalPosition(0f);
1078 mExpandedViewYAnim.animateToFinalPosition(getExpandedViewY());
1079 mExpandedViewContainer.animate()
1080 .setDuration(100)
1081 .alpha(1f);
1082 }
1083
1084 private float getCollapsedX() {
1085 return mStackAnimationController.getStackPosition().x < getWidth() / 2
1086 ? -mExpandedAnimateXDistance
1087 : mExpandedAnimateXDistance;
1088 }
1089
1090 private float getCollapsedY() {
1091 return Math.min(mStackAnimationController.getStackPosition().y,
1092 mExpandedAnimateYDistance);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001093 }
1094
Mady Mellor99a302602019-06-14 11:39:56 -07001095 private void notifyExpansionChanged(Bubble bubble, boolean expanded) {
1096 if (mExpandListener != null && bubble != null) {
1097 mExpandListener.onBubbleExpandChanged(expanded, bubble.getKey());
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001098 }
1099 }
1100
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001101 /** Return the BubbleView at the given index from the bubble container. */
Mady Mellorb8aaf972019-11-26 10:28:00 -08001102 public BadgedImageView getBubbleAt(int i) {
Lyn Hanc47e1712020-01-28 21:43:34 -08001103 return getBubbleCount() > i
Mady Mellorb8aaf972019-11-26 10:28:00 -08001104 ? (BadgedImageView) mBubbleContainer.getChildAt(i)
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001105 : null;
1106 }
1107
Joshua Tsujia19515f2019-02-13 18:02:29 -05001108 /** Moves the bubbles out of the way if they're going to be over the keyboard. */
1109 public void onImeVisibilityChanged(boolean visible, int height) {
Mady Mellordf611cf2019-08-21 17:28:49 -07001110 mStackAnimationController.setImeHeight(visible ? height + mImeOffset : 0);
Joshua Tsuji4b395912019-04-19 17:18:40 -04001111
Joshua Tsujia19515f2019-02-13 18:02:29 -05001112 if (!mIsExpanded) {
Joshua Tsuji4b395912019-04-19 17:18:40 -04001113 mStackAnimationController.animateForImeVisibility(visible);
Joshua Tsujia19515f2019-02-13 18:02:29 -05001114 }
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001115 }
1116
1117 /** Called when a drag operation on an individual bubble has started. */
Joshua Tsuji442b6272019-02-08 13:23:43 -05001118 public void onBubbleDragStart(View bubble) {
Issei Suzukia8d07312019-06-07 12:56:19 +02001119 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001120 Log.d(TAG, "onBubbleDragStart: bubble=" + bubble);
1121 }
Joshua Tsuji442b6272019-02-08 13:23:43 -05001122 mExpandedAnimationController.prepareForBubbleDrag(bubble);
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001123 }
1124
1125 /** Called with the coordinates to which an individual bubble has been dragged. */
Joshua Tsuji442b6272019-02-08 13:23:43 -05001126 public void onBubbleDragged(View bubble, float x, float y) {
Mady Mellorbc078c22019-03-26 17:10:34 -07001127 if (!mIsExpanded || mIsExpansionAnimating) {
Joshua Tsuji442b6272019-02-08 13:23:43 -05001128 return;
1129 }
1130
1131 mExpandedAnimationController.dragBubbleOut(bubble, x, y);
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001132 springInDismissTarget();
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001133 }
1134
1135 /** Called when a drag operation on an individual bubble has finished. */
Joshua Tsuji442b6272019-02-08 13:23:43 -05001136 public void onBubbleDragFinish(
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001137 View bubble, float x, float y, float velX, float velY) {
Issei Suzukia8d07312019-06-07 12:56:19 +02001138 if (DEBUG_BUBBLE_STACK_VIEW) {
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001139 Log.d(TAG, "onBubbleDragFinish: bubble=" + bubble);
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001140 }
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001141
Mady Mellorbc078c22019-03-26 17:10:34 -07001142 if (!mIsExpanded || mIsExpansionAnimating) {
Joshua Tsuji442b6272019-02-08 13:23:43 -05001143 return;
1144 }
1145
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001146 mExpandedAnimationController.snapBubbleBack(bubble, velX, velY);
Lyn Han634483c2019-06-28 16:52:47 -07001147 hideDismissTarget();
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001148 }
1149
1150 void onDragStart() {
Issei Suzukia8d07312019-06-07 12:56:19 +02001151 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001152 Log.d(TAG, "onDragStart()");
1153 }
Mady Mellorbc078c22019-03-26 17:10:34 -07001154 if (mIsExpanded || mIsExpansionAnimating) {
Lyn Hanb58c7562020-01-07 14:29:20 -08001155 if (DEBUG_BUBBLE_STACK_VIEW) {
1156 Log.d(TAG, "mIsExpanded or mIsExpansionAnimating");
1157 }
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001158 return;
1159 }
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001160 mStackAnimationController.cancelStackPositionAnimations();
Joshua Tsujic36ee6f2019-05-28 17:00:16 -04001161 mBubbleContainer.setActiveController(mStackAnimationController);
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001162 hideFlyoutImmediate();
1163
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001164 mDraggingInDismissTarget = false;
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001165 }
1166
1167 void onDragged(float x, float y) {
Mady Mellorbc078c22019-03-26 17:10:34 -07001168 if (mIsExpanded || mIsExpansionAnimating) {
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001169 return;
1170 }
1171
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001172 springInDismissTarget();
1173 mStackAnimationController.moveStackFromTouch(x, y);
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001174 }
1175
1176 void onDragFinish(float x, float y, float velX, float velY) {
Issei Suzukia8d07312019-06-07 12:56:19 +02001177 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001178 Log.d(TAG, "onDragFinish");
1179 }
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001180
Mady Mellorbc078c22019-03-26 17:10:34 -07001181 if (mIsExpanded || mIsExpansionAnimating) {
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001182 return;
1183 }
1184
Joshua Tsuji6549e702019-05-02 13:13:16 -04001185 final float newStackX = mStackAnimationController.flingStackThenSpringToEdge(x, velX, velY);
Steven Wua254dab2019-01-29 11:30:39 -05001186 logBubbleEvent(null /* no bubble associated with bubble stack move */,
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -08001187 SysUiStatsLog.BUBBLE_UICHANGED__ACTION__STACK_MOVED);
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001188
Joshua Tsuji6549e702019-05-02 13:13:16 -04001189 mStackOnLeftOrWillBe = newStackX <= 0;
Joshua Tsuji2862f2e2019-07-29 12:32:33 -04001190 updateBubbleZOrdersAndDotPosition(true /* animate */);
Lyn Han634483c2019-06-28 16:52:47 -07001191 hideDismissTarget();
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001192 }
1193
Joshua Tsuji6549e702019-05-02 13:13:16 -04001194 void onFlyoutDragStart() {
1195 mFlyout.removeCallbacks(mHideFlyout);
1196 }
1197
1198 void onFlyoutDragged(float deltaX) {
Joshua Tsuji8e05aab2019-08-22 14:57:50 -04001199 // This shouldn't happen, but if it does, just wait until the flyout lays out. This method
1200 // is continually called.
1201 if (mFlyout.getWidth() <= 0) {
1202 return;
1203 }
1204
Joshua Tsuji6549e702019-05-02 13:13:16 -04001205 final boolean onLeft = mStackAnimationController.isStackOnLeftSide();
1206 mFlyoutDragDeltaX = deltaX;
1207
1208 final float collapsePercent =
1209 onLeft ? -deltaX / mFlyout.getWidth() : deltaX / mFlyout.getWidth();
1210 mFlyout.setCollapsePercent(Math.min(1f, Math.max(0f, collapsePercent)));
1211
Lyn Han61d5d562019-07-01 17:39:38 -07001212 // Calculate how to translate the flyout if it has been dragged too far in either direction.
Joshua Tsuji6549e702019-05-02 13:13:16 -04001213 float overscrollTranslation = 0f;
1214 if (collapsePercent < 0f || collapsePercent > 1f) {
1215 // Whether we are more than 100% transitioned to the dot.
1216 final boolean overscrollingPastDot = collapsePercent > 1f;
1217
1218 // Whether we are overscrolling physically to the left - this can either be pulling the
1219 // flyout away from the stack (if the stack is on the right) or pushing it to the left
1220 // after it has already become the dot.
1221 final boolean overscrollingLeft =
1222 (onLeft && collapsePercent > 1f) || (!onLeft && collapsePercent < 0f);
Joshua Tsuji6549e702019-05-02 13:13:16 -04001223 overscrollTranslation =
1224 (overscrollingPastDot ? collapsePercent - 1f : collapsePercent * -1)
1225 * (overscrollingLeft ? -1 : 1)
1226 * (mFlyout.getWidth() / (FLYOUT_OVERSCROLL_ATTENUATION_FACTOR
Lyn Han522e9ff2019-05-17 13:26:13 -07001227 // Attenuate the smaller dot less than the larger flyout.
1228 / (overscrollingPastDot ? 2 : 1)));
Joshua Tsuji6549e702019-05-02 13:13:16 -04001229 }
1230
1231 mFlyout.setTranslationX(mFlyout.getRestingTranslationX() + overscrollTranslation);
1232 }
1233
1234 /**
Joshua Tsuji14e68552019-06-06 17:17:08 -04001235 * Set when the flyout is tapped, so that we can expand the bubble associated with the flyout
1236 * once it collapses.
1237 */
1238 @Nullable private Bubble mBubbleToExpandAfterFlyoutCollapse = null;
1239
1240 void onFlyoutTapped() {
1241 mBubbleToExpandAfterFlyoutCollapse = mBubbleData.getSelectedBubble();
1242
1243 mFlyout.removeCallbacks(mHideFlyout);
1244 mHideFlyout.run();
1245 }
1246
1247 /**
Joshua Tsuji6549e702019-05-02 13:13:16 -04001248 * Called when the flyout drag has finished, and returns true if the gesture successfully
1249 * dismissed the flyout.
1250 */
1251 void onFlyoutDragFinished(float deltaX, float velX) {
1252 final boolean onLeft = mStackAnimationController.isStackOnLeftSide();
1253 final boolean metRequiredVelocity =
1254 onLeft ? velX < -FLYOUT_DISMISS_VELOCITY : velX > FLYOUT_DISMISS_VELOCITY;
1255 final boolean metRequiredDeltaX =
1256 onLeft
1257 ? deltaX < -mFlyout.getWidth() * FLYOUT_DRAG_PERCENT_DISMISS
1258 : deltaX > mFlyout.getWidth() * FLYOUT_DRAG_PERCENT_DISMISS;
1259 final boolean isCancelFling = onLeft ? velX > 0 : velX < 0;
1260 final boolean shouldDismiss = metRequiredVelocity || (metRequiredDeltaX && !isCancelFling);
1261
1262 mFlyout.removeCallbacks(mHideFlyout);
1263 animateFlyoutCollapsed(shouldDismiss, velX);
1264 }
1265
1266 /**
1267 * Called when the first touch event of a gesture (stack drag, bubble drag, flyout drag, etc.)
1268 * is received.
1269 */
1270 void onGestureStart() {
1271 mIsGestureInProgress = true;
1272 }
1273
1274 /** Called when a gesture is completed or cancelled. */
1275 void onGestureFinished() {
1276 mIsGestureInProgress = false;
Joshua Tsujif49ee142019-05-29 16:32:01 -04001277
1278 if (mIsExpanded) {
1279 mExpandedAnimationController.onGestureFinished();
1280 }
Joshua Tsuji6549e702019-05-02 13:13:16 -04001281 }
1282
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001283 /** Prepares and starts the desaturate/darken animation on the bubble stack. */
1284 private void animateDesaturateAndDarken(View targetView, boolean desaturateAndDarken) {
1285 mDesaturateAndDarkenTargetView = targetView;
1286
1287 if (desaturateAndDarken) {
1288 // Use the animated paint for the bubbles.
1289 mDesaturateAndDarkenTargetView.setLayerType(
1290 View.LAYER_TYPE_HARDWARE, mDesaturateAndDarkenPaint);
1291 mDesaturateAndDarkenAnimator.removeAllListeners();
1292 mDesaturateAndDarkenAnimator.start();
1293 } else {
1294 mDesaturateAndDarkenAnimator.removeAllListeners();
1295 mDesaturateAndDarkenAnimator.addListener(new AnimatorListenerAdapter() {
1296 @Override
1297 public void onAnimationEnd(Animator animation) {
1298 super.onAnimationEnd(animation);
1299 // Stop using the animated paint.
1300 resetDesaturationAndDarken();
1301 }
1302 });
1303 mDesaturateAndDarkenAnimator.reverse();
1304 }
1305 }
1306
1307 private void resetDesaturationAndDarken() {
1308 mDesaturateAndDarkenAnimator.removeAllListeners();
1309 mDesaturateAndDarkenAnimator.cancel();
1310 mDesaturateAndDarkenTargetView.setLayerType(View.LAYER_TYPE_NONE, null);
1311 }
1312
1313 /**
1314 * Magnets the stack to the target, while also transforming the target to encircle the stack and
1315 * desaturating/darkening the bubbles.
1316 */
1317 void animateMagnetToDismissTarget(
1318 View magnetView, boolean toTarget, float x, float y, float velX, float velY) {
1319 mDraggingInDismissTarget = toTarget;
1320
1321 if (toTarget) {
1322 // The Y-value for the bubble stack to be positioned in the center of the dismiss target
1323 final float destY = mDismissContainer.getDismissTargetCenterY() - mBubbleSize / 2f;
1324
1325 mAnimatingMagnet = true;
1326
1327 final Runnable afterMagnet = () -> {
1328 mAnimatingMagnet = false;
1329 if (mAfterMagnet != null) {
1330 mAfterMagnet.run();
1331 }
1332 };
1333
1334 if (magnetView == this) {
1335 mStackAnimationController.magnetToDismiss(velX, velY, destY, afterMagnet);
1336 animateDesaturateAndDarken(mBubbleContainer, true);
1337 } else {
1338 mExpandedAnimationController.magnetBubbleToDismiss(
1339 magnetView, velX, velY, destY, afterMagnet);
1340
1341 animateDesaturateAndDarken(magnetView, true);
1342 }
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001343 } else {
1344 mAnimatingMagnet = false;
1345
1346 if (magnetView == this) {
1347 mStackAnimationController.demagnetizeFromDismissToPoint(x, y, velX, velY);
1348 animateDesaturateAndDarken(mBubbleContainer, false);
1349 } else {
1350 mExpandedAnimationController.demagnetizeBubbleTo(x, y, velX, velY);
1351 animateDesaturateAndDarken(magnetView, false);
1352 }
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001353 }
1354
1355 mVibrator.vibrate(VibrationEffect.get(toTarget
1356 ? VibrationEffect.EFFECT_CLICK
1357 : VibrationEffect.EFFECT_TICK));
1358 }
1359
1360 /**
1361 * Magnets the stack to the dismiss target if it's not already there. Then, dismiss the stack
1362 * using the 'implode' animation and animate out the target.
1363 */
1364 void magnetToStackIfNeededThenAnimateDismissal(
1365 View touchedView, float velX, float velY, Runnable after) {
Joshua Tsujif49ee142019-05-29 16:32:01 -04001366 final View draggedOutBubble = mExpandedAnimationController.getDraggedOutBubble();
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001367 final Runnable animateDismissal = () -> {
1368 mAfterMagnet = null;
1369
1370 mVibrator.vibrate(VibrationEffect.get(VibrationEffect.EFFECT_CLICK));
Lyn Han634483c2019-06-28 16:52:47 -07001371 mDismissContainer.springOut();
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001372
1373 // 'Implode' the stack and then hide the dismiss target.
1374 if (touchedView == this) {
1375 mStackAnimationController.implodeStack(
1376 () -> {
1377 mAnimatingMagnet = false;
1378 mShowingDismiss = false;
1379 mDraggingInDismissTarget = false;
1380 after.run();
1381 resetDesaturationAndDarken();
1382 });
1383 } else {
Joshua Tsujif49ee142019-05-29 16:32:01 -04001384 mExpandedAnimationController.dismissDraggedOutBubble(draggedOutBubble, () -> {
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001385 mAnimatingMagnet = false;
1386 mShowingDismiss = false;
1387 mDraggingInDismissTarget = false;
1388 resetDesaturationAndDarken();
1389 after.run();
1390 });
1391 }
1392 };
1393
1394 if (mAnimatingMagnet) {
1395 // If the magnet animation is currently playing, dismiss the stack after it's done. This
1396 // happens if the stack is flung towards the target.
1397 mAfterMagnet = animateDismissal;
1398 } else if (mDraggingInDismissTarget) {
1399 // If we're in the dismiss target, but not animating, we already magneted - dismiss
1400 // immediately.
1401 animateDismissal.run();
1402 } else {
1403 // Otherwise, we need to start the magnet animation and then dismiss afterward.
1404 animateMagnetToDismissTarget(touchedView, true, -1 /* x */, -1 /* y */, velX, velY);
1405 mAfterMagnet = animateDismissal;
1406 }
1407 }
1408
Lyn Han634483c2019-06-28 16:52:47 -07001409 /** Animates in the dismiss target. */
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001410 private void springInDismissTarget() {
1411 if (mShowingDismiss) {
1412 return;
1413 }
1414
1415 mShowingDismiss = true;
1416
1417 // Show the dismiss container and bring it to the front so the bubbles will go behind it.
1418 mDismissContainer.springIn();
1419 mDismissContainer.bringToFront();
1420 mDismissContainer.setZ(Short.MAX_VALUE - 1);
1421 }
1422
1423 /**
1424 * Animates the dismiss target out, as well as the circle that encircles the bubbles, if they
1425 * were dragged into the target and encircled.
1426 */
Lyn Han634483c2019-06-28 16:52:47 -07001427 private void hideDismissTarget() {
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001428 if (!mShowingDismiss) {
1429 return;
1430 }
1431
1432 mDismissContainer.springOut();
1433 mShowingDismiss = false;
1434 }
1435
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001436 /** Whether the location of the given MotionEvent is within the dismiss target area. */
Joshua Tsuji6549e702019-05-02 13:13:16 -04001437 boolean isInDismissTarget(MotionEvent ev) {
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001438 return isIntersecting(mDismissContainer.getDismissTarget(), ev.getRawX(), ev.getRawY());
Joshua Tsuji19e22e4242019-04-17 13:29:10 -04001439 }
1440
Joshua Tsuji6549e702019-05-02 13:13:16 -04001441 /** Animates the flyout collapsed (to dot), or the reverse, starting with the given velocity. */
1442 private void animateFlyoutCollapsed(boolean collapsed, float velX) {
1443 final boolean onLeft = mStackAnimationController.isStackOnLeftSide();
Joshua Tsuji14e68552019-06-06 17:17:08 -04001444 // If the flyout was tapped, we want a higher stiffness for the collapse animation so it's
1445 // faster.
1446 mFlyoutTransitionSpring.getSpring().setStiffness(
1447 (mBubbleToExpandAfterFlyoutCollapse != null)
1448 ? SpringForce.STIFFNESS_MEDIUM
1449 : SpringForce.STIFFNESS_LOW);
Joshua Tsuji6549e702019-05-02 13:13:16 -04001450 mFlyoutTransitionSpring
1451 .setStartValue(mFlyoutDragDeltaX)
1452 .setStartVelocity(velX)
1453 .animateToFinalPosition(collapsed
1454 ? (onLeft ? -mFlyout.getWidth() : mFlyout.getWidth())
1455 : 0f);
1456 }
1457
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001458 /**
Mady Mellor44ee2fe2019-01-30 17:51:16 -08001459 * Calculates the y position of the expanded view when it is expanded.
1460 */
Lyn Han285ad302019-05-29 19:01:39 -07001461 float getExpandedViewY() {
Lyn Han4a8efe32019-05-30 09:43:27 -07001462 return getStatusBarHeight() + mBubbleSize + mBubblePaddingTop + mPointerHeight;
Mady Mellor44ee2fe2019-01-30 17:51:16 -08001463 }
1464
1465 /**
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001466 * Animates in the flyout for the given bubble, if available, and then hides it after some time.
1467 */
1468 @VisibleForTesting
1469 void animateInFlyoutForBubble(Bubble bubble) {
Mady Mellordf898fd2020-01-09 09:26:36 -08001470 Bubble.FlyoutMessage flyoutMessage = bubble.getFlyoutMessage();
Mady Mellorb8aaf972019-11-26 10:28:00 -08001471 final BadgedImageView bubbleView = bubble.getIconView();
Mady Mellordf898fd2020-01-09 09:26:36 -08001472 if (flyoutMessage == null
1473 || flyoutMessage.message == null
Mady Mellorb8aaf972019-11-26 10:28:00 -08001474 || !bubble.showFlyout()
Mark Renoufc19b4732019-06-26 12:08:33 -04001475 || isExpanded()
1476 || mIsExpansionAnimating
Joshua Tsuji14e68552019-06-06 17:17:08 -04001477 || mIsGestureInProgress
Lyn Hanf1f2c332019-08-23 17:06:56 -07001478 || mBubbleToExpandAfterFlyoutCollapse != null
Mady Mellorb8aaf972019-11-26 10:28:00 -08001479 || bubbleView == null) {
1480 if (bubbleView != null) {
1481 bubbleView.setDotState(DOT_STATE_DEFAULT);
1482 }
Joshua Tsuji14e68552019-06-06 17:17:08 -04001483 // Skip the message if none exists, we're expanded or animating expansion, or we're
Lyn Hanf1f2c332019-08-23 17:06:56 -07001484 // about to expand a bubble from the previous tapped flyout, or if bubble view is null.
Mark Renoufc19b4732019-06-26 12:08:33 -04001485 return;
1486 }
Mady Mellorb8aaf972019-11-26 10:28:00 -08001487
Lyn Hanf1f2c332019-08-23 17:06:56 -07001488 mFlyoutDragDeltaX = 0f;
1489 clearFlyoutOnHide();
Mady Mellorb8aaf972019-11-26 10:28:00 -08001490 mAfterFlyoutHidden = () -> {
1491 // Null it out to ensure it runs once.
1492 mAfterFlyoutHidden = null;
1493
1494 if (mBubbleToExpandAfterFlyoutCollapse != null) {
1495 // User tapped on the flyout and we should expand
1496 mBubbleData.setSelectedBubble(mBubbleToExpandAfterFlyoutCollapse);
1497 mBubbleData.setExpanded(true);
1498 mBubbleToExpandAfterFlyoutCollapse = null;
Joshua Tsuji36b1b2c2019-04-18 16:27:35 -04001499 }
Mady Mellorb8aaf972019-11-26 10:28:00 -08001500 bubbleView.setDotState(DOT_STATE_DEFAULT);
Lyn Hanf1f2c332019-08-23 17:06:56 -07001501 };
1502 mFlyout.setVisibility(INVISIBLE);
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001503
Mady Mellorb8aaf972019-11-26 10:28:00 -08001504 // Don't show the dot when we're animating the flyout
1505 bubbleView.setDotState(DOT_STATE_SUPPRESSED_FOR_FLYOUT);
Joshua Tsuji14e68552019-06-06 17:17:08 -04001506
Lyn Hanf1f2c332019-08-23 17:06:56 -07001507 // Start flyout expansion. Post in case layout isn't complete and getWidth returns 0.
1508 post(() -> {
1509 // An auto-expanding bubble could have been posted during the time it takes to
1510 // layout.
1511 if (isExpanded()) {
1512 return;
1513 }
1514 final Runnable expandFlyoutAfterDelay = () -> {
1515 mAnimateInFlyout = () -> {
1516 mFlyout.setVisibility(VISIBLE);
1517 mFlyoutDragDeltaX =
1518 mStackAnimationController.isStackOnLeftSide()
1519 ? -mFlyout.getWidth()
1520 : mFlyout.getWidth();
1521 animateFlyoutCollapsed(false /* collapsed */, 0 /* velX */);
1522 mFlyout.postDelayed(mHideFlyout, FLYOUT_HIDE_AFTER);
Joshua Tsuji14e68552019-06-06 17:17:08 -04001523 };
Lyn Hanf1f2c332019-08-23 17:06:56 -07001524 mFlyout.postDelayed(mAnimateInFlyout, 200);
1525 };
Mady Mellordf898fd2020-01-09 09:26:36 -08001526 mFlyout.setupFlyoutStartingAsDot(flyoutMessage,
1527 mStackAnimationController.getStackPosition(), getWidth(),
Lyn Hanf1f2c332019-08-23 17:06:56 -07001528 mStackAnimationController.isStackOnLeftSide(),
Mady Mellor05e860b2019-10-30 22:48:15 -07001529 bubble.getIconView().getDotColor() /* dotColor */,
Lyn Hanf1f2c332019-08-23 17:06:56 -07001530 expandFlyoutAfterDelay /* onLayoutComplete */,
Mady Mellorb8aaf972019-11-26 10:28:00 -08001531 mAfterFlyoutHidden,
1532 bubble.getIconView().getDotCenter(),
1533 !bubble.showDot());
Lyn Hanf1f2c332019-08-23 17:06:56 -07001534 mFlyout.bringToFront();
1535 });
Mark Renoufc19b4732019-06-26 12:08:33 -04001536 mFlyout.removeCallbacks(mHideFlyout);
1537 mFlyout.postDelayed(mHideFlyout, FLYOUT_HIDE_AFTER);
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -08001538 logBubbleEvent(bubble, SysUiStatsLog.BUBBLE_UICHANGED__ACTION__FLYOUT);
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001539 }
1540
1541 /** Hide the flyout immediately and cancel any pending hide runnables. */
1542 private void hideFlyoutImmediate() {
Lyn Hanf1f2c332019-08-23 17:06:56 -07001543 clearFlyoutOnHide();
Joshua Tsuji14e68552019-06-06 17:17:08 -04001544 mFlyout.removeCallbacks(mAnimateInFlyout);
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001545 mFlyout.removeCallbacks(mHideFlyout);
Joshua Tsuji6549e702019-05-02 13:13:16 -04001546 mFlyout.hideFlyout();
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001547 }
1548
Lyn Hanf1f2c332019-08-23 17:06:56 -07001549 private void clearFlyoutOnHide() {
1550 mFlyout.removeCallbacks(mAnimateInFlyout);
Mady Mellorb8aaf972019-11-26 10:28:00 -08001551 if (mAfterFlyoutHidden == null) {
Lyn Hanf1f2c332019-08-23 17:06:56 -07001552 return;
1553 }
Mady Mellorb8aaf972019-11-26 10:28:00 -08001554 mAfterFlyoutHidden.run();
1555 mAfterFlyoutHidden = null;
Lyn Hanf1f2c332019-08-23 17:06:56 -07001556 }
1557
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001558 @Override
1559 public void getBoundsOnScreen(Rect outRect) {
Aran Inkaa4dfa72019-11-18 16:49:07 -05001560 // If the bubble menu is open, the entire screen should capture touch events.
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001561 if (!mIsExpanded) {
Lyn Hanc47e1712020-01-28 21:43:34 -08001562 if (getBubbleCount() > 0) {
Mady Mellor217b2e92019-02-27 11:44:16 -08001563 mBubbleContainer.getChildAt(0).getBoundsOnScreen(outRect);
1564 }
Mady Mellore9371bc2019-07-10 18:50:59 -07001565 // Increase the touch target size of the bubble
1566 outRect.top -= mBubbleTouchPadding;
1567 outRect.left -= mBubbleTouchPadding;
1568 outRect.right += mBubbleTouchPadding;
1569 outRect.bottom += mBubbleTouchPadding;
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001570 } else {
1571 mBubbleContainer.getBoundsOnScreen(outRect);
1572 }
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001573
Joshua Tsuji6549e702019-05-02 13:13:16 -04001574 if (mFlyout.getVisibility() == View.VISIBLE) {
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001575 final Rect flyoutBounds = new Rect();
1576 mFlyout.getBoundsOnScreen(flyoutBounds);
1577 outRect.union(flyoutBounds);
1578 }
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001579 }
1580
1581 private int getStatusBarHeight() {
1582 if (getRootWindowInsets() != null) {
Joshua Tsuji0fee7682019-01-25 11:37:49 -05001583 WindowInsets insets = getRootWindowInsets();
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001584 return Math.max(
Joshua Tsujif44347f2019-02-12 14:28:06 -05001585 mStatusBarHeight,
Joshua Tsuji0fee7682019-01-25 11:37:49 -05001586 insets.getDisplayCutout() != null
1587 ? insets.getDisplayCutout().getSafeInsetTop()
1588 : 0);
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001589 }
1590
1591 return 0;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001592 }
1593
1594 private boolean isIntersecting(View view, float x, float y) {
1595 mTempLoc = view.getLocationOnScreen();
1596 mTempRect.set(mTempLoc[0], mTempLoc[1], mTempLoc[0] + view.getWidth(),
1597 mTempLoc[1] + view.getHeight());
1598 return mTempRect.contains(x, y);
1599 }
1600
1601 private void requestUpdate() {
Mady Mellorbc078c22019-03-26 17:10:34 -07001602 if (mViewUpdatedRequested || mIsExpansionAnimating) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001603 return;
1604 }
1605 mViewUpdatedRequested = true;
1606 getViewTreeObserver().addOnPreDrawListener(mViewUpdater);
1607 invalidate();
1608 }
1609
1610 private void updateExpandedBubble() {
Issei Suzukia8d07312019-06-07 12:56:19 +02001611 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001612 Log.d(TAG, "updateExpandedBubble()");
1613 }
Mady Mellor3dff9e62019-02-05 18:12:53 -08001614 mExpandedViewContainer.removeAllViews();
Lyn Hana0bb02e2020-01-28 17:57:27 -08001615 if (mIsExpanded) {
1616 BubbleExpandedView bev = mOverflowExpandedView;
1617 if (mExpandedBubble != null) {
1618 bev = mExpandedBubble.getExpandedView();
1619 }
1620 mExpandedViewContainer.addView(bev);
1621 bev.populateExpandedView();
1622 mExpandedViewContainer.setVisibility(VISIBLE);
Issei Suzukic0387542019-03-08 17:31:14 +01001623 mExpandedViewContainer.setAlpha(1.0f);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001624 }
1625 }
1626
Lyn Han285ad302019-05-29 19:01:39 -07001627 private void updateExpandedView() {
Issei Suzukia8d07312019-06-07 12:56:19 +02001628 if (DEBUG_BUBBLE_STACK_VIEW) {
Lyn Han285ad302019-05-29 19:01:39 -07001629 Log.d(TAG, "updateExpandedView: mIsExpanded=" + mIsExpanded);
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001630 }
Joshua Tsuji6549e702019-05-02 13:13:16 -04001631
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001632 mExpandedViewContainer.setVisibility(mIsExpanded ? VISIBLE : GONE);
Mady Mellor3dff9e62019-02-05 18:12:53 -08001633 if (mIsExpanded) {
Lyn Han285ad302019-05-29 19:01:39 -07001634 final float y = getExpandedViewY();
Mady Mellor5d8f1402019-02-21 18:23:52 -08001635 if (!mExpandedViewYAnim.isRunning()) {
1636 // We're not animating so set the value
1637 mExpandedViewContainer.setTranslationY(y);
Lyn Hanb58c7562020-01-07 14:29:20 -08001638 if (mExpandedBubble == null) {
1639 mOverflowExpandedView.updateView();
1640 } else {
1641 mExpandedBubble.getExpandedView().updateView();
1642 }
Mady Mellor5d8f1402019-02-21 18:23:52 -08001643 } else {
Mady Mellorbc078c22019-03-26 17:10:34 -07001644 // We are animating so update the value; there is an end listener on the animator
1645 // that will ensure expandedeView.updateView gets called.
Mady Mellor5d8f1402019-02-21 18:23:52 -08001646 mExpandedViewYAnim.animateToFinalPosition(y);
1647 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001648 }
Mady Mellor3dff9e62019-02-05 18:12:53 -08001649
Joshua Tsuji6549e702019-05-02 13:13:16 -04001650 mStackOnLeftOrWillBe = mStackAnimationController.isStackOnLeftSide();
Joshua Tsuji2862f2e2019-07-29 12:32:33 -04001651 updateBubbleZOrdersAndDotPosition(false);
Joshua Tsuji6549e702019-05-02 13:13:16 -04001652 }
1653
1654 /** Sets the appropriate Z-order and dot position for each bubble in the stack. */
Joshua Tsuji2862f2e2019-07-29 12:32:33 -04001655 private void updateBubbleZOrdersAndDotPosition(boolean animate) {
Lyn Hanc47e1712020-01-28 21:43:34 -08001656 int bubbleCount = getBubbleCount();
Lyn Han1b4f25e2019-06-11 13:56:34 -07001657 for (int i = 0; i < bubbleCount; i++) {
Mady Mellorb8aaf972019-11-26 10:28:00 -08001658 BadgedImageView bv = (BadgedImageView) mBubbleContainer.getChildAt(i);
Mady Mellor70958542019-09-24 17:12:46 -07001659 bv.setZ((mMaxBubbles * mBubbleElevation) - i);
Joshua Tsuji6549e702019-05-02 13:13:16 -04001660 // If the dot is on the left, and so is the stack, we need to change the dot position.
1661 if (bv.getDotPositionOnLeft() == mStackOnLeftOrWillBe) {
1662 bv.setDotPosition(!mStackOnLeftOrWillBe, animate);
1663 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001664 }
1665 }
1666
Mady Mellorde2d4d22019-01-29 14:15:34 -08001667 private void updatePointerPosition() {
Lyn Han522e9ff2019-05-17 13:26:13 -07001668 Bubble expandedBubble = getExpandedBubble();
1669 if (expandedBubble == null) {
1670 return;
Mady Mellorde2d4d22019-01-29 14:15:34 -08001671 }
Lyn Han522e9ff2019-05-17 13:26:13 -07001672 int index = getBubbleIndex(expandedBubble);
1673 float bubbleLeftFromScreenLeft = mExpandedAnimationController.getBubbleLeft(index);
1674 float halfBubble = mBubbleSize / 2f;
Mady Mellor9be3bed2019-08-21 17:26:26 -07001675 float bubbleCenter = bubbleLeftFromScreenLeft + halfBubble;
1676 // Padding might be adjusted for insets, so get it directly from the view
1677 bubbleCenter -= mExpandedViewContainer.getPaddingLeft();
Lyn Hanc47e1712020-01-28 21:43:34 -08001678 expandedBubble.getExpandedView().setPointerPosition(bubbleCenter);
Mady Mellorde2d4d22019-01-29 14:15:34 -08001679 }
1680
Steven Wua254dab2019-01-29 11:30:39 -05001681 /**
1682 * @return the number of bubbles in the stack view.
1683 */
Steven Wub00225b2019-02-08 14:27:42 -05001684 public int getBubbleCount() {
Lyn Hanc47e1712020-01-28 21:43:34 -08001685 if (BubbleExperimentConfig.allowBubbleOverflow(mContext)) {
1686 // Subtract 1 for the overflow button which is always in the bubble container.
1687 return mBubbleContainer.getChildCount() - 1;
1688 }
Steven Wua254dab2019-01-29 11:30:39 -05001689 return mBubbleContainer.getChildCount();
1690 }
1691
1692 /**
1693 * Finds the bubble index within the stack.
1694 *
Mady Mellor3dff9e62019-02-05 18:12:53 -08001695 * @param bubble the bubble to look up.
Steven Wua254dab2019-01-29 11:30:39 -05001696 * @return the index of the bubble view within the bubble stack. The range of the position
1697 * is between 0 and the bubble count minus 1.
1698 */
Steven Wua62cb6a2019-02-15 17:12:51 -05001699 int getBubbleIndex(@Nullable Bubble bubble) {
1700 if (bubble == null) {
1701 return 0;
1702 }
Mady Mellored99c272019-06-13 15:58:30 -07001703 return mBubbleContainer.indexOfChild(bubble.getIconView());
Steven Wua254dab2019-01-29 11:30:39 -05001704 }
1705
1706 /**
1707 * @return the normalized x-axis position of the bubble stack rounded to 4 decimal places.
1708 */
Steven Wub00225b2019-02-08 14:27:42 -05001709 public float getNormalizedXPosition() {
Joshua Tsuji442b6272019-02-08 13:23:43 -05001710 return new BigDecimal(getStackPosition().x / mDisplaySize.x)
Steven Wua254dab2019-01-29 11:30:39 -05001711 .setScale(4, RoundingMode.CEILING.HALF_UP)
1712 .floatValue();
1713 }
1714
1715 /**
1716 * @return the normalized y-axis position of the bubble stack rounded to 4 decimal places.
1717 */
Steven Wub00225b2019-02-08 14:27:42 -05001718 public float getNormalizedYPosition() {
Joshua Tsuji442b6272019-02-08 13:23:43 -05001719 return new BigDecimal(getStackPosition().y / mDisplaySize.y)
Steven Wua254dab2019-01-29 11:30:39 -05001720 .setScale(4, RoundingMode.CEILING.HALF_UP)
1721 .floatValue();
1722 }
1723
Joshua Tsujia19515f2019-02-13 18:02:29 -05001724 public PointF getStackPosition() {
1725 return mStackAnimationController.getStackPosition();
1726 }
1727
Steven Wua254dab2019-01-29 11:30:39 -05001728 /**
1729 * Logs the bubble UI event.
1730 *
1731 * @param bubble the bubble that is being interacted on. Null value indicates that
1732 * the user interaction is not specific to one bubble.
1733 * @param action the user interaction enum.
1734 */
Mady Mellor3dff9e62019-02-05 18:12:53 -08001735 private void logBubbleEvent(@Nullable Bubble bubble, int action) {
Mady Mellored99c272019-06-13 15:58:30 -07001736 if (bubble == null || bubble.getEntry() == null
Ned Burns00b4b2d2019-10-17 22:09:27 -04001737 || bubble.getEntry().getSbn() == null) {
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -08001738 SysUiStatsLog.write(SysUiStatsLog.BUBBLE_UI_CHANGED,
Steven Wua254dab2019-01-29 11:30:39 -05001739 null /* package name */,
1740 null /* notification channel */,
1741 0 /* notification ID */,
1742 0 /* bubble position */,
1743 getBubbleCount(),
1744 action,
1745 getNormalizedXPosition(),
Steven Wu45e38ae2019-03-25 16:16:59 -04001746 getNormalizedYPosition(),
Steven Wu8ba8ca92019-04-11 10:47:42 -04001747 false /* unread bubble */,
1748 false /* on-going bubble */,
Mady Mellor6cec3502019-06-17 19:20:49 -07001749 false /* isAppForeground (unused) */);
Steven Wua254dab2019-01-29 11:30:39 -05001750 } else {
Ned Burns00b4b2d2019-10-17 22:09:27 -04001751 StatusBarNotification notification = bubble.getEntry().getSbn();
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -08001752 SysUiStatsLog.write(SysUiStatsLog.BUBBLE_UI_CHANGED,
Steven Wua254dab2019-01-29 11:30:39 -05001753 notification.getPackageName(),
1754 notification.getNotification().getChannelId(),
1755 notification.getId(),
1756 getBubbleIndex(bubble),
1757 getBubbleCount(),
1758 action,
1759 getNormalizedXPosition(),
Steven Wu45e38ae2019-03-25 16:16:59 -04001760 getNormalizedYPosition(),
Mady Mellorb8aaf972019-11-26 10:28:00 -08001761 bubble.showInShade(),
Mady Mellor99a302602019-06-14 11:39:56 -07001762 bubble.isOngoing(),
Mady Mellor6cec3502019-06-17 19:20:49 -07001763 false /* isAppForeground (unused) */);
Steven Wua254dab2019-01-29 11:30:39 -05001764 }
1765 }
Mark Renouf041d7262019-02-06 12:09:41 -05001766
1767 /**
1768 * Called when a back gesture should be directed to the Bubbles stack. When expanded,
1769 * a back key down/up event pair is forwarded to the bubble Activity.
1770 */
1771 boolean performBackPressIfNeeded() {
1772 if (!isExpanded()) {
1773 return false;
1774 }
Lyn Hanb58c7562020-01-07 14:29:20 -08001775 if (mExpandedBubble == null) {
1776 return mOverflowExpandedView.performBackPressIfNeeded();
1777 } else {
1778 return mExpandedBubble.getExpandedView().performBackPressIfNeeded();
1779 }
Mark Renouf041d7262019-02-06 12:09:41 -05001780 }
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001781
1782 /** For debugging only */
1783 List<Bubble> getBubblesOnScreen() {
1784 List<Bubble> bubbles = new ArrayList<>();
Lyn Hanc47e1712020-01-28 21:43:34 -08001785 for (int i = 0; i < getBubbleCount(); i++) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001786 View child = mBubbleContainer.getChildAt(i);
Mady Mellorb8aaf972019-11-26 10:28:00 -08001787 if (child instanceof BadgedImageView) {
1788 String key = ((BadgedImageView) child).getKey();
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001789 Bubble bubble = mBubbleData.getBubbleWithKey(key);
1790 bubbles.add(bubble);
1791 }
1792 }
1793 return bubbles;
1794 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001795}