From 648a7926ca2b7f4deb490903c97b395441fa39f9 Mon Sep 17 00:00:00 2001 From: Yi Jiang Date: Tue, 20 Apr 2021 15:46:11 -0700 Subject: [PATCH] Assigns Ids to the internal rotation resolver callback. Uses Id to track the freshness of a rotation resolver callback, if the callback is not the latest one, then we should ignore its result. Test: manually tested the feature still work. Bug: 185618470 Bug: 186163795 Change-Id: I3b6843caeccd98274731aaf9611415aee02d7bea --- .../server/wm/WindowOrientationListener.java | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/services/core/java/com/android/server/wm/WindowOrientationListener.java b/services/core/java/com/android/server/wm/WindowOrientationListener.java index 3e099fb84f033..3847cb1e0895b 100644 --- a/services/core/java/com/android/server/wm/WindowOrientationListener.java +++ b/services/core/java/com/android/server/wm/WindowOrientationListener.java @@ -1076,6 +1076,9 @@ public abstract class WindowOrientationListener { private boolean mRotationEvaluationScheduled; private long mRotationResolverTimeoutMillis; private final ActivityTaskManagerInternal mActivityTaskManagerInternal; + private int mCurrentCallbackId = 0; + private Runnable mCancelRotationResolverRequest; + OrientationSensorJudge() { super(); setupRotationResolverParameters(); @@ -1142,8 +1145,6 @@ public abstract class WindowOrientationListener { RotationResolverInternal.class); } - final CancellationSignal cancellationSignal = new CancellationSignal(); - String packageName = null; if (mActivityTaskManagerInternal != null) { final WindowProcessController controller = @@ -1155,16 +1156,40 @@ public abstract class WindowOrientationListener { } } + mCurrentCallbackId++; + + if (mCancelRotationResolverRequest != null) { + getHandler().removeCallbacks(mCancelRotationResolverRequest); + } + final CancellationSignal cancellationSignal = new CancellationSignal(); + mCancelRotationResolverRequest = cancellationSignal::cancel; + getHandler().postDelayed( + mCancelRotationResolverRequest, mRotationResolverTimeoutMillis); + mRotationResolverService.resolveRotation( new RotationResolverInternal.RotationResolverCallbackInternal() { + private final int mCallbackId = mCurrentCallbackId; @Override public void onSuccess(int result) { - finalizeRotation(result); + finalizeRotationIfFresh(result); } @Override public void onFailure(int error) { - finalizeRotation(reportedRotation); + finalizeRotationIfFresh(reportedRotation); + } + + private void finalizeRotationIfFresh(int rotation) { + // Ignore the callback if it's not the latest. + if (mCallbackId == mCurrentCallbackId) { + getHandler().removeCallbacks(mCancelRotationResolverRequest); + finalizeRotation(rotation); + } else { + Slog.d(TAG, String.format( + "An outdated callback received [%s vs. %s]. Ignoring " + + "it.", + mCallbackId, mCurrentCallbackId)); + } } }, packageName, @@ -1172,8 +1197,6 @@ public abstract class WindowOrientationListener { mCurrentRotation, mRotationResolverTimeoutMillis, cancellationSignal); - getHandler().postDelayed(cancellationSignal::cancel, - mRotationResolverTimeoutMillis); } else { finalizeRotation(reportedRotation); }