Don't update SimInfo db if restore match is from newer db schema. am: 1fa92318bf

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/providers/TelephonyProvider/+/16263893

Change-Id: Iaa974abdfd570fb941da2c6649972b97e75af561
diff --git a/assets/latest_carrier_id/carrier_list.pb b/assets/latest_carrier_id/carrier_list.pb
index d038016..c19db3a 100644
--- a/assets/latest_carrier_id/carrier_list.pb
+++ b/assets/latest_carrier_id/carrier_list.pb
Binary files differ
diff --git a/assets/latest_carrier_id/carrier_list.textpb b/assets/latest_carrier_id/carrier_list.textpb
index 62398c0..4c7523a 100644
--- a/assets/latest_carrier_id/carrier_list.textpb
+++ b/assets/latest_carrier_id/carrier_list.textpb
Binary files differ
diff --git a/assets/sdk31_carrier_id/carrier_list.pb b/assets/sdk31_carrier_id/carrier_list.pb
new file mode 100644
index 0000000..d038016
--- /dev/null
+++ b/assets/sdk31_carrier_id/carrier_list.pb
Binary files differ
diff --git a/assets/sdk31_carrier_id/carrier_list.textpb b/assets/sdk31_carrier_id/carrier_list.textpb
new file mode 100644
index 0000000..62398c0
--- /dev/null
+++ b/assets/sdk31_carrier_id/carrier_list.textpb
Binary files differ
diff --git a/res/values-as/strings.xml b/res/values-as/strings.xml
index 20ad0b0..6994d18 100644
--- a/res/values-as/strings.xml
+++ b/res/values-as/strings.xml
@@ -17,5 +17,5 @@
 <resources xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
     <string name="app_label" product="tablet" msgid="9194799012395299737">"ম’বাইল নেটৱৰ্ক কনফিগাৰেশ্বন"</string>
-    <string name="app_label" product="default" msgid="8338087656149558019">"ফ’ন আৰু বাৰ্তাৰ ষ্ট’ৰেজ"</string>
+    <string name="app_label" product="default" msgid="8338087656149558019">"ফ\'ন আৰু বাৰ্তাৰ সঞ্চয়াগাৰ"</string>
 </resources>
diff --git a/src/com/android/providers/telephony/TelephonyProvider.java b/src/com/android/providers/telephony/TelephonyProvider.java
index 16cc3b3..e186f76 100644
--- a/src/com/android/providers/telephony/TelephonyProvider.java
+++ b/src/com/android/providers/telephony/TelephonyProvider.java
@@ -155,7 +155,7 @@
     private static final boolean DBG = true;
     private static final boolean VDBG = false; // STOPSHIP if true
 
-    private static final int DATABASE_VERSION = 51 << 16;
+    private static final int DATABASE_VERSION = 52 << 16;
     private static final int URL_UNKNOWN = 0;
     private static final int URL_TELEPHONY = 1;
     private static final int URL_CURRENT = 2;
@@ -439,7 +439,8 @@
         SIM_INFO_COLUMNS_TO_BACKUP.put(
                 Telephony.SimInfo.COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS,
                 Cursor.FIELD_TYPE_STRING);
-
+        SIM_INFO_COLUMNS_TO_BACKUP.put(
+                Telephony.SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED, Cursor.FIELD_TYPE_INTEGER);
     }
 
     @VisibleForTesting
@@ -564,7 +565,8 @@
                 + Telephony.SimInfo.COLUMN_ALLOWED_NETWORK_TYPES_FOR_REASONS + " TEXT,"
                 + Telephony.SimInfo.COLUMN_VOIMS_OPT_IN_STATUS + " INTEGER DEFAULT 0,"
                 + Telephony.SimInfo.COLUMN_D2D_STATUS_SHARING + " INTEGER DEFAULT 0,"
-                + Telephony.SimInfo.COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS + " TEXT"
+                + Telephony.SimInfo.COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS + " TEXT,"
+                + Telephony.SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED + " INTEGER DEFAULT -1"
                 + ");";
     }
 
@@ -1688,6 +1690,21 @@
                 }
                 oldVersion = 51 << 16 | 6;
             }
+
+            if (oldVersion < (52 << 16 | 6)) {
+                try {
+                    // Try to update the siminfo table. It might not be there.
+                    db.execSQL("ALTER TABLE " + SIMINFO_TABLE + " ADD COLUMN "
+                            + Telephony.SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED
+                            + " INTEGER DEFAULT -1;");
+                } catch (SQLiteException e) {
+                    if (DBG) {
+                        log("onUpgrade skipping " + SIMINFO_TABLE + " upgrade. " +
+                                "The table will get created in onOpen.");
+                    }
+                }
+                oldVersion = 52 << 16 | 6;
+            }
             if (DBG) {
                 log("dbh.onUpgrade:- db=" + db + " oldV=" + oldVersion + " newV=" + newVersion);
             }
