blob: f5c83c1fffd718431491f29229814243032cffe0 [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;
Wale Ogunwale65ebd952018-04-25 15:41:44 -070026import android.app.IActivityTaskManager;
Winson Chung9b919412017-06-19 17:01:51 -070027import android.content.ComponentName;
Winson73bc1592016-10-18 18:47:43 -070028import android.content.Context;
Winson Chung136d1ec82017-07-18 11:16:30 -070029import android.content.res.Resources;
Joshua Tsujif39539d2020-04-03 18:53:06 -040030import android.graphics.PixelFormat;
Mady Mellora7f69742017-02-03 11:00:20 -080031import android.graphics.Point;
Winson73bc1592016-10-18 18:47:43 -070032import android.graphics.PointF;
33import android.graphics.Rect;
Mady Mellord4e40fb2017-01-26 10:43:16 -080034import android.os.Handler;
Winson73bc1592016-10-18 18:47:43 -070035import android.os.RemoteException;
36import android.util.Log;
Winson Chungbe00a832020-03-10 11:08:35 -070037import android.util.Pair;
Mady Mellora7f69742017-02-03 11:00:20 -080038import android.util.Size;
Joshua Tsujif39539d2020-04-03 18:53:06 -040039import android.view.Gravity;
Winson Chung655332c2016-10-31 13:14:28 -070040import android.view.IPinnedStackController;
Tracy Zhou535c2122019-02-21 17:02:47 -080041import android.view.InputEvent;
Winson73bc1592016-10-18 18:47:43 -070042import android.view.MotionEvent;
Joshua Tsujif39539d2020-04-03 18:53:06 -040043import android.view.View;
Winson73bc1592016-10-18 18:47:43 -070044import android.view.ViewConfiguration;
Joshua Tsujif39539d2020-04-03 18:53:06 -040045import android.view.ViewGroup;
46import android.view.WindowManager;
Phil Weaverf00cd142017-03-03 13:44:00 -080047import android.view.accessibility.AccessibilityEvent;
48import android.view.accessibility.AccessibilityManager;
49import android.view.accessibility.AccessibilityNodeInfo;
Phil Weaver0a8caa12017-08-09 11:28:41 -070050import android.view.accessibility.AccessibilityWindowInfo;
Joshua Tsujif39539d2020-04-03 18:53:06 -040051import android.widget.FrameLayout;
52
53import androidx.annotation.NonNull;
54import androidx.dynamicanimation.animation.DynamicAnimation;
55import androidx.dynamicanimation.animation.SpringForce;
Chenjie Yuae9fdf042018-02-15 10:19:32 -080056
Ben Lin094c7752020-03-11 12:10:51 -070057import com.android.internal.annotations.VisibleForTesting;
Chenjie Yu52cacc62017-12-08 18:11:45 -080058import com.android.internal.os.logging.MetricsLoggerWrapper;
Mady Mellora7f69742017-02-03 11:00:20 -080059import com.android.systemui.R;
Hongwei Wang081ecb32020-04-13 16:49:03 -070060import com.android.systemui.pip.PipAnimationController;
Hongwei Wang43a752b2019-09-17 20:20:30 +000061import com.android.systemui.pip.PipBoundsHandler;
Hongwei Wangebf18082019-09-26 14:25:11 -070062import com.android.systemui.pip.PipSnapAlgorithm;
Hongwei Wang85cf41f2020-01-15 15:14:47 -080063import com.android.systemui.pip.PipTaskOrganizer;
Winson Chungcbb15a92018-01-25 17:46:16 +000064import com.android.systemui.shared.system.InputConsumerController;
Winson73bc1592016-10-18 18:47:43 -070065import com.android.systemui.statusbar.FlingAnimationUtils;
Beverly660d0a72020-02-26 12:32:42 -050066import com.android.systemui.util.DeviceConfigProxy;
Joshua Tsujif39539d2020-04-03 18:53:06 -040067import com.android.systemui.util.DismissCircleView;
Joshua Tsujic81ff3d2020-02-13 14:48:40 -050068import com.android.systemui.util.FloatingContentCoordinator;
Joshua Tsujif39539d2020-04-03 18:53:06 -040069import com.android.systemui.util.animation.PhysicsAnimator;
70import com.android.systemui.util.magnetictarget.MagnetizedObject;
Winson73bc1592016-10-18 18:47:43 -070071
Winson Chung29a78652017-02-09 18:35:26 -080072import java.io.PrintWriter;
73
Winson73bc1592016-10-18 18:47:43 -070074/**
75 * Manages all the touch handling for PIP on the Phone, including moving, dismissing and expanding
76 * the PIP.
77 */
Mady Mellor8c7dc422017-05-10 12:55:06 -070078public class PipTouchHandler {
Winson73bc1592016-10-18 18:47:43 -070079 private static final String TAG = "PipTouchHandler";
Winson73bc1592016-10-18 18:47:43 -070080
Mady Mellor5d58d252017-04-18 12:48:04 -070081 private static final int SHOW_DISMISS_AFFORDANCE_DELAY = 225;
Tracy Zhoue19d0512018-07-31 16:29:34 -070082 private static final int BOTTOM_OFFSET_BUFFER_DP = 1;
Winson Chungfa7053782016-11-08 15:45:10 -080083
Winson Chung6e35ee1f2017-02-14 12:06:44 -080084 // Allow dragging the PIP to a location to close it
Hongwei Wang85cf41f2020-01-15 15:14:47 -080085 private final boolean mEnableDismissDragToEdge;
Ben Line55f85f2020-01-27 14:49:41 -080086 // Allow PIP to resize to a slightly bigger state upon touch
87 private final boolean mEnableResize;
Winson73bc1592016-10-18 18:47:43 -070088 private final Context mContext;
Joshua Tsujif39539d2020-04-03 18:53:06 -040089 private final WindowManager mWindowManager;
Winson73bc1592016-10-18 18:47:43 -070090 private final IActivityManager mActivityManager;
Hongwei Wang43a752b2019-09-17 20:20:30 +000091 private final PipBoundsHandler mPipBoundsHandler;
Ben Lin094c7752020-03-11 12:10:51 -070092 private PipResizeGestureHandler mPipResizeGestureHandler;
Winson Chung655332c2016-10-31 13:14:28 -070093 private IPinnedStackController mPinnedStackController;
Winson73bc1592016-10-18 18:47:43 -070094
Winson Chung2a82fe52017-02-02 14:43:34 -080095 private final PipMenuActivityController mMenuController;
Winson Chung14fefc22016-11-02 10:02:29 -070096 private final PipSnapAlgorithm mSnapAlgorithm;
Phil Weaverf00cd142017-03-03 13:44:00 -080097 private final AccessibilityManager mAccessibilityManager;
Wale Ogunwale6455e502017-04-17 14:16:43 -070098 private boolean mShowPipMenuOnAnimationEnd = false;
Winson73bc1592016-10-18 18:47:43 -070099
Joshua Tsujif39539d2020-04-03 18:53:06 -0400100 /**
101 * MagnetizedObject wrapper for PIP. This allows the magnetic target library to locate and move
102 * PIP.
103 */
104 private MagnetizedObject<Rect> mMagnetizedPip;
105
106 /**
107 * Container for the dismiss circle, so that it can be animated within the container via
108 * translation rather than within the WindowManager via slow layout animations.
109 */
110 private ViewGroup mTargetViewContainer;
111
112 /** Circle view used to render the dismiss target. */
113 private DismissCircleView mTargetView;
114
115 /**
116 * MagneticTarget instance wrapping the target view and allowing us to set its magnetic radius.
117 */
118 private MagnetizedObject.MagneticTarget mMagneticTarget;
119
120 /** PhysicsAnimator instance for animating the dismiss target in/out. */
121 private PhysicsAnimator<View> mMagneticTargetAnimator;
122
123 /** Default configuration to use for springing the dismiss target in/out. */
124 private final PhysicsAnimator.SpringConfig mTargetSpringConfig =
125 new PhysicsAnimator.SpringConfig(
126 SpringForce.STIFFNESS_MEDIUM, SpringForce.DAMPING_RATIO_NO_BOUNCY);
127
Winson Chung2a82fe52017-02-02 14:43:34 -0800128 // The current movement bounds
129 private Rect mMovementBounds = new Rect();
Ben Lin094c7752020-03-11 12:10:51 -0700130 // The current resized bounds, changed by user resize.
131 // This is used during expand/un-expand to save/restore the user's resized size.
132 @VisibleForTesting Rect mResizedBounds = new Rect();
Winson Chung2a82fe52017-02-02 14:43:34 -0800133
Winson Chung0a2f34f2017-08-11 18:13:24 -0700134 // The reference inset bounds, used to determine the dismiss fraction
135 private Rect mInsetBounds = new Rect();
Ben Lin06dc41f2020-03-05 00:39:27 +0000136 // The reference bounds used to calculate the normal/expanded target bounds
137 private Rect mNormalBounds = new Rect();
Ben Lin094c7752020-03-11 12:10:51 -0700138 @VisibleForTesting Rect mNormalMovementBounds = new Rect();
Ben Lin06dc41f2020-03-05 00:39:27 +0000139 private Rect mExpandedBounds = new Rect();
Ben Lin094c7752020-03-11 12:10:51 -0700140 @VisibleForTesting Rect mExpandedMovementBounds = new Rect();
Mady Mellora7f69742017-02-03 11:00:20 -0800141 private int mExpandedShortestEdgeSize;
Winson73bc1592016-10-18 18:47:43 -0700142
Winson Chungef4dc812017-04-11 13:31:44 -0700143 // Used to workaround an issue where the WM rotation happens before we are notified, allowing
144 // us to send stale bounds
145 private int mDeferResizeToNormalBoundsUntilRotation = -1;
146 private int mDisplayRotation;
147
Joshua Tsujif39539d2020-04-03 18:53:06 -0400148 /**
149 * Runnable that can be posted delayed to show the target. This needs to be saved as a member
150 * variable so we can pass it to removeCallbacks.
151 */
152 private Runnable mShowTargetAction = this::showDismissTargetMaybe;
153
Mady Mellord4e40fb2017-01-26 10:43:16 -0800154 private Handler mHandler = new Handler();
Mady Mellord4e40fb2017-01-26 10:43:16 -0800155
Winson Chungfa7053782016-11-08 15:45:10 -0800156 // Behaviour states
Winson Chung9b919412017-06-19 17:01:51 -0700157 private int mMenuState = MENU_STATE_NONE;
Winson Chung2a82fe52017-02-02 14:43:34 -0800158 private boolean mIsImeShowing;
159 private int mImeHeight;
Winson Chung136d1ec82017-07-18 11:16:30 -0700160 private int mImeOffset;
Joshua Tsujif39539d2020-04-03 18:53:06 -0400161 private int mDismissAreaHeight;
Tracy Zhou43513082018-03-08 21:58:36 -0800162 private boolean mIsShelfShowing;
163 private int mShelfHeight;
Winson Chungdec1a662019-06-04 21:20:41 -0700164 private int mMovementBoundsExtraOffsets;
Winson Chung2a82fe52017-02-02 14:43:34 -0800165 private float mSavedSnapFraction = -1f;
Phil Weaverf00cd142017-03-03 13:44:00 -0800166 private boolean mSendingHoverAccessibilityEvents;
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700167 private boolean mMovementWithinDismiss;
Arthur Hungd8961972019-10-14 17:00:17 +0800168 private PipAccessibilityInteractionConnection mConnection;
Winson73bc1592016-10-18 18:47:43 -0700169
Winson Chungfa7053782016-11-08 15:45:10 -0800170 // Touch state
171 private final PipTouchState mTouchState;
Winson73bc1592016-10-18 18:47:43 -0700172 private final FlingAnimationUtils mFlingAnimationUtils;
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500173 private final FloatingContentCoordinator mFloatingContentCoordinator;
Ben Lin094c7752020-03-11 12:10:51 -0700174 private PipMotionHelper mMotionHelper;
Ben Lin1a80f122020-01-27 14:09:52 -0800175 private PipTouchGesture mGesture;
Winson73bc1592016-10-18 18:47:43 -0700176
Winson Chung2a82fe52017-02-02 14:43:34 -0800177 // Temp vars
Winson Chung655332c2016-10-31 13:14:28 -0700178 private final Rect mTmpBounds = new Rect();
179
Winson73bc1592016-10-18 18:47:43 -0700180 /**
Winson Chung15504af2016-11-02 18:11:36 -0700181 * A listener for the PIP menu activity.
182 */
183 private class PipMenuListener implements PipMenuActivityController.Listener {
184 @Override
Mady Mellor637cd482017-03-21 10:39:42 -0700185 public void onPipMenuStateChanged(int menuState, boolean resize) {
186 setMenuState(menuState, resize);
Winson Chung15504af2016-11-02 18:11:36 -0700187 }
Winson Chunga29eb982016-12-14 12:01:27 -0800188
189 @Override
190 public void onPipExpand() {
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800191 mMotionHelper.expandPip();
Winson Chunga29eb982016-12-14 12:01:27 -0800192 }
193
194 @Override
195 public void onPipDismiss() {
Winson Chungbe00a832020-03-10 11:08:35 -0700196 Pair<ComponentName, Integer> topPipActivity = PipUtils.getTopPipActivity(mContext,
197 mActivityManager);
198 if (topPipActivity.first != null) {
199 MetricsLoggerWrapper.logPictureInPictureDismissByTap(mContext, topPipActivity);
200 }
Winson Chung2a82fe52017-02-02 14:43:34 -0800201 mMotionHelper.dismissPip();
Winson Chunga29eb982016-12-14 12:01:27 -0800202 }
Mady Mellor637cd482017-03-21 10:39:42 -0700203
204 @Override
205 public void onPipShowMenu() {
206 mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
Winson Chungbb787442017-09-01 11:33:47 -0700207 mMovementBounds, true /* allowMenuTimeout */, willResizeMenu());
Mady Mellor637cd482017-03-21 10:39:42 -0700208 }
Winson Chung15504af2016-11-02 18:11:36 -0700209 }
210
Joshua Tsujif39539d2020-04-03 18:53:06 -0400211 @SuppressLint("InflateParams")
Winson Chungd2d90972017-02-28 11:40:41 -0800212 public PipTouchHandler(Context context, IActivityManager activityManager,
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700213 IActivityTaskManager activityTaskManager, PipMenuActivityController menuController,
Hongwei Wang43a752b2019-09-17 20:20:30 +0000214 InputConsumerController inputConsumerController,
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500215 PipBoundsHandler pipBoundsHandler,
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800216 PipTaskOrganizer pipTaskOrganizer,
Beverly660d0a72020-02-26 12:32:42 -0500217 FloatingContentCoordinator floatingContentCoordinator,
jorgegil@google.com5bdec5f2020-02-28 13:14:39 -0800218 DeviceConfigProxy deviceConfig,
219 PipSnapAlgorithm pipSnapAlgorithm) {
Winson73bc1592016-10-18 18:47:43 -0700220 // Initialize the Pip input consumer
Winson73bc1592016-10-18 18:47:43 -0700221 mContext = context;
222 mActivityManager = activityManager;
Phil Weaverf00cd142017-03-03 13:44:00 -0800223 mAccessibilityManager = context.getSystemService(AccessibilityManager.class);
Joshua Tsujif39539d2020-04-03 18:53:06 -0400224 mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Winson Chung15504af2016-11-02 18:11:36 -0700225 mMenuController = menuController;
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800226 mMenuController.addListener(new PipMenuListener());
jorgegil@google.com5bdec5f2020-02-28 13:14:39 -0800227 mSnapAlgorithm = pipSnapAlgorithm;
Dave Mankoff1373fdb2019-12-18 14:04:37 -0500228 mFlingAnimationUtils = new FlingAnimationUtils(context.getResources().getDisplayMetrics(),
229 2.5f);
Ben Lin1a80f122020-01-27 14:09:52 -0800230 mGesture = new DefaultPipTouchGesture();
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800231 mMotionHelper = new PipMotionHelper(mContext, activityTaskManager, pipTaskOrganizer,
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500232 mMenuController, mSnapAlgorithm, mFlingAnimationUtils, floatingContentCoordinator);
Ben Lin04c83f62019-12-20 10:56:45 -0800233 mPipResizeGestureHandler =
Hongwei Wang081ecb32020-04-13 16:49:03 -0700234 new PipResizeGestureHandler(context, pipBoundsHandler, mMotionHelper,
Ben Lin7d6b8e72020-02-27 17:48:16 -0800235 deviceConfig, pipTaskOrganizer);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800236 mTouchState = new PipTouchState(ViewConfiguration.get(context), mHandler,
Winson Chungbca03112017-08-16 10:38:15 -0700237 () -> mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
238 mMovementBounds, 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(),
Winson Chungbb787442017-09-01 11:33:47 -0700325 mMovementBounds, 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(),
Winson Chungbb787442017-09-01 11:33:47 -0700361 mMovementBounds, 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(),
Rhed Jao5e00c982018-11-27 19:41:05 +0800560 mMovementBounds, 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
Joshua Tsujif39539d2020-04-03 18:53:06 -0400575 if (mMagnetizedPip.maybeConsumeMotionEvent(ev)) {
576 // If the first touch event occurs within the magnetic field, pass the ACTION_DOWN event
577 // to the touch state. Touch state needs a DOWN event in order to later process MOVE
578 // events it'll receive if the object is dragged out of the magnetic field.
579 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
580 mTouchState.onTouchEvent(ev);
581 }
582
583 // Continue tracking velocity when the object is in the magnetic field, since we want to
584 // respect touch input velocity if the object is dragged out and then flung.
585 mTouchState.addMovementToVelocityTracker(ev);
586
587 return true;
588 }
589
Winson Chungfa7053782016-11-08 15:45:10 -0800590 // Update the touch state
591 mTouchState.onTouchEvent(ev);
592
Arthur Hungd52d1192019-08-30 16:06:59 +0800593 boolean shouldDeliverToMenu = mMenuState != MENU_STATE_NONE;
594
Winson73bc1592016-10-18 18:47:43 -0700595 switch (ev.getAction()) {
596 case MotionEvent.ACTION_DOWN: {
Joshua Tsuji601dc852020-04-02 15:54:15 -0400597 mMotionHelper.synchronizePinnedStackBoundsForTouchGesture();
Ben Lin1a80f122020-01-27 14:09:52 -0800598 mGesture.onDown(mTouchState);
Winson73bc1592016-10-18 18:47:43 -0700599 break;
600 }
601 case MotionEvent.ACTION_MOVE: {
Ben Lin1a80f122020-01-27 14:09:52 -0800602 if (mGesture.onMove(mTouchState)) {
603 break;
Winson73bc1592016-10-18 18:47:43 -0700604 }
Arthur Hungd52d1192019-08-30 16:06:59 +0800605
606 shouldDeliverToMenu = !mTouchState.isDragging();
Winson73bc1592016-10-18 18:47:43 -0700607 break;
608 }
609 case MotionEvent.ACTION_UP: {
Winson Chung14fefc22016-11-02 10:02:29 -0700610 // Update the movement bounds again if the state has changed since the user started
611 // dragging (ie. when the IME shows)
Ben Lin094c7752020-03-11 12:10:51 -0700612 updateMovementBounds();
Winson Chung14fefc22016-11-02 10:02:29 -0700613
Ben Lin1a80f122020-01-27 14:09:52 -0800614 if (mGesture.onUp(mTouchState)) {
615 break;
Winson Chung5cd26ff2016-10-24 11:50:44 -0700616 }
Winson73bc1592016-10-18 18:47:43 -0700617
618 // Fall through to clean up
619 }
620 case MotionEvent.ACTION_CANCEL: {
Arthur Hungd52d1192019-08-30 16:06:59 +0800621 shouldDeliverToMenu = !mTouchState.startedDragging() && !mTouchState.isDragging();
Winson Chung85d39982017-02-24 15:21:25 -0800622 mTouchState.reset();
Winson73bc1592016-10-18 18:47:43 -0700623 break;
624 }
Phil Weaverf00cd142017-03-03 13:44:00 -0800625 case MotionEvent.ACTION_HOVER_ENTER:
Ben Lin388df602020-04-08 14:07:06 -0700626 // If Touch Exploration is enabled, some a11y services (e.g. Talkback) is probably
627 // on and changing MotionEvents into HoverEvents.
628 // Let's not enable menu show/hide for a11y services.
629 if (!mAccessibilityManager.isTouchExplorationEnabled()) {
630 mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
631 mMovementBounds, false /* allowMenuTimeout */,
632 false /* willResizeMenu */);
633 }
Phil Weaverf00cd142017-03-03 13:44:00 -0800634 case MotionEvent.ACTION_HOVER_MOVE: {
Arthur Hungd8961972019-10-14 17:00:17 +0800635 if (!shouldDeliverToMenu && !mSendingHoverAccessibilityEvents) {
636 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Phil Weaverf00cd142017-03-03 13:44:00 -0800637 mSendingHoverAccessibilityEvents = true;
638 }
639 break;
640 }
641 case MotionEvent.ACTION_HOVER_EXIT: {
Ben Lin388df602020-04-08 14:07:06 -0700642 // If Touch Exploration is enabled, some a11y services (e.g. Talkback) is probably
643 // on and changing MotionEvents into HoverEvents.
644 // Let's not enable menu show/hide for a11y services.
645 if (!mAccessibilityManager.isTouchExplorationEnabled()) {
646 mMenuController.hideMenu();
647 }
Arthur Hungd8961972019-10-14 17:00:17 +0800648 if (!shouldDeliverToMenu && mSendingHoverAccessibilityEvents) {
649 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Phil Weaverf00cd142017-03-03 13:44:00 -0800650 mSendingHoverAccessibilityEvents = false;
651 }
652 break;
653 }
Winson73bc1592016-10-18 18:47:43 -0700654 }
Arthur Hungd52d1192019-08-30 16:06:59 +0800655
656 // Deliver the event to PipMenuActivity to handle button click if the menu has shown.
657 if (shouldDeliverToMenu) {
658 final MotionEvent cloneEvent = MotionEvent.obtain(ev);
659 // Send the cancel event and cancel menu timeout if it starts to drag.
660 if (mTouchState.startedDragging()) {
661 cloneEvent.setAction(MotionEvent.ACTION_CANCEL);
662 mMenuController.pokeMenu();
663 }
664
Arthur Hungd8961972019-10-14 17:00:17 +0800665 mMenuController.handlePointerEvent(cloneEvent);
Arthur Hungd52d1192019-08-30 16:06:59 +0800666 }
667
668 return true;
Winson Chung15504af2016-11-02 18:11:36 -0700669 }
670
Arthur Hungd8961972019-10-14 17:00:17 +0800671 private void sendAccessibilityHoverEvent(int type) {
672 if (!mAccessibilityManager.isEnabled()) {
673 return;
674 }
675
676 AccessibilityEvent event = AccessibilityEvent.obtain(type);
677 event.setImportantForAccessibility(true);
678 event.setSourceNodeId(AccessibilityNodeInfo.ROOT_NODE_ID);
679 event.setWindowId(
680 AccessibilityWindowInfo.PICTURE_IN_PICTURE_ACTION_REPLACER_WINDOW_ID);
681 mAccessibilityManager.sendAccessibilityEvent(event);
682 }
683
Winson Chung15504af2016-11-02 18:11:36 -0700684 /**
Mady Mellor81d40612017-03-10 15:14:10 -0800685 * Updates the appearance of the menu and scrim on top of the PiP while dismissing.
686 */
Winson Chung8ec0e162017-07-07 14:49:49 -0700687 private void updateDismissFraction() {
Winson Chung0a2f34f2017-08-11 18:13:24 -0700688 // Skip updating the dismiss fraction when the IME is showing. This is to work around an
689 // issue where starting the menu activity for the dismiss overlay will steal the window
690 // focus, which closes the IME.
691 if (mMenuController != null && !mIsImeShowing) {
Mady Mellor81d40612017-03-10 15:14:10 -0800692 Rect bounds = mMotionHelper.getBounds();
Winson Chung0a2f34f2017-08-11 18:13:24 -0700693 final float target = mInsetBounds.bottom;
Mady Mellor81d40612017-03-10 15:14:10 -0800694 float fraction = 0f;
695 if (bounds.bottom > target) {
696 final float distance = bounds.bottom - target;
697 fraction = Math.min(distance / bounds.height(), 1f);
698 }
Winson Chungbca03112017-08-16 10:38:15 -0700699 if (Float.compare(fraction, 0f) != 0 || mMenuController.isMenuActivityVisible()) {
Winson Chung87e5d552017-04-05 11:49:38 -0700700 // Update if the fraction > 0, or if fraction == 0 and the menu was already visible
701 mMenuController.setDismissFraction(fraction);
702 }
Mady Mellor81d40612017-03-10 15:14:10 -0800703 }
704 }
705
706 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800707 * Sets the controller to update the system of changes from user interaction.
708 */
709 void setPinnedStackController(IPinnedStackController controller) {
710 mPinnedStackController = controller;
711 }
712
713 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800714 * Sets the menu visibility.
Winson Chungfa7053782016-11-08 15:45:10 -0800715 */
Winson Chung8ec0e162017-07-07 14:49:49 -0700716 private void setMenuState(int menuState, boolean resize) {
Arthur Hungd8961972019-10-14 17:00:17 +0800717 if (mMenuState == menuState && !resize) {
718 return;
719 }
720
Winson Chunga89053d2018-06-12 16:47:30 -0700721 if (menuState == MENU_STATE_FULL && mMenuState != MENU_STATE_FULL) {
Winson Chungd2d90972017-02-28 11:40:41 -0800722 // Save the current snap fraction and if we do not drag or move the PiP, then
723 // we store back to this snap fraction. Otherwise, we'll reset the snap
Ben Lin094c7752020-03-11 12:10:51 -0700724 // fraction and snap to the closest edge.
725 // Also save the current resized bounds so when the menu disappears, we can restore it.
Winson Chungd2d90972017-02-28 11:40:41 -0800726 if (resize) {
Ben Lin094c7752020-03-11 12:10:51 -0700727 mResizedBounds.set(mMotionHelper.getBounds());
728 Rect expandedBounds = new Rect(mExpandedBounds);
Winson Chung2a82fe52017-02-02 14:43:34 -0800729 mSavedSnapFraction = mMotionHelper.animateToExpandedState(expandedBounds,
Ben Lin06dc41f2020-03-05 00:39:27 +0000730 mMovementBounds, mExpandedMovementBounds);
Winson Chungd2d90972017-02-28 11:40:41 -0800731 }
Winson Chunga89053d2018-06-12 16:47:30 -0700732 } else if (menuState == MENU_STATE_NONE && mMenuState == MENU_STATE_FULL) {
Winson Chungd2d90972017-02-28 11:40:41 -0800733 // Try and restore the PiP to the closest edge, using the saved snap fraction
734 // if possible
735 if (resize) {
Winson Chungef4dc812017-04-11 13:31:44 -0700736 if (mDeferResizeToNormalBoundsUntilRotation == -1) {
Winson Chungbb233762017-05-15 14:20:46 -0700737 // This is a very special case: when the menu is expanded and visible,
738 // navigating to another activity can trigger auto-enter PiP, and if the
739 // revealed activity has a forced rotation set, then the controller will get
740 // updated with the new rotation of the display. However, at the same time,
741 // SystemUI will try to hide the menu by creating an animation to the normal
742 // bounds which are now stale. In such a case we defer the animation to the
743 // normal bounds until after the next onMovementBoundsChanged() call to get the
744 // bounds in the new orientation
Winson Chungef4dc812017-04-11 13:31:44 -0700745 try {
746 int displayRotation = mPinnedStackController.getDisplayRotation();
747 if (mDisplayRotation != displayRotation) {
748 mDeferResizeToNormalBoundsUntilRotation = displayRotation;
749 }
750 } catch (RemoteException e) {
751 Log.e(TAG, "Could not get display rotation from controller");
752 }
753 }
754
755 if (mDeferResizeToNormalBoundsUntilRotation == -1) {
Ben Lin094c7752020-03-11 12:10:51 -0700756 Rect restoreBounds = new Rect(mResizedBounds);
757 Rect restoredMovementBounds = new Rect();
758 mSnapAlgorithm.getMovementBounds(restoreBounds, mInsetBounds,
759 restoredMovementBounds, mIsImeShowing ? mImeHeight : 0);
760 mMotionHelper.animateToUnexpandedState(restoreBounds, mSavedSnapFraction,
761 restoredMovementBounds, mMovementBounds, false /* immediate */);
Winson Chungef4dc812017-04-11 13:31:44 -0700762 mSavedSnapFraction = -1f;
763 }
764 } else {
765 mSavedSnapFraction = -1f;
Winson Chunga29eb982016-12-14 12:01:27 -0800766 }
Winson Chungfa7053782016-11-08 15:45:10 -0800767 }
Mady Mellor637cd482017-03-21 10:39:42 -0700768 mMenuState = menuState;
Ben Lin094c7752020-03-11 12:10:51 -0700769 updateMovementBounds();
Arthur Hungd8961972019-10-14 17:00:17 +0800770 // If pip menu has dismissed, we should register the A11y ActionReplacingConnection for pip
771 // as well, or it can't handle a11y focus and pip menu can't perform any action.
772 onRegistrationChanged(menuState == MENU_STATE_NONE);
Mady Mellor637cd482017-03-21 10:39:42 -0700773 if (menuState != MENU_STATE_CLOSE) {
Chenjie Yu52cacc62017-12-08 18:11:45 -0800774 MetricsLoggerWrapper.logPictureInPictureMenuVisible(mContext, menuState == MENU_STATE_FULL);
Mady Mellor637cd482017-03-21 10:39:42 -0700775 }
Winson Chungfa7053782016-11-08 15:45:10 -0800776 }
777
778 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800779 * @return the motion helper.
Winson Chungfa7053782016-11-08 15:45:10 -0800780 */
Winson Chung2a82fe52017-02-02 14:43:34 -0800781 public PipMotionHelper getMotionHelper() {
782 return mMotionHelper;
Winson73bc1592016-10-18 18:47:43 -0700783 }
Winson Chungfa7053782016-11-08 15:45:10 -0800784
Ben Lin094c7752020-03-11 12:10:51 -0700785 @VisibleForTesting
786 PipResizeGestureHandler getPipResizeGestureHandler() {
787 return mPipResizeGestureHandler;
788 }
789
790 @VisibleForTesting
791 void setPipResizeGestureHandler(PipResizeGestureHandler pipResizeGestureHandler) {
792 mPipResizeGestureHandler = pipResizeGestureHandler;
793 }
794
795 @VisibleForTesting
796 void setPipMotionHelper(PipMotionHelper pipMotionHelper) {
797 mMotionHelper = pipMotionHelper;
798 }
799
Winson Chungfa7053782016-11-08 15:45:10 -0800800 /**
jorgegil@google.com62f735e2019-10-30 14:37:19 -0700801 * @return the unexpanded bounds.
802 */
Ben Lin06dc41f2020-03-05 00:39:27 +0000803 public Rect getNormalBounds() {
804 return mNormalBounds;
jorgegil@google.com62f735e2019-10-30 14:37:19 -0700805 }
806
807 /**
Winson Chungfa7053782016-11-08 15:45:10 -0800808 * Gesture controlling normal movement of the PIP.
809 */
Ben Lin1a80f122020-01-27 14:09:52 -0800810 private class DefaultPipTouchGesture extends PipTouchGesture {
Winson Chung8ec0e162017-07-07 14:49:49 -0700811 private final Point mStartPosition = new Point();
812 private final PointF mDelta = new PointF();
Mady Mellord4e40fb2017-01-26 10:43:16 -0800813
814 @Override
815 public void onDown(PipTouchState touchState) {
Winson Chung85d39982017-02-24 15:21:25 -0800816 if (!touchState.isUserInteracting()) {
817 return;
818 }
819
Mady Mellor15b29c72017-06-07 14:53:58 -0700820 Rect bounds = mMotionHelper.getBounds();
Winson Chung8ec0e162017-07-07 14:49:49 -0700821 mDelta.set(0f, 0f);
822 mStartPosition.set(bounds.left, bounds.top);
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700823 mMovementWithinDismiss = touchState.getDownTouchPosition().y >= mMovementBounds.bottom;
Joshua Tsujif39539d2020-04-03 18:53:06 -0400824 mMotionHelper.setSpringingToTouch(false);
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700825
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800826 // If the menu is still visible then just poke the menu
Mady Mellora7f69742017-02-03 11:00:20 -0800827 // so that it will timeout after the user stops touching it
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800828 if (mMenuState != MENU_STATE_NONE) {
Mady Mellora7f69742017-02-03 11:00:20 -0800829 mMenuController.pokeMenu();
830 }
Mady Mellord4e40fb2017-01-26 10:43:16 -0800831 }
832
Winson Chungfa7053782016-11-08 15:45:10 -0800833 @Override
Ben Lin1a80f122020-01-27 14:09:52 -0800834 public boolean onMove(PipTouchState touchState) {
Winson Chung85d39982017-02-24 15:21:25 -0800835 if (!touchState.isUserInteracting()) {
836 return false;
837 }
838
Winson Chung2a82fe52017-02-02 14:43:34 -0800839 if (touchState.startedDragging()) {
840 mSavedSnapFraction = -1f;
Winson Chung2a82fe52017-02-02 14:43:34 -0800841
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800842 if (mEnableDismissDragToEdge) {
Joshua Tsujif39539d2020-04-03 18:53:06 -0400843 if (mTargetViewContainer.getVisibility() != View.VISIBLE) {
844 mHandler.removeCallbacks(mShowTargetAction);
845 showDismissTargetMaybe();
846 }
Winson Chungb54b65b2017-04-26 14:02:13 -0700847 }
Mady Mellord4e40fb2017-01-26 10:43:16 -0800848 }
849
Winson Chungfa7053782016-11-08 15:45:10 -0800850 if (touchState.isDragging()) {
851 // Move the pinned stack freely
Winson Chung2a82fe52017-02-02 14:43:34 -0800852 final PointF lastDelta = touchState.getLastTouchDelta();
Winson Chung8ec0e162017-07-07 14:49:49 -0700853 float lastX = mStartPosition.x + mDelta.x;
854 float lastY = mStartPosition.y + mDelta.y;
855 float left = lastX + lastDelta.x;
856 float top = lastY + lastDelta.y;
Winson Chung8ec0e162017-07-07 14:49:49 -0700857
858 // Add to the cumulative delta after bounding the position
859 mDelta.x += left - lastX;
860 mDelta.y += top - lastY;
861
862 mTmpBounds.set(mMotionHelper.getBounds());
Winson Chungfa7053782016-11-08 15:45:10 -0800863 mTmpBounds.offsetTo((int) left, (int) top);
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500864 mMotionHelper.movePip(mTmpBounds, true /* isDragging */);
Winson Chung2a82fe52017-02-02 14:43:34 -0800865
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700866 final PointF curPos = touchState.getLastTouchPosition();
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700867 if (mMovementWithinDismiss) {
868 // Track if movement remains near the bottom edge to identify swipe to dismiss
869 mMovementWithinDismiss = curPos.y >= mMovementBounds.bottom;
870 }
Winson Chungfa7053782016-11-08 15:45:10 -0800871 return true;
872 }
873 return false;
874 }
875
876 @Override
877 public boolean onUp(PipTouchState touchState) {
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800878 if (mEnableDismissDragToEdge) {
Joshua Tsujif39539d2020-04-03 18:53:06 -0400879 hideDismissTarget();
Winson Chungb54b65b2017-04-26 14:02:13 -0700880 }
881
Winson Chung85d39982017-02-24 15:21:25 -0800882 if (!touchState.isUserInteracting()) {
883 return false;
884 }
885
Mady Mellor60421c92017-03-29 15:27:37 -0700886 final PointF vel = touchState.getVelocity();
Mady Mellor60421c92017-03-29 15:27:37 -0700887 final float velocity = PointF.length(vel.x, vel.y);
888 final boolean isFling = velocity > mFlingAnimationUtils.getMinVelocityPxPerSecond();
Winson Chungd2d90972017-02-28 11:40:41 -0800889
Winson Chungfa7053782016-11-08 15:45:10 -0800890 if (touchState.isDragging()) {
Joshua Tsuji2e252462020-01-02 11:27:41 -0500891 Runnable endAction = null;
Mady Mellor637cd482017-03-21 10:39:42 -0700892 if (mMenuState != MENU_STATE_NONE) {
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800893 // If the menu is still visible, then just poke the menu so that
894 // it will timeout after the user stops touching it
Mady Mellor637cd482017-03-21 10:39:42 -0700895 mMenuController.showMenu(mMenuState, mMotionHelper.getBounds(),
Winson Chungbb787442017-09-01 11:33:47 -0700896 mMovementBounds, true /* allowMenuTimeout */, willResizeMenu());
Winson Chung87e5d552017-04-05 11:49:38 -0700897 } else {
898 // If the menu is not visible, then we can still be showing the activity for the
899 // dismiss overlay, so just finish it after the animation completes
Joshua Tsuji2e252462020-01-02 11:27:41 -0500900 endAction = mMenuController::hideMenu;
Winson Chungd2d90972017-02-28 11:40:41 -0800901 }
902
Mady Mellor2fbdd3b2017-03-21 17:45:00 -0700903 if (isFling) {
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500904 mMotionHelper.flingToSnapTarget(vel.x, vel.y,
Joshua Tsuji2e252462020-01-02 11:27:41 -0500905 PipTouchHandler.this::updateDismissFraction /* updateAction */,
906 endAction /* endAction */);
Winson Chungfa7053782016-11-08 15:45:10 -0800907 } else {
Joshua Tsujic81ff3d2020-02-13 14:48:40 -0500908 mMotionHelper.animateToClosestSnapTarget();
Winson Chungfa7053782016-11-08 15:45:10 -0800909 }
Arthur Hungd52d1192019-08-30 16:06:59 +0800910 } else if (mTouchState.isDoubleTap()) {
911 // Expand to fullscreen if this is a double tap
Ben Lin34b6fbf2020-03-13 19:36:02 -0700912 // the PiP should be frozen until the transition ends
913 setTouchEnabled(false);
Arthur Hungd52d1192019-08-30 16:06:59 +0800914 mMotionHelper.expandPip();
Mady Mellor637cd482017-03-21 10:39:42 -0700915 } else if (mMenuState != MENU_STATE_FULL) {
Arthur Hungd52d1192019-08-30 16:06:59 +0800916 if (!mTouchState.isWaitingForDoubleTap()) {
Winson Chungbca03112017-08-16 10:38:15 -0700917 // User has stalled long enough for this not to be a drag or a double tap, just
918 // expand the menu
919 mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
920 mMovementBounds, true /* allowMenuTimeout */, willResizeMenu());
921 } else {
922 // Next touch event _may_ be the second tap for the double-tap, schedule a
923 // fallback runnable to trigger the menu if no touch event occurs before the
924 // next tap
925 mTouchState.scheduleDoubleTapTimeoutCallback();
926 }
Winson Chungfa7053782016-11-08 15:45:10 -0800927 }
928 return true;
929 }
930 };
Mady Mellor3b10dcd2017-01-23 10:08:35 -0800931
932 /**
Ben Lin06dc41f2020-03-05 00:39:27 +0000933 * Updates the current movement bounds based on whether the menu is currently visible and
934 * resized.
Winson Chung2a82fe52017-02-02 14:43:34 -0800935 */
Ben Lin094c7752020-03-11 12:10:51 -0700936 private void updateMovementBounds() {
937 mSnapAlgorithm.getMovementBounds(mMotionHelper.getBounds(), mInsetBounds,
938 mMovementBounds, mIsImeShowing ? mImeHeight : 0);
Ben Lin06dc41f2020-03-05 00:39:27 +0000939 mMotionHelper.setCurrentMovementBounds(mMovementBounds);
Ben Lin094c7752020-03-11 12:10:51 -0700940
941 boolean isMenuExpanded = mMenuState == MENU_STATE_FULL;
942 mPipBoundsHandler.setMinEdgeSize(
943 isMenuExpanded && willResizeMenu() ? mExpandedShortestEdgeSize : 0);
Winson Chung2a82fe52017-02-02 14:43:34 -0800944 }
Winson Chung29a78652017-02-09 18:35:26 -0800945
Winson Chungb54b65b2017-04-26 14:02:13 -0700946 /**
Winson Chungbb787442017-09-01 11:33:47 -0700947 * @return whether the menu will resize as a part of showing the full menu.
948 */
949 private boolean willResizeMenu() {
Ben Line55f85f2020-01-27 14:49:41 -0800950 if (!mEnableResize) {
951 return false;
952 }
Ben Lin06dc41f2020-03-05 00:39:27 +0000953 return mExpandedBounds.width() != mNormalBounds.width()
954 || mExpandedBounds.height() != mNormalBounds.height();
Winson Chungbb787442017-09-01 11:33:47 -0700955 }
956
Winson Chung29a78652017-02-09 18:35:26 -0800957 public void dump(PrintWriter pw, String prefix) {
958 final String innerPrefix = prefix + " ";
959 pw.println(prefix + TAG);
960 pw.println(innerPrefix + "mMovementBounds=" + mMovementBounds);
Ben Lin06dc41f2020-03-05 00:39:27 +0000961 pw.println(innerPrefix + "mNormalBounds=" + mNormalBounds);
962 pw.println(innerPrefix + "mNormalMovementBounds=" + mNormalMovementBounds);
963 pw.println(innerPrefix + "mExpandedBounds=" + mExpandedBounds);
964 pw.println(innerPrefix + "mExpandedMovementBounds=" + mExpandedMovementBounds);
Mady Mellor637cd482017-03-21 10:39:42 -0700965 pw.println(innerPrefix + "mMenuState=" + mMenuState);
Winson Chung29a78652017-02-09 18:35:26 -0800966 pw.println(innerPrefix + "mIsImeShowing=" + mIsImeShowing);
967 pw.println(innerPrefix + "mImeHeight=" + mImeHeight);
Tracy Zhou43513082018-03-08 21:58:36 -0800968 pw.println(innerPrefix + "mIsShelfShowing=" + mIsShelfShowing);
969 pw.println(innerPrefix + "mShelfHeight=" + mShelfHeight);
Winson Chung29a78652017-02-09 18:35:26 -0800970 pw.println(innerPrefix + "mSavedSnapFraction=" + mSavedSnapFraction);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800971 pw.println(innerPrefix + "mEnableDragToEdgeDismiss=" + mEnableDismissDragToEdge);
Winson Chung29a78652017-02-09 18:35:26 -0800972 mSnapAlgorithm.dump(pw, innerPrefix);
973 mTouchState.dump(pw, innerPrefix);
974 mMotionHelper.dump(pw, innerPrefix);
975 }
Phil Weaverf00cd142017-03-03 13:44:00 -0800976
Winson73bc1592016-10-18 18:47:43 -0700977}