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

Commit c8a53f6a authored by Yuichiro Hanada's avatar Yuichiro Hanada Committed by Android (Google) Code Review
Browse files

Merge "Extract minimize animation to WMShell" into main

parents fb7a8dfe 300726e4
Loading
Loading
Loading
Loading
+74 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.wm.shell.shared.animation

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.util.DisplayMetrics
import android.view.SurfaceControl.Transaction
import android.view.animation.LinearInterpolator
import android.view.animation.PathInterpolator
import android.window.TransitionInfo.Change

/** Creates minimization animation */
object MinimizeAnimator {

    private const val MINIMIZE_ANIM_ALPHA_DURATION_MS = 100L

    private val STANDARD_ACCELERATE = PathInterpolator(0.3f, 0f, 1f, 1f)

    private val minimizeBoundsAnimationDef =
        WindowAnimator.BoundsAnimationParams(
            durationMs = 200,
            endOffsetYDp = 12f,
            endScale = 0.97f,
            interpolator = STANDARD_ACCELERATE,
        )

    @JvmStatic
    fun create(
        displayMetrics: DisplayMetrics,
        change: Change,
        transaction: Transaction,
        onAnimFinish: (Animator) -> Unit,
    ): Animator {
        val boundsAnimator = WindowAnimator.createBoundsAnimator(
            displayMetrics,
            minimizeBoundsAnimationDef,
            change,
            transaction,
        )
        val alphaAnimator = ValueAnimator.ofFloat(1f, 0f).apply {
            duration = MINIMIZE_ANIM_ALPHA_DURATION_MS
            interpolator = LinearInterpolator()
            addUpdateListener { animation ->
                transaction.setAlpha(change.leash, animation.animatedValue as Float).apply()
            }
        }
        val listener = object : Animator.AnimatorListener {
            override fun onAnimationEnd(animator: Animator) = onAnimFinish(animator)
            override fun onAnimationCancel(animator: Animator) = Unit
            override fun onAnimationRepeat(animator: Animator) = Unit
            override fun onAnimationStart(animator: Animator) = Unit
        }
        return AnimatorSet().apply {
            playTogether(boundsAnimator, alphaAnimator)
            addListener(listener)
        }
    }
}
+105 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.wm.shell.shared.animation

import android.animation.RectEvaluator
import android.animation.ValueAnimator
import android.graphics.Rect
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.SurfaceControl
import android.view.animation.Interpolator
import android.window.TransitionInfo

/** Creates animations that can be applied to windows/surfaces. */
object WindowAnimator {

    /** Parameters defining a window bounds animation. */
    data class BoundsAnimationParams(
        val durationMs: Long,
        val startOffsetYDp: Float = 0f,
        val endOffsetYDp: Float = 0f,
        val startScale: Float = 1f,
        val endScale: Float = 1f,
        val interpolator: Interpolator,
    )

    /**
     * Creates an animator to reposition and scale the bounds of the leash of the given change.
     *
     * @param displayMetrics the metrics of the display where the animation plays in
     * @param boundsAnimDef the parameters for the animation itself (duration, scale, position)
     * @param change the change to which the animation should be applied
     * @param transaction the transaction to apply the animation to
     */
    fun createBoundsAnimator(
        displayMetrics: DisplayMetrics,
        boundsAnimDef: BoundsAnimationParams,
        change: TransitionInfo.Change,
        transaction: SurfaceControl.Transaction,
    ): ValueAnimator {
        val startBounds =
            createBounds(
                displayMetrics,
                change.startAbsBounds,
                boundsAnimDef.startScale,
                boundsAnimDef.startOffsetYDp,
            )
        val leash = change.leash
        val endBounds =
            createBounds(
                displayMetrics,
                change.startAbsBounds,
                boundsAnimDef.endScale,
                boundsAnimDef.endOffsetYDp,
            )
        return ValueAnimator.ofObject(RectEvaluator(), startBounds, endBounds).apply {
            duration = boundsAnimDef.durationMs
            interpolator = boundsAnimDef.interpolator
            addUpdateListener { animation ->
                val animBounds = animation.animatedValue as Rect
                val animScale = 1 - (1 - boundsAnimDef.endScale) * animation.animatedFraction
                transaction
                    .setPosition(leash, animBounds.left.toFloat(), animBounds.top.toFloat())
                    .setScale(leash, animScale, animScale)
                    .apply()
            }
        }
    }

