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

Commit 611af796 authored by Thomas Nguyen's avatar Thomas Nguyen Committed by Android (Google) Code Review
Browse files

Merge "Add util methods to validate satellite config data" into 24D1-dev

parents 1069e00f 42871a37
Loading
Loading
Loading
Loading
+23 −6
Original line number Diff line number Diff line
@@ -349,21 +349,38 @@ public final class TelephonyUtils {
    }

    /**
     * @param plmn target plmn for validation.
     * @return {@code true} if the target plmn is valid {@code false} otherwise.
     * @param input string that want to be compared.
     * @param regex string that express regular expression
     * @return {@code true} if matched  {@code false} otherwise.
     */
    public static boolean isValidPlmn(@Nullable String plmn) {
        if (TextUtils.isEmpty(plmn)) {
    private static boolean isValidPattern(@Nullable String input, @Nullable String regex) {
        if (TextUtils.isEmpty(input) || TextUtils.isEmpty(regex)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^(?:[0-9]{3})(?:[0-9]{2}|[0-9]{3})$");
        Matcher matcher = pattern.matcher(plmn);
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        if (!matcher.matches()) {
            return false;
        }
        return true;
    }

    /**
     * @param countryCode two letters country code based on the ISO 3166-1.
     * @return {@code true} if the countryCode is valid {@code false} otherwise.
     */
    public static boolean isValidCountryCode(@Nullable String countryCode) {
        return isValidPattern(countryCode, "^[A-Za-z]{2}$");
    }

    /**
     * @param plmn target plmn for validation.
     * @return {@code true} if the target plmn is valid {@code false} otherwise.
     */
    public static boolean isValidPlmn(@Nullable String plmn) {
        return isValidPattern(plmn, "^(?:[0-9]{3})(?:[0-9]{2}|[0-9]{3})$");
    }

    /**
     * @param serviceType target serviceType for validation.
     * @return {@code true} if the target serviceType is valid {@code false} otherwise.
+15 −0
Original line number Diff line number Diff line
@@ -85,6 +85,8 @@ public class TelephonyUtilsTest {
        assertTrue(TelephonyUtils.isValidPlmn("45006"));
        assertFalse(TelephonyUtils.isValidPlmn("1234567"));
        assertFalse(TelephonyUtils.isValidPlmn("1234"));
        assertFalse(TelephonyUtils.isValidPlmn(""));
        assertFalse(TelephonyUtils.isValidPlmn(null));
    }

    @Test
@@ -94,6 +96,19 @@ public class TelephonyUtilsTest {
        assertFalse(TelephonyUtils.isValidService(FIRST_SERVICE_TYPE - 1));
        assertFalse(TelephonyUtils.isValidService(LAST_SERVICE_TYPE + 1));
    }

    @Test
    public void testIsValidCountryCode() {
        assertTrue(TelephonyUtils.isValidCountryCode("US"));
        assertTrue(TelephonyUtils.isValidCountryCode("cn"));
        assertFalse(TelephonyUtils.isValidCountryCode("11"));
        assertFalse(TelephonyUtils.isValidCountryCode("USA"));
        assertFalse(TelephonyUtils.isValidCountryCode("chn"));
        assertFalse(TelephonyUtils.isValidCountryCode("U"));
        assertFalse(TelephonyUtils.isValidCountryCode("G7"));
        assertFalse(TelephonyUtils.isValidCountryCode(""));
        assertFalse(TelephonyUtils.isValidCountryCode(null));
    }
}