Fixes NPE from resolveRotation()

RotatatinResolverInternal can be null when the service is not ready.
This change adds a null check to avoid NPE.

Test: atest WindowOrientatinListenerTest
Bug: 199375757
Bug: 199381926

Change-Id: I1b6f3aee3eddf8f565f7ca21b1879f40c36f4e5c
This commit is contained in:
Yi Jiang
2021-12-29 23:59:33 +00:00
parent 4e8636d145
commit 835f63f972
2 changed files with 14 additions and 1 deletions

View File

@@ -1167,6 +1167,10 @@ public abstract class WindowOrientationListener {
if (mRotationResolverService == null) {
mRotationResolverService = LocalServices.getService(
RotationResolverInternal.class);
if (mRotationResolverService == null) {
finalizeRotation(reportedRotation);
return;
}
}
String packageName = null;

View File

@@ -114,7 +114,7 @@ public class WindowOrientationListenerTest {
}
@Test
public void testSensorChanged_normalCase2() {
public void testOnSensorChanged_normalCase2() {
mWindowOrientationListener.mOrientationJudge.onSensorChanged(mFakeSensorEvent);
mFakeRotationResolverInternal.callbackWithFailureResult(
@@ -123,6 +123,15 @@ public class WindowOrientationListenerTest {
assertThat(mFinalizedRotation).isEqualTo(DEFAULT_SENSOR_ROTATION);
}
@Test
public void testOnSensorChanged_rotationResolverServiceIsNull_useSensorResult() {
mWindowOrientationListener.mRotationResolverService = null;
mWindowOrientationListener.mOrientationJudge.onSensorChanged(mFakeSensorEvent);
assertThat(mFinalizedRotation).isEqualTo(DEFAULT_SENSOR_ROTATION);
}
static final class TestableRotationResolver extends RotationResolverInternal {
@Surface.Rotation
RotationResolverCallbackInternal mCallback;