blob: 09bc042265b22d8f5527b968daf9dbc546a1e54b [file] [log] [blame]
Ruben Brunk99e69712015-05-26 17:25:07 -07001/*
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 Nelissendcb346b2015-09-09 10:47:29 -070017#define LOG_TAG "BatteryNotifier"
18//#define LOG_NDEBUG 0
19
Ruben Brunk99e69712015-05-26 17:25:07 -070020#include "include/mediautils/BatteryNotifier.h"
21
22#include <binder/IServiceManager.h>
23#include <utils/Log.h>
24#include <private/android_filesystem_config.h>
25
26namespace android {
27
28void BatteryNotifier::DeathNotifier::binderDied(const wp<IBinder>& /*who*/) {
29 BatteryNotifier::getInstance().onBatteryStatServiceDied();
30}
31
Wei Jiaf2ae3e12016-10-27 17:10:59 -070032BatteryNotifier::BatteryNotifier() {}
Ruben Brunk99e69712015-05-26 17:25:07 -070033
34BatteryNotifier::~BatteryNotifier() {
35 Mutex::Autolock _l(mLock);
36 if (mDeathNotifier != nullptr) {
37 IInterface::asBinder(mBatteryStatService)->unlinkToDeath(mDeathNotifier);
38 }
39}
40
Andy Hung1afd0982016-11-08 16:13:44 -080041void BatteryNotifier::noteStartVideo(uid_t uid) {
Ruben Brunk99e69712015-05-26 17:25:07 -070042 Mutex::Autolock _l(mLock);
43 sp<IBatteryStats> batteryService = getBatteryService_l();
Wei Jiaf2ae3e12016-10-27 17:10:59 -070044 if (mVideoRefCounts[uid] == 0 && batteryService != nullptr) {
45 batteryService->noteStartVideo(uid);
Ruben Brunk99e69712015-05-26 17:25:07 -070046 }
Wei Jiaf2ae3e12016-10-27 17:10:59 -070047 mVideoRefCounts[uid]++;
Ruben Brunk99e69712015-05-26 17:25:07 -070048}
49
Andy Hung1afd0982016-11-08 16:13:44 -080050void BatteryNotifier::noteStopVideo(uid_t uid) {
Ruben Brunk99e69712015-05-26 17:25:07 -070051 Mutex::Autolock _l(mLock);
Wei Jiaf2ae3e12016-10-27 17:10:59 -070052 if (mVideoRefCounts.find(uid) == mVideoRefCounts.end()) {
53 ALOGW("%s: video refcount is broken for uid(%d).", __FUNCTION__, (int)uid);
Ruben Brunk99e69712015-05-26 17:25:07 -070054 return;
55 }
56
57 sp<IBatteryStats> batteryService = getBatteryService_l();
58
Wei Jiaf2ae3e12016-10-27 17:10:59 -070059 mVideoRefCounts[uid]--;
60 if (mVideoRefCounts[uid] == 0) {
61 if (batteryService != nullptr) {
62 batteryService->noteStopVideo(uid);
63 }
64 mVideoRefCounts.erase(uid);
Ruben Brunk99e69712015-05-26 17:25:07 -070065 }
66}
67
68void BatteryNotifier::noteResetVideo() {
69 Mutex::Autolock _l(mLock);
70 sp<IBatteryStats> batteryService = getBatteryService_l();
Wei Jiaf2ae3e12016-10-27 17:10:59 -070071 mVideoRefCounts.clear();
Ruben Brunk99e69712015-05-26 17:25:07 -070072 if (batteryService != nullptr) {
Marco Nelissendcb346b2015-09-09 10:47:29 -070073 batteryService->noteResetVideo();
Ruben Brunk99e69712015-05-26 17:25:07 -070074 }
75}
76
Andy Hung1afd0982016-11-08 16:13:44 -080077void BatteryNotifier::noteStartAudio(uid_t uid) {
Ruben Brunk99e69712015-05-26 17:25:07 -070078 Mutex::Autolock _l(mLock);
79 sp<IBatteryStats> batteryService = getBatteryService_l();
Wei Jiaf2ae3e12016-10-27 17:10:59 -070080 if (mAudioRefCounts[uid] == 0 && batteryService != nullptr) {
81 batteryService->noteStartAudio(uid);
Ruben Brunk99e69712015-05-26 17:25:07 -070082 }
Wei Jiaf2ae3e12016-10-27 17:10:59 -070083 mAudioRefCounts[uid]++;
Ruben Brunk99e69712015-05-26 17:25:07 -070084}
85
Andy Hung1afd0982016-11-08 16:13:44 -080086void BatteryNotifier::noteStopAudio(uid_t uid) {
Ruben Brunk99e69712015-05-26 17:25:07 -070087 Mutex::Autolock _l(mLock);
Wei Jiaf2ae3e12016-10-27 17:10:59 -070088 if (mAudioRefCounts.find(uid) == mAudioRefCounts.end()) {
89 ALOGW("%s: audio refcount is broken for uid(%d).", __FUNCTION__, (int)uid);
Ruben Brunk99e69712015-05-26 17:25:07 -070090 return;
91 }
92
93 sp<IBatteryStats> batteryService = getBatteryService_l();
94
Wei Jiaf2ae3e12016-10-27 17:10:59 -070095 mAudioRefCounts[uid]--;
96 if (mAudioRefCounts[uid] == 0) {
97 if (batteryService != nullptr) {
98 batteryService->noteStopAudio(uid);
99 }
100 mAudioRefCounts.erase(uid);
Ruben Brunk99e69712015-05-26 17:25:07 -0700101 }
102}
103
104void BatteryNotifier::noteResetAudio() {
105 Mutex::Autolock _l(mLock);
106 sp<IBatteryStats> batteryService = getBatteryService_l();
Wei Jiaf2ae3e12016-10-27 17:10:59 -0700107 mAudioRefCounts.clear();
Ruben Brunk99e69712015-05-26 17:25:07 -0700108 if (batteryService != nullptr) {
109 batteryService->noteResetAudio();
110 }
111}
112
Andy Hung1afd0982016-11-08 16:13:44 -0800113void BatteryNotifier::noteFlashlightOn(const String8& id, uid_t uid) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700114 Mutex::Autolock _l(mLock);
115 sp<IBatteryStats> batteryService = getBatteryService_l();
116
Andy Hung1afd0982016-11-08 16:13:44 -0800117 std::pair<String8, uid_t> k = std::make_pair(id, uid);
Ruben Brunk99e69712015-05-26 17:25:07 -0700118 if (!mFlashlightState[k]) {
119 mFlashlightState[k] = true;
120 if (batteryService != nullptr) {
121 batteryService->noteFlashlightOn(uid);
122 }
123 }
124}
125
Andy Hung1afd0982016-11-08 16:13:44 -0800126void BatteryNotifier::noteFlashlightOff(const String8& id, uid_t uid) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700127 Mutex::Autolock _l(mLock);
128 sp<IBatteryStats> batteryService = getBatteryService_l();
129
Andy Hung1afd0982016-11-08 16:13:44 -0800130 std::pair<String8, uid_t> k = std::make_pair(id, uid);
Ruben Brunk99e69712015-05-26 17:25:07 -0700131 if (mFlashlightState[k]) {
132 mFlashlightState[k] = false;
133 if (batteryService != nullptr) {
134 batteryService->noteFlashlightOff(uid);
135 }
136 }
137}
138
139void 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 Hung1afd0982016-11-08 16:13:44 -0800148void BatteryNotifier::noteStartCamera(const String8& id, uid_t uid) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700149 Mutex::Autolock _l(mLock);
150 sp<IBatteryStats> batteryService = getBatteryService_l();
Andy Hung1afd0982016-11-08 16:13:44 -0800151 std::pair<String8, uid_t> k = std::make_pair(id, uid);
Ruben Brunk99e69712015-05-26 17:25:07 -0700152 if (!mCameraState[k]) {
153 mCameraState[k] = true;
154 if (batteryService != nullptr) {
155 batteryService->noteStartCamera(uid);
156 }
157 }
158}
159
Andy Hung1afd0982016-11-08 16:13:44 -0800160void BatteryNotifier::noteStopCamera(const String8& id, uid_t uid) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700161 Mutex::Autolock _l(mLock);
162 sp<IBatteryStats> batteryService = getBatteryService_l();
Andy Hung1afd0982016-11-08 16:13:44 -0800163 std::pair<String8, uid_t> k = std::make_pair(id, uid);
Ruben Brunk99e69712015-05-26 17:25:07 -0700164 if (mCameraState[k]) {
165 mCameraState[k] = false;
166 if (batteryService != nullptr) {
167 batteryService->noteStopCamera(uid);
168 }
169 }
170}
171
172void 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
181void BatteryNotifier::onBatteryStatServiceDied() {
182 Mutex::Autolock _l(mLock);
183 mBatteryStatService.clear();
184 mDeathNotifier.clear();
Wei Jiaf2ae3e12016-10-27 17:10:59 -0700185 // Do not reset mVideoRefCounts and mAudioRefCounts here. The ref
Ruben Brunk99e69712015-05-26 17:25:07 -0700186 // counting is independent of the battery service availability.
187 // We need this if battery service becomes available after media
188 // started.
189
190}
191
192sp<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 Nelissendcb346b2015-09-09 10:47:29 -0700202 // 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 Brunk99e69712015-05-26 17:25:07 -0700205 return nullptr;
206 }
207
208 mDeathNotifier = new DeathNotifier();
209 IInterface::asBinder(mBatteryStatService)->linkToDeath(mDeathNotifier);
210
Marco Nelissendcb346b2015-09-09 10:47:29 -0700211 // 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 Hung1afd0982016-11-08 16:13:44 -0800214 std::map<uid_t, int>::iterator it = mVideoRefCounts.begin();
Wei Jiaf2ae3e12016-10-27 17:10:59 -0700215 for (; it != mVideoRefCounts.end(); ++it) {
216 mBatteryStatService->noteStartVideo(it->first);
Ruben Brunk99e69712015-05-26 17:25:07 -0700217 }
Wei Jiaf2ae3e12016-10-27 17:10:59 -0700218 it = mAudioRefCounts.begin();
219 for (; it != mAudioRefCounts.end(); ++it) {
220 mBatteryStatService->noteStartAudio(it->first);
Ruben Brunk99e69712015-05-26 17:25:07 -0700221 }
Marco Nelissendcb346b2015-09-09 10:47:29 -0700222 // TODO: Notify for camera and flashlight state as well?
Ruben Brunk99e69712015-05-26 17:25:07 -0700223 }
224 return mBatteryStatService;
225}
226
227ANDROID_SINGLETON_STATIC_INSTANCE(BatteryNotifier);
228
229} // namespace android