vibrator: Add TW_SUPPORT_INPUT_AIDL_HAPTICS_FIX_OFF

Vibration cannot be stopped after triggering in some Xiaomi sm8350 devices.
Tested on Xiaomi sm8350 devices: Redmi K40 Pro+ (haydnpro) and Xiaomi 11T Pro (vili).

Change-Id: Ia5988bede5af7def90a265eb527ba4b4ad10de9d
diff --git a/minuitwrp/events.cpp b/minuitwrp/events.cpp
index 5ae7846..b5e5a14 100644
--- a/minuitwrp/events.cpp
+++ b/minuitwrp/events.cpp
@@ -27,6 +27,9 @@
 #include <stdio.h>
 #include <string.h>
 #include <fstream>
+#ifdef USE_QTI_AIDL_HAPTICS_FIX_OFF
+#include <thread>
+#endif
 
 #ifdef USE_QTI_HAPTICS
 #include <android/hardware/vibrator/1.2/IVibrator.h>
@@ -41,6 +44,9 @@
 #else
 static const std::string kVibratorInstance = std::string(IVibrator::descriptor) + "/default";
 #endif
+#ifdef USE_QTI_AIDL_HAPTICS_FIX_OFF
+static std::atomic_int vib_on_count = 0;
+#endif
 #endif
 
 #include "common.h"
@@ -154,7 +160,18 @@
 #elif defined(USE_QTI_AIDL_HAPTICS)
     std::shared_ptr<IVibrator> vib = IVibrator::fromBinder(ndk::SpAIBinder(AServiceManager_getService(kVibratorInstance.c_str())));
     if (vib != nullptr) {
+#ifdef USE_QTI_AIDL_HAPTICS_FIX_OFF
+        std::thread([vib, timeout_ms] {
+            if (vib->on((uint32_t)timeout_ms, nullptr).isOk()) {
+                vib_on_count++;
+                usleep(timeout_ms * 1000);
+                vib_on_count--;
+                if (!vib_on_count) vib->off();
+            }
+        }).detach();
+#else
         vib->on((uint32_t)timeout_ms, nullptr);
+#endif
     }
 #elif defined(USE_SAMSUNG_HAPTICS)
     /* Newer Samsung devices have duration file only
diff --git a/minuitwrp/libminuitwrp_defaults.go b/minuitwrp/libminuitwrp_defaults.go
index d58ec96..bbd6624 100644
--- a/minuitwrp/libminuitwrp_defaults.go
+++ b/minuitwrp/libminuitwrp_defaults.go
@@ -23,6 +23,10 @@
 		cflags = append(cflags, "-DUSE_QTI_AIDL_HAPTICS_FQNAME="+getMakeVars(ctx, "TW_SUPPORT_INPUT_AIDL_HAPTICS_FQNAME"))
 	}
 
+	if getMakeVars(ctx, "TW_SUPPORT_INPUT_AIDL_HAPTICS_FIX_OFF") == "true" {
+		cflags = append(cflags, "-DUSE_QTI_AIDL_HAPTICS_FIX_OFF")
+	}
+
 	if getMakeVars(ctx, "TW_USE_SAMSUNG_HAPTICS") == "true" {
                 cflags = append(cflags, "-DUSE_SAMSUNG_HAPTICS")
         }