hal: Enable support for Listen voice wakeup feature

enable the support for Listen voice wakeup feature by loading
listen hal library dynamically.

Change-Id: Ife8e71589620342e21b4c52751466edaccc536ef
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index c00b067..f7e7428 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -134,7 +134,8 @@
 
 static struct audio_device *adev = NULL;
 static pthread_mutex_t adev_init_lock;
-static bool is_adev_initialised = false;
+static unsigned int audio_device_ref_count;
+
 static int set_voice_volume_l(struct audio_device *adev, float volume);
 
 static bool is_supported_format(audio_format_t format)
@@ -693,6 +694,9 @@
     /* 2. Disable the tx device */
     disable_snd_device(adev, uc_info->in_snd_device, true);
 
+    audio_extn_listen_update_status(uc_info,
+            LISTEN_EVENT_AUDIO_CAPTURE_INACTIVE);
+
     list_remove(&uc_info->list);
     free(uc_info);
 
@@ -736,6 +740,9 @@
     list_add_tail(&adev->usecase_list, &uc_info->list);
     select_devices(adev, in->usecase);
 
+    audio_extn_listen_update_status(uc_info,
+            LISTEN_EVENT_AUDIO_CAPTURE_ACTIVE);
+
     ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
           __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
     in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
@@ -2287,10 +2294,21 @@
 static int adev_close(hw_device_t *device)
 {
     struct audio_device *adev = (struct audio_device *)device;
-    audio_route_free(adev->audio_route);
-    free(adev->snd_dev_ref_cnt);
-    platform_deinit(adev->platform);
-    free(device);
+
+    if (!adev)
+        return 0;
+
+    pthread_mutex_lock(&adev_init_lock);
+
+    if ((--audio_device_ref_count) == 0) {
+        audio_route_free(adev->audio_route);
+        free(adev->snd_dev_ref_cnt);
+        platform_deinit(adev->platform);
+        audio_extn_listen_deinit(adev);
+        free(device);
+        adev = NULL;
+    }
+    pthread_mutex_unlock(&adev_init_lock);
     return 0;
 }
 
@@ -2303,8 +2321,9 @@
     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
 
     pthread_mutex_lock(&adev_init_lock);
-    if (is_adev_initialised == true){
+    if (audio_device_ref_count != 0){
             *device = &adev->device.common;
+            audio_device_ref_count++;
             ALOGD("%s: returning existing instance of adev", __func__);
             ALOGD("%s: exit", __func__);
             pthread_mutex_unlock(&adev_init_lock);
@@ -2372,11 +2391,11 @@
                                                         "visualizer_hal_stop_output");
         }
     }
+    audio_extn_listen_init(adev);
 
     *device = &adev->device.common;
 
-    /* update init flag*/
-    is_adev_initialised = true;
+    audio_device_ref_count++;
     pthread_mutex_unlock(&adev_init_lock);
 
     ALOGV("%s: exit", __func__);