diff --git a/telephony/common/com/android/internal/telephony/util/TelephonyUtils.java b/telephony/common/com/android/internal/telephony/util/TelephonyUtils.java index 7b5b07c0fbf6e4ae8593cefb649a9123fcc0a616..f31a87f2b1bfdcb0d03f6024e33c16e2ae039037 100644 --- a/telephony/common/com/android/internal/telephony/util/TelephonyUtils.java +++ b/telephony/common/com/android/internal/telephony/util/TelephonyUtils.java @@ -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. diff --git a/tests/TelephonyCommonTests/src/com/android/internal/telephony/tests/TelephonyUtilsTest.java b/tests/TelephonyCommonTests/src/com/android/internal/telephony/tests/TelephonyUtilsTest.java index 755833234e02e9c080619e6808b753dad0bd5673..f88d82bf29a8f2b774300becdeae87941abe2497 100644 --- a/tests/TelephonyCommonTests/src/com/android/internal/telephony/tests/TelephonyUtilsTest.java +++ b/tests/TelephonyCommonTests/src/com/android/internal/telephony/tests/TelephonyUtilsTest.java @@ -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)); + } }