1 /*
2  * Copyright (C) 2019 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 
17 package com.android.launcher3.uioverrides;
18 
19 import static com.android.launcher3.anim.Interpolators.AGGRESSIVE_EASE_IN_OUT;
20 import static com.android.launcher3.anim.Interpolators.FINAL_FRAME;
21 import static com.android.launcher3.anim.Interpolators.INSTANT;
22 import static com.android.launcher3.anim.Interpolators.LINEAR;
23 import static com.android.launcher3.states.StateAnimationConfig.ANIM_OVERVIEW_FADE;
24 import static com.android.launcher3.states.StateAnimationConfig.ANIM_OVERVIEW_MODAL;
25 import static com.android.launcher3.states.StateAnimationConfig.ANIM_OVERVIEW_SCALE;
26 import static com.android.launcher3.states.StateAnimationConfig.ANIM_OVERVIEW_TRANSLATE_X;
27 import static com.android.launcher3.states.StateAnimationConfig.ANIM_OVERVIEW_TRANSLATE_Y;
28 import static com.android.launcher3.states.StateAnimationConfig.SKIP_OVERVIEW;
29 import static com.android.quickstep.views.RecentsView.ADJACENT_PAGE_HORIZONTAL_OFFSET;
30 import static com.android.quickstep.views.RecentsView.RECENTS_GRID_PROGRESS;
31 import static com.android.quickstep.views.RecentsView.RECENTS_SCALE_PROPERTY;
32 import static com.android.quickstep.views.RecentsView.TASK_SECONDARY_TRANSLATION;
33 
34 import android.util.FloatProperty;
35 
36 import androidx.annotation.NonNull;
37 
38 import com.android.launcher3.BaseQuickstepLauncher;
39 import com.android.launcher3.LauncherState;
40 import com.android.launcher3.anim.PendingAnimation;
41 import com.android.launcher3.statemanager.StateManager.StateHandler;
42 import com.android.launcher3.states.StateAnimationConfig;
43 import com.android.quickstep.views.RecentsView;
44 
45 /**
46  * State handler for recents view. Manages UI changes and animations for recents view based off the
47  * current {@link LauncherState}.
48  *
49  * @param <T> the recents view
50  */
51 public abstract class BaseRecentsViewStateController<T extends RecentsView>
52         implements StateHandler<LauncherState> {
53     protected final T mRecentsView;
54     protected final BaseQuickstepLauncher mLauncher;
55 
BaseRecentsViewStateController(@onNull BaseQuickstepLauncher launcher)56     public BaseRecentsViewStateController(@NonNull BaseQuickstepLauncher launcher) {
57         mLauncher = launcher;
58         mRecentsView = launcher.getOverviewPanel();
59     }
60 
61     @Override
setState(@onNull LauncherState state)62     public void setState(@NonNull LauncherState state) {
63         float[] scaleAndOffset = state.getOverviewScaleAndOffset(mLauncher);
64         RECENTS_SCALE_PROPERTY.set(mRecentsView, scaleAndOffset[0]);
65         ADJACENT_PAGE_HORIZONTAL_OFFSET.set(mRecentsView, scaleAndOffset[1]);
66         TASK_SECONDARY_TRANSLATION.set(mRecentsView, 0f);
67 
68         getContentAlphaProperty().set(mRecentsView, state.overviewUi ? 1f : 0);
69         getTaskModalnessProperty().set(mRecentsView, state.getOverviewModalness());
70         RECENTS_GRID_PROGRESS.set(mRecentsView,
71                 state.displayOverviewTasksAsGrid(mLauncher.getDeviceProfile()) ? 1f : 0f);
72     }
73 
74     @Override
setStateWithAnimation(LauncherState toState, StateAnimationConfig config, PendingAnimation builder)75     public void setStateWithAnimation(LauncherState toState, StateAnimationConfig config,
76             PendingAnimation builder) {
77         if (config.hasAnimationFlag(SKIP_OVERVIEW)) {
78             return;
79         }
80         setStateWithAnimationInternal(toState, config, builder);
81     }
82 
83     /**
84      * Core logic for animating the recents view UI.
85      *
86      * @param toState state to animate to
87      * @param config current animation config
88      * @param setter animator set builder
89      */
setStateWithAnimationInternal(@onNull final LauncherState toState, @NonNull StateAnimationConfig config, @NonNull PendingAnimation setter)90     void setStateWithAnimationInternal(@NonNull final LauncherState toState,
91             @NonNull StateAnimationConfig config, @NonNull PendingAnimation setter) {
92         float[] scaleAndOffset = toState.getOverviewScaleAndOffset(mLauncher);
93         setter.setFloat(mRecentsView, RECENTS_SCALE_PROPERTY, scaleAndOffset[0],
94                 config.getInterpolator(ANIM_OVERVIEW_SCALE, LINEAR));
95         setter.setFloat(mRecentsView, ADJACENT_PAGE_HORIZONTAL_OFFSET, scaleAndOffset[1],
96                 config.getInterpolator(ANIM_OVERVIEW_TRANSLATE_X, LINEAR));
97         setter.setFloat(mRecentsView, TASK_SECONDARY_TRANSLATION, 0f,
98                 config.getInterpolator(ANIM_OVERVIEW_TRANSLATE_Y, LINEAR));
99 
100         setter.setFloat(mRecentsView, getContentAlphaProperty(), toState.overviewUi ? 1 : 0,
101                 config.getInterpolator(ANIM_OVERVIEW_FADE, AGGRESSIVE_EASE_IN_OUT));
102 
103         setter.setFloat(
104                 mRecentsView, getTaskModalnessProperty(),
105                 toState.getOverviewModalness(),
106                 config.getInterpolator(ANIM_OVERVIEW_MODAL, LINEAR));
107         boolean showAsGrid = toState.displayOverviewTasksAsGrid(mLauncher.getDeviceProfile());
108         setter.setFloat(mRecentsView, RECENTS_GRID_PROGRESS, showAsGrid ? 1f : 0f,
109                 showAsGrid ? INSTANT : FINAL_FRAME);
110     }
111 
getTaskModalnessProperty()112     abstract FloatProperty getTaskModalnessProperty();
113 
114     /**
115      * Get property for content alpha for the recents view.
116      *
117      * @return the float property for the view's content alpha
118      */
getContentAlphaProperty()119     abstract FloatProperty getContentAlphaProperty();
120 }
121