Merge "Make routing type an enum instead of boolean"
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 27d1de3..7b5cf81 100755
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -17,6 +17,7 @@
 package com.android.phone;
 
 import static android.content.pm.PackageManager.PERMISSION_GRANTED;
+import static android.telephony.TelephonyManager.HAL_SERVICE_RADIO;
 
 import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_CDMA;
 import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_GSM;
@@ -9779,12 +9780,22 @@
 
     /**
      * Get the IRadio HAL Version
+     * @deprecated use getHalVersion instead
      */
+    @Deprecated
     @Override
     public int getRadioHalVersion() {
+        return getHalVersion(HAL_SERVICE_RADIO);
+    }
+
+    /**
+     * Get the HAL Version of a specific service
+     */
+    @Override
+    public int getHalVersion(int service) {
         Phone phone = getDefaultPhone();
         if (phone == null) return -1;
-        HalVersion hv = phone.getHalVersion();
+        HalVersion hv = phone.getHalVersion(service);
         if (hv.equals(HalVersion.UNKNOWN)) return -1;
         return hv.major * 100 + hv.minor;
     }
diff --git a/src/com/android/phone/slice/SlicePurchaseController.java b/src/com/android/phone/slice/SlicePurchaseController.java
index 710f27d..5c3b63e 100644
--- a/src/com/android/phone/slice/SlicePurchaseController.java
+++ b/src/com/android/phone/slice/SlicePurchaseController.java
@@ -20,6 +20,7 @@
 import static android.telephony.TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_ALREADY_PURCHASED;
 import static android.telephony.TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_CARRIER_DISABLED;
 import static android.telephony.TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_ENTITLEMENT_CHECK_FAILED;
+import static android.telephony.TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUBSCRIPTION;
 
 import android.annotation.IntDef;
 import android.annotation.NonNull;
@@ -157,8 +158,9 @@
     private static final String ACTION_SLICE_PURCHASE_APP_RESPONSE_REQUEST_FAILED =
             "com.android.phone.slice.action.SLICE_PURCHASE_APP_RESPONSE_REQUEST_FAILED";
     /** Action indicating the purchase request was not made on the default data subscription. */
-    private static final String ACTION_SLICE_PURCHASE_APP_RESPONSE_NOT_DEFAULT_DATA_SUB =
-            "com.android.phone.slice.action.SLICE_PURCHASE_APP_RESPONSE_NOT_DEFAULT_DATA_SUB";
+    private static final String ACTION_SLICE_PURCHASE_APP_RESPONSE_NOT_DEFAULT_DATA_SUBSCRIPTION =
+            "com.android.phone.slice.action."
+                    + "SLICE_PURCHASE_APP_RESPONSE_NOT_DEFAULT_DATA_SUBSCRIPTION";
     /** Action indicating the purchase request was successful. */
     private static final String ACTION_SLICE_PURCHASE_APP_RESPONSE_SUCCESS =
             "com.android.phone.slice.action.SLICE_PURCHASE_APP_RESPONSE_SUCCESS";
@@ -214,10 +216,10 @@
      * Extra for the not-default data subscription ID PendingIntent that the slice purchase
      * application can send as a response if the premium capability purchase request failed because
      * it was not requested on the default data subscription.
