1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef AUDIO_RUNNING_LOCK_H 17 #define AUDIO_RUNNING_LOCK_H 18 19 #ifdef FEATURE_POWER_MANAGER 20 #include <iostream> 21 #include <unordered_set> 22 #include <mutex> 23 #include "running_lock.h" 24 #include "power_mgr_client.h" 25 #include "audio_utils.h" 26 #endif 27 28 namespace OHOS { 29 namespace AudioStandard { 30 #ifdef FEATURE_POWER_MANAGER 31 class AudioRunningLock { 32 public: 33 explicit AudioRunningLock(const std::string &lockName); 34 int32_t Lock(const int32_t timeoutMs); 35 int32_t UnLock(void); 36 template<typename T> UpdateAppsUid(const T & itBegin,const T & itEnd)37 void UpdateAppsUid(const T &itBegin, const T &itEnd) 38 { 39 Trace trace("AudioRunningLock::UpdateAppsUid"); 40 std::lock_guard<std::mutex> lock(mutex_); 41 std::unordered_set<int32_t> appsUidSet(itBegin, itEnd); 42 currentAppsUid_ = std::move(appsUidSet); 43 } 44 int32_t UpdateAppsUidToPowerMgr(void); 45 46 private: 47 static constexpr uint32_t LOCK_TIMEOUT_SECONDS = 8; 48 49 std::shared_ptr<PowerMgr::RunningLock> runningLock_ = nullptr; 50 std::mutex mutex_; 51 std::unordered_set<int32_t> currentAppsUid_; 52 std::unordered_set<int32_t> lastAppsUid_; 53 std::atomic<bool> isLocked_ = false; 54 }; 55 #endif 56 57 } // namespace AudioStandard 58 } // namespace OHOS 59 60 #endif // AUDIO_RUNNING_LOCK_H 61