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

Commit 55a6cb5f authored by Mårten Kongstad's avatar Mårten Kongstad
Browse files

Build: add hidden method to convert major.minor int to human readable string

Add a method to convert an int that represents a major.minor SDK version
(e.g. SDK_INT_FULL) into a human readable string (e.g. "43.21"). This is
intended only for debugging and error messages.

Bug: 377689343
Test: atest 'FrameworksCoreTests:android.os.BuildTest'
Flag: EXEMPT unused flag, call sites will be flagged when added
Change-Id: I2df5bfdee6e319b5ba1478032797aee23e941ae6
parent 810bc56a
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -1577,6 +1577,25 @@ public class Build {
        return major * VERSION_CODES_FULL.SDK_INT_MULTIPLIER + minor;
    }

    /**
     * Convert an int representing a major.minor version like SDK_INT_FULL to a
     * human readable string. The returned string is only intended for debug
     * and error messages.
     *
     * @param version the int to convert to a string
     * @return a String representing the same major.minor version as the int passed in
     * @throws IllegalArgumentException if {@code version} is negative
     *
     * @hide
     */
    public static String fullVersionToString(@SdkIntFull int version) {
        if (version < 0) {
            throw new IllegalArgumentException("failed to convert '" + version
                    + "' to string: not a valid major.minor version code");
        }
        return String.format("%d.%d", getMajorSdkVersion(version), getMinorSdkVersion(version));
    }

    /**
     * The vendor API for 2024 Q2
     *
+24 −0
Original line number Diff line number Diff line
@@ -176,4 +176,28 @@ public class BuildTest {
            Build.parseFullVersion("12.-34");
        });
    }

    @Test
    public void testFullVersionToStringCorrectInput() throws Exception {
        assertEquals("0.0", Build.fullVersionToString(0));
        assertEquals("1.0", Build.fullVersionToString(1 * 100000 + 0));
        assertEquals("1.1", Build.fullVersionToString(1 * 100000 + 1));
        assertEquals("12.34", Build.fullVersionToString(12 * 100000 + 34));
    }

    @Test
    public void testFullVersionToStringSameStringAfterRoundTripViaParseFullVersion()
            throws Exception {
        String s = "12.34";
        int major = Build.getMajorSdkVersion(Build.parseFullVersion(s));
        int minor = Build.getMinorSdkVersion(Build.parseFullVersion(s));
        assertEquals(s, Build.fullVersionToString(major * 100000 + minor));
    }

    @Test
    public void testFullVersionToStringIncorrectInputNegativeVersion() throws Exception {
        assertThrows(IllegalArgumentException.class, () -> {
            Build.fullVersionToString(-1);
        });
    }
}