Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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.bubbles; |
| 18 | |
| 19 | import android.content.Context; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 20 | import android.graphics.PointF; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 21 | import android.view.MotionEvent; |
| 22 | import android.view.VelocityTracker; |
| 23 | import android.view.View; |
| 24 | import android.view.ViewConfiguration; |
| 25 | |
| 26 | import com.android.systemui.Dependency; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Handles interpreting touches on a {@link BubbleStackView}. This includes expanding, collapsing, |
| 30 | * dismissing, and flings. |
| 31 | */ |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 32 | class BubbleTouchHandler implements View.OnTouchListener { |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 33 | /** Velocity required to dismiss the stack without dragging it into the dismiss target. */ |
| 34 | private static final float STACK_DISMISS_MIN_VELOCITY = 4000f; |
| 35 | |
| 36 | /** |
| 37 | * Velocity required to dismiss an individual bubble without dragging it into the dismiss |
| 38 | * target. |
| 39 | * |
| 40 | * This is higher than the stack dismiss velocity since unlike the stack, a downward fling could |
| 41 | * also be an attempted gesture to return the bubble to the row of expanded bubbles, which would |
| 42 | * usually be below the dragged bubble. By increasing the required velocity, it's less likely |
| 43 | * that the user is trying to drop it back into the row vs. fling it away. |
| 44 | */ |
| 45 | private static final float INDIVIDUAL_BUBBLE_DISMISS_MIN_VELOCITY = 6000f; |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 46 | |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 47 | /** |
| 48 | * When the stack is flung towards the bottom of the screen, it'll be dismissed if it's flung |
| 49 | * towards the center of the screen (where the dismiss target is). This value is the width of |
| 50 | * the target area to be considered 'towards the target'. For example 50% means that the stack |
| 51 | * needs to be flung towards the middle 50%, and the 25% on the left and right sides won't |
| 52 | * count. |
| 53 | */ |
| 54 | private static final float DISMISS_FLING_TARGET_WIDTH_PERCENT = 0.5f; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 55 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 56 | private final PointF mTouchDown = new PointF(); |
| 57 | private final PointF mViewPositionOnTouchDown = new PointF(); |
| 58 | private final BubbleStackView mStack; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 59 | private final BubbleData mBubbleData; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 60 | |
| 61 | private BubbleController mController = Dependency.get(BubbleController.class); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 62 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 63 | private boolean mMovedEnough; |
| 64 | private int mTouchSlopSquared; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 65 | private VelocityTracker mVelocityTracker; |
| 66 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 67 | /** View that was initially touched, when we received the first ACTION_DOWN event. */ |
| 68 | private View mTouchedView; |
Mady Mellor | e9371bc | 2019-07-10 18:50:59 -0700 | [diff] [blame] | 69 | /** Whether the current touched view is in the dismiss target. */ |
| 70 | private boolean mInDismissTarget; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 71 | |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 72 | BubbleTouchHandler(BubbleStackView stackView, |
| 73 | BubbleData bubbleData, Context context) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 74 | final int touchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); |
| 75 | mTouchSlopSquared = touchSlop * touchSlop; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 76 | mBubbleData = bubbleData; |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 77 | mStack = stackView; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean onTouch(View v, MotionEvent event) { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 82 | final int action = event.getActionMasked(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 83 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 84 | // If we aren't currently in the process of touching a view, figure out what we're touching. |
| 85 | // It'll be the stack, an individual bubble, or nothing. |
| 86 | if (mTouchedView == null) { |
| 87 | mTouchedView = mStack.getTargetView(event); |
| 88 | } |
| 89 | |
| 90 | // If this is an ACTION_OUTSIDE event, or the stack reported that we aren't touching |
| 91 | // anything, collapse the stack. |
| 92 | if (action == MotionEvent.ACTION_OUTSIDE || mTouchedView == null) { |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 93 | mBubbleData.setExpanded(false); |
Mark Renouf | a867b7e | 2019-02-28 15:14:47 -0500 | [diff] [blame] | 94 | resetForNextGesture(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | |
Mady Mellor | 47b11e3 | 2019-07-11 19:06:21 -0700 | [diff] [blame] | 98 | if (!(mTouchedView instanceof BubbleView) |
| 99 | && !(mTouchedView instanceof BubbleStackView) |
| 100 | && !(mTouchedView instanceof BubbleFlyoutView)) { |
| 101 | // Not touching anything touchable, but we shouldn't collapse (e.g. touching edge |
| 102 | // of expanded view). |
| 103 | resetForNextGesture(); |
| 104 | return false; |
| 105 | } |
| 106 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 107 | final boolean isStack = mStack.equals(mTouchedView); |
Joshua Tsuji | 614b1df | 2019-03-26 13:57:05 -0400 | [diff] [blame] | 108 | final boolean isFlyout = mStack.getFlyoutView().equals(mTouchedView); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 109 | final float rawX = event.getRawX(); |
| 110 | final float rawY = event.getRawY(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 111 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 112 | // The coordinates of the touch event, in terms of the touched view's position. |
| 113 | final float viewX = mViewPositionOnTouchDown.x + rawX - mTouchDown.x; |
| 114 | final float viewY = mViewPositionOnTouchDown.y + rawY - mTouchDown.y; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 115 | switch (action) { |
| 116 | case MotionEvent.ACTION_DOWN: |
| 117 | trackMovement(event); |
| 118 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 119 | mTouchDown.set(rawX, rawY); |
Joshua Tsuji | 6549e70 | 2019-05-02 13:13:16 -0400 | [diff] [blame] | 120 | mStack.onGestureStart(); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 121 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 122 | if (isStack) { |
| 123 | mViewPositionOnTouchDown.set(mStack.getStackPosition()); |
| 124 | mStack.onDragStart(); |
Joshua Tsuji | 614b1df | 2019-03-26 13:57:05 -0400 | [diff] [blame] | 125 | } else if (isFlyout) { |
Joshua Tsuji | 6549e70 | 2019-05-02 13:13:16 -0400 | [diff] [blame] | 126 | mStack.onFlyoutDragStart(); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 127 | } else { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 128 | mViewPositionOnTouchDown.set( |
| 129 | mTouchedView.getTranslationX(), mTouchedView.getTranslationY()); |
| 130 | mStack.onBubbleDragStart(mTouchedView); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 133 | break; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 134 | case MotionEvent.ACTION_MOVE: |
| 135 | trackMovement(event); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 136 | final float deltaX = rawX - mTouchDown.x; |
| 137 | final float deltaY = rawY - mTouchDown.y; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 138 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 139 | if ((deltaX * deltaX) + (deltaY * deltaY) > mTouchSlopSquared && !mMovedEnough) { |
| 140 | mMovedEnough = true; |
| 141 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 142 | |
| 143 | if (mMovedEnough) { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 144 | if (isStack) { |
| 145 | mStack.onDragged(viewX, viewY); |
Joshua Tsuji | 614b1df | 2019-03-26 13:57:05 -0400 | [diff] [blame] | 146 | } else if (isFlyout) { |
Joshua Tsuji | 6549e70 | 2019-05-02 13:13:16 -0400 | [diff] [blame] | 147 | mStack.onFlyoutDragged(deltaX); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 148 | } else { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 149 | mStack.onBubbleDragged(mTouchedView, viewX, viewY); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 150 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 151 | } |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 152 | |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 153 | final boolean currentlyInDismissTarget = mStack.isInDismissTarget(event); |
| 154 | if (currentlyInDismissTarget != mInDismissTarget) { |
| 155 | mInDismissTarget = currentlyInDismissTarget; |
| 156 | |
| 157 | mVelocityTracker.computeCurrentVelocity(/* maxVelocity */ 1000); |
| 158 | final float velX = mVelocityTracker.getXVelocity(); |
| 159 | final float velY = mVelocityTracker.getYVelocity(); |
| 160 | |
| 161 | // If the touch event is within the dismiss target, magnet the stack to it. |
Joshua Tsuji | 6549e70 | 2019-05-02 13:13:16 -0400 | [diff] [blame] | 162 | if (!isFlyout) { |
| 163 | mStack.animateMagnetToDismissTarget( |
| 164 | mTouchedView, mInDismissTarget, viewX, viewY, velX, velY); |
| 165 | } |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 166 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 167 | break; |
| 168 | |
| 169 | case MotionEvent.ACTION_CANCEL: |
Mark Renouf | a867b7e | 2019-02-28 15:14:47 -0500 | [diff] [blame] | 170 | resetForNextGesture(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 171 | break; |
| 172 | |
| 173 | case MotionEvent.ACTION_UP: |
| 174 | trackMovement(event); |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 175 | mVelocityTracker.computeCurrentVelocity(/* maxVelocity */ 1000); |
| 176 | final float velX = mVelocityTracker.getXVelocity(); |
| 177 | final float velY = mVelocityTracker.getYVelocity(); |
| 178 | |
| 179 | final boolean shouldDismiss = |
| 180 | isStack |
| 181 | ? mInDismissTarget |
| 182 | || isFastFlingTowardsDismissTarget(rawX, rawY, velX, velY) |
| 183 | : mInDismissTarget |
| 184 | || velY > INDIVIDUAL_BUBBLE_DISMISS_MIN_VELOCITY; |
| 185 | |
Joshua Tsuji | 6549e70 | 2019-05-02 13:13:16 -0400 | [diff] [blame] | 186 | if (isFlyout && mMovedEnough) { |
| 187 | mStack.onFlyoutDragFinished(rawX - mTouchDown.x /* deltaX */, velX); |
| 188 | } else if (shouldDismiss) { |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 189 | final String individualBubbleKey = |
| 190 | isStack ? null : ((BubbleView) mTouchedView).getKey(); |
| 191 | mStack.magnetToStackIfNeededThenAnimateDismissal(mTouchedView, velX, velY, |
| 192 | () -> { |
| 193 | if (isStack) { |
| 194 | mController.dismissStack(BubbleController.DISMISS_USER_GESTURE); |
| 195 | } else { |
| 196 | mController.removeBubble( |
| 197 | individualBubbleKey, |
| 198 | BubbleController.DISMISS_USER_GESTURE); |
| 199 | } |
| 200 | }); |
Joshua Tsuji | 614b1df | 2019-03-26 13:57:05 -0400 | [diff] [blame] | 201 | } else if (isFlyout) { |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 202 | if (!mBubbleData.isExpanded() && !mMovedEnough) { |
Joshua Tsuji | 14e6855 | 2019-06-06 17:17:08 -0400 | [diff] [blame] | 203 | mStack.onFlyoutTapped(); |
Joshua Tsuji | 614b1df | 2019-03-26 13:57:05 -0400 | [diff] [blame] | 204 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 205 | } else if (mMovedEnough) { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 206 | if (isStack) { |
| 207 | mStack.onDragFinish(viewX, viewY, velX, velY); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 208 | } else { |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 209 | mStack.onBubbleDragFinish(mTouchedView, viewX, viewY, velX, velY); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 210 | } |
Mark Renouf | a867b7e | 2019-02-28 15:14:47 -0500 | [diff] [blame] | 211 | } else if (mTouchedView == mStack.getExpandedBubbleView()) { |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 212 | mBubbleData.setExpanded(false); |
Joshua Tsuji | 6549e70 | 2019-05-02 13:13:16 -0400 | [diff] [blame] | 213 | } else if (isStack || isFlyout) { |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 214 | // Toggle expansion |
| 215 | mBubbleData.setExpanded(!mBubbleData.isExpanded()); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 216 | } else { |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 217 | final String key = ((BubbleView) mTouchedView).getKey(); |
| 218 | mBubbleData.setSelectedBubble(mBubbleData.getBubbleWithKey(key)); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 219 | } |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 220 | |
Mark Renouf | a867b7e | 2019-02-28 15:14:47 -0500 | [diff] [blame] | 221 | resetForNextGesture(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 222 | break; |
| 223 | } |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 224 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 225 | return true; |
| 226 | } |
| 227 | |
Joshua Tsuji | 4accf598 | 2019-04-22 17:36:11 -0400 | [diff] [blame] | 228 | /** |
| 229 | * Whether the given touch data represents a powerful fling towards the bottom-center of the |
| 230 | * screen (the dismiss target). |
| 231 | */ |
| 232 | private boolean isFastFlingTowardsDismissTarget( |
| 233 | float rawX, float rawY, float velX, float velY) { |
| 234 | // Not a fling downward towards the target if velocity is zero or negative. |
| 235 | if (velY <= 0) { |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | float bottomOfScreenInterceptX = rawX; |
| 240 | |
| 241 | // Only do math if the X velocity is non-zero, otherwise X won't change. |
| 242 | if (velX != 0) { |
| 243 | // Rise over run... |
| 244 | final float slope = velY / velX; |
| 245 | // ...y = mx + b, b = y / mx... |
| 246 | final float yIntercept = rawY - slope * rawX; |
| 247 | // ...calculate the x value when y = bottom of the screen. |
| 248 | bottomOfScreenInterceptX = (mStack.getHeight() - yIntercept) / slope; |
| 249 | } |
| 250 | |
| 251 | final float dismissTargetWidth = |
| 252 | mStack.getWidth() * DISMISS_FLING_TARGET_WIDTH_PERCENT; |
| 253 | return velY > STACK_DISMISS_MIN_VELOCITY |
| 254 | && bottomOfScreenInterceptX > dismissTargetWidth / 2f |
| 255 | && bottomOfScreenInterceptX < mStack.getWidth() - dismissTargetWidth / 2f; |
| 256 | } |
| 257 | |
Mark Renouf | a867b7e | 2019-02-28 15:14:47 -0500 | [diff] [blame] | 258 | /** Clears all touch-related state. */ |
| 259 | private void resetForNextGesture() { |
Mark Renouf | a867b7e | 2019-02-28 15:14:47 -0500 | [diff] [blame] | 260 | if (mVelocityTracker != null) { |
| 261 | mVelocityTracker.recycle(); |
| 262 | mVelocityTracker = null; |
| 263 | } |
Joshua Tsuji | 6549e70 | 2019-05-02 13:13:16 -0400 | [diff] [blame] | 264 | |
Mark Renouf | a867b7e | 2019-02-28 15:14:47 -0500 | [diff] [blame] | 265 | mTouchedView = null; |
| 266 | mMovedEnough = false; |
| 267 | mInDismissTarget = false; |
Joshua Tsuji | 6549e70 | 2019-05-02 13:13:16 -0400 | [diff] [blame] | 268 | |
| 269 | mStack.onGestureFinished(); |
Mark Renouf | a867b7e | 2019-02-28 15:14:47 -0500 | [diff] [blame] | 270 | } |
| 271 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 272 | private void trackMovement(MotionEvent event) { |
| 273 | if (mVelocityTracker == null) { |
| 274 | mVelocityTracker = VelocityTracker.obtain(); |
| 275 | } |
| 276 | mVelocityTracker.addMovement(event); |
| 277 | } |
| 278 | } |