|
| 1 | +#include "platform.hpp" |
| 2 | +#if CF_ANDROID |
| 3 | + |
| 4 | +#include <string> |
| 5 | +#include <string_view> |
| 6 | +#include <vector> |
| 7 | + |
| 8 | +#include "cufetch/fmt/format.h" |
| 9 | +#include "core-modules.hh" |
| 10 | +#include "switch_fnv1a.hpp" |
| 11 | +#include "cufetch/common.hh" |
| 12 | +#include "json.h" |
| 13 | +#include "util.hpp" |
| 14 | + |
| 15 | +static json::jobject doc; |
| 16 | +static std::vector<std::string> dumpsys; |
| 17 | + |
| 18 | +static bool assert_doc() |
| 19 | +{ |
| 20 | + if (doc.size() <= 0) |
| 21 | + { |
| 22 | + std::string result; |
| 23 | + if (!read_exec({ "/data/data/com.termux/files/usr/libexec/termux-api", "BatteryStatus" }, result)) |
| 24 | + return false; |
| 25 | + if (!json::jobject::tryparse(result.c_str(), doc)) |
| 26 | + return false; |
| 27 | + } |
| 28 | + return true; |
| 29 | +} |
| 30 | + |
| 31 | +static bool assert_dumpsys() |
| 32 | +{ |
| 33 | + if (dumpsys.size() <= 0) |
| 34 | + { |
| 35 | + std::string result; |
| 36 | + if (!read_exec({ "/system/bin/dumpsys", "battery" }, result)) |
| 37 | + return false; |
| 38 | + dumpsys = split(result, '\n'); |
| 39 | + if (dumpsys[0] != "Current Battery Service state:") |
| 40 | + return false; |
| 41 | + } |
| 42 | + return true; |
| 43 | +} |
| 44 | + |
| 45 | +static std::string read_value_dumpsys(const std::string_view name, const bool is_status = false) |
| 46 | +{ |
| 47 | + if (!assert_dumpsys()) |
| 48 | + return MAGIC_LINE; |
| 49 | + |
| 50 | + for (size_t i = 1; i < dumpsys.size(); ++i) |
| 51 | + { |
| 52 | + const size_t pos = dumpsys.at(i).rfind(':'); |
| 53 | + const std::string& key = dumpsys.at(i).substr(2, pos); |
| 54 | + const std::string& value = dumpsys.at(i).substr(pos + 2); |
| 55 | + |
| 56 | + if (is_status) |
| 57 | + { |
| 58 | + if (key.find("powered") != key.npos && value == "true") |
| 59 | + return key; |
| 60 | + } |
| 61 | + |
| 62 | + else if (key == name) |
| 63 | + return value; |
| 64 | + } |
| 65 | + |
| 66 | + return MAGIC_LINE; |
| 67 | +} |
| 68 | + |
| 69 | +MODFUNC(battery_modelname) |
| 70 | +{ return MAGIC_LINE; } |
| 71 | + |
| 72 | +MODFUNC(battery_vendor) |
| 73 | +{ return MAGIC_LINE; } |
| 74 | + |
| 75 | +MODFUNC(battery_capacity_level) |
| 76 | +{ return MAGIC_LINE; } |
| 77 | + |
| 78 | +MODFUNC(battery_status) |
| 79 | +{ |
| 80 | + if (!assert_doc()) |
| 81 | + return read_value_dumpsys("powered", true); |
| 82 | + |
| 83 | + switch (fnv1a16::hash(doc["plugged"].as_string())) |
| 84 | + { |
| 85 | + case "PLUGGED_AC"_fnv1a16: return "AC Connected, " + str_tolower(doc["status"].as_string()); |
| 86 | + case "PLUGGED_USB"_fnv1a16: return "USB Connected, " + str_tolower(doc["status"].as_string()); |
| 87 | + case "PLUGGED_WIRELESS"_fnv1a16: return "Wireless Connected, "+ str_tolower(doc["status"].as_string()); |
| 88 | + default: return "Discharging"; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +MODFUNC(battery_technology) |
| 93 | +{ |
| 94 | + if (!assert_doc()) |
| 95 | + return read_value_dumpsys("technology"); |
| 96 | + return MAGIC_LINE; |
| 97 | +} |
| 98 | + |
| 99 | +MODFUNC(battery_perc) |
| 100 | +{ |
| 101 | + if (!assert_doc()) |
| 102 | + { |
| 103 | + double level = std::stod(read_value_dumpsys("level")); |
| 104 | + double scale = std::stod(read_value_dumpsys("scale")); |
| 105 | + if (level > 0 && scale > 0) |
| 106 | + return fmt::to_string(level * 100 / scale); |
| 107 | + } |
| 108 | + |
| 109 | + return doc["percentage"]; |
| 110 | +} |
| 111 | + |
| 112 | +double battery_temp() |
| 113 | +{ |
| 114 | + if (!assert_doc()) |
| 115 | + return std::stod(read_value_dumpsys("temperature")) / 10; |
| 116 | + return doc["temperature"]; |
| 117 | +} |
| 118 | + |
| 119 | +#endif |
0 commit comments