UsbHostManager

简介: UsbHostManager

代码路径 :frameworks\base\services\usb\java\com\android\server\usb\UsbHostManager.java

1.添加usb设备

/* Called from JNI in monitorUsbHostBus() to report new USB devices
       Returns true if successful, i.e. the USB Audio device descriptors are
       correctly parsed and the unique device is added to the audio device list.
     */
    @SuppressWarnings("unused")
    private boolean usbDeviceAdded(String deviceAddress, int deviceClass, int deviceSubclass,
            byte[] descriptors) {
        if (DEBUG) {
            Slog.d(TAG, "usbDeviceAdded(" + deviceAddress + ") - start");
        }
        if (isDenyListed(deviceAddress)) {
            if (DEBUG) {
                Slog.d(TAG, "device address is Deny listed");
            }
            return false;
        }
        if (isDenyListed(deviceClass, deviceSubclass)) {
            if (DEBUG) {
                Slog.d(TAG, "device class is deny listed");
            }
            return false;
        }
        UsbDescriptorParser parser = new UsbDescriptorParser(deviceAddress, descriptors);
        if (deviceClass == UsbConstants.USB_CLASS_PER_INTERFACE
                && !checkUsbInterfacesDenyListed(parser)) {
            return false;
        }
        // Potentially can block as it may read data from the USB device.
        logUsbDevice(parser);
        synchronized (mLock) {
            if (mDevices.get(deviceAddress) != null) {
                Slog.w(TAG, "device already on mDevices list: " + deviceAddress);
                //TODO If this is the same peripheral as is being connected, replace
                // it with the new connection.
                return false;
            }
            UsbDevice.Builder newDeviceBuilder = parser.toAndroidUsbDeviceBuilder();
            if (newDeviceBuilder == null) {
                Slog.e(TAG, "Couldn't create UsbDevice object.");
                // Tracking
                addConnectionRecord(deviceAddress, ConnectionRecord.CONNECT_BADDEVICE,
                        parser.getRawDescriptors());
            } else {
                UsbSerialReader serialNumberReader = new UsbSerialReader(mContext,
                        mPermissionManager, newDeviceBuilder.serialNumber);
                UsbDevice newDevice = newDeviceBuilder.build(serialNumberReader);
                serialNumberReader.setDevice(newDevice);
                mDevices.put(deviceAddress, newDevice);
                Slog.d(TAG, "Added device " + newDevice);
                // It is fine to call this only for the current user as all broadcasts are
                // sent to all profiles of the user and the dialogs should only show once.
                ComponentName usbDeviceConnectionHandler = getUsbDeviceConnectionHandler();
                if (usbDeviceConnectionHandler == null) {
                    getCurrentUserSettings().deviceAttached(newDevice);
                } else {
                    getCurrentUserSettings().deviceAttachedForFixedHandler(newDevice,
                            usbDeviceConnectionHandler);
                }
                mUsbAlsaManager.usbDeviceAdded(deviceAddress, newDevice, parser);
                // Tracking
                addConnectionRecord(deviceAddress, ConnectionRecord.CONNECT,
                        parser.getRawDescriptors());
                // Stats collection
                FrameworkStatsLog.write(FrameworkStatsLog.USB_DEVICE_ATTACHED,
                        newDevice.getVendorId(), newDevice.getProductId(),
                        parser.hasAudioInterface(), parser.hasHIDInterface(),
                        parser.hasStorageInterface(),
                        FrameworkStatsLog.USB_DEVICE_ATTACHED__STATE__STATE_CONNECTED, 0);
            }
        }
        if (DEBUG) {
            Slog.d(TAG, "beginUsbDeviceAdded(" + deviceAddress + ") end");
        }
        return true;
    }

2.拒绝添加的usb设备

private boolean isDenyListed(String deviceAddress) {
        int count = mHostDenyList.length;
        for (int i = 0; i < count; i++) {
            if (deviceAddress.startsWith(mHostDenyList[i])) {
                return true;
            }
        }
        return false;
    }
    /* returns true if the USB device should not be accessible by applications */
    private boolean isDenyListed(int clazz, int subClass) {
        // deny hubs
        if (clazz == UsbConstants.USB_CLASS_HUB) return true;
        // deny HID boot devices (mouse and keyboard)
        return clazz == UsbConstants.USB_CLASS_HID
                && subClass == UsbConstants.USB_INTERFACE_SUBCLASS_BOOT;
    }

3.构造方法

/*
     * UsbHostManager
     */
    public UsbHostManager(Context context, UsbAlsaManager alsaManager,
            UsbPermissionManager permissionManager) {
        mContext = context;
        mHostDenyList = context.getResources().getStringArray(
                com.android.internal.R.array.config_usbHostDenylist);
        mUsbAlsaManager = alsaManager;
        mPermissionManager = permissionManager;
        String deviceConnectionHandler = context.getResources().getString(
                com.android.internal.R.string.config_UsbDeviceConnectionHandling_component);
        if (!TextUtils.isEmpty(deviceConnectionHandler)) {
            setUsbDeviceConnectionHandler(ComponentName.unflattenFromString(
                    deviceConnectionHandler));
        }
    }

4.config_usbHostDenylist  frameworks\base\core\res\res\values\config.xml

<!-- List of file paths for USB host busses to exclude from USB host support.
         For example, if the first USB bus on the device is used to communicate
         with the modem or some other restricted hardware, add "/dev/bus/usb/001/"
         to this list.  If this is empty, no parts of the host USB bus will be excluded.
    -->
    <string-array name="config_usbHostDenylist" translatable="false">
    </string-array>



目录
相关文章
|
7月前
|
存储 缓存 Java
dex、vdex、.odex与.oat
dex、vdex、.odex与.oat
353 8
|
存储 API Android开发
getExternalFilesDir到底是什么
getExternalFilesDir对应的目录是/sdcard/Android/data/包名/files/... 可以看到它主要是用来存放应用私有的一些文件。这个目录有几个特性:
1653 0
|
7月前
|
Android开发
Android监听USB设备插拔
Android监听USB设备插拔
892 7
|
7月前
|
监控 测试技术 API
Python Web应用程序构建
【4月更文挑战第11天】Python Web开发涉及多种框架,如Django、Flask和FastAPI,选择合适框架是成功的关键。示例展示了使用Flask创建简单Web应用,以及如何使用ORM(如SQLAlchemy)管理数据库。
59326 7
|
7月前
|
Java Android开发
Android 触摸音的播放
Android 触摸音的播放
50 5
|
7月前
|
Ubuntu Linux 测试技术
【ZYNQ】简单几步,教你使用 Petalinux 定制 Linux
【ZYNQ】简单几步,教你使用 Petalinux 定制 Linux
331 0
|
Shell Android开发
Android 如何将 data 分区格式由 ext4 转为 f2fs
Android 如何将 data 分区格式由 ext4 转为 f2fs
820 0
|
API Android开发
Android11.0(R) framework 新增类 lint 编码检查问题
Android11.0(R) framework 新增类 lint 编码检查问题
1356 0
|
编解码 Android开发
Android SELinux Enforcing 模式下问题及解决
Android SELinux Enforcing 模式下问题及解决
704 0