summaryrefslogtreecommitdiff
path: root/src/com/android/launcher3/touch/AllAppsSwipeController.java
blob: 447d22b59145e7b68dc43feed56d5121d978a889 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.launcher3.touch;

import static com.android.app.animation.Interpolators.DECELERATED_EASE;
import static com.android.app.animation.Interpolators.EMPHASIZED;
import static com.android.app.animation.Interpolators.EMPHASIZED_ACCELERATE;
import static com.android.app.animation.Interpolators.EMPHASIZED_DECELERATE;
import static com.android.app.animation.Interpolators.FINAL_FRAME;
import static com.android.app.animation.Interpolators.INSTANT;
import static com.android.app.animation.Interpolators.LINEAR;
import static com.android.app.animation.Interpolators.clampToProgress;
import static com.android.app.animation.Interpolators.mapToProgress;
import static com.android.launcher3.LauncherState.ALL_APPS;
import static com.android.launcher3.LauncherState.NORMAL;
import static com.android.launcher3.states.StateAnimationConfig.ANIM_ALL_APPS_BOTTOM_SHEET_FADE;
import static com.android.launcher3.states.StateAnimationConfig.ANIM_ALL_APPS_FADE;
import static com.android.launcher3.states.StateAnimationConfig.ANIM_ALL_APPS_KEYBOARD_FADE;
import static com.android.launcher3.states.StateAnimationConfig.ANIM_DEPTH;
import static com.android.launcher3.states.StateAnimationConfig.ANIM_HOTSEAT_FADE;
import static com.android.launcher3.states.StateAnimationConfig.ANIM_HOTSEAT_SCALE;
import static com.android.launcher3.states.StateAnimationConfig.ANIM_HOTSEAT_TRANSLATE;
import static com.android.launcher3.states.StateAnimationConfig.ANIM_SCRIM_FADE;
import static com.android.launcher3.states.StateAnimationConfig.ANIM_VERTICAL_PROGRESS;
import static com.android.launcher3.states.StateAnimationConfig.ANIM_WORKSPACE_FADE;
import static com.android.launcher3.states.StateAnimationConfig.ANIM_WORKSPACE_SCALE;
import static com.android.launcher3.states.StateAnimationConfig.ANIM_WORKSPACE_TRANSLATE;
import static com.android.launcher3.states.StateAnimationConfig.SKIP_OVERVIEW;

import android.view.MotionEvent;
import android.view.animation.Interpolator;

import com.android.app.animation.Interpolators;
import com.android.launcher3.AbstractFloatingView;
import com.android.launcher3.DeviceProfile;
import com.android.launcher3.Launcher;
import com.android.launcher3.LauncherState;
import com.android.launcher3.states.StateAnimationConfig;

/**
 * TouchController to switch between NORMAL and ALL_APPS state.
 */
public class AllAppsSwipeController extends AbstractStateChangeTouchController {

    private static final float ALL_APPS_CONTENT_FADE_MAX_CLAMPING_THRESHOLD = 0.8f;
    private static final float ALL_APPS_CONTENT_FADE_MIN_CLAMPING_THRESHOLD = 0.5f;
    private static final float ALL_APPS_SCRIM_VISIBLE_THRESHOLD = 0.1f;
    private static final float ALL_APPS_STAGGERED_FADE_THRESHOLD = 0.5f;

    public static final Interpolator ALL_APPS_SCRIM_RESPONDER =
            Interpolators.clampToProgress(
                    LINEAR, ALL_APPS_SCRIM_VISIBLE_THRESHOLD, ALL_APPS_STAGGERED_FADE_THRESHOLD);
    public static final Interpolator ALL_APPS_CLAMPING_RESPONDER =
            Interpolators.clampToProgress(
                    LINEAR,
                    1 - ALL_APPS_CONTENT_FADE_MAX_CLAMPING_THRESHOLD,
                    1 - ALL_APPS_CONTENT_FADE_MIN_CLAMPING_THRESHOLD);

    // ---- Custom interpolators for NORMAL -> ALL_APPS on phones only. ----

    public static final float ALL_APPS_STATE_TRANSITION_ATOMIC = 0.3333f;
    public static final float ALL_APPS_STATE_TRANSITION_MANUAL = 0.4f;
    private static final float ALL_APPS_FADE_END_ATOMIC = 0.8333f;
    private static final float ALL_APPS_FADE_END_MANUAL = 0.8f;
    private static final float ALL_APPS_FULL_DEPTH_PROGRESS = 0.5f;
    private static final float SCRIM_FADE_START_ATOMIC = 0.2642f;
    private static final float SCRIM_FADE_START_MANUAL = 0.117f;
    private static final float WORKSPACE_MOTION_START_ATOMIC = 0.1667f;

