Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [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 | |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 19 | import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_CLOSE; |
| 20 | import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_FULL; |
Chenjie Yu | ae9fdf04 | 2018-02-15 10:19:32 -0800 | [diff] [blame] | 21 | import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_NONE; |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 22 | |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 23 | import android.annotation.SuppressLint; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 24 | import android.app.IActivityManager; |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 25 | import android.app.IActivityTaskManager; |
Winson Chung | 9b91941 | 2017-06-19 17:01:51 -0700 | [diff] [blame] | 26 | import android.content.ComponentName; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 27 | import android.content.Context; |
Winson Chung | 136d1ec8 | 2017-07-18 11:16:30 -0700 | [diff] [blame] | 28 | import android.content.res.Resources; |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 29 | import android.graphics.PixelFormat; |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 30 | import android.graphics.Point; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 31 | import android.graphics.PointF; |
| 32 | import android.graphics.Rect; |
Mady Mellor | d4e40fb | 2017-01-26 10:43:16 -0800 | [diff] [blame] | 33 | import android.os.Handler; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 34 | import android.os.RemoteException; |
| 35 | import android.util.Log; |
Winson Chung | be00a83 | 2020-03-10 11:08:35 -0700 | [diff] [blame] | 36 | import android.util.Pair; |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 37 | import android.util.Size; |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 38 | import android.view.Gravity; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 39 | import android.view.IPinnedStackController; |
Tracy Zhou | 535c212 | 2019-02-21 17:02:47 -0800 | [diff] [blame] | 40 | import android.view.InputEvent; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 41 | import android.view.MotionEvent; |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 42 | import android.view.View; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 43 | import android.view.ViewConfiguration; |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 44 | import android.view.ViewGroup; |
| 45 | import android.view.WindowManager; |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 46 | import android.view.accessibility.AccessibilityEvent; |
| 47 | import android.view.accessibility.AccessibilityManager; |
| 48 | import android.view.accessibility.AccessibilityNodeInfo; |
Phil Weaver | 0a8caa1 | 2017-08-09 11:28:41 -0700 | [diff] [blame] | 49 | import android.view.accessibility.AccessibilityWindowInfo; |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 50 | import android.widget.FrameLayout; |
| 51 | |
| 52 | import androidx.annotation.NonNull; |
| 53 | import androidx.dynamicanimation.animation.DynamicAnimation; |
| 54 | import androidx.dynamicanimation.animation.SpringForce; |
Chenjie Yu | ae9fdf04 | 2018-02-15 10:19:32 -0800 | [diff] [blame] | 55 | |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 56 | import com.android.internal.annotations.VisibleForTesting; |
Chenjie Yu | 52cacc6 | 2017-12-08 18:11:45 -0800 | [diff] [blame] | 57 | import com.android.internal.os.logging.MetricsLoggerWrapper; |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 58 | import com.android.systemui.R; |
Hongwei Wang | 43a752b | 2019-09-17 20:20:30 +0000 | [diff] [blame] | 59 | import com.android.systemui.pip.PipBoundsHandler; |
Hongwei Wang | ebf1808 | 2019-09-26 14:25:11 -0700 | [diff] [blame] | 60 | import com.android.systemui.pip.PipSnapAlgorithm; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 61 | import com.android.systemui.pip.PipTaskOrganizer; |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 62 | import com.android.systemui.shared.system.InputConsumerController; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 63 | import com.android.systemui.statusbar.FlingAnimationUtils; |
Beverly | 660d0a7 | 2020-02-26 12:32:42 -0500 | [diff] [blame] | 64 | import com.android.systemui.util.DeviceConfigProxy; |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 65 | import com.android.systemui.util.DismissCircleView; |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 66 | import com.android.systemui.util.FloatingContentCoordinator; |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 67 | import com.android.systemui.util.animation.PhysicsAnimator; |
| 68 | import com.android.systemui.util.magnetictarget.MagnetizedObject; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 69 | |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 70 | import java.io.PrintWriter; |
| 71 | |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Manages all the touch handling for PIP on the Phone, including moving, dismissing and expanding |
| 74 | * the PIP. |
| 75 | */ |
Mady Mellor | 8c7dc42 | 2017-05-10 12:55:06 -0700 | [diff] [blame] | 76 | public class PipTouchHandler { |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 77 | private static final String TAG = "PipTouchHandler"; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 78 | |
Mady Mellor | 5d58d25 | 2017-04-18 12:48:04 -0700 | [diff] [blame] | 79 | private static final int SHOW_DISMISS_AFFORDANCE_DELAY = 225; |
Tracy Zhou | e19d051 | 2018-07-31 16:29:34 -0700 | [diff] [blame] | 80 | private static final int BOTTOM_OFFSET_BUFFER_DP = 1; |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 81 | |
Winson Chung | 6e35ee1f | 2017-02-14 12:06:44 -0800 | [diff] [blame] | 82 | // Allow dragging the PIP to a location to close it |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 83 | private final boolean mEnableDismissDragToEdge; |
Ben Lin | e55f85f | 2020-01-27 14:49:41 -0800 | [diff] [blame] | 84 | // Allow PIP to resize to a slightly bigger state upon touch |
| 85 | private final boolean mEnableResize; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 86 | private final Context mContext; |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 87 | private final WindowManager mWindowManager; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 88 | private final IActivityManager mActivityManager; |
Hongwei Wang | 43a752b | 2019-09-17 20:20:30 +0000 | [diff] [blame] | 89 | private final PipBoundsHandler mPipBoundsHandler; |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 90 | private PipResizeGestureHandler mPipResizeGestureHandler; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 91 | private IPinnedStackController mPinnedStackController; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 92 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 93 | private final PipMenuActivityController mMenuController; |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 94 | private final PipSnapAlgorithm mSnapAlgorithm; |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 95 | private final AccessibilityManager mAccessibilityManager; |
Wale Ogunwale | 6455e50 | 2017-04-17 14:16:43 -0700 | [diff] [blame] | 96 | private boolean mShowPipMenuOnAnimationEnd = false; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 97 | |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 98 | /** |
| 99 | * MagnetizedObject wrapper for PIP. This allows the magnetic target library to locate and move |
| 100 | * PIP. |
| 101 | */ |
| 102 | private MagnetizedObject<Rect> mMagnetizedPip; |
| 103 | |
| 104 | /** |
| 105 | * Container for the dismiss circle, so that it can be animated within the container via |
| 106 | * translation rather than within the WindowManager via slow layout animations. |
| 107 | */ |
| 108 | private ViewGroup mTargetViewContainer; |
| 109 | |
| 110 | /** Circle view used to render the dismiss target. */ |
| 111 | private DismissCircleView mTargetView; |
| 112 | |
| 113 | /** |
| 114 | * MagneticTarget instance wrapping the target view and allowing us to set its magnetic radius. |
| 115 | */ |
| 116 | private MagnetizedObject.MagneticTarget mMagneticTarget; |
| 117 | |
| 118 | /** PhysicsAnimator instance for animating the dismiss target in/out. */ |
| 119 | private PhysicsAnimator<View> mMagneticTargetAnimator; |
| 120 | |
| 121 | /** Default configuration to use for springing the dismiss target in/out. */ |
| 122 | private final PhysicsAnimator.SpringConfig mTargetSpringConfig = |
| 123 | new PhysicsAnimator.SpringConfig( |
| 124 | SpringForce.STIFFNESS_MEDIUM, SpringForce.DAMPING_RATIO_NO_BOUNCY); |
| 125 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 126 | // The current movement bounds |
| 127 | private Rect mMovementBounds = new Rect(); |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 128 | // The current resized bounds, changed by user resize. |
| 129 | // This is used during expand/un-expand to save/restore the user's resized size. |
| 130 | @VisibleForTesting Rect mResizedBounds = new Rect(); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 131 | |
Winson Chung | 0a2f34f | 2017-08-11 18:13:24 -0700 | [diff] [blame] | 132 | // The reference inset bounds, used to determine the dismiss fraction |
| 133 | private Rect mInsetBounds = new Rect(); |
Ben Lin | 06dc41f | 2020-03-05 00:39:27 +0000 | [diff] [blame] | 134 | // The reference bounds used to calculate the normal/expanded target bounds |
| 135 | private Rect mNormalBounds = new Rect(); |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 136 | @VisibleForTesting Rect mNormalMovementBounds = new Rect(); |
Ben Lin | 06dc41f | 2020-03-05 00:39:27 +0000 | [diff] [blame] | 137 | private Rect mExpandedBounds = new Rect(); |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 138 | @VisibleForTesting Rect mExpandedMovementBounds = new Rect(); |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 139 | private int mExpandedShortestEdgeSize; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 140 | |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 141 | // Used to workaround an issue where the WM rotation happens before we are notified, allowing |
| 142 | // us to send stale bounds |
| 143 | private int mDeferResizeToNormalBoundsUntilRotation = -1; |
| 144 | private int mDisplayRotation; |
| 145 | |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 146 | /** |
| 147 | * Runnable that can be posted delayed to show the target. This needs to be saved as a member |
| 148 | * variable so we can pass it to removeCallbacks. |
| 149 | */ |
| 150 | private Runnable mShowTargetAction = this::showDismissTargetMaybe; |
| 151 | |
Mady Mellor | d4e40fb | 2017-01-26 10:43:16 -0800 | [diff] [blame] | 152 | private Handler mHandler = new Handler(); |
Mady Mellor | d4e40fb | 2017-01-26 10:43:16 -0800 | [diff] [blame] | 153 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 154 | // Behaviour states |
Winson Chung | 9b91941 | 2017-06-19 17:01:51 -0700 | [diff] [blame] | 155 | private int mMenuState = MENU_STATE_NONE; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 156 | private boolean mIsImeShowing; |
| 157 | private int mImeHeight; |
Winson Chung | 136d1ec8 | 2017-07-18 11:16:30 -0700 | [diff] [blame] | 158 | private int mImeOffset; |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 159 | private int mDismissAreaHeight; |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 160 | private boolean mIsShelfShowing; |
| 161 | private int mShelfHeight; |
Winson Chung | dec1a66 | 2019-06-04 21:20:41 -0700 | [diff] [blame] | 162 | private int mMovementBoundsExtraOffsets; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 163 | private float mSavedSnapFraction = -1f; |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 164 | private boolean mSendingHoverAccessibilityEvents; |
Mady Mellor | 2fbdd3b | 2017-03-21 17:45:00 -0700 | [diff] [blame] | 165 | private boolean mMovementWithinDismiss; |
Arthur Hung | d896197 | 2019-10-14 17:00:17 +0800 | [diff] [blame] | 166 | private PipAccessibilityInteractionConnection mConnection; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 167 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 168 | // Touch state |
| 169 | private final PipTouchState mTouchState; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 170 | private final FlingAnimationUtils mFlingAnimationUtils; |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 171 | private final FloatingContentCoordinator mFloatingContentCoordinator; |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 172 | private PipMotionHelper mMotionHelper; |
Ben Lin | 1a80f12 | 2020-01-27 14:09:52 -0800 | [diff] [blame] | 173 | private PipTouchGesture mGesture; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 174 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 175 | // Temp vars |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 176 | private final Rect mTmpBounds = new Rect(); |
| 177 | |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 178 | /** |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 179 | * A listener for the PIP menu activity. |
| 180 | */ |
| 181 | private class PipMenuListener implements PipMenuActivityController.Listener { |
| 182 | @Override |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 183 | public void onPipMenuStateChanged(int menuState, boolean resize) { |
| 184 | setMenuState(menuState, resize); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 185 | } |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 186 | |
| 187 | @Override |
| 188 | public void onPipExpand() { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 189 | mMotionHelper.expandPip(); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void onPipDismiss() { |
Winson Chung | be00a83 | 2020-03-10 11:08:35 -0700 | [diff] [blame] | 194 | Pair<ComponentName, Integer> topPipActivity = PipUtils.getTopPipActivity(mContext, |
| 195 | mActivityManager); |
| 196 | if (topPipActivity.first != null) { |
| 197 | MetricsLoggerWrapper.logPictureInPictureDismissByTap(mContext, topPipActivity); |
| 198 | } |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 199 | mMotionHelper.dismissPip(); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 200 | } |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 201 | |
| 202 | @Override |
| 203 | public void onPipShowMenu() { |
| 204 | mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(), |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 205 | mMovementBounds, true /* allowMenuTimeout */, willResizeMenu()); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 206 | } |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 209 | @SuppressLint("InflateParams") |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 210 | public PipTouchHandler(Context context, IActivityManager activityManager, |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 211 | IActivityTaskManager activityTaskManager, PipMenuActivityController menuController, |
Hongwei Wang | 43a752b | 2019-09-17 20:20:30 +0000 | [diff] [blame] | 212 | InputConsumerController inputConsumerController, |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 213 | PipBoundsHandler pipBoundsHandler, |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 214 | PipTaskOrganizer pipTaskOrganizer, |
Beverly | 660d0a7 | 2020-02-26 12:32:42 -0500 | [diff] [blame] | 215 | FloatingContentCoordinator floatingContentCoordinator, |
jorgegil@google.com | 5bdec5f | 2020-02-28 13:14:39 -0800 | [diff] [blame] | 216 | DeviceConfigProxy deviceConfig, |
| 217 | PipSnapAlgorithm pipSnapAlgorithm) { |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 218 | // Initialize the Pip input consumer |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 219 | mContext = context; |
| 220 | mActivityManager = activityManager; |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 221 | mAccessibilityManager = context.getSystemService(AccessibilityManager.class); |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 222 | mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 223 | mMenuController = menuController; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 224 | mMenuController.addListener(new PipMenuListener()); |
jorgegil@google.com | 5bdec5f | 2020-02-28 13:14:39 -0800 | [diff] [blame] | 225 | mSnapAlgorithm = pipSnapAlgorithm; |
Dave Mankoff | 1373fdb | 2019-12-18 14:04:37 -0500 | [diff] [blame] | 226 | mFlingAnimationUtils = new FlingAnimationUtils(context.getResources().getDisplayMetrics(), |
| 227 | 2.5f); |
Ben Lin | 1a80f12 | 2020-01-27 14:09:52 -0800 | [diff] [blame] | 228 | mGesture = new DefaultPipTouchGesture(); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 229 | mMotionHelper = new PipMotionHelper(mContext, activityTaskManager, pipTaskOrganizer, |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 230 | mMenuController, mSnapAlgorithm, mFlingAnimationUtils, floatingContentCoordinator); |
Ben Lin | 04c83f6 | 2019-12-20 10:56:45 -0800 | [diff] [blame] | 231 | mPipResizeGestureHandler = |
Beverly | 660d0a7 | 2020-02-26 12:32:42 -0500 | [diff] [blame] | 232 | new PipResizeGestureHandler(context, pipBoundsHandler, this, mMotionHelper, |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 233 | deviceConfig, pipTaskOrganizer); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 234 | mTouchState = new PipTouchState(ViewConfiguration.get(context), mHandler, |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 235 | () -> mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(), |
| 236 | mMovementBounds, true /* allowMenuTimeout */, willResizeMenu())); |
Winson Chung | 136d1ec8 | 2017-07-18 11:16:30 -0700 | [diff] [blame] | 237 | |
| 238 | Resources res = context.getResources(); |
| 239 | mExpandedShortestEdgeSize = res.getDimensionPixelSize( |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 240 | R.dimen.pip_expanded_shortest_edge_size); |
Winson Chung | 136d1ec8 | 2017-07-18 11:16:30 -0700 | [diff] [blame] | 241 | mImeOffset = res.getDimensionPixelSize(R.dimen.pip_ime_offset); |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 242 | mDismissAreaHeight = res.getDimensionPixelSize(R.dimen.floating_dismiss_gradient_height); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 243 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 244 | mEnableDismissDragToEdge = res.getBoolean(R.bool.config_pipEnableDismissDragToEdge); |
Ben Lin | e55f85f | 2020-01-27 14:49:41 -0800 | [diff] [blame] | 245 | mEnableResize = res.getBoolean(R.bool.config_pipEnableResizeForMenu); |
Eliot Courtney | 3dc12f1 | 2018-06-01 14:26:26 +0900 | [diff] [blame] | 246 | |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 247 | // Register the listener for input consumer touch events |
Tracy Zhou | 535c212 | 2019-02-21 17:02:47 -0800 | [diff] [blame] | 248 | inputConsumerController.setInputListener(this::handleTouchEvent); |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 249 | inputConsumerController.setRegistrationListener(this::onRegistrationChanged); |
Hongwei Wang | 43a752b | 2019-09-17 20:20:30 +0000 | [diff] [blame] | 250 | |
| 251 | mPipBoundsHandler = pipBoundsHandler; |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 252 | mFloatingContentCoordinator = floatingContentCoordinator; |
Arthur Hung | d896197 | 2019-10-14 17:00:17 +0800 | [diff] [blame] | 253 | mConnection = new PipAccessibilityInteractionConnection(mMotionHelper, |
| 254 | this::onAccessibilityShowMenu, mHandler); |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 255 | |
| 256 | final int targetSize = res.getDimensionPixelSize(R.dimen.dismiss_circle_size); |
| 257 | mTargetView = new DismissCircleView(context); |
| 258 | final FrameLayout.LayoutParams newParams = |
| 259 | new FrameLayout.LayoutParams(targetSize, targetSize); |
| 260 | newParams.gravity = Gravity.CENTER; |
| 261 | mTargetView.setLayoutParams(newParams); |
| 262 | |
| 263 | mTargetViewContainer = new FrameLayout(context); |
| 264 | mTargetViewContainer.setClipChildren(false); |
| 265 | mTargetViewContainer.addView(mTargetView); |
| 266 | |
| 267 | mMagnetizedPip = mMotionHelper.getMagnetizedPip(); |
| 268 | mMagneticTarget = mMagnetizedPip.addTarget(mTargetView, 0); |
| 269 | mMagnetizedPip.setPhysicsAnimatorUpdateListener(mMotionHelper.mResizePipUpdateListener); |
| 270 | mMagnetizedPip.setMagnetListener(new MagnetizedObject.MagnetListener() { |
| 271 | @Override |
| 272 | public void onStuckToTarget(@NonNull MagnetizedObject.MagneticTarget target) { |
| 273 | mMotionHelper.prepareForAnimation(); |
| 274 | |
| 275 | // Show the dismiss target, in case the initial touch event occurred within the |
| 276 | // magnetic field radius. |
| 277 | showDismissTargetMaybe(); |
| 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public void onUnstuckFromTarget(@NonNull MagnetizedObject.MagneticTarget target, |
| 282 | float velX, float velY, boolean wasFlungOut) { |
| 283 | if (wasFlungOut) { |
| 284 | mMotionHelper.flingToSnapTarget(velX, velY, null, null); |
| 285 | hideDismissTarget(); |
| 286 | } else { |
| 287 | mMotionHelper.setSpringingToTouch(true); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public void onReleasedInTarget(@NonNull MagnetizedObject.MagneticTarget target) { |
| 293 | mHandler.post(() -> { |
| 294 | mMotionHelper.animateDismiss(0, 0, null); |
| 295 | hideDismissTarget(); |
| 296 | }); |
| 297 | |
| 298 | |
| 299 | MetricsLoggerWrapper.logPictureInPictureDismissByDrag(mContext, |
| 300 | PipUtils.getTopPipActivity(mContext, mActivityManager)); |
| 301 | } |
| 302 | }); |
| 303 | |
| 304 | mMagneticTargetAnimator = PhysicsAnimator.getInstance(mTargetView); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Ben Lin | 1a80f12 | 2020-01-27 14:09:52 -0800 | [diff] [blame] | 307 | public void setTouchGesture(PipTouchGesture gesture) { |
| 308 | mGesture = gesture; |
| 309 | } |
| 310 | |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 311 | public void setTouchEnabled(boolean enabled) { |
| 312 | mTouchState.setAllowTouches(enabled); |
| 313 | } |
| 314 | |
Winson Chung | ac52f28 | 2017-03-30 14:44:52 -0700 | [diff] [blame] | 315 | public void showPictureInPictureMenu() { |
| 316 | // Only show the menu if the user isn't currently interacting with the PiP |
| 317 | if (!mTouchState.isUserInteracting()) { |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 318 | mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(), |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 319 | mMovementBounds, false /* allowMenuTimeout */, willResizeMenu()); |
Winson Chung | ac52f28 | 2017-03-30 14:44:52 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 323 | public void onActivityPinned() { |
Joshua Tsuji | f7de680 | 2020-04-13 13:30:11 -0400 | [diff] [blame] | 324 | createOrUpdateDismissTarget(); |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 325 | |
Wale Ogunwale | 6455e50 | 2017-04-17 14:16:43 -0700 | [diff] [blame] | 326 | mShowPipMenuOnAnimationEnd = true; |
Ben Lin | 04c83f6 | 2019-12-20 10:56:45 -0800 | [diff] [blame] | 327 | mPipResizeGestureHandler.onActivityPinned(); |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 328 | mFloatingContentCoordinator.onContentAdded(mMotionHelper); |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Winson Chung | 9b91941 | 2017-06-19 17:01:51 -0700 | [diff] [blame] | 331 | public void onActivityUnpinned(ComponentName topPipActivity) { |
| 332 | if (topPipActivity == null) { |
| 333 | // Clean up state after the last PiP activity is removed |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 334 | cleanUpDismissTarget(); |
| 335 | |
| 336 | mFloatingContentCoordinator.onContentRemoved(mMotionHelper); |
Winson Chung | 9b91941 | 2017-06-19 17:01:51 -0700 | [diff] [blame] | 337 | } |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 338 | mResizedBounds.setEmpty(); |
Ben Lin | 04c83f6 | 2019-12-20 10:56:45 -0800 | [diff] [blame] | 339 | mPipResizeGestureHandler.onActivityUnpinned(); |
Winson Chung | 9b91941 | 2017-06-19 17:01:51 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Winson Chung | ac52f28 | 2017-03-30 14:44:52 -0700 | [diff] [blame] | 342 | public void onPinnedStackAnimationEnded() { |
| 343 | // Always synchronize the motion helper bounds once PiP animations finish |
| 344 | mMotionHelper.synchronizePinnedStackBounds(); |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 345 | updateMovementBounds(); |
| 346 | mResizedBounds.set(mMotionHelper.getBounds()); |
Wale Ogunwale | 6455e50 | 2017-04-17 14:16:43 -0700 | [diff] [blame] | 347 | |
| 348 | if (mShowPipMenuOnAnimationEnd) { |
| 349 | mMenuController.showMenu(MENU_STATE_CLOSE, mMotionHelper.getBounds(), |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 350 | mMovementBounds, true /* allowMenuTimeout */, false /* willResizeMenu */); |
Wale Ogunwale | 6455e50 | 2017-04-17 14:16:43 -0700 | [diff] [blame] | 351 | mShowPipMenuOnAnimationEnd = false; |
| 352 | } |
Winson Chung | ac52f28 | 2017-03-30 14:44:52 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Winson Chung | 303c6b7 | 2016-10-24 17:12:49 -0700 | [diff] [blame] | 355 | public void onConfigurationChanged() { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 356 | mMotionHelper.onConfigurationChanged(); |
| 357 | mMotionHelper.synchronizePinnedStackBounds(); |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 358 | |
| 359 | // Recreate the dismiss target for the new orientation. |
Joshua Tsuji | f7de680 | 2020-04-13 13:30:11 -0400 | [diff] [blame] | 360 | createOrUpdateDismissTarget(); |
Winson Chung | 303c6b7 | 2016-10-24 17:12:49 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 363 | public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) { |
| 364 | mIsImeShowing = imeVisible; |
| 365 | mImeHeight = imeHeight; |
| 366 | } |
| 367 | |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 368 | public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight) { |
| 369 | mIsShelfShowing = shelfVisible; |
| 370 | mShelfHeight = shelfHeight; |
| 371 | } |
| 372 | |
Ben Lin | 06dc41f | 2020-03-05 00:39:27 +0000 | [diff] [blame] | 373 | public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds, Rect curBounds, |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 374 | boolean fromImeAdjustment, boolean fromShelfAdjustment, int displayRotation) { |
| 375 | final int bottomOffset = mIsImeShowing ? mImeHeight : 0; |
Hongwei Wang | bede2c1 | 2020-02-26 12:11:53 -0800 | [diff] [blame] | 376 | final boolean fromDisplayRotationChanged = (mDisplayRotation != displayRotation); |
| 377 | if (fromDisplayRotationChanged) { |
| 378 | mTouchState.reset(); |
| 379 | } |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 380 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 381 | // Re-calculate the expanded bounds |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 382 | mNormalBounds.set(normalBounds); |
Ben Lin | 06dc41f | 2020-03-05 00:39:27 +0000 | [diff] [blame] | 383 | Rect normalMovementBounds = new Rect(); |
| 384 | mSnapAlgorithm.getMovementBounds(mNormalBounds, insetBounds, normalMovementBounds, |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 385 | bottomOffset); |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 386 | |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 387 | if (mMovementBounds.isEmpty()) { |
| 388 | // mMovementBounds is not initialized yet and a clean movement bounds without |
| 389 | // bottom offset shall be used later in this function. |
| 390 | mSnapAlgorithm.getMovementBounds(curBounds, insetBounds, mMovementBounds, |
| 391 | 0 /* bottomOffset */); |
| 392 | } |
| 393 | |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 394 | // Calculate the expanded size |
Ben Lin | 06dc41f | 2020-03-05 00:39:27 +0000 | [diff] [blame] | 395 | float aspectRatio = (float) normalBounds.width() / normalBounds.height(); |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 396 | Point displaySize = new Point(); |
| 397 | mContext.getDisplay().getRealSize(displaySize); |
Ben Lin | 06dc41f | 2020-03-05 00:39:27 +0000 | [diff] [blame] | 398 | Size expandedSize = mSnapAlgorithm.getSizeForAspectRatio(aspectRatio, |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 399 | mExpandedShortestEdgeSize, displaySize.x, displaySize.y); |
Ben Lin | 06dc41f | 2020-03-05 00:39:27 +0000 | [diff] [blame] | 400 | mExpandedBounds.set(0, 0, expandedSize.getWidth(), expandedSize.getHeight()); |
Ben Lin | 06dc41f | 2020-03-05 00:39:27 +0000 | [diff] [blame] | 401 | Rect expandedMovementBounds = new Rect(); |
| 402 | mSnapAlgorithm.getMovementBounds(mExpandedBounds, insetBounds, expandedMovementBounds, |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 403 | bottomOffset); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 404 | |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 405 | mPipResizeGestureHandler.updateMinSize(mNormalBounds.width(), mNormalBounds.height()); |
| 406 | mPipResizeGestureHandler.updateMaxSize(mExpandedBounds.width(), mExpandedBounds.height()); |
| 407 | |
Winson Chung | dec1a66 | 2019-06-04 21:20:41 -0700 | [diff] [blame] | 408 | // The extra offset does not really affect the movement bounds, but are applied based on the |
| 409 | // current state (ime showing, or shelf offset) when we need to actually shift |
| 410 | int extraOffset = Math.max( |
| 411 | mIsImeShowing ? mImeOffset : 0, |
| 412 | !mIsImeShowing && mIsShelfShowing ? mShelfHeight : 0); |
| 413 | |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 414 | // If this is from an IME or shelf adjustment, then we should move the PiP so that it is not |
| 415 | // occluded by the IME or shelf. |
Hongwei Wang | bede2c1 | 2020-02-26 12:11:53 -0800 | [diff] [blame] | 416 | if (fromImeAdjustment || fromShelfAdjustment || fromDisplayRotationChanged) { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 417 | if (mTouchState.isUserInteracting()) { |
| 418 | // Defer the update of the current movement bounds until after the user finishes |
| 419 | // touching the screen |
| 420 | } else { |
Winson Chung | dec1a66 | 2019-06-04 21:20:41 -0700 | [diff] [blame] | 421 | final float offsetBufferPx = BOTTOM_OFFSET_BUFFER_DP |
Tracy Zhou | e19d051 | 2018-07-31 16:29:34 -0700 | [diff] [blame] | 422 | * mContext.getResources().getDisplayMetrics().density; |
Ben Lin | e55f85f | 2020-01-27 14:49:41 -0800 | [diff] [blame] | 423 | final Rect toMovementBounds = mMenuState == MENU_STATE_FULL && willResizeMenu() |
Ben Lin | 06dc41f | 2020-03-05 00:39:27 +0000 | [diff] [blame] | 424 | ? new Rect(expandedMovementBounds) |
| 425 | : new Rect(normalMovementBounds); |
Winson Chung | dec1a66 | 2019-06-04 21:20:41 -0700 | [diff] [blame] | 426 | final int prevBottom = mMovementBounds.bottom - mMovementBoundsExtraOffsets; |
| 427 | final int toBottom = toMovementBounds.bottom < toMovementBounds.top |
| 428 | ? toMovementBounds.bottom |
| 429 | : toMovementBounds.bottom - extraOffset; |
| 430 | if ((Math.min(prevBottom, toBottom) - offsetBufferPx) <= curBounds.top |
| 431 | && curBounds.top <= (Math.max(prevBottom, toBottom) + offsetBufferPx)) { |
| 432 | mMotionHelper.animateToOffset(curBounds, toBottom - curBounds.top); |
Tracy Zhou | 805e66c | 2018-04-04 23:45:26 -0700 | [diff] [blame] | 433 | } |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 434 | } |
Winson Chung | 14fbe14 | 2016-12-19 16:18:24 -0800 | [diff] [blame] | 435 | } |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 436 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 437 | // Update the movement bounds after doing the calculations based on the old movement bounds |
| 438 | // above |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 439 | mNormalMovementBounds.set(normalMovementBounds); |
| 440 | mExpandedMovementBounds.set(expandedMovementBounds); |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 441 | mDisplayRotation = displayRotation; |
Winson Chung | 0a2f34f | 2017-08-11 18:13:24 -0700 | [diff] [blame] | 442 | mInsetBounds.set(insetBounds); |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 443 | updateMovementBounds(); |
Winson Chung | dec1a66 | 2019-06-04 21:20:41 -0700 | [diff] [blame] | 444 | mMovementBoundsExtraOffsets = extraOffset; |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 445 | |
| 446 | // If we have a deferred resize, apply it now |
| 447 | if (mDeferResizeToNormalBoundsUntilRotation == displayRotation) { |
Ben Lin | 06dc41f | 2020-03-05 00:39:27 +0000 | [diff] [blame] | 448 | mMotionHelper.animateToUnexpandedState(normalBounds, mSavedSnapFraction, |
| 449 | mNormalMovementBounds, mMovementBounds, true /* immediate */); |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 450 | mSavedSnapFraction = -1f; |
| 451 | mDeferResizeToNormalBoundsUntilRotation = -1; |
| 452 | } |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 453 | } |
| 454 | |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 455 | /** Adds the magnetic target view to the WindowManager so it's ready to be animated in. */ |
Joshua Tsuji | f7de680 | 2020-04-13 13:30:11 -0400 | [diff] [blame] | 456 | private void createOrUpdateDismissTarget() { |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 457 | if (!mTargetViewContainer.isAttachedToWindow()) { |
| 458 | mHandler.removeCallbacks(mShowTargetAction); |
| 459 | mMagneticTargetAnimator.cancel(); |
| 460 | |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 461 | mTargetViewContainer.setVisibility(View.INVISIBLE); |
Joshua Tsuji | f7de680 | 2020-04-13 13:30:11 -0400 | [diff] [blame] | 462 | |
| 463 | try { |
| 464 | mWindowManager.addView(mTargetViewContainer, getDismissTargetLayoutParams()); |
| 465 | } catch (IllegalStateException e) { |
| 466 | // This shouldn't happen, but if the target is already added, just update its layout |
| 467 | // params. |
| 468 | mWindowManager.updateViewLayout( |
| 469 | mTargetViewContainer, getDismissTargetLayoutParams()); |
| 470 | } |
| 471 | } else { |
| 472 | mWindowManager.updateViewLayout(mTargetViewContainer, getDismissTargetLayoutParams()); |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
Joshua Tsuji | f7de680 | 2020-04-13 13:30:11 -0400 | [diff] [blame] | 476 | /** Returns layout params for the dismiss target, using the latest display metrics. */ |
| 477 | private WindowManager.LayoutParams getDismissTargetLayoutParams() { |
| 478 | final Point windowSize = new Point(); |
| 479 | mWindowManager.getDefaultDisplay().getRealSize(windowSize); |
| 480 | |
| 481 | final WindowManager.LayoutParams lp = new WindowManager.LayoutParams( |
| 482 | WindowManager.LayoutParams.MATCH_PARENT, |
| 483 | mDismissAreaHeight, |
| 484 | 0, windowSize.y - mDismissAreaHeight, |
| 485 | WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL, |
| 486 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
| 487 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
| 488 | | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, |
| 489 | PixelFormat.TRANSLUCENT); |
| 490 | |
| 491 | lp.setTitle("pip-dismiss-overlay"); |
| 492 | lp.privateFlags |= WindowManager.LayoutParams.SYSTEM_FLAG_SHOW_FOR_ALL_USERS; |
| 493 | lp.setFitInsetsTypes(0 /* types */); |
| 494 | |
| 495 | return lp; |
| 496 | } |
| 497 | |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 498 | /** Makes the dismiss target visible and animates it in, if it isn't already visible. */ |
| 499 | private void showDismissTargetMaybe() { |
Joshua Tsuji | f7de680 | 2020-04-13 13:30:11 -0400 | [diff] [blame] | 500 | createOrUpdateDismissTarget(); |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 501 | |
| 502 | if (mTargetViewContainer.getVisibility() != View.VISIBLE) { |
| 503 | |
| 504 | mTargetView.setTranslationY(mTargetViewContainer.getHeight()); |
| 505 | mTargetViewContainer.setVisibility(View.VISIBLE); |
| 506 | |
| 507 | // Set the magnetic field radius to half of PIP's width. |
| 508 | mMagneticTarget.setMagneticFieldRadiusPx(mMotionHelper.getBounds().width()); |
| 509 | |
| 510 | // Cancel in case we were in the middle of animating it out. |
| 511 | mMagneticTargetAnimator.cancel(); |
| 512 | mMagneticTargetAnimator |
| 513 | .spring(DynamicAnimation.TRANSLATION_Y, 0f, mTargetSpringConfig) |
| 514 | .start(); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | /** Animates the magnetic dismiss target out and then sets it to GONE. */ |
| 519 | private void hideDismissTarget() { |
| 520 | mHandler.removeCallbacks(mShowTargetAction); |
| 521 | mMagneticTargetAnimator |
| 522 | .spring(DynamicAnimation.TRANSLATION_Y, |
| 523 | mTargetViewContainer.getHeight(), |
| 524 | mTargetSpringConfig) |
| 525 | .withEndActions(() -> mTargetViewContainer.setVisibility(View.GONE)) |
| 526 | .start(); |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Removes the dismiss target and cancels any pending callbacks to show it. |
| 531 | */ |
| 532 | private void cleanUpDismissTarget() { |
| 533 | mHandler.removeCallbacks(mShowTargetAction); |
| 534 | |
| 535 | if (mTargetViewContainer.isAttachedToWindow()) { |
Joshua Tsuji | 06745aa | 2020-04-09 11:17:42 -0400 | [diff] [blame] | 536 | mWindowManager.removeViewImmediate(mTargetViewContainer); |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 540 | private void onRegistrationChanged(boolean isRegistered) { |
| 541 | mAccessibilityManager.setPictureInPictureActionReplacingConnection(isRegistered |
Arthur Hung | d896197 | 2019-10-14 17:00:17 +0800 | [diff] [blame] | 542 | ? mConnection : null); |
Winson Chung | b54b65b | 2017-04-26 14:02:13 -0700 | [diff] [blame] | 543 | if (!isRegistered && mTouchState.isUserInteracting()) { |
| 544 | // If the input consumer is unregistered while the user is interacting, then we may not |
| 545 | // get the final TOUCH_UP event, so clean up the dismiss target as well |
| 546 | cleanUpDismissTarget(); |
| 547 | } |
Winson Chung | fe1fa64 | 2017-03-13 10:51:22 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | private void onAccessibilityShowMenu() { |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 551 | mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(), |
Rhed Jao | 5e00c98 | 2018-11-27 19:41:05 +0800 | [diff] [blame] | 552 | mMovementBounds, true /* allowMenuTimeout */, willResizeMenu()); |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Tracy Zhou | 535c212 | 2019-02-21 17:02:47 -0800 | [diff] [blame] | 555 | private boolean handleTouchEvent(InputEvent inputEvent) { |
| 556 | // Skip any non motion events |
| 557 | if (!(inputEvent instanceof MotionEvent)) { |
| 558 | return true; |
| 559 | } |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 560 | // Skip touch handling until we are bound to the controller |
| 561 | if (mPinnedStackController == null) { |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 562 | return true; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 563 | } |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 564 | |
Tracy Zhou | 535c212 | 2019-02-21 17:02:47 -0800 | [diff] [blame] | 565 | MotionEvent ev = (MotionEvent) inputEvent; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 566 | |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 567 | if (mMagnetizedPip.maybeConsumeMotionEvent(ev)) { |
| 568 | // If the first touch event occurs within the magnetic field, pass the ACTION_DOWN event |
| 569 | // to the touch state. Touch state needs a DOWN event in order to later process MOVE |
| 570 | // events it'll receive if the object is dragged out of the magnetic field. |
| 571 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 572 | mTouchState.onTouchEvent(ev); |
| 573 | } |
| 574 | |
| 575 | // Continue tracking velocity when the object is in the magnetic field, since we want to |
| 576 | // respect touch input velocity if the object is dragged out and then flung. |
| 577 | mTouchState.addMovementToVelocityTracker(ev); |
| 578 | |
| 579 | return true; |
| 580 | } |
| 581 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 582 | // Update the touch state |
| 583 | mTouchState.onTouchEvent(ev); |
| 584 | |
Arthur Hung | d52d119 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 585 | boolean shouldDeliverToMenu = mMenuState != MENU_STATE_NONE; |
| 586 | |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 587 | switch (ev.getAction()) { |
| 588 | case MotionEvent.ACTION_DOWN: { |
Joshua Tsuji | 601dc85 | 2020-04-02 15:54:15 -0400 | [diff] [blame] | 589 | mMotionHelper.synchronizePinnedStackBoundsForTouchGesture(); |
Ben Lin | 1a80f12 | 2020-01-27 14:09:52 -0800 | [diff] [blame] | 590 | mGesture.onDown(mTouchState); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | case MotionEvent.ACTION_MOVE: { |
Ben Lin | 1a80f12 | 2020-01-27 14:09:52 -0800 | [diff] [blame] | 594 | if (mGesture.onMove(mTouchState)) { |
| 595 | break; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 596 | } |
Arthur Hung | d52d119 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 597 | |
| 598 | shouldDeliverToMenu = !mTouchState.isDragging(); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 599 | break; |
| 600 | } |
| 601 | case MotionEvent.ACTION_UP: { |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 602 | // Update the movement bounds again if the state has changed since the user started |
| 603 | // dragging (ie. when the IME shows) |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 604 | updateMovementBounds(); |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 605 | |
Ben Lin | 1a80f12 | 2020-01-27 14:09:52 -0800 | [diff] [blame] | 606 | if (mGesture.onUp(mTouchState)) { |
| 607 | break; |
Winson Chung | 5cd26ff | 2016-10-24 11:50:44 -0700 | [diff] [blame] | 608 | } |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 609 | |
| 610 | // Fall through to clean up |
| 611 | } |
| 612 | case MotionEvent.ACTION_CANCEL: { |
Arthur Hung | d52d119 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 613 | shouldDeliverToMenu = !mTouchState.startedDragging() && !mTouchState.isDragging(); |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 614 | mTouchState.reset(); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 615 | break; |
| 616 | } |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 617 | case MotionEvent.ACTION_HOVER_ENTER: |
Ben Lin | 388df60 | 2020-04-08 14:07:06 -0700 | [diff] [blame] | 618 | // If Touch Exploration is enabled, some a11y services (e.g. Talkback) is probably |
| 619 | // on and changing MotionEvents into HoverEvents. |
| 620 | // Let's not enable menu show/hide for a11y services. |
| 621 | if (!mAccessibilityManager.isTouchExplorationEnabled()) { |
| 622 | mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(), |
| 623 | mMovementBounds, false /* allowMenuTimeout */, |
| 624 | false /* willResizeMenu */); |
| 625 | } |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 626 | case MotionEvent.ACTION_HOVER_MOVE: { |
Arthur Hung | d896197 | 2019-10-14 17:00:17 +0800 | [diff] [blame] | 627 | if (!shouldDeliverToMenu && !mSendingHoverAccessibilityEvents) { |
| 628 | sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER); |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 629 | mSendingHoverAccessibilityEvents = true; |
| 630 | } |
| 631 | break; |
| 632 | } |
| 633 | case MotionEvent.ACTION_HOVER_EXIT: { |
Ben Lin | 388df60 | 2020-04-08 14:07:06 -0700 | [diff] [blame] | 634 | // If Touch Exploration is enabled, some a11y services (e.g. Talkback) is probably |
| 635 | // on and changing MotionEvents into HoverEvents. |
| 636 | // Let's not enable menu show/hide for a11y services. |
| 637 | if (!mAccessibilityManager.isTouchExplorationEnabled()) { |
| 638 | mMenuController.hideMenu(); |
| 639 | } |
Arthur Hung | d896197 | 2019-10-14 17:00:17 +0800 | [diff] [blame] | 640 | if (!shouldDeliverToMenu && mSendingHoverAccessibilityEvents) { |
| 641 | sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT); |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 642 | mSendingHoverAccessibilityEvents = false; |
| 643 | } |
| 644 | break; |
| 645 | } |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 646 | } |
Arthur Hung | d52d119 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 647 | |
| 648 | // Deliver the event to PipMenuActivity to handle button click if the menu has shown. |
| 649 | if (shouldDeliverToMenu) { |
| 650 | final MotionEvent cloneEvent = MotionEvent.obtain(ev); |
| 651 | // Send the cancel event and cancel menu timeout if it starts to drag. |
| 652 | if (mTouchState.startedDragging()) { |
| 653 | cloneEvent.setAction(MotionEvent.ACTION_CANCEL); |
| 654 | mMenuController.pokeMenu(); |
| 655 | } |
| 656 | |
Arthur Hung | d896197 | 2019-10-14 17:00:17 +0800 | [diff] [blame] | 657 | mMenuController.handlePointerEvent(cloneEvent); |
Arthur Hung | d52d119 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | return true; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Arthur Hung | d896197 | 2019-10-14 17:00:17 +0800 | [diff] [blame] | 663 | private void sendAccessibilityHoverEvent(int type) { |
| 664 | if (!mAccessibilityManager.isEnabled()) { |
| 665 | return; |
| 666 | } |
| 667 | |
| 668 | AccessibilityEvent event = AccessibilityEvent.obtain(type); |
| 669 | event.setImportantForAccessibility(true); |
| 670 | event.setSourceNodeId(AccessibilityNodeInfo.ROOT_NODE_ID); |
| 671 | event.setWindowId( |
| 672 | AccessibilityWindowInfo.PICTURE_IN_PICTURE_ACTION_REPLACER_WINDOW_ID); |
| 673 | mAccessibilityManager.sendAccessibilityEvent(event); |
| 674 | } |
| 675 | |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 676 | /** |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 677 | * Updates the appearance of the menu and scrim on top of the PiP while dismissing. |
| 678 | */ |
Winson Chung | 8ec0e16 | 2017-07-07 14:49:49 -0700 | [diff] [blame] | 679 | private void updateDismissFraction() { |
Winson Chung | 0a2f34f | 2017-08-11 18:13:24 -0700 | [diff] [blame] | 680 | // Skip updating the dismiss fraction when the IME is showing. This is to work around an |
| 681 | // issue where starting the menu activity for the dismiss overlay will steal the window |
| 682 | // focus, which closes the IME. |
| 683 | if (mMenuController != null && !mIsImeShowing) { |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 684 | Rect bounds = mMotionHelper.getBounds(); |
Winson Chung | 0a2f34f | 2017-08-11 18:13:24 -0700 | [diff] [blame] | 685 | final float target = mInsetBounds.bottom; |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 686 | float fraction = 0f; |
| 687 | if (bounds.bottom > target) { |
| 688 | final float distance = bounds.bottom - target; |
| 689 | fraction = Math.min(distance / bounds.height(), 1f); |
| 690 | } |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 691 | if (Float.compare(fraction, 0f) != 0 || mMenuController.isMenuActivityVisible()) { |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 692 | // Update if the fraction > 0, or if fraction == 0 and the menu was already visible |
| 693 | mMenuController.setDismissFraction(fraction); |
| 694 | } |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
| 698 | /** |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 699 | * Sets the controller to update the system of changes from user interaction. |
| 700 | */ |
| 701 | void setPinnedStackController(IPinnedStackController controller) { |
| 702 | mPinnedStackController = controller; |
| 703 | } |
| 704 | |
| 705 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 706 | * Sets the menu visibility. |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 707 | */ |
Winson Chung | 8ec0e16 | 2017-07-07 14:49:49 -0700 | [diff] [blame] | 708 | private void setMenuState(int menuState, boolean resize) { |
Arthur Hung | d896197 | 2019-10-14 17:00:17 +0800 | [diff] [blame] | 709 | if (mMenuState == menuState && !resize) { |
| 710 | return; |
| 711 | } |
| 712 | |
Winson Chung | a89053d | 2018-06-12 16:47:30 -0700 | [diff] [blame] | 713 | if (menuState == MENU_STATE_FULL && mMenuState != MENU_STATE_FULL) { |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 714 | // Save the current snap fraction and if we do not drag or move the PiP, then |
| 715 | // we store back to this snap fraction. Otherwise, we'll reset the snap |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 716 | // fraction and snap to the closest edge. |
| 717 | // Also save the current resized bounds so when the menu disappears, we can restore it. |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 718 | if (resize) { |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 719 | mResizedBounds.set(mMotionHelper.getBounds()); |
| 720 | Rect expandedBounds = new Rect(mExpandedBounds); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 721 | mSavedSnapFraction = mMotionHelper.animateToExpandedState(expandedBounds, |
Ben Lin | 06dc41f | 2020-03-05 00:39:27 +0000 | [diff] [blame] | 722 | mMovementBounds, mExpandedMovementBounds); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 723 | } |
Winson Chung | a89053d | 2018-06-12 16:47:30 -0700 | [diff] [blame] | 724 | } else if (menuState == MENU_STATE_NONE && mMenuState == MENU_STATE_FULL) { |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 725 | // Try and restore the PiP to the closest edge, using the saved snap fraction |
| 726 | // if possible |
| 727 | if (resize) { |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 728 | if (mDeferResizeToNormalBoundsUntilRotation == -1) { |
Winson Chung | bb23376 | 2017-05-15 14:20:46 -0700 | [diff] [blame] | 729 | // This is a very special case: when the menu is expanded and visible, |
| 730 | // navigating to another activity can trigger auto-enter PiP, and if the |
| 731 | // revealed activity has a forced rotation set, then the controller will get |
| 732 | // updated with the new rotation of the display. However, at the same time, |
| 733 | // SystemUI will try to hide the menu by creating an animation to the normal |
| 734 | // bounds which are now stale. In such a case we defer the animation to the |
| 735 | // normal bounds until after the next onMovementBoundsChanged() call to get the |
| 736 | // bounds in the new orientation |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 737 | try { |
| 738 | int displayRotation = mPinnedStackController.getDisplayRotation(); |
| 739 | if (mDisplayRotation != displayRotation) { |
| 740 | mDeferResizeToNormalBoundsUntilRotation = displayRotation; |
| 741 | } |
| 742 | } catch (RemoteException e) { |
| 743 | Log.e(TAG, "Could not get display rotation from controller"); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | if (mDeferResizeToNormalBoundsUntilRotation == -1) { |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 748 | Rect restoreBounds = new Rect(mResizedBounds); |
| 749 | Rect restoredMovementBounds = new Rect(); |
| 750 | mSnapAlgorithm.getMovementBounds(restoreBounds, mInsetBounds, |
| 751 | restoredMovementBounds, mIsImeShowing ? mImeHeight : 0); |
| 752 | mMotionHelper.animateToUnexpandedState(restoreBounds, mSavedSnapFraction, |
| 753 | restoredMovementBounds, mMovementBounds, false /* immediate */); |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 754 | mSavedSnapFraction = -1f; |
| 755 | } |
| 756 | } else { |
| 757 | mSavedSnapFraction = -1f; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 758 | } |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 759 | } |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 760 | mMenuState = menuState; |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 761 | updateMovementBounds(); |
Arthur Hung | d896197 | 2019-10-14 17:00:17 +0800 | [diff] [blame] | 762 | // If pip menu has dismissed, we should register the A11y ActionReplacingConnection for pip |
| 763 | // as well, or it can't handle a11y focus and pip menu can't perform any action. |
| 764 | onRegistrationChanged(menuState == MENU_STATE_NONE); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 765 | if (menuState != MENU_STATE_CLOSE) { |
Chenjie Yu | 52cacc6 | 2017-12-08 18:11:45 -0800 | [diff] [blame] | 766 | MetricsLoggerWrapper.logPictureInPictureMenuVisible(mContext, menuState == MENU_STATE_FULL); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 767 | } |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 771 | * @return the motion helper. |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 772 | */ |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 773 | public PipMotionHelper getMotionHelper() { |
| 774 | return mMotionHelper; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 775 | } |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 776 | |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 777 | @VisibleForTesting |
| 778 | PipResizeGestureHandler getPipResizeGestureHandler() { |
| 779 | return mPipResizeGestureHandler; |
| 780 | } |
| 781 | |
| 782 | @VisibleForTesting |
| 783 | void setPipResizeGestureHandler(PipResizeGestureHandler pipResizeGestureHandler) { |
| 784 | mPipResizeGestureHandler = pipResizeGestureHandler; |
| 785 | } |
| 786 | |
| 787 | @VisibleForTesting |
| 788 | void setPipMotionHelper(PipMotionHelper pipMotionHelper) { |
| 789 | mMotionHelper = pipMotionHelper; |
| 790 | } |
| 791 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 792 | /** |
jorgegil@google.com | 62f735e | 2019-10-30 14:37:19 -0700 | [diff] [blame] | 793 | * @return the unexpanded bounds. |
| 794 | */ |
Ben Lin | 06dc41f | 2020-03-05 00:39:27 +0000 | [diff] [blame] | 795 | public Rect getNormalBounds() { |
| 796 | return mNormalBounds; |
jorgegil@google.com | 62f735e | 2019-10-30 14:37:19 -0700 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /** |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 800 | * Gesture controlling normal movement of the PIP. |
| 801 | */ |
Ben Lin | 1a80f12 | 2020-01-27 14:09:52 -0800 | [diff] [blame] | 802 | private class DefaultPipTouchGesture extends PipTouchGesture { |
Winson Chung | 8ec0e16 | 2017-07-07 14:49:49 -0700 | [diff] [blame] | 803 | private final Point mStartPosition = new Point(); |
| 804 | private final PointF mDelta = new PointF(); |
Mady Mellor | d4e40fb | 2017-01-26 10:43:16 -0800 | [diff] [blame] | 805 | |
| 806 | @Override |
| 807 | public void onDown(PipTouchState touchState) { |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 808 | if (!touchState.isUserInteracting()) { |
| 809 | return; |
| 810 | } |
| 811 | |
Mady Mellor | 15b29c7 | 2017-06-07 14:53:58 -0700 | [diff] [blame] | 812 | Rect bounds = mMotionHelper.getBounds(); |
Winson Chung | 8ec0e16 | 2017-07-07 14:49:49 -0700 | [diff] [blame] | 813 | mDelta.set(0f, 0f); |
| 814 | mStartPosition.set(bounds.left, bounds.top); |
Mady Mellor | 2fbdd3b | 2017-03-21 17:45:00 -0700 | [diff] [blame] | 815 | mMovementWithinDismiss = touchState.getDownTouchPosition().y >= mMovementBounds.bottom; |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 816 | mMotionHelper.setSpringingToTouch(false); |
Mady Mellor | 2fbdd3b | 2017-03-21 17:45:00 -0700 | [diff] [blame] | 817 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 818 | // If the menu is still visible then just poke the menu |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 819 | // so that it will timeout after the user stops touching it |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 820 | if (mMenuState != MENU_STATE_NONE) { |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 821 | mMenuController.pokeMenu(); |
| 822 | } |
Mady Mellor | d4e40fb | 2017-01-26 10:43:16 -0800 | [diff] [blame] | 823 | } |
| 824 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 825 | @Override |
Ben Lin | 1a80f12 | 2020-01-27 14:09:52 -0800 | [diff] [blame] | 826 | public boolean onMove(PipTouchState touchState) { |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 827 | if (!touchState.isUserInteracting()) { |
| 828 | return false; |
| 829 | } |
| 830 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 831 | if (touchState.startedDragging()) { |
| 832 | mSavedSnapFraction = -1f; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 833 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 834 | if (mEnableDismissDragToEdge) { |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 835 | if (mTargetViewContainer.getVisibility() != View.VISIBLE) { |
| 836 | mHandler.removeCallbacks(mShowTargetAction); |
| 837 | showDismissTargetMaybe(); |
| 838 | } |
Winson Chung | b54b65b | 2017-04-26 14:02:13 -0700 | [diff] [blame] | 839 | } |
Mady Mellor | d4e40fb | 2017-01-26 10:43:16 -0800 | [diff] [blame] | 840 | } |
| 841 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 842 | if (touchState.isDragging()) { |
| 843 | // Move the pinned stack freely |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 844 | final PointF lastDelta = touchState.getLastTouchDelta(); |
Winson Chung | 8ec0e16 | 2017-07-07 14:49:49 -0700 | [diff] [blame] | 845 | float lastX = mStartPosition.x + mDelta.x; |
| 846 | float lastY = mStartPosition.y + mDelta.y; |
| 847 | float left = lastX + lastDelta.x; |
| 848 | float top = lastY + lastDelta.y; |
Winson Chung | 8ec0e16 | 2017-07-07 14:49:49 -0700 | [diff] [blame] | 849 | |
| 850 | // Add to the cumulative delta after bounding the position |
| 851 | mDelta.x += left - lastX; |
| 852 | mDelta.y += top - lastY; |
| 853 | |
| 854 | mTmpBounds.set(mMotionHelper.getBounds()); |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 855 | mTmpBounds.offsetTo((int) left, (int) top); |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 856 | mMotionHelper.movePip(mTmpBounds, true /* isDragging */); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 857 | |
Mady Mellor | 2fbdd3b | 2017-03-21 17:45:00 -0700 | [diff] [blame] | 858 | final PointF curPos = touchState.getLastTouchPosition(); |
Mady Mellor | 2fbdd3b | 2017-03-21 17:45:00 -0700 | [diff] [blame] | 859 | if (mMovementWithinDismiss) { |
| 860 | // Track if movement remains near the bottom edge to identify swipe to dismiss |
| 861 | mMovementWithinDismiss = curPos.y >= mMovementBounds.bottom; |
| 862 | } |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 863 | return true; |
| 864 | } |
| 865 | return false; |
| 866 | } |
| 867 | |
| 868 | @Override |
| 869 | public boolean onUp(PipTouchState touchState) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 870 | if (mEnableDismissDragToEdge) { |
Joshua Tsuji | f39539d | 2020-04-03 18:53:06 -0400 | [diff] [blame] | 871 | hideDismissTarget(); |
Winson Chung | b54b65b | 2017-04-26 14:02:13 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 874 | if (!touchState.isUserInteracting()) { |
| 875 | return false; |
| 876 | } |
| 877 | |
Mady Mellor | 60421c9 | 2017-03-29 15:27:37 -0700 | [diff] [blame] | 878 | final PointF vel = touchState.getVelocity(); |
Mady Mellor | 60421c9 | 2017-03-29 15:27:37 -0700 | [diff] [blame] | 879 | final float velocity = PointF.length(vel.x, vel.y); |
| 880 | final boolean isFling = velocity > mFlingAnimationUtils.getMinVelocityPxPerSecond(); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 881 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 882 | if (touchState.isDragging()) { |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 883 | Runnable endAction = null; |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 884 | if (mMenuState != MENU_STATE_NONE) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 885 | // If the menu is still visible, then just poke the menu so that |
| 886 | // it will timeout after the user stops touching it |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 887 | mMenuController.showMenu(mMenuState, mMotionHelper.getBounds(), |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 888 | mMovementBounds, true /* allowMenuTimeout */, willResizeMenu()); |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 889 | } else { |
| 890 | // If the menu is not visible, then we can still be showing the activity for the |
| 891 | // dismiss overlay, so just finish it after the animation completes |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 892 | endAction = mMenuController::hideMenu; |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 893 | } |
| 894 | |
Mady Mellor | 2fbdd3b | 2017-03-21 17:45:00 -0700 | [diff] [blame] | 895 | if (isFling) { |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 896 | mMotionHelper.flingToSnapTarget(vel.x, vel.y, |
Joshua Tsuji | 2e25246 | 2020-01-02 11:27:41 -0500 | [diff] [blame] | 897 | PipTouchHandler.this::updateDismissFraction /* updateAction */, |
| 898 | endAction /* endAction */); |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 899 | } else { |
Joshua Tsuji | c81ff3d | 2020-02-13 14:48:40 -0500 | [diff] [blame] | 900 | mMotionHelper.animateToClosestSnapTarget(); |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 901 | } |
Arthur Hung | d52d119 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 902 | } else if (mTouchState.isDoubleTap()) { |
| 903 | // Expand to fullscreen if this is a double tap |
Ben Lin | 34b6fbf | 2020-03-13 19:36:02 -0700 | [diff] [blame] | 904 | // the PiP should be frozen until the transition ends |
| 905 | setTouchEnabled(false); |
Arthur Hung | d52d119 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 906 | mMotionHelper.expandPip(); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 907 | } else if (mMenuState != MENU_STATE_FULL) { |
Arthur Hung | d52d119 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 908 | if (!mTouchState.isWaitingForDoubleTap()) { |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 909 | // User has stalled long enough for this not to be a drag or a double tap, just |
| 910 | // expand the menu |
| 911 | mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(), |
| 912 | mMovementBounds, true /* allowMenuTimeout */, willResizeMenu()); |
| 913 | } else { |
| 914 | // Next touch event _may_ be the second tap for the double-tap, schedule a |
| 915 | // fallback runnable to trigger the menu if no touch event occurs before the |
| 916 | // next tap |
| 917 | mTouchState.scheduleDoubleTapTimeoutCallback(); |
| 918 | } |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 919 | } |
| 920 | return true; |
| 921 | } |
| 922 | }; |
Mady Mellor | 3b10dcd | 2017-01-23 10:08:35 -0800 | [diff] [blame] | 923 | |
| 924 | /** |
Ben Lin | 06dc41f | 2020-03-05 00:39:27 +0000 | [diff] [blame] | 925 | * Updates the current movement bounds based on whether the menu is currently visible and |
| 926 | * resized. |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 927 | */ |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 928 | private void updateMovementBounds() { |
| 929 | mSnapAlgorithm.getMovementBounds(mMotionHelper.getBounds(), mInsetBounds, |
| 930 | mMovementBounds, mIsImeShowing ? mImeHeight : 0); |
Ben Lin | 06dc41f | 2020-03-05 00:39:27 +0000 | [diff] [blame] | 931 | mMotionHelper.setCurrentMovementBounds(mMovementBounds); |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 932 | |
| 933 | boolean isMenuExpanded = mMenuState == MENU_STATE_FULL; |
| 934 | mPipBoundsHandler.setMinEdgeSize( |
| 935 | isMenuExpanded && willResizeMenu() ? mExpandedShortestEdgeSize : 0); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 936 | } |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 937 | |
Winson Chung | b54b65b | 2017-04-26 14:02:13 -0700 | [diff] [blame] | 938 | /** |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 939 | * @return whether the menu will resize as a part of showing the full menu. |
| 940 | */ |
| 941 | private boolean willResizeMenu() { |
Ben Lin | e55f85f | 2020-01-27 14:49:41 -0800 | [diff] [blame] | 942 | if (!mEnableResize) { |
| 943 | return false; |
| 944 | } |
Ben Lin | 06dc41f | 2020-03-05 00:39:27 +0000 | [diff] [blame] | 945 | return mExpandedBounds.width() != mNormalBounds.width() |
| 946 | || mExpandedBounds.height() != mNormalBounds.height(); |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 949 | public void dump(PrintWriter pw, String prefix) { |
| 950 | final String innerPrefix = prefix + " "; |
| 951 | pw.println(prefix + TAG); |
| 952 | pw.println(innerPrefix + "mMovementBounds=" + mMovementBounds); |
Ben Lin | 06dc41f | 2020-03-05 00:39:27 +0000 | [diff] [blame] | 953 | pw.println(innerPrefix + "mNormalBounds=" + mNormalBounds); |
| 954 | pw.println(innerPrefix + "mNormalMovementBounds=" + mNormalMovementBounds); |
| 955 | pw.println(innerPrefix + "mExpandedBounds=" + mExpandedBounds); |
| 956 | pw.println(innerPrefix + "mExpandedMovementBounds=" + mExpandedMovementBounds); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 957 | pw.println(innerPrefix + "mMenuState=" + mMenuState); |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 958 | pw.println(innerPrefix + "mIsImeShowing=" + mIsImeShowing); |
| 959 | pw.println(innerPrefix + "mImeHeight=" + mImeHeight); |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 960 | pw.println(innerPrefix + "mIsShelfShowing=" + mIsShelfShowing); |
| 961 | pw.println(innerPrefix + "mShelfHeight=" + mShelfHeight); |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 962 | pw.println(innerPrefix + "mSavedSnapFraction=" + mSavedSnapFraction); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 963 | pw.println(innerPrefix + "mEnableDragToEdgeDismiss=" + mEnableDismissDragToEdge); |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 964 | mSnapAlgorithm.dump(pw, innerPrefix); |
| 965 | mTouchState.dump(pw, innerPrefix); |
| 966 | mMotionHelper.dump(pw, innerPrefix); |
| 967 | } |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 968 | |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 969 | } |