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 #include "audio_errors.h" 32 33 namespace OHOS { 34 namespace AudioStandard { 35 36 class VolumeDataMaintainer { 37 public: 38 enum VolumeDataMaintainerStreamType { // define with Dual framework 39 VT_STREAM_DEFAULT = -1, 40 VT_STREAM_VOICE_CALL = 0, 41 VT_STREAM_SYSTEM = 1, 42 VT_STREAM_RING = 2, 43 VT_STREAM_MUSIC = 3, 44 VT_STREAM_ALARM = 4, 45 VT_STREAM_NOTIFICATION = 5, 46 VT_STREAM_BLUETOOTH_SCO = 6, 47 VT_STREAM_SYSTEM_ENFORCED = 7, 48 VT_STREAM_DTMF = 8, 49 VT_STREAM_TTS = 9, 50 VT_STREAM_ACCESSIBILITY = 10, 51 VT_STREAM_ASSISTANT = 11, 52 }; 53 54 enum class VolumeDbAccessExceptionFuncId : int32_t { 55 UNKNOWN = 0, 56 SAVE_VOLUME_INTERNA_A, 57 SAVE_VOLUME_INTERNA_B, 58 GET_VOLUME_INTERNAL_A, 59 GET_VOLUME_INTERNAL_B, 60 SAVE_MUTE_STATUS_INTERNAL, 61 GET_MUTE_STATUS_INTERNAL_A, 62 GET_MUTE_STATUS_INTERNAL_B, 63 GET_MUTE_AFFECTED, 64 GET_MUTE_TRANSFER_STATUS, 65 SAVE_MUTE_TRANSFER_STATUS, 66 SAVE_RINGER_MODE, 67 GET_RINGER_MODE, 68 SAVE_SAFE_STATUS, 69 GET_SAFE_STATUS_A, 70 GET_SAFE_STATUS_B, 71 SAVE_SAFE_VOLUME_TIME_A, 72 SAVE_SAFE_VOLUME_TIME_B, 73 GET_SAFE_VOLUME_TIME_A, 74 GET_SAFE_VOLUME_TIME_B, 75 SET_RESTORE_VOLUME_LEVEL_A, 76 SET_RESTORE_VOLUME_LEVEL_B, 77 GET_RESTORE_VOLUME_LEVEL_A, 78 GET_RESTORE_VOLUME_LEVEL_B, 79 SAVE_SYSTEM_SOUND_URL, 80 GET_SYSTEM_SOUND_URL, 81 SAVE_MIC_MUTE_STATE, 82 GET_MIC_MUTE_STATE, 83 }; 84 85 VolumeDataMaintainer(); 86 ~VolumeDataMaintainer(); 87 88 void SetDataShareReady(std::atomic<bool> isDataShareReady); 89 bool SaveVolume(DeviceType type, AudioStreamType streamType, int32_t volumeLevel, 90 std::string networkId = LOCAL_NETWORK_ID); 91 bool GetVolume(DeviceType deviceType, AudioStreamType streamType, std::string networkId = LOCAL_NETWORK_ID); 92 void SetStreamVolume(AudioStreamType streamType, int32_t volumeLevel); 93 void SetAppVolume(int32_t appUid, int32_t volumeLevel); 94 void GetAppMute(int32_t appUid, bool &isMute); 95 void GetAppMuteOwned(int32_t appUid, bool &isMute); 96 void SetAppVolumeMuted(int32_t appUid, bool muted); 97 void SetAppStreamMuted(int32_t appUid, AudioStreamType streamType, bool muted); 98 bool IsAppStreamMuted(int32_t appUid, AudioStreamType streamType); 99 int32_t GetStreamVolume(AudioStreamType streamType); 100 int32_t GetDeviceVolume(DeviceType deviceType, AudioStreamType streamType); 101 int32_t GetAppVolume(int32_t appUid); 102 bool IsSetAppVolume(int32_t appUid); 103 std::unordered_map<AudioStreamType, int32_t> GetVolumeMap(); 104 105 bool GetMuteStatusWithDatabaseVolumeName(const std::string &databaseVolumeName, AudioStreamType streamType); 106 bool SaveMuteStatusWithDatabaseVolumeName(const std::string &databaseVolumeName, AudioStreamType streamType, 107 bool muteStatus); 108 bool GetVolumeWithDatabaseVolumeName(const std::string &databaseVolumeName, AudioStreamType streamType); 109 bool SaveVolumeWithDatabaseVolumeName(const std::string &databaseVolumeName, AudioStreamType streamType, 110 int32_t volumeLevel); 111 bool SaveMuteStatus(DeviceType deviceType, AudioStreamType streamType, bool muteStatus, 112 std::string networkId = LOCAL_NETWORK_ID); 113 bool GetMuteStatus(DeviceType deviceType, AudioStreamType streamType, std::string networkId = LOCAL_NETWORK_ID); 114 bool SetStreamMuteStatus(AudioStreamType streamType, bool muteStatus); 115 bool GetStreamMute(AudioStreamType streamType); 116 117 bool GetMuteAffected(int32_t &affected); 118 bool GetMuteTransferStatus(bool &status); 119 bool SetMuteAffectedToMuteStatusDataBase(int32_t affected); 120 bool SaveMuteTransferStatus(bool status); 121 122 bool SaveRingerMode(AudioRingerMode ringerMode); 123 bool GetRingerMode(AudioRingerMode &ringerMode); 124 bool SaveSafeStatus(DeviceType deviceType, SafeStatus safeStatus); 125 bool GetSafeStatus(DeviceType deviceType, SafeStatus &safeStatus); 126 bool SaveSafeVolumeTime(DeviceType deviceType, int64_t time); 127 bool GetSafeVolumeTime(DeviceType deviceType, int64_t &time); 128 bool SaveSystemSoundUrl(const std::string &key, const std::string &value); 129 bool GetSystemSoundUrl(const std::string &key, std::string &value); 130 bool SetRestoreVolumeLevel(DeviceType deviceType, int32_t volume); 131 bool GetRestoreVolumeLevel(DeviceType deviceType, int32_t &volume); 132 void RegisterCloned(); 133 bool SaveMicMuteState(bool isMute); 134 bool GetMicMuteState(bool &isMute); 135 bool CheckOsAccountReady(); 136 137 void SetVolumeDegree(AudioStreamType streamType, int32_t volumeDegree); 138 int32_t GetVolumeDegree(AudioStreamType streamType); 139 bool SaveVolumeDegree(DeviceType type, AudioStreamType streamType, int32_t volumeDegree, 140 std::string networkId = LOCAL_NETWORK_ID); 141 bool GetVolumeDegree(DeviceType deviceType, AudioStreamType streamType, std::string networkId = LOCAL_NETWORK_ID); 142 private: 143 static std::string GetVolumeKeyForDataShare(DeviceType deviceType, AudioStreamType streamType, 144 std::string networkId = LOCAL_NETWORK_ID); 145 static std::string GetMuteKeyForDataShare(DeviceType deviceType, AudioStreamType streamType, 146 std::string networkId = LOCAL_NETWORK_ID); 147 static std::string GetVolumeKeyForDatabaseVolumeName(std::string databaseVolumeName, AudioStreamType streamType); 148 static std::string GetMuteKeyForDatabaseVolumeName(std::string databaseVolumeName, AudioStreamType streamType); 149 static std::string GetDeviceTypeName(DeviceType deviceType); 150 bool SaveVolumeInternal(DeviceType type, AudioStreamType streamType, int32_t volumeLevel, std::string networkId); 151 int32_t GetDeviceVolumeInternal(DeviceType deviceType, AudioStreamType streamType); 152 bool GetVolumeInternal(DeviceType deviceType, AudioStreamType streamType, std::string networkId); 153 void SetStreamVolumeInternal(AudioStreamType streamType, int32_t volumeLevel); 154 bool SaveMuteStatusInternal(DeviceType deviceType, AudioStreamType streamType, bool muteStatus, 155 std::string networkId = LOCAL_NETWORK_ID); 156 bool GetMuteStatusInternal(DeviceType deviceType, AudioStreamType streamType, 157 std::string networkId = LOCAL_NETWORK_ID); 158 bool GetStreamMuteInternal(AudioStreamType streamType); 159 int32_t GetStreamVolumeInternal(AudioStreamType streamType); 160 void WriteVolumeDbAccessExceptionEvent(int32_t errorCase, int32_t errorMsg); 161 162 ffrt::mutex volumeMutex_; 163 ffrt::mutex volumeForDbMutex_; 164 std::unordered_map<AudioStreamType, bool> muteStatusMap_; // save System volume Mutestatus map 165 std::unordered_map<AudioStreamType, int32_t> volumeLevelMap_; // save system volume map 166 std::unordered_map<int32_t, int32_t> appVolumeLevelMap_; // save App volume map 167 std::unordered_map<int32_t, std::unordered_map<int32_t, bool>> appMuteStatusMap_; // save App volume Mutestatus map 168 169 // Stores the mute status of audio streams used by the app. 170 std::unordered_map<int32_t, std::unordered_map<AudioStreamType, bool>> appStreamMuteMap_; 171 std::unordered_map<AudioStreamType, int32_t> volumeDegreeMap_; // save system volume degree map 172 bool isSettingsCloneHaveStarted_ = false; 173 }; 174 } // namespace AudioStandard 175 } // namespace OHOS 176 #endif // VOLUME_DATA_MAINTAINER_H 177