1 /*
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 
30 namespace android {
31 // ---------------------------------------------------------------------------
32 
BatteryService()33 BatteryService::BatteryService() {
34     const sp<IServiceManager> sm(defaultServiceManager());
35     if (sm != NULL) {
36         const String16 name("batterystats");
37         mBatteryStatService = interface_cast<IBatteryStats>(sm->getService(name));
38     }
39 }
40 
addSensor(uid_t uid,int handle)41 bool 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 
removeSensor(uid_t uid,int handle)53 bool 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 
enableSensorImpl(uid_t uid,int handle)63 void BatteryService::enableSensorImpl(uid_t uid, int handle) {
64     if (mBatteryStatService != 0) {
65         if (addSensor(uid, handle)) {
66             int64_t identity = IPCThreadState::self()->clearCallingIdentity();
67             mBatteryStatService->noteStartSensor(uid, handle);
68             IPCThreadState::self()->restoreCallingIdentity(identity);
69         }
70     }
71 }
disableSensorImpl(uid_t uid,int handle)72 void BatteryService::disableSensorImpl(uid_t uid, int handle) {
73     if (mBatteryStatService != 0) {
74         if (removeSensor(uid, handle)) {
75             int64_t identity = IPCThreadState::self()->clearCallingIdentity();
76             mBatteryStatService->noteStopSensor(uid, handle);
77             IPCThreadState::self()->restoreCallingIdentity(identity);
78         }
79     }
80 }
81 
cleanupImpl(uid_t uid)82 void BatteryService::cleanupImpl(uid_t uid) {
83     if (mBatteryStatService != 0) {
84         Mutex::Autolock _l(mActivationsLock);
85         int64_t identity = IPCThreadState::self()->clearCallingIdentity();
86         for (ssize_t i=0 ; i<mActivations.size() ; i++) {
87             const Info& info(mActivations[i]);
88             if (info.uid == uid) {
89                 mBatteryStatService->noteStopSensor(info.uid, info.handle);
90                 mActivations.removeAt(i);
91                 i--;
92             }
93         }
94         IPCThreadState::self()->restoreCallingIdentity(identity);
95     }
96 }
97 
98 ANDROID_SINGLETON_STATIC_INSTANCE(BatteryService)
99 
100 // ---------------------------------------------------------------------------
101 }; // namespace android
102 
103