Tony Wickham | 1743ac4 | 2016-03-17 11:53:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Tony Wickham | dadb304 | 2016-02-24 11:07:00 -0800 | [diff] [blame] | 17 | package com.android.launcher3; |
| 18 | |
| 19 | /** |
| 20 | * Keeps track of when thresholds are passed during a pinch gesture, |
| 21 | * used to inform {@link PinchAnimationManager} throughout. |
| 22 | * |
| 23 | * @see PinchToOverviewListener |
| 24 | * @see PinchAnimationManager |
| 25 | */ |
| 26 | public class PinchThresholdManager { |
| 27 | public static final float THRESHOLD_ZERO = 0.0f; |
| 28 | public static final float THRESHOLD_ONE = 0.40f; |
| 29 | public static final float THRESHOLD_TWO = 0.70f; |
| 30 | public static final float THRESHOLD_THREE = 0.95f; |
| 31 | |
Sunny Goyal | f9403d9 | 2017-10-18 10:55:56 -0700 | [diff] [blame^] | 32 | private final Workspace mWorkspace; |
| 33 | private final Launcher mLauncher; |
Tony Wickham | dadb304 | 2016-02-24 11:07:00 -0800 | [diff] [blame] | 34 | |
| 35 | private float mPassedThreshold = THRESHOLD_ZERO; |
| 36 | |
| 37 | public PinchThresholdManager(Workspace workspace) { |
| 38 | mWorkspace = workspace; |
Sunny Goyal | f9403d9 | 2017-10-18 10:55:56 -0700 | [diff] [blame^] | 39 | mLauncher = mWorkspace.mLauncher; |
Tony Wickham | dadb304 | 2016-02-24 11:07:00 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Uses the pinch progress to determine whether a threshold has been passed, |
| 44 | * and asks the {@param animationManager} to animate if so. |
| 45 | * @param progress From 0 to 1, where 0 is overview and 1 is workspace. |
| 46 | * @param animationManager Animates the threshold change if one is passed. |
| 47 | * @return The last passed threshold, one of |
| 48 | * {@link PinchThresholdManager#THRESHOLD_ZERO}, |
| 49 | * {@link PinchThresholdManager#THRESHOLD_ONE}, |
| 50 | * {@link PinchThresholdManager#THRESHOLD_TWO}, or |
| 51 | * {@link PinchThresholdManager#THRESHOLD_THREE} |
| 52 | */ |
| 53 | public float updateAndAnimatePassedThreshold(float progress, |
| 54 | PinchAnimationManager animationManager) { |
Sunny Goyal | f9403d9 | 2017-10-18 10:55:56 -0700 | [diff] [blame^] | 55 | if (!mLauncher.isInState(LauncherState.OVERVIEW)) { |
Tony Wickham | dadb304 | 2016-02-24 11:07:00 -0800 | [diff] [blame] | 56 | // Invert the progress, because going from workspace to overview is 1 to 0. |
| 57 | progress = 1f - progress; |
| 58 | } |
| 59 | |
| 60 | float previousPassedThreshold = mPassedThreshold; |
| 61 | |
| 62 | if (progress < THRESHOLD_ONE) { |
| 63 | mPassedThreshold = THRESHOLD_ZERO; |
| 64 | } else if (progress < THRESHOLD_TWO) { |
| 65 | mPassedThreshold = THRESHOLD_ONE; |
| 66 | } else if (progress < THRESHOLD_THREE) { |
| 67 | mPassedThreshold = THRESHOLD_TWO; |
| 68 | } else { |
| 69 | mPassedThreshold = THRESHOLD_THREE; |
| 70 | } |
| 71 | |
| 72 | if (mPassedThreshold != previousPassedThreshold) { |
Sunny Goyal | f9403d9 | 2017-10-18 10:55:56 -0700 | [diff] [blame^] | 73 | LauncherState fromState = mLauncher.getWorkspace().getState(); |
| 74 | LauncherState toState = mLauncher.isInState(LauncherState.OVERVIEW) |
| 75 | ? LauncherState.NORMAL : LauncherState.OVERVIEW; |
Tony Wickham | dadb304 | 2016-02-24 11:07:00 -0800 | [diff] [blame] | 76 | float thresholdToAnimate = mPassedThreshold; |
| 77 | if (mPassedThreshold < previousPassedThreshold) { |
| 78 | // User reversed pinch, so heading back to the state that they started from. |
| 79 | toState = fromState; |
| 80 | thresholdToAnimate = previousPassedThreshold; |
| 81 | } |
| 82 | animationManager.animateThreshold(thresholdToAnimate, fromState, toState); |
| 83 | } |
| 84 | return mPassedThreshold; |
| 85 | } |
| 86 | |
| 87 | public float getPassedThreshold() { |
| 88 | return mPassedThreshold; |
| 89 | } |
| 90 | |
| 91 | public void reset() { |
| 92 | mPassedThreshold = THRESHOLD_ZERO; |
| 93 | } |
| 94 | } |