Account for User's Voice SIM preference for E911

Currently, we do not take into account a user's Voice SIM preference
when making an emergency call. The CreateConnectionProcessor now first
trys to use the Voice SIM preference of the user and backs up to other
SIM accounts if that account fails.

Bug: 27059146
Change-Id: Ic64ecdd23fe081f9660820e5659d314f2c9b9902
diff --git a/src/com/android/server/telecom/CreateConnectionProcessor.java b/src/com/android/server/telecom/CreateConnectionProcessor.java
index 7f9bbab..c89a896 100644
--- a/src/com/android/server/telecom/CreateConnectionProcessor.java
+++ b/src/com/android/server/telecom/CreateConnectionProcessor.java
@@ -127,7 +127,7 @@
                     mCall.getTargetPhoneAccount(), mCall.getTargetPhoneAccount()));
         }
         adjustAttemptsForConnectionManager();
-        adjustAttemptsForEmergency();
+        adjustAttemptsForEmergency(mCall.getTargetPhoneAccount());
         mAttemptRecordIterator = mAttemptRecords.iterator();
         attemptNextPhoneAccount();
     }
@@ -288,7 +288,7 @@
 
     // If we are possibly attempting to call a local emergency number, ensure that the
     // plain PSTN connection services are listed, and nothing else.
-    private void adjustAttemptsForEmergency() {
+    private void adjustAttemptsForEmergency(PhoneAccountHandle preferredPAH) {
         if (mCall.isEmergencyCall()) {
             Log.i(this, "Emergency number detected");
             mAttemptRecords.clear();
@@ -307,16 +307,29 @@
                 allAccounts.add(TelephonyUtil.getDefaultEmergencyPhoneAccount());
             }
 
-            // First, add SIM phone accounts which can place emergency calls.
+            // First, possibly add the SIM phone account that the user prefers
+            PhoneAccount preferredPA = mPhoneAccountRegistrar.getPhoneAccountUnchecked(
+                    preferredPAH);
+            if (preferredPA != null &&
+                    preferredPA.hasCapabilities(PhoneAccount.CAPABILITY_PLACE_EMERGENCY_CALLS) &&
+                    preferredPA.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
+                Log.i(this, "Will try PSTN account %s for emergency",
+                        preferredPA.getAccountHandle());
+                mAttemptRecords.add(new CallAttemptRecord(preferredPAH, preferredPAH));
+            }
+
+            // Next, add all SIM phone accounts which can place emergency calls.
+            TelephonyUtil.sortSimPhoneAccounts(mContext, allAccounts);
             for (PhoneAccount phoneAccount : allAccounts) {
                 if (phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_PLACE_EMERGENCY_CALLS) &&
                         phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
-                    Log.i(this, "Will try PSTN account %s for emergency",
-                            phoneAccount.getAccountHandle());
-                    mAttemptRecords.add(
-                            new CallAttemptRecord(
-                                    phoneAccount.getAccountHandle(),
-                                    phoneAccount.getAccountHandle()));
+                    PhoneAccountHandle phoneAccountHandle = phoneAccount.getAccountHandle();
+                    // Don't add the preferred account since it has already been added previously.
+                    if (!phoneAccountHandle.equals(preferredPAH)) {
+                        Log.i(this, "Will try PSTN account %s for emergency", phoneAccountHandle);
+                        mAttemptRecords.add(new CallAttemptRecord(phoneAccountHandle,
+                                phoneAccountHandle));
+                    }
                 }
             }
 
@@ -419,7 +432,7 @@
     public void handleCreateConnectionFailure(DisconnectCause errorDisconnectCause) {
         // Failure of some sort; record the reasons for failure and try again if possible
         Log.d(CreateConnectionProcessor.this, "Connection failed: (%s)", errorDisconnectCause);
-        if(shouldFailCallIfConnectionManagerFails(errorDisconnectCause)){
+        if (shouldFailCallIfConnectionManagerFails(errorDisconnectCause)) {
             notifyCallConnectionFailure(errorDisconnectCause);
             return;
         }