audio policy: add support for device combo with speaker safe

combo devices with AUDIO_DEVICE_OUT_SPEAKER_SAFE were not
supported by audio policy manager causing selection
of speaker+headphone when spekaer_safe+headphone would be
the right selection.

Bug: 21537010.
Change-Id: I9865352559c9d32c6754ad3d2b84bddfe2dc8aac
diff --git a/services/audiopolicy/common/include/Volume.h b/services/audiopolicy/common/include/Volume.h
index 4205589..712f7a7 100755
--- a/services/audiopolicy/common/include/Volume.h
+++ b/services/audiopolicy/common/include/Volume.h
@@ -82,6 +82,8 @@
             //  - HDMI-CEC system audio mode only output: give priority to available item in order.
             if (device & AUDIO_DEVICE_OUT_SPEAKER) {
                 device = AUDIO_DEVICE_OUT_SPEAKER;
+            } else if (device & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
+                device = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
             } else if (device & AUDIO_DEVICE_OUT_HDMI_ARC) {
                 device = AUDIO_DEVICE_OUT_HDMI_ARC;
             } else if (device & AUDIO_DEVICE_OUT_AUX_LINE) {
diff --git a/services/audiopolicy/enginedefault/src/Engine.cpp b/services/audiopolicy/enginedefault/src/Engine.cpp
index 7a785eb..0686414 100755
--- a/services/audiopolicy/enginedefault/src/Engine.cpp
+++ b/services/audiopolicy/enginedefault/src/Engine.cpp
@@ -319,8 +319,11 @@
             device = getDeviceForStrategy(STRATEGY_SONIFICATION);
             //user "safe" speaker if available instead of normal speaker to avoid triggering
             //other acoustic safety mechanisms for notification
-            if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE))
-                device = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
+            if ((device & AUDIO_DEVICE_OUT_SPEAKER) &&
+                    (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
+                device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
+                device &= ~AUDIO_DEVICE_OUT_SPEAKER;
+            }
         } else if (outputs.isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
             // while media is playing (or has recently played), use the same device
             device = getDeviceForStrategy(STRATEGY_MEDIA);
@@ -329,8 +332,11 @@
             device = getDeviceForStrategy(STRATEGY_SONIFICATION);
             //user "safe" speaker if available instead of normal speaker to avoid triggering
             //other acoustic safety mechanisms for notification
-            if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE))
-                device = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
+            if ((device & AUDIO_DEVICE_OUT_SPEAKER) &&
+                    (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
+                device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
+                device &= ~AUDIO_DEVICE_OUT_SPEAKER;
+            }
         }
         break;