Audio policy: use SPEAKER_SAFE device for alarms and ringtones

Use AUDIO_DEVICE_OUT_SPEAKER_SAFE when available for
  STRATEGY_SONIFICATION instead of AUDIO_DEVICE_OUT_SPEAKER.
It is alrady the case for STRATEGY_SONIFICATION_RESPECTFUL
  (notifications).

Test: play an alarm on a device with SPEAKER_SAFE in the policy conf, verify routing matches mixer path
Bug: 35675592
Change-Id: I03a4f4238a1ac73165d44f72e4e587b31ec88cc5
diff --git a/services/audiopolicy/enginedefault/src/Engine.cpp b/services/audiopolicy/enginedefault/src/Engine.cpp
index 096ffd1..b6fff8c 100644
--- a/services/audiopolicy/enginedefault/src/Engine.cpp
+++ b/services/audiopolicy/enginedefault/src/Engine.cpp
@@ -552,6 +552,15 @@
                 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
             device &= ~AUDIO_DEVICE_OUT_SPEAKER;
         }
+
+        // for STRATEGY_SONIFICATION:
+        // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
+        if ((strategy == STRATEGY_SONIFICATION) &&
+                (device & AUDIO_DEVICE_OUT_SPEAKER) &&
+                (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
+            device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
+            device &= ~AUDIO_DEVICE_OUT_SPEAKER;
+        }
         } break;
 
     default: