Tony Wickham | dadb304 | 2016-02-24 11:07:00 -0800 | [diff] [blame^] | 1 | package com.android.launcher3; |
| 2 | |
| 3 | /** |
| 4 | * Keeps track of when thresholds are passed during a pinch gesture, |
| 5 | * used to inform {@link PinchAnimationManager} throughout. |
| 6 | * |
| 7 | * @see PinchToOverviewListener |
| 8 | * @see PinchAnimationManager |
| 9 | */ |
| 10 | public class PinchThresholdManager { |
| 11 | public static final float THRESHOLD_ZERO = 0.0f; |
| 12 | public static final float THRESHOLD_ONE = 0.40f; |
| 13 | public static final float THRESHOLD_TWO = 0.70f; |
| 14 | public static final float THRESHOLD_THREE = 0.95f; |
| 15 | |
| 16 | private Workspace mWorkspace; |
| 17 | |
| 18 | private float mPassedThreshold = THRESHOLD_ZERO; |
| 19 | |
| 20 | public PinchThresholdManager(Workspace workspace) { |
| 21 | mWorkspace = workspace; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Uses the pinch progress to determine whether a threshold has been passed, |
| 26 | * and asks the {@param animationManager} to animate if so. |
| 27 | * @param progress From 0 to 1, where 0 is overview and 1 is workspace. |
| 28 | * @param animationManager Animates the threshold change if one is passed. |
| 29 | * @return The last passed threshold, one of |
| 30 | * {@link PinchThresholdManager#THRESHOLD_ZERO}, |
| 31 | * {@link PinchThresholdManager#THRESHOLD_ONE}, |
| 32 | * {@link PinchThresholdManager#THRESHOLD_TWO}, or |
| 33 | * {@link PinchThresholdManager#THRESHOLD_THREE} |
| 34 | */ |
| 35 | public float updateAndAnimatePassedThreshold(float progress, |
| 36 | PinchAnimationManager animationManager) { |
| 37 | if (!mWorkspace.isInOverviewMode()) { |
| 38 | // Invert the progress, because going from workspace to overview is 1 to 0. |
| 39 | progress = 1f - progress; |
| 40 | } |
| 41 | |
| 42 | float previousPassedThreshold = mPassedThreshold; |
| 43 | |
| 44 | if (progress < THRESHOLD_ONE) { |
| 45 | mPassedThreshold = THRESHOLD_ZERO; |
| 46 | } else if (progress < THRESHOLD_TWO) { |
| 47 | mPassedThreshold = THRESHOLD_ONE; |
| 48 | } else if (progress < THRESHOLD_THREE) { |
| 49 | mPassedThreshold = THRESHOLD_TWO; |
| 50 | } else { |
| 51 | mPassedThreshold = THRESHOLD_THREE; |
| 52 | } |
| 53 | |
| 54 | if (mPassedThreshold != previousPassedThreshold) { |
| 55 | Workspace.State fromState = mWorkspace.isInOverviewMode() ? Workspace.State.OVERVIEW |
| 56 | : Workspace.State.NORMAL; |
| 57 | Workspace.State toState = mWorkspace.isInOverviewMode() ? Workspace.State.NORMAL |
| 58 | : Workspace.State.OVERVIEW; |
| 59 | float thresholdToAnimate = mPassedThreshold; |
| 60 | if (mPassedThreshold < previousPassedThreshold) { |
| 61 | // User reversed pinch, so heading back to the state that they started from. |
| 62 | toState = fromState; |
| 63 | thresholdToAnimate = previousPassedThreshold; |
| 64 | } |
| 65 | animationManager.animateThreshold(thresholdToAnimate, fromState, toState); |
| 66 | } |
| 67 | return mPassedThreshold; |
| 68 | } |
| 69 | |
| 70 | public float getPassedThreshold() { |
| 71 | return mPassedThreshold; |
| 72 | } |
| 73 | |
| 74 | public void reset() { |
| 75 | mPassedThreshold = THRESHOLD_ZERO; |
| 76 | } |
| 77 | } |