Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.recents.views; |
| 18 | |
Winson | c5fd350 | 2016-01-18 15:18:37 -0800 | [diff] [blame] | 19 | import android.animation.Animator; |
Winson | 36a5a2c | 2015-10-29 18:04:39 -0700 | [diff] [blame] | 20 | import android.animation.ValueAnimator; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
Winson | a5e6b36 | 2015-11-02 17:17:20 -0800 | [diff] [blame] | 23 | import android.graphics.Rect; |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 24 | import android.util.ArrayMap; |
| 25 | import android.util.MutableBoolean; |
Winson Chung | d213a1e | 2014-10-02 11:18:30 -0700 | [diff] [blame] | 26 | import android.view.InputDevice; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 27 | import android.view.MotionEvent; |
| 28 | import android.view.VelocityTracker; |
| 29 | import android.view.View; |
| 30 | import android.view.ViewConfiguration; |
| 31 | import android.view.ViewParent; |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 32 | import android.view.animation.Interpolator; |
| 33 | import android.view.animation.PathInterpolator; |
Winson Chung | 5c9f4b9 | 2015-06-25 16:16:46 -0700 | [diff] [blame] | 34 | import com.android.internal.logging.MetricsLogger; |
Winson | 2536c7e | 2015-10-01 15:49:31 -0700 | [diff] [blame] | 35 | import com.android.systemui.R; |
Winson | 671e8f9 | 2016-01-12 13:16:56 -0800 | [diff] [blame] | 36 | import com.android.systemui.SwipeHelper; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 37 | import com.android.systemui.recents.Constants; |
Winson | a5e6b36 | 2015-11-02 17:17:20 -0800 | [diff] [blame] | 38 | import com.android.systemui.recents.Recents; |
Winson | 2536c7e | 2015-10-01 15:49:31 -0700 | [diff] [blame] | 39 | import com.android.systemui.recents.events.EventBus; |
Winson | 412e180 | 2015-10-20 16:57:57 -0700 | [diff] [blame] | 40 | import com.android.systemui.recents.events.activity.HideRecentsEvent; |
Winson | 8b1871d | 2015-11-20 09:56:20 -0800 | [diff] [blame] | 41 | import com.android.systemui.recents.events.ui.StackViewScrolledEvent; |
Winson | dc8de84 | 2016-01-06 15:21:41 -0800 | [diff] [blame] | 42 | import com.android.systemui.recents.events.ui.TaskViewDismissedEvent; |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 43 | import com.android.systemui.recents.misc.RectFEvaluator; |
Winson | a5e6b36 | 2015-11-02 17:17:20 -0800 | [diff] [blame] | 44 | import com.android.systemui.recents.misc.SystemServicesProxy; |
Winson | 36a5a2c | 2015-10-29 18:04:39 -0700 | [diff] [blame] | 45 | import com.android.systemui.recents.misc.Utilities; |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 46 | import com.android.systemui.recents.model.Task; |
Winson | 36a5a2c | 2015-10-29 18:04:39 -0700 | [diff] [blame] | 47 | import com.android.systemui.statusbar.FlingAnimationUtils; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 48 | |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 49 | import java.util.ArrayList; |
Winson Chung | 6ac8bd6 | 2015-01-07 16:38:35 -0800 | [diff] [blame] | 50 | import java.util.List; |
| 51 | |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 52 | /** |
| 53 | * Handles touch events for a TaskStackView. |
| 54 | */ |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 55 | class TaskStackViewTouchHandler implements SwipeHelper.Callback { |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 56 | |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 57 | private static final int INACTIVE_POINTER_ID = -1; |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 58 | |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 59 | private static final RectFEvaluator RECT_EVALUATOR = new RectFEvaluator(); |
| 60 | private static final Interpolator STACK_TRANSFORM_INTERPOLATOR = |
| 61 | new PathInterpolator(0.73f, 0.33f, 0.42f, 0.85f); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 62 | |
Winson | 35f3050 | 2015-09-28 11:24:36 -0700 | [diff] [blame] | 63 | Context mContext; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 64 | TaskStackView mSv; |
Winson Chung | 012ef36 | 2014-07-31 18:36:25 -0700 | [diff] [blame] | 65 | TaskStackViewScroller mScroller; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 66 | VelocityTracker mVelocityTracker; |
Winson | 36a5a2c | 2015-10-29 18:04:39 -0700 | [diff] [blame] | 67 | FlingAnimationUtils mFlingAnimUtils; |
| 68 | ValueAnimator mScrollFlingAnimator; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 69 | |
| 70 | boolean mIsScrolling; |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 71 | float mDownScrollP; |
| 72 | int mDownX, mDownY; |
Winson | 8b1871d | 2015-11-20 09:56:20 -0800 | [diff] [blame] | 73 | int mLastY; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 74 | int mActivePointerId = INACTIVE_POINTER_ID; |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 75 | int mOverscrollSize; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 76 | TaskView mActiveTaskView = null; |
| 77 | |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 78 | int mMinimumVelocity; |
| 79 | int mMaximumVelocity; |
| 80 | // The scroll touch slop is used to calculate when we start scrolling |
| 81 | int mScrollTouchSlop; |
James Cook | 4bd79b7 | 2015-03-17 07:33:28 -0700 | [diff] [blame] | 82 | // Used to calculate when a tap is outside a task view rectangle. |
| 83 | final int mWindowTouchSlop; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 84 | |
Winson | 5500390 | 2016-01-12 12:00:37 -0800 | [diff] [blame] | 85 | private final StackViewScrolledEvent mStackViewScrolledEvent = new StackViewScrolledEvent(); |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 86 | |
| 87 | // The current and final set of task transforms, sized to match the list of tasks in the stack |
| 88 | private ArrayList<Task> mCurrentTasks = new ArrayList<>(); |
| 89 | private ArrayList<TaskViewTransform> mCurrentTaskTransforms = new ArrayList<>(); |
| 90 | private ArrayList<TaskViewTransform> mFinalTaskTransforms = new ArrayList<>(); |
| 91 | private ArrayMap<View, Animator> mSwipeHelperAnimations = new ArrayMap<>(); |
| 92 | private TaskViewTransform mTmpTransform = new TaskViewTransform(); |
| 93 | private float mTargetStackScroll; |
| 94 | |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 95 | SwipeHelper mSwipeHelper; |
| 96 | boolean mInterceptedBySwipeHelper; |
| 97 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 98 | public TaskStackViewTouchHandler(Context context, TaskStackView sv, |
Winson | 35f3050 | 2015-09-28 11:24:36 -0700 | [diff] [blame] | 99 | TaskStackViewScroller scroller) { |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 100 | Resources res = context.getResources(); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 101 | ViewConfiguration configuration = ViewConfiguration.get(context); |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 102 | mContext = context; |
Winson | 671e8f9 | 2016-01-12 13:16:56 -0800 | [diff] [blame] | 103 | mSv = sv; |
| 104 | mScroller = scroller; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 105 | mMinimumVelocity = configuration.getScaledMinimumFlingVelocity(); |
| 106 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
| 107 | mScrollTouchSlop = configuration.getScaledTouchSlop(); |
James Cook | 4bd79b7 | 2015-03-17 07:33:28 -0700 | [diff] [blame] | 108 | mWindowTouchSlop = configuration.getScaledWindowTouchSlop(); |
Winson | 36a5a2c | 2015-10-29 18:04:39 -0700 | [diff] [blame] | 109 | mFlingAnimUtils = new FlingAnimationUtils(context, 0.2f); |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 110 | mOverscrollSize = res.getDimensionPixelSize(R.dimen.recents_stack_overscroll); |
Winson | 671e8f9 | 2016-01-12 13:16:56 -0800 | [diff] [blame] | 111 | mSwipeHelper = new SwipeHelper(SwipeHelper.X, this, context) { |
| 112 | @Override |
| 113 | protected float getSize(View v) { |
| 114 | return mSv.getWidth(); |
| 115 | } |
Winson | c5fd350 | 2016-01-18 15:18:37 -0800 | [diff] [blame] | 116 | |
| 117 | @Override |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 118 | protected void prepareDismissAnimation(View v, Animator anim) { |
| 119 | mSwipeHelperAnimations.put(v, anim); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | protected void prepareSnapBackAnimation(View v, Animator anim) { |
Winson | c5fd350 | 2016-01-18 15:18:37 -0800 | [diff] [blame] | 124 | anim.setInterpolator(mSv.mFastOutSlowInInterpolator); |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 125 | mSwipeHelperAnimations.put(v, anim); |
Winson | c5fd350 | 2016-01-18 15:18:37 -0800 | [diff] [blame] | 126 | } |
Winson | 671e8f9 | 2016-01-12 13:16:56 -0800 | [diff] [blame] | 127 | }; |
| 128 | mSwipeHelper.setDisableHardwareLayers(true); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** Velocity tracker helpers */ |
| 132 | void initOrResetVelocityTracker() { |
| 133 | if (mVelocityTracker == null) { |
| 134 | mVelocityTracker = VelocityTracker.obtain(); |
| 135 | } else { |
| 136 | mVelocityTracker.clear(); |
| 137 | } |
| 138 | } |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 139 | void recycleVelocityTracker() { |
| 140 | if (mVelocityTracker != null) { |
| 141 | mVelocityTracker.recycle(); |
| 142 | mVelocityTracker = null; |
| 143 | } |
| 144 | } |
| 145 | |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 146 | /** Touch preprocessing for handling below */ |
| 147 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 148 | // Pass through to swipe helper if we are swiping |
| 149 | mInterceptedBySwipeHelper = mSwipeHelper.onInterceptTouchEvent(ev); |
| 150 | if (mInterceptedBySwipeHelper) { |
| 151 | return true; |
| 152 | } |
| 153 | |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 154 | return handleTouchEvent(ev); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** Handles touch events once we have intercepted them */ |
| 158 | public boolean onTouchEvent(MotionEvent ev) { |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 159 | // Pass through to swipe helper if we are swiping |
| 160 | if (mInterceptedBySwipeHelper && mSwipeHelper.onTouchEvent(ev)) { |
| 161 | return true; |
| 162 | } |
| 163 | |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 164 | handleTouchEvent(ev); |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | private boolean handleTouchEvent(MotionEvent ev) { |
| 169 | // Short circuit if we have no children |
| 170 | if (mSv.getTaskViews().size() == 0) { |
| 171 | return false; |
| 172 | } |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 173 | |
Winson | 36a5a2c | 2015-10-29 18:04:39 -0700 | [diff] [blame] | 174 | final TaskStackLayoutAlgorithm layoutAlgorithm = mSv.mLayoutAlgorithm; |
Winson | 147ecaf | 2015-09-16 16:49:55 -0700 | [diff] [blame] | 175 | int action = ev.getAction(); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 176 | switch (action & MotionEvent.ACTION_MASK) { |
| 177 | case MotionEvent.ACTION_DOWN: { |
| 178 | // Save the touch down info |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 179 | mDownX = (int) ev.getX(); |
| 180 | mDownY = (int) ev.getY(); |
Winson | 8b1871d | 2015-11-20 09:56:20 -0800 | [diff] [blame] | 181 | mLastY = mDownY; |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 182 | mDownScrollP = mScroller.getStackScroll(); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 183 | mActivePointerId = ev.getPointerId(0); |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 184 | mActiveTaskView = findViewAtPoint(mDownX, mDownY); |
| 185 | |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 186 | // Stop the current scroll if it is still flinging |
Winson Chung | 012ef36 | 2014-07-31 18:36:25 -0700 | [diff] [blame] | 187 | mScroller.stopScroller(); |
| 188 | mScroller.stopBoundScrollAnimation(); |
Winson | 36a5a2c | 2015-10-29 18:04:39 -0700 | [diff] [blame] | 189 | Utilities.cancelAnimationWithoutCallbacks(mScrollFlingAnimator); |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 190 | |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 191 | // Finish any existing task animations from the delete |
| 192 | mSv.cancelAllTaskViewAnimations(); |
| 193 | // Finish any of the swipe helper animations |
| 194 | ArrayMap<View, Animator> existingAnimators = new ArrayMap<>(mSwipeHelperAnimations); |
| 195 | for (int i = 0; i < existingAnimators.size(); i++) { |
| 196 | existingAnimators.get(existingAnimators.keyAt(i)).end(); |
| 197 | } |
| 198 | mSwipeHelperAnimations.clear(); |
| 199 | |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 200 | // Initialize the velocity tracker |
| 201 | initOrResetVelocityTracker(); |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 202 | mVelocityTracker.addMovement(ev); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 203 | break; |
| 204 | } |
| 205 | case MotionEvent.ACTION_POINTER_DOWN: { |
| 206 | final int index = ev.getActionIndex(); |
Winson | 40149bf | 2015-12-03 10:07:52 -0800 | [diff] [blame] | 207 | mActivePointerId = ev.getPointerId(index); |
| 208 | mDownX = (int) ev.getX(index); |
| 209 | mDownY = (int) ev.getY(index); |
Winson | 8b1871d | 2015-11-20 09:56:20 -0800 | [diff] [blame] | 210 | mLastY = mDownY; |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 211 | mDownScrollP = mScroller.getStackScroll(); |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 212 | mVelocityTracker.addMovement(ev); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 213 | break; |
| 214 | } |
| 215 | case MotionEvent.ACTION_MOVE: { |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 216 | int activePointerIndex = ev.findPointerIndex(mActivePointerId); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 217 | int y = (int) ev.getY(activePointerIndex); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 218 | if (!mIsScrolling) { |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 219 | if (Math.abs(y - mDownY) > mScrollTouchSlop) { |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 220 | mIsScrolling = true; |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 221 | |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 222 | // Disallow parents from intercepting touch events |
| 223 | final ViewParent parent = mSv.getParent(); |
| 224 | if (parent != null) { |
| 225 | parent.requestDisallowInterceptTouchEvent(true); |
| 226 | } |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | if (mIsScrolling) { |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 230 | // If we just move linearly on the screen, then that would map to 1/arclength |
| 231 | // of the curve, so just move the scroll proportional to that |
| 232 | float deltaP = layoutAlgorithm.getDeltaPForY(mDownY, y); |
| 233 | float curScrollP = mDownScrollP + deltaP; |
| 234 | mScroller.setStackScroll(curScrollP); |
Winson | 5500390 | 2016-01-12 12:00:37 -0800 | [diff] [blame] | 235 | mStackViewScrolledEvent.updateY(y - mLastY); |
| 236 | EventBus.getDefault().send(mStackViewScrolledEvent); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Winson | 8b1871d | 2015-11-20 09:56:20 -0800 | [diff] [blame] | 239 | mLastY = y; |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 240 | mVelocityTracker.addMovement(ev); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 241 | break; |
| 242 | } |
| 243 | case MotionEvent.ACTION_POINTER_UP: { |
| 244 | int pointerIndex = ev.getActionIndex(); |
| 245 | int pointerId = ev.getPointerId(pointerIndex); |
| 246 | if (pointerId == mActivePointerId) { |
| 247 | // Select a new active pointer id and reset the motion state |
| 248 | final int newPointerIndex = (pointerIndex == 0) ? 1 : 0; |
| 249 | mActivePointerId = ev.getPointerId(newPointerIndex); |
Winson | 40149bf | 2015-12-03 10:07:52 -0800 | [diff] [blame] | 250 | mDownX = (int) ev.getX(pointerIndex); |
| 251 | mDownY = (int) ev.getY(pointerIndex); |
| 252 | mLastY = mDownY; |
| 253 | mDownScrollP = mScroller.getStackScroll(); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 254 | } |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 255 | mVelocityTracker.addMovement(ev); |
| 256 | break; |
| 257 | } |
| 258 | case MotionEvent.ACTION_UP: { |
| 259 | mVelocityTracker.addMovement(ev); |
| 260 | mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 261 | int activePointerIndex = ev.findPointerIndex(mActivePointerId); |
| 262 | int y = (int) ev.getY(activePointerIndex); |
| 263 | int velocity = (int) mVelocityTracker.getYVelocity(mActivePointerId); |
| 264 | if (mIsScrolling) { |
Winson | a5e6b36 | 2015-11-02 17:17:20 -0800 | [diff] [blame] | 265 | if (mScroller.isScrollOutOfBounds()) { |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 266 | mScroller.animateBoundScroll(); |
| 267 | } else if (Math.abs(velocity) > mMinimumVelocity) { |
Winson | 36a5a2c | 2015-10-29 18:04:39 -0700 | [diff] [blame] | 268 | float minY = mDownY + layoutAlgorithm.getYForDeltaP(mDownScrollP, |
Winson | a5e6b36 | 2015-11-02 17:17:20 -0800 | [diff] [blame] | 269 | layoutAlgorithm.mMaxScrollP); |
Winson | 36a5a2c | 2015-10-29 18:04:39 -0700 | [diff] [blame] | 270 | float maxY = mDownY + layoutAlgorithm.getYForDeltaP(mDownScrollP, |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 271 | layoutAlgorithm.mMinScrollP); |
Winson | 36a5a2c | 2015-10-29 18:04:39 -0700 | [diff] [blame] | 272 | mScroller.fling(mDownScrollP, mDownY, y, velocity, (int) minY, (int) maxY, |
| 273 | mOverscrollSize); |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 274 | mSv.invalidate(); |
| 275 | } |
| 276 | } else if (mActiveTaskView == null) { |
| 277 | // This tap didn't start on a task. |
| 278 | maybeHideRecentsFromBackgroundTap((int) ev.getX(), (int) ev.getY()); |
| 279 | } |
| 280 | |
| 281 | mActivePointerId = INACTIVE_POINTER_ID; |
| 282 | mIsScrolling = false; |
| 283 | recycleVelocityTracker(); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 284 | break; |
| 285 | } |
| 286 | case MotionEvent.ACTION_CANCEL: { |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 287 | mActivePointerId = INACTIVE_POINTER_ID; |
| 288 | mIsScrolling = false; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 289 | recycleVelocityTracker(); |
| 290 | break; |
| 291 | } |
| 292 | } |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 293 | return mIsScrolling; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 294 | } |
| 295 | |
James Cook | 4bd79b7 | 2015-03-17 07:33:28 -0700 | [diff] [blame] | 296 | /** Hides recents if the up event at (x, y) is a tap on the background area. */ |
| 297 | void maybeHideRecentsFromBackgroundTap(int x, int y) { |
| 298 | // Ignore the up event if it's too far from its start position. The user might have been |
| 299 | // trying to scroll or swipe. |
Winson | 23afcae | 2015-10-28 11:14:54 -0700 | [diff] [blame] | 300 | int dx = Math.abs(mDownX - x); |
| 301 | int dy = Math.abs(mDownY - y); |
James Cook | 4bd79b7 | 2015-03-17 07:33:28 -0700 | [diff] [blame] | 302 | if (dx > mScrollTouchSlop || dy > mScrollTouchSlop) { |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | // Shift the tap position toward the center of the task stack and check to see if it would |
| 307 | // have hit a view. The user might have tried to tap on a task and missed slightly. |
| 308 | int shiftedX = x; |
Winson | 147ecaf | 2015-09-16 16:49:55 -0700 | [diff] [blame] | 309 | if (x > (mSv.getRight() - mSv.getLeft()) / 2) { |
James Cook | 4bd79b7 | 2015-03-17 07:33:28 -0700 | [diff] [blame] | 310 | shiftedX -= mWindowTouchSlop; |
| 311 | } else { |
| 312 | shiftedX += mWindowTouchSlop; |
| 313 | } |
| 314 | if (findViewAtPoint(shiftedX, y) != null) { |
| 315 | return; |
| 316 | } |
| 317 | |
Winson | a5e6b36 | 2015-11-02 17:17:20 -0800 | [diff] [blame] | 318 | // If tapping on the freeform workspace background, just launch the first freeform task |
| 319 | SystemServicesProxy ssp = Recents.getSystemServices(); |
| 320 | if (ssp.hasFreeformWorkspaceSupport()) { |
| 321 | Rect freeformRect = mSv.mLayoutAlgorithm.mFreeformRect; |
| 322 | if (freeformRect.top <= y && y <= freeformRect.bottom) { |
| 323 | if (mSv.launchFreeformTasks()) { |
Winson | f24f216 | 2016-01-05 12:11:55 -0800 | [diff] [blame] | 324 | // TODO: Animate Recents away as we launch the freeform tasks |
Winson | a5e6b36 | 2015-11-02 17:17:20 -0800 | [diff] [blame] | 325 | return; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
James Cook | 4bd79b7 | 2015-03-17 07:33:28 -0700 | [diff] [blame] | 330 | // The user intentionally tapped on the background, which is like a tap on the "desktop". |
| 331 | // Hide recents and transition to the launcher. |
Winson | 412e180 | 2015-10-20 16:57:57 -0700 | [diff] [blame] | 332 | EventBus.getDefault().send(new HideRecentsEvent(false, true)); |
James Cook | 4bd79b7 | 2015-03-17 07:33:28 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Winson Chung | d213a1e | 2014-10-02 11:18:30 -0700 | [diff] [blame] | 335 | /** Handles generic motion events */ |
| 336 | public boolean onGenericMotionEvent(MotionEvent ev) { |
| 337 | if ((ev.getSource() & InputDevice.SOURCE_CLASS_POINTER) == |
| 338 | InputDevice.SOURCE_CLASS_POINTER) { |
| 339 | int action = ev.getAction(); |
| 340 | switch (action & MotionEvent.ACTION_MASK) { |
| 341 | case MotionEvent.ACTION_SCROLL: |
| 342 | // Find the front most task and scroll the next task to the front |
| 343 | float vScroll = ev.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 344 | if (vScroll > 0) { |
Winson | 1b58561 | 2015-11-06 09:16:26 -0800 | [diff] [blame] | 345 | mSv.setRelativeFocusedTask(true, true /* stackTasksOnly */, |
| 346 | false /* animated */); |
Winson Chung | d213a1e | 2014-10-02 11:18:30 -0700 | [diff] [blame] | 347 | } else { |
Winson | 1b58561 | 2015-11-06 09:16:26 -0800 | [diff] [blame] | 348 | mSv.setRelativeFocusedTask(false, true /* stackTasksOnly */, |
| 349 | false /* animated */); |
Winson Chung | d213a1e | 2014-10-02 11:18:30 -0700 | [diff] [blame] | 350 | } |
| 351 | return true; |
| 352 | } |
| 353 | } |
| 354 | return false; |
| 355 | } |
| 356 | |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 357 | /**** SwipeHelper Implementation ****/ |
| 358 | |
| 359 | @Override |
| 360 | public View getChildAtPosition(MotionEvent ev) { |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 361 | TaskView tv = findViewAtPoint((int) ev.getX(), (int) ev.getY()); |
| 362 | if (tv != null && canChildBeDismissed(tv)) { |
| 363 | return tv; |
| 364 | } |
| 365 | return null; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public boolean canChildBeDismissed(View v) { |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 370 | // Disallow dismissing an already dismissed task |
| 371 | TaskView tv = (TaskView) v; |
| 372 | return !mSwipeHelperAnimations.containsKey(v) && |
| 373 | (mSv.getStack().indexOfStackTask(tv.getTask()) != -1); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public void onBeginDrag(View v) { |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 378 | TaskView tv = (TaskView) v; |
Winson | 671e8f9 | 2016-01-12 13:16:56 -0800 | [diff] [blame] | 379 | |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 380 | // Disable clipping with the stack while we are swiping |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 381 | tv.setClipViewInStack(false); |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 382 | // Disallow touch events from this task view |
Winson Chung | 1f24c7e | 2014-07-11 17:06:48 -0700 | [diff] [blame] | 383 | tv.setTouchEnabled(false); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 384 | // Disallow parents from intercepting touch events |
| 385 | final ViewParent parent = mSv.getParent(); |
| 386 | if (parent != null) { |
| 387 | parent.requestDisallowInterceptTouchEvent(true); |
| 388 | } |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 389 | |
| 390 | // Add this task to the set of tasks we are deleting |
| 391 | mSv.addIgnoreTask(tv.getTask()); |
| 392 | |
| 393 | // Determine if we are animating the other tasks while dismissing this task |
| 394 | mCurrentTasks = mSv.getStack().getStackTasks(); |
| 395 | MutableBoolean isFrontMostTask = new MutableBoolean(false); |
| 396 | Task anchorTask = mSv.findAnchorTask(mCurrentTasks, isFrontMostTask); |
| 397 | TaskStackViewScroller stackScroller = mSv.getScroller(); |
| 398 | if (anchorTask != null) { |
| 399 | // Get the current set of task transforms |
| 400 | mSv.getCurrentTaskTransforms(mCurrentTasks, mCurrentTaskTransforms); |
| 401 | |
| 402 | // Get the stack scroll of the task to anchor to (since we are removing something, the |
| 403 | // front most task will be our anchor task) |
| 404 | float prevAnchorTaskScroll = 0; |
| 405 | boolean pullStackForward = mCurrentTasks.size() > 0; |
| 406 | if (pullStackForward) { |
| 407 | prevAnchorTaskScroll = mSv.getStackAlgorithm().getStackScrollForTask(anchorTask); |
| 408 | } |
| 409 | |
| 410 | // Calculate where the views would be without the deleting tasks |
| 411 | mSv.updateLayoutAlgorithm(false /* boundScroll */); |
| 412 | |
| 413 | float newStackScroll = stackScroller.getStackScroll(); |
| 414 | if (isFrontMostTask.value) { |
| 415 | // Bound the stack scroll to pull tasks forward if necessary |
| 416 | newStackScroll = stackScroller.getBoundedStackScroll(newStackScroll); |
| 417 | } else if (pullStackForward) { |
| 418 | // Otherwise, offset the scroll by the movement of the anchor task |
| 419 | float anchorTaskScroll = mSv.getStackAlgorithm().getStackScrollForTask(anchorTask); |
| 420 | float stackScrollOffset = (anchorTaskScroll - prevAnchorTaskScroll); |
| 421 | if (mSv.getStackAlgorithm().getFocusState() != |
| 422 | TaskStackLayoutAlgorithm.STATE_FOCUSED) { |
| 423 | // If we are focused, we don't want the front task to move, but otherwise, we |
| 424 | // allow the back task to move up, and the front task to move back |
| 425 | stackScrollOffset /= 2; |
| 426 | } |
| 427 | newStackScroll = stackScroller.getBoundedStackScroll(stackScroller.getStackScroll() |
| 428 | + stackScrollOffset); |
| 429 | } |
| 430 | |
| 431 | // Pick up the newly visible views, not including the deleting tasks |
| 432 | mSv.bindVisibleTaskViews(newStackScroll); |
| 433 | |
| 434 | // Get the final set of task transforms (with task removed) |
| 435 | mSv.getLayoutTaskTransforms(newStackScroll, mCurrentTasks, mFinalTaskTransforms); |
| 436 | |
| 437 | // Set the target to scroll towards upon dismissal |
| 438 | mTargetStackScroll = newStackScroll; |
| 439 | |
| 440 | /* |
| 441 | * Post condition: All views that will be visible as a part of the gesture are retrieved |
| 442 | * and at their initial positions. The stack is still at the current |
| 443 | * scroll, but the layout is updated without the task currently being |
| 444 | * dismissed. |
| 445 | */ |
| 446 | } |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | @Override |
Winson | 671e8f9 | 2016-01-12 13:16:56 -0800 | [diff] [blame] | 450 | public boolean updateSwipeProgress(View v, boolean dismissable, float swipeProgress) { |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 451 | updateTaskViewTransforms(getDismissFraction(v)); |
Winson | 671e8f9 | 2016-01-12 13:16:56 -0800 | [diff] [blame] | 452 | return true; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 455 | /** |
| 456 | * Called after the {@link TaskView} is finished animating away. |
| 457 | */ |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 458 | @Override |
| 459 | public void onChildDismissed(View v) { |
| 460 | TaskView tv = (TaskView) v; |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 461 | |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 462 | // Re-enable clipping with the stack (we will reuse this view) |
| 463 | tv.setClipViewInStack(true); |
Winson Chung | 3e3365b | 2014-07-17 18:39:02 -0700 | [diff] [blame] | 464 | // Re-enable touch events from this task view |
| 465 | tv.setTouchEnabled(true); |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 466 | // Update the scroll to the final scroll position from onBeginDrag() |
| 467 | mSv.getScroller().setStackScroll(mTargetStackScroll, null); |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 468 | // Remove the task view from the stack |
Winson | ef06413 | 2016-01-05 12:11:31 -0800 | [diff] [blame] | 469 | EventBus.getDefault().send(new TaskViewDismissedEvent(tv.getTask(), tv)); |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 470 | // Stop tracking this deletion animation |
| 471 | mSwipeHelperAnimations.remove(v); |
Winson Chung | 5c9f4b9 | 2015-06-25 16:16:46 -0700 | [diff] [blame] | 472 | // Keep track of deletions by keyboard |
| 473 | MetricsLogger.histogram(tv.getContext(), "overview_task_dismissed_source", |
| 474 | Constants.Metrics.DismissSourceSwipeGesture); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 477 | /** |
| 478 | * Called after the {@link TaskView} is finished animating back into the list. |
| 479 | * onChildDismissed() calls. |
| 480 | */ |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 481 | @Override |
Winson | 671e8f9 | 2016-01-12 13:16:56 -0800 | [diff] [blame] | 482 | public void onChildSnappedBack(View v) { |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 483 | TaskView tv = (TaskView) v; |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 484 | |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 485 | // Re-enable clipping with the stack |
| 486 | tv.setClipViewInStack(true); |
| 487 | // Re-enable touch events from this task view |
Winson Chung | 1f24c7e | 2014-07-11 17:06:48 -0700 | [diff] [blame] | 488 | tv.setTouchEnabled(true); |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 489 | |
| 490 | // Stop tracking this deleting task, and update the layout to include this task again. The |
| 491 | // stack scroll does not need to be reset, since the scroll has not actually changed in |
| 492 | // onBeginDrag(). |
| 493 | mSv.removeIgnoreTask(tv.getTask()); |
| 494 | mSv.updateLayoutAlgorithm(false /* boundScroll */); |
| 495 | mSwipeHelperAnimations.remove(v); |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 496 | } |
Winson Chung | 7aceb9a | 2014-07-03 13:38:01 -0700 | [diff] [blame] | 497 | |
| 498 | @Override |
| 499 | public void onDragCancelled(View v) { |
| 500 | // Do nothing |
| 501 | } |
Winson | 671e8f9 | 2016-01-12 13:16:56 -0800 | [diff] [blame] | 502 | |
| 503 | @Override |
| 504 | public View getChildContentView(View v) { |
| 505 | return v; |
| 506 | } |
| 507 | |
| 508 | @Override |
| 509 | public boolean isAntiFalsingNeeded() { |
| 510 | return false; |
| 511 | } |
| 512 | |
| 513 | @Override |
| 514 | public float getFalsingThresholdFactor() { |
| 515 | return 0; |
| 516 | } |
| 517 | |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 518 | /** |
| 519 | * Interpolates the non-deleting tasks to their final transforms from their current transforms. |
| 520 | */ |
| 521 | private void updateTaskViewTransforms(float dismissFraction) { |
| 522 | List<TaskView> taskViews = mSv.getTaskViews(); |
| 523 | int taskViewCount = taskViews.size(); |
| 524 | for (int i = 0; i < taskViewCount; i++) { |
| 525 | TaskView tv = taskViews.get(i); |
| 526 | Task task = tv.getTask(); |
| 527 | |
| 528 | if (mSv.isIgnoredTask(task)) { |
| 529 | continue; |
| 530 | } |
| 531 | |
| 532 | int taskIndex = mCurrentTasks.indexOf(task); |
| 533 | TaskViewTransform fromTransform = mCurrentTaskTransforms.get(taskIndex); |
| 534 | TaskViewTransform toTransform = mFinalTaskTransforms.get(taskIndex); |
| 535 | |
| 536 | mTmpTransform.copyFrom(fromTransform); |
| 537 | // We only really need to interpolate the bounds, progress and translation |
| 538 | mTmpTransform.rect.set(RECT_EVALUATOR.evaluate(dismissFraction, fromTransform.rect, |
| 539 | toTransform.rect)); |
| 540 | mTmpTransform.p = fromTransform.p + (toTransform.p - fromTransform.p) * dismissFraction; |
| 541 | mTmpTransform.translationZ = fromTransform.translationZ + |
| 542 | (toTransform.translationZ - fromTransform.translationZ) * dismissFraction; |
| 543 | |
| 544 | mSv.updateTaskViewToTransform(tv, mTmpTransform, TaskViewAnimation.IMMEDIATE); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /** Returns the view at the specified coordinates */ |
| 549 | private TaskView findViewAtPoint(int x, int y) { |
| 550 | List<Task> tasks = mSv.getStack().getStackTasks(); |
| 551 | int taskCount = tasks.size(); |
| 552 | for (int i = taskCount - 1; i >= 0; i--) { |
| 553 | TaskView tv = mSv.getChildViewForTask(tasks.get(i)); |
| 554 | if (tv != null && tv.getVisibility() == View.VISIBLE) { |
| 555 | if (mSv.isTouchPointInView(x, y, tv)) { |
| 556 | return tv; |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | return null; |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Returns the fraction which we should interpolate the other task views based on the dismissal |
| 565 | * of this given task. |
| 566 | * |
| 567 | * TODO: We can interpolate this to adjust when the other tasks should respond to the dismissal |
| 568 | */ |
| 569 | private float getDismissFraction(View v) { |
| 570 | float fraction = Math.min(1f, Math.abs(v.getTranslationX() / mSv.getWidth())); |
| 571 | return STACK_TRANSFORM_INTERPOLATOR.getInterpolation(fraction); |
| 572 | } |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 573 | } |