1 /*
2  * Copyright (C) 2015 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 package com.android.launcher3.touch;
17 
18 import static com.android.launcher3.LauncherState.ALL_APPS;
19 import static com.android.launcher3.LauncherState.NORMAL;
20 import static com.android.launcher3.anim.Interpolators.LINEAR;
21 import static com.android.launcher3.states.StateAnimationConfig.ANIM_ALL_APPS_FADE;
22 import static com.android.launcher3.states.StateAnimationConfig.ANIM_SCRIM_FADE;
23 
24 import android.view.MotionEvent;
25 import android.view.animation.Interpolator;
26 
27 import com.android.launcher3.AbstractFloatingView;
28 import com.android.launcher3.Launcher;
29 import com.android.launcher3.LauncherState;
30 import com.android.launcher3.anim.Interpolators;
31 import com.android.launcher3.states.StateAnimationConfig;
32 
33 /**
34  * TouchController to switch between NORMAL and ALL_APPS state.
35  */
36 public class AllAppsSwipeController extends AbstractStateChangeTouchController {
37 
38     private static final float ALLAPPS_STAGGERED_FADE_THRESHOLD = 0.5f;
39 
40     public static final Interpolator ALLAPPS_STAGGERED_FADE_EARLY_RESPONDER =
41             Interpolators.clampToProgress(LINEAR, 0, ALLAPPS_STAGGERED_FADE_THRESHOLD);
42     public static final Interpolator ALLAPPS_STAGGERED_FADE_LATE_RESPONDER =
43             Interpolators.clampToProgress(LINEAR, ALLAPPS_STAGGERED_FADE_THRESHOLD, 1f);
44 
AllAppsSwipeController(Launcher l)45     public AllAppsSwipeController(Launcher l) {
46         super(l, SingleAxisSwipeDetector.VERTICAL);
47     }
48 
49     @Override
canInterceptTouch(MotionEvent ev)50     protected boolean canInterceptTouch(MotionEvent ev) {
51         if (mCurrentAnimation != null) {
52             // If we are already animating from a previous state, we can intercept.
53             return true;
54         }
55         if (AbstractFloatingView.getTopOpenView(mLauncher) != null) {
56             return false;
57         }
58         if (!mLauncher.isInState(NORMAL) && !mLauncher.isInState(ALL_APPS)) {
59             // Don't listen for the swipe gesture if we are already in some other state.
60             return false;
61         }
62         if (mLauncher.isInState(ALL_APPS) && !mLauncher.getAppsView().shouldContainerScroll(ev)) {
63             return false;
64         }
65         return true;
66     }
67 
68     @Override
getTargetState(LauncherState fromState, boolean isDragTowardPositive)69     protected LauncherState getTargetState(LauncherState fromState, boolean isDragTowardPositive) {
70         if (fromState == NORMAL && isDragTowardPositive) {
71             return ALL_APPS;
72         } else if (fromState == ALL_APPS && !isDragTowardPositive) {
73             return NORMAL;
74         }
75         return fromState;
76     }
77 
78     @Override
initCurrentAnimation()79     protected float initCurrentAnimation() {
80         float range = getShiftRange();
81         StateAnimationConfig config = getConfigForStates(mFromState, mToState);
82         config.duration = (long) (2 * range);
83 
84         mCurrentAnimation = mLauncher.getStateManager()
85                 .createAnimationToNewWorkspace(mToState, config);
86         float startVerticalShift = mFromState.getVerticalProgress(mLauncher) * range;
87         float endVerticalShift = mToState.getVerticalProgress(mLauncher) * range;
88         float totalShift = endVerticalShift - startVerticalShift;
89         return 1 / totalShift;
90     }
91 
92     @Override
getConfigForStates(LauncherState fromState, LauncherState toState)93     protected StateAnimationConfig getConfigForStates(LauncherState fromState,
94             LauncherState toState) {
95         StateAnimationConfig config = super.getConfigForStates(fromState, toState);
96         if (fromState == NORMAL && toState == ALL_APPS) {
97             applyNormalToAllAppsAnimConfig(config);
98         } else if (fromState == ALL_APPS && toState == NORMAL) {
99             applyAllAppsToNormalConfig(config);
100         }
101         return config;
102     }
103 
104     /**
105      * Applies Animation config values for transition from all apps to home
106      */
applyAllAppsToNormalConfig(StateAnimationConfig config)107     public static void applyAllAppsToNormalConfig(StateAnimationConfig config) {
108         config.setInterpolator(ANIM_SCRIM_FADE, ALLAPPS_STAGGERED_FADE_LATE_RESPONDER);
109         config.setInterpolator(ANIM_ALL_APPS_FADE, ALLAPPS_STAGGERED_FADE_EARLY_RESPONDER);
110     }
111 
112     /**
113      * Applies Animation config values for transition from home to all apps
114      */
applyNormalToAllAppsAnimConfig(StateAnimationConfig config)115     public static void applyNormalToAllAppsAnimConfig(StateAnimationConfig config) {
116         config.setInterpolator(ANIM_SCRIM_FADE, ALLAPPS_STAGGERED_FADE_EARLY_RESPONDER);
117         config.setInterpolator(ANIM_ALL_APPS_FADE, ALLAPPS_STAGGERED_FADE_LATE_RESPONDER);
118     }
119 
120 
121 }
122