Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e14523a4 authored by Chris Ye's avatar Chris Ye
Browse files

Check input device's all sub devices for isEnabled()

We now allow individual sub devices in an input device to be enabled
and disabled, the isEnalbed() function should check all sub devices and return
true if any device is enabled.

Bug: 161634265
Bug: 175953789
Test: atest inputflinger_tests
Change-Id: Ia5b93089f34d464e9c61595af621e3711837171b
parent 5cad1fa9
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -54,10 +54,11 @@ bool InputDevice::isEnabled() {
    if (!hasEventHubDevices()) {
        return false;
    }
    // devices are either all enabled or all disabled, so we only need to check the first
    auto& devicePair = mDevices.begin()->second;
    auto& contextPtr = devicePair.first;
    return contextPtr->isDeviceEnabled();
    // An input device composed of sub devices can be individually enabled or disabled.
    // If any of the sub device is enabled then the input device is considered as enabled.
    bool enabled = false;
    for_each_subdevice([&enabled](auto& context) { enabled |= context.isDeviceEnabled(); });
    return enabled;
}

void InputDevice::setEnabled(bool enabled, nsecs_t when) {
+3 −1
Original line number Diff line number Diff line
@@ -222,7 +222,9 @@ void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
    }

    // Sensor input device is noisy, to save power disable it by default.
    if (device->getClasses().test(InputDeviceClass::SENSOR)) {
    // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
    // device class to disable SENSOR sub device only.
    if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
        mEventHub->disableDevice(eventHubId);
    }
}
+28 −0
Original line number Diff line number Diff line
@@ -1481,6 +1481,32 @@ TEST_F(InputReaderTest, GetMergedInputDevices) {
    ASSERT_EQ(1U, mReader->getInputDevices().size());
}

TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
    constexpr int32_t deviceId = END_RESERVED_ID + 1000;
    constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
    // Add two subdevices to device
    std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
    // Must add at least one mapper or the device will be ignored!
    device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
    device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);

    // Push same device instance for next device to be added, so they'll have same identifier.
    mReader->pushNextDevice(device);
    mReader->pushNextDevice(device);
    // Sensor device is initially disabled
    ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
                                      InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
                                      nullptr));
    // Device is disabled because the only sub device is a sensor device and disabled initially.
    ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
    ASSERT_FALSE(device->isEnabled());
    ASSERT_NO_FATAL_FAILURE(
            addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
    // The merged device is enabled if any sub device is enabled
    ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
    ASSERT_TRUE(device->isEnabled());
}

TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
    constexpr int32_t deviceId = END_RESERVED_ID + 1000;
    constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
@@ -2721,6 +2747,7 @@ TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
    ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
                                    std::chrono::microseconds(10000),
                                    std::chrono::microseconds(0)));
    ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
    process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, 20000);
    process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, -20000);
    process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Z, 40000);
@@ -2750,6 +2777,7 @@ TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
    ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
                                    std::chrono::microseconds(10000),
                                    std::chrono::microseconds(0)));
    ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
    process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RX, 20000);
    process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RY, -20000);
    process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RZ, 40000);