1 /*
<lambda>null2  * Copyright (C) 2024 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.wm.shell.shared.animation
18 
19 import android.animation.Animator
20 import android.animation.AnimatorSet
21 import android.animation.ValueAnimator
22 import android.util.DisplayMetrics
23 import android.view.SurfaceControl.Transaction
24 import android.view.animation.LinearInterpolator
25 import android.view.animation.PathInterpolator
26 import android.window.TransitionInfo.Change
27 
28 /** Creates minimization animation */
29 object MinimizeAnimator {
30 
31     private const val MINIMIZE_ANIM_ALPHA_DURATION_MS = 100L
32 
33     private val STANDARD_ACCELERATE = PathInterpolator(0.3f, 0f, 1f, 1f)
34 
35     private val minimizeBoundsAnimationDef =
36         WindowAnimator.BoundsAnimationParams(
37             durationMs = 200,
38             endOffsetYDp = 12f,
39             endScale = 0.97f,
40             interpolator = STANDARD_ACCELERATE,
41         )
42 
43     @JvmStatic
44     fun create(
45         displayMetrics: DisplayMetrics,
46         change: Change,
47         transaction: Transaction,
48         onAnimFinish: (Animator) -> Unit,
49     ): Animator {
50         val boundsAnimator = WindowAnimator.createBoundsAnimator(
51             displayMetrics,
52             minimizeBoundsAnimationDef,
53             change,
54             transaction,
55         )
56         val alphaAnimator = ValueAnimator.ofFloat(1f, 0f).apply {
57             duration = MINIMIZE_ANIM_ALPHA_DURATION_MS
58             interpolator = LinearInterpolator()
59             addUpdateListener { animation ->
60                 transaction.setAlpha(change.leash, animation.animatedValue as Float).apply()
61             }
62         }
63         val listener = object : Animator.AnimatorListener {
64             override fun onAnimationEnd(animator: Animator) = onAnimFinish(animator)
65             override fun onAnimationCancel(animator: Animator) = Unit
66             override fun onAnimationRepeat(animator: Animator) = Unit
67             override fun onAnimationStart(animator: Animator) = Unit
68         }
69         return AnimatorSet().apply {
70             playTogether(boundsAnimator, alphaAnimator)
71             addListener(listener)
72         }
73     }
74 }
75