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/Health.cpp b/healthd/Health.cpp
index 74f3eec..d271811 100644
--- a/healthd/Health.cpp
+++ b/healthd/Health.cpp
@@ -13,6 +13,8 @@
namespace V2_0 {
namespace implementation {
+sp<Health> Health::instance_;
+
Health::Health(struct healthd_config* c) {
battery_monitor_ = std::make_unique<BatteryMonitor>();
battery_monitor_->init(c);
@@ -154,7 +156,17 @@
(void)unregisterCallbackInternal(who.promote());
}
-// Methods from ::android::hidl::base::V1_0::IBase follow.
+sp<IHealth> Health::initInstance(struct healthd_config* c) {
+ if (instance_ == nullptr) {
+ instance_ = new Health(c);
+ }
+ return instance_;
+}
+
+sp<Health> Health::getImplementation() {
+ CHECK(instance_ != nullptr);
+ return instance_;
+}
} // namespace implementation
} // namespace V2_0