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