twrp: unguard `healthd services` & run battery updation in background

Change-Id: If0eddf2040395d97edd2a0d4fe05264e963fe099
Signed-off-by: Mohd Faraz <androiabledroid@gmail.com>
diff --git a/Android.mk b/Android.mk
index f092e81..c80fec3 100644
--- a/Android.mk
+++ b/Android.mk
@@ -158,8 +158,6 @@
 
 ifeq ($(PRODUCT_USE_DYNAMIC_PARTITIONS),true)
     LOCAL_CFLAGS += -DPRODUCT_USE_DYNAMIC_PARTITIONS=1
-    TWRP_REQUIRED_MODULES += android.hardware.health@2.1-service android.hardware.health@2.1-impl.recovery android.hardware.health@2.1-service.rc android.hardware.health@2.1.xml
-    TWRP_REQUIRED_MODULES += android.hardware.health@2.0-service android.hardware.health@2.0-impl.recovery android.hardware.health@2.0-service.rc
     ifeq ($(TW_EXCLUDE_LPDUMP),)
         TWRP_REQUIRED_MODULES += lpdump lpdumpd.rc
     endif
@@ -478,7 +476,14 @@
     privapp-permissions-twrpapp.xml \
     adbd_system_api_recovery \
     libsync.recovery \
-    libandroidicu.recovery
+    libandroidicu.recovery \
+    android.hardware.health@2.1-service \
+    android.hardware.health@2.1-impl.recovery \
+    android.hardware.health@2.1-service.rc \
+    android.hardware.health@2.1.xml \
+    android.hardware.health@2.0-service \
+    android.hardware.health@2.0-impl.recovery \
+    android.hardware.health@2.0-service.rc
 
 ifneq ($(TW_EXCLUDE_TZDATA), true)
 TWRP_REQUIRED_MODULES += \
diff --git a/data.cpp b/data.cpp
index 4ff0edd..c19fe4c 100644
--- a/data.cpp
+++ b/data.cpp
@@ -36,7 +36,6 @@
 #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
@@ -1032,30 +1031,6 @@
 		value = TWFunc::to_string(convert_temp);
 		return 0;
 	}
-	else if (varName == "tw_battery")
-	{
-		char tmp[16];
-		static char charging = ' ';
-		static int lastVal = -1;
-		static time_t nextSecCheck = 0;
-		struct timeval curTime;
-		gettimeofday(&curTime, NULL);
-		if (curTime.tv_sec > nextSecCheck)
-		{
-			auto battery_info = GetBatteryInfo();
-			if (battery_info.charging) {
-				charging = '+';
-			} else {
-				charging = ' ';
-			}
-			lastVal = battery_info.capacity;
-			nextSecCheck = curTime.tv_sec + 1;
-		}
-
-		sprintf(tmp, "%i%%%c", lastVal, charging);
-		value = tmp;
-		return 0;
-	}
 	return -1;
 }
 
diff --git a/etc/Android.mk b/etc/Android.mk
index 4aa769c..1084b01 100755
--- a/etc/Android.mk
+++ b/etc/Android.mk
@@ -146,32 +146,32 @@
 	include $(BUILD_PREBUILT)
 endif
 
+include $(CLEAR_VARS)
+LOCAL_MODULE := android.hardware.health@2.1-service.rc
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := EXECUTABLES
+LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/system/etc/init
+LOCAL_SRC_FILES := init/$(LOCAL_MODULE)
+include $(BUILD_PREBUILT)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := android.hardware.health@2.1.xml
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := EXECUTABLES
+LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/vendor/etc/vintf/manifest
+LOCAL_SRC_FILES := init/$(LOCAL_MODULE)
+include $(BUILD_PREBUILT)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := android.hardware.health@2.0-service.rc
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := EXECUTABLES
+LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/system/etc/init
+LOCAL_SRC_FILES := init/$(LOCAL_MODULE)
+include $(BUILD_PREBUILT)
+
 ifeq ($(PRODUCT_USE_DYNAMIC_PARTITIONS),true)
 	include $(CLEAR_VARS)
-	LOCAL_MODULE := android.hardware.health@2.1-service.rc
-	LOCAL_MODULE_TAGS := optional
-	LOCAL_MODULE_CLASS := EXECUTABLES
-	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/system/etc/init
-	LOCAL_SRC_FILES := init/$(LOCAL_MODULE)
-	include $(BUILD_PREBUILT)
-
-	include $(CLEAR_VARS)
-	LOCAL_MODULE := android.hardware.health@2.1.xml
-	LOCAL_MODULE_TAGS := optional
-	LOCAL_MODULE_CLASS := EXECUTABLES
-	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/vendor/etc/vintf/manifest
-	LOCAL_SRC_FILES := init/$(LOCAL_MODULE)
-	include $(BUILD_PREBUILT)
-
-	include $(CLEAR_VARS)
-	LOCAL_MODULE := android.hardware.health@2.0-service.rc
-	LOCAL_MODULE_TAGS := optional
-	LOCAL_MODULE_CLASS := EXECUTABLES
-	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/system/etc/init
-	LOCAL_SRC_FILES := init/$(LOCAL_MODULE)
-	include $(BUILD_PREBUILT)
-
-	include $(CLEAR_VARS)
 	LOCAL_MODULE := lpdumpd.rc
 	LOCAL_MODULE_TAGS := optional
 	LOCAL_MODULE_CLASS := EXECUTABLES
diff --git a/twrp.cpp b/twrp.cpp
index bcd03e5..b7f4058 100644
--- a/twrp.cpp
+++ b/twrp.cpp
@@ -24,6 +24,9 @@
 #include <time.h>
 #include <unistd.h>
 #include <signal.h>
+#include <thread>
+#include <chrono>
+#include "recovery_utils/battery_utils.h"
 #include "gui/twmsg.h"
 
 #include "cutils/properties.h"
@@ -420,6 +423,32 @@
 	// Load up all the resources
 	gui_loadResources();
 
+	std::string value;
+	static char charging = ' ';
+	static int lastVal = -1;
+
+	// Function to monitor battery in the background
+	auto monitorBatteryInBackground = [&]() {
+		while (true) {
+			auto battery_info = GetBatteryInfo();
+			if (battery_info.charging) {
+				charging = '+';
+			} else {
+				charging = ' ';
+			}
+			lastVal = battery_info.capacity;
+			// Format the value based on the background updates
+			value = std::to_string(lastVal) + "%" + charging;
+			DataManager::SetValue("tw_battery", value);
+
+			// Sleep for a specified interval (e.g., 1 second) before checking again
+			std::this_thread::sleep_for(std::chrono::seconds(1));
+		}
+	};
+
+	// Create a thread for battery monitoring
+	static std::thread battery_monitor(monitorBatteryInBackground);
+
 	twrpAdbBuFifo *adb_bu_fifo = new twrpAdbBuFifo();
 	TWFunc::Clear_Bootloader_Message();