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 | |
Lyn Han | f473031 | 2019-06-18 11:18:58 -0700 | [diff] [blame] | 19 | import android.content.res.Configuration; |
Joshua Tsuji | f44347f | 2019-02-12 14:28:06 -0500 | [diff] [blame] | 20 | import android.content.res.Resources; |
Joshua Tsuji | debd831 | 2019-06-06 17:17:08 -0400 | [diff] [blame] | 21 | import android.graphics.Path; |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 22 | import android.graphics.Point; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 23 | import android.graphics.PointF; |
Mady Mellor | e19353d | 2019-08-21 17:25:02 -0700 | [diff] [blame] | 24 | import android.view.DisplayCutout; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 25 | import android.view.View; |
| 26 | import android.view.WindowInsets; |
| 27 | |
Joshua Tsuji | 2010354 | 2020-02-18 14:06:28 -0500 | [diff] [blame] | 28 | import androidx.annotation.NonNull; |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 29 | import androidx.annotation.Nullable; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 30 | import androidx.dynamicanimation.animation.DynamicAnimation; |
| 31 | import androidx.dynamicanimation.animation.SpringForce; |
| 32 | |
Joshua Tsuji | debd831 | 2019-06-06 17:17:08 -0400 | [diff] [blame] | 33 | import com.android.systemui.Interpolators; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 34 | import com.android.systemui.R; |
Joshua Tsuji | 4395bbd | 2020-05-19 17:53:33 -0400 | [diff] [blame] | 35 | import com.android.systemui.util.animation.PhysicsAnimator; |
Joshua Tsuji | 2010354 | 2020-02-18 14:06:28 -0500 | [diff] [blame] | 36 | import com.android.systemui.util.magnetictarget.MagnetizedObject; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 37 | |
| 38 | import com.google.android.collect.Sets; |
| 39 | |
Joshua Tsuji | 395bcfe | 2019-07-02 19:23:23 -0400 | [diff] [blame] | 40 | import java.io.FileDescriptor; |
| 41 | import java.io.PrintWriter; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 42 | import java.util.Set; |
| 43 | |
| 44 | /** |
| 45 | * Animation controller for bubbles when they're in their expanded state, or animating to/from the |
| 46 | * expanded state. This controls the expansion animation as well as bubbles 'dragging out' to be |
| 47 | * dismissed. |
| 48 | */ |
| 49 | public class ExpandedAnimationController |
| 50 | extends PhysicsAnimationLayout.PhysicsAnimationController { |
| 51 | |
| 52 | /** |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 53 | * How much to translate the bubbles when they're animating in/out. This value is multiplied by |
| 54 | * the bubble size. |
| 55 | */ |
| 56 | private static final int ANIMATE_TRANSLATION_FACTOR = 4; |
| 57 | |
Joshua Tsuji | debd831 | 2019-06-06 17:17:08 -0400 | [diff] [blame] | 58 | /** Duration of the expand/collapse target path animation. */ |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 59 | public static final int EXPAND_COLLAPSE_TARGET_ANIM_DURATION = 175; |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 60 | |
Joshua Tsuji | debd831 | 2019-06-06 17:17:08 -0400 | [diff] [blame] | 61 | /** Stiffness for the expand/collapse path-following animation. */ |
| 62 | private static final int EXPAND_COLLAPSE_ANIM_STIFFNESS = 1000; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 63 | |
Mady Mellor | e19353d | 2019-08-21 17:25:02 -0700 | [diff] [blame] | 64 | /** What percentage of the screen to use when centering the bubbles in landscape. */ |
| 65 | private static final float CENTER_BUBBLES_LANDSCAPE_PERCENT = 0.66f; |
| 66 | |
Joshua Tsuji | 2010354 | 2020-02-18 14:06:28 -0500 | [diff] [blame] | 67 | /** |
| 68 | * Velocity required to dismiss an individual bubble without dragging it into the dismiss |
| 69 | * target. |
| 70 | */ |
| 71 | private static final float FLING_TO_DISMISS_MIN_VELOCITY = 6000f; |
| 72 | |
Joshua Tsuji | 4395bbd | 2020-05-19 17:53:33 -0400 | [diff] [blame] | 73 | private final PhysicsAnimator.SpringConfig mAnimateOutSpringConfig = |
| 74 | new PhysicsAnimator.SpringConfig( |
| 75 | EXPAND_COLLAPSE_ANIM_STIFFNESS, SpringForce.DAMPING_RATIO_NO_BOUNCY); |
| 76 | |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 77 | /** Horizontal offset between bubbles, which we need to know to re-stack them. */ |
| 78 | private float mStackOffsetPx; |
Lyn Han | 4a8efe3 | 2019-05-30 09:43:27 -0700 | [diff] [blame] | 79 | /** Space between status bar and bubbles in the expanded state. */ |
| 80 | private float mBubblePaddingTop; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 81 | /** Size of each bubble. */ |
| 82 | private float mBubbleSizePx; |
Lyn Han | c47e171 | 2020-01-28 21:43:34 -0800 | [diff] [blame] | 83 | /** Space between bubbles in row above expanded view. */ |
| 84 | private float mSpaceBetweenBubbles; |
Joshua Tsuji | f44347f | 2019-02-12 14:28:06 -0500 | [diff] [blame] | 85 | /** Height of the status bar. */ |
| 86 | private float mStatusBarHeight; |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 87 | /** Size of display. */ |
| 88 | private Point mDisplaySize; |
Lyn Han | c47e171 | 2020-01-28 21:43:34 -0800 | [diff] [blame] | 89 | /** Max number of bubbles shown in row above expanded view. */ |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 90 | private int mBubblesMaxRendered; |
Mady Mellor | e19353d | 2019-08-21 17:25:02 -0700 | [diff] [blame] | 91 | /** What the current screen orientation is. */ |
| 92 | private int mScreenOrientation; |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 93 | |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 94 | private boolean mAnimatingExpand = false; |
Josh Tsuji | d12d46a | 2020-06-12 12:51:19 -0400 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * Whether we are animating other Bubbles UI elements out in preparation for a call to |
| 98 | * {@link #collapseBackToStack}. If true, we won't animate bubbles in response to adds or |
| 99 | * reorders. |
| 100 | */ |
| 101 | private boolean mPreparingToCollapse = false; |
| 102 | |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 103 | private boolean mAnimatingCollapse = false; |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 104 | private @Nullable Runnable mAfterExpand; |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 105 | private Runnable mAfterCollapse; |
| 106 | private PointF mCollapsePoint; |
| 107 | |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 108 | /** |
| 109 | * Whether the dragged out bubble is springing towards the touch point, rather than using the |
| 110 | * default behavior of moving directly to the touch point. |
| 111 | * |
| 112 | * This happens when the user's finger exits the dismiss area while the bubble is magnetized to |
| 113 | * the center. Since the touch point differs from the bubble location, we need to animate the |
| 114 | * bubble back to the touch point to avoid a jarring instant location change from the center of |
| 115 | * the target to the touch point just outside the target bounds. |
| 116 | */ |
| 117 | private boolean mSpringingBubbleToTouch = false; |
| 118 | |
Joshua Tsuji | 2010354 | 2020-02-18 14:06:28 -0500 | [diff] [blame] | 119 | /** |
| 120 | * Whether to spring the bubble to the next touch event coordinates. This is used to animate the |
| 121 | * bubble out of the magnetic dismiss target to the touch location. |
| 122 | * |
| 123 | * Once it 'catches up' and the animation ends, we'll revert to moving it directly. |
| 124 | */ |
| 125 | private boolean mSpringToTouchOnNextMotionEvent = false; |
| 126 | |
| 127 | /** The bubble currently being dragged out of the row (to potentially be dismissed). */ |
| 128 | private MagnetizedObject<View> mMagnetizedBubbleDraggingOut; |
| 129 | |
Lyn Han | 6f6b3ae | 2019-05-16 14:17:30 -0700 | [diff] [blame] | 130 | private int mExpandedViewPadding; |
| 131 | |
Joshua Tsuji | 4395bbd | 2020-05-19 17:53:33 -0400 | [diff] [blame] | 132 | /** |
| 133 | * Callback to run whenever any bubble is animated out. The BubbleStackView will check if the |
| 134 | * end of this animation means we have no bubbles left, and notify the BubbleController. |
| 135 | */ |
| 136 | private Runnable mOnBubbleAnimatedOutAction; |
| 137 | |
Lyn Han | f473031 | 2019-06-18 11:18:58 -0700 | [diff] [blame] | 138 | public ExpandedAnimationController(Point displaySize, int expandedViewPadding, |
Joshua Tsuji | 4395bbd | 2020-05-19 17:53:33 -0400 | [diff] [blame] | 139 | int orientation, Runnable onBubbleAnimatedOutAction) { |
Lyn Han | b4b0613 | 2020-05-11 09:25:20 -0700 | [diff] [blame] | 140 | updateResources(orientation, displaySize); |
Lyn Han | 6f6b3ae | 2019-05-16 14:17:30 -0700 | [diff] [blame] | 141 | mExpandedViewPadding = expandedViewPadding; |
Joshua Tsuji | 4395bbd | 2020-05-19 17:53:33 -0400 | [diff] [blame] | 142 | mOnBubbleAnimatedOutAction = onBubbleAnimatedOutAction; |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 143 | } |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 144 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 145 | /** |
| 146 | * Whether the individual bubble has been dragged out of the row of bubbles far enough to cause |
| 147 | * the rest of the bubbles to animate to fill the gap. |
| 148 | */ |
| 149 | private boolean mBubbleDraggedOutEnough = false; |
| 150 | |
Joshua Tsuji | 5084b66 | 2020-04-10 12:54:05 -0400 | [diff] [blame] | 151 | /** End action to run when the lead bubble's expansion animation completes. */ |
| 152 | @Nullable private Runnable mLeadBubbleEndAction; |
| 153 | |
| 154 | /** |
| 155 | * Animates expanding the bubbles into a row along the top of the screen, optionally running an |
| 156 | * end action when the entire animation completes, and an end action when the lead bubble's |
| 157 | * animation ends. |
| 158 | */ |
| 159 | public void expandFromStack( |
| 160 | @Nullable Runnable after, @Nullable Runnable leadBubbleEndAction) { |
Josh Tsuji | d12d46a | 2020-06-12 12:51:19 -0400 | [diff] [blame] | 161 | mPreparingToCollapse = false; |
Joshua Tsuji | 5084b66 | 2020-04-10 12:54:05 -0400 | [diff] [blame] | 162 | mAnimatingCollapse = false; |
| 163 | mAnimatingExpand = true; |
| 164 | mAfterExpand = after; |
| 165 | mLeadBubbleEndAction = leadBubbleEndAction; |
| 166 | |
| 167 | startOrUpdatePathAnimation(true /* expanding */); |
| 168 | } |
| 169 | |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 170 | /** |
| 171 | * 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] | 172 | */ |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 173 | public void expandFromStack(@Nullable Runnable after) { |
Joshua Tsuji | 5084b66 | 2020-04-10 12:54:05 -0400 | [diff] [blame] | 174 | expandFromStack(after, null /* leadBubbleEndAction */); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Josh Tsuji | d12d46a | 2020-06-12 12:51:19 -0400 | [diff] [blame] | 177 | /** |
| 178 | * Sets that we're animating the stack collapsed, but haven't yet called |
| 179 | * {@link #collapseBackToStack}. This will temporarily suspend animations for bubbles that are |
| 180 | * added or re-ordered, since the upcoming collapse animation will handle positioning those |
| 181 | * bubbles in the collapsed stack. |
| 182 | */ |
| 183 | public void notifyPreparingToCollapse() { |
| 184 | mPreparingToCollapse = true; |
| 185 | } |
| 186 | |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 187 | /** Animate collapsing the bubbles back to their stacked position. */ |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 188 | public void collapseBackToStack(PointF collapsePoint, Runnable after) { |
| 189 | mAnimatingExpand = false; |
Josh Tsuji | d12d46a | 2020-06-12 12:51:19 -0400 | [diff] [blame] | 190 | mPreparingToCollapse = false; |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 191 | mAnimatingCollapse = true; |
| 192 | mAfterCollapse = after; |
| 193 | mCollapsePoint = collapsePoint; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 194 | |
Joshua Tsuji | debd831 | 2019-06-06 17:17:08 -0400 | [diff] [blame] | 195 | startOrUpdatePathAnimation(false /* expanding */); |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Lyn Han | f473031 | 2019-06-18 11:18:58 -0700 | [diff] [blame] | 198 | /** |
| 199 | * Update effective screen width based on current orientation. |
| 200 | * @param orientation Landscape or portrait. |
Mady Mellor | e19353d | 2019-08-21 17:25:02 -0700 | [diff] [blame] | 201 | * @param displaySize Updated display size. |
Lyn Han | f473031 | 2019-06-18 11:18:58 -0700 | [diff] [blame] | 202 | */ |
Lyn Han | b4b0613 | 2020-05-11 09:25:20 -0700 | [diff] [blame] | 203 | public void updateResources(int orientation, Point displaySize) { |
Mady Mellor | e19353d | 2019-08-21 17:25:02 -0700 | [diff] [blame] | 204 | mScreenOrientation = orientation; |
| 205 | mDisplaySize = displaySize; |
Lyn Han | 55f53a5 | 2020-06-12 01:19:05 -0700 | [diff] [blame] | 206 | if (mLayout == null) { |
| 207 | return; |
Mady Mellor | 818eef0 | 2019-08-16 16:12:29 -0700 | [diff] [blame] | 208 | } |
Lyn Han | 55f53a5 | 2020-06-12 01:19:05 -0700 | [diff] [blame] | 209 | Resources res = mLayout.getContext().getResources(); |
| 210 | mBubblePaddingTop = res.getDimensionPixelSize(R.dimen.bubble_padding_top); |
| 211 | mStatusBarHeight = res.getDimensionPixelSize( |
| 212 | com.android.internal.R.dimen.status_bar_height); |
| 213 | mStackOffsetPx = res.getDimensionPixelSize(R.dimen.bubble_stack_offset); |
| 214 | mBubblePaddingTop = res.getDimensionPixelSize(R.dimen.bubble_padding_top); |
| 215 | mBubbleSizePx = res.getDimensionPixelSize(R.dimen.individual_bubble_size); |
| 216 | mBubblesMaxRendered = res.getInteger(R.integer.bubbles_max_rendered); |
| 217 | |
| 218 | // Includes overflow button. |
| 219 | float totalGapWidth = getWidthForDisplayingBubbles() - (mExpandedViewPadding * 2) |
| 220 | - (mBubblesMaxRendered + 1) * mBubbleSizePx; |
| 221 | mSpaceBetweenBubbles = totalGapWidth / mBubblesMaxRendered; |
Lyn Han | f473031 | 2019-06-18 11:18:58 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Joshua Tsuji | debd831 | 2019-06-06 17:17:08 -0400 | [diff] [blame] | 224 | /** |
| 225 | * Animates the bubbles along a curved path, either to expand them along the top or collapse |
| 226 | * them back into a stack. |
| 227 | */ |
| 228 | private void startOrUpdatePathAnimation(boolean expanding) { |
| 229 | Runnable after; |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 230 | |
Joshua Tsuji | debd831 | 2019-06-06 17:17:08 -0400 | [diff] [blame] | 231 | if (expanding) { |
| 232 | after = () -> { |
| 233 | mAnimatingExpand = false; |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 234 | |
Joshua Tsuji | debd831 | 2019-06-06 17:17:08 -0400 | [diff] [blame] | 235 | if (mAfterExpand != null) { |
| 236 | mAfterExpand.run(); |
| 237 | } |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 238 | |
Joshua Tsuji | debd831 | 2019-06-06 17:17:08 -0400 | [diff] [blame] | 239 | mAfterExpand = null; |
| 240 | }; |
| 241 | } else { |
| 242 | after = () -> { |
| 243 | mAnimatingCollapse = false; |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 244 | |
Joshua Tsuji | debd831 | 2019-06-06 17:17:08 -0400 | [diff] [blame] | 245 | if (mAfterCollapse != null) { |
| 246 | mAfterCollapse.run(); |
| 247 | } |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 248 | |
Joshua Tsuji | debd831 | 2019-06-06 17:17:08 -0400 | [diff] [blame] | 249 | mAfterCollapse = null; |
| 250 | }; |
| 251 | } |
| 252 | |
| 253 | // Animate each bubble individually, since each path will end in a different spot. |
| 254 | animationsForChildrenFromIndex(0, (index, animation) -> { |
| 255 | final View bubble = mLayout.getChildAt(index); |
| 256 | |
| 257 | // Start a path at the bubble's current position. |
| 258 | final Path path = new Path(); |
| 259 | path.moveTo(bubble.getTranslationX(), bubble.getTranslationY()); |
| 260 | |
| 261 | final float expandedY = getExpandedY(); |
| 262 | if (expanding) { |
| 263 | // If we're expanding, first draw a line from the bubble's current position to the |
| 264 | // top of the screen. |
| 265 | path.lineTo(bubble.getTranslationX(), expandedY); |
| 266 | |
| 267 | // Then, draw a line across the screen to the bubble's resting position. |
| 268 | path.lineTo(getBubbleLeft(index), expandedY); |
| 269 | } else { |
| 270 | final float sideMultiplier = |
| 271 | mLayout.isFirstChildXLeftOfCenter(mCollapsePoint.x) ? -1 : 1; |
| 272 | final float stackedX = mCollapsePoint.x + (sideMultiplier * index * mStackOffsetPx); |
| 273 | |
| 274 | // If we're collapsing, draw a line from the bubble's current position to the side |
| 275 | // of the screen where the bubble will be stacked. |
| 276 | path.lineTo(stackedX, expandedY); |
| 277 | |
| 278 | // Then, draw a line down to the stack position. |
| 279 | path.lineTo(stackedX, mCollapsePoint.y); |
| 280 | } |
| 281 | |
| 282 | // The lead bubble should be the bubble with the longest distance to travel when we're |
| 283 | // expanding, and the bubble with the shortest distance to travel when we're collapsing. |
| 284 | // During expansion from the left side, the last bubble has to travel to the far right |
| 285 | // side, so we have it lead and 'pull' the rest of the bubbles into place. From the |
| 286 | // right side, the first bubble is traveling to the top left, so it leads. During |
| 287 | // collapse to the left, the first bubble has the shortest travel time back to the stack |
| 288 | // position, so it leads (and vice versa). |
| 289 | final boolean firstBubbleLeads = |
| 290 | (expanding && !mLayout.isFirstChildXLeftOfCenter(bubble.getTranslationX())) |
| 291 | || (!expanding && mLayout.isFirstChildXLeftOfCenter(mCollapsePoint.x)); |
| 292 | final int startDelay = firstBubbleLeads |
| 293 | ? (index * 10) |
| 294 | : ((mLayout.getChildCount() - index) * 10); |
| 295 | |
Joshua Tsuji | 5084b66 | 2020-04-10 12:54:05 -0400 | [diff] [blame] | 296 | final boolean isLeadBubble = |
| 297 | (firstBubbleLeads && index == 0) |
| 298 | || (!firstBubbleLeads && index == mLayout.getChildCount() - 1); |
| 299 | |
Joshua Tsuji | debd831 | 2019-06-06 17:17:08 -0400 | [diff] [blame] | 300 | animation |
| 301 | .followAnimatedTargetAlongPath( |
| 302 | path, |
| 303 | EXPAND_COLLAPSE_TARGET_ANIM_DURATION /* targetAnimDuration */, |
Joshua Tsuji | 5084b66 | 2020-04-10 12:54:05 -0400 | [diff] [blame] | 304 | Interpolators.LINEAR /* targetAnimInterpolator */, |
| 305 | isLeadBubble ? mLeadBubbleEndAction : null /* endAction */, |
| 306 | () -> mLeadBubbleEndAction = null /* endAction */) |
Joshua Tsuji | debd831 | 2019-06-06 17:17:08 -0400 | [diff] [blame] | 307 | .withStartDelay(startDelay) |
| 308 | .withStiffness(EXPAND_COLLAPSE_ANIM_STIFFNESS); |
| 309 | }).startAll(after); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Joshua Tsuji | 2010354 | 2020-02-18 14:06:28 -0500 | [diff] [blame] | 312 | /** Notifies the controller that the dragged-out bubble was unstuck from the magnetic target. */ |
| 313 | public void onUnstuckFromTarget() { |
| 314 | mSpringToTouchOnNextMotionEvent = true; |
| 315 | } |
| 316 | |
Joshua Tsuji | 7dd88b0 | 2020-03-27 17:43:09 -0400 | [diff] [blame] | 317 | /** |
| 318 | * Prepares the given bubble view to be dragged out, using the provided magnetic target and |
| 319 | * listener. |
| 320 | */ |
| 321 | public void prepareForBubbleDrag( |
| 322 | View bubble, |
| 323 | MagnetizedObject.MagneticTarget target, |
| 324 | MagnetizedObject.MagnetListener listener) { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 325 | mLayout.cancelAnimationsOnView(bubble); |
| 326 | |
Joshua Tsuji | 2010354 | 2020-02-18 14:06:28 -0500 | [diff] [blame] | 327 | bubble.setTranslationZ(Short.MAX_VALUE); |
| 328 | mMagnetizedBubbleDraggingOut = new MagnetizedObject<View>( |
| 329 | mLayout.getContext(), bubble, |
| 330 | DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y) { |
| 331 | @Override |
| 332 | public float getWidth(@NonNull View underlyingObject) { |
| 333 | return mBubbleSizePx; |
| 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public float getHeight(@NonNull View underlyingObject) { |
| 338 | return mBubbleSizePx; |
| 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public void getLocationOnScreen(@NonNull View underlyingObject, @NonNull int[] loc) { |
| 343 | loc[0] = (int) bubble.getTranslationX(); |
| 344 | loc[1] = (int) bubble.getTranslationY(); |
| 345 | } |
| 346 | }; |
| 347 | mMagnetizedBubbleDraggingOut.addTarget(target); |
Joshua Tsuji | 7dd88b0 | 2020-03-27 17:43:09 -0400 | [diff] [blame] | 348 | mMagnetizedBubbleDraggingOut.setMagnetListener(listener); |
Joshua Tsuji | 2010354 | 2020-02-18 14:06:28 -0500 | [diff] [blame] | 349 | mMagnetizedBubbleDraggingOut.setHapticsEnabled(true); |
| 350 | mMagnetizedBubbleDraggingOut.setFlingToTargetMinVelocity(FLING_TO_DISMISS_MIN_VELOCITY); |
| 351 | } |
| 352 | |
| 353 | private void springBubbleTo(View bubble, float x, float y) { |
| 354 | animationForChild(bubble) |
| 355 | .translationX(x) |
| 356 | .translationY(y) |
| 357 | .withStiffness(SpringForce.STIFFNESS_HIGH) |
| 358 | .start(); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Drags an individual bubble to the given coordinates. Bubbles to the right will animate to |
| 363 | * take its place once it's dragged out of the row of bubbles, and animate out of the way if the |
| 364 | * bubble is dragged back into the row. |
| 365 | */ |
| 366 | public void dragBubbleOut(View bubbleView, float x, float y) { |
Joshua Tsuji | 2010354 | 2020-02-18 14:06:28 -0500 | [diff] [blame] | 367 | if (mSpringToTouchOnNextMotionEvent) { |
| 368 | springBubbleTo(mMagnetizedBubbleDraggingOut.getUnderlyingObject(), x, y); |
| 369 | mSpringToTouchOnNextMotionEvent = false; |
| 370 | mSpringingBubbleToTouch = true; |
| 371 | } else if (mSpringingBubbleToTouch) { |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 372 | if (mLayout.arePropertiesAnimatingOnView( |
| 373 | bubbleView, DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y)) { |
Joshua Tsuji | 2010354 | 2020-02-18 14:06:28 -0500 | [diff] [blame] | 374 | springBubbleTo(mMagnetizedBubbleDraggingOut.getUnderlyingObject(), x, y); |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 375 | } else { |
| 376 | mSpringingBubbleToTouch = false; |
| 377 | } |
| 378 | } |
| 379 | |
Joshua Tsuji | 2010354 | 2020-02-18 14:06:28 -0500 | [diff] [blame] | 380 | if (!mSpringingBubbleToTouch && !mMagnetizedBubbleDraggingOut.getObjectStuckToTarget()) { |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 381 | bubbleView.setTranslationX(x); |
| 382 | bubbleView.setTranslationY(y); |
| 383 | } |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 384 | |
| 385 | final boolean draggedOutEnough = |
| 386 | y > getExpandedY() + mBubbleSizePx || y < getExpandedY() - mBubbleSizePx; |
| 387 | if (draggedOutEnough != mBubbleDraggedOutEnough) { |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 388 | updateBubblePositions(); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 389 | mBubbleDraggedOutEnough = draggedOutEnough; |
| 390 | } |
| 391 | } |
| 392 | |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 393 | /** Plays a dismiss animation on the dragged out bubble. */ |
Joshua Tsuji | 79a58ee | 2020-03-27 17:55:37 -0400 | [diff] [blame] | 394 | public void dismissDraggedOutBubble(View bubble, float translationYBy, Runnable after) { |
Mady Mellor | 12c9095 | 2020-04-06 12:29:07 -0700 | [diff] [blame] | 395 | if (bubble == null) { |
| 396 | return; |
| 397 | } |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 398 | animationForChild(bubble) |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 399 | .withStiffness(SpringForce.STIFFNESS_HIGH) |
Joshua Tsuji | 4395bbd | 2020-05-19 17:53:33 -0400 | [diff] [blame] | 400 | .scaleX(0f) |
| 401 | .scaleY(0f) |
Joshua Tsuji | 79a58ee | 2020-03-27 17:55:37 -0400 | [diff] [blame] | 402 | .translationY(bubble.getTranslationY() + translationYBy) |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 403 | .alpha(0f, after) |
| 404 | .start(); |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 405 | |
| 406 | updateBubblePositions(); |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 407 | } |
| 408 | |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 409 | @Nullable public View getDraggedOutBubble() { |
Joshua Tsuji | 2010354 | 2020-02-18 14:06:28 -0500 | [diff] [blame] | 410 | return mMagnetizedBubbleDraggingOut == null |
| 411 | ? null |
| 412 | : mMagnetizedBubbleDraggingOut.getUnderlyingObject(); |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 413 | } |
| 414 | |
Joshua Tsuji | 2010354 | 2020-02-18 14:06:28 -0500 | [diff] [blame] | 415 | /** Returns the MagnetizedObject instance for the dragging-out bubble. */ |
| 416 | public MagnetizedObject<View> getMagnetizedBubbleDraggingOut() { |
| 417 | return mMagnetizedBubbleDraggingOut; |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 418 | } |
| 419 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 420 | /** |
| 421 | * Snaps a bubble back to its position within the bubble row, and animates the rest of the |
| 422 | * bubbles to accommodate it if it was previously dragged out past the threshold. |
| 423 | */ |
| 424 | public void snapBubbleBack(View bubbleView, float velX, float velY) { |
| 425 | final int index = mLayout.indexOfChild(bubbleView); |
| 426 | |
Joshua Tsuji | c110843 | 2019-02-22 16:10:12 -0500 | [diff] [blame] | 427 | animationForChildAtIndex(index) |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 428 | .position(getBubbleLeft(index), getExpandedY()) |
| 429 | .withPositionStartVelocities(velX, velY) |
| 430 | .start(() -> bubbleView.setTranslationZ(0f) /* after */); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 431 | |
Joshua Tsuji | 2010354 | 2020-02-18 14:06:28 -0500 | [diff] [blame] | 432 | mMagnetizedBubbleDraggingOut = null; |
| 433 | |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 434 | updateBubblePositions(); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 435 | } |
| 436 | |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 437 | /** Resets bubble drag out gesture flags. */ |
| 438 | public void onGestureFinished() { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 439 | mBubbleDraggedOutEnough = false; |
Lyn Han | f44562b | 2020-03-30 16:40:46 -0700 | [diff] [blame] | 440 | mMagnetizedBubbleDraggingOut = null; |
Joshua Tsuji | 61b38f5 | 2019-05-31 16:20:22 -0400 | [diff] [blame] | 441 | updateBubblePositions(); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /** |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 445 | * Animates the bubbles to {@link #getExpandedY()} position. Used in response to IME showing. |
| 446 | */ |
| 447 | public void updateYPosition(Runnable after) { |
| 448 | if (mLayout == null) return; |
Joshua Tsuji | c110843 | 2019-02-22 16:10:12 -0500 | [diff] [blame] | 449 | animationsForChildrenFromIndex( |
| 450 | 0, (i, anim) -> anim.translationY(getExpandedY())).startAll(after); |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 453 | /** The Y value of the row of expanded bubbles. */ |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 454 | public float getExpandedY() { |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 455 | if (mLayout == null || mLayout.getRootWindowInsets() == null) { |
| 456 | return 0; |
| 457 | } |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 458 | final WindowInsets insets = mLayout.getRootWindowInsets(); |
Lyn Han | 4a8efe3 | 2019-05-30 09:43:27 -0700 | [diff] [blame] | 459 | return mBubblePaddingTop + Math.max( |
Lyn Han | c47e171 | 2020-01-28 21:43:34 -0800 | [diff] [blame] | 460 | mStatusBarHeight, |
| 461 | insets.getDisplayCutout() != null |
| 462 | ? insets.getDisplayCutout().getSafeInsetTop() |
| 463 | : 0); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Joshua Tsuji | 395bcfe | 2019-07-02 19:23:23 -0400 | [diff] [blame] | 466 | /** Description of current animation controller state. */ |
| 467 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 468 | pw.println("ExpandedAnimationController state:"); |
| 469 | pw.print(" isActive: "); pw.println(isActiveController()); |
| 470 | pw.print(" animatingExpand: "); pw.println(mAnimatingExpand); |
| 471 | pw.print(" animatingCollapse: "); pw.println(mAnimatingCollapse); |
Joshua Tsuji | 395bcfe | 2019-07-02 19:23:23 -0400 | [diff] [blame] | 472 | pw.print(" springingBubble: "); pw.println(mSpringingBubbleToTouch); |
| 473 | } |
| 474 | |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 475 | @Override |
Joshua Tsuji | c36ee6f | 2019-05-28 17:00:16 -0400 | [diff] [blame] | 476 | void onActiveControllerForLayout(PhysicsAnimationLayout layout) { |
Lyn Han | 55f53a5 | 2020-06-12 01:19:05 -0700 | [diff] [blame] | 477 | updateResources(mScreenOrientation, mDisplaySize); |
Lyn Han | c47e171 | 2020-01-28 21:43:34 -0800 | [diff] [blame] | 478 | |
Joshua Tsuji | c36ee6f | 2019-05-28 17:00:16 -0400 | [diff] [blame] | 479 | // Ensure that all child views are at 1x scale, and visible, in case they were animating |
| 480 | // in. |
| 481 | mLayout.setVisibility(View.VISIBLE); |
| 482 | animationsForChildrenFromIndex(0 /* startIndex */, (index, animation) -> |
| 483 | animation.scaleX(1f).scaleY(1f).alpha(1f)).startAll(); |
| 484 | } |
| 485 | |
| 486 | @Override |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 487 | Set<DynamicAnimation.ViewProperty> getAnimatedProperties() { |
| 488 | return Sets.newHashSet( |
| 489 | DynamicAnimation.TRANSLATION_X, |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 490 | DynamicAnimation.TRANSLATION_Y, |
| 491 | DynamicAnimation.SCALE_X, |
| 492 | DynamicAnimation.SCALE_Y, |
| 493 | DynamicAnimation.ALPHA); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | @Override |
| 497 | int getNextAnimationInChain(DynamicAnimation.ViewProperty property, int index) { |
| 498 | return NONE; |
| 499 | } |
| 500 | |
| 501 | @Override |
| 502 | float getOffsetForChainedPropertyAnimation(DynamicAnimation.ViewProperty property) { |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | @Override |
| 507 | SpringForce getSpringForce(DynamicAnimation.ViewProperty property, View view) { |
| 508 | return new SpringForce() |
Joshua Tsuji | 010c2b1 | 2019-02-25 18:11:25 -0500 | [diff] [blame] | 509 | .setDampingRatio(SpringForce.DAMPING_RATIO_LOW_BOUNCY) |
| 510 | .setStiffness(SpringForce.STIFFNESS_LOW); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | @Override |
| 514 | void onChildAdded(View child, int index) { |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 515 | // If a bubble is added while the expand/collapse animations are playing, update the |
| 516 | // animation to include the new bubble. |
| 517 | if (mAnimatingExpand) { |
Joshua Tsuji | debd831 | 2019-06-06 17:17:08 -0400 | [diff] [blame] | 518 | startOrUpdatePathAnimation(true /* expanding */); |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 519 | } else if (mAnimatingCollapse) { |
Joshua Tsuji | debd831 | 2019-06-06 17:17:08 -0400 | [diff] [blame] | 520 | startOrUpdatePathAnimation(false /* expanding */); |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 521 | } else { |
Joshua Tsuji | 61b38f5 | 2019-05-31 16:20:22 -0400 | [diff] [blame] | 522 | child.setTranslationX(getBubbleLeft(index)); |
Josh Tsuji | d12d46a | 2020-06-12 12:51:19 -0400 | [diff] [blame] | 523 | |
| 524 | // If we're preparing to collapse, don't start animations since the collapse animation |
| 525 | // will take over and animate the new bubble into the correct (stacked) position. |
| 526 | if (!mPreparingToCollapse) { |
| 527 | animationForChild(child) |
| 528 | .translationY( |
| 529 | getExpandedY() |
| 530 | - mBubbleSizePx * ANIMATE_TRANSLATION_FACTOR, /* from */ |
| 531 | getExpandedY() /* to */) |
| 532 | .start(); |
| 533 | updateBubblePositions(); |
| 534 | } |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 535 | } |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | @Override |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 539 | void onChildRemoved(View child, int index, Runnable finishRemoval) { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 540 | // If we're removing the dragged-out bubble, that means it got dismissed. |
Joshua Tsuji | 2010354 | 2020-02-18 14:06:28 -0500 | [diff] [blame] | 541 | if (child.equals(getDraggedOutBubble())) { |
| 542 | mMagnetizedBubbleDraggingOut = null; |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 543 | finishRemoval.run(); |
Joshua Tsuji | 4395bbd | 2020-05-19 17:53:33 -0400 | [diff] [blame] | 544 | mOnBubbleAnimatedOutAction.run(); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 545 | } else { |
Joshua Tsuji | 4395bbd | 2020-05-19 17:53:33 -0400 | [diff] [blame] | 546 | PhysicsAnimator.getInstance(child) |
| 547 | .spring(DynamicAnimation.ALPHA, 0f) |
| 548 | .spring(DynamicAnimation.SCALE_X, 0f, mAnimateOutSpringConfig) |
| 549 | .spring(DynamicAnimation.SCALE_Y, 0f, mAnimateOutSpringConfig) |
| 550 | .withEndActions(finishRemoval, mOnBubbleAnimatedOutAction) |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 551 | .start(); |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 552 | } |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 553 | |
| 554 | // Animate all the other bubbles to their new positions sans this bubble. |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 555 | updateBubblePositions(); |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | @Override |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 559 | void onChildReordered(View child, int oldIndex, int newIndex) { |
Josh Tsuji | d12d46a | 2020-06-12 12:51:19 -0400 | [diff] [blame] | 560 | if (mPreparingToCollapse) { |
| 561 | // If a re-order is received while we're preparing to collapse, ignore it. Once started, |
| 562 | // the collapse animation will animate all of the bubbles to their correct (stacked) |
| 563 | // position. |
| 564 | return; |
| 565 | } |
Joshua Tsuji | 2862f2e | 2019-07-29 12:32:33 -0400 | [diff] [blame] | 566 | |
Joshua Tsuji | 2862f2e | 2019-07-29 12:32:33 -0400 | [diff] [blame] | 567 | if (mAnimatingCollapse) { |
Josh Tsuji | d12d46a | 2020-06-12 12:51:19 -0400 | [diff] [blame] | 568 | // If a re-order is received during collapse, update the animation so that the bubbles |
| 569 | // end up in the correct (stacked) position. |
Joshua Tsuji | 2862f2e | 2019-07-29 12:32:33 -0400 | [diff] [blame] | 570 | startOrUpdatePathAnimation(false /* expanding */); |
Josh Tsuji | d12d46a | 2020-06-12 12:51:19 -0400 | [diff] [blame] | 571 | } else { |
| 572 | // Otherwise, animate the bubbles around to reflect their new order. |
| 573 | updateBubblePositions(); |
Joshua Tsuji | 2862f2e | 2019-07-29 12:32:33 -0400 | [diff] [blame] | 574 | } |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 575 | } |
| 576 | |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 577 | private void updateBubblePositions() { |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 578 | if (mAnimatingExpand || mAnimatingCollapse) { |
| 579 | return; |
| 580 | } |
| 581 | |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 582 | for (int i = 0; i < mLayout.getChildCount(); i++) { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 583 | final View bubble = mLayout.getChildAt(i); |
| 584 | |
| 585 | // Don't animate the dragging out bubble, or it'll jump around while being dragged. It |
| 586 | // will be snapped to the correct X value after the drag (if it's not dismissed). |
Joshua Tsuji | 2010354 | 2020-02-18 14:06:28 -0500 | [diff] [blame] | 587 | if (bubble.equals(getDraggedOutBubble())) { |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 588 | return; |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 589 | } |
Joshua Tsuji | f49ee14 | 2019-05-29 16:32:01 -0400 | [diff] [blame] | 590 | |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 591 | animationForChild(bubble) |
| 592 | .translationX(getBubbleLeft(i)) |
| 593 | .start(); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 597 | /** |
| 598 | * @param index Bubble index in row. |
| 599 | * @return Bubble left x from left edge of screen. |
| 600 | */ |
| 601 | public float getBubbleLeft(int index) { |
Lyn Han | c47e171 | 2020-01-28 21:43:34 -0800 | [diff] [blame] | 602 | final float bubbleFromRowLeft = index * (mBubbleSizePx + mSpaceBetweenBubbles); |
Lyn Han | 4a8efe3 | 2019-05-30 09:43:27 -0700 | [diff] [blame] | 603 | return getRowLeft() + bubbleFromRowLeft; |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Mady Mellor | e19353d | 2019-08-21 17:25:02 -0700 | [diff] [blame] | 606 | /** |
| 607 | * When expanded, the bubbles are centered in the screen. In portrait, all available space is |
| 608 | * used. In landscape we have too much space so the value is restricted. This method accounts |
| 609 | * for window decorations (nav bar, cutouts). |
| 610 | * |
| 611 | * @return the desired width to display the expanded bubbles in. |
| 612 | */ |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 613 | public float getWidthForDisplayingBubbles() { |
Mady Mellor | e19353d | 2019-08-21 17:25:02 -0700 | [diff] [blame] | 614 | final float availableWidth = getAvailableScreenWidth(true /* includeStableInsets */); |
| 615 | if (mScreenOrientation == Configuration.ORIENTATION_LANDSCAPE) { |
| 616 | // display size y in landscape will be the smaller dimension of the screen |
| 617 | return Math.max(mDisplaySize.y, availableWidth * CENTER_BUBBLES_LANDSCAPE_PERCENT); |
| 618 | } else { |
| 619 | return availableWidth; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Determines the available screen width without the cutout. |
| 625 | * |
| 626 | * @param subtractStableInsets Whether or not stable insets should also be removed from the |
Lyn Han | c47e171 | 2020-01-28 21:43:34 -0800 | [diff] [blame] | 627 | * returned width. |
Mady Mellor | e19353d | 2019-08-21 17:25:02 -0700 | [diff] [blame] | 628 | * @return the total screen width available accounting for cutouts and insets, |
| 629 | * iff {@param includeStableInsets} is true. |
| 630 | */ |
| 631 | private float getAvailableScreenWidth(boolean subtractStableInsets) { |
| 632 | float availableSize = mDisplaySize.x; |
| 633 | WindowInsets insets = mLayout != null ? mLayout.getRootWindowInsets() : null; |
| 634 | if (insets != null) { |
| 635 | int cutoutLeft = 0; |
| 636 | int cutoutRight = 0; |
| 637 | DisplayCutout cutout = insets.getDisplayCutout(); |
| 638 | if (cutout != null) { |
| 639 | cutoutLeft = cutout.getSafeInsetLeft(); |
| 640 | cutoutRight = cutout.getSafeInsetRight(); |
| 641 | } |
| 642 | final int stableLeft = subtractStableInsets ? insets.getStableInsetLeft() : 0; |
| 643 | final int stableRight = subtractStableInsets ? insets.getStableInsetRight() : 0; |
| 644 | availableSize -= Math.max(stableLeft, cutoutLeft); |
| 645 | availableSize -= Math.max(stableRight, cutoutRight); |
| 646 | } |
| 647 | return availableSize; |
| 648 | } |
| 649 | |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 650 | private float getRowLeft() { |
| 651 | if (mLayout == null) { |
| 652 | return 0; |
| 653 | } |
Lyn Han | c47e171 | 2020-01-28 21:43:34 -0800 | [diff] [blame] | 654 | float rowWidth = (mLayout.getChildCount() * mBubbleSizePx) |
| 655 | + ((mLayout.getChildCount() - 1) * mSpaceBetweenBubbles); |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 656 | |
Mady Mellor | e19353d | 2019-08-21 17:25:02 -0700 | [diff] [blame] | 657 | // This display size we're using includes the size of the insets, we want the true |
| 658 | // center of the display minus the notch here, which means we should include the |
| 659 | // stable insets (e.g. status bar, nav bar) in this calculation. |
| 660 | final float trueCenter = getAvailableScreenWidth(false /* subtractStableInsets */) / 2f; |
Lyn Han | c47e171 | 2020-01-28 21:43:34 -0800 | [diff] [blame] | 661 | return trueCenter - (rowWidth / 2f); |
Lyn Han | 522e9ff | 2019-05-17 13:26:13 -0700 | [diff] [blame] | 662 | } |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 663 | } |