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

Commit 401bc29f authored by Jordan Demeulenaere's avatar Jordan Demeulenaere
Browse files

Extract interpolators and durations out of LaunchAnimator

This CL extracts the durations and interpolators from LaunchAnimator so
that we can use different durations and interpolators in the
DialogLaunchAnimator (compared to the ActivityLaunchAnimator).

This CL is a no-op and does not change the interpolators and durations
yet. Those will be changed in a follow-up CL.

Bug: 208241926
Test: Manual
Change-Id: Ic4d7def45cabc8c73700efcec67706eaf81b2b9e
parent 9ba35a27
Loading
Loading
Loading
Loading
+0 −18
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2021 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.
-->
<pathInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
    android:pathData="M 0, 0 C 0.1217, 0.0462, 0.15, 0.4686, 0.1667, 0.66 C 0.1834, 0.8878, 0.1667, 1, 1, 1" />
 No newline at end of file
+0 −18
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2021 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.
-->
<pathInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
    android:pathData="M 0,0 C 0.05, 0, 0.133333, 0.06, 0.166666, 0.4 C 0.208333, 0.82, 0.25, 1, 1, 1" />
 No newline at end of file
+41 −9
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.app.ActivityTaskManager
import android.app.PendingIntent
import android.app.TaskInfo
import android.graphics.Matrix
import android.graphics.Path
import android.graphics.Rect
import android.graphics.RectF
import android.os.Looper
@@ -34,6 +35,7 @@ import android.view.SyncRtSurfaceTransactionApplier
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.view.animation.Interpolator
import android.view.animation.PathInterpolator
import com.android.internal.annotations.VisibleForTesting
import com.android.internal.policy.ScreenDecorationsUtils
@@ -45,16 +47,46 @@ private const val TAG = "ActivityLaunchAnimator"
 * A class that allows activities to be started in a seamless way from a view that is transforming
 * nicely into the starting window.
 */
