1 /* 2 * Copyright (C) 2023 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.taskbar; 17 18 import static com.android.launcher3.anim.AnimatedFloat.VALUE; 19 20 import android.animation.ObjectAnimator; 21 import android.animation.ValueAnimator; 22 23 import androidx.annotation.Nullable; 24 import androidx.dynamicanimation.animation.SpringForce; 25 26 import com.android.launcher3.R; 27 import com.android.launcher3.anim.AnimatedFloat; 28 import com.android.launcher3.anim.SpringAnimationBuilder; 29 import com.android.launcher3.taskbar.TaskbarControllers.LoggableTaskbarController; 30 31 import java.io.PrintWriter; 32 33 /** 34 * Manages the spring animation when stashing the transient taskbar. 35 */ 36 public class TaskbarSpringOnStashController implements LoggableTaskbarController { 37 38 private final TaskbarActivityContext mContext; 39 private TaskbarControllers mControllers; 40 private final AnimatedFloat mTranslationForStash = new AnimatedFloat( 41 this::updateTranslationYForStash); 42 43 private final boolean mIsTransientTaskbar; 44 45 private final float mStartVelocityPxPerS; 46 TaskbarSpringOnStashController(TaskbarActivityContext context)47 public TaskbarSpringOnStashController(TaskbarActivityContext context) { 48 mContext = context; 49 mIsTransientTaskbar = context.isTransientTaskbar(); 50 mStartVelocityPxPerS = context.getResources() 51 .getDimension(R.dimen.transient_taskbar_stash_spring_velocity_dp_per_s); 52 } 53 54 /** 55 * Initialization method. 56 */ init(TaskbarControllers controllers)57 public void init(TaskbarControllers controllers) { 58 mControllers = controllers; 59 } 60 updateTranslationYForStash()61 private void updateTranslationYForStash() { 62 if (!mIsTransientTaskbar) { 63 return; 64 } 65 66 float transY = mTranslationForStash.value; 67 mControllers.stashedHandleViewController.setTranslationYForStash(transY); 68 mControllers.taskbarViewController.setTranslationYForStash(transY); 69 mControllers.taskbarDragLayerController.setTranslationYForStash(transY); 70 } 71 72 /** 73 * Returns a spring animation to be used when stashing the transient taskbar. 74 */ createSpringToStash()75 public @Nullable ValueAnimator createSpringToStash() { 76 if (!mIsTransientTaskbar) { 77 return null; 78 } 79 return new SpringAnimationBuilder(mContext) 80 .setStartValue(mTranslationForStash.value) 81 .setEndValue(0) 82 .setDampingRatio(SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY) 83 .setStiffness(SpringForce.STIFFNESS_LOW) 84 .setStartVelocity(mStartVelocityPxPerS) 85 .build(mTranslationForStash, VALUE); 86 } 87 88 /** 89 * Returns an animation to reset the stash translation back to 0 when unstashing. 90 */ createResetAnimForUnstash()91 public @Nullable ObjectAnimator createResetAnimForUnstash() { 92 if (!mIsTransientTaskbar) { 93 return null; 94 } 95 return mTranslationForStash.animateToValue(0); 96 } 97 98 @Override dumpLogs(String prefix, PrintWriter pw)99 public void dumpLogs(String prefix, PrintWriter pw) { 100 pw.println(prefix + "TaskbarSpringOnStashController:"); 101 102 pw.println(prefix + "\tmTranslationForStash=" + mTranslationForStash.value); 103 pw.println(prefix + "\tmStartVelocityPxPerS=" + mStartVelocityPxPerS); 104 } 105 } 106 107