Consolidate subscription ID acquisition.

Update TelephonyConnectionService to use
PhoneUtil.getSubIdForPhoneAccount instead of duplicating the code.

Change-Id: I7fbbc406a45ccea0e7a544d311878697562273b8
diff --git a/src/com/android/phone/PhoneUtils.java b/src/com/android/phone/PhoneUtils.java
index 761d11b..7da2e1f 100644
--- a/src/com/android/phone/PhoneUtils.java
+++ b/src/com/android/phone/PhoneUtils.java
@@ -2462,14 +2462,18 @@
     }
 
     public static int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
-        if (phoneAccount != null) {
-            PhoneAccountHandle handle = phoneAccount.getAccountHandle();
-            if (handle != null && handle.getComponentName().equals(getPstnConnectionServiceName()) &&
-                    phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
-                String id = handle.getId();
-                if (TextUtils.isDigitsOnly(id)) {
-                    return Integer.parseInt(id);
-                }
+        if (phoneAccount != null
+                && phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
+            return getSubIdForPhoneAccountHandle(phoneAccount.getAccountHandle());
+        }
+        return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+    }
+
+    public static int getSubIdForPhoneAccountHandle(PhoneAccountHandle handle) {
+        if (handle != null && handle.getComponentName().equals(getPstnConnectionServiceName())) {
+            String id = handle.getId();
+            if (TextUtils.isDigitsOnly(id)) {
+                return Integer.parseInt(id);
             }
         }
         return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index 22f21ed..c4eb2e9 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -26,6 +26,7 @@
 import android.telecom.PhoneAccountHandle;
 import android.telephony.PhoneNumberUtils;
 import android.telephony.ServiceState;
+import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 
@@ -38,6 +39,7 @@
 import com.android.internal.telephony.SubscriptionController;
 import com.android.internal.telephony.cdma.CDMAPhone;
 import com.android.phone.MMIDialogActivity;
+import com.android.phone.PhoneUtils;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -393,22 +395,14 @@
     }
 
     private Phone getPhoneForAccount(PhoneAccountHandle accountHandle, boolean isEmergency) {
-        if (Objects.equals(mExpectedComponentName, accountHandle.getComponentName())) {
-            if (accountHandle.getId() != null) {
-                try {
-                    int phoneId = SubscriptionController.getInstance().getPhoneId(
-                            Integer.parseInt(accountHandle.getId()));
-                    return PhoneFactory.getPhone(phoneId);
-                } catch (NumberFormatException e) {
-                    Log.w(this, "Could not get subId from account: " + accountHandle.getId());
-                }
-            }
+        if (isEmergency) {
+            return PhoneFactory.getDefaultPhone();
         }
 
-        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();
+        int subId = PhoneUtils.getSubIdForPhoneAccountHandle(accountHandle);
+        if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
+            int phoneId = SubscriptionController.getInstance().getPhoneId(subId);
+            return PhoneFactory.getPhone(phoneId);
         }
 
         return null;