Add a url for getting the current APN list

There has three steps:
1. Query the APN based on carrier ID and fall back to query { MCC, MNC, MVNO }.
2. If can't find the current APN, then query the parent APN. Query based on
   MNO carrier id and { MCC, MNC }.
3. else return empty cursor

Bug: 115709816
Test: run TelephonyProviderTest
Change-Id: I573e5279ee37e9325880fb1527a698da7e24fb9f
Merged-in: I573e5279ee37e9325880fb1527a698da7e24fb9f
diff --git a/src/com/android/providers/telephony/TelephonyProvider.java b/src/com/android/providers/telephony/TelephonyProvider.java
index b7d4f86..54ef14f 100644
--- a/src/com/android/providers/telephony/TelephonyProvider.java
+++ b/src/com/android/providers/telephony/TelephonyProvider.java
@@ -46,8 +46,8 @@
 import static android.provider.Telephony.Carriers.NO_SET_SET;
 import static android.provider.Telephony.Carriers.NUMERIC;
 import static android.provider.Telephony.Carriers.OWNED_BY;
-import static android.provider.Telephony.Carriers.OWNED_BY_OTHERS;
 import static android.provider.Telephony.Carriers.OWNED_BY_DPC;
+import static android.provider.Telephony.Carriers.OWNED_BY_OTHERS;
 import static android.provider.Telephony.Carriers.PASSWORD;
 import static android.provider.Telephony.Carriers.PORT;
 import static android.provider.Telephony.Carriers.PROFILE_ID;
@@ -132,8 +132,8 @@
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
-import java.util.zip.CRC32;
 import java.util.Set;
+import java.util.zip.CRC32;
 
 public class TelephonyProvider extends ContentProvider
 {
@@ -166,7 +166,8 @@
     private static final int URL_ENFORCE_MANAGED = 20;
     private static final int URL_PREFERAPNSET = 21;
     private static final int URL_PREFERAPNSET_USING_SUBID = 22;
-
+    private static final int URL_SIM_APN_LIST = 23;
+    private static final int URL_SIM_APN_LIST_ID = 24;
 
     private static final String TAG = "TelephonyProvider";
     private static final String CARRIERS_TABLE = "carriers";
@@ -415,6 +416,8 @@
         s_urlMatcher.addURI("telephony", "carriers/filtered/#", URL_FILTERED_ID);
         // Only Called by DevicePolicyManager to enforce DPC records.
         s_urlMatcher.addURI("telephony", "carriers/enforce_managed", URL_ENFORCE_MANAGED);
+        s_urlMatcher.addURI("telephony", "carriers/sim_apn_list", URL_SIM_APN_LIST);
+        s_urlMatcher.addURI("telephony", "carriers/sim_apn_list/#", URL_SIM_APN_LIST_ID);
 
         s_currentNullMap = new ContentValues(1);
         s_currentNullMap.put(CURRENT, "0");
@@ -2616,6 +2619,19 @@
                 qb.setTables(SIMINFO_TABLE);
                 break;
             }
+            case URL_SIM_APN_LIST_ID: {
+                subIdString = url.getLastPathSegment();
+                try {
+                    subId = Integer.parseInt(subIdString);
+                } catch (NumberFormatException e) {
+                    loge("NumberFormatException" + e);
+                    return null;
+                }
+            }
+            //intentional fall through from above case
+            case URL_SIM_APN_LIST: {
+                return getSubscriptionMatchingAPNList(qb, projectionIn, sort, subId);
+            }
 
             default: {
                 return null;
@@ -2674,6 +2690,92 @@
         return ret;
     }
 
