Android13禁用外置USB鼠标、键盘

打开//frameworks/native/services/inputflinger/reader/EventHub.cpp,找到openDeviceLocked(const std::string& devicePath)函数

void EventHub::openDeviceLocked(const std::string& devicePath) {
    // If an input device happens to register around the time when EventHub's constructor runs, it
    // is possible that the same input event node (for example, /dev/input/event3) will be noticed
    // in both 'inotify' callback and also in the 'scanDirLocked' pass. To prevent duplicate devices
    // from getting registered, ensure that this path is not already covered by an existing device.
    for (const auto& [deviceId, device] : mDevices) {
        if (device->path == devicePath) {
            return; // device was already registered
        }
    }
   ............................................

if (registerDeviceForEpollLocked(*device) != OK) {
        return;
    }

之前添加代码:

 if (device->classes.any(InputDeviceClass::JOYSTICK | InputDeviceClass::DPAD) &&
        device->classes.test(InputDeviceClass::GAMEPAD)) {
        device->controllerNumber = getNextControllerNumberLocked(device->identifier.name);
        device->setLedForControllerLocked();
    }

    //add code
    int index=devicePath.find(DEVICE_INPUT_PATH_EVENT);
    if(index!=-1){
        char isPeripheralEnable[PROPERTY_VALUE_MAX]={0x00};
        property_get("persist.sys.peripheral",isPeripheralEnable, "1");       
        int usbkeyboard=device->identifier.name.find("USB Keyboard");
        int externDevice=device->classes.string().find("EXTERNAL");
        bool isusbkeyborad=false;
        bool isMouse=false;
	if((usbkeyboard!=-1)&&(externDevice!=-1)){
            ALOGE("find usb keyboard");
            isusbkeyborad=true;
        }
    int usbmouse=device->identifier.name.find("Mouse");
	if((usbmouse!=-1)&&(externDevice!=-1)){
            ALOGE("find usb mouse");
            isMouse=true;
        }
        if (strcmp(isPeripheralEnable,"0")==0) {            
            if(isusbkeyborad||isMouse){
                ALOGE("Opening device,need return");            	
            	return;
	   }
        }
        else
            ALOGE("Opening device,not need return");
    }
    //add end  
    if (registerDeviceForEpollLocked(*device) != OK) {
        return;
    }

通过设置系统属性persist.sys.peripheral的值,控制是否禁用外置USB鼠标、键盘。

你可能感兴趣的:(Android,Framework)