blob: 81f32cdc585ed8c55395c490d8f8bbbfae356fba [file] [log] [blame]
Mathias Agopian787ac1b2012-09-18 18:49:18 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdint.h>
18#include <math.h>
19#include <sys/types.h>
20
21#include <utils/Atomic.h>
22#include <utils/Errors.h>
23#include <utils/Singleton.h>
24
25#include <binder/BinderService.h>
26#include <binder/Parcel.h>
27
28#include "BatteryService.h"
29
30namespace android {
31// ---------------------------------------------------------------------------
32
33BatteryService::BatteryService() {
34 const sp<IServiceManager> sm(defaultServiceManager());
35 if (sm != NULL) {
Dianne Hackbornee833972013-07-15 15:20:11 -070036 const String16 name("batterystats");
Mike Lockwood63ff1c62013-09-25 09:28:41 -070037 mBatteryStatService = interface_cast<IBatteryStats>(sm->getService(name));
Mathias Agopian787ac1b2012-09-18 18:49:18 -070038 }
39}
40
Mathias Agopian787ac1b2012-09-18 18:49:18 -070041bool BatteryService::addSensor(uid_t uid, int handle) {
42 Mutex::Autolock _l(mActivationsLock);
43 Info key(uid, handle);
44 ssize_t index = mActivations.indexOf(key);
45 if (index < 0) {
46 index = mActivations.add(key);
47 }
48 Info& info(mActivations.editItemAt(index));
49 info.count++;
50 return info.count == 1;
51}
52
53bool BatteryService::removeSensor(uid_t uid, int handle) {
54 Mutex::Autolock _l(mActivationsLock);
55 ssize_t index = mActivations.indexOf(Info(uid, handle));
56 if (index < 0) return false;
57 Info& info(mActivations.editItemAt(index));
58 info.count--;
59 return info.count == 0;
60}
61
62
63void BatteryService::enableSensorImpl(uid_t uid, int handle) {
64 if (mBatteryStatService != 0) {
65 if (addSensor(uid, handle)) {
66 int64_t identity = IPCThreadState::self()->clearCallingIdentity();
Mike Lockwood63ff1c62013-09-25 09:28:41 -070067 mBatteryStatService->noteStartSensor(uid, handle);
Mathias Agopian787ac1b2012-09-18 18:49:18 -070068 IPCThreadState::self()->restoreCallingIdentity(identity);
69 }
70 }
71}
72void BatteryService::disableSensorImpl(uid_t uid, int handle) {
73 if (mBatteryStatService != 0) {
74 if (removeSensor(uid, handle)) {
75 int64_t identity = IPCThreadState::self()->clearCallingIdentity();
Mike Lockwood63ff1c62013-09-25 09:28:41 -070076 mBatteryStatService->noteStopSensor(uid, handle);
Mathias Agopian787ac1b2012-09-18 18:49:18 -070077 IPCThreadState::self()->restoreCallingIdentity(identity);
78 }
79 }
80}
81
82void BatteryService::cleanupImpl(uid_t uid) {
83 if (mBatteryStatService != 0) {
84 Mutex::Autolock _l(mActivationsLock);
85 int64_t identity = IPCThreadState::self()->clearCallingIdentity();
Andreas Gamped4036b62015-07-28 13:49:04 -070086 for (size_t i=0 ; i<mActivations.size() ; i++) {
Mathias Agopian787ac1b2012-09-18 18:49:18 -070087 const Info& info(mActivations[i]);
88 if (info.uid == uid) {
Mike Lockwood63ff1c62013-09-25 09:28:41 -070089 mBatteryStatService->noteStopSensor(info.uid, info.handle);
Mathias Agopian787ac1b2012-09-18 18:49:18 -070090 mActivations.removeAt(i);
91 i--;
92 }
93 }
94 IPCThreadState::self()->restoreCallingIdentity(identity);
95 }
96}
97
Mathias Agopian787ac1b2012-09-18 18:49:18 -070098ANDROID_SINGLETON_STATIC_INSTANCE(BatteryService)
99
100// ---------------------------------------------------------------------------
101}; // namespace android
102