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