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