Choose active SIM on MSIM devices when making emergency calls.

We previously always chose the default Phone object for emergency calls, which
forced us to try to dial with the SIM in slot1 always. When the only SIM
is in slot 2, this caused the call not to go through.

Fixed to choose the SIM that was given to us and if that fails we move
on to the first active SIM and if that fails, *then* we opt for the
default phone.

Bug: 18746266
Change-Id: Ia005c1b2e9d1a08e9ab119f799552c97316f0853
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index 946ee3e..1a2ef53 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -393,10 +393,6 @@
     }
 
     private Phone getPhoneForAccount(PhoneAccountHandle accountHandle, boolean isEmergency) {
-        if (isEmergency) {
-            return PhoneFactory.getDefaultPhone();
-        }
-
         if (Objects.equals(mExpectedComponentName, accountHandle.getComponentName())) {
             if (accountHandle.getId() != null) {
                 try {
@@ -408,9 +404,53 @@
                 }
             }
         }
+
+        if (isEmergency) {
+            // If this is an emergency number and we've been asked to dial it using a PhoneAccount
+            // which does not exist, then default to whatever subscription is available currently.
+            return getFirstPhoneForEmergencyCall();
+        }
+
         return null;
     }
 
+    private Phone getFirstPhoneForEmergencyCall() {
+        Phone selectPhone = null;
+        for (int i = 0; i < TelephonyManager.getDefault().getSimCount(); i++) {
+            int[] subIds = SubscriptionController.getInstance().getSubIdUsingSlotId(i);
+            if (subIds.length == 0)
+                continue;
+
+            int phoneId = SubscriptionController.getInstance().getPhoneId(subIds[0]);
+            Phone phone = PhoneFactory.getPhone(phoneId);
+            if (phone == null)
+                continue;
+
+            if (ServiceState.STATE_IN_SERVICE == phone.getServiceState().getState()) {
+                // the slot is radio on & state is in service
+                Log.d(this, "pickBestPhoneForEmergencyCall, radio on & in service, slotId:" + i);
+                return phone;
+            } else if (ServiceState.STATE_POWER_OFF != phone.getServiceState().getState()) {
+                // the slot is radio on & with SIM card inserted.
+                if (TelephonyManager.getDefault().hasIccCard(i)) {
+                    Log.d(this, "pickBestPhoneForEmergencyCall," +
+                            "radio on and SIM card inserted, slotId:" + i);
+                    selectPhone = phone;
+                } else if (selectPhone == null) {
+                    Log.d(this, "pickBestPhoneForEmergencyCall, radio on, slotId:" + i);
+                    selectPhone = phone;
+                }
+            }
+        }
+
+        if (selectPhone == null) {
+            Log.d(this, "pickBestPhoneForEmergencyCall, return default phone");
+            selectPhone = PhoneFactory.getDefaultPhone();
+        }
+
+        return selectPhone;
+    }
+
     /**
      * Determines if the connection should allow mute.
      *