Modify the URL_FILTERED logic and fix issue in URL_SIM_APN_LIST

1. The logic in URL_FILTERED will same as URL_SIM_APN_LIST but filtered
by OWNED_BY_DPC.
2. Add permission check and null check in URL_SIM_APN_LIST

Bug: 115709816
Test: run TelephonyProviderTest
Change-Id: Id70cdc48ce5a7393f8f5555f5914d37ad5875883
diff --git a/src/com/android/providers/telephony/TelephonyProvider.java b/src/com/android/providers/telephony/TelephonyProvider.java
index af8de7e..a5edf08 100644
--- a/src/com/android/providers/telephony/TelephonyProvider.java
+++ b/src/com/android/providers/telephony/TelephonyProvider.java
@@ -2562,6 +2562,7 @@
         List<String> constraints = new ArrayList<String>();
 
         int match = s_urlMatcher.match(url);
+        checkQueryPermission(match, projectionIn);
         switch (match) {
             case URL_TELEPHONY_USING_SUBID: {
                 subIdString = url.getLastPathSegment();
@@ -2658,18 +2659,19 @@
             }
 
             case URL_FILTERED_ID: {
-                constraints.add("_id = " + url.getLastPathSegment());
+                qb.appendWhere("_id = " + url.getLastPathSegment());
             }
             //intentional fall through from above case
             case URL_FILTERED: {
                 if (isManagedApnEnforced()) {
                     // If enforced, return DPC records only.
-                    constraints.add(IS_OWNED_BY_DPC);
+                    qb.appendWhereStandalone(IS_OWNED_BY_DPC);
                 } else {
                     // Otherwise return non-DPC records only.
-                    constraints.add(IS_NOT_OWNED_BY_DPC);
+                    qb.appendWhereStandalone(IS_NOT_OWNED_BY_DPC);
                 }
-                break;
+                return getSubscriptionMatchingAPNList(qb, projectionIn, selection, selectionArgs,
+                        sort, subId);
             }
 
             case URL_ENFORCE_MANAGED: {
@@ -2695,7 +2697,9 @@
             }
             //intentional fall through from above case
             case URL_SIM_APN_LIST: {
-                return getSubscriptionMatchingAPNList(qb, projectionIn, sort, subId);
+                qb.appendWhere(IS_NOT_OWNED_BY_DPC);
+                return getSubscriptionMatchingAPNList(qb, projectionIn, selection, selectionArgs,
+                        sort, subId);
             }
 
             default: {
@@ -2708,28 +2712,6 @@
             qb.appendWhere(TextUtils.join(" AND ", constraints));
         }
 
-        if (match != URL_SIMINFO) {
-            if (projectionIn != null) {
-                for (String column : projectionIn) {
-                    if (TYPE.equals(column) ||
-                            MMSC.equals(column) ||
-                            MMSPROXY.equals(column) ||
-                            MMSPORT.equals(column) ||
-                            MVNO_TYPE.equals(column) ||
-                            MVNO_MATCH_DATA.equals(column) ||
-                            APN.equals(column)) {
-                        // noop
-                    } else {
-                        checkPermission();
-                        break;
-                    }
-                }
-            } else {
-                // null returns all columns, so need permission check
-                checkPermission();
-            }
-        }
-
         SQLiteDatabase db = getReadableDatabase();
         Cursor ret = null;
         try {
@@ -2755,6 +2737,30 @@
         return ret;
     }
 
+    private void checkQueryPermission(int match, String[] projectionIn) {
+        if (match != URL_SIMINFO) {
+            if (projectionIn != null) {
+                for (String column : projectionIn) {
+                    if (TYPE.equals(column) ||
+                            MMSC.equals(column) ||
+                            MMSPROXY.equals(column) ||
+                            MMSPORT.equals(column) ||
+                            MVNO_TYPE.equals(column) ||
+                            MVNO_MATCH_DATA.equals(column) ||
+                            APN.equals(column)) {
+                        // noop
+                    } else {
+                        checkPermission();
+                        break;
+                    }
+                }
+            } else {
+                // null returns all columns, so need permission check
+                checkPermission();
+            }
+        }
+    }
+
     /**
      * To find the current sim APN.
      *
@@ -2766,7 +2772,7 @@
      *
      */
     private Cursor getSubscriptionMatchingAPNList(SQLiteQueryBuilder qb, String[] projectionIn,
-            String sort, int subId) {
+            String selection, String[] selectionArgs, String sort, int subId) {
 
         Cursor ret;
         final TelephonyManager tm = ((TelephonyManager)
@@ -2774,24 +2780,25 @@
                 .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);
+        // 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 + " = '" + carrierId + "' OR " +
+                NUMERIC + " = '" + mccmnc + "' OR " +
+                CARRIER_ID + " = '" + mnoCarrierId + "' ";
+        qb.appendWhereStandalone(carrierIDSelection);
+
+        ret = qb.query(db, null, selection, selectionArgs, null, null, sort);
 
         if (DBG) log("match current APN size:  " + ret.getCount());
 
-        MatrixCursor currentCursor = new MatrixCursor(projectionIn);
-        MatrixCursor parentCursor = new MatrixCursor(projectionIn);
+        String[] coulmnNames = projectionIn != null ? projectionIn : ret.getColumnNames();
+        MatrixCursor currentCursor = new MatrixCursor(coulmnNames);
+        MatrixCursor parentCursor = new MatrixCursor(coulmnNames);
 
         int carrierIdIndex = ret.getColumnIndex(CARRIER_ID);
         int numericIndex = ret.getColumnIndex(NUMERIC);
@@ -2803,7 +2810,7 @@
         //Separate the result into MatrixCursor
         while (ret.moveToNext()) {
             List<String> data = new ArrayList<>();
-            for (String column : projectionIn) {
+            for (String column : coulmnNames) {
                 data.add(ret.getString(ret.getColumnIndex(column)));
             }
 
@@ -2837,7 +2844,7 @@
             return parentCursor;
         } else {
             if (DBG) log("APN no match");
-            return new MatrixCursor(projectionIn);
+            return new MatrixCursor(coulmnNames);
         }
     }
 
diff --git a/tests/src/com/android/providers/telephony/TelephonyProviderTest.java b/tests/src/com/android/providers/telephony/TelephonyProviderTest.java
index cf4409a..91a7a46 100644
--- a/tests/src/com/android/providers/telephony/TelephonyProviderTest.java
+++ b/tests/src/com/android/providers/telephony/TelephonyProviderTest.java
@@ -136,6 +136,9 @@
             // return test subId 0 for all operators
             doReturn(TEST_OPERATOR).when(mTelephonyManager).getSimOperator(anyInt());
 
+            doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(anyInt());
+            doReturn(TEST_OPERATOR).when(mTelephonyManager).getSimOperator();
+
             // Add authority="telephony" to given telephonyProvider
             ProviderInfo providerInfo = new ProviderInfo();
             providerInfo.authority = "telephony";
@@ -599,7 +602,7 @@
         mTelephonyProviderTestable.fakeCallingUid(Process.SYSTEM_UID);
 
         final int current = 1;
-        final String numeric = "123456789";
+        final String numeric = TEST_OPERATOR;
 
         // Insert DPC record.
         final String dpcRecordApn = "exampleApnNameDPC";
@@ -1444,7 +1447,7 @@
 
     @Test
     @SmallTest
-    public void testGetCurrentAPNList_APNMatchTheCarrierID() {
+    public void testSIMAPNLIST_APNMatchTheCarrierID() {
         // Test on getCurrentAPNList() step 1
         TelephonyManager telephonyManager =
                 ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE));
@@ -1464,6 +1467,7 @@
         contentValues.put(Carriers.CARRIER_ID, carrierID);
         mContentResolver.insert(Carriers.CONTENT_URI, contentValues);
 
+        // Query DB
         final String[] testProjection =
             {
                 Carriers.APN,
@@ -1471,7 +1475,7 @@
                 Carriers.CARRIER_ID,
             };
         Cursor cursor = mContentResolver.query(URL_SIM_APN_LIST,
-            testProjection, null, null, null);
+                testProjection, null, null, null);
 
         cursor.moveToFirst();
         assertEquals(apnName, cursor.getString(0));
@@ -1481,7 +1485,7 @@
 
     @Test
     @SmallTest
-    public void testGetCurrentAPNList_APNMatchTheMCCMNCAndMVNO() {
+    public void testSIMAPNLIST_APNMatchTheMCCMNCAndMVNO() {
         // Test on getCurrentAPNList() step 2
         TelephonyManager telephonyManager =
                 ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE));
@@ -1495,9 +1499,8 @@
         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
+        // Insert the APN and DB only have the MCC/MNC and MVNO APN
         ContentValues contentValues = new ContentValues();
         contentValues.put(Carriers.APN, apnName);
         contentValues.put(Carriers.NAME, carrierName);
@@ -1506,6 +1509,7 @@
         contentValues.put(Carriers.MVNO_MATCH_DATA, mvnoData);
         mContentResolver.insert(Carriers.CONTENT_URI, contentValues);
 
+        // Query DB
         final String[] testProjection =
             {
                 Carriers.APN,
@@ -1514,8 +1518,7 @@
                 Carriers.MVNO_MATCH_DATA
             };
         Cursor cursor = mContentResolver.query(URL_SIM_APN_LIST,
-            testProjection, null, null, null);
-
+                testProjection, null, null, null);
 
         cursor.moveToFirst();
         assertEquals(apnName, cursor.getString(0));
