blob: 3140e6db24428632c9e7c4fa9223866063ae11d0 [file] [log] [blame]
Winson73bc1592016-10-18 18:47:43 -07001/*
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
17package com.android.systemui.pip.phone;
18
Mady Mellor637cd482017-03-21 10:39:42 -070019import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_CLOSE;
20import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_FULL;
Chenjie Yuae9fdf042018-02-15 10:19:32 -080021import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_NONE;
Mady Mellor637cd482017-03-21 10:39:42 -070022
Winson Chung87e5d552017-04-05 11:49:38 -070023import android.animation.Animator;
24import android.animation.AnimatorListenerAdapter;
Mady Mellor81d40612017-03-10 15:14:10 -080025import android.animation.ValueAnimator;
26import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson73bc1592016-10-18 18:47:43 -070027import android.app.IActivityManager;
Wale Ogunwale65ebd952018-04-25 15:41:44 -070028import android.app.IActivityTaskManager;
Winson Chung9b919412017-06-19 17:01:51 -070029import android.content.ComponentName;
Winson73bc1592016-10-18 18:47:43 -070030import android.content.Context;
Winson Chung136d1ec82017-07-18 11:16:30 -070031import android.content.res.Resources;
Mady Mellora7f69742017-02-03 11:00:20 -080032import android.graphics.Point;
Winson73bc1592016-10-18 18:47:43 -070033import android.graphics.PointF;
34import android.graphics.Rect;
Mady Mellord4e40fb2017-01-26 10:43:16 -080035import android.os.Handler;
Winson73bc1592016-10-18 18:47:43 -070036import android.os.RemoteException;
37import android.util.Log;
Mady Mellora7f69742017-02-03 11:00:20 -080038import android.util.Size;
Winson Chung655332c2016-10-31 13:14:28 -070039import android.view.IPinnedStackController;
Tracy Zhou535c2122019-02-21 17:02:47 -080040import android.view.InputEvent;
Winson73bc1592016-10-18 18:47:43 -070041import android.view.MotionEvent;
Winson73bc1592016-10-18 18:47:43 -070042import android.view.ViewConfiguration;
Phil Weaverf00cd142017-03-03 13:44:00 -080043import android.view.accessibility.AccessibilityEvent;
44import android.view.accessibility.AccessibilityManager;
45import android.view.accessibility.AccessibilityNodeInfo;
Phil Weaver0a8caa12017-08-09 11:28:41 -070046import android.view.accessibility.AccessibilityWindowInfo;
Chenjie Yuae9fdf042018-02-15 10:19:32 -080047
Chenjie Yu52cacc62017-12-08 18:11:45 -080048import com.android.internal.os.logging.MetricsLoggerWrapper;
Winson Chungcd1ff642016-10-26 09:44:43 -070049import com.android.internal.policy.PipSnapAlgorithm;
Mady Mellora7f69742017-02-03 11:00:20 -080050import com.android.systemui.R;
Winson Chungcbb15a92018-01-25 17:46:16 +000051import com.android.systemui.shared.system.InputConsumerController;
Winson73bc1592016-10-18 18:47:43 -070052import com.android.systemui.statusbar.FlingAnimationUtils;
53
Winson Chung29a78652017-02-09 18:35:26 -080054import java.io.PrintWriter;
55
Winson73bc1592016-10-18 18:47:43 -070056/**
57 * Manages all the touch handling for PIP on the Phone, including moving, dismissing and expanding
58 * the PIP.
59 */
Mady Mellor8c7dc422017-05-10 12:55:06 -070060public class PipTouchHandler {
Winson73bc1592016-10-18 18:47:43 -070061 private static final String TAG = "PipTouchHandler";
Winson73bc1592016-10-18 18:47:43 -070062
Mady Mellor8c7dc422017-05-10 12:55:06 -070063 // 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 Mellor2e138782017-03-27 11:09:50 -070067
Mady Mellor5d58d252017-04-18 12:48:04 -070068 private static final int SHOW_DISMISS_AFFORDANCE_DELAY = 225;
Tracy Zhoue19d0512018-07-31 16:29:34 -070069 private static final int BOTTOM_OFFSET_BUFFER_DP = 1;
Winson Chungfa7053782016-11-08 15:45:10 -080070
Winson Chung6e35ee1f2017-02-14 12:06:44 -080071 // Allow dragging the PIP to a location to close it
Eliot Courtney3dc12f12018-06-01 14:26:26 +090072 private final boolean mEnableDimissDragToEdge;
Winson73bc1592016-10-18 18:47:43 -070073 private final Context mContext;
74 private final IActivityManager mActivityManager;
Wale Ogunwale65ebd952018-04-25 15:41:44 -070075 private final IActivityTaskManager mActivityTaskManager;
Winson73bc1592016-10-18 18:47:43 -070076 private final ViewConfiguration mViewConfig;
Winson Chung15504af2016-11-02 18:11:36 -070077 private final PipMenuListener mMenuListener = new PipMenuListener();
Winson Chung655332c2016-10-31 13:14:28 -070078 private IPinnedStackController mPinnedStackController;
Winson73bc1592016-10-18 18:47:43 -070079
Winson Chung2a82fe52017-02-02 14:43:34 -080080 private final PipMenuActivityController mMenuController;
81 private final PipDismissViewController mDismissViewController;
Winson Chung14fefc22016-11-02 10:02:29 -070082 private final PipSnapAlgorithm mSnapAlgorithm;
Phil Weaverf00cd142017-03-03 13:44:00 -080083 private final AccessibilityManager mAccessibilityManager;
Wale Ogunwale6455e502017-04-17 14:16:43 -070084 private boolean mShowPipMenuOnAnimationEnd = false;
Winson73bc1592016-10-18 18:47:43 -070085
Winson Chung2a82fe52017-02-02 14:43:34 -080086 // The current movement bounds
87 private Rect mMovementBounds = new Rect();
88
Winson Chung0a2f34f2017-08-11 18:13:24 -070089 // The reference inset bounds, used to determine the dismiss fraction
90 private Rect mInsetBounds = new Rect();
Winson Chung2a82fe52017-02-02 14:43:34 -080091 // 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 Mellora7f69742017-02-03 11:00:20 -080096 private int mExpandedShortestEdgeSize;
Winson73bc1592016-10-18 18:47:43 -070097
Winson Chungef4dc812017-04-11 13:31:44 -070098 // 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 Mellord4e40fb2017-01-26 10:43:16 -0800103 private Handler mHandler = new Handler();
104 private Runnable mShowDismissAffordance = new Runnable() {
105 @Override
106 public void run() {
Eliot Courtney3dc12f12018-06-01 14:26:26 +0900107 if (mEnableDimissDragToEdge) {
Mady Mellor60421c92017-03-29 15:27:37 -0700108 mDismissViewController.showDismissTarget();
Mady Mellord4e40fb2017-01-26 10:43:16 -0800109 }
110 }
111 };
Mady Mellor81d40612017-03-10 15:14:10 -0800112 private ValueAnimator.AnimatorUpdateListener mUpdateScrimListener =
113 new AnimatorUpdateListener() {
114 @Override
115 public void onAnimationUpdate(ValueAnimator animation) {
116 updateDismissFraction();
117 }
118 };
Mady Mellord4e40fb2017-01-26 10:43:16 -0800119
Winson Chungfa7053782016-11-08 15:45:10 -0800120 // Behaviour states
Winson Chung9b919412017-06-19 17:01:51 -0700121 private int mMenuState = MENU_STATE_NONE;
Winson Chungd2d90972017-02-28 11:40:41 -0800122 private boolean mIsMinimized;
Winson Chung2a82fe52017-02-02 14:43:34 -0800123 private boolean mIsImeShowing;
124 private int mImeHeight;
Winson Chung136d1ec82017-07-18 11:16:30 -0700125 private int mImeOffset;
Tracy Zhou43513082018-03-08 21:58:36 -0800126 private boolean mIsShelfShowing;
127 private int mShelfHeight;
Winson Chung2a82fe52017-02-02 14:43:34 -0800128 private float mSavedSnapFraction = -1f;
Phil Weaverf00cd142017-03-03 13:44:00 -0800129 private boolean mSendingHoverAccessibilityEvents;
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700130 private boolean mMovementWithinMinimize;
131 private boolean mMovementWithinDismiss;
Winson73bc1592016-10-18 18:47:43 -0700132
Winson Chungfa7053782016-11-08 15:45:10 -0800133 // Touch state
134 private final PipTouchState mTouchState;
Winson73bc1592016-10-18 18:47:43 -0700135 private final FlingAnimationUtils mFlingAnimationUtils;
Winson Chungfa7053782016-11-08 15:45:10 -0800136 private final PipTouchGesture[] mGestures;
Winson Chung2a82fe52017-02-02 14:43:34 -0800137 private final PipMotionHelper mMotionHelper;
Winson73bc1592016-10-18 18:47:43 -0700138
Winson Chung2a82fe52017-02-02 14:43:34 -0800139 // Temp vars
Winson Chung655332c2016-10-31 13:14:28 -0700140 private final Rect mTmpBounds = new Rect();
141
Winson73bc1592016-10-18 18:47:43 -0700142 /**
Winson Chung15504af2016-11-02 18:11:36 -0700143 * A listener for the PIP menu activity.
144 */
145 private class PipMenuListener implements PipMenuActivityController.Listener {
146 @Override
Mady Mellor637cd482017-03-21 10:39:42 -0700147 public void onPipMenuStateChanged(int menuState, boolean resize) {
148 setMenuState(menuState, resize);
Winson Chung15504af2016-11-02 18:11:36 -0700149 }
Winson Chunga29eb982016-12-14 12:01:27 -0800150
151 @Override
152 public void onPipExpand() {
153 if (!mIsMinimized) {
Winson Chung2a82fe52017-02-02 14:43:34 -0800154 mMotionHelper.expandPip();
Winson Chunga29eb982016-12-14 12:01:27 -0800155 }
156 }
157
158 @Override
159 public void onPipMinimize() {
Winson Chung2a82fe52017-02-02 14:43:34 -0800160 setMinimizedStateInternal(true);
Mady Mellor81d40612017-03-10 15:14:10 -0800161 mMotionHelper.animateToClosestMinimizedState(mMovementBounds, null /* updateListener */);
Winson Chunga29eb982016-12-14 12:01:27 -0800162 }
163
164 @Override
165 public void onPipDismiss() {
Chenjie Yuae9fdf042018-02-15 10:19:32 -0800166 MetricsLoggerWrapper.logPictureInPictureDismissByTap(mContext,
167 PipUtils.getTopPinnedActivity(mContext, mActivityManager));
Winson Chung2a82fe52017-02-02 14:43:34 -0800168 mMotionHelper.dismissPip();
Winson Chunga29eb982016-12-14 12:01:27 -0800169 }
Mady Mellor637cd482017-03-21 10:39:42 -0700170
171 @Override
172 public void onPipShowMenu() {
173 mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
Winson Chungbb787442017-09-01 11:33:47 -0700174 mMovementBounds, true /* allowMenuTimeout */, willResizeMenu());
Mady Mellor637cd482017-03-21 10:39:42 -0700175 }
Winson Chung15504af2016-11-02 18:11:36 -0700176 }
177
Winson Chungd2d90972017-02-28 11:40:41 -0800178 public PipTouchHandler(Context context, IActivityManager activityManager,
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700179 IActivityTaskManager activityTaskManager, PipMenuActivityController menuController,
Winson Chungd2d90972017-02-28 11:40:41 -0800180 InputConsumerController inputConsumerController) {
Winson73bc1592016-10-18 18:47:43 -0700181
182 // Initialize the Pip input consumer
Winson73bc1592016-10-18 18:47:43 -0700183 mContext = context;
184 mActivityManager = activityManager;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700185 mActivityTaskManager = activityTaskManager;
Phil Weaverf00cd142017-03-03 13:44:00 -0800186 mAccessibilityManager = context.getSystemService(AccessibilityManager.class);
Winson73bc1592016-10-18 18:47:43 -0700187 mViewConfig = ViewConfiguration.get(context);
Winson Chung15504af2016-11-02 18:11:36 -0700188 mMenuController = menuController;
189 mMenuController.addListener(mMenuListener);
190 mDismissViewController = new PipDismissViewController(context);
Winson Chung14fefc22016-11-02 10:02:29 -0700191 mSnapAlgorithm = new PipSnapAlgorithm(mContext);
Mady Mellor15b29c72017-06-07 14:53:58 -0700192 mFlingAnimationUtils = new FlingAnimationUtils(context, 2.5f);
Winson Chunga5acf182017-01-05 16:02:27 -0800193 mGestures = new PipTouchGesture[] {
Mady Mellord4e40fb2017-01-26 10:43:16 -0800194 mDefaultMovementGesture
Winson Chungfa7053782016-11-08 15:45:10 -0800195 };
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700196 mMotionHelper = new PipMotionHelper(mContext, mActivityManager, mActivityTaskManager,
197 mMenuController, mSnapAlgorithm, mFlingAnimationUtils);
Winson Chungbca03112017-08-16 10:38:15 -0700198 mTouchState = new PipTouchState(mViewConfig, mHandler,
199 () -> mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
200 mMovementBounds, true /* allowMenuTimeout */, willResizeMenu()));
Winson Chung136d1ec82017-07-18 11:16:30 -0700201
202 Resources res = context.getResources();
203 mExpandedShortestEdgeSize = res.getDimensionPixelSize(
Mady Mellora7f69742017-02-03 11:00:20 -0800204 R.dimen.pip_expanded_shortest_edge_size);
Winson Chung136d1ec82017-07-18 11:16:30 -0700205 mImeOffset = res.getDimensionPixelSize(R.dimen.pip_ime_offset);
Winson Chungd2d90972017-02-28 11:40:41 -0800206
Eliot Courtney3dc12f12018-06-01 14:26:26 +0900207 mEnableDimissDragToEdge = res.getBoolean(R.bool.config_pipEnableDismissDragToEdge);
208
Winson Chungd2d90972017-02-28 11:40:41 -0800209 // Register the listener for input consumer touch events
Tracy Zhou535c2122019-02-21 17:02:47 -0800210 inputConsumerController.setInputListener(this::handleTouchEvent);
Phil Weaverf00cd142017-03-03 13:44:00 -0800211 inputConsumerController.setRegistrationListener(this::onRegistrationChanged);
212 onRegistrationChanged(inputConsumerController.isRegistered());
Winson73bc1592016-10-18 18:47:43 -0700213 }
214
Winson Chung85d39982017-02-24 15:21:25 -0800215 public void setTouchEnabled(boolean enabled) {
216 mTouchState.setAllowTouches(enabled);
217 }
218
Winson Chungac52f282017-03-30 14:44:52 -0700219 public void showPictureInPictureMenu() {
220 // Only show the menu if the user isn't currently interacting with the PiP
221 if (!mTouchState.isUserInteracting()) {
Mady Mellor637cd482017-03-21 10:39:42 -0700222 mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
Winson Chungbb787442017-09-01 11:33:47 -0700223 mMovementBounds, false /* allowMenuTimeout */, willResizeMenu());
Winson Chungac52f282017-03-30 14:44:52 -0700224 }
225 }
226
Winson Chung929d4f72017-01-13 10:21:33 -0800227 public void onActivityPinned() {
Winson Chung9b919412017-06-19 17:01:51 -0700228 cleanUp();
Wale Ogunwale6455e502017-04-17 14:16:43 -0700229 mShowPipMenuOnAnimationEnd = true;
Winson Chung929d4f72017-01-13 10:21:33 -0800230 }
231
Winson Chung9b919412017-06-19 17:01:51 -0700232 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 Chungac52f282017-03-30 14:44:52 -0700239 public void onPinnedStackAnimationEnded() {
240 // Always synchronize the motion helper bounds once PiP animations finish
241 mMotionHelper.synchronizePinnedStackBounds();
Wale Ogunwale6455e502017-04-17 14:16:43 -0700242
243 if (mShowPipMenuOnAnimationEnd) {
244 mMenuController.showMenu(MENU_STATE_CLOSE, mMotionHelper.getBounds(),
Winson Chungbb787442017-09-01 11:33:47 -0700245 mMovementBounds, true /* allowMenuTimeout */, false /* willResizeMenu */);
Wale Ogunwale6455e502017-04-17 14:16:43 -0700246 mShowPipMenuOnAnimationEnd = false;
247 }
Winson Chungac52f282017-03-30 14:44:52 -0700248 }
249
Winson Chung303c6b72016-10-24 17:12:49 -0700250 public void onConfigurationChanged() {
Winson Chung2a82fe52017-02-02 14:43:34 -0800251 mMotionHelper.onConfigurationChanged();
252 mMotionHelper.synchronizePinnedStackBounds();
Winson Chung303c6b72016-10-24 17:12:49 -0700253 }
254
Winson Chung2a82fe52017-02-02 14:43:34 -0800255 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
256 mIsImeShowing = imeVisible;
257 mImeHeight = imeHeight;
258 }
259
Tracy Zhou43513082018-03-08 21:58:36 -0800260 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight) {
261 mIsShelfShowing = shelfVisible;
262 mShelfHeight = shelfHeight;
263 }
264
Winson Chungbaa7b722017-03-03 21:33:44 -0800265 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds, Rect animatingBounds,
Tracy Zhou43513082018-03-08 21:58:36 -0800266 boolean fromImeAdjustment, boolean fromShelfAdjustment, int displayRotation) {
267 final int bottomOffset = mIsImeShowing ? mImeHeight : 0;
268
Winson Chung2a82fe52017-02-02 14:43:34 -0800269 // Re-calculate the expanded bounds
270 mNormalBounds = normalBounds;
271 Rect normalMovementBounds = new Rect();
272 mSnapAlgorithm.getMovementBounds(mNormalBounds, insetBounds, normalMovementBounds,
Tracy Zhou43513082018-03-08 21:58:36 -0800273 bottomOffset);
Mady Mellora7f69742017-02-03 11:00:20 -0800274
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 Chung2a82fe52017-02-02 14:43:34 -0800282 Rect expandedMovementBounds = new Rect();
283 mSnapAlgorithm.getMovementBounds(mExpandedBounds, insetBounds, expandedMovementBounds,
Tracy Zhou43513082018-03-08 21:58:36 -0800284 bottomOffset);
Winson Chung2a82fe52017-02-02 14:43:34 -0800285
Tracy Zhou43513082018-03-08 21:58:36 -0800286 // 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 Chung2a82fe52017-02-02 14:43:34 -0800289 if (mTouchState.isUserInteracting()) {
290 // Defer the update of the current movement bounds until after the user finishes
291 // touching the screen
292 } else {
Tracy Zhou4be72d22018-03-22 11:41:00 -0700293 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 Zhou805e66c2018-04-04 23:45:26 -0700304 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 Zhoue19d0512018-07-31 16:29:34 -0700319 final float bottomOffsetBufferInPx = BOTTOM_OFFSET_BUFFER_DP
320 * mContext.getResources().getDisplayMetrics().density;
Tracy Zhou805e66c2018-04-04 23:45:26 -0700321 if (toAdjustedBounds.bottom >= mMovementBounds.bottom
Tracy Zhoue19d0512018-07-31 16:29:34 -0700322 && animatingBounds.top + Math.round(bottomOffsetBufferInPx)
Tracy Zhou805e66c2018-04-04 23:45:26 -0700323 < toAdjustedBounds.bottom - movementBoundsAdjustment - offsetAdjustment) {
324 return;
325 }
Tracy Zhou4be72d22018-03-22 11:41:00 -0700326
327 animateToOffset(animatingBounds, toAdjustedBounds);
Winson Chung2a82fe52017-02-02 14:43:34 -0800328 }
Winson Chung14fbe142016-12-19 16:18:24 -0800329 }
Winson Chunga29eb982016-12-14 12:01:27 -0800330
Winson Chung2a82fe52017-02-02 14:43:34 -0800331 // Update the movement bounds after doing the calculations based on the old movement bounds
332 // above
333 mNormalMovementBounds = normalMovementBounds;
334 mExpandedMovementBounds = expandedMovementBounds;
Winson Chungef4dc812017-04-11 13:31:44 -0700335 mDisplayRotation = displayRotation;
Winson Chung0a2f34f2017-08-11 18:13:24 -0700336 mInsetBounds.set(insetBounds);
Mady Mellor637cd482017-03-21 10:39:42 -0700337 updateMovementBounds(mMenuState);
Winson Chungef4dc812017-04-11 13:31:44 -0700338
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 Chunga29eb982016-12-14 12:01:27 -0800347 }
348
Tracy Zhou4be72d22018-03-22 11:41:00 -0700349 private void animateToOffset(Rect animatingBounds, Rect toAdjustedBounds) {
wilsonshih5c4cf522019-01-25 09:03:47 +0800350 int offset = toAdjustedBounds.bottom - animatingBounds.top;
Tracy Zhou4b696822018-04-02 18:19:43 -0700351 // 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.
wilsonshih5c4cf522019-01-25 09:03:47 +0800353 offset += Math.max(0, mMovementBounds.top - animatingBounds.top);
354 mMotionHelper.animateToOffset(animatingBounds, offset);
Tracy Zhou43513082018-03-08 21:58:36 -0800355 }
356
Phil Weaverf00cd142017-03-03 13:44:00 -0800357 private void onRegistrationChanged(boolean isRegistered) {
358 mAccessibilityManager.setPictureInPictureActionReplacingConnection(isRegistered
Winson Chungfe1fa642017-03-13 10:51:22 -0700359 ? new PipAccessibilityInteractionConnection(mMotionHelper,
360 this::onAccessibilityShowMenu, mHandler) : null);
Winson Chungb54b65b2017-04-26 14:02:13 -0700361
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 Chungfe1fa642017-03-13 10:51:22 -0700367 }
368
369 private void onAccessibilityShowMenu() {
Mady Mellor637cd482017-03-21 10:39:42 -0700370 mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
Rhed Jao5e00c982018-11-27 19:41:05 +0800371 mMovementBounds, true /* allowMenuTimeout */, willResizeMenu());
Phil Weaverf00cd142017-03-03 13:44:00 -0800372 }
373
Tracy Zhou535c2122019-02-21 17:02:47 -0800374 private boolean handleTouchEvent(InputEvent inputEvent) {
375 // Skip any non motion events
376 if (!(inputEvent instanceof MotionEvent)) {
377 return true;
378 }
Winson Chung655332c2016-10-31 13:14:28 -0700379 // Skip touch handling until we are bound to the controller
380 if (mPinnedStackController == null) {
Winson Chung15504af2016-11-02 18:11:36 -0700381 return true;
Winson Chung655332c2016-10-31 13:14:28 -0700382 }
Tracy Zhou535c2122019-02-21 17:02:47 -0800383 MotionEvent ev = (MotionEvent) inputEvent;
Winson Chung655332c2016-10-31 13:14:28 -0700384
Winson Chungfa7053782016-11-08 15:45:10 -0800385 // Update the touch state
386 mTouchState.onTouchEvent(ev);
387
Winson73bc1592016-10-18 18:47:43 -0700388 switch (ev.getAction()) {
389 case MotionEvent.ACTION_DOWN: {
Winson Chung2a82fe52017-02-02 14:43:34 -0800390 mMotionHelper.synchronizePinnedStackBounds();
Winson73bc1592016-10-18 18:47:43 -0700391
Winson Chungfa7053782016-11-08 15:45:10 -0800392 for (PipTouchGesture gesture : mGestures) {
393 gesture.onDown(mTouchState);
394 }
Winson73bc1592016-10-18 18:47:43 -0700395 break;
396 }
397 case MotionEvent.ACTION_MOVE: {
Winson Chungfa7053782016-11-08 15:45:10 -0800398 for (PipTouchGesture gesture : mGestures) {
399 if (gesture.onMove(mTouchState)) {
400 break;
Winson73bc1592016-10-18 18:47:43 -0700401 }
402 }
Winson73bc1592016-10-18 18:47:43 -0700403 break;
404 }
405 case MotionEvent.ACTION_UP: {
Winson Chung14fefc22016-11-02 10:02:29 -0700406 // Update the movement bounds again if the state has changed since the user started
407 // dragging (ie. when the IME shows)
Mady Mellor637cd482017-03-21 10:39:42 -0700408 updateMovementBounds(mMenuState);
Winson Chung14fefc22016-11-02 10:02:29 -0700409
Winson Chungfa7053782016-11-08 15:45:10 -0800410 for (PipTouchGesture gesture : mGestures) {
411 if (gesture.onUp(mTouchState)) {
412 break;
Winson Chung5cd26ff2016-10-24 11:50:44 -0700413 }
Winson Chung5cd26ff2016-10-24 11:50:44 -0700414 }
Winson73bc1592016-10-18 18:47:43 -0700415
416 // Fall through to clean up
417 }
418 case MotionEvent.ACTION_CANCEL: {
Winson Chung85d39982017-02-24 15:21:25 -0800419 mTouchState.reset();
Winson73bc1592016-10-18 18:47:43 -0700420 break;
421 }
Phil Weaverf00cd142017-03-03 13:44:00 -0800422 case MotionEvent.ACTION_HOVER_ENTER:
423 case MotionEvent.ACTION_HOVER_MOVE: {
Eugene Suslad4128ec2017-12-04 19:48:41 +0000424 if (mAccessibilityManager.isEnabled() && !mSendingHoverAccessibilityEvents) {
Phil Weaverf00cd142017-03-03 13:44:00 -0800425 AccessibilityEvent event = AccessibilityEvent.obtain(
426 AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Phil Weaver0a8caa12017-08-09 11:28:41 -0700427 event.setImportantForAccessibility(true);
Phil Weaver651fe9f2017-05-24 16:43:46 -0700428 event.setSourceNodeId(AccessibilityNodeInfo.ROOT_NODE_ID);
Phil Weaver0a8caa12017-08-09 11:28:41 -0700429 event.setWindowId(
430 AccessibilityWindowInfo.PICTURE_IN_PICTURE_ACTION_REPLACER_WINDOW_ID);
Phil Weaverf00cd142017-03-03 13:44:00 -0800431 mAccessibilityManager.sendAccessibilityEvent(event);
432 mSendingHoverAccessibilityEvents = true;
433 }
434 break;
435 }
436 case MotionEvent.ACTION_HOVER_EXIT: {
Eugene Suslad4128ec2017-12-04 19:48:41 +0000437 if (mAccessibilityManager.isEnabled() && mSendingHoverAccessibilityEvents) {
Phil Weaverf00cd142017-03-03 13:44:00 -0800438 AccessibilityEvent event = AccessibilityEvent.obtain(
439 AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Phil Weaver0a8caa12017-08-09 11:28:41 -0700440 event.setImportantForAccessibility(true);
Phil Weaver651fe9f2017-05-24 16:43:46 -0700441 event.setSourceNodeId(AccessibilityNodeInfo.ROOT_NODE_ID);
Phil Weaver0a8caa12017-08-09 11:28:41 -0700442 event.setWindowId(
443 AccessibilityWindowInfo.PICTURE_IN_PICTURE_ACTION_REPLACER_WINDOW_ID);
Phil Weaverf00cd142017-03-03 13:44:00 -0800444 mAccessibilityManager.sendAccessibilityEvent(event);
445 mSendingHoverAccessibilityEvents = false;
446 }
447 break;
448 }
Winson73bc1592016-10-18 18:47:43 -0700449 }
Mady Mellor637cd482017-03-21 10:39:42 -0700450 return mMenuState == MENU_STATE_NONE;
Winson Chung15504af2016-11-02 18:11:36 -0700451 }
452
453 /**
Mady Mellor81d40612017-03-10 15:14:10 -0800454 * Updates the appearance of the menu and scrim on top of the PiP while dismissing.
455 */
Winson Chung8ec0e162017-07-07 14:49:49 -0700456 private void updateDismissFraction() {
Winson Chung0a2f34f2017-08-11 18:13:24 -0700457 // 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 Mellor81d40612017-03-10 15:14:10 -0800461 Rect bounds = mMotionHelper.getBounds();
Winson Chung0a2f34f2017-08-11 18:13:24 -0700462 final float target = mInsetBounds.bottom;
Mady Mellor81d40612017-03-10 15:14:10 -0800463 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 Chungbca03112017-08-16 10:38:15 -0700468 if (Float.compare(fraction, 0f) != 0 || mMenuController.isMenuActivityVisible()) {
Winson Chung87e5d552017-04-05 11:49:38 -0700469 // Update if the fraction > 0, or if fraction == 0 and the menu was already visible
470 mMenuController.setDismissFraction(fraction);
471 }
Mady Mellor81d40612017-03-10 15:14:10 -0800472 }
473 }
474
475 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800476 * 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 Chung2a82fe52017-02-02 14:43:34 -0800483 * Sets the minimized state.
Winson Chungdff5c082016-11-02 17:28:03 -0700484 */
Winson Chung8ec0e162017-07-07 14:49:49 -0700485 private void setMinimizedStateInternal(boolean isMinimized) {
Mady Mellor8c7dc422017-05-10 12:55:06 -0700486 if (!ENABLE_MINIMIZE) {
Mady Mellor2e138782017-03-27 11:09:50 -0700487 return;
488 }
Winson Chung2a82fe52017-02-02 14:43:34 -0800489 setMinimizedState(isMinimized, false /* fromController */);
Winson Chungdff5c082016-11-02 17:28:03 -0700490 }
491
492 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800493 * Sets the minimized state.
Winson Chungfa7053782016-11-08 15:45:10 -0800494 */
Winson Chung2a82fe52017-02-02 14:43:34 -0800495 void setMinimizedState(boolean isMinimized, boolean fromController) {
Mady Mellor8c7dc422017-05-10 12:55:06 -0700496 if (!ENABLE_MINIMIZE) {
Mady Mellor2e138782017-03-27 11:09:50 -0700497 return;
498 }
Winson Chung2a82fe52017-02-02 14:43:34 -0800499 if (mIsMinimized != isMinimized) {
Chenjie Yuae9fdf042018-02-15 10:19:32 -0800500 MetricsLoggerWrapper.logPictureInPictureMinimize(mContext,
501 isMinimized, PipUtils.getTopPinnedActivity(mContext, mActivityManager));
Winson Chung2a82fe52017-02-02 14:43:34 -0800502 }
503 mIsMinimized = isMinimized;
504 mSnapAlgorithm.setMinimized(isMinimized);
Winson Chung54f0c652016-12-06 14:46:31 -0800505
Winson Chung2a82fe52017-02-02 14:43:34 -0800506 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 Chung54f0c652016-12-06 14:46:31 -0800513 try {
514 mPinnedStackController.setIsMinimized(isMinimized);
515 } catch (RemoteException e) {
516 Log.e(TAG, "Could not set minimized state", e);
517 }
Winson Chungfa7053782016-11-08 15:45:10 -0800518 }
519 }
520
521 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800522 * Sets the menu visibility.
Winson Chungfa7053782016-11-08 15:45:10 -0800523 */
Winson Chung8ec0e162017-07-07 14:49:49 -0700524 private void setMenuState(int menuState, boolean resize) {
Winson Chunga89053d2018-06-12 16:47:30 -0700525 if (menuState == MENU_STATE_FULL && mMenuState != MENU_STATE_FULL) {
Winson Chungd2d90972017-02-28 11:40:41 -0800526 // 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 Chung2a82fe52017-02-02 14:43:34 -0800531 mSavedSnapFraction = mMotionHelper.animateToExpandedState(expandedBounds,
532 mMovementBounds, mExpandedMovementBounds);
Winson Chungd2d90972017-02-28 11:40:41 -0800533 }
Winson Chunga89053d2018-06-12 16:47:30 -0700534 } else if (menuState == MENU_STATE_NONE && mMenuState == MENU_STATE_FULL) {
Winson Chungd2d90972017-02-28 11:40:41 -0800535 // Try and restore the PiP to the closest edge, using the saved snap fraction
536 // if possible
537 if (resize) {
Winson Chungef4dc812017-04-11 13:31:44 -0700538 if (mDeferResizeToNormalBoundsUntilRotation == -1) {
Winson Chungbb233762017-05-15 14:20:46 -0700539 // 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 Chungef4dc812017-04-11 13:31:44 -0700547 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 Chungbb233762017-05-15 14:20:46 -0700565 // If resizing is not allowed, then the PiP should be frozen until the transition
566 // ends as well
567 setTouchEnabled(false);
Winson Chungef4dc812017-04-11 13:31:44 -0700568 mSavedSnapFraction = -1f;
Winson Chunga29eb982016-12-14 12:01:27 -0800569 }
Winson Chungfa7053782016-11-08 15:45:10 -0800570 }
Mady Mellor637cd482017-03-21 10:39:42 -0700571 mMenuState = menuState;
572 updateMovementBounds(menuState);
573 if (menuState != MENU_STATE_CLOSE) {
Chenjie Yu52cacc62017-12-08 18:11:45 -0800574 MetricsLoggerWrapper.logPictureInPictureMenuVisible(mContext, menuState == MENU_STATE_FULL);
Mady Mellor637cd482017-03-21 10:39:42 -0700575 }
Winson Chungfa7053782016-11-08 15:45:10 -0800576 }
577
578 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800579 * @return the motion helper.
Winson Chungfa7053782016-11-08 15:45:10 -0800580 */
Winson Chung2a82fe52017-02-02 14:43:34 -0800581 public PipMotionHelper getMotionHelper() {
582 return mMotionHelper;
Winson73bc1592016-10-18 18:47:43 -0700583 }
Winson Chungfa7053782016-11-08 15:45:10 -0800584
585 /**
Winson Chungfa7053782016-11-08 15:45:10 -0800586 * Gesture controlling normal movement of the PIP.
587 */
588 private PipTouchGesture mDefaultMovementGesture = new PipTouchGesture() {
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700589 // Whether the PiP was on the left side of the screen at the start of the gesture
590 private boolean mStartedOnLeft;
Winson Chung8ec0e162017-07-07 14:49:49 -0700591 private final Point mStartPosition = new Point();
592 private final PointF mDelta = new PointF();
Mady Mellord4e40fb2017-01-26 10:43:16 -0800593
594 @Override
595 public void onDown(PipTouchState touchState) {
Winson Chung85d39982017-02-24 15:21:25 -0800596 if (!touchState.isUserInteracting()) {
597 return;
598 }
599
Mady Mellor15b29c72017-06-07 14:53:58 -0700600 Rect bounds = mMotionHelper.getBounds();
Winson Chung8ec0e162017-07-07 14:49:49 -0700601 mDelta.set(0f, 0f);
602 mStartPosition.set(bounds.left, bounds.top);
Mady Mellor15b29c72017-06-07 14:53:58 -0700603 mStartedOnLeft = bounds.left < mMovementBounds.centerX();
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700604 mMovementWithinMinimize = true;
605 mMovementWithinDismiss = touchState.getDownTouchPosition().y >= mMovementBounds.bottom;
606
Mady Mellora7f69742017-02-03 11:00:20 -0800607 // 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 Mellor637cd482017-03-21 10:39:42 -0700609 if (mMenuState != MENU_STATE_NONE && !mIsMinimized) {
Mady Mellora7f69742017-02-03 11:00:20 -0800610 mMenuController.pokeMenu();
611 }
612
Eliot Courtney3dc12f12018-06-01 14:26:26 +0900613 if (mEnableDimissDragToEdge) {
Mady Mellord4e40fb2017-01-26 10:43:16 -0800614 mDismissViewController.createDismissTarget();
615 mHandler.postDelayed(mShowDismissAffordance, SHOW_DISMISS_AFFORDANCE_DELAY);
616 }
617 }
618
Winson Chungfa7053782016-11-08 15:45:10 -0800619 @Override
620 boolean onMove(PipTouchState touchState) {
Winson Chung85d39982017-02-24 15:21:25 -0800621 if (!touchState.isUserInteracting()) {
622 return false;
623 }
624
Winson Chung2a82fe52017-02-02 14:43:34 -0800625 if (touchState.startedDragging()) {
626 mSavedSnapFraction = -1f;
Winson Chung2a82fe52017-02-02 14:43:34 -0800627
Eliot Courtney3dc12f12018-06-01 14:26:26 +0900628 if (mEnableDimissDragToEdge) {
Winson Chungb54b65b2017-04-26 14:02:13 -0700629 mHandler.removeCallbacks(mShowDismissAffordance);
630 mDismissViewController.showDismissTarget();
631 }
Mady Mellord4e40fb2017-01-26 10:43:16 -0800632 }
633
Winson Chungfa7053782016-11-08 15:45:10 -0800634 if (touchState.isDragging()) {
635 // Move the pinned stack freely
Winson Chung2a82fe52017-02-02 14:43:34 -0800636 final PointF lastDelta = touchState.getLastTouchDelta();
Winson Chung8ec0e162017-07-07 14:49:49 -0700637 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 Mellor8c7dc422017-05-10 12:55:06 -0700641 if (!touchState.allowDraggingOffscreen() || !ENABLE_MINIMIZE) {
Winson Chung2a82fe52017-02-02 14:43:34 -0800642 left = Math.max(mMovementBounds.left, Math.min(mMovementBounds.right, left));
Winson Chungfa7053782016-11-08 15:45:10 -0800643 }
Eliot Courtney3dc12f12018-06-01 14:26:26 +0900644 if (mEnableDimissDragToEdge) {
Mady Mellor57d22552017-03-09 15:37:13 -0800645 // 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 Chung8ec0e162017-07-07 14:49:49 -0700650
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 Chungfa7053782016-11-08 15:45:10 -0800656 mTmpBounds.offsetTo((int) left, (int) top);
Winson Chung2a82fe52017-02-02 14:43:34 -0800657 mMotionHelper.movePip(mTmpBounds);
658
Eliot Courtney3dc12f12018-06-01 14:26:26 +0900659 if (mEnableDimissDragToEdge) {
Mady Mellor81d40612017-03-10 15:14:10 -0800660 updateDismissFraction();
661 }
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700662
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 Chungfa7053782016-11-08 15:45:10 -0800674 return true;
675 }
676 return false;
677 }
678
679 @Override
680 public boolean onUp(PipTouchState touchState) {
Eliot Courtney3dc12f12018-06-01 14:26:26 +0900681 if (mEnableDimissDragToEdge) {
Winson Chungb54b65b2017-04-26 14:02:13 -0700682 // 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 Chung85d39982017-02-24 15:21:25 -0800687 if (!touchState.isUserInteracting()) {
688 return false;
689 }
690
Mady Mellor60421c92017-03-29 15:27:37 -0700691 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 Mellor8c7dc422017-05-10 12:55:06 -0700695 final boolean isUpWithinDimiss = ENABLE_FLING_DISMISS
Mady Mellor47ba1402017-04-04 17:25:43 -0700696 && 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 Courtney3dc12f12018-06-01 14:26:26 +0900701 if (mEnableDimissDragToEdge) {
Winson Chungb54b65b2017-04-26 14:02:13 -0700702 // Check if the user dragged or flung the PiP offscreen to dismiss it
703 if (mMotionHelper.shouldDismissPip() || isFlingToBot) {
Chenjie Yuae9fdf042018-02-15 10:19:32 -0800704 MetricsLoggerWrapper.logPictureInPictureDismissByDrag(mContext,
705 PipUtils.getTopPinnedActivity(mContext, mActivityManager));
Winson Chungb54b65b2017-04-26 14:02:13 -0700706 mMotionHelper.animateDismiss(mMotionHelper.getBounds(), vel.x,
707 vel.y, mUpdateScrimListener);
Winson Chungb54b65b2017-04-26 14:02:13 -0700708 return true;
Mady Mellord4e40fb2017-01-26 10:43:16 -0800709 }
Mady Mellord4e40fb2017-01-26 10:43:16 -0800710 }
Winson Chungd2d90972017-02-28 11:40:41 -0800711
Winson Chungfa7053782016-11-08 15:45:10 -0800712 if (touchState.isDragging()) {
Mady Mellor84a0f892017-03-27 14:10:46 -0700713 final boolean isFlingToEdge = isFling && isHorizontal && mMovementWithinMinimize
714 && (mStartedOnLeft ? vel.x < 0 : vel.x > 0);
Mady Mellor8c7dc422017-05-10 12:55:06 -0700715 if (ENABLE_MINIMIZE &&
Mady Mellor2e138782017-03-27 11:09:50 -0700716 !mIsMinimized && (mMotionHelper.shouldMinimizePip() || isFlingToEdge)) {
Mady Mellor3b10dcd2017-01-23 10:08:35 -0800717 // Pip should be minimized
Winson Chung2a82fe52017-02-02 14:43:34 -0800718 setMinimizedStateInternal(true);
Mady Mellor637cd482017-03-21 10:39:42 -0700719 if (mMenuState == MENU_STATE_FULL) {
Winson Chungd2d90972017-02-28 11:40:41 -0800720 // 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 Mellor81d40612017-03-10 15:14:10 -0800725 mMotionHelper.animateToClosestMinimizedState(mMovementBounds,
726 mUpdateScrimListener);
Winson Chungd2d90972017-02-28 11:40:41 -0800727 }
Mady Mellor3b10dcd2017-01-23 10:08:35 -0800728 return true;
729 }
730 if (mIsMinimized) {
Winson Chungd2d90972017-02-28 11:40:41 -0800731 // If we're dragging and it wasn't a minimize gesture then we shouldn't be
732 // minimized.
Winson Chung2a82fe52017-02-02 14:43:34 -0800733 setMinimizedStateInternal(false);
Mady Mellor3b10dcd2017-01-23 10:08:35 -0800734 }
735
Winson Chung87e5d552017-04-05 11:49:38 -0700736 AnimatorListenerAdapter postAnimationCallback = null;
Mady Mellor637cd482017-03-21 10:39:42 -0700737 if (mMenuState != MENU_STATE_NONE) {
Winson Chung87e5d552017-04-05 11:49:38 -0700738 // 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 Mellor637cd482017-03-21 10:39:42 -0700740 mMenuController.showMenu(mMenuState, mMotionHelper.getBounds(),
Winson Chungbb787442017-09-01 11:33:47 -0700741 mMovementBounds, true /* allowMenuTimeout */, willResizeMenu());
Winson Chung87e5d552017-04-05 11:49:38 -0700742 } 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 Chungd2d90972017-02-28 11:40:41 -0800751 }
752
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700753 if (isFling) {
Mady Mellor81d40612017-03-10 15:14:10 -0800754 mMotionHelper.flingToSnapTarget(velocity, vel.x, vel.y, mMovementBounds,
Mady Mellor15b29c72017-06-07 14:53:58 -0700755 mUpdateScrimListener, postAnimationCallback,
756 mStartPosition);
Winson Chungfa7053782016-11-08 15:45:10 -0800757 } else {
Winson Chung87e5d552017-04-05 11:49:38 -0700758 mMotionHelper.animateToClosestSnapTarget(mMovementBounds, mUpdateScrimListener,
759 postAnimationCallback);
Winson Chungfa7053782016-11-08 15:45:10 -0800760 }
Mady Mellor3b10dcd2017-01-23 10:08:35 -0800761 } else if (mIsMinimized) {
762 // This was a tap, so no longer minimized
Winson Chung87e5d552017-04-05 11:49:38 -0700763 mMotionHelper.animateToClosestSnapTarget(mMovementBounds, null /* updateListener */,
764 null /* animatorListener */);
Winson Chung2a82fe52017-02-02 14:43:34 -0800765 setMinimizedStateInternal(false);
Mady Mellor637cd482017-03-21 10:39:42 -0700766 } else if (mMenuState != MENU_STATE_FULL) {
Winson Chungbca03112017-08-16 10:38:15 -0700767 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 Chungfa7053782016-11-08 15:45:10 -0800781 } else {
Winson Chungbe4a8082017-04-13 13:52:04 -0700782 mMenuController.hideMenu();
Winson Chung2a82fe52017-02-02 14:43:34 -0800783 mMotionHelper.expandPip();
Winson Chungfa7053782016-11-08 15:45:10 -0800784 }
785 return true;
786 }
787 };
Mady Mellor3b10dcd2017-01-23 10:08:35 -0800788
789 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800790 * Updates the current movement bounds based on whether the menu is currently visible.
791 */
Mady Mellor637cd482017-03-21 10:39:42 -0700792 private void updateMovementBounds(int menuState) {
Winson Chunga71febe2017-05-22 11:14:22 -0700793 boolean isMenuExpanded = menuState == MENU_STATE_FULL;
794 mMovementBounds = isMenuExpanded
Winson Chung2a82fe52017-02-02 14:43:34 -0800795 ? mExpandedMovementBounds
796 : mNormalMovementBounds;
Winson Chunga71febe2017-05-22 11:14:22 -0700797 try {
798 mPinnedStackController.setMinEdgeSize(isMenuExpanded ? mExpandedShortestEdgeSize : 0);
799 } catch (RemoteException e) {
800 Log.e(TAG, "Could not set minimized state", e);
801 }
Winson Chung2a82fe52017-02-02 14:43:34 -0800802 }
Winson Chung29a78652017-02-09 18:35:26 -0800803
Winson Chungb54b65b2017-04-26 14:02:13 -0700804 /**
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 Chung9b919412017-06-19 17:01:51 -0700812 /**
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 Chungbb787442017-09-01 11:33:47 -0700822 /**
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 Chung29a78652017-02-09 18:35:26 -0800830 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 Mellor637cd482017-03-21 10:39:42 -0700838 pw.println(innerPrefix + "mMenuState=" + mMenuState);
Winson Chungd2d90972017-02-28 11:40:41 -0800839 pw.println(innerPrefix + "mIsMinimized=" + mIsMinimized);
Winson Chung29a78652017-02-09 18:35:26 -0800840 pw.println(innerPrefix + "mIsImeShowing=" + mIsImeShowing);
841 pw.println(innerPrefix + "mImeHeight=" + mImeHeight);
Tracy Zhou43513082018-03-08 21:58:36 -0800842 pw.println(innerPrefix + "mIsShelfShowing=" + mIsShelfShowing);
843 pw.println(innerPrefix + "mShelfHeight=" + mShelfHeight);
Winson Chung29a78652017-02-09 18:35:26 -0800844 pw.println(innerPrefix + "mSavedSnapFraction=" + mSavedSnapFraction);
Eliot Courtney3dc12f12018-06-01 14:26:26 +0900845 pw.println(innerPrefix + "mEnableDragToEdgeDismiss=" + mEnableDimissDragToEdge);
Mady Mellor8c7dc422017-05-10 12:55:06 -0700846 pw.println(innerPrefix + "mEnableMinimize=" + ENABLE_MINIMIZE);
Winson Chung29a78652017-02-09 18:35:26 -0800847 mSnapAlgorithm.dump(pw, innerPrefix);
848 mTouchState.dump(pw, innerPrefix);
849 mMotionHelper.dump(pw, innerPrefix);
850 }
Phil Weaverf00cd142017-03-03 13:44:00 -0800851
Winson73bc1592016-10-18 18:47:43 -0700852}