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
This commit is contained in:
Yi Jiang
2021-04-20 15:46:11 -07:00
parent 332dd22ff9
commit 648a7926ca

View File

@@ -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);
}