blob: 350ce293a13f8dfb7881848fa31424fb228b3f2b [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
Joshua Tsujif39539d2020-04-03 18:53:06 -040023import android.annotation.SuppressLint;
Winson73bc1592016-10-18 18:47:43 -070024import android.app.IActivityManager;
Wale Ogunwale65ebd952018-04-25 15:41:44 -070025import android.app.IActivityTaskManager;
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 Wang43a752b2019-09-17 20:20:30 +000059import com.android.systemui.pip.PipBoundsHandler;
Hongwei Wangebf18082019-09-26 14:25:11 -070060import com.android.systemui.pip.PipSnapAlgorithm;
Hongwei Wang85cf41f2020-01-15 15:14:47 -080061import com.android.systemui.pip.PipTaskOrganizer;
Winson Chungcbb15a92018-01-25 17:46:16 +000062import com.android.systemui.shared.system.InputConsumerController;
Winson73bc1592016-10-18 18:47:43 -070063import com.android.systemui.statusbar.FlingAnimationUtils;
Beverly660d0a72020-02-26 12:32:42 -050064import com.android.systemui.util.DeviceConfigProxy;
Joshua Tsujif39539d2020-04-03 18:53:06 -040065import com.android.systemui.util.DismissCircleView;
Joshua Tsujic81ff3d2020-02-13 14:48:40 -050066import com.android.systemui.util.FloatingContentCoordinator;
Joshua Tsujif39539d2020-04-03 18:53:06 -040067import com.android.systemui.util.animation.PhysicsAnimator;
68import com.android.systemui.util.magnetictarget.MagnetizedObject;
Winson73bc1592016-10-18 18:47:43 -070069
Winson Chung29a78652017-02-09 18:35:26 -080070import java.io.PrintWriter;
71
Winson73bc1592016-10-18 18:47:43 -070072/**
73 * Manages all the touch handling for PIP on the Phone, including moving, dismissing and expanding
74 * the PIP.
75 */
Mady Mellor8c7dc422017-05-10 12:55:06 -070076public class PipTouchHandler {
Winson73bc1592016-10-18 18:47:43 -070077 private static final String TAG = "PipTouchHandler";
Winson73bc1592016-10-18 18:47:43 -070078
Mady Mellor5d58d252017-04-18 12:48:04 -070079 private static final int SHOW_DISMISS_AFFORDANCE_DELAY = 225;
Tracy Zhoue19d0512018-07-31 16:29:34 -070080 private static final int BOTTOM_OFFSET_BUFFER_DP = 1;
Winson Chungfa7053782016-11-08 15:45:10 -080081
Winson Chung6e35ee1f2017-02-14 12:06:44 -080082 // Allow dragging the PIP to a location to close it
Hongwei Wang85cf41f2020-01-15 15:14:47 -080083 private final boolean mEnableDismissDragToEdge;
Ben Line55f85f2020-01-27 14:49:41 -080084 // Allow PIP to resize to a slightly bigger state upon touch
85 private final boolean mEnableResize;
Winson73bc1592016-10-18 18:47:43 -070086 private final Context mContext;
Joshua Tsujif39539d2020-04-03 18:53:06 -040087 private final WindowManager mWindowManager;
Winson73bc1592016-10-18 18:47:43 -070088 private final IActivityManager mActivityManager;
Hongwei Wang43a752b2019-09-17 20:20:30 +000089 private final PipBoundsHandler mPipBoundsHandler;
Ben Lin094c7752020-03-11 12:10:51 -070090 private PipResizeGestureHandler mPipResizeGestureHandler;
Winson Chung655332c2016-10-31 13:14:28 -070091 private IPinnedStackController mPinnedStackController;
Winson73bc1592016-10-18 18:47:43 -070092
Winson Chung2a82fe52017-02-02 14:43:34 -080093 private final PipMenuActivityController mMenuController;
Winson Chung14fefc22016-11-02 10:02:29 -070094 private final PipSnapAlgorithm mSnapAlgorithm;
Phil Weaverf00cd142017-03-03 13:44:00 -080095 private final AccessibilityManager mAccessibilityManager;
Wale Ogunwale6455e502017-04-17 14:16:43 -070096 private boolean mShowPipMenuOnAnimationEnd = false;
Winson73bc1592016-10-18 18:47:43 -070097
Joshua Tsujif39539d2020-04-03 18:53:06 -040098 /**
99 * MagnetizedObject wrapper for PIP. This allows the magnetic target library to locate and move
100 * PIP.
101 */
102 private MagnetizedObject<Rect> mMagnetizedPip;
103
104 /**
105 * Container for the dismiss circle, so that it can be animated within the container via
106 * translation rather than within the WindowManager via slow layout animations.
107 */
108 private ViewGroup mTargetViewContainer;
109
110 /** Circle view used to render the dismiss target. */
111 private DismissCircleView mTargetView;
112
113 /**
114 * MagneticTarget instance wrapping the target view and allowing us to set its magnetic radius.
115 */
116 private MagnetizedObject.MagneticTarget mMagneticTarget;
117
118 /** PhysicsAnimator instance for animating the dismiss target in/out. */
119 private PhysicsAnimator<View> mMagneticTargetAnimator;
120
121 /** Default configuration to use for springing the dismiss target in/out. */
122 private final PhysicsAnimator.SpringConfig mTargetSpringConfig =
123 new PhysicsAnimator.SpringConfig(
124 SpringForce.STIFFNESS_MEDIUM, SpringForce.DAMPING_RATIO_NO_BOUNCY);
125
Winson Chung2a82fe52017-02-02 14:43:34 -0800126 // The current movement bounds
127 private Rect mMovementBounds = new Rect();
Ben Lin094c7752020-03-11 12:10:51 -0700128 // The current resized bounds, changed by user resize.
129 // This is used during expand/un-expand to save/restore the user's resized size.
130 @VisibleForTesting Rect mResizedBounds = new Rect();
Winson Chung2a82fe52017-02-02 14:43:34 -0800131
Winson Chung0a2f34f2017-08-11 18:13:24 -0700132 // The reference inset bounds, used to determine the dismiss fraction
133 private Rect mInsetBounds = new Rect();
Ben Lin06dc41f2020-03-05 00:39:27 +0000134 // The reference bounds used to calculate the normal/expanded target bounds
135 private Rect mNormalBounds = new Rect();
Ben Lin094c7752020-03-11 12:10:51 -0700136 @VisibleForTesting Rect mNormalMovementBounds = new Rect();
Ben Lin06dc41f2020-03-05 00:39:27 +0000137 private Rect mExpandedBounds = new Rect();
Ben Lin094c7752020-03-11 12:10:51 -0700138 @VisibleForTesting Rect mExpandedMovementBounds = new Rect();
Mady Mellora7f69742017-02-03 11:00:20 -0800139 private int mExpandedShortestEdgeSize;
Winson73bc1592016-10-18 18:47:43 -0700140
Winson Chungef4dc812017-04-11 13:31:44 -0700141 // Used to workaround an issue where the WM rotation happens before we are notified, allowing
142 // us to send stale bounds
143 private int mDeferResizeToNormalBoundsUntilRotation = -1;
144 private int mDisplayRotation;
145
Joshua Tsujif39539d2020-04-03 18:53:06 -0400146 /**
147 * Runnable that can be posted delayed to show the target. This needs to be saved as a member
148 * variable so we can pass it to removeCallbacks.
149 */
150 private Runnable mShowTargetAction = this::showDismissTargetMaybe;
151
Mady Mellord4e40fb2017-01-26 10:43:16 -0800152 private Handler mHandler = new Handler();
Mady Mellord4e40fb2017-01-26 10:43:16 -0800153
Winson Chungfa7053782016-11-08 15:45:10 -0800154 // Behaviour states
Winson Chung9b919412017-06-19 17:01:51 -0700155 private int mMenuState = MENU_STATE_NONE;
Winson Chung2a82fe52017-02-02 14:43:34 -0800156 private boolean mIsImeShowing;
157 private int mImeHeight;
Winson Chung136d1ec82017-07-18 11:16:30 -0700158 private int mImeOffset;
Joshua Tsujif39539d2020-04-03 18:53:06 -0400159 private int mDismissAreaHeight;
Tracy Zhou43513082018-03-08 21:58:36 -0800160 private boolean mIsShelfShowing;
161 private int mShelfHeight;
Winson Chungdec1a662019-06-04 21:20:41 -0700162 private int mMovementBoundsExtraOffsets;
Winson Chung2a82fe52017-02-02 14:43:34 -0800163 private float mSavedSnapFraction = -1f;
Phil Weaverf00cd142017-03-03 13:44:00 -0800164 private boolean mSendingHoverAccessibilityEvents;
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700165 private boolean mMovementWithinDismiss;
Arthur Hungd8961972019-10-14 17:00:17 +0800166 private PipAccessibilityInteractionConnection mConnection;
Winson73bc1592016-10-18 18:47:43 -0700167
Winson Chungfa7053782016-11-08 15:45:10 -0800168 // Touch state
169 private final PipTouchState mTouchState;
Winson73bc1592016-10-18 18:47:43 -0700170 private final FlingAnimationUtils mFlingAnimationUtils;
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500171 private final FloatingContentCoordinator mFloatingContentCoordinator;
Ben Lin094c7752020-03-11 12:10:51 -0700172 private PipMotionHelper mMotionHelper;
Ben Lin1a80f122020-01-27 14:09:52 -0800173 private PipTouchGesture mGesture;
Winson73bc1592016-10-18 18:47:43 -0700174
Winson Chung2a82fe52017-02-02 14:43:34 -0800175 // Temp vars
Winson Chung655332c2016-10-31 13:14:28 -0700176 private final Rect mTmpBounds = new Rect();
177
Winson73bc1592016-10-18 18:47:43 -0700178 /**
Winson Chung15504af2016-11-02 18:11:36 -0700179 * A listener for the PIP menu activity.
180 */
181 private class PipMenuListener implements PipMenuActivityController.Listener {
182 @Override
Mady Mellor637cd482017-03-21 10:39:42 -0700183 public void onPipMenuStateChanged(int menuState, boolean resize) {
184 setMenuState(menuState, resize);
Winson Chung15504af2016-11-02 18:11:36 -0700185 }
Winson Chunga29eb982016-12-14 12:01:27 -0800186
187 @Override
188 public void onPipExpand() {
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800189 mMotionHelper.expandPip();
Winson Chunga29eb982016-12-14 12:01:27 -0800190 }
191
192 @Override
193 public void onPipDismiss() {
Winson Chungbe00a832020-03-10 11:08:35 -0700194 Pair<ComponentName, Integer> topPipActivity = PipUtils.getTopPipActivity(mContext,
195 mActivityManager);
196 if (topPipActivity.first != null) {
197 MetricsLoggerWrapper.logPictureInPictureDismissByTap(mContext, topPipActivity);
198 }
Winson Chung2a82fe52017-02-02 14:43:34 -0800199 mMotionHelper.dismissPip();
Winson Chunga29eb982016-12-14 12:01:27 -0800200 }
Mady Mellor637cd482017-03-21 10:39:42 -0700201
202 @Override
203 public void onPipShowMenu() {
204 mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
Winson Chungbb787442017-09-01 11:33:47 -0700205 mMovementBounds, true /* allowMenuTimeout */, willResizeMenu());
Mady Mellor637cd482017-03-21 10:39:42 -0700206 }
Winson Chung15504af2016-11-02 18:11:36 -0700207 }
208
Joshua Tsujif39539d2020-04-03 18:53:06 -0400209 @SuppressLint("InflateParams")
Winson Chungd2d90972017-02-28 11:40:41 -0800210 public PipTouchHandler(Context context, IActivityManager activityManager,
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700211 IActivityTaskManager activityTaskManager, PipMenuActivityController menuController,
Hongwei Wang43a752b2019-09-17 20:20:30 +0000212 InputConsumerController inputConsumerController,
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500213 PipBoundsHandler pipBoundsHandler,
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800214 PipTaskOrganizer pipTaskOrganizer,
Beverly660d0a72020-02-26 12:32:42 -0500215 FloatingContentCoordinator floatingContentCoordinator,
jorgegil@google.com5bdec5f2020-02-28 13:14:39 -0800216 DeviceConfigProxy deviceConfig,
217 PipSnapAlgorithm pipSnapAlgorithm) {
Winson73bc1592016-10-18 18:47:43 -0700218 // Initialize the Pip input consumer
Winson73bc1592016-10-18 18:47:43 -0700219 mContext = context;
220 mActivityManager = activityManager;
Phil Weaverf00cd142017-03-03 13:44:00 -0800221 mAccessibilityManager = context.getSystemService(AccessibilityManager.class);
Joshua Tsujif39539d2020-04-03 18:53:06 -0400222 mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Winson Chung15504af2016-11-02 18:11:36 -0700223 mMenuController = menuController;
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800224 mMenuController.addListener(new PipMenuListener());
jorgegil@google.com5bdec5f2020-02-28 13:14:39 -0800225 mSnapAlgorithm = pipSnapAlgorithm;
Dave Mankoff1373fdb2019-12-18 14:04:37 -0500226 mFlingAnimationUtils = new FlingAnimationUtils(context.getResources().getDisplayMetrics(),
227 2.5f);
Ben Lin1a80f122020-01-27 14:09:52 -0800228 mGesture = new DefaultPipTouchGesture();
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800229 mMotionHelper = new PipMotionHelper(mContext, activityTaskManager, pipTaskOrganizer,
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500230 mMenuController, mSnapAlgorithm, mFlingAnimationUtils, floatingContentCoordinator);
Ben Lin04c83f62019-12-20 10:56:45 -0800231 mPipResizeGestureHandler =
Beverly660d0a72020-02-26 12:32:42 -0500232 new PipResizeGestureHandler(context, pipBoundsHandler, this, mMotionHelper,
Ben Lin7d6b8e72020-02-27 17:48:16 -0800233 deviceConfig, pipTaskOrganizer);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800234 mTouchState = new PipTouchState(ViewConfiguration.get(context), mHandler,
Winson Chungbca03112017-08-16 10:38:15 -0700235 () -> mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
236 mMovementBounds, true /* allowMenuTimeout */, willResizeMenu()));
Winson Chung136d1ec82017-07-18 11:16:30 -0700237
238 Resources res = context.getResources();
239 mExpandedShortestEdgeSize = res.getDimensionPixelSize(
Mady Mellora7f69742017-02-03 11:00:20 -0800240 R.dimen.pip_expanded_shortest_edge_size);
Winson Chung136d1ec82017-07-18 11:16:30 -0700241 mImeOffset = res.getDimensionPixelSize(R.dimen.pip_ime_offset);
Joshua Tsujif39539d2020-04-03 18:53:06 -0400242 mDismissAreaHeight = res.getDimensionPixelSize(R.dimen.floating_dismiss_gradient_height);
Winson Chungd2d90972017-02-28 11:40:41 -0800243
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800244 mEnableDismissDragToEdge = res.getBoolean(R.bool.config_pipEnableDismissDragToEdge);
Ben Line55f85f2020-01-27 14:49:41 -0800245 mEnableResize = res.getBoolean(R.bool.config_pipEnableResizeForMenu);
Eliot Courtney3dc12f12018-06-01 14:26:26 +0900246
Winson Chungd2d90972017-02-28 11:40:41 -0800247 // Register the listener for input consumer touch events
Tracy Zhou535c2122019-02-21 17:02:47 -0800248 inputConsumerController.setInputListener(this::handleTouchEvent);
Phil Weaverf00cd142017-03-03 13:44:00 -0800249 inputConsumerController.setRegistrationListener(this::onRegistrationChanged);
Hongwei Wang43a752b2019-09-17 20:20:30 +0000250
251 mPipBoundsHandler = pipBoundsHandler;
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500252 mFloatingContentCoordinator = floatingContentCoordinator;
Arthur Hungd8961972019-10-14 17:00:17 +0800253 mConnection = new PipAccessibilityInteractionConnection(mMotionHelper,
254 this::onAccessibilityShowMenu, mHandler);
Joshua Tsujif39539d2020-04-03 18:53:06 -0400255
256 final int targetSize = res.getDimensionPixelSize(R.dimen.dismiss_circle_size);
257 mTargetView = new DismissCircleView(context);
258 final FrameLayout.LayoutParams newParams =
259 new FrameLayout.LayoutParams(targetSize, targetSize);
260 newParams.gravity = Gravity.CENTER;
261 mTargetView.setLayoutParams(newParams);
262
263 mTargetViewContainer = new FrameLayout(context);
264 mTargetViewContainer.setClipChildren(false);
265 mTargetViewContainer.addView(mTargetView);
266
267 mMagnetizedPip = mMotionHelper.getMagnetizedPip();
268 mMagneticTarget = mMagnetizedPip.addTarget(mTargetView, 0);
269 mMagnetizedPip.setPhysicsAnimatorUpdateListener(mMotionHelper.mResizePipUpdateListener);
270 mMagnetizedPip.setMagnetListener(new MagnetizedObject.MagnetListener() {
271 @Override
272 public void onStuckToTarget(@NonNull MagnetizedObject.MagneticTarget target) {
273 mMotionHelper.prepareForAnimation();
274
275 // Show the dismiss target, in case the initial touch event occurred within the
276 // magnetic field radius.
277 showDismissTargetMaybe();
278 }
279
280 @Override
281 public void onUnstuckFromTarget(@NonNull MagnetizedObject.MagneticTarget target,
282 float velX, float velY, boolean wasFlungOut) {
283 if (wasFlungOut) {
284 mMotionHelper.flingToSnapTarget(velX, velY, null, null);
285 hideDismissTarget();
286 } else {
287 mMotionHelper.setSpringingToTouch(true);
288 }
289 }
290
291 @Override
292 public void onReleasedInTarget(@NonNull MagnetizedObject.MagneticTarget target) {
293 mHandler.post(() -> {
294 mMotionHelper.animateDismiss(0, 0, null);
295 hideDismissTarget();
296 });
297
298
299 MetricsLoggerWrapper.logPictureInPictureDismissByDrag(mContext,
300 PipUtils.getTopPipActivity(mContext, mActivityManager));
301 }
302 });
303
304 mMagneticTargetAnimator = PhysicsAnimator.getInstance(mTargetView);
Winson73bc1592016-10-18 18:47:43 -0700305 }
306
Ben Lin1a80f122020-01-27 14:09:52 -0800307 public void setTouchGesture(PipTouchGesture gesture) {
308 mGesture = gesture;
309 }
310
Winson Chung85d39982017-02-24 15:21:25 -0800311 public void setTouchEnabled(boolean enabled) {
312 mTouchState.setAllowTouches(enabled);
313 }
314
Winson Chungac52f282017-03-30 14:44:52 -0700315 public void showPictureInPictureMenu() {
316 // Only show the menu if the user isn't currently interacting with the PiP
317 if (!mTouchState.isUserInteracting()) {
Mady Mellor637cd482017-03-21 10:39:42 -0700318 mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
Winson Chungbb787442017-09-01 11:33:47 -0700319 mMovementBounds, false /* allowMenuTimeout */, willResizeMenu());
Winson Chungac52f282017-03-30 14:44:52 -0700320 }
321 }
322
Winson Chung929d4f72017-01-13 10:21:33 -0800323 public void onActivityPinned() {
Joshua Tsujif7de6802020-04-13 13:30:11 -0400324 createOrUpdateDismissTarget();
Joshua Tsujif39539d2020-04-03 18:53:06 -0400325
Wale Ogunwale6455e502017-04-17 14:16:43 -0700326 mShowPipMenuOnAnimationEnd = true;
Ben Lin04c83f62019-12-20 10:56:45 -0800327 mPipResizeGestureHandler.onActivityPinned();
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500328 mFloatingContentCoordinator.onContentAdded(mMotionHelper);
Winson Chung929d4f72017-01-13 10:21:33 -0800329 }
330
Winson Chung9b919412017-06-19 17:01:51 -0700331 public void onActivityUnpinned(ComponentName topPipActivity) {
332 if (topPipActivity == null) {
333 // Clean up state after the last PiP activity is removed
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500334 cleanUpDismissTarget();
335
336 mFloatingContentCoordinator.onContentRemoved(mMotionHelper);
Winson Chung9b919412017-06-19 17:01:51 -0700337 }
Ben Lin094c7752020-03-11 12:10:51 -0700338 mResizedBounds.setEmpty();
Ben Lin04c83f62019-12-20 10:56:45 -0800339 mPipResizeGestureHandler.onActivityUnpinned();
Winson Chung9b919412017-06-19 17:01:51 -0700340 }
341
Winson Chungac52f282017-03-30 14:44:52 -0700342 public void onPinnedStackAnimationEnded() {
343 // Always synchronize the motion helper bounds once PiP animations finish
344 mMotionHelper.synchronizePinnedStackBounds();
Ben Lin094c7752020-03-11 12:10:51 -0700345 updateMovementBounds();
346 mResizedBounds.set(mMotionHelper.getBounds());
Wale Ogunwale6455e502017-04-17 14:16:43 -0700347
348 if (mShowPipMenuOnAnimationEnd) {
349 mMenuController.showMenu(MENU_STATE_CLOSE, mMotionHelper.getBounds(),
Winson Chungbb787442017-09-01 11:33:47 -0700350 mMovementBounds, true /* allowMenuTimeout */, false /* willResizeMenu */);
Wale Ogunwale6455e502017-04-17 14:16:43 -0700351 mShowPipMenuOnAnimationEnd = false;
352 }
Winson Chungac52f282017-03-30 14:44:52 -0700353 }
354
Winson Chung303c6b72016-10-24 17:12:49 -0700355 public void onConfigurationChanged() {
Winson Chung2a82fe52017-02-02 14:43:34 -0800356 mMotionHelper.onConfigurationChanged();
357 mMotionHelper.synchronizePinnedStackBounds();
Joshua Tsujif39539d2020-04-03 18:53:06 -0400358
359 // Recreate the dismiss target for the new orientation.
Joshua Tsujif7de6802020-04-13 13:30:11 -0400360 createOrUpdateDismissTarget();
Winson Chung303c6b72016-10-24 17:12:49 -0700361 }
362
Winson Chung2a82fe52017-02-02 14:43:34 -0800363 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
364 mIsImeShowing = imeVisible;
365 mImeHeight = imeHeight;
366 }
367
Tracy Zhou43513082018-03-08 21:58:36 -0800368 public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight) {
369 mIsShelfShowing = shelfVisible;
370 mShelfHeight = shelfHeight;
371 }
372
Ben Lin06dc41f2020-03-05 00:39:27 +0000373 public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds, Rect curBounds,
Tracy Zhou43513082018-03-08 21:58:36 -0800374 boolean fromImeAdjustment, boolean fromShelfAdjustment, int displayRotation) {
375 final int bottomOffset = mIsImeShowing ? mImeHeight : 0;
Hongwei Wangbede2c12020-02-26 12:11:53 -0800376 final boolean fromDisplayRotationChanged = (mDisplayRotation != displayRotation);
377 if (fromDisplayRotationChanged) {
378 mTouchState.reset();
379 }
Tracy Zhou43513082018-03-08 21:58:36 -0800380
Winson Chung2a82fe52017-02-02 14:43:34 -0800381 // Re-calculate the expanded bounds
Hongwei Wang3c981f62020-04-07 16:16:26 -0700382 mNormalBounds.set(normalBounds);
Ben Lin06dc41f2020-03-05 00:39:27 +0000383 Rect normalMovementBounds = new Rect();
384 mSnapAlgorithm.getMovementBounds(mNormalBounds, insetBounds, normalMovementBounds,
Tracy Zhou43513082018-03-08 21:58:36 -0800385 bottomOffset);
Mady Mellora7f69742017-02-03 11:00:20 -0800386
Hongwei Wang3c981f62020-04-07 16:16:26 -0700387 if (mMovementBounds.isEmpty()) {
388 // mMovementBounds is not initialized yet and a clean movement bounds without
389 // bottom offset shall be used later in this function.
390 mSnapAlgorithm.getMovementBounds(curBounds, insetBounds, mMovementBounds,
391 0 /* bottomOffset */);
392 }
393
Mady Mellora7f69742017-02-03 11:00:20 -0800394 // Calculate the expanded size
Ben Lin06dc41f2020-03-05 00:39:27 +0000395 float aspectRatio = (float) normalBounds.width() / normalBounds.height();
Mady Mellora7f69742017-02-03 11:00:20 -0800396 Point displaySize = new Point();
397 mContext.getDisplay().getRealSize(displaySize);
Ben Lin06dc41f2020-03-05 00:39:27 +0000398 Size expandedSize = mSnapAlgorithm.getSizeForAspectRatio(aspectRatio,
Mady Mellora7f69742017-02-03 11:00:20 -0800399 mExpandedShortestEdgeSize, displaySize.x, displaySize.y);
Ben Lin06dc41f2020-03-05 00:39:27 +0000400 mExpandedBounds.set(0, 0, expandedSize.getWidth(), expandedSize.getHeight());
Ben Lin06dc41f2020-03-05 00:39:27 +0000401 Rect expandedMovementBounds = new Rect();
402 mSnapAlgorithm.getMovementBounds(mExpandedBounds, insetBounds, expandedMovementBounds,
Tracy Zhou43513082018-03-08 21:58:36 -0800403 bottomOffset);
Winson Chung2a82fe52017-02-02 14:43:34 -0800404
Ben Lin094c7752020-03-11 12:10:51 -0700405 mPipResizeGestureHandler.updateMinSize(mNormalBounds.width(), mNormalBounds.height());
406 mPipResizeGestureHandler.updateMaxSize(mExpandedBounds.width(), mExpandedBounds.height());
407
Winson Chungdec1a662019-06-04 21:20:41 -0700408 // The extra offset does not really affect the movement bounds, but are applied based on the
409 // current state (ime showing, or shelf offset) when we need to actually shift
410 int extraOffset = Math.max(
411 mIsImeShowing ? mImeOffset : 0,
412 !mIsImeShowing && mIsShelfShowing ? mShelfHeight : 0);
413
Tracy Zhou43513082018-03-08 21:58:36 -0800414 // If this is from an IME or shelf adjustment, then we should move the PiP so that it is not
415 // occluded by the IME or shelf.
Hongwei Wangbede2c12020-02-26 12:11:53 -0800416 if (fromImeAdjustment || fromShelfAdjustment || fromDisplayRotationChanged) {
Winson Chung2a82fe52017-02-02 14:43:34 -0800417 if (mTouchState.isUserInteracting()) {
418 // Defer the update of the current movement bounds until after the user finishes
419 // touching the screen
420 } else {
Winson Chungdec1a662019-06-04 21:20:41 -0700421 final float offsetBufferPx = BOTTOM_OFFSET_BUFFER_DP
Tracy Zhoue19d0512018-07-31 16:29:34 -0700422 * mContext.getResources().getDisplayMetrics().density;
Ben Line55f85f2020-01-27 14:49:41 -0800423 final Rect toMovementBounds = mMenuState == MENU_STATE_FULL && willResizeMenu()
Ben Lin06dc41f2020-03-05 00:39:27 +0000424 ? new Rect(expandedMovementBounds)
425 : new Rect(normalMovementBounds);
Winson Chungdec1a662019-06-04 21:20:41 -0700426 final int prevBottom = mMovementBounds.bottom - mMovementBoundsExtraOffsets;
427 final int toBottom = toMovementBounds.bottom < toMovementBounds.top
428 ? toMovementBounds.bottom
429 : toMovementBounds.bottom - extraOffset;
430 if ((Math.min(prevBottom, toBottom) - offsetBufferPx) <= curBounds.top
431 && curBounds.top <= (Math.max(prevBottom, toBottom) + offsetBufferPx)) {
432 mMotionHelper.animateToOffset(curBounds, toBottom - curBounds.top);
Tracy Zhou805e66c2018-04-04 23:45:26 -0700433 }
Winson Chung2a82fe52017-02-02 14:43:34 -0800434 }
Winson Chung14fbe142016-12-19 16:18:24 -0800435 }
Winson Chunga29eb982016-12-14 12:01:27 -0800436
Winson Chung2a82fe52017-02-02 14:43:34 -0800437 // Update the movement bounds after doing the calculations based on the old movement bounds
438 // above
Hongwei Wang3c981f62020-04-07 16:16:26 -0700439 mNormalMovementBounds.set(normalMovementBounds);
440 mExpandedMovementBounds.set(expandedMovementBounds);
Winson Chungef4dc812017-04-11 13:31:44 -0700441 mDisplayRotation = displayRotation;
Winson Chung0a2f34f2017-08-11 18:13:24 -0700442 mInsetBounds.set(insetBounds);
Ben Lin094c7752020-03-11 12:10:51 -0700443 updateMovementBounds();
Winson Chungdec1a662019-06-04 21:20:41 -0700444 mMovementBoundsExtraOffsets = extraOffset;
Winson Chungef4dc812017-04-11 13:31:44 -0700445
446 // If we have a deferred resize, apply it now
447 if (mDeferResizeToNormalBoundsUntilRotation == displayRotation) {
Ben Lin06dc41f2020-03-05 00:39:27 +0000448 mMotionHelper.animateToUnexpandedState(normalBounds, mSavedSnapFraction,
449 mNormalMovementBounds, mMovementBounds, true /* immediate */);
Winson Chungef4dc812017-04-11 13:31:44 -0700450 mSavedSnapFraction = -1f;
451 mDeferResizeToNormalBoundsUntilRotation = -1;
452 }
Winson Chunga29eb982016-12-14 12:01:27 -0800453 }
454
Joshua Tsujif39539d2020-04-03 18:53:06 -0400455 /** Adds the magnetic target view to the WindowManager so it's ready to be animated in. */
Joshua Tsujif7de6802020-04-13 13:30:11 -0400456 private void createOrUpdateDismissTarget() {
Joshua Tsujif39539d2020-04-03 18:53:06 -0400457 if (!mTargetViewContainer.isAttachedToWindow()) {
458 mHandler.removeCallbacks(mShowTargetAction);
459 mMagneticTargetAnimator.cancel();
460
Joshua Tsujif39539d2020-04-03 18:53:06 -0400461 mTargetViewContainer.setVisibility(View.INVISIBLE);
Joshua Tsujif7de6802020-04-13 13:30:11 -0400462
463 try {
464 mWindowManager.addView(mTargetViewContainer, getDismissTargetLayoutParams());
465 } catch (IllegalStateException e) {
466 // This shouldn't happen, but if the target is already added, just update its layout
467 // params.
468 mWindowManager.updateViewLayout(
469 mTargetViewContainer, getDismissTargetLayoutParams());
470 }
471 } else {
472 mWindowManager.updateViewLayout(mTargetViewContainer, getDismissTargetLayoutParams());
Joshua Tsujif39539d2020-04-03 18:53:06 -0400473 }
474 }
475
Joshua Tsujif7de6802020-04-13 13:30:11 -0400476 /** Returns layout params for the dismiss target, using the latest display metrics. */
477 private WindowManager.LayoutParams getDismissTargetLayoutParams() {
478 final Point windowSize = new Point();
479 mWindowManager.getDefaultDisplay().getRealSize(windowSize);
480
481 final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
482 WindowManager.LayoutParams.MATCH_PARENT,
483 mDismissAreaHeight,
484 0, windowSize.y - mDismissAreaHeight,
485 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL,
486 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
487 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
488 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
489 PixelFormat.TRANSLUCENT);
490
491 lp.setTitle("pip-dismiss-overlay");
492 lp.privateFlags |= WindowManager.LayoutParams.SYSTEM_FLAG_SHOW_FOR_ALL_USERS;
493 lp.setFitInsetsTypes(0 /* types */);
494
495 return lp;
496 }
497
Joshua Tsujif39539d2020-04-03 18:53:06 -0400498 /** Makes the dismiss target visible and animates it in, if it isn't already visible. */
499 private void showDismissTargetMaybe() {
Joshua Tsujif7de6802020-04-13 13:30:11 -0400500 createOrUpdateDismissTarget();
Joshua Tsujif39539d2020-04-03 18:53:06 -0400501
502 if (mTargetViewContainer.getVisibility() != View.VISIBLE) {
503
504 mTargetView.setTranslationY(mTargetViewContainer.getHeight());
505 mTargetViewContainer.setVisibility(View.VISIBLE);
506
507 // Set the magnetic field radius to half of PIP's width.
508 mMagneticTarget.setMagneticFieldRadiusPx(mMotionHelper.getBounds().width());
509
510 // Cancel in case we were in the middle of animating it out.
511 mMagneticTargetAnimator.cancel();
512 mMagneticTargetAnimator
513 .spring(DynamicAnimation.TRANSLATION_Y, 0f, mTargetSpringConfig)
514 .start();
515 }
516 }
517
518 /** Animates the magnetic dismiss target out and then sets it to GONE. */
519 private void hideDismissTarget() {
520 mHandler.removeCallbacks(mShowTargetAction);
521 mMagneticTargetAnimator
522 .spring(DynamicAnimation.TRANSLATION_Y,
523 mTargetViewContainer.getHeight(),
524 mTargetSpringConfig)
525 .withEndActions(() -> mTargetViewContainer.setVisibility(View.GONE))
526 .start();
527 }
528
529 /**
530 * Removes the dismiss target and cancels any pending callbacks to show it.
531 */
532 private void cleanUpDismissTarget() {
533 mHandler.removeCallbacks(mShowTargetAction);
534
535 if (mTargetViewContainer.isAttachedToWindow()) {
Joshua Tsuji06745aa2020-04-09 11:17:42 -0400536 mWindowManager.removeViewImmediate(mTargetViewContainer);
Joshua Tsujif39539d2020-04-03 18:53:06 -0400537 }
538 }
539
Phil Weaverf00cd142017-03-03 13:44:00 -0800540 private void onRegistrationChanged(boolean isRegistered) {
541 mAccessibilityManager.setPictureInPictureActionReplacingConnection(isRegistered
Arthur Hungd8961972019-10-14 17:00:17 +0800542 ? mConnection : null);
Winson Chungb54b65b2017-04-26 14:02:13 -0700543 if (!isRegistered && mTouchState.isUserInteracting()) {
544 // If the input consumer is unregistered while the user is interacting, then we may not
545 // get the final TOUCH_UP event, so clean up the dismiss target as well
546 cleanUpDismissTarget();
547 }
Winson Chungfe1fa642017-03-13 10:51:22 -0700548 }
549
550 private void onAccessibilityShowMenu() {
Mady Mellor637cd482017-03-21 10:39:42 -0700551 mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
Rhed Jao5e00c982018-11-27 19:41:05 +0800552 mMovementBounds, true /* allowMenuTimeout */, willResizeMenu());
Phil Weaverf00cd142017-03-03 13:44:00 -0800553 }
554
Tracy Zhou535c2122019-02-21 17:02:47 -0800555 private boolean handleTouchEvent(InputEvent inputEvent) {
556 // Skip any non motion events
557 if (!(inputEvent instanceof MotionEvent)) {
558 return true;
559 }
Winson Chung655332c2016-10-31 13:14:28 -0700560 // Skip touch handling until we are bound to the controller
561 if (mPinnedStackController == null) {
Winson Chung15504af2016-11-02 18:11:36 -0700562 return true;
Winson Chung655332c2016-10-31 13:14:28 -0700563 }
Joshua Tsujif39539d2020-04-03 18:53:06 -0400564
Tracy Zhou535c2122019-02-21 17:02:47 -0800565 MotionEvent ev = (MotionEvent) inputEvent;
Winson Chung655332c2016-10-31 13:14:28 -0700566
Joshua Tsujif39539d2020-04-03 18:53:06 -0400567 if (mMagnetizedPip.maybeConsumeMotionEvent(ev)) {
568 // If the first touch event occurs within the magnetic field, pass the ACTION_DOWN event
569 // to the touch state. Touch state needs a DOWN event in order to later process MOVE
570 // events it'll receive if the object is dragged out of the magnetic field.
571 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
572 mTouchState.onTouchEvent(ev);
573 }
574
575 // Continue tracking velocity when the object is in the magnetic field, since we want to
576 // respect touch input velocity if the object is dragged out and then flung.
577 mTouchState.addMovementToVelocityTracker(ev);
578
579 return true;
580 }
581
Winson Chungfa7053782016-11-08 15:45:10 -0800582 // Update the touch state
583 mTouchState.onTouchEvent(ev);
584
Arthur Hungd52d1192019-08-30 16:06:59 +0800585 boolean shouldDeliverToMenu = mMenuState != MENU_STATE_NONE;
586
Winson73bc1592016-10-18 18:47:43 -0700587 switch (ev.getAction()) {
588 case MotionEvent.ACTION_DOWN: {
Joshua Tsuji601dc852020-04-02 15:54:15 -0400589 mMotionHelper.synchronizePinnedStackBoundsForTouchGesture();
Ben Lin1a80f122020-01-27 14:09:52 -0800590 mGesture.onDown(mTouchState);
Winson73bc1592016-10-18 18:47:43 -0700591 break;
592 }
593 case MotionEvent.ACTION_MOVE: {
Ben Lin1a80f122020-01-27 14:09:52 -0800594 if (mGesture.onMove(mTouchState)) {
595 break;
Winson73bc1592016-10-18 18:47:43 -0700596 }
Arthur Hungd52d1192019-08-30 16:06:59 +0800597
598 shouldDeliverToMenu = !mTouchState.isDragging();
Winson73bc1592016-10-18 18:47:43 -0700599 break;
600 }
601 case MotionEvent.ACTION_UP: {
Winson Chung14fefc22016-11-02 10:02:29 -0700602 // Update the movement bounds again if the state has changed since the user started
603 // dragging (ie. when the IME shows)
Ben Lin094c7752020-03-11 12:10:51 -0700604 updateMovementBounds();
Winson Chung14fefc22016-11-02 10:02:29 -0700605
Ben Lin1a80f122020-01-27 14:09:52 -0800606 if (mGesture.onUp(mTouchState)) {
607 break;
Winson Chung5cd26ff2016-10-24 11:50:44 -0700608 }
Winson73bc1592016-10-18 18:47:43 -0700609
610 // Fall through to clean up
611 }
612 case MotionEvent.ACTION_CANCEL: {
Arthur Hungd52d1192019-08-30 16:06:59 +0800613 shouldDeliverToMenu = !mTouchState.startedDragging() && !mTouchState.isDragging();
Winson Chung85d39982017-02-24 15:21:25 -0800614 mTouchState.reset();
Winson73bc1592016-10-18 18:47:43 -0700615 break;
616 }
Phil Weaverf00cd142017-03-03 13:44:00 -0800617 case MotionEvent.ACTION_HOVER_ENTER:
Ben Lin388df602020-04-08 14:07:06 -0700618 // If Touch Exploration is enabled, some a11y services (e.g. Talkback) is probably
619 // on and changing MotionEvents into HoverEvents.
620 // Let's not enable menu show/hide for a11y services.
621 if (!mAccessibilityManager.isTouchExplorationEnabled()) {
622 mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
623 mMovementBounds, false /* allowMenuTimeout */,
624 false /* willResizeMenu */);
625 }
Phil Weaverf00cd142017-03-03 13:44:00 -0800626 case MotionEvent.ACTION_HOVER_MOVE: {
Arthur Hungd8961972019-10-14 17:00:17 +0800627 if (!shouldDeliverToMenu && !mSendingHoverAccessibilityEvents) {
628 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Phil Weaverf00cd142017-03-03 13:44:00 -0800629 mSendingHoverAccessibilityEvents = true;
630 }
631 break;
632 }
633 case MotionEvent.ACTION_HOVER_EXIT: {
Ben Lin388df602020-04-08 14:07:06 -0700634 // If Touch Exploration is enabled, some a11y services (e.g. Talkback) is probably
635 // on and changing MotionEvents into HoverEvents.
636 // Let's not enable menu show/hide for a11y services.
637 if (!mAccessibilityManager.isTouchExplorationEnabled()) {
638 mMenuController.hideMenu();
639 }
Arthur Hungd8961972019-10-14 17:00:17 +0800640 if (!shouldDeliverToMenu && mSendingHoverAccessibilityEvents) {
641 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Phil Weaverf00cd142017-03-03 13:44:00 -0800642 mSendingHoverAccessibilityEvents = false;
643 }
644 break;
645 }
Winson73bc1592016-10-18 18:47:43 -0700646 }
Arthur Hungd52d1192019-08-30 16:06:59 +0800647
648 // Deliver the event to PipMenuActivity to handle button click if the menu has shown.
649 if (shouldDeliverToMenu) {
650 final MotionEvent cloneEvent = MotionEvent.obtain(ev);
651 // Send the cancel event and cancel menu timeout if it starts to drag.
652 if (mTouchState.startedDragging()) {
653 cloneEvent.setAction(MotionEvent.ACTION_CANCEL);
654 mMenuController.pokeMenu();
655 }
656
Arthur Hungd8961972019-10-14 17:00:17 +0800657 mMenuController.handlePointerEvent(cloneEvent);
Arthur Hungd52d1192019-08-30 16:06:59 +0800658 }
659
660 return true;
Winson Chung15504af2016-11-02 18:11:36 -0700661 }
662
Arthur Hungd8961972019-10-14 17:00:17 +0800663 private void sendAccessibilityHoverEvent(int type) {
664 if (!mAccessibilityManager.isEnabled()) {
665 return;
666 }
667
668 AccessibilityEvent event = AccessibilityEvent.obtain(type);
669 event.setImportantForAccessibility(true);
670 event.setSourceNodeId(AccessibilityNodeInfo.ROOT_NODE_ID);
671 event.setWindowId(
672 AccessibilityWindowInfo.PICTURE_IN_PICTURE_ACTION_REPLACER_WINDOW_ID);
673 mAccessibilityManager.sendAccessibilityEvent(event);
674 }
675
Winson Chung15504af2016-11-02 18:11:36 -0700676 /**
Mady Mellor81d40612017-03-10 15:14:10 -0800677 * Updates the appearance of the menu and scrim on top of the PiP while dismissing.
678 */
Winson Chung8ec0e162017-07-07 14:49:49 -0700679 private void updateDismissFraction() {
Winson Chung0a2f34f2017-08-11 18:13:24 -0700680 // Skip updating the dismiss fraction when the IME is showing. This is to work around an
681 // issue where starting the menu activity for the dismiss overlay will steal the window
682 // focus, which closes the IME.
683 if (mMenuController != null && !mIsImeShowing) {
Mady Mellor81d40612017-03-10 15:14:10 -0800684 Rect bounds = mMotionHelper.getBounds();
Winson Chung0a2f34f2017-08-11 18:13:24 -0700685 final float target = mInsetBounds.bottom;
Mady Mellor81d40612017-03-10 15:14:10 -0800686 float fraction = 0f;
687 if (bounds.bottom > target) {
688 final float distance = bounds.bottom - target;
689 fraction = Math.min(distance / bounds.height(), 1f);
690 }
Winson Chungbca03112017-08-16 10:38:15 -0700691 if (Float.compare(fraction, 0f) != 0 || mMenuController.isMenuActivityVisible()) {
Winson Chung87e5d552017-04-05 11:49:38 -0700692 // Update if the fraction > 0, or if fraction == 0 and the menu was already visible
693 mMenuController.setDismissFraction(fraction);
694 }
Mady Mellor81d40612017-03-10 15:14:10 -0800695 }
696 }
697
698 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800699 * Sets the controller to update the system of changes from user interaction.
700 */
701 void setPinnedStackController(IPinnedStackController controller) {
702 mPinnedStackController = controller;
703 }
704
705 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800706 * Sets the menu visibility.
Winson Chungfa7053782016-11-08 15:45:10 -0800707 */
Winson Chung8ec0e162017-07-07 14:49:49 -0700708 private void setMenuState(int menuState, boolean resize) {
Arthur Hungd8961972019-10-14 17:00:17 +0800709 if (mMenuState == menuState && !resize) {
710 return;
711 }
712
Winson Chunga89053d2018-06-12 16:47:30 -0700713 if (menuState == MENU_STATE_FULL && mMenuState != MENU_STATE_FULL) {
Winson Chungd2d90972017-02-28 11:40:41 -0800714 // Save the current snap fraction and if we do not drag or move the PiP, then
715 // we store back to this snap fraction. Otherwise, we'll reset the snap
Ben Lin094c7752020-03-11 12:10:51 -0700716 // fraction and snap to the closest edge.
717 // Also save the current resized bounds so when the menu disappears, we can restore it.
Winson Chungd2d90972017-02-28 11:40:41 -0800718 if (resize) {
Ben Lin094c7752020-03-11 12:10:51 -0700719 mResizedBounds.set(mMotionHelper.getBounds());
720 Rect expandedBounds = new Rect(mExpandedBounds);
Winson Chung2a82fe52017-02-02 14:43:34 -0800721 mSavedSnapFraction = mMotionHelper.animateToExpandedState(expandedBounds,
Ben Lin06dc41f2020-03-05 00:39:27 +0000722 mMovementBounds, mExpandedMovementBounds);
Winson Chungd2d90972017-02-28 11:40:41 -0800723 }
Winson Chunga89053d2018-06-12 16:47:30 -0700724 } else if (menuState == MENU_STATE_NONE && mMenuState == MENU_STATE_FULL) {
Winson Chungd2d90972017-02-28 11:40:41 -0800725 // Try and restore the PiP to the closest edge, using the saved snap fraction
726 // if possible
727 if (resize) {
Winson Chungef4dc812017-04-11 13:31:44 -0700728 if (mDeferResizeToNormalBoundsUntilRotation == -1) {
Winson Chungbb233762017-05-15 14:20:46 -0700729 // This is a very special case: when the menu is expanded and visible,
730 // navigating to another activity can trigger auto-enter PiP, and if the
731 // revealed activity has a forced rotation set, then the controller will get
732 // updated with the new rotation of the display. However, at the same time,
733 // SystemUI will try to hide the menu by creating an animation to the normal
734 // bounds which are now stale. In such a case we defer the animation to the
735 // normal bounds until after the next onMovementBoundsChanged() call to get the
736 // bounds in the new orientation
Winson Chungef4dc812017-04-11 13:31:44 -0700737 try {
738 int displayRotation = mPinnedStackController.getDisplayRotation();
739 if (mDisplayRotation != displayRotation) {
740 mDeferResizeToNormalBoundsUntilRotation = displayRotation;
741 }
742 } catch (RemoteException e) {
743 Log.e(TAG, "Could not get display rotation from controller");
744 }
745 }
746
747 if (mDeferResizeToNormalBoundsUntilRotation == -1) {
Ben Lin094c7752020-03-11 12:10:51 -0700748 Rect restoreBounds = new Rect(mResizedBounds);
749 Rect restoredMovementBounds = new Rect();
750 mSnapAlgorithm.getMovementBounds(restoreBounds, mInsetBounds,
751 restoredMovementBounds, mIsImeShowing ? mImeHeight : 0);
752 mMotionHelper.animateToUnexpandedState(restoreBounds, mSavedSnapFraction,
753 restoredMovementBounds, mMovementBounds, false /* immediate */);
Winson Chungef4dc812017-04-11 13:31:44 -0700754 mSavedSnapFraction = -1f;
755 }
756 } else {
757 mSavedSnapFraction = -1f;
Winson Chunga29eb982016-12-14 12:01:27 -0800758 }
Winson Chungfa7053782016-11-08 15:45:10 -0800759 }
Mady Mellor637cd482017-03-21 10:39:42 -0700760 mMenuState = menuState;
Ben Lin094c7752020-03-11 12:10:51 -0700761 updateMovementBounds();
Arthur Hungd8961972019-10-14 17:00:17 +0800762 // If pip menu has dismissed, we should register the A11y ActionReplacingConnection for pip
763 // as well, or it can't handle a11y focus and pip menu can't perform any action.
764 onRegistrationChanged(menuState == MENU_STATE_NONE);
Mady Mellor637cd482017-03-21 10:39:42 -0700765 if (menuState != MENU_STATE_CLOSE) {
Chenjie Yu52cacc62017-12-08 18:11:45 -0800766 MetricsLoggerWrapper.logPictureInPictureMenuVisible(mContext, menuState == MENU_STATE_FULL);
Mady Mellor637cd482017-03-21 10:39:42 -0700767 }
Winson Chungfa7053782016-11-08 15:45:10 -0800768 }
769
770 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800771 * @return the motion helper.
Winson Chungfa7053782016-11-08 15:45:10 -0800772 */
Winson Chung2a82fe52017-02-02 14:43:34 -0800773 public PipMotionHelper getMotionHelper() {
774 return mMotionHelper;
Winson73bc1592016-10-18 18:47:43 -0700775 }
Winson Chungfa7053782016-11-08 15:45:10 -0800776
Ben Lin094c7752020-03-11 12:10:51 -0700777 @VisibleForTesting
778 PipResizeGestureHandler getPipResizeGestureHandler() {
779 return mPipResizeGestureHandler;
780 }
781
782 @VisibleForTesting
783 void setPipResizeGestureHandler(PipResizeGestureHandler pipResizeGestureHandler) {
784 mPipResizeGestureHandler = pipResizeGestureHandler;
785 }
786
787 @VisibleForTesting
788 void setPipMotionHelper(PipMotionHelper pipMotionHelper) {
789 mMotionHelper = pipMotionHelper;
790 }
791
Winson Chungfa7053782016-11-08 15:45:10 -0800792 /**
jorgegil@google.com62f735e2019-10-30 14:37:19 -0700793 * @return the unexpanded bounds.
794 */
Ben Lin06dc41f2020-03-05 00:39:27 +0000795 public Rect getNormalBounds() {
796 return mNormalBounds;
jorgegil@google.com62f735e2019-10-30 14:37:19 -0700797 }
798
799 /**
Winson Chungfa7053782016-11-08 15:45:10 -0800800 * Gesture controlling normal movement of the PIP.
801 */
Ben Lin1a80f122020-01-27 14:09:52 -0800802 private class DefaultPipTouchGesture extends PipTouchGesture {
Winson Chung8ec0e162017-07-07 14:49:49 -0700803 private final Point mStartPosition = new Point();
804 private final PointF mDelta = new PointF();
Mady Mellord4e40fb2017-01-26 10:43:16 -0800805
806 @Override
807 public void onDown(PipTouchState touchState) {
Winson Chung85d39982017-02-24 15:21:25 -0800808 if (!touchState.isUserInteracting()) {
809 return;
810 }
811
Mady Mellor15b29c72017-06-07 14:53:58 -0700812 Rect bounds = mMotionHelper.getBounds();
Winson Chung8ec0e162017-07-07 14:49:49 -0700813 mDelta.set(0f, 0f);
814 mStartPosition.set(bounds.left, bounds.top);
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700815 mMovementWithinDismiss = touchState.getDownTouchPosition().y >= mMovementBounds.bottom;
Joshua Tsujif39539d2020-04-03 18:53:06 -0400816 mMotionHelper.setSpringingToTouch(false);
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700817
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800818 // If the menu is still visible then just poke the menu
Mady Mellora7f69742017-02-03 11:00:20 -0800819 // so that it will timeout after the user stops touching it
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800820 if (mMenuState != MENU_STATE_NONE) {
Mady Mellora7f69742017-02-03 11:00:20 -0800821 mMenuController.pokeMenu();
822 }
Mady Mellord4e40fb2017-01-26 10:43:16 -0800823 }
824
Winson Chungfa7053782016-11-08 15:45:10 -0800825 @Override
Ben Lin1a80f122020-01-27 14:09:52 -0800826 public boolean onMove(PipTouchState touchState) {
Winson Chung85d39982017-02-24 15:21:25 -0800827 if (!touchState.isUserInteracting()) {
828 return false;
829 }
830
Winson Chung2a82fe52017-02-02 14:43:34 -0800831 if (touchState.startedDragging()) {
832 mSavedSnapFraction = -1f;
Winson Chung2a82fe52017-02-02 14:43:34 -0800833
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800834 if (mEnableDismissDragToEdge) {
Joshua Tsujif39539d2020-04-03 18:53:06 -0400835 if (mTargetViewContainer.getVisibility() != View.VISIBLE) {
836 mHandler.removeCallbacks(mShowTargetAction);
837 showDismissTargetMaybe();
838 }
Winson Chungb54b65b2017-04-26 14:02:13 -0700839 }
Mady Mellord4e40fb2017-01-26 10:43:16 -0800840 }
841
Winson Chungfa7053782016-11-08 15:45:10 -0800842 if (touchState.isDragging()) {
843 // Move the pinned stack freely
Winson Chung2a82fe52017-02-02 14:43:34 -0800844 final PointF lastDelta = touchState.getLastTouchDelta();
Winson Chung8ec0e162017-07-07 14:49:49 -0700845 float lastX = mStartPosition.x + mDelta.x;
846 float lastY = mStartPosition.y + mDelta.y;
847 float left = lastX + lastDelta.x;
848 float top = lastY + lastDelta.y;
Winson Chung8ec0e162017-07-07 14:49:49 -0700849
850 // Add to the cumulative delta after bounding the position
851 mDelta.x += left - lastX;
852 mDelta.y += top - lastY;
853
854 mTmpBounds.set(mMotionHelper.getBounds());
Winson Chungfa7053782016-11-08 15:45:10 -0800855 mTmpBounds.offsetTo((int) left, (int) top);
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500856 mMotionHelper.movePip(mTmpBounds, true /* isDragging */);
Winson Chung2a82fe52017-02-02 14:43:34 -0800857
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700858 final PointF curPos = touchState.getLastTouchPosition();
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700859 if (mMovementWithinDismiss) {
860 // Track if movement remains near the bottom edge to identify swipe to dismiss
861 mMovementWithinDismiss = curPos.y >= mMovementBounds.bottom;
862 }
Winson Chungfa7053782016-11-08 15:45:10 -0800863 return true;
864 }
865 return false;
866 }
867
868 @Override
869 public boolean onUp(PipTouchState touchState) {
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800870 if (mEnableDismissDragToEdge) {
Joshua Tsujif39539d2020-04-03 18:53:06 -0400871 hideDismissTarget();
Winson Chungb54b65b2017-04-26 14:02:13 -0700872 }
873
Winson Chung85d39982017-02-24 15:21:25 -0800874 if (!touchState.isUserInteracting()) {
875 return false;
876 }
877
Mady Mellor60421c92017-03-29 15:27:37 -0700878 final PointF vel = touchState.getVelocity();
Mady Mellor60421c92017-03-29 15:27:37 -0700879 final float velocity = PointF.length(vel.x, vel.y);
880 final boolean isFling = velocity > mFlingAnimationUtils.getMinVelocityPxPerSecond();
Winson Chungd2d90972017-02-28 11:40:41 -0800881
Winson Chungfa7053782016-11-08 15:45:10 -0800882 if (touchState.isDragging()) {
Joshua Tsuji2e252462020-01-02 11:27:41 -0500883 Runnable endAction = null;
Mady Mellor637cd482017-03-21 10:39:42 -0700884 if (mMenuState != MENU_STATE_NONE) {
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800885 // If the menu is still visible, then just poke the menu so that
886 // it will timeout after the user stops touching it
Mady Mellor637cd482017-03-21 10:39:42 -0700887 mMenuController.showMenu(mMenuState, mMotionHelper.getBounds(),
Winson Chungbb787442017-09-01 11:33:47 -0700888 mMovementBounds, true /* allowMenuTimeout */, willResizeMenu());
Winson Chung87e5d552017-04-05 11:49:38 -0700889 } else {
890 // If the menu is not visible, then we can still be showing the activity for the
891 // dismiss overlay, so just finish it after the animation completes
Joshua Tsuji2e252462020-01-02 11:27:41 -0500892 endAction = mMenuController::hideMenu;
Winson Chungd2d90972017-02-28 11:40:41 -0800893 }
894
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700895 if (isFling) {
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500896 mMotionHelper.flingToSnapTarget(vel.x, vel.y,
Joshua Tsuji2e252462020-01-02 11:27:41 -0500897 PipTouchHandler.this::updateDismissFraction /* updateAction */,
898 endAction /* endAction */);
Winson Chungfa7053782016-11-08 15:45:10 -0800899 } else {
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500900 mMotionHelper.animateToClosestSnapTarget();
Winson Chungfa7053782016-11-08 15:45:10 -0800901 }
Arthur Hungd52d1192019-08-30 16:06:59 +0800902 } else if (mTouchState.isDoubleTap()) {
903 // Expand to fullscreen if this is a double tap
Ben Lin34b6fbf2020-03-13 19:36:02 -0700904 // the PiP should be frozen until the transition ends
905 setTouchEnabled(false);
Arthur Hungd52d1192019-08-30 16:06:59 +0800906 mMotionHelper.expandPip();
Mady Mellor637cd482017-03-21 10:39:42 -0700907 } else if (mMenuState != MENU_STATE_FULL) {
Arthur Hungd52d1192019-08-30 16:06:59 +0800908 if (!mTouchState.isWaitingForDoubleTap()) {
Winson Chungbca03112017-08-16 10:38:15 -0700909 // User has stalled long enough for this not to be a drag or a double tap, just
910 // expand the menu
911 mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
912 mMovementBounds, true /* allowMenuTimeout */, willResizeMenu());
913 } else {
914 // Next touch event _may_ be the second tap for the double-tap, schedule a
915 // fallback runnable to trigger the menu if no touch event occurs before the
916 // next tap
917 mTouchState.scheduleDoubleTapTimeoutCallback();
918 }
Winson Chungfa7053782016-11-08 15:45:10 -0800919 }
920 return true;
921 }
922 };
Mady Mellor3b10dcd2017-01-23 10:08:35 -0800923
924 /**
Ben Lin06dc41f2020-03-05 00:39:27 +0000925 * Updates the current movement bounds based on whether the menu is currently visible and
926 * resized.
Winson Chung2a82fe52017-02-02 14:43:34 -0800927 */
Ben Lin094c7752020-03-11 12:10:51 -0700928 private void updateMovementBounds() {
929 mSnapAlgorithm.getMovementBounds(mMotionHelper.getBounds(), mInsetBounds,
930 mMovementBounds, mIsImeShowing ? mImeHeight : 0);
Ben Lin06dc41f2020-03-05 00:39:27 +0000931 mMotionHelper.setCurrentMovementBounds(mMovementBounds);
Ben Lin094c7752020-03-11 12:10:51 -0700932
933 boolean isMenuExpanded = mMenuState == MENU_STATE_FULL;
934 mPipBoundsHandler.setMinEdgeSize(
935 isMenuExpanded && willResizeMenu() ? mExpandedShortestEdgeSize : 0);
Winson Chung2a82fe52017-02-02 14:43:34 -0800936 }
Winson Chung29a78652017-02-09 18:35:26 -0800937
Winson Chungb54b65b2017-04-26 14:02:13 -0700938 /**
Winson Chungbb787442017-09-01 11:33:47 -0700939 * @return whether the menu will resize as a part of showing the full menu.
940 */
941 private boolean willResizeMenu() {
Ben Line55f85f2020-01-27 14:49:41 -0800942 if (!mEnableResize) {
943 return false;
944 }
Ben Lin06dc41f2020-03-05 00:39:27 +0000945 return mExpandedBounds.width() != mNormalBounds.width()
946 || mExpandedBounds.height() != mNormalBounds.height();
Winson Chungbb787442017-09-01 11:33:47 -0700947 }
948
Winson Chung29a78652017-02-09 18:35:26 -0800949 public void dump(PrintWriter pw, String prefix) {
950 final String innerPrefix = prefix + " ";
951 pw.println(prefix + TAG);
952 pw.println(innerPrefix + "mMovementBounds=" + mMovementBounds);
Ben Lin06dc41f2020-03-05 00:39:27 +0000953 pw.println(innerPrefix + "mNormalBounds=" + mNormalBounds);
954 pw.println(innerPrefix + "mNormalMovementBounds=" + mNormalMovementBounds);
955 pw.println(innerPrefix + "mExpandedBounds=" + mExpandedBounds);
956 pw.println(innerPrefix + "mExpandedMovementBounds=" + mExpandedMovementBounds);
Mady Mellor637cd482017-03-21 10:39:42 -0700957 pw.println(innerPrefix + "mMenuState=" + mMenuState);
Winson Chung29a78652017-02-09 18:35:26 -0800958 pw.println(innerPrefix + "mIsImeShowing=" + mIsImeShowing);
959 pw.println(innerPrefix + "mImeHeight=" + mImeHeight);
Tracy Zhou43513082018-03-08 21:58:36 -0800960 pw.println(innerPrefix + "mIsShelfShowing=" + mIsShelfShowing);
961 pw.println(innerPrefix + "mShelfHeight=" + mShelfHeight);
Winson Chung29a78652017-02-09 18:35:26 -0800962 pw.println(innerPrefix + "mSavedSnapFraction=" + mSavedSnapFraction);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800963 pw.println(innerPrefix + "mEnableDragToEdgeDismiss=" + mEnableDismissDragToEdge);
Winson Chung29a78652017-02-09 18:35:26 -0800964 mSnapAlgorithm.dump(pw, innerPrefix);
965 mTouchState.dump(pw, innerPrefix);
966 mMotionHelper.dump(pw, innerPrefix);
967 }
Phil Weaverf00cd142017-03-03 13:44:00 -0800968
Winson73bc1592016-10-18 18:47:43 -0700969}