Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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; |
| 18 | |
| 19 | import android.animation.Animator; |
Chet Haase | 2f2022a | 2011-10-11 06:41:59 -0700 | [diff] [blame] | 20 | import android.animation.AnimatorListenerAdapter; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 21 | import android.animation.ObjectAnimator; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 22 | import android.animation.ValueAnimator; |
| 23 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Dan Sandler | eceda3d | 2014-07-21 15:35:01 -0400 | [diff] [blame] | 24 | import android.content.Context; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 25 | import android.graphics.RectF; |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 26 | import android.os.Handler; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 27 | import android.util.Log; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 28 | import android.view.MotionEvent; |
| 29 | import android.view.VelocityTracker; |
| 30 | import android.view.View; |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 31 | import android.view.ViewConfiguration; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 32 | import android.view.accessibility.AccessibilityEvent; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 33 | |
Blazej Magnowski | 6dc59b4 | 2015-09-22 15:14:20 -0700 | [diff] [blame] | 34 | import com.android.systemui.classifier.FalsingManager; |
Mady Mellor | 2879631 | 2016-03-08 14:12:42 -0800 | [diff] [blame] | 35 | import com.android.systemui.statusbar.FlingAnimationUtils; |
Blazej Magnowski | 6dc59b4 | 2015-09-22 15:14:20 -0700 | [diff] [blame] | 36 | |
dongwan0605.kim | 30637e4 | 2016-03-02 17:16:47 +0900 | [diff] [blame] | 37 | import java.util.HashMap; |
| 38 | |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 39 | public class SwipeHelper implements Gefingerpoken { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 40 | static final String TAG = "com.android.systemui.SwipeHelper"; |
Daniel Sandler | 96f4818 | 2011-08-17 09:50:35 -0400 | [diff] [blame] | 41 | private static final boolean DEBUG = false; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 42 | private static final boolean DEBUG_INVALIDATE = false; |
| 43 | private static final boolean SLOW_ANIMATIONS = false; // DEBUG; |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 44 | private static final boolean CONSTRAIN_SWIPE = true; |
| 45 | private static final boolean FADE_OUT_DURING_SWIPE = true; |
| 46 | private static final boolean DISMISS_IF_SWIPED_FAR_ENOUGH = true; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 47 | |
| 48 | public static final int X = 0; |
| 49 | public static final int Y = 1; |
| 50 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 51 | private float SWIPE_ESCAPE_VELOCITY = 100f; // dp/sec |
Michael Jurka | 0e8063a | 2011-09-09 15:31:55 -0700 | [diff] [blame] | 52 | private int DEFAULT_ESCAPE_ANIMATION_DURATION = 200; // ms |
| 53 | private int MAX_ESCAPE_ANIMATION_DURATION = 400; // ms |
| 54 | private int MAX_DISMISS_VELOCITY = 2000; // dp/sec |
| 55 | private static final int SNAP_ANIM_LEN = SLOW_ANIMATIONS ? 1000 : 150; // ms |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 56 | |
Adrian Roos | 5d9cc66 | 2014-05-28 17:08:13 +0200 | [diff] [blame] | 57 | public static float SWIPE_PROGRESS_FADE_START = 0f; // fraction of thumbnail width |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 58 | // where fade starts |
Adrian Roos | 5d9cc66 | 2014-05-28 17:08:13 +0200 | [diff] [blame] | 59 | static final float SWIPE_PROGRESS_FADE_END = 0.5f; // fraction of thumbnail width |
| 60 | // beyond which swipe progress->0 |
| 61 | private float mMinSwipeProgress = 0f; |
| 62 | private float mMaxSwipeProgress = 1f; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 63 | |
Mady Mellor | 2879631 | 2016-03-08 14:12:42 -0800 | [diff] [blame] | 64 | private FlingAnimationUtils mFlingAnimationUtils; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 65 | private float mPagingTouchSlop; |
| 66 | private Callback mCallback; |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 67 | private Handler mHandler; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 68 | private int mSwipeDirection; |
| 69 | private VelocityTracker mVelocityTracker; |
Blazej Magnowski | 6dc59b4 | 2015-09-22 15:14:20 -0700 | [diff] [blame] | 70 | private FalsingManager mFalsingManager; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 71 | |
| 72 | private float mInitialTouchPos; |
Jorim Jaggi | 9b0a2c9 | 2016-01-26 18:34:13 -0800 | [diff] [blame] | 73 | private float mPerpendicularInitialTouchPos; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 74 | private boolean mDragging; |
dongwan0605.kim | 30637e4 | 2016-03-02 17:16:47 +0900 | [diff] [blame] | 75 | private boolean mSnappingChild; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 76 | private View mCurrView; |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 77 | private boolean mCanCurrViewBeDimissed; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 78 | private float mDensityScale; |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 79 | private float mTranslation = 0; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 80 | |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 81 | private boolean mLongPressSent; |
Dan Sandler | 4247a5c | 2014-07-23 15:58:08 -0400 | [diff] [blame] | 82 | private LongPressListener mLongPressListener; |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 83 | private Runnable mWatchLongPress; |
Daniel Sandler | 469e96e | 2012-05-04 15:56:19 -0400 | [diff] [blame] | 84 | private long mLongPressTimeout; |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 85 | |
Dan Sandler | 4247a5c | 2014-07-23 15:58:08 -0400 | [diff] [blame] | 86 | final private int[] mTmpPos = new int[2]; |
Selim Cinek | 19c8c70 | 2014-08-25 22:09:19 +0200 | [diff] [blame] | 87 | private int mFalsingThreshold; |
| 88 | private boolean mTouchAboveFalsingThreshold; |
Winson | 671e8f9 | 2016-01-12 13:16:56 -0800 | [diff] [blame] | 89 | private boolean mDisableHwLayers; |
Dan Sandler | 4247a5c | 2014-07-23 15:58:08 -0400 | [diff] [blame] | 90 | |
dongwan0605.kim | 30637e4 | 2016-03-02 17:16:47 +0900 | [diff] [blame] | 91 | private HashMap<View, Animator> mDismissPendingMap = new HashMap<>(); |
| 92 | |
Dan Sandler | eceda3d | 2014-07-21 15:35:01 -0400 | [diff] [blame] | 93 | public SwipeHelper(int swipeDirection, Callback callback, Context context) { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 94 | mCallback = callback; |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 95 | mHandler = new Handler(); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 96 | mSwipeDirection = swipeDirection; |
| 97 | mVelocityTracker = VelocityTracker.obtain(); |
Dan Sandler | eceda3d | 2014-07-21 15:35:01 -0400 | [diff] [blame] | 98 | mDensityScale = context.getResources().getDisplayMetrics().density; |
| 99 | mPagingTouchSlop = ViewConfiguration.get(context).getScaledPagingTouchSlop(); |
Daniel Sandler | 469e96e | 2012-05-04 15:56:19 -0400 | [diff] [blame] | 100 | |
| 101 | mLongPressTimeout = (long) (ViewConfiguration.getLongPressTimeout() * 1.5f); // extra long-press! |
Selim Cinek | 19c8c70 | 2014-08-25 22:09:19 +0200 | [diff] [blame] | 102 | mFalsingThreshold = context.getResources().getDimensionPixelSize( |
| 103 | R.dimen.swipe_helper_falsing_threshold); |
Blazej Magnowski | 6dc59b4 | 2015-09-22 15:14:20 -0700 | [diff] [blame] | 104 | mFalsingManager = FalsingManager.getInstance(context); |
Mady Mellor | 2879631 | 2016-03-08 14:12:42 -0800 | [diff] [blame] | 105 | mFlingAnimationUtils = new FlingAnimationUtils(context, |
| 106 | MAX_ESCAPE_ANIMATION_DURATION / 1000f /* maxLengthSeconds */); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Dan Sandler | 4247a5c | 2014-07-23 15:58:08 -0400 | [diff] [blame] | 109 | public void setLongPressListener(LongPressListener listener) { |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 110 | mLongPressListener = listener; |
| 111 | } |
| 112 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 113 | public void setDensityScale(float densityScale) { |
| 114 | mDensityScale = densityScale; |
| 115 | } |
| 116 | |
| 117 | public void setPagingTouchSlop(float pagingTouchSlop) { |
| 118 | mPagingTouchSlop = pagingTouchSlop; |
| 119 | } |
| 120 | |
Winson | 671e8f9 | 2016-01-12 13:16:56 -0800 | [diff] [blame] | 121 | public void setDisableHardwareLayers(boolean disableHwLayers) { |
| 122 | mDisableHwLayers = disableHwLayers; |
| 123 | } |
| 124 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 125 | private float getPos(MotionEvent ev) { |
| 126 | return mSwipeDirection == X ? ev.getX() : ev.getY(); |
| 127 | } |
| 128 | |
Jorim Jaggi | 9b0a2c9 | 2016-01-26 18:34:13 -0800 | [diff] [blame] | 129 | private float getPerpendicularPos(MotionEvent ev) { |
| 130 | return mSwipeDirection == X ? ev.getY() : ev.getX(); |
| 131 | } |
| 132 | |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 133 | protected float getTranslation(View v) { |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 134 | return mSwipeDirection == X ? v.getTranslationX() : v.getTranslationY(); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | private float getVelocity(VelocityTracker vt) { |
| 138 | return mSwipeDirection == X ? vt.getXVelocity() : |
| 139 | vt.getYVelocity(); |
| 140 | } |
| 141 | |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 142 | protected ObjectAnimator createTranslationAnimation(View v, float newPos) { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 143 | ObjectAnimator anim = ObjectAnimator.ofFloat(v, |
Winson | c5fd350 | 2016-01-18 15:18:37 -0800 | [diff] [blame] | 144 | mSwipeDirection == X ? View.TRANSLATION_X : View.TRANSLATION_Y, newPos); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 145 | return anim; |
| 146 | } |
| 147 | |
| 148 | private float getPerpendicularVelocity(VelocityTracker vt) { |
| 149 | return mSwipeDirection == X ? vt.getYVelocity() : |
| 150 | vt.getXVelocity(); |
| 151 | } |
| 152 | |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 153 | protected Animator getViewTranslationAnimator(View v, float target, |
| 154 | AnimatorUpdateListener listener) { |
| 155 | ObjectAnimator anim = createTranslationAnimation(v, target); |
Mady Mellor | 34958fa | 2016-02-23 09:52:17 -0800 | [diff] [blame] | 156 | if (listener != null) { |
| 157 | anim.addUpdateListener(listener); |
| 158 | } |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 159 | return anim; |
| 160 | } |
| 161 | |
| 162 | protected void setTranslation(View v, float translate) { |
| 163 | if (v == null) { |
| 164 | return; |
| 165 | } |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 166 | if (mSwipeDirection == X) { |
| 167 | v.setTranslationX(translate); |
| 168 | } else { |
| 169 | v.setTranslationY(translate); |
| 170 | } |
| 171 | } |
| 172 | |
Winson | 671e8f9 | 2016-01-12 13:16:56 -0800 | [diff] [blame] | 173 | protected float getSize(View v) { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 174 | return mSwipeDirection == X ? v.getMeasuredWidth() : |
| 175 | v.getMeasuredHeight(); |
| 176 | } |
| 177 | |
Adrian Roos | 5d9cc66 | 2014-05-28 17:08:13 +0200 | [diff] [blame] | 178 | public void setMinSwipeProgress(float minSwipeProgress) { |
| 179 | mMinSwipeProgress = minSwipeProgress; |
Michael Jurka | 4eaa983 | 2012-02-29 15:51:49 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Adrian Roos | 5d9cc66 | 2014-05-28 17:08:13 +0200 | [diff] [blame] | 182 | public void setMaxSwipeProgress(float maxSwipeProgress) { |
| 183 | mMaxSwipeProgress = maxSwipeProgress; |
Adrian Roos | de61fd7 | 2014-05-26 14:59:15 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Adrian Roos | 5d9cc66 | 2014-05-28 17:08:13 +0200 | [diff] [blame] | 186 | private float getSwipeProgressForOffset(View view) { |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 187 | float viewSize = getSize(view); |
Adrian Roos | 5d9cc66 | 2014-05-28 17:08:13 +0200 | [diff] [blame] | 188 | final float fadeSize = SWIPE_PROGRESS_FADE_END * viewSize; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 189 | float result = 1.0f; |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 190 | float pos = getTranslation(view); |
Adrian Roos | 5d9cc66 | 2014-05-28 17:08:13 +0200 | [diff] [blame] | 191 | if (pos >= viewSize * SWIPE_PROGRESS_FADE_START) { |
| 192 | result = 1.0f - (pos - viewSize * SWIPE_PROGRESS_FADE_START) / fadeSize; |
| 193 | } else if (pos < viewSize * (1.0f - SWIPE_PROGRESS_FADE_START)) { |
| 194 | result = 1.0f + (viewSize * SWIPE_PROGRESS_FADE_START + pos) / fadeSize; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 195 | } |
Adrian Roos | 5d9cc66 | 2014-05-28 17:08:13 +0200 | [diff] [blame] | 196 | return Math.min(Math.max(mMinSwipeProgress, result), mMaxSwipeProgress); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Adrian Roos | 5d9cc66 | 2014-05-28 17:08:13 +0200 | [diff] [blame] | 199 | private void updateSwipeProgressFromOffset(View animView, boolean dismissable) { |
| 200 | float swipeProgress = getSwipeProgressForOffset(animView); |
| 201 | if (!mCallback.updateSwipeProgress(animView, dismissable, swipeProgress)) { |
| 202 | if (FADE_OUT_DURING_SWIPE && dismissable) { |
| 203 | float alpha = swipeProgress; |
Winson | 671e8f9 | 2016-01-12 13:16:56 -0800 | [diff] [blame] | 204 | if (!mDisableHwLayers) { |
| 205 | if (alpha != 0f && alpha != 1f) { |
| 206 | animView.setLayerType(View.LAYER_TYPE_HARDWARE, null); |
| 207 | } else { |
| 208 | animView.setLayerType(View.LAYER_TYPE_NONE, null); |
| 209 | } |
Adrian Roos | 5d9cc66 | 2014-05-28 17:08:13 +0200 | [diff] [blame] | 210 | } |
| 211 | animView.setAlpha(getSwipeProgressForOffset(animView)); |
Michael Jurka | 499293f | 2013-03-06 18:08:45 +0100 | [diff] [blame] | 212 | } |
Michael Jurka | 67b0370 | 2013-02-15 17:35:48 +0100 | [diff] [blame] | 213 | } |
| 214 | invalidateGlobalRegion(animView); |
| 215 | } |
| 216 | |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 217 | // invalidate the view's own bounds all the way up the view hierarchy |
| 218 | public static void invalidateGlobalRegion(View view) { |
| 219 | invalidateGlobalRegion( |
| 220 | view, |
| 221 | new RectF(view.getLeft(), view.getTop(), view.getRight(), view.getBottom())); |
| 222 | } |
| 223 | |
| 224 | // invalidate a rectangle relative to the view's coordinate system all the way up the view |
| 225 | // hierarchy |
| 226 | public static void invalidateGlobalRegion(View view, RectF childBounds) { |
Daniel Sandler | 96f4818 | 2011-08-17 09:50:35 -0400 | [diff] [blame] | 227 | //childBounds.offset(view.getTranslationX(), view.getTranslationY()); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 228 | if (DEBUG_INVALIDATE) |
| 229 | Log.v(TAG, "-------------"); |
| 230 | while (view.getParent() != null && view.getParent() instanceof View) { |
| 231 | view = (View) view.getParent(); |
| 232 | view.getMatrix().mapRect(childBounds); |
| 233 | view.invalidate((int) Math.floor(childBounds.left), |
| 234 | (int) Math.floor(childBounds.top), |
| 235 | (int) Math.ceil(childBounds.right), |
| 236 | (int) Math.ceil(childBounds.bottom)); |
| 237 | if (DEBUG_INVALIDATE) { |
| 238 | Log.v(TAG, "INVALIDATE(" + (int) Math.floor(childBounds.left) |
| 239 | + "," + (int) Math.floor(childBounds.top) |
| 240 | + "," + (int) Math.ceil(childBounds.right) |
| 241 | + "," + (int) Math.ceil(childBounds.bottom)); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
Daniel Sandler | 469e96e | 2012-05-04 15:56:19 -0400 | [diff] [blame] | 246 | public void removeLongPressCallback() { |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 247 | if (mWatchLongPress != null) { |
| 248 | mHandler.removeCallbacks(mWatchLongPress); |
Daniel Sandler | 491d3a9 | 2012-05-16 13:04:06 -0400 | [diff] [blame] | 249 | mWatchLongPress = null; |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Dan Sandler | 4247a5c | 2014-07-23 15:58:08 -0400 | [diff] [blame] | 253 | public boolean onInterceptTouchEvent(final MotionEvent ev) { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 254 | final int action = ev.getAction(); |
| 255 | |
| 256 | switch (action) { |
| 257 | case MotionEvent.ACTION_DOWN: |
Selim Cinek | 19c8c70 | 2014-08-25 22:09:19 +0200 | [diff] [blame] | 258 | mTouchAboveFalsingThreshold = false; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 259 | mDragging = false; |
dongwan0605.kim | 30637e4 | 2016-03-02 17:16:47 +0900 | [diff] [blame] | 260 | mSnappingChild = false; |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 261 | mLongPressSent = false; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 262 | mVelocityTracker.clear(); |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 263 | mCurrView = mCallback.getChildAtPosition(ev); |
| 264 | |
Michael Jurka | 21ce2d8 | 2011-09-02 15:28:06 -0700 | [diff] [blame] | 265 | if (mCurrView != null) { |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 266 | onDownUpdate(mCurrView); |
Michael Jurka | 21ce2d8 | 2011-09-02 15:28:06 -0700 | [diff] [blame] | 267 | mCanCurrViewBeDimissed = mCallback.canChildBeDismissed(mCurrView); |
| 268 | mVelocityTracker.addMovement(ev); |
| 269 | mInitialTouchPos = getPos(ev); |
Jorim Jaggi | 9b0a2c9 | 2016-01-26 18:34:13 -0800 | [diff] [blame] | 270 | mPerpendicularInitialTouchPos = getPerpendicularPos(ev); |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 271 | mTranslation = getTranslation(mCurrView); |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 272 | if (mLongPressListener != null) { |
| 273 | if (mWatchLongPress == null) { |
| 274 | mWatchLongPress = new Runnable() { |
| 275 | @Override |
| 276 | public void run() { |
Selim Cinek | 197823d | 2016-03-24 13:06:00 -0700 | [diff] [blame] | 277 | if (mCurrView != null && !mLongPressSent) { |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 278 | mLongPressSent = true; |
Dan Sandler | 4247a5c | 2014-07-23 15:58:08 -0400 | [diff] [blame] | 279 | mCurrView.sendAccessibilityEvent( |
| 280 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED); |
| 281 | mCurrView.getLocationOnScreen(mTmpPos); |
| 282 | final int x = (int) ev.getRawX() - mTmpPos[0]; |
| 283 | final int y = (int) ev.getRawY() - mTmpPos[1]; |
| 284 | mLongPressListener.onLongPress(mCurrView, x, y); |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | }; |
| 288 | } |
Daniel Sandler | 469e96e | 2012-05-04 15:56:19 -0400 | [diff] [blame] | 289 | mHandler.postDelayed(mWatchLongPress, mLongPressTimeout); |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 290 | } |
Michael Jurka | 21ce2d8 | 2011-09-02 15:28:06 -0700 | [diff] [blame] | 291 | } |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 292 | break; |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 293 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 294 | case MotionEvent.ACTION_MOVE: |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 295 | if (mCurrView != null && !mLongPressSent) { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 296 | mVelocityTracker.addMovement(ev); |
| 297 | float pos = getPos(ev); |
Jorim Jaggi | 9b0a2c9 | 2016-01-26 18:34:13 -0800 | [diff] [blame] | 298 | float perpendicularPos = getPerpendicularPos(ev); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 299 | float delta = pos - mInitialTouchPos; |
Jorim Jaggi | 9b0a2c9 | 2016-01-26 18:34:13 -0800 | [diff] [blame] | 300 | float deltaPerpendicular = perpendicularPos - mPerpendicularInitialTouchPos; |
| 301 | if (Math.abs(delta) > mPagingTouchSlop |
| 302 | && Math.abs(delta) > Math.abs(deltaPerpendicular)) { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 303 | mCallback.onBeginDrag(mCurrView); |
| 304 | mDragging = true; |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 305 | mInitialTouchPos = getPos(ev); |
| 306 | mTranslation = getTranslation(mCurrView); |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 307 | removeLongPressCallback(); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | break; |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 311 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 312 | case MotionEvent.ACTION_UP: |
Jeff Brown | 68ebcdf | 2011-09-12 14:12:17 -0700 | [diff] [blame] | 313 | case MotionEvent.ACTION_CANCEL: |
Dan Sandler | 4247a5c | 2014-07-23 15:58:08 -0400 | [diff] [blame] | 314 | final boolean captured = (mDragging || mLongPressSent); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 315 | mDragging = false; |
| 316 | mCurrView = null; |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 317 | mLongPressSent = false; |
Daniel Sandler | 491d3a9 | 2012-05-16 13:04:06 -0400 | [diff] [blame] | 318 | removeLongPressCallback(); |
Dan Sandler | 4247a5c | 2014-07-23 15:58:08 -0400 | [diff] [blame] | 319 | if (captured) return true; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 320 | break; |
| 321 | } |
Dan Sandler | 4247a5c | 2014-07-23 15:58:08 -0400 | [diff] [blame] | 322 | return mDragging || mLongPressSent; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Chet Haase | 2f2022a | 2011-10-11 06:41:59 -0700 | [diff] [blame] | 325 | /** |
| 326 | * @param view The view to be dismissed |
| 327 | * @param velocity The desired pixels/second speed at which the view should move |
| 328 | */ |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 329 | public void dismissChild(final View view, float velocity) { |
Mady Mellor | 2879631 | 2016-03-08 14:12:42 -0800 | [diff] [blame] | 330 | dismissChild(view, velocity, null /* endAction */, 0 /* delay */, |
| 331 | velocity == 0 /* useAccelerateInterpolator */, 0 /* fixedDuration */); |
Dan Sandler | eceda3d | 2014-07-21 15:35:01 -0400 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /** |
| 335 | * @param view The view to be dismissed |
| 336 | * @param velocity The desired pixels/second speed at which the view should move |
| 337 | * @param endAction The action to perform at the end |
| 338 | * @param delay The delay after which we should start |
| 339 | * @param useAccelerateInterpolator Should an accelerating Interpolator be used |
| 340 | * @param fixedDuration If not 0, this exact duration will be taken |
| 341 | */ |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 342 | public void dismissChild(final View animView, float velocity, final Runnable endAction, |
Dan Sandler | eceda3d | 2014-07-21 15:35:01 -0400 | [diff] [blame] | 343 | long delay, boolean useAccelerateInterpolator, long fixedDuration) { |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 344 | final boolean canBeDismissed = mCallback.canChildBeDismissed(animView); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 345 | float newPos; |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 346 | boolean isLayoutRtl = animView.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; |
Michael Jurka | c6461ca | 2011-09-02 12:12:15 -0700 | [diff] [blame] | 347 | |
| 348 | if (velocity < 0 |
| 349 | || (velocity == 0 && getTranslation(animView) < 0) |
| 350 | // if we use the Menu to dismiss an item in landscape, animate up |
Selim Cinek | 1d59af4 | 2014-10-30 21:04:38 +0100 | [diff] [blame] | 351 | || (velocity == 0 && getTranslation(animView) == 0 && mSwipeDirection == Y) |
| 352 | // if the language is rtl we prefer swiping to the left |
| 353 | || (velocity == 0 && getTranslation(animView) == 0 && isLayoutRtl)) { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 354 | newPos = -getSize(animView); |
| 355 | } else { |
| 356 | newPos = getSize(animView); |
| 357 | } |
Dan Sandler | eceda3d | 2014-07-21 15:35:01 -0400 | [diff] [blame] | 358 | long duration; |
| 359 | if (fixedDuration == 0) { |
| 360 | duration = MAX_ESCAPE_ANIMATION_DURATION; |
| 361 | if (velocity != 0) { |
| 362 | duration = Math.min(duration, |
| 363 | (int) (Math.abs(newPos - getTranslation(animView)) * 1000f / Math |
| 364 | .abs(velocity)) |
| 365 | ); |
| 366 | } else { |
| 367 | duration = DEFAULT_ESCAPE_ANIMATION_DURATION; |
| 368 | } |
Michael Jurka | 0e8063a | 2011-09-09 15:31:55 -0700 | [diff] [blame] | 369 | } else { |
Dan Sandler | eceda3d | 2014-07-21 15:35:01 -0400 | [diff] [blame] | 370 | duration = fixedDuration; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 371 | } |
Michael Jurka | 0e8063a | 2011-09-09 15:31:55 -0700 | [diff] [blame] | 372 | |
Winson | 671e8f9 | 2016-01-12 13:16:56 -0800 | [diff] [blame] | 373 | if (!mDisableHwLayers) { |
| 374 | animView.setLayerType(View.LAYER_TYPE_HARDWARE, null); |
| 375 | } |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 376 | AnimatorUpdateListener updateListener = new AnimatorUpdateListener() { |
| 377 | public void onAnimationUpdate(ValueAnimator animation) { |
| 378 | onTranslationUpdate(animView, (float) animation.getAnimatedValue(), canBeDismissed); |
| 379 | } |
| 380 | }; |
| 381 | |
| 382 | Animator anim = getViewTranslationAnimator(animView, newPos, updateListener); |
Mady Mellor | 34958fa | 2016-02-23 09:52:17 -0800 | [diff] [blame] | 383 | if (anim == null) { |
| 384 | return; |
| 385 | } |
Dan Sandler | eceda3d | 2014-07-21 15:35:01 -0400 | [diff] [blame] | 386 | if (useAccelerateInterpolator) { |
Selim Cinek | c18010f | 2016-01-20 13:41:30 -0800 | [diff] [blame] | 387 | anim.setInterpolator(Interpolators.FAST_OUT_LINEAR_IN); |
Mady Mellor | 2879631 | 2016-03-08 14:12:42 -0800 | [diff] [blame] | 388 | anim.setDuration(duration); |
Dan Sandler | eceda3d | 2014-07-21 15:35:01 -0400 | [diff] [blame] | 389 | } else { |
Mady Mellor | 2879631 | 2016-03-08 14:12:42 -0800 | [diff] [blame] | 390 | mFlingAnimationUtils.applyDismissing(anim, getTranslation(animView), |
| 391 | newPos, velocity, getSize(animView)); |
Dan Sandler | eceda3d | 2014-07-21 15:35:01 -0400 | [diff] [blame] | 392 | } |
Dan Sandler | eceda3d | 2014-07-21 15:35:01 -0400 | [diff] [blame] | 393 | if (delay > 0) { |
| 394 | anim.setStartDelay(delay); |
| 395 | } |
Chet Haase | 2f2022a | 2011-10-11 06:41:59 -0700 | [diff] [blame] | 396 | anim.addListener(new AnimatorListenerAdapter() { |
dongwan0605.kim | 30637e4 | 2016-03-02 17:16:47 +0900 | [diff] [blame] | 397 | private boolean mCancelled; |
| 398 | |
| 399 | public void onAnimationCancel(Animator animation) { |
| 400 | mCancelled = true; |
| 401 | } |
| 402 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 403 | public void onAnimationEnd(Animator animation) { |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 404 | updateSwipeProgressFromOffset(animView, canBeDismissed); |
dongwan0605.kim | 30637e4 | 2016-03-02 17:16:47 +0900 | [diff] [blame] | 405 | mDismissPendingMap.remove(animView); |
| 406 | if (!mCancelled) { |
| 407 | mCallback.onChildDismissed(animView); |
| 408 | } |
Dan Sandler | eceda3d | 2014-07-21 15:35:01 -0400 | [diff] [blame] | 409 | if (endAction != null) { |
| 410 | endAction.run(); |
| 411 | } |
Winson | 671e8f9 | 2016-01-12 13:16:56 -0800 | [diff] [blame] | 412 | if (!mDisableHwLayers) { |
| 413 | animView.setLayerType(View.LAYER_TYPE_NONE, null); |
| 414 | } |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 415 | } |
| 416 | }); |
dongwan0605.kim | 30637e4 | 2016-03-02 17:16:47 +0900 | [diff] [blame] | 417 | |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 418 | prepareDismissAnimation(animView, anim); |
dongwan0605.kim | 30637e4 | 2016-03-02 17:16:47 +0900 | [diff] [blame] | 419 | mDismissPendingMap.put(animView, anim); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 420 | anim.start(); |
| 421 | } |
| 422 | |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 423 | /** |
| 424 | * Called to update the dismiss animation. |
| 425 | */ |
| 426 | protected void prepareDismissAnimation(View view, Animator anim) { |
| 427 | // Do nothing |
| 428 | } |
| 429 | |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 430 | public void snapChild(final View animView, final float targetLeft, float velocity) { |
| 431 | final boolean canBeDismissed = mCallback.canChildBeDismissed(animView); |
| 432 | AnimatorUpdateListener updateListener = new AnimatorUpdateListener() { |
| 433 | public void onAnimationUpdate(ValueAnimator animation) { |
| 434 | onTranslationUpdate(animView, (float) animation.getAnimatedValue(), canBeDismissed); |
| 435 | } |
| 436 | }; |
| 437 | |
| 438 | Animator anim = getViewTranslationAnimator(animView, targetLeft, updateListener); |
Mady Mellor | 34958fa | 2016-02-23 09:52:17 -0800 | [diff] [blame] | 439 | if (anim == null) { |
| 440 | return; |
| 441 | } |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 442 | int duration = SNAP_ANIM_LEN; |
| 443 | anim.setDuration(duration); |
Michael Jurka | 67b0370 | 2013-02-15 17:35:48 +0100 | [diff] [blame] | 444 | anim.addListener(new AnimatorListenerAdapter() { |
| 445 | public void onAnimationEnd(Animator animator) { |
dongwan0605.kim | 30637e4 | 2016-03-02 17:16:47 +0900 | [diff] [blame] | 446 | mSnappingChild = false; |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 447 | updateSwipeProgressFromOffset(animView, canBeDismissed); |
| 448 | mCallback.onChildSnappedBack(animView, targetLeft); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 449 | } |
| 450 | }); |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 451 | prepareSnapBackAnimation(animView, anim); |
dongwan0605.kim | 30637e4 | 2016-03-02 17:16:47 +0900 | [diff] [blame] | 452 | mSnappingChild = true; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 453 | anim.start(); |
| 454 | } |
| 455 | |
Winson | c5fd350 | 2016-01-18 15:18:37 -0800 | [diff] [blame] | 456 | /** |
| 457 | * Called to update the snap back animation. |
| 458 | */ |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 459 | protected void prepareSnapBackAnimation(View view, Animator anim) { |
Winson | c5fd350 | 2016-01-18 15:18:37 -0800 | [diff] [blame] | 460 | // Do nothing |
| 461 | } |
| 462 | |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 463 | /** |
| 464 | * Called when there's a down event. |
| 465 | */ |
| 466 | public void onDownUpdate(View currView) { |
| 467 | // Do nothing |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Called on a move event. |
| 472 | */ |
| 473 | protected void onMoveUpdate(View view, float totalTranslation, float delta) { |
| 474 | // Do nothing |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Called in {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)} when the current |
| 479 | * view is being animated to dismiss or snap. |
| 480 | */ |
| 481 | public void onTranslationUpdate(View animView, float value, boolean canBeDismissed) { |
| 482 | updateSwipeProgressFromOffset(animView, canBeDismissed); |
| 483 | } |
| 484 | |
dongwan0605.kim | 30637e4 | 2016-03-02 17:16:47 +0900 | [diff] [blame] | 485 | private void snapChildInstantly(final View view) { |
| 486 | final boolean canAnimViewBeDismissed = mCallback.canChildBeDismissed(view); |
| 487 | setTranslation(view, 0); |
| 488 | updateSwipeProgressFromOffset(view, canAnimViewBeDismissed); |
| 489 | } |
| 490 | |
| 491 | public void snapChildIfNeeded(final View view, boolean animate) { |
| 492 | if ((mDragging && mCurrView == view) || mSnappingChild) { |
| 493 | return; |
| 494 | } |
| 495 | boolean needToSnap = false; |
| 496 | Animator dismissPendingAnim = mDismissPendingMap.get(view); |
| 497 | if (dismissPendingAnim != null) { |
| 498 | needToSnap = true; |
| 499 | dismissPendingAnim.cancel(); |
| 500 | } else if (getTranslation(view) != 0) { |
| 501 | needToSnap = true; |
| 502 | } |
| 503 | if (needToSnap) { |
| 504 | if (animate) { |
| 505 | snapChild(view, 0 /* targetLeft */, 0.0f /* velocity */); |
| 506 | } else { |
| 507 | snapChildInstantly(view); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 512 | public boolean onTouchEvent(MotionEvent ev) { |
Daniel Sandler | f7a1956 | 2012-04-04 14:04:21 -0400 | [diff] [blame] | 513 | if (mLongPressSent) { |
| 514 | return true; |
| 515 | } |
| 516 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 517 | if (!mDragging) { |
Jorim Jaggi | 28f0e59 | 2014-08-05 22:03:07 +0200 | [diff] [blame] | 518 | if (mCallback.getChildAtPosition(ev) != null) { |
| 519 | |
| 520 | // We are dragging directly over a card, make sure that we also catch the gesture |
| 521 | // even if nobody else wants the touch event. |
| 522 | onInterceptTouchEvent(ev); |
| 523 | return true; |
| 524 | } else { |
| 525 | |
| 526 | // We are not doing anything, make sure the long press callback |
| 527 | // is not still ticking like a bomb waiting to go off. |
| 528 | removeLongPressCallback(); |
| 529 | return false; |
| 530 | } |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | mVelocityTracker.addMovement(ev); |
| 534 | final int action = ev.getAction(); |
| 535 | switch (action) { |
| 536 | case MotionEvent.ACTION_OUTSIDE: |
| 537 | case MotionEvent.ACTION_MOVE: |
| 538 | if (mCurrView != null) { |
| 539 | float delta = getPos(ev) - mInitialTouchPos; |
Selim Cinek | 19c8c70 | 2014-08-25 22:09:19 +0200 | [diff] [blame] | 540 | float absDelta = Math.abs(delta); |
Selim Cinek | 34cf5c4 | 2014-09-26 15:39:00 +0200 | [diff] [blame] | 541 | if (absDelta >= getFalsingThreshold()) { |
Selim Cinek | 19c8c70 | 2014-08-25 22:09:19 +0200 | [diff] [blame] | 542 | mTouchAboveFalsingThreshold = true; |
| 543 | } |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 544 | // don't let items that can't be dismissed be dragged more than |
| 545 | // maxScrollDistance |
| 546 | if (CONSTRAIN_SWIPE && !mCallback.canChildBeDismissed(mCurrView)) { |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 547 | float size = getSize(mCurrView); |
| 548 | float maxScrollDistance = 0.25f * size; |
Selim Cinek | 19c8c70 | 2014-08-25 22:09:19 +0200 | [diff] [blame] | 549 | if (absDelta >= size) { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 550 | delta = delta > 0 ? maxScrollDistance : -maxScrollDistance; |
| 551 | } else { |
| 552 | delta = maxScrollDistance * (float) Math.sin((delta/size)*(Math.PI/2)); |
| 553 | } |
| 554 | } |
Michael Jurka | 67b0370 | 2013-02-15 17:35:48 +0100 | [diff] [blame] | 555 | |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 556 | setTranslation(mCurrView, mTranslation + delta); |
| 557 | updateSwipeProgressFromOffset(mCurrView, mCanCurrViewBeDimissed); |
| 558 | onMoveUpdate(mCurrView, mTranslation + delta, delta); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 559 | } |
| 560 | break; |
| 561 | case MotionEvent.ACTION_UP: |
| 562 | case MotionEvent.ACTION_CANCEL: |
Mady Mellor | 3a5e8dd | 2016-03-12 00:13:23 +0000 | [diff] [blame] | 563 | if (mCurrView == null) { |
| 564 | break; |
| 565 | } |
| 566 | mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, getMaxVelocity()); |
| 567 | float velocity = getVelocity(mVelocityTracker); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 568 | |
Mady Mellor | 3a5e8dd | 2016-03-12 00:13:23 +0000 | [diff] [blame] | 569 | if (!handleUpEvent(ev, mCurrView, velocity, getTranslation(mCurrView))) { |
| 570 | if (isDismissGesture(ev)) { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 571 | // flingadingy |
Mady Mellor | 3a5e8dd | 2016-03-12 00:13:23 +0000 | [diff] [blame] | 572 | dismissChild(mCurrView, swipedFastEnough() ? velocity : 0f); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 573 | } else { |
| 574 | // snappity |
Peter Ng | 622a976 | 2011-08-29 10:56:53 -0700 | [diff] [blame] | 575 | mCallback.onDragCancelled(mCurrView); |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 576 | snapChild(mCurrView, 0 /* leftTarget */, velocity); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 577 | } |
dongwan0605.kim | 30637e4 | 2016-03-02 17:16:47 +0900 | [diff] [blame] | 578 | mCurrView = null; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 579 | } |
dongwan0605.kim | 30637e4 | 2016-03-02 17:16:47 +0900 | [diff] [blame] | 580 | mDragging = false; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 581 | break; |
| 582 | } |
| 583 | return true; |
| 584 | } |
| 585 | |
Selim Cinek | 34cf5c4 | 2014-09-26 15:39:00 +0200 | [diff] [blame] | 586 | private int getFalsingThreshold() { |
| 587 | float factor = mCallback.getFalsingThresholdFactor(); |
| 588 | return (int) (mFalsingThreshold * factor); |
| 589 | } |
| 590 | |
Mady Mellor | 3a5e8dd | 2016-03-12 00:13:23 +0000 | [diff] [blame] | 591 | private float getMaxVelocity() { |
| 592 | return MAX_DISMISS_VELOCITY * mDensityScale; |
| 593 | } |
| 594 | |
| 595 | protected float getEscapeVelocity() { |
| 596 | return SWIPE_ESCAPE_VELOCITY * mDensityScale; |
| 597 | } |
| 598 | |
| 599 | protected boolean swipedFarEnough() { |
| 600 | float translation = getTranslation(mCurrView); |
| 601 | return DISMISS_IF_SWIPED_FAR_ENOUGH && Math.abs(translation) > 0.4 * getSize(mCurrView); |
| 602 | } |
| 603 | |
| 604 | protected boolean isDismissGesture(MotionEvent ev) { |
| 605 | boolean falsingDetected = mCallback.isAntiFalsingNeeded(); |
| 606 | if (mFalsingManager.isClassiferEnabled()) { |
| 607 | falsingDetected = falsingDetected && mFalsingManager.isFalseTouch(); |
| 608 | } else { |
| 609 | falsingDetected = falsingDetected && !mTouchAboveFalsingThreshold; |
| 610 | } |
| 611 | return !falsingDetected && (swipedFastEnough() || swipedFarEnough()) |
| 612 | && ev.getActionMasked() == MotionEvent.ACTION_UP |
| 613 | && mCallback.canChildBeDismissed(mCurrView); |
| 614 | } |
| 615 | |
| 616 | protected boolean swipedFastEnough() { |
| 617 | float velocity = getVelocity(mVelocityTracker); |
| 618 | float perpendicularVelocity = getPerpendicularVelocity(mVelocityTracker); |
| 619 | float translation = getTranslation(mCurrView); |
| 620 | boolean ret = (Math.abs(velocity) > getEscapeVelocity()) && |
| 621 | (Math.abs(velocity) > Math.abs(perpendicularVelocity)) && |
| 622 | (velocity > 0) == (translation > 0); |
| 623 | return ret; |
| 624 | } |
| 625 | |
| 626 | protected boolean handleUpEvent(MotionEvent ev, View animView, float velocity, |
| 627 | float translation) { |
| 628 | return false; |
| 629 | } |
| 630 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 631 | public interface Callback { |
| 632 | View getChildAtPosition(MotionEvent ev); |
| 633 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 634 | boolean canChildBeDismissed(View v); |
| 635 | |
Selim Cinek | 19c8c70 | 2014-08-25 22:09:19 +0200 | [diff] [blame] | 636 | boolean isAntiFalsingNeeded(); |
| 637 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 638 | void onBeginDrag(View v); |
| 639 | |
| 640 | void onChildDismissed(View v); |
Peter Ng | 622a976 | 2011-08-29 10:56:53 -0700 | [diff] [blame] | 641 | |
| 642 | void onDragCancelled(View v); |
Selim Cinek | eb97356 | 2014-05-02 17:07:49 +0200 | [diff] [blame] | 643 | |
Mady Mellor | 4b80b10 | 2016-01-22 08:03:58 -0800 | [diff] [blame] | 644 | /** |
| 645 | * Called when the child is snapped to a position. |
| 646 | * |
| 647 | * @param animView the view that was snapped. |
| 648 | * @param targetLeft the left position the view was snapped to. |
| 649 | */ |
| 650 | void onChildSnappedBack(View animView, float targetLeft); |
Adrian Roos | 5d9cc66 | 2014-05-28 17:08:13 +0200 | [diff] [blame] | 651 | |
| 652 | /** |
| 653 | * Updates the swipe progress on a child. |
| 654 | * |
| 655 | * @return if true, prevents the default alpha fading. |
| 656 | */ |
| 657 | boolean updateSwipeProgress(View animView, boolean dismissable, float swipeProgress); |
Selim Cinek | 34cf5c4 | 2014-09-26 15:39:00 +0200 | [diff] [blame] | 658 | |
| 659 | /** |
| 660 | * @return The factor the falsing threshold should be multiplied with |
| 661 | */ |
| 662 | float getFalsingThresholdFactor(); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 663 | } |
Dan Sandler | 4247a5c | 2014-07-23 15:58:08 -0400 | [diff] [blame] | 664 | |
| 665 | /** |
| 666 | * Equivalent to View.OnLongClickListener with coordinates |
| 667 | */ |
| 668 | public interface LongPressListener { |
| 669 | /** |
| 670 | * Equivalent to {@link View.OnLongClickListener#onLongClick(View)} with coordinates |
| 671 | * @return whether the longpress was handled |
| 672 | */ |
| 673 | boolean onLongPress(View v, int x, int y); |
| 674 | } |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 675 | } |