Send audio status only when in system audio mode

And use clearCallingIdentity to avoid permission issue.

Bug: 111504981
Test: make; local tests;
Change-Id: Ie62dc80416fbffe1cb54fa729ffc80ca008946f5
diff --git a/services/core/java/com/android/server/audio/AudioService.java b/services/core/java/com/android/server/audio/AudioService.java
index 6bebbe2..c7e103c 100644
--- a/services/core/java/com/android/server/audio/AudioService.java
+++ b/services/core/java/com/android/server/audio/AudioService.java
@@ -1781,12 +1781,15 @@
                     }
 
                     if (mHdmiAudioSystemClient != null &&
+                            mHdmiSystemAudioSupported &&
                             streamTypeAlias == AudioSystem.STREAM_MUSIC &&
                             (oldIndex != newIndex || isMuteAdjust)) {
+                        final long identity = Binder.clearCallingIdentity();
                         mHdmiAudioSystemClient.sendReportAudioStatusCecCommand(
                                 isMuteAdjust, getStreamVolume(AudioSystem.STREAM_MUSIC),
                                 getStreamMaxVolume(AudioSystem.STREAM_MUSIC),
                                 isStreamMute(AudioSystem.STREAM_MUSIC));
+                        Binder.restoreCallingIdentity(identity);
                     }
                 }
             }
@@ -2044,12 +2047,15 @@
         synchronized (mHdmiClientLock) {
             if (mHdmiManager != null &&
                     mHdmiAudioSystemClient != null &&
+                    mHdmiSystemAudioSupported &&
                     streamTypeAlias == AudioSystem.STREAM_MUSIC &&
                     (oldIndex != index)) {
+                final long identity = Binder.clearCallingIdentity();
                 mHdmiAudioSystemClient.sendReportAudioStatusCecCommand(
                         false, getStreamVolume(AudioSystem.STREAM_MUSIC),
                         getStreamMaxVolume(AudioSystem.STREAM_MUSIC),
                         isStreamMute(AudioSystem.STREAM_MUSIC));
+                Binder.restoreCallingIdentity(identity);
             }
         }
         sendVolumeUpdate(streamType, oldIndex, index, flags);
@@ -7104,11 +7110,11 @@
         int device = AudioSystem.DEVICE_NONE;
         synchronized (mHdmiClientLock) {
             if (mHdmiManager != null) {
-                if (mHdmiTvClient == null) {
-                    Log.w(TAG, "Only Hdmi-Cec enabled TV device supports system audio mode.");
+                if (mHdmiTvClient == null && mHdmiAudioSystemClient == null) {
+                    Log.w(TAG, "Only Hdmi-Cec enabled TV or audio system device supports"
+                            + "system audio mode.");
                     return device;
                 }
-
                 if (mHdmiSystemAudioSupported != on) {
                     mHdmiSystemAudioSupported = on;
                     final int config = on ? AudioSystem.FORCE_HDMI_SYSTEM_AUDIO_ENFORCED :
diff --git a/services/core/java/com/android/server/hdmi/HdmiCecLocalDeviceAudioSystem.java b/services/core/java/com/android/server/hdmi/HdmiCecLocalDeviceAudioSystem.java
index ae43ddf..7ba8e14 100644
--- a/services/core/java/com/android/server/hdmi/HdmiCecLocalDeviceAudioSystem.java
+++ b/services/core/java/com/android/server/hdmi/HdmiCecLocalDeviceAudioSystem.java
@@ -224,6 +224,7 @@
             return true;
         }
 
+        mSystemAudioSource = systemAudioStatusOn ? message.getSource() : null;
         mService.sendCecCommand(
                 HdmiCecMessageBuilder.buildSetSystemAudioMode(
                         mAddress, Constants.ADDR_BROADCAST, systemAudioStatusOn));