class ActivityLaunchAnimator(private val launchAnimator: LaunchAnimator) {
class ActivityLaunchAnimator(
    private val launchAnimator: LaunchAnimator = LaunchAnimator(TIMINGS, INTERPOLATORS)
) {
    companion object {
        @JvmField
        val TIMINGS = LaunchAnimator.Timings(
            totalDuration = 500L,
            contentBeforeFadeOutDelay = 0L,
            contentBeforeFadeOutDuration = 150L,
            contentAfterFadeInDelay = 150L,
            contentAfterFadeInDuration = 183L
        )

        val INTERPOLATORS = LaunchAnimator.Interpolators(
            positionInterpolator = Interpolators.EMPHASIZED,
            positionXInterpolator = createPositionXInterpolator(),
            contentBeforeFadeOutInterpolator = Interpolators.LINEAR_OUT_SLOW_IN,
            contentAfterFadeInInterpolator = PathInterpolator(0f, 0f, 0.6f, 1f)
        )

        /** Durations & interpolators for the navigation bar fading in & out. */
        private const val ANIMATION_DURATION_NAV_FADE_IN = 266L
        private const val ANIMATION_DURATION_NAV_FADE_OUT = 133L
        private const val ANIMATION_DELAY_NAV_FADE_IN =
            LaunchAnimator.ANIMATION_DURATION - ANIMATION_DURATION_NAV_FADE_IN
        private const val LAUNCH_TIMEOUT = 1000L
        private val ANIMATION_DELAY_NAV_FADE_IN =
            TIMINGS.totalDuration - ANIMATION_DURATION_NAV_FADE_IN

        private val NAV_FADE_IN_INTERPOLATOR = PathInterpolator(0f, 0f, 0f, 1f)
        private val NAV_FADE_IN_INTERPOLATOR = Interpolators.STANDARD_DECELERATE
        private val NAV_FADE_OUT_INTERPOLATOR = PathInterpolator(0.2f, 0f, 1f, 1f)

        /** The time we wait before timing out the remote animation after starting the intent. */
        private const val LAUNCH_TIMEOUT = 1000L

        private fun createPositionXInterpolator(): Interpolator {
            val path = Path().apply {
                moveTo(0f, 0f)
                cubicTo(0.1217f, 0.0462f, 0.15f, 0.4686f, 0.1667f, 0.66f)
                cubicTo(0.1834f, 0.8878f, 0.1667f, 1f, 1f, 1f)
            }
            return PathInterpolator(path)
        }
    }

    /**
@@ -107,8 +139,8 @@ class ActivityLaunchAnimator(private val launchAnimator: LaunchAnimator) {
        val animationAdapter = if (!hideKeyguardWithAnimation) {
            RemoteAnimationAdapter(
                runner,
                LaunchAnimator.ANIMATION_DURATION,
                LaunchAnimator.ANIMATION_DURATION - 150 /* statusBarTransitionDelay */
                TIMINGS.totalDuration,
                TIMINGS.totalDuration - 150 /* statusBarTransitionDelay */
            )
        } else {
            null
@@ -449,7 +481,7 @@ class ActivityLaunchAnimator(private val launchAnimator: LaunchAnimator) {
            state: LaunchAnimator.State,
            linearProgress: Float
        ) {
            val fadeInProgress = LaunchAnimator.getProgress(linearProgress,
            val fadeInProgress = LaunchAnimator.getProgress(TIMINGS, linearProgress,
                ANIMATION_DELAY_NAV_FADE_IN, ANIMATION_DURATION_NAV_FADE_OUT)

            val params = SyncRtSurfaceTransactionApplier.SurfaceParams.Builder(navigationBar.leash)
@@ -464,7 +496,7 @@ class ActivityLaunchAnimator(private val launchAnimator: LaunchAnimator) {
                    .withWindowCrop(windowCrop)
                    .withVisibility(true)
            } else {
                val fadeOutProgress = LaunchAnimator.getProgress(linearProgress, 0,
                val fadeOutProgress = LaunchAnimator.getProgress(TIMINGS, linearProgress, 0,
                    ANIMATION_DURATION_NAV_FADE_OUT)
                params.withAlpha(1f - NAV_FADE_OUT_INTERPOLATOR.getInterpolation(fadeOutProgress))
            }
+7 −9
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.Rect
import android.os.Looper
@@ -44,12 +43,13 @@ private const val TAG = "DialogLaunchAnimator"
 * nicely into the starting dialog.
 */
class DialogLaunchAnimator @JvmOverloads constructor(
    private val context: Context,
    private val launchAnimator: LaunchAnimator,
    private val dreamManager: IDreamManager,
    private val launchAnimator: LaunchAnimator = LaunchAnimator(TIMINGS, INTERPOLATORS),
    private var isForTesting: Boolean = false
) {
    private companion object {
        private val TIMINGS = ActivityLaunchAnimator.TIMINGS
        private val INTERPOLATORS = ActivityLaunchAnimator.INTERPOLATORS
        private val TAG_LAUNCH_ANIMATION_RUNNING = R.id.launch_animation_running
    }

@@ -98,7 +98,6 @@ class DialogLaunchAnimator @JvmOverloads constructor(
        animateFrom.setTag(TAG_LAUNCH_ANIMATION_RUNNING, true)

        val animatedDialog = AnimatedDialog(
                context,
                launchAnimator,
                dreamManager,
                animateFrom,
@@ -160,7 +159,6 @@ class DialogLaunchAnimator @JvmOverloads constructor(
}

private class AnimatedDialog(
    private val context: Context,
    private val launchAnimator: LaunchAnimator,
    private val dreamManager: IDreamManager,

@@ -177,7 +175,7 @@ private class AnimatedDialog(
    val dialog: Dialog,

    /** Whether we should animate the dialog background when its bounds change. */
    private val animateBackgroundBoundsChange: Boolean,
    animateBackgroundBoundsChange: Boolean,

    /** Launch animation corresponding to the parent [AnimatedDialog]. */
    private val parentAnimatedDialog: AnimatedDialog? = null,
@@ -275,14 +273,14 @@ private class AnimatedDialog(
            // and the view that we added so that we can dismiss the dialog when this view is
            // clicked. This is necessary because DecorView overrides onTouchEvent and therefore we
            // can't set the click listener directly on the (now fullscreen) DecorView.
            val fullscreenTransparentBackground = FrameLayout(context)
            val fullscreenTransparentBackground = FrameLayout(dialog.context)
            decorView.addView(
                fullscreenTransparentBackground,
                0 /* index */,
                FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
            )

            val dialogContentWithBackground = FrameLayout(context)
            val dialogContentWithBackground = FrameLayout(dialog.context)
            dialogContentWithBackground.background = decorView.background

            // Make the window background transparent. Note that setting the window (or DecorView)
@@ -510,7 +508,7 @@ private class AnimatedDialog(

    private fun onDialogDismissed() {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            context.mainExecutor.execute { onDialogDismissed() }
            dialog.context.mainExecutor.execute { onDialogDismissed() }
            return
        }

+76 −30
Original line number Diff line number Diff line
@@ -27,25 +27,19 @@ import android.util.Log
import android.util.MathUtils
import android.view.View
import android.view.ViewGroup
import android.view.animation.AnimationUtils
import android.view.animation.PathInterpolator
import android.view.animation.Interpolator
import com.android.systemui.animation.Interpolators.LINEAR
import kotlin.math.roundToInt

private const val TAG = "LaunchAnimator"

/** A base class to animate a window launch (activity or dialog) from a view . */
class LaunchAnimator @JvmOverloads constructor(
    context: Context,
    private val isForTesting: Boolean = false
class LaunchAnimator(
    private val timings: Timings,
    private val interpolators: Interpolators
) {
    companion object {
        internal const val DEBUG = false
        const val ANIMATION_DURATION = 500L
        private const val ANIMATION_DURATION_FADE_OUT_CONTENT = 150L
        private const val ANIMATION_DURATION_FADE_IN_WINDOW = 183L
        private const val ANIMATION_DELAY_FADE_IN_WINDOW = ANIMATION_DURATION_FADE_OUT_CONTENT

        private val WINDOW_FADE_IN_INTERPOLATOR = PathInterpolator(0f, 0f, 0.6f, 1f)
        private val SRC_MODE = PorterDuffXfermode(PorterDuff.Mode.SRC)

        /**
@@ -53,23 +47,20 @@ class LaunchAnimator @JvmOverloads constructor(
         * sub-animation starting [delay] ms after the launch animation and that lasts [duration].
         */
        @JvmStatic
        fun getProgress(linearProgress: Float, delay: Long, duration: Long): Float {
        fun getProgress(
            timings: Timings,
            linearProgress: Float,
            delay: Long,
            duration: Long
        ): Float {
            return MathUtils.constrain(
                (linearProgress * ANIMATION_DURATION - delay) / duration,
                (linearProgress * timings.totalDuration - delay) / duration,
                0.0f,
                1.0f
            )
        }
    }

    /** The interpolator used for the width, height, Y position and corner radius. */
    private val animationInterpolator = AnimationUtils.loadInterpolator(context,
        R.interpolator.launch_animation_interpolator_y)

    /** The interpolator used for the X position. */
    private val animationInterpolatorX = AnimationUtils.loadInterpolator(context,
        R.interpolator.launch_animation_interpolator_x)

    private val launchContainerLocation = IntArray(2)
    private val cornerRadii = FloatArray(8)

@@ -159,6 +150,45 @@ class LaunchAnimator @JvmOverloads constructor(
        fun cancel()
    }

    /** The timings (durations and delays) used by this animator. */
    class Timings(
        /** The total duration of the animation. */
        val totalDuration: Long,

        /** The time to wait before fading out the expanding content. */
        val contentBeforeFadeOutDelay: Long,

        /** The duration of the expanding content fade out. */
        val contentBeforeFadeOutDuration: Long,

        /**
         * The time to wait before fading in the expanded content (usually an activity or dialog
         * window).
         */
        val contentAfterFadeInDelay: Long,

        /** The duration of the expanded content fade in. */
        val contentAfterFadeInDuration: Long
    )

    /** The interpolators used by this animator. */
    class Interpolators(
        /** The interpolator used for the Y position, width, height and corner radius. */
        val positionInterpolator: Interpolator,

        /**
         * The interpolator used for the X position. This can be different than
         * [positionInterpolator] to create an arc-path during the animation.
         */
        val positionXInterpolator: Interpolator = positionInterpolator,

        /** The interpolator used when fading out the expanding content. */
        val contentBeforeFadeOutInterpolator: Interpolator,

        /** The interpolator used when fading in the expanded content. */
        val contentAfterFadeInInterpolator: Interpolator
    )

    /**
     * Start a launch animation controlled by [controller] towards [endState]. An intermediary
     * layer with [windowBackgroundColor] will fade in then fade out above the expanding view, and
@@ -221,8 +251,8 @@ class LaunchAnimator @JvmOverloads constructor(

        // Update state.
        val animator = ValueAnimator.ofFloat(0f, 1f)
        animator.duration = if (isForTesting) 0 else ANIMATION_DURATION
        animator.interpolator = Interpolators.LINEAR
        animator.duration = timings.totalDuration
        animator.interpolator = LINEAR

        val launchContainerOverlay = launchContainer.overlay
        var cancelled = false
@@ -260,8 +290,8 @@ class LaunchAnimator @JvmOverloads constructor(
            // TODO(b/184121838): Use reverse interpolators to get the same path/arc as the non
            // reversed animation.
            val linearProgress = animation.animatedFraction
            val progress = animationInterpolator.getInterpolation(linearProgress)
            val xProgress = animationInterpolatorX.getInterpolation(linearProgress)
            val progress = interpolators.positionInterpolator.getInterpolation(linearProgress)
            val xProgress = interpolators.positionXInterpolator.getInterpolation(linearProgress)

            val xCenter = MathUtils.lerp(startCenterX, endCenterX, xProgress)
            val halfWidth = MathUtils.lerp(startWidth, endWidth, progress) / 2f
@@ -278,7 +308,12 @@ class LaunchAnimator @JvmOverloads constructor(

            // The expanding view can/should be hidden once it is completely covered by the opening
            // window.
            state.visible = getProgress(linearProgress, 0, ANIMATION_DURATION_FADE_OUT_CONTENT) < 1
            state.visible = getProgress(
                timings,
                linearProgress,
                timings.contentBeforeFadeOutDelay,
                timings.contentBeforeFadeOutDuration
            ) < 1

            applyStateToWindowBackgroundLayer(
                windowBackgroundLayer,
@@ -337,14 +372,25 @@ class LaunchAnimator @JvmOverloads constructor(

        // We first fade in the background layer to hide the expanding view, then fade it out
        // with SRC mode to draw a hole punch in the status bar and reveal the opening window.
        val fadeInProgress = getProgress(linearProgress, 0, ANIMATION_DURATION_FADE_OUT_CONTENT)
        val fadeInProgress = getProgress(
            timings,
            linearProgress,
            timings.contentBeforeFadeOutDelay,
            timings.contentBeforeFadeOutDuration
        )
        if (fadeInProgress < 1) {
            val alpha = Interpolators.LINEAR_OUT_SLOW_IN.getInterpolation(fadeInProgress)
            val alpha =
                interpolators.contentBeforeFadeOutInterpolator.getInterpolation(fadeInProgress)
            drawable.alpha = (alpha * 0xFF).roundToInt()
        } else {
            val fadeOutProgress = getProgress(
                linearProgress, ANIMATION_DELAY_FADE_IN_WINDOW, ANIMATION_DURATION_FADE_IN_WINDOW)
            val alpha = 1 - WINDOW_FADE_IN_INTERPOLATOR.getInterpolation(fadeOutProgress)
                timings,
                linearProgress,
                timings.contentAfterFadeInDelay,
                timings.contentAfterFadeInDuration
            )
            val alpha =
                1 - interpolators.contentAfterFadeInInterpolator.getInterpolation(fadeOutProgress)
            drawable.alpha = (alpha * 0xFF).roundToInt()

            if (drawHole) {
Loading