SubId is not included in extra for carrier config change intent for SIM2

If SIM status is not ready, SubId is not included in extra for carrier
config change intent. TelephonyManager#getSimApplicationState is used
to check the SIM status but it always checks default SIM's.

To solve the issue, checks correct SIM's status.

Test: manual - Checked that can get SIM2 status on Multi-SIM device.
Also Verified that when in DSDS mode with only eSIM profile
active, default voice / sms / data values will be auto-updated after
switching eSIM profile, and then sending out sms will not trigger
unnecessary dialog.
Bug: 134549235
Bug: 137680869

Change-Id: I97c734a38dc8b37cc4996862d696351063fafd58
Merged-In: I97c734a38dc8b37cc4996862d696351063fafd58
diff --git a/src/com/android/phone/CarrierConfigLoader.java b/src/com/android/phone/CarrierConfigLoader.java
index ed95a2c..f846111 100644
--- a/src/com/android/phone/CarrierConfigLoader.java
+++ b/src/com/android/phone/CarrierConfigLoader.java
@@ -57,6 +57,7 @@
 import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.SubscriptionInfoUpdater;
 import com.android.internal.telephony.TelephonyPermissions;
+import com.android.internal.util.ArrayUtils;
 import com.android.internal.util.FastXmlSerializer;
 import com.android.internal.util.IndentingPrintWriter;
 
@@ -581,15 +582,22 @@
         intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
                 Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND |
                 Intent.FLAG_RECEIVER_FOREGROUND);
-        // Include subId/carrier id extra only if SIM records are loaded
-        TelephonyManager telephonyManager = TelephonyManager.from(mContext);
-        int simApplicationState = telephonyManager.getSimApplicationState();
-        if (addSubIdExtra && (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
-                && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY)) {
-            SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
-            intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
-                    getSpecificCarrierIdForPhoneId(phoneId));
-            intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
+        if (addSubIdExtra) {
+            int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
+            int[] subIds = SubscriptionManager.getSubId(phoneId);
+            if (!ArrayUtils.isEmpty(subIds)) {
+                TelephonyManager telMgr = TelephonyManager.from(mContext)
+                        .createForSubscriptionId(subIds[0]);
+                simApplicationState = telMgr.getSimApplicationState();
+            }
+            // Include subId/carrier id extra only if SIM records are loaded
+            if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
+                    && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
+                SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
+                intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
+                        getSpecificCarrierIdForPhoneId(phoneId));
+                intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
+            }
         }
         intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
         log("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);