@@ -2303,7 +2320,7 @@
                 mncString = getBestStringMnc(mContext, mccString, Integer.parseInt(mnc));
             }
 
-            String numeric = mccString + mncString;
+            String numeric = (mccString == null | mncString == null) ? null : mccString + mncString;
             map.put(NUMERIC, numeric);
             map.put(MCC, mccString);
             map.put(MNC, mncString);
@@ -3496,7 +3513,7 @@
                 PersistableBundle backedUpSimInfoEntry, int backupDataFormatVersion,
                 String isoCountryCodeFromDb,
                 List<String> wfcRestoreBlockedCountries) {
-            if (DATABASE_VERSION != 51 << 16) {
+            if (DATABASE_VERSION != 52 << 16) {
                 throw new AssertionError("The database schema has been updated which might make "
                     + "the format of #BACKED_UP_SIM_SPECIFIC_SETTINGS_FILE outdated. Make sure to "
                     + "1) review whether any of the columns in #SIM_INFO_COLUMNS_TO_BACKUP have "
@@ -3538,6 +3555,12 @@
              * Also make sure to add necessary removal of sensitive settings in
              * polishContentValues(ContentValues contentValues).
              */
+            if (backupDataFormatVersion >= 52 << 16) {
+                contentValues.put(Telephony.SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED,
+                        backedUpSimInfoEntry.getInt(
+                                Telephony.SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED,
+                                DEFAULT_INT_COLUMN_VALUE));
+            }
             if (backupDataFormatVersion >= 51 << 16) {
                 contentValues.put(Telephony.SimInfo.COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS,
                         backedUpSimInfoEntry.getString(
@@ -3943,7 +3966,7 @@
                 .createForSubscriptionId(subId);
         SQLiteDatabase db = getReadableDatabase();
         String mccmnc = tm.getSimOperator();
-        int carrierId = tm.getSimCarrierId();
+        int carrierId = tm.getSimSpecificCarrierId();
 
         qb.appendWhereStandalone(IS_NOT_USER_DELETED + " and " +
                 IS_NOT_USER_DELETED_BUT_PRESENT_IN_XML + " and " +
@@ -3968,6 +3991,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);
@@ -3981,15 +4005,17 @@
                 data.add(ret.getString(ret.getColumnIndex(column)));
             }
 
-            boolean isCurrentSimOperator;
-            final long identity = Binder.clearCallingIdentity();
-            try {
-                isCurrentSimOperator = tm.matchesCurrentSimOperator(
-                        ret.getString(numericIndex),
-                        getMvnoTypeIntFromString(ret.getString(mvnoIndex)),
-                        ret.getString(mvnoDataIndex));
-            } finally {
-                Binder.restoreCallingIdentity(identity);
+            boolean isCurrentSimOperator = false;
+            if (!TextUtils.isEmpty(ret.getString(numericIndex))) {
+                final long identity = Binder.clearCallingIdentity();
+                try {
+                    isCurrentSimOperator = tm.matchesCurrentSimOperator(
+                            ret.getString(numericIndex),
+                            getMvnoTypeIntFromString(ret.getString(mvnoIndex)),
+                            ret.getString(mvnoDataIndex));
+                } finally {
+                    Binder.restoreCallingIdentity(identity);
+                }
             }
 
             boolean isMVNOAPN = !TextUtils.isEmpty(ret.getString(numericIndex))
@@ -4009,7 +4035,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();
@@ -4022,8 +4052,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());
@@ -4751,6 +4784,12 @@
                                         Telephony.SimInfo.COLUMN_VOIMS_OPT_IN_STATUS),
                                 usingSubId, subId), null, true, UserHandle.USER_ALL);
                     }
+                    if (values.containsKey(Telephony.SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED)) {
+                        getContext().getContentResolver().notifyChange(getNotifyContentUri(
+                                Uri.withAppendedPath(Telephony.SimInfo.CONTENT_URI,
+                                        Telephony.SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED),
+                                usingSubId, subId), null, true, UserHandle.USER_ALL);
+                    }
                     break;
                 default:
                     getContext().getContentResolver().notifyChange(
diff --git a/tests/src/com/android/providers/telephony/TelephonyDatabaseHelperTest.java b/tests/src/com/android/providers/telephony/TelephonyDatabaseHelperTest.java
index 6f3f842..eb03443 100644
--- a/tests/src/com/android/providers/telephony/TelephonyDatabaseHelperTest.java
+++ b/tests/src/com/android/providers/telephony/TelephonyDatabaseHelperTest.java
@@ -266,6 +266,23 @@
                 Telephony.SimInfo.COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS));
     }
 
