Add phoneAccount->subID conversion method.

Adds method to convert from a phone account to sub ID so that other apps
can use the method instead of implementing the logic themselves.

Change-Id: Icfd8337f7aeca740076afa82db786a905e03ae9c
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 9eb0b0b..246c376 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -35,6 +35,7 @@
 import android.os.UserHandle;
 import android.preference.PreferenceManager;
 import android.provider.Settings;
+import android.telecom.PhoneAccount;
 import android.telephony.CellInfo;
 import android.telephony.IccOpenLogicalChannelResponse;
 import android.telephony.NeighboringCellInfo;
@@ -2256,7 +2257,13 @@
      * {@hide}
      * Returns the IMS Registration Status
      */
+    @Override
     public boolean isImsRegistered() {
         return mPhone.isImsRegistered();
     }
+
+    @Override
+    public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
+        return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
+    }
 }
diff --git a/src/com/android/phone/PhoneUtils.java b/src/com/android/phone/PhoneUtils.java
index 6b34208..761d11b 100644
--- a/src/com/android/phone/PhoneUtils.java
+++ b/src/com/android/phone/PhoneUtils.java
@@ -36,6 +36,7 @@
 import android.telecom.PhoneAccountHandle;
 import android.telecom.VideoProfile;
 import android.telephony.PhoneNumberUtils;
+import android.telephony.SubscriptionManager;
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.ContextThemeWrapper;
@@ -2453,14 +2454,31 @@
 
     public static PhoneAccountHandle makePstnPhoneAccountHandleWithPrefix(
             Phone phone, String prefix, boolean isEmergency) {
-        ComponentName pstnConnectionServiceName =
-                new ComponentName(phone.getContext(), TelephonyConnectionService.class);
+        ComponentName pstnConnectionServiceName = getPstnConnectionServiceName();
         // TODO: Should use some sort of special hidden flag to decorate this account as
         // an emergency-only account
         String id = isEmergency ? "E" : prefix + String.valueOf(phone.getSubId());
         return new PhoneAccountHandle(pstnConnectionServiceName, id);
     }
 
+    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);
+                }
+            }
+        }
+        return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+    }
+
+    private static ComponentName getPstnConnectionServiceName() {
+        return new ComponentName(PhoneGlobals.getInstance(), TelephonyConnectionService.class);
+    }
+
     /**
      * Register ICC status for all phones.
      */