Merge "Fix a race-condition that returns incorrect foreground call"
diff --git a/src/com/android/server/telecom/Call.java b/src/com/android/server/telecom/Call.java
index b86d658..7be5847 100644
--- a/src/com/android/server/telecom/Call.java
+++ b/src/com/android/server/telecom/Call.java
@@ -735,7 +735,6 @@
 
     void setTargetPhoneAccount(PhoneAccountHandle accountHandle) {
         if (!Objects.equals(mTargetPhoneAccountHandle, accountHandle)) {
-            Log.i(this, "setTargetPhoneAccount  %s ", accountHandle);
             mTargetPhoneAccountHandle = accountHandle;
             for (Listener l : mListeners) {
                 l.onTargetPhoneAccountChanged(this);
diff --git a/src/com/android/server/telecom/CallAudioManager.java b/src/com/android/server/telecom/CallAudioManager.java
index 74b4cbb..72a8158 100644
--- a/src/com/android/server/telecom/CallAudioManager.java
+++ b/src/com/android/server/telecom/CallAudioManager.java
@@ -422,17 +422,11 @@
     }
 
     private void onCallUpdated(Call call) {
-
-        if (call != null) {
-            if (call.getState() != CallState.DISCONNECTED) {
-                updateAudioStreamAndMode(call);
-            }
-
-            if (call.getState() == CallState.ACTIVE &&
+        updateAudioStreamAndMode(call);
+        if (call != null && call.getState() == CallState.ACTIVE &&
                             call == mCallToSpeedUpMTAudio) {
                 mCallToSpeedUpMTAudio = null;
             }
-        }
     }
 
     private void setSystemAudioState(boolean isMuted, int route, int supportedRouteMask) {
@@ -520,7 +514,8 @@
                 Log.v(this, "updateAudioStreamAndMode : no foreground, speeding up MT audio.");
                 requestAudioFocusAndSetMode(AudioManager.STREAM_VOICE_CALL,
                                                          AudioManager.MODE_IN_CALL);
-            } else if (foregroundCall != null && waitingForAccountSelectionCall == null) {
+            } else if (foregroundCall != null && waitingForAccountSelectionCall == null
+                    && (foregroundCall.getState() != CallState.DISCONNECTED)) {
                 // In the case where there is a call that is waiting for account selection,
                 // this will fall back to abandonAudioFocus() below, which temporarily exits
                 // the in-call audio mode. This is to allow TalkBack to speak the "Call with"
@@ -538,7 +533,7 @@
                 Log.v(this, "updateAudioStreamAndMode : tone playing");
                 requestAudioFocusAndSetMode(
                         AudioManager.STREAM_VOICE_CALL, mMostRecentlyUsedMode);
-            } else if (!hasRingingForegroundCall()) {
+            } else if (call == null) {
                 Log.v(this, "updateAudioStreamAndMode : no ringing call");
                 abandonAudioFocus();
             } else {
diff --git a/src/com/android/server/telecom/ConnectionServiceWrapper.java b/src/com/android/server/telecom/ConnectionServiceWrapper.java
index f8e9f19..63307d3 100644
--- a/src/com/android/server/telecom/ConnectionServiceWrapper.java
+++ b/src/com/android/server/telecom/ConnectionServiceWrapper.java
@@ -36,6 +36,7 @@
 import android.telecom.StatusHints;
 import android.telecom.TelecomManager;
 import android.telecom.VideoProfile;
+import android.telephony.TelephonyManager;
 
 import com.android.internal.telecom.IConnectionService;
 import com.android.internal.telecom.IConnectionServiceAdapter;
@@ -521,7 +522,14 @@
                     if (mCallIdMapper.isValidCallId(callId)
                             || mCallIdMapper.isValidConferenceId(callId)) {
                         Call call = mCallIdMapper.getCall(callId);
-                        if (call != null) {
+                        if (call != null && extras != null) {
+                            if (extras.getParcelable(TelephonyManager.EMR_DIAL_ACCOUNT) instanceof
+                                    PhoneAccountHandle) {
+                                PhoneAccountHandle account = extras.
+                                        getParcelable(TelephonyManager.EMR_DIAL_ACCOUNT);
+                                Log.d(this, "setTargetPhoneAccount, account = " + account);
+                                call.setTargetPhoneAccount(account);
+                            }
                             call.setExtras(extras);
                         }
                     }
@@ -613,26 +621,6 @@
                 Binder.restoreCallingIdentity(token);
             }
         }
-
-        @Override
-        public void setPhoneAccountHandle(String callId, PhoneAccountHandle pHandle) {
-            long token = Binder.clearCallingIdentity();
-            try {
-                synchronized (mLock) {
-                    logIncoming("setPhoneAccountHandle %s %s", callId, pHandle);
-                    if (mCallIdMapper.isValidCallId(callId)) {
-                        Call call = mCallIdMapper.getCall(callId);
-                        if (call != null) {
-                            call.setTargetPhoneAccount(pHandle);
-                        } else {
-                            Log.w(this, "setPhoneAccountHandle, unknown call id: %s", callId);
-                        }
-                    }
-                }
-            } finally {
-                Binder.restoreCallingIdentity(token);
-            }
-        }
     }
 
     private final Adapter mAdapter = new Adapter();