Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.pip.phone; |
| 18 | |
Wale Ogunwale | 6827856 | 2017-09-23 17:13:55 -0700 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 20 | |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 23 | import android.app.IActivityTaskManager; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 25 | import android.graphics.Rect; |
Winson Chung | bb23376 | 2017-05-15 14:20:46 -0700 | [diff] [blame] | 26 | import android.os.Debug; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 27 | import android.os.RemoteException; |
| 28 | import android.util.Log; |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 29 | |
| 30 | import androidx.dynamicanimation.animation.SpringForce; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 31 | |
Hongwei Wang | ebf1808 | 2019-09-26 14:25:11 -0700 | [diff] [blame] | 32 | import com.android.systemui.pip.PipSnapAlgorithm; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 33 | import com.android.systemui.pip.PipTaskOrganizer; |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 34 | import com.android.systemui.shared.system.WindowManagerWrapper; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 35 | import com.android.systemui.statusbar.FlingAnimationUtils; |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 36 | import com.android.systemui.util.FloatingContentCoordinator; |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 37 | import com.android.systemui.util.animation.FloatProperties; |
| 38 | import com.android.systemui.util.animation.PhysicsAnimator; |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 39 | import com.android.systemui.util.magnetictarget.MagnetizedObject; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 40 | |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 41 | import java.io.PrintWriter; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 42 | import java.util.function.Consumer; |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 43 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 44 | /** |
| 45 | * A helper to animate and manipulate the PiP. |
| 46 | */ |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 47 | public class PipMotionHelper implements PipAppOpsListener.Callback, |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 48 | FloatingContentCoordinator.FloatingContent { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 49 | |
| 50 | private static final String TAG = "PipMotionHelper"; |
Winson Chung | bb23376 | 2017-05-15 14:20:46 -0700 | [diff] [blame] | 51 | private static final boolean DEBUG = false; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 52 | |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 53 | private static final int SHRINK_STACK_FROM_MENU_DURATION = 250; |
| 54 | private static final int EXPAND_STACK_TO_MENU_DURATION = 250; |
| 55 | private static final int EXPAND_STACK_TO_FULLSCREEN_DURATION = 300; |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 56 | private static final int SHIFT_DURATION = 300; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 57 | |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 58 | /** Friction to use for PIP when it moves via physics fling animations. */ |
| 59 | private static final float DEFAULT_FRICTION = 2f; |
| 60 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 61 | private final Context mContext; |
| 62 | private final IActivityTaskManager mActivityTaskManager; |
| 63 | private final PipTaskOrganizer mPipTaskOrganizer; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 64 | |
Winson Chung | 79f852e | 2017-05-04 15:06:18 -0700 | [diff] [blame] | 65 | private PipMenuActivityController mMenuController; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 66 | private PipSnapAlgorithm mSnapAlgorithm; |
| 67 | private FlingAnimationUtils mFlingAnimationUtils; |
| 68 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 69 | private final Rect mStableInsets = new Rect(); |
| 70 | |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 71 | /** PIP's current bounds on the screen. */ |
| 72 | private final Rect mBounds = new Rect(); |
| 73 | |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 74 | /** The bounds within which PIP's top-left coordinate is allowed to move. */ |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 75 | private final Rect mMovementBounds = new Rect(); |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 76 | |
| 77 | /** The region that all of PIP must stay within. */ |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 78 | private final Rect mFloatingAllowedArea = new Rect(); |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 79 | |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 80 | /** |
Joshua Tsuji | 0584470 | 2020-02-04 18:24:35 -0800 | [diff] [blame] | 81 | * Bounds that are animated using the physics animator. |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 82 | */ |
| 83 | private final Rect mAnimatedBounds = new Rect(); |
| 84 | |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 85 | /** The destination bounds to which PIP is animating. */ |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 86 | private final Rect mAnimatingToBounds = new Rect(); |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 87 | |
| 88 | /** Coordinator instance for resolving conflicts with other floating content. */ |
| 89 | private FloatingContentCoordinator mFloatingContentCoordinator; |
| 90 | |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 91 | /** |
| 92 | * PhysicsAnimator instance for animating {@link #mAnimatedBounds} using physics animations. |
| 93 | */ |
| 94 | private PhysicsAnimator<Rect> mAnimatedBoundsPhysicsAnimator = PhysicsAnimator.getInstance( |
| 95 | mAnimatedBounds); |
| 96 | |
| 97 | /** |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 98 | * Update listener that resizes the PIP to {@link #mAnimatedBounds}. |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 99 | */ |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 100 | final PhysicsAnimator.UpdateListener<Rect> mResizePipUpdateListener = |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 101 | (target, values) -> resizePipUnchecked(mAnimatedBounds); |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 102 | |
| 103 | /** FlingConfig instances provided to PhysicsAnimator for fling gestures. */ |
| 104 | private PhysicsAnimator.FlingConfig mFlingConfigX; |
| 105 | private PhysicsAnimator.FlingConfig mFlingConfigY; |
| 106 | |
| 107 | /** SpringConfig to use for fling-then-spring animations. */ |
| 108 | private final PhysicsAnimator.SpringConfig mSpringConfig = |
| 109 | new PhysicsAnimator.SpringConfig( |
| 110 | SpringForce.STIFFNESS_MEDIUM, SpringForce.DAMPING_RATIO_LOW_BOUNCY); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 111 | |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 112 | /** SpringConfig to use for springing PIP away from conflicting floating content. */ |
| 113 | private final PhysicsAnimator.SpringConfig mConflictResolutionSpringConfig = |
| 114 | new PhysicsAnimator.SpringConfig( |
| 115 | SpringForce.STIFFNESS_LOW, SpringForce.DAMPING_RATIO_LOW_BOUNCY); |
| 116 | |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 117 | private final Consumer<Rect> mUpdateBoundsCallback = mBounds::set; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 118 | |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 119 | /** |
| 120 | * Whether we're springing to the touch event location (vs. moving it to that position |
| 121 | * instantly). We spring-to-touch after PIP is dragged out of the magnetic target, since it was |
| 122 | * 'stuck' in the target and needs to catch up to the touch location. |
| 123 | */ |
| 124 | private boolean mSpringingToTouch = false; |
| 125 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 126 | public PipMotionHelper(Context context, IActivityTaskManager activityTaskManager, |
| 127 | PipTaskOrganizer pipTaskOrganizer, PipMenuActivityController menuController, |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 128 | PipSnapAlgorithm snapAlgorithm, FlingAnimationUtils flingAnimationUtils, |
| 129 | FloatingContentCoordinator floatingContentCoordinator) { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 130 | mContext = context; |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 131 | mActivityTaskManager = activityTaskManager; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 132 | mPipTaskOrganizer = pipTaskOrganizer; |
Winson Chung | 79f852e | 2017-05-04 15:06:18 -0700 | [diff] [blame] | 133 | mMenuController = menuController; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 134 | mSnapAlgorithm = snapAlgorithm; |
| 135 | mFlingAnimationUtils = flingAnimationUtils; |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 136 | mFloatingContentCoordinator = floatingContentCoordinator; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 137 | onConfigurationChanged(); |
| 138 | } |
| 139 | |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 140 | @NonNull |
| 141 | @Override |
| 142 | public Rect getFloatingBoundsOnScreen() { |
| 143 | return !mAnimatingToBounds.isEmpty() ? mAnimatingToBounds : mBounds; |
| 144 | } |
| 145 | |
| 146 | @NonNull |
| 147 | @Override |
| 148 | public Rect getAllowedFloatingBoundsRegion() { |
| 149 | return mFloatingAllowedArea; |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void moveToBounds(@NonNull Rect bounds) { |
| 154 | animateToBounds(bounds, mConflictResolutionSpringConfig); |
| 155 | } |
| 156 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 157 | /** |
| 158 | * Updates whenever the configuration changes. |
| 159 | */ |
| 160 | void onConfigurationChanged() { |
| 161 | mSnapAlgorithm.onConfigurationChanged(); |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 162 | WindowManagerWrapper.getInstance().getStableInsets(mStableInsets); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
Joshua Tsuji | 601dc85 | 2020-04-02 15:54:15 -0400 | [diff] [blame] | 166 | * Synchronizes the current bounds with the pinned stack, cancelling any ongoing animations. |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 167 | */ |
| 168 | void synchronizePinnedStackBounds() { |
| 169 | cancelAnimations(); |
Ben Lin | 3cd7bc3 | 2020-04-07 17:15:21 -0700 | [diff] [blame] | 170 | mBounds.set(mPipTaskOrganizer.getLastReportedBounds()); |
Joshua Tsuji | faf890f | 2020-04-16 13:38:32 -0400 | [diff] [blame] | 171 | mFloatingContentCoordinator.onContentMoved(this); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
Joshua Tsuji | 601dc85 | 2020-04-02 15:54:15 -0400 | [diff] [blame] | 175 | * Synchronizes the current bounds with either the pinned stack, or the ongoing animation. This |
| 176 | * is done to prepare for a touch gesture. |
| 177 | */ |
| 178 | void synchronizePinnedStackBoundsForTouchGesture() { |
| 179 | if (mAnimatingToBounds.isEmpty()) { |
| 180 | // If we're not animating anywhere, sync normally. |
| 181 | synchronizePinnedStackBounds(); |
| 182 | } else { |
| 183 | // If we're animating, set the current bounds to the animated bounds. That way, the |
| 184 | // touch gesture will begin at the most recent animated location of the bounds. |
| 185 | mBounds.set(mAnimatedBounds); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 190 | * Tries to move the pinned stack to the given {@param bounds}. |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 191 | */ |
| 192 | void movePip(Rect toBounds) { |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 193 | movePip(toBounds, false /* isDragging */); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Tries to move the pinned stack to the given {@param bounds}. |
| 198 | * |
| 199 | * @param isDragging Whether this movement is the result of a drag touch gesture. If so, we |
| 200 | * won't notify the floating content coordinator of this move, since that will |
| 201 | * happen when the gesture ends. |
| 202 | */ |
| 203 | void movePip(Rect toBounds, boolean isDragging) { |
| 204 | if (!isDragging) { |
| 205 | mFloatingContentCoordinator.onContentMoved(this); |
| 206 | } |
| 207 | |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 208 | if (!mSpringingToTouch) { |
| 209 | // If we are moving PIP directly to the touch event locations, cancel any animations and |
| 210 | // move PIP to the given bounds. |
| 211 | cancelAnimations(); |
| 212 | resizePipUnchecked(toBounds); |
| 213 | mBounds.set(toBounds); |
| 214 | } else { |
| 215 | // If PIP is 'catching up' after being stuck in the dismiss target, update the animation |
| 216 | // to spring towards the new touch location. |
| 217 | mAnimatedBoundsPhysicsAnimator |
| 218 | .spring(FloatProperties.RECT_X, toBounds.left, mSpringConfig) |
| 219 | .spring(FloatProperties.RECT_Y, toBounds.top, mSpringConfig) |
| 220 | .withEndActions(() -> mSpringingToTouch = false); |
| 221 | |
| 222 | startBoundsAnimator(toBounds.left /* toX */, toBounds.top /* toY */); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** Set whether we're springing-to-touch to catch up after being stuck in the dismiss target. */ |
| 227 | void setSpringingToTouch(boolean springingToTouch) { |
| 228 | if (springingToTouch) { |
| 229 | mAnimatedBounds.set(mBounds); |
| 230 | } |
| 231 | |
| 232 | mSpringingToTouch = springingToTouch; |
| 233 | } |
| 234 | |
| 235 | void prepareForAnimation() { |
| 236 | mAnimatedBounds.set(mBounds); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Resizes the pinned stack back to fullscreen. |
| 241 | */ |
| 242 | void expandPip() { |
Winson Chung | e6385a2 | 2017-05-02 18:15:16 -0700 | [diff] [blame] | 243 | expandPip(false /* skipAnimation */); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Resizes the pinned stack back to fullscreen. |
| 248 | */ |
| 249 | void expandPip(boolean skipAnimation) { |
Winson Chung | bb23376 | 2017-05-15 14:20:46 -0700 | [diff] [blame] | 250 | if (DEBUG) { |
| 251 | Log.d(TAG, "expandPip: skipAnimation=" + skipAnimation |
| 252 | + " callers=\n" + Debug.getCallers(5, " ")); |
| 253 | } |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 254 | cancelAnimations(); |
Winson Chung | 79f852e | 2017-05-04 15:06:18 -0700 | [diff] [blame] | 255 | mMenuController.hideMenuWithoutResize(); |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 256 | mPipTaskOrganizer.getUpdateHandler().post(() -> { |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 257 | mPipTaskOrganizer.dismissPip(skipAnimation ? 0 : EXPAND_STACK_TO_FULLSCREEN_DURATION); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 258 | }); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Dismisses the pinned stack. |
| 263 | */ |
Eliot Courtney | 293d86e | 2019-01-08 16:42:39 +0900 | [diff] [blame] | 264 | @Override |
| 265 | public void dismissPip() { |
Winson Chung | bb23376 | 2017-05-15 14:20:46 -0700 | [diff] [blame] | 266 | if (DEBUG) { |
| 267 | Log.d(TAG, "dismissPip: callers=\n" + Debug.getCallers(5, " ")); |
| 268 | } |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 269 | cancelAnimations(); |
Winson Chung | 79f852e | 2017-05-04 15:06:18 -0700 | [diff] [blame] | 270 | mMenuController.hideMenuWithoutResize(); |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 271 | mPipTaskOrganizer.getUpdateHandler().post(() -> { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 272 | try { |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 273 | mActivityTaskManager.removeStacksInWindowingModes( |
| 274 | new int[]{ WINDOWING_MODE_PINNED }); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 275 | } catch (RemoteException e) { |
| 276 | Log.e(TAG, "Failed to remove PiP", e); |
| 277 | } |
| 278 | }); |
| 279 | } |
| 280 | |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 281 | /** Sets the movement bounds to use to constrain PIP position animations. */ |
| 282 | void setCurrentMovementBounds(Rect movementBounds) { |
| 283 | mMovementBounds.set(movementBounds); |
| 284 | rebuildFlingConfigs(); |
| 285 | |
| 286 | // The movement bounds represent the area within which we can move PIP's top-left position. |
| 287 | // The allowed area for all of PIP is those bounds plus PIP's width and height. |
| 288 | mFloatingAllowedArea.set(mMovementBounds); |
| 289 | mFloatingAllowedArea.right += mBounds.width(); |
| 290 | mFloatingAllowedArea.bottom += mBounds.height(); |
| 291 | } |
| 292 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 293 | /** |
| 294 | * @return the PiP bounds. |
| 295 | */ |
| 296 | Rect getBounds() { |
| 297 | return mBounds; |
| 298 | } |
| 299 | |
| 300 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 301 | * Flings the PiP to the closest snap target. |
| 302 | */ |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 303 | void flingToSnapTarget( |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 304 | float velocityX, float velocityY, |
| 305 | @Nullable Runnable updateAction, @Nullable Runnable endAction) { |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 306 | mAnimatedBounds.set(mBounds); |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 307 | mAnimatedBoundsPhysicsAnimator |
| 308 | .flingThenSpring( |
| 309 | FloatProperties.RECT_X, velocityX, mFlingConfigX, mSpringConfig, |
| 310 | true /* flingMustReachMinOrMax */) |
| 311 | .flingThenSpring( |
| 312 | FloatProperties.RECT_Y, velocityY, mFlingConfigY, mSpringConfig) |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 313 | .withEndActions(endAction); |
| 314 | |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 315 | if (updateAction != null) { |
| 316 | mAnimatedBoundsPhysicsAnimator.addUpdateListener( |
| 317 | (target, values) -> updateAction.run()); |
| 318 | } |
| 319 | |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 320 | final float xEndValue = velocityX < 0 ? mMovementBounds.left : mMovementBounds.right; |
| 321 | final float estimatedFlingYEndValue = |
| 322 | PhysicsAnimator.estimateFlingEndValue(mBounds.top, velocityY, mFlingConfigY); |
| 323 | |
Joshua Tsuji | 601dc85 | 2020-04-02 15:54:15 -0400 | [diff] [blame] | 324 | startBoundsAnimator(xEndValue /* toX */, estimatedFlingYEndValue /* toY */); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Animates the PiP to the closest snap target. |
| 329 | */ |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 330 | void animateToClosestSnapTarget() { |
Hongwei Wang | db779c3 | 2020-03-02 17:20:57 -0800 | [diff] [blame] | 331 | final Rect newBounds = new Rect(); |
| 332 | mSnapAlgorithm.snapRectToClosestEdge(mBounds, mMovementBounds, newBounds); |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 333 | animateToBounds(newBounds, mSpringConfig); |
| 334 | } |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 335 | |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 336 | /** |
| 337 | * Animates PIP to the provided bounds, using physics animations and the given spring |
| 338 | * configuration |
| 339 | */ |
| 340 | void animateToBounds(Rect bounds, PhysicsAnimator.SpringConfig springConfig) { |
| 341 | mAnimatedBounds.set(mBounds); |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 342 | mAnimatedBoundsPhysicsAnimator |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 343 | .spring(FloatProperties.RECT_X, bounds.left, springConfig) |
| 344 | .spring(FloatProperties.RECT_Y, bounds.top, springConfig); |
Joshua Tsuji | 601dc85 | 2020-04-02 15:54:15 -0400 | [diff] [blame] | 345 | startBoundsAnimator(bounds.left /* toX */, bounds.top /* toY */); |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Animates the dismissal of the PiP off the edge of the screen. |
| 350 | */ |
| 351 | void animateDismiss(float velocityX, float velocityY, @Nullable Runnable updateAction) { |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 352 | mAnimatedBounds.set(mBounds); |
| 353 | |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 354 | // Animate off the bottom of the screen, then dismiss PIP. |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 355 | mAnimatedBoundsPhysicsAnimator |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 356 | .spring(FloatProperties.RECT_Y, |
| 357 | mBounds.bottom + mBounds.height(), |
| 358 | velocityY, |
| 359 | mSpringConfig) |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 360 | .withEndActions(this::dismissPip); |
| 361 | |
| 362 | // If we were provided with an update action, run it whenever there's an update. |
| 363 | if (updateAction != null) { |
| 364 | mAnimatedBoundsPhysicsAnimator.addUpdateListener( |
| 365 | (target, values) -> updateAction.run()); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 366 | } |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 367 | |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 368 | startBoundsAnimator(mBounds.left /* toX */, mBounds.bottom + mBounds.height() /* toY */); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Animates the PiP to the expanded state to show the menu. |
| 373 | */ |
| 374 | float animateToExpandedState(Rect expandedBounds, Rect movementBounds, |
| 375 | Rect expandedMovementBounds) { |
| 376 | float savedSnapFraction = mSnapAlgorithm.getSnapFraction(new Rect(mBounds), movementBounds); |
| 377 | mSnapAlgorithm.applySnapFraction(expandedBounds, expandedMovementBounds, savedSnapFraction); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 378 | resizeAndAnimatePipUnchecked(expandedBounds, EXPAND_STACK_TO_MENU_DURATION); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 379 | return savedSnapFraction; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Animates the PiP from the expanded state to the normal state after the menu is hidden. |
| 384 | */ |
| 385 | void animateToUnexpandedState(Rect normalBounds, float savedSnapFraction, |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 386 | Rect normalMovementBounds, Rect currentMovementBounds, boolean immediate) { |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 387 | if (savedSnapFraction < 0f) { |
| 388 | // If there are no saved snap fractions, then just use the current bounds |
| 389 | savedSnapFraction = mSnapAlgorithm.getSnapFraction(new Rect(mBounds), |
| 390 | currentMovementBounds); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 391 | } |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 392 | mSnapAlgorithm.applySnapFraction(normalBounds, normalMovementBounds, savedSnapFraction); |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 393 | |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 394 | if (immediate) { |
| 395 | movePip(normalBounds); |
| 396 | } else { |
| 397 | resizeAndAnimatePipUnchecked(normalBounds, SHRINK_STACK_FROM_MENU_DURATION); |
| 398 | } |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /** |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 402 | * Animates the PiP to offset it from the IME or shelf. |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 403 | */ |
wilsonshih | 5c4cf52 | 2019-01-25 09:03:47 +0800 | [diff] [blame] | 404 | void animateToOffset(Rect originalBounds, int offset) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 405 | if (DEBUG) { |
| 406 | Log.d(TAG, "animateToOffset: originalBounds=" + originalBounds + " offset=" + offset |
| 407 | + " callers=\n" + Debug.getCallers(5, " ")); |
| 408 | } |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 409 | cancelAnimations(); |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 410 | mPipTaskOrganizer.scheduleOffsetPip(originalBounds, offset, SHIFT_DURATION, |
| 411 | mUpdateBoundsCallback); |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 415 | * Cancels all existing animations. |
| 416 | */ |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 417 | private void cancelAnimations() { |
| 418 | mAnimatedBoundsPhysicsAnimator.cancel(); |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 419 | mAnimatingToBounds.setEmpty(); |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 420 | mSpringingToTouch = false; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 423 | /** Set new fling configs whose min/max values respect the given movement bounds. */ |
| 424 | private void rebuildFlingConfigs() { |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 425 | mFlingConfigX = new PhysicsAnimator.FlingConfig( |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 426 | DEFAULT_FRICTION, mMovementBounds.left, mMovementBounds.right); |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 427 | mFlingConfigY = new PhysicsAnimator.FlingConfig( |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 428 | DEFAULT_FRICTION, mMovementBounds.top, mMovementBounds.bottom); |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Starts the physics animator which will update the animated PIP bounds using physics |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 433 | * animations, as well as the TimeAnimator which will apply those bounds to PIP. |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 434 | * |
| 435 | * This will also add end actions to the bounds animator that cancel the TimeAnimator and update |
| 436 | * the 'real' bounds to equal the final animated bounds. |
| 437 | */ |
Joshua Tsuji | 601dc85 | 2020-04-02 15:54:15 -0400 | [diff] [blame] | 438 | private void startBoundsAnimator(float toX, float toY) { |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 439 | if (!mSpringingToTouch) { |
| 440 | cancelAnimations(); |
| 441 | } |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 442 | |
Joshua Tsuji | 601dc85 | 2020-04-02 15:54:15 -0400 | [diff] [blame] | 443 | // Set animatingToBounds directly to avoid allocating a new Rect, but then call |
| 444 | // setAnimatingToBounds to run the normal logic for changing animatingToBounds. |
| 445 | mAnimatingToBounds.set( |
| 446 | (int) toX, |
| 447 | (int) toY, |
| 448 | (int) toX + mBounds.width(), |
| 449 | (int) toY + mBounds.height()); |
| 450 | setAnimatingToBounds(mAnimatingToBounds); |
| 451 | |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 452 | mAnimatedBoundsPhysicsAnimator |
Joshua Tsuji | 601dc85 | 2020-04-02 15:54:15 -0400 | [diff] [blame] | 453 | .withEndActions(() -> { |
| 454 | mPipTaskOrganizer.scheduleFinishResizePip(mAnimatedBounds); |
| 455 | mAnimatingToBounds.setEmpty(); |
| 456 | }) |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 457 | .addUpdateListener(mResizePipUpdateListener) |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 458 | .start(); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /** |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 462 | * Notifies the floating coordinator that we're moving, and sets {@link #mAnimatingToBounds} so |
| 463 | * we return these bounds from |
| 464 | * {@link FloatingContentCoordinator.FloatingContent#getFloatingBoundsOnScreen()}. |
| 465 | */ |
| 466 | private void setAnimatingToBounds(Rect bounds) { |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 467 | mAnimatingToBounds.set(bounds); |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 468 | mFloatingContentCoordinator.onContentMoved(this); |
| 469 | } |
| 470 | |
| 471 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 472 | * Directly resizes the PiP to the given {@param bounds}. |
| 473 | */ |
| 474 | private void resizePipUnchecked(Rect toBounds) { |
Winson Chung | bb23376 | 2017-05-15 14:20:46 -0700 | [diff] [blame] | 475 | if (DEBUG) { |
| 476 | Log.d(TAG, "resizePipUnchecked: toBounds=" + toBounds |
| 477 | + " callers=\n" + Debug.getCallers(5, " ")); |
| 478 | } |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 479 | if (!toBounds.equals(mBounds)) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 480 | mPipTaskOrganizer.scheduleResizePip(toBounds, mUpdateBoundsCallback); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Directly resizes the PiP to the given {@param bounds}. |
| 486 | */ |
| 487 | private void resizeAndAnimatePipUnchecked(Rect toBounds, int duration) { |
Winson Chung | bb23376 | 2017-05-15 14:20:46 -0700 | [diff] [blame] | 488 | if (DEBUG) { |
| 489 | Log.d(TAG, "resizeAndAnimatePipUnchecked: toBounds=" + toBounds |
| 490 | + " duration=" + duration + " callers=\n" + Debug.getCallers(5, " ")); |
| 491 | } |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 492 | if (!toBounds.equals(mBounds)) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 493 | mPipTaskOrganizer.scheduleAnimateResizePip(toBounds, duration, mUpdateBoundsCallback); |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 494 | setAnimatingToBounds(toBounds); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
| 498 | /** |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 499 | * Returns a MagnetizedObject wrapper for PIP's animated bounds. This is provided to the |
| 500 | * magnetic dismiss target so it can calculate PIP's size and position. |
Mady Mellor | 60421c9 | 2017-03-29 15:27:37 -0700 | [diff] [blame] | 501 | */ |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 502 | MagnetizedObject<Rect> getMagnetizedPip() { |
| 503 | return new MagnetizedObject<Rect>( |
| 504 | mContext, mAnimatedBounds, FloatProperties.RECT_X, FloatProperties.RECT_Y) { |
| 505 | @Override |
| 506 | public float getWidth(@NonNull Rect animatedPipBounds) { |
| 507 | return animatedPipBounds.width(); |
| 508 | } |
Mady Mellor | 60421c9 | 2017-03-29 15:27:37 -0700 | [diff] [blame] | 509 | |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 510 | @Override |
| 511 | public float getHeight(@NonNull Rect animatedPipBounds) { |
| 512 | return animatedPipBounds.height(); |
| 513 | } |
Mady Mellor | 47ba140 | 2017-04-04 17:25:43 -0700 | [diff] [blame] | 514 | |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 515 | @Override |
| 516 | public void getLocationOnScreen( |
| 517 | @NonNull Rect animatedPipBounds, @NonNull int[] loc) { |
| 518 | loc[0] = animatedPipBounds.left; |
| 519 | loc[1] = animatedPipBounds.top; |
| 520 | } |
| 521 | }; |
Mady Mellor | 47ba140 | 2017-04-04 17:25:43 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 524 | public void dump(PrintWriter pw, String prefix) { |
| 525 | final String innerPrefix = prefix + " "; |
| 526 | pw.println(prefix + TAG); |
| 527 | pw.println(innerPrefix + "mBounds=" + mBounds); |
| 528 | pw.println(innerPrefix + "mStableInsets=" + mStableInsets); |
| 529 | } |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 530 | } |