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

Commit 5a5e5a4e authored by Luca Zuccarini's avatar Luca Zuccarini
Browse files

1.2 Extract IRemoteAnimationFinishedCallback from the impl.

This removes the first half of the legacy API from the common impl
by wrapping it in a generic runnable.

Trying to make each of the CLs as small as possible to keep them
digestible and low risk. For the refactor plan see
go/animlib-shell-refactor-plan.

Bug: 397180418
Flag: EXEMPT simple refactor
Test: atest ActivityTransitionAnimatorTest + manual
Change-Id: Iddf7eb7bfa631b8c4370d40ca3acb9e951bf890f
parent 1e94d65b
Loading
Loading
Loading
Loading
+43 −25
Original line number Original line Diff line number Diff line
@@ -1218,7 +1218,7 @@ constructor(
            wallpapers: Array<out RemoteAnimationTarget>?,
            wallpapers: Array<out RemoteAnimationTarget>?,
            nonApps: Array<out RemoteAnimationTarget>?,
            nonApps: Array<out RemoteAnimationTarget>?,
            callback: IRemoteAnimationFinishedCallback?,
            callback: IRemoteAnimationFinishedCallback?,
        ) = impl.onAnimationStart(apps, callback)
        ) = impl.onAnimationStart(apps, onAnimationFinished = { callback?.invoke() })


        @UiThread
        @UiThread
        internal fun takeOverAnimation(
        internal fun takeOverAnimation(
@@ -1226,9 +1226,24 @@ constructor(
            startWindowStates: Array<out WindowAnimationState>,
            startWindowStates: Array<out WindowAnimationState>,
            startTransaction: SurfaceControl.Transaction,
            startTransaction: SurfaceControl.Transaction,
            callback: IRemoteAnimationFinishedCallback?,
            callback: IRemoteAnimationFinishedCallback?,
        ) = impl.takeOverAnimation(apps, startWindowStates, startTransaction, callback)
        ) {
            impl.takeOverAnimation(
                apps,
                startWindowStates,
                startTransaction,
                onAnimationFinished = { callback?.invoke() },
            )
        }


        @UiThread override fun onAnimationCancelled() = impl.onAnimationCancelled()
        @UiThread override fun onAnimationCancelled() = impl.onAnimationCancelled()

        private fun IRemoteAnimationFinishedCallback.invoke() {
            try {
                onAnimationFinished()
            } catch (e: RemoteException) {
                e.printStackTrace()
            }
        }
    }
    }


    /** Implementation of launch and return animations agnostic of WindowManager API. */
    /** Implementation of launch and return animations agnostic of WindowManager API. */
@@ -1321,19 +1336,19 @@ constructor(
        @UiThread
        @UiThread
        fun onAnimationStart(
        fun onAnimationStart(
            apps: Array<out RemoteAnimationTarget>?,
            apps: Array<out RemoteAnimationTarget>?,
            callback: IRemoteAnimationFinishedCallback?,
            onAnimationFinished: () -> Unit,
        ) {
        ) {
            val window = setUpAnimation(apps, callback) ?: return
            val window = setUpAnimation(apps, onAnimationFinished) ?: return


            if (controller.windowAnimatorState == null || !longLivedReturnAnimationsEnabled()) {
            if (controller.windowAnimatorState == null || !longLivedReturnAnimationsEnabled()) {
                startAnimation(window, iCallback = callback)
                startAnimation(window, onAnimationFinished = onAnimationFinished)
            } else {
            } else {
                // If a [controller.windowAnimatorState] exists, treat this like a takeover.
                // If a [controller.windowAnimatorState] exists, treat this like a takeover.
                takeOverAnimationInternal(
                takeOverAnimationInternal(
                    window,
                    window,
                    startWindowState = null,
                    startWindowState = null,
                    startTransaction = null,
                    startTransaction = null,
                    callback,
                    onAnimationFinished,
                )
                )
            }
            }
        }
        }
@@ -1343,35 +1358,46 @@ constructor(
            apps: Array<out RemoteAnimationTarget>?,
            apps: Array<out RemoteAnimationTarget>?,
            startWindowStates: Array<out WindowAnimationState>,
            startWindowStates: Array<out WindowAnimationState>,
            startTransaction: SurfaceControl.Transaction,
            startTransaction: SurfaceControl.Transaction,
            callback: IRemoteAnimationFinishedCallback?,
            onAnimationFinished: () -> Unit,
        ) {
        ) {
            val window = setUpAnimation(apps, callback) ?: return
            val window = setUpAnimation(apps, onAnimationFinished) ?: return
            val startWindowState = startWindowStates[apps!!.indexOf(window)]
            val startWindowState = startWindowStates[apps!!.indexOf(window)]
            takeOverAnimationInternal(window, startWindowState, startTransaction, callback)
            takeOverAnimationInternal(
                window,
                startWindowState,
                startTransaction,
                onAnimationFinished,
            )
        }
        }


        private fun takeOverAnimationInternal(
        private fun takeOverAnimationInternal(
            window: RemoteAnimationTarget,
            window: RemoteAnimationTarget,
            startWindowState: WindowAnimationState?,
            startWindowState: WindowAnimationState?,
            startTransaction: SurfaceControl.Transaction?,
            startTransaction: SurfaceControl.Transaction?,
            callback: IRemoteAnimationFinishedCallback?,
            onAnimationFinished: () -> Unit,
        ) {
        ) {
            val useSpring =
            val useSpring =
                !controller.isLaunching && startWindowState != null && startTransaction != null
                !controller.isLaunching && startWindowState != null && startTransaction != null
            startAnimation(window, useSpring, startWindowState, startTransaction, callback)
            startAnimation(
                window,
                useSpring,
                startWindowState,
                startTransaction,
                onAnimationFinished,
            )
        }
        }


        @UiThread
        @UiThread
        private fun setUpAnimation(
        private fun setUpAnimation(
            apps: Array<out RemoteAnimationTarget>?,
            apps: Array<out RemoteAnimationTarget>?,
            callback: IRemoteAnimationFinishedCallback?,
            onAnimationFinished: () -> Unit,
        ): RemoteAnimationTarget? {
        ): RemoteAnimationTarget? {
            removeTimeouts()
            removeTimeouts()


            // The animation was started too late and we already notified the controller that it
            // The animation was started too late and we already notified the controller that it
            // timed out.
            // timed out.
            if (timedOut) {
            if (timedOut) {
                callback?.invoke()
                onAnimationFinished()
                return null
                return null
            }
            }


@@ -1384,7 +1410,7 @@ constructor(
            val window = findTargetWindowIfPossible(apps)
            val window = findTargetWindowIfPossible(apps)
            if (window == null) {
            if (window == null) {
                Log.i(TAG, "Aborting the animation as no window is opening")
                Log.i(TAG, "Aborting the animation as no window is opening")
                callback?.invoke()
                onAnimationFinished()


                if (DEBUG_TRANSITION_ANIMATION) {
                if (DEBUG_TRANSITION_ANIMATION) {
                    Log.d(
                    Log.d(
@@ -1455,7 +1481,7 @@ constructor(
            useSpring: Boolean = false,
            useSpring: Boolean = false,
            startingWindowState: WindowAnimationState? = null,
            startingWindowState: WindowAnimationState? = null,
            startTransaction: SurfaceControl.Transaction? = null,
            startTransaction: SurfaceControl.Transaction? = null,
            iCallback: IRemoteAnimationFinishedCallback? = null,
            onAnimationFinished: () -> Unit,
        ) {
        ) {
            if (TransitionAnimator.DEBUG) {
            if (TransitionAnimator.DEBUG) {
                Log.d(TAG, "Remote animation started")
                Log.d(TAG, "Remote animation started")
@@ -1617,7 +1643,7 @@ constructor(


                    override fun onTransitionAnimationEnd(isExpandingFullyAbove: Boolean) {
                    override fun onTransitionAnimationEnd(isExpandingFullyAbove: Boolean) {
                        listener?.onTransitionAnimationEnd()
                        listener?.onTransitionAnimationEnd()
                        if (!instantHideShade()) iCallback?.invoke()
                        if (!instantHideShade()) onAnimationFinished()


                        if (reparent) {
                        if (reparent) {
                            val cleanUpTransitionLeash: () -> Unit = {
                            val cleanUpTransitionLeash: () -> Unit = {
@@ -1666,7 +1692,7 @@ constructor(
                        }
                        }
                        delegate.onTransitionAnimationEnd(isExpandingFullyAbove)
                        delegate.onTransitionAnimationEnd(isExpandingFullyAbove)


                        if (instantHideShade()) iCallback?.invoke()
                        if (instantHideShade()) onAnimationFinished()
                    }
                    }


                    override fun onTransitionAnimationProgress(
                    override fun onTransitionAnimationProgress(
@@ -1895,14 +1921,6 @@ constructor(
            listener?.onTransitionAnimationCancelled()
            listener?.onTransitionAnimationCancelled()
        }
        }


        private fun IRemoteAnimationFinishedCallback.invoke() {
            try {
                onAnimationFinished()
            } catch (e: RemoteException) {
                e.printStackTrace()
            }
        }

        private fun Rect.hasGreaterAreaThan(other: Rect): Boolean {
        private fun Rect.hasGreaterAreaThan(other: Rect): Boolean {
            return (this.width() * this.height()) > (other.width() * other.height())
            return (this.width() * this.height()) > (other.width() * other.height())
        }
        }