    private fun createBounds(
        displayMetrics: DisplayMetrics,
        origBounds: Rect,
        scale: Float,
        offsetYDp: Float
    ) = Rect(origBounds).apply {
            check(scale in 0.0..1.0)
            // Scale the  bounds down with an anchor in the center
            inset(
                (origBounds.width().toFloat() * (1 - scale) / 2).toInt(),
                (origBounds.height().toFloat() * (1 - scale) / 2).toInt(),
            )
            val offsetYPx =
                TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_DIP,
                        offsetYDp,
                        displayMetrics,
                    )
                    .toInt()
            offset(/* dx= */ 0, offsetYPx)
        }
}
+147 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.wm.shell.shared.animation

import android.animation.ValueAnimator
import android.graphics.Rect
import android.util.DisplayMetrics
import android.view.SurfaceControl
import android.window.TransitionInfo
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread
import com.android.app.animation.Interpolators
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.anyFloat
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever

@SmallTest
@RunWith(AndroidJUnit4::class)
class WindowAnimatorTest {

    private val transaction = mock<SurfaceControl.Transaction>()
    private val change = mock<TransitionInfo.Change>()
    private val leash = mock<SurfaceControl>()

    private val displayMetrics = DisplayMetrics().apply { density = 1f }

    @Before
    fun setup() {
        whenever(change.leash).thenReturn(leash)
        whenever(change.startAbsBounds).thenReturn(START_BOUNDS)
        whenever(transaction.setPosition(any(), anyFloat(), anyFloat())).thenReturn(transaction)
        whenever(transaction.setScale(any(), anyFloat(), anyFloat())).thenReturn(transaction)
    }

    @Test
    fun createBoundsAnimator_returnsCorrectDefaultAnimatorParams() = runOnUiThread {
        val boundsAnimParams =
            WindowAnimator.BoundsAnimationParams(
                durationMs = 100L,
                interpolator = Interpolators.STANDARD_ACCELERATE,
            )

        val valueAnimator =
            WindowAnimator.createBoundsAnimator(
                displayMetrics,
                boundsAnimParams,
                change,
                transaction
            )

        assertThat(valueAnimator.duration).isEqualTo(100L)
        assertThat(valueAnimator.interpolator).isEqualTo(Interpolators.STANDARD_ACCELERATE)
        assertStartAndEndBounds(valueAnimator, startBounds = START_BOUNDS, endBounds = START_BOUNDS)
    }

    @Test
    fun createBoundsAnimator_startScaleAndOffset_returnsCorrectBounds() = runOnUiThread {
        val bounds = Rect(/* left= */ 100, /* top= */ 200, /* right= */ 300, /* bottom= */ 400)
        whenever(change.startAbsBounds).thenReturn(bounds)
        val boundsAnimParams =
            WindowAnimator.BoundsAnimationParams(
                durationMs = 100L,
                startOffsetYDp = 10f,
                startScale = 0.5f,
                interpolator = Interpolators.STANDARD_ACCELERATE,
            )

        val valueAnimator =
            WindowAnimator.createBoundsAnimator(
                displayMetrics,
                boundsAnimParams,
                change,
                transaction
            )

        assertStartAndEndBounds(
            valueAnimator,
            startBounds =
                Rect(/* left= */ 150, /* top= */ 260, /* right= */ 250, /* bottom= */ 360),
            endBounds = bounds,
        )
    }

    @Test
    fun createBoundsAnimator_endScaleAndOffset_returnsCorrectBounds() = runOnUiThread {
        val bounds = Rect(/* left= */ 100, /* top= */ 200, /* right= */ 300, /* bottom= */ 400)
        whenever(change.startAbsBounds).thenReturn(bounds)
        val boundsAnimParams =
            WindowAnimator.BoundsAnimationParams(
                durationMs = 100L,
                endOffsetYDp = 10f,
                endScale = 0.5f,
                interpolator = Interpolators.STANDARD_ACCELERATE,
            )

        val valueAnimator =
            WindowAnimator.createBoundsAnimator(
                displayMetrics,
                boundsAnimParams,
                change,
                transaction
            )

        assertStartAndEndBounds(
            valueAnimator,
            startBounds = bounds,
            endBounds = Rect(/* left= */ 150, /* top= */ 260, /* right= */ 250, /* bottom= */ 360),
        )
    }

    private fun assertStartAndEndBounds(
        valueAnimator: ValueAnimator,
        startBounds: Rect,
        endBounds: Rect,
    ) {
        valueAnimator.start()
        valueAnimator.animatedValue
        assertThat(valueAnimator.animatedValue).isEqualTo(startBounds)
        valueAnimator.end()
        assertThat(valueAnimator.animatedValue).isEqualTo(endBounds)
    }

    companion object {
        private val START_BOUNDS =
            Rect(/* left= */ 10, /* top= */ 20, /* right= */ 30, /* bottom= */ 40)
    }
}