| Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 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 |  | 
| Steven Moreland | 2716e11 | 2018-02-23 14:57:20 -0800 | [diff] [blame] | 21 | #include <cutils/atomic.h> | 
| Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 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 |  | 
| Peng Xu | 0a03159 | 2016-11-29 17:54:50 -0800 | [diff] [blame] | 33 | BatteryService::BatteryService() : mBatteryStatService(nullptr) { | 
| Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 34 | } | 
|  | 35 |  | 
| Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 36 | bool BatteryService::addSensor(uid_t uid, int handle) { | 
|  | 37 | Mutex::Autolock _l(mActivationsLock); | 
|  | 38 | Info key(uid, handle); | 
|  | 39 | ssize_t index = mActivations.indexOf(key); | 
|  | 40 | if (index < 0) { | 
|  | 41 | index = mActivations.add(key); | 
|  | 42 | } | 
|  | 43 | Info& info(mActivations.editItemAt(index)); | 
|  | 44 | info.count++; | 
|  | 45 | return info.count == 1; | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | bool BatteryService::removeSensor(uid_t uid, int handle) { | 
|  | 49 | Mutex::Autolock _l(mActivationsLock); | 
|  | 50 | ssize_t index = mActivations.indexOf(Info(uid, handle)); | 
|  | 51 | if (index < 0) return false; | 
|  | 52 | Info& info(mActivations.editItemAt(index)); | 
|  | 53 | info.count--; | 
|  | 54 | return info.count == 0; | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 |  | 
|  | 58 | void BatteryService::enableSensorImpl(uid_t uid, int handle) { | 
| Peng Xu | 0a03159 | 2016-11-29 17:54:50 -0800 | [diff] [blame] | 59 | if (checkService()) { | 
| Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 60 | if (addSensor(uid, handle)) { | 
|  | 61 | int64_t identity = IPCThreadState::self()->clearCallingIdentity(); | 
| Mike Lockwood | 63ff1c6 | 2013-09-25 09:28:41 -0700 | [diff] [blame] | 62 | mBatteryStatService->noteStartSensor(uid, handle); | 
| Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 63 | IPCThreadState::self()->restoreCallingIdentity(identity); | 
|  | 64 | } | 
|  | 65 | } | 
|  | 66 | } | 
|  | 67 | void BatteryService::disableSensorImpl(uid_t uid, int handle) { | 
| Peng Xu | 0a03159 | 2016-11-29 17:54:50 -0800 | [diff] [blame] | 68 | if (checkService()) { | 
| Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 69 | if (removeSensor(uid, handle)) { | 
|  | 70 | int64_t identity = IPCThreadState::self()->clearCallingIdentity(); | 
| Mike Lockwood | 63ff1c6 | 2013-09-25 09:28:41 -0700 | [diff] [blame] | 71 | mBatteryStatService->noteStopSensor(uid, handle); | 
| Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 72 | IPCThreadState::self()->restoreCallingIdentity(identity); | 
|  | 73 | } | 
|  | 74 | } | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | void BatteryService::cleanupImpl(uid_t uid) { | 
| Peng Xu | 0a03159 | 2016-11-29 17:54:50 -0800 | [diff] [blame] | 78 | if (checkService()) { | 
| Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 79 | Mutex::Autolock _l(mActivationsLock); | 
|  | 80 | int64_t identity = IPCThreadState::self()->clearCallingIdentity(); | 
| Joel Galenson | 7aafa16 | 2017-11-06 11:08:01 -0800 | [diff] [blame] | 81 | for (size_t i=0 ; i<mActivations.size() ; ) { | 
| Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 82 | const Info& info(mActivations[i]); | 
|  | 83 | if (info.uid == uid) { | 
| Mike Lockwood | 63ff1c6 | 2013-09-25 09:28:41 -0700 | [diff] [blame] | 84 | mBatteryStatService->noteStopSensor(info.uid, info.handle); | 
| Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 85 | mActivations.removeAt(i); | 
| Joel Galenson | 7aafa16 | 2017-11-06 11:08:01 -0800 | [diff] [blame] | 86 | } else { | 
|  | 87 | i++; | 
| Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 88 | } | 
|  | 89 | } | 
|  | 90 | IPCThreadState::self()->restoreCallingIdentity(identity); | 
|  | 91 | } | 
|  | 92 | } | 
|  | 93 |  | 
| Peng Xu | 0a03159 | 2016-11-29 17:54:50 -0800 | [diff] [blame] | 94 | bool BatteryService::checkService() { | 
|  | 95 | if (mBatteryStatService == nullptr) { | 
|  | 96 | const sp<IServiceManager> sm(defaultServiceManager()); | 
|  | 97 | if (sm != NULL) { | 
|  | 98 | const String16 name("batterystats"); | 
|  | 99 | mBatteryStatService = interface_cast<IBatteryStats>(sm->getService(name)); | 
|  | 100 | } | 
|  | 101 | } | 
|  | 102 | return mBatteryStatService != nullptr; | 
|  | 103 | } | 
|  | 104 |  | 
| Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 105 | ANDROID_SINGLETON_STATIC_INSTANCE(BatteryService) | 
|  | 106 |  | 
|  | 107 | // --------------------------------------------------------------------------- | 
|  | 108 | }; // namespace android |