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