hal: fix no audio in qchat call with speaker as default device

HAL doesnt let voice calls start if the device is set to speaker
as voice calls generally start on devices other than speaker.
However QCHAT calls start by calling setForceUse and setting the
device to speaker.

Fix by updating a flag whenever device routing happens when
AUDIO_MODE_IN_CALL. Reset the flag when mode is AUDIO_MODE_NORMAL.

CRs-fixed: 596074
Change-Id: I546959d2b0123828562dba1f6439aa494a365c3f
diff --git a/hal/voice.c b/hal/voice.c
index 74d1978..cc546f5 100644
--- a/hal/voice.c
+++ b/hal/voice.c
@@ -424,6 +424,7 @@
     adev->voice.tty_mode = TTY_MODE_OFF;
     adev->voice.volume = 1.0f;
     adev->voice.mic_mute = false;
+    adev->voice.voice_device_set = false;
     for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
         adev->voice.session[i].pcm_rx = NULL;
         adev->voice.session[i].pcm_tx = NULL;
diff --git a/hal/voice.h b/hal/voice.h
index 38b304e..a7733b1 100644
--- a/hal/voice.h
+++ b/hal/voice.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
  * Not a contribution.
  *
  * Copyright (C) 2013 The Android Open Source Project
@@ -60,6 +60,7 @@
     int tty_mode;
     bool mic_mute;
     float volume;
+    bool voice_device_set;
 };
 
 enum {
diff --git a/hal/voice_extn/voice_extn.c b/hal/voice_extn/voice_extn.c
index 12fce09..a840fcd 100644
--- a/hal/voice_extn/voice_extn.c
+++ b/hal/voice_extn/voice_extn.c
@@ -143,6 +143,7 @@
     struct voice_session *session = NULL;
     int fd = 0;
     int ret = 0;
+    bool is_in_call = false;
 
     ALOGD("%s: enter:", __func__);
 
@@ -202,6 +203,10 @@
                     ALOGE("%s: voice_end_call() failed for usecase: %d\n",
                           __func__, usecase_id);
                 } else {
+                    voice_extn_is_in_call(adev, &is_in_call);
+                    if (!is_in_call) {
+                        adev->voice.voice_device_set = false;
+                    }
                     session->state.current = session->state.new;
                 }
                 break;
@@ -274,6 +279,7 @@
     struct voice_session *session = NULL;
     int i = 0;
     bool is_in_call;
+    int no_of_calls_active = 0;
 
     for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
         if (vsid == adev->voice.session[i].vsid) {
@@ -282,17 +288,27 @@
         }
     }
 
+    for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
+        if (CALL_INACTIVE != adev->voice.session[i].state.current)
+            no_of_calls_active++;
+    }
+
+    /* When there is only one call active, wait for audio policy manager to set
+     * the mode to AUDIO_MODE_NORMAL and trigger routing to end the last call.
+     */
+    if (no_of_calls_active == 1 && call_state == CALL_INACTIVE)
+        return 0;
+
     if (session) {
         session->state.new = call_state;
         voice_extn_is_in_call(adev, &is_in_call);
-        ALOGD("%s is_in_call:%d mode:%d\n", __func__, is_in_call, adev->mode);
+        ALOGD("%s is_in_call:%d voice_device_set:%d, mode:%d\n",
+              __func__, is_in_call, adev->voice.voice_device_set, adev->mode);
         /* Dont start voice call before device routing for voice usescases has
          * occured, otherwise voice calls will be started unintendedly on
          * speaker.
          */
-        if (is_in_call ||
-            (adev->mode == AUDIO_MODE_IN_CALL &&
-             adev->primary_output->devices != AUDIO_DEVICE_OUT_SPEAKER)) {
+        if (is_in_call || adev->voice.voice_device_set) {
             /* Device routing is not triggered for voice calls on the subsequent
              * subs, Hence update the call states if voice call is already
              * active on other sub.
@@ -377,6 +393,7 @@
      * udpated.
      */
     ALOGV("%s: enter:", __func__);
+    adev->voice.voice_device_set = true;
     return update_calls(adev);
 }