Improve carrier id GTS performance

In some lower CPU/memory devices, carrier id GTS will exceed the
TelephonyHostTest timeout(20 mins). The setCarrierId() have some DB
operators and it takes nearly 70% of the time.

The carrier id GTS will execute updateCarrierIdAndName() more than
10,000 times, so if the carrierResoler receives the SIM loading event
triggered by test override, then skip setCarrierId() to improve.

Bug: 191729329
Test: atest --iterations 10. On pixel, execution time from 4.5 mins to
1 min.

Change-Id: I7f2b6e8b3befd657a2d22a0bad453c5b63929bbd
diff --git a/src/java/com/android/internal/telephony/CarrierResolver.java b/src/java/com/android/internal/telephony/CarrierResolver.java
index 794bb41..c1e1136 100644
--- a/src/java/com/android/internal/telephony/CarrierResolver.java
+++ b/src/java/com/android/internal/telephony/CarrierResolver.java
@@ -27,6 +27,7 @@
 import android.database.ContentObserver;
 import android.database.Cursor;
 import android.net.Uri;
+import android.os.AsyncResult;
 import android.os.Handler;
 import android.os.Message;
 import android.provider.Telephony;
@@ -164,7 +165,7 @@
                 updateCarrierIdAndName(
                     carrierId, carrierName != null ? carrierName : "",
                     specificCarrierId, specificCarrierName != null ? carrierName : "",
-                    mnoCarrierId);
+                    mnoCarrierId, false);
             }
         }
     };
@@ -215,12 +216,12 @@
                 handleSimAbsent();
                 break;
             case IccCardConstants.INTENT_VALUE_ICC_LOADED:
-                handleSimLoaded();
+                handleSimLoaded(false);
                 break;
         }
     }
 
-    private void handleSimLoaded() {
+    private void handleSimLoaded(boolean isSimOverride) {
         if (mIccRecords != null) {
             /**
              * returns empty string to be consistent with
@@ -232,7 +233,9 @@
             loge("mIccRecords is null on SIM_LOAD_EVENT, could not get SPN");
         }
         mPreferApn = getPreferApn();
-        loadCarrierMatchingRulesOnMccMnc(false /* update carrier config */);
+        loadCarrierMatchingRulesOnMccMnc(
+                false /* update carrier config */,
+                isSimOverride);
     }
 
     private void handleSimAbsent() {
@@ -241,7 +244,7 @@
         mPreferApn = null;
         updateCarrierIdAndName(TelephonyManager.UNKNOWN_CARRIER_ID, null,
                 TelephonyManager.UNKNOWN_CARRIER_ID, null,
-                TelephonyManager.UNKNOWN_CARRIER_ID);
+                TelephonyManager.UNKNOWN_CARRIER_ID, false);
     }
 
     private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
@@ -274,19 +277,24 @@
         if (DBG) logd("handleMessage: " + msg.what);
         switch (msg.what) {
             case SIM_LOAD_EVENT:
-                handleSimLoaded();
+                AsyncResult result = (AsyncResult) msg.obj;
+                boolean isSimOverride = false;
+                if (result != null) {
+                    isSimOverride = result.userObj instanceof Boolean && (Boolean) result.userObj;
+                }
+                handleSimLoaded(isSimOverride);
                 break;
             case CARRIER_ID_DB_UPDATE_EVENT:
                 // clean the cached carrier list version, so that a new one will be queried.
                 mCarrierListVersion = null;
-                loadCarrierMatchingRulesOnMccMnc(true /* update carrier config*/);
+                loadCarrierMatchingRulesOnMccMnc(true /* update carrier config*/, false);
                 break;
             case PREFER_APN_UPDATE_EVENT:
                 String preferApn = getPreferApn();
                 if (!equals(mPreferApn, preferApn, true)) {
                     logd("[updatePreferApn] from:" + mPreferApn + " to:" + preferApn);
                     mPreferApn = preferApn;
-                    matchSubscriptionCarrier(true /* update carrier config*/);
+                    matchSubscriptionCarrier(true /* update carrier config*/, false);
                 }
                 break;
             case ICC_CHANGED_EVENT:
@@ -301,7 +309,8 @@
                     }
                     if (newIccRecords != null) {
                         logd("new Icc object");
-                        newIccRecords.registerForRecordsOverride(this, SIM_LOAD_EVENT, null);
+                        newIccRecords.registerForRecordsOverride(this, SIM_LOAD_EVENT,
+                                /* is sim override*/true);
                         mIccRecords = newIccRecords;
                     }
                 }
@@ -312,7 +321,9 @@
         }
     }
 
-    private void loadCarrierMatchingRulesOnMccMnc(boolean updateCarrierConfig) {
+    private void loadCarrierMatchingRulesOnMccMnc(
+            boolean updateCarrierConfig,
+            boolean isSimOverride) {
         try {
             String mccmnc = mTelephonyMgr.getSimOperatorNumericForPhone(mPhone.getPhoneId());
             Cursor cursor = mContext.getContentResolver().query(
@@ -330,7 +341,7 @@
                     while (cursor.moveToNext()) {
                         mCarrierMatchingRulesOnMccMnc.add(makeCarrierMatchingRule(cursor));
                     }
-                    matchSubscriptionCarrier(updateCarrierConfig);
+                    matchSubscriptionCarrier(updateCarrierConfig, isSimOverride);
 
                     // Generate metrics related to carrier ID table version.
                     CarrierIdMatchStats.sendCarrierIdTableVersion(getCarrierListVersion());
@@ -465,7 +476,7 @@
 
     private void updateCarrierIdAndName(int cid, String name,
                                         int specificCarrierId, String specificCarrierName,
-                                        int mnoCid) {
+                                        int mnoCid, boolean isSimOverride) {
         boolean update = false;
         if (specificCarrierId != mSpecificCarrierId) {
             logd("[updateSpecificCarrierId] from:" + mSpecificCarrierId + " to:"
@@ -534,7 +545,7 @@
         // during esim profile switch, there is no sim absent thus carrier id will persist and
         // might not trigger an update if switch profiles for the same carrier. thus always update
         // subscriptioninfo db to make sure we have correct carrier id set.
-        if (SubscriptionManager.isValidSubscriptionId(mPhone.getSubId())) {
+        if (SubscriptionManager.isValidSubscriptionId(mPhone.getSubId()) && !isSimOverride) {
             // only persist carrier id to simInfo db when subId is valid.
             SubscriptionController.getInstance().setCarrierId(mCarrierId, mPhone.getSubId());
         }
@@ -827,7 +838,7 @@
     /**
      * find the best matching carrier from candidates with matched subscription MCCMNC.
      */
-    private void matchSubscriptionCarrier(boolean updateCarrierConfig) {
+    private void matchSubscriptionCarrier(boolean updateCarrierConfig, boolean isSimOverride) {
         if (!SubscriptionManager.isValidSubscriptionId(mPhone.getSubId())) {
             logd("[matchSubscriptionCarrier]" + "skip before sim records loaded");
             return;
@@ -870,7 +881,7 @@
                     + " name: " + null);
             updateCarrierIdAndName(TelephonyManager.UNKNOWN_CARRIER_ID, null,
                     TelephonyManager.UNKNOWN_CARRIER_ID, null,
-                    TelephonyManager.UNKNOWN_CARRIER_ID);
+                    TelephonyManager.UNKNOWN_CARRIER_ID, isSimOverride);
         } else {
             // if there is a single matching result, check if this rule has parent cid assigned.
             if ((maxRule == maxRuleParent)
@@ -884,7 +895,7 @@
                     + " name: " + maxRuleParent.mName);
             updateCarrierIdAndName(maxRuleParent.mCid, maxRuleParent.mName,
                     maxRule.mCid, maxRule.mName,
-                    (mnoRule == null) ? maxRule.mCid : mnoRule.mCid);
+                    (mnoRule == null) ? maxRule.mCid : mnoRule.mCid, isSimOverride);
 
             if (updateCarrierConfig) {
                 logd("[matchSubscriptionCarrier] - Calling updateCarrierConfig()");