Populate cardId in updateCache instead of waiting for activation

Test: verified that with this code, after downloading profile the
cardString is set in AllSubs list, verified that without this patch it
is not set
Bug: 139424928
Change-Id: I0f5a1fef62dfc33e7f6b4da2cb99f982515e7203
diff --git a/src/java/com/android/internal/telephony/SubscriptionInfoUpdater.java b/src/java/com/android/internal/telephony/SubscriptionInfoUpdater.java
index f3d6248..9f270ba 100644
--- a/src/java/com/android/internal/telephony/SubscriptionInfoUpdater.java
+++ b/src/java/com/android/internal/telephony/SubscriptionInfoUpdater.java
@@ -53,6 +53,7 @@
 import android.telephony.UiccAccessRule;
 import android.telephony.euicc.EuiccManager;
 import android.text.TextUtils;
+import android.util.Pair;
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.euicc.EuiccController;
@@ -716,19 +717,20 @@
         }
 
         mBackgroundHandler.post(() -> {
-            List<GetEuiccProfileInfoListResult> results = new ArrayList<>();
+            List<Pair<Integer, GetEuiccProfileInfoListResult>> results = new ArrayList<>();
             for (int cardId : cardIds) {
                 GetEuiccProfileInfoListResult result =
                         EuiccController.get().blockingGetEuiccProfileInfoList(cardId);
                 if (DBG) logd("blockingGetEuiccProfileInfoList cardId " + cardId);
-                results.add(result);
+                results.add(Pair.create(cardId, result));
             }
 
             // The runnable will be executed in the main thread.
             this.post(() -> {
                 boolean hasChanges = false;
-                for (GetEuiccProfileInfoListResult result : results) {
-                    if (updateEmbeddedSubscriptionsCache(result)) {
+                for (Pair<Integer, GetEuiccProfileInfoListResult> cardIdAndResult : results) {
+                    if (updateEmbeddedSubscriptionsCache(cardIdAndResult.first,
+                            cardIdAndResult.second)) {
                         hasChanges = true;
                     }
                 }
@@ -749,7 +751,8 @@
      * but notifications about subscription changes may be skipped if this returns false as an
      * optimization to avoid spurious notifications.
      */
-    private boolean updateEmbeddedSubscriptionsCache(GetEuiccProfileInfoListResult result) {
+    private boolean updateEmbeddedSubscriptionsCache(int cardId,
+            GetEuiccProfileInfoListResult result) {
         if (DBG) logd("updateEmbeddedSubscriptionsCache");
 
         if (result == null) {
@@ -854,6 +857,14 @@
                 values.put(SubscriptionManager.MNC_STRING, mnc);
                 values.put(SubscriptionManager.MNC, mnc);
             }
+            // If cardId = unsupported or unitialized, we have no reason to update DB.
+            // Additionally, if the device does not support cardId for default eUICC, the CARD_ID
+            // field should not contain the EID
+            if (cardId >= 0 && UiccController.getInstance().getCardIdForDefaultEuicc()
+                    != TelephonyManager.UNSUPPORTED_CARD_ID) {
+                values.put(SubscriptionManager.CARD_ID,
+                        mEuiccManager.createForCardId(cardId).getEid());
+            }
             hasChanges = true;
             contentResolver.update(SubscriptionManager.CONTENT_URI, values,
                     SubscriptionManager.ICC_ID + "=\"" + embeddedProfile.getIccid() + "\"", null);
diff --git a/tests/telephonytests/src/com/android/internal/telephony/SubscriptionInfoUpdaterTest.java b/tests/telephonytests/src/com/android/internal/telephony/SubscriptionInfoUpdaterTest.java
index cf1fe80..795e4d3 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/SubscriptionInfoUpdaterTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/SubscriptionInfoUpdaterTest.java
@@ -79,6 +79,7 @@
     private static final int FAKE_SUB_ID_1 = 0;
     private static final int FAKE_SUB_ID_2 = 1;
     private static final int FAKE_CARD_ID = 0;
+    private static final String FAKE_EID = "89049032000001000000031328322874";
     private static final String FAKE_MCC_MNC_1 = "123456";
     private static final String FAKE_MCC_MNC_2 = "456789";
 
@@ -488,6 +489,8 @@
     @SmallTest
     public void testUpdateEmbeddedSubscriptions_listSuccess() throws Exception {
         when(mEuiccManager.isEnabled()).thenReturn(true);
+        when(mEuiccManager.createForCardId(anyInt())).thenReturn(mEuiccManager);
+        when(mEuiccManager.getEid()).thenReturn(FAKE_EID);
 
         EuiccProfileInfo[] euiccProfiles = new EuiccProfileInfo[] {
                 new EuiccProfileInfo("1", null /* accessRules */, null /* nickname */),