twrp: use health services for battery capacity

Change-Id: I99dfb82238e2b3ae90932af0c7fd74822d971232
Signed-off-by: Mohd Faraz <androiabledroid@gmail.com>
diff --git a/Android.mk b/Android.mk
old mode 100755
new mode 100644
index 3bf7968..f092e81
--- a/Android.mk
+++ b/Android.mk
@@ -79,8 +79,8 @@
     LOCAL_CFLAGS += -DTW_EXCLUDE_APEX
 endif
 
-LOCAL_STATIC_LIBRARIES += libavb libtwrpinstall libminadbd_services libinit libsnapshot_nobinder update_metadata-protos
-LOCAL_SHARED_LIBRARIES += libfs_mgr libhardware android.hardware.boot@1.0 android.hardware.boot@1.1 android.hardware.boot@1.2 libprotobuf-cpp-lite liblp libutils libhidlbase
+LOCAL_STATIC_LIBRARIES += libavb libtwrpinstall libminadbd_services libinit libsnapshot_nobinder update_metadata-protos librecovery_utils libhealthhalutils
+LOCAL_SHARED_LIBRARIES += libfs_mgr libhardware android.hardware.boot@1.0 android.hardware.boot@1.1 android.hardware.boot@1.2 libprotobuf-cpp-lite liblp libutils libhidlbase android.hardware.health@2.0
 LOCAL_C_INCLUDES += \
     system/core/fs_mgr/libfs_avb/include/ \
     system/core/fs_mgr/include_fstab/ \
@@ -378,11 +378,6 @@
 ifneq ($(TW_CUSTOM_BATTERY_PATH),)
 	LOCAL_CFLAGS += -DTW_CUSTOM_BATTERY_PATH=$(TW_CUSTOM_BATTERY_PATH)
 endif
-ifneq ($(TW_BATTERY_SYSFS_WAIT_SECONDS),)
-	LOCAL_CFLAGS += -DTW_BATTERY_SYSFS_WAIT_SECONDS=$(TW_BATTERY_SYSFS_WAIT_SECONDS)
-else
-	LOCAL_CFLAGS += -DTW_BATTERY_SYSFS_WAIT_SECONDS=3
-endif
 ifneq ($(TW_CUSTOM_CPU_TEMP_PATH),)
 	LOCAL_CFLAGS += -DTW_CUSTOM_CPU_TEMP_PATH=$(TW_CUSTOM_CPU_TEMP_PATH)
 endif
diff --git a/data.cpp b/data.cpp
old mode 100755
new mode 100644
index b3b69ac..4ff0edd
--- a/data.cpp
+++ b/data.cpp
@@ -36,6 +36,7 @@
 #include "set_metadata.h"
 #include "gui/gui.hpp"
 #include "infomanager.hpp"
+#include "recovery_utils/battery_utils.h"
 
 #define DEVID_MAX 64
 #define HWID_MAX 32
@@ -1041,37 +1042,14 @@
 		gettimeofday(&curTime, NULL);
 		if (curTime.tv_sec > nextSecCheck)
 		{
-			char cap_s[4];
-#ifdef TW_CUSTOM_BATTERY_PATH
-			string capacity_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
-			capacity_file += "/capacity";
-			FILE * cap = fopen(capacity_file.c_str(),"rt");
-#else
-			FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
-#endif
-			if (cap) {
-				fgets(cap_s, 4, cap);
-				fclose(cap);
-				lastVal = atoi(cap_s);
-				if (lastVal > 100)	lastVal = 101;
-				if (lastVal < 0)	lastVal = 0;
+			auto battery_info = GetBatteryInfo();
+			if (battery_info.charging) {
+				charging = '+';
+			} else {
+				charging = ' ';
 			}
-#ifdef TW_CUSTOM_BATTERY_PATH
-			string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
-			status_file += "/status";
-			cap = fopen(status_file.c_str(),"rt");
-#else
-			cap = fopen("/sys/class/power_supply/battery/status","rt");
-#endif
-			if (cap) {
-				fgets(cap_s, 2, cap);
-				fclose(cap);
-				if (cap_s[0] == 'C')
-					charging = '+';
-				else
-					charging = ' ';
-			}
-			nextSecCheck = curTime.tv_sec + 60;
+			lastVal = battery_info.capacity;
+			nextSecCheck = curTime.tv_sec + 1;
 		}
 
 		sprintf(tmp, "%i%%%c", lastVal, charging);
diff --git a/kernel_module_loader.cpp b/kernel_module_loader.cpp
index 88f1d78..73304a6 100644
--- a/kernel_module_loader.cpp
+++ b/kernel_module_loader.cpp
@@ -78,10 +78,6 @@
 
 	android::base::SetProperty("twrp.modules.loaded", "true");
 
-#ifdef TW_BATTERY_SYSFS_WAIT_SECONDS
-	TWFunc::Wait_For_Battery(std::chrono::seconds(TW_BATTERY_SYSFS_WAIT_SECONDS));
-#endif
-
 	return true;
 }
 
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
old mode 100755
new mode 100644
index 38f59ef..0695611
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -424,18 +424,6 @@
 	return false;
 }
 
-bool TWFunc::Wait_For_Battery(std::chrono::nanoseconds timeout) {
-	std::string battery_path;
-#ifdef TW_CUSTOM_BATTERY_PATH
-	battery_path = EXPAND(TW_CUSTOM_BATTERY_PATH);
-#else
-	battery_path = "/sys/class/power_supply/battery";
-#endif
-	if (!battery_path.empty()) return TWFunc::Wait_For_File(battery_path, timeout);
-
-	return false;
-}
-
 #ifndef BUILD_TWRPTAR_MAIN
 
 // Returns "/path" from a full /path/to/file.name
diff --git a/twrp-functions.hpp b/twrp-functions.hpp
old mode 100755
new mode 100644
index 02d51e1..a66f3e0
--- a/twrp-functions.hpp
+++ b/twrp-functions.hpp
@@ -75,7 +75,6 @@
 	static timespec timespec_diff(timespec& start, timespec& end);	            // Return a diff for 2 times
 	static int32_t timespec_diff_ms(timespec& start, timespec& end);            // Returns diff in ms
 	static bool Wait_For_File(const string& path, std::chrono::nanoseconds timeout); // Wait For File, True is success, False is timeout;
-	static bool Wait_For_Battery(std::chrono::nanoseconds timeout);             // Wait For /sys/class/power_supply/battery or TW_CUSTOM_BATTERY_PATH, True is success, False is timeout;
 
 #ifndef BUILD_TWRPTAR_MAIN
 	static int Recursive_Mkdir(string Path);                                    // Recursively makes the entire path