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.PointFEvaluator
20 import android.animation.ValueAnimator
21 import android.graphics.PointF
22 import android.graphics.Rect
23 import android.util.DisplayMetrics
24 import android.util.TypedValue
25 import android.view.SurfaceControl
26 import android.view.animation.Interpolator
27 import android.window.TransitionInfo
28 
29 /** Creates animations that can be applied to windows/surfaces. */
30 object WindowAnimator {
31 
32     /** Parameters defining a window bounds animation. */
33     data class BoundsAnimationParams(
34         val durationMs: Long,
35         val startOffsetYDp: Float = 0f,
36         val endOffsetYDp: Float = 0f,
37         val startScale: Float = 1f,
38         val endScale: Float = 1f,
39         val interpolator: Interpolator,
40     )
41 
42     /**
43      * Creates an animator to reposition and scale the bounds of the leash of the given change.
44      *
45      * @param displayMetrics the metrics of the display where the animation plays in
46      * @param boundsAnimDef the parameters for the animation itself (duration, scale, position)
47      * @param change the change to which the animation should be applied
48      * @param transaction the transaction to apply the animation to
49      */
50     fun createBoundsAnimator(
51         displayMetrics: DisplayMetrics,
52         boundsAnimDef: BoundsAnimationParams,
53         change: TransitionInfo.Change,
54         transaction: SurfaceControl.Transaction,
55     ): ValueAnimator {
56         val startPos =
57             getPosition(
58                 displayMetrics,
59                 change.endAbsBounds,
60                 boundsAnimDef.startScale,
61                 boundsAnimDef.startOffsetYDp,
62             )
63         val leash = change.leash
64         val endPos =
65             getPosition(
66                 displayMetrics,
67                 change.endAbsBounds,
68                 boundsAnimDef.endScale,
69                 boundsAnimDef.endOffsetYDp,
70             )
71         return ValueAnimator.ofObject(PointFEvaluator(), startPos, endPos).apply {
72             duration = boundsAnimDef.durationMs
73             interpolator = boundsAnimDef.interpolator
74             addUpdateListener { animation ->
75                 val animPos = animation.animatedValue as PointF
76                 val animScale =
77                     interpolate(
78                         boundsAnimDef.startScale,
79                         boundsAnimDef.endScale,
80                         animation.animatedFraction
81                     )
82                 transaction
83                     .setPosition(leash, animPos.x, animPos.y)
84                     .setScale(leash, animScale, animScale)
85                     .apply()
86             }
87         }
88     }
89 
90     private fun interpolate(startVal: Float, endVal: Float, fraction: Float): Float {
91         require(fraction in 0.0f..1.0f)
92         return startVal + (endVal - startVal) * fraction
93     }
94 
95     private fun getPosition(
96         displayMetrics: DisplayMetrics,
97         bounds: Rect,
98         scale: Float,
99         offsetYDp: Float
100     ) = PointF(bounds.left.toFloat(), bounds.top.toFloat()).apply {
101             check(scale in 0.0f..1.0f)
102             // Scale the bounds down with an anchor in the center
103             offset(
104                 (bounds.width().toFloat() * (1 - scale) / 2),
105                 (bounds.height().toFloat() * (1 - scale) / 2),
106             )
107             val offsetYPx =
108                 TypedValue.applyDimension(
109                         TypedValue.COMPLEX_UNIT_DIP,
110                         offsetYDp,
111                         displayMetrics,
112                     )
113                     .toInt()
114             offset(/* dx= */ 0f, offsetYPx.toFloat())
115         }
116 }
117