Speed up audio setup for IMS MT call

Set audio mode into in_call mode right after accepting the call
without waiting for the call be ACTIVE from RIL.

Bug: 19656525
Change-Id: I83ec03f14ba859680ab5a42e945d940d698e6915
diff --git a/src/com/android/server/telecom/CallAudioManager.java b/src/com/android/server/telecom/CallAudioManager.java
index 9daf815..b555f85 100644
--- a/src/com/android/server/telecom/CallAudioManager.java
+++ b/src/com/android/server/telecom/CallAudioManager.java
@@ -44,6 +44,7 @@
     private boolean mIsTonePlaying;
     private boolean mWasSpeakerOn;
     private int mMostRecentlyUsedMode = AudioManager.MODE_IN_CALL;
+    private Call mCallToSpeedUpMTAudio = null;
 
     CallAudioManager(Context context, StatusBarNotifier statusBarNotifier,
             WiredHeadsetManager wiredHeadsetManager) {
@@ -106,6 +107,12 @@
         }
 
         setSystemAudioState(false /* isMute */, route, mAudioState.getSupportedRouteMask());
+
+        if (call.can(android.telecom.Call.Details.CAPABILITY_SPEED_UP_MT_AUDIO)) {
+            Log.v(this, "Speed up audio setup for IMS MT call.");
+            mCallToSpeedUpMTAudio = call;
+            updateAudioStreamAndMode();
+        }
     }
 
     @Override
@@ -282,6 +289,10 @@
         boolean wasNotVoiceCall = mAudioFocusStreamType != AudioManager.STREAM_VOICE_CALL;
         updateAudioStreamAndMode();
 
+        if (call != null && call.getState() == CallState.ACTIVE &&
+                            call == mCallToSpeedUpMTAudio) {
+            mCallToSpeedUpMTAudio = null;
+        }
         // If we transition from not voice call to voice call, we need to set an initial state.
         if (wasNotVoiceCall && mAudioFocusStreamType == AudioManager.STREAM_VOICE_CALL) {
             setInitialAudioState(call, true /* force */);
@@ -361,7 +372,11 @@
             Call foregroundCall = getForegroundCall();
             Call waitingForAccountSelectionCall =
                     CallsManager.getInstance().getFirstCallWithState(CallState.PRE_DIAL_WAIT);
-            if (foregroundCall != null && waitingForAccountSelectionCall == null) {
+            Call call = CallsManager.getInstance().getForegroundCall();
+            if (foregroundCall == null && call != null && call == mCallToSpeedUpMTAudio) {
+                requestAudioFocusAndSetMode(AudioManager.STREAM_VOICE_CALL,
+                                                         AudioManager.MODE_IN_CALL);
+            } else if (foregroundCall != null && waitingForAccountSelectionCall == null) {
                 // 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"
@@ -412,6 +427,7 @@
             Log.v(this, "abandoning audio focus");
             mAudioManager.abandonAudioFocusForCall();
             mAudioFocusStreamType = STREAM_NONE;
+            mCallToSpeedUpMTAudio = null;
         }
     }