-     * Sends {@link #ACTION_SLICE_PURCHASE_APP_RESPONSE_NOT_DEFAULT_DATA_SUB}.
+     * Sends {@link #ACTION_SLICE_PURCHASE_APP_RESPONSE_NOT_DEFAULT_DATA_SUBSCRIPTION}.
      */
-    public static final String EXTRA_INTENT_NOT_DEFAULT_DATA_SUB =
-            "com.android.phone.slice.extra.INTENT_NOT_DEFAULT_DATA_SUB";
+    public static final String EXTRA_INTENT_NOT_DEFAULT_DATA_SUBSCRIPTION =
+            "com.android.phone.slice.extra.INTENT_NOT_DEFAULT_DATA_SUBSCRIPTION";
     /**
      * Extra for the success PendingIntent that the slice purchase application can send as a
      * response if the premium capability purchase request was successful.
@@ -312,14 +314,13 @@
                             false);
                     break;
                 }
-                case ACTION_SLICE_PURCHASE_APP_RESPONSE_NOT_DEFAULT_DATA_SUB: {
+                case ACTION_SLICE_PURCHASE_APP_RESPONSE_NOT_DEFAULT_DATA_SUBSCRIPTION: {
                     logd("Purchase premium capability request was not made on the default data "
                             + "subscription for capability: "
                             + TelephonyManager.convertPremiumCapabilityToString(capability));
                     SlicePurchaseController.getInstance(phoneId)
                             .handlePurchaseResult(capability,
-                            TelephonyManager
-                                    .PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUB,
+                            PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUBSCRIPTION,
                             false);
                     break;
                 }
@@ -479,7 +480,7 @@
         }
         if (!isDefaultDataSub()) {
             sendPurchaseResult(capability,
-                    TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUB,
+                    PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUBSCRIPTION,
                     onComplete);
             return;
         }
@@ -629,8 +630,9 @@
                 ACTION_SLICE_PURCHASE_APP_RESPONSE_CARRIER_ERROR, capability, true));
         intent.putExtra(EXTRA_INTENT_REQUEST_FAILED, createPendingIntent(
                 ACTION_SLICE_PURCHASE_APP_RESPONSE_REQUEST_FAILED, capability, false));
-        intent.putExtra(EXTRA_INTENT_NOT_DEFAULT_DATA_SUB, createPendingIntent(
-                ACTION_SLICE_PURCHASE_APP_RESPONSE_NOT_DEFAULT_DATA_SUB, capability, false));
+        intent.putExtra(EXTRA_INTENT_NOT_DEFAULT_DATA_SUBSCRIPTION, createPendingIntent(
+                ACTION_SLICE_PURCHASE_APP_RESPONSE_NOT_DEFAULT_DATA_SUBSCRIPTION, capability,
+                false));
         intent.putExtra(EXTRA_INTENT_SUCCESS, createPendingIntent(
                 ACTION_SLICE_PURCHASE_APP_RESPONSE_SUCCESS, capability, true));
         logd("Broadcasting start intent to SlicePurchaseBroadcastReceiver.");
@@ -643,7 +645,7 @@
         filter.addAction(ACTION_SLICE_PURCHASE_APP_RESPONSE_CANCELED);
         filter.addAction(ACTION_SLICE_PURCHASE_APP_RESPONSE_CARRIER_ERROR);
         filter.addAction(ACTION_SLICE_PURCHASE_APP_RESPONSE_REQUEST_FAILED);
-        filter.addAction(ACTION_SLICE_PURCHASE_APP_RESPONSE_NOT_DEFAULT_DATA_SUB);
+        filter.addAction(ACTION_SLICE_PURCHASE_APP_RESPONSE_NOT_DEFAULT_DATA_SUBSCRIPTION);
         filter.addAction(ACTION_SLICE_PURCHASE_APP_RESPONSE_SUCCESS);
         mPhone.getContext().registerReceiver(
                 mSlicePurchaseControllerBroadcastReceivers.get(capability), filter);
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index da918ad..cb1277f 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -16,6 +16,8 @@
 
 package com.android.services.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_VOICE;
+
 import android.annotation.NonNull;
 import android.app.AlertDialog;
 import android.app.Dialog;
@@ -211,6 +213,7 @@
     @VisibleForTesting
     public interface SubscriptionManagerProxy {
         int getDefaultVoicePhoneId();
+        int getDefaultDataPhoneId();
         int getSimStateForSlotIdx(int slotId);
         int getPhoneId(int subId);
     }
@@ -222,6 +225,11 @@
         }
 
         @Override
+        public int getDefaultDataPhoneId() {
+            return getPhoneId(SubscriptionManager.getDefaultDataSubscriptionId());
+        }
+
+        @Override
         public int getSimStateForSlotIdx(int slotId) {
             return SubscriptionManager.getSimStateForSlotIndex(slotId);
         }
@@ -823,7 +831,8 @@
                     // an isTesting parameter. For HAL 1.4+, do not wait for IN_SERVICE, this will
                     // be handled at the RIL/vendor level by emergencyDial(...).
                     boolean waitForInServiceToDialEmergency = isTestEmergencyNumber
-                            && phone.getHalVersion().less(RIL.RADIO_HAL_VERSION_1_4);
+                            && phone.getHalVersion(HAL_SERVICE_VOICE)
+                            .less(RIL.RADIO_HAL_VERSION_1_4);
                     if (isEmergencyNumber && !waitForInServiceToDialEmergency) {
                         // We currently only look to make sure that the radio is on before dialing.
                         // We should be able to make emergency calls at any time after the radio has
@@ -2165,39 +2174,57 @@
     /**
      * Retrieves the most sensible Phone to use for an emergency call using the following Priority
      *  list (for multi-SIM devices):
-     *  1) The User's SIM preference for Voice calling
-     *  2) The First Phone that is currently IN_SERVICE or is available for emergency calling
-     *  3) Prioritize phones that have the dialed emergency number as part of their emergency
+     *  1) The Phone that is in emergency SMS mode
+     *  2) The phone based on User's SIM preference of Voice calling or Data in order
+     *  3) The First Phone that is currently IN_SERVICE or is available for emergency calling
+     *  4) Prioritize phones that have the dialed emergency number as part of their emergency
      *     number list
-     *  4) If there is a PUK locked SIM, compare the SIMs that are not PUK locked. If all the SIMs
-     *     are locked, skip to condition 5).
-     *  5) The Phone with more Capabilities.
-     *  6) The First Phone that has a SIM card in it (Starting from Slot 0...N)
-     *  7) The Default Phone (Currently set as Slot 0)
+     *  5) If there is a PUK locked SIM, compare the SIMs that are not PUK locked. If all the SIMs
+     *     are locked, skip to condition 6).
+     *  6) The Phone with more Capabilities.
+     *  7) The First Phone that has a SIM card in it (Starting from Slot 0...N)
+     *  8) The Default Phone (Currently set as Slot 0)
      */
     @VisibleForTesting
+    @NonNull
     public Phone getFirstPhoneForEmergencyCall(List<Phone> phonesWithEmergencyNumber) {
-        // 1)
+        int phoneCount = mTelephonyManagerProxy.getPhoneCount();
+        for (int i = 0; i < phoneCount; i++) {
+            Phone phone = mPhoneFactoryProxy.getPhone(i);
+            // 1)
+            if (phone != null && phone.isInEmergencySmsMode()) {
+                if (isAvailableForEmergencyCalls(phone)) {
+                    if (phonesWithEmergencyNumber == null
+                            || phonesWithEmergencyNumber.contains(phone)) {
+                        return phone;
+                    }
+                }
+            }
+        }
+
+        // 2)
         int phoneId = mSubscriptionManagerProxy.getDefaultVoicePhoneId();
+        if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
+            phoneId = mSubscriptionManagerProxy.getDefaultDataPhoneId();
+        }
         if (phoneId != SubscriptionManager.INVALID_PHONE_INDEX) {
-            Phone defaultPhone = mPhoneFactoryProxy.getPhone(phoneId);
-            if (defaultPhone != null && isAvailableForEmergencyCalls(defaultPhone)) {
+            Phone selectedPhone = mPhoneFactoryProxy.getPhone(phoneId);
+            if (selectedPhone != null && isAvailableForEmergencyCalls(selectedPhone)) {
                 if (phonesWithEmergencyNumber == null
-                        || phonesWithEmergencyNumber.contains(defaultPhone)) {
-                    return defaultPhone;
+                        || phonesWithEmergencyNumber.contains(selectedPhone)) {
+                    return selectedPhone;
                 }
             }
         }
 
         Phone firstPhoneWithSim = null;
-        int phoneCount = mTelephonyManagerProxy.getPhoneCount();
         List<SlotStatus> phoneSlotStatus = new ArrayList<>(phoneCount);
         for (int i = 0; i < phoneCount; i++) {
             Phone phone = mPhoneFactoryProxy.getPhone(i);
             if (phone == null) {
                 continue;
             }
-            // 2)
+            // 3)
             if (isAvailableForEmergencyCalls(phone)) {
                 if (phonesWithEmergencyNumber == null
                         || phonesWithEmergencyNumber.contains(phone)) {
@@ -2207,7 +2234,7 @@
                     return phone;
                 }
             }
-            // 5)
+            // 6)
             // Store the RAF Capabilities for sorting later.
             int radioAccessFamily = phone.getRadioAccessFamily();
             SlotStatus status = new SlotStatus(i, radioAccessFamily, phone.getSubId());
@@ -2215,7 +2242,7 @@
             Log.i(this, "getFirstPhoneForEmergencyCall, RAF:" +
                 Integer.toHexString(radioAccessFamily) + " saved for Phone Id:" + i + " subId:"
                 + phone.getSubId());
-            // 4)
+            // 5)
             // Report Slot's PIN/PUK lock status for sorting later.
             int simState = mSubscriptionManagerProxy.getSimStateForSlotIdx(i);
             // Record SimState.
@@ -2225,7 +2252,7 @@
                 status.isLocked = true;
             }
 
-            // 3) Store if the Phone has the corresponding emergency number
+            // 4) Store if the Phone has the corresponding emergency number
             if (phonesWithEmergencyNumber != null) {
                 for (Phone phoneWithEmergencyNumber : phonesWithEmergencyNumber) {
                     if (phoneWithEmergencyNumber != null
@@ -2234,7 +2261,7 @@
                     }
                 }
             }
-            // 6)
+            // 7)
             if (firstPhoneWithSim == null &&
                 (phone.getSubId() != SubscriptionManager.INVALID_SIM_SLOT_INDEX)) {
                 // The slot has a SIM card inserted (and an active subscription), but is not in
@@ -2246,16 +2273,21 @@
                     firstPhoneWithSim.getPhoneId());
             }
         }
-        // 7)
+        // 8)
         if (firstPhoneWithSim == null && phoneSlotStatus.isEmpty()) {
-            if (phonesWithEmergencyNumber == null || phonesWithEmergencyNumber.isEmpty()) {
-                // No Phones available, get the default
-                Log.i(this, "getFirstPhoneForEmergencyCall, return default phone");
-                return  mPhoneFactoryProxy.getDefaultPhone();
+            if (phonesWithEmergencyNumber != null) {
+                for (Phone phoneWithEmergencyNumber : phonesWithEmergencyNumber) {
+                    if (phoneWithEmergencyNumber != null) {
+                        return phoneWithEmergencyNumber;
+                    }
+                }
             }
-            return phonesWithEmergencyNumber.get(0);
+
+            // No Phones available, get the default
+            Log.i(this, "getFirstPhoneForEmergencyCall, return default phone");
+            return  mPhoneFactoryProxy.getDefaultPhone();
         } else {
-            // 5)
+            // 6)
             final int defaultPhoneId = mPhoneFactoryProxy.getDefaultPhone().getPhoneId();
             final Phone firstOccupiedSlot = firstPhoneWithSim;
             if (!phoneSlotStatus.isEmpty()) {
@@ -2318,7 +2350,7 @@
                         "with highest capability");
                 return mPhoneFactoryProxy.getPhone(mostCapablePhoneId);
             } else {
-                // 6)
+                // 7)
                 return firstPhoneWithSim;
             }
         }
diff --git a/testapps/TestSliceApp/app/src/main/java/com/google/android/sample/testsliceapp/PrioritizeLatency.java b/testapps/TestSliceApp/app/src/main/java/com/google/android/sample/testsliceapp/PrioritizeLatency.java
index 6ad6ebd..9dc4732 100644
--- a/testapps/TestSliceApp/app/src/main/java/com/google/android/sample/testsliceapp/PrioritizeLatency.java
+++ b/testapps/TestSliceApp/app/src/main/java/com/google/android/sample/testsliceapp/PrioritizeLatency.java
@@ -22,7 +22,7 @@
 import static android.telephony.TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_ENTITLEMENT_CHECK_FAILED;
 import static android.telephony.TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_FEATURE_NOT_SUPPORTED;
 import static android.telephony.TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NETWORK_NOT_AVAILABLE;
-import static android.telephony.TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUB;
+import static android.telephony.TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUBSCRIPTION;
 import static android.telephony.TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_OVERRIDDEN;
 import static android.telephony.TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_PENDING_NETWORK_SETUP;
 import static android.telephony.TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_SUCCESS;
@@ -272,8 +272,8 @@
                 return "Network not available";
             case PURCHASE_PREMIUM_CAPABILITY_RESULT_ENTITLEMENT_CHECK_FAILED:
                 return "Entitlement check failed";
-            case PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUB:
-                return "No default data";
+            case PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUBSCRIPTION:
+                return "Not default data subscription";
             case PURCHASE_PREMIUM_CAPABILITY_RESULT_PENDING_NETWORK_SETUP:
                 return "Pending network setup";
             default:
diff --git a/tests/src/com/android/phone/SlicePurchaseControllerTest.java b/tests/src/com/android/phone/SlicePurchaseControllerTest.java
index f61c66b..bfde6a4 100644
--- a/tests/src/com/android/phone/SlicePurchaseControllerTest.java
+++ b/tests/src/com/android/phone/SlicePurchaseControllerTest.java
@@ -182,14 +182,16 @@
         doReturn(mBundle).when(mCarrierConfigManager).getConfigForSubId(anyInt());
 
         tryPurchasePremiumCapability();
-        assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUB,
+        assertEquals(
+                TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUBSCRIPTION,
                 mResult);
 
         // retry on default data subscription
         doReturn(SubscriptionManager.getDefaultDataSubscriptionId()).when(mPhone).getSubId();
 
         tryPurchasePremiumCapability();
-        assertNotEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUB,
+        assertNotEquals(
+                TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUBSCRIPTION,
                 mResult);
     }
 