+    @Test
+    public void databaseHelperOnUpgrade_hasNrAdvancedCallingEnabledField() {
+        Log.d(TAG, "databaseHelperOnUpgrade_hasNrAdvancedCallingEnabledField");
+        // (5 << 16 | 6) is the first upgrade trigger in onUpgrade
+        SQLiteDatabase db = mInMemoryDbHelper.getWritableDatabase();
+        mHelper.onUpgrade(db, (4 << 16), TelephonyProvider.getVersion(mContext));
+
+        // the upgraded db must have the
+        // Telephony.SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED field
+        Cursor cursor = db.query("siminfo", null, null, null, null, null, null);
+        String[] upgradedColumns = cursor.getColumnNames();
+        Log.d(TAG, "siminfo columns: " + Arrays.toString(upgradedColumns));
+
+        assertTrue(Arrays.asList(upgradedColumns).contains(
+                Telephony.SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED));
+    }
+
     /**
      * Helper for an in memory DB used to test the TelephonyProvider#DatabaseHelper.
      *
diff --git a/tests/src/com/android/providers/telephony/TelephonyProviderTest.java b/tests/src/com/android/providers/telephony/TelephonyProviderTest.java
index 1a9b46c..3455ffd 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;
@@ -213,6 +214,7 @@
         contentValues.put(Telephony.SimInfo.COLUMN_D2D_STATUS_SHARING, arbitraryIntVal);
         contentValues.put(Telephony.SimInfo.COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS,
                 arbitraryStringVal);
+        contentValues.put(Telephony.SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED, arbitraryIntVal);
         if (isoCountryCode != null) {
             contentValues.put(Telephony.SimInfo.COLUMN_ISO_COUNTRY_CODE, isoCountryCode);
         }
@@ -257,7 +259,7 @@
                     .isActiveSubscriptionId(anyInt());
             doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(anyInt());
             doReturn(TEST_OPERATOR).when(mTelephonyManager).getSimOperator();
-            doReturn(TEST_CARRIERID).when(mTelephonyManager).getSimCarrierId();
+            doReturn(TEST_CARRIERID).when(mTelephonyManager).getSimSpecificCarrierId();
 
             // Add authority="telephony" to given telephonyProvider
             ProviderInfo providerInfo = new ProviderInfo();
@@ -790,7 +792,10 @@
                 getIntValueFromCursor(cursor, Telephony.SimInfo.COLUMN_WFC_IMS_MODE));
         assertEquals(ARBITRARY_SIMINFO_DB_TEST_INT_VALUE_1,
                 getIntValueFromCursor(cursor, Telephony.SimInfo.COLUMN_WFC_IMS_ROAMING_MODE));
-
+        assertEquals(
+                ARBITRARY_SIMINFO_DB_TEST_INT_VALUE_1,
+                getIntValueFromCursor(
+                        cursor, Telephony.SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED));
         assertRestoredSubIdIsRemembered();
     }
 
@@ -831,7 +836,10 @@
                 getIntValueFromCursor(cursor, Telephony.SimInfo.COLUMN_WFC_IMS_MODE));
         assertEquals(ARBITRARY_SIMINFO_DB_TEST_INT_VALUE_2,
                 getIntValueFromCursor(cursor, Telephony.SimInfo.COLUMN_WFC_IMS_ROAMING_MODE));
-
+        assertEquals(
+                ARBITRARY_SIMINFO_DB_TEST_INT_VALUE_2,
+                getIntValueFromCursor(
+                        cursor, Telephony.SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED));
         assertRestoredSubIdIsRemembered();
     }
 
@@ -872,7 +880,10 @@
                 getIntValueFromCursor(cursor, Telephony.SimInfo.COLUMN_WFC_IMS_MODE));
         assertEquals(ARBITRARY_SIMINFO_DB_TEST_INT_VALUE_3,
                 getIntValueFromCursor(cursor, Telephony.SimInfo.COLUMN_WFC_IMS_ROAMING_MODE));
-
+        assertEquals(
+                ARBITRARY_SIMINFO_DB_TEST_INT_VALUE_3,
+                getIntValueFromCursor(
+                        cursor, Telephony.SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED));
         assertRestoredSubIdIsRemembered();
     }
 
@@ -914,7 +925,10 @@
                 getIntValueFromCursor(cursor, Telephony.SimInfo.COLUMN_WFC_IMS_MODE));
         assertEquals(ARBITRARY_SIMINFO_DB_TEST_INT_VALUE_1,
                 getIntValueFromCursor(cursor, Telephony.SimInfo.COLUMN_WFC_IMS_ROAMING_MODE));
-
+        assertEquals(
+                ARBITRARY_SIMINFO_DB_TEST_INT_VALUE_1,
+                getIntValueFromCursor(
+                        cursor, Telephony.SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED));
         assertRestoredSubIdIsRemembered();
     }
 
@@ -2096,4 +2110,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)));
+    }
 }