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(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 84 | cleanUpDismissTarget(); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 85 | mTouchedView = null; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 86 | return false; |
| 87 | } |
| 88 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 89 | final boolean isStack = mStack.equals(mTouchedView); |
| 90 | final float rawX = event.getRawX(); |
| 91 | final float rawY = event.getRawY(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 92 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 93 | // The coordinates of the touch event, in terms of the touched view's position. |
| 94 | final float viewX = mViewPositionOnTouchDown.x + rawX - mTouchDown.x; |
| 95 | final float viewY = mViewPositionOnTouchDown.y + rawY - mTouchDown.y; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 96 | switch (action) { |
| 97 | case MotionEvent.ACTION_DOWN: |
| 98 | trackMovement(event); |
| 99 | |
Mady Mellor | 2f01ed9 | 2018-11-07 09:24:04 -0800 | [diff] [blame] | 100 | mDismissViewController.createDismissTarget(); |
| 101 | mHandler.postDelayed(mShowDismissAffordance, SHOW_TARGET_DELAY); |
| 102 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 103 | mTouchDown.set(rawX, rawY); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 104 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 105 | if (isStack) { |
| 106 | mViewPositionOnTouchDown.set(mStack.getStackPosition()); |
| 107 | mStack.onDragStart(); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 108 | } else { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 109 | mViewPositionOnTouchDown.set( |
| 110 | mTouchedView.getTranslationX(), mTouchedView.getTranslationY()); |
| 111 | mStack.onBubbleDragStart(mTouchedView); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 114 | break; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 115 | case MotionEvent.ACTION_MOVE: |
| 116 | trackMovement(event); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 117 | final float deltaX = rawX - mTouchDown.x; |
| 118 | final float deltaY = rawY - mTouchDown.y; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 119 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 120 | if ((deltaX * deltaX) + (deltaY * deltaY) > mTouchSlopSquared && !mMovedEnough) { |
| 121 | mMovedEnough = true; |
| 122 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 123 | |
| 124 | if (mMovedEnough) { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 125 | if (isStack) { |
| 126 | mStack.onDragged(viewX, viewY); |
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 | mStack.onBubbleDragged(mTouchedView, viewX, viewY); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 129 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 130 | } |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 131 | |
Mady Mellor | 2f01ed9 | 2018-11-07 09:24:04 -0800 | [diff] [blame] | 132 | // 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] | 133 | mInDismissTarget = mDismissViewController.updateTarget( |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 134 | isStack ? mStack.getBubbleAt(0) : mTouchedView); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 135 | break; |
| 136 | |
| 137 | case MotionEvent.ACTION_CANCEL: |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 138 | mTouchedView = null; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 139 | cleanUpDismissTarget(); |
| 140 | break; |
| 141 | |
| 142 | case MotionEvent.ACTION_UP: |
| 143 | trackMovement(event); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 144 | if (mInDismissTarget && isStack) { |
| 145 | mController.dismissStack(); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 146 | } else if (mMovedEnough) { |
| 147 | mVelocityTracker.computeCurrentVelocity(1000); |
| 148 | final float velX = mVelocityTracker.getXVelocity(); |
| 149 | final float velY = mVelocityTracker.getYVelocity(); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 150 | if (isStack) { |
| 151 | mStack.onDragFinish(viewX, viewY, velX, velY); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 152 | } else { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 153 | final boolean dismissed = mInDismissTarget || velY > DISMISS_MIN_VELOCITY; |
| 154 | mStack.onBubbleDragFinish( |
| 155 | mTouchedView, viewX, viewY, velX, velY, /* dismissed */ dismissed); |
| 156 | if (dismissed) { |
| 157 | mController.removeBubble(((BubbleView) mTouchedView).getKey()); |
| 158 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 159 | } |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 160 | } else if (mTouchedView.equals(mStack.getExpandedBubbleView())) { |
| 161 | mStack.collapseStack(); |
| 162 | } else if (isStack) { |
| 163 | if (mStack.isExpanded()) { |
| 164 | mStack.collapseStack(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 165 | } else { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 166 | mStack.expandStack(); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame] | 167 | } |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 168 | } else { |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 169 | mStack.setExpandedBubble(((BubbleView) mTouchedView).getKey()); |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 170 | } |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 171 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 172 | cleanUpDismissTarget(); |
| 173 | mVelocityTracker.recycle(); |
| 174 | mVelocityTracker = null; |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 175 | mTouchedView = null; |
| 176 | mMovedEnough = false; |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 177 | break; |
| 178 | } |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 179 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 180 | return true; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Removes the dismiss target and cancels any pending callbacks to show it. |
| 185 | */ |
| 186 | private void cleanUpDismissTarget() { |
| 187 | mHandler.removeCallbacks(mShowDismissAffordance); |
| 188 | mDismissViewController.destroyDismissTarget(); |
| 189 | } |
| 190 | |
Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame] | 191 | |
| 192 | private void trackMovement(MotionEvent event) { |
| 193 | if (mVelocityTracker == null) { |
| 194 | mVelocityTracker = VelocityTracker.obtain(); |
| 195 | } |
| 196 | mVelocityTracker.addMovement(event); |
| 197 | } |
| 198 | } |