[STL] Fix predictive back with activity-compose:1.10.0-alpha03
activity-compose:1.10.0-alpha03 introduced a stricter coroutine
cancellation policy. The coroutine is now cancelled immediately when the
PredictiveBackHandler is disabled. Therefore we have to run the
transition on the animation scope instead.
Bug: 384152530
Flag: EXEMPT bugfix
Test: PredictiveBackHandlerTest
Change-Id: I2c9eda8c6c51335c547b10430ecfb34db02752f0
Merged-In: I2c9eda8c6c51335c547b10430ecfb34db02752f0
diff --git a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/PredictiveBackHandler.kt b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/PredictiveBackHandler.kt
index b00c8ad..8a6a0d6 100644
--- a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/PredictiveBackHandler.kt
+++ b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/PredictiveBackHandler.kt
@@ -70,6 +70,7 @@
// The predictive back APIs will automatically animate the progress for us in this case
// so there is no need to animate it.
cancelSpec = snap(),
+ animationScope = layoutImpl.animationScope,
)
}
}
diff --git a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/transition/Seek.kt b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/transition/Seek.kt
index 715d979..2b33224 100644
--- a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/transition/Seek.kt
+++ b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/transition/Seek.kt
@@ -30,6 +30,8 @@
import com.android.compose.animation.scene.UserActionResult
import com.android.compose.animation.scene.createSwipeAnimation
import kotlin.coroutines.cancellation.CancellationException
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Job
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collectLatest
@@ -141,6 +143,7 @@
progress: Flow<Float>,
commitSpec: AnimationSpec<Float>?,
cancelSpec: AnimationSpec<Float>?,
+ animationScope: CoroutineScope? = null,
) {
fun animateOffset(targetContent: T, spec: AnimationSpec<Float>?) {
if (state.transitionState != animation.contentTransition || animation.isAnimatingOffset()) {
@@ -176,12 +179,20 @@
}
// Start the transition.
- state.startTransition(animation.contentTransition)
+ animationScope?.launch { startTransition(state, animation, collectionJob) }
+ ?: startTransition(state, animation, collectionJob)
+ }
+}
- // The transition is done. Cancel the collection in case the transition was finished because
- // it was interrupted by another transition.
- if (collectionJob.isActive) {
- collectionJob.cancel()
- }
+private suspend fun <T : ContentKey> startTransition(
+ state: MutableSceneTransitionLayoutStateImpl,
+ animation: SwipeAnimation<T>,
+ progressCollectionJob: Job,
+) {
+ state.startTransition(animation.contentTransition)
+ // The transition is done. Cancel the collection in case the transition was finished
+ // because it was interrupted by another transition.
+ if (progressCollectionJob.isActive) {
+ progressCollectionJob.cancel()
}
}