blob: 341d391434251943ec42950ad9e44ecbfbccf0b6 [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
32BatteryNotifier::BatteryNotifier() : mVideoRefCount(0), mAudioRefCount(0) {}
33
34BatteryNotifier::~BatteryNotifier() {
35 Mutex::Autolock _l(mLock);
36 if (mDeathNotifier != nullptr) {
37 IInterface::asBinder(mBatteryStatService)->unlinkToDeath(mDeathNotifier);
38 }
39}
40
41void BatteryNotifier::noteStartVideo() {
42 Mutex::Autolock _l(mLock);
43 sp<IBatteryStats> batteryService = getBatteryService_l();
44 if (mVideoRefCount == 0 && batteryService != nullptr) {
45 batteryService->noteStartVideo(AID_MEDIA);
46 }
47 mVideoRefCount++;
48}
49
50void BatteryNotifier::noteStopVideo() {
51 Mutex::Autolock _l(mLock);
52 if (mVideoRefCount == 0) {
53 ALOGW("%s: video refcount is broken.", __FUNCTION__);
54 return;
55 }
56
57 sp<IBatteryStats> batteryService = getBatteryService_l();
58
59 mVideoRefCount--;
60 if (mVideoRefCount == 0 && batteryService != nullptr) {
61 batteryService->noteStopVideo(AID_MEDIA);
62 }
63}
64
65void BatteryNotifier::noteResetVideo() {
66 Mutex::Autolock _l(mLock);
67 sp<IBatteryStats> batteryService = getBatteryService_l();
68 mVideoRefCount = 0;
69 if (batteryService != nullptr) {
Marco Nelissendcb346b2015-09-09 10:47:29 -070070 batteryService->noteResetVideo();
Ruben Brunk99e69712015-05-26 17:25:07 -070071 }
72}
73
74void BatteryNotifier::noteStartAudio() {
75 Mutex::Autolock _l(mLock);
76 sp<IBatteryStats> batteryService = getBatteryService_l();
77 if (mAudioRefCount == 0 && batteryService != nullptr) {
Marco Nelissendcb346b2015-09-09 10:47:29 -070078 batteryService->noteStartAudio(AID_AUDIOSERVER);
Ruben Brunk99e69712015-05-26 17:25:07 -070079 }
80 mAudioRefCount++;
81}
82
83void BatteryNotifier::noteStopAudio() {
84 Mutex::Autolock _l(mLock);
85 if (mAudioRefCount == 0) {
86 ALOGW("%s: audio refcount is broken.", __FUNCTION__);
87 return;
88 }
89
90 sp<IBatteryStats> batteryService = getBatteryService_l();
91
92 mAudioRefCount--;
93 if (mAudioRefCount == 0 && batteryService != nullptr) {
Marco Nelissendcb346b2015-09-09 10:47:29 -070094 batteryService->noteStopAudio(AID_AUDIOSERVER);
Ruben Brunk99e69712015-05-26 17:25:07 -070095 }
96}
97
98void BatteryNotifier::noteResetAudio() {
99 Mutex::Autolock _l(mLock);
100 sp<IBatteryStats> batteryService = getBatteryService_l();
101 mAudioRefCount = 0;
102 if (batteryService != nullptr) {
103 batteryService->noteResetAudio();
104 }
105}
106
107void BatteryNotifier::noteFlashlightOn(const String8& id, int uid) {
108 Mutex::Autolock _l(mLock);
109 sp<IBatteryStats> batteryService = getBatteryService_l();
110
111 std::pair<String8, int> k = std::make_pair(id, uid);
112 if (!mFlashlightState[k]) {
113 mFlashlightState[k] = true;
114 if (batteryService != nullptr) {
115 batteryService->noteFlashlightOn(uid);
116 }
117 }
118}
119
120void BatteryNotifier::noteFlashlightOff(const String8& id, int uid) {
121 Mutex::Autolock _l(mLock);
122 sp<IBatteryStats> batteryService = getBatteryService_l();
123
124 std::pair<String8, int> k = std::make_pair(id, uid);
125 if (mFlashlightState[k]) {
126 mFlashlightState[k] = false;
127 if (batteryService != nullptr) {
128 batteryService->noteFlashlightOff(uid);
129 }
130 }
131}
132
133void BatteryNotifier::noteResetFlashlight() {
134 Mutex::Autolock _l(mLock);
135 sp<IBatteryStats> batteryService = getBatteryService_l();
136 mFlashlightState.clear();
137 if (batteryService != nullptr) {
138 batteryService->noteResetFlashlight();
139 }
140}
141
142void BatteryNotifier::noteStartCamera(const String8& id, int uid) {
143 Mutex::Autolock _l(mLock);
144 sp<IBatteryStats> batteryService = getBatteryService_l();
145 std::pair<String8, int> k = std::make_pair(id, uid);
146 if (!mCameraState[k]) {
147 mCameraState[k] = true;
148 if (batteryService != nullptr) {
149 batteryService->noteStartCamera(uid);
150 }
151 }
152}
153
154void BatteryNotifier::noteStopCamera(const String8& id, int uid) {
155 Mutex::Autolock _l(mLock);
156 sp<IBatteryStats> batteryService = getBatteryService_l();
157 std::pair<String8, int> k = std::make_pair(id, uid);
158 if (mCameraState[k]) {
159 mCameraState[k] = false;
160 if (batteryService != nullptr) {
161 batteryService->noteStopCamera(uid);
162 }
163 }
164}
165
166void BatteryNotifier::noteResetCamera() {
167 Mutex::Autolock _l(mLock);
168 sp<IBatteryStats> batteryService = getBatteryService_l();
169 mCameraState.clear();
170 if (batteryService != nullptr) {
171 batteryService->noteResetCamera();
172 }
173}
174
175void BatteryNotifier::onBatteryStatServiceDied() {
176 Mutex::Autolock _l(mLock);
177 mBatteryStatService.clear();
178 mDeathNotifier.clear();
179 // Do not reset mVideoRefCount and mAudioRefCount here. The ref
180 // counting is independent of the battery service availability.
181 // We need this if battery service becomes available after media
182 // started.
183
184}
185
186sp<IBatteryStats> BatteryNotifier::getBatteryService_l() {
187 if (mBatteryStatService != nullptr) {
188 return mBatteryStatService;
189 }
190 // Get battery service from service manager
191 const sp<IServiceManager> sm(defaultServiceManager());
192 if (sm != nullptr) {
193 const String16 name("batterystats");
194 mBatteryStatService = interface_cast<IBatteryStats>(sm->checkService(name));
195 if (mBatteryStatService == nullptr) {
Marco Nelissendcb346b2015-09-09 10:47:29 -0700196 // this may occur normally during the init sequence as mediaserver
197 // and audioserver start before the batterystats service is available.
198 ALOGW("batterystats service unavailable!");
Ruben Brunk99e69712015-05-26 17:25:07 -0700199 return nullptr;
200 }
201
202 mDeathNotifier = new DeathNotifier();
203 IInterface::asBinder(mBatteryStatService)->linkToDeath(mDeathNotifier);
204
Marco Nelissendcb346b2015-09-09 10:47:29 -0700205 // Notify start now if mediaserver or audioserver is already started.
206 // 1) mediaserver and audioserver is started before batterystats service
207 // 2) batterystats server may have crashed.
Ruben Brunk99e69712015-05-26 17:25:07 -0700208 if (mVideoRefCount > 0) {
209 mBatteryStatService->noteStartVideo(AID_MEDIA);
210 }
211 if (mAudioRefCount > 0) {
Marco Nelissendcb346b2015-09-09 10:47:29 -0700212 mBatteryStatService->noteStartAudio(AID_AUDIOSERVER);
Ruben Brunk99e69712015-05-26 17:25:07 -0700213 }
Marco Nelissendcb346b2015-09-09 10:47:29 -0700214 // TODO: Notify for camera and flashlight state as well?
Ruben Brunk99e69712015-05-26 17:25:07 -0700215 }
216 return mBatteryStatService;
217}
218
219ANDROID_SINGLETON_STATIC_INSTANCE(BatteryNotifier);
220
221} // namespace android