1 /*
<lambda>null2  * 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 
17 package com.android.systemui.animation.back
18 
19 import android.util.DisplayMetrics
20 import android.view.animation.Interpolator
21 import android.window.BackEvent
22 import com.android.app.animation.Interpolators
23 import com.android.systemui.util.dpToPx
24 
25 /** Used to convert [BackEvent] into a [BackTransformation]. */
26 fun interface BackAnimationSpec {
27 
28     /** Computes transformation based on a [backEvent] and sets it to [result]. */
29     fun getBackTransformation(
30         backEvent: BackEvent,
31         progressY: Float, // TODO(b/265060720): Remove progressY. Could be retrieved from backEvent
32         result: BackTransformation,
33     )
34 
35     companion object
36 }
37 
38 /** Create a [BackAnimationSpec] from [displayMetrics] and design specs. */
createFloatingSurfaceAnimationSpecnull39 fun BackAnimationSpec.Companion.createFloatingSurfaceAnimationSpec(
40     displayMetricsProvider: () -> DisplayMetrics,
41     maxMarginXdp: Float,
42     maxMarginYdp: Float,
43     minScale: Float,
44     translateXEasing: Interpolator = Interpolators.BACK_GESTURE,
45     translateYEasing: Interpolator = Interpolators.LINEAR,
46     scaleEasing: Interpolator = Interpolators.BACK_GESTURE,
47 ): BackAnimationSpec {
48     return BackAnimationSpec { backEvent, progressY, result ->
49         val displayMetrics = displayMetricsProvider()
50         val screenWidthPx = displayMetrics.widthPixels
51         val screenHeightPx = displayMetrics.heightPixels
52 
53         val maxMarginXPx = maxMarginXdp.dpToPx(displayMetrics)
54         val maxMarginYPx = maxMarginYdp.dpToPx(displayMetrics)
55         val maxTranslationXByScale = (screenWidthPx - screenWidthPx * minScale) / 2
56         val maxTranslationX = maxTranslationXByScale - maxMarginXPx
57         val maxTranslationYByScale = (screenHeightPx - screenHeightPx * minScale) / 2
58         val maxTranslationY = maxTranslationYByScale - maxMarginYPx
59         val minScaleReversed = 1f - minScale
60 
61         val direction =
62             when (backEvent.swipeEdge) {
63                 BackEvent.EDGE_LEFT -> 1
64                 BackEvent.EDGE_RIGHT -> -1
65                 else -> 0
66             }
67         val progressX = backEvent.progress
68 
69         val ratioTranslateX = translateXEasing.getInterpolation(progressX)
70         val ratioTranslateY = translateYEasing.getInterpolation(progressY)
71         val ratioScale = scaleEasing.getInterpolation(progressX)
72 
73         result.apply {
74             translateX = ratioTranslateX * direction * maxTranslationX
75             translateY = ratioTranslateY * maxTranslationY
76             scale = 1f - (ratioScale * minScaleReversed)
77         }
78     }
79 }
80