@@ -443,13 +445,14 @@
         sendValidPurchaseRequest();
 
         Intent intent = new Intent();
-        intent.setAction(
-                "com.android.phone.slice.action.SLICE_PURCHASE_APP_RESPONSE_NOT_DEFAULT_DATA_SUB");
+        intent.setAction("com.android.phone.slice.action."
+                + "SLICE_PURCHASE_APP_RESPONSE_NOT_DEFAULT_DATA_SUBSCRIPTION");
         intent.putExtra(SlicePurchaseController.EXTRA_PHONE_ID, PHONE_ID);
         intent.putExtra(SlicePurchaseController.EXTRA_PREMIUM_CAPABILITY,
                 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY);
         receiveSlicePurchaseResponse(intent);
-        assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUB,
+        assertEquals(
+                TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUBSCRIPTION,
                 mResult);
 
         // retry to verify no throttling
diff --git a/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java b/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
index f2209db..553dbc9 100644
--- a/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
+++ b/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
@@ -192,6 +192,54 @@
     /**
      * Prerequisites:
      * - MSIM Device, two slots with SIMs inserted
+     * - Slot 0 is IN_SERVICE, Slot 1 is OUT_OF_SERVICE (emergency calls only)
+     * - Slot 1 is in Emergency SMS Mode
+     *
+     * Result: getFirstPhoneForEmergencyCall returns the slot 1 phone
+     */
+    @Test
+    @SmallTest
+    public void testEmergencySmsModeSimEmergencyOnly() {
+        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_IN_SERVICE,
+                false /*isEmergencyOnly*/);
+        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
+                true /*isEmergencyOnly*/);
+        setDefaultPhone(slot0Phone);
+        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
+        setEmergencySmsMode(slot1Phone, true);
+
+        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
+
+        assertEquals(slot1Phone, resultPhone);
+    }
+
+    /**
+     * Prerequisites:
+     * - MSIM Device, two slots with SIMs inserted
+     * - Slot 0 is IN_SERVICE, Slot 1 is OUT_OF_SERVICE
+     * - Slot 1 is in Emergency SMS Mode
+     *
+     * Result: getFirstPhoneForEmergencyCall returns the slot 0 phone
+     */
+    @Test
+    @SmallTest
+    public void testEmergencySmsModeSimOutOfService() {
+        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_IN_SERVICE,
+                false /*isEmergencyOnly*/);
+        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
+                false /*isEmergencyOnly*/);
+        setDefaultPhone(slot0Phone);
+        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
+        setEmergencySmsMode(slot1Phone, true);
+
+        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
+
+        assertEquals(slot0Phone, resultPhone);
+    }
+
+    /**
+     * Prerequisites:
+     * - MSIM Device, two slots with SIMs inserted
      * - Users default Voice SIM choice is IN_SERVICE
      *
      * Result: getFirstPhoneForEmergencyCall returns the default Voice SIM choice.
@@ -214,6 +262,52 @@
     /**
      * Prerequisites:
      * - MSIM Device, two slots with SIMs inserted
+     * - Users default data SIM choice is OUT_OF_SERVICE (emergency calls only)
+     *
+     * Result: getFirstPhoneForEmergencyCall returns the default data SIM choice.
+     */
+    @Test
+    @SmallTest
+    public void testDefaultDataSimEmergencyOnly() {
+        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_IN_SERVICE,
+                false /*isEmergencyOnly*/);
+        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
+                true /*isEmergencyOnly*/);
+        setDefaultPhone(slot0Phone);
+        setupDeviceConfig(slot0Phone, slot1Phone, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        setDefaultDataPhoneId(SLOT_1_PHONE_ID);
+
+        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
+
+        assertEquals(slot1Phone, resultPhone);
+    }
+
+    /**
+     * Prerequisites:
+     * - MSIM Device, two slots with SIMs inserted
+     * - Users default data SIM choice is OUT_OF_SERVICE
+     *
+     * Result: getFirstPhoneForEmergencyCall does not return the default data SIM choice.
+     */
+    @Test
+    @SmallTest
+    public void testDefaultDataSimOutOfService() {
+        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_IN_SERVICE,
+                false /*isEmergencyOnly*/);
+        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
+                false /*isEmergencyOnly*/);
+        setDefaultPhone(slot0Phone);
+        setupDeviceConfig(slot0Phone, slot1Phone, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        setDefaultDataPhoneId(SLOT_1_PHONE_ID);
+
+        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
+
+        assertEquals(slot0Phone, resultPhone);
+    }
+
+    /**
+     * Prerequisites:
+     * - MSIM Device, two slots with SIMs inserted
      * - Slot 0 is OUT_OF_SERVICE, Slot 1 is OUT_OF_SERVICE (emergency calls only)
      *
      * Result: getFirstPhoneForEmergencyCall returns the slot 1 phone
@@ -1615,10 +1709,18 @@
         when(mPhoneFactoryProxy.getPhone(eq(SLOT_1_PHONE_ID))).thenReturn(slot1Phone);
     }
 
+    private void setDefaultDataPhoneId(int defaultDataPhoneId) {
+        when(mSubscriptionManagerProxy.getDefaultDataPhoneId()).thenReturn(defaultDataPhoneId);
+    }
+
     private void setPhoneRadioAccessFamily(Phone phone, int radioAccessFamily) {
         when(phone.getRadioAccessFamily()).thenReturn(radioAccessFamily);
     }
 
+    private void setEmergencySmsMode(Phone phone, boolean isInEmergencySmsMode) {
+        when(phone.isInEmergencySmsMode()).thenReturn(isInEmergencySmsMode);
+    }
+
     private void setPhoneSlotState(int slotId, int slotState) {
         when(mSubscriptionManagerProxy.getSimStateForSlotIdx(slotId)).thenReturn(slotState);
     }