Skip to content

Commit e136587

Browse files
committed
EventHub: Refactor AssociatedDevice creation logic
Refactor the AssociatedDevice creation logic to make it easier to implement upcoming changes to AssociatedDevice initialization. In particular, we want to make it simple to differentiate the case where an existing AssociatedDevice needs to be updated from the case where a new AssociatedDevice needs to be created. Bug: 245989146 Bug: 357090960 Test: Presubmit Flag: EXEMPT refactor Change-Id: I8d86f56f3c00f9ae13232a60e338ff7ec246ce3b
1 parent 6cd42cb commit e136587

2 files changed

Lines changed: 33 additions & 31 deletions

File tree

services/inputflinger/reader/EventHub.cpp

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,41 +1620,47 @@ std::shared_ptr<const EventHub::AssociatedDevice> EventHub::obtainAssociatedDevi
16201620

16211621
const auto& path = *sysfsRootPathOpt;
16221622

1623-
std::shared_ptr<const AssociatedDevice> associatedDevice = std::make_shared<AssociatedDevice>(
1624-
AssociatedDevice{.sysfsRootPath = path,
1625-
.batteryInfos = readBatteryConfiguration(path),
1626-
.lightInfos = readLightsConfiguration(path),
1627-
.layoutInfo = readLayoutConfiguration(path)});
1628-
1629-
bool associatedDeviceChanged = false;
1623+
std::shared_ptr<const AssociatedDevice> associatedDevice;
16301624
for (const auto& [id, dev] : mDevices) {
1631-
if (dev->associatedDevice && dev->associatedDevice->sysfsRootPath == path) {
1632-
if (*associatedDevice != *dev->associatedDevice) {
1633-
associatedDeviceChanged = true;
1634-
dev->associatedDevice = associatedDevice;
1635-
}
1625+
if (!dev->associatedDevice || dev->associatedDevice->sysfsRootPath != path) {
1626+
continue;
1627+
}
1628+
if (!associatedDevice) {
1629+
// Found matching associated device for the first time.
16361630
associatedDevice = dev->associatedDevice;
1631+
// Reload this associated device if needed.
1632+
const auto reloadedDevice = AssociatedDevice(path);
1633+
if (reloadedDevice != *dev->associatedDevice) {
1634+
ALOGI("The AssociatedDevice changed for path '%s'. Using new AssociatedDevice: %s",
1635+
path.c_str(), associatedDevice->dump().c_str());
1636+
associatedDevice = std::make_shared<AssociatedDevice>(std::move(reloadedDevice));
1637+
}
16371638
}
1639+
// Update the associatedDevice.
1640+
dev->associatedDevice = associatedDevice;
1641+
}
1642+
1643+
if (!associatedDevice) {
1644+
// No existing associated device found for this path, so create a new one.
1645+
associatedDevice = std::make_shared<AssociatedDevice>(path);
16381646
}
1639-
ALOGI_IF(associatedDeviceChanged,
1640-
"The AssociatedDevice changed for path '%s'. Using new AssociatedDevice: %s",
1641-
path.c_str(), associatedDevice->dump().c_str());
16421647

16431648
return associatedDevice;
16441649
}
16451650

1651+
EventHub::AssociatedDevice::AssociatedDevice(const std::filesystem::path& sysfsRootPath)
1652+
: sysfsRootPath(sysfsRootPath),
1653+
batteryInfos(readBatteryConfiguration(sysfsRootPath)),
1654+
lightInfos(readLightsConfiguration(sysfsRootPath)),
1655+
layoutInfo(readLayoutConfiguration(sysfsRootPath)) {}
1656+
16461657
bool EventHub::AssociatedDevice::isChanged() const {
1647-
std::unordered_map<int32_t, RawBatteryInfo> newBatteryInfos =
1648-
readBatteryConfiguration(sysfsRootPath);
1649-
std::unordered_map<int32_t, RawLightInfo> newLightInfos =
1650-
readLightsConfiguration(sysfsRootPath);
1651-
std::optional<RawLayoutInfo> newLayoutInfo = readLayoutConfiguration(sysfsRootPath);
1652-
1653-
if (newBatteryInfos == batteryInfos && newLightInfos == lightInfos &&
1654-
newLayoutInfo == layoutInfo) {
1655-
return false;
1656-
}
1657-
return true;
1658+
return AssociatedDevice(sysfsRootPath) != *this;
1659+
}
1660+
1661+
std::string EventHub::AssociatedDevice::dump() const {
1662+
return StringPrintf("path=%s, numBatteries=%zu, numLight=%zu", sysfsRootPath.c_str(),
1663+
batteryInfos.size(), lightInfos.size());
16581664
}
16591665

16601666
void EventHub::vibrate(int32_t deviceId, const VibrationElement& element) {
@@ -2972,9 +2978,4 @@ void EventHub::monitor() const {
29722978
std::unique_lock<std::mutex> lock(mLock);
29732979
}
29742980

2975-
std::string EventHub::AssociatedDevice::dump() const {
2976-
return StringPrintf("path=%s, numBatteries=%zu, numLight=%zu", sysfsRootPath.c_str(),
2977-
batteryInfos.size(), lightInfos.size());
2978-
}
2979-
29802981
} // namespace android

services/inputflinger/reader/include/EventHub.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ class EventHub : public EventHubInterface {
619619
private:
620620
// Holds information about the sysfs device associated with the Device.
621621
struct AssociatedDevice {
622+
AssociatedDevice(const std::filesystem::path& sysfsRootPath);
622623
// The sysfs root path of the misc device.
623624
std::filesystem::path sysfsRootPath;
624625
std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> batteryInfos;

0 commit comments

Comments
 (0)