Use config size for DisplayMetrics when override doesn't exist
Use the resource configuration to obtain the display size when
calculation the DisplayMetrics to make sure the app compatibility
overrides can be applied. If there are other overrides such as overrides
for multi-windowing mode, the override value will be used.
Bug: 366337720
Test: Check display size in createAppContext
Test: ResourcesManagerTest
Test: AppConfigurationTests
Flag: EXEMPT bugfix
Change-Id: I08dd396fde38778062b02a17b9ee5b7e175856bc
diff --git a/core/java/android/app/ResourcesManager.java b/core/java/android/app/ResourcesManager.java
index 84a4eb4..e043a5d 100644
--- a/core/java/android/app/ResourcesManager.java
+++ b/core/java/android/app/ResourcesManager.java
@@ -431,16 +431,19 @@
}
/**
- * Protected so that tests can override and returns something a fixed value.
+ * public so that tests can access and override
*/
@VisibleForTesting
- protected @NonNull DisplayMetrics getDisplayMetrics(int displayId, DisplayAdjustments da) {
+ public @NonNull DisplayMetrics getDisplayMetrics(int displayId, DisplayAdjustments da) {
final DisplayManagerGlobal displayManagerGlobal = DisplayManagerGlobal.getInstance();
final DisplayMetrics dm = new DisplayMetrics();
final DisplayInfo displayInfo = displayManagerGlobal != null
? displayManagerGlobal.getDisplayInfo(displayId) : null;
if (displayInfo != null) {
- displayInfo.getAppMetrics(dm, da);
+ final Configuration dajConfig = da.getConfiguration();
+ displayInfo.getAppMetrics(dm, da.getCompatibilityInfo(),
+ (mResDisplayId == displayId && Configuration.EMPTY.equals(dajConfig))
+ ? mResConfiguration : dajConfig);
} else {
dm.setToDefaults();
}
diff --git a/core/tests/coretests/src/android/content/res/ResourcesManagerTest.java b/core/tests/coretests/src/android/content/res/ResourcesManagerTest.java
index 3eefe04..b16c237 100644
--- a/core/tests/coretests/src/android/content/res/ResourcesManagerTest.java
+++ b/core/tests/coretests/src/android/content/res/ResourcesManagerTest.java
@@ -119,7 +119,7 @@
}
@Override
- protected DisplayMetrics getDisplayMetrics(int displayId, DisplayAdjustments daj) {
+ public DisplayMetrics getDisplayMetrics(int displayId, DisplayAdjustments daj) {
return mDisplayMetricsMap.get(displayId);
}
};
@@ -470,6 +470,48 @@
@Test
@SmallTest
+ public void testResourceConfigurationAppliedWhenOverrideDoesNotExist() {
+ final int width = 240;
+ final int height = 360;
+ final float densityDpi = mDisplayMetricsMap.get(Display.DEFAULT_DISPLAY).densityDpi;
+ final int widthDp = (int) (width / densityDpi + 0.5f);
+ final int heightDp = (int) (height / densityDpi + 0.5f);
+
+ final int overrideWidth = 480;
+ final int overrideHeight = 720;
+ final int overrideWidthDp = (int) (overrideWidth / densityDpi + 0.5f);
+ final int overrideHeightDp = (int) (height / densityDpi + 0.5f);
+
+ // The method to be tested is overridden for other tests to provide a setup environment.
+ // Create a new one for this test only.
+ final ResourcesManager resourcesManager = new ResourcesManager();
+
+ Configuration newConfig = new Configuration();
+ newConfig.windowConfiguration.setAppBounds(0, 0, width, height);
+ newConfig.screenWidthDp = widthDp;
+ newConfig.screenHeightDp = heightDp;
+ resourcesManager.applyConfigurationToResources(newConfig, null);
+
+ assertEquals(width, resourcesManager.getDisplayMetrics(Display.DEFAULT_DISPLAY,
+ DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS).widthPixels);
+ assertEquals(height, resourcesManager.getDisplayMetrics(Display.DEFAULT_DISPLAY,
+ DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS).heightPixels);
+
+ Configuration overrideConfig = new Configuration();
+ overrideConfig.windowConfiguration.setAppBounds(0, 0, overrideWidth, overrideHeight);
+ overrideConfig.screenWidthDp = overrideWidthDp;
+ overrideConfig.screenHeightDp = overrideHeightDp;
+
+ final DisplayAdjustments daj = new DisplayAdjustments(overrideConfig);
+
+ assertEquals(overrideWidth, resourcesManager.getDisplayMetrics(
+ Display.DEFAULT_DISPLAY, daj).widthPixels);
+ assertEquals(overrideHeight, resourcesManager.getDisplayMetrics(
+ Display.DEFAULT_DISPLAY, daj).heightPixels);
+ }
+
+ @Test
+ @SmallTest
@RequiresFlagsEnabled(Flags.FLAG_REGISTER_RESOURCE_PATHS)
@DisabledOnRavenwood(blockedBy = PackageManager.class)
public void testNewResourcesWithOutdatedImplAfterResourcePathsRegistration()