Added extra logic for selecting APNs for a subscription

A carrier can have multiple mcc/mnc attributed to it, but
have the same carrier_id.
In case APNs that match mcc/mnc/mvno are found in database,
discard any APNs that match carrier_id, but do not match
mcc/mnc.
In case there is no APN matching mcc/mnc/mvno, add all
APNs that match carrier_id.

Test: Add APNs for Telia SE 240 01 and 240 05 with carrier_id 1690 and check in APN Settings.
Bug: 197470879
Change-Id: Ia7528aea598c86f9a51d2de5360936f46bfdbc9c
diff --git a/src/com/android/providers/telephony/TelephonyProvider.java b/src/com/android/providers/telephony/TelephonyProvider.java
index 0ff9fc6..5749549 100644
--- a/src/com/android/providers/telephony/TelephonyProvider.java
+++ b/src/com/android/providers/telephony/TelephonyProvider.java
@@ -3964,6 +3964,7 @@
         MatrixCursor currentCursor = new MatrixCursor(columnNames);
         MatrixCursor parentCursor = new MatrixCursor(columnNames);
         MatrixCursor carrierIdCursor = new MatrixCursor(columnNames);
+        MatrixCursor carrierIdNonMatchingMNOCursor = new MatrixCursor(columnNames);
 
         int numericIndex = ret.getColumnIndex(NUMERIC);
         int mvnoIndex = ret.getColumnIndex(MVNO_TYPE);
@@ -4005,7 +4006,11 @@
                 parentCursor.addRow(data);
             } else if (isCarrierIdAPN) {
                 // The APN that query based on carrier Id (not include the MVNO or MNO APN)
-                carrierIdCursor.addRow(data);
+                if (TextUtils.isEmpty(ret.getString(numericIndex))) {
+                    carrierIdCursor.addRow(data);
+                } else {
+                    carrierIdNonMatchingMNOCursor.addRow(data);
+                }
             }
         }
         ret.close();
@@ -4018,8 +4023,11 @@
             if (DBG) log("match MNO APN: " + parentCursor.getCount());
             result = parentCursor;
         } else {
-            if (DBG) log("can't find the MVNO and MNO APN");
-            result = new MatrixCursor(columnNames);
+            if (DBG) {
+                log("No MVNO, MNO and no MCC/MNC match, but we have match/matches with the " +
+                        "same carrier id, count: " + carrierIdNonMatchingMNOCursor.getCount());
+            }
+            result = carrierIdNonMatchingMNOCursor;
         }
 
         if (DBG) log("match carrier id APN: " + carrierIdCursor.getCount());
diff --git a/tests/src/com/android/providers/telephony/TelephonyProviderTest.java b/tests/src/com/android/providers/telephony/TelephonyProviderTest.java
index 1a9b46c..64ff171 100644
--- a/tests/src/com/android/providers/telephony/TelephonyProviderTest.java
+++ b/tests/src/com/android/providers/telephony/TelephonyProviderTest.java
@@ -96,6 +96,7 @@
 
     private static final String TEST_SUBID = "1";
     private static final String TEST_OPERATOR = "123456";
+    private static final String TEST_OPERATOR_SECOND_MCCMNC = "567890";
     private static final String TEST_MCC = "123";
     private static final String TEST_MNC = "456";
     private static final String TEST_SPN = TelephonyProviderTestable.TEST_SPN;
@@ -2096,4 +2097,119 @@
 
         assertNull(cursor);
     }
+
+    @Test
+    @SmallTest
+    public void testSIMAPNLIST_MatchTheCarrierIDANDdifferentMNOAPN() {
+        setUpMockContext(true);
+
+        final String apnName = "apnName";
+        final String carrierName = "name";
+        final int carrierId = TEST_CARRIERID;
+
+        // Add an APN that have carrier id and matching MNO
+        ContentValues contentValues = new ContentValues();
+        contentValues.put(Carriers.APN, apnName);
+        contentValues.put(Carriers.NAME, carrierName);
+        contentValues.put(Carriers.CARRIER_ID, carrierId);
+        contentValues.put(Carriers.NUMERIC, TEST_OPERATOR);
+        mContentResolver.insert(Carriers.CONTENT_URI, contentValues);
+
+        // Add MNO APN that have same carrier id, but different MNO
+        contentValues = new ContentValues();
+        contentValues.put(Carriers.APN, apnName);
+        contentValues.put(Carriers.NAME, carrierName);
+        contentValues.put(Carriers.CARRIER_ID, carrierId);
+        contentValues.put(Carriers.NUMERIC, TEST_OPERATOR_SECOND_MCCMNC);
+        mContentResolver.insert(Carriers.CONTENT_URI, contentValues);
+
+        // Query DB
+        final String[] testProjection =
+            {
+                Carriers.APN,
+                Carriers.NAME,
+                Carriers.CARRIER_ID,
+                Carriers.NUMERIC,
+            };
+
+        Cursor cursor = mContentResolver.query(URL_SIM_APN_LIST, testProjection, null, null, null);
+
+        // The query based on SIM_APN_LIST will return the APN which matches both carrier id and MNO
+        assertEquals(1, cursor.getCount());
+        cursor.moveToFirst();
+        assertEquals(TEST_OPERATOR, cursor.getString(cursor.getColumnIndex(Carriers.NUMERIC)));
+    }
+
+    @Test
+    @SmallTest
+    public void testSIMAPNLIST_MatchTheCarrierIDMissingMNO() {
+        setUpMockContext(true);
+
+        final String apnName = "apnName";
+        final String carrierName = "name";
+        final int carrierId = TEST_CARRIERID;
+
+        // Add an APN that have matching carrier id and no mno
+        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);
+
+        // Add MNO APN that have non matching carrier id and no mno
+        contentValues = new ContentValues();
+        contentValues.put(Carriers.APN, apnName);
+        contentValues.put(Carriers.NAME, carrierName);
+        contentValues.put(Carriers.CARRIER_ID, 99999);
+        mContentResolver.insert(Carriers.CONTENT_URI, contentValues);
+
+        // Query DB
+        final String[] testProjection =
+            {
+                Carriers.APN,
+                Carriers.NAME,
+                Carriers.CARRIER_ID,
+            };
+
+        Cursor cursor = mContentResolver.query(URL_SIM_APN_LIST, testProjection, null, null, null);
+
+        // The query based on SIM_APN_LIST will return the APN which matches carrier id
+        assertEquals(1, cursor.getCount());
+        cursor.moveToFirst();
+        assertEquals(TEST_CARRIERID, cursor.getInt(cursor.getColumnIndex(Carriers.CARRIER_ID)));
+    }
+
+    @Test
+    @SmallTest
+    public void testSIMAPNLIST_MatchTheCarrierIDNOTMatchingMNO() {
+        setUpMockContext(true);
+
+        final String apnName = "apnName";
+        final String carrierName = "name";
+        final int carrierId = TEST_CARRIERID;
+
+        // Add an APN that have matching carrier id and not matching mno
+        ContentValues contentValues = new ContentValues();
+        contentValues.put(Carriers.APN, apnName);
+        contentValues.put(Carriers.NAME, carrierName);
+        contentValues.put(Carriers.CARRIER_ID, carrierId);
+        contentValues.put(Carriers.NUMERIC, TEST_OPERATOR_SECOND_MCCMNC);
+        mContentResolver.insert(Carriers.CONTENT_URI, contentValues);
+
+        // Query DB
+        final String[] testProjection =
+            {
+                Carriers.APN,
+                Carriers.NAME,
+                Carriers.CARRIER_ID,
+            };
+
+        Cursor cursor = mContentResolver.query(URL_SIM_APN_LIST, testProjection, null, null, null);
+
+        // The query based on SIM_APN_LIST will return the APN which matches carrier id,
+        // even though the mno does not match
+        assertEquals(1, cursor.getCount());
+        cursor.moveToFirst();
+        assertEquals(TEST_CARRIERID, cursor.getInt(cursor.getColumnIndex(Carriers.CARRIER_ID)));
+    }
 }