Reuse the active esim slot

If there is the active esim slot in SS mode, the settings should
reuse it and does not change the sim slot mapping.

Bug: 229803628
Test: manually test.
Change-Id: I6daa38f54abfaf67c7640d9dc8be0da02eb59554
Merged-In: I6daa38f54abfaf67c7640d9dc8be0da02eb59554
diff --git a/src/com/android/settings/network/SwitchToEuiccSubscriptionSidecar.java b/src/com/android/settings/network/SwitchToEuiccSubscriptionSidecar.java
index 0b39d6a..935e235 100644
--- a/src/com/android/settings/network/SwitchToEuiccSubscriptionSidecar.java
+++ b/src/com/android/settings/network/SwitchToEuiccSubscriptionSidecar.java
@@ -21,12 +21,14 @@
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
 import android.telephony.UiccCardInfo;
+import android.telephony.UiccSlotMapping;
 import android.telephony.euicc.EuiccManager;
 import android.util.Log;
 
 import com.android.settings.SidecarFragment;
 import com.android.settings.network.telephony.EuiccOperationSidecar;
 
+import java.util.Collection;
 import java.util.Comparator;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -94,7 +96,7 @@
 
         // To check whether the esim slot's port is active. If yes, skip setSlotMapping. If no,
         // set this slot+port into setSimSlotMapping.
-        mPort = (port < 0) ? getTargetPortId(removedSubInfo) : port;
+        mPort = (port < 0) ? getTargetPortId(targetSlot, removedSubInfo) : port;
         mRemovedSubInfo = removedSubInfo;
         Log.d(TAG,
                 String.format("set esim into the SubId%d Slot%d:Port%d",
@@ -114,12 +116,24 @@
         }
     }
 
-    private int getTargetPortId(SubscriptionInfo removedSubInfo) {
-        if (!mTelephonyManager.isMultiSimEnabled() || !isMultipleEnabledProfilesSupported()) {
-            // In the 'SS mode' or 'DSDS+no MEP', the port is 0.
+    private int getTargetPortId(int physicalEsimSlotIndex, SubscriptionInfo removedSubInfo) {
+        if (!isMultipleEnabledProfilesSupported()) {
+            Log.d(TAG, "The device is no MEP, port is 0");
             return 0;
         }
 
+        if (!mTelephonyManager.isMultiSimEnabled()) {
+            // In the 'SS mode'
+            // If there is the esim slot is active, the port is from the current esim slot.
+            // If there is no esim slot in device, then the esim's port is 0.
+            Collection<UiccSlotMapping> uiccSlotMappings = mTelephonyManager.getSimSlotMapping();
+            Log.d(TAG, "In SS mode, the UiccSlotMapping: " + uiccSlotMappings);
+            return uiccSlotMappings.stream()
+                    .filter(i -> i.getPhysicalSlotIndex() == physicalEsimSlotIndex)
+                    .mapToInt(i -> i.getPortIndex())
+                    .findFirst().orElse(0);
+        }
+
         // In the 'DSDS+MEP', if the removedSubInfo is esim, then the port is
         // removedSubInfo's port.
         if (removedSubInfo != null && removedSubInfo.isEmbedded()) {
diff --git a/src/com/android/settings/network/UiccSlotUtil.java b/src/com/android/settings/network/UiccSlotUtil.java
index c27621a..c9c52cb 100644
--- a/src/com/android/settings/network/UiccSlotUtil.java
+++ b/src/com/android/settings/network/UiccSlotUtil.java
@@ -282,6 +282,21 @@
     // 1. pSIM's logical slots always is [RIL 0].
     // 2. assign the new active port to the same stack that will be de-activated
     //    For example: mode#3->mode#4
+    // 3. Add an eSIM carrier or enable eSIM carrier. The cases are at the below.
+    //    1) 1   => 2 / 2.1 / 3 / 3.1
+    //    2) 2   => 1 / 3 / 3.2
+    //    3) 2.1 => 3.1 / 4
+    //    4) 3   => 4
+    //    5) 3.1 => 3.2
+    //    Note:
+    //        1) 2 <=> 2.1  blocked by LPA (reason: existing active port in SS so just re-use)
+    //        2) 3 <=> 3.1 blocked by LPA (reason: if pSIM+an active port, re-use the active port)
+    // 4. pSIM insertion or enabling
+    //     1) 2   => 1 / 3
+    //     2) 2.1 => 1 / 3.1
+    //     3) 3.2 => 3 / 3.1
+    //     4) 4   => 3 / 3.1
+
 
     @VisibleForTesting
     static Collection<UiccSlotMapping> prepareUiccSlotMappings(