blob: 59c962da7baa0bc25dd9b345841503b99a728e37 [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
Hongwei Wang081ecb32020-04-13 16:49:03 -070019import static com.android.systemui.pip.PipAnimationController.TRANSITION_DIRECTION_TO_PIP;
Mady Mellor637cd482017-03-21 10:39:42 -070020import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_CLOSE;
21import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_FULL;
Chenjie Yuae9fdf042018-02-15 10:19:32 -080022import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_NONE;
Mady Mellor637cd482017-03-21 10:39:42 -070023
Joshua Tsujif39539d2020-04-03 18:53:06 -040024import android.annotation.SuppressLint;
Winson73bc1592016-10-18 18:47:43 -070025import android.app.IActivityManager;
Winson Chung9b919412017-06-19 17:01:51 -070026import android.content.ComponentName;
Winson73bc1592016-10-18 18:47:43 -070027import android.content.Context;
Winson Chung136d1ec82017-07-18 11:16:30 -070028import android.content.res.Resources;
Joshua Tsujif39539d2020-04-03 18:53:06 -040029import android.graphics.PixelFormat;
Mady Mellora7f69742017-02-03 11:00:20 -080030import android.graphics.Point;
Winson73bc1592016-10-18 18:47:43 -070031import android.graphics.PointF;
32import android.graphics.Rect;
Mady Mellord4e40fb2017-01-26 10:43:16 -080033import android.os.Handler;
Winson73bc1592016-10-18 18:47:43 -070034import android.os.RemoteException;
35import android.util.Log;
Winson Chungbe00a832020-03-10 11:08:35 -070036import android.util.Pair;
Mady Mellora7f69742017-02-03 11:00:20 -080037import android.util.Size;
Joshua Tsujif39539d2020-04-03 18:53:06 -040038import android.view.Gravity;
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;
Joshua Tsujif39539d2020-04-03 18:53:06 -040042import android.view.View;
Winson73bc1592016-10-18 18:47:43 -070043import android.view.ViewConfiguration;
Joshua Tsujif39539d2020-04-03 18:53:06 -040044import android.view.ViewGroup;
45import android.view.WindowManager;
Phil Weaverf00cd142017-03-03 13:44:00 -080046import android.view.accessibility.AccessibilityEvent;
47import android.view.accessibility.AccessibilityManager;
48import android.view.accessibility.AccessibilityNodeInfo;
Phil Weaver0a8caa12017-08-09 11:28:41 -070049import android.view.accessibility.AccessibilityWindowInfo;
Joshua Tsujif39539d2020-04-03 18:53:06 -040050import android.widget.FrameLayout;
51
52import androidx.annotation.NonNull;
53import androidx.dynamicanimation.animation.DynamicAnimation;
54import androidx.dynamicanimation.animation.SpringForce;
Chenjie Yuae9fdf042018-02-15 10:19:32 -080055
Ben Lin094c7752020-03-11 12:10:51 -070056import com.android.internal.annotations.VisibleForTesting;
Chenjie Yu52cacc62017-12-08 18:11:45 -080057import com.android.internal.os.logging.MetricsLoggerWrapper;
Mady Mellora7f69742017-02-03 11:00:20 -080058import com.android.systemui.R;
Hongwei Wang081ecb32020-04-13 16:49:03 -070059import com.android.systemui.pip.PipAnimationController;
Hongwei Wang43a752b2019-09-17 20:20:30 +000060import com.android.systemui.pip.PipBoundsHandler;
Hongwei Wangebf18082019-09-26 14:25:11 -070061import com.android.systemui.pip.PipSnapAlgorithm;
Hongwei Wang85cf41f2020-01-15 15:14:47 -080062import com.android.systemui.pip.PipTaskOrganizer;
Winson Chungcbb15a92018-01-25 17:46:16 +000063import com.android.systemui.shared.system.InputConsumerController;
Winson73bc1592016-10-18 18:47:43 -070064import com.android.systemui.statusbar.FlingAnimationUtils;
Beverly660d0a72020-02-26 12:32:42 -050065import com.android.systemui.util.DeviceConfigProxy;
Joshua Tsujif39539d2020-04-03 18:53:06 -040066import com.android.systemui.util.DismissCircleView;
Joshua Tsujic81ff3d2020-02-13 14:48:40 -050067import com.android.systemui.util.FloatingContentCoordinator;
Joshua Tsujif39539d2020-04-03 18:53:06 -040068import com.android.systemui.util.animation.PhysicsAnimator;
69import com.android.systemui.util.magnetictarget.MagnetizedObject;
Winson73bc1592016-10-18 18:47:43 -070070
Winson Chung29a78652017-02-09 18:35:26 -080071import java.io.PrintWriter;
72
Winson73bc1592016-10-18 18:47:43 -070073/**
74 * Manages all the touch handling for PIP on the Phone, including moving, dismissing and expanding
75 * the PIP.
76 */
Mady Mellor8c7dc422017-05-10 12:55:06 -070077public class PipTouchHandler {
Winson73bc1592016-10-18 18:47:43 -070078 private static final String TAG = "PipTouchHandler";
Winson73bc1592016-10-18 18:47:43 -070079
Mady Mellor5d58d252017-04-18 12:48:04 -070080 private static final int SHOW_DISMISS_AFFORDANCE_DELAY = 225;
Tracy Zhoue19d0512018-07-31 16:29:34 -070081 private static final int BOTTOM_OFFSET_BUFFER_DP = 1;
Winson Chungfa7053782016-11-08 15:45:10 -080082
Winson Chung6e35ee1f2017-02-14 12:06:44 -080083 // Allow dragging the PIP to a location to close it
Hongwei Wang85cf41f2020-01-15 15:14:47 -080084 private final boolean mEnableDismissDragToEdge;
Ben Line55f85f2020-01-27 14:49:41 -080085 // Allow PIP to resize to a slightly bigger state upon touch
86 private final boolean mEnableResize;
Winson73bc1592016-10-18 18:47:43 -070087 private final Context mContext;
Joshua Tsujif39539d2020-04-03 18:53:06 -040088 private final WindowManager mWindowManager;
Winson73bc1592016-10-18 18:47:43 -070089 private final IActivityManager mActivityManager;
Hongwei Wang43a752b2019-09-17 20:20:30 +000090 private final PipBoundsHandler mPipBoundsHandler;
Ben Lin094c7752020-03-11 12:10:51 -070091 private PipResizeGestureHandler mPipResizeGestureHandler;
Winson Chung655332c2016-10-31 13:14:28 -070092 private IPinnedStackController mPinnedStackController;
Winson73bc1592016-10-18 18:47:43 -070093
Winson Chung2a82fe52017-02-02 14:43:34 -080094 private final PipMenuActivityController mMenuController;
Winson Chung14fefc22016-11-02 10:02:29 -070095 private final PipSnapAlgorithm mSnapAlgorithm;
Phil Weaverf00cd142017-03-03 13:44:00 -080096 private final AccessibilityManager mAccessibilityManager;
Wale Ogunwale6455e502017-04-17 14:16:43 -070097 private boolean mShowPipMenuOnAnimationEnd = false;
Winson73bc1592016-10-18 18:47:43 -070098
Joshua Tsujif39539d2020-04-03 18:53:06 -040099 /**
100 * MagnetizedObject wrapper for PIP. This allows the magnetic target library to locate and move
101 * PIP.
102 */
103 private MagnetizedObject<Rect> mMagnetizedPip;
104
105 /**
106 * Container for the dismiss circle, so that it can be animated within the container via
107 * translation rather than within the WindowManager via slow layout animations.
108 */
109 private ViewGroup mTargetViewContainer;
110
111 /** Circle view used to render the dismiss target. */
112 private DismissCircleView mTargetView;
113
114 /**
115 * MagneticTarget instance wrapping the target view and allowing us to set its magnetic radius.
116 */
117 private MagnetizedObject.MagneticTarget mMagneticTarget;
118
119 /** PhysicsAnimator instance for animating the dismiss target in/out. */
120 private PhysicsAnimator<View> mMagneticTargetAnimator;
121
122 /** Default configuration to use for springing the dismiss target in/out. */
123 private final PhysicsAnimator.SpringConfig mTargetSpringConfig =
124 new PhysicsAnimator.SpringConfig(
125 SpringForce.STIFFNESS_MEDIUM, SpringForce.DAMPING_RATIO_NO_BOUNCY);
126
Winson Chung2a82fe52017-02-02 14:43:34 -0800127 // The current movement bounds
128 private Rect mMovementBounds = new Rect();
Ben Lin094c7752020-03-11 12:10:51 -0700129 // The current resized bounds, changed by user resize.
130 // This is used during expand/un-expand to save/restore the user's resized size.
131 @VisibleForTesting Rect mResizedBounds = new Rect();
Winson Chung2a82fe52017-02-02 14:43:34 -0800132
Winson Chung0a2f34f2017-08-11 18:13:24 -0700133 // The reference inset bounds, used to determine the dismiss fraction
134 private Rect mInsetBounds = new Rect();
Ben Lin06dc41f2020-03-05 00:39:27 +0000135 // The reference bounds used to calculate the normal/expanded target bounds
136 private Rect mNormalBounds = new Rect();
Ben Lin094c7752020-03-11 12:10:51 -0700137 @VisibleForTesting Rect mNormalMovementBounds = new Rect();
Ben Lin06dc41f2020-03-05 00:39:27 +0000138 private Rect mExpandedBounds = new Rect();
Ben Lin094c7752020-03-11 12:10:51 -0700139 @VisibleForTesting Rect mExpandedMovementBounds = new Rect();
Mady Mellora7f69742017-02-03 11:00:20 -0800140 private int mExpandedShortestEdgeSize;
Winson73bc1592016-10-18 18:47:43 -0700141
Winson Chungef4dc812017-04-11 13:31:44 -0700142 // Used to workaround an issue where the WM rotation happens before we are notified, allowing
143 // us to send stale bounds
144 private int mDeferResizeToNormalBoundsUntilRotation = -1;
145 private int mDisplayRotation;
146
Joshua Tsujif39539d2020-04-03 18:53:06 -0400147 /**
148 * Runnable that can be posted delayed to show the target. This needs to be saved as a member
149 * variable so we can pass it to removeCallbacks.
150 */
151 private Runnable mShowTargetAction = this::showDismissTargetMaybe;
152
Mady Mellord4e40fb2017-01-26 10:43:16 -0800153 private Handler mHandler = new Handler();
Mady Mellord4e40fb2017-01-26 10:43:16 -0800154
Winson Chungfa7053782016-11-08 15:45:10 -0800155 // Behaviour states
Winson Chung9b919412017-06-19 17:01:51 -0700156 private int mMenuState = MENU_STATE_NONE;
Winson Chung2a82fe52017-02-02 14:43:34 -0800157 private boolean mIsImeShowing;
158 private int mImeHeight;
Winson Chung136d1ec82017-07-18 11:16:30 -0700159 private int mImeOffset;
Joshua Tsujif39539d2020-04-03 18:53:06 -0400160 private int mDismissAreaHeight;
Tracy Zhou43513082018-03-08 21:58:36 -0800161 private boolean mIsShelfShowing;
162 private int mShelfHeight;
Winson Chungdec1a662019-06-04 21:20:41 -0700163 private int mMovementBoundsExtraOffsets;
Winson Chung2a82fe52017-02-02 14:43:34 -0800164 private float mSavedSnapFraction = -1f;
Phil Weaverf00cd142017-03-03 13:44:00 -0800165 private boolean mSendingHoverAccessibilityEvents;
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700166 private boolean mMovementWithinDismiss;
Arthur Hungd8961972019-10-14 17:00:17 +0800167 private PipAccessibilityInteractionConnection mConnection;
Winson73bc1592016-10-18 18:47:43 -0700168
Winson Chungfa7053782016-11-08 15:45:10 -0800169 // Touch state
170 private final PipTouchState mTouchState;
Winson73bc1592016-10-18 18:47:43 -0700171 private final FlingAnimationUtils mFlingAnimationUtils;
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500172 private final FloatingContentCoordinator mFloatingContentCoordinator;
Ben Lin094c7752020-03-11 12:10:51 -0700173 private PipMotionHelper mMotionHelper;
Ben Lin1a80f122020-01-27 14:09:52 -0800174 private PipTouchGesture mGesture;
Winson73bc1592016-10-18 18:47:43 -0700175
Winson Chung2a82fe52017-02-02 14:43:34 -0800176 // Temp vars
Winson Chung655332c2016-10-31 13:14:28 -0700177 private final Rect mTmpBounds = new Rect();
178
Winson73bc1592016-10-18 18:47:43 -0700179 /**
Winson Chung15504af2016-11-02 18:11:36 -0700180 * A listener for the PIP menu activity.
181 */
182 private class PipMenuListener implements PipMenuActivityController.Listener {
183 @Override
Hongwei Wang7c622402020-05-07 20:45:22 -0700184 public void onPipMenuStateChanged(int menuState, boolean resize, Runnable callback) {
185 setMenuState(menuState, resize, callback);
Winson Chung15504af2016-11-02 18:11:36 -0700186 }
Winson Chunga29eb982016-12-14 12:01:27 -0800187
188 @Override
189 public void onPipExpand() {
Winson Chungc4d4ee82020-05-05 12:51:06 -0700190 mMotionHelper.expandPipToFullscreen();
Winson Chunga29eb982016-12-14 12:01:27 -0800191 }
192
193 @Override
194 public void onPipDismiss() {
Winson Chungbe00a832020-03-10 11:08:35 -0700195 Pair<ComponentName, Integer> topPipActivity = PipUtils.getTopPipActivity(mContext,
196 mActivityManager);
197 if (topPipActivity.first != null) {
198 MetricsLoggerWrapper.logPictureInPictureDismissByTap(mContext, topPipActivity);
199 }
Winson Chung2a82fe52017-02-02 14:43:34 -0800200 mMotionHelper.dismissPip();
Winson Chunga29eb982016-12-14 12:01:27 -0800201 }
Mady Mellor637cd482017-03-21 10:39:42 -0700202
203 @Override
204 public void onPipShowMenu() {
205 mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
Hongwei Wang7c622402020-05-07 20:45:22 -0700206 true /* allowMenuTimeout */, willResizeMenu());
Mady Mellor637cd482017-03-21 10:39:42 -0700207 }
Winson Chung15504af2016-11-02 18:11:36 -0700208 }
209
Joshua Tsujif39539d2020-04-03 18:53:06 -0400210 @SuppressLint("InflateParams")
Winson Chungd2d90972017-02-28 11:40:41 -0800211 public PipTouchHandler(Context context, IActivityManager activityManager,
Winson Chungc4d4ee82020-05-05 12:51:06 -0700212 PipMenuActivityController menuController,
Hongwei Wang43a752b2019-09-17 20:20:30 +0000213 InputConsumerController inputConsumerController,
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500214 PipBoundsHandler pipBoundsHandler,
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800215 PipTaskOrganizer pipTaskOrganizer,
Beverly660d0a72020-02-26 12:32:42 -0500216 FloatingContentCoordinator floatingContentCoordinator,
jorgegil@google.com5bdec5f2020-02-28 13:14:39 -0800217 DeviceConfigProxy deviceConfig,
218 PipSnapAlgorithm pipSnapAlgorithm) {
Winson73bc1592016-10-18 18:47:43 -0700219 // Initialize the Pip input consumer
Winson73bc1592016-10-18 18:47:43 -0700220 mContext = context;
221 mActivityManager = activityManager;
Phil Weaverf00cd142017-03-03 13:44:00 -0800222 mAccessibilityManager = context.getSystemService(AccessibilityManager.class);
Joshua Tsujif39539d2020-04-03 18:53:06 -0400223 mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Winson Chung15504af2016-11-02 18:11:36 -0700224 mMenuController = menuController;
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800225 mMenuController.addListener(new PipMenuListener());
jorgegil@google.com5bdec5f2020-02-28 13:14:39 -0800226 mSnapAlgorithm = pipSnapAlgorithm;
Dave Mankoff1373fdb2019-12-18 14:04:37 -0500227 mFlingAnimationUtils = new FlingAnimationUtils(context.getResources().getDisplayMetrics(),
228 2.5f);
Ben Lin1a80f122020-01-27 14:09:52 -0800229 mGesture = new DefaultPipTouchGesture();
Winson Chungc4d4ee82020-05-05 12:51:06 -0700230 mMotionHelper = new PipMotionHelper(mContext, pipTaskOrganizer, mMenuController,
231 mSnapAlgorithm, floatingContentCoordinator);
Ben Lin04c83f62019-12-20 10:56:45 -0800232 mPipResizeGestureHandler =
Hongwei Wang081ecb32020-04-13 16:49:03 -0700233 new PipResizeGestureHandler(context, pipBoundsHandler, mMotionHelper,
Ben Lin4a8e2572020-05-08 15:55:15 -0700234 deviceConfig, pipTaskOrganizer, this::getMovementBounds,
235 this::updateMovementBounds);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800236 mTouchState = new PipTouchState(ViewConfiguration.get(context), mHandler,
Hongwei Wang7c622402020-05-07 20:45:22 -0700237 () -> mMenuController.showMenuWithDelay(MENU_STATE_FULL, mMotionHelper.getBounds(),
238 true /* allowMenuTimeout */, willResizeMenu()));
Winson Chung136d1ec82017-07-18 11:16:30 -0700239
240 Resources res = context.getResources();
241 mExpandedShortestEdgeSize = res.getDimensionPixelSize(
Mady Mellora7f69742017-02-03 11:00:20 -0800242 R.dimen.pip_expanded_shortest_edge_size);
Winson Chung136d1ec82017-07-18 11:16:30 -0700243 mImeOffset = res.getDimensionPixelSize(R.dimen.pip_ime_offset);
Joshua Tsujif39539d2020-04-03 18:53:06 -0400244 mDismissAreaHeight = res.getDimensionPixelSize(R.dimen.floating_dismiss_gradient_height);
Winson Chungd2d90972017-02-28 11:40:41 -0800245
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800246 mEnableDismissDragToEdge = res.getBoolean(R.bool.config_pipEnableDismissDragToEdge);
Ben Line55f85f2020-01-27 14:49:41 -0800247 mEnableResize = res.getBoolean(R.bool.config_pipEnableResizeForMenu);
Eliot Courtney3dc12f12018-06-01 14:26:26 +0900248
Winson Chungd2d90972017-02-28 11:40:41 -0800249 // Register the listener for input consumer touch events
Tracy Zhou535c2122019-02-21 17:02:47 -0800250 inputConsumerController.setInputListener(this::handleTouchEvent);
Phil Weaverf00cd142017-03-03 13:44:00 -0800251 inputConsumerController.setRegistrationListener(this::onRegistrationChanged);
Hongwei Wang43a752b2019-09-17 20:20:30 +0000252
253 mPipBoundsHandler = pipBoundsHandler;
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500254 mFloatingContentCoordinator = floatingContentCoordinator;
Arthur Hungd8961972019-10-14 17:00:17 +0800255 mConnection = new PipAccessibilityInteractionConnection(mMotionHelper,
256 this::onAccessibilityShowMenu, mHandler);
Joshua Tsujif39539d2020-04-03 18:53:06 -0400257
258 final int targetSize = res.getDimensionPixelSize(R.dimen.dismiss_circle_size);
259 mTargetView = new DismissCircleView(context);
260 final FrameLayout.LayoutParams newParams =
261 new FrameLayout.LayoutParams(targetSize, targetSize);
262 newParams.gravity = Gravity.CENTER;
263 mTargetView.setLayoutParams(newParams);
264
265 mTargetViewContainer = new FrameLayout(context);
266 mTargetViewContainer.setClipChildren(false);
267 mTargetViewContainer.addView(mTargetView);
268
269 mMagnetizedPip = mMotionHelper.getMagnetizedPip();
270 mMagneticTarget = mMagnetizedPip.addTarget(mTargetView, 0);
Joshua Tsujia3dfef42020-04-15 14:09:48 -0400271
272 // Set the magnetic field radius equal to twice the size of the target.
273 mMagneticTarget.setMagneticFieldRadiusPx(targetSize * 2);
274
Joshua Tsujif39539d2020-04-03 18:53:06 -0400275 mMagnetizedPip.setPhysicsAnimatorUpdateListener(mMotionHelper.mResizePipUpdateListener);
276 mMagnetizedPip.setMagnetListener(new MagnetizedObject.MagnetListener() {
277 @Override
278 public void onStuckToTarget(@NonNull MagnetizedObject.MagneticTarget target) {
279 mMotionHelper.prepareForAnimation();
280
281 // Show the dismiss target, in case the initial touch event occurred within the
282 // magnetic field radius.
283 showDismissTargetMaybe();
284 }
285
286 @Override
287 public void onUnstuckFromTarget(@NonNull MagnetizedObject.MagneticTarget target,
288 float velX, float velY, boolean wasFlungOut) {
289 if (wasFlungOut) {
290 mMotionHelper.flingToSnapTarget(velX, velY, null, null);
291 hideDismissTarget();
292 } else {
293 mMotionHelper.setSpringingToTouch(true);
294 }
295 }
296
297 @Override
298 public void onReleasedInTarget(@NonNull MagnetizedObject.MagneticTarget target) {
299 mHandler.post(() -> {
300 mMotionHelper.animateDismiss(0, 0, null);
301 hideDismissTarget();
302 });
303
304
305 MetricsLoggerWrapper.logPictureInPictureDismissByDrag(mContext,
306 PipUtils.getTopPipActivity(mContext, mActivityManager));
307 }
308 });
309
310 mMagneticTargetAnimator = PhysicsAnimator.getInstance(mTargetView);
Winson73bc1592016-10-18 18:47:43 -0700311 }
312
Ben Lin1a80f122020-01-27 14:09:52 -0800313 public void setTouchGesture(PipTouchGesture gesture) {
314 mGesture = gesture;
315 }
316
Winson Chung85d39982017-02-24 15:21:25 -0800317 public void setTouchEnabled(boolean enabled) {
318 mTouchState.setAllowTouches(enabled);
319 }
320
Winson Chungac52f282017-03-30 14:44:52 -0700321 public void showPictureInPictureMenu() {
322 // Only show the menu if the user isn't currently interacting with the PiP
323 if (!mTouchState.isUserInteracting()) {
Mady Mellor637cd482017-03-21 10:39:42 -0700324 mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
Hongwei Wang7c622402020-05-07 20:45:22 -0700325 false /* allowMenuTimeout */, willResizeMenu());
Winson Chungac52f282017-03-30 14:44:52 -0700326 }
327 }
328
Winson Chung929d4f72017-01-13 10:21:33 -0800329 public void onActivityPinned() {
Joshua Tsujif7de6802020-04-13 13:30:11 -0400330 createOrUpdateDismissTarget();
Joshua Tsujif39539d2020-04-03 18:53:06 -0400331
Wale Ogunwale6455e502017-04-17 14:16:43 -0700332 mShowPipMenuOnAnimationEnd = true;
Ben Lin04c83f62019-12-20 10:56:45 -0800333 mPipResizeGestureHandler.onActivityPinned();
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500334 mFloatingContentCoordinator.onContentAdded(mMotionHelper);
Winson Chung929d4f72017-01-13 10:21:33 -0800335 }
336
Winson Chung9b919412017-06-19 17:01:51 -0700337 public void onActivityUnpinned(ComponentName topPipActivity) {
338 if (topPipActivity == null) {
339 // Clean up state after the last PiP activity is removed
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500340 cleanUpDismissTarget();
341
342 mFloatingContentCoordinator.onContentRemoved(mMotionHelper);
Winson Chung9b919412017-06-19 17:01:51 -0700343 }
Ben Lin094c7752020-03-11 12:10:51 -0700344 mResizedBounds.setEmpty();
Ben Lin04c83f62019-12-20 10:56:45 -0800345 mPipResizeGestureHandler.onActivityUnpinned();
Winson Chung9b919412017-06-19 17:01:51 -0700346 }
347
Hongwei Wang081ecb32020-04-13 16:49:03 -0700348 public void onPinnedStackAnimationEnded(
349 @PipAnimationController.TransitionDirection int direction) {
Winson Chungac52f282017-03-30 14:44:52 -0700350 // Always synchronize the motion helper bounds once PiP animations finish
351 mMotionHelper.synchronizePinnedStackBounds();
Ben Lin094c7752020-03-11 12:10:51 -0700352 updateMovementBounds();
Hongwei Wang081ecb32020-04-13 16:49:03 -0700353 if (direction == TRANSITION_DIRECTION_TO_PIP) {
354 // updates mResizedBounds only if it's an entering PiP animation
355 // mResized should be otherwise updated in setMenuState.
356 mResizedBounds.set(mMotionHelper.getBounds());
357 }
Wale Ogunwale6455e502017-04-17 14:16:43 -0700358
359 if (mShowPipMenuOnAnimationEnd) {
360 mMenuController.showMenu(MENU_STATE_CLOSE, mMotionHelper.getBounds(),
Hongwei Wang7c622402020-05-07 20:45:22 -0700361 true /* allowMenuTimeout */, false /* willResizeMenu */);
Wale Ogunwale6455e502017-04-17 14:16:43 -0700362 mShowPipMenuOnAnimationEnd = false;
363 }
Winson Chungac52f282017-03-30 14:44:52 -0700364 }
365
Winson Chung303c6b72016-10-24 17:12:49 -0700366 public void onConfigurationChanged() {
Winson Chung2a82fe52017-02-02 14:43:34 -0800367 mMotionHelper.onConfigurationChanged();
368 mMotionHelper.synchronizePinnedStackBounds();
Joshua Tsujif39539d2020-04-03 18:53:06 -0400369
370 // Recreate the dismiss target for the new orientation.
Joshua Tsujif7de6802020-04-13 13:30:11 -0400371 createOrUpdateDismissTarget();
Winson Chung303c6b72016-10-24 17:12:49 -0700372 }
373
Winson Chung2a82fe52017-02-02 14:43:34 -0800374 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
375 mIsImeShowing = imeVisible;
376 mImeHeight = imeHeight;
377 }
378
Tracy Zhou43513082018-03-08 21:58:36 -0800379 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight) {
380 mIsShelfShowing = shelfVisible;
381 mShelfHeight = shelfHeight;
382 }
383
Ben Lin06dc41f2020-03-05 00:39:27 +0000384 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds, Rect curBounds,
Tracy Zhou43513082018-03-08 21:58:36 -0800385 boolean fromImeAdjustment, boolean fromShelfAdjustment, int displayRotation) {
386 final int bottomOffset = mIsImeShowing ? mImeHeight : 0;
Hongwei Wangbede2c12020-02-26 12:11:53 -0800387 final boolean fromDisplayRotationChanged = (mDisplayRotation != displayRotation);
388 if (fromDisplayRotationChanged) {
389 mTouchState.reset();
390 }
Tracy Zhou43513082018-03-08 21:58:36 -0800391
Winson Chung2a82fe52017-02-02 14:43:34 -0800392 // Re-calculate the expanded bounds
Hongwei Wang3c981f62020-04-07 16:16:26 -0700393 mNormalBounds.set(normalBounds);
Ben Lin06dc41f2020-03-05 00:39:27 +0000394 Rect normalMovementBounds = new Rect();
395 mSnapAlgorithm.getMovementBounds(mNormalBounds, insetBounds, normalMovementBounds,
Tracy Zhou43513082018-03-08 21:58:36 -0800396 bottomOffset);
Mady Mellora7f69742017-02-03 11:00:20 -0800397
Hongwei Wang3c981f62020-04-07 16:16:26 -0700398 if (mMovementBounds.isEmpty()) {
399 // mMovementBounds is not initialized yet and a clean movement bounds without
400 // bottom offset shall be used later in this function.
401 mSnapAlgorithm.getMovementBounds(curBounds, insetBounds, mMovementBounds,
402 0 /* bottomOffset */);
403 }
404
Mady Mellora7f69742017-02-03 11:00:20 -0800405 // Calculate the expanded size
Ben Lin06dc41f2020-03-05 00:39:27 +0000406 float aspectRatio = (float) normalBounds.width() / normalBounds.height();
Mady Mellora7f69742017-02-03 11:00:20 -0800407 Point displaySize = new Point();
408 mContext.getDisplay().getRealSize(displaySize);
Ben Lin06dc41f2020-03-05 00:39:27 +0000409 Size expandedSize = mSnapAlgorithm.getSizeForAspectRatio(aspectRatio,
Mady Mellora7f69742017-02-03 11:00:20 -0800410 mExpandedShortestEdgeSize, displaySize.x, displaySize.y);
Ben Lin06dc41f2020-03-05 00:39:27 +0000411 mExpandedBounds.set(0, 0, expandedSize.getWidth(), expandedSize.getHeight());
Ben Lin06dc41f2020-03-05 00:39:27 +0000412 Rect expandedMovementBounds = new Rect();
413 mSnapAlgorithm.getMovementBounds(mExpandedBounds, insetBounds, expandedMovementBounds,
Tracy Zhou43513082018-03-08 21:58:36 -0800414 bottomOffset);
Winson Chung2a82fe52017-02-02 14:43:34 -0800415
Ben Lin094c7752020-03-11 12:10:51 -0700416 mPipResizeGestureHandler.updateMinSize(mNormalBounds.width(), mNormalBounds.height());
417 mPipResizeGestureHandler.updateMaxSize(mExpandedBounds.width(), mExpandedBounds.height());
418
Winson Chungdec1a662019-06-04 21:20:41 -0700419 // The extra offset does not really affect the movement bounds, but are applied based on the
420 // current state (ime showing, or shelf offset) when we need to actually shift
421 int extraOffset = Math.max(
422 mIsImeShowing ? mImeOffset : 0,
423 !mIsImeShowing && mIsShelfShowing ? mShelfHeight : 0);
424
Tracy Zhou43513082018-03-08 21:58:36 -0800425 // If this is from an IME or shelf adjustment, then we should move the PiP so that it is not
426 // occluded by the IME or shelf.
Hongwei Wangbede2c12020-02-26 12:11:53 -0800427 if (fromImeAdjustment || fromShelfAdjustment || fromDisplayRotationChanged) {
Winson Chung2a82fe52017-02-02 14:43:34 -0800428 if (mTouchState.isUserInteracting()) {
429 // Defer the update of the current movement bounds until after the user finishes
430 // touching the screen
431 } else {
Winson Chungdec1a662019-06-04 21:20:41 -0700432 final float offsetBufferPx = BOTTOM_OFFSET_BUFFER_DP
Tracy Zhoue19d0512018-07-31 16:29:34 -0700433 * mContext.getResources().getDisplayMetrics().density;
Ben Line55f85f2020-01-27 14:49:41 -0800434 final Rect toMovementBounds = mMenuState == MENU_STATE_FULL && willResizeMenu()
Ben Lin06dc41f2020-03-05 00:39:27 +0000435 ? new Rect(expandedMovementBounds)
436 : new Rect(normalMovementBounds);
Winson Chungdec1a662019-06-04 21:20:41 -0700437 final int prevBottom = mMovementBounds.bottom - mMovementBoundsExtraOffsets;
438 final int toBottom = toMovementBounds.bottom < toMovementBounds.top
439 ? toMovementBounds.bottom
440 : toMovementBounds.bottom - extraOffset;
441 if ((Math.min(prevBottom, toBottom) - offsetBufferPx) <= curBounds.top
442 && curBounds.top <= (Math.max(prevBottom, toBottom) + offsetBufferPx)) {
443 mMotionHelper.animateToOffset(curBounds, toBottom - curBounds.top);
Tracy Zhou805e66c2018-04-04 23:45:26 -0700444 }
Winson Chung2a82fe52017-02-02 14:43:34 -0800445 }
Winson Chung14fbe142016-12-19 16:18:24 -0800446 }
Winson Chunga29eb982016-12-14 12:01:27 -0800447
Winson Chung2a82fe52017-02-02 14:43:34 -0800448 // Update the movement bounds after doing the calculations based on the old movement bounds
449 // above
Hongwei Wang3c981f62020-04-07 16:16:26 -0700450 mNormalMovementBounds.set(normalMovementBounds);
451 mExpandedMovementBounds.set(expandedMovementBounds);
Winson Chungef4dc812017-04-11 13:31:44 -0700452 mDisplayRotation = displayRotation;
Winson Chung0a2f34f2017-08-11 18:13:24 -0700453 mInsetBounds.set(insetBounds);
Ben Lin094c7752020-03-11 12:10:51 -0700454 updateMovementBounds();
Winson Chungdec1a662019-06-04 21:20:41 -0700455 mMovementBoundsExtraOffsets = extraOffset;
Winson Chungef4dc812017-04-11 13:31:44 -0700456
457 // If we have a deferred resize, apply it now
458 if (mDeferResizeToNormalBoundsUntilRotation == displayRotation) {
Ben Lin06dc41f2020-03-05 00:39:27 +0000459 mMotionHelper.animateToUnexpandedState(normalBounds, mSavedSnapFraction,
460 mNormalMovementBounds, mMovementBounds, true /* immediate */);
Winson Chungef4dc812017-04-11 13:31:44 -0700461 mSavedSnapFraction = -1f;
462 mDeferResizeToNormalBoundsUntilRotation = -1;
463 }
Winson Chunga29eb982016-12-14 12:01:27 -0800464 }
465
Joshua Tsujif39539d2020-04-03 18:53:06 -0400466 /** Adds the magnetic target view to the WindowManager so it's ready to be animated in. */
Joshua Tsujif7de6802020-04-13 13:30:11 -0400467 private void createOrUpdateDismissTarget() {
Joshua Tsujif39539d2020-04-03 18:53:06 -0400468 if (!mTargetViewContainer.isAttachedToWindow()) {
469 mHandler.removeCallbacks(mShowTargetAction);
470 mMagneticTargetAnimator.cancel();
471
Joshua Tsujif39539d2020-04-03 18:53:06 -0400472 mTargetViewContainer.setVisibility(View.INVISIBLE);
Joshua Tsujif7de6802020-04-13 13:30:11 -0400473
474 try {
475 mWindowManager.addView(mTargetViewContainer, getDismissTargetLayoutParams());
476 } catch (IllegalStateException e) {
477 // This shouldn't happen, but if the target is already added, just update its layout
478 // params.
479 mWindowManager.updateViewLayout(
480 mTargetViewContainer, getDismissTargetLayoutParams());
481 }
482 } else {
483 mWindowManager.updateViewLayout(mTargetViewContainer, getDismissTargetLayoutParams());
Joshua Tsujif39539d2020-04-03 18:53:06 -0400484 }
485 }
486
Joshua Tsujif7de6802020-04-13 13:30:11 -0400487 /** Returns layout params for the dismiss target, using the latest display metrics. */
488 private WindowManager.LayoutParams getDismissTargetLayoutParams() {
489 final Point windowSize = new Point();
490 mWindowManager.getDefaultDisplay().getRealSize(windowSize);
491
492 final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
493 WindowManager.LayoutParams.MATCH_PARENT,
494 mDismissAreaHeight,
495 0, windowSize.y - mDismissAreaHeight,
496 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL,
497 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
498 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
499 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
500 PixelFormat.TRANSLUCENT);
501
502 lp.setTitle("pip-dismiss-overlay");
503 lp.privateFlags |= WindowManager.LayoutParams.SYSTEM_FLAG_SHOW_FOR_ALL_USERS;
504 lp.setFitInsetsTypes(0 /* types */);
505
506 return lp;
507 }
508
Joshua Tsujif39539d2020-04-03 18:53:06 -0400509 /** Makes the dismiss target visible and animates it in, if it isn't already visible. */
510 private void showDismissTargetMaybe() {
Joshua Tsujif7de6802020-04-13 13:30:11 -0400511 createOrUpdateDismissTarget();
Joshua Tsujif39539d2020-04-03 18:53:06 -0400512
513 if (mTargetViewContainer.getVisibility() != View.VISIBLE) {
514
515 mTargetView.setTranslationY(mTargetViewContainer.getHeight());
516 mTargetViewContainer.setVisibility(View.VISIBLE);
517
Joshua Tsujif39539d2020-04-03 18:53:06 -0400518 // Cancel in case we were in the middle of animating it out.
519 mMagneticTargetAnimator.cancel();
520 mMagneticTargetAnimator
521 .spring(DynamicAnimation.TRANSLATION_Y, 0f, mTargetSpringConfig)
522 .start();
523 }
524 }
525
526 /** Animates the magnetic dismiss target out and then sets it to GONE. */
527 private void hideDismissTarget() {
528 mHandler.removeCallbacks(mShowTargetAction);
529 mMagneticTargetAnimator
530 .spring(DynamicAnimation.TRANSLATION_Y,
531 mTargetViewContainer.getHeight(),
532 mTargetSpringConfig)
533 .withEndActions(() -> mTargetViewContainer.setVisibility(View.GONE))
534 .start();
535 }
536
537 /**
538 * Removes the dismiss target and cancels any pending callbacks to show it.
539 */
540 private void cleanUpDismissTarget() {
541 mHandler.removeCallbacks(mShowTargetAction);
542
543 if (mTargetViewContainer.isAttachedToWindow()) {
Joshua Tsuji06745aa2020-04-09 11:17:42 -0400544 mWindowManager.removeViewImmediate(mTargetViewContainer);
Joshua Tsujif39539d2020-04-03 18:53:06 -0400545 }
546 }
547
Phil Weaverf00cd142017-03-03 13:44:00 -0800548 private void onRegistrationChanged(boolean isRegistered) {
549 mAccessibilityManager.setPictureInPictureActionReplacingConnection(isRegistered
Arthur Hungd8961972019-10-14 17:00:17 +0800550 ? mConnection : null);
Winson Chungb54b65b2017-04-26 14:02:13 -0700551 if (!isRegistered && mTouchState.isUserInteracting()) {
552 // If the input consumer is unregistered while the user is interacting, then we may not
553 // get the final TOUCH_UP event, so clean up the dismiss target as well
554 cleanUpDismissTarget();
555 }
Winson Chungfe1fa642017-03-13 10:51:22 -0700556 }
557
558 private void onAccessibilityShowMenu() {
Mady Mellor637cd482017-03-21 10:39:42 -0700559 mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
Hongwei Wang7c622402020-05-07 20:45:22 -0700560 true /* allowMenuTimeout */, willResizeMenu());
Phil Weaverf00cd142017-03-03 13:44:00 -0800561 }
562
Tracy Zhou535c2122019-02-21 17:02:47 -0800563 private boolean handleTouchEvent(InputEvent inputEvent) {
564 // Skip any non motion events
565 if (!(inputEvent instanceof MotionEvent)) {
566 return true;
567 }
Winson Chung655332c2016-10-31 13:14:28 -0700568 // Skip touch handling until we are bound to the controller
569 if (mPinnedStackController == null) {
Winson Chung15504af2016-11-02 18:11:36 -0700570 return true;
Winson Chung655332c2016-10-31 13:14:28 -0700571 }
Joshua Tsujif39539d2020-04-03 18:53:06 -0400572
Tracy Zhou535c2122019-02-21 17:02:47 -0800573 MotionEvent ev = (MotionEvent) inputEvent;
Winson Chung655332c2016-10-31 13:14:28 -0700574
Ben Lin7f12e212020-05-11 16:54:13 -0700575
576 if (mPipResizeGestureHandler.isWithinTouchRegion((int) ev.getX(), (int) ev.getY())) {
577 return true;
578 }
579
Joshua Tsujif39539d2020-04-03 18:53:06 -0400580 if (mMagnetizedPip.maybeConsumeMotionEvent(ev)) {
581 // If the first touch event occurs within the magnetic field, pass the ACTION_DOWN event
582 // to the touch state. Touch state needs a DOWN event in order to later process MOVE
583 // events it'll receive if the object is dragged out of the magnetic field.
584 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
585 mTouchState.onTouchEvent(ev);
586 }
587
588 // Continue tracking velocity when the object is in the magnetic field, since we want to
589 // respect touch input velocity if the object is dragged out and then flung.
590 mTouchState.addMovementToVelocityTracker(ev);
591
592 return true;
593 }
594
Winson Chungfa7053782016-11-08 15:45:10 -0800595 // Update the touch state
596 mTouchState.onTouchEvent(ev);
597
Arthur Hungd52d1192019-08-30 16:06:59 +0800598 boolean shouldDeliverToMenu = mMenuState != MENU_STATE_NONE;
599
Winson73bc1592016-10-18 18:47:43 -0700600 switch (ev.getAction()) {
601 case MotionEvent.ACTION_DOWN: {
Joshua Tsuji601dc852020-04-02 15:54:15 -0400602 mMotionHelper.synchronizePinnedStackBoundsForTouchGesture();
Ben Lin1a80f122020-01-27 14:09:52 -0800603 mGesture.onDown(mTouchState);
Winson73bc1592016-10-18 18:47:43 -0700604 break;
605 }
606 case MotionEvent.ACTION_MOVE: {
Ben Lin1a80f122020-01-27 14:09:52 -0800607 if (mGesture.onMove(mTouchState)) {
608 break;
Winson73bc1592016-10-18 18:47:43 -0700609 }
Arthur Hungd52d1192019-08-30 16:06:59 +0800610
611 shouldDeliverToMenu = !mTouchState.isDragging();
Winson73bc1592016-10-18 18:47:43 -0700612 break;
613 }
614 case MotionEvent.ACTION_UP: {
Winson Chung14fefc22016-11-02 10:02:29 -0700615 // Update the movement bounds again if the state has changed since the user started
616 // dragging (ie. when the IME shows)
Ben Lin094c7752020-03-11 12:10:51 -0700617 updateMovementBounds();
Winson Chung14fefc22016-11-02 10:02:29 -0700618
Ben Lin1a80f122020-01-27 14:09:52 -0800619 if (mGesture.onUp(mTouchState)) {
620 break;
Winson Chung5cd26ff2016-10-24 11:50:44 -0700621 }
Winson73bc1592016-10-18 18:47:43 -0700622
623 // Fall through to clean up
624 }
625 case MotionEvent.ACTION_CANCEL: {
Arthur Hungd52d1192019-08-30 16:06:59 +0800626 shouldDeliverToMenu = !mTouchState.startedDragging() && !mTouchState.isDragging();
Winson Chung85d39982017-02-24 15:21:25 -0800627 mTouchState.reset();
Winson73bc1592016-10-18 18:47:43 -0700628 break;
629 }
Phil Weaverf00cd142017-03-03 13:44:00 -0800630 case MotionEvent.ACTION_HOVER_ENTER:
Ben Lin388df602020-04-08 14:07:06 -0700631 // If Touch Exploration is enabled, some a11y services (e.g. Talkback) is probably
632 // on and changing MotionEvents into HoverEvents.
633 // Let's not enable menu show/hide for a11y services.
634 if (!mAccessibilityManager.isTouchExplorationEnabled()) {
635 mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
Hongwei Wang7c622402020-05-07 20:45:22 -0700636 false /* allowMenuTimeout */, false /* willResizeMenu */);
Ben Lin388df602020-04-08 14:07:06 -0700637 }
Phil Weaverf00cd142017-03-03 13:44:00 -0800638 case MotionEvent.ACTION_HOVER_MOVE: {
Arthur Hungd8961972019-10-14 17:00:17 +0800639 if (!shouldDeliverToMenu && !mSendingHoverAccessibilityEvents) {
640 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Phil Weaverf00cd142017-03-03 13:44:00 -0800641 mSendingHoverAccessibilityEvents = true;
642 }
643 break;
644 }
645 case MotionEvent.ACTION_HOVER_EXIT: {
Ben Lin388df602020-04-08 14:07:06 -0700646 // If Touch Exploration is enabled, some a11y services (e.g. Talkback) is probably
647 // on and changing MotionEvents into HoverEvents.
648 // Let's not enable menu show/hide for a11y services.
649 if (!mAccessibilityManager.isTouchExplorationEnabled()) {
650 mMenuController.hideMenu();
651 }
Arthur Hungd8961972019-10-14 17:00:17 +0800652 if (!shouldDeliverToMenu && mSendingHoverAccessibilityEvents) {
653 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Phil Weaverf00cd142017-03-03 13:44:00 -0800654 mSendingHoverAccessibilityEvents = false;
655 }
656 break;
657 }
Winson73bc1592016-10-18 18:47:43 -0700658 }
Arthur Hungd52d1192019-08-30 16:06:59 +0800659
660 // Deliver the event to PipMenuActivity to handle button click if the menu has shown.
661 if (shouldDeliverToMenu) {
662 final MotionEvent cloneEvent = MotionEvent.obtain(ev);
663 // Send the cancel event and cancel menu timeout if it starts to drag.
664 if (mTouchState.startedDragging()) {
665 cloneEvent.setAction(MotionEvent.ACTION_CANCEL);
666 mMenuController.pokeMenu();
667 }
668
Arthur Hungd8961972019-10-14 17:00:17 +0800669 mMenuController.handlePointerEvent(cloneEvent);
Arthur Hungd52d1192019-08-30 16:06:59 +0800670 }
671
672 return true;
Winson Chung15504af2016-11-02 18:11:36 -0700673 }
674
Arthur Hungd8961972019-10-14 17:00:17 +0800675 private void sendAccessibilityHoverEvent(int type) {
676 if (!mAccessibilityManager.isEnabled()) {
677 return;
678 }
679
680 AccessibilityEvent event = AccessibilityEvent.obtain(type);
681 event.setImportantForAccessibility(true);
682 event.setSourceNodeId(AccessibilityNodeInfo.ROOT_NODE_ID);
683 event.setWindowId(
684 AccessibilityWindowInfo.PICTURE_IN_PICTURE_ACTION_REPLACER_WINDOW_ID);
685 mAccessibilityManager.sendAccessibilityEvent(event);
686 }
687
Winson Chung15504af2016-11-02 18:11:36 -0700688 /**
Mady Mellor81d40612017-03-10 15:14:10 -0800689 * Updates the appearance of the menu and scrim on top of the PiP while dismissing.
690 */
Winson Chung8ec0e162017-07-07 14:49:49 -0700691 private void updateDismissFraction() {
Winson Chung0a2f34f2017-08-11 18:13:24 -0700692 // Skip updating the dismiss fraction when the IME is showing. This is to work around an
693 // issue where starting the menu activity for the dismiss overlay will steal the window
694 // focus, which closes the IME.
695 if (mMenuController != null && !mIsImeShowing) {
Mady Mellor81d40612017-03-10 15:14:10 -0800696 Rect bounds = mMotionHelper.getBounds();
Winson Chung0a2f34f2017-08-11 18:13:24 -0700697 final float target = mInsetBounds.bottom;
Mady Mellor81d40612017-03-10 15:14:10 -0800698 float fraction = 0f;
699 if (bounds.bottom > target) {
700 final float distance = bounds.bottom - target;
701 fraction = Math.min(distance / bounds.height(), 1f);
702 }
Winson Chungbca03112017-08-16 10:38:15 -0700703 if (Float.compare(fraction, 0f) != 0 || mMenuController.isMenuActivityVisible()) {
Winson Chung87e5d552017-04-05 11:49:38 -0700704 // Update if the fraction > 0, or if fraction == 0 and the menu was already visible
705 mMenuController.setDismissFraction(fraction);
706 }
Mady Mellor81d40612017-03-10 15:14:10 -0800707 }
708 }
709
710 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800711 * Sets the controller to update the system of changes from user interaction.
712 */
713 void setPinnedStackController(IPinnedStackController controller) {
714 mPinnedStackController = controller;
715 }
716
717 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800718 * Sets the menu visibility.
Winson Chungfa7053782016-11-08 15:45:10 -0800719 */
Hongwei Wang7c622402020-05-07 20:45:22 -0700720 private void setMenuState(int menuState, boolean resize, Runnable callback) {
Arthur Hungd8961972019-10-14 17:00:17 +0800721 if (mMenuState == menuState && !resize) {
722 return;
723 }
724
Winson Chunga89053d2018-06-12 16:47:30 -0700725 if (menuState == MENU_STATE_FULL && mMenuState != MENU_STATE_FULL) {
Winson Chungd2d90972017-02-28 11:40:41 -0800726 // Save the current snap fraction and if we do not drag or move the PiP, then
727 // we store back to this snap fraction. Otherwise, we'll reset the snap
Ben Lin094c7752020-03-11 12:10:51 -0700728 // fraction and snap to the closest edge.
729 // Also save the current resized bounds so when the menu disappears, we can restore it.
Winson Chungd2d90972017-02-28 11:40:41 -0800730 if (resize) {
Ben Lin094c7752020-03-11 12:10:51 -0700731 mResizedBounds.set(mMotionHelper.getBounds());
732 Rect expandedBounds = new Rect(mExpandedBounds);
Winson Chung2a82fe52017-02-02 14:43:34 -0800733 mSavedSnapFraction = mMotionHelper.animateToExpandedState(expandedBounds,
Hongwei Wang7c622402020-05-07 20:45:22 -0700734 mMovementBounds, mExpandedMovementBounds, callback);
Winson Chungd2d90972017-02-28 11:40:41 -0800735 }
Winson Chunga89053d2018-06-12 16:47:30 -0700736 } else if (menuState == MENU_STATE_NONE && mMenuState == MENU_STATE_FULL) {
Winson Chungd2d90972017-02-28 11:40:41 -0800737 // Try and restore the PiP to the closest edge, using the saved snap fraction
738 // if possible
739 if (resize) {
Winson Chungef4dc812017-04-11 13:31:44 -0700740 if (mDeferResizeToNormalBoundsUntilRotation == -1) {
Winson Chungbb233762017-05-15 14:20:46 -0700741 // This is a very special case: when the menu is expanded and visible,
742 // navigating to another activity can trigger auto-enter PiP, and if the
743 // revealed activity has a forced rotation set, then the controller will get
744 // updated with the new rotation of the display. However, at the same time,
745 // SystemUI will try to hide the menu by creating an animation to the normal
746 // bounds which are now stale. In such a case we defer the animation to the
747 // normal bounds until after the next onMovementBoundsChanged() call to get the
748 // bounds in the new orientation
Winson Chungef4dc812017-04-11 13:31:44 -0700749 try {
750 int displayRotation = mPinnedStackController.getDisplayRotation();
751 if (mDisplayRotation != displayRotation) {
752 mDeferResizeToNormalBoundsUntilRotation = displayRotation;
753 }
754 } catch (RemoteException e) {
755 Log.e(TAG, "Could not get display rotation from controller");
756 }
757 }
758
759 if (mDeferResizeToNormalBoundsUntilRotation == -1) {
Ben Lin094c7752020-03-11 12:10:51 -0700760 Rect restoreBounds = new Rect(mResizedBounds);
761 Rect restoredMovementBounds = new Rect();
762 mSnapAlgorithm.getMovementBounds(restoreBounds, mInsetBounds,
763 restoredMovementBounds, mIsImeShowing ? mImeHeight : 0);
764 mMotionHelper.animateToUnexpandedState(restoreBounds, mSavedSnapFraction,
765 restoredMovementBounds, mMovementBounds, false /* immediate */);
Winson Chungef4dc812017-04-11 13:31:44 -0700766 mSavedSnapFraction = -1f;
767 }
768 } else {
769 mSavedSnapFraction = -1f;
Winson Chunga29eb982016-12-14 12:01:27 -0800770 }
Winson Chungfa7053782016-11-08 15:45:10 -0800771 }
Mady Mellor637cd482017-03-21 10:39:42 -0700772 mMenuState = menuState;
Ben Lin094c7752020-03-11 12:10:51 -0700773 updateMovementBounds();
Arthur Hungd8961972019-10-14 17:00:17 +0800774 // If pip menu has dismissed, we should register the A11y ActionReplacingConnection for pip
775 // as well, or it can't handle a11y focus and pip menu can't perform any action.
776 onRegistrationChanged(menuState == MENU_STATE_NONE);
Mady Mellor637cd482017-03-21 10:39:42 -0700777 if (menuState != MENU_STATE_CLOSE) {
Chenjie Yu52cacc62017-12-08 18:11:45 -0800778 MetricsLoggerWrapper.logPictureInPictureMenuVisible(mContext, menuState == MENU_STATE_FULL);
Mady Mellor637cd482017-03-21 10:39:42 -0700779 }
Winson Chungfa7053782016-11-08 15:45:10 -0800780 }
781
782 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800783 * @return the motion helper.
Winson Chungfa7053782016-11-08 15:45:10 -0800784 */
Winson Chung2a82fe52017-02-02 14:43:34 -0800785 public PipMotionHelper getMotionHelper() {
786 return mMotionHelper;
Winson73bc1592016-10-18 18:47:43 -0700787 }
Winson Chungfa7053782016-11-08 15:45:10 -0800788
Ben Lin094c7752020-03-11 12:10:51 -0700789 @VisibleForTesting
790 PipResizeGestureHandler getPipResizeGestureHandler() {
791 return mPipResizeGestureHandler;
792 }
793
794 @VisibleForTesting
795 void setPipResizeGestureHandler(PipResizeGestureHandler pipResizeGestureHandler) {
796 mPipResizeGestureHandler = pipResizeGestureHandler;
797 }
798
799 @VisibleForTesting
800 void setPipMotionHelper(PipMotionHelper pipMotionHelper) {
801 mMotionHelper = pipMotionHelper;
802 }
803
Winson Chungfa7053782016-11-08 15:45:10 -0800804 /**
jorgegil@google.com62f735e2019-10-30 14:37:19 -0700805 * @return the unexpanded bounds.
806 */
Ben Lin06dc41f2020-03-05 00:39:27 +0000807 public Rect getNormalBounds() {
808 return mNormalBounds;
jorgegil@google.com62f735e2019-10-30 14:37:19 -0700809 }
810
811 /**
Winson Chungfa7053782016-11-08 15:45:10 -0800812 * Gesture controlling normal movement of the PIP.
813 */
Ben Lin1a80f122020-01-27 14:09:52 -0800814 private class DefaultPipTouchGesture extends PipTouchGesture {
Winson Chung8ec0e162017-07-07 14:49:49 -0700815 private final Point mStartPosition = new Point();
816 private final PointF mDelta = new PointF();
Tony Huang413e9312020-05-12 16:09:14 +0800817 private boolean mShouldHideMenuAfterFling;
Mady Mellord4e40fb2017-01-26 10:43:16 -0800818
819 @Override
820 public void onDown(PipTouchState touchState) {
Winson Chung85d39982017-02-24 15:21:25 -0800821 if (!touchState.isUserInteracting()) {
822 return;
823 }
824
Mady Mellor15b29c72017-06-07 14:53:58 -0700825 Rect bounds = mMotionHelper.getBounds();
Winson Chung8ec0e162017-07-07 14:49:49 -0700826 mDelta.set(0f, 0f);
827 mStartPosition.set(bounds.left, bounds.top);
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700828 mMovementWithinDismiss = touchState.getDownTouchPosition().y >= mMovementBounds.bottom;
Joshua Tsujif39539d2020-04-03 18:53:06 -0400829 mMotionHelper.setSpringingToTouch(false);
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700830
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800831 // If the menu is still visible then just poke the menu
Mady Mellora7f69742017-02-03 11:00:20 -0800832 // so that it will timeout after the user stops touching it
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800833 if (mMenuState != MENU_STATE_NONE) {
Mady Mellora7f69742017-02-03 11:00:20 -0800834 mMenuController.pokeMenu();
835 }
Mady Mellord4e40fb2017-01-26 10:43:16 -0800836 }
837
Winson Chungfa7053782016-11-08 15:45:10 -0800838 @Override
Ben Lin1a80f122020-01-27 14:09:52 -0800839 public boolean onMove(PipTouchState touchState) {
Winson Chung85d39982017-02-24 15:21:25 -0800840 if (!touchState.isUserInteracting()) {
841 return false;
842 }
843
Winson Chung2a82fe52017-02-02 14:43:34 -0800844 if (touchState.startedDragging()) {
845 mSavedSnapFraction = -1f;
Winson Chung2a82fe52017-02-02 14:43:34 -0800846
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800847 if (mEnableDismissDragToEdge) {
Joshua Tsujif39539d2020-04-03 18:53:06 -0400848 if (mTargetViewContainer.getVisibility() != View.VISIBLE) {
849 mHandler.removeCallbacks(mShowTargetAction);
850 showDismissTargetMaybe();
851 }
Winson Chungb54b65b2017-04-26 14:02:13 -0700852 }
Mady Mellord4e40fb2017-01-26 10:43:16 -0800853 }
854
Winson Chungfa7053782016-11-08 15:45:10 -0800855 if (touchState.isDragging()) {
856 // Move the pinned stack freely
Winson Chung2a82fe52017-02-02 14:43:34 -0800857 final PointF lastDelta = touchState.getLastTouchDelta();
Winson Chung8ec0e162017-07-07 14:49:49 -0700858 float lastX = mStartPosition.x + mDelta.x;
859 float lastY = mStartPosition.y + mDelta.y;
860 float left = lastX + lastDelta.x;
861 float top = lastY + lastDelta.y;
Winson Chung8ec0e162017-07-07 14:49:49 -0700862
863 // Add to the cumulative delta after bounding the position
864 mDelta.x += left - lastX;
865 mDelta.y += top - lastY;
866
867 mTmpBounds.set(mMotionHelper.getBounds());
Winson Chungfa7053782016-11-08 15:45:10 -0800868 mTmpBounds.offsetTo((int) left, (int) top);
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500869 mMotionHelper.movePip(mTmpBounds, true /* isDragging */);
Winson Chung2a82fe52017-02-02 14:43:34 -0800870
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700871 final PointF curPos = touchState.getLastTouchPosition();
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700872 if (mMovementWithinDismiss) {
873 // Track if movement remains near the bottom edge to identify swipe to dismiss
874 mMovementWithinDismiss = curPos.y >= mMovementBounds.bottom;
875 }
Winson Chungfa7053782016-11-08 15:45:10 -0800876 return true;
877 }
878 return false;
879 }
880
881 @Override
882 public boolean onUp(PipTouchState touchState) {
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800883 if (mEnableDismissDragToEdge) {
Joshua Tsujif39539d2020-04-03 18:53:06 -0400884 hideDismissTarget();
Winson Chungb54b65b2017-04-26 14:02:13 -0700885 }
886
Winson Chung85d39982017-02-24 15:21:25 -0800887 if (!touchState.isUserInteracting()) {
888 return false;
889 }
890
Mady Mellor60421c92017-03-29 15:27:37 -0700891 final PointF vel = touchState.getVelocity();
Mady Mellor60421c92017-03-29 15:27:37 -0700892 final float velocity = PointF.length(vel.x, vel.y);
Winson Chungd2d90972017-02-28 11:40:41 -0800893
Winson Chungfa7053782016-11-08 15:45:10 -0800894 if (touchState.isDragging()) {
Mady Mellor637cd482017-03-21 10:39:42 -0700895 if (mMenuState != MENU_STATE_NONE) {
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800896 // If the menu is still visible, then just poke the menu so that
897 // it will timeout after the user stops touching it
Mady Mellor637cd482017-03-21 10:39:42 -0700898 mMenuController.showMenu(mMenuState, mMotionHelper.getBounds(),
Hongwei Wang7c622402020-05-07 20:45:22 -0700899 true /* allowMenuTimeout */, willResizeMenu());
Winson Chungd2d90972017-02-28 11:40:41 -0800900 }
Tony Huang413e9312020-05-12 16:09:14 +0800901 mShouldHideMenuAfterFling = mMenuState == MENU_STATE_NONE;
Winson Chungd2d90972017-02-28 11:40:41 -0800902
Joshua Tsuji06927c22020-05-07 12:15:24 -0400903 mMotionHelper.flingToSnapTarget(vel.x, vel.y,
904 PipTouchHandler.this::updateDismissFraction /* updateAction */,
Tony Huang413e9312020-05-12 16:09:14 +0800905 this::flingEndAction /* endAction */);
Arthur Hungd52d1192019-08-30 16:06:59 +0800906 } else if (mTouchState.isDoubleTap()) {
907 // Expand to fullscreen if this is a double tap
Ben Lin34b6fbf2020-03-13 19:36:02 -0700908 // the PiP should be frozen until the transition ends
909 setTouchEnabled(false);
Winson Chungc4d4ee82020-05-05 12:51:06 -0700910 mMotionHelper.expandPipToFullscreen();
Mady Mellor637cd482017-03-21 10:39:42 -0700911 } else if (mMenuState != MENU_STATE_FULL) {
Arthur Hungd52d1192019-08-30 16:06:59 +0800912 if (!mTouchState.isWaitingForDoubleTap()) {
Winson Chungbca03112017-08-16 10:38:15 -0700913 // User has stalled long enough for this not to be a drag or a double tap, just
914 // expand the menu
915 mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
Hongwei Wang7c622402020-05-07 20:45:22 -0700916 true /* allowMenuTimeout */, willResizeMenu());
Winson Chungbca03112017-08-16 10:38:15 -0700917 } else {
918 // Next touch event _may_ be the second tap for the double-tap, schedule a
919 // fallback runnable to trigger the menu if no touch event occurs before the
920 // next tap
921 mTouchState.scheduleDoubleTapTimeoutCallback();
922 }
Winson Chungfa7053782016-11-08 15:45:10 -0800923 }
924 return true;
925 }
Tony Huang413e9312020-05-12 16:09:14 +0800926
927 private void flingEndAction() {
928 mTouchState.setAllowTouches(true);
929 if (mShouldHideMenuAfterFling) {
930 // If the menu is not visible, then we can still be showing the activity for the
931 // dismiss overlay, so just finish it after the animation completes
932 mMenuController.hideMenu();
933 }
934 }
Winson Chungfa7053782016-11-08 15:45:10 -0800935 };
Mady Mellor3b10dcd2017-01-23 10:08:35 -0800936
937 /**
Ben Lin06dc41f2020-03-05 00:39:27 +0000938 * Updates the current movement bounds based on whether the menu is currently visible and
939 * resized.
Winson Chung2a82fe52017-02-02 14:43:34 -0800940 */
Ben Lin094c7752020-03-11 12:10:51 -0700941 private void updateMovementBounds() {
942 mSnapAlgorithm.getMovementBounds(mMotionHelper.getBounds(), mInsetBounds,
943 mMovementBounds, mIsImeShowing ? mImeHeight : 0);
Ben Lin06dc41f2020-03-05 00:39:27 +0000944 mMotionHelper.setCurrentMovementBounds(mMovementBounds);
Ben Lin094c7752020-03-11 12:10:51 -0700945
946 boolean isMenuExpanded = mMenuState == MENU_STATE_FULL;
947 mPipBoundsHandler.setMinEdgeSize(
948 isMenuExpanded && willResizeMenu() ? mExpandedShortestEdgeSize : 0);
Winson Chung2a82fe52017-02-02 14:43:34 -0800949 }
Winson Chung29a78652017-02-09 18:35:26 -0800950
Ben Lin4a8e2572020-05-08 15:55:15 -0700951 private Rect getMovementBounds() {
952 return mMovementBounds;
953 }
954
Winson Chungb54b65b2017-04-26 14:02:13 -0700955 /**
Winson Chungbb787442017-09-01 11:33:47 -0700956 * @return whether the menu will resize as a part of showing the full menu.
957 */
958 private boolean willResizeMenu() {
Ben Line55f85f2020-01-27 14:49:41 -0800959 if (!mEnableResize) {
960 return false;
961 }
Ben Lin06dc41f2020-03-05 00:39:27 +0000962 return mExpandedBounds.width() != mNormalBounds.width()
963 || mExpandedBounds.height() != mNormalBounds.height();
Winson Chungbb787442017-09-01 11:33:47 -0700964 }
965
Winson Chung29a78652017-02-09 18:35:26 -0800966 public void dump(PrintWriter pw, String prefix) {
967 final String innerPrefix = prefix + " ";
968 pw.println(prefix + TAG);
969 pw.println(innerPrefix + "mMovementBounds=" + mMovementBounds);
Ben Lin06dc41f2020-03-05 00:39:27 +0000970 pw.println(innerPrefix + "mNormalBounds=" + mNormalBounds);
971 pw.println(innerPrefix + "mNormalMovementBounds=" + mNormalMovementBounds);
972 pw.println(innerPrefix + "mExpandedBounds=" + mExpandedBounds);
973 pw.println(innerPrefix + "mExpandedMovementBounds=" + mExpandedMovementBounds);
Mady Mellor637cd482017-03-21 10:39:42 -0700974 pw.println(innerPrefix + "mMenuState=" + mMenuState);
Winson Chung29a78652017-02-09 18:35:26 -0800975 pw.println(innerPrefix + "mIsImeShowing=" + mIsImeShowing);
976 pw.println(innerPrefix + "mImeHeight=" + mImeHeight);
Tracy Zhou43513082018-03-08 21:58:36 -0800977 pw.println(innerPrefix + "mIsShelfShowing=" + mIsShelfShowing);
978 pw.println(innerPrefix + "mShelfHeight=" + mShelfHeight);
Winson Chung29a78652017-02-09 18:35:26 -0800979 pw.println(innerPrefix + "mSavedSnapFraction=" + mSavedSnapFraction);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800980 pw.println(innerPrefix + "mEnableDragToEdgeDismiss=" + mEnableDismissDragToEdge);
Winson Chung29a78652017-02-09 18:35:26 -0800981 mSnapAlgorithm.dump(pw, innerPrefix);
982 mTouchState.dump(pw, innerPrefix);
983 mMotionHelper.dump(pw, innerPrefix);
984 }
Phil Weaverf00cd142017-03-03 13:44:00 -0800985
Winson73bc1592016-10-18 18:47:43 -0700986}