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