hal: switch to current active device when a2dp suspended

When SCO profile is disabled, during voice call, device can be
switched to handset first and followed with a2dpsuspended param,
device will be switched to speaker and if it shares backend with
handset, voice call can be eventually switched to speaker which
is unexpected.
Check if there's any other active usecase and switch to the same
one during check_a2dp_restore.

Change-Id: I2cabccffc5d2601362a5da16b9793c218a009822
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index fd04f3f..b41ebca 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2020, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2013-2021, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright (C) 2013 The Android Open Source Project
@@ -10456,9 +10456,11 @@
 int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore)
 {
     struct audio_usecase *uc_info;
+    struct audio_usecase *usecase;
     float left_p;
     float right_p;
     struct listnode devices;
+    struct listnode *node;
 
     uc_info = get_usecase_from_list(adev, out->usecase);
     if (uc_info == NULL) {
@@ -10494,9 +10496,17 @@
         pthread_mutex_lock(&out->latch_lock);
         // mute stream and switch to speaker if suspended
         if (!out->a2dp_muted && !out->standby) {
-            ALOGD("%s: selecting speaker and muting stream", __func__);
             assign_devices(&devices, &out->device_list);
             reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
+            list_for_each(node, &adev->usecase_list) {
+                usecase = node_to_item(node, struct audio_usecase, list);
+                if ((usecase != uc_info) &&
+                        platform_check_backends_match(SND_DEVICE_OUT_SPEAKER,
+                                                      usecase->out_snd_device)) {
+                    assign_devices(&out->device_list, &usecase->stream.out->device_list);
+                    break;
+                }
+            }
             left_p = out->volume_l;
             right_p = out->volume_r;
             out->a2dp_muted = true;
@@ -10515,6 +10525,8 @@
                 usleep(latency * 1000);
             }
             select_devices(adev, out->usecase);
+            ALOGD("%s: switched to device:%s and mute stream",
+                   __func__, platform_get_snd_device_name(uc_info->out_snd_device));
             if (is_offload_usecase(out->usecase)) {
                 if (out->offload_state == OFFLOAD_STATE_PLAYING)
                     compress_resume(out->compr);