Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
| 17 | package com.android.systemui.bubbles.animation; |
| 18 | |
Joshua Tsuji | f44347f | 2019-02-12 14:28:06 -0500 | [diff] [blame] | 19 | import android.content.res.Resources; |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 20 | import android.graphics.Point; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 21 | import android.graphics.PointF; |
| 22 | import android.view.View; |
| 23 | import android.view.WindowInsets; |
| 24 | |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 25 | import androidx.annotation.Nullable; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 26 | import androidx.dynamicanimation.animation.DynamicAnimation; |
| 27 | import androidx.dynamicanimation.animation.SpringForce; |
| 28 | |
| 29 | import com.android.systemui.R; |
| 30 | |
| 31 | import com.google.android.collect.Sets; |
| 32 | |
| 33 | import java.util.Set; |
| 34 | |
| 35 | /** |
| 36 | * Animation controller for bubbles when they're in their expanded state, or animating to/from the |
| 37 | * expanded state. This controls the expansion animation as well as bubbles 'dragging out' to be |
| 38 | * dismissed. |
| 39 | */ |
| 40 | public class ExpandedAnimationController |
| 41 | extends PhysicsAnimationLayout.PhysicsAnimationController { |
| 42 | |
| 43 | /** |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 44 | * How much to translate the bubbles when they're animating in/out. This value is multiplied by |
| 45 | * the bubble size. |
| 46 | */ |
| 47 | private static final int ANIMATE_TRANSLATION_FACTOR = 4; |
| 48 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 49 | /** How much to scale down bubbles when they're animating in/out. */ |
| 50 | private static final float ANIMATE_SCALE_PERCENT = 0.5f; |
| 51 | |
Joshua Tsuji | 3829caa | 2019-03-05 18:09:13 -0500 | [diff] [blame] | 52 | /** The stack position to collapse back to in {@link #collapseBackToStack}. */ |
| 53 | private PointF mCollapseToPoint; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 54 | |
| 55 | /** Horizontal offset between bubbles, which we need to know to re-stack them. */ |
| 56 | private float mStackOffsetPx; |
Lyn Han | 4a8efe3 | 2019-05-30 09:43:27 -0700 | [diff] [blame] | 57 | /** Space between status bar and bubbles in the expanded state. */ |
| 58 | private float mBubblePaddingTop; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 59 | /** Size of each bubble. */ |
| 60 | private float mBubbleSizePx; |
Joshua Tsuji | f44347f | 2019-02-12 14:28:06 -0500 | [diff] [blame] | 61 | /** Height of the status bar. */ |
| 62 | private float mStatusBarHeight; |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 63 | /** Size of display. */ |
| 64 | private Point mDisplaySize; |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 65 | /** Max number of bubbles shown in row above expanded view.*/ |
| 66 | private int mBubblesMaxRendered; |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 67 | |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 68 | /** Whether the dragged-out bubble is in the dismiss target. */ |
| 69 | private boolean mIndividualBubbleWithinDismissTarget = false; |
| 70 | |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 71 | private boolean mAnimatingExpand = false; |
| 72 | private boolean mAnimatingCollapse = false; |
| 73 | private Runnable mAfterExpand; |
| 74 | private Runnable mAfterCollapse; |
| 75 | private PointF mCollapsePoint; |
| 76 | |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 77 | /** |
| 78 | * Whether the dragged out bubble is springing towards the touch point, rather than using the |
| 79 | * default behavior of moving directly to the touch point. |
| 80 | * |
| 81 | * This happens when the user's finger exits the dismiss area while the bubble is magnetized to |
| 82 | * the center. Since the touch point differs from the bubble location, we need to animate the |
| 83 | * bubble back to the touch point to avoid a jarring instant location change from the center of |
| 84 | * the target to the touch point just outside the target bounds. |
| 85 | */ |
| 86 | private boolean mSpringingBubbleToTouch = false; |
| 87 | |
Lyn Han | 6f6b3ae | 2019-05-16 14:17:30 -0700 | [diff] [blame] | 88 | private int mExpandedViewPadding; |
Lyn Han | 1b4f25e | 2019-06-11 13:56:34 -0700 | [diff] [blame] | 89 | private float mLauncherGridDiff; |
Lyn Han | 6f6b3ae | 2019-05-16 14:17:30 -0700 | [diff] [blame] | 90 | |
| 91 | public ExpandedAnimationController(Point displaySize, int expandedViewPadding) { |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 92 | mDisplaySize = displaySize; |
Lyn Han | 6f6b3ae | 2019-05-16 14:17:30 -0700 | [diff] [blame] | 93 | mExpandedViewPadding = expandedViewPadding; |
Lyn Han | 1b4f25e | 2019-06-11 13:56:34 -0700 | [diff] [blame] | 94 | mLauncherGridDiff = 30f; |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 95 | } |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 96 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 97 | /** |
| 98 | * Whether the individual bubble has been dragged out of the row of bubbles far enough to cause |
| 99 | * the rest of the bubbles to animate to fill the gap. |
| 100 | */ |
| 101 | private boolean mBubbleDraggedOutEnough = false; |
| 102 | |
| 103 | /** The bubble currently being dragged out of the row (to potentially be dismissed). */ |
| 104 | private View mBubbleDraggingOut; |
| 105 | |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 106 | /** |
| 107 | * Animates expanding the bubbles into a row along the top of the screen. |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 108 | */ |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 109 | public void expandFromStack(Runnable after) { |
| 110 | mAnimatingCollapse = false; |
| 111 | mAnimatingExpand = true; |
| 112 | mAfterExpand = after; |
Joshua Tsuji | c110843 | 2019-02-22 16:10:12 -0500 | [diff] [blame] | 113 | |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 114 | startOrUpdateExpandAnimation(); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** Animate collapsing the bubbles back to their stacked position. */ |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 118 | public void collapseBackToStack(PointF collapsePoint, Runnable after) { |
| 119 | mAnimatingExpand = false; |
| 120 | mAnimatingCollapse = true; |
| 121 | mAfterCollapse = after; |
| 122 | mCollapsePoint = collapsePoint; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 123 | |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 124 | startOrUpdateCollapseAnimation(); |
| 125 | } |
| 126 | |
| 127 | private void startOrUpdateExpandAnimation() { |
Joshua Tsuji | c110843 | 2019-02-22 16:10:12 -0500 | [diff] [blame] | 128 | animationsForChildrenFromIndex( |
| 129 | 0, /* startIndex */ |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 130 | (index, animation) -> animation.position(getBubbleLeft(index), getExpandedY())) |
| 131 | .startAll(() -> { |
| 132 | mAnimatingExpand = false; |
| 133 | |
| 134 | if (mAfterExpand != null) { |
| 135 | mAfterExpand.run(); |
| 136 | } |
| 137 | |
| 138 | mAfterExpand = null; |
| 139 | }); |
| 140 | } |
| 141 | |
| 142 | private void startOrUpdateCollapseAnimation() { |
| 143 | // Stack to the left if we're going to the left, or right if not. |
| 144 | final float sideMultiplier = mLayout.isFirstChildXLeftOfCenter(mCollapsePoint.x) ? -1 : 1; |
| 145 | animationsForChildrenFromIndex( |
| 146 | 0, /* startIndex */ |
| 147 | (index, animation) -> { |
Joshua Tsuji | c110843 | 2019-02-22 16:10:12 -0500 | [diff] [blame] | 148 | animation.position( |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 149 | mCollapsePoint.x + (sideMultiplier * index * mStackOffsetPx), |
| 150 | mCollapsePoint.y); |
| 151 | }) |
| 152 | .startAll(() -> { |
| 153 | mAnimatingCollapse = false; |
| 154 | |
| 155 | if (mAfterCollapse != null) { |
| 156 | mAfterCollapse.run(); |
| 157 | } |
| 158 | |
| 159 | mAfterCollapse = null; |
| 160 | }); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 163 | /** Prepares the given bubble to be dragged out. */ |
| 164 | public void prepareForBubbleDrag(View bubble) { |
| 165 | mLayout.cancelAnimationsOnView(bubble); |
| 166 | |
| 167 | mBubbleDraggingOut = bubble; |
| 168 | mBubbleDraggingOut.setTranslationZ(Short.MAX_VALUE); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Drags an individual bubble to the given coordinates. Bubbles to the right will animate to |
| 173 | * take its place once it's dragged out of the row of bubbles, and animate out of the way if the |
| 174 | * bubble is dragged back into the row. |
| 175 | */ |
| 176 | public void dragBubbleOut(View bubbleView, float x, float y) { |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 177 | if (mSpringingBubbleToTouch) { |
| 178 | if (mLayout.arePropertiesAnimatingOnView( |
| 179 | bubbleView, DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y)) { |
| 180 | animationForChild(mBubbleDraggingOut) |
| 181 | .translationX(x) |
| 182 | .translationY(y) |
| 183 | .withStiffness(SpringForce.STIFFNESS_HIGH) |
| 184 | .start(); |
| 185 | } else { |
| 186 | mSpringingBubbleToTouch = false; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (!mSpringingBubbleToTouch && !mIndividualBubbleWithinDismissTarget) { |
| 191 | bubbleView.setTranslationX(x); |
| 192 | bubbleView.setTranslationY(y); |
| 193 | } |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 194 | |
| 195 | final boolean draggedOutEnough = |
| 196 | y > getExpandedY() + mBubbleSizePx || y < getExpandedY() - mBubbleSizePx; |
| 197 | if (draggedOutEnough != mBubbleDraggedOutEnough) { |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 198 | updateBubblePositions(); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 199 | mBubbleDraggedOutEnough = draggedOutEnough; |
| 200 | } |
| 201 | } |
| 202 | |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 203 | /** Plays a dismiss animation on the dragged out bubble. */ |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 204 | public void dismissDraggedOutBubble(View bubble, Runnable after) { |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 205 | mIndividualBubbleWithinDismissTarget = false; |
| 206 | |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 207 | animationForChild(bubble) |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 208 | .withStiffness(SpringForce.STIFFNESS_HIGH) |
| 209 | .scaleX(1.1f) |
| 210 | .scaleY(1.1f) |
| 211 | .alpha(0f, after) |
| 212 | .start(); |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 213 | |
| 214 | updateBubblePositions(); |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 217 | @Nullable public View getDraggedOutBubble() { |
| 218 | return mBubbleDraggingOut; |
| 219 | } |
| 220 | |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 221 | /** Magnets the given bubble to the dismiss target. */ |
| 222 | public void magnetBubbleToDismiss( |
| 223 | View bubbleView, float velX, float velY, float destY, Runnable after) { |
| 224 | mIndividualBubbleWithinDismissTarget = true; |
| 225 | mSpringingBubbleToTouch = false; |
| 226 | animationForChild(bubbleView) |
| 227 | .withStiffness(SpringForce.STIFFNESS_MEDIUM) |
| 228 | .withDampingRatio(SpringForce.DAMPING_RATIO_LOW_BOUNCY) |
| 229 | .withPositionStartVelocities(velX, velY) |
| 230 | .translationX(mLayout.getWidth() / 2f - mBubbleSizePx / 2f) |
| 231 | .translationY(destY, after) |
| 232 | .start(); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Springs the dragged-out bubble towards the given coordinates and sets flags to have touch |
| 237 | * events update the spring's final position until it's settled. |
| 238 | */ |
| 239 | public void demagnetizeBubbleTo(float x, float y, float velX, float velY) { |
| 240 | mIndividualBubbleWithinDismissTarget = false; |
| 241 | mSpringingBubbleToTouch = true; |
| 242 | |
| 243 | animationForChild(mBubbleDraggingOut) |
| 244 | .translationX(x) |
| 245 | .translationY(y) |
| 246 | .withPositionStartVelocities(velX, velY) |
| 247 | .withStiffness(SpringForce.STIFFNESS_HIGH) |
| 248 | .start(); |
| 249 | } |
| 250 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 251 | /** |
| 252 | * Snaps a bubble back to its position within the bubble row, and animates the rest of the |
| 253 | * bubbles to accommodate it if it was previously dragged out past the threshold. |
| 254 | */ |
| 255 | public void snapBubbleBack(View bubbleView, float velX, float velY) { |
| 256 | final int index = mLayout.indexOfChild(bubbleView); |
| 257 | |
Joshua Tsuji | c110843 | 2019-02-22 16:10:12 -0500 | [diff] [blame] | 258 | animationForChildAtIndex(index) |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 259 | .position(getBubbleLeft(index), getExpandedY()) |
| 260 | .withPositionStartVelocities(velX, velY) |
| 261 | .start(() -> bubbleView.setTranslationZ(0f) /* after */); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 262 | |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 263 | updateBubblePositions(); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 264 | } |
| 265 | |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 266 | /** Resets bubble drag out gesture flags. */ |
| 267 | public void onGestureFinished() { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 268 | mBubbleDraggedOutEnough = false; |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 269 | mBubbleDraggingOut = null; |
Joshua Tsuji | 61b38f5 | 2019-05-31 16:20:22 -0400 | [diff] [blame] | 270 | updateBubblePositions(); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 274 | * Animates the bubbles to {@link #getExpandedY()} position. Used in response to IME showing. |
| 275 | */ |
| 276 | public void updateYPosition(Runnable after) { |
| 277 | if (mLayout == null) return; |
Joshua Tsuji | c110843 | 2019-02-22 16:10:12 -0500 | [diff] [blame] | 278 | animationsForChildrenFromIndex( |
| 279 | 0, (i, anim) -> anim.translationY(getExpandedY())).startAll(after); |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 282 | /** The Y value of the row of expanded bubbles. */ |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 283 | public float getExpandedY() { |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 284 | if (mLayout == null || mLayout.getRootWindowInsets() == null) { |
| 285 | return 0; |
| 286 | } |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 287 | final WindowInsets insets = mLayout.getRootWindowInsets(); |
Lyn Han | 4a8efe3 | 2019-05-30 09:43:27 -0700 | [diff] [blame] | 288 | return mBubblePaddingTop + Math.max( |
Lyn Han | 5aa27e2 | 2019-05-15 10:55:07 -0700 | [diff] [blame] | 289 | mStatusBarHeight, |
| 290 | insets.getDisplayCutout() != null |
| 291 | ? insets.getDisplayCutout().getSafeInsetTop() |
| 292 | : 0); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 295 | @Override |
Joshua Tsuji | c36ee6f | 2019-05-28 17:00:16 -0400 | [diff] [blame] | 296 | void onActiveControllerForLayout(PhysicsAnimationLayout layout) { |
| 297 | final Resources res = layout.getResources(); |
| 298 | mStackOffsetPx = res.getDimensionPixelSize(R.dimen.bubble_stack_offset); |
Joshua Tsuji | 61b38f5 | 2019-05-31 16:20:22 -0400 | [diff] [blame] | 299 | mBubblePaddingTop = res.getDimensionPixelSize(R.dimen.bubble_padding_top); |
Joshua Tsuji | c36ee6f | 2019-05-28 17:00:16 -0400 | [diff] [blame] | 300 | mBubbleSizePx = res.getDimensionPixelSize(R.dimen.individual_bubble_size); |
| 301 | mStatusBarHeight = |
| 302 | res.getDimensionPixelSize(com.android.internal.R.dimen.status_bar_height); |
Joshua Tsuji | 61b38f5 | 2019-05-31 16:20:22 -0400 | [diff] [blame] | 303 | mBubblesMaxRendered = res.getInteger(R.integer.bubbles_max_rendered); |
Joshua Tsuji | c36ee6f | 2019-05-28 17:00:16 -0400 | [diff] [blame] | 304 | |
| 305 | // Ensure that all child views are at 1x scale, and visible, in case they were animating |
| 306 | // in. |
| 307 | mLayout.setVisibility(View.VISIBLE); |
| 308 | animationsForChildrenFromIndex(0 /* startIndex */, (index, animation) -> |
| 309 | animation.scaleX(1f).scaleY(1f).alpha(1f)).startAll(); |
| 310 | } |
| 311 | |
| 312 | @Override |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 313 | Set<DynamicAnimation.ViewProperty> getAnimatedProperties() { |
| 314 | return Sets.newHashSet( |
| 315 | DynamicAnimation.TRANSLATION_X, |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 316 | DynamicAnimation.TRANSLATION_Y, |
| 317 | DynamicAnimation.SCALE_X, |
| 318 | DynamicAnimation.SCALE_Y, |
| 319 | DynamicAnimation.ALPHA); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | @Override |
| 323 | int getNextAnimationInChain(DynamicAnimation.ViewProperty property, int index) { |
| 324 | return NONE; |
| 325 | } |
| 326 | |
| 327 | @Override |
| 328 | float getOffsetForChainedPropertyAnimation(DynamicAnimation.ViewProperty property) { |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | @Override |
| 333 | SpringForce getSpringForce(DynamicAnimation.ViewProperty property, View view) { |
| 334 | return new SpringForce() |
Joshua Tsuji | 010c2b1 | 2019-02-25 18:11:25 -0500 | [diff] [blame] | 335 | .setDampingRatio(SpringForce.DAMPING_RATIO_LOW_BOUNCY) |
| 336 | .setStiffness(SpringForce.STIFFNESS_LOW); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | @Override |
| 340 | void onChildAdded(View child, int index) { |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 341 | // If a bubble is added while the expand/collapse animations are playing, update the |
| 342 | // animation to include the new bubble. |
| 343 | if (mAnimatingExpand) { |
| 344 | startOrUpdateExpandAnimation(); |
| 345 | } else if (mAnimatingCollapse) { |
| 346 | startOrUpdateCollapseAnimation(); |
| 347 | } else { |
Joshua Tsuji | 61b38f5 | 2019-05-31 16:20:22 -0400 | [diff] [blame] | 348 | child.setTranslationX(getBubbleLeft(index)); |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 349 | animationForChild(child) |
| 350 | .translationY( |
| 351 | getExpandedY() - mBubbleSizePx * ANIMATE_TRANSLATION_FACTOR, /* from */ |
| 352 | getExpandedY() /* to */) |
| 353 | .start(); |
| 354 | updateBubblePositions(); |
| 355 | } |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | @Override |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 359 | void onChildRemoved(View child, int index, Runnable finishRemoval) { |
Joshua Tsuji | c110843 | 2019-02-22 16:10:12 -0500 | [diff] [blame] | 360 | final PhysicsAnimationLayout.PhysicsPropertyAnimator animator = animationForChild(child); |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 361 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 362 | // If we're removing the dragged-out bubble, that means it got dismissed. |
| 363 | if (child.equals(mBubbleDraggingOut)) { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 364 | mBubbleDraggingOut = null; |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 365 | finishRemoval.run(); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 366 | } else { |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 367 | animator.alpha(0f, finishRemoval /* endAction */) |
| 368 | .withStiffness(SpringForce.STIFFNESS_HIGH) |
| 369 | .withDampingRatio(SpringForce.DAMPING_RATIO_NO_BOUNCY) |
| 370 | .scaleX(1.1f) |
| 371 | .scaleY(1.1f) |
| 372 | .start(); |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 373 | } |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 374 | |
| 375 | // Animate all the other bubbles to their new positions sans this bubble. |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 376 | updateBubblePositions(); |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | @Override |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 380 | void onChildReordered(View child, int oldIndex, int newIndex) { |
| 381 | updateBubblePositions(); |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 382 | } |
| 383 | |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 384 | private void updateBubblePositions() { |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 385 | if (mAnimatingExpand || mAnimatingCollapse) { |
| 386 | return; |
| 387 | } |
| 388 | |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 389 | for (int i = 0; i < mLayout.getChildCount(); i++) { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 390 | final View bubble = mLayout.getChildAt(i); |
| 391 | |
| 392 | // Don't animate the dragging out bubble, or it'll jump around while being dragged. It |
| 393 | // will be snapped to the correct X value after the drag (if it's not dismissed). |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 394 | if (bubble.equals(mBubbleDraggingOut)) { |
| 395 | return; |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 396 | } |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 397 | |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 398 | animationForChild(bubble) |
| 399 | .translationX(getBubbleLeft(i)) |
| 400 | .start(); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 404 | /** |
| 405 | * @param index Bubble index in row. |
| 406 | * @return Bubble left x from left edge of screen. |
| 407 | */ |
| 408 | public float getBubbleLeft(int index) { |
Lyn Han | 5390ce3 | 2019-05-31 07:25:50 -0700 | [diff] [blame] | 409 | final float bubbleFromRowLeft = index * (mBubbleSizePx + getSpaceBetweenBubbles()); |
Lyn Han | 4a8efe3 | 2019-05-30 09:43:27 -0700 | [diff] [blame] | 410 | return getRowLeft() + bubbleFromRowLeft; |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | private float getRowLeft() { |
| 414 | if (mLayout == null) { |
| 415 | return 0; |
| 416 | } |
Lyn Han | 4a8efe3 | 2019-05-30 09:43:27 -0700 | [diff] [blame] | 417 | |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 418 | int bubbleCount = mLayout.getChildCount(); |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 419 | |
Lyn Han | 4a8efe3 | 2019-05-30 09:43:27 -0700 | [diff] [blame] | 420 | final float totalBubbleWidth = bubbleCount * mBubbleSizePx; |
| 421 | final float totalGapWidth = (bubbleCount - 1) * getSpaceBetweenBubbles(); |
| 422 | final float rowWidth = totalGapWidth + totalBubbleWidth; |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 423 | |
Lyn Han | 4a8efe3 | 2019-05-30 09:43:27 -0700 | [diff] [blame] | 424 | final float centerScreen = mDisplaySize.x / 2f; |
| 425 | final float halfRow = rowWidth / 2f; |
| 426 | final float rowLeft = centerScreen - halfRow; |
| 427 | |
| 428 | return rowLeft; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * @return Space between bubbles in row above expanded view. |
| 433 | */ |
| 434 | private float getSpaceBetweenBubbles() { |
| 435 | /** |
| 436 | * Ordered left to right: |
| 437 | * Screen edge |
| 438 | * [mExpandedViewPadding] |
| 439 | * Expanded view edge |
| 440 | * [launcherGridDiff] --- arbitrary value until launcher exports widths |
| 441 | * Launcher's app icon grid edge that we must match |
| 442 | */ |
Lyn Han | 1b4f25e | 2019-06-11 13:56:34 -0700 | [diff] [blame] | 443 | final float rowMargins = (mExpandedViewPadding + mLauncherGridDiff) * 2; |
Lyn Han | 4a8efe3 | 2019-05-30 09:43:27 -0700 | [diff] [blame] | 444 | final float maxRowWidth = mDisplaySize.x - rowMargins; |
| 445 | |
| 446 | final float totalBubbleWidth = mBubblesMaxRendered * mBubbleSizePx; |
| 447 | final float totalGapWidth = maxRowWidth - totalBubbleWidth; |
| 448 | |
| 449 | final int gapCount = mBubblesMaxRendered - 1; |
| 450 | final float gapWidth = totalGapWidth / gapCount; |
Lyn Han | 4a8efe3 | 2019-05-30 09:43:27 -0700 | [diff] [blame] | 451 | return gapWidth; |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 452 | } |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 453 | } |