Skip to content

Commit 5713c9c

Browse files
Mike LockwoodAndroid (Google) Code Review
authored andcommitted
Merge "Add Intents to notify when USB audio devices or accessories are attached"
2 parents 29d6fa9 + 9d5a4be commit 5713c9c

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

core/java/android/content/Intent.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,38 @@ public static Intent createChooser(Intent target, CharSequence title) {
20332033
public static final String ACTION_HDMI_AUDIO_PLUG =
20342034
"android.intent.action.HDMI_AUDIO_PLUG";
20352035

2036+
/**
2037+
* Broadcast Action: A USB audio device was plugged in or unplugged.
2038+
*
2039+
* <p>The intent will have the following extra values:
2040+
* <ul>
2041+
* <li><em>state</em> - 0 for unplugged, 1 for plugged. </li>
2042+
* <li><em>card</em> - ALSA card number (integer) </li>
2043+
* <li><em>device</em> - ALSA device number (integer) </li>
2044+
* </ul>
2045+
* </ul>
2046+
* @hide
2047+
*/
2048+
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
2049+
public static final String ACTION_USB_AUDIO_DEVICE_PLUG =
2050+
"android.intent.action.USB_AUDIO_DEVICE_PLUG";
2051+
2052+
/**
2053+
* Broadcast Action: A USB audio accessory was plugged in or unplugged.
2054+
*
2055+
* <p>The intent will have the following extra values:
2056+
* <ul>
2057+
* <li><em>state</em> - 0 for unplugged, 1 for plugged. </li>
2058+
* <li><em>card</em> - ALSA card number (integer) </li>
2059+
* <li><em>device</em> - ALSA device number (integer) </li>
2060+
* </ul>
2061+
* </ul>
2062+
* @hide
2063+
*/
2064+
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
2065+
public static final String ACTION_USB_AUDIO_ACCESSORY_PLUG =
2066+
"android.intent.action.USB_AUDIO_ACCESSORY_PLUG";
2067+
20362068
/**
20372069
* <p>Broadcast Action: The user has switched on advanced settings in the settings app:</p>
20382070
* <ul>

core/java/android/hardware/usb/UsbManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public class UsbManager {
6666
* PTP function is enabled
6767
* <li> {@link #USB_FUNCTION_PTP} boolean extra indicating whether the
6868
* accessory function is enabled
69+
* <li> {@link #USB_FUNCTION_AUDIO_SOURCE} boolean extra indicating whether the
70+
* audio source function is enabled
6971
* </ul>
7072
*
7173
* {@hide}
@@ -177,6 +179,14 @@ public class UsbManager {
177179
*/
178180
public static final String USB_FUNCTION_PTP = "ptp";
179181

182+
/**
183+
* Name of the audio source USB function.
184+
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
185+
*
186+
* {@hide}
187+
*/
188+
public static final String USB_FUNCTION_AUDIO_SOURCE = "audio_source";
189+
180190
/**
181191
* Name of the Accessory USB function.
182192
* Used in extras for the {@link #ACTION_USB_STATE} broadcast

services/java/com/android/server/usb/UsbDeviceManager.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.util.List;
6161
import java.util.HashMap;
6262
import java.util.Map;
63+
import java.util.Scanner;
6364

6465
/**
6566
* UsbDeviceManager manages USB state in device mode.
@@ -81,6 +82,8 @@ public class UsbDeviceManager {
8182
"/sys/class/android_usb/android0/f_mass_storage/lun/file";
8283
private static final String RNDIS_ETH_ADDR_PATH =
8384
"/sys/class/android_usb/android0/f_rndis/ethaddr";
85+
private static final String AUDIO_SOURCE_PCM_PATH =
86+
"/sys/class/android_usb/android0/f_audio_source/pcm";
8487

8588
private static final int MSG_UPDATE_STATE = 0;
8689
private static final int MSG_ENABLE_ADB = 1;
@@ -105,6 +108,7 @@ public class UsbDeviceManager {
105108
private final boolean mHasUsbAccessory;
106109
private boolean mUseUsbNotification;
107110
private boolean mAdbEnabled;
111+
private boolean mAudioSourceEnabled;
108112
private Map<String, List<Pair<String, String>>> mOemModeMap;
109113

110114
private class AdbSettingsObserver extends ContentObserver {
@@ -291,6 +295,8 @@ public UsbHandler(Looper looper) {
291295
String state = FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim();
292296
updateState(state);
293297
mAdbEnabled = containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_ADB);
298+
mAudioSourceEnabled = containsFunction(mCurrentFunctions,
299+
UsbManager.USB_FUNCTION_AUDIO_SOURCE);
294300

295301
// Upgrade step for previous versions that used persist.service.adb.enable
296302
String value = SystemProperties.get("persist.service.adb.enable", "");
@@ -504,6 +510,28 @@ private void updateUsbState() {
504510
mContext.sendStickyBroadcast(intent);
505511
}
506512

513+
private void updateAudioSourceFunction(boolean enabled) {
514+
// send a sticky broadcast containing current USB state
515+
Intent intent = new Intent(Intent.ACTION_USB_AUDIO_ACCESSORY_PLUG);
516+
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
517+
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
518+
intent.putExtra("state", (enabled ? 1 : 0));
519+
if (enabled) {
520+
try {
521+
Scanner scanner = new Scanner(new File(AUDIO_SOURCE_PCM_PATH));
522+
int card = scanner.nextInt();
523+
int device = scanner.nextInt();
524+
intent.putExtra("card", card);
525+
intent.putExtra("device", device);
526+
} catch (FileNotFoundException e) {
527+
Slog.e(TAG, "could not open audio source PCM file", e);
528+
}
529+
}
530+
531+
mContext.sendStickyBroadcast(intent);
532+
mAudioSourceEnabled = enabled;
533+
}
534+
507535
@Override
508536
public void handleMessage(Message msg) {
509537
switch (msg.what) {
@@ -523,6 +551,11 @@ public void handleMessage(Message msg) {
523551
}
524552
if (mBootCompleted) {
525553
updateUsbState();
554+
boolean audioSourceEnabled = containsFunction(mCurrentFunctions,
555+
UsbManager.USB_FUNCTION_AUDIO_SOURCE);
556+
if (audioSourceEnabled != mAudioSourceEnabled) {
557+
updateAudioSourceFunction(audioSourceEnabled);
558+
}
526559
}
527560
break;
528561
case MSG_ENABLE_ADB:
@@ -543,6 +576,7 @@ public void handleMessage(Message msg) {
543576
if (mCurrentAccessory != null) {
544577
mSettingsManager.accessoryAttached(mCurrentAccessory);
545578
}
579+
updateAudioSourceFunction(mAudioSourceEnabled);
546580
break;
547581
}
548582
}

0 commit comments

Comments
 (0)