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 #ifndef ST_AUDIO_BACKGROUND_MANAGER_H 16 #define ST_AUDIO_BACKGROUND_MANAGER_H 17 18 #include <bitset> 19 #include <string> 20 #include <mutex> 21 #include <unordered_map> 22 #include <unordered_set> 23 #include "audio_stream_collector.h" 24 #include "common_event_manager.h" 25 26 #include "background_task_listener.h" 27 #include "continuous_task_callback_info.h" 28 #include "background_task_subscriber.h" 29 30 namespace OHOS { 31 namespace AudioStandard { 32 33 struct AppState 34 { 35 bool isFreeze = false; 36 bool isBack = false; 37 bool hasSession = false; 38 bool hasBackTask = false; 39 bool isBinder = false; 40 bool isSystem = false; 41 }; 42 43 class AudioBackgroundManager { 44 public: GetInstance()45 static AudioBackgroundManager& GetInstance() 46 { 47 static AudioBackgroundManager instance; 48 return instance; 49 } 50 51 int32_t SetQueryAllowedPlaybackCallback(const sptr<IRemoteObject> &object); 52 int32_t SetBackgroundMuteCallback(const sptr<IRemoteObject> &object); 53 bool IsAllowedPlayback(const int32_t &uid, const int32_t &pid); 54 void SubscribeBackgroundTask(); 55 void NotifyAppStateChange(const int32_t uid, const int32_t pid, AppIsBackState state); 56 void NotifyBackgroundTaskStateChange(const int32_t uid, const int32_t pid, bool hasBackgroundTask); 57 int32_t NotifySessionStateChange(const int32_t uid, const int32_t pid, const bool hasSession); 58 void HandleSessionStateChange(const int32_t uid, const int32_t pid); 59 int32_t NotifyFreezeStateChange(const std::set<int32_t> &pidList, const bool isFreeze); 60 int32_t ResetAllProxy(); 61 void HandleFreezeStateChange(const int32_t pid, bool isFreeze); 62 63 private: AudioBackgroundManager()64 AudioBackgroundManager() : streamCollector_(AudioStreamCollector::GetAudioStreamCollector()) {} ~AudioBackgroundManager()65 ~AudioBackgroundManager() {} 66 67 void InsertIntoAppStatesMap(int32_t pid, AppState appState); 68 void DeleteFromMap(int32_t pid); 69 bool FindKeyInMap(int32_t pid); 70 71 std::map<int32_t, AppState> appStatesMap_; 72 AudioStreamCollector& streamCollector_; 73 74 sptr<IStandardAudioPolicyManagerListener> isAllowedPlaybackListener_; 75 sptr<IStandardAudioPolicyManagerListener> backgroundMuteListener_; 76 std::shared_ptr<BackgroundTaskListener> backgroundTaskListener_; 77 78 std::mutex appStatesMapMutex_; 79 }; 80 } 81 } 82 #endif