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