Audioflinger: tracks monitor OP_PLAY_AUDIO

Mute/unmute tracks according to changes in OP_PLAY_AUDIO for
  the current usage.
In audio policy: always assign AUDIO_STREAM_ENFORCED_AUDIBLE
  to sonification tracks with AUDIBILITY_ENFORCED flag.
Do not mute tracks from root / audio server.
Do not mute UI sounds on AUDIO_STREAM_ENFORCED_AUDIBLE
  stream type.

Bug: 112339570
Test: enter DnD, play notifications, verify not heard
Change-Id: Ia5f1118481cf0573101acf2092fbd0ce2cf8c038
diff --git a/services/audiopolicy/engine/common/src/EngineBase.cpp b/services/audiopolicy/engine/common/src/EngineBase.cpp
index 07a7e65..530a2e4 100644
--- a/services/audiopolicy/engine/common/src/EngineBase.cpp
+++ b/services/audiopolicy/engine/common/src/EngineBase.cpp
@@ -70,7 +70,20 @@
 
 audio_stream_type_t EngineBase::getStreamTypeForAttributes(const audio_attributes_t &attr) const
 {
-    return mProductStrategies.getStreamTypeForAttributes(attr);
+    audio_stream_type_t engineStream = mProductStrategies.getStreamTypeForAttributes(attr);
+    // ensure the audibility flag for sonification is honored for stream types
+    // Note this is typically implemented in the product strategy configuration files, but is
+    //   duplicated here for safety.
+    if (attr.usage == AUDIO_USAGE_ASSISTANCE_SONIFICATION
+            && ((attr.flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) != 0)) {
+        engineStream = AUDIO_STREAM_ENFORCED_AUDIBLE;
+    }
+    // ensure the ENFORCED_AUDIBLE stream type reflects the "force use" setting:
+    if ((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
+            && (engineStream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
+        return AUDIO_STREAM_SYSTEM;
+    }
+    return engineStream;
 }
 
 audio_attributes_t EngineBase::getAttributesForStreamType(audio_stream_type_t stream) const
diff --git a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
index 762a4b1..651017d 100644
--- a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
+++ b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
@@ -1832,7 +1832,7 @@
 
         if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE &&
                 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
-            setStrategyMute(streamToStrategy(AUDIO_STREAM_RING), false, outputDesc);
+            setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), false, outputDesc);
         }
 
         if (followsSameRouting(client->attributes(), attributes_initializer(AUDIO_USAGE_MEDIA))) {