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
17package 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 */
26public 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
32    private Workspace mWorkspace;
33
34    private float mPassedThreshold = THRESHOLD_ZERO;
35
36    public PinchThresholdManager(Workspace workspace) {
37        mWorkspace = workspace;
38    }
39
40    /**
41     * Uses the pinch progress to determine whether a threshold has been passed,
42     * and asks the {@param animationManager} to animate if so.
43     * @param progress From 0 to 1, where 0 is overview and 1 is workspace.
44     * @param animationManager Animates the threshold change if one is passed.
45     * @return The last passed threshold, one of
46     *         {@link PinchThresholdManager#THRESHOLD_ZERO},
47     *         {@link PinchThresholdManager#THRESHOLD_ONE},
48     *         {@link PinchThresholdManager#THRESHOLD_TWO}, or
49     *         {@link PinchThresholdManager#THRESHOLD_THREE}
50     */
51    public float updateAndAnimatePassedThreshold(float progress,
52            PinchAnimationManager animationManager) {
53        if (!mWorkspace.isInOverviewMode()) {
54            // Invert the progress, because going from workspace to overview is 1 to 0.
55            progress = 1f - progress;
56        }
57
58        float previousPassedThreshold = mPassedThreshold;
59
60        if (progress < THRESHOLD_ONE) {
61            mPassedThreshold = THRESHOLD_ZERO;
62        } else if (progress < THRESHOLD_TWO) {
63            mPassedThreshold = THRESHOLD_ONE;
64        } else if (progress < THRESHOLD_THREE) {
65            mPassedThreshold = THRESHOLD_TWO;
66        } else {
67            mPassedThreshold = THRESHOLD_THREE;
68        }
69
70        if (mPassedThreshold != previousPassedThreshold) {
71            Workspace.State fromState = mWorkspace.isInOverviewMode() ? Workspace.State.OVERVIEW
72                    : Workspace.State.NORMAL;
73            Workspace.State toState = mWorkspace.isInOverviewMode() ? Workspace.State.NORMAL
74                    : Workspace.State.OVERVIEW;
75            float thresholdToAnimate = mPassedThreshold;
76            if (mPassedThreshold < previousPassedThreshold) {
77                // User reversed pinch, so heading back to the state that they started from.
78                toState = fromState;
79                thresholdToAnimate = previousPassedThreshold;
80            }
81            animationManager.animateThreshold(thresholdToAnimate, fromState, toState);
82        }
83        return mPassedThreshold;
84    }
85
86    public float getPassedThreshold() {
87        return mPassedThreshold;
88    }
89
90    public void reset() {
91        mPassedThreshold = THRESHOLD_ZERO;
92    }
93}
94