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 = if (backEvent.swipeEdge == BackEvent.EDGE_LEFT) 1 else -1
62 val progressX = backEvent.progress
63
64 val ratioTranslateX = translateXEasing.getInterpolation(progressX)
65 val ratioTranslateY = translateYEasing.getInterpolation(progressY)
66 val ratioScale = scaleEasing.getInterpolation(progressX)
67
68 result.apply {
69 translateX = ratioTranslateX * direction * maxTranslationX
70 translateY = ratioTranslateY * maxTranslationY
71 scale = 1f - (ratioScale * minScaleReversed)
72 }
73 }
74 }
75