summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Peng Xu <pengxu@google.com> 2016-11-29 17:54:50 -0800
committer Peng Xu <pengxu@google.com> 2016-12-17 01:59:57 +0000
commit0f48c45ec06f98b10b17ebe0231b0e60508c135f (patch)
treeb109a153a44eae359576593aa0534189c9c740dc
parentc556562471e8ed27360adae57e17fc4bfa2cfa6c (diff)
[sensorservice] Init batterystats service when needed
This avoid race condition at parallel services start up that may results in sensor power usage not being properly recorded. Test: check all sensors, they still work Bug: 33199244 Bug: 33623300 Change-Id: I48946667db54fc72d9be6c13b981b44d1bab88c2 (cherry picked from commit 0a031596e12ce0f081b9ee116f48f4f4635d516e)
-rw-r--r--services/sensorservice/BatteryService.cpp24
-rw-r--r--services/sensorservice/BatteryService.h1
2 files changed, 16 insertions, 9 deletions
diff --git a/services/sensorservice/BatteryService.cpp b/services/sensorservice/BatteryService.cpp
index 81f32cdc58..452c8c64b0 100644
--- a/services/sensorservice/BatteryService.cpp
+++ b/services/sensorservice/BatteryService.cpp
@@ -30,12 +30,7 @@
namespace android {
// ---------------------------------------------------------------------------
-BatteryService::BatteryService() {
- const sp<IServiceManager> sm(defaultServiceManager());
- if (sm != NULL) {
- const String16 name("batterystats");
- mBatteryStatService = interface_cast<IBatteryStats>(sm->getService(name));
- }
+BatteryService::BatteryService() : mBatteryStatService(nullptr) {
}
bool BatteryService::addSensor(uid_t uid, int handle) {
@@ -61,7 +56,7 @@ bool BatteryService::removeSensor(uid_t uid, int handle) {
void BatteryService::enableSensorImpl(uid_t uid, int handle) {
- if (mBatteryStatService != 0) {
+ if (checkService()) {
if (addSensor(uid, handle)) {
int64_t identity = IPCThreadState::self()->clearCallingIdentity();
mBatteryStatService->noteStartSensor(uid, handle);
@@ -70,7 +65,7 @@ void BatteryService::enableSensorImpl(uid_t uid, int handle) {
}
}
void BatteryService::disableSensorImpl(uid_t uid, int handle) {
- if (mBatteryStatService != 0) {
+ if (checkService()) {
if (removeSensor(uid, handle)) {
int64_t identity = IPCThreadState::self()->clearCallingIdentity();
mBatteryStatService->noteStopSensor(uid, handle);
@@ -80,7 +75,7 @@ void BatteryService::disableSensorImpl(uid_t uid, int handle) {
}
void BatteryService::cleanupImpl(uid_t uid) {
- if (mBatteryStatService != 0) {
+ if (checkService()) {
Mutex::Autolock _l(mActivationsLock);
int64_t identity = IPCThreadState::self()->clearCallingIdentity();
for (size_t i=0 ; i<mActivations.size() ; i++) {
@@ -95,6 +90,17 @@ void BatteryService::cleanupImpl(uid_t uid) {
}
}
+bool BatteryService::checkService() {
+ if (mBatteryStatService == nullptr) {
+ const sp<IServiceManager> sm(defaultServiceManager());
+ if (sm != NULL) {
+ const String16 name("batterystats");
+ mBatteryStatService = interface_cast<IBatteryStats>(sm->getService(name));
+ }
+ }
+ return mBatteryStatService != nullptr;
+}
+
ANDROID_SINGLETON_STATIC_INSTANCE(BatteryService)
// ---------------------------------------------------------------------------
diff --git a/services/sensorservice/BatteryService.h b/services/sensorservice/BatteryService.h
index 08ba857518..43a750c6c2 100644
--- a/services/sensorservice/BatteryService.h
+++ b/services/sensorservice/BatteryService.h
@@ -49,6 +49,7 @@ class BatteryService : public Singleton<BatteryService> {
SortedVector<Info> mActivations;
bool addSensor(uid_t uid, int handle);
bool removeSensor(uid_t uid, int handle);
+ bool checkService();
public:
static void enableSensor(uid_t uid, int handle) {