| 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 | |
| 17 | #ifndef MEDIA_BATTERY_NOTIFIER_H |
| 18 | #define MEDIA_BATTERY_NOTIFIER_H |
| 19 | |
| Steven Moreland | 3696000 | 2021-04-01 00:08:45 +0000 | [diff] [blame] | 20 | #include <batterystats/IBatteryStats.h> |
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 21 | #include <utils/Singleton.h> |
| 22 | #include <utils/String8.h> |
| 23 | |
| 24 | #include <map> |
| 25 | #include <utility> |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | /** |
| 30 | * Class used for logging battery life events in mediaserver. |
| 31 | */ |
| 32 | class BatteryNotifier : public Singleton<BatteryNotifier> { |
| 33 | |
| 34 | friend class Singleton<BatteryNotifier>; |
| 35 | BatteryNotifier(); |
| 36 | |
| 37 | public: |
| 38 | ~BatteryNotifier(); |
| 39 | |
| Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 40 | void noteStartVideo(uid_t uid); |
| 41 | void noteStopVideo(uid_t uid); |
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 42 | void noteResetVideo(); |
| Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 43 | void noteStartAudio(uid_t uid); |
| 44 | void noteStopAudio(uid_t uid); |
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 45 | void noteResetAudio(); |
| Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 46 | void noteFlashlightOn(const String8& id, uid_t uid); |
| 47 | void noteFlashlightOff(const String8& id, uid_t uid); |
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 48 | void noteResetFlashlight(); |
| Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 49 | void noteStartCamera(const String8& id, uid_t uid); |
| 50 | void noteStopCamera(const String8& id, uid_t uid); |
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 51 | void noteResetCamera(); |
| 52 | |
| 53 | private: |
| 54 | void onBatteryStatServiceDied(); |
| 55 | |
| 56 | class DeathNotifier : public IBinder::DeathRecipient { |
| 57 | virtual void binderDied(const wp<IBinder>& /*who*/); |
| 58 | }; |
| 59 | |
| 60 | Mutex mLock; |
| Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 61 | std::map<uid_t, int> mVideoRefCounts; |
| 62 | std::map<uid_t, int> mAudioRefCounts; |
| 63 | std::map<std::pair<String8, uid_t>, bool> mFlashlightState; |
| 64 | std::map<std::pair<String8, uid_t>, bool> mCameraState; |
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 65 | sp<IBatteryStats> mBatteryStatService; |
| 66 | sp<DeathNotifier> mDeathNotifier; |
| 67 | |
| 68 | sp<IBatteryStats> getBatteryService_l(); |
| 69 | }; |
| 70 | |
| Atneya Nair | 166663a | 2023-06-27 19:16:24 -0700 | [diff] [blame] | 71 | namespace mediautils { |
| 72 | class BatteryStatsAudioHandle { |
| 73 | public: |
| 74 | static constexpr uid_t INVALID_UID = static_cast<uid_t>(-1); |
| 75 | |
| 76 | explicit BatteryStatsAudioHandle(uid_t uid) : mUid(uid) { |
| 77 | if (uid != INVALID_UID) { |
| 78 | BatteryNotifier::getInstance().noteStartAudio(mUid); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | BatteryStatsAudioHandle(BatteryStatsAudioHandle&& other) : mUid(other.mUid) { |
| 83 | other.mUid = INVALID_UID; |
| 84 | } |
| 85 | |
| 86 | BatteryStatsAudioHandle(const BatteryStatsAudioHandle& other) = delete; |
| 87 | |
| 88 | BatteryStatsAudioHandle& operator=(const BatteryStatsAudioHandle& other) = delete; |
| 89 | |
| 90 | BatteryStatsAudioHandle& operator=(BatteryStatsAudioHandle&& other) = delete; |
| 91 | |
| 92 | ~BatteryStatsAudioHandle() { |
| 93 | if (mUid != INVALID_UID) { |
| 94 | BatteryNotifier::getInstance().noteStopAudio(mUid); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | // Logically const |
| 100 | uid_t mUid = INVALID_UID; |
| 101 | }; |
| 102 | } // namespace mediautils |
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 103 | } // namespace android |
| 104 | |
| 105 | #endif // MEDIA_BATTERY_NOTIFIER_H |