Merge "Add Intents to notify when USB audio devices or accessories are attached"
This commit is contained in:
committed by
Android (Google) Code Review
commit
5713c9cfa9
@@ -2033,6 +2033,38 @@ public class Intent implements Parcelable, Cloneable {
|
||||
public static final String ACTION_HDMI_AUDIO_PLUG =
|
||||
"android.intent.action.HDMI_AUDIO_PLUG";
|
||||
|
||||
/**
|
||||
* Broadcast Action: A USB audio device was plugged in or unplugged.
|
||||
*
|
||||
* <p>The intent will have the following extra values:
|
||||
* <ul>
|
||||
* <li><em>state</em> - 0 for unplugged, 1 for plugged. </li>
|
||||
* <li><em>card</em> - ALSA card number (integer) </li>
|
||||
* <li><em>device</em> - ALSA device number (integer) </li>
|
||||
* </ul>
|
||||
* </ul>
|
||||
* @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.
|
||||
*
|
||||
* <p>The intent will have the following extra values:
|
||||
* <ul>
|
||||
* <li><em>state</em> - 0 for unplugged, 1 for plugged. </li>
|
||||
* <li><em>card</em> - ALSA card number (integer) </li>
|
||||
* <li><em>device</em> - ALSA device number (integer) </li>
|
||||
* </ul>
|
||||
* </ul>
|
||||
* @hide
|
||||
*/
|
||||
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
|
||||
public static final String ACTION_USB_AUDIO_ACCESSORY_PLUG =
|
||||
"android.intent.action.USB_AUDIO_ACCESSORY_PLUG";
|
||||
|
||||
/**
|
||||
* <p>Broadcast Action: The user has switched on advanced settings in the settings app:</p>
|
||||
* <ul>
|
||||
|
||||
@@ -66,6 +66,8 @@ public class UsbManager {
|
||||
* PTP function is enabled
|
||||
* <li> {@link #USB_FUNCTION_PTP} boolean extra indicating whether the
|
||||
* accessory function is enabled
|
||||
* <li> {@link #USB_FUNCTION_AUDIO_SOURCE} boolean extra indicating whether the
|
||||
* audio source function is enabled
|
||||
* </ul>
|
||||
*
|
||||
* {@hide}
|
||||
@@ -177,6 +179,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
|
||||
|
||||
@@ -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<String, List<Pair<String, String>>> 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user