Add listener for the initial battery status

Listen to the initial charging status as DPO mode
depends on it;
Change ALOG to LOC_LOG.

Change-Id: Ic61f76038f0e464b18ade8c9b20c058de35748c8
CRs-fixed: 2544828
This commit is contained in:
Yingjie Wang 2019-10-14 18:21:15 +08:00 committed by Gerrit - the friendly Code Review server
parent 5c568ca1bd
commit ae1d9031ef
5 changed files with 45 additions and 34 deletions

View file

@ -56,6 +56,8 @@ void location_on_battery_status_changed(bool charging) {
Gnss::Gnss() {
ENTRY_LOG_CALLFLOW();
sGnss = this;
// initilize gnss interface at first in case needing notify battery status
sGnss->getGnssInterface()->initialize();
// register health client to listen on battery change
loc_extn_battery_properties_listener_init(location_on_battery_status_changed);
// clear pending GnssConfig

View file

@ -97,6 +97,8 @@ void location_on_battery_status_changed(bool charging) {
Gnss::Gnss() {
ENTRY_LOG_CALLFLOW();
sGnss = this;
// initilize gnss interface at first in case needing notify battery status
sGnss->getGnssInterface()->initialize();
// register health client to listen on battery change
loc_extn_battery_properties_listener_init(location_on_battery_status_changed);
// clear pending GnssConfig

View file

@ -96,6 +96,8 @@ void location_on_battery_status_changed(bool charging) {
Gnss::Gnss() {
ENTRY_LOG_CALLFLOW();
sGnss = this;
// initilize gnss interface at first in case needing notify battery status
sGnss->getGnssInterface()->initialize();
// register health client to listen on battery change
loc_extn_battery_properties_listener_init(location_on_battery_status_changed);
// clear pending GnssConfig

View file

@ -26,6 +26,9 @@ LOCAL_SHARED_LIBRARIES := \
android.hardware.power@1.2 \
libbase
LOCAL_HEADER_LIBRARIES := \
libgps.utils_headers \
LOCAL_STATIC_LIBRARIES := libhealthhalutils
LOCAL_CFLAGS += -DBATTERY_LISTENER_ENABLED

View file

@ -31,12 +31,15 @@
#undef LOG_TAG
#endif
#define LOG_TAG "LocSvc_BatteryListener"
#define LOG_NDEBUG 0
#include <android/hidl/manager/1.0/IServiceManager.h>
#include <android/hardware/health/2.0/IHealth.h>
#include <healthhalutils/HealthHalUtils.h>
#include <hidl/HidlTransportSupport.h>
#include <thread>
#include <log_util.h>
using android::hardware::interfacesEqual;
using android::hardware::Return;
using android::hardware::Void;
@ -99,24 +102,25 @@ status_t BatteryListenerImpl::init()
} while(tries < GET_HEALTH_SVC_RETRY_CNT);
if (mHealth == NULL) {
ALOGE("no health service found, retries %d", tries);
LOC_LOGe("no health service found, retries %d", tries);
return NO_INIT;
} else {
ALOGI("Get health service in %d tries", tries);
LOC_LOGi("Get health service in %d tries", tries);
}
mStatus = BatteryStatus::UNKNOWN;
auto ret = mHealth->getChargeStatus([&](Result r, BatteryStatus status) {
if (r != Result::SUCCESS) {
ALOGE("batterylistener: cannot get battery status");
LOC_LOGe("batterylistener: cannot get battery status");
return;
}
mStatus = status;
});
if (!ret.isOk())
ALOGE("batterylistener: get charge status transaction error");
if (mStatus == BatteryStatus::UNKNOWN)
ALOGW("batterylistener: init: invalid battery status");
if (!ret.isOk()) {
LOC_LOGe("batterylistener: get charge status transaction error");
}
if (mStatus == BatteryStatus::UNKNOWN) {
LOC_LOGw("batterylistener: init: invalid battery status");
}
mDone = false;
mThread = std::make_unique<std::thread>([this]() {
std::unique_lock<std::mutex> l(mLock);
@ -146,7 +150,7 @@ status_t BatteryListenerImpl::init()
}
default:
bool c = statusToBool(local_status);
ALOGI("healthInfo cb thread: cb %s", c ? "CHARGING" : "NOT CHARGING");
LOC_LOGi("healthInfo cb thread: cb %s", c ? "CHARGING" : "NOT CHARGING");
l.unlock();
mCb(c);
l.lock();
@ -156,13 +160,14 @@ status_t BatteryListenerImpl::init()
});
auto reg = mHealth->registerCallback(this);
if (!reg.isOk()) {
ALOGE("Transaction error in registeringCb to HealthHAL death: %s",
LOC_LOGe("Transaction error in registeringCb to HealthHAL death: %s",
reg.description().c_str());
}
auto linked = mHealth->linkToDeath(this, 0 /* cookie */);
if (!linked.isOk() || linked == false) {
ALOGE("Transaction error in linking to HealthHAL death: %s", linked.description().c_str());
LOC_LOGe("Transaction error in linking to HealthHAL death: %s",
linked.description().c_str());
}
return NO_ERROR;
}
@ -181,7 +186,7 @@ BatteryListenerImpl::~BatteryListenerImpl()
mHealth->unlinkToDeath(this);
auto r = mHealth->unlinkToDeath(this);
if (!r.isOk() || r == false) {
ALOGE("Transaction error in unregister to HealthHAL death: %s",
LOC_LOGe("Transaction error in unregister to HealthHAL death: %s",
r.description().c_str());
}
}
@ -195,10 +200,10 @@ void BatteryListenerImpl::serviceDied(uint64_t cookie __unused,
{
std::lock_guard<std::mutex> _l(mLock);
if (mHealth == NULL || !interfacesEqual(mHealth, who.promote())) {
ALOGE("health not initialized or unknown interface died");
LOC_LOGe("health not initialized or unknown interface died");
return;
}
ALOGI("health service died, reinit");
LOC_LOGi("health service died, reinit");
mDone = true;
}
mThread->join();
@ -212,9 +217,8 @@ void BatteryListenerImpl::serviceDied(uint64_t cookie __unused,
// NOT_CHARGING and CHARGING concurrencies.
// Replace single var by a list if this assumption is broken
Return<void> BatteryListenerImpl::healthInfoChanged(
const hardware::health::V2_0::HealthInfo& info)
{
ALOGV("healthInfoChanged: %d", info.legacy.batteryStatus);
const hardware::health::V2_0::HealthInfo& info) {
LOC_LOGv("healthInfoChanged: %d", info.legacy.batteryStatus);
std::unique_lock<std::mutex> l(mLock);
if (info.legacy.batteryStatus != mStatus) {
mStatus = info.legacy.batteryStatus;
@ -224,29 +228,29 @@ Return<void> BatteryListenerImpl::healthInfoChanged(
}
static sp<BatteryListenerImpl> batteryListener;
status_t batteryPropertiesListenerInit(BatteryListenerImpl::cb_fn_t cb)
{
ALOGV("batteryPropertiesListenerInit entry");
bool batteryPropertiesListenerIsCharging() {
return batteryListener->isCharging();
}
status_t batteryPropertiesListenerInit(BatteryListenerImpl::cb_fn_t cb) {
batteryListener = new BatteryListenerImpl(cb);
bool isCharging = batteryPropertiesListenerIsCharging();
LOC_LOGv("charging status: %s charging", isCharging ? "" : "not");;
if (isCharging) {
cb(isCharging);
}
return NO_ERROR;
}
status_t batteryPropertiesListenerDeinit()
{
status_t batteryPropertiesListenerDeinit() {
batteryListener.clear();
return OK;
}
bool batteryPropertiesListenerIsCharging()
{
return batteryListener->isCharging();
}
} // namespace android
void loc_extn_battery_properties_listener_init(battery_status_change_fn_t fn)
{
ALOGV("loc_extn_battery_properties_listener_init entry");
void loc_extn_battery_properties_listener_init(battery_status_change_fn_t fn) {
if (!sIsBatteryListened) {
std::thread t1(android::batteryPropertiesListenerInit,
[=](bool charging) { fn(charging); });
@ -255,12 +259,10 @@ void loc_extn_battery_properties_listener_init(battery_status_change_fn_t fn)
}
}
void loc_extn_battery_properties_listener_deinit()
{
void loc_extn_battery_properties_listener_deinit() {
android::batteryPropertiesListenerDeinit();
}
bool loc_extn_battery_properties_is_charging()
{
bool loc_extn_battery_properties_is_charging() {
return android::batteryPropertiesListenerIsCharging();
}