blob: 49376333205d56e28241818d6b1c630f6f47351d [file] [log] [blame]
Tony Wickham1743ac42016-03-17 11:53:26 -07001/*
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 Wickhamdadb3042016-02-24 11:07:00 -080017package 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
Sunny Goyalf9403d92017-10-18 10:55:56 -070032 private final Workspace mWorkspace;
33 private final Launcher mLauncher;
Tony Wickhamdadb3042016-02-24 11:07:00 -080034
35 private float mPassedThreshold = THRESHOLD_ZERO;
36
37 public PinchThresholdManager(Workspace workspace) {
38 mWorkspace = workspace;
Sunny Goyalf9403d92017-10-18 10:55:56 -070039 mLauncher = mWorkspace.mLauncher;
Tony Wickhamdadb3042016-02-24 11:07:00 -080040 }
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 Goyalf9403d92017-10-18 10:55:56 -070055 if (!mLauncher.isInState(LauncherState.OVERVIEW)) {
Tony Wickhamdadb3042016-02-24 11:07:00 -080056 // 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 Goyalf9403d92017-10-18 10:55:56 -070073 LauncherState fromState = mLauncher.getWorkspace().getState();
74 LauncherState toState = mLauncher.isInState(LauncherState.OVERVIEW)
75 ? LauncherState.NORMAL : LauncherState.OVERVIEW;
Tony Wickhamdadb3042016-02-24 11:07:00 -080076 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}