PAL: switch to default device first when disconnecting headset

When headset disconnected, the music playback paused and then
the audio policy manager sent "routing=0", but if the headset
connected back before the standby time, it could not switch to
headset anymore when resuming playback since current pal device
was same with new coming device.

Switch to default device first if receiving "routing = 0" when
disconnecting headset until APM updates new device to HAL.

Change-Id: I60f20b7e434d97e8ce2075e768ae8b4b4e5de281
(cherry picked from commit bf31c36ddcacb6a7aa48e393e88f25d440cf05a7)
diff --git a/Pal.cpp b/Pal.cpp
index e0fc84f..a2b09bd 100644
--- a/Pal.cpp
+++ b/Pal.cpp
@@ -1050,22 +1050,23 @@
                 break;
         }
 
-      /*
-       * When USB headset is disconnected the music playback pauses
-       * and the policy manager sends routing=0. But if the USB is connected
-       * back before the standby time, it can not switch device to usb hs any more
-       * because current pal device is usb hs which equals new device when resuming playback.
-       * So routing to default device first during handling routing = 0 msg will guarantee
-       * the device switch to usb can be executed once USB is connected again.
-       */
-        bool curPalDevice_usb = curPalDevices.find(PAL_DEVICE_OUT_USB_DEVICE) != curPalDevices.end() ||
-                                curPalDevices.find(PAL_DEVICE_OUT_USB_HEADSET) != curPalDevices.end();
-        bool activeDevices_usb = activeDevices.find(PAL_DEVICE_OUT_USB_DEVICE) != activeDevices.end() ||
-                                 activeDevices.find(PAL_DEVICE_OUT_USB_HEADSET) != activeDevices.end();
-        bool usb_active = rm->isDeviceAvailable(PAL_DEVICE_OUT_USB_DEVICE) ||
-                          rm->isDeviceAvailable(PAL_DEVICE_OUT_USB_HEADSET);
+        /*
+        * When headset is disconnected the music playback pauses
+        * and the policy manager sends routing=0. But if the headset is connected
+        * back before the standby time, it can not switch device to headset any more
+        * because current pal device is headset which equals new device when resuming playback.
+        * So routing to default device first during handling routing = 0 msg will guarantee
+        * the device switch to headset can be executed once headset is connected again.
+        */
         if (devices[0].id == PAL_DEVICE_NONE &&
-            curPalDevice_usb && activeDevices_usb && !usb_active)
+            (rm->isDisconnectedDeviceStillActive(curPalDevices,activeDevices,
+            PAL_DEVICE_OUT_USB_DEVICE) ||
+            rm->isDisconnectedDeviceStillActive(curPalDevices,activeDevices,
+            PAL_DEVICE_OUT_USB_HEADSET) ||
+            rm->isDisconnectedDeviceStillActive(curPalDevices,activeDevices,
+            PAL_DEVICE_OUT_WIRED_HEADPHONE) ||
+            rm->isDisconnectedDeviceStillActive(curPalDevices,activeDevices,
+            PAL_DEVICE_OUT_WIRED_HEADSET)))
         {
             devices[0].id = PAL_DEVICE_OUT_SPEAKER;
         }
diff --git a/resource_manager/inc/ResourceManager.h b/resource_manager/inc/ResourceManager.h
index f5fcfda..26f265e 100644
--- a/resource_manager/inc/ResourceManager.h
+++ b/resource_manager/inc/ResourceManager.h
@@ -45,6 +45,7 @@
 #include "PalCommon.h"
 #include <array>
 #include <map>
+#include <set>
 #include <expat.h>
 #include <stdio.h>
 #include <queue>
@@ -930,6 +931,7 @@
     bool isDeviceAvailable(pal_device_id_t id);
     bool isDeviceAvailable(std::vector<std::shared_ptr<Device>> devices, pal_device_id_t id);
     bool isDeviceAvailable(struct pal_device *devices, uint32_t devCount, pal_device_id_t id);
+    bool isDisconnectedDeviceStillActive(std::set<pal_device_id_t> &curPalDevices, std::set<pal_device_id_t> &activeDevices, pal_device_id_t id);
     bool isDeviceReady(pal_device_id_t id);
     static bool isBtScoDevice(pal_device_id_t id);
     static bool isBtDevice(pal_device_id_t id);
diff --git a/resource_manager/src/ResourceManager.cpp b/resource_manager/src/ResourceManager.cpp
index 374d1ff..526d90c 100644
--- a/resource_manager/src/ResourceManager.cpp
+++ b/resource_manager/src/ResourceManager.cpp
@@ -10528,6 +10528,15 @@
     return isAvailable;
 }
 
+bool ResourceManager::isDisconnectedDeviceStillActive(
+    std::set<pal_device_id_t> &curPalDevices, std::set<pal_device_id_t> &activeDevices,
+    pal_device_id_t id)
+{
+    return (!isDeviceAvailable(id)) &&
+        (curPalDevices.find(id) != curPalDevices.end()) &&
+        (activeDevices.find(id) != activeDevices.end());
+}
+
 bool ResourceManager::isDeviceReady(pal_device_id_t id)
 {
     struct pal_device dAttr;