Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2a09810f authored by Yunfan Chen's avatar Yunfan Chen
Browse files

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
parent 4cc911dd
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -431,16 +431,19 @@ public class ResourcesManager {
    }

    /**
     * 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();
        }
+43 −1
Original line number Diff line number Diff line
@@ -119,7 +119,7 @@ public class ResourcesManagerTest {
            }

            @Override
            protected DisplayMetrics getDisplayMetrics(int displayId, DisplayAdjustments daj) {
            public DisplayMetrics getDisplayMetrics(int displayId, DisplayAdjustments daj) {
                return mDisplayMetricsMap.get(displayId);
            }
        };
@@ -468,6 +468,48 @@ public class ResourcesManagerTest {
        ResourcesManager.setInstance(oriResourcesManager);
    }

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