Fix logic for Intent.ACTION_HEADSET_PLUG for USB headsets
In the broadcast of Intent.ACTION_HEADSET_PLUG:
- restore setting "microphone" to 0 for line and headphone as
the input bit is never set for those devices
- for USB_HEADSET: there is no input bit for this device, so:
* when observing change for DEVICE_OUT, check status of DEVICE_IN
* when observing change for DEVICE_IN, only send the intent
if DEVICE_OUT is available.
Test: test broadcast and extras for Intent.ACTION_HEADSET_PLUG
Bug: 64250808
Change-Id: I0913933d03e21189bb552f60ae5dd35ffe28ddca
diff --git a/services/core/java/com/android/server/audio/AudioService.java b/services/core/java/com/android/server/audio/AudioService.java
index d85460f..cff9173 100644
--- a/services/core/java/com/android/server/audio/AudioService.java
+++ b/services/core/java/com/android/server/audio/AudioService.java
@@ -5413,10 +5413,21 @@
} else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE ||
device == AudioSystem.DEVICE_OUT_LINE) {
intent.setAction(Intent.ACTION_HEADSET_PLUG);
- intent.putExtra("microphone", (device & AudioSystem.DEVICE_BIT_IN) != 0 ? 1 : 0);
+ intent.putExtra("microphone", 0);
} else if (device == AudioSystem.DEVICE_OUT_USB_HEADSET) {
intent.setAction(Intent.ACTION_HEADSET_PLUG);
- intent.putExtra("microphone", 0);
+ intent.putExtra("microphone",
+ AudioSystem.getDeviceConnectionState(AudioSystem.DEVICE_IN_USB_HEADSET, "")
+ == AudioSystem.DEVICE_STATE_AVAILABLE ? 1 : 0);
+ } else if (device == AudioSystem.DEVICE_IN_USB_HEADSET) {
+ if (AudioSystem.getDeviceConnectionState(AudioSystem.DEVICE_OUT_USB_HEADSET, "")
+ == AudioSystem.DEVICE_STATE_AVAILABLE) {
+ intent.setAction(Intent.ACTION_HEADSET_PLUG);
+ intent.putExtra("microphone", 1);
+ } else {
+ // do not send ACTION_HEADSET_PLUG when only the input side is seen as changing
+ return;
+ }
} else if (device == AudioSystem.DEVICE_OUT_HDMI ||
device == AudioSystem.DEVICE_OUT_HDMI_ARC) {
configureHdmiPlugIntent(intent, state);