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>



目录
相关文章
|
6月前
|
存储 缓存 Java
dex、vdex、.odex与.oat
dex、vdex、.odex与.oat
321 8
|
存储 API Android开发
getExternalFilesDir到底是什么
getExternalFilesDir对应的目录是/sdcard/Android/data/包名/files/... 可以看到它主要是用来存放应用私有的一些文件。这个目录有几个特性:
1565 0
|
6月前
|
Android开发
Android监听USB设备插拔
Android监听USB设备插拔
825 7
|
6月前
|
监控 测试技术 API
Python Web应用程序构建
【4月更文挑战第11天】Python Web开发涉及多种框架,如Django、Flask和FastAPI,选择合适框架是成功的关键。示例展示了使用Flask创建简单Web应用,以及如何使用ORM(如SQLAlchemy)管理数据库。
59321 7
|
6月前
|
Java Android开发
Android 触摸音的播放
Android 触摸音的播放
44 5
|
6月前
|
Ubuntu
ubuntu下使用ndk编译libevnet
ubuntu下使用ndk编译libevnet
68 1
|
6月前
|
Java Android开发
Android 导航方式切换
Android 导航方式切换
115 1
|
6月前
|
数据建模 Java 开发工具
Android bugreport的使用
Android bugreport的使用
96 0
|
Shell Android开发
Android 如何将 data 分区格式由 ext4 转为 f2fs
Android 如何将 data 分区格式由 ext4 转为 f2fs
796 0
|
API Android开发
Android11.0(R) framework 新增类 lint 编码检查问题
Android11.0(R) framework 新增类 lint 编码检查问题
1310 0