    private static final Interpolator LINEAR_EARLY_MANUAL =
            Interpolators.clampToProgress(LINEAR, 0f, ALL_APPS_STATE_TRANSITION_MANUAL);
    private static final Interpolator STEP_TRANSITION_ATOMIC =
            Interpolators.clampToProgress(FINAL_FRAME, 0f, ALL_APPS_STATE_TRANSITION_ATOMIC);
    private static final Interpolator STEP_TRANSITION_MANUAL =
            Interpolators.clampToProgress(FINAL_FRAME, 0f, ALL_APPS_STATE_TRANSITION_MANUAL);

    // The blur to All Apps is set to be complete when the interpolator is at 0.5.
    private static final Interpolator BLUR_ADJUSTED =
            Interpolators.mapToProgress(LINEAR, 0f, ALL_APPS_FULL_DEPTH_PROGRESS);
    public static final Interpolator BLUR_ATOMIC =
            Interpolators.clampToProgress(
                    BLUR_ADJUSTED, WORKSPACE_MOTION_START_ATOMIC, ALL_APPS_STATE_TRANSITION_ATOMIC);
    public static final Interpolator BLUR_MANUAL =
            Interpolators.clampToProgress(BLUR_ADJUSTED, 0f, ALL_APPS_STATE_TRANSITION_MANUAL);

    public static final Interpolator WORKSPACE_FADE_ATOMIC = STEP_TRANSITION_ATOMIC;
    public static final Interpolator WORKSPACE_FADE_MANUAL = STEP_TRANSITION_MANUAL;

    public static final Interpolator WORKSPACE_SCALE_ATOMIC =
            Interpolators.clampToProgress(
                    EMPHASIZED_ACCELERATE, WORKSPACE_MOTION_START_ATOMIC,
                    ALL_APPS_STATE_TRANSITION_ATOMIC);
    public static final Interpolator WORKSPACE_SCALE_MANUAL = LINEAR_EARLY_MANUAL;

    public static final Interpolator HOTSEAT_FADE_ATOMIC = STEP_TRANSITION_ATOMIC;
    public static final Interpolator HOTSEAT_FADE_MANUAL = STEP_TRANSITION_MANUAL;

    public static final Interpolator HOTSEAT_SCALE_ATOMIC =
            Interpolators.clampToProgress(
                    EMPHASIZED_ACCELERATE, WORKSPACE_MOTION_START_ATOMIC,
                    ALL_APPS_STATE_TRANSITION_ATOMIC);
    public static final Interpolator HOTSEAT_SCALE_MANUAL = LINEAR_EARLY_MANUAL;

    public static final Interpolator HOTSEAT_TRANSLATE_ATOMIC = STEP_TRANSITION_ATOMIC;
    public static final Interpolator HOTSEAT_TRANSLATE_MANUAL = STEP_TRANSITION_MANUAL;

    public static final Interpolator SCRIM_FADE_ATOMIC =
            Interpolators.clampToProgress(
                    Interpolators.mapToProgress(LINEAR, 0f, 0.8f),
                    SCRIM_FADE_START_ATOMIC, ALL_APPS_STATE_TRANSITION_ATOMIC);
    public static final Interpolator SCRIM_FADE_MANUAL =
            Interpolators.clampToProgress(
                    LINEAR, SCRIM_FADE_START_MANUAL, ALL_APPS_STATE_TRANSITION_MANUAL);

    public static final Interpolator ALL_APPS_FADE_ATOMIC =
            Interpolators.clampToProgress(
                    Interpolators.mapToProgress(EMPHASIZED_DECELERATE, 0.2f, 1f),
                    ALL_APPS_STATE_TRANSITION_ATOMIC, ALL_APPS_FADE_END_ATOMIC);
    public static final Interpolator ALL_APPS_FADE_MANUAL =
            Interpolators.clampToProgress(
                    LINEAR, ALL_APPS_STATE_TRANSITION_MANUAL, ALL_APPS_FADE_END_MANUAL);

    public static final Interpolator ALL_APPS_VERTICAL_PROGRESS_ATOMIC =
            Interpolators.clampToProgress(
                    Interpolators.mapToProgress(EMPHASIZED_DECELERATE, 0.4f, 1f),
                    ALL_APPS_STATE_TRANSITION_ATOMIC, 1f);
    public static final Interpolator ALL_APPS_VERTICAL_PROGRESS_MANUAL = LINEAR;

    // --------

    public AllAppsSwipeController(Launcher l) {
        super(l, SingleAxisSwipeDetector.VERTICAL);
    }

