Detect USB headsets
authorChristian Weiske <[email protected]>
Wed, 28 Aug 2024 16:43:28 +0000 (18:43 +0200)
committerChristian Weiske <[email protected]>
Wed, 28 Aug 2024 16:43:28 +0000 (18:43 +0200)
src/de/cweiske/headphoneindicator/MainActivity.java
src/de/cweiske/headphoneindicator/NotificationReceiver.java
src/de/cweiske/headphoneindicator/PlugIntentHelper.java

index f6d2bb3dcc11ed0f4c1b75c89c86ceb9783eec22..789efd9d0ea8f6dca34a6c21a28dab04563b0a51 100644 (file)
@@ -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);
             }
index 680312da0604e9b10966188b2c8ef904a576a435..dc8ec68e6b98faaf9289b04789c6fb22175abd69 100644 (file)
@@ -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;
         }
index 8844f6a9f6b304a0a1e4a661105bd8dab936716e..7257d021e2eb5bbb79777075ce9e614cb4512e92 100644 (file)
@@ -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);
     }
 }