最近处理了一个需求,就是控制系统选择的麦克风和扬声器。原生的逻辑当插上一个既是麦克风又是扬声器的设备时,会去同时切换,与我们的需求不符。
原生逻辑看以下的代码
frameworks\base\services\usb\java\com\android\server\usb\UsbAlsaDevice.java
核心的代码如下:
/** Updates AudioService with the connection state of the alsaDevice.
* Checks ALSA Jack state for inputs and outputs before reporting.
*/
public synchronized void updateWiredDeviceConnectionState(boolean enable) {
if (!mSelected) {
Slog.e(TAG, "updateWiredDeviceConnectionState on unselected AlsaDevice!");
return;
}
String alsaCardDeviceString = getAlsaCardDeviceString();
if (alsaCardDeviceString == null) {
return;
}
try {
// Output Device
if (mHasOutput) {
int device = mIsOutputHeadset
? AudioSystem.DEVICE_OUT_USB_HEADSET
: AudioSystem.DEVICE_OUT_USB_DEVICE;
if (DEBUG) {
Slog.d(TAG, "pre-call device:0x" + Integer.toHexString(device)
+ " addr:" + alsaCardDeviceString
+ " name:" + mDeviceName);
}
boolean connected = isOutputJackConnected();
Slog.i(TAG, "OUTPUT JACK connected: " + connected);
int outputState = (enable && connected) ? 1 : 0;
if (outputState != mOutputState) {
mOutputState = outputState;
mAudioService.setWiredDeviceConnectionState(device, outputState,
alsaCardDeviceString,
mDeviceName, TAG);
}
}
// Input Device
if (mHasInput) {
int device = mIsInputHeadset ? AudioSystem.DEVICE_IN_USB_HEADSET
: AudioSystem.DEVICE_IN_USB_DEVICE;
boolean connected = isInputJackConnected();
Slog.i(TAG, "INPUT JACK connected: " + connected);
int inputState = (enable && connected) ? 1 : 0;
if (inputState != mInputState) {
mInputState = inputState;
mAudioService.setWiredDeviceConnectionState(
device, inputState, alsaCardDeviceString,
mDeviceName, TAG);
}
}
} catch (RemoteException e) {
Slog.e(TAG, "RemoteException in setWiredDeviceConnectionState");
}
}
可以看到原生的代码其实是有对输入设备和输出设备有分开切换的,只不过系统合并成了一个方法。这样我们的思路就出来了,我们单独摘出来做处理就行了,还是这个文件。
private boolean mSelectedInput = false;
private boolean mSelectedOutput = false;
public synchronized void startInput() {
mSelectedInput = true;
mInputState = 0;
startJackDetect();
updateWiredDeviceConnectionStateInput(true);
}
public synchronized void startOutput() {
mSelectedOutput = true;
mOutputState = 0;
startJackDetect();
updateWiredDeviceConnectionStateOutput(true);
}
public synchronized void stopInput() {
if (!mSelectedOutput) {
stopJackDetect();
}
updateWiredDeviceConnectionStateInput(false);
mSelectedInput = false;
}
public synchronized void stopOutput() {
if (!mSelectedInput) {
stopJackDetect();
}
updateWiredDeviceConnectionStateOutput(false);
mSelectedOutput = false;
}
public synchronized void updateWiredDeviceConnectionStateOutput(boolean enable) {
if (!mSelectedOutput) {
Slog.e(TAG, "updateWiredDeviceConnectionStateOutput on unselected AlsaDevice!");
return;
}
String alsaCardDeviceString = getAlsaCardDeviceString();
if (alsaCardDeviceString == null) {
return;
}
try {
// Output Device
if (mHasOutput) {
int device = mIsOutputHeadset
? AudioSystem.DEVICE_OUT_USB_HEADSET
: AudioSystem.DEVICE_OUT_USB_DEVICE;
if (DEBUG) {
Slog.d(TAG, "pre-call device:0x" + Integer.toHexString(device)
+ " addr:" + alsaCardDeviceString
+ " name:" + mDeviceName);
}
boolean connected = isOutputJackConnected();
Slog.i(TAG, "OUTPUT JACK connected: " + connected);
int outputState = (enable && connected) ? 1 : 0;
if (outputState != mOutputState) {
mOutputState = outputState;
mAudioService.setWiredDeviceConnectionState(device, outputState,
alsaCardDeviceString,
mDeviceName, TAG);
}
}
} catch (RemoteException e) {
Slog.e(TAG, "RemoteE