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 #define LOG_TAG "BatteryNotifier" 18 //#define LOG_NDEBUG 0 19 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 32 BatteryNotifier::BatteryNotifier() : mVideoRefCount(0), mAudioRefCount(0) {} 33 34 BatteryNotifier::~BatteryNotifier() { 35 Mutex::Autolock _l(mLock); 36 if (mDeathNotifier != nullptr) { 37 IInterface::asBinder(mBatteryStatService)->unlinkToDeath(mDeathNotifier); 38 } 39 } 40 41 void 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 50 void 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 65 void BatteryNotifier::noteResetVideo() { 66 Mutex::Autolock _l(mLock); 67 sp<IBatteryStats> batteryService = getBatteryService_l(); 68 mVideoRefCount = 0; 69 if (batteryService != nullptr) { 70 batteryService->noteResetVideo(); 71 } 72 } 73 74 void BatteryNotifier::noteStartAudio() { 75 Mutex::Autolock _l(mLock); 76 sp<IBatteryStats> batteryService = getBatteryService_l(); 77 if (mAudioRefCount == 0 && batteryService != nullptr) { 78 batteryService->noteStartAudio(AID_AUDIOSERVER); 79 } 80 mAudioRefCount++; 81 } 82 83 void 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) { 94 batteryService->noteStopAudio(AID_AUDIOSERVER); 95 } 96 } 97 98 void 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 107 void 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 120 void 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 133 void 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 142 void 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 154 void 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 166 void 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 175 void 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 186 sp<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) { 196 // 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!"); 199 return nullptr; 200 } 201 202 mDeathNotifier = new DeathNotifier(); 203 IInterface::asBinder(mBatteryStatService)->linkToDeath(mDeathNotifier); 204 205 // 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. 208 if (mVideoRefCount > 0) { 209 mBatteryStatService->noteStartVideo(AID_MEDIA); 210 } 211 if (mAudioRefCount > 0) { 212 mBatteryStatService->noteStartAudio(AID_AUDIOSERVER); 213 } 214 // TODO: Notify for camera and flashlight state as well? 215 } 216 return mBatteryStatService; 217 } 218 219 ANDROID_SINGLETON_STATIC_INSTANCE(BatteryNotifier); 220 221 } // namespace android 222