healthd: move global gHealth service pointer

... to class implementation::Health. Expose APIs
InitInstance(), GetImplmentation()
for its setter and getter.

Clients that statically links to android.hardware
.health@2.0-impl (e.g. charger, recovery)
should use InitInstance().

Test: health VTS tests
Bug: 63702641
Change-Id: I51b9b976d0b723dac1a03e5113d5d9e84300c0e9
diff --git a/healthd/healthd_common.cpp b/healthd/healthd_common.cpp
index 19e600f..140c49d 100644
--- a/healthd/healthd_common.cpp
+++ b/healthd/healthd_common.cpp
@@ -91,7 +91,7 @@
 #ifndef HEALTHD_USE_HEALTH_2_0
 static BatteryMonitor* gBatteryMonitor = nullptr;
 #else
-extern sp<::android::hardware::health::V2_0::IHealth> gHealth;
+using ::android::hardware::health::V2_0::implementation::Health;
 #endif
 
 struct healthd_mode_ops *healthd_mode_ops = nullptr;
@@ -160,42 +160,42 @@
     status_t err = UNKNOWN_ERROR;
     switch (id) {
         case BATTERY_PROP_CHARGE_COUNTER: {
-            gHealth->getChargeCounter([&](Result r, int32_t v) {
+            Health::getImplementation()->getChargeCounter([&](Result r, int32_t v) {
                 err = convertStatus(r);
                 val->valueInt64 = v;
             });
             break;
         }
         case BATTERY_PROP_CURRENT_NOW: {
-            gHealth->getCurrentNow([&](Result r, int32_t v) {
+            Health::getImplementation()->getCurrentNow([&](Result r, int32_t v) {
                 err = convertStatus(r);
                 val->valueInt64 = v;
             });
             break;
         }
         case BATTERY_PROP_CURRENT_AVG: {
-            gHealth->getCurrentAverage([&](Result r, int32_t v) {
+            Health::getImplementation()->getCurrentAverage([&](Result r, int32_t v) {
                 err = convertStatus(r);
                 val->valueInt64 = v;
             });
             break;
         }
         case BATTERY_PROP_CAPACITY: {
-            gHealth->getCapacity([&](Result r, int32_t v) {
+            Health::getImplementation()->getCapacity([&](Result r, int32_t v) {
                 err = convertStatus(r);
                 val->valueInt64 = v;
             });
             break;
         }
         case BATTERY_PROP_ENERGY_COUNTER: {
-            gHealth->getEnergyCounter([&](Result r, int64_t v) {
+            Health::getImplementation()->getEnergyCounter([&](Result r, int64_t v) {
                 err = convertStatus(r);
                 val->valueInt64 = v;
             });
             break;
         }
         case BATTERY_PROP_BATTERY_STATUS: {
-            gHealth->getChargeStatus([&](Result r, BatteryStatus v) {
+            Health::getImplementation()->getChargeStatus([&](Result r, BatteryStatus v) {
                 err = convertStatus(r);
                 val->valueInt64 = static_cast<int64_t>(v);
             });
@@ -237,7 +237,7 @@
 #ifndef HEALTHD_USE_HEALTH_2_0
     healthd_battery_update_internal(gBatteryMonitor->update());
 #else
-    gHealth->update();
+    Health::getImplementation()->update();
 #endif
 }
 
@@ -249,7 +249,7 @@
     nativeHandle->data[0] = fd;
     ::android::hardware::hidl_handle handle;
     handle.setTo(nativeHandle, true /* shouldOwn */);
-    gHealth->debug(handle, {} /* options */);
+    Health::getImplementation()->debug(handle, {} /* options */);
 #endif
 
     fsync(fd);