blob: b68b7627fe8c4eeb9c23f6a9dc3a82e9d9ea8f08 [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
Issei Suzukia8d07312019-06-07 12:56:19 +020022import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_STACK_VIEW;
23import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
24import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
25
Joshua Tsuji4accf5982019-04-22 17:36:11 -040026import android.animation.Animator;
27import android.animation.AnimatorListenerAdapter;
28import android.animation.ValueAnimator;
Issei Suzukic0387542019-03-08 17:31:14 +010029import android.annotation.NonNull;
Lyn Han6c40fe72019-05-08 14:06:33 -070030import android.app.Notification;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080031import android.content.Context;
Lyn Hanf4730312019-06-18 11:18:58 -070032import android.content.res.Configuration;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080033import android.content.res.Resources;
Joshua Tsuji4accf5982019-04-22 17:36:11 -040034import android.graphics.ColorMatrix;
35import android.graphics.ColorMatrixColorFilter;
Joshua Tsuji4accf5982019-04-22 17:36:11 -040036import android.graphics.Paint;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080037import android.graphics.Point;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080038import android.graphics.PointF;
39import android.graphics.Rect;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080040import android.graphics.RectF;
Mady Mellor217b2e92019-02-27 11:44:16 -080041import android.os.Bundle;
Joshua Tsuji4accf5982019-04-22 17:36:11 -040042import android.os.VibrationEffect;
43import android.os.Vibrator;
Steven Wua254dab2019-01-29 11:30:39 -050044import android.service.notification.StatusBarNotification;
Mark Renouf89b1a4a2018-12-04 14:59:45 -050045import android.util.Log;
Steven Wua254dab2019-01-29 11:30:39 -050046import android.util.StatsLog;
Issei Suzukic0387542019-03-08 17:31:14 +010047import android.view.Choreographer;
Mady Mellor9be3bed2019-08-21 17:26:26 -070048import android.view.DisplayCutout;
Joshua Tsuji36b1b2c2019-04-18 16:27:35 -040049import android.view.Gravity;
Mady Mellordea7ecf2018-12-10 15:47:40 -080050import android.view.LayoutInflater;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080051import android.view.MotionEvent;
52import android.view.View;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080053import android.view.ViewTreeObserver;
Joshua Tsuji0fee7682019-01-25 11:37:49 -050054import android.view.WindowInsets;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080055import android.view.WindowManager;
Mady Mellor217b2e92019-02-27 11:44:16 -080056import android.view.accessibility.AccessibilityNodeInfo;
Lyn Hane68d0912019-05-02 18:28:01 -070057import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
Joshua Tsuji614b1df2019-03-26 13:57:05 -040058import android.view.animation.AccelerateDecelerateInterpolator;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080059import android.widget.FrameLayout;
60
Mark Renoufcecc77b2019-01-30 16:32:24 -050061import androidx.annotation.MainThread;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080062import androidx.annotation.Nullable;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080063import androidx.dynamicanimation.animation.DynamicAnimation;
Joshua Tsuji6549e702019-05-02 13:13:16 -040064import androidx.dynamicanimation.animation.FloatPropertyCompat;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080065import androidx.dynamicanimation.animation.SpringAnimation;
66import androidx.dynamicanimation.animation.SpringForce;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080067
Mady Melloredd4ee12019-01-18 10:45:11 -080068import com.android.internal.annotations.VisibleForTesting;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080069import com.android.internal.widget.ViewClippingUtil;
70import com.android.systemui.R;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080071import com.android.systemui.bubbles.animation.ExpandedAnimationController;
72import com.android.systemui.bubbles.animation.PhysicsAnimationLayout;
73import com.android.systemui.bubbles.animation.StackAnimationController;
Ned Burnsf81c4c42019-01-07 14:10:43 -050074import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080075
Joshua Tsuji395bcfe2019-07-02 19:23:23 -040076import java.io.FileDescriptor;
77import java.io.PrintWriter;
Steven Wua254dab2019-01-29 11:30:39 -050078import java.math.BigDecimal;
79import java.math.RoundingMode;
Mark Renouf9ba6cea2019-04-17 11:53:50 -040080import java.util.ArrayList;
Mark Renouf821e6782019-04-01 14:17:37 -040081import java.util.Collections;
82import java.util.List;
Steven Wua254dab2019-01-29 11:30:39 -050083
Mady Mellorc3d6f7d2018-11-07 09:36:56 -080084/**
85 * Renders bubbles in a stack and handles animating expanded and collapsed states.
86 */
Joshua Tsuji442b6272019-02-08 13:23:43 -050087public class BubbleStackView extends FrameLayout {
Issei Suzukia8d07312019-06-07 12:56:19 +020088 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleStackView" : TAG_BUBBLES;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080089
Joshua Tsuji6549e702019-05-02 13:13:16 -040090 /** How far the flyout needs to be dragged before it's dismissed regardless of velocity. */
91 static final float FLYOUT_DRAG_PERCENT_DISMISS = 0.25f;
92
93 /** Velocity required to dismiss the flyout via drag. */
94 private static final float FLYOUT_DISMISS_VELOCITY = 2000f;
95
96 /**
97 * Factor for attenuating translation when the flyout is overscrolled (8f = flyout moves 1 pixel
98 * for every 8 pixels overscrolled).
99 */
100 private static final float FLYOUT_OVERSCROLL_ATTENUATION_FACTOR = 8f;
101
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400102 /** Duration of the flyout alpha animations. */
103 private static final int FLYOUT_ALPHA_ANIMATION_DURATION = 100;
104
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400105 /** Percent to darken the bubbles when they're in the dismiss target. */
106 private static final float DARKEN_PERCENT = 0.3f;
107
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400108 /** How long to wait, in milliseconds, before hiding the flyout. */
109 @VisibleForTesting
110 static final int FLYOUT_HIDE_AFTER = 5000;
111
Issei Suzukic0387542019-03-08 17:31:14 +0100112 /**
113 * Interface to synchronize {@link View} state and the screen.
114 *
115 * {@hide}
116 */
117 interface SurfaceSynchronizer {
118 /**
119 * Wait until requested change on a {@link View} is reflected on the screen.
120 *
121 * @param callback callback to run after the change is reflected on the screen.
122 */
123 void syncSurfaceAndRun(Runnable callback);
124 }
125
126 private static final SurfaceSynchronizer DEFAULT_SURFACE_SYNCHRONIZER =
127 new SurfaceSynchronizer() {
128 @Override
129 public void syncSurfaceAndRun(Runnable callback) {
130 Choreographer.getInstance().postFrameCallback(new Choreographer.FrameCallback() {
131 // Just wait 2 frames. There is no guarantee, but this is usually enough time that
132 // the requested change is reflected on the screen.
133 // TODO: Once SurfaceFlinger provide APIs to sync the state of {@code View} and
134 // surfaces, rewrite this logic with them.
135 private int mFrameWait = 2;
136
137 @Override
138 public void doFrame(long frameTimeNanos) {
139 if (--mFrameWait > 0) {
140 Choreographer.getInstance().postFrameCallback(this);
141 } else {
142 callback.run();
143 }
144 }
145 });
146 }
147 };
148
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800149 private Point mDisplaySize;
150
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800151 private final SpringAnimation mExpandedViewXAnim;
152 private final SpringAnimation mExpandedViewYAnim;
Mady Mellorcfd06c12019-02-13 14:32:12 -0800153 private final BubbleData mBubbleData;
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800154
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400155 private final Vibrator mVibrator;
156 private final ValueAnimator mDesaturateAndDarkenAnimator;
157 private final Paint mDesaturateAndDarkenPaint = new Paint();
158
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800159 private PhysicsAnimationLayout mBubbleContainer;
160 private StackAnimationController mStackAnimationController;
161 private ExpandedAnimationController mExpandedAnimationController;
162
Mady Mellor3dff9e62019-02-05 18:12:53 -0800163 private FrameLayout mExpandedViewContainer;
164
Joshua Tsuji6549e702019-05-02 13:13:16 -0400165 private BubbleFlyoutView mFlyout;
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400166 /** Runnable that fades out the flyout and then sets it to GONE. */
Joshua Tsuji6549e702019-05-02 13:13:16 -0400167 private Runnable mHideFlyout = () -> animateFlyoutCollapsed(true, 0 /* velX */);
Mady Mellordf48d0a2019-06-25 18:26:46 -0700168 /**
169 * Callback to run after the flyout hides. Also called if a new flyout is shown before the
170 * previous one animates out.
171 */
172 private Runnable mAfterFlyoutHides;
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400173
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400174 /** Layout change listener that moves the stack to the nearest valid position on rotation. */
Lyn Hanf4730312019-06-18 11:18:58 -0700175 private OnLayoutChangeListener mOrientationChangedListener;
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400176 /** Whether the stack was on the left side of the screen prior to rotation. */
177 private boolean mWasOnLeftBeforeRotation = false;
178 /**
179 * How far down the screen the stack was before rotation, in terms of percentage of the way down
180 * the allowable region. Defaults to -1 if not set.
181 */
182 private float mVerticalPosPercentBeforeRotation = -1;
183
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800184 private int mBubbleSize;
Lyn Han4a8efe32019-05-30 09:43:27 -0700185 private int mBubblePaddingTop;
Mady Mellore9371bc2019-07-10 18:50:59 -0700186 private int mBubbleTouchPadding;
Lyn Han6f6b3ae2019-05-16 14:17:30 -0700187 private int mExpandedViewPadding;
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800188 private int mExpandedAnimateXDistance;
189 private int mExpandedAnimateYDistance;
Lyn Han5aa27e22019-05-15 10:55:07 -0700190 private int mPointerHeight;
Joshua Tsujif44347f2019-02-12 14:28:06 -0500191 private int mStatusBarHeight;
Joshua Tsujia19515f2019-02-13 18:02:29 -0500192 private int mImeOffset;
Lyn Han1b4f25e2019-06-11 13:56:34 -0700193 private BubbleIconFactory mBubbleIconFactory;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800194 private Bubble mExpandedBubble;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800195 private boolean mIsExpanded;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800196
Joshua Tsuji6549e702019-05-02 13:13:16 -0400197 /** Whether the stack is currently on the left side of the screen, or animating there. */
198 private boolean mStackOnLeftOrWillBe = false;
199
200 /** Whether a touch gesture, such as a stack/bubble drag or flyout drag, is in progress. */
201 private boolean mIsGestureInProgress = false;
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400202
Joshua Tsuji395bcfe2019-07-02 19:23:23 -0400203 /** Description of current animation controller state. */
204 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
205 pw.println("Stack view state:");
206 pw.print(" gestureInProgress: "); pw.println(mIsGestureInProgress);
207 pw.print(" showingDismiss: "); pw.println(mShowingDismiss);
208 pw.print(" isExpansionAnimating: "); pw.println(mIsExpansionAnimating);
209 pw.print(" draggingInDismiss: "); pw.println(mDraggingInDismissTarget);
210 pw.print(" animatingMagnet: "); pw.println(mAnimatingMagnet);
211 mStackAnimationController.dump(fd, pw, args);
212 mExpandedAnimationController.dump(fd, pw, args);
213 }
214
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800215 private BubbleTouchHandler mTouchHandler;
Mady Mellorcd9b1302018-11-06 18:08:04 -0800216 private BubbleController.BubbleExpandListener mExpandListener;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800217
218 private boolean mViewUpdatedRequested = false;
Mady Mellorbc078c22019-03-26 17:10:34 -0700219 private boolean mIsExpansionAnimating = false;
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400220 private boolean mShowingDismiss = false;
221
222 /**
223 * Whether the user is currently dragging their finger within the dismiss target. In this state
224 * the stack will be magnetized to the center of the target, so we shouldn't move it until the
225 * touch exits the dismiss target area.
226 */
227 private boolean mDraggingInDismissTarget = false;
228
229 /** Whether the stack is magneting towards the dismiss target. */
230 private boolean mAnimatingMagnet = false;
231
232 /** The view to desaturate/darken when magneted to the dismiss target. */
233 private View mDesaturateAndDarkenTargetView;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800234
Mady Mellor3dff9e62019-02-05 18:12:53 -0800235 private LayoutInflater mInflater;
236
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800237 // Used for determining view / touch intersection
238 int[] mTempLoc = new int[2];
239 RectF mTempRect = new RectF();
240
Mark Renouf821e6782019-04-01 14:17:37 -0400241 private final List<Rect> mSystemGestureExclusionRects = Collections.singletonList(new Rect());
242
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800243 private ViewTreeObserver.OnPreDrawListener mViewUpdater =
244 new ViewTreeObserver.OnPreDrawListener() {
245 @Override
246 public boolean onPreDraw() {
247 getViewTreeObserver().removeOnPreDrawListener(mViewUpdater);
Lyn Han285ad302019-05-29 19:01:39 -0700248 updateExpandedView();
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800249 mViewUpdatedRequested = false;
250 return true;
251 }
252 };
253
Mark Renouf821e6782019-04-01 14:17:37 -0400254 private ViewTreeObserver.OnDrawListener mSystemGestureExcludeUpdater =
255 this::updateSystemGestureExcludeRects;
256
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800257 private ViewClippingUtil.ClippingParameters mClippingParameters =
258 new ViewClippingUtil.ClippingParameters() {
259
Lyn Han522e9ff2019-05-17 13:26:13 -0700260 @Override
261 public boolean shouldFinish(View view) {
262 return false;
263 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800264
Lyn Han522e9ff2019-05-17 13:26:13 -0700265 @Override
266 public boolean isClippingEnablingAllowed(View view) {
267 return !mIsExpanded;
268 }
269 };
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800270
Joshua Tsuji6549e702019-05-02 13:13:16 -0400271 /** Float property that 'drags' the flyout. */
272 private final FloatPropertyCompat mFlyoutCollapseProperty =
273 new FloatPropertyCompat("FlyoutCollapseSpring") {
274 @Override
275 public float getValue(Object o) {
276 return mFlyoutDragDeltaX;
277 }
278
279 @Override
280 public void setValue(Object o, float v) {
281 onFlyoutDragged(v);
282 }
283 };
284
285 /** SpringAnimation that springs the flyout collapsed via onFlyoutDragged. */
286 private final SpringAnimation mFlyoutTransitionSpring =
287 new SpringAnimation(this, mFlyoutCollapseProperty);
288
289 /** Distance the flyout has been dragged in the X axis. */
290 private float mFlyoutDragDeltaX = 0f;
291
292 /**
Joshua Tsuji14e68552019-06-06 17:17:08 -0400293 * Runnable that animates in the flyout. This reference is needed to cancel delayed postings.
294 */
295 private Runnable mAnimateInFlyout;
296
297 /**
Joshua Tsuji6549e702019-05-02 13:13:16 -0400298 * End listener for the flyout spring that either posts a runnable to hide the flyout, or hides
299 * it immediately.
300 */
301 private final DynamicAnimation.OnAnimationEndListener mAfterFlyoutTransitionSpring =
302 (dynamicAnimation, b, v, v1) -> {
303 if (mFlyoutDragDeltaX == 0) {
304 mFlyout.postDelayed(mHideFlyout, FLYOUT_HIDE_AFTER);
305 } else {
306 mFlyout.hideFlyout();
307 }
308 };
309
Lyn Han1b4f25e2019-06-11 13:56:34 -0700310 @NonNull
311 private final SurfaceSynchronizer mSurfaceSynchronizer;
Issei Suzukic0387542019-03-08 17:31:14 +0100312
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400313 private BubbleDismissView mDismissContainer;
314 private Runnable mAfterMagnet;
Issei Suzukic0387542019-03-08 17:31:14 +0100315
Lyn Hanf4730312019-06-18 11:18:58 -0700316 private int mOrientation = Configuration.ORIENTATION_UNDEFINED;
317
Issei Suzukic0387542019-03-08 17:31:14 +0100318 public BubbleStackView(Context context, BubbleData data,
Lyn Han522e9ff2019-05-17 13:26:13 -0700319 @Nullable SurfaceSynchronizer synchronizer) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800320 super(context);
321
Mady Mellorcfd06c12019-02-13 14:32:12 -0800322 mBubbleData = data;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800323 mInflater = LayoutInflater.from(context);
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400324 mTouchHandler = new BubbleTouchHandler(this, data, context);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800325 setOnTouchListener(mTouchHandler);
Joshua Tsuji442b6272019-02-08 13:23:43 -0500326 mInflater = LayoutInflater.from(context);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800327
328 Resources res = getResources();
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800329 mBubbleSize = res.getDimensionPixelSize(R.dimen.individual_bubble_size);
Lyn Han4a8efe32019-05-30 09:43:27 -0700330 mBubblePaddingTop = res.getDimensionPixelSize(R.dimen.bubble_padding_top);
Mady Mellore9371bc2019-07-10 18:50:59 -0700331 mBubbleTouchPadding = res.getDimensionPixelSize(R.dimen.bubble_touch_padding);
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800332 mExpandedAnimateXDistance =
333 res.getDimensionPixelSize(R.dimen.bubble_expanded_animate_x_distance);
334 mExpandedAnimateYDistance =
335 res.getDimensionPixelSize(R.dimen.bubble_expanded_animate_y_distance);
Lyn Han5aa27e22019-05-15 10:55:07 -0700336 mPointerHeight = res.getDimensionPixelSize(R.dimen.bubble_pointer_height);
337
Joshua Tsujif44347f2019-02-12 14:28:06 -0500338 mStatusBarHeight =
339 res.getDimensionPixelSize(com.android.internal.R.dimen.status_bar_height);
Joshua Tsujia19515f2019-02-13 18:02:29 -0500340 mImeOffset = res.getDimensionPixelSize(R.dimen.pip_ime_offset);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800341
342 mDisplaySize = new Point();
343 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Mady Mellor9be3bed2019-08-21 17:26:26 -0700344 // We use the real size & subtract screen decorations / window insets ourselves when needed
Mady Mellore19353d2019-08-21 17:25:02 -0700345 wm.getDefaultDisplay().getRealSize(mDisplaySize);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800346
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400347 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
348
Lyn Han6f6b3ae2019-05-16 14:17:30 -0700349 mExpandedViewPadding = res.getDimensionPixelSize(R.dimen.bubble_expanded_view_padding);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800350 int elevation = res.getDimensionPixelSize(R.dimen.bubble_elevation);
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800351
352 mStackAnimationController = new StackAnimationController();
Lyn Hanf4730312019-06-18 11:18:58 -0700353
Lyn Han6f6b3ae2019-05-16 14:17:30 -0700354 mExpandedAnimationController = new ExpandedAnimationController(
Lyn Hanf4730312019-06-18 11:18:58 -0700355 mDisplaySize, mExpandedViewPadding, res.getConfiguration().orientation);
Issei Suzukic0387542019-03-08 17:31:14 +0100356 mSurfaceSynchronizer = synchronizer != null ? synchronizer : DEFAULT_SURFACE_SYNCHRONIZER;
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800357
358 mBubbleContainer = new PhysicsAnimationLayout(context);
Joshua Tsujic36ee6f2019-05-28 17:00:16 -0400359 mBubbleContainer.setActiveController(mStackAnimationController);
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800360 mBubbleContainer.setElevation(elevation);
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800361 mBubbleContainer.setClipChildren(false);
362 addView(mBubbleContainer, new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
363
Lyn Han61d5d562019-07-01 17:39:38 -0700364 mBubbleIconFactory = new BubbleIconFactory(context);
Lyn Han1b4f25e2019-06-11 13:56:34 -0700365
Mady Mellor3dff9e62019-02-05 18:12:53 -0800366 mExpandedViewContainer = new FrameLayout(context);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800367 mExpandedViewContainer.setElevation(elevation);
Lyn Han6f6b3ae2019-05-16 14:17:30 -0700368 mExpandedViewContainer.setPadding(mExpandedViewPadding, mExpandedViewPadding,
369 mExpandedViewPadding, mExpandedViewPadding);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800370 mExpandedViewContainer.setClipChildren(false);
371 addView(mExpandedViewContainer);
372
Mady Mellor8bfe5412019-07-31 14:56:44 -0700373 setUpFlyout();
Joshua Tsuji6549e702019-05-02 13:13:16 -0400374 mFlyoutTransitionSpring.setSpring(new SpringForce()
Joshua Tsuji14e68552019-06-06 17:17:08 -0400375 .setStiffness(SpringForce.STIFFNESS_LOW)
Joshua Tsuji6549e702019-05-02 13:13:16 -0400376 .setDampingRatio(SpringForce.DAMPING_RATIO_LOW_BOUNCY));
377 mFlyoutTransitionSpring.addEndListener(mAfterFlyoutTransitionSpring);
378
379 mDismissContainer = new BubbleDismissView(mContext);
380 mDismissContainer.setLayoutParams(new FrameLayout.LayoutParams(
381 MATCH_PARENT,
382 getResources().getDimensionPixelSize(R.dimen.pip_dismiss_gradient_height),
383 Gravity.BOTTOM));
384 addView(mDismissContainer);
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400385
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800386 mExpandedViewXAnim =
387 new SpringAnimation(mExpandedViewContainer, DynamicAnimation.TRANSLATION_X);
388 mExpandedViewXAnim.setSpring(
389 new SpringForce()
390 .setStiffness(SpringForce.STIFFNESS_LOW)
391 .setDampingRatio(SpringForce.DAMPING_RATIO_LOW_BOUNCY));
392
393 mExpandedViewYAnim =
394 new SpringAnimation(mExpandedViewContainer, DynamicAnimation.TRANSLATION_Y);
395 mExpandedViewYAnim.setSpring(
396 new SpringForce()
397 .setStiffness(SpringForce.STIFFNESS_LOW)
398 .setDampingRatio(SpringForce.DAMPING_RATIO_LOW_BOUNCY));
Mady Mellorbc078c22019-03-26 17:10:34 -0700399 mExpandedViewYAnim.addEndListener((anim, cancelled, value, velocity) -> {
400 if (mIsExpanded && mExpandedBubble != null) {
Mady Mellored99c272019-06-13 15:58:30 -0700401 mExpandedBubble.getExpandedView().updateView();
Mady Mellorbc078c22019-03-26 17:10:34 -0700402 }
403 });
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800404
405 setClipChildren(false);
Mady Mellor217b2e92019-02-27 11:44:16 -0800406 setFocusable(true);
Joshua Tsuji442b6272019-02-08 13:23:43 -0500407 mBubbleContainer.bringToFront();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800408
409 setOnApplyWindowInsetsListener((View view, WindowInsets insets) -> {
Mady Mellorbc078c22019-03-26 17:10:34 -0700410 if (!mIsExpanded || mIsExpansionAnimating) {
Mady Mellor5d8f1402019-02-21 18:23:52 -0800411 return view.onApplyWindowInsets(insets);
412 }
Mady Mellor5d8f1402019-02-21 18:23:52 -0800413 mExpandedAnimationController.updateYPosition(
414 // Update the insets after we're done translating otherwise position
415 // calculation for them won't be correct.
Mady Mellored99c272019-06-13 15:58:30 -0700416 () -> mExpandedBubble.getExpandedView().updateInsets(insets));
Mady Mellor5d8f1402019-02-21 18:23:52 -0800417 return view.onApplyWindowInsets(insets);
418 });
Mark Renouf821e6782019-04-01 14:17:37 -0400419
Lyn Hanf4730312019-06-18 11:18:58 -0700420 mOrientationChangedListener =
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400421 (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
Mady Mellor9be3bed2019-08-21 17:26:26 -0700422 mExpandedAnimationController.updateOrientation(mOrientation, mDisplaySize);
423 mStackAnimationController.updateOrientation(mOrientation);
424
425 // Reposition & adjust the height for new orientation
426 if (mIsExpanded) {
427 mExpandedViewContainer.setTranslationY(getExpandedViewY());
428 mExpandedBubble.getExpandedView().updateView();
429 }
430
431 // Need to update the padding around the view
432 WindowInsets insets = getRootWindowInsets();
433 int leftPadding = mExpandedViewPadding;
434 int rightPadding = mExpandedViewPadding;
435 if (insets != null) {
436 // Can't have the expanded view overlaying notches
437 int cutoutLeft = 0;
438 int cutoutRight = 0;
439 DisplayCutout cutout = insets.getDisplayCutout();
440 if (cutout != null) {
441 cutoutLeft = cutout.getSafeInsetLeft();
442 cutoutRight = cutout.getSafeInsetRight();
443 }
444 // Or overlaying nav or status bar
445 leftPadding += Math.max(cutoutLeft, insets.getStableInsetLeft());
446 rightPadding += Math.max(cutoutRight, insets.getStableInsetRight());
447 }
448 mExpandedViewContainer.setPadding(leftPadding, mExpandedViewPadding,
449 rightPadding, mExpandedViewPadding);
450
Lyn Hanf4730312019-06-18 11:18:58 -0700451 if (mIsExpanded) {
452 // Re-draw bubble row and pointer for new orientation.
453 mExpandedAnimationController.expandFromStack(() -> {
454 updatePointerPosition();
455 } /* after */);
456 }
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400457 if (mVerticalPosPercentBeforeRotation >= 0) {
458 mStackAnimationController.moveStackToSimilarPositionAfterRotation(
459 mWasOnLeftBeforeRotation, mVerticalPosPercentBeforeRotation);
460 }
Lyn Hanf4730312019-06-18 11:18:58 -0700461 removeOnLayoutChangeListener(mOrientationChangedListener);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400462 };
463
Mark Renouf821e6782019-04-01 14:17:37 -0400464 // This must be a separate OnDrawListener since it should be called for every draw.
465 getViewTreeObserver().addOnDrawListener(mSystemGestureExcludeUpdater);
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400466
467 final ColorMatrix animatedMatrix = new ColorMatrix();
468 final ColorMatrix darkenMatrix = new ColorMatrix();
469
470 mDesaturateAndDarkenAnimator = ValueAnimator.ofFloat(1f, 0f);
471 mDesaturateAndDarkenAnimator.addUpdateListener(animation -> {
472 final float animatedValue = (float) animation.getAnimatedValue();
473 animatedMatrix.setSaturation(animatedValue);
474
475 final float animatedDarkenValue = (1f - animatedValue) * DARKEN_PERCENT;
476 darkenMatrix.setScale(
477 1f - animatedDarkenValue /* red */,
478 1f - animatedDarkenValue /* green */,
479 1f - animatedDarkenValue /* blue */,
480 1f /* alpha */);
481
482 // Concat the matrices so that the animatedMatrix both desaturates and darkens.
483 animatedMatrix.postConcat(darkenMatrix);
484
485 // Update the paint and apply it to the bubble container.
486 mDesaturateAndDarkenPaint.setColorFilter(new ColorMatrixColorFilter(animatedMatrix));
487 mDesaturateAndDarkenTargetView.setLayerPaint(mDesaturateAndDarkenPaint);
488 });
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800489 }
490
Mady Mellor8bfe5412019-07-31 14:56:44 -0700491 private void setUpFlyout() {
492 if (mFlyout != null) {
493 removeView(mFlyout);
494 }
495 mFlyout = new BubbleFlyoutView(getContext());
496 mFlyout.setVisibility(GONE);
497 mFlyout.animate()
498 .setDuration(FLYOUT_ALPHA_ANIMATION_DURATION)
499 .setInterpolator(new AccelerateDecelerateInterpolator());
500 addView(mFlyout, new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
501 }
502
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700503 /**
Lyn Han02cca812019-04-02 16:27:32 -0700504 * Handle theme changes.
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700505 */
Lyn Han02cca812019-04-02 16:27:32 -0700506 public void onThemeChanged() {
Lyn Han6e653a12019-07-10 16:47:32 -0700507 // Recreate icon factory to update default adaptive icon scale.
Lyn Han61d5d562019-07-01 17:39:38 -0700508 mBubbleIconFactory = new BubbleIconFactory(mContext);
Mady Mellor8bfe5412019-07-31 14:56:44 -0700509 setUpFlyout();
Mady Mellored99c272019-06-13 15:58:30 -0700510 for (Bubble b: mBubbleData.getBubbles()) {
Lyn Han6e653a12019-07-10 16:47:32 -0700511 b.getIconView().setBubbleIconFactory(mBubbleIconFactory);
Mady Mellored99c272019-06-13 15:58:30 -0700512 b.getIconView().updateViews();
513 b.getExpandedView().applyThemeAttrs();
Mady Mellore37f60b2019-03-25 14:48:34 -0700514 }
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700515 }
516
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400517 /** Respond to the phone being rotated by repositioning the stack and hiding any flyouts. */
Lyn Hanf4730312019-06-18 11:18:58 -0700518 public void onOrientationChanged(int orientation) {
519 mOrientation = orientation;
520
Mady Mellore19353d2019-08-21 17:25:02 -0700521 // Display size is based on the rotation device was in when requested, we should update it
Mady Mellor9be3bed2019-08-21 17:26:26 -0700522 // We use the real size & subtract screen decorations / window insets ourselves when needed
Mady Mellore19353d2019-08-21 17:25:02 -0700523 WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
524 wm.getDefaultDisplay().getRealSize(mDisplaySize);
525
Mady Mellor818eef02019-08-16 16:12:29 -0700526 // Some resources change depending on orientation
527 Resources res = getContext().getResources();
528 mStatusBarHeight = res.getDimensionPixelSize(
529 com.android.internal.R.dimen.status_bar_height);
530 mBubblePaddingTop = res.getDimensionPixelSize(R.dimen.bubble_padding_top);
531
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400532 final RectF allowablePos = mStackAnimationController.getAllowableStackPositionRegion();
533 mWasOnLeftBeforeRotation = mStackAnimationController.isStackOnLeftSide();
534 mVerticalPosPercentBeforeRotation =
535 (mStackAnimationController.getStackPosition().y - allowablePos.top)
536 / (allowablePos.bottom - allowablePos.top);
Lyn Hanf4730312019-06-18 11:18:58 -0700537 addOnLayoutChangeListener(mOrientationChangedListener);
Joshua Tsujif418f9e2019-04-04 17:09:53 -0400538 hideFlyoutImmediate();
539 }
540
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800541 @Override
Mady Mellor217b2e92019-02-27 11:44:16 -0800542 public void getBoundsOnScreen(Rect outRect, boolean clipToParent) {
543 getBoundsOnScreen(outRect);
544 }
545
546 @Override
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800547 protected void onDetachedFromWindow() {
548 super.onDetachedFromWindow();
549 getViewTreeObserver().removeOnPreDrawListener(mViewUpdater);
550 }
551
552 @Override
Mady Mellor217b2e92019-02-27 11:44:16 -0800553 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
554 super.onInitializeAccessibilityNodeInfoInternal(info);
Lyn Hane68d0912019-05-02 18:28:01 -0700555
556 // Custom actions.
557 AccessibilityAction moveTopLeft = new AccessibilityAction(R.id.action_move_top_left,
558 getContext().getResources()
559 .getString(R.string.bubble_accessibility_action_move_top_left));
560 info.addAction(moveTopLeft);
561
562 AccessibilityAction moveTopRight = new AccessibilityAction(R.id.action_move_top_right,
563 getContext().getResources()
564 .getString(R.string.bubble_accessibility_action_move_top_right));
565 info.addAction(moveTopRight);
566
567 AccessibilityAction moveBottomLeft = new AccessibilityAction(R.id.action_move_bottom_left,
568 getContext().getResources()
569 .getString(R.string.bubble_accessibility_action_move_bottom_left));
570 info.addAction(moveBottomLeft);
571
572 AccessibilityAction moveBottomRight = new AccessibilityAction(R.id.action_move_bottom_right,
573 getContext().getResources()
574 .getString(R.string.bubble_accessibility_action_move_bottom_right));
575 info.addAction(moveBottomRight);
576
577 // Default actions.
578 info.addAction(AccessibilityAction.ACTION_DISMISS);
Mady Mellor217b2e92019-02-27 11:44:16 -0800579 if (mIsExpanded) {
Lyn Hane68d0912019-05-02 18:28:01 -0700580 info.addAction(AccessibilityAction.ACTION_COLLAPSE);
Mady Mellor217b2e92019-02-27 11:44:16 -0800581 } else {
Lyn Hane68d0912019-05-02 18:28:01 -0700582 info.addAction(AccessibilityAction.ACTION_EXPAND);
Mady Mellor217b2e92019-02-27 11:44:16 -0800583 }
584 }
585
586 @Override
587 public boolean performAccessibilityActionInternal(int action, Bundle arguments) {
588 if (super.performAccessibilityActionInternal(action, arguments)) {
589 return true;
590 }
Lyn Hane68d0912019-05-02 18:28:01 -0700591 final RectF stackBounds = mStackAnimationController.getAllowableStackPositionRegion();
592
593 // R constants are not final so we cannot use switch-case here.
594 if (action == AccessibilityNodeInfo.ACTION_DISMISS) {
595 mBubbleData.dismissAll(BubbleController.DISMISS_ACCESSIBILITY_ACTION);
596 return true;
597 } else if (action == AccessibilityNodeInfo.ACTION_COLLAPSE) {
598 mBubbleData.setExpanded(false);
599 return true;
600 } else if (action == AccessibilityNodeInfo.ACTION_EXPAND) {
601 mBubbleData.setExpanded(true);
602 return true;
603 } else if (action == R.id.action_move_top_left) {
604 mStackAnimationController.springStack(stackBounds.left, stackBounds.top);
605 return true;
606 } else if (action == R.id.action_move_top_right) {
607 mStackAnimationController.springStack(stackBounds.right, stackBounds.top);
608 return true;
609 } else if (action == R.id.action_move_bottom_left) {
610 mStackAnimationController.springStack(stackBounds.left, stackBounds.bottom);
611 return true;
612 } else if (action == R.id.action_move_bottom_right) {
613 mStackAnimationController.springStack(stackBounds.right, stackBounds.bottom);
614 return true;
Mady Mellor217b2e92019-02-27 11:44:16 -0800615 }
616 return false;
617 }
618
Lyn Han6c40fe72019-05-08 14:06:33 -0700619 /**
620 * Update content description for a11y TalkBack.
621 */
622 public void updateContentDescription() {
623 if (mBubbleData.getBubbles().isEmpty()) {
624 return;
625 }
626 Bubble topBubble = mBubbleData.getBubbles().get(0);
627 String appName = topBubble.getAppName();
Mady Mellored99c272019-06-13 15:58:30 -0700628 Notification notification = topBubble.getEntry().notification.getNotification();
Lyn Han6c40fe72019-05-08 14:06:33 -0700629 CharSequence titleCharSeq = notification.extras.getCharSequence(Notification.EXTRA_TITLE);
630 String titleStr = getResources().getString(R.string.stream_notification);
631 if (titleCharSeq != null) {
632 titleStr = titleCharSeq.toString();
633 }
634 int moreCount = mBubbleContainer.getChildCount() - 1;
635
636 // Example: Title from app name.
637 String singleDescription = getResources().getString(
638 R.string.bubble_content_description_single, titleStr, appName);
639
640 // Example: Title from app name and 4 more.
641 String stackDescription = getResources().getString(
642 R.string.bubble_content_description_stack, titleStr, appName, moreCount);
643
644 if (mIsExpanded) {
645 // TODO(b/129522932) - update content description for each bubble in expanded view.
646 } else {
647 // Collapsed stack.
648 if (moreCount > 0) {
649 mBubbleContainer.setContentDescription(stackDescription);
650 } else {
651 mBubbleContainer.setContentDescription(singleDescription);
652 }
653 }
654 }
655
Mark Renouf821e6782019-04-01 14:17:37 -0400656 private void updateSystemGestureExcludeRects() {
657 // Exclude the region occupied by the first BubbleView in the stack
658 Rect excludeZone = mSystemGestureExclusionRects.get(0);
659 if (mBubbleContainer.getChildCount() > 0) {
660 View firstBubble = mBubbleContainer.getChildAt(0);
661 excludeZone.set(firstBubble.getLeft(), firstBubble.getTop(), firstBubble.getRight(),
662 firstBubble.getBottom());
663 excludeZone.offset((int) (firstBubble.getTranslationX() + 0.5f),
664 (int) (firstBubble.getTranslationY() + 0.5f));
665 mBubbleContainer.setSystemGestureExclusionRects(mSystemGestureExclusionRects);
666 } else {
667 excludeZone.setEmpty();
668 mBubbleContainer.setSystemGestureExclusionRects(Collections.emptyList());
669 }
670 }
671
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800672 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800673 * Updates the visibility of the 'dot' indicating an update on the bubble.
Lyn Han1b4f25e2019-06-11 13:56:34 -0700674 *
Mady Mellor3dff9e62019-02-05 18:12:53 -0800675 * @param key the {@link NotificationEntry#key} associated with the bubble.
676 */
677 public void updateDotVisibility(String key) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400678 Bubble b = mBubbleData.getBubbleWithKey(key);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800679 if (b != null) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400680 b.updateDotVisibility();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800681 }
682 }
683
684 /**
Mady Mellorcd9b1302018-11-06 18:08:04 -0800685 * Sets the listener to notify when the bubble stack is expanded.
686 */
687 public void setExpandListener(BubbleController.BubbleExpandListener listener) {
688 mExpandListener = listener;
689 }
690
691 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800692 * Whether the stack of bubbles is expanded or not.
693 */
694 public boolean isExpanded() {
695 return mIsExpanded;
696 }
697
698 /**
Mady Mellor047e24e2019-08-05 11:35:40 -0700699 * Whether the stack of bubbles is animating to or from expansion.
700 */
701 public boolean isExpansionAnimating() {
702 return mIsExpansionAnimating;
703 }
704
705 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800706 * The {@link BubbleView} that is expanded, null if one does not exist.
707 */
Mady Mellor3dff9e62019-02-05 18:12:53 -0800708 BubbleView getExpandedBubbleView() {
Mady Mellored99c272019-06-13 15:58:30 -0700709 return mExpandedBubble != null ? mExpandedBubble.getIconView() : null;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800710 }
711
712 /**
713 * The {@link Bubble} that is expanded, null if one does not exist.
714 */
715 Bubble getExpandedBubble() {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800716 return mExpandedBubble;
717 }
718
719 /**
720 * Sets the bubble that should be expanded and expands if needed.
Mady Mellor3dff9e62019-02-05 18:12:53 -0800721 *
722 * @param key the {@link NotificationEntry#key} associated with the bubble to expand.
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400723 * @deprecated replaced by setSelectedBubble(Bubble) + setExpanded(true)
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800724 */
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400725 @Deprecated
Mady Mellor3dff9e62019-02-05 18:12:53 -0800726 void setExpandedBubble(String key) {
Mark Renouf71a3af62019-04-08 15:02:54 -0400727 Bubble bubbleToExpand = mBubbleData.getBubbleWithKey(key);
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400728 if (bubbleToExpand != null) {
729 setSelectedBubble(bubbleToExpand);
Mady Mellor99a302602019-06-14 11:39:56 -0700730 bubbleToExpand.setShowInShadeWhenBubble(false);
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400731 setExpanded(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800732 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800733 }
734
Mark Renouf71a3af62019-04-08 15:02:54 -0400735 // via BubbleData.Listener
736 void addBubble(Bubble bubble) {
Issei Suzukia8d07312019-06-07 12:56:19 +0200737 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400738 Log.d(TAG, "addBubble: " + bubble);
739 }
Joshua Tsujib35f5912019-07-24 16:15:21 -0400740
741 if (mBubbleContainer.getChildCount() == 0) {
742 mStackOnLeftOrWillBe = mStackAnimationController.isStackOnLeftSide();
743 }
744
Mark Renouf71a3af62019-04-08 15:02:54 -0400745 bubble.inflate(mInflater, this);
Mady Mellored99c272019-06-13 15:58:30 -0700746 bubble.getIconView().setBubbleIconFactory(mBubbleIconFactory);
747 bubble.getIconView().updateViews();
Lyn Han1b4f25e2019-06-11 13:56:34 -0700748
Joshua Tsujib35f5912019-07-24 16:15:21 -0400749 // Set the dot position to the opposite of the side the stack is resting on, since the stack
750 // resting slightly off-screen would result in the dot also being off-screen.
751 bubble.getIconView().setDotPosition(
752 !mStackOnLeftOrWillBe /* onLeft */, false /* animate */);
753
Mady Mellored99c272019-06-13 15:58:30 -0700754 mBubbleContainer.addView(bubble.getIconView(), 0,
Mark Renouf71a3af62019-04-08 15:02:54 -0400755 new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
Mady Mellored99c272019-06-13 15:58:30 -0700756 ViewClippingUtil.setClippingDeactivated(bubble.getIconView(), true, mClippingParameters);
Mark Renoufba5ab512019-05-02 15:21:01 -0400757 animateInFlyoutForBubble(bubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400758 requestUpdate();
759 logBubbleEvent(bubble, StatsLog.BUBBLE_UICHANGED__ACTION__POSTED);
Lyn Han522e9ff2019-05-17 13:26:13 -0700760 updatePointerPosition();
Mark Renouf71a3af62019-04-08 15:02:54 -0400761 }
762
763 // via BubbleData.Listener
764 void removeBubble(Bubble bubble) {
Issei Suzukia8d07312019-06-07 12:56:19 +0200765 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400766 Log.d(TAG, "removeBubble: " + bubble);
767 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400768 // Remove it from the views
Mady Mellored99c272019-06-13 15:58:30 -0700769 int removedIndex = mBubbleContainer.indexOfChild(bubble.getIconView());
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400770 if (removedIndex >= 0) {
771 mBubbleContainer.removeViewAt(removedIndex);
Mark Renoufc19b4732019-06-26 12:08:33 -0400772 bubble.cleanupExpandedState();
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400773 logBubbleEvent(bubble, StatsLog.BUBBLE_UICHANGED__ACTION__DISMISSED);
774 } else {
775 Log.d(TAG, "was asked to remove Bubble, but didn't find the view! " + bubble);
776 }
Lyn Han522e9ff2019-05-17 13:26:13 -0700777 updatePointerPosition();
Mark Renouf71a3af62019-04-08 15:02:54 -0400778 }
779
780 // via BubbleData.Listener
781 void updateBubble(Bubble bubble) {
Mark Renoufba5ab512019-05-02 15:21:01 -0400782 animateInFlyoutForBubble(bubble);
Mark Renouf71a3af62019-04-08 15:02:54 -0400783 requestUpdate();
784 logBubbleEvent(bubble, StatsLog.BUBBLE_UICHANGED__ACTION__UPDATED);
785 }
786
Mark Renoufba5ab512019-05-02 15:21:01 -0400787 public void updateBubbleOrder(List<Bubble> bubbles) {
788 for (int i = 0; i < bubbles.size(); i++) {
789 Bubble bubble = bubbles.get(i);
Mady Mellored99c272019-06-13 15:58:30 -0700790 mBubbleContainer.reorderView(bubble.getIconView(), i);
Mark Renoufba5ab512019-05-02 15:21:01 -0400791 }
Joshua Tsuji2862f2e2019-07-29 12:32:33 -0400792
793 updateBubbleZOrdersAndDotPosition(false /* animate */);
Mark Renoufba5ab512019-05-02 15:21:01 -0400794 }
795
Mady Melloredd4ee12019-01-18 10:45:11 -0800796 /**
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400797 * Changes the currently selected bubble. If the stack is already expanded, the newly selected
798 * bubble will be shown immediately. This does not change the expanded state or change the
799 * position of any bubble.
800 */
Mark Renouf71a3af62019-04-08 15:02:54 -0400801 // via BubbleData.Listener
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400802 public void setSelectedBubble(@Nullable Bubble bubbleToSelect) {
Issei Suzukia8d07312019-06-07 12:56:19 +0200803 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400804 Log.d(TAG, "setSelectedBubble: " + bubbleToSelect);
805 }
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400806 if (mExpandedBubble != null && mExpandedBubble.equals(bubbleToSelect)) {
807 return;
808 }
809 final Bubble previouslySelected = mExpandedBubble;
810 mExpandedBubble = bubbleToSelect;
Issei Suzukicac2a502019-04-16 16:52:50 +0200811
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400812 if (mIsExpanded) {
813 // Make the container of the expanded view transparent before removing the expanded view
814 // from it. Otherwise a punch hole created by {@link android.view.SurfaceView} in the
815 // expanded view becomes visible on the screen. See b/126856255
816 mExpandedViewContainer.setAlpha(0.0f);
817 mSurfaceSynchronizer.syncSurfaceAndRun(() -> {
Issei Suzukicac2a502019-04-16 16:52:50 +0200818 if (previouslySelected != null) {
819 previouslySelected.setContentVisibility(false);
820 }
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400821 updateExpandedBubble();
822 updatePointerPosition();
823 requestUpdate();
824 logBubbleEvent(previouslySelected, StatsLog.BUBBLE_UICHANGED__ACTION__COLLAPSED);
825 logBubbleEvent(bubbleToSelect, StatsLog.BUBBLE_UICHANGED__ACTION__EXPANDED);
Mady Mellor99a302602019-06-14 11:39:56 -0700826 notifyExpansionChanged(previouslySelected, false /* expanded */);
827 notifyExpansionChanged(bubbleToSelect, true /* expanded */);
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400828 });
829 }
830 }
831
832 /**
833 * Changes the expanded state of the stack.
834 *
Mark Renouf71a3af62019-04-08 15:02:54 -0400835 * @param shouldExpand whether the bubble stack should appear expanded
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400836 */
Mark Renouf71a3af62019-04-08 15:02:54 -0400837 // via BubbleData.Listener
838 public void setExpanded(boolean shouldExpand) {
Issei Suzukia8d07312019-06-07 12:56:19 +0200839 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400840 Log.d(TAG, "setExpanded: " + shouldExpand);
841 }
Lyn Han285ad302019-05-29 19:01:39 -0700842 if (shouldExpand == mIsExpanded) {
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400843 return;
844 }
Lyn Han285ad302019-05-29 19:01:39 -0700845 if (mIsExpanded) {
846 animateCollapse();
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400847 logBubbleEvent(mExpandedBubble, StatsLog.BUBBLE_UICHANGED__ACTION__COLLAPSED);
848 } else {
Lyn Han285ad302019-05-29 19:01:39 -0700849 animateExpansion();
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400850 // TODO: move next line to BubbleData
851 logBubbleEvent(mExpandedBubble, StatsLog.BUBBLE_UICHANGED__ACTION__EXPANDED);
852 logBubbleEvent(mExpandedBubble, StatsLog.BUBBLE_UICHANGED__ACTION__STACK_EXPANDED);
853 }
Mady Mellor99a302602019-06-14 11:39:56 -0700854 notifyExpansionChanged(mExpandedBubble, mIsExpanded);
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400855 }
856
857 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800858 * Dismiss the stack of bubbles.
Lyn Han1b4f25e2019-06-11 13:56:34 -0700859 *
Mark Renouf71a3af62019-04-08 15:02:54 -0400860 * @deprecated
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800861 */
Mark Renouf71a3af62019-04-08 15:02:54 -0400862 @Deprecated
Mady Mellorc3d7d062019-03-28 16:13:05 -0700863 void stackDismissed(int reason) {
Issei Suzukia8d07312019-06-07 12:56:19 +0200864 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400865 Log.d(TAG, "stackDismissed: reason=" + reason);
866 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400867 mBubbleData.dismissAll(reason);
Steven Wua254dab2019-01-29 11:30:39 -0500868 logBubbleEvent(null /* no bubble associated with bubble stack dismiss */,
869 StatsLog.BUBBLE_UICHANGED__ACTION__STACK_DISMISSED);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800870 }
871
872 /**
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800873 * @return the view the touch event is on
874 */
875 @Nullable
876 public View getTargetView(MotionEvent event) {
877 float x = event.getRawX();
878 float y = event.getRawY();
879 if (mIsExpanded) {
880 if (isIntersecting(mBubbleContainer, x, y)) {
Mady Mellor47b11e32019-07-11 19:06:21 -0700881 // Could be tapping or dragging a bubble while expanded
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800882 for (int i = 0; i < mBubbleContainer.getChildCount(); i++) {
883 BubbleView view = (BubbleView) mBubbleContainer.getChildAt(i);
884 if (isIntersecting(view, x, y)) {
885 return view;
886 }
887 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800888 }
Mady Mellor47b11e32019-07-11 19:06:21 -0700889 BubbleExpandedView bev = (BubbleExpandedView) mExpandedViewContainer.getChildAt(0);
890 if (bev.intersectingTouchableContent((int) x, (int) y)) {
891 return bev;
892 }
893 // Outside of the parts we care about.
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800894 return null;
Joshua Tsuji6549e702019-05-02 13:13:16 -0400895 } else if (mFlyout.getVisibility() == VISIBLE && isIntersecting(mFlyout, x, y)) {
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400896 return mFlyout;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800897 }
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400898 // 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 -0800899 return this;
900 }
901
Mark Renouf71a3af62019-04-08 15:02:54 -0400902 View getFlyoutView() {
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400903 return mFlyout;
904 }
905
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800906 /**
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800907 * Collapses the stack of bubbles.
Mark Renoufcecc77b2019-01-30 16:32:24 -0500908 * <p>
909 * Must be called from the main thread.
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400910 *
911 * @deprecated use {@link #setExpanded(boolean)} and {@link #setSelectedBubble(Bubble)}
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800912 */
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400913 @Deprecated
Mark Renoufcecc77b2019-01-30 16:32:24 -0500914 @MainThread
Mark Renouf71a3af62019-04-08 15:02:54 -0400915 void collapseStack() {
Issei Suzukia8d07312019-06-07 12:56:19 +0200916 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400917 Log.d(TAG, "collapseStack()");
918 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400919 mBubbleData.setExpanded(false);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800920 }
921
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400922 /**
923 * @deprecated use {@link #setExpanded(boolean)} and {@link #setSelectedBubble(Bubble)}
924 */
925 @Deprecated
926 @MainThread
Mady Mellor9801e852019-01-22 14:50:28 -0800927 void collapseStack(Runnable endRunnable) {
Issei Suzukia8d07312019-06-07 12:56:19 +0200928 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400929 Log.d(TAG, "collapseStack(endRunnable)");
930 }
Mady Mellor9801e852019-01-22 14:50:28 -0800931 collapseStack();
932 // TODO - use the runnable at end of animation
933 endRunnable.run();
934 }
935
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800936 /**
Mady Mellor94d94a72019-03-05 18:16:59 -0800937 * Expands the stack of bubbles.
Mark Renoufcecc77b2019-01-30 16:32:24 -0500938 * <p>
939 * Must be called from the main thread.
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400940 *
941 * @deprecated use {@link #setExpanded(boolean)} and {@link #setSelectedBubble(Bubble)}
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800942 */
Mark Renoufc6ab73d2019-04-09 16:42:22 -0400943 @Deprecated
Mark Renoufcecc77b2019-01-30 16:32:24 -0500944 @MainThread
Mark Renouf71a3af62019-04-08 15:02:54 -0400945 void expandStack() {
Issei Suzukia8d07312019-06-07 12:56:19 +0200946 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400947 Log.d(TAG, "expandStack()");
948 }
Mark Renouf71a3af62019-04-08 15:02:54 -0400949 mBubbleData.setExpanded(true);
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800950 }
951
Lyn Han285ad302019-05-29 19:01:39 -0700952 private void beforeExpandedViewAnimation() {
953 hideFlyoutImmediate();
954 updateExpandedBubble();
955 updateExpandedView();
956 mIsExpansionAnimating = true;
957 }
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400958
Lyn Han285ad302019-05-29 19:01:39 -0700959 private void afterExpandedViewAnimation() {
960 updateExpandedView();
961 mIsExpansionAnimating = false;
962 requestUpdate();
963 }
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800964
Lyn Han285ad302019-05-29 19:01:39 -0700965 private void animateCollapse() {
966 mIsExpanded = false;
Issei Suzuki71f7ae42019-07-11 14:19:50 +0200967 final Bubble previouslySelected = mExpandedBubble;
Lyn Han285ad302019-05-29 19:01:39 -0700968 beforeExpandedViewAnimation();
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800969
Lyn Han285ad302019-05-29 19:01:39 -0700970 mBubbleContainer.cancelAllAnimations();
971 mExpandedAnimationController.collapseBackToStack(
Joshua Tsuji61b38f52019-05-31 16:20:22 -0400972 mStackAnimationController.getStackPositionAlongNearestHorizontalEdge()
973 /* collapseTo */,
Lyn Han285ad302019-05-29 19:01:39 -0700974 () -> {
Joshua Tsuji61b38f52019-05-31 16:20:22 -0400975 mBubbleContainer.setActiveController(mStackAnimationController);
Lyn Han285ad302019-05-29 19:01:39 -0700976 afterExpandedViewAnimation();
Issei Suzuki71f7ae42019-07-11 14:19:50 +0200977 previouslySelected.setContentVisibility(false);
Lyn Han285ad302019-05-29 19:01:39 -0700978 });
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800979
Lyn Han285ad302019-05-29 19:01:39 -0700980 mExpandedViewXAnim.animateToFinalPosition(getCollapsedX());
981 mExpandedViewYAnim.animateToFinalPosition(getCollapsedY());
982 mExpandedViewContainer.animate()
983 .setDuration(100)
984 .alpha(0f);
985 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -0800986
Lyn Han285ad302019-05-29 19:01:39 -0700987 private void animateExpansion() {
988 mIsExpanded = true;
989 beforeExpandedViewAnimation();
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800990
Joshua Tsuji61b38f52019-05-31 16:20:22 -0400991 mBubbleContainer.setActiveController(mExpandedAnimationController);
992 mExpandedAnimationController.expandFromStack(() -> {
993 updatePointerPosition();
994 afterExpandedViewAnimation();
995 } /* after */);
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800996
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800997
Lyn Han285ad302019-05-29 19:01:39 -0700998 mExpandedViewContainer.setTranslationX(getCollapsedX());
999 mExpandedViewContainer.setTranslationY(getCollapsedY());
1000 mExpandedViewContainer.setAlpha(0f);
1001
1002 mExpandedViewXAnim.animateToFinalPosition(0f);
1003 mExpandedViewYAnim.animateToFinalPosition(getExpandedViewY());
1004 mExpandedViewContainer.animate()
1005 .setDuration(100)
1006 .alpha(1f);
1007 }
1008
1009 private float getCollapsedX() {
1010 return mStackAnimationController.getStackPosition().x < getWidth() / 2
1011 ? -mExpandedAnimateXDistance
1012 : mExpandedAnimateXDistance;
1013 }
1014
1015 private float getCollapsedY() {
1016 return Math.min(mStackAnimationController.getStackPosition().y,
1017 mExpandedAnimateYDistance);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001018 }
1019
Mady Mellor99a302602019-06-14 11:39:56 -07001020 private void notifyExpansionChanged(Bubble bubble, boolean expanded) {
1021 if (mExpandListener != null && bubble != null) {
1022 mExpandListener.onBubbleExpandChanged(expanded, bubble.getKey());
Mady Mellor3f2efdb2018-11-21 11:30:45 -08001023 }
1024 }
1025
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001026 /** Return the BubbleView at the given index from the bubble container. */
1027 public BubbleView getBubbleAt(int i) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001028 return mBubbleContainer.getChildCount() > i
1029 ? (BubbleView) mBubbleContainer.getChildAt(i)
1030 : null;
1031 }
1032
Joshua Tsujia19515f2019-02-13 18:02:29 -05001033 /** Moves the bubbles out of the way if they're going to be over the keyboard. */
1034 public void onImeVisibilityChanged(boolean visible, int height) {
Mady Mellordf611cf2019-08-21 17:28:49 -07001035 mStackAnimationController.setImeHeight(visible ? height + mImeOffset : 0);
Joshua Tsuji4b395912019-04-19 17:18:40 -04001036
Joshua Tsujia19515f2019-02-13 18:02:29 -05001037 if (!mIsExpanded) {
Joshua Tsuji4b395912019-04-19 17:18:40 -04001038 mStackAnimationController.animateForImeVisibility(visible);
Joshua Tsujia19515f2019-02-13 18:02:29 -05001039 }
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001040 }
1041
1042 /** Called when a drag operation on an individual bubble has started. */
Joshua Tsuji442b6272019-02-08 13:23:43 -05001043 public void onBubbleDragStart(View bubble) {
Issei Suzukia8d07312019-06-07 12:56:19 +02001044 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001045 Log.d(TAG, "onBubbleDragStart: bubble=" + bubble);
1046 }
Joshua Tsuji442b6272019-02-08 13:23:43 -05001047 mExpandedAnimationController.prepareForBubbleDrag(bubble);
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001048 }
1049
1050 /** Called with the coordinates to which an individual bubble has been dragged. */
Joshua Tsuji442b6272019-02-08 13:23:43 -05001051 public void onBubbleDragged(View bubble, float x, float y) {
Mady Mellorbc078c22019-03-26 17:10:34 -07001052 if (!mIsExpanded || mIsExpansionAnimating) {
Joshua Tsuji442b6272019-02-08 13:23:43 -05001053 return;
1054 }
1055
1056 mExpandedAnimationController.dragBubbleOut(bubble, x, y);
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001057 springInDismissTarget();
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001058 }
1059
1060 /** Called when a drag operation on an individual bubble has finished. */
Joshua Tsuji442b6272019-02-08 13:23:43 -05001061 public void onBubbleDragFinish(
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001062 View bubble, float x, float y, float velX, float velY) {
Issei Suzukia8d07312019-06-07 12:56:19 +02001063 if (DEBUG_BUBBLE_STACK_VIEW) {
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001064 Log.d(TAG, "onBubbleDragFinish: bubble=" + bubble);
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001065 }
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001066
Mady Mellorbc078c22019-03-26 17:10:34 -07001067 if (!mIsExpanded || mIsExpansionAnimating) {
Joshua Tsuji442b6272019-02-08 13:23:43 -05001068 return;
1069 }
1070
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001071 mExpandedAnimationController.snapBubbleBack(bubble, velX, velY);
Lyn Han634483c2019-06-28 16:52:47 -07001072 hideDismissTarget();
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001073 }
1074
1075 void onDragStart() {
Issei Suzukia8d07312019-06-07 12:56:19 +02001076 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001077 Log.d(TAG, "onDragStart()");
1078 }
Mady Mellorbc078c22019-03-26 17:10:34 -07001079 if (mIsExpanded || mIsExpansionAnimating) {
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001080 return;
1081 }
1082
1083 mStackAnimationController.cancelStackPositionAnimations();
Joshua Tsujic36ee6f2019-05-28 17:00:16 -04001084 mBubbleContainer.setActiveController(mStackAnimationController);
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001085 hideFlyoutImmediate();
1086
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001087 mDraggingInDismissTarget = false;
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001088 }
1089
1090 void onDragged(float x, float y) {
Mady Mellorbc078c22019-03-26 17:10:34 -07001091 if (mIsExpanded || mIsExpansionAnimating) {
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001092 return;
1093 }
1094
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001095 springInDismissTarget();
1096 mStackAnimationController.moveStackFromTouch(x, y);
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001097 }
1098
1099 void onDragFinish(float x, float y, float velX, float velY) {
Issei Suzukia8d07312019-06-07 12:56:19 +02001100 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001101 Log.d(TAG, "onDragFinish");
1102 }
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001103
Mady Mellorbc078c22019-03-26 17:10:34 -07001104 if (mIsExpanded || mIsExpansionAnimating) {
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001105 return;
1106 }
1107
Joshua Tsuji6549e702019-05-02 13:13:16 -04001108 final float newStackX = mStackAnimationController.flingStackThenSpringToEdge(x, velX, velY);
Steven Wua254dab2019-01-29 11:30:39 -05001109 logBubbleEvent(null /* no bubble associated with bubble stack move */,
1110 StatsLog.BUBBLE_UICHANGED__ACTION__STACK_MOVED);
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001111
Joshua Tsuji6549e702019-05-02 13:13:16 -04001112 mStackOnLeftOrWillBe = newStackX <= 0;
Joshua Tsuji2862f2e2019-07-29 12:32:33 -04001113 updateBubbleZOrdersAndDotPosition(true /* animate */);
Lyn Han634483c2019-06-28 16:52:47 -07001114 hideDismissTarget();
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001115 }
1116
Joshua Tsuji6549e702019-05-02 13:13:16 -04001117 void onFlyoutDragStart() {
1118 mFlyout.removeCallbacks(mHideFlyout);
1119 }
1120
1121 void onFlyoutDragged(float deltaX) {
Joshua Tsuji8e05aab2019-08-22 14:57:50 -04001122 // This shouldn't happen, but if it does, just wait until the flyout lays out. This method
1123 // is continually called.
1124 if (mFlyout.getWidth() <= 0) {
1125 return;
1126 }
1127
Joshua Tsuji6549e702019-05-02 13:13:16 -04001128 final boolean onLeft = mStackAnimationController.isStackOnLeftSide();
1129 mFlyoutDragDeltaX = deltaX;
1130
1131 final float collapsePercent =
1132 onLeft ? -deltaX / mFlyout.getWidth() : deltaX / mFlyout.getWidth();
1133 mFlyout.setCollapsePercent(Math.min(1f, Math.max(0f, collapsePercent)));
1134
Lyn Han61d5d562019-07-01 17:39:38 -07001135 // Calculate how to translate the flyout if it has been dragged too far in either direction.
Joshua Tsuji6549e702019-05-02 13:13:16 -04001136 float overscrollTranslation = 0f;
1137 if (collapsePercent < 0f || collapsePercent > 1f) {
1138 // Whether we are more than 100% transitioned to the dot.
1139 final boolean overscrollingPastDot = collapsePercent > 1f;
1140
1141 // Whether we are overscrolling physically to the left - this can either be pulling the
1142 // flyout away from the stack (if the stack is on the right) or pushing it to the left
1143 // after it has already become the dot.
1144 final boolean overscrollingLeft =
1145 (onLeft && collapsePercent > 1f) || (!onLeft && collapsePercent < 0f);
Joshua Tsuji6549e702019-05-02 13:13:16 -04001146 overscrollTranslation =
1147 (overscrollingPastDot ? collapsePercent - 1f : collapsePercent * -1)
1148 * (overscrollingLeft ? -1 : 1)
1149 * (mFlyout.getWidth() / (FLYOUT_OVERSCROLL_ATTENUATION_FACTOR
Lyn Han522e9ff2019-05-17 13:26:13 -07001150 // Attenuate the smaller dot less than the larger flyout.
1151 / (overscrollingPastDot ? 2 : 1)));
Joshua Tsuji6549e702019-05-02 13:13:16 -04001152 }
1153
1154 mFlyout.setTranslationX(mFlyout.getRestingTranslationX() + overscrollTranslation);
1155 }
1156
1157 /**
Joshua Tsuji14e68552019-06-06 17:17:08 -04001158 * Set when the flyout is tapped, so that we can expand the bubble associated with the flyout
1159 * once it collapses.
1160 */
1161 @Nullable private Bubble mBubbleToExpandAfterFlyoutCollapse = null;
1162
1163 void onFlyoutTapped() {
1164 mBubbleToExpandAfterFlyoutCollapse = mBubbleData.getSelectedBubble();
1165
1166 mFlyout.removeCallbacks(mHideFlyout);
1167 mHideFlyout.run();
1168 }
1169
1170 /**
Joshua Tsuji6549e702019-05-02 13:13:16 -04001171 * Called when the flyout drag has finished, and returns true if the gesture successfully
1172 * dismissed the flyout.
1173 */
1174 void onFlyoutDragFinished(float deltaX, float velX) {
1175 final boolean onLeft = mStackAnimationController.isStackOnLeftSide();
1176 final boolean metRequiredVelocity =
1177 onLeft ? velX < -FLYOUT_DISMISS_VELOCITY : velX > FLYOUT_DISMISS_VELOCITY;
1178 final boolean metRequiredDeltaX =
1179 onLeft
1180 ? deltaX < -mFlyout.getWidth() * FLYOUT_DRAG_PERCENT_DISMISS
1181 : deltaX > mFlyout.getWidth() * FLYOUT_DRAG_PERCENT_DISMISS;
1182 final boolean isCancelFling = onLeft ? velX > 0 : velX < 0;
1183 final boolean shouldDismiss = metRequiredVelocity || (metRequiredDeltaX && !isCancelFling);
1184
1185 mFlyout.removeCallbacks(mHideFlyout);
1186 animateFlyoutCollapsed(shouldDismiss, velX);
1187 }
1188
1189 /**
1190 * Called when the first touch event of a gesture (stack drag, bubble drag, flyout drag, etc.)
1191 * is received.
1192 */
1193 void onGestureStart() {
1194 mIsGestureInProgress = true;
1195 }
1196
1197 /** Called when a gesture is completed or cancelled. */
1198 void onGestureFinished() {
1199 mIsGestureInProgress = false;
Joshua Tsujif49ee142019-05-29 16:32:01 -04001200
1201 if (mIsExpanded) {
1202 mExpandedAnimationController.onGestureFinished();
1203 }
Joshua Tsuji6549e702019-05-02 13:13:16 -04001204 }
1205
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001206 /** Prepares and starts the desaturate/darken animation on the bubble stack. */
1207 private void animateDesaturateAndDarken(View targetView, boolean desaturateAndDarken) {
1208 mDesaturateAndDarkenTargetView = targetView;
1209
1210 if (desaturateAndDarken) {
1211 // Use the animated paint for the bubbles.
1212 mDesaturateAndDarkenTargetView.setLayerType(
1213 View.LAYER_TYPE_HARDWARE, mDesaturateAndDarkenPaint);
1214 mDesaturateAndDarkenAnimator.removeAllListeners();
1215 mDesaturateAndDarkenAnimator.start();
1216 } else {
1217 mDesaturateAndDarkenAnimator.removeAllListeners();
1218 mDesaturateAndDarkenAnimator.addListener(new AnimatorListenerAdapter() {
1219 @Override
1220 public void onAnimationEnd(Animator animation) {
1221 super.onAnimationEnd(animation);
1222 // Stop using the animated paint.
1223 resetDesaturationAndDarken();
1224 }
1225 });
1226 mDesaturateAndDarkenAnimator.reverse();
1227 }
1228 }
1229
1230 private void resetDesaturationAndDarken() {
1231 mDesaturateAndDarkenAnimator.removeAllListeners();
1232 mDesaturateAndDarkenAnimator.cancel();
1233 mDesaturateAndDarkenTargetView.setLayerType(View.LAYER_TYPE_NONE, null);
1234 }
1235
1236 /**
1237 * Magnets the stack to the target, while also transforming the target to encircle the stack and
1238 * desaturating/darkening the bubbles.
1239 */
1240 void animateMagnetToDismissTarget(
1241 View magnetView, boolean toTarget, float x, float y, float velX, float velY) {
1242 mDraggingInDismissTarget = toTarget;
1243
1244 if (toTarget) {
1245 // The Y-value for the bubble stack to be positioned in the center of the dismiss target
1246 final float destY = mDismissContainer.getDismissTargetCenterY() - mBubbleSize / 2f;
1247
1248 mAnimatingMagnet = true;
1249
1250 final Runnable afterMagnet = () -> {
1251 mAnimatingMagnet = false;
1252 if (mAfterMagnet != null) {
1253 mAfterMagnet.run();
1254 }
1255 };
1256
1257 if (magnetView == this) {
1258 mStackAnimationController.magnetToDismiss(velX, velY, destY, afterMagnet);
1259 animateDesaturateAndDarken(mBubbleContainer, true);
1260 } else {
1261 mExpandedAnimationController.magnetBubbleToDismiss(
1262 magnetView, velX, velY, destY, afterMagnet);
1263
1264 animateDesaturateAndDarken(magnetView, true);
1265 }
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001266 } else {
1267 mAnimatingMagnet = false;
1268
1269 if (magnetView == this) {
1270 mStackAnimationController.demagnetizeFromDismissToPoint(x, y, velX, velY);
1271 animateDesaturateAndDarken(mBubbleContainer, false);
1272 } else {
1273 mExpandedAnimationController.demagnetizeBubbleTo(x, y, velX, velY);
1274 animateDesaturateAndDarken(magnetView, false);
1275 }
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001276 }
1277
1278 mVibrator.vibrate(VibrationEffect.get(toTarget
1279 ? VibrationEffect.EFFECT_CLICK
1280 : VibrationEffect.EFFECT_TICK));
1281 }
1282
1283 /**
1284 * Magnets the stack to the dismiss target if it's not already there. Then, dismiss the stack
1285 * using the 'implode' animation and animate out the target.
1286 */
1287 void magnetToStackIfNeededThenAnimateDismissal(
1288 View touchedView, float velX, float velY, Runnable after) {
Joshua Tsujif49ee142019-05-29 16:32:01 -04001289 final View draggedOutBubble = mExpandedAnimationController.getDraggedOutBubble();
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001290 final Runnable animateDismissal = () -> {
1291 mAfterMagnet = null;
1292
1293 mVibrator.vibrate(VibrationEffect.get(VibrationEffect.EFFECT_CLICK));
Lyn Han634483c2019-06-28 16:52:47 -07001294 mDismissContainer.springOut();
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001295
1296 // 'Implode' the stack and then hide the dismiss target.
1297 if (touchedView == this) {
1298 mStackAnimationController.implodeStack(
1299 () -> {
1300 mAnimatingMagnet = false;
1301 mShowingDismiss = false;
1302 mDraggingInDismissTarget = false;
1303 after.run();
1304 resetDesaturationAndDarken();
1305 });
1306 } else {
Joshua Tsujif49ee142019-05-29 16:32:01 -04001307 mExpandedAnimationController.dismissDraggedOutBubble(draggedOutBubble, () -> {
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001308 mAnimatingMagnet = false;
1309 mShowingDismiss = false;
1310 mDraggingInDismissTarget = false;
1311 resetDesaturationAndDarken();
1312 after.run();
1313 });
1314 }
1315 };
1316
1317 if (mAnimatingMagnet) {
1318 // If the magnet animation is currently playing, dismiss the stack after it's done. This
1319 // happens if the stack is flung towards the target.
1320 mAfterMagnet = animateDismissal;
1321 } else if (mDraggingInDismissTarget) {
1322 // If we're in the dismiss target, but not animating, we already magneted - dismiss
1323 // immediately.
1324 animateDismissal.run();
1325 } else {
1326 // Otherwise, we need to start the magnet animation and then dismiss afterward.
1327 animateMagnetToDismissTarget(touchedView, true, -1 /* x */, -1 /* y */, velX, velY);
1328 mAfterMagnet = animateDismissal;
1329 }
1330 }
1331
Lyn Han634483c2019-06-28 16:52:47 -07001332 /** Animates in the dismiss target. */
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001333 private void springInDismissTarget() {
1334 if (mShowingDismiss) {
1335 return;
1336 }
1337
1338 mShowingDismiss = true;
1339
1340 // Show the dismiss container and bring it to the front so the bubbles will go behind it.
1341 mDismissContainer.springIn();
1342 mDismissContainer.bringToFront();
1343 mDismissContainer.setZ(Short.MAX_VALUE - 1);
1344 }
1345
1346 /**
1347 * Animates the dismiss target out, as well as the circle that encircles the bubbles, if they
1348 * were dragged into the target and encircled.
1349 */
Lyn Han634483c2019-06-28 16:52:47 -07001350 private void hideDismissTarget() {
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001351 if (!mShowingDismiss) {
1352 return;
1353 }
1354
1355 mDismissContainer.springOut();
1356 mShowingDismiss = false;
1357 }
1358
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001359 /** Whether the location of the given MotionEvent is within the dismiss target area. */
Joshua Tsuji6549e702019-05-02 13:13:16 -04001360 boolean isInDismissTarget(MotionEvent ev) {
Joshua Tsuji4accf5982019-04-22 17:36:11 -04001361 return isIntersecting(mDismissContainer.getDismissTarget(), ev.getRawX(), ev.getRawY());
Joshua Tsuji19e22e4242019-04-17 13:29:10 -04001362 }
1363
Joshua Tsuji6549e702019-05-02 13:13:16 -04001364 /** Animates the flyout collapsed (to dot), or the reverse, starting with the given velocity. */
1365 private void animateFlyoutCollapsed(boolean collapsed, float velX) {
1366 final boolean onLeft = mStackAnimationController.isStackOnLeftSide();
Joshua Tsuji14e68552019-06-06 17:17:08 -04001367 // If the flyout was tapped, we want a higher stiffness for the collapse animation so it's
1368 // faster.
1369 mFlyoutTransitionSpring.getSpring().setStiffness(
1370 (mBubbleToExpandAfterFlyoutCollapse != null)
1371 ? SpringForce.STIFFNESS_MEDIUM
1372 : SpringForce.STIFFNESS_LOW);
Joshua Tsuji6549e702019-05-02 13:13:16 -04001373 mFlyoutTransitionSpring
1374 .setStartValue(mFlyoutDragDeltaX)
1375 .setStartVelocity(velX)
1376 .animateToFinalPosition(collapsed
1377 ? (onLeft ? -mFlyout.getWidth() : mFlyout.getWidth())
1378 : 0f);
1379 }
1380
Mady Mellordf48d0a2019-06-25 18:26:46 -07001381 /** Updates the dot visibility, this is used in response to a zen mode config change. */
1382 void updateDots() {
1383 int bubbsCount = mBubbleContainer.getChildCount();
1384 for (int i = 0; i < bubbsCount; i++) {
1385 BubbleView bv = (BubbleView) mBubbleContainer.getChildAt(i);
1386 // If nothing changed the animation won't happen
1387 bv.updateDotVisibility(true /* animate */);
1388 }
1389 }
1390
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001391 /**
Mady Mellor44ee2fe2019-01-30 17:51:16 -08001392 * Calculates the y position of the expanded view when it is expanded.
1393 */
Lyn Han285ad302019-05-29 19:01:39 -07001394 float getExpandedViewY() {
Lyn Han4a8efe32019-05-30 09:43:27 -07001395 return getStatusBarHeight() + mBubbleSize + mBubblePaddingTop + mPointerHeight;
Mady Mellor44ee2fe2019-01-30 17:51:16 -08001396 }
1397
1398 /**
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001399 * Animates in the flyout for the given bubble, if available, and then hides it after some time.
1400 */
1401 @VisibleForTesting
1402 void animateInFlyoutForBubble(Bubble bubble) {
Mady Mellor99a302602019-06-14 11:39:56 -07001403 final CharSequence updateMessage = bubble.getUpdateMessage(getContext());
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001404
Mark Renoufc19b4732019-06-26 12:08:33 -04001405 if (!bubble.showFlyoutForBubble()) {
1406 // In case flyout was suppressed for this update, reset now.
1407 bubble.setSuppressFlyout(false);
1408 return;
1409 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001410
Mark Renoufc19b4732019-06-26 12:08:33 -04001411 if (updateMessage == null
1412 || isExpanded()
1413 || mIsExpansionAnimating
Joshua Tsuji14e68552019-06-06 17:17:08 -04001414 || mIsGestureInProgress
1415 || mBubbleToExpandAfterFlyoutCollapse != null) {
1416 // Skip the message if none exists, we're expanded or animating expansion, or we're
1417 // about to expand a bubble from the previous tapped flyout.
Mark Renoufc19b4732019-06-26 12:08:33 -04001418 return;
1419 }
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001420
Mark Renoufc19b4732019-06-26 12:08:33 -04001421 if (bubble.getIconView() != null) {
1422 // Temporarily suppress the dot while the flyout is visible.
1423 bubble.getIconView().setSuppressDot(
1424 true /* suppressDot */, false /* animate */);
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001425
Joshua Tsuji14e68552019-06-06 17:17:08 -04001426 mFlyout.removeCallbacks(mAnimateInFlyout);
Mark Renoufc19b4732019-06-26 12:08:33 -04001427 mFlyoutDragDeltaX = 0f;
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001428
Mark Renoufc19b4732019-06-26 12:08:33 -04001429 if (mAfterFlyoutHides != null) {
1430 mAfterFlyoutHides.run();
Joshua Tsuji36b1b2c2019-04-18 16:27:35 -04001431 }
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001432
Mark Renoufc19b4732019-06-26 12:08:33 -04001433 mAfterFlyoutHides = () -> {
Mark Renoufc19b4732019-06-26 12:08:33 -04001434 final boolean suppressDot = !bubble.showBubbleDot();
1435 // If we're going to suppress the dot, make it visible first so it'll
1436 // visibly animate away.
1437 if (suppressDot) {
1438 bubble.getIconView().setSuppressDot(
1439 false /* suppressDot */, false /* animate */);
1440 }
1441 // Reset dot suppression. If we're not suppressing due to DND, then
1442 // stop suppressing it with no animation (since the flyout has
1443 // transformed into the dot). If we are suppressing due to DND, animate
1444 // it away.
1445 bubble.getIconView().setSuppressDot(
1446 suppressDot /* suppressDot */,
1447 suppressDot /* animate */);
Joshua Tsuji14e68552019-06-06 17:17:08 -04001448
1449 if (mBubbleToExpandAfterFlyoutCollapse != null) {
1450 mBubbleData.setSelectedBubble(mBubbleToExpandAfterFlyoutCollapse);
1451 mBubbleData.setExpanded(true);
1452 mBubbleToExpandAfterFlyoutCollapse = null;
1453 }
Mark Renoufc19b4732019-06-26 12:08:33 -04001454 };
1455
Joshua Tsuji14e68552019-06-06 17:17:08 -04001456 mFlyout.setVisibility(INVISIBLE);
1457
Mark Renoufc19b4732019-06-26 12:08:33 -04001458 // Post in case layout isn't complete and getWidth returns 0.
1459 post(() -> {
1460 // An auto-expanding bubble could have been posted during the time it takes to
1461 // layout.
1462 if (isExpanded()) {
1463 return;
1464 }
1465
Joshua Tsuji14e68552019-06-06 17:17:08 -04001466 final Runnable afterShow = () -> {
1467 mAnimateInFlyout = () -> {
1468 mFlyout.setVisibility(VISIBLE);
1469 bubble.getIconView().setSuppressDot(
1470 true /* suppressDot */, false /* animate */);
1471 mFlyoutDragDeltaX =
1472 mStackAnimationController.isStackOnLeftSide()
1473 ? -mFlyout.getWidth()
1474 : mFlyout.getWidth();
1475 animateFlyoutCollapsed(false /* collapsed */, 0 /* velX */);
1476 mFlyout.postDelayed(mHideFlyout, FLYOUT_HIDE_AFTER);
1477 };
1478
1479 mFlyout.postDelayed(mAnimateInFlyout, 200);
1480 };
1481
1482 mFlyout.setupFlyoutStartingAsDot(
Mark Renoufc19b4732019-06-26 12:08:33 -04001483 updateMessage, mStackAnimationController.getStackPosition(), getWidth(),
1484 mStackAnimationController.isStackOnLeftSide(),
Joshua Tsuji14e68552019-06-06 17:17:08 -04001485 bubble.getIconView().getBadgeColor(),
1486 afterShow,
Lyn Han61d5d562019-07-01 17:39:38 -07001487 mAfterFlyoutHides,
1488 bubble.getIconView().getDotCenter());
Joshua Tsuji14e68552019-06-06 17:17:08 -04001489 mFlyout.bringToFront();
Mark Renoufc19b4732019-06-26 12:08:33 -04001490 });
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001491 }
Mark Renoufc19b4732019-06-26 12:08:33 -04001492
1493 mFlyout.removeCallbacks(mHideFlyout);
1494 mFlyout.postDelayed(mHideFlyout, FLYOUT_HIDE_AFTER);
1495 logBubbleEvent(bubble, StatsLog.BUBBLE_UICHANGED__ACTION__FLYOUT);
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001496 }
1497
1498 /** Hide the flyout immediately and cancel any pending hide runnables. */
1499 private void hideFlyoutImmediate() {
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001500 if (mAfterFlyoutHides != null) {
1501 mAfterFlyoutHides.run();
1502 }
1503
Joshua Tsuji14e68552019-06-06 17:17:08 -04001504 mFlyout.removeCallbacks(mAnimateInFlyout);
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001505 mFlyout.removeCallbacks(mHideFlyout);
Joshua Tsuji6549e702019-05-02 13:13:16 -04001506 mFlyout.hideFlyout();
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001507 }
1508
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001509 @Override
1510 public void getBoundsOnScreen(Rect outRect) {
1511 if (!mIsExpanded) {
Mady Mellor217b2e92019-02-27 11:44:16 -08001512 if (mBubbleContainer.getChildCount() > 0) {
1513 mBubbleContainer.getChildAt(0).getBoundsOnScreen(outRect);
1514 }
Mady Mellore9371bc2019-07-10 18:50:59 -07001515 // Increase the touch target size of the bubble
1516 outRect.top -= mBubbleTouchPadding;
1517 outRect.left -= mBubbleTouchPadding;
1518 outRect.right += mBubbleTouchPadding;
1519 outRect.bottom += mBubbleTouchPadding;
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001520 } else {
1521 mBubbleContainer.getBoundsOnScreen(outRect);
1522 }
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001523
Joshua Tsuji6549e702019-05-02 13:13:16 -04001524 if (mFlyout.getVisibility() == View.VISIBLE) {
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001525 final Rect flyoutBounds = new Rect();
1526 mFlyout.getBoundsOnScreen(flyoutBounds);
1527 outRect.union(flyoutBounds);
1528 }
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001529 }
1530
1531 private int getStatusBarHeight() {
1532 if (getRootWindowInsets() != null) {
Joshua Tsuji0fee7682019-01-25 11:37:49 -05001533 WindowInsets insets = getRootWindowInsets();
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001534 return Math.max(
Joshua Tsujif44347f2019-02-12 14:28:06 -05001535 mStatusBarHeight,
Joshua Tsuji0fee7682019-01-25 11:37:49 -05001536 insets.getDisplayCutout() != null
1537 ? insets.getDisplayCutout().getSafeInsetTop()
1538 : 0);
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001539 }
1540
1541 return 0;
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001542 }
1543
1544 private boolean isIntersecting(View view, float x, float y) {
1545 mTempLoc = view.getLocationOnScreen();
1546 mTempRect.set(mTempLoc[0], mTempLoc[1], mTempLoc[0] + view.getWidth(),
1547 mTempLoc[1] + view.getHeight());
1548 return mTempRect.contains(x, y);
1549 }
1550
1551 private void requestUpdate() {
Mady Mellorbc078c22019-03-26 17:10:34 -07001552 if (mViewUpdatedRequested || mIsExpansionAnimating) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001553 return;
1554 }
1555 mViewUpdatedRequested = true;
1556 getViewTreeObserver().addOnPreDrawListener(mViewUpdater);
1557 invalidate();
1558 }
1559
1560 private void updateExpandedBubble() {
Issei Suzukia8d07312019-06-07 12:56:19 +02001561 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001562 Log.d(TAG, "updateExpandedBubble()");
1563 }
Mady Mellor3dff9e62019-02-05 18:12:53 -08001564 mExpandedViewContainer.removeAllViews();
1565 if (mExpandedBubble != null && mIsExpanded) {
Mady Mellored99c272019-06-13 15:58:30 -07001566 mExpandedViewContainer.addView(mExpandedBubble.getExpandedView());
1567 mExpandedBubble.getExpandedView().populateExpandedView();
Mady Mellor3dff9e62019-02-05 18:12:53 -08001568 mExpandedViewContainer.setVisibility(mIsExpanded ? VISIBLE : GONE);
Issei Suzukic0387542019-03-08 17:31:14 +01001569 mExpandedViewContainer.setAlpha(1.0f);
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001570 }
1571 }
1572
Lyn Han285ad302019-05-29 19:01:39 -07001573 private void updateExpandedView() {
Issei Suzukia8d07312019-06-07 12:56:19 +02001574 if (DEBUG_BUBBLE_STACK_VIEW) {
Lyn Han285ad302019-05-29 19:01:39 -07001575 Log.d(TAG, "updateExpandedView: mIsExpanded=" + mIsExpanded);
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001576 }
Joshua Tsuji6549e702019-05-02 13:13:16 -04001577
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001578 mExpandedViewContainer.setVisibility(mIsExpanded ? VISIBLE : GONE);
Mady Mellor3dff9e62019-02-05 18:12:53 -08001579 if (mIsExpanded) {
Mady Mellor5029fa62019-03-05 12:16:21 -08001580 // First update the view so that it calculates a new height (ensuring the y position
1581 // calculation is correct)
Mady Mellored99c272019-06-13 15:58:30 -07001582 mExpandedBubble.getExpandedView().updateView();
Lyn Han285ad302019-05-29 19:01:39 -07001583 final float y = getExpandedViewY();
Mady Mellor5d8f1402019-02-21 18:23:52 -08001584 if (!mExpandedViewYAnim.isRunning()) {
1585 // We're not animating so set the value
1586 mExpandedViewContainer.setTranslationY(y);
Mady Mellored99c272019-06-13 15:58:30 -07001587 mExpandedBubble.getExpandedView().updateView();
Mady Mellor5d8f1402019-02-21 18:23:52 -08001588 } else {
Mady Mellorbc078c22019-03-26 17:10:34 -07001589 // We are animating so update the value; there is an end listener on the animator
1590 // that will ensure expandedeView.updateView gets called.
Mady Mellor5d8f1402019-02-21 18:23:52 -08001591 mExpandedViewYAnim.animateToFinalPosition(y);
1592 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001593 }
Mady Mellor3dff9e62019-02-05 18:12:53 -08001594
Joshua Tsuji6549e702019-05-02 13:13:16 -04001595 mStackOnLeftOrWillBe = mStackAnimationController.isStackOnLeftSide();
Joshua Tsuji2862f2e2019-07-29 12:32:33 -04001596 updateBubbleZOrdersAndDotPosition(false);
Joshua Tsuji6549e702019-05-02 13:13:16 -04001597 }
1598
1599 /** Sets the appropriate Z-order and dot position for each bubble in the stack. */
Joshua Tsuji2862f2e2019-07-29 12:32:33 -04001600 private void updateBubbleZOrdersAndDotPosition(boolean animate) {
Lyn Han1b4f25e2019-06-11 13:56:34 -07001601 int bubbleCount = mBubbleContainer.getChildCount();
1602 for (int i = 0; i < bubbleCount; i++) {
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001603 BubbleView bv = (BubbleView) mBubbleContainer.getChildAt(i);
Joshua Tsujidd4d9f92019-05-13 13:57:38 -04001604 bv.updateDotVisibility(true /* animate */);
Joshua Tsuji25a4b7b2019-03-22 14:11:06 -04001605 bv.setZ((BubbleController.MAX_BUBBLES
1606 * getResources().getDimensionPixelSize(R.dimen.bubble_elevation)) - i);
Joshua Tsuji6549e702019-05-02 13:13:16 -04001607 // If the dot is on the left, and so is the stack, we need to change the dot position.
1608 if (bv.getDotPositionOnLeft() == mStackOnLeftOrWillBe) {
1609 bv.setDotPosition(!mStackOnLeftOrWillBe, animate);
1610 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001611 }
1612 }
1613
Mady Mellorde2d4d22019-01-29 14:15:34 -08001614 private void updatePointerPosition() {
Issei Suzukia8d07312019-06-07 12:56:19 +02001615 if (DEBUG_BUBBLE_STACK_VIEW) {
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001616 Log.d(TAG, "updatePointerPosition()");
1617 }
Lyn Han6f6b3ae2019-05-16 14:17:30 -07001618
Lyn Han522e9ff2019-05-17 13:26:13 -07001619 Bubble expandedBubble = getExpandedBubble();
1620 if (expandedBubble == null) {
1621 return;
Mady Mellorde2d4d22019-01-29 14:15:34 -08001622 }
Lyn Han522e9ff2019-05-17 13:26:13 -07001623
1624 int index = getBubbleIndex(expandedBubble);
1625 float bubbleLeftFromScreenLeft = mExpandedAnimationController.getBubbleLeft(index);
1626 float halfBubble = mBubbleSize / 2f;
Mady Mellor9be3bed2019-08-21 17:26:26 -07001627 float bubbleCenter = bubbleLeftFromScreenLeft + halfBubble;
1628 // Padding might be adjusted for insets, so get it directly from the view
1629 bubbleCenter -= mExpandedViewContainer.getPaddingLeft();
Lyn Han522e9ff2019-05-17 13:26:13 -07001630
Mady Mellored99c272019-06-13 15:58:30 -07001631 expandedBubble.getExpandedView().setPointerPosition(bubbleCenter);
Mady Mellorde2d4d22019-01-29 14:15:34 -08001632 }
1633
Steven Wua254dab2019-01-29 11:30:39 -05001634 /**
1635 * @return the number of bubbles in the stack view.
1636 */
Steven Wub00225b2019-02-08 14:27:42 -05001637 public int getBubbleCount() {
Steven Wua254dab2019-01-29 11:30:39 -05001638 return mBubbleContainer.getChildCount();
1639 }
1640
1641 /**
1642 * Finds the bubble index within the stack.
1643 *
Mady Mellor3dff9e62019-02-05 18:12:53 -08001644 * @param bubble the bubble to look up.
Steven Wua254dab2019-01-29 11:30:39 -05001645 * @return the index of the bubble view within the bubble stack. The range of the position
1646 * is between 0 and the bubble count minus 1.
1647 */
Steven Wua62cb6a2019-02-15 17:12:51 -05001648 int getBubbleIndex(@Nullable Bubble bubble) {
1649 if (bubble == null) {
1650 return 0;
1651 }
Mady Mellored99c272019-06-13 15:58:30 -07001652 return mBubbleContainer.indexOfChild(bubble.getIconView());
Steven Wua254dab2019-01-29 11:30:39 -05001653 }
1654
1655 /**
1656 * @return the normalized x-axis position of the bubble stack rounded to 4 decimal places.
1657 */
Steven Wub00225b2019-02-08 14:27:42 -05001658 public float getNormalizedXPosition() {
Joshua Tsuji442b6272019-02-08 13:23:43 -05001659 return new BigDecimal(getStackPosition().x / mDisplaySize.x)
Steven Wua254dab2019-01-29 11:30:39 -05001660 .setScale(4, RoundingMode.CEILING.HALF_UP)
1661 .floatValue();
1662 }
1663
1664 /**
1665 * @return the normalized y-axis position of the bubble stack rounded to 4 decimal places.
1666 */
Steven Wub00225b2019-02-08 14:27:42 -05001667 public float getNormalizedYPosition() {
Joshua Tsuji442b6272019-02-08 13:23:43 -05001668 return new BigDecimal(getStackPosition().y / mDisplaySize.y)
Steven Wua254dab2019-01-29 11:30:39 -05001669 .setScale(4, RoundingMode.CEILING.HALF_UP)
1670 .floatValue();
1671 }
1672
Joshua Tsujia19515f2019-02-13 18:02:29 -05001673 public PointF getStackPosition() {
1674 return mStackAnimationController.getStackPosition();
1675 }
1676
Steven Wua254dab2019-01-29 11:30:39 -05001677 /**
1678 * Logs the bubble UI event.
1679 *
1680 * @param bubble the bubble that is being interacted on. Null value indicates that
1681 * the user interaction is not specific to one bubble.
1682 * @param action the user interaction enum.
1683 */
Mady Mellor3dff9e62019-02-05 18:12:53 -08001684 private void logBubbleEvent(@Nullable Bubble bubble, int action) {
Mady Mellored99c272019-06-13 15:58:30 -07001685 if (bubble == null || bubble.getEntry() == null
1686 || bubble.getEntry().notification == null) {
Steven Wua254dab2019-01-29 11:30:39 -05001687 StatsLog.write(StatsLog.BUBBLE_UI_CHANGED,
1688 null /* package name */,
1689 null /* notification channel */,
1690 0 /* notification ID */,
1691 0 /* bubble position */,
1692 getBubbleCount(),
1693 action,
1694 getNormalizedXPosition(),
Steven Wu45e38ae2019-03-25 16:16:59 -04001695 getNormalizedYPosition(),
Steven Wu8ba8ca92019-04-11 10:47:42 -04001696 false /* unread bubble */,
1697 false /* on-going bubble */,
Mady Mellor6cec3502019-06-17 19:20:49 -07001698 false /* isAppForeground (unused) */);
Steven Wua254dab2019-01-29 11:30:39 -05001699 } else {
Mady Mellored99c272019-06-13 15:58:30 -07001700 StatusBarNotification notification = bubble.getEntry().notification;
Steven Wua254dab2019-01-29 11:30:39 -05001701 StatsLog.write(StatsLog.BUBBLE_UI_CHANGED,
1702 notification.getPackageName(),
1703 notification.getNotification().getChannelId(),
1704 notification.getId(),
1705 getBubbleIndex(bubble),
1706 getBubbleCount(),
1707 action,
1708 getNormalizedXPosition(),
Steven Wu45e38ae2019-03-25 16:16:59 -04001709 getNormalizedYPosition(),
Mady Mellor99a302602019-06-14 11:39:56 -07001710 bubble.showInShadeWhenBubble(),
1711 bubble.isOngoing(),
Mady Mellor6cec3502019-06-17 19:20:49 -07001712 false /* isAppForeground (unused) */);
Steven Wua254dab2019-01-29 11:30:39 -05001713 }
1714 }
Mark Renouf041d7262019-02-06 12:09:41 -05001715
1716 /**
1717 * Called when a back gesture should be directed to the Bubbles stack. When expanded,
1718 * a back key down/up event pair is forwarded to the bubble Activity.
1719 */
1720 boolean performBackPressIfNeeded() {
1721 if (!isExpanded()) {
1722 return false;
1723 }
Mady Mellored99c272019-06-13 15:58:30 -07001724 return mExpandedBubble.getExpandedView().performBackPressIfNeeded();
Mark Renouf041d7262019-02-06 12:09:41 -05001725 }
Mark Renouf9ba6cea2019-04-17 11:53:50 -04001726
1727 /** For debugging only */
1728 List<Bubble> getBubblesOnScreen() {
1729 List<Bubble> bubbles = new ArrayList<>();
1730 for (int i = 0; i < mBubbleContainer.getChildCount(); i++) {
1731 View child = mBubbleContainer.getChildAt(i);
1732 if (child instanceof BubbleView) {
1733 String key = ((BubbleView) child).getKey();
1734 Bubble bubble = mBubbleData.getBubbleWithKey(key);
1735 bubbles.add(bubble);
1736 }
1737 }
1738 return bubbles;
1739 }
Mady Mellorc3d6f7d2018-11-07 09:36:56 -08001740}