    @Override
    protected boolean canInterceptTouch(MotionEvent ev) {
        if (mCurrentAnimation != null) {
            // If we are already animating from a previous state, we can intercept.
            return true;
        }
        if (AbstractFloatingView.getTopOpenView(mLauncher) != null) {
            return false;
        }
        if (!mLauncher.isInState(NORMAL) && !mLauncher.isInState(ALL_APPS)) {
            // Don't listen for the swipe gesture if we are already in some other state.
            return false;
        }
        if (mLauncher.isInState(ALL_APPS) && !mLauncher.getAppsView().shouldContainerScroll(ev)) {
            return false;
        }
        return true;
    }

    @Override
    protected LauncherState getTargetState(LauncherState fromState, boolean isDragTowardPositive) {
        if (fromState == NORMAL && isDragTowardPositive) {
            return ALL_APPS;
        } else if (fromState == ALL_APPS && !isDragTowardPositive) {
            return NORMAL;
        }
        return fromState;
    }

    @Override
    protected float initCurrentAnimation() {
        float range = getShiftRange();
        StateAnimationConfig config = getConfigForStates(mFromState, mToState);
        config.duration = (long) (2 * range);

        mCurrentAnimation = mLauncher.getStateManager()
                .createAnimationToNewWorkspace(mToState, config);
        float startVerticalShift = mFromState.getVerticalProgress(mLauncher) * range;
        float endVerticalShift = mToState.getVerticalProgress(mLauncher) * range;
        float totalShift = endVerticalShift - startVerticalShift;
        return 1 / totalShift;
    }

    @Override
    protected StateAnimationConfig getConfigForStates(LauncherState fromState,
            LauncherState toState) {
        StateAnimationConfig config = super.getConfigForStates(fromState, toState);
        config.userControlled = true;
        if (fromState == NORMAL && toState == ALL_APPS) {
            applyNormalToAllAppsAnimConfig(mLauncher, config);
        } else if (fromState == ALL_APPS && toState == NORMAL) {
            applyAllAppsToNormalConfig(mLauncher, config);
        }
        return config;
    }

    /**
     * Applies Animation config values for transition from all apps to home.
     */
    public static void applyAllAppsToNormalConfig(Launcher launcher, StateAnimationConfig config) {
        if (launcher.getDeviceProfile().isTablet) {
            config.setInterpolator(ANIM_SCRIM_FADE,
                    Interpolators.reverse(ALL_APPS_SCRIM_RESPONDER));
            config.setInterpolator(ANIM_ALL_APPS_FADE, FINAL_FRAME);
            if (!config.userControlled) {
                config.setInterpolator(ANIM_VERTICAL_PROGRESS, EMPHASIZED);
            }
            config.setInterpolator(ANIM_WORKSPACE_SCALE, DECELERATED_EASE);
            config.setInterpolator(ANIM_DEPTH, DECELERATED_EASE);
        } else {
            if (config.userControlled) {
                config.setInterpolator(ANIM_DEPTH, Interpolators.reverse(BLUR_MANUAL));
                config.setInterpolator(ANIM_WORKSPACE_FADE,
                        Interpolators.reverse(WORKSPACE_FADE_MANUAL));
                config.setInterpolator(ANIM_WORKSPACE_SCALE,
                        Interpolators.reverse(WORKSPACE_SCALE_MANUAL));
                config.setInterpolator(ANIM_HOTSEAT_FADE,
                        Interpolators.reverse(HOTSEAT_FADE_MANUAL));
                config.setInterpolator(ANIM_HOTSEAT_SCALE,
                        Interpolators.reverse(HOTSEAT_SCALE_MANUAL));
                config.setInterpolator(ANIM_HOTSEAT_TRANSLATE,
                        Interpolators.reverse(HOTSEAT_TRANSLATE_MANUAL));
                config.setInterpolator(ANIM_SCRIM_FADE, Interpolators.reverse(SCRIM_FADE_MANUAL));
                config.setInterpolator(ANIM_ALL_APPS_FADE,
                        Interpolators.reverse(ALL_APPS_FADE_MANUAL));
                config.setInterpolator(ANIM_VERTICAL_PROGRESS,
                        Interpolators.reverse(ALL_APPS_VERTICAL_PROGRESS_MANUAL));
            } else {
                config.setInterpolator(ANIM_SCRIM_FADE,
                        Interpolators.reverse(ALL_APPS_SCRIM_RESPONDER));
                config.setInterpolator(ANIM_ALL_APPS_FADE, ALL_APPS_CLAMPING_RESPONDER);
                config.setInterpolator(ANIM_WORKSPACE_FADE, INSTANT);
                config.setInterpolator(ANIM_VERTICAL_PROGRESS, EMPHASIZED_ACCELERATE);
            }
        }
    }

