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

Commit dc403705 authored by Marc Kassis's avatar Marc Kassis
Browse files

Return copies of private array members in Display.java to protect them from...

Return copies of private array members in Display.java to protect them from being modified from the outside.

Bug: 278237145
Test: atest DisplayTest.java
Change-Id: I21c670d45c9282c555c98924bf55f5147c5c7ba7
parent a6b13ba0
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -2188,7 +2188,7 @@ public final class Display {
         */
        @NonNull
        public float[] getAlternativeRefreshRates() {
            return mAlternativeRefreshRates;
            return Arrays.copyOf(mAlternativeRefreshRates, mAlternativeRefreshRates.length);
        }

        /**
@@ -2197,7 +2197,7 @@ public final class Display {
        @NonNull
        @HdrCapabilities.HdrType
        public int[] getSupportedHdrTypes() {
            return mSupportedHdrTypes;
            return Arrays.copyOf(mSupportedHdrTypes, mSupportedHdrTypes.length);
        }

        /**
@@ -2497,8 +2497,10 @@ public final class Display {
         * @deprecated use {@link Display#getMode()}
         * and {@link Mode#getSupportedHdrTypes()} instead
         */
        public @HdrType int[] getSupportedHdrTypes() {
            return mSupportedHdrTypes;
        @Deprecated
        @HdrType
        public int[] getSupportedHdrTypes() {
            return Arrays.copyOf(mSupportedHdrTypes, mSupportedHdrTypes.length);
        }
        /**
         * Returns the desired content max luminance data in cd/m2 for this display.
+30 −0
Original line number Diff line number Diff line
@@ -460,6 +460,36 @@ public class DisplayTest {
        assertArrayEquals(sortedHdrTypes, displayMode.getSupportedHdrTypes());
    }

    @Test
    public void testGetSupportedHdrTypesReturnsCopy() {
        int[] hdrTypes = new int[]{1, 2, 3};
        Display.Mode displayMode = new Display.Mode(0, 0, 0, 0, new float[0], hdrTypes);

        int[] hdrTypesCopy = displayMode.getSupportedHdrTypes();
        hdrTypesCopy[0] = 0;
        assertArrayEquals(hdrTypes, displayMode.getSupportedHdrTypes());
    }

    @Test
    public void testGetAlternativeRefreshRatesReturnsCopy() {
        float[] alternativeRates = new float[]{1.0f, 2.0f};
        Display.Mode displayMode = new Display.Mode(0, 0, 0, 0, alternativeRates, new int[0]);

        float[] alternativeRatesCopy = displayMode.getAlternativeRefreshRates();
        alternativeRatesCopy[0] = 0.0f;
        assertArrayEquals(alternativeRates, displayMode.getAlternativeRefreshRates(), 0.0f);
    }

    @Test
    public void testHdrCapabilitiesGetSupportedHdrTypesReturnsCopy() {
        int[] hdrTypes = new int[]{1, 2, 3};
        Display.HdrCapabilities hdrCapabilities = new Display.HdrCapabilities(hdrTypes, 0, 0, 0);

        int[] hdrTypesCopy = hdrCapabilities.getSupportedHdrTypes();
        hdrTypesCopy[0] = 0;
        assertArrayEquals(hdrTypes, hdrCapabilities.getSupportedHdrTypes());
    }

    // Given rotated display dimensions, calculate the letterboxed app bounds.
    private static Rect buildAppBounds(int displayWidth, int displayHeight) {
        final int midWidth = displayWidth / 2;