Respect allowAllRotations for rotation suggestion button

AllowAllRotation config flag controls if it is allowed
to perform rotation in all directions (including
upside down). This config is true for tablets to allow
upside down landscape rotation. When auto rotate is on
this allows to perform this rotation, but when auto
rotate is off, rotation suggestion button
didn't respect this configuration.

Adding allowing to show the rotation suggestion button
when this configuration is 'true'.

Bug: 229565988
Test: atest WmTests:DisplayRotationTests
Change-Id: I62fd2f49b87bf1739baac968581f94f9b9b181a8
This commit is contained in:
Nick Chameyev
2022-04-27 12:51:50 +00:00
parent a3193ed1db
commit 624b03d6cd
2 changed files with 86 additions and 9 deletions

View File

@@ -1224,16 +1224,8 @@ public class DisplayRotation {
|| orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT) {
// Otherwise, use sensor only if requested by the application or enabled
// by default for USER or UNSPECIFIED modes. Does not apply to NOSENSOR.
if (mAllowAllRotations == ALLOW_ALL_ROTATIONS_UNDEFINED) {
// Can't read this during init() because the context doesn't have display metrics at
// that time so we cannot determine tablet vs. phone then.
mAllowAllRotations = mContext.getResources().getBoolean(
R.bool.config_allowAllRotations)
? ALLOW_ALL_ROTATIONS_ENABLED
: ALLOW_ALL_ROTATIONS_DISABLED;
}
if (sensorRotation != Surface.ROTATION_180
|| mAllowAllRotations == ALLOW_ALL_ROTATIONS_ENABLED
|| getAllowAllRotations() == ALLOW_ALL_ROTATIONS_ENABLED
|| orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
|| orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_USER) {
preferredRotation = sensorRotation;
@@ -1322,6 +1314,19 @@ public class DisplayRotation {
}
}
private int getAllowAllRotations() {
if (mAllowAllRotations == ALLOW_ALL_ROTATIONS_UNDEFINED) {
// Can't read this during init() because the context doesn't have display metrics at
// that time so we cannot determine tablet vs. phone then.
mAllowAllRotations = mContext.getResources().getBoolean(
R.bool.config_allowAllRotations)
? ALLOW_ALL_ROTATIONS_ENABLED
: ALLOW_ALL_ROTATIONS_DISABLED;
}
return mAllowAllRotations;
}
private boolean isLandscapeOrSeascape(int rotation) {
return rotation == mLandscapeRotation || rotation == mSeascapeRotation;
}
@@ -1349,6 +1354,11 @@ public class DisplayRotation {
case ActivityInfo.SCREEN_ORIENTATION_USER:
case ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED:
// When all rotations enabled it works with any of the 4 rotations
if (getAllowAllRotations() == ALLOW_ALL_ROTATIONS_ENABLED) {
return preferredRotation >= 0;
}
// Works with any rotation except upside down.
return (preferredRotation >= 0) && (preferredRotation != Surface.ROTATION_180);
}

View File

@@ -40,7 +40,9 @@ import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.clearInvocations;
import android.app.WindowConfiguration;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
@@ -514,6 +516,69 @@ public class DisplayRotationTests {
verify(mMockStatusBarManagerInternal).onProposedRotationChanged(Surface.ROTATION_90, true);
}
@Test
public void testAllowAllRotations_allowsUpsideDownSuggestion()
throws Exception {
mBuilder.build();
mTarget.updateOrientation(SCREEN_ORIENTATION_UNSPECIFIED, true);
configureDisplayRotation(SCREEN_ORIENTATION_LANDSCAPE, false, false);
when(mMockRes.getBoolean(com.android.internal.R.bool.config_allowAllRotations))
.thenReturn(true);
freezeRotation(Surface.ROTATION_0);
enableOrientationSensor();
mOrientationSensorListener.onSensorChanged(createSensorEvent(Surface.ROTATION_180));
assertTrue(waitForUiHandler());
verify(mMockStatusBarManagerInternal)
.onProposedRotationChanged(Surface.ROTATION_180, true);
}
@Test
public void testDoNotAllowAllRotations_doesNotAllowUpsideDownSuggestion()
throws Exception {
mBuilder.build();
mTarget.updateOrientation(SCREEN_ORIENTATION_UNSPECIFIED, true);
configureDisplayRotation(SCREEN_ORIENTATION_LANDSCAPE, false, false);
when(mMockRes.getBoolean(com.android.internal.R.bool.config_allowAllRotations))
.thenReturn(false);
freezeRotation(Surface.ROTATION_0);
enableOrientationSensor();
mOrientationSensorListener.onSensorChanged(createSensorEvent(Surface.ROTATION_180));
assertTrue(waitForUiHandler());
verify(mMockStatusBarManagerInternal)
.onProposedRotationChanged(Surface.ROTATION_180, false);
}
@Test
public void testAllowAllRotations_allowAllRotationsBecomesDisabled_forbidsUpsideDownSuggestion()
throws Exception {
mBuilder.build();
mTarget.updateOrientation(SCREEN_ORIENTATION_UNSPECIFIED, true);
configureDisplayRotation(SCREEN_ORIENTATION_LANDSCAPE, false, false);
when(mMockRes.getBoolean(com.android.internal.R.bool.config_allowAllRotations))
.thenReturn(true);
freezeRotation(Surface.ROTATION_0);
enableOrientationSensor();
mOrientationSensorListener.onSensorChanged(createSensorEvent(Surface.ROTATION_0));
assertTrue(waitForUiHandler());
// Change resource to disallow all rotations.
// Reset "allowAllRotations".
mTarget.applyCurrentRotation(Surface.ROTATION_0);
clearInvocations(mMockStatusBarManagerInternal);
when(mMockRes.getBoolean(com.android.internal.R.bool.config_allowAllRotations))
.thenReturn(false);
mTarget.resetAllowAllRotations();
mOrientationSensorListener.onSensorChanged(createSensorEvent(Surface.ROTATION_180));
assertTrue(waitForUiHandler());
verify(mMockStatusBarManagerInternal)
.onProposedRotationChanged(Surface.ROTATION_180, false);
}
@Test
public void testReturnsCompatibleRotation_SensorEnabled_RotationThawed() throws Exception {
mBuilder.build();
@@ -935,6 +1000,8 @@ public class DisplayRotationTests {
.thenReturn(WmDisplayCutout.NO_CUTOUT);
when(mMockDisplayContent.getDefaultTaskDisplayArea())
.thenReturn(mock(TaskDisplayArea.class));
when(mMockDisplayContent.getWindowConfiguration())
.thenReturn(new WindowConfiguration());
mMockDisplayPolicy = mock(DisplayPolicy.class);