Use AIDL HAL for Dev GC

Test: run idle maint
Bug: 177470478

Change-Id: Iefddf8628e31f020b86ff56dfb4926c2c9a2a888
diff --git a/Android.bp b/Android.bp
index 1c0ed17..861abba 100644
--- a/Android.bp
+++ b/Android.bp
@@ -155,6 +155,8 @@
     },
     shared_libs: [
         "android.hardware.health.storage@1.0",
+        "android.hardware.health.storage-unstable-ndk_platform",
+        "libbinder_ndk",
     ],
     whole_static_libs: [
         "com.android.sysprop.apex",
@@ -184,6 +186,8 @@
 
     shared_libs: [
         "android.hardware.health.storage@1.0",
+        "android.hardware.health.storage-unstable-ndk_platform",
+        "libbinder_ndk",
     ],
 }
 
diff --git a/IdleMaint.cpp b/IdleMaint.cpp
index 466cf19..4c3041b 100644
--- a/IdleMaint.cpp
+++ b/IdleMaint.cpp
@@ -24,11 +24,14 @@
 #include <thread>
 #include <utility>
 
+#include <aidl/android/hardware/health/storage/BnGarbageCollectCallback.h>
+#include <aidl/android/hardware/health/storage/IStorage.h>
 #include <android-base/chrono_utils.h>
 #include <android-base/file.h>
 #include <android-base/logging.h>
 #include <android-base/stringprintf.h>
 #include <android-base/strings.h>
+#include <android/binder_manager.h>
 #include <android/hardware/health/storage/1.0/IStorage.h>
 #include <fs_mgr.h>
 #include <private/android_filesystem_config.h>
@@ -49,9 +52,14 @@
 using android::base::WriteStringToFile;
 using android::hardware::Return;
 using android::hardware::Void;
+using AStorage = aidl::android::hardware::health::storage::IStorage;
+using ABnGarbageCollectCallback =
+        aidl::android::hardware::health::storage::BnGarbageCollectCallback;
+using AResult = aidl::android::hardware::health::storage::Result;
 using HStorage = android::hardware::health::storage::V1_0::IStorage;
 using HGarbageCollectCallback = android::hardware::health::storage::V1_0::IGarbageCollectCallback;
 using HResult = android::hardware::health::storage::V1_0::Result;
+using std::string_literals::operator""s;
 
 namespace android {
 namespace vold {
@@ -301,9 +309,9 @@
     return;
 }
 
-enum class IDL { HIDL };
+enum class IDL { HIDL, AIDL };
 std::ostream& operator<<(std::ostream& os, IDL idl) {
-    return os << "HIDL";
+    return os << (idl == IDL::HIDL ? "HIDL" : "AIDL");
 }
 
 template <IDL idl, typename Result>
@@ -338,6 +346,14 @@
     Result mResult{Result::UNKNOWN_ERROR};
 };
 
+class AGcCallbackImpl : public ABnGarbageCollectCallback,
+                        public GcCallbackImpl<IDL::AIDL, AResult> {
+    ndk::ScopedAStatus onFinish(AResult result) override {
+        onFinishInternal(result);
+        return ndk::ScopedAStatus::ok();
+    }
+};
+
 class HGcCallbackImpl : public HGarbageCollectCallback, public GcCallbackImpl<IDL::HIDL, HResult> {
     Return<void> onFinish(HResult result) override {
         onFinishInternal(result);
@@ -358,6 +374,21 @@
 }
 
 static void runDevGc(void) {
+    auto aidl_service_name = AStorage::descriptor + "/default"s;
+    if (AServiceManager_isDeclared(aidl_service_name.c_str())) {
+        ndk::SpAIBinder binder(AServiceManager_waitForService(aidl_service_name.c_str()));
+        if (binder.get() != nullptr) {
+            std::shared_ptr<AStorage> aidl_service = AStorage::fromBinder(binder);
+            if (aidl_service != nullptr) {
+                runDevGcOnHal<IDL::AIDL>(aidl_service, ndk::SharedRefBase::make<AGcCallbackImpl>(),
+                                         &ndk::ScopedAStatus::getDescription);
+                return;
+            }
+        }
+        LOG(WARNING) << "Device declares " << aidl_service_name
+                     << " but it is not running, skip dev GC on AIDL HAL";
+        return;
+    }
     auto hidl_service = HStorage::getService();
     if (hidl_service != nullptr) {
         runDevGcOnHal<IDL::HIDL>(hidl_service, sp<HGcCallbackImpl>(new HGcCallbackImpl()),