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