1 /*
2  * Copyright (C) 2017 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.uioverrides;
17 
18 import static com.android.launcher3.LauncherState.CLEAR_ALL_BUTTON;
19 import static com.android.launcher3.LauncherState.OVERVIEW_ACTIONS;
20 import static com.android.launcher3.LauncherState.OVERVIEW_SPLIT_SELECT;
21 import static com.android.launcher3.anim.Interpolators.LINEAR;
22 import static com.android.launcher3.states.StateAnimationConfig.ANIM_OVERVIEW_ACTIONS_FADE;
23 import static com.android.quickstep.views.RecentsView.CONTENT_ALPHA;
24 import static com.android.quickstep.views.RecentsView.FULLSCREEN_PROGRESS;
25 import static com.android.quickstep.views.RecentsView.TASK_MODALNESS;
26 import static com.android.quickstep.views.RecentsView.TASK_PRIMARY_SPLIT_TRANSLATION;
27 import static com.android.quickstep.views.RecentsView.TASK_SECONDARY_SPLIT_TRANSLATION;
28 import static com.android.quickstep.views.TaskView.FLAG_UPDATE_ALL;
29 
30 import android.annotation.TargetApi;
31 import android.os.Build;
32 import android.util.FloatProperty;
33 import android.util.Pair;
34 
35 import androidx.annotation.NonNull;
36 import androidx.annotation.Nullable;
37 
38 import com.android.launcher3.BaseQuickstepLauncher;
39 import com.android.launcher3.LauncherState;
40 import com.android.launcher3.anim.AnimatorListeners;
41 import com.android.launcher3.anim.PendingAnimation;
42 import com.android.launcher3.anim.PropertySetter;
43 import com.android.launcher3.states.StateAnimationConfig;
44 import com.android.launcher3.touch.PagedOrientationHandler;
45 import com.android.launcher3.util.MultiValueAlpha;
46 import com.android.quickstep.views.ClearAllButton;
47 import com.android.quickstep.views.LauncherRecentsView;
48 import com.android.quickstep.views.RecentsView;
49 
50 /**
51  * State handler for handling UI changes for {@link LauncherRecentsView}. In addition to managing
52  * the basic view properties, this class also manages changes in the task visuals.
53  */
54 @TargetApi(Build.VERSION_CODES.O)
55 public final class RecentsViewStateController extends
56         BaseRecentsViewStateController<LauncherRecentsView> {
57 
RecentsViewStateController(BaseQuickstepLauncher launcher)58     public RecentsViewStateController(BaseQuickstepLauncher launcher) {
59         super(launcher);
60     }
61 
62     @Override
setState(@onNull LauncherState state)63     public void setState(@NonNull LauncherState state) {
64         super.setState(state);
65         if (state.overviewUi) {
66             mRecentsView.updateEmptyMessage();
67             mRecentsView.resetTaskVisuals();
68         }
69         setAlphas(PropertySetter.NO_ANIM_PROPERTY_SETTER, new StateAnimationConfig(), state);
70         mRecentsView.setFullscreenProgress(state.getOverviewFullscreenProgress());
71         // In Overview, we may be layering app surfaces behind Launcher, so we need to notify
72         // DepthController to prevent optimizations which might occlude the layers behind
73         mLauncher.getDepthController().setHasContentBehindLauncher(state.overviewUi);
74 
75         handleSplitSelectionState(state, null);
76     }
77 
78     @Override
setStateWithAnimationInternal(@onNull LauncherState toState, @NonNull StateAnimationConfig config, @NonNull PendingAnimation builder)79     void setStateWithAnimationInternal(@NonNull LauncherState toState,
80             @NonNull StateAnimationConfig config, @NonNull PendingAnimation builder) {
81         super.setStateWithAnimationInternal(toState, config, builder);
82 
83         if (toState.overviewUi) {
84             // While animating into recents, update the visible task data as needed
85             builder.addOnFrameCallback(() -> mRecentsView.loadVisibleTaskData(FLAG_UPDATE_ALL));
86             mRecentsView.updateEmptyMessage();
87         } else {
88             builder.addListener(
89                     AnimatorListeners.forSuccessCallback(mRecentsView::resetTaskVisuals));
90         }
91         // In Overview, we may be layering app surfaces behind Launcher, so we need to notify
92         // DepthController to prevent optimizations which might occlude the layers behind
93         builder.addListener(AnimatorListeners.forSuccessCallback(() ->
94                 mLauncher.getDepthController().setHasContentBehindLauncher(toState.overviewUi)));
95 
96         handleSplitSelectionState(toState, builder);
97 
98         setAlphas(builder, config, toState);
99         builder.setFloat(mRecentsView, FULLSCREEN_PROGRESS,
100                 toState.getOverviewFullscreenProgress(), LINEAR);
101     }
102 
103     /**
104      * Create or dismiss split screen select animations.
105      * @param builder if null then this will run the split select animations right away, otherwise
106      *                will add animations to builder.
107      */
handleSplitSelectionState(@onNull LauncherState toState, @Nullable PendingAnimation builder)108     private void handleSplitSelectionState(@NonNull LauncherState toState,
109             @Nullable PendingAnimation builder) {
110         LauncherState currentState = mLauncher.getStateManager().getState();
111         boolean animate = builder != null;
112         PagedOrientationHandler orientationHandler =
113                 ((RecentsView) mLauncher.getOverviewPanel()).getPagedOrientationHandler();
114         Pair<FloatProperty, FloatProperty> taskViewsFloat =
115                 orientationHandler.getSplitSelectTaskOffset(
116                         TASK_PRIMARY_SPLIT_TRANSLATION, TASK_SECONDARY_SPLIT_TRANSLATION,
117                         mLauncher.getDeviceProfile());
118 
119         if (isSplitSelectionState(currentState, toState)) {
120             // Animation to "dismiss" selected taskView
121             PendingAnimation splitSelectInitAnimation =
122                     mRecentsView.createSplitSelectInitAnimation();
123             // Add properties to shift remaining taskViews to get out of placeholder view
124             splitSelectInitAnimation.setFloat(mRecentsView, taskViewsFloat.first,
125                     toState.getSplitSelectTranslation(mLauncher), LINEAR);
126             splitSelectInitAnimation.setFloat(mRecentsView, taskViewsFloat.second, 0, LINEAR);
127 
128             if (!animate && isSplitSelectionState(currentState, toState)) {
129                 splitSelectInitAnimation.buildAnim().start();
130             } else if (animate &&
131                     isSplitSelectionState(currentState, toState)) {
132                 builder.add(splitSelectInitAnimation.buildAnim());
133             }
134         }
135 
136         if (isSplitSelectionState(currentState, toState)) {
137             mRecentsView.applySplitPrimaryScrollOffset();
138         } else {
139             mRecentsView.resetSplitPrimaryScrollOffset();
140         }
141     }
142 
143     /**
144      * @return true if {@param toState} is {@link LauncherState#OVERVIEW_SPLIT_SELECT}
145      *          and {@param fromState} is not {@link LauncherState#OVERVIEW_SPLIT_SELECT}
146      */
isSplitSelectionState(@onNull LauncherState fromState, @NonNull LauncherState toState)147     private boolean isSplitSelectionState(@NonNull LauncherState fromState,
148             @NonNull LauncherState toState) {
149         return fromState != OVERVIEW_SPLIT_SELECT && toState == OVERVIEW_SPLIT_SELECT;
150     }
151 
setAlphas(PropertySetter propertySetter, StateAnimationConfig config, LauncherState state)152     private void setAlphas(PropertySetter propertySetter, StateAnimationConfig config,
153             LauncherState state) {
154         float clearAllButtonAlpha = state.areElementsVisible(mLauncher, CLEAR_ALL_BUTTON) ? 1 : 0;
155         propertySetter.setFloat(mRecentsView.getClearAllButton(), ClearAllButton.VISIBILITY_ALPHA,
156                 clearAllButtonAlpha, LINEAR);
157         float overviewButtonAlpha = state.areElementsVisible(mLauncher, OVERVIEW_ACTIONS) ? 1 : 0;
158         propertySetter.setFloat(mLauncher.getActionsView().getVisibilityAlpha(),
159                 MultiValueAlpha.VALUE, overviewButtonAlpha, config.getInterpolator(
160                         ANIM_OVERVIEW_ACTIONS_FADE, LINEAR));
161     }
162 
163     @Override
getTaskModalnessProperty()164     FloatProperty<RecentsView> getTaskModalnessProperty() {
165         return TASK_MODALNESS;
166     }
167 
168     @Override
getContentAlphaProperty()169     FloatProperty<RecentsView> getContentAlphaProperty() {
170         return CONTENT_ALPHA;
171     }
172 }
173