赞
踩
最近处理了一个需求,就是控制系统选择的麦克风和扬声器。原生的逻辑当插上一个既是麦克风又是扬声器的设备时,会去同时切换,与我们的需求不符。
原生逻辑看以下的代码
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, "RemoteException in setWiredDeviceConnectionState");
- }
- }
-
- public synchronized void updateWiredDeviceConnectionStateInput(boolean enable) {
- if (!mSelectedInput) {
- Slog.e(TAG, "updateWiredDeviceConnectionStateInput on unselected AlsaDevice!");
- return;
- }
- String alsaCardDeviceString = getAlsaCardDeviceString();
- if (alsaCardDeviceString == null) {
- return;
- }
- try {
- // 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");
- }
- }

这里其实可以看到,我们并没有写一行代码,都是参照原生的代码,将输出和输入逻辑代码分隔开。接下来来到管理类
/frameworks/base/services/usb/java/com/android/server/usb/UsbAlsaManager.java
- /*
- * Copyright (C) 2014 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- package com.android.server.usb;
-
- import android.content.Context;
- import android.content.pm.PackageManager;
- import android.content.res.Resources;
- import android.hardware.usb.UsbDevice;
- import android.media.IAudioService;
- import android.media.midi.MidiDeviceInfo;
- import android.os.Bundle;
- import android.os.ServiceManager;
- import android.provider.Settings;
- import android.service.usb.UsbAlsaManagerProto;
- import android.util.Slog;
-
- import com.android.internal.alsa.AlsaCardsParser;
- import com.android.internal.util.dump.DualDumpOutputStream;
- import com.android.server.usb.descriptors.UsbDescriptorParser;
-
- import libcore.io.IoUtils;
-
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import android.os.SystemProperties;
- import android.os.UserHandle;
- import android.hardware.usb.UsbManager;
- import android.content.Intent;
- /**
- * UsbAlsaManager manages USB audio and MIDI devices.
- */
- public final class UsbAlsaManager {
- private static final String TAG = UsbAlsaManager.class.getSimpleName();
- private static final boolean DEBUG = false;
-
- // Flag to turn on/off multi-peripheral select mode
- // Set to true to have single-device-only mode
- private static final boolean mIsSingleMode = true;
-
- private static final String ALSA_DIRECTORY = "/dev/snd/";
-
- private final Context mContext;
- private IAudioService mAudioService;
- private final boolean mHasMidiFeature;
-
- private final AlsaCardsParser mCardsParser = new AlsaCardsParser();
-
- // this is needed to map USB devices to ALSA Audio Devices, especially to remove an
- // ALSA device when we are notified that its associated USB device has been removed.
- private final ArrayList<UsbAlsaDevice> mAlsaDevices = new ArrayList<UsbAlsaDevice>();
- private UsbAlsaDevice mSelectedInputDevice;
- private UsbAlsaDevice mSelectedOutputDevice;
-
- //
- // Device Denylist
- //
- // This exists due to problems with Sony game controllers which present as an audio device
- // even if no headset is connected and have no way to set the volume on the unit.
- // Handle this by simply declining to use them as an audio device.
- private static final int USB_VENDORID_SONY = 0x054C;
- private static final int USB_PRODUCTID_PS4CONTROLLER_ZCT1 = 0x05C4;
- private static final int USB_PRODUCTID_PS4CONTROLLER_ZCT2 = 0x09CC;
-
- private static final int USB_DENYLIST_OUTPUT = 0x0001;
- private static final int USB_DENYLIST_INPUT = 0x0002;
-
- private static class DenyListEntry {
- final int mVendorId;
- final int mProductId;
- final int mFlags;
-
- DenyListEntry(int vendorId, int productId, int flags) {
- mVendorId = vendorId;
- mProductId = productId;
- mFlags = flags;
- }
- }
-
- static final List<DenyListEntry> sDeviceDenylist = Arrays.asList(
- new DenyListEntry(USB_VENDORID_SONY,
- USB_PRODUCTID_PS4CONTROLLER_ZCT1,
- USB_DENYLIST_OUTPUT),
- new DenyListEntry(USB_VENDORID_SONY,
- USB_PRODUCTID_PS4CONTROLLER_ZCT2,
- USB_DENYLIST_OUTPUT));
-
- private static boolean isDeviceDenylisted(int vendorId, int productId, int flags) {
- for (DenyListEntry entry : sDeviceDenylist) {
- if (entry.mVendorId == vendorId && entry.mProductId == productId) {
- // see if the type flag is set
- return (entry.mFlags & flags) != 0;
- }
- }
-
- return false;
- }
-
- // UsbMidiDevice for USB peripheral mode (gadget) device
- private UsbMidiDevice mPeripheralMidiDevice = null;
-
- /* package */ UsbAlsaManager(Context context) {
- mContext = context;
- mHasMidiFeature = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MIDI);
- }
-
- public void systemReady() {
- mAudioService = IAudioService.Stub.asInterface(
- ServiceManager.getService(Context.AUDIO_SERVICE));
- }
-
- public synchronized void selectAlsaInputDeviceByUsbDevice(UsbDevice usbDevice) {
- if (mAlsaDevices == null || mAlsaDevices.size() == 0) {
- return ;
- }
- List<UsbAlsaDevice> data = new ArrayList<UsbAlsaDevice>(mAlsaDevices);
- for (UsbAlsaDevice usbAlsaDevice : data) {
- if (usbDevice.getDeviceName().equals(usbAlsaDevice.getDeviceAddress())) {
- selectAlsaInputDevice(usbAlsaDevice);
- }
- }
- }
-
-
- public synchronized void selectAlsaOutputDeviceByUsbDevice(UsbDevice usbDevice) {
- if (mAlsaDevices == null || mAlsaDevices.size() == 0) {
- return ;
- }
- List<UsbAlsaDevice> data = new ArrayList<UsbAlsaDevice>(mAlsaDevices);
- for (UsbAlsaDevice usbAlsaDevice : data) {
- if (usbDevice.getDeviceName().equals(usbAlsaDevice.getDeviceAddress())) {
- selectAlsaOutputDevice(usbAlsaDevice);
- }
- }
- }
-
- public synchronized void deselectAlsaInputDeviceByNothing() {
- deselectAlsaInputDevice();
- }
-
- public synchronized void deselectAlsaOutputDeviceByNothing() {
- deselectAlsaOutputDevice();
- }
-
- /**
- * Select the AlsaDevice to be used for AudioService.
- * AlsaDevice.start() notifies AudioService of it's connected state.
- *
- * @param alsaDevice The selected UsbAlsaDevice for system USB audio.
- */
- private synchronized void selectAlsaInputDevice(UsbAlsaDevice alsaDevice) {
- if (DEBUG) {
- Slog.d(TAG, "selectAlsaInputDevice() " + alsaDevice);
- }
-
- // This must be where an existing USB audio device is deselected.... (I think)
- if (mIsSingleMode && mSelectedInputDevice != null) {
- deselectAlsaInputDevice();
- }
-
- // FIXME Does not yet handle the case where the setting is changed
- // after device connection. Ideally we should handle the settings change
- // in SettingsObserver. Here we should log that a USB device is connected
- // and disconnected with its address (card , device) and force the
- // connection or disconnection when the setting changes.
- int isDisabled = Settings.Secure.getInt(mContext.getContentResolver(),
- Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, 0);
- if (isDisabled != 0) {
- return;
- }
-
- mSelectedInputDevice = alsaDevice;
- SystemProperties.set("sys.hardware.mic.selected", alsaDevice.getDeviceAddress());
- alsaDevice.startInput();
- if (DEBUG) {
- Slog.d(TAG, "selectAlsaDevice() - done.");
- }
- }
-
- private synchronized void selectAlsaOutputDevice(UsbAlsaDevice alsaDevice) {
- if (DEBUG) {
- Slog.d(TAG, "selectAlsaOutputDevice() " + alsaDevice);
- }
-
- // This must be where an existing USB audio device is deselected.... (I think)
- if (mIsSingleMode && mSelectedOutputDevice != null) {
- deselectAlsaOutputDevice();
- }
-
- // FIXME Does not yet handle the case where the setting is changed
- // after device connection. Ideally we should handle the settings change
- // in SettingsObserver. Here we should log that a USB device is connected
- // and disconnected with its address (card , device) and force the
- // connection or disconnection when the setting changes.
- int isDisabled = Settings.Secure.getInt(mContext.getContentResolver(),
- Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, 0);
- if (isDisabled != 0) {
- return;
- }
-
- mSelectedOutputDevice = alsaDevice;
- SystemProperties.set("sys.hardware.usbspeaker.selected", alsaDevice.getDeviceAddress());
- alsaDevice.startOutput();
- if (DEBUG) {
- Slog.d(TAG, "selectAlsaDevice() - done.");
- }
- }
-
- private synchronized void deselectAlsaInputDevice() {
- if (DEBUG) {
- Slog.d(TAG, "deselectAlsaInputDevice() mSelectedDevice " + mSelectedInputDevice);
- }
- if (mSelectedInputDevice != null) {
- mSelectedInputDevice.stopInput();
- mSelectedInputDevice = null;
- SystemProperties.set("sys.hardware.mic.selected", "");
- }
- }
-
- private synchronized void deselectAlsaOutputDevice() {
- if (DEBUG) {
- Slog.d(TAG, "deselectAlsaOutputDevice() mSelectedDevice " + mSelectedOutputDevice);
- }
- if (mSelectedOutputDevice != null) {
- mSelectedOutputDevice.stopOutput();
- mSelectedOutputDevice = null;
- SystemProperties.set("sys.hardware.usbspeaker.selected", "");
- }
- }
-
- private int getAlsaDeviceListIndexFor(String deviceAddress) {
- for (int index = 0; index < mAlsaDevices.size(); index++) {
- if (mAlsaDevices.get(index).getDeviceAddress().equals(deviceAddress)) {
- return index;
- }
- }
- return -1;
- }
-
- private UsbAlsaDevice removeAlsaDeviceFromList(String deviceAddress) {
- int index = getAlsaDeviceListIndexFor(deviceAddress);
- if (index > -1) {
- UsbAlsaDevice ret = mAlsaDevices.remove(index);
- persistCurrentState();
- return ret;
- } else {
- return null;
- }
- }
-
- /* package */ UsbAlsaDevice selectDefaultOutputDevice() {
- if (DEBUG) {
- Slog.d(TAG, "selectDefaultDevice()");
- }
-
- /**
- if (mAlsaDevices.size() > 0) {
- for (UsbAlsaDevice dev : mAlsaDevices) {
- if (dev.hasOutput()) {
- selectAlsaOutputDevice(dev);
- break;
- }
- }
- }
-
-
- if (mAlsaDevices.size() > 0) {
- UsbAlsaDevice alsaDevice = mAlsaDevices.get(0);
- if (DEBUG) {
- Slog.d(TAG, " alsaDevice:" + alsaDevice);
- }
- if (alsaDevice != null) {
- selectAlsaDevice(alsaDevice);
- }
- return alsaDevice;
- } else {
- return null;
- }
- **/
- return null;
- }
-
- /* package */ void usbDeviceAdded(String deviceAddress, UsbDevice usbDevice,
- UsbDescriptorParser parser) {
- if (DEBUG) {
- Slog.d(TAG, "usbDeviceAdded(): " + usbDevice.getManufacturerName()
- + " nm:" + usbDevice.getProductName());
- }
-
- // Scan the Alsa File Space
- mCardsParser.scan();
-
- // Find the ALSA spec for this device address
- AlsaCardsParser.AlsaCardRecord cardRec =
- mCardsParser.findCardNumFor(deviceAddress);
- if (cardRec == null) {
- return;
- }
-
- // Add it to the devices list
- boolean hasInput = parser.hasInput()
- && !isDeviceDenylisted(usbDevice.getVendorId(), usbDevice.getProductId(),
- USB_DENYLIST_INPUT);
- boolean hasOutput = parser.hasOutput()
- && !isDeviceDenylisted(usbDevice.getVendorId(), usbDevice.getProductId(),
- USB_DENYLIST_OUTPUT);
- if (DEBUG) {
- Slog.d(TAG, "hasInput: " + hasInput + " hasOutput:" + hasOutput);
- }
- if (hasInput || hasOutput) {
- boolean isInputHeadset = parser.isInputHeadset();
- boolean isOutputHeadset = parser.isOutputHeadset();
- boolean isDock = parser.isDock();
-
- if (mAudioService == null) {
- Slog.e(TAG, "no AudioService");
- return;
- }
-
- UsbAlsaDevice alsaDevice =
- new UsbAlsaDevice(mAudioService, cardRec.getCardNum(), 0 /*device*/,
- deviceAddress, hasOutput, hasInput,
- isInputHeadset, isOutputHeadset, isDock);
- if (alsaDevice != null) {
- alsaDevice.setDeviceNameAndDescription(
- cardRec.getCardName(), cardRec.getCardDescription());
- mAlsaDevices.add(0, alsaDevice);
- persistCurrentState();
- if(mContext!=null){
- Intent i = new Intent("skg.hardware.alsa.device_attached");
- i.putExtra(UsbManager.EXTRA_DEVICE,usbDevice);
- mContext.sendBroadcastAsUser(i,UserHandle.SYSTEM);
- }
- //selectAlsaDevice(alsaDevice);
- /**
- if (alsaDevice.hasOutput()) {
- selectAlsaOutputDevice(alsaDevice);
- }
- **/
- }
- }
-
- logDevices("deviceAdded()");
-
- if (DEBUG) {
- Slog.d(TAG, "deviceAdded() - done");
- }
- }
-
- private void persistCurrentState() {
- if (mContext != null) {
- if (mAlsaDevices == null || mAlsaDevices.size() == 0) {
- return ;
- }
- List<UsbAlsaDevice> data = new ArrayList<UsbAlsaDevice>(mAlsaDevices);
- int idx = 0;
- for (UsbAlsaDevice usbAlsaDevice : data) {
- String value = String.format("%s#%s#%s", usbAlsaDevice.getDeviceAddress(), usbAlsaDevice.hasInput(), usbAlsaDevice.hasOutput());
- Settings.Global.putString(mContext.getContentResolver(), "usb_alsa_device_" + idx, value);
- idx++;
- }
- // what if we had more than 6 devices, crash
- while (idx < 6) {
- Settings.Global.putString(mContext.getContentResolver(), "usb_alsa_device_" + idx, "");
- idx++;
- }
- }
- }
-
- /* package */ synchronized void usbDeviceRemoved(String deviceAddress/*UsbDevice usbDevice*/) {
- if (DEBUG) {
- Slog.d(TAG, "deviceRemoved(" + deviceAddress + ")");
- }
-
- // Audio
- UsbAlsaDevice alsaDevice = removeAlsaDeviceFromList(deviceAddress);
- Slog.i(TAG, "USB Audio Device Removed: " + alsaDevice);
- if (alsaDevice != null && alsaDevice == mSelectedOutputDevice) {
- // deselectAlsaDevice();
- // selectDefaultDevice(); // if there any external devices left, select one of them
- // deselectAlsaOutputDevice();
- // selectDefaultOutputDevice();
- }
-
- logDevices("usbDeviceRemoved()");
-
- }
-
- /* package */ void setPeripheralMidiState(boolean enabled, int card, int device) {
- if (!mHasMidiFeature) {
- return;
- }
-
- if (enabled && mPeripheralMidiDevice == null) {
- Bundle properties = new Bundle();
- Resources r = mContext.getResources();
- properties.putString(MidiDeviceInfo.PROPERTY_NAME, r.getString(
- com.android.internal.R.string.usb_midi_peripheral_name));
- properties.putString(MidiDeviceInfo.PROPERTY_MANUFACTURER, r.getString(
- com.android.internal.R.string.usb_midi_peripheral_manufacturer_name));
- properties.putString(MidiDeviceInfo.PROPERTY_PRODUCT, r.getString(
- com.android.internal.R.string.usb_midi_peripheral_product_name));
- properties.putInt(MidiDeviceInfo.PROPERTY_ALSA_CARD, card);
- properties.putInt(MidiDeviceInfo.PROPERTY_ALSA_DEVICE, device);
- mPeripheralMidiDevice = UsbMidiDevice.create(mContext, properties, card, device,
- 1 /* numInputs */, 1 /* numOutputs */);
- } else if (!enabled && mPeripheralMidiDevice != null) {
- IoUtils.closeQuietly(mPeripheralMidiDevice);
- mPeripheralMidiDevice = null;
- }
- }
-
- //
- // Devices List
- //
- /*
- //import java.util.ArrayList;
- public ArrayList<UsbAudioDevice> getConnectedDevices() {
- ArrayList<UsbAudioDevice> devices = new ArrayList<UsbAudioDevice>(mAudioDevices.size());
- for (HashMap.Entry<UsbDevice,UsbAudioDevice> entry : mAudioDevices.entrySet()) {
- devices.add(entry.getValue());
- }
- return devices;
- }
- */
-
- /**
- * Dump the USB alsa state.
- */
- // invoked with "adb shell dumpsys usb"
- public void dump(DualDumpOutputStream dump, String idName, long id) {
- long token = dump.start(idName, id);
-
- dump.write("cards_parser", UsbAlsaManagerProto.CARDS_PARSER, mCardsParser.getScanStatus());
-
- for (UsbAlsaDevice usbAlsaDevice : mAlsaDevices) {
- usbAlsaDevice.dump(dump, "alsa_devices", UsbAlsaManagerProto.ALSA_DEVICES);
- }
-
- dump.end(token);
- }
-
- public void logDevicesList(String title) {
- if (DEBUG) {
- Slog.i(TAG, title + "----------------");
- for (UsbAlsaDevice alsaDevice : mAlsaDevices) {
- Slog.i(TAG, " -->");
- Slog.i(TAG, "" + alsaDevice);
- Slog.i(TAG, " <--");
- }
- Slog.i(TAG, "----------------");
- }
- }
-
- // This logs a more terse (and more readable) version of the devices list
- public void logDevices(String title) {
- if (DEBUG) {
- Slog.i(TAG, title + "----------------");
- for (UsbAlsaDevice alsaDevice : mAlsaDevices) {
- Slog.i(TAG, alsaDevice.toShortString());
- }
- Slog.i(TAG, "----------------");
- }
- }
- }

原生是在usbDeviceAdded、usbDeviceRemoved方法里面去做设备切换的。我们已经弃用,在selectAlsaOutputDevice、selectAlsaInputDevice做设备选择。
frameworks/base/services/usb/java/com/android/server/usb/UsbService.java
- BroadcastReceiver receiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- final String action = intent.getAction();
- if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED
- .equals(action)) {
- if (mDeviceManager != null) {
- mDeviceManager.updateUserRestrictions();
- }
- } else if (action.equals("com.hardware.action.SELECT_MIC")) {
- UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
- if (mAlsaManager != null) {
- if (device != null) {
- mAlsaManager.selectAlsaInputDeviceByUsbDevice(device);
- } else {
- mAlsaManager.deselectAlsaInputDeviceByNothing();
- }
- }
- } else if (action.equals("com.hardware.action.SELECT_SPEAKER")) {
- UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
- if (mAlsaManager != null) {
- if (device != null) {
- mAlsaManager.selectAlsaOutputDeviceByUsbDevice(device);
- } else {
- mAlsaManager.deselectAlsaOutputDeviceByNothing();
- }
- }
- }
- }
- };

设计思路就是这样
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。