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

Commit 1c3f13b5 authored by Amit Mahajan's avatar Amit Mahajan
Browse files

Avoid using Phone object in SmsNumberUtils.

Test: atest SmsNumberUtilsTest
Bug: 140763963
Change-Id: I30f7f607961a45d81e00ade1394742ced5245583
parent fa7d4b35
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -1315,8 +1315,7 @@ public class IccSmsInterfaceManager {

    @UnsupportedAppUsage
    private String filterDestAddress(String destAddr) {
        String result  = null;
        result = SmsNumberUtils.filterDestAddr(mPhone, destAddr);
        String result = SmsNumberUtils.filterDestAddr(mContext, mPhone.getSubId(), destAddr);
        return result != null ? result : destAddr;
    }

+26 −23
Original line number Diff line number Diff line
@@ -112,7 +112,8 @@ public class SmsNumberUtils {
        }
    }

    /* Breaks the given number down and formats it according to the rules
    /**
     * Breaks the given number down and formats it according to the rules
     * for different number plans and different network.
     *
     * @param number destination number which need to be format
@@ -231,7 +232,8 @@ public class SmsNumberUtils {
        return returnNumber;
    }

    /* Query International direct dialing from HbpcdLookup.db
    /**
     * Query International direct dialing from HbpcdLookup.db
     * for specified country code
     *
     * @param mcc current network's country code
@@ -284,7 +286,8 @@ public class SmsNumberUtils {
    }


    /* Verify if the the destination number is a NANP number
    /**
     * Verify if the the destination number is a NANP number
     *
     * @param numberEntry including number and IDD array
     * @param allIDDs the IDD array list of the current network's country code
@@ -367,7 +370,8 @@ public class SmsNumberUtils {
        return false;
    }

    /* Verify if the the destination number is an internal number
    /**
     * Verify if the the destination number is an internal number
     *
     * @param numberEntry including number and IDD array
     * @param allIDDs the IDD array list of the current network's country code
@@ -530,7 +534,7 @@ public class SmsNumberUtils {
    /**
     * Filter the destination number if using VZW sim card.
     */
    public static String filterDestAddr(Phone phone, String destAddr) {
    public static String filterDestAddr(Context context, int subId, String destAddr) {
        if (DBG) Rlog.d(TAG, "enter filterDestAddr. destAddr=\"" + Rlog.pii(TAG, destAddr) + "\"" );

        if (destAddr == null || !PhoneNumberUtils.isGlobalPhoneNumber(destAddr)) {
@@ -539,16 +543,17 @@ public class SmsNumberUtils {
            return destAddr;
        }

        final String networkOperator = TelephonyManager.from(phone.getContext()).
                getNetworkOperator(phone.getSubId());
        final TelephonyManager telephonyManager = ((TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE)).createForSubscriptionId(subId);
        final String networkOperator = telephonyManager.getNetworkOperator();
        String result = null;

        if (needToConvert(phone)) {
            final int networkType = getNetworkType(phone);
        if (needToConvert(context, subId)) {
            final int networkType = getNetworkType(telephonyManager);
            if (networkType != -1 && !TextUtils.isEmpty(networkOperator)) {
                String networkMcc = networkOperator.substring(0, 3);
                if (networkMcc != null && networkMcc.trim().length() > 0) {
                    result = formatNumber(phone.getContext(), destAddr, networkMcc, networkType);
                    result = formatNumber(context, destAddr, networkMcc, networkType);
                }
            }
        }
@@ -564,14 +569,14 @@ public class SmsNumberUtils {
    /**
     * Returns the current network type
     */
    private static int getNetworkType(Phone phone) {
    private static int getNetworkType(TelephonyManager telephonyManager) {
        int networkType = -1;
        int phoneType = phone.getPhoneType();
        int phoneType = telephonyManager.getPhoneType();

        if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
            networkType = GSM_UMTS_NETWORK;
        } else if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
            if (isInternationalRoaming(phone)) {
            if (isInternationalRoaming(telephonyManager)) {
                networkType = CDMA_ROAMING_NETWORK;
            } else {
                networkType = CDMA_HOME_NETWORK;
@@ -583,11 +588,9 @@ public class SmsNumberUtils {
        return networkType;
    }

    private static boolean isInternationalRoaming(Phone phone) {
        String operatorIsoCountry = TelephonyManager.from(phone.getContext()).
                getNetworkCountryIsoForPhone(phone.getPhoneId());
        String simIsoCountry = TelephonyManager.from(phone.getContext()).getSimCountryIsoForPhone(
                phone.getPhoneId());
    private static boolean isInternationalRoaming(TelephonyManager telephonyManager) {
        String operatorIsoCountry = telephonyManager.getNetworkCountryIso();
        String simIsoCountry = telephonyManager.getSimCountryIso();
        boolean internationalRoaming = !TextUtils.isEmpty(operatorIsoCountry)
                && !TextUtils.isEmpty(simIsoCountry)
                && !simIsoCountry.equals(operatorIsoCountry);
@@ -601,15 +604,15 @@ public class SmsNumberUtils {
        return internationalRoaming;
    }

    private static boolean needToConvert(Phone phone) {
    private static boolean needToConvert(Context context, int subId) {
        // Calling package may not have READ_PHONE_STATE which is required for getConfig().
        // Clear the calling identity so that it is called as self.
        final long identity = Binder.clearCallingIdentity();
        try {
            CarrierConfigManager configManager = (CarrierConfigManager)
                    phone.getContext().getSystemService(Context.CARRIER_CONFIG_SERVICE);
                    context.getSystemService(Context.CARRIER_CONFIG_SERVICE);
            if (configManager != null) {
                PersistableBundle bundle = configManager.getConfigForSubId(phone.getSubId());
                PersistableBundle bundle = configManager.getConfigForSubId(subId);
                if (bundle != null) {
                    return bundle.getBoolean(CarrierConfigManager
                            .KEY_SMS_REQUIRES_DESTINATION_NUMBER_CONVERSION_BOOL);
+37 −25
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.internal.telephony;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doReturn;

import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
@@ -31,10 +34,6 @@ import org.junit.Test;

import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.doReturn;

public class SmsNumberUtilsTest extends TelephonyTest {

    private static final String TMO_MCC_MNC = "310260";
@@ -119,7 +118,7 @@ public class SmsNumberUtilsTest extends TelephonyTest {

        mHbpcdContentProvider = new HbpcdContentProvider();

        doReturn(TMO_MCC_MNC).when(mTelephonyManager).getNetworkOperator(anyInt());
        doReturn(TMO_MCC_MNC).when(mTelephonyManager).getNetworkOperator();

        ((MockContentResolver) mContextFixture.getTestDouble().getContentResolver())
                .addProvider(HbpcdLookup.MccIdd.CONTENT_URI.getAuthority(), mHbpcdContentProvider);
@@ -138,103 +137,116 @@ public class SmsNumberUtilsTest extends TelephonyTest {
    @Test
    @SmallTest
    public void testInvalidNumberConversion() {
        assertEquals("123", SmsNumberUtils.filterDestAddr(mPhone, "123"));
        assertEquals("123", SmsNumberUtils.filterDestAddr(mContext, mPhone.getSubId(), "123"));
    }

    @Test
    @SmallTest
    public void testNaPcCountryCodeAreaLocalNumberConversion() {
        // NP_NANP_NBPCD_CC_AREA_LOCAL tests
        doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(mPhone).getPhoneType();
        assertEquals("18583420022", SmsNumberUtils.filterDestAddr(mPhone, "+1-858-342-0022"));
        doReturn(TelephonyManager.PHONE_TYPE_CDMA).when(mTelephonyManager).getPhoneType();
        assertEquals("18583420022", SmsNumberUtils.filterDestAddr(mContext, mPhone.getSubId(),
                "+1-858-342-0022"));
    }

    @Test
    @SmallTest
    public void testPcCountryCodeAreaLocalNumberConversion() {
        // NP_NBPCD_CC_AREA_LOCAL tests
        assertEquals("01188671234567", SmsNumberUtils.filterDestAddr(mPhone, "+886-7-1234567"));
        assertEquals("01188671234567", SmsNumberUtils.filterDestAddr(mContext, mPhone.getSubId(),
                "+886-7-1234567"));
    }

    @Test
    @SmallTest
    public void testIndiaPcCountryCodeAreaLocalNumberConversion() {
        // NP_NBPCD_CC_AREA_LOCAL tests
        doReturn(INDIA_AIRTEL_MCC_MNC).when(mTelephonyManager).getNetworkOperator(anyInt());
        assertEquals("0119172345678", SmsNumberUtils.filterDestAddr(mPhone, "+91-7-234-5678"));
        doReturn(INDIA_AIRTEL_MCC_MNC).when(mTelephonyManager).getNetworkOperator();
        assertEquals("0119172345678", SmsNumberUtils.filterDestAddr(mContext, mPhone.getSubId(),
                "+91-7-234-5678"));
    }

    @Test
    @SmallTest
    public void testPcHomeIddCountryCodeAreaLocalNumberConversion() {
        // NP_NBPCD_HOMEIDD_CC_AREA_LOCAL tests
        assertEquals("01188671234567", SmsNumberUtils.filterDestAddr(mPhone, "+011886-7-1234567"));
        assertEquals("01188671234567", SmsNumberUtils.filterDestAddr(mContext, mPhone.getSubId(),
                "+011886-7-1234567"));
    }

    @Test
    @SmallTest
    public void testHomeIddCountryCodeAreaLocalNumberConversion() {
        // NP_HOMEIDD_CC_AREA_LOCAL tests
        assertEquals("01188671234567", SmsNumberUtils.filterDestAddr(mPhone, "011886-7-1234567"));
        assertEquals("01188671234567", SmsNumberUtils.filterDestAddr(mContext, mPhone.getSubId(),
                "011886-7-1234567"));
    }

    @Test
    @SmallTest
    public void testLocalIddCountryCodeAreaLocalNumberConversion() {
        // NP_LOCALIDD_CC_AREA_LOCAL tests
        doReturn(TAIWAN_FET_MCC_MNC).when(mTelephonyManager).getNetworkOperator(anyInt());
        assertEquals("01118581234567", SmsNumberUtils.filterDestAddr(mPhone, "002-1-858-1234567"));
        doReturn(TAIWAN_FET_MCC_MNC).when(mTelephonyManager).getNetworkOperator();
        assertEquals("01118581234567", SmsNumberUtils.filterDestAddr(mContext, mPhone.getSubId(),
                "002-1-858-1234567"));
    }

    @Test
    @SmallTest
    public void testIndiaLocalIddCountryCodeAreaLocalNumberConversion() {
        // NP_LOCALIDD_CC_AREA_LOCAL tests
        doReturn(INDIA_AIRTEL_MCC_MNC).when(mTelephonyManager).getNetworkOperator(anyInt());
        assertEquals("01118581234567", SmsNumberUtils.filterDestAddr(mPhone, "010-1-858-1234567"));
        doReturn(INDIA_AIRTEL_MCC_MNC).when(mTelephonyManager).getNetworkOperator();
        assertEquals("01118581234567", SmsNumberUtils.filterDestAddr(mContext, mPhone.getSubId(),
                "010-1-858-1234567"));
    }

    @Test
    @SmallTest
    public void testJapanLocalIddCountryCodeAreaLocalNumberConversion() {
        // NP_LOCALIDD_CC_AREA_LOCAL tests
        doReturn(JAPAN_NTTDOCOMO_MCC_MNC).when(mTelephonyManager).getNetworkOperator(anyInt());
        assertEquals("01118581234567", SmsNumberUtils.filterDestAddr(mPhone, "010-1-858-1234567"));
        doReturn(JAPAN_NTTDOCOMO_MCC_MNC).when(mTelephonyManager).getNetworkOperator();
        assertEquals("01118581234567", SmsNumberUtils.filterDestAddr(mContext, mPhone.getSubId(),
                "010-1-858-1234567"));
    }

    @Test
    @SmallTest
    public void testCountryCodeAreaLocalNumberConversion() {
        // NP_CC_AREA_LOCAL tests
        assertEquals("011886286281234", SmsNumberUtils.filterDestAddr(mPhone, "886-2-86281234"));
        assertEquals("011886286281234", SmsNumberUtils.filterDestAddr(mContext, mPhone.getSubId(),
                "886-2-86281234"));
    }

    @Test
    @SmallTest
    public void testNaLocalNumberConversion() {
        // NP_NANP_LOCAL
        assertEquals("2345678", SmsNumberUtils.filterDestAddr(mPhone, "234-5678"));
        assertEquals("2345678", SmsNumberUtils.filterDestAddr(mContext, mPhone.getSubId(),
                "234-5678"));
    }

    @Test
    @SmallTest
    public void testNaAreaLocalNumberConversion() {
        // NP_NANP_AREA_LOCAL
        assertEquals("8582345678", SmsNumberUtils.filterDestAddr(mPhone, "858-234-5678"));
        assertEquals("8582345678", SmsNumberUtils.filterDestAddr(mContext, mPhone.getSubId(),
                "858-234-5678"));
    }

    @Test
    @SmallTest
    public void testNaNddAreaLocalNumberConversion() {
        // NP_NANP_NDD_AREA_LOCAL
        assertEquals("18582345678", SmsNumberUtils.filterDestAddr(mPhone, "1-858-234-5678"));
        assertEquals("18582345678", SmsNumberUtils.filterDestAddr(mContext, mPhone.getSubId(),
                "1-858-234-5678"));
    }

    @Test
    @SmallTest
    public void testNaLocalIddCcAreaLocalNumberConversion() {
        // NP_NANP_LOCALIDD_CC_AREA_LOCAL
        assertEquals("+18582345678", SmsNumberUtils.filterDestAddr(mPhone, "011-1-858-234-5678"));
        assertEquals("+18582345678", SmsNumberUtils.filterDestAddr(mContext, mPhone.getSubId(),
                "011-1-858-234-5678"));
    }

    @Test
@@ -242,6 +254,6 @@ public class SmsNumberUtilsTest extends TelephonyTest {
    public void testNaPcHomeIddCcAreaLocalNumberConversion() {
        // NP_NANP_NBPCD_HOMEIDD_CC_AREA_LOCAL
        assertEquals("01118582345678",
                SmsNumberUtils.filterDestAddr(mPhone, "+011-1-858-234-5678"));
                SmsNumberUtils.filterDestAddr(mContext, mPhone.getSubId(), "+011-1-858-234-5678"));
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -544,6 +544,7 @@ public abstract class TelephonyTest {
                nullable(Intent[].class), nullable(String[].class), anyInt(),
                nullable(Bundle.class), anyInt());
        doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(anyInt());
        doReturn(TelephonyManager.PHONE_TYPE_GSM).when(mTelephonyManager).getPhoneType();
        doReturn(mServiceState).when(mSST).getServiceState();
        mSST.mSS = mServiceState;
        mSST.mRestrictedState = mRestrictedState;