2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <sys/types.h>
21 #include <utils/Atomic.h>
22 #include <utils/Errors.h>
23 #include <utils/Singleton.h>
25 #include <binder/BinderService.h>
26 #include <binder/Parcel.h>
28 #include "BatteryService.h"
31 // ---------------------------------------------------------------------------
33 BatteryService::BatteryService() {
34 const sp<IServiceManager> sm(defaultServiceManager());
36 const String16 name("batterystats");
37 mBatteryStatService = sm->getService(name);
41 status_t BatteryService::noteStartSensor(int uid, int handle) {
43 data.writeInterfaceToken(DESCRIPTOR);
45 data.writeInt32(handle);
46 status_t err = mBatteryStatService->transact(
47 TRANSACTION_noteStartSensor, data, &reply, 0);
48 err = reply.readExceptionCode();
52 status_t BatteryService::noteStopSensor(int uid, int handle) {
54 data.writeInterfaceToken(DESCRIPTOR);
56 data.writeInt32(handle);
57 status_t err = mBatteryStatService->transact(
58 TRANSACTION_noteStopSensor, data, &reply, 0);
59 err = reply.readExceptionCode();
63 bool BatteryService::addSensor(uid_t uid, int handle) {
64 Mutex::Autolock _l(mActivationsLock);
65 Info key(uid, handle);
66 ssize_t index = mActivations.indexOf(key);
68 index = mActivations.add(key);
70 Info& info(mActivations.editItemAt(index));
72 return info.count == 1;
75 bool BatteryService::removeSensor(uid_t uid, int handle) {
76 Mutex::Autolock _l(mActivationsLock);
77 ssize_t index = mActivations.indexOf(Info(uid, handle));
78 if (index < 0) return false;
79 Info& info(mActivations.editItemAt(index));
81 return info.count == 0;
85 void BatteryService::enableSensorImpl(uid_t uid, int handle) {
86 if (mBatteryStatService != 0) {
87 if (addSensor(uid, handle)) {
88 int64_t identity = IPCThreadState::self()->clearCallingIdentity();
89 noteStartSensor(uid, handle);
90 IPCThreadState::self()->restoreCallingIdentity(identity);
94 void BatteryService::disableSensorImpl(uid_t uid, int handle) {
95 if (mBatteryStatService != 0) {
96 if (removeSensor(uid, handle)) {
97 int64_t identity = IPCThreadState::self()->clearCallingIdentity();
98 noteStopSensor(uid, handle);
99 IPCThreadState::self()->restoreCallingIdentity(identity);
104 void BatteryService::cleanupImpl(uid_t uid) {
105 if (mBatteryStatService != 0) {
106 Mutex::Autolock _l(mActivationsLock);
107 int64_t identity = IPCThreadState::self()->clearCallingIdentity();
108 for (ssize_t i=0 ; i<mActivations.size() ; i++) {
109 const Info& info(mActivations[i]);
110 if (info.uid == uid) {
111 noteStopSensor(info.uid, info.handle);
112 mActivations.removeAt(i);
116 IPCThreadState::self()->restoreCallingIdentity(identity);
120 const String16 BatteryService::DESCRIPTOR("com.android.internal.app.IBatteryStats");
122 ANDROID_SINGLETON_STATIC_INSTANCE(BatteryService)
124 // ---------------------------------------------------------------------------
125 }; // namespace android