From 444cb5e4f0229689073fe139fb5b459be42e0c0d Mon Sep 17 00:00:00 2001 From: Shane Date: Thu, 13 Jul 2023 16:59:19 +0000 Subject: [PATCH] Handle when AnimationUtils getExpectedPresentationTimeNanos is not called in a callback This change tries to handle the case when getExpectedPresentationTimeNanos is not called in a callback (it happens rarely). When getExpectedPresentationTimeNanos is called outside of a callback, we call mChoreographer.getLatestExpectedPresentTimeNanos() to obtain the value. Bug: 288630576 Test: atest AnimationUtilsTest Change-Id: Ide033c69655ddf714d0626ca4a7643476693708c --- core/java/android/view/animation/AnimationUtils.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/core/java/android/view/animation/AnimationUtils.java b/core/java/android/view/animation/AnimationUtils.java index 8ba8b8cca5edf..0699bc1cb734b 100644 --- a/core/java/android/view/animation/AnimationUtils.java +++ b/core/java/android/view/animation/AnimationUtils.java @@ -32,6 +32,7 @@ import android.os.SystemClock; import android.util.AttributeSet; import android.util.TimeUtils; import android.util.Xml; +import android.view.Choreographer; import android.view.InflateException; import org.xmlpull.v1.XmlPullParser; @@ -153,7 +154,13 @@ public class AnimationUtils { */ public static long getExpectedPresentationTimeNanos() { AnimationState state = sAnimationState.get(); - return state.mExpectedPresentationTimeNanos; + if (state.animationClockLocked) { + return state.mExpectedPresentationTimeNanos; + } + // When this methoed is called outside of a Choreographer callback, + // we obtain the value of expectedPresentTimeNanos from the Choreographer. + // This helps avoid returning a time that could potentially be earlier than current time. + return Choreographer.getInstance().getLatestExpectedPresentTimeNanos(); } /**