1787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian/*
2787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * Copyright (C) 2012 The Android Open Source Project
3787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian *
4787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * you may not use this file except in compliance with the License.
6787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * You may obtain a copy of the License at
7787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian *
8787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian *
10787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * Unless required by applicable law or agreed to in writing, software
11787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * See the License for the specific language governing permissions and
14787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * limitations under the License.
15787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian */
16787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
17787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <stdint.h>
18787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <math.h>
19787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <sys/types.h>
20787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
21787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <utils/Atomic.h>
22787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <utils/Errors.h>
23787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <utils/Singleton.h>
24787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
25787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <binder/BinderService.h>
26787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <binder/Parcel.h>
27787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
28787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include "BatteryService.h"
29787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
30787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopiannamespace android {
31787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian// ---------------------------------------------------------------------------
32787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
33787ac1b388f144f5e6dd38f8b807866a5256dafcMathias AgopianBatteryService::BatteryService() {
34787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    const sp<IServiceManager> sm(defaultServiceManager());
35787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    if (sm != NULL) {
36ee83397bb07ef6807ca3f4d691da7d4fd7d632f0Dianne Hackborn        const String16 name("batterystats");
3763ff1c644f785c78adbf65b9abf6b998cee2c7a1Mike Lockwood        mBatteryStatService = interface_cast<IBatteryStats>(sm->getService(name));
38787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    }
39787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian}
40787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
41787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopianbool BatteryService::addSensor(uid_t uid, int handle) {
42787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    Mutex::Autolock _l(mActivationsLock);
43787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    Info key(uid, handle);
44787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    ssize_t index = mActivations.indexOf(key);
45787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    if (index < 0) {
46787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        index = mActivations.add(key);
47787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    }
48787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    Info& info(mActivations.editItemAt(index));
49787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    info.count++;
50787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    return info.count == 1;
51787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian}
52787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
53787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopianbool BatteryService::removeSensor(uid_t uid, int handle) {
54787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    Mutex::Autolock _l(mActivationsLock);
55787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    ssize_t index = mActivations.indexOf(Info(uid, handle));
56787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    if (index < 0) return false;
57787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    Info& info(mActivations.editItemAt(index));
58787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    info.count--;
59787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    return info.count == 0;
60787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian}
61787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
62787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
63787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopianvoid BatteryService::enableSensorImpl(uid_t uid, int handle) {
64787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    if (mBatteryStatService != 0) {
65787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        if (addSensor(uid, handle)) {
66787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian            int64_t identity = IPCThreadState::self()->clearCallingIdentity();
6763ff1c644f785c78adbf65b9abf6b998cee2c7a1Mike Lockwood            mBatteryStatService->noteStartSensor(uid, handle);
68787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian            IPCThreadState::self()->restoreCallingIdentity(identity);
69787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        }
70787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    }
71787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian}
72787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopianvoid BatteryService::disableSensorImpl(uid_t uid, int handle) {
73787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    if (mBatteryStatService != 0) {
74787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        if (removeSensor(uid, handle)) {
75787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian            int64_t identity = IPCThreadState::self()->clearCallingIdentity();
7663ff1c644f785c78adbf65b9abf6b998cee2c7a1Mike Lockwood            mBatteryStatService->noteStopSensor(uid, handle);
77787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian            IPCThreadState::self()->restoreCallingIdentity(identity);
78787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        }
79787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    }
80787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian}
81787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
82787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopianvoid BatteryService::cleanupImpl(uid_t uid) {
83787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    if (mBatteryStatService != 0) {
84787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        Mutex::Autolock _l(mActivationsLock);
85787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        int64_t identity = IPCThreadState::self()->clearCallingIdentity();
86787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        for (ssize_t i=0 ; i<mActivations.size() ; i++) {
87787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian            const Info& info(mActivations[i]);
88787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian            if (info.uid == uid) {
8963ff1c644f785c78adbf65b9abf6b998cee2c7a1Mike Lockwood                mBatteryStatService->noteStopSensor(info.uid, info.handle);
90787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian                mActivations.removeAt(i);
91787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian                i--;
92787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian            }
93787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        }
94787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        IPCThreadState::self()->restoreCallingIdentity(identity);
95787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    }
96787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian}
97787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
98787ac1b388f144f5e6dd38f8b807866a5256dafcMathias AgopianANDROID_SINGLETON_STATIC_INSTANCE(BatteryService)
99787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
100787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian// ---------------------------------------------------------------------------
101787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian}; // namespace android
102787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
103