Loading telephony/common/com/android/internal/telephony/util/TelephonyUtils.java +32 −0 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package com.android.internal.telephony.util; import static android.telephony.Annotation.DataState; import static android.telephony.NetworkRegistrationInfo.FIRST_SERVICE_TYPE; import static android.telephony.NetworkRegistrationInfo.LAST_SERVICE_TYPE; import android.annotation.NonNull; import android.annotation.Nullable; Loading @@ -37,6 +39,7 @@ import android.provider.Telephony.Carriers.EditStatus; import android.telephony.SubscriptionManager; import android.telephony.TelephonyFrameworkInitializer; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Log; import com.android.internal.telephony.ITelephony; Loading @@ -48,6 +51,8 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * This class provides various util functions Loading Loading @@ -342,4 +347,31 @@ public final class TelephonyUtils { return false; } /** * @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) { if (TextUtils.isEmpty(plmn)) { return false; } Pattern pattern = Pattern.compile("^(?:[0-9]{3})(?:[0-9]{2}|[0-9]{3})$"); Matcher matcher = pattern.matcher(plmn); if (!matcher.matches()) { return false; } return true; } /** * @param serviceType target serviceType for validation. * @return {@code true} if the target serviceType is valid {@code false} otherwise. */ public static boolean isValidService(int serviceType) { if (serviceType < FIRST_SERVICE_TYPE || serviceType > LAST_SERVICE_TYPE) { return false; } return true; } } tests/TelephonyCommonTests/src/com/android/internal/telephony/tests/TelephonyUtilsTest.java +23 −1 Original line number Diff line number Diff line Loading @@ -16,10 +16,16 @@ package com.android.internal.telephony.tests; import static android.telephony.NetworkRegistrationInfo.FIRST_SERVICE_TYPE; import static android.telephony.NetworkRegistrationInfo.LAST_SERVICE_TYPE; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.times; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import android.content.Context; Loading Loading @@ -72,6 +78,22 @@ public class TelephonyUtilsTest { // getSubscriptionUserHandle should be called if subID is active. verify(mSubscriptionManager, times(1)).getSubscriptionUserHandle(eq(activeSubId)); } @Test public void testIsValidPlmn() { assertTrue(TelephonyUtils.isValidPlmn("310260")); assertTrue(TelephonyUtils.isValidPlmn("45006")); assertFalse(TelephonyUtils.isValidPlmn("1234567")); assertFalse(TelephonyUtils.isValidPlmn("1234")); } @Test public void testIsValidService() { assertTrue(TelephonyUtils.isValidService(FIRST_SERVICE_TYPE)); assertTrue(TelephonyUtils.isValidService(LAST_SERVICE_TYPE)); assertFalse(TelephonyUtils.isValidService(FIRST_SERVICE_TYPE - 1)); assertFalse(TelephonyUtils.isValidService(LAST_SERVICE_TYPE + 1)); } } Loading
telephony/common/com/android/internal/telephony/util/TelephonyUtils.java +32 −0 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package com.android.internal.telephony.util; import static android.telephony.Annotation.DataState; import static android.telephony.NetworkRegistrationInfo.FIRST_SERVICE_TYPE; import static android.telephony.NetworkRegistrationInfo.LAST_SERVICE_TYPE; import android.annotation.NonNull; import android.annotation.Nullable; Loading @@ -37,6 +39,7 @@ import android.provider.Telephony.Carriers.EditStatus; import android.telephony.SubscriptionManager; import android.telephony.TelephonyFrameworkInitializer; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Log; import com.android.internal.telephony.ITelephony; Loading @@ -48,6 +51,8 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * This class provides various util functions Loading Loading @@ -342,4 +347,31 @@ public final class TelephonyUtils { return false; } /** * @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) { if (TextUtils.isEmpty(plmn)) { return false; } Pattern pattern = Pattern.compile("^(?:[0-9]{3})(?:[0-9]{2}|[0-9]{3})$"); Matcher matcher = pattern.matcher(plmn); if (!matcher.matches()) { return false; } return true; } /** * @param serviceType target serviceType for validation. * @return {@code true} if the target serviceType is valid {@code false} otherwise. */ public static boolean isValidService(int serviceType) { if (serviceType < FIRST_SERVICE_TYPE || serviceType > LAST_SERVICE_TYPE) { return false; } return true; } }
tests/TelephonyCommonTests/src/com/android/internal/telephony/tests/TelephonyUtilsTest.java +23 −1 Original line number Diff line number Diff line Loading @@ -16,10 +16,16 @@ package com.android.internal.telephony.tests; import static android.telephony.NetworkRegistrationInfo.FIRST_SERVICE_TYPE; import static android.telephony.NetworkRegistrationInfo.LAST_SERVICE_TYPE; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.times; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import android.content.Context; Loading Loading @@ -72,6 +78,22 @@ public class TelephonyUtilsTest { // getSubscriptionUserHandle should be called if subID is active. verify(mSubscriptionManager, times(1)).getSubscriptionUserHandle(eq(activeSubId)); } @Test public void testIsValidPlmn() { assertTrue(TelephonyUtils.isValidPlmn("310260")); assertTrue(TelephonyUtils.isValidPlmn("45006")); assertFalse(TelephonyUtils.isValidPlmn("1234567")); assertFalse(TelephonyUtils.isValidPlmn("1234")); } @Test public void testIsValidService() { assertTrue(TelephonyUtils.isValidService(FIRST_SERVICE_TYPE)); assertTrue(TelephonyUtils.isValidService(LAST_SERVICE_TYPE)); assertFalse(TelephonyUtils.isValidService(FIRST_SERVICE_TYPE - 1)); assertFalse(TelephonyUtils.isValidService(LAST_SERVICE_TYPE + 1)); } }