Merge "minadbd: Return battery level via getprop." am: e0e34a7f5c am: 58b5a5a550 am: a216efcb8d
am: fe12eead1b

Change-Id: Ic4a0e9bea04c7d2c17803c46ce98bf5dd7c1a592
diff --git a/minadbd/Android.bp b/minadbd/Android.bp
index 0717125..b7075e6 100644
--- a/minadbd/Android.bp
+++ b/minadbd/Android.bp
@@ -40,6 +40,7 @@
 
     defaults: [
         "minadbd_defaults",
+        "librecovery_utils_defaults",
     ],
 
     srcs: [
@@ -48,6 +49,7 @@
     ],
 
     static_libs: [
+        "librecovery_utils",
         "libotautil",
     ],
 
@@ -97,6 +99,7 @@
 
     defaults: [
         "minadbd_defaults",
+        "librecovery_utils_defaults",
     ],
 
     srcs: [
@@ -107,6 +110,7 @@
     static_libs: [
         "libminadbd_services",
         "libfusesideload",
+        "librecovery_utils",
         "libotautil",
         "libadbd",
     ],
diff --git a/minadbd/minadbd_services.cpp b/minadbd/minadbd_services.cpp
index cabcdaa..eb91fb3 100644
--- a/minadbd/minadbd_services.cpp
+++ b/minadbd/minadbd_services.cpp
@@ -44,6 +44,7 @@
 #include "fuse_adb_provider.h"
 #include "fuse_sideload.h"
 #include "minadbd/types.h"
+#include "recovery_utils/battery_utils.h"
 #include "services.h"
 #include "sysdeps.h"
 
@@ -160,7 +161,10 @@
 // If given an empty string, dumps all the supported properties (analogous to `adb shell getprop`)
 // in lines, e.g. "[prop]: [value]".
 static void RescueGetpropHostService(unique_fd sfd, const std::string& prop) {
+  constexpr const char* kRescueBatteryLevelProp = "rescue.battery_level";
   static const std::set<std::string> kGetpropAllowedProps = {
+    // clang-format off
+    kRescueBatteryLevelProp,
     "ro.build.date.utc",
     "ro.build.fingerprint",
     "ro.build.flavor",
@@ -170,18 +174,28 @@
     "ro.build.version.incremental",
     "ro.product.device",
     "ro.product.vendor.device",
+    // clang-format on
   };
+
+  auto query_prop = [](const std::string& key) {
+    if (key == kRescueBatteryLevelProp) {
+      auto battery_info = GetBatteryInfo();
+      return std::to_string(battery_info.capacity);
+    }
+    return android::base::GetProperty(key, "");
+  };
+
   std::string result;
   if (prop.empty()) {
     for (const auto& key : kGetpropAllowedProps) {
-      auto value = android::base::GetProperty(key, "");
+      auto value = query_prop(key);
       if (value.empty()) {
         continue;
       }
       result += "[" + key + "]: [" + value + "]\n";
     }
   } else if (kGetpropAllowedProps.find(prop) != kGetpropAllowedProps.end()) {
-    result = android::base::GetProperty(prop, "") + "\n";
+    result = query_prop(prop) + "\n";
   }
   if (result.empty()) {
     result = "\n";
diff --git a/recovery_utils/Android.bp b/recovery_utils/Android.bp
index 1f42217..bf79a2e 100644
--- a/recovery_utils/Android.bp
+++ b/recovery_utils/Android.bp
@@ -75,6 +75,7 @@
     visibility: [
         "//bootable/recovery",
         "//bootable/recovery/install",
+        "//bootable/recovery/minadbd",
         "//bootable/recovery/tests",
     ],
 }