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 | |
Mady Mellor | 2f01ed9 | 2018-11-07 09:24:04 -0800 | [diff] [blame] | 19 | import static com.android.systemui.pip.phone.PipDismissViewController.SHOW_TARGET_DELAY; |
| 20 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 22 | import android.graphics.PointF; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 23 | import android.os.Handler; |
| 24 | import android.view.MotionEvent; |
| 25 | import android.view.VelocityTracker; |
| 26 | import android.view.View; |
| 27 | import android.view.ViewConfiguration; |
| 28 | |
| 29 | import com.android.systemui.Dependency; |
| 30 | import com.android.systemui.pip.phone.PipDismissViewController; |
| 31 | |
| 32 | /** |
| 33 | * Handles interpreting touches on a {@link BubbleStackView}. This includes expanding, collapsing, |
| 34 | * dismissing, and flings. |
| 35 | */ |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 36 | class BubbleTouchHandler implements View.OnTouchListener { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 37 | /** Velocity required to dismiss a bubble without dragging it into the dismiss target. */ |
| 38 | private static final float DISMISS_MIN_VELOCITY = 4000f; |
| 39 | |
| 40 | private final PointF mTouchDown = new PointF(); |
| 41 | private final PointF mViewPositionOnTouchDown = new PointF(); |
| 42 | private final BubbleStackView mStack; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 43 | |
| 44 | private BubbleController mController = Dependency.get(BubbleController.class); |
| 45 | private PipDismissViewController mDismissViewController; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 46 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 47 | private boolean mMovedEnough; |
| 48 | private int mTouchSlopSquared; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 49 | private VelocityTracker mVelocityTracker; |
| 50 | |
| 51 | private boolean mInDismissTarget; |
| 52 | private Handler mHandler = new Handler(); |
| 53 | private Runnable mShowDismissAffordance = new Runnable() { |
| 54 | @Override |
| 55 | public void run() { |
| 56 | mDismissViewController.showDismissTarget(); |
| 57 | } |
| 58 | }; |
| 59 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 60 | /** View that was initially touched, when we received the first ACTION_DOWN event. */ |
| 61 | private View mTouchedView; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 62 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 63 | BubbleTouchHandler(Context context, BubbleStackView stackView) { |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 64 | final int touchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); |
| 65 | mTouchSlopSquared = touchSlop * touchSlop; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 66 | mDismissViewController = new PipDismissViewController(context); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 67 | mStack = stackView; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public boolean onTouch(View v, MotionEvent event) { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 72 | final int action = event.getActionMasked(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 73 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 74 | // If we aren't currently in the process of touching a view, figure out what we're touching. |
| 75 | // It'll be the stack, an individual bubble, or nothing. |
| 76 | if (mTouchedView == null) { |
| 77 | mTouchedView = mStack.getTargetView(event); |
| 78 | } |
| 79 | |
| 80 | // If this is an ACTION_OUTSIDE event, or the stack reported that we aren't touching |
| 81 | // anything, collapse the stack. |
| 82 | if (action == MotionEvent.ACTION_OUTSIDE || mTouchedView == null) { |
| 83 | mStack.collapseStack(); |
Mark Renouf | a867b7e | 2019-02-28 15:14:47 -0500 | [diff] [blame] | 84 | resetForNextGesture(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 85 | return false; |
| 86 | } |
| 87 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 88 | final boolean isStack = mStack.equals(mTouchedView); |
| 89 | final float rawX = event.getRawX(); |
| 90 | final float rawY = event.getRawY(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 91 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 92 | // The coordinates of the touch event, in terms of the touched view's position. |
| 93 | final float viewX = mViewPositionOnTouchDown.x + rawX - mTouchDown.x; |
| 94 | final float viewY = mViewPositionOnTouchDown.y + rawY - mTouchDown.y; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 95 | switch (action) { |
| 96 | case MotionEvent.ACTION_DOWN: |
| 97 | trackMovement(event); |
| 98 | |
Mady Mellor | 2f01ed9 | 2018-11-07 09:24:04 -0800 | [diff] [blame] | 99 | mDismissViewController.createDismissTarget(); |
| 100 | mHandler.postDelayed(mShowDismissAffordance, SHOW_TARGET_DELAY); |
| 101 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 102 | mTouchDown.set(rawX, rawY); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 103 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 104 | if (isStack) { |
| 105 | mViewPositionOnTouchDown.set(mStack.getStackPosition()); |
| 106 | mStack.onDragStart(); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 107 | } else { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 108 | mViewPositionOnTouchDown.set( |
| 109 | mTouchedView.getTranslationX(), mTouchedView.getTranslationY()); |
| 110 | mStack.onBubbleDragStart(mTouchedView); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 113 | break; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 114 | case MotionEvent.ACTION_MOVE: |
| 115 | trackMovement(event); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 116 | final float deltaX = rawX - mTouchDown.x; |
| 117 | final float deltaY = rawY - mTouchDown.y; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 118 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 119 | if ((deltaX * deltaX) + (deltaY * deltaY) > mTouchSlopSquared && !mMovedEnough) { |
| 120 | mMovedEnough = true; |
| 121 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 122 | |
| 123 | if (mMovedEnough) { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 124 | if (isStack) { |
| 125 | mStack.onDragged(viewX, viewY); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 126 | } else { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 127 | mStack.onBubbleDragged(mTouchedView, viewX, viewY); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 128 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 129 | } |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 130 | |
Mady Mellor | 2f01ed9 | 2018-11-07 09:24:04 -0800 | [diff] [blame] | 131 | // TODO - when we're in the target stick to it / animate in some way? |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 132 | mInDismissTarget = mDismissViewController.updateTarget( |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 133 | isStack ? mStack.getBubbleAt(0) : mTouchedView); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 134 | break; |
| 135 | |
| 136 | case MotionEvent.ACTION_CANCEL: |
Mark Renouf | a867b7e | 2019-02-28 15:14:47 -0500 | [diff] [blame] | 137 | resetForNextGesture(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 138 | break; |
| 139 | |
| 140 | case MotionEvent.ACTION_UP: |
| 141 | trackMovement(event); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 142 | if (mInDismissTarget && isStack) { |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 143 | mController.dismissStack(BubbleController.DISMISS_USER_GESTURE); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 144 | } else if (mMovedEnough) { |
Mark Renouf | a867b7e | 2019-02-28 15:14:47 -0500 | [diff] [blame] | 145 | mVelocityTracker.computeCurrentVelocity(/* maxVelocity */ 1000); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 146 | final float velX = mVelocityTracker.getXVelocity(); |
| 147 | final float velY = mVelocityTracker.getYVelocity(); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 148 | if (isStack) { |
| 149 | mStack.onDragFinish(viewX, viewY, velX, velY); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 150 | } else { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 151 | final boolean dismissed = mInDismissTarget || velY > DISMISS_MIN_VELOCITY; |
| 152 | mStack.onBubbleDragFinish( |
| 153 | mTouchedView, viewX, viewY, velX, velY, /* dismissed */ dismissed); |
| 154 | if (dismissed) { |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 155 | mController.removeBubble(((BubbleView) mTouchedView).getKey(), |
| 156 | BubbleController.DISMISS_USER_GESTURE); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 157 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 158 | } |
Mark Renouf | a867b7e | 2019-02-28 15:14:47 -0500 | [diff] [blame] | 159 | } else if (mTouchedView == mStack.getExpandedBubbleView()) { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 160 | mStack.collapseStack(); |
| 161 | } else if (isStack) { |
| 162 | if (mStack.isExpanded()) { |
| 163 | mStack.collapseStack(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 164 | } else { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 165 | mStack.expandStack(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 166 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 167 | } else { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 168 | mStack.setExpandedBubble(((BubbleView) mTouchedView).getKey()); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 169 | } |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 170 | |
Mark Renouf | a867b7e | 2019-02-28 15:14:47 -0500 | [diff] [blame] | 171 | resetForNextGesture(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 172 | break; |
| 173 | } |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 174 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 175 | return true; |
| 176 | } |
| 177 | |
Mark Renouf | a867b7e | 2019-02-28 15:14:47 -0500 | [diff] [blame] | 178 | /** Clears all touch-related state. */ |
| 179 | private void resetForNextGesture() { |
| 180 | cleanUpDismissTarget(); |
| 181 | if (mVelocityTracker != null) { |
| 182 | mVelocityTracker.recycle(); |
| 183 | mVelocityTracker = null; |
| 184 | } |
| 185 | mTouchedView = null; |
| 186 | mMovedEnough = false; |
| 187 | mInDismissTarget = false; |
| 188 | } |
| 189 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 190 | /** |
| 191 | * Removes the dismiss target and cancels any pending callbacks to show it. |
| 192 | */ |
| 193 | private void cleanUpDismissTarget() { |
| 194 | mHandler.removeCallbacks(mShowDismissAffordance); |
| 195 | mDismissViewController.destroyDismissTarget(); |
| 196 | } |
| 197 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 198 | |
| 199 | private void trackMovement(MotionEvent event) { |
| 200 | if (mVelocityTracker == null) { |
| 201 | mVelocityTracker = VelocityTracker.obtain(); |
| 202 | } |
| 203 | mVelocityTracker.addMovement(event); |
| 204 | } |
| 205 | } |