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

Commit 862919f8 authored by Omar Miatello's avatar Omar Miatello Committed by Automerger Merge Worker
Browse files

Merge changes from topic "predback-qs-dialog" into tm-qpr-dev am: 1b5fb5d8

parents beeb1739 1b5fb5d8
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
package com.android.systemui.animation

interface AnimationFeatureFlags {
    val isPredictiveBackQsDialogAnim: Boolean
        get() = false
}
+50 −9
Original line number Diff line number Diff line
@@ -33,8 +33,13 @@ import android.view.WindowInsets
import android.view.WindowManager
import android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS
import android.widget.FrameLayout
import android.window.OnBackInvokedDispatcher
import com.android.internal.jank.InteractionJankMonitor
import com.android.internal.jank.InteractionJankMonitor.CujType
import com.android.systemui.animation.back.BackAnimationSpec
import com.android.systemui.animation.back.applyTo
import com.android.systemui.animation.back.floatingSystemSurfacesForSysUi
import com.android.systemui.animation.back.onBackAnimationCallbackFrom
import kotlin.math.roundToInt

private const val TAG = "DialogLaunchAnimator"
@@ -55,8 +60,9 @@ class DialogLaunchAnimator
constructor(
    private val callback: Callback,
    private val interactionJankMonitor: InteractionJankMonitor,
    private val featureFlags: AnimationFeatureFlags,
    private val launchAnimator: LaunchAnimator = LaunchAnimator(TIMINGS, INTERPOLATORS),
    private val isForTesting: Boolean = false
    private val isForTesting: Boolean = false,
) {
    private companion object {
        private val TIMINGS = ActivityLaunchAnimator.TIMINGS
@@ -273,15 +279,16 @@ constructor(

        val animatedDialog =
            AnimatedDialog(
                launchAnimator,
                callback,
                interactionJankMonitor,
                controller,
                launchAnimator = launchAnimator,
                callback = callback,
                interactionJankMonitor = interactionJankMonitor,
                controller = controller,
                onDialogDismissed = { openedDialogs.remove(it) },
                dialog = dialog,
                animateBackgroundBoundsChange,
                animatedParent,
                isForTesting,
                animateBackgroundBoundsChange = animateBackgroundBoundsChange,
                parentAnimatedDialog = animatedParent,
                forceDisableSynchronization = isForTesting,
                featureFlags = featureFlags,
            )

        openedDialogs.add(animatedDialog)
@@ -517,6 +524,7 @@ private class AnimatedDialog(
     * Whether synchronization should be disabled, which can be useful if we are running in a test.
     */
    private val forceDisableSynchronization: Boolean,
    private val featureFlags: AnimationFeatureFlags,
) {
    /**
     * The DecorView of this dialog window.
@@ -778,12 +786,45 @@ private class AnimatedDialog(
        // the dialog.
        dialog.setDismissOverride(this::onDialogDismissed)

        if (featureFlags.isPredictiveBackQsDialogAnim) {
            // TODO(b/265923095) Improve animations for QS dialogs on configuration change
            registerOnBackInvokedCallback(targetView = dialogContentWithBackground)
        }

        // Show the dialog.
        dialog.show()

        moveSourceDrawingToDialog()
    }

    private fun registerOnBackInvokedCallback(targetView: View) {
        val metrics = targetView.resources.displayMetrics

        val onBackAnimationCallback =
            onBackAnimationCallbackFrom(
                backAnimationSpec = BackAnimationSpec.floatingSystemSurfacesForSysUi(metrics),
                displayMetrics = metrics, // TODO(b/265060720): We could remove this
                onBackProgressed = { backTransformation -> backTransformation.applyTo(targetView) },
                onBackInvoked = { dialog.dismiss() },
            )

        val dispatcher = dialog.onBackInvokedDispatcher
        targetView.addOnAttachStateChangeListener(
            object : View.OnAttachStateChangeListener {
                override fun onViewAttachedToWindow(v: View) {
                    dispatcher.registerOnBackInvokedCallback(
                        OnBackInvokedDispatcher.PRIORITY_DEFAULT,
                        onBackAnimationCallback
                    )
                }

                override fun onViewDetachedFromWindow(v: View) {
                    targetView.removeOnAttachStateChangeListener(this)
                    dispatcher.unregisterOnBackInvokedCallback(onBackAnimationCallback)
                }
            }
        )
    }

    private fun moveSourceDrawingToDialog() {
        if (decorView.viewRootImpl == null) {
            // Make sure that we have access to the dialog view root to move the drawing to the
+73 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.systemui.animation.back

import android.util.DisplayMetrics
import android.view.animation.Interpolator
import android.window.BackEvent
import com.android.systemui.animation.Interpolators
import com.android.systemui.util.dpToPx

/** Used to convert [BackEvent] into a [BackTransformation]. */
fun interface BackAnimationSpec {

    /** Computes transformation based on a [backEvent] and sets it to [result]. */
    fun getBackTransformation(
        backEvent: BackEvent,
        progressY: Float, // TODO(b/265060720): Remove progressY. Could be retrieved from backEvent
        result: BackTransformation,
    )

    companion object
}

/** Create a [BackAnimationSpec] from [displayMetrics] and design specs. */
fun BackAnimationSpec.Companion.createFloatingSurfaceAnimationSpec(
    displayMetrics: DisplayMetrics,
    maxMarginXdp: Float,
    maxMarginYdp: Float,
    minScale: Float,
    translateXEasing: Interpolator = Interpolators.STANDARD_DECELERATE,
    translateYEasing: Interpolator = Interpolators.LINEAR,
    scaleEasing: Interpolator = Interpolators.STANDARD_DECELERATE,
): BackAnimationSpec {
    val screenWidthPx = displayMetrics.widthPixels
    val screenHeightPx = displayMetrics.heightPixels

    val maxMarginXPx = maxMarginXdp.dpToPx(displayMetrics)
    val maxMarginYPx = maxMarginYdp.dpToPx(displayMetrics)
    val maxTranslationXByScale = (screenWidthPx - screenWidthPx * minScale) / 2
    val maxTranslationX = maxTranslationXByScale - maxMarginXPx
    val maxTranslationYByScale = (screenHeightPx - screenHeightPx * minScale) / 2
    val maxTranslationY = maxTranslationYByScale - maxMarginYPx
    val minScaleReversed = 1f - minScale

    return BackAnimationSpec { backEvent, progressY, result ->
        val direction = if (backEvent.swipeEdge == BackEvent.EDGE_LEFT) 1 else -1
        val progressX = backEvent.progress

        val ratioTranslateX = translateXEasing.getInterpolation(progressX)
        val ratioTranslateY = translateYEasing.getInterpolation(progressY)
        val ratioScale = scaleEasing.getInterpolation(progressX)

        result.apply {
            translateX = ratioTranslateX * direction * maxTranslationX
            translateY = ratioTranslateY * maxTranslationY
            scale = 1f - (ratioScale * minScaleReversed)
        }
    }
}
+75 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.systemui.animation.back

import android.util.DisplayMetrics

/**
 * SysUI transitions - Dismiss app (ST1) Return to launching surface or place of origin
 * https://carbon.googleplex.com/predictive-back-for-apps/pages/st-1-dismiss-app
 */
fun BackAnimationSpec.Companion.dismissAppForSysUi(
    displayMetrics: DisplayMetrics,
): BackAnimationSpec =
    BackAnimationSpec.createFloatingSurfaceAnimationSpec(
        displayMetrics = displayMetrics,
        maxMarginXdp = 8f,
        maxMarginYdp = 8f,
        minScale = 0.8f,
    )

/**
 * SysUI transitions - Cross task (ST2) Return to previous task/app, keeping the current one open
 * https://carbon.googleplex.com/predictive-back-for-apps/pages/st-2-cross-task
 */
fun BackAnimationSpec.Companion.crossTaskForSysUi(
    displayMetrics: DisplayMetrics,
): BackAnimationSpec =
    BackAnimationSpec.createFloatingSurfaceAnimationSpec(
        displayMetrics = displayMetrics,
        maxMarginXdp = 8f,
        maxMarginYdp = 8f,
        minScale = 0.8f,
    )

/**
 * SysUI transitions - Inner area dismiss (ST3) Dismiss non-detachable surface
 * https://carbon.googleplex.com/predictive-back-for-apps/pages/st-3-inner-area-dismiss
 */
fun BackAnimationSpec.Companion.innerAreaDismissForSysUi(
    displayMetrics: DisplayMetrics,
): BackAnimationSpec =
    BackAnimationSpec.createFloatingSurfaceAnimationSpec(
        displayMetrics = displayMetrics,
        maxMarginXdp = 0f,
        maxMarginYdp = 0f,
        minScale = 0.9f,
    )

/**
 * SysUI transitions - Floating system surfaces (ST4)
 * https://carbon.googleplex.com/predictive-back-for-apps/pages/st-4-floating-system-surfaces
 */
fun BackAnimationSpec.Companion.floatingSystemSurfacesForSysUi(
    displayMetrics: DisplayMetrics,
): BackAnimationSpec =
    BackAnimationSpec.createFloatingSurfaceAnimationSpec(
        displayMetrics = displayMetrics,
        maxMarginXdp = 8f,
        maxMarginYdp = 8f,
        minScale = 0.8f,
    )
+39 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.systemui.animation.back

import android.view.View

/**
 * This object that represents the transformation to apply to the target. The properties of this
 * object are mutable for performance reasons (avoid recreating this object)
 */
data class BackTransformation(
    var translateX: Float = Float.NaN,
    var translateY: Float = Float.NaN,
    var scale: Float = Float.NaN,
)

/** Apply the transformation to the [targetView] */
fun BackTransformation.applyTo(targetView: View) {
    if (translateX.isFinite()) targetView.translationX = translateX
    if (translateY.isFinite()) targetView.translationY = translateY
    if (scale.isFinite()) {
        targetView.scaleX = scale
        targetView.scaleY = scale
    }
}
Loading