From 9d5a4be05b6d51ebb768e5fbcfa27ed9a39269b3 Mon Sep 17 00:00:00 2001 From: Mike Lockwood Date: Fri, 6 Apr 2012 09:41:32 -0700 Subject: Add Intents to notify when USB audio devices or accessories are attached In this change, only the USB audio accessory support is implemented. Change-Id: Id9b411319b07a96dc56649ca74cc5f3f89a55a7c Signed-off-by: Mike Lockwood --- core/java/android/content/Intent.java | 32 ++++++++++++++++++++ core/java/android/hardware/usb/UsbManager.java | 10 +++++++ .../com/android/server/usb/UsbDeviceManager.java | 34 ++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/core/java/android/content/Intent.java b/core/java/android/content/Intent.java index 2a9f1af..736dd24 100644 --- a/core/java/android/content/Intent.java +++ b/core/java/android/content/Intent.java @@ -2034,6 +2034,38 @@ public class Intent implements Parcelable, Cloneable { "android.intent.action.HDMI_AUDIO_PLUG"; /** + * Broadcast Action: A USB audio device was plugged in or unplugged. + * + *

The intent will have the following extra values: + *

+ * + * @hide + */ + @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) + public static final String ACTION_USB_AUDIO_DEVICE_PLUG = + "android.intent.action.USB_AUDIO_DEVICE_PLUG"; + + /** + * Broadcast Action: A USB audio accessory was plugged in or unplugged. + * + *

The intent will have the following extra values: + *

+ * + * @hide + */ + @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) + public static final String ACTION_USB_AUDIO_ACCESSORY_PLUG = + "android.intent.action.USB_AUDIO_ACCESSORY_PLUG"; + + /** *

Broadcast Action: The user has switched on advanced settings in the settings app:

* * * {@hide} @@ -178,6 +180,14 @@ public class UsbManager { public static final String USB_FUNCTION_PTP = "ptp"; /** + * Name of the audio source USB function. + * Used in extras for the {@link #ACTION_USB_STATE} broadcast + * + * {@hide} + */ + public static final String USB_FUNCTION_AUDIO_SOURCE = "audio_source"; + + /** * Name of the Accessory USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * diff --git a/services/java/com/android/server/usb/UsbDeviceManager.java b/services/java/com/android/server/usb/UsbDeviceManager.java index 4bea5e4..1bd15f6 100644 --- a/services/java/com/android/server/usb/UsbDeviceManager.java +++ b/services/java/com/android/server/usb/UsbDeviceManager.java @@ -60,6 +60,7 @@ import java.util.LinkedList; import java.util.List; import java.util.HashMap; import java.util.Map; +import java.util.Scanner; /** * UsbDeviceManager manages USB state in device mode. @@ -81,6 +82,8 @@ public class UsbDeviceManager { "/sys/class/android_usb/android0/f_mass_storage/lun/file"; private static final String RNDIS_ETH_ADDR_PATH = "/sys/class/android_usb/android0/f_rndis/ethaddr"; + private static final String AUDIO_SOURCE_PCM_PATH = + "/sys/class/android_usb/android0/f_audio_source/pcm"; private static final int MSG_UPDATE_STATE = 0; private static final int MSG_ENABLE_ADB = 1; @@ -105,6 +108,7 @@ public class UsbDeviceManager { private final boolean mHasUsbAccessory; private boolean mUseUsbNotification; private boolean mAdbEnabled; + private boolean mAudioSourceEnabled; private Map>> mOemModeMap; private class AdbSettingsObserver extends ContentObserver { @@ -291,6 +295,8 @@ public class UsbDeviceManager { String state = FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim(); updateState(state); mAdbEnabled = containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_ADB); + mAudioSourceEnabled = containsFunction(mCurrentFunctions, + UsbManager.USB_FUNCTION_AUDIO_SOURCE); // Upgrade step for previous versions that used persist.service.adb.enable String value = SystemProperties.get("persist.service.adb.enable", ""); @@ -504,6 +510,28 @@ public class UsbDeviceManager { mContext.sendStickyBroadcast(intent); } + private void updateAudioSourceFunction(boolean enabled) { + // send a sticky broadcast containing current USB state + Intent intent = new Intent(Intent.ACTION_USB_AUDIO_ACCESSORY_PLUG); + intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); + intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); + intent.putExtra("state", (enabled ? 1 : 0)); + if (enabled) { + try { + Scanner scanner = new Scanner(new File(AUDIO_SOURCE_PCM_PATH)); + int card = scanner.nextInt(); + int device = scanner.nextInt(); + intent.putExtra("card", card); + intent.putExtra("device", device); + } catch (FileNotFoundException e) { + Slog.e(TAG, "could not open audio source PCM file", e); + } + } + + mContext.sendStickyBroadcast(intent); + mAudioSourceEnabled = enabled; + } + @Override public void handleMessage(Message msg) { switch (msg.what) { @@ -523,6 +551,11 @@ public class UsbDeviceManager { } if (mBootCompleted) { updateUsbState(); + boolean audioSourceEnabled = containsFunction(mCurrentFunctions, + UsbManager.USB_FUNCTION_AUDIO_SOURCE); + if (audioSourceEnabled != mAudioSourceEnabled) { + updateAudioSourceFunction(audioSourceEnabled); + } } break; case MSG_ENABLE_ADB: @@ -543,6 +576,7 @@ public class UsbDeviceManager { if (mCurrentAccessory != null) { mSettingsManager.accessoryAttached(mCurrentAccessory); } + updateAudioSourceFunction(mAudioSourceEnabled); break; } } -- cgit v1.1