[3/n] Pre-select fullscreen option if overridden

Settings > Apps > Aspect ratio (experimental)

Pre-select the fullscreen option if all are true:
- device manufacturer has overridden app to fullscreen
- app has not opted out of orientation override
- app has not opted out of user's fullscreen option
- user has not set any aspect ratio (USER_MIN_ASPECT_RATIO_UNSET)

"App default" will have a new value of USER_MIN_ASPECT_RATIO_APP_DEFAULT
if device manufacturer has overridden app to fullscreen, which will
behave the same as USER_MIN_ASPECT_RATIO_UNSET without the
device-applied fullscreen override.

Bug: 310816437
Test: atest UserAspectRatioDetailsTest
      atest UserAspectRatioManagerTest
      atest UserAspectRatioAppPreferenceTest
Change-Id: I6be634bb4369292687b865ce30d902540419183c
This commit is contained in:
Graciela Wissen Putri
2023-12-15 16:15:03 +00:00
parent 72d638e681
commit 480d523a0c
7 changed files with 372 additions and 96 deletions

View File

@@ -19,15 +19,19 @@ package com.android.settings.applications.appcompat;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_16_9;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_3_2;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_4_3;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_APP_DEFAULT;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_DISPLAY_SIZE;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_FULLSCREEN;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_SPLIT_SCREEN;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_UNSET;
import static android.platform.test.flag.junit.SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT;
import static android.view.WindowManager.PROPERTY_COMPAT_ALLOW_ORIENTATION_OVERRIDE;
import static android.view.WindowManager.PROPERTY_COMPAT_ALLOW_USER_ASPECT_RATIO_FULLSCREEN_OVERRIDE;
import static android.view.WindowManager.PROPERTY_COMPAT_ALLOW_USER_ASPECT_RATIO_OVERRIDE;
import static com.android.settings.applications.appcompat.UserAspectRatioManager.KEY_ENABLE_USER_ASPECT_RATIO_FULLSCREEN;
import static com.android.settings.applications.appcompat.UserAspectRatioManager.KEY_ENABLE_USER_ASPECT_RATIO_SETTINGS;
import static com.android.window.flags.Flags.FLAG_USER_MIN_ASPECT_RATIO_APP_DEFAULT;
import static com.google.common.truth.Truth.assertThat;
@@ -44,12 +48,16 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
import android.content.pm.LauncherActivityInfo;
import android.content.pm.LauncherApps;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.RemoteException;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.DeviceConfig;
import androidx.annotation.NonNull;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
@@ -58,6 +66,7 @@ import com.android.settings.testutils.ResourcesUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -68,30 +77,36 @@ import java.util.List;
*/
@RunWith(AndroidJUnit4.class)
public class UserAspectRatioManagerTest {
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(DEVICE_DEFAULT);
private final String mPackageName = "com.test.mypackage";
private Context mContext;
private Resources mResources;
private UserAspectRatioManager mUtils;
private FakeUserAspectRatioManager mUtils;
private String mOriginalSettingsFlag;
private String mOriginalFullscreenFlag;
private String mPackageName = "com.test.mypackage";
private LauncherApps mLauncherApps;
private IPackageManager mIPm;
private PackageManager mPm;
private List<LauncherActivityInfo> mLauncherActivities;
@Before
public void setUp() {
public void setUp() throws RemoteException, PackageManager.NameNotFoundException {
mContext = spy(ApplicationProvider.getApplicationContext());
mResources = mock(Resources.class);
mLauncherApps = mock(LauncherApps.class);
final LauncherApps launcherApps = mock(LauncherApps.class);
mLauncherActivities = mock(List.class);
mIPm = mock(IPackageManager.class);
mPm = mock(PackageManager.class);
when(mContext.getPackageManager()).thenReturn(mPm);
when(mContext.getResources()).thenReturn(mResources);
when(mContext.getSystemService(LauncherApps.class)).thenReturn(mLauncherApps);
when(mContext.getSystemService(LauncherApps.class)).thenReturn(launcherApps);
enableAllDefaultAspectRatioOptions();
mSetFlagsRule.disableFlags(FLAG_USER_MIN_ASPECT_RATIO_APP_DEFAULT);
mUtils = new UserAspectRatioManager(mContext);
mUtils = new FakeUserAspectRatioManager(mContext, mIPm);
doReturn(mLauncherActivities).when(mLauncherApps).getActivityList(anyString(), any());
doReturn(mLauncherActivities).when(launcherApps).getActivityList(anyString(), any());
mOriginalSettingsFlag = DeviceConfig.getProperty(
DeviceConfig.NAMESPACE_WINDOW_MANAGER, KEY_ENABLE_USER_ASPECT_RATIO_SETTINGS);
@@ -102,6 +117,10 @@ public class UserAspectRatioManagerTest {
DeviceConfig.NAMESPACE_WINDOW_MANAGER, KEY_ENABLE_USER_ASPECT_RATIO_FULLSCREEN);
setAspectRatioFullscreenBuildTimeFlagEnabled(true);
setAspectRatioFullscreenDeviceConfigEnabled("true" /* enabled */, false /* makeDefault */);
mockProperty(PROPERTY_COMPAT_ALLOW_USER_ASPECT_RATIO_FULLSCREEN_OVERRIDE, true);
mockProperty(PROPERTY_COMPAT_ALLOW_ORIENTATION_OVERRIDE, true);
mockProperty(PROPERTY_COMPAT_ALLOW_USER_ASPECT_RATIO_OVERRIDE, true);
}
@After
@@ -114,13 +133,13 @@ public class UserAspectRatioManagerTest {
@Test
public void testCanDisplayAspectRatioUi() {
final ApplicationInfo canDisplay = new ApplicationInfo();
canDisplay.packageName = "com.app.candisplay";
canDisplay.packageName = mPackageName;
doReturn(false).when(mLauncherActivities).isEmpty();
assertTrue(mUtils.canDisplayAspectRatioUi(canDisplay));
final ApplicationInfo noLauncherEntry = new ApplicationInfo();
noLauncherEntry.packageName = "com.app.nolauncherentry";
noLauncherEntry.packageName = mPackageName;
doReturn(true).when(mLauncherActivities).isEmpty();
assertFalse(mUtils.canDisplayAspectRatioUi(noLauncherEntry));
@@ -221,36 +240,40 @@ public class UserAspectRatioManagerTest {
mPackageName));
}
private String getUserMinAspectRatioEntry(int aspectRatio, String packageName) {
return mUtils.getUserMinAspectRatioEntry(aspectRatio, packageName, mContext.getUserId());
}
@Test
public void testGetUserMinAspectRatioEntry() {
final Context context = ApplicationProvider.getApplicationContext();
// R.string.user_aspect_ratio_app_default
final String appDefault = ResourcesUtils.getResourcesString(context,
"user_aspect_ratio_app_default");
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_UNSET, mPackageName))
assertThat(getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_UNSET, mPackageName))
.isEqualTo(appDefault);
// should always return default if value does not correspond to anything
assertThat(mUtils.getUserMinAspectRatioEntry(-1, mPackageName))
assertThat(getUserMinAspectRatioEntry(-1, mPackageName))
.isEqualTo(appDefault);
// R.string.user_aspect_ratio_half_screen
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_SPLIT_SCREEN,
assertThat(getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_SPLIT_SCREEN,
mPackageName)).isEqualTo(ResourcesUtils.getResourcesString(context,
"user_aspect_ratio_half_screen"));
// R.string.user_aspect_ratio_display_size
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_DISPLAY_SIZE,
assertThat(getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_DISPLAY_SIZE,
mPackageName)).isEqualTo(ResourcesUtils.getResourcesString(context,
"user_aspect_ratio_device_size"));
// R.string.user_aspect_ratio_16_9
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_16_9, mPackageName))
assertThat(getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_16_9, mPackageName))
.isEqualTo(ResourcesUtils.getResourcesString(context, "user_aspect_ratio_16_9"));
// R.string.user_aspect_ratio_4_3
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_4_3, mPackageName))
assertThat(getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_4_3, mPackageName))
.isEqualTo(ResourcesUtils.getResourcesString(context, "user_aspect_ratio_4_3"));
// R.string.user_aspect_ratio_3_2
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_3_2, mPackageName))
assertThat(getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_3_2, mPackageName))
.isEqualTo(ResourcesUtils.getResourcesString(context, "user_aspect_ratio_3_2"));
// R.string.user_aspect_ratio_fullscreen
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_FULLSCREEN,
assertThat(getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_FULLSCREEN,
mPackageName)).isEqualTo(ResourcesUtils.getResourcesString(context,
"user_aspect_ratio_fullscreen"));
}
@@ -258,7 +281,7 @@ public class UserAspectRatioManagerTest {
@Test
public void testGetUserMinAspectRatioEntry_fullscreenDisabled_shouldReturnDefault() {
setAspectRatioFullscreenBuildTimeFlagEnabled(false);
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_FULLSCREEN,
assertThat(getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_FULLSCREEN,
mPackageName)).isEqualTo(ResourcesUtils.getResourcesString(
ApplicationProvider.getApplicationContext(),
"user_aspect_ratio_app_default"));
@@ -270,9 +293,9 @@ public class UserAspectRatioManagerTest {
when(mResources.getIntArray(anyInt())).thenReturn(new int[] {USER_MIN_ASPECT_RATIO_UNSET});
when(mResources.getStringArray(anyInt())).thenReturn(new String[] {newOptionName});
mUtils = new UserAspectRatioManager(mContext);
mUtils = new FakeUserAspectRatioManager(mContext, mIPm);
assertThat(mUtils.getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_UNSET, mPackageName))
assertThat(getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_UNSET, mPackageName))
.isEqualTo(newOptionName);
}
@@ -282,7 +305,7 @@ public class UserAspectRatioManagerTest {
when(mResources.getIntArray(anyInt())).thenReturn(new int[] {USER_MIN_ASPECT_RATIO_4_3});
when(mResources.getStringArray(anyInt())).thenReturn(new String[] {"4:3"});
assertThrows(RuntimeException.class, () -> new UserAspectRatioManager(mContext));
assertThrows(RuntimeException.class, () -> new FakeUserAspectRatioManager(mContext, mIPm));
}
@Test
@@ -292,7 +315,88 @@ public class UserAspectRatioManagerTest {
USER_MIN_ASPECT_RATIO_4_3});
when(mResources.getStringArray(anyInt())).thenReturn(new String[] {"4:3"});
assertThrows(RuntimeException.class, () -> new UserAspectRatioManager(mContext));
assertThrows(RuntimeException.class, () -> new FakeUserAspectRatioManager(mContext, mIPm));
}
@Test
public void testGetUserMinAspectRatioMapping_appDefaultFlagEnabled() {
// Flag is disabled by default, app default not loaded
assertFalse(mUtils.hasAspectRatioOption(USER_MIN_ASPECT_RATIO_APP_DEFAULT, mPackageName));
mSetFlagsRule.enableFlags(FLAG_USER_MIN_ASPECT_RATIO_APP_DEFAULT);
mUtils = new FakeUserAspectRatioManager(mContext, mIPm);
assertTrue(mUtils.hasAspectRatioOption(USER_MIN_ASPECT_RATIO_APP_DEFAULT, mPackageName));
assertThat(getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_APP_DEFAULT, mPackageName))
.isEqualTo(getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_UNSET, mPackageName));
}
@Test
public void testGetUserMinAspectRatioEntry_enabledFullscreenOverride_returnsFullscreen() {
setIsOverrideToFullscreenEnabled(true);
// Fullscreen option is pre-selected
assertThat(getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_UNSET, mPackageName))
.isEqualTo(ResourcesUtils.getResourcesString(
ApplicationProvider.getApplicationContext(),
"user_aspect_ratio_fullscreen"));
// App default exists
assertThat(getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_APP_DEFAULT, mPackageName))
.isEqualTo(ResourcesUtils.getResourcesString(
ApplicationProvider.getApplicationContext(),
"user_aspect_ratio_app_default"));
}
@Test
public void testGetUserMinAspectRatioEntry_disabledFullscreenOverride_returnsUnchanged() {
setIsOverrideToFullscreenEnabled(false);
// Fullscreen option is not pre-selected
assertThat(getUserMinAspectRatioEntry(USER_MIN_ASPECT_RATIO_UNSET, mPackageName))
.isEqualTo(ResourcesUtils.getResourcesString(
ApplicationProvider.getApplicationContext(),
"user_aspect_ratio_app_default"));
}
@Test
public void testIsOverrideToFullscreenEnabled_returnsTrue()
throws PackageManager.NameNotFoundException {
setIsOverrideToFullscreenEnabled(true);
assertTrue(mUtils.isOverrideToFullscreenEnabled(mPackageName, mContext.getUserId()));
mockProperty(PROPERTY_COMPAT_ALLOW_ORIENTATION_OVERRIDE, true);
assertTrue(mUtils.isOverrideToFullscreenEnabled(mPackageName, mContext.getUserId()));
}
@Test
public void testIsOverrideToFullscreenEnabled_optOut_returnsFalse()
throws PackageManager.NameNotFoundException {
setIsOverrideToFullscreenEnabled(true);
mockProperty(PROPERTY_COMPAT_ALLOW_ORIENTATION_OVERRIDE, false);
assertFalse(mUtils.isOverrideToFullscreenEnabled(mPackageName, mContext.getUserId()));
}
@Test
public void testIsOverrideToFullscreenEnabled_flagDisabled_returnsFalse() {
mUtils.setFullscreenCompatChange(true);
assertFalse(mUtils.isOverrideToFullscreenEnabled(mPackageName, mContext.getUserId()));
}
@Test
public void testIsOverrideToFullscreenEnabled_optionDisabled_returnsFalse() {
mUtils.setFullscreenCompatChange(true);
when(mUtils.hasAspectRatioOption(USER_MIN_ASPECT_RATIO_FULLSCREEN, mPackageName))
.thenReturn(false);
assertFalse(mUtils.isOverrideToFullscreenEnabled(mPackageName, mContext.getUserId()));
}
private void setIsOverrideToFullscreenEnabled(boolean enabled) {
if (enabled) {
mSetFlagsRule.enableFlags(FLAG_USER_MIN_ASPECT_RATIO_APP_DEFAULT);
mUtils = new FakeUserAspectRatioManager(mContext, mIPm);
}
mUtils.setFullscreenCompatChange(enabled);
when(mUtils.hasAspectRatioOption(USER_MIN_ASPECT_RATIO_FULLSCREEN, mPackageName))
.thenReturn(enabled);
}
private void enableAllDefaultAspectRatioOptions() {
@@ -328,9 +432,7 @@ public class UserAspectRatioManagerTest {
throws PackageManager.NameNotFoundException {
PackageManager.Property prop = new PackageManager.Property(
propertyName, value, mPackageName, "" /* className */);
PackageManager pm = mock(PackageManager.class);
when(mContext.getPackageManager()).thenReturn(pm);
when(pm.getProperty(propertyName, mPackageName)).thenReturn(prop);
when(mPm.getProperty(propertyName, mPackageName)).thenReturn(prop);
}
private void setAspectRatioSettingsBuildTimeFlagEnabled(boolean enabled) {
@@ -352,4 +454,21 @@ public class UserAspectRatioManagerTest {
DeviceConfig.setProperty(DeviceConfig.NAMESPACE_WINDOW_MANAGER,
KEY_ENABLE_USER_ASPECT_RATIO_FULLSCREEN, enabled, makeDefault);
}
private static class FakeUserAspectRatioManager extends UserAspectRatioManager {
private boolean mFullscreenCompatChange = false;
private FakeUserAspectRatioManager(@NonNull Context context, IPackageManager pm) {
super(context, pm);
}
@Override
boolean isFullscreenCompatChangeEnabled(String pkgName, int userId) {
return mFullscreenCompatChange;
}
void setFullscreenCompatChange(boolean enabled) {
mFullscreenCompatChange = enabled;
}
}
}