Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0f48c45e authored by Peng Xu's avatar Peng Xu
Browse files

[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 0a031596)
parent c5565624
Loading
Loading
Loading
Loading
+15 −9
Original line number Original line Diff line number Diff line
@@ -30,12 +30,7 @@
namespace android {
namespace android {
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------


BatteryService::BatteryService() {
BatteryService::BatteryService() : mBatteryStatService(nullptr) {
    const sp<IServiceManager> sm(defaultServiceManager());
    if (sm != NULL) {
        const String16 name("batterystats");
        mBatteryStatService = interface_cast<IBatteryStats>(sm->getService(name));
    }
}
}


bool BatteryService::addSensor(uid_t uid, int handle) {
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) {
void BatteryService::enableSensorImpl(uid_t uid, int handle) {
    if (mBatteryStatService != 0) {
    if (checkService()) {
        if (addSensor(uid, handle)) {
        if (addSensor(uid, handle)) {
            int64_t identity = IPCThreadState::self()->clearCallingIdentity();
            int64_t identity = IPCThreadState::self()->clearCallingIdentity();
            mBatteryStatService->noteStartSensor(uid, handle);
            mBatteryStatService->noteStartSensor(uid, handle);
@@ -70,7 +65,7 @@ void BatteryService::enableSensorImpl(uid_t uid, int handle) {
    }
    }
}
}
void BatteryService::disableSensorImpl(uid_t uid, int handle) {
void BatteryService::disableSensorImpl(uid_t uid, int handle) {
    if (mBatteryStatService != 0) {
    if (checkService()) {
        if (removeSensor(uid, handle)) {
        if (removeSensor(uid, handle)) {
            int64_t identity = IPCThreadState::self()->clearCallingIdentity();
            int64_t identity = IPCThreadState::self()->clearCallingIdentity();
            mBatteryStatService->noteStopSensor(uid, handle);
            mBatteryStatService->noteStopSensor(uid, handle);
@@ -80,7 +75,7 @@ void BatteryService::disableSensorImpl(uid_t uid, int handle) {
}
}


void BatteryService::cleanupImpl(uid_t uid) {
void BatteryService::cleanupImpl(uid_t uid) {
    if (mBatteryStatService != 0) {
    if (checkService()) {
        Mutex::Autolock _l(mActivationsLock);
        Mutex::Autolock _l(mActivationsLock);
        int64_t identity = IPCThreadState::self()->clearCallingIdentity();
        int64_t identity = IPCThreadState::self()->clearCallingIdentity();
        for (size_t i=0 ; i<mActivations.size() ; i++) {
        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)
ANDROID_SINGLETON_STATIC_INSTANCE(BatteryService)


// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
+1 −0
Original line number Original line Diff line number Diff line
@@ -49,6 +49,7 @@ class BatteryService : public Singleton<BatteryService> {
    SortedVector<Info> mActivations;
    SortedVector<Info> mActivations;
    bool addSensor(uid_t uid, int handle);
    bool addSensor(uid_t uid, int handle);
    bool removeSensor(uid_t uid, int handle);
    bool removeSensor(uid_t uid, int handle);
    bool checkService();


public:
public:
    static void enableSensor(uid_t uid, int handle) {
    static void enableSensor(uid_t uid, int handle) {