@@ -1526,7 +1529,7 @@
 
     @Test
     @SmallTest
-    public void testGetCurrentAPNList_APNMatchTheMNOCarrierID() {
+    public void testSIMAPNLIST_APNMatchTheMNOCarrierID() {
         // Test on getCurrentAPNList() step 3
         TelephonyManager telephonyManager =
                 ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE));
@@ -1541,13 +1544,14 @@
         doReturn(mnoCarrierId).when(telephonyManager).getSimMNOCarrierId();
         doReturn(numeric).when(telephonyManager).getSimOperator();
 
-        // The DB only have the MNO carrier id APN
+        // Insert the APN and 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);
 
+        // Query DB
         final String[] testProjection =
             {
                 Carriers.APN,
@@ -1565,7 +1569,7 @@
 
     @Test
     @SmallTest
-    public void testGetCurrentAPNList_APNMatchTheParentMCCMNC() {
+    public void testSIMAPNLIST_APNMatchTheParentMCCMNC() {
         // Test on getCurrentAPNList() step 4
         TelephonyManager telephonyManager =
                 ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE));
@@ -1583,13 +1587,14 @@
         doReturn(mvnoData).when(telephonyManager).getSimOperatorName();
         doReturn(mnoCarrierId).when(telephonyManager).getSimMNOCarrierId();
 
-        // The DB only have the MNO APN
+        // Insert the APN and 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);
 
+        // Query DB
         final String[] testProjection =
             {
                 Carriers.APN,
@@ -1597,7 +1602,7 @@
                 Carriers.NUMERIC,
             };
         Cursor cursor = mContentResolver.query(URL_SIM_APN_LIST,
-            testProjection, null, null, null);
+                testProjection, null, null, null);
 
         cursor.moveToFirst();
         assertEquals(apnName, cursor.getString(0));