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

Commit 810bc56a authored by Mårten Kongstad's avatar Mårten Kongstad
Browse files

Build: add hidden method to parse a major.minor String to int

Add a method to parse a string representation of a major.minor SDK
version (e.g. "43.21") into an int, encoded in the same way as
SDK_INT_FULL. This will be used by the package parser when reading
manifest values that include minor versions.

Bug: 377689343
Test: atest 'FrameworksCoreTests:android.os.BuildTest'
Flag: EXEMPT unused flag, call sites will be flagged when added
Change-Id: Ice31da5bbb6de5d2a8938c606c56738c8943f440
parent 80bfccd5
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
@@ -1545,6 +1545,38 @@ public class Build {
        return sdkIntFull % VERSION_CODES_FULL.SDK_INT_MULTIPLIER;
    }

    /**
     * Convert a major.minor version String like "36.1" to an int that
     * represents both major and minor version.
     *
     * @param version the String to parse
     * @return an int encoding the major and minor version
     * @throws IllegalArgumentException if the string could not be converted into an int
     *
     * @hide
     */
    @SuppressWarnings("FlaggedApi") // SDK_INT_MULTIPLIER is defined in this file
    public static @SdkIntFull int parseFullVersion(@NonNull String version) {
        int index = version.indexOf('.');
        int major;
        int minor = 0;
        try {
            if (index == -1) {
                major = Integer.parseInt(version);
            } else {
                major = Integer.parseInt(version.substring(0, index));
                minor = Integer.parseInt(version.substring(index + 1));
            }
            if (major < 0 || minor < 0) {
                throw new NumberFormatException();
            }
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("failed to parse '" + version
                    + "' as a major.minor version code");
        }
        return major * VERSION_CODES_FULL.SDK_INT_MULTIPLIER + minor;
    }

    /**
     * The vendor API for 2024 Q2
     *
+73 −0
Original line number Diff line number Diff line
@@ -16,8 +16,10 @@

package android.os;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import android.platform.test.flag.junit.SetFlagsRule;
@@ -103,4 +105,75 @@ public class BuildTest {
        mSetFlagsRule.disableFlags(Flags.FLAG_ANDROID_OS_BUILD_VANILLA_ICE_CREAM);
        assertFalse(Flags.androidOsBuildVanillaIceCream());
    }

    @Test
    public void testParseFullVersionCorrectInputMajorDotMinor() throws Exception {
        int version = Build.parseFullVersion("12.34");
        assertEquals(12, Build.getMajorSdkVersion(version));
        assertEquals(34, Build.getMinorSdkVersion(version));
    }

    @Test
    public void testParseFullVersionCorrectInputOmitDotMinor() throws Exception {
        int version = Build.parseFullVersion("1234");
        assertEquals(1234, Build.getMajorSdkVersion(version));
        assertEquals(0, Build.getMinorSdkVersion(version));
    }

    @Test
    public void testParseFullVersionCorrectInputCurDevelopment() throws Exception {
        int version = Build.parseFullVersion(Integer.toString(Build.VERSION_CODES.CUR_DEVELOPMENT));
        assertEquals(Build.VERSION_CODES.CUR_DEVELOPMENT, Build.getMajorSdkVersion(version));
        assertEquals(0, Build.getMinorSdkVersion(version));
    }

    @Test
    public void testParseFullVersionIncorrectInputEmptyString() throws Exception {
        assertThrows(IllegalArgumentException.class, () -> {
            Build.parseFullVersion("");
        });
    }

    @Test
    public void testParseFullVersionIncorrectInputNoNumbersInString() throws Exception {
        assertThrows(IllegalArgumentException.class, () -> {
            Build.parseFullVersion("foobar");
        });
    }

    @Test
    public void testParseFullVersionIncorrectInputUnexpectedPatchVersion() throws Exception {
        assertThrows(IllegalArgumentException.class, () -> {
            Build.parseFullVersion("1.2.3");
        });
    }

    @Test
    public void testParseFullVersionIncorrectInputLeadingDotMissingMajorVersion() throws Exception {
        assertThrows(IllegalArgumentException.class, () -> {
            Build.parseFullVersion(".1234");
        });
    }

    @Test
    public void testParseFullVersionIncorrectInputTrailingDotMissingMinorVersion()
            throws Exception {
        assertThrows(IllegalArgumentException.class, () -> {
            Build.parseFullVersion("1234.");
        });
    }

    @Test
    public void testParseFullVersionIncorrectInputNegativeMajorVersion() throws Exception {
        assertThrows(IllegalArgumentException.class, () -> {
            Build.parseFullVersion("-12.34");
        });
    }

    @Test
    public void testParseFullVersionIncorrectInputNegativeMinorVersion() throws Exception {
        assertThrows(IllegalArgumentException.class, () -> {
            Build.parseFullVersion("12.-34");
        });
    }
}