Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.statusbar.phone; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.ValueAnimator; |
| 22 | import android.content.Context; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 23 | import android.view.MotionEvent; |
| 24 | import android.view.VelocityTracker; |
| 25 | import android.view.View; |
| 26 | import android.view.ViewConfiguration; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 27 | |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 28 | import com.android.systemui.Interpolators; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 29 | import com.android.systemui.R; |
Dave Mankoff | 468d4f6 | 2019-05-08 14:56:29 -0400 | [diff] [blame] | 30 | import com.android.systemui.plugins.FalsingManager; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 31 | import com.android.systemui.statusbar.FlingAnimationUtils; |
| 32 | import com.android.systemui.statusbar.KeyguardAffordanceView; |
| 33 | |
| 34 | /** |
| 35 | * A touch handler of the keyguard which is responsible for launching phone and camera affordances. |
| 36 | */ |
| 37 | public class KeyguardAffordanceHelper { |
| 38 | |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 39 | public static final long HINT_PHASE1_DURATION = 200; |
| 40 | private static final long HINT_PHASE2_DURATION = 350; |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 41 | private static final float BACKGROUND_RADIUS_SCALE_FACTOR = 0.25f; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 42 | private static final int HINT_CIRCLE_OPEN_DURATION = 500; |
| 43 | |
| 44 | private final Context mContext; |
Adrian Roos | 6a70b88 | 2016-04-19 17:09:07 -0700 | [diff] [blame] | 45 | private final Callback mCallback; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 46 | |
| 47 | private FlingAnimationUtils mFlingAnimationUtils; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 48 | private VelocityTracker mVelocityTracker; |
| 49 | private boolean mSwipingInProgress; |
| 50 | private float mInitialTouchX; |
| 51 | private float mInitialTouchY; |
| 52 | private float mTranslation; |
| 53 | private float mTranslationOnDown; |
| 54 | private int mTouchSlop; |
| 55 | private int mMinTranslationAmount; |
| 56 | private int mMinFlingVelocity; |
| 57 | private int mHintGrowAmount; |
Jorim Jaggi | ef067fb | 2014-09-01 16:44:08 +0200 | [diff] [blame] | 58 | private KeyguardAffordanceView mLeftIcon; |
Jorim Jaggi | ef067fb | 2014-09-01 16:44:08 +0200 | [diff] [blame] | 59 | private KeyguardAffordanceView mRightIcon; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 60 | private Animator mSwipeAnimator; |
Dave Mankoff | c195ea8 | 2019-06-28 16:33:25 -0400 | [diff] [blame] | 61 | private final FalsingManager mFalsingManager; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 62 | private int mMinBackgroundRadius; |
Selim Cinek | 1dc4065 | 2014-11-12 17:53:52 +0100 | [diff] [blame] | 63 | private boolean mMotionCancelled; |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 64 | private int mTouchTargetSize; |
| 65 | private View mTargetedView; |
| 66 | private boolean mTouchSlopExeeded; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 67 | private AnimatorListenerAdapter mFlingEndListener = new AnimatorListenerAdapter() { |
| 68 | @Override |
| 69 | public void onAnimationEnd(Animator animation) { |
| 70 | mSwipeAnimator = null; |
Jorim Jaggi | a86790b | 2015-04-02 16:32:29 -0700 | [diff] [blame] | 71 | mSwipingInProgress = false; |
Selim Cinek | 158af6a | 2015-07-06 18:39:28 -0700 | [diff] [blame] | 72 | mTargetedView = null; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 73 | } |
| 74 | }; |
| 75 | private Runnable mAnimationEndRunnable = new Runnable() { |
| 76 | @Override |
| 77 | public void run() { |
| 78 | mCallback.onAnimationToSideEnded(); |
| 79 | } |
| 80 | }; |
| 81 | |
Dave Mankoff | c195ea8 | 2019-06-28 16:33:25 -0400 | [diff] [blame] | 82 | KeyguardAffordanceHelper(Callback callback, Context context, FalsingManager falsingManager) { |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 83 | mContext = context; |
| 84 | mCallback = callback; |
Jorim Jaggi | ef067fb | 2014-09-01 16:44:08 +0200 | [diff] [blame] | 85 | initIcons(); |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 86 | updateIcon(mLeftIcon, 0.0f, mLeftIcon.getRestingAlpha(), false, false, true, false); |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 87 | updateIcon(mRightIcon, 0.0f, mRightIcon.getRestingAlpha(), false, false, true, false); |
Dave Mankoff | c195ea8 | 2019-06-28 16:33:25 -0400 | [diff] [blame] | 88 | mFalsingManager = falsingManager; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 89 | initDimens(); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | private void initDimens() { |
| 93 | final ViewConfiguration configuration = ViewConfiguration.get(mContext); |
Jorim Jaggi | 28f0e59 | 2014-08-05 22:03:07 +0200 | [diff] [blame] | 94 | mTouchSlop = configuration.getScaledPagingTouchSlop(); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 95 | mMinFlingVelocity = configuration.getScaledMinimumFlingVelocity(); |
| 96 | mMinTranslationAmount = mContext.getResources().getDimensionPixelSize( |
| 97 | R.dimen.keyguard_min_swipe_amount); |
| 98 | mMinBackgroundRadius = mContext.getResources().getDimensionPixelSize( |
| 99 | R.dimen.keyguard_affordance_min_background_radius); |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 100 | mTouchTargetSize = mContext.getResources().getDimensionPixelSize( |
| 101 | R.dimen.keyguard_affordance_touch_target_size); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 102 | mHintGrowAmount = |
| 103 | mContext.getResources().getDimensionPixelSize(R.dimen.hint_grow_amount_sideways); |
Dave Mankoff | 1373fdb | 2019-12-18 14:04:37 -0500 | [diff] [blame] | 104 | mFlingAnimationUtils = new FlingAnimationUtils(mContext.getResources().getDisplayMetrics(), |
| 105 | 0.4f); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Jorim Jaggi | ef067fb | 2014-09-01 16:44:08 +0200 | [diff] [blame] | 108 | private void initIcons() { |
| 109 | mLeftIcon = mCallback.getLeftIcon(); |
Jorim Jaggi | ef067fb | 2014-09-01 16:44:08 +0200 | [diff] [blame] | 110 | mRightIcon = mCallback.getRightIcon(); |
Selim Cinek | e70d653 | 2015-04-24 16:46:13 -0700 | [diff] [blame] | 111 | updatePreviews(); |
| 112 | } |
| 113 | |
| 114 | public void updatePreviews() { |
Jorim Jaggi | ef067fb | 2014-09-01 16:44:08 +0200 | [diff] [blame] | 115 | mLeftIcon.setPreviewView(mCallback.getLeftPreview()); |
| 116 | mRightIcon.setPreviewView(mCallback.getRightPreview()); |
| 117 | } |
| 118 | |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 119 | public boolean onTouchEvent(MotionEvent event) { |
Jorim Jaggi | a86790b | 2015-04-02 16:32:29 -0700 | [diff] [blame] | 120 | int action = event.getActionMasked(); |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 121 | if (mMotionCancelled && action != MotionEvent.ACTION_DOWN) { |
Selim Cinek | 1dc4065 | 2014-11-12 17:53:52 +0100 | [diff] [blame] | 122 | return false; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 123 | } |
Selim Cinek | 1dc4065 | 2014-11-12 17:53:52 +0100 | [diff] [blame] | 124 | final float y = event.getY(); |
| 125 | final float x = event.getX(); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 126 | |
| 127 | boolean isUp = false; |
Jorim Jaggi | a86790b | 2015-04-02 16:32:29 -0700 | [diff] [blame] | 128 | switch (action) { |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 129 | case MotionEvent.ACTION_DOWN: |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 130 | View targetView = getIconAtPosition(x, y); |
| 131 | if (targetView == null || (mTargetedView != null && mTargetedView != targetView)) { |
| 132 | mMotionCancelled = true; |
| 133 | return false; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 134 | } |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 135 | if (mTargetedView != null) { |
| 136 | cancelAnimation(); |
| 137 | } else { |
| 138 | mTouchSlopExeeded = false; |
| 139 | } |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 140 | startSwiping(targetView); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 141 | mInitialTouchX = x; |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 142 | mInitialTouchY = y; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 143 | mTranslationOnDown = mTranslation; |
| 144 | initVelocityTracker(); |
| 145 | trackMovement(event); |
Selim Cinek | 1dc4065 | 2014-11-12 17:53:52 +0100 | [diff] [blame] | 146 | mMotionCancelled = false; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 147 | break; |
Selim Cinek | 1dc4065 | 2014-11-12 17:53:52 +0100 | [diff] [blame] | 148 | case MotionEvent.ACTION_POINTER_DOWN: |
| 149 | mMotionCancelled = true; |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 150 | endMotion(true /* forceSnapBack */, x, y); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 151 | break; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 152 | case MotionEvent.ACTION_MOVE: |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 153 | trackMovement(event); |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 154 | float xDist = x - mInitialTouchX; |
| 155 | float yDist = y - mInitialTouchY; |
| 156 | float distance = (float) Math.hypot(xDist, yDist); |
| 157 | if (!mTouchSlopExeeded && distance > mTouchSlop) { |
| 158 | mTouchSlopExeeded = true; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 159 | } |
| 160 | if (mSwipingInProgress) { |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 161 | if (mTargetedView == mRightIcon) { |
| 162 | distance = mTranslationOnDown - distance; |
| 163 | distance = Math.min(0, distance); |
| 164 | } else { |
| 165 | distance = mTranslationOnDown + distance; |
| 166 | distance = Math.max(0, distance); |
| 167 | } |
Selim Cinek | c5fec9a | 2017-07-05 16:22:53 +0200 | [diff] [blame] | 168 | setTranslation(distance, false /* isReset */, false /* animateReset */); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 169 | } |
| 170 | break; |
| 171 | |
| 172 | case MotionEvent.ACTION_UP: |
| 173 | isUp = true; |
| 174 | case MotionEvent.ACTION_CANCEL: |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 175 | boolean hintOnTheRight = mTargetedView == mRightIcon; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 176 | trackMovement(event); |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 177 | endMotion(!isUp, x, y); |
| 178 | if (!mTouchSlopExeeded && isUp) { |
| 179 | mCallback.onIconClicked(hintOnTheRight); |
| 180 | } |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 186 | private void startSwiping(View targetView) { |
| 187 | mCallback.onSwipingStarted(targetView == mRightIcon); |
| 188 | mSwipingInProgress = true; |
| 189 | mTargetedView = targetView; |
| 190 | } |
| 191 | |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 192 | private View getIconAtPosition(float x, float y) { |
| 193 | if (leftSwipePossible() && isOnIcon(mLeftIcon, x, y)) { |
| 194 | return mLeftIcon; |
| 195 | } |
| 196 | if (rightSwipePossible() && isOnIcon(mRightIcon, x, y)) { |
| 197 | return mRightIcon; |
| 198 | } |
| 199 | return null; |
| 200 | } |
| 201 | |
Selim Cinek | 9db7105 | 2015-04-24 18:54:30 -0700 | [diff] [blame] | 202 | public boolean isOnAffordanceIcon(float x, float y) { |
| 203 | return isOnIcon(mLeftIcon, x, y) || isOnIcon(mRightIcon, x, y); |
| 204 | } |
| 205 | |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 206 | private boolean isOnIcon(View icon, float x, float y) { |
| 207 | float iconX = icon.getX() + icon.getWidth() / 2.0f; |
| 208 | float iconY = icon.getY() + icon.getHeight() / 2.0f; |
| 209 | double distance = Math.hypot(x - iconX, y - iconY); |
| 210 | return distance <= mTouchTargetSize / 2; |
| 211 | } |
| 212 | |
| 213 | private void endMotion(boolean forceSnapBack, float lastX, float lastY) { |
Selim Cinek | 1dc4065 | 2014-11-12 17:53:52 +0100 | [diff] [blame] | 214 | if (mSwipingInProgress) { |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 215 | flingWithCurrentVelocity(forceSnapBack, lastX, lastY); |
| 216 | } else { |
| 217 | mTargetedView = null; |
Selim Cinek | 1dc4065 | 2014-11-12 17:53:52 +0100 | [diff] [blame] | 218 | } |
| 219 | if (mVelocityTracker != null) { |
| 220 | mVelocityTracker.recycle(); |
| 221 | mVelocityTracker = null; |
| 222 | } |
| 223 | } |
| 224 | |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 225 | private boolean rightSwipePossible() { |
| 226 | return mRightIcon.getVisibility() == View.VISIBLE; |
| 227 | } |
| 228 | |
| 229 | private boolean leftSwipePossible() { |
| 230 | return mLeftIcon.getVisibility() == View.VISIBLE; |
| 231 | } |
| 232 | |
| 233 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 234 | return false; |
| 235 | } |
| 236 | |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 237 | public void startHintAnimation(boolean right, |
| 238 | Runnable onFinishedListener) { |
| 239 | cancelAnimation(); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 240 | startHintAnimationPhase1(right, onFinishedListener); |
| 241 | } |
| 242 | |
| 243 | private void startHintAnimationPhase1(final boolean right, final Runnable onFinishedListener) { |
| 244 | final KeyguardAffordanceView targetView = right ? mRightIcon : mLeftIcon; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 245 | ValueAnimator animator = getAnimatorToRadius(right, mHintGrowAmount); |
| 246 | animator.addListener(new AnimatorListenerAdapter() { |
| 247 | private boolean mCancelled; |
| 248 | |
| 249 | @Override |
| 250 | public void onAnimationCancel(Animator animation) { |
| 251 | mCancelled = true; |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public void onAnimationEnd(Animator animation) { |
| 256 | if (mCancelled) { |
| 257 | mSwipeAnimator = null; |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 258 | mTargetedView = null; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 259 | onFinishedListener.run(); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 260 | } else { |
| 261 | startUnlockHintAnimationPhase2(right, onFinishedListener); |
| 262 | } |
| 263 | } |
| 264 | }); |
Selim Cinek | c18010f | 2016-01-20 13:41:30 -0800 | [diff] [blame] | 265 | animator.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 266 | animator.setDuration(HINT_PHASE1_DURATION); |
| 267 | animator.start(); |
| 268 | mSwipeAnimator = animator; |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 269 | mTargetedView = targetView; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Phase 2: Move back. |
| 274 | */ |
| 275 | private void startUnlockHintAnimationPhase2(boolean right, final Runnable onFinishedListener) { |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 276 | ValueAnimator animator = getAnimatorToRadius(right, 0); |
| 277 | animator.addListener(new AnimatorListenerAdapter() { |
| 278 | @Override |
| 279 | public void onAnimationEnd(Animator animation) { |
| 280 | mSwipeAnimator = null; |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 281 | mTargetedView = null; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 282 | onFinishedListener.run(); |
| 283 | } |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 284 | }); |
Selim Cinek | c18010f | 2016-01-20 13:41:30 -0800 | [diff] [blame] | 285 | animator.setInterpolator(Interpolators.FAST_OUT_LINEAR_IN); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 286 | animator.setDuration(HINT_PHASE2_DURATION); |
| 287 | animator.setStartDelay(HINT_CIRCLE_OPEN_DURATION); |
| 288 | animator.start(); |
| 289 | mSwipeAnimator = animator; |
| 290 | } |
| 291 | |
| 292 | private ValueAnimator getAnimatorToRadius(final boolean right, int radius) { |
| 293 | final KeyguardAffordanceView targetView = right ? mRightIcon : mLeftIcon; |
| 294 | ValueAnimator animator = ValueAnimator.ofFloat(targetView.getCircleRadius(), radius); |
| 295 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 296 | @Override |
| 297 | public void onAnimationUpdate(ValueAnimator animation) { |
| 298 | float newRadius = (float) animation.getAnimatedValue(); |
| 299 | targetView.setCircleRadiusWithoutAnimation(newRadius); |
| 300 | float translation = getTranslationFromRadius(newRadius); |
| 301 | mTranslation = right ? -translation : translation; |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 302 | updateIconsFromTranslation(targetView); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 303 | } |
| 304 | }); |
| 305 | return animator; |
| 306 | } |
| 307 | |
| 308 | private void cancelAnimation() { |
| 309 | if (mSwipeAnimator != null) { |
| 310 | mSwipeAnimator.cancel(); |
| 311 | } |
| 312 | } |
| 313 | |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 314 | private void flingWithCurrentVelocity(boolean forceSnapBack, float lastX, float lastY) { |
| 315 | float vel = getCurrentVelocity(lastX, lastY); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 316 | |
| 317 | // We snap back if the current translation is not far enough |
Adrian Roos | 6a70b88 | 2016-04-19 17:09:07 -0700 | [diff] [blame] | 318 | boolean snapBack = false; |
| 319 | if (mCallback.needsAntiFalsing()) { |
| 320 | snapBack = snapBack || mFalsingManager.isFalseTouch(); |
Blazej Magnowski | 6dc59b4 | 2015-09-22 15:14:20 -0700 | [diff] [blame] | 321 | } |
Adrian Roos | 6a70b88 | 2016-04-19 17:09:07 -0700 | [diff] [blame] | 322 | snapBack = snapBack || isBelowFalsingThreshold(); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 323 | |
| 324 | // or if the velocity is in the opposite direction. |
| 325 | boolean velIsInWrongDirection = vel * mTranslation < 0; |
| 326 | snapBack |= Math.abs(vel) > mMinFlingVelocity && velIsInWrongDirection; |
| 327 | vel = snapBack ^ velIsInWrongDirection ? 0 : vel; |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 328 | fling(vel, snapBack || forceSnapBack, mTranslation < 0); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 329 | } |
| 330 | |
Selim Cinek | 5386fb3 | 2014-09-03 16:37:36 +0200 | [diff] [blame] | 331 | private boolean isBelowFalsingThreshold() { |
Selim Cinek | 34cf5c4 | 2014-09-26 15:39:00 +0200 | [diff] [blame] | 332 | return Math.abs(mTranslation) < Math.abs(mTranslationOnDown) + getMinTranslationAmount(); |
| 333 | } |
| 334 | |
| 335 | private int getMinTranslationAmount() { |
| 336 | float factor = mCallback.getAffordanceFalsingFactor(); |
| 337 | return (int) (mMinTranslationAmount * factor); |
Selim Cinek | 5386fb3 | 2014-09-03 16:37:36 +0200 | [diff] [blame] | 338 | } |
| 339 | |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 340 | private void fling(float vel, final boolean snapBack, boolean right) { |
| 341 | float target = right ? -mCallback.getMaxTranslationDistance() |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 342 | : mCallback.getMaxTranslationDistance(); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 343 | target = snapBack ? 0 : target; |
| 344 | |
| 345 | ValueAnimator animator = ValueAnimator.ofFloat(mTranslation, target); |
| 346 | mFlingAnimationUtils.apply(animator, mTranslation, target, vel); |
| 347 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 348 | @Override |
| 349 | public void onAnimationUpdate(ValueAnimator animation) { |
| 350 | mTranslation = (float) animation.getAnimatedValue(); |
| 351 | } |
| 352 | }); |
| 353 | animator.addListener(mFlingEndListener); |
| 354 | if (!snapBack) { |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 355 | startFinishingCircleAnimation(vel * 0.375f, mAnimationEndRunnable, right); |
| 356 | mCallback.onAnimationToSideStarted(right, mTranslation, vel); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 357 | } else { |
| 358 | reset(true); |
| 359 | } |
| 360 | animator.start(); |
| 361 | mSwipeAnimator = animator; |
Jorim Jaggi | a86790b | 2015-04-02 16:32:29 -0700 | [diff] [blame] | 362 | if (snapBack) { |
| 363 | mCallback.onSwipingAborted(); |
| 364 | } |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Lucas Dupin | 29678e6 | 2018-06-19 10:42:42 -0700 | [diff] [blame] | 367 | private void startFinishingCircleAnimation(float velocity, Runnable animationEndRunnable, |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 368 | boolean right) { |
| 369 | KeyguardAffordanceView targetView = right ? mRightIcon : mLeftIcon; |
Lucas Dupin | 29678e6 | 2018-06-19 10:42:42 -0700 | [diff] [blame] | 370 | targetView.finishAnimation(velocity, animationEndRunnable); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Selim Cinek | c5fec9a | 2017-07-05 16:22:53 +0200 | [diff] [blame] | 373 | private void setTranslation(float translation, boolean isReset, boolean animateReset) { |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 374 | translation = rightSwipePossible() ? translation : Math.max(0, translation); |
| 375 | translation = leftSwipePossible() ? translation : Math.min(0, translation); |
| 376 | float absTranslation = Math.abs(translation); |
Selim Cinek | c5fec9a | 2017-07-05 16:22:53 +0200 | [diff] [blame] | 377 | if (translation != mTranslation || isReset) { |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 378 | KeyguardAffordanceView targetView = translation > 0 ? mLeftIcon : mRightIcon; |
| 379 | KeyguardAffordanceView otherView = translation > 0 ? mRightIcon : mLeftIcon; |
Selim Cinek | 34cf5c4 | 2014-09-26 15:39:00 +0200 | [diff] [blame] | 380 | float alpha = absTranslation / getMinTranslationAmount(); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 381 | |
| 382 | // We interpolate the alpha of the other icons to 0 |
Jorim Jaggi | 27c9b74 | 2015-04-09 10:34:49 -0700 | [diff] [blame] | 383 | float fadeOutAlpha = 1.0f - alpha; |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 384 | fadeOutAlpha = Math.max(fadeOutAlpha, 0.0f); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 385 | |
| 386 | boolean animateIcons = isReset && animateReset; |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 387 | boolean forceNoCircleAnimation = isReset && !animateReset; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 388 | float radius = getRadiusFromTranslation(absTranslation); |
Selim Cinek | 5386fb3 | 2014-09-03 16:37:36 +0200 | [diff] [blame] | 389 | boolean slowAnimation = isReset && isBelowFalsingThreshold(); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 390 | if (!isReset) { |
Jorim Jaggi | 27c9b74 | 2015-04-09 10:34:49 -0700 | [diff] [blame] | 391 | updateIcon(targetView, radius, alpha + fadeOutAlpha * targetView.getRestingAlpha(), |
Selim Cinek | c5fec9a | 2017-07-05 16:22:53 +0200 | [diff] [blame] | 392 | false, false, false, false); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 393 | } else { |
Jorim Jaggi | 27c9b74 | 2015-04-09 10:34:49 -0700 | [diff] [blame] | 394 | updateIcon(targetView, 0.0f, fadeOutAlpha * targetView.getRestingAlpha(), |
Selim Cinek | c5fec9a | 2017-07-05 16:22:53 +0200 | [diff] [blame] | 395 | animateIcons, slowAnimation, true /* isReset */, forceNoCircleAnimation); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 396 | } |
Jorim Jaggi | 27c9b74 | 2015-04-09 10:34:49 -0700 | [diff] [blame] | 397 | updateIcon(otherView, 0.0f, fadeOutAlpha * otherView.getRestingAlpha(), |
Selim Cinek | c5fec9a | 2017-07-05 16:22:53 +0200 | [diff] [blame] | 398 | animateIcons, slowAnimation, isReset, forceNoCircleAnimation); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 399 | |
| 400 | mTranslation = translation; |
| 401 | } |
| 402 | } |
| 403 | |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 404 | private void updateIconsFromTranslation(KeyguardAffordanceView targetView) { |
| 405 | float absTranslation = Math.abs(mTranslation); |
| 406 | float alpha = absTranslation / getMinTranslationAmount(); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 407 | |
| 408 | // We interpolate the alpha of the other icons to 0 |
Jorim Jaggi | 27c9b74 | 2015-04-09 10:34:49 -0700 | [diff] [blame] | 409 | float fadeOutAlpha = 1.0f - alpha; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 410 | fadeOutAlpha = Math.max(0.0f, fadeOutAlpha); |
| 411 | |
| 412 | // We interpolate the alpha of the targetView to 1 |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 413 | KeyguardAffordanceView otherView = targetView == mRightIcon ? mLeftIcon : mRightIcon; |
Jorim Jaggi | 27c9b74 | 2015-04-09 10:34:49 -0700 | [diff] [blame] | 414 | updateIconAlpha(targetView, alpha + fadeOutAlpha * targetView.getRestingAlpha(), false); |
| 415 | updateIconAlpha(otherView, fadeOutAlpha * otherView.getRestingAlpha(), false); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | private float getTranslationFromRadius(float circleSize) { |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 419 | float translation = (circleSize - mMinBackgroundRadius) |
| 420 | / BACKGROUND_RADIUS_SCALE_FACTOR; |
| 421 | return translation > 0.0f ? translation + mTouchSlop : 0.0f; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | private float getRadiusFromTranslation(float translation) { |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 425 | if (translation <= mTouchSlop) { |
| 426 | return 0.0f; |
| 427 | } |
| 428 | return (translation - mTouchSlop) * BACKGROUND_RADIUS_SCALE_FACTOR + mMinBackgroundRadius; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 429 | } |
| 430 | |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 431 | public void animateHideLeftRightIcon() { |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 432 | cancelAnimation(); |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 433 | updateIcon(mRightIcon, 0f, 0f, true, false, false, false); |
| 434 | updateIcon(mLeftIcon, 0f, 0f, true, false, false, false); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | private void updateIcon(KeyguardAffordanceView view, float circleRadius, float alpha, |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 438 | boolean animate, boolean slowRadiusAnimation, boolean force, |
| 439 | boolean forceNoCircleAnimation) { |
Adrian Roos | a4eba9f | 2015-07-22 18:13:04 -0700 | [diff] [blame] | 440 | if (view.getVisibility() != View.VISIBLE && !force) { |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 441 | return; |
| 442 | } |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 443 | if (forceNoCircleAnimation) { |
| 444 | view.setCircleRadiusWithoutAnimation(circleRadius); |
| 445 | } else { |
| 446 | view.setCircleRadius(circleRadius, slowRadiusAnimation); |
| 447 | } |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 448 | updateIconAlpha(view, alpha, animate); |
| 449 | } |
| 450 | |
| 451 | private void updateIconAlpha(KeyguardAffordanceView view, float alpha, boolean animate) { |
Jorim Jaggi | 27c9b74 | 2015-04-09 10:34:49 -0700 | [diff] [blame] | 452 | float scale = getScale(alpha, view); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 453 | alpha = Math.min(1.0f, alpha); |
| 454 | view.setImageAlpha(alpha, animate); |
| 455 | view.setImageScale(scale, animate); |
| 456 | } |
| 457 | |
Jorim Jaggi | 27c9b74 | 2015-04-09 10:34:49 -0700 | [diff] [blame] | 458 | private float getScale(float alpha, KeyguardAffordanceView icon) { |
| 459 | float scale = alpha / icon.getRestingAlpha() * 0.2f + |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 460 | KeyguardAffordanceView.MIN_ICON_SCALE_AMOUNT; |
| 461 | return Math.min(scale, KeyguardAffordanceView.MAX_ICON_SCALE_AMOUNT); |
| 462 | } |
| 463 | |
| 464 | private void trackMovement(MotionEvent event) { |
| 465 | if (mVelocityTracker != null) { |
| 466 | mVelocityTracker.addMovement(event); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | private void initVelocityTracker() { |
| 471 | if (mVelocityTracker != null) { |
| 472 | mVelocityTracker.recycle(); |
| 473 | } |
| 474 | mVelocityTracker = VelocityTracker.obtain(); |
| 475 | } |
| 476 | |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 477 | private float getCurrentVelocity(float lastX, float lastY) { |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 478 | if (mVelocityTracker == null) { |
| 479 | return 0; |
| 480 | } |
| 481 | mVelocityTracker.computeCurrentVelocity(1000); |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 482 | float aX = mVelocityTracker.getXVelocity(); |
| 483 | float aY = mVelocityTracker.getYVelocity(); |
| 484 | float bX = lastX - mInitialTouchX; |
| 485 | float bY = lastY - mInitialTouchY; |
| 486 | float bLen = (float) Math.hypot(bX, bY); |
| 487 | // Project the velocity onto the distance vector: a * b / |b| |
| 488 | float projectedVelocity = (aX * bX + aY * bY) / bLen; |
| 489 | if (mTargetedView == mRightIcon) { |
| 490 | projectedVelocity = -projectedVelocity; |
| 491 | } |
| 492 | return projectedVelocity; |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | public void onConfigurationChanged() { |
| 496 | initDimens(); |
Jorim Jaggi | ef067fb | 2014-09-01 16:44:08 +0200 | [diff] [blame] | 497 | initIcons(); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 498 | } |
| 499 | |
Jorim Jaggi | 0a81822 | 2014-11-10 19:04:34 +0100 | [diff] [blame] | 500 | public void onRtlPropertiesChanged() { |
| 501 | initIcons(); |
| 502 | } |
| 503 | |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 504 | public void reset(boolean animate) { |
Selim Cinek | 158af6a | 2015-07-06 18:39:28 -0700 | [diff] [blame] | 505 | cancelAnimation(); |
Selim Cinek | c5fec9a | 2017-07-05 16:22:53 +0200 | [diff] [blame] | 506 | setTranslation(0.0f, true /* isReset */, animate); |
Jorim Jaggi | a86790b | 2015-04-02 16:32:29 -0700 | [diff] [blame] | 507 | mMotionCancelled = true; |
| 508 | if (mSwipingInProgress) { |
| 509 | mCallback.onSwipingAborted(); |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 510 | mSwipingInProgress = false; |
Jorim Jaggi | a86790b | 2015-04-02 16:32:29 -0700 | [diff] [blame] | 511 | } |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | public boolean isSwipingInProgress() { |
| 515 | return mSwipingInProgress; |
| 516 | } |
| 517 | |
| 518 | public void launchAffordance(boolean animate, boolean left) { |
| 519 | if (mSwipingInProgress) { |
| 520 | // We don't want to mess with the state if the user is actually swiping already. |
| 521 | return; |
| 522 | } |
| 523 | KeyguardAffordanceView targetView = left ? mLeftIcon : mRightIcon; |
| 524 | KeyguardAffordanceView otherView = left ? mRightIcon : mLeftIcon; |
| 525 | startSwiping(targetView); |
Lucas Dupin | f78c540 | 2018-01-05 12:57:27 -0800 | [diff] [blame] | 526 | |
| 527 | // Do not animate the circle expanding if the affordance isn't visible, |
| 528 | // otherwise the circle will be meaningless. |
| 529 | if (targetView.getVisibility() != View.VISIBLE) { |
| 530 | animate = false; |
| 531 | } |
| 532 | |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 533 | if (animate) { |
| 534 | fling(0, false, !left); |
| 535 | updateIcon(otherView, 0.0f, 0, true, false, true, false); |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 536 | } else { |
| 537 | mCallback.onAnimationToSideStarted(!left, mTranslation, 0); |
| 538 | mTranslation = left ? mCallback.getMaxTranslationDistance() |
| 539 | : mCallback.getMaxTranslationDistance(); |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 540 | updateIcon(otherView, 0.0f, 0.0f, false, false, true, false); |
| 541 | targetView.instantFinishAnimation(); |
| 542 | mFlingEndListener.onAnimationEnd(null); |
| 543 | mAnimationEndRunnable.run(); |
| 544 | } |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | public interface Callback { |
| 548 | |
| 549 | /** |
| 550 | * Notifies the callback when an animation to a side page was started. |
| 551 | * |
| 552 | * @param rightPage Is the page animated to the right page? |
| 553 | */ |
Christoph Studer | b018399 | 2014-12-22 21:02:26 +0100 | [diff] [blame] | 554 | void onAnimationToSideStarted(boolean rightPage, float translation, float vel); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 555 | |
| 556 | /** |
| 557 | * Notifies the callback the animation to a side page has ended. |
| 558 | */ |
| 559 | void onAnimationToSideEnded(); |
| 560 | |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 561 | float getMaxTranslationDistance(); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 562 | |
Jorim Jaggi | d944986 | 2015-05-29 14:49:08 -0700 | [diff] [blame] | 563 | void onSwipingStarted(boolean rightIcon); |
Jorim Jaggi | a86790b | 2015-04-02 16:32:29 -0700 | [diff] [blame] | 564 | |
| 565 | void onSwipingAborted(); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 566 | |
Selim Cinek | 6746c28 | 2015-04-21 19:58:31 -0700 | [diff] [blame] | 567 | void onIconClicked(boolean rightIcon); |
| 568 | |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 569 | KeyguardAffordanceView getLeftIcon(); |
| 570 | |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 571 | KeyguardAffordanceView getRightIcon(); |
Jorim Jaggi | b6cdcbc | 2014-07-25 21:58:29 +0200 | [diff] [blame] | 572 | |
| 573 | View getLeftPreview(); |
| 574 | |
| 575 | View getRightPreview(); |
Selim Cinek | 34cf5c4 | 2014-09-26 15:39:00 +0200 | [diff] [blame] | 576 | |
| 577 | /** |
| 578 | * @return The factor the minimum swipe amount should be multiplied with. |
| 579 | */ |
| 580 | float getAffordanceFalsingFactor(); |
Adrian Roos | 6a70b88 | 2016-04-19 17:09:07 -0700 | [diff] [blame] | 581 | |
| 582 | boolean needsAntiFalsing(); |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 583 | } |
| 584 | } |