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