From 5ab7f167fc1d1b1041dd3003bf4a101e548329a9 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 28 Aug 2024 18:43:28 +0200 Subject: [PATCH] Detect USB headsets --- .../headphoneindicator/MainActivity.java | 3 +- .../NotificationReceiver.java | 3 +- .../headphoneindicator/PlugIntentHelper.java | 43 ++++++++++++++----- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/de/cweiske/headphoneindicator/MainActivity.java b/src/de/cweiske/headphoneindicator/MainActivity.java index f6d2bb3..789efd9 100644 --- a/src/de/cweiske/headphoneindicator/MainActivity.java +++ b/src/de/cweiske/headphoneindicator/MainActivity.java @@ -24,7 +24,8 @@ public class MainActivity extends Activity { BroadcastReceiver headsetPlugReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - PlugInfo plugInfo = PlugIntentHelper.getPlugInfo(intent); + PlugIntentHelper plugIntentHelper = new PlugIntentHelper(context); + PlugInfo plugInfo = plugIntentHelper.getPlugInfo(intent); if (plugInfo.isAudioEvent) { setPlugged(plugInfo.isPlugged, plugInfo.hasMicrophone); } diff --git a/src/de/cweiske/headphoneindicator/NotificationReceiver.java b/src/de/cweiske/headphoneindicator/NotificationReceiver.java index 680312d..dc8ec68 100644 --- a/src/de/cweiske/headphoneindicator/NotificationReceiver.java +++ b/src/de/cweiske/headphoneindicator/NotificationReceiver.java @@ -21,7 +21,8 @@ public class NotificationReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - PlugInfo plugInfo = PlugIntentHelper.getPlugInfo(intent); + PlugIntentHelper plugIntentHelper = new PlugIntentHelper(context); + PlugInfo plugInfo = plugIntentHelper.getPlugInfo(intent); if (!plugInfo.isAudioEvent) { return; } diff --git a/src/de/cweiske/headphoneindicator/PlugIntentHelper.java b/src/de/cweiske/headphoneindicator/PlugIntentHelper.java index 8844f6a..7257d02 100644 --- a/src/de/cweiske/headphoneindicator/PlugIntentHelper.java +++ b/src/de/cweiske/headphoneindicator/PlugIntentHelper.java @@ -1,16 +1,26 @@ package de.cweiske.headphoneindicator; +import android.content.Context; import android.content.Intent; import android.hardware.usb.UsbConstants; import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbManager; +import android.media.AudioDeviceInfo; +import android.media.AudioManager; +import android.os.Build; import android.os.Bundle; public class PlugIntentHelper { + private AudioManager audioManager; + + public PlugIntentHelper(Context context) { + this.audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); + } + /** * Handle all supported intent events and extract audio device information from it */ - public static PlugInfo getPlugInfo(Intent intent) + public PlugInfo getPlugInfo(Intent intent) { if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) { return getHeadsetPlugInfo(intent); @@ -27,7 +37,7 @@ public class PlugIntentHelper { /** * Extract interesting information from a "headset plugged" event */ - protected static PlugInfo getHeadsetPlugInfo(Intent intent) + protected PlugInfo getHeadsetPlugInfo(Intent intent) { Bundle extras = intent.getExtras(); if (extras == null) { @@ -44,30 +54,41 @@ public class PlugIntentHelper { /** * Extract the interesting information from an USB device attached/detached event */ - protected static PlugInfo getUsbDevicePlugInfo(Intent intent) + protected PlugInfo getUsbDevicePlugInfo(Intent intent) { UsbDevice usbDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (usbDevice == null) { return new PlugInfo(false, false, false); } - boolean audioDevice = false; + boolean isAudioDevice = false; for (int i = 0; i < usbDevice.getInterfaceCount(); i++) { if (usbDevice.getInterface(i).getInterfaceClass() == UsbConstants.USB_CLASS_AUDIO) { - audioDevice = true; + isAudioDevice = true; } } - if (!audioDevice) { + if (!isAudioDevice) { return new PlugInfo(false, false, false); } - if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) { - return new PlugInfo(true, true, false); - } else if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) { + if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) { return new PlugInfo(true, false, false); } - //should never be called :) - return new PlugInfo(false, false, false); + if (!intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) { + //should never be called :) + return new PlugInfo(false, false, false); + } + + boolean hasMicrophone = false; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && audioManager != null) { + for (AudioDeviceInfo audioDevice: audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS)) { + if (audioDevice.getType() == AudioDeviceInfo.TYPE_USB_HEADSET) { + hasMicrophone = true; + break; + } + } + } + return new PlugInfo(true, true, hasMicrophone); } } -- 2.30.2