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