    /**
     * Applies Animation config values for transition from home to all apps.
     */
    public static void applyNormalToAllAppsAnimConfig(
            Launcher launcher, StateAnimationConfig config) {
        if (launcher.getDeviceProfile().isTablet) {
            config.setInterpolator(ANIM_ALL_APPS_FADE, INSTANT);
            config.setInterpolator(ANIM_SCRIM_FADE, ALL_APPS_SCRIM_RESPONDER);
            if (!config.userControlled) {
                config.setInterpolator(ANIM_VERTICAL_PROGRESS, EMPHASIZED);
            }
            config.setInterpolator(ANIM_WORKSPACE_SCALE, DECELERATED_EASE);
            config.setInterpolator(ANIM_DEPTH, DECELERATED_EASE);
        } else {
            config.setInterpolator(ANIM_DEPTH, config.userControlled ? BLUR_MANUAL : BLUR_ATOMIC);
            config.setInterpolator(ANIM_WORKSPACE_FADE,
                    config.userControlled ? WORKSPACE_FADE_MANUAL : WORKSPACE_FADE_ATOMIC);
            config.setInterpolator(ANIM_WORKSPACE_SCALE,
                    config.userControlled ? WORKSPACE_SCALE_MANUAL : WORKSPACE_SCALE_ATOMIC);
            config.setInterpolator(ANIM_HOTSEAT_FADE,
                    config.userControlled ? HOTSEAT_FADE_MANUAL : HOTSEAT_FADE_ATOMIC);
            config.setInterpolator(ANIM_HOTSEAT_SCALE,
                    config.userControlled ? HOTSEAT_SCALE_MANUAL : HOTSEAT_SCALE_ATOMIC);
            config.setInterpolator(ANIM_HOTSEAT_TRANSLATE,
                    config.userControlled ? HOTSEAT_TRANSLATE_MANUAL : HOTSEAT_TRANSLATE_ATOMIC);
            config.setInterpolator(ANIM_SCRIM_FADE,
                    config.userControlled ? SCRIM_FADE_MANUAL : SCRIM_FADE_ATOMIC);
            config.setInterpolator(ANIM_ALL_APPS_FADE,
                    config.userControlled ? ALL_APPS_FADE_MANUAL : ALL_APPS_FADE_ATOMIC);
            config.setInterpolator(ANIM_VERTICAL_PROGRESS,
                    config.userControlled
                            ? ALL_APPS_VERTICAL_PROGRESS_MANUAL
                            : ALL_APPS_VERTICAL_PROGRESS_ATOMIC);
        }
    }

    /**
     * Applies Animation config values for transition from overview to all apps.
     *
     * @param threshold progress at which all apps will open upon release
     */
    public static void applyOverviewToAllAppsAnimConfig(
            DeviceProfile deviceProfile, StateAnimationConfig config, float threshold) {
        config.userControlled = true;
        config.animFlags = SKIP_OVERVIEW;
        if (deviceProfile.isTablet) {
            config.setInterpolator(ANIM_ALL_APPS_FADE, INSTANT);
            config.setInterpolator(ANIM_SCRIM_FADE, ALL_APPS_SCRIM_RESPONDER);
            // The fact that we end on Workspace is not very ideal, but since we do, fade it in at
            // the end of the transition. Don't scale/translate it.
            config.setInterpolator(ANIM_WORKSPACE_FADE, clampToProgress(LINEAR, 0.8f, 1));
            config.setInterpolator(ANIM_WORKSPACE_SCALE, INSTANT);
            config.setInterpolator(ANIM_WORKSPACE_TRANSLATE, INSTANT);
        } else {
            // Pop the background panel, keyboard, and content in at full opacity at the threshold.
            config.setInterpolator(ANIM_ALL_APPS_BOTTOM_SHEET_FADE,
                    thresholdInterpolator(threshold, INSTANT));
            config.setInterpolator(ANIM_ALL_APPS_KEYBOARD_FADE,
                    thresholdInterpolator(threshold, INSTANT));
            config.setInterpolator(ANIM_ALL_APPS_FADE, thresholdInterpolator(threshold, INSTANT));

            config.setInterpolator(ANIM_VERTICAL_PROGRESS,
                    thresholdInterpolator(threshold, mapToProgress(LINEAR, threshold, 1f)));
        }
    }

    /** Creates an interpolator that is 0 until the threshold, then follows given interpolator. */
    private static Interpolator thresholdInterpolator(float threshold, Interpolator interpolator) {
        return progress -> progress <= threshold ? 0 : interpolator.getInterpolation(progress);
    }
}