FileDocCategorySizeDatePackage
UsbHostManager.javaAPI DocAndroid 5.1 API11186Thu Mar 12 22:22:42 GMT 2015com.android.server.usb

UsbHostManager

public class UsbHostManager extends Object
UsbHostManager manages USB state in host mode.

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private final HashMap
mDevices
private final String[]
mHostBlacklist
private final android.content.Context
mContext
private final Object
mLock
private android.hardware.usb.UsbDevice
mNewDevice
private android.hardware.usb.UsbConfiguration
mNewConfiguration
private android.hardware.usb.UsbInterface
mNewInterface
private ArrayList
mNewConfigurations
private ArrayList
mNewInterfaces
private ArrayList
mNewEndpoints
private UsbAudioManager
mUsbAudioManager
private UsbSettingsManager
mCurrentSettings
Constructors Summary
public UsbHostManager(android.content.Context context)

        mContext = context;
        mHostBlacklist = context.getResources().getStringArray(
                com.android.internal.R.array.config_usbHostBlacklist);
        mUsbAudioManager = new UsbAudioManager(context);
    
Methods Summary
private voidaddUsbConfiguration(int id, java.lang.String name, int attributes, int maxPower)

        if (mNewConfiguration != null) {
            mNewConfiguration.setInterfaces(
                    mNewInterfaces.toArray(new UsbInterface[mNewInterfaces.size()]));
            mNewInterfaces.clear();
        }

        mNewConfiguration = new UsbConfiguration(id, name, attributes, maxPower);
        mNewConfigurations.add(mNewConfiguration);
    
private voidaddUsbEndpoint(int address, int attributes, int maxPacketSize, int interval)

        mNewEndpoints.add(new UsbEndpoint(address, attributes, maxPacketSize, interval));
    
private voidaddUsbInterface(int id, java.lang.String name, int altSetting, int Class, int subClass, int protocol)

        if (mNewInterface != null) {
            mNewInterface.setEndpoints(
                    mNewEndpoints.toArray(new UsbEndpoint[mNewEndpoints.size()]));
            mNewEndpoints.clear();
        }

        mNewInterface = new UsbInterface(id, altSetting, name, Class, subClass, protocol);
        mNewInterfaces.add(mNewInterface);
    
private booleanbeginUsbDeviceAdded(java.lang.String deviceName, int vendorID, int productID, int deviceClass, int deviceSubclass, int deviceProtocol, java.lang.String manufacturerName, java.lang.String productName, java.lang.String serialNumber)


        if (DEBUG) {
            Slog.d(TAG, "usb:UsbHostManager.beginUsbDeviceAdded(" + deviceName + ")");
            // Audio Class Codes:
            // Audio: 0x01
            // Audio Subclass Codes:
            // undefined: 0x00
            // audio control: 0x01
            // audio streaming: 0x02
            // midi streaming: 0x03

            // some useful debugging info
            Slog.d(TAG, "usb: nm:" + deviceName + " vnd:" + vendorID + " prd:" + productID + " cls:"
                    + deviceClass + " sub:" + deviceSubclass + " proto:" + deviceProtocol);
        }

        // OK this is non-obvious, but true. One can't tell if the device being attached is even
        // potentially an audio device without parsing the interface descriptors, so punt on any
        // such test until endUsbDeviceAdded() when we have that info.

        if (isBlackListed(deviceName) ||
                isBlackListed(deviceClass, deviceSubclass, deviceProtocol)) {
            return false;
        }

        synchronized (mLock) {
            if (mDevices.get(deviceName) != null) {
                Slog.w(TAG, "device already on mDevices list: " + deviceName);
                return false;
            }

            if (mNewDevice != null) {
                Slog.e(TAG, "mNewDevice is not null in endUsbDeviceAdded");
                return false;
            }

            mNewDevice = new UsbDevice(deviceName, vendorID, productID,
                    deviceClass, deviceSubclass, deviceProtocol,
                    manufacturerName, productName, serialNumber);

            mNewConfigurations = new ArrayList<UsbConfiguration>();
            mNewInterfaces = new ArrayList<UsbInterface>();
            mNewEndpoints = new ArrayList<UsbEndpoint>();
        }

        return true;
    
public voiddump(java.io.FileDescriptor fd, java.io.PrintWriter pw)

        synchronized (mLock) {
            pw.println("  USB Host State:");
            for (String name : mDevices.keySet()) {
                pw.println("    " + name + ": " + mDevices.get(name));
            }
        }
        mUsbAudioManager.dump(fd, pw);
    
private voidendUsbDeviceAdded()

        if (DEBUG) {
            Slog.d(TAG, "usb:UsbHostManager.endUsbDeviceAdded()");
        }
        if (mNewInterface != null) {
            mNewInterface.setEndpoints(
                    mNewEndpoints.toArray(new UsbEndpoint[mNewEndpoints.size()]));
        }
        if (mNewConfiguration != null) {
            mNewConfiguration.setInterfaces(
                    mNewInterfaces.toArray(new UsbInterface[mNewInterfaces.size()]));
        }


        synchronized (mLock) {
            if (mNewDevice != null) {
                mNewDevice.setConfigurations(
                        mNewConfigurations.toArray(new UsbConfiguration[mNewConfigurations.size()]));
                mDevices.put(mNewDevice.getDeviceName(), mNewDevice);
                Slog.d(TAG, "Added device " + mNewDevice);
                getCurrentSettings().deviceAttached(mNewDevice);
                mUsbAudioManager.deviceAdded(mNewDevice);
            } else {
                Slog.e(TAG, "mNewDevice is null in endUsbDeviceAdded");
            }
            mNewDevice = null;
            mNewConfigurations = null;
            mNewInterfaces = null;
            mNewEndpoints = null;
        }
    
private UsbSettingsManagergetCurrentSettings()

        synchronized (mLock) {
            return mCurrentSettings;
        }
    
public voidgetDeviceList(android.os.Bundle devices)

        synchronized (mLock) {
            for (String name : mDevices.keySet()) {
                devices.putParcelable(name, mDevices.get(name));
            }
        }
    
private booleanisBlackListed(java.lang.String deviceName)

        int count = mHostBlacklist.length;
        for (int i = 0; i < count; i++) {
            if (deviceName.startsWith(mHostBlacklist[i])) {
                return true;
            }
        }
        return false;
    
private booleanisBlackListed(int clazz, int subClass, int protocol)

        // blacklist hubs
        if (clazz == UsbConstants.USB_CLASS_HUB) return true;

        // blacklist HID boot devices (mouse and keyboard)
        if (clazz == UsbConstants.USB_CLASS_HID &&
                subClass == UsbConstants.USB_INTERFACE_SUBCLASS_BOOT) {
            return true;
        }

        return false;
    
private native voidmonitorUsbHostBus()

private native android.os.ParcelFileDescriptornativeOpenDevice(java.lang.String deviceName)

public android.os.ParcelFileDescriptoropenDevice(java.lang.String deviceName)

        synchronized (mLock) {
            if (isBlackListed(deviceName)) {
                throw new SecurityException("USB device is on a restricted bus");
            }
            UsbDevice device = mDevices.get(deviceName);
            if (device == null) {
                // if it is not in mDevices, it either does not exist or is blacklisted
                throw new IllegalArgumentException(
                        "device " + deviceName + " does not exist or is restricted");
            }
            getCurrentSettings().checkPermission(device);
            return nativeOpenDevice(deviceName);
        }
    
public voidsetCurrentSettings(UsbSettingsManager settings)

        synchronized (mLock) {
            mCurrentSettings = settings;
        }
    
public voidsystemReady()

        synchronized (mLock) {
            // Create a thread to call into native code to wait for USB host events.
            // This thread will call us back on usbDeviceAdded and usbDeviceRemoved.
            Runnable runnable = new Runnable() {
                public void run() {
                    monitorUsbHostBus();
                }
            };
            new Thread(null, runnable, "UsbService host thread").start();
        }
    
private voidusbDeviceRemoved(java.lang.String deviceName)

        synchronized (mLock) {
            UsbDevice device = mDevices.remove(deviceName);
            if (device != null) {
                mUsbAudioManager.deviceRemoved(device);
                getCurrentSettings().deviceDetached(device);
            }
        }