Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015, 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 | |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 17 | #define LOG_TAG "BatteryNotifier" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 20 | #include "include/mediautils/BatteryNotifier.h" |
| 21 | |
| 22 | #include <binder/IServiceManager.h> |
| 23 | #include <utils/Log.h> |
| 24 | #include <private/android_filesystem_config.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | void BatteryNotifier::DeathNotifier::binderDied(const wp<IBinder>& /*who*/) { |
| 29 | BatteryNotifier::getInstance().onBatteryStatServiceDied(); |
| 30 | } |
| 31 | |
Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 32 | BatteryNotifier::BatteryNotifier() {} |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 33 | |
| 34 | BatteryNotifier::~BatteryNotifier() { |
| 35 | Mutex::Autolock _l(mLock); |
| 36 | if (mDeathNotifier != nullptr) { |
| 37 | IInterface::asBinder(mBatteryStatService)->unlinkToDeath(mDeathNotifier); |
| 38 | } |
| 39 | } |
| 40 | |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 41 | void BatteryNotifier::noteStartVideo(uid_t uid) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 42 | Mutex::Autolock _l(mLock); |
| 43 | sp<IBatteryStats> batteryService = getBatteryService_l(); |
Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 44 | if (mVideoRefCounts[uid] == 0 && batteryService != nullptr) { |
| 45 | batteryService->noteStartVideo(uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 46 | } |
Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 47 | mVideoRefCounts[uid]++; |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 50 | void BatteryNotifier::noteStopVideo(uid_t uid) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 51 | Mutex::Autolock _l(mLock); |
Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 52 | if (mVideoRefCounts.find(uid) == mVideoRefCounts.end()) { |
| 53 | ALOGW("%s: video refcount is broken for uid(%d).", __FUNCTION__, (int)uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 54 | return; |
| 55 | } |
| 56 | |
| 57 | sp<IBatteryStats> batteryService = getBatteryService_l(); |
| 58 | |
Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 59 | mVideoRefCounts[uid]--; |
| 60 | if (mVideoRefCounts[uid] == 0) { |
| 61 | if (batteryService != nullptr) { |
| 62 | batteryService->noteStopVideo(uid); |
| 63 | } |
| 64 | mVideoRefCounts.erase(uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | void BatteryNotifier::noteResetVideo() { |
| 69 | Mutex::Autolock _l(mLock); |
| 70 | sp<IBatteryStats> batteryService = getBatteryService_l(); |
Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 71 | mVideoRefCounts.clear(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 72 | if (batteryService != nullptr) { |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 73 | batteryService->noteResetVideo(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 77 | void BatteryNotifier::noteStartAudio(uid_t uid) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 78 | Mutex::Autolock _l(mLock); |
| 79 | sp<IBatteryStats> batteryService = getBatteryService_l(); |
Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 80 | if (mAudioRefCounts[uid] == 0 && batteryService != nullptr) { |
| 81 | batteryService->noteStartAudio(uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 82 | } |
Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 83 | mAudioRefCounts[uid]++; |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 86 | void BatteryNotifier::noteStopAudio(uid_t uid) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 87 | Mutex::Autolock _l(mLock); |
Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 88 | if (mAudioRefCounts.find(uid) == mAudioRefCounts.end()) { |
| 89 | ALOGW("%s: audio refcount is broken for uid(%d).", __FUNCTION__, (int)uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
| 93 | sp<IBatteryStats> batteryService = getBatteryService_l(); |
| 94 | |
Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 95 | mAudioRefCounts[uid]--; |
| 96 | if (mAudioRefCounts[uid] == 0) { |
| 97 | if (batteryService != nullptr) { |
| 98 | batteryService->noteStopAudio(uid); |
| 99 | } |
| 100 | mAudioRefCounts.erase(uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | void BatteryNotifier::noteResetAudio() { |
| 105 | Mutex::Autolock _l(mLock); |
| 106 | sp<IBatteryStats> batteryService = getBatteryService_l(); |
Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 107 | mAudioRefCounts.clear(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 108 | if (batteryService != nullptr) { |
| 109 | batteryService->noteResetAudio(); |
| 110 | } |
| 111 | } |
| 112 | |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 113 | void BatteryNotifier::noteFlashlightOn(const String8& id, uid_t uid) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 114 | Mutex::Autolock _l(mLock); |
| 115 | sp<IBatteryStats> batteryService = getBatteryService_l(); |
| 116 | |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 117 | std::pair<String8, uid_t> k = std::make_pair(id, uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 118 | if (!mFlashlightState[k]) { |
| 119 | mFlashlightState[k] = true; |
| 120 | if (batteryService != nullptr) { |
| 121 | batteryService->noteFlashlightOn(uid); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 126 | void BatteryNotifier::noteFlashlightOff(const String8& id, uid_t uid) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 127 | Mutex::Autolock _l(mLock); |
| 128 | sp<IBatteryStats> batteryService = getBatteryService_l(); |
| 129 | |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 130 | std::pair<String8, uid_t> k = std::make_pair(id, uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 131 | if (mFlashlightState[k]) { |
| 132 | mFlashlightState[k] = false; |
| 133 | if (batteryService != nullptr) { |
| 134 | batteryService->noteFlashlightOff(uid); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void BatteryNotifier::noteResetFlashlight() { |
| 140 | Mutex::Autolock _l(mLock); |
| 141 | sp<IBatteryStats> batteryService = getBatteryService_l(); |
| 142 | mFlashlightState.clear(); |
| 143 | if (batteryService != nullptr) { |
| 144 | batteryService->noteResetFlashlight(); |
| 145 | } |
| 146 | } |
| 147 | |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 148 | void BatteryNotifier::noteStartCamera(const String8& id, uid_t uid) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 149 | Mutex::Autolock _l(mLock); |
| 150 | sp<IBatteryStats> batteryService = getBatteryService_l(); |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 151 | std::pair<String8, uid_t> k = std::make_pair(id, uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 152 | if (!mCameraState[k]) { |
| 153 | mCameraState[k] = true; |
| 154 | if (batteryService != nullptr) { |
| 155 | batteryService->noteStartCamera(uid); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 160 | void BatteryNotifier::noteStopCamera(const String8& id, uid_t uid) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 161 | Mutex::Autolock _l(mLock); |
| 162 | sp<IBatteryStats> batteryService = getBatteryService_l(); |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 163 | std::pair<String8, uid_t> k = std::make_pair(id, uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 164 | if (mCameraState[k]) { |
| 165 | mCameraState[k] = false; |
| 166 | if (batteryService != nullptr) { |
| 167 | batteryService->noteStopCamera(uid); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void BatteryNotifier::noteResetCamera() { |
| 173 | Mutex::Autolock _l(mLock); |
| 174 | sp<IBatteryStats> batteryService = getBatteryService_l(); |
| 175 | mCameraState.clear(); |
| 176 | if (batteryService != nullptr) { |
| 177 | batteryService->noteResetCamera(); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void BatteryNotifier::onBatteryStatServiceDied() { |
| 182 | Mutex::Autolock _l(mLock); |
| 183 | mBatteryStatService.clear(); |
| 184 | mDeathNotifier.clear(); |
Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 185 | // Do not reset mVideoRefCounts and mAudioRefCounts here. The ref |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 186 | // counting is independent of the battery service availability. |
| 187 | // We need this if battery service becomes available after media |
| 188 | // started. |
| 189 | |
| 190 | } |
| 191 | |
| 192 | sp<IBatteryStats> BatteryNotifier::getBatteryService_l() { |
| 193 | if (mBatteryStatService != nullptr) { |
| 194 | return mBatteryStatService; |
| 195 | } |
| 196 | // Get battery service from service manager |
| 197 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 198 | if (sm != nullptr) { |
| 199 | const String16 name("batterystats"); |
| 200 | mBatteryStatService = interface_cast<IBatteryStats>(sm->checkService(name)); |
| 201 | if (mBatteryStatService == nullptr) { |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 202 | // this may occur normally during the init sequence as mediaserver |
| 203 | // and audioserver start before the batterystats service is available. |
| 204 | ALOGW("batterystats service unavailable!"); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 205 | return nullptr; |
| 206 | } |
| 207 | |
| 208 | mDeathNotifier = new DeathNotifier(); |
| 209 | IInterface::asBinder(mBatteryStatService)->linkToDeath(mDeathNotifier); |
| 210 | |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 211 | // Notify start now if mediaserver or audioserver is already started. |
| 212 | // 1) mediaserver and audioserver is started before batterystats service |
| 213 | // 2) batterystats server may have crashed. |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 214 | std::map<uid_t, int>::iterator it = mVideoRefCounts.begin(); |
Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 215 | for (; it != mVideoRefCounts.end(); ++it) { |
| 216 | mBatteryStatService->noteStartVideo(it->first); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 217 | } |
Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 218 | it = mAudioRefCounts.begin(); |
| 219 | for (; it != mAudioRefCounts.end(); ++it) { |
| 220 | mBatteryStatService->noteStartAudio(it->first); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 221 | } |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 222 | // TODO: Notify for camera and flashlight state as well? |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 223 | } |
| 224 | return mBatteryStatService; |
| 225 | } |
| 226 | |
| 227 | ANDROID_SINGLETON_STATIC_INSTANCE(BatteryNotifier); |
| 228 | |
| 229 | } // namespace android |