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.app.animation.Interpolators.ACCELERATE_2; 22 import static com.android.app.animation.Interpolators.LINEAR; 23 import static com.android.app.animation.Interpolators.ZOOM_OUT; 24 import static com.android.launcher3.LauncherAnimUtils.HOTSEAT_SCALE_PROPERTY_FACTORY; 25 import static com.android.launcher3.LauncherAnimUtils.SCALE_INDEX_WORKSPACE_STATE; 26 import static com.android.launcher3.LauncherAnimUtils.VIEW_ALPHA; 27 import static com.android.launcher3.LauncherAnimUtils.VIEW_TRANSLATE_X; 28 import static com.android.launcher3.LauncherAnimUtils.VIEW_TRANSLATE_Y; 29 import static com.android.launcher3.LauncherAnimUtils.WORKSPACE_SCALE_PROPERTY_FACTORY; 30 import static com.android.launcher3.LauncherState.FLAG_HAS_SYS_UI_SCRIM; 31 import static com.android.launcher3.LauncherState.FLAG_HOTSEAT_INACCESSIBLE; 32 import static com.android.launcher3.LauncherState.HINT_STATE; 33 import static com.android.launcher3.LauncherState.HOTSEAT_ICONS; 34 import static com.android.launcher3.LauncherState.NORMAL; 35 import static com.android.launcher3.LauncherState.WORKSPACE_PAGE_INDICATOR; 36 import static com.android.launcher3.anim.PropertySetter.NO_ANIM_PROPERTY_SETTER; 37 import static com.android.launcher3.config.FeatureFlags.FOLDABLE_SINGLE_PAGE; 38 import static com.android.launcher3.graphics.Scrim.SCRIM_PROGRESS; 39 import static com.android.launcher3.states.StateAnimationConfig.ANIM_HOTSEAT_FADE; 40 import static com.android.launcher3.states.StateAnimationConfig.ANIM_HOTSEAT_SCALE; 41 import static com.android.launcher3.states.StateAnimationConfig.ANIM_HOTSEAT_TRANSLATE; 42 import static com.android.launcher3.states.StateAnimationConfig.ANIM_SCRIM_FADE; 43 import static com.android.launcher3.states.StateAnimationConfig.ANIM_WORKSPACE_FADE; 44 import static com.android.launcher3.states.StateAnimationConfig.ANIM_WORKSPACE_PAGE_TRANSLATE_X; 45 import static com.android.launcher3.states.StateAnimationConfig.ANIM_WORKSPACE_SCALE; 46 import static com.android.launcher3.states.StateAnimationConfig.ANIM_WORKSPACE_TRANSLATE; 47 import static com.android.launcher3.states.StateAnimationConfig.SKIP_SCRIM; 48 49 import android.animation.ValueAnimator; 50 import android.util.FloatProperty; 51 import android.view.View; 52 import android.view.ViewGroup; 53 import android.view.animation.Interpolator; 54 55 import com.android.launcher3.LauncherState.PageAlphaProvider; 56 import com.android.launcher3.LauncherState.PageTranslationProvider; 57 import com.android.launcher3.LauncherState.ScaleAndTranslation; 58 import com.android.launcher3.anim.AnimatedFloat; 59 import com.android.launcher3.anim.PendingAnimation; 60 import com.android.launcher3.anim.PropertySetter; 61 import com.android.launcher3.anim.SpringAnimationBuilder; 62 import com.android.launcher3.graphics.Scrim; 63 import com.android.launcher3.graphics.SysUiScrim; 64 import com.android.launcher3.states.EditModeState; 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 // Update the accessibility flags for hotseat based on launcher state. 164 hotseat.setImportantForAccessibility( 165 state.hasFlag(FLAG_HOTSEAT_INACCESSIBLE) 166 ? View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS 167 : View.IMPORTANT_FOR_ACCESSIBILITY_AUTO); 168 hotseat.setDescendantFocusability(state.hasFlag(FLAG_HOTSEAT_INACCESSIBLE) 169 ? ViewGroup.FOCUS_BLOCK_DESCENDANTS : ViewGroup.FOCUS_BEFORE_DESCENDANTS); 170 171 Interpolator translationInterpolator = 172 config.getInterpolator(ANIM_WORKSPACE_TRANSLATE, ZOOM_OUT); 173 propertySetter.setFloat(mWorkspace, VIEW_TRANSLATE_X, 174 scaleAndTranslation.translationX, translationInterpolator); 175 propertySetter.setFloat(mWorkspace, VIEW_TRANSLATE_Y, 176 scaleAndTranslation.translationY, translationInterpolator); 177 PageTranslationProvider pageTranslationProvider = state.getWorkspacePageTranslationProvider( 178 mLauncher); 179 180 if (!FOLDABLE_SINGLE_PAGE.get()) { 181 for (int i = 0; i < childCount; i++) { 182 applyPageTranslation((CellLayout) mWorkspace.getChildAt(i), i, 183 pageTranslationProvider, 184 propertySetter, config); 185 } 186 } 187 188 Interpolator hotseatTranslationInterpolator = config.getInterpolator( 189 ANIM_HOTSEAT_TRANSLATE, translationInterpolator); 190 propertySetter.setFloat(hotseat, VIEW_TRANSLATE_Y, 191 hotseatScaleAndTranslation.translationY, hotseatTranslationInterpolator); 192 propertySetter.setFloat(mWorkspace.getPageIndicator(), VIEW_TRANSLATE_Y, 193 hotseatScaleAndTranslation.translationY, hotseatTranslationInterpolator); 194 195 if (!config.hasAnimationFlag(SKIP_SCRIM)) { 196 setScrim(propertySetter, state, config); 197 } 198 } 199 setScrim(PropertySetter propertySetter, LauncherState state, StateAnimationConfig config)200 public void setScrim(PropertySetter propertySetter, LauncherState state, 201 StateAnimationConfig config) { 202 Scrim workspaceDragScrim = mLauncher.getDragLayer().getWorkspaceDragScrim(); 203 propertySetter.setFloat(workspaceDragScrim, SCRIM_PROGRESS, 204 state.getWorkspaceBackgroundAlpha(mLauncher), LINEAR); 205 206 SysUiScrim sysUiScrim = mLauncher.getRootView().getSysUiScrim(); 207 propertySetter.setFloat(sysUiScrim.getSysUIProgress(), AnimatedFloat.VALUE, 208 state.hasFlag(FLAG_HAS_SYS_UI_SCRIM) ? 1 : 0, LINEAR); 209 210 propertySetter.setViewBackgroundColor(mLauncher.getScrimView(), 211 state.getWorkspaceScrimColor(mLauncher), 212 config.getInterpolator(ANIM_SCRIM_FADE, ACCELERATE_2)); 213 } 214 applyChildState(LauncherState state, CellLayout cl, int childIndex)215 public void applyChildState(LauncherState state, CellLayout cl, int childIndex) { 216 applyChildState(state, cl, childIndex, state.getWorkspacePageAlphaProvider(mLauncher), 217 NO_ANIM_PROPERTY_SETTER, new StateAnimationConfig()); 218 } 219 applyChildState(LauncherState state, CellLayout cl, int childIndex, PageAlphaProvider pageAlphaProvider, PropertySetter propertySetter, StateAnimationConfig config)220 private void applyChildState(LauncherState state, CellLayout cl, int childIndex, 221 PageAlphaProvider pageAlphaProvider, PropertySetter propertySetter, 222 StateAnimationConfig config) { 223 float pageAlpha = pageAlphaProvider.getPageAlpha(childIndex); 224 float springLoadedProgress = 225 (state instanceof SpringLoadedState || state instanceof EditModeState) ? 1f : 0f; 226 propertySetter.setFloat(cl, 227 CellLayout.SPRING_LOADED_PROGRESS, springLoadedProgress, ZOOM_OUT); 228 Interpolator fadeInterpolator = config.getInterpolator(ANIM_WORKSPACE_FADE, 229 pageAlphaProvider.interpolator); 230 propertySetter.setFloat(cl.getShortcutsAndWidgets(), VIEW_ALPHA, 231 pageAlpha, fadeInterpolator); 232 } 233 applyPageTranslation(CellLayout cellLayout, int childIndex, PageTranslationProvider pageTranslationProvider, PropertySetter propertySetter, StateAnimationConfig config)234 private void applyPageTranslation(CellLayout cellLayout, int childIndex, 235 PageTranslationProvider pageTranslationProvider, PropertySetter propertySetter, 236 StateAnimationConfig config) { 237 float pageTranslation = pageTranslationProvider.getPageTranslation(childIndex); 238 Interpolator translationInterpolator = config.getInterpolator( 239 ANIM_WORKSPACE_PAGE_TRANSLATE_X, pageTranslationProvider.interpolator); 240 propertySetter.setFloat(cellLayout, VIEW_TRANSLATE_X, pageTranslation, 241 translationInterpolator); 242 } 243 244 /** 245 * Returns a spring based animator for the scale property of {@param workspace}. 246 */ getWorkspaceSpringScaleAnimator(Launcher launcher, Workspace<?> workspace, float scale)247 public static ValueAnimator getWorkspaceSpringScaleAnimator(Launcher launcher, 248 Workspace<?> workspace, float scale) { 249 return getSpringScaleAnimator(launcher, workspace, scale, WORKSPACE_SCALE_PROPERTY); 250 } 251 252 /** 253 * Returns a spring based animator for the scale property of {@param v}. 254 */ getSpringScaleAnimator(Launcher launcher, T v, float scale, FloatProperty<T> property)255 public static <T extends View> ValueAnimator getSpringScaleAnimator(Launcher launcher, T v, 256 float scale, FloatProperty<T> property) { 257 ResourceProvider rp = DynamicResource.provider(launcher); 258 float damping = rp.getFloat(R.dimen.hint_scale_damping_ratio); 259 float stiffness = rp.getFloat(R.dimen.hint_scale_stiffness); 260 float velocityPxPerS = rp.getDimension(R.dimen.hint_scale_velocity_dp_per_s); 261 262 return new SpringAnimationBuilder(v.getContext()) 263 .setStiffness(stiffness) 264 .setDampingRatio(damping) 265 .setMinimumVisibleChange(MIN_VISIBLE_CHANGE_SCALE) 266 .setEndValue(scale) 267 .setStartValue(property.get(v)) 268 .setStartVelocity(velocityPxPerS) 269 .build(v, property); 270 271 } 272 }