Remove the trailing new line from output of sysfs capacity_level file.
Some input device kernel driver populates capacity_level string with
new line, need to trim it before looking it up.
Bug: 180925649
Test: Manual test with Nintendo Switch Pro controller
Change-Id: Iae1744075c56ad7f95060d07bfe9f254e1a5f3dc
diff --git a/services/inputflinger/reader/EventHub.cpp b/services/inputflinger/reader/EventHub.cpp
index e939d1c..d298691 100644
--- a/services/inputflinger/reader/EventHub.cpp
+++ b/services/inputflinger/reader/EventHub.cpp
@@ -37,6 +37,7 @@
// #define LOG_NDEBUG 0
#include <android-base/file.h>
+#include <android-base/strings.h>
#include <android-base/stringprintf.h>
#include <cutils/properties.h>
#include <input/KeyCharacterMap.h>
@@ -1361,13 +1362,14 @@
// Some devices report battery capacity as an integer through the "capacity" file
if (base::ReadFileToString(device->sysfsBatteryPath.value() / "capacity", &buffer)) {
- return std::stoi(buffer);
+ return std::stoi(base::Trim(buffer));
}
// Other devices report capacity as an enum value POWER_SUPPLY_CAPACITY_LEVEL_XXX
// These values are taken from kernel source code include/linux/power_supply.h
if (base::ReadFileToString(device->sysfsBatteryPath.value() / "capacity_level", &buffer)) {
- const auto it = BATTERY_LEVEL.find(buffer);
+ // Remove any white space such as trailing new line
+ const auto it = BATTERY_LEVEL.find(base::Trim(buffer));
if (it != BATTERY_LEVEL.end()) {
return it->second;
}
@@ -1389,9 +1391,8 @@
return std::nullopt;
}
- // Remove trailing new line
- buffer.erase(std::remove(buffer.begin(), buffer.end(), '\n'), buffer.end());
- const auto it = BATTERY_STATUS.find(buffer);
+ // Remove white space like trailing new line
+ const auto it = BATTERY_STATUS.find(base::Trim(buffer));
if (it != BATTERY_STATUS.end()) {
return it->second;