Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 47c31e0c authored by Josh Tsuji's avatar Josh Tsuji
Browse files

Manually tweak the dismiss spring.

Removes the bounce for non-touch swipes (zero velocity), caps the velocity to prevent excessive bouncing, and changes some other constants.

Bug: 278086361
Test: manual but actually
Flag: com.android.systemui.keyguard_wm_state_refactor
Change-Id: I1f0a814f0833020ce99e132aff90db042c13cf7d
parent 7cfbf1fd
Loading
Loading
Loading
Loading
+2 −5
Original line number Diff line number Diff line
@@ -67,6 +67,7 @@ class KeyguardSurfaceBehindParamsApplierTest : SysuiTestCase() {
                executor = executor,
                keyguardViewController = keyguardViewController,
                interactor = interactor,
                context = context,
            )

        doAnswer {
@@ -83,11 +84,7 @@ class KeyguardSurfaceBehindParamsApplierTest : SysuiTestCase() {

    @Test
    fun testNotAnimating_setParamsWithNoAnimation() {
        underTest.viewParams =
            KeyguardSurfaceBehindModel(
                alpha = 0.3f,
                translationY = 300f,
            )
        underTest.viewParams = KeyguardSurfaceBehindModel(alpha = 0.3f, translationY = 300f)

        // A surface has not yet been provided, so we shouldn't have set animating to false OR true
        // just yet.
+1 −1
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ import kotlinx.coroutines.flow.onStart
 * Distance over which the surface behind the keyguard is animated in during a Y-translation
 * animation.
 */
const val SURFACE_TRANSLATION_Y_DISTANCE_DP = 250
const val SURFACE_TRANSLATION_Y_DISTANCE_DP = 200

@SysUISingleton
class KeyguardSurfaceBehindInteractor
+14 −8
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn

/**
@@ -52,13 +52,19 @@ constructor(
     */
    val dismissFling: StateFlow<FlingInfo?> =
        shadeRepository.currentFling
            .filter { flingInfo ->
            .map { flingInfo ->
                if (
                    flingInfo != null &&
                        !flingInfo.expand &&
                        keyguardInteractor.statusBarState.value != StatusBarState.SHADE_LOCKED &&
                        transitionInteractor.startedKeyguardTransitionStep.value.to ==
                            KeyguardState.LOCKSCREEN &&
                        keyguardInteractor.isKeyguardDismissible.value
                ) {
                    flingInfo
                } else {
                    null
                }
            }
            .stateIn(backgroundScope, SharingStarted.Eagerly, null)
}
+59 −11
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.systemui.keyguard.ui.binder
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Matrix
import android.util.Log
import android.view.RemoteAnimationTarget
@@ -28,6 +29,7 @@ import android.view.View
import androidx.dynamicanimation.animation.FloatValueHolder
import androidx.dynamicanimation.animation.SpringAnimation
import androidx.dynamicanimation.animation.SpringForce
import com.android.internal.R
import com.android.keyguard.KeyguardViewController
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Main
@@ -37,6 +39,16 @@ import com.android.systemui.keyguard.shared.model.KeyguardSurfaceBehindModel
import com.android.wm.shell.shared.animation.Interpolators
import java.util.concurrent.Executor
import javax.inject.Inject
import kotlin.math.max

/** Damping ratio to use for animations resulting from touch gesture fling animation. */
private const val TOUCH_FLING_DAMPING_RATIO = 0.992f

/**
 * Fastest swipe velocity we'll use for the spring animations. Higher values will result in an
 * unreasonable amount of overshoot.
 */
private const val FASTEST_SWIPE_VELOCITY = -21500f

/**
 * Applies [KeyguardSurfaceBehindViewParams] to a RemoteAnimationTarget, starting and managing
@@ -49,6 +61,7 @@ constructor(
    @Main private val executor: Executor,
    private val keyguardViewController: KeyguardViewController,
    private val interactor: KeyguardSurfaceBehindInteractor,
    context: Context,
) {
    private var surfaceBehind: RemoteAnimationTarget? = null
        set(value) {
@@ -62,14 +75,12 @@ constructor(
    private val matrix = Matrix()
    private val tmpFloat = FloatArray(9)

    private val defaultSpring = SpringForce().apply { stiffness = 675f }

    private var animatedTranslationY = FloatValueHolder()
    private val translateYSpring =
        SpringAnimation(animatedTranslationY).apply {
            spring =
                SpringForce().apply {
                    stiffness = 275f
                    dampingRatio = 0.98f
                }
            spring = defaultSpring
            addUpdateListener { _, _, _ -> applyToSurfaceBehind() }
            addEndListener { _, _, _, _ ->
                try {
@@ -84,7 +95,7 @@ constructor(
    private var animatedAlpha = 0f
    private var alphaAnimator =
        ValueAnimator.ofFloat(0f, 1f).apply {
            duration = 150
            duration = 125
            interpolator = Interpolators.ALPHA_IN
            addUpdateListener {
                animatedAlpha = it.animatedValue as Float
@@ -99,6 +110,10 @@ constructor(
            )
        }

    /** Rounded corner radius to apply to the surface behind the keyguard. */
    private val roundedCornerRadius =
        context.resources.getDimensionPixelSize(R.dimen.rounded_corner_radius).toFloat()

    /**
     * ViewParams to apply to the surface provided to [applyParamsToSurface]. If the surface is null
     * these will be applied once someone gives us a surface via [applyParamsToSurface].
@@ -162,7 +177,15 @@ constructor(
                // If the spring isn't running yet, set the start value. Otherwise, respect the
                // current position.
                animatedTranslationY.value = viewParams.animateFromTranslationY
                translateYSpring.setStartVelocity(viewParams.startVelocity)
                translateYSpring.setStartVelocity(
                    max(FASTEST_SWIPE_VELOCITY, viewParams.startVelocity)
                )
                translateYSpring.setSpring(
                    defaultSpring.apply {
                        dampingRatio =
                            if (viewParams.startVelocity > 0f) TOUCH_FLING_DAMPING_RATIO else 1f
                    }
                )
            }

            translateYSpring.animateToFinalPosition(viewParams.translationY)
@@ -182,7 +205,7 @@ constructor(
                    Log.d(
                        TAG,
                        "Attempting to modify params of surface that isn't " +
                            "animating. Ignoring."
                            "animating. Ignoring.",
                    )
                    matrix.set(Matrix.IDENTITY_MATRIX)
                    return@execute
@@ -206,17 +229,42 @@ constructor(
                    with(SurfaceControl.Transaction()) {
                        setMatrix(
                            sc,
                            matrix.apply { setTranslate(/* dx= */ 0f, translationY) },
                            tmpFloat
                            matrix.apply {
                                setTranslate(/* dx= */ 0f, translationY)
                                val percentTranslated =
                                    1f -
                                        (animatedTranslationY.value /
                                            viewParams.animateFromTranslationY)
                                setScale(
                                    95f + (5f * percentTranslated),
                                    95f + (5f * percentTranslated),
                                    surfaceBehind!!.screenSpaceBounds.width() / 2f,
                                    surfaceBehind!!.screenSpaceBounds.height() * .75f,
                                )
                            },
                            tmpFloat,
                        )
                        setAlpha(sc, alpha)
                        setCornerRadius(sc, roundedCornerRadius)
                        apply()
                    }
                } else {
                    val percentTranslated = 1f - (translationY / viewParams.animateFromTranslationY)
                    surfaceTransactionApplier.scheduleApply(
                        SyncRtSurfaceTransactionApplier.SurfaceParams.Builder(sc)
                            .withMatrix(matrix.apply { setTranslate(/* dx= */ 0f, translationY) })
                            .withMatrix(
                                matrix.apply {
                                    setScale(
                                        .93f + (.07f * percentTranslated),
                                        .93f + (.07f * percentTranslated),
                                        surfaceBehind!!.screenSpaceBounds.width() / 2f,
                                        surfaceBehind!!.screenSpaceBounds.height() * .66f,
                                    )
                                    postTranslate(/* dx= */ 0f, translationY)
                                }
                            )
                            .withAlpha(alpha)
                            .withCornerRadius(roundedCornerRadius)
                            .build()
                    )
                }