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