+    /**
+     * To find the current sim APN.
+     *
+     * There has three steps:
+     * 1. Query the APN based on carrier ID and fall back to query { MCC, MNC, MVNO }.
+     * 2. If can't find the current APN, then query the parent APN. Query based on
+     *    MNO carrier id and { MCC, MNC }.
+     * 3. else return empty cursor
+     *
+     */
+    private Cursor getSubscriptionMatchingAPNList(SQLiteQueryBuilder qb, String[] projectionIn,
+            String sort, int subId) {
+
+        Cursor ret;
+        final TelephonyManager tm = ((TelephonyManager)
+                getContext().getSystemService(Context.TELEPHONY_SERVICE))
+                .createForSubscriptionId(subId);
+        SQLiteDatabase db = getReadableDatabase();
+
+        // For query db one time, append step 1 and step 2 condition in one selection and
+        // separate results after the query is completed. Because IMSI has special match rule,
+        // so just query the MCC / MNC and filter the MVNO by ourselves
+        String carrierIDSelection = CARRIER_ID + " =? OR " + NUMERIC + " =? OR "
+                + CARRIER_ID + " =? ";
+
+
+        String mccmnc = tm.getSimOperator();
+        String carrierId = String.valueOf(tm.getSimCarrierId());
+        String mnoCarrierId = String.valueOf(tm.getSimMNOCarrierId());
+
+        ret = qb.query(db, null, carrierIDSelection,
+                new String[]{carrierId, mccmnc, mnoCarrierId}, null, null, sort);
+
+        if (DBG) log("match current APN size:  " + ret.getCount());
+
+        MatrixCursor currentCursor = new MatrixCursor(projectionIn);
+        MatrixCursor parentCursor = new MatrixCursor(projectionIn);
+
+        int carrierIdIndex = ret.getColumnIndex(CARRIER_ID);
+        int numericIndex = ret.getColumnIndex(NUMERIC);
+        int mvnoIndex = ret.getColumnIndex(MVNO_TYPE);
+        int mvnoDataIndex = ret.getColumnIndex(MVNO_MATCH_DATA);
+
+        IccRecords iccRecords = getIccRecords(subId);
+
+        //Separate the result into MatrixCursor
+        while (ret.moveToNext()) {
+            List<String> data = new ArrayList<>();
+            for (String column : projectionIn) {
+                data.add(ret.getString(ret.getColumnIndex(column)));
+            }
+
+            if (ret.getString(carrierIdIndex).equals(carrierId)) {
+                // 1. APN query result based on SIM carrier id
+                currentCursor.addRow(data);
+            } else if (!TextUtils.isEmpty(ret.getString(numericIndex)) &&
+                    ApnSettingUtils.mvnoMatches(iccRecords,
+                            ApnSetting.getMvnoTypeIntFromString(ret.getString(mvnoIndex)),
+                            ret.getString(mvnoDataIndex))) {
+                // 1. APN query result based on legacy SIM MCC/MCC and MVNO in case APN carrier id
+                // migration is not 100%. some APNSettings can not find match id.
+                // TODO: remove legacy {mcc,mnc, mvno} support in the future.
+                currentCursor.addRow(data);
+            } else if (ret.getString(carrierIdIndex).equals(mnoCarrierId)) {
+                // 2. APN query result based on SIM MNO carrier id in case no APN found from
+                // exact carrier id fallback to query the MNO carrier id
+                parentCursor.addRow(data);
+            } else if (!TextUtils.isEmpty(ret.getString(numericIndex))) {
+                // 2. APN query result based on SIM MCC/MNC
+                // TODO: remove legacy {mcc, mnc} support in the future.
+                parentCursor.addRow(data);
+            }
+        }
+
+        if (currentCursor.getCount() > 0) {
+            if (DBG) log("match Carrier Id APN: " + currentCursor.getCount());
+            return currentCursor;
+        } else if (parentCursor.getCount() > 0) {
+            if (DBG) log("match MNO Carrier ID APN: " + parentCursor.getCount());
+            return parentCursor;
+        } else {
+            if (DBG) log("APN no match");
+            return new MatrixCursor(projectionIn);
+        }
+    }
+
     @Override
     public String getType(Uri url)
     {
diff --git a/tests/src/com/android/providers/telephony/TelephonyProviderTest.java b/tests/src/com/android/providers/telephony/TelephonyProviderTest.java
index 715f3dd..cf4409a 100644
--- a/tests/src/com/android/providers/telephony/TelephonyProviderTest.java
+++ b/tests/src/com/android/providers/telephony/TelephonyProviderTest.java
@@ -40,7 +40,6 @@
 import android.test.mock.MockContentResolver;
 import android.test.mock.MockContext;
 import android.test.suitebuilder.annotation.SmallTest;
-import android.text.TextUtils;
 import android.util.Log;
 
 import junit.framework.TestCase;
@@ -80,6 +79,7 @@
     private static final String TEST_OPERATOR = "123456";
     private static final String TEST_MCC = "123";
     private static final String TEST_MNC = "456";
+
     // Used to test the path for URL_TELEPHONY_USING_SUBID with subid 1
     private static final Uri CONTENT_URI_WITH_SUBID = Uri.parse(
             "content://telephony/carriers/subId/" + TEST_SUBID);
@@ -92,6 +92,8 @@
             "content://telephony/carriers/preferapn/subId/" + TEST_SUBID);
     private static final Uri URL_WFC_ENABLED_USING_SUBID = Uri.parse(
             "content://telephony/siminfo/" + TEST_SUBID);
+    private static final Uri URL_SIM_APN_LIST = Uri.parse(
+        "content://telephony/carriers/sim_apn_list");
 
     private static final String COLUMN_APN_ID = "apn_id";
 
@@ -1439,4 +1441,167 @@
         assertEquals(1, notifyWfcCount);
         assertEquals(0, notifyWfcCountWithTestSubId);
     }
+
+    @Test
+    @SmallTest
+    public void testGetCurrentAPNList_APNMatchTheCarrierID() {
+        // Test on getCurrentAPNList() step 1
+        TelephonyManager telephonyManager =
+                ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE));
+        doReturn(telephonyManager).when(telephonyManager).createForSubscriptionId(anyInt());
+
+        final String apnName = "apnName";
+        final String carrierName = "name";
+        final int carrierID = 100;
+        final String numeric = TEST_OPERATOR;
+        doReturn(carrierID).when(telephonyManager).getSimCarrierId();
+        doReturn(numeric).when(telephonyManager).getSimOperator();
+
+        // Insert the APN
+        ContentValues contentValues = new ContentValues();
+        contentValues.put(Carriers.APN, apnName);
+        contentValues.put(Carriers.NAME, carrierName);
+        contentValues.put(Carriers.CARRIER_ID, carrierID);
+        mContentResolver.insert(Carriers.CONTENT_URI, contentValues);
+
+        final String[] testProjection =
+            {
+                Carriers.APN,
+                Carriers.NAME,
+                Carriers.CARRIER_ID,
+            };
+        Cursor cursor = mContentResolver.query(URL_SIM_APN_LIST,
+            testProjection, null, null, null);
+
+        cursor.moveToFirst();
+        assertEquals(apnName, cursor.getString(0));
+        assertEquals(carrierName, cursor.getString(1));
+        assertEquals(String.valueOf(carrierID), cursor.getString(2));
+    }
+
+    @Test
+    @SmallTest
+    public void testGetCurrentAPNList_APNMatchTheMCCMNCAndMVNO() {
+        // Test on getCurrentAPNList() step 2
+        TelephonyManager telephonyManager =
+                ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE));
+        doReturn(telephonyManager).when(telephonyManager).createForSubscriptionId(anyInt());
+
+        final String apnName = "apnName";
+        final String carrierName = "name";
+        final String numeric = TEST_OPERATOR;
+        final String mvnoType = "spn";
+        final String mvnoData = TelephonyProviderTestable.TEST_SPN;
+        final int carrierId = 100;
+        doReturn(carrierId).when(telephonyManager).getSimCarrierId();
+        doReturn(numeric).when(telephonyManager).getSimOperator();
+        //TelephonyProviderTestable had mock iccreord
+
+        // The DB only have the MCC/MNC and MVNO APN
+        ContentValues contentValues = new ContentValues();
+        contentValues.put(Carriers.APN, apnName);
+        contentValues.put(Carriers.NAME, carrierName);
+        contentValues.put(Carriers.NUMERIC, numeric);
+        contentValues.put(Carriers.MVNO_TYPE, mvnoType);
+        contentValues.put(Carriers.MVNO_MATCH_DATA, mvnoData);
+        mContentResolver.insert(Carriers.CONTENT_URI, contentValues);
+
+        final String[] testProjection =
+            {
+                Carriers.APN,
+                Carriers.NAME,
+                Carriers.NUMERIC,
+                Carriers.MVNO_MATCH_DATA
+            };
+        Cursor cursor = mContentResolver.query(URL_SIM_APN_LIST,
+            testProjection, null, null, null);
+
+
+        cursor.moveToFirst();
+        assertEquals(apnName, cursor.getString(0));
+        assertEquals(carrierName, cursor.getString(1));
+        assertEquals(numeric, cursor.getString(2));
+        assertEquals(mvnoData, cursor.getString(3));
+    }
+
+    @Test
+    @SmallTest
+    public void testGetCurrentAPNList_APNMatchTheMNOCarrierID() {
+        // Test on getCurrentAPNList() step 3
+        TelephonyManager telephonyManager =
+                ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE));
+        doReturn(telephonyManager).when(telephonyManager).createForSubscriptionId(anyInt());
+
+        final String apnName = "apnName";
+        final String carrierName = "name";
+        final int carrierId = 100;
+        final int mnoCarrierId = 101;
+        final String numeric = TEST_OPERATOR;
+        doReturn(carrierId).when(telephonyManager).getSimCarrierId();
+        doReturn(mnoCarrierId).when(telephonyManager).getSimMNOCarrierId();
+        doReturn(numeric).when(telephonyManager).getSimOperator();
+
+        // The DB only have the MNO carrier id APN
+        ContentValues contentValues = new ContentValues();
+        contentValues.put(Carriers.APN, apnName);
+        contentValues.put(Carriers.NAME, carrierName);
+        contentValues.put(Carriers.CARRIER_ID, mnoCarrierId);
+        mContentResolver.insert(Carriers.CONTENT_URI, contentValues);
+
+        final String[] testProjection =
+            {
+                Carriers.APN,
+                Carriers.NAME,
+                Carriers.CARRIER_ID,
+            };
+        Cursor cursor = mContentResolver.query(URL_SIM_APN_LIST,
+            testProjection, null, null, null);
+
+        cursor.moveToFirst();
+        assertEquals(apnName, cursor.getString(0));
+        assertEquals(carrierName, cursor.getString(1));
+        assertEquals(String.valueOf(mnoCarrierId), cursor.getString(2));
+    }
+
+    @Test
+    @SmallTest
+    public void testGetCurrentAPNList_APNMatchTheParentMCCMNC() {
+        // Test on getCurrentAPNList() step 4
+        TelephonyManager telephonyManager =
+                ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE));
+        doReturn(telephonyManager).when(telephonyManager).createForSubscriptionId(anyInt());
+
+        final String apnName = "apnName";
+        final String carrierName = "name";
+        final String numeric = TEST_OPERATOR;
+        final String mvnoData = TelephonyProviderTestable.TEST_SPN;
+        final int carrierId = 100;
+        final int mnoCarrierId = 101;
+
+        doReturn(carrierId).when(telephonyManager).getSimCarrierId();
+        doReturn(numeric).when(telephonyManager).getSimOperator();
+        doReturn(mvnoData).when(telephonyManager).getSimOperatorName();
+        doReturn(mnoCarrierId).when(telephonyManager).getSimMNOCarrierId();
+
+        // The DB only have the MNO APN
+        ContentValues contentValues = new ContentValues();
+        contentValues.put(Carriers.APN, apnName);
+        contentValues.put(Carriers.NAME, carrierName);
+        contentValues.put(Carriers.NUMERIC, numeric);
+        mContentResolver.insert(Carriers.CONTENT_URI, contentValues);
+
+        final String[] testProjection =
+            {
+                Carriers.APN,
+                Carriers.NAME,
+                Carriers.NUMERIC,
+            };
+        Cursor cursor = mContentResolver.query(URL_SIM_APN_LIST,
+            testProjection, null, null, null);
+
+        cursor.moveToFirst();
+        assertEquals(apnName, cursor.getString(0));
+        assertEquals(carrierName, cursor.getString(1));
+        assertEquals(numeric, cursor.getString(2));
+    }
 }