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