FP2-1659:<Preferred SIM for Cellular Calls> and <Make calls with> is inconsistent

 [ST_3[Phone]Sometimes the <Preferred SIM for Cellular Calls>
 and <Make calls with> is inconsistent

Change-Id: Ibd9b1d39c1b7c300e95df10828467cc18efec191
diff --git a/src/com/android/server/telecom/PhoneAccountRegistrar.java b/src/com/android/server/telecom/PhoneAccountRegistrar.java
index de2ac06..6a77826 100755
--- a/src/com/android/server/telecom/PhoneAccountRegistrar.java
+++ b/src/com/android/server/telecom/PhoneAccountRegistrar.java
@@ -195,11 +195,15 @@
      *      if it was set by another user).
      */
     PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() {
-        if (TelephonyManager.getDefault().getPhoneCount() > 1) {
-            return getUserSelectedVoicePhoneAccount();
-        }
+         if (mState.defaultOutgoing == null){
+             return getUserSelectedVoicePhoneAccount();
+         }
+         if (mState.defaultOutgoing != null) {
 
-        if (mState.defaultOutgoing != null) {
+            if ((TelephonyManager.getDefault().getPhoneCount() > 1) &&
+                    !mState.defaultOutgoing.getId().contains("sip")) {
+                return getUserSelectedVoicePhoneAccount();
+            }
             // Return the registered outgoing default iff it still exists (we keep a sticky
             // default to survive account deletion and re-addition)
             for (int i = 0; i < mState.accounts.size(); i++) {
@@ -214,7 +218,7 @@
         return null;
     }
 
-    PhoneAccountHandle getUserSelectedVoicePhoneAccount() {
+    /*PhoneAccountHandle getUserSelectedVoicePhoneAccount() {
         String defaultId =null;
         if (mState.defaultOutgoing != null){
             defaultId = mState.defaultOutgoing.getId();
@@ -245,6 +249,42 @@
         }
 
         return prefPhoneAccount;
+    }*/
+
+    PhoneAccountHandle getUserSelectedVoicePhoneAccount() {
+        int voiceSubId = SubscriptionManager.getDefaultVoiceSubId();
+        boolean isVoicePrompt = SubscriptionManager.isVoicePromptEnabled();
+        PhoneAccountHandle prefPhoneAccount = null;
+
+        Log.i(this, "getUserSelVoicePhoneAccount, voice subId = " + voiceSubId
+                 + " prompt = " + isVoicePrompt);
+        if (!isVoicePrompt) {
+            for (int i = 0; i < mState.accounts.size(); i++) {
+                String id = mState.accounts.get(i).getAccountHandle().getId();
+
+                // emergency account present return it
+                if (id.equals("E")) {
+                   Log.i(this, "getUserSelVoicePhoneAccount, emergency account ");
+                   return mState.accounts.get(i).getAccountHandle();
+                }else if (!id.contains("sip")) {
+                    int subId = voiceSubId;
+                    try {
+                        subId = Integer.parseInt(id);
+                    } catch (NumberFormatException e) {
+                        Log.w(this, " NumberFormatException " + e);
+                    }
+                    Log.i(this, "getUserSelectedVoicePhoneAccount, voice subId = "
+                            + voiceSubId + " subId = " + subId + " mId = " + id);
+                    if (subId == voiceSubId) {
+                            prefPhoneAccount = mState.accounts.get(i).getAccountHandle();
+                            break;
+                    }
+                }
+
+            }
+        }
+
+        return prefPhoneAccount;
     }
 
     public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) {
@@ -322,24 +362,27 @@
             }
         } else {
             String id = mState.defaultOutgoing.getId();
+
             // Return from here, only emergency account available
             if (id.equals("E")) {
                 Log.i(this, "setDefaultVoicePhoneAccount, only emergency account present ");
                 return;
             }
-            int subId = SubscriptionManager.getDefaultVoiceSubId();
-            try {
-                subId = Integer.parseInt(mState.defaultOutgoing.getId());
-            } catch (NumberFormatException e) {
-                Log.w(this, " NumberFormatException " + e);
+            if(!id.contains("sip")){
+                int subId = SubscriptionManager.getDefaultVoiceSubId();
+                try {
+                    subId = Integer.parseInt(mState.defaultOutgoing.getId());
+                } catch (NumberFormatException e) {
+                    Log.w(this, " NumberFormatException " + e);
+                }
+                Log.i(this, "set voice default subId as  " + subId + " prmotp = " + voicePrompt);
+                if (SubscriptionManager.getDefaultVoiceSubId() != subId) {
+                    mSubscriptionManager.setDefaultVoiceSubId(subId);
+                }
             }
-            Log.i(this, "set voice default subId as  " + subId + " prmotp = " + voicePrompt);
-            if (SubscriptionManager.getDefaultVoiceSubId() != subId) {
-                mSubscriptionManager.setDefaultVoiceSubId(subId);
-            }
-            if (voicePrompt == true) {
-                SubscriptionManager.setVoicePromptEnabled(false);
-            }
+           if (voicePrompt == true) {
+               SubscriptionManager.setVoicePromptEnabled(false);
+           }
         }
     }