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 displayMetrics: DisplayMetrics,
41 maxMarginXdp: Float,
42 maxMarginYdp: Float,
43 minScale: Float,
44 translateXEasing: Interpolator = Interpolators.STANDARD_DECELERATE,
45 translateYEasing: Interpolator = Interpolators.LINEAR,
46 scaleEasing: Interpolator = Interpolators.STANDARD_DECELERATE,
47 ): BackAnimationSpec {
48 val screenWidthPx = displayMetrics.widthPixels
49 val screenHeightPx = displayMetrics.heightPixels
50
51 val maxMarginXPx = maxMarginXdp.dpToPx(displayMetrics)
52 val maxMarginYPx = maxMarginYdp.dpToPx(displayMetrics)
53 val maxTranslationXByScale = (screenWidthPx - screenWidthPx * minScale) / 2
54 val maxTranslationX = maxTranslationXByScale - maxMarginXPx
55 val maxTranslationYByScale = (screenHeightPx - screenHeightPx * minScale) / 2
56 val maxTranslationY = maxTranslationYByScale - maxMarginYPx
57 val minScaleReversed = 1f - minScale
58
59 return BackAnimationSpec { backEvent, progressY, result ->
60 val direction = if (backEvent.swipeEdge == BackEvent.EDGE_LEFT) 1 else -1
61 val progressX = backEvent.progress
62
63 val ratioTranslateX = translateXEasing.getInterpolation(progressX)
64 val ratioTranslateY = translateYEasing.getInterpolation(progressY)
65 val ratioScale = scaleEasing.getInterpolation(progressX)
66
67 result.apply {
68 translateX = ratioTranslateX * direction * maxTranslationX
69 translateY = ratioTranslateY * maxTranslationY
70 scale = 1f - (ratioScale * minScaleReversed)
71 }
72 }
73 }
74