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

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

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
parent 329808e1
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -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 @@ std::optional<int32_t> EventHub::getBatteryCapacity(int32_t deviceId) const {

    // 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 @@ std::optional<int32_t> EventHub::getBatteryStatus(int32_t deviceId) const {
        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;