Merge "[MEP] Inserting a pSIM while user has 2 esims, showing the MEP dialog"
diff --git a/src/com/android/settings/sim/receivers/SimSlotChangeHandler.java b/src/com/android/settings/sim/receivers/SimSlotChangeHandler.java
index 8c8964f..61241d0 100644
--- a/src/com/android/settings/sim/receivers/SimSlotChangeHandler.java
+++ b/src/com/android/settings/sim/receivers/SimSlotChangeHandler.java
@@ -26,12 +26,14 @@
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
+import android.telephony.UiccCardInfo;
 import android.telephony.UiccSlotInfo;
 import android.util.Log;
 
 import com.android.settings.network.SubscriptionUtil;
 import com.android.settings.network.UiccSlotUtil;
 import com.android.settings.network.UiccSlotsException;
+import com.android.settings.network.telephony.ToggleSubscriptionDialogActivity;
 import com.android.settings.sim.ChooseSimActivity;
 import com.android.settings.sim.DsdsDialogActivity;
 import com.android.settings.sim.SimActivationNotifier;
@@ -40,6 +42,7 @@
 
 import com.google.common.collect.ImmutableList;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -83,8 +86,8 @@
             throw new IllegalStateException("Cannot be called from main thread.");
         }
 
-        if (mTelMgr.getActiveModemCount() > 1) {
-            Log.i(TAG, "The device is already in DSDS mode. Do nothing.");
+        if (mTelMgr.getActiveModemCount() > 1 && !isMultipleEnabledProfilesSupported()) {
+            Log.i(TAG, "The device is already in DSDS mode and no MEP. Do nothing.");
             return;
         }
 
@@ -96,17 +99,30 @@
 
         int lastRemovableSlotState = getLastRemovableSimSlotState(mContext);
         int currentRemovableSlotState = removableSlotInfo.getCardStateInfo();
+        boolean isRemovableSimInserted =
+                lastRemovableSlotState == UiccSlotInfo.CARD_STATE_INFO_ABSENT
+                        && currentRemovableSlotState == UiccSlotInfo.CARD_STATE_INFO_PRESENT;
+        boolean isRemovableSimRemoved =
+                lastRemovableSlotState == UiccSlotInfo.CARD_STATE_INFO_PRESENT
+                        && currentRemovableSlotState == UiccSlotInfo.CARD_STATE_INFO_ABSENT;
 
         // Sets the current removable slot state.
         setRemovableSimSlotState(mContext, currentRemovableSlotState);
 
-        if (lastRemovableSlotState == UiccSlotInfo.CARD_STATE_INFO_ABSENT
-                && currentRemovableSlotState == UiccSlotInfo.CARD_STATE_INFO_PRESENT) {
+        if (mTelMgr.getActiveModemCount() > 1 && isMultipleEnabledProfilesSupported()) {
+            if(!isRemovableSimInserted) {
+                Log.i(TAG, "Removable Sim is not inserted in DSDS mode and MEP. Do nothing.");
+                return;
+            }
+            handleRemovableSimInsertUnderDsdsMep(removableSlotInfo);
+            return;
+        }
+
+        if (isRemovableSimInserted) {
             handleSimInsert(removableSlotInfo);
             return;
         }
-        if (lastRemovableSlotState == UiccSlotInfo.CARD_STATE_INFO_PRESENT
-                && currentRemovableSlotState == UiccSlotInfo.CARD_STATE_INFO_ABSENT) {
+        if (isRemovableSimRemoved) {
             handleSimRemove(removableSlotInfo);
             return;
         }
@@ -232,6 +248,23 @@
         startChooseSimActivity(false);
     }
 
+    private void handleRemovableSimInsertUnderDsdsMep(UiccSlotInfo removableSlotInfo) {
+        Log.i(TAG, "Handle Removable SIM inserted under DSDS+Mep.");
+
+        if (removableSlotInfo.getPorts().stream().findFirst().get().isActive()) {
+            Log.i(TAG, "The removable slot is already active. Do nothing. removableSlotInfo: "
+                    + removableSlotInfo);
+            return;
+        }
+
+        List<SubscriptionInfo> subscriptionInfos = getAvailableRemovableSubscription();
+        if (subscriptionInfos == null && subscriptionInfos.get(0) != null) {
+            Log.e(TAG, "Unable to find the removable subscriptionInfo. Do nothing.");
+            return;
+        }
+        startSimConfirmDialogActivity(subscriptionInfos.get(0).getSubscriptionId());
+    }
+
     private int getLastRemovableSimSlotState(Context context) {
         final SharedPreferences prefs = context.getSharedPreferences(EUICC_PREFS, MODE_PRIVATE);
         return prefs.getInt(KEY_REMOVABLE_SLOT_STATE, UiccSlotInfo.CARD_STATE_INFO_ABSENT);
@@ -261,7 +294,6 @@
         }
         for (UiccSlotInfo slotInfo : slotInfos) {
             if (slotInfo != null && slotInfo.isRemovable()) {
-
                 return slotInfo;
             }
         }
@@ -297,6 +329,16 @@
                         .collect(Collectors.toList()));
     }
 
+    protected List<SubscriptionInfo> getAvailableRemovableSubscription() {
+        List<SubscriptionInfo> subList = new ArrayList<>();
+        for (SubscriptionInfo info : SubscriptionUtil.getAvailableSubscriptions(mContext)) {
+            if (!info.isEmbedded()) {
+                subList.add(info);
+            }
+        }
+        return subList;
+    }
+
     private void startChooseSimActivity(boolean psimInserted) {
         Intent intent = ChooseSimActivity.getIntent(mContext);
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
@@ -317,5 +359,25 @@
         mContext.startActivity(intent);
     }
 
+    private void startSimConfirmDialogActivity(int subId) {
+        if (!SubscriptionManager.isUsableSubscriptionId(subId)) {
+            Log.i(TAG, "Unable to enable subscription due to invalid subscription ID.");
+            return;
+        }
+        Intent intent = ToggleSubscriptionDialogActivity.getIntent(mContext, subId, true);
+        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        mContext.startActivity(intent);
+    }
+
+    private boolean isMultipleEnabledProfilesSupported() {
+        List<UiccCardInfo> cardInfos = mTelMgr.getUiccCardsInfo();
+        if (cardInfos == null) {
+            Log.w(TAG, "UICC cards info list is empty.");
+            return false;
+        }
+        return cardInfos.stream().anyMatch(
+                cardInfo -> cardInfo.isMultipleEnabledProfilesSupported());
+    }
+
     private SimSlotChangeHandler() {}
 }