• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.launcher3;
18 
19 import static androidx.dynamicanimation.animation.DynamicAnimation.MIN_VISIBLE_CHANGE_SCALE;
20 
21 import static com.android.launcher3.LauncherAnimUtils.HOTSEAT_SCALE_PROPERTY_FACTORY;
22 import static com.android.launcher3.LauncherAnimUtils.SCALE_INDEX_WORKSPACE_STATE;
23 import static com.android.launcher3.LauncherAnimUtils.VIEW_ALPHA;
24 import static com.android.launcher3.LauncherAnimUtils.VIEW_TRANSLATE_X;
25 import static com.android.launcher3.LauncherAnimUtils.VIEW_TRANSLATE_Y;
26 import static com.android.launcher3.LauncherAnimUtils.WORKSPACE_SCALE_PROPERTY_FACTORY;
27 import static com.android.launcher3.LauncherState.FLAG_HAS_SYS_UI_SCRIM;
28 import static com.android.launcher3.LauncherState.FLAG_HOTSEAT_INACCESSIBLE;
29 import static com.android.launcher3.LauncherState.HINT_STATE;
30 import static com.android.launcher3.LauncherState.HOTSEAT_ICONS;
31 import static com.android.launcher3.LauncherState.NORMAL;
32 import static com.android.launcher3.LauncherState.SPRING_LOADED;
33 import static com.android.launcher3.LauncherState.WORKSPACE_PAGE_INDICATOR;
34 import static com.android.launcher3.anim.Interpolators.ACCEL_2;
35 import static com.android.launcher3.anim.Interpolators.LINEAR;
36 import static com.android.launcher3.anim.Interpolators.ZOOM_OUT;
37 import static com.android.launcher3.anim.PropertySetter.NO_ANIM_PROPERTY_SETTER;
38 import static com.android.launcher3.config.FeatureFlags.HOME_GARDENING_WORKSPACE_BUTTONS;
39 import static com.android.launcher3.config.FeatureFlags.SHOW_HOME_GARDENING;
40 import static com.android.launcher3.graphics.Scrim.SCRIM_PROGRESS;
41 import static com.android.launcher3.graphics.SysUiScrim.SYSUI_PROGRESS;
42 import static com.android.launcher3.states.StateAnimationConfig.ANIM_HOTSEAT_FADE;
43 import static com.android.launcher3.states.StateAnimationConfig.ANIM_HOTSEAT_SCALE;
44 import static com.android.launcher3.states.StateAnimationConfig.ANIM_HOTSEAT_TRANSLATE;
45 import static com.android.launcher3.states.StateAnimationConfig.ANIM_SCRIM_FADE;
46 import static com.android.launcher3.states.StateAnimationConfig.ANIM_WORKSPACE_FADE;
47 import static com.android.launcher3.states.StateAnimationConfig.ANIM_WORKSPACE_PAGE_TRANSLATE_X;
48 import static com.android.launcher3.states.StateAnimationConfig.ANIM_WORKSPACE_SCALE;
49 import static com.android.launcher3.states.StateAnimationConfig.ANIM_WORKSPACE_TRANSLATE;
50 import static com.android.launcher3.states.StateAnimationConfig.SKIP_SCRIM;
51 
52 import android.animation.ValueAnimator;
53 import android.util.FloatProperty;
54 import android.view.View;
55 import android.view.animation.Interpolator;
56 
57 import com.android.launcher3.LauncherState.PageAlphaProvider;
58 import com.android.launcher3.LauncherState.PageTranslationProvider;
59 import com.android.launcher3.LauncherState.ScaleAndTranslation;
60 import com.android.launcher3.anim.PendingAnimation;
61 import com.android.launcher3.anim.PropertySetter;
62 import com.android.launcher3.anim.SpringAnimationBuilder;
63 import com.android.launcher3.graphics.Scrim;
64 import com.android.launcher3.graphics.SysUiScrim;
65 import com.android.launcher3.states.SpringLoadedState;
66 import com.android.launcher3.states.StateAnimationConfig;
67 import com.android.launcher3.util.DynamicResource;
68 import com.android.systemui.plugins.ResourceProvider;
69 
70 /**
71  * Manages the animations between each of the workspace states.
72  */
73 public class WorkspaceStateTransitionAnimation {
74 
75     private static final float FIRST_PAGE_PINNED_WIDGET_DISABLED_ALPHA = 0.3f;
76 
77     private static final FloatProperty<Workspace<?>> WORKSPACE_SCALE_PROPERTY =
78             WORKSPACE_SCALE_PROPERTY_FACTORY.get(SCALE_INDEX_WORKSPACE_STATE);
79 
80     private static final FloatProperty<Hotseat> HOTSEAT_SCALE_PROPERTY =
81             HOTSEAT_SCALE_PROPERTY_FACTORY.get(SCALE_INDEX_WORKSPACE_STATE);
82 
83     private final Launcher mLauncher;
84     private final Workspace<?> mWorkspace;
85 
86     private float mNewScale;
87 
WorkspaceStateTransitionAnimation(Launcher launcher, Workspace<?> workspace)88     public WorkspaceStateTransitionAnimation(Launcher launcher, Workspace<?> workspace) {
89         mLauncher = launcher;
90         mWorkspace = workspace;
91     }
92 
setState(LauncherState toState)93     public void setState(LauncherState toState) {
94         setWorkspaceProperty(toState, NO_ANIM_PROPERTY_SETTER, new StateAnimationConfig());
95     }
96 
97     /**
98      * @see com.android.launcher3.statemanager.StateManager.StateHandler#setStateWithAnimation
99      */
setStateWithAnimation( LauncherState toState, StateAnimationConfig config, PendingAnimation animation)100     public void setStateWithAnimation(
101             LauncherState toState, StateAnimationConfig config, PendingAnimation animation) {
102         setWorkspaceProperty(toState, animation, config);
103     }
104 
getFinalScale()105     public float getFinalScale() {
106         return mNewScale;
107     }
108 
109     /**
110      * Starts a transition animation for the workspace.
111      */
setWorkspaceProperty(LauncherState state, PropertySetter propertySetter, StateAnimationConfig config)112     private void setWorkspaceProperty(LauncherState state, PropertySetter propertySetter,
113             StateAnimationConfig config) {
114         ScaleAndTranslation scaleAndTranslation = state.getWorkspaceScaleAndTranslation(mLauncher);
115         ScaleAndTranslation hotseatScaleAndTranslation = state.getHotseatScaleAndTranslation(
116                 mLauncher);
117         mNewScale = scaleAndTranslation.scale;
118         PageAlphaProvider pageAlphaProvider = state.getWorkspacePageAlphaProvider(mLauncher);
119         final int childCount = mWorkspace.getChildCount();
120         for (int i = 0; i < childCount; i++) {
121             applyChildState(state, (CellLayout) mWorkspace.getChildAt(i), i, pageAlphaProvider,
122                     propertySetter, config);
123         }
124 
125         int elements = state.getVisibleElements(mLauncher);
126         Hotseat hotseat = mWorkspace.getHotseat();
127         Interpolator scaleInterpolator = config.getInterpolator(ANIM_WORKSPACE_SCALE, ZOOM_OUT);
128         LauncherState fromState = mLauncher.getStateManager().getState();
129 
130         boolean shouldSpring = propertySetter instanceof PendingAnimation
131                 && fromState == HINT_STATE && state == NORMAL;
132         if (shouldSpring) {
133             ((PendingAnimation) propertySetter).add(getSpringScaleAnimator(mLauncher,
134                     mWorkspace, mNewScale, WORKSPACE_SCALE_PROPERTY));
135         } else {
136             propertySetter.setFloat(mWorkspace, WORKSPACE_SCALE_PROPERTY, mNewScale,
137                     scaleInterpolator);
138         }
139 
140         mWorkspace.setPivotToScaleWithSelf(hotseat);
141         float hotseatScale = hotseatScaleAndTranslation.scale;
142         if (shouldSpring) {
143             PendingAnimation pa = (PendingAnimation) propertySetter;
144             pa.add(getSpringScaleAnimator(mLauncher, hotseat, hotseatScale,
145                     HOTSEAT_SCALE_PROPERTY));
146         } else {
147             Interpolator hotseatScaleInterpolator = config.getInterpolator(ANIM_HOTSEAT_SCALE,
148                     scaleInterpolator);
149             propertySetter.setFloat(hotseat, HOTSEAT_SCALE_PROPERTY, hotseatScale,
150                     hotseatScaleInterpolator);
151         }
152 
153         Interpolator workspaceFadeInterpolator = config.getInterpolator(ANIM_WORKSPACE_FADE,
154                 pageAlphaProvider.interpolator);
155         float workspacePageIndicatorAlpha = (elements & WORKSPACE_PAGE_INDICATOR) != 0 ? 1 : 0;
156         propertySetter.setViewAlpha(mLauncher.getWorkspace().getPageIndicator(),
157                 workspacePageIndicatorAlpha, workspaceFadeInterpolator);
158         Interpolator hotseatFadeInterpolator = config.getInterpolator(ANIM_HOTSEAT_FADE,
159                 workspaceFadeInterpolator);
160         float hotseatIconsAlpha = (elements & HOTSEAT_ICONS) != 0 ? 1 : 0;
161         propertySetter.setViewAlpha(hotseat, hotseatIconsAlpha, hotseatFadeInterpolator);
162 
163         if (SHOW_HOME_GARDENING.get()) {
164             propertySetter.setViewAlpha(
165                     mWorkspace.getFirstPagePinnedItem(),
166                     state == SPRING_LOADED ? FIRST_PAGE_PINNED_WIDGET_DISABLED_ALPHA : 1,
167                     workspaceFadeInterpolator);
168             propertySetter.addEndListener(success -> {
169                 if (success) {
170                     mWorkspace.getFirstPagePinnedItem().setClickable(state != SPRING_LOADED);
171                 }
172             });
173         }
174 
175         if (HOME_GARDENING_WORKSPACE_BUTTONS.get()) {
176             propertySetter.setViewAlpha(
177                     mLauncher.getHotseat().getQsb(),
178                     state == SPRING_LOADED ? 0 : 1,
179                     workspaceFadeInterpolator);
180             propertySetter.addEndListener(success -> {
181                 if (success) {
182                     mLauncher.getHotseat().getQsb().setClickable(state != SPRING_LOADED);
183                 }
184             });
185         }
186 
187         // Update the accessibility flags for hotseat based on launcher state.
188         hotseat.setImportantForAccessibility(
189                 state.hasFlag(FLAG_HOTSEAT_INACCESSIBLE)
190                         ? View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
191                         : View.IMPORTANT_FOR_ACCESSIBILITY_AUTO);
192 
193         Interpolator translationInterpolator =
194                 config.getInterpolator(ANIM_WORKSPACE_TRANSLATE, ZOOM_OUT);
195         propertySetter.setFloat(mWorkspace, VIEW_TRANSLATE_X,
196                 scaleAndTranslation.translationX, translationInterpolator);
197         propertySetter.setFloat(mWorkspace, VIEW_TRANSLATE_Y,
198                 scaleAndTranslation.translationY, translationInterpolator);
199         PageTranslationProvider pageTranslationProvider = state.getWorkspacePageTranslationProvider(
200                 mLauncher);
201         for (int i = 0; i < childCount; i++) {
202             applyPageTranslation((CellLayout) mWorkspace.getChildAt(i), i, pageTranslationProvider,
203                     propertySetter, config);
204         }
205 
206         Interpolator hotseatTranslationInterpolator = config.getInterpolator(
207                 ANIM_HOTSEAT_TRANSLATE, translationInterpolator);
208         propertySetter.setFloat(hotseat, VIEW_TRANSLATE_Y,
209                 hotseatScaleAndTranslation.translationY, hotseatTranslationInterpolator);
210         propertySetter.setFloat(mWorkspace.getPageIndicator(), VIEW_TRANSLATE_Y,
211                 hotseatScaleAndTranslation.translationY, hotseatTranslationInterpolator);
212 
213         if (!config.hasAnimationFlag(SKIP_SCRIM)) {
214             setScrim(propertySetter, state, config);
215         }
216     }
217 
setScrim(PropertySetter propertySetter, LauncherState state, StateAnimationConfig config)218     public void setScrim(PropertySetter propertySetter, LauncherState state,
219             StateAnimationConfig config) {
220         Scrim workspaceDragScrim = mLauncher.getDragLayer().getWorkspaceDragScrim();
221         propertySetter.setFloat(workspaceDragScrim, SCRIM_PROGRESS,
222                 state.getWorkspaceBackgroundAlpha(mLauncher), LINEAR);
223 
224         SysUiScrim sysUiScrim = mLauncher.getRootView().getSysUiScrim();
225         propertySetter.setFloat(sysUiScrim, SYSUI_PROGRESS,
226                 state.hasFlag(FLAG_HAS_SYS_UI_SCRIM) ? 1 : 0, LINEAR);
227 
228         propertySetter.setViewBackgroundColor(mLauncher.getScrimView(),
229                 state.getWorkspaceScrimColor(mLauncher),
230                 config.getInterpolator(ANIM_SCRIM_FADE, ACCEL_2));
231     }
232 
applyChildState(LauncherState state, CellLayout cl, int childIndex)233     public void applyChildState(LauncherState state, CellLayout cl, int childIndex) {
234         applyChildState(state, cl, childIndex, state.getWorkspacePageAlphaProvider(mLauncher),
235                 NO_ANIM_PROPERTY_SETTER, new StateAnimationConfig());
236     }
237 
applyChildState(LauncherState state, CellLayout cl, int childIndex, PageAlphaProvider pageAlphaProvider, PropertySetter propertySetter, StateAnimationConfig config)238     private void applyChildState(LauncherState state, CellLayout cl, int childIndex,
239             PageAlphaProvider pageAlphaProvider, PropertySetter propertySetter,
240             StateAnimationConfig config) {
241         float pageAlpha = pageAlphaProvider.getPageAlpha(childIndex);
242         float springLoadedProgress = (state instanceof SpringLoadedState) ? 1.0f : 0f;
243 
244         propertySetter.setFloat(cl,
245                 CellLayout.SPRING_LOADED_PROGRESS, springLoadedProgress, ZOOM_OUT);
246         Interpolator fadeInterpolator = config.getInterpolator(ANIM_WORKSPACE_FADE,
247                 pageAlphaProvider.interpolator);
248         propertySetter.setFloat(cl.getShortcutsAndWidgets(), VIEW_ALPHA,
249                 pageAlpha, fadeInterpolator);
250     }
251 
applyPageTranslation(CellLayout cellLayout, int childIndex, PageTranslationProvider pageTranslationProvider, PropertySetter propertySetter, StateAnimationConfig config)252     private void applyPageTranslation(CellLayout cellLayout, int childIndex,
253             PageTranslationProvider pageTranslationProvider, PropertySetter propertySetter,
254             StateAnimationConfig config) {
255         float pageTranslation = pageTranslationProvider.getPageTranslation(childIndex);
256         Interpolator translationInterpolator = config.getInterpolator(
257                 ANIM_WORKSPACE_PAGE_TRANSLATE_X, pageTranslationProvider.interpolator);
258         propertySetter.setFloat(cellLayout, VIEW_TRANSLATE_X, pageTranslation,
259                 translationInterpolator);
260     }
261 
262     /**
263      * Returns a spring based animator for the scale property of {@param workspace}.
264      */
getWorkspaceSpringScaleAnimator(Launcher launcher, Workspace<?> workspace, float scale)265     public static ValueAnimator getWorkspaceSpringScaleAnimator(Launcher launcher,
266             Workspace<?> workspace, float scale) {
267         return getSpringScaleAnimator(launcher, workspace, scale, WORKSPACE_SCALE_PROPERTY);
268     }
269 
270     /**
271      * Returns a spring based animator for the scale property of {@param v}.
272      */
getSpringScaleAnimator(Launcher launcher, T v, float scale, FloatProperty<T> property)273     public static <T extends View> ValueAnimator getSpringScaleAnimator(Launcher launcher, T v,
274             float scale, FloatProperty<T> property) {
275         ResourceProvider rp = DynamicResource.provider(launcher);
276         float damping = rp.getFloat(R.dimen.hint_scale_damping_ratio);
277         float stiffness = rp.getFloat(R.dimen.hint_scale_stiffness);
278         float velocityPxPerS = rp.getDimension(R.dimen.hint_scale_velocity_dp_per_s);
279 
280         return new SpringAnimationBuilder(v.getContext())
281                 .setStiffness(stiffness)
282                 .setDampingRatio(damping)
283                 .setMinimumVisibleChange(MIN_VISIBLE_CHANGE_SCALE)
284                 .setEndValue(scale)
285                 .setStartValue(property.get(v))
286                 .setStartVelocity(velocityPxPerS)
287                 .build(v, property);
288 
289     }
290 }