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>



目录
相关文章
|
Shell
Shell 字符串操作(截取、替换、去头、去尾)
Shell 字符串操作(截取、替换、去头、去尾)
1947 0
|
人工智能 IDE 开发工具
从6人日到1人日:一次AI驱动的客户端需求开发实战
从6人日到1人日:一次AI驱动的客户端需求开发实战
从6人日到1人日:一次AI驱动的客户端需求开发实战
|
人工智能 JavaScript 开发工具
【完全免费】VS Code 最好用的 12 款 AI 代码提示插件!!!
🎉 探索12款免费VSCode AI代码提示插件:Codeium、Codegeex、CodeFuse、TONGYI Lingma、Comate、iFlyCode、Fitten Code、Bito AI、Mintlify Doc Writer、Kodezi AI、aiXcoder、IntelliCode。这些插件提供智能补全、代码生成、注释、优化,支持多种语言,提升编程效率!🚀👩‍💻👨‍💻
32128 121
|
Java 开发工具 Android开发
Android字体Font相关知识
Android字体简介 Android系统默认支持三种字体,分别为:“sans”,“serif”,“monospace”。 android.
5782 0
|
安全 网络安全 API
kotlin安卓开发JetPack Compose 如何使用webview 打开网页时给webview注入cookie
在Jetpack Compose中使用WebView需借助AndroidView。要注入Cookie,首先在`build.gradle`添加WebView依赖,如`androidx.webkit:webkit:1.4.0`。接着创建自定义`ComposableWebView`,通过`CookieManager`设置接受第三方Cookie并注入Cookie字符串。最后在Compose界面使用这个自定义组件加载URL。注意Android 9及以上版本可能需要在网络安全配置中允许第三方Cookie。
|
Shell 开发工具 git
上传文件到gitee(小白都能学会)
上传文件到gitee(小白都能学会)
6809 13
|
人工智能 前端开发 JavaScript
阿里云安全类云产品,验证码使用时滑动验证流程及线上问题排查
阿里云验证码产品,使用业界先进的风控引擎结合“规则+AI”模型,有效区分真实用户和机器自动化脚本攻击,避免机器请求造成业务损失。主要适用于垃圾注册、刷库撞库,薅羊毛,短信被刷等风险场景。为您提供安全可靠的业务环境。本文为大家介绍验证码使用时滑动验证流程及验证不通过的问题排查。
67801 5
阿里云安全类云产品,验证码使用时滑动验证流程及线上问题排查
Linux中的错误码
Linux中的错误码
|
Java Android开发 Windows
使用keytool查看Android APK签名
本文介绍了如何使用Windows命令行工具和keytool查看APK的签名信息,并提供了使用AOSP环境中的signapk.jar工具对APK进行系统签名的方法。
3184 0
使用keytool查看Android APK签名

热门文章

最新文章