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 | |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 23 | import android.animation.Animator; |
| 24 | import android.animation.AnimatorListenerAdapter; |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 25 | import android.animation.ValueAnimator; |
| 26 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 27 | import android.app.IActivityManager; |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 28 | import android.app.IActivityTaskManager; |
Winson Chung | 9b91941 | 2017-06-19 17:01:51 -0700 | [diff] [blame] | 29 | import android.content.ComponentName; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 30 | import android.content.Context; |
Winson Chung | 136d1ec8 | 2017-07-18 11:16:30 -0700 | [diff] [blame] | 31 | import android.content.res.Resources; |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 32 | import android.graphics.Point; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 33 | import android.graphics.PointF; |
| 34 | import android.graphics.Rect; |
Mady Mellor | d4e40fb | 2017-01-26 10:43:16 -0800 | [diff] [blame] | 35 | import android.os.Handler; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 36 | import android.os.RemoteException; |
| 37 | import android.util.Log; |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 38 | import android.util.Size; |
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; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 42 | import android.view.ViewConfiguration; |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 43 | import android.view.accessibility.AccessibilityEvent; |
| 44 | import android.view.accessibility.AccessibilityManager; |
| 45 | import android.view.accessibility.AccessibilityNodeInfo; |
Phil Weaver | 0a8caa1 | 2017-08-09 11:28:41 -0700 | [diff] [blame] | 46 | import android.view.accessibility.AccessibilityWindowInfo; |
Chenjie Yu | ae9fdf04 | 2018-02-15 10:19:32 -0800 | [diff] [blame] | 47 | |
Chenjie Yu | 52cacc6 | 2017-12-08 18:11:45 -0800 | [diff] [blame] | 48 | import com.android.internal.os.logging.MetricsLoggerWrapper; |
Winson Chung | cd1ff64 | 2016-10-26 09:44:43 -0700 | [diff] [blame] | 49 | import com.android.internal.policy.PipSnapAlgorithm; |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 50 | import com.android.systemui.R; |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 51 | import com.android.systemui.shared.system.InputConsumerController; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 52 | import com.android.systemui.statusbar.FlingAnimationUtils; |
| 53 | |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 54 | import java.io.PrintWriter; |
| 55 | |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Manages all the touch handling for PIP on the Phone, including moving, dismissing and expanding |
| 58 | * the PIP. |
| 59 | */ |
Mady Mellor | 8c7dc42 | 2017-05-10 12:55:06 -0700 | [diff] [blame] | 60 | public class PipTouchHandler { |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 61 | private static final String TAG = "PipTouchHandler"; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 62 | |
Mady Mellor | 8c7dc42 | 2017-05-10 12:55:06 -0700 | [diff] [blame] | 63 | // Allow the PIP to be dragged to the edge of the screen to be minimized. |
| 64 | private static final boolean ENABLE_MINIMIZE = false; |
| 65 | // Allow the PIP to be flung from anywhere on the screen to the bottom to be dismissed. |
| 66 | private static final boolean ENABLE_FLING_DISMISS = false; |
Mady Mellor | 2e13878 | 2017-03-27 11:09:50 -0700 | [diff] [blame] | 67 | |
Mady Mellor | 5d58d25 | 2017-04-18 12:48:04 -0700 | [diff] [blame] | 68 | private static final int SHOW_DISMISS_AFFORDANCE_DELAY = 225; |
Tracy Zhou | e19d051 | 2018-07-31 16:29:34 -0700 | [diff] [blame] | 69 | private static final int BOTTOM_OFFSET_BUFFER_DP = 1; |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 70 | |
Winson Chung | 6e35ee1f | 2017-02-14 12:06:44 -0800 | [diff] [blame] | 71 | // Allow dragging the PIP to a location to close it |
Eliot Courtney | 3dc12f1 | 2018-06-01 14:26:26 +0900 | [diff] [blame] | 72 | private final boolean mEnableDimissDragToEdge; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 73 | private final Context mContext; |
| 74 | private final IActivityManager mActivityManager; |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 75 | private final IActivityTaskManager mActivityTaskManager; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 76 | private final ViewConfiguration mViewConfig; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 77 | private final PipMenuListener mMenuListener = new PipMenuListener(); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 78 | private IPinnedStackController mPinnedStackController; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 79 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 80 | private final PipMenuActivityController mMenuController; |
| 81 | private final PipDismissViewController mDismissViewController; |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 82 | private final PipSnapAlgorithm mSnapAlgorithm; |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 83 | private final AccessibilityManager mAccessibilityManager; |
Wale Ogunwale | 6455e50 | 2017-04-17 14:16:43 -0700 | [diff] [blame] | 84 | private boolean mShowPipMenuOnAnimationEnd = false; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 85 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 86 | // The current movement bounds |
| 87 | private Rect mMovementBounds = new Rect(); |
| 88 | |
Winson Chung | 0a2f34f | 2017-08-11 18:13:24 -0700 | [diff] [blame] | 89 | // The reference inset bounds, used to determine the dismiss fraction |
| 90 | private Rect mInsetBounds = new Rect(); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 91 | // The reference bounds used to calculate the normal/expanded target bounds |
| 92 | private Rect mNormalBounds = new Rect(); |
| 93 | private Rect mNormalMovementBounds = new Rect(); |
| 94 | private Rect mExpandedBounds = new Rect(); |
| 95 | private Rect mExpandedMovementBounds = new Rect(); |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 96 | private int mExpandedShortestEdgeSize; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 97 | |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 98 | // Used to workaround an issue where the WM rotation happens before we are notified, allowing |
| 99 | // us to send stale bounds |
| 100 | private int mDeferResizeToNormalBoundsUntilRotation = -1; |
| 101 | private int mDisplayRotation; |
| 102 | |
Mady Mellor | d4e40fb | 2017-01-26 10:43:16 -0800 | [diff] [blame] | 103 | private Handler mHandler = new Handler(); |
| 104 | private Runnable mShowDismissAffordance = new Runnable() { |
| 105 | @Override |
| 106 | public void run() { |
Eliot Courtney | 3dc12f1 | 2018-06-01 14:26:26 +0900 | [diff] [blame] | 107 | if (mEnableDimissDragToEdge) { |
Mady Mellor | 60421c9 | 2017-03-29 15:27:37 -0700 | [diff] [blame] | 108 | mDismissViewController.showDismissTarget(); |
Mady Mellor | d4e40fb | 2017-01-26 10:43:16 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | }; |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 112 | private ValueAnimator.AnimatorUpdateListener mUpdateScrimListener = |
| 113 | new AnimatorUpdateListener() { |
| 114 | @Override |
| 115 | public void onAnimationUpdate(ValueAnimator animation) { |
| 116 | updateDismissFraction(); |
| 117 | } |
| 118 | }; |
Mady Mellor | d4e40fb | 2017-01-26 10:43:16 -0800 | [diff] [blame] | 119 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 120 | // Behaviour states |
Winson Chung | 9b91941 | 2017-06-19 17:01:51 -0700 | [diff] [blame] | 121 | private int mMenuState = MENU_STATE_NONE; |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 122 | private boolean mIsMinimized; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 123 | private boolean mIsImeShowing; |
| 124 | private int mImeHeight; |
Winson Chung | 136d1ec8 | 2017-07-18 11:16:30 -0700 | [diff] [blame] | 125 | private int mImeOffset; |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 126 | private boolean mIsShelfShowing; |
| 127 | private int mShelfHeight; |
Winson Chung | dec1a66 | 2019-06-04 21:20:41 -0700 | [diff] [blame] | 128 | private int mMovementBoundsExtraOffsets; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 129 | private float mSavedSnapFraction = -1f; |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 130 | private boolean mSendingHoverAccessibilityEvents; |
Mady Mellor | 2fbdd3b | 2017-03-21 17:45:00 -0700 | [diff] [blame] | 131 | private boolean mMovementWithinMinimize; |
| 132 | private boolean mMovementWithinDismiss; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 133 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 134 | // Touch state |
| 135 | private final PipTouchState mTouchState; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 136 | private final FlingAnimationUtils mFlingAnimationUtils; |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 137 | private final PipTouchGesture[] mGestures; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 138 | private final PipMotionHelper mMotionHelper; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 139 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 140 | // Temp vars |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 141 | private final Rect mTmpBounds = new Rect(); |
| 142 | |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 143 | /** |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 144 | * A listener for the PIP menu activity. |
| 145 | */ |
| 146 | private class PipMenuListener implements PipMenuActivityController.Listener { |
| 147 | @Override |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 148 | public void onPipMenuStateChanged(int menuState, boolean resize) { |
| 149 | setMenuState(menuState, resize); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 150 | } |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 151 | |
| 152 | @Override |
| 153 | public void onPipExpand() { |
| 154 | if (!mIsMinimized) { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 155 | mMotionHelper.expandPip(); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void onPipMinimize() { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 161 | setMinimizedStateInternal(true); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 162 | mMotionHelper.animateToClosestMinimizedState(mMovementBounds, null /* updateListener */); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void onPipDismiss() { |
Chenjie Yu | ae9fdf04 | 2018-02-15 10:19:32 -0800 | [diff] [blame] | 167 | MetricsLoggerWrapper.logPictureInPictureDismissByTap(mContext, |
| 168 | PipUtils.getTopPinnedActivity(mContext, mActivityManager)); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 169 | mMotionHelper.dismissPip(); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 170 | } |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 171 | |
| 172 | @Override |
| 173 | public void onPipShowMenu() { |
| 174 | mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(), |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 175 | mMovementBounds, true /* allowMenuTimeout */, willResizeMenu()); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 176 | } |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 179 | public PipTouchHandler(Context context, IActivityManager activityManager, |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 180 | IActivityTaskManager activityTaskManager, PipMenuActivityController menuController, |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 181 | InputConsumerController inputConsumerController) { |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 182 | |
| 183 | // Initialize the Pip input consumer |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 184 | mContext = context; |
| 185 | mActivityManager = activityManager; |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 186 | mActivityTaskManager = activityTaskManager; |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 187 | mAccessibilityManager = context.getSystemService(AccessibilityManager.class); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 188 | mViewConfig = ViewConfiguration.get(context); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 189 | mMenuController = menuController; |
| 190 | mMenuController.addListener(mMenuListener); |
| 191 | mDismissViewController = new PipDismissViewController(context); |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 192 | mSnapAlgorithm = new PipSnapAlgorithm(mContext); |
Mady Mellor | 15b29c7 | 2017-06-07 14:53:58 -0700 | [diff] [blame] | 193 | mFlingAnimationUtils = new FlingAnimationUtils(context, 2.5f); |
Winson Chung | a5acf18 | 2017-01-05 16:02:27 -0800 | [diff] [blame] | 194 | mGestures = new PipTouchGesture[] { |
Mady Mellor | d4e40fb | 2017-01-26 10:43:16 -0800 | [diff] [blame] | 195 | mDefaultMovementGesture |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 196 | }; |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 197 | mMotionHelper = new PipMotionHelper(mContext, mActivityManager, mActivityTaskManager, |
| 198 | mMenuController, mSnapAlgorithm, mFlingAnimationUtils); |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 199 | mTouchState = new PipTouchState(mViewConfig, mHandler, |
| 200 | () -> mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(), |
| 201 | mMovementBounds, true /* allowMenuTimeout */, willResizeMenu())); |
Winson Chung | 136d1ec8 | 2017-07-18 11:16:30 -0700 | [diff] [blame] | 202 | |
| 203 | Resources res = context.getResources(); |
| 204 | mExpandedShortestEdgeSize = res.getDimensionPixelSize( |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 205 | R.dimen.pip_expanded_shortest_edge_size); |
Winson Chung | 136d1ec8 | 2017-07-18 11:16:30 -0700 | [diff] [blame] | 206 | mImeOffset = res.getDimensionPixelSize(R.dimen.pip_ime_offset); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 207 | |
Eliot Courtney | 3dc12f1 | 2018-06-01 14:26:26 +0900 | [diff] [blame] | 208 | mEnableDimissDragToEdge = res.getBoolean(R.bool.config_pipEnableDismissDragToEdge); |
| 209 | |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 210 | // Register the listener for input consumer touch events |
Tracy Zhou | 535c212 | 2019-02-21 17:02:47 -0800 | [diff] [blame] | 211 | inputConsumerController.setInputListener(this::handleTouchEvent); |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 212 | inputConsumerController.setRegistrationListener(this::onRegistrationChanged); |
| 213 | onRegistrationChanged(inputConsumerController.isRegistered()); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 216 | public void setTouchEnabled(boolean enabled) { |
| 217 | mTouchState.setAllowTouches(enabled); |
| 218 | } |
| 219 | |
Winson Chung | ac52f28 | 2017-03-30 14:44:52 -0700 | [diff] [blame] | 220 | public void showPictureInPictureMenu() { |
| 221 | // Only show the menu if the user isn't currently interacting with the PiP |
| 222 | if (!mTouchState.isUserInteracting()) { |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 223 | mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(), |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 224 | mMovementBounds, false /* allowMenuTimeout */, willResizeMenu()); |
Winson Chung | ac52f28 | 2017-03-30 14:44:52 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 228 | public void onActivityPinned() { |
Winson Chung | 9b91941 | 2017-06-19 17:01:51 -0700 | [diff] [blame] | 229 | cleanUp(); |
Wale Ogunwale | 6455e50 | 2017-04-17 14:16:43 -0700 | [diff] [blame] | 230 | mShowPipMenuOnAnimationEnd = true; |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Winson Chung | 9b91941 | 2017-06-19 17:01:51 -0700 | [diff] [blame] | 233 | public void onActivityUnpinned(ComponentName topPipActivity) { |
| 234 | if (topPipActivity == null) { |
| 235 | // Clean up state after the last PiP activity is removed |
| 236 | cleanUp(); |
| 237 | } |
| 238 | } |
| 239 | |
Winson Chung | ac52f28 | 2017-03-30 14:44:52 -0700 | [diff] [blame] | 240 | public void onPinnedStackAnimationEnded() { |
| 241 | // Always synchronize the motion helper bounds once PiP animations finish |
| 242 | mMotionHelper.synchronizePinnedStackBounds(); |
Wale Ogunwale | 6455e50 | 2017-04-17 14:16:43 -0700 | [diff] [blame] | 243 | |
| 244 | if (mShowPipMenuOnAnimationEnd) { |
| 245 | mMenuController.showMenu(MENU_STATE_CLOSE, mMotionHelper.getBounds(), |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 246 | mMovementBounds, true /* allowMenuTimeout */, false /* willResizeMenu */); |
Wale Ogunwale | 6455e50 | 2017-04-17 14:16:43 -0700 | [diff] [blame] | 247 | mShowPipMenuOnAnimationEnd = false; |
| 248 | } |
Winson Chung | ac52f28 | 2017-03-30 14:44:52 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Winson Chung | 303c6b7 | 2016-10-24 17:12:49 -0700 | [diff] [blame] | 251 | public void onConfigurationChanged() { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 252 | mMotionHelper.onConfigurationChanged(); |
| 253 | mMotionHelper.synchronizePinnedStackBounds(); |
Winson Chung | 303c6b7 | 2016-10-24 17:12:49 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 256 | public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) { |
| 257 | mIsImeShowing = imeVisible; |
| 258 | mImeHeight = imeHeight; |
| 259 | } |
| 260 | |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 261 | public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight) { |
| 262 | mIsShelfShowing = shelfVisible; |
| 263 | mShelfHeight = shelfHeight; |
| 264 | } |
| 265 | |
Winson Chung | dec1a66 | 2019-06-04 21:20:41 -0700 | [diff] [blame] | 266 | public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds, Rect curBounds, |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 267 | boolean fromImeAdjustment, boolean fromShelfAdjustment, int displayRotation) { |
| 268 | final int bottomOffset = mIsImeShowing ? mImeHeight : 0; |
| 269 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 270 | // Re-calculate the expanded bounds |
| 271 | mNormalBounds = normalBounds; |
| 272 | Rect normalMovementBounds = new Rect(); |
| 273 | mSnapAlgorithm.getMovementBounds(mNormalBounds, insetBounds, normalMovementBounds, |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 274 | bottomOffset); |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 275 | |
| 276 | // Calculate the expanded size |
| 277 | float aspectRatio = (float) normalBounds.width() / normalBounds.height(); |
| 278 | Point displaySize = new Point(); |
| 279 | mContext.getDisplay().getRealSize(displaySize); |
| 280 | Size expandedSize = mSnapAlgorithm.getSizeForAspectRatio(aspectRatio, |
| 281 | mExpandedShortestEdgeSize, displaySize.x, displaySize.y); |
| 282 | mExpandedBounds.set(0, 0, expandedSize.getWidth(), expandedSize.getHeight()); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 283 | Rect expandedMovementBounds = new Rect(); |
| 284 | mSnapAlgorithm.getMovementBounds(mExpandedBounds, insetBounds, expandedMovementBounds, |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 285 | bottomOffset); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 286 | |
Winson Chung | dec1a66 | 2019-06-04 21:20:41 -0700 | [diff] [blame] | 287 | // The extra offset does not really affect the movement bounds, but are applied based on the |
| 288 | // current state (ime showing, or shelf offset) when we need to actually shift |
| 289 | int extraOffset = Math.max( |
| 290 | mIsImeShowing ? mImeOffset : 0, |
| 291 | !mIsImeShowing && mIsShelfShowing ? mShelfHeight : 0); |
| 292 | |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 293 | // If this is from an IME or shelf adjustment, then we should move the PiP so that it is not |
| 294 | // occluded by the IME or shelf. |
| 295 | if (fromImeAdjustment || fromShelfAdjustment) { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 296 | if (mTouchState.isUserInteracting()) { |
| 297 | // Defer the update of the current movement bounds until after the user finishes |
| 298 | // touching the screen |
| 299 | } else { |
Winson Chung | dec1a66 | 2019-06-04 21:20:41 -0700 | [diff] [blame] | 300 | final float offsetBufferPx = BOTTOM_OFFSET_BUFFER_DP |
Tracy Zhou | e19d051 | 2018-07-31 16:29:34 -0700 | [diff] [blame] | 301 | * mContext.getResources().getDisplayMetrics().density; |
Winson Chung | dec1a66 | 2019-06-04 21:20:41 -0700 | [diff] [blame] | 302 | final Rect toMovementBounds = mMenuState == MENU_STATE_FULL |
| 303 | ? new Rect(expandedMovementBounds) |
| 304 | : new Rect(normalMovementBounds); |
| 305 | final int prevBottom = mMovementBounds.bottom - mMovementBoundsExtraOffsets; |
| 306 | final int toBottom = toMovementBounds.bottom < toMovementBounds.top |
| 307 | ? toMovementBounds.bottom |
| 308 | : toMovementBounds.bottom - extraOffset; |
| 309 | if ((Math.min(prevBottom, toBottom) - offsetBufferPx) <= curBounds.top |
| 310 | && curBounds.top <= (Math.max(prevBottom, toBottom) + offsetBufferPx)) { |
| 311 | mMotionHelper.animateToOffset(curBounds, toBottom - curBounds.top); |
Tracy Zhou | 805e66c | 2018-04-04 23:45:26 -0700 | [diff] [blame] | 312 | } |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 313 | } |
Winson Chung | 14fbe14 | 2016-12-19 16:18:24 -0800 | [diff] [blame] | 314 | } |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 315 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 316 | // Update the movement bounds after doing the calculations based on the old movement bounds |
| 317 | // above |
| 318 | mNormalMovementBounds = normalMovementBounds; |
| 319 | mExpandedMovementBounds = expandedMovementBounds; |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 320 | mDisplayRotation = displayRotation; |
Winson Chung | 0a2f34f | 2017-08-11 18:13:24 -0700 | [diff] [blame] | 321 | mInsetBounds.set(insetBounds); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 322 | updateMovementBounds(mMenuState); |
Winson Chung | dec1a66 | 2019-06-04 21:20:41 -0700 | [diff] [blame] | 323 | mMovementBoundsExtraOffsets = extraOffset; |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 324 | |
| 325 | // If we have a deferred resize, apply it now |
| 326 | if (mDeferResizeToNormalBoundsUntilRotation == displayRotation) { |
| 327 | mMotionHelper.animateToUnexpandedState(normalBounds, mSavedSnapFraction, |
| 328 | mNormalMovementBounds, mMovementBounds, mIsMinimized, |
| 329 | true /* immediate */); |
| 330 | mSavedSnapFraction = -1f; |
| 331 | mDeferResizeToNormalBoundsUntilRotation = -1; |
| 332 | } |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 335 | private void onRegistrationChanged(boolean isRegistered) { |
| 336 | mAccessibilityManager.setPictureInPictureActionReplacingConnection(isRegistered |
Winson Chung | fe1fa64 | 2017-03-13 10:51:22 -0700 | [diff] [blame] | 337 | ? new PipAccessibilityInteractionConnection(mMotionHelper, |
| 338 | this::onAccessibilityShowMenu, mHandler) : null); |
Winson Chung | b54b65b | 2017-04-26 14:02:13 -0700 | [diff] [blame] | 339 | |
| 340 | if (!isRegistered && mTouchState.isUserInteracting()) { |
| 341 | // If the input consumer is unregistered while the user is interacting, then we may not |
| 342 | // get the final TOUCH_UP event, so clean up the dismiss target as well |
| 343 | cleanUpDismissTarget(); |
| 344 | } |
Winson Chung | fe1fa64 | 2017-03-13 10:51:22 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | private void onAccessibilityShowMenu() { |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 348 | mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(), |
Rhed Jao | 5e00c98 | 2018-11-27 19:41:05 +0800 | [diff] [blame] | 349 | mMovementBounds, true /* allowMenuTimeout */, willResizeMenu()); |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Tracy Zhou | 535c212 | 2019-02-21 17:02:47 -0800 | [diff] [blame] | 352 | private boolean handleTouchEvent(InputEvent inputEvent) { |
| 353 | // Skip any non motion events |
| 354 | if (!(inputEvent instanceof MotionEvent)) { |
| 355 | return true; |
| 356 | } |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 357 | // Skip touch handling until we are bound to the controller |
| 358 | if (mPinnedStackController == null) { |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 359 | return true; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 360 | } |
Tracy Zhou | 535c212 | 2019-02-21 17:02:47 -0800 | [diff] [blame] | 361 | MotionEvent ev = (MotionEvent) inputEvent; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 362 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 363 | // Update the touch state |
| 364 | mTouchState.onTouchEvent(ev); |
| 365 | |
Arthur Hung | 0ede6e9 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 366 | boolean shouldDeliverToMenu = mMenuState != MENU_STATE_NONE; |
| 367 | |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 368 | switch (ev.getAction()) { |
| 369 | case MotionEvent.ACTION_DOWN: { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 370 | mMotionHelper.synchronizePinnedStackBounds(); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 371 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 372 | for (PipTouchGesture gesture : mGestures) { |
| 373 | gesture.onDown(mTouchState); |
| 374 | } |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 375 | break; |
| 376 | } |
| 377 | case MotionEvent.ACTION_MOVE: { |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 378 | for (PipTouchGesture gesture : mGestures) { |
| 379 | if (gesture.onMove(mTouchState)) { |
| 380 | break; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
Arthur Hung | 0ede6e9 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 383 | |
| 384 | shouldDeliverToMenu = !mTouchState.isDragging(); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 385 | break; |
| 386 | } |
| 387 | case MotionEvent.ACTION_UP: { |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 388 | // Update the movement bounds again if the state has changed since the user started |
| 389 | // dragging (ie. when the IME shows) |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 390 | updateMovementBounds(mMenuState); |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 391 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 392 | for (PipTouchGesture gesture : mGestures) { |
| 393 | if (gesture.onUp(mTouchState)) { |
| 394 | break; |
Winson Chung | 5cd26ff | 2016-10-24 11:50:44 -0700 | [diff] [blame] | 395 | } |
Winson Chung | 5cd26ff | 2016-10-24 11:50:44 -0700 | [diff] [blame] | 396 | } |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 397 | |
| 398 | // Fall through to clean up |
| 399 | } |
| 400 | case MotionEvent.ACTION_CANCEL: { |
Arthur Hung | 0ede6e9 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 401 | shouldDeliverToMenu = !mTouchState.startedDragging() && !mTouchState.isDragging(); |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 402 | mTouchState.reset(); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 403 | break; |
| 404 | } |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 405 | case MotionEvent.ACTION_HOVER_ENTER: |
| 406 | case MotionEvent.ACTION_HOVER_MOVE: { |
Eugene Susla | d4128ec | 2017-12-04 19:48:41 +0000 | [diff] [blame] | 407 | if (mAccessibilityManager.isEnabled() && !mSendingHoverAccessibilityEvents) { |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 408 | AccessibilityEvent event = AccessibilityEvent.obtain( |
| 409 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER); |
Phil Weaver | 0a8caa1 | 2017-08-09 11:28:41 -0700 | [diff] [blame] | 410 | event.setImportantForAccessibility(true); |
Phil Weaver | 651fe9f | 2017-05-24 16:43:46 -0700 | [diff] [blame] | 411 | event.setSourceNodeId(AccessibilityNodeInfo.ROOT_NODE_ID); |
Phil Weaver | 0a8caa1 | 2017-08-09 11:28:41 -0700 | [diff] [blame] | 412 | event.setWindowId( |
| 413 | AccessibilityWindowInfo.PICTURE_IN_PICTURE_ACTION_REPLACER_WINDOW_ID); |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 414 | mAccessibilityManager.sendAccessibilityEvent(event); |
| 415 | mSendingHoverAccessibilityEvents = true; |
| 416 | } |
| 417 | break; |
| 418 | } |
| 419 | case MotionEvent.ACTION_HOVER_EXIT: { |
Eugene Susla | d4128ec | 2017-12-04 19:48:41 +0000 | [diff] [blame] | 420 | if (mAccessibilityManager.isEnabled() && mSendingHoverAccessibilityEvents) { |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 421 | AccessibilityEvent event = AccessibilityEvent.obtain( |
| 422 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT); |
Phil Weaver | 0a8caa1 | 2017-08-09 11:28:41 -0700 | [diff] [blame] | 423 | event.setImportantForAccessibility(true); |
Phil Weaver | 651fe9f | 2017-05-24 16:43:46 -0700 | [diff] [blame] | 424 | event.setSourceNodeId(AccessibilityNodeInfo.ROOT_NODE_ID); |
Phil Weaver | 0a8caa1 | 2017-08-09 11:28:41 -0700 | [diff] [blame] | 425 | event.setWindowId( |
| 426 | AccessibilityWindowInfo.PICTURE_IN_PICTURE_ACTION_REPLACER_WINDOW_ID); |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 427 | mAccessibilityManager.sendAccessibilityEvent(event); |
| 428 | mSendingHoverAccessibilityEvents = false; |
| 429 | } |
| 430 | break; |
| 431 | } |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 432 | } |
Arthur Hung | 0ede6e9 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 433 | |
| 434 | // Deliver the event to PipMenuActivity to handle button click if the menu has shown. |
| 435 | if (shouldDeliverToMenu) { |
| 436 | final MotionEvent cloneEvent = MotionEvent.obtain(ev); |
| 437 | // Send the cancel event and cancel menu timeout if it starts to drag. |
| 438 | if (mTouchState.startedDragging()) { |
| 439 | cloneEvent.setAction(MotionEvent.ACTION_CANCEL); |
| 440 | mMenuController.pokeMenu(); |
| 441 | } |
| 442 | |
| 443 | mMenuController.handleTouchEvent(cloneEvent); |
| 444 | } |
| 445 | |
| 446 | return true; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /** |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 450 | * Updates the appearance of the menu and scrim on top of the PiP while dismissing. |
| 451 | */ |
Winson Chung | 8ec0e16 | 2017-07-07 14:49:49 -0700 | [diff] [blame] | 452 | private void updateDismissFraction() { |
Winson Chung | 0a2f34f | 2017-08-11 18:13:24 -0700 | [diff] [blame] | 453 | // Skip updating the dismiss fraction when the IME is showing. This is to work around an |
| 454 | // issue where starting the menu activity for the dismiss overlay will steal the window |
| 455 | // focus, which closes the IME. |
| 456 | if (mMenuController != null && !mIsImeShowing) { |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 457 | Rect bounds = mMotionHelper.getBounds(); |
Winson Chung | 0a2f34f | 2017-08-11 18:13:24 -0700 | [diff] [blame] | 458 | final float target = mInsetBounds.bottom; |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 459 | float fraction = 0f; |
| 460 | if (bounds.bottom > target) { |
| 461 | final float distance = bounds.bottom - target; |
| 462 | fraction = Math.min(distance / bounds.height(), 1f); |
| 463 | } |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 464 | if (Float.compare(fraction, 0f) != 0 || mMenuController.isMenuActivityVisible()) { |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 465 | // Update if the fraction > 0, or if fraction == 0 and the menu was already visible |
| 466 | mMenuController.setDismissFraction(fraction); |
| 467 | } |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
| 471 | /** |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 472 | * Sets the controller to update the system of changes from user interaction. |
| 473 | */ |
| 474 | void setPinnedStackController(IPinnedStackController controller) { |
| 475 | mPinnedStackController = controller; |
| 476 | } |
| 477 | |
| 478 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 479 | * Sets the minimized state. |
Winson Chung | dff5c08 | 2016-11-02 17:28:03 -0700 | [diff] [blame] | 480 | */ |
Winson Chung | 8ec0e16 | 2017-07-07 14:49:49 -0700 | [diff] [blame] | 481 | private void setMinimizedStateInternal(boolean isMinimized) { |
Mady Mellor | 8c7dc42 | 2017-05-10 12:55:06 -0700 | [diff] [blame] | 482 | if (!ENABLE_MINIMIZE) { |
Mady Mellor | 2e13878 | 2017-03-27 11:09:50 -0700 | [diff] [blame] | 483 | return; |
| 484 | } |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 485 | setMinimizedState(isMinimized, false /* fromController */); |
Winson Chung | dff5c08 | 2016-11-02 17:28:03 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 489 | * Sets the minimized state. |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 490 | */ |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 491 | void setMinimizedState(boolean isMinimized, boolean fromController) { |
Mady Mellor | 8c7dc42 | 2017-05-10 12:55:06 -0700 | [diff] [blame] | 492 | if (!ENABLE_MINIMIZE) { |
Mady Mellor | 2e13878 | 2017-03-27 11:09:50 -0700 | [diff] [blame] | 493 | return; |
| 494 | } |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 495 | if (mIsMinimized != isMinimized) { |
Chenjie Yu | ae9fdf04 | 2018-02-15 10:19:32 -0800 | [diff] [blame] | 496 | MetricsLoggerWrapper.logPictureInPictureMinimize(mContext, |
| 497 | isMinimized, PipUtils.getTopPinnedActivity(mContext, mActivityManager)); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 498 | } |
| 499 | mIsMinimized = isMinimized; |
| 500 | mSnapAlgorithm.setMinimized(isMinimized); |
Winson Chung | 54f0c65 | 2016-12-06 14:46:31 -0800 | [diff] [blame] | 501 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 502 | if (fromController) { |
| 503 | if (isMinimized) { |
| 504 | // Move the PiP to the new bounds immediately if minimized |
| 505 | mMotionHelper.movePip(mMotionHelper.getClosestMinimizedBounds(mNormalBounds, |
| 506 | mMovementBounds)); |
| 507 | } |
| 508 | } else if (mPinnedStackController != null) { |
Winson Chung | 54f0c65 | 2016-12-06 14:46:31 -0800 | [diff] [blame] | 509 | try { |
| 510 | mPinnedStackController.setIsMinimized(isMinimized); |
| 511 | } catch (RemoteException e) { |
| 512 | Log.e(TAG, "Could not set minimized state", e); |
| 513 | } |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
| 517 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 518 | * Sets the menu visibility. |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 519 | */ |
Winson Chung | 8ec0e16 | 2017-07-07 14:49:49 -0700 | [diff] [blame] | 520 | private void setMenuState(int menuState, boolean resize) { |
Winson Chung | a89053d | 2018-06-12 16:47:30 -0700 | [diff] [blame] | 521 | if (menuState == MENU_STATE_FULL && mMenuState != MENU_STATE_FULL) { |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 522 | // Save the current snap fraction and if we do not drag or move the PiP, then |
| 523 | // we store back to this snap fraction. Otherwise, we'll reset the snap |
| 524 | // fraction and snap to the closest edge |
| 525 | Rect expandedBounds = new Rect(mExpandedBounds); |
| 526 | if (resize) { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 527 | mSavedSnapFraction = mMotionHelper.animateToExpandedState(expandedBounds, |
| 528 | mMovementBounds, mExpandedMovementBounds); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 529 | } |
Winson Chung | a89053d | 2018-06-12 16:47:30 -0700 | [diff] [blame] | 530 | } else if (menuState == MENU_STATE_NONE && mMenuState == MENU_STATE_FULL) { |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 531 | // Try and restore the PiP to the closest edge, using the saved snap fraction |
| 532 | // if possible |
| 533 | if (resize) { |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 534 | if (mDeferResizeToNormalBoundsUntilRotation == -1) { |
Winson Chung | bb23376 | 2017-05-15 14:20:46 -0700 | [diff] [blame] | 535 | // This is a very special case: when the menu is expanded and visible, |
| 536 | // navigating to another activity can trigger auto-enter PiP, and if the |
| 537 | // revealed activity has a forced rotation set, then the controller will get |
| 538 | // updated with the new rotation of the display. However, at the same time, |
| 539 | // SystemUI will try to hide the menu by creating an animation to the normal |
| 540 | // bounds which are now stale. In such a case we defer the animation to the |
| 541 | // normal bounds until after the next onMovementBoundsChanged() call to get the |
| 542 | // bounds in the new orientation |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 543 | try { |
| 544 | int displayRotation = mPinnedStackController.getDisplayRotation(); |
| 545 | if (mDisplayRotation != displayRotation) { |
| 546 | mDeferResizeToNormalBoundsUntilRotation = displayRotation; |
| 547 | } |
| 548 | } catch (RemoteException e) { |
| 549 | Log.e(TAG, "Could not get display rotation from controller"); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | if (mDeferResizeToNormalBoundsUntilRotation == -1) { |
| 554 | Rect normalBounds = new Rect(mNormalBounds); |
| 555 | mMotionHelper.animateToUnexpandedState(normalBounds, mSavedSnapFraction, |
| 556 | mNormalMovementBounds, mMovementBounds, mIsMinimized, |
| 557 | false /* immediate */); |
| 558 | mSavedSnapFraction = -1f; |
| 559 | } |
| 560 | } else { |
Winson Chung | bb23376 | 2017-05-15 14:20:46 -0700 | [diff] [blame] | 561 | // If resizing is not allowed, then the PiP should be frozen until the transition |
| 562 | // ends as well |
| 563 | setTouchEnabled(false); |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 564 | mSavedSnapFraction = -1f; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 565 | } |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 566 | } |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 567 | mMenuState = menuState; |
| 568 | updateMovementBounds(menuState); |
| 569 | if (menuState != MENU_STATE_CLOSE) { |
Chenjie Yu | 52cacc6 | 2017-12-08 18:11:45 -0800 | [diff] [blame] | 570 | MetricsLoggerWrapper.logPictureInPictureMenuVisible(mContext, menuState == MENU_STATE_FULL); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 571 | } |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 575 | * @return the motion helper. |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 576 | */ |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 577 | public PipMotionHelper getMotionHelper() { |
| 578 | return mMotionHelper; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 579 | } |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 580 | |
| 581 | /** |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 582 | * Gesture controlling normal movement of the PIP. |
| 583 | */ |
| 584 | private PipTouchGesture mDefaultMovementGesture = new PipTouchGesture() { |
Mady Mellor | 2fbdd3b | 2017-03-21 17:45:00 -0700 | [diff] [blame] | 585 | // Whether the PiP was on the left side of the screen at the start of the gesture |
| 586 | private boolean mStartedOnLeft; |
Winson Chung | 8ec0e16 | 2017-07-07 14:49:49 -0700 | [diff] [blame] | 587 | private final Point mStartPosition = new Point(); |
| 588 | private final PointF mDelta = new PointF(); |
Mady Mellor | d4e40fb | 2017-01-26 10:43:16 -0800 | [diff] [blame] | 589 | |
| 590 | @Override |
| 591 | public void onDown(PipTouchState touchState) { |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 592 | if (!touchState.isUserInteracting()) { |
| 593 | return; |
| 594 | } |
| 595 | |
Mady Mellor | 15b29c7 | 2017-06-07 14:53:58 -0700 | [diff] [blame] | 596 | Rect bounds = mMotionHelper.getBounds(); |
Winson Chung | 8ec0e16 | 2017-07-07 14:49:49 -0700 | [diff] [blame] | 597 | mDelta.set(0f, 0f); |
| 598 | mStartPosition.set(bounds.left, bounds.top); |
Mady Mellor | 15b29c7 | 2017-06-07 14:53:58 -0700 | [diff] [blame] | 599 | mStartedOnLeft = bounds.left < mMovementBounds.centerX(); |
Mady Mellor | 2fbdd3b | 2017-03-21 17:45:00 -0700 | [diff] [blame] | 600 | mMovementWithinMinimize = true; |
| 601 | mMovementWithinDismiss = touchState.getDownTouchPosition().y >= mMovementBounds.bottom; |
| 602 | |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 603 | // If the menu is still visible, and we aren't minimized, then just poke the menu |
| 604 | // so that it will timeout after the user stops touching it |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 605 | if (mMenuState != MENU_STATE_NONE && !mIsMinimized) { |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 606 | mMenuController.pokeMenu(); |
| 607 | } |
| 608 | |
Eliot Courtney | 3dc12f1 | 2018-06-01 14:26:26 +0900 | [diff] [blame] | 609 | if (mEnableDimissDragToEdge) { |
Mady Mellor | d4e40fb | 2017-01-26 10:43:16 -0800 | [diff] [blame] | 610 | mDismissViewController.createDismissTarget(); |
| 611 | mHandler.postDelayed(mShowDismissAffordance, SHOW_DISMISS_AFFORDANCE_DELAY); |
| 612 | } |
| 613 | } |
| 614 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 615 | @Override |
| 616 | boolean onMove(PipTouchState touchState) { |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 617 | if (!touchState.isUserInteracting()) { |
| 618 | return false; |
| 619 | } |
| 620 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 621 | if (touchState.startedDragging()) { |
| 622 | mSavedSnapFraction = -1f; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 623 | |
Eliot Courtney | 3dc12f1 | 2018-06-01 14:26:26 +0900 | [diff] [blame] | 624 | if (mEnableDimissDragToEdge) { |
Winson Chung | b54b65b | 2017-04-26 14:02:13 -0700 | [diff] [blame] | 625 | mHandler.removeCallbacks(mShowDismissAffordance); |
| 626 | mDismissViewController.showDismissTarget(); |
| 627 | } |
Mady Mellor | d4e40fb | 2017-01-26 10:43:16 -0800 | [diff] [blame] | 628 | } |
| 629 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 630 | if (touchState.isDragging()) { |
| 631 | // Move the pinned stack freely |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 632 | final PointF lastDelta = touchState.getLastTouchDelta(); |
Winson Chung | 8ec0e16 | 2017-07-07 14:49:49 -0700 | [diff] [blame] | 633 | float lastX = mStartPosition.x + mDelta.x; |
| 634 | float lastY = mStartPosition.y + mDelta.y; |
| 635 | float left = lastX + lastDelta.x; |
| 636 | float top = lastY + lastDelta.y; |
Mady Mellor | 8c7dc42 | 2017-05-10 12:55:06 -0700 | [diff] [blame] | 637 | if (!touchState.allowDraggingOffscreen() || !ENABLE_MINIMIZE) { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 638 | left = Math.max(mMovementBounds.left, Math.min(mMovementBounds.right, left)); |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 639 | } |
Eliot Courtney | 3dc12f1 | 2018-06-01 14:26:26 +0900 | [diff] [blame] | 640 | if (mEnableDimissDragToEdge) { |
Mady Mellor | 57d2255 | 2017-03-09 15:37:13 -0800 | [diff] [blame] | 641 | // Allow pip to move past bottom bounds |
| 642 | top = Math.max(mMovementBounds.top, top); |
| 643 | } else { |
| 644 | top = Math.max(mMovementBounds.top, Math.min(mMovementBounds.bottom, top)); |
| 645 | } |
Winson Chung | 8ec0e16 | 2017-07-07 14:49:49 -0700 | [diff] [blame] | 646 | |
| 647 | // Add to the cumulative delta after bounding the position |
| 648 | mDelta.x += left - lastX; |
| 649 | mDelta.y += top - lastY; |
| 650 | |
| 651 | mTmpBounds.set(mMotionHelper.getBounds()); |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 652 | mTmpBounds.offsetTo((int) left, (int) top); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 653 | mMotionHelper.movePip(mTmpBounds); |
| 654 | |
Eliot Courtney | 3dc12f1 | 2018-06-01 14:26:26 +0900 | [diff] [blame] | 655 | if (mEnableDimissDragToEdge) { |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 656 | updateDismissFraction(); |
| 657 | } |
Mady Mellor | 2fbdd3b | 2017-03-21 17:45:00 -0700 | [diff] [blame] | 658 | |
| 659 | final PointF curPos = touchState.getLastTouchPosition(); |
| 660 | if (mMovementWithinMinimize) { |
| 661 | // Track if movement remains near starting edge to identify swipes to minimize |
| 662 | mMovementWithinMinimize = mStartedOnLeft |
| 663 | ? curPos.x <= mMovementBounds.left + mTmpBounds.width() |
| 664 | : curPos.x >= mMovementBounds.right; |
| 665 | } |
| 666 | if (mMovementWithinDismiss) { |
| 667 | // Track if movement remains near the bottom edge to identify swipe to dismiss |
| 668 | mMovementWithinDismiss = curPos.y >= mMovementBounds.bottom; |
| 669 | } |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 670 | return true; |
| 671 | } |
| 672 | return false; |
| 673 | } |
| 674 | |
| 675 | @Override |
| 676 | public boolean onUp(PipTouchState touchState) { |
Eliot Courtney | 3dc12f1 | 2018-06-01 14:26:26 +0900 | [diff] [blame] | 677 | if (mEnableDimissDragToEdge) { |
Winson Chung | b54b65b | 2017-04-26 14:02:13 -0700 | [diff] [blame] | 678 | // Clean up the dismiss target regardless of the touch state in case the touch |
| 679 | // enabled state changes while the user is interacting |
| 680 | cleanUpDismissTarget(); |
| 681 | } |
| 682 | |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 683 | if (!touchState.isUserInteracting()) { |
| 684 | return false; |
| 685 | } |
| 686 | |
Mady Mellor | 60421c9 | 2017-03-29 15:27:37 -0700 | [diff] [blame] | 687 | final PointF vel = touchState.getVelocity(); |
| 688 | final boolean isHorizontal = Math.abs(vel.x) > Math.abs(vel.y); |
| 689 | final float velocity = PointF.length(vel.x, vel.y); |
| 690 | final boolean isFling = velocity > mFlingAnimationUtils.getMinVelocityPxPerSecond(); |
Mady Mellor | 8c7dc42 | 2017-05-10 12:55:06 -0700 | [diff] [blame] | 691 | final boolean isUpWithinDimiss = ENABLE_FLING_DISMISS |
Mady Mellor | 47ba140 | 2017-04-04 17:25:43 -0700 | [diff] [blame] | 692 | && touchState.getLastTouchPosition().y >= mMovementBounds.bottom |
| 693 | && mMotionHelper.isGestureToDismissArea(mMotionHelper.getBounds(), vel.x, |
| 694 | vel.y, isFling); |
| 695 | final boolean isFlingToBot = isFling && vel.y > 0 && !isHorizontal |
| 696 | && (mMovementWithinDismiss || isUpWithinDimiss); |
Eliot Courtney | 3dc12f1 | 2018-06-01 14:26:26 +0900 | [diff] [blame] | 697 | if (mEnableDimissDragToEdge) { |
Winson Chung | b54b65b | 2017-04-26 14:02:13 -0700 | [diff] [blame] | 698 | // Check if the user dragged or flung the PiP offscreen to dismiss it |
| 699 | if (mMotionHelper.shouldDismissPip() || isFlingToBot) { |
Chenjie Yu | ae9fdf04 | 2018-02-15 10:19:32 -0800 | [diff] [blame] | 700 | MetricsLoggerWrapper.logPictureInPictureDismissByDrag(mContext, |
| 701 | PipUtils.getTopPinnedActivity(mContext, mActivityManager)); |
Winson Chung | b54b65b | 2017-04-26 14:02:13 -0700 | [diff] [blame] | 702 | mMotionHelper.animateDismiss(mMotionHelper.getBounds(), vel.x, |
| 703 | vel.y, mUpdateScrimListener); |
Winson Chung | b54b65b | 2017-04-26 14:02:13 -0700 | [diff] [blame] | 704 | return true; |
Mady Mellor | d4e40fb | 2017-01-26 10:43:16 -0800 | [diff] [blame] | 705 | } |
Mady Mellor | d4e40fb | 2017-01-26 10:43:16 -0800 | [diff] [blame] | 706 | } |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 707 | |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 708 | if (touchState.isDragging()) { |
Mady Mellor | 84a0f89 | 2017-03-27 14:10:46 -0700 | [diff] [blame] | 709 | final boolean isFlingToEdge = isFling && isHorizontal && mMovementWithinMinimize |
| 710 | && (mStartedOnLeft ? vel.x < 0 : vel.x > 0); |
Mady Mellor | 8c7dc42 | 2017-05-10 12:55:06 -0700 | [diff] [blame] | 711 | if (ENABLE_MINIMIZE && |
Mady Mellor | 2e13878 | 2017-03-27 11:09:50 -0700 | [diff] [blame] | 712 | !mIsMinimized && (mMotionHelper.shouldMinimizePip() || isFlingToEdge)) { |
Mady Mellor | 3b10dcd | 2017-01-23 10:08:35 -0800 | [diff] [blame] | 713 | // Pip should be minimized |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 714 | setMinimizedStateInternal(true); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 715 | if (mMenuState == MENU_STATE_FULL) { |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 716 | // If the user dragged the expanded PiP to the edge, then hiding the menu |
| 717 | // will trigger the PiP to be scaled back to the normal size with the |
| 718 | // minimize offset adjusted |
| 719 | mMenuController.hideMenu(); |
| 720 | } else { |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 721 | mMotionHelper.animateToClosestMinimizedState(mMovementBounds, |
| 722 | mUpdateScrimListener); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 723 | } |
Mady Mellor | 3b10dcd | 2017-01-23 10:08:35 -0800 | [diff] [blame] | 724 | return true; |
| 725 | } |
| 726 | if (mIsMinimized) { |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 727 | // If we're dragging and it wasn't a minimize gesture then we shouldn't be |
| 728 | // minimized. |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 729 | setMinimizedStateInternal(false); |
Mady Mellor | 3b10dcd | 2017-01-23 10:08:35 -0800 | [diff] [blame] | 730 | } |
| 731 | |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 732 | AnimatorListenerAdapter postAnimationCallback = null; |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 733 | if (mMenuState != MENU_STATE_NONE) { |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 734 | // If the menu is still visible, and we aren't minimized, then just poke the |
| 735 | // menu so that it will timeout after the user stops touching it |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 736 | mMenuController.showMenu(mMenuState, mMotionHelper.getBounds(), |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 737 | mMovementBounds, true /* allowMenuTimeout */, willResizeMenu()); |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 738 | } else { |
| 739 | // If the menu is not visible, then we can still be showing the activity for the |
| 740 | // dismiss overlay, so just finish it after the animation completes |
| 741 | postAnimationCallback = new AnimatorListenerAdapter() { |
| 742 | @Override |
| 743 | public void onAnimationEnd(Animator animation) { |
| 744 | mMenuController.hideMenu(); |
| 745 | } |
| 746 | }; |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 747 | } |
| 748 | |
Mady Mellor | 2fbdd3b | 2017-03-21 17:45:00 -0700 | [diff] [blame] | 749 | if (isFling) { |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 750 | mMotionHelper.flingToSnapTarget(velocity, vel.x, vel.y, mMovementBounds, |
Mady Mellor | 15b29c7 | 2017-06-07 14:53:58 -0700 | [diff] [blame] | 751 | mUpdateScrimListener, postAnimationCallback, |
| 752 | mStartPosition); |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 753 | } else { |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 754 | mMotionHelper.animateToClosestSnapTarget(mMovementBounds, mUpdateScrimListener, |
| 755 | postAnimationCallback); |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 756 | } |
Mady Mellor | 3b10dcd | 2017-01-23 10:08:35 -0800 | [diff] [blame] | 757 | } else if (mIsMinimized) { |
| 758 | // This was a tap, so no longer minimized |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 759 | mMotionHelper.animateToClosestSnapTarget(mMovementBounds, null /* updateListener */, |
| 760 | null /* animatorListener */); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 761 | setMinimizedStateInternal(false); |
Arthur Hung | 0ede6e9 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 762 | } else if (mTouchState.isDoubleTap()) { |
| 763 | // Expand to fullscreen if this is a double tap |
| 764 | mMotionHelper.expandPip(); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 765 | } else if (mMenuState != MENU_STATE_FULL) { |
Arthur Hung | 0ede6e9 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 766 | if (!mTouchState.isWaitingForDoubleTap()) { |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 767 | // User has stalled long enough for this not to be a drag or a double tap, just |
| 768 | // expand the menu |
| 769 | mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(), |
| 770 | mMovementBounds, true /* allowMenuTimeout */, willResizeMenu()); |
| 771 | } else { |
| 772 | // Next touch event _may_ be the second tap for the double-tap, schedule a |
| 773 | // fallback runnable to trigger the menu if no touch event occurs before the |
| 774 | // next tap |
| 775 | mTouchState.scheduleDoubleTapTimeoutCallback(); |
| 776 | } |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 777 | } |
| 778 | return true; |
| 779 | } |
| 780 | }; |
Mady Mellor | 3b10dcd | 2017-01-23 10:08:35 -0800 | [diff] [blame] | 781 | |
| 782 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 783 | * Updates the current movement bounds based on whether the menu is currently visible. |
| 784 | */ |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 785 | private void updateMovementBounds(int menuState) { |
Winson Chung | a71febe | 2017-05-22 11:14:22 -0700 | [diff] [blame] | 786 | boolean isMenuExpanded = menuState == MENU_STATE_FULL; |
| 787 | mMovementBounds = isMenuExpanded |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 788 | ? mExpandedMovementBounds |
| 789 | : mNormalMovementBounds; |
Winson Chung | a71febe | 2017-05-22 11:14:22 -0700 | [diff] [blame] | 790 | try { |
Winson Chung | 54844b4 | 2019-04-03 11:32:41 -0700 | [diff] [blame] | 791 | if (mPinnedStackController != null) { |
| 792 | mPinnedStackController.setMinEdgeSize( |
| 793 | isMenuExpanded ? mExpandedShortestEdgeSize : 0); |
| 794 | } |
Winson Chung | a71febe | 2017-05-22 11:14:22 -0700 | [diff] [blame] | 795 | } catch (RemoteException e) { |
| 796 | Log.e(TAG, "Could not set minimized state", e); |
| 797 | } |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 798 | } |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 799 | |
Winson Chung | b54b65b | 2017-04-26 14:02:13 -0700 | [diff] [blame] | 800 | /** |
| 801 | * Removes the dismiss target and cancels any pending callbacks to show it. |
| 802 | */ |
| 803 | private void cleanUpDismissTarget() { |
| 804 | mHandler.removeCallbacks(mShowDismissAffordance); |
| 805 | mDismissViewController.destroyDismissTarget(); |
| 806 | } |
| 807 | |
Winson Chung | 9b91941 | 2017-06-19 17:01:51 -0700 | [diff] [blame] | 808 | /** |
| 809 | * Resets some states related to the touch handling. |
| 810 | */ |
| 811 | private void cleanUp() { |
| 812 | if (mIsMinimized) { |
| 813 | setMinimizedStateInternal(false); |
| 814 | } |
| 815 | cleanUpDismissTarget(); |
| 816 | } |
| 817 | |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 818 | /** |
| 819 | * @return whether the menu will resize as a part of showing the full menu. |
| 820 | */ |
| 821 | private boolean willResizeMenu() { |
| 822 | return mExpandedBounds.width() != mNormalBounds.width() || |
| 823 | mExpandedBounds.height() != mNormalBounds.height(); |
| 824 | } |
| 825 | |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 826 | public void dump(PrintWriter pw, String prefix) { |
| 827 | final String innerPrefix = prefix + " "; |
| 828 | pw.println(prefix + TAG); |
| 829 | pw.println(innerPrefix + "mMovementBounds=" + mMovementBounds); |
| 830 | pw.println(innerPrefix + "mNormalBounds=" + mNormalBounds); |
| 831 | pw.println(innerPrefix + "mNormalMovementBounds=" + mNormalMovementBounds); |
| 832 | pw.println(innerPrefix + "mExpandedBounds=" + mExpandedBounds); |
| 833 | pw.println(innerPrefix + "mExpandedMovementBounds=" + mExpandedMovementBounds); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 834 | pw.println(innerPrefix + "mMenuState=" + mMenuState); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 835 | pw.println(innerPrefix + "mIsMinimized=" + mIsMinimized); |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 836 | pw.println(innerPrefix + "mIsImeShowing=" + mIsImeShowing); |
| 837 | pw.println(innerPrefix + "mImeHeight=" + mImeHeight); |
Tracy Zhou | 4351308 | 2018-03-08 21:58:36 -0800 | [diff] [blame] | 838 | pw.println(innerPrefix + "mIsShelfShowing=" + mIsShelfShowing); |
| 839 | pw.println(innerPrefix + "mShelfHeight=" + mShelfHeight); |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 840 | pw.println(innerPrefix + "mSavedSnapFraction=" + mSavedSnapFraction); |
Eliot Courtney | 3dc12f1 | 2018-06-01 14:26:26 +0900 | [diff] [blame] | 841 | pw.println(innerPrefix + "mEnableDragToEdgeDismiss=" + mEnableDimissDragToEdge); |
Mady Mellor | 8c7dc42 | 2017-05-10 12:55:06 -0700 | [diff] [blame] | 842 | pw.println(innerPrefix + "mEnableMinimize=" + ENABLE_MINIMIZE); |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 843 | mSnapAlgorithm.dump(pw, innerPrefix); |
| 844 | mTouchState.dump(pw, innerPrefix); |
| 845 | mMotionHelper.dump(pw, innerPrefix); |
| 846 | } |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 847 | |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 848 | } |