1 /* 2 * Copyright (c) 2024-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 VOLUME_DATA_MAINTAINER_H 16 #define VOLUME_DATA_MAINTAINER_H 17 18 #include <list> 19 #include <unordered_map> 20 #include <mutex> 21 #include <cinttypes> 22 #include "errors.h" 23 #include "ipc_skeleton.h" 24 #include "ffrt.h" 25 26 #include "audio_utils.h" 27 #include "audio_setting_provider.h" 28 #include "audio_policy_log.h" 29 #include "audio_info.h" 30 #include "audio_setting_provider.h" 31 32 namespace OHOS { 33 namespace AudioStandard { 34 35 class VolumeDataMaintainer { 36 public: 37 enum VolumeDataMaintainerStreamType { // define with Dual framework 38 VT_STREAM_DEFAULT = -1, 39 VT_STREAM_VOICE_CALL = 0, 40 VT_STREAM_SYSTEM = 1, 41 VT_STREAM_RING = 2, 42 VT_STREAM_MUSIC = 3, 43 VT_STREAM_ALARM = 4, 44 VT_STREAM_NOTIFICATION = 5, 45 VT_STREAM_BLUETOOTH_SCO = 6, 46 VT_STREAM_SYSTEM_ENFORCED = 7, 47 VT_STREAM_DTMF = 8, 48 VT_STREAM_TTS = 9, 49 VT_STREAM_ACCESSIBILITY = 10, 50 VT_STREAM_ASSISTANT = 11, 51 }; 52 GetVolumeDataMaintainer()53 static VolumeDataMaintainer& GetVolumeDataMaintainer() 54 { 55 static VolumeDataMaintainer volumeDataMainTainer; 56 return volumeDataMainTainer; 57 } 58 ~VolumeDataMaintainer(); 59 60 void SetDataShareReady(std::atomic<bool> isDataShareReady); 61 bool SaveVolume(DeviceType type, AudioStreamType streamType, int32_t volumeLevel); 62 bool GetVolume(DeviceType deviceType, AudioStreamType streamType); 63 void SetStreamVolume(AudioStreamType streamType, int32_t volumeLevel); 64 void SetAppVolume(int32_t appUid, int32_t volumeLevel); 65 bool GetAppMute(int32_t appUid); 66 bool GetAppMuteOwned(int32_t appUid); 67 void SetAppVolumeMuted(int32_t appUid, bool muted); 68 int32_t GetStreamVolume(AudioStreamType streamType); 69 int32_t GetDeviceVolume(DeviceType deviceType, AudioStreamType streamType); 70 int32_t GetAppVolume(int32_t appUid); 71 bool IsSetAppVolume(int32_t appUid); 72 std::unordered_map<AudioStreamType, int32_t> GetVolumeMap(); 73 74 bool SaveMuteStatus(DeviceType deviceType, AudioStreamType streamType, 75 bool muteStatus); 76 bool GetMuteStatus(DeviceType deviceType, AudioStreamType streamType); 77 bool SetStreamMuteStatus(AudioStreamType streamType, bool muteStatus); 78 bool GetStreamMute(AudioStreamType streamType); 79 80 bool GetMuteAffected(int32_t &affected); 81 bool GetMuteTransferStatus(bool &status); 82 bool SetMuteAffectedToMuteStatusDataBase(int32_t affected); 83 bool SaveMuteTransferStatus(bool status); 84 85 bool SaveRingerMode(AudioRingerMode ringerMode); 86 bool GetRingerMode(AudioRingerMode &ringerMode); 87 bool SaveSafeStatus(DeviceType deviceType, SafeStatus safeStatus); 88 bool GetSafeStatus(DeviceType deviceType, SafeStatus &safeStatus); 89 bool SaveSafeVolumeTime(DeviceType deviceType, int64_t time); 90 bool GetSafeVolumeTime(DeviceType deviceType, int64_t &time); 91 bool SaveSystemSoundUrl(const std::string &key, const std::string &value); 92 bool GetSystemSoundUrl(const std::string &key, std::string &value); 93 bool SetRestoreVolumeLevel(DeviceType deviceType, int32_t volume); 94 bool GetRestoreVolumeLevel(DeviceType deviceType, int32_t &volume); 95 void RegisterCloned(); 96 bool SaveMicMuteState(bool isMute); 97 bool GetMicMuteState(bool &isMute); 98 bool CheckOsAccountReady(); 99 100 private: 101 VolumeDataMaintainer(); 102 static std::string GetVolumeKeyForDataShare(DeviceType deviceType, AudioStreamType streamType); 103 static std::string GetMuteKeyForDataShare(DeviceType deviceType, AudioStreamType streamType); 104 static std::string GetDeviceTypeName(DeviceType deviceType); 105 bool SaveVolumeInternal(DeviceType type, AudioStreamType streamType, int32_t volumeLevel); 106 int32_t GetDeviceVolumeInternal(DeviceType deviceType, AudioStreamType streamType); 107 bool GetVolumeInternal(DeviceType deviceType, AudioStreamType streamType); 108 void SetStreamVolumeInternal(AudioStreamType streamType, int32_t volumeLevel); 109 bool SaveMuteStatusInternal(DeviceType deviceType, AudioStreamType streamType, bool muteStatus); 110 bool GetMuteStatusInternal(DeviceType deviceType, AudioStreamType streamType); 111 bool GetStreamMuteInternal(AudioStreamType streamType); 112 int32_t GetStreamVolumeInternal(AudioStreamType streamType); 113 114 ffrt::mutex volumeMutex_; 115 ffrt::mutex volumeForDbMutex_; 116 std::unordered_map<AudioStreamType, bool> muteStatusMap_; // save System volume Mutestatus map 117 std::unordered_map<AudioStreamType, int32_t> volumeLevelMap_; // save system volume map 118 std::unordered_map<int32_t, int32_t> appVolumeLevelMap_; // save App volume map 119 std::unordered_map<int32_t, std::unordered_map<int32_t, bool>> appMuteStatusMap_; // save App volume Mutestatus map 120 bool isSettingsCloneHaveStarted_ = false; 121 }; 122 } // namespace AudioStandard 123 } // namespace OHOS 124 #endif // VOLUME_DATA_MAINTAINER_H 125