• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 LOG_TAG
16 #define LOG_TAG "VolumeDataMaintainer"
17 #endif
18 
19 #include "volume_data_maintainer.h"
20 #include "system_ability_definition.h"
21 #include "audio_policy_manager_factory.h"
22 
23 namespace OHOS {
24 namespace AudioStandard {
25 const std::string AUDIO_SAFE_VOLUME_STATE = "audio_safe_volume_state";
26 const std::string AUDIO_SAFE_VOLUME_STATE_BT = "audio_safe_volume_state_bt";
27 const std::string UNSAFE_VOLUME_MUSIC_ACTIVE_MS = "unsafe_volume_music_active_ms";
28 const std::string UNSAFE_VOLUME_MUSIC_ACTIVE_MS_BT = "unsafe_volume_music_active_ms_bt";
29 const std::string UNSAFE_VOLUME_LEVEL = "unsafe_volume_level";
30 const std::string UNSAFE_VOLUME_LEVEL_BT = "unsafe_volume_level_bt";
31 const std::string SETTINGS_CLONED = "settingsCloneStatus";
32 const int32_t INVALIAD_SETTINGS_CLONE_STATUS = -1;
33 const int32_t SETTINGS_CLONING_STATUS = 1;
34 const int32_t SETTINGS_CLONED_STATUS = 0;
35 constexpr int32_t MAX_SAFE_STATUS = 2;
36 
37 static const std::vector<VolumeDataMaintainer::VolumeDataMaintainerStreamType> VOLUME_MUTE_STREAM_TYPE = {
38     // all volume types except STREAM_ALL
39     VolumeDataMaintainer::VT_STREAM_ALARM,
40     VolumeDataMaintainer::VT_STREAM_DTMF,
41     VolumeDataMaintainer::VT_STREAM_TTS,
42     VolumeDataMaintainer::VT_STREAM_ACCESSIBILITY,
43     VolumeDataMaintainer::VT_STREAM_ASSISTANT,
44 };
45 
46 static const std::vector<DeviceType> DEVICE_TYPE_LIST = {
47     // The three devices represent the three volume groups(build-in, wireless, wired).
48     DEVICE_TYPE_SPEAKER,
49     DEVICE_TYPE_BLUETOOTH_A2DP,
50     DEVICE_TYPE_WIRED_HEADSET,
51     DEVICE_TYPE_REMOTE_CAST
52 };
53 
54 static std::map<VolumeDataMaintainer::VolumeDataMaintainerStreamType, AudioStreamType> AUDIO_STREAMTYPE_MAP = {
55     {VolumeDataMaintainer::VT_STREAM_ALARM, STREAM_ALARM},
56     {VolumeDataMaintainer::VT_STREAM_DTMF, STREAM_DTMF},
57     {VolumeDataMaintainer::VT_STREAM_TTS, STREAM_VOICE_ASSISTANT},
58     {VolumeDataMaintainer::VT_STREAM_ACCESSIBILITY, STREAM_ACCESSIBILITY},
59 };
60 
61 static std::map<AudioStreamType, std::string> AUDIO_STREAMTYPE_VOLUME_MAP = {
62     {STREAM_MUSIC, "music_volume"},
63     {STREAM_RING, "ring_volume"},
64     {STREAM_SYSTEM, "system_volume"},
65     {STREAM_NOTIFICATION, "notification_volume"},
66     {STREAM_ALARM, "alarm_volume"},
67     {STREAM_DTMF, "dtmf_volume"},
68     {STREAM_VOICE_CALL, "voice_call_volume"},
69     {STREAM_VOICE_ASSISTANT, "voice_assistant_volume"},
70     {STREAM_ACCESSIBILITY, "accessibility_volume"},
71     {STREAM_ULTRASONIC, "ultrasonic_volume"},
72     {STREAM_WAKEUP,  "wakeup"},
73 };
74 
75 static std::map<AudioStreamType, std::string> AUDIO_STREAMTYPE_MUTE_STATUS_MAP = {
76     {STREAM_MUSIC, "music_mute_status"},
77     {STREAM_RING, "ring_mute_status"},
78     {STREAM_SYSTEM, "system_mute_status"},
79     {STREAM_NOTIFICATION, "notification_mute_status"},
80     {STREAM_ALARM, "alarm_mute_status"},
81     {STREAM_DTMF, "dtmf_mute_status"},
82     {STREAM_VOICE_CALL, "voice_call_mute_status"},
83     {STREAM_VOICE_ASSISTANT, "voice_assistant_mute_status"},
84     {STREAM_ACCESSIBILITY, "accessibility_mute_status"},
85     {STREAM_ULTRASONIC, "unltrasonic_mute_status"},
86 };
87 
VolumeDataMaintainer()88 VolumeDataMaintainer::VolumeDataMaintainer()
89 {
90     AUDIO_DEBUG_LOG("VolumeDataMaintainer Create");
91 }
92 
~VolumeDataMaintainer()93 VolumeDataMaintainer::~VolumeDataMaintainer()
94 {
95     AUDIO_DEBUG_LOG("VolumeDataMaintainer Destory");
96 }
97 
CheckOsAccountReady()98 bool VolumeDataMaintainer::CheckOsAccountReady()
99 {
100     return AudioSettingProvider::CheckOsAccountReady();
101 }
102 
SetDataShareReady(std::atomic<bool> isDataShareReady)103 void VolumeDataMaintainer::SetDataShareReady(std::atomic<bool> isDataShareReady)
104 {
105     AudioSettingProvider& audioSettingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
106     audioSettingProvider.SetDataShareReady(std::atomic_load(&isDataShareReady));
107 }
108 
SaveVolume(DeviceType type,AudioStreamType streamType,int32_t volumeLevel)109 bool VolumeDataMaintainer::SaveVolume(DeviceType type, AudioStreamType streamType, int32_t volumeLevel)
110 {
111     std::lock_guard<ffrt::mutex> lock(volumeForDbMutex_);
112     AudioStreamType streamForVolumeMap = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
113     return SaveVolumeInternal(type, streamForVolumeMap, volumeLevel);
114 }
115 
SaveVolumeInternal(DeviceType type,AudioStreamType streamType,int32_t volumeLevel)116 bool VolumeDataMaintainer::SaveVolumeInternal(DeviceType type, AudioStreamType streamType, int32_t volumeLevel)
117 {
118     std::string volumeKey = GetVolumeKeyForDataShare(type, streamType);
119     if (!volumeKey.compare("")) {
120         AUDIO_ERR_LOG("[device %{public}d, streamType %{public}d] is not supported for datashare",
121             type, streamType);
122         return false;
123     }
124 
125     AudioSettingProvider& audioSettingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
126     ErrCode ret = audioSettingProvider.PutIntValue(volumeKey, volumeLevel, "system");
127     if (ret != SUCCESS) {
128         AUDIO_ERR_LOG("Save Volume To DataBase volumeMap failed");
129         return false;
130     }
131     return true;
132 }
133 
GetVolume(DeviceType deviceType,AudioStreamType streamType)134 bool VolumeDataMaintainer::GetVolume(DeviceType deviceType, AudioStreamType streamType)
135 {
136     std::lock_guard<ffrt::mutex> lock(volumeForDbMutex_);
137     AudioStreamType streamForVolumeMap = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
138     return GetVolumeInternal(deviceType, streamForVolumeMap);
139 }
140 
GetVolumeInternal(DeviceType deviceType,AudioStreamType streamType)141 bool VolumeDataMaintainer::GetVolumeInternal(DeviceType deviceType, AudioStreamType streamType)
142 {
143     // Voice call assistant stream is full volume by default
144     if (streamType == STREAM_VOICE_CALL_ASSISTANT) {
145         return true;
146     }
147     std::string volumeKey = GetVolumeKeyForDataShare(deviceType, streamType);
148     if (!volumeKey.compare("")) {
149         AUDIO_ERR_LOG("[device %{public}d, streamType %{public}d] is not supported for datashare",
150             deviceType, streamType);
151         return false;
152     }
153 
154     AudioSettingProvider& audioSettingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
155     int32_t volumeValue = 0;
156     ErrCode ret = audioSettingProvider.GetIntValue(volumeKey, volumeValue, "system");
157     if (ret != SUCCESS) {
158         AUDIO_ERR_LOG("Get streamType %{public}d, deviceType %{public}d, Volume FromDataBase volumeMap failed.",
159             streamType, deviceType);
160         return false;
161     } else {
162         volumeLevelMap_[streamType] = volumeValue;
163         AUDIO_PRERELEASE_LOGI("Get streamType %{public}d, deviceType %{public}d, "\
164             "Volume FromDataBase volumeMap from datashare %{public}d.", streamType, deviceType, volumeValue);
165     }
166 
167     return true;
168 }
169 
SetAppVolume(int32_t appUid,int32_t volumeLevel)170 void VolumeDataMaintainer::SetAppVolume(int32_t appUid, int32_t volumeLevel)
171 {
172     std::lock_guard<ffrt::mutex> lock(volumeMutex_);
173     appVolumeLevelMap_[appUid] = volumeLevel;
174 }
175 
SetAppVolumeMuted(int32_t appUid,bool muted)176 void VolumeDataMaintainer::SetAppVolumeMuted(int32_t appUid, bool muted)
177 {
178     std::lock_guard<ffrt::mutex> lock(volumeMutex_);
179     int ownedAppUid = IPCSkeleton::GetCallingUid();
180     appMuteStatusMap_[appUid][ownedAppUid] = muted;
181 }
182 
GetAppMute(int32_t appUid)183 bool VolumeDataMaintainer::GetAppMute(int32_t appUid)
184 {
185     std::lock_guard<ffrt::mutex> lock(volumeMutex_);
186     auto iter = appMuteStatusMap_.find(appUid);
187     if (iter == appMuteStatusMap_.end()) {
188         return false;
189     } else {
190         for (auto subIter : iter->second) {
191             if (subIter.second) {
192                 return true;
193             }
194         }
195     }
196     return false;
197 }
198 
GetAppMuteOwned(int32_t appUid)199 bool VolumeDataMaintainer::GetAppMuteOwned(int32_t appUid)
200 {
201     std::lock_guard<ffrt::mutex> lock(volumeMutex_);
202     int ownedAppUid = IPCSkeleton::GetCallingUid();
203     auto iter = appMuteStatusMap_.find(appUid);
204     if (iter == appMuteStatusMap_.end()) {
205         return false;
206     } else {
207         return iter->second[ownedAppUid];
208     }
209 }
210 
SetStreamVolume(AudioStreamType streamType,int32_t volumeLevel)211 void VolumeDataMaintainer::SetStreamVolume(AudioStreamType streamType, int32_t volumeLevel)
212 {
213     std::lock_guard<ffrt::mutex> lock(volumeMutex_);
214     SetStreamVolumeInternal(streamType, volumeLevel);
215 }
216 
SetStreamVolumeInternal(AudioStreamType streamType,int32_t volumeLevel)217 void VolumeDataMaintainer::SetStreamVolumeInternal(AudioStreamType streamType, int32_t volumeLevel)
218 {
219     AudioStreamType streamForVolumeMap = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
220     volumeLevelMap_[streamForVolumeMap] = volumeLevel;
221 }
222 
GetStreamVolume(AudioStreamType streamType)223 int32_t VolumeDataMaintainer::GetStreamVolume(AudioStreamType streamType)
224 {
225     std::lock_guard<ffrt::mutex> lock(volumeMutex_);
226     return GetStreamVolumeInternal(streamType);
227 }
228 
GetDeviceVolume(DeviceType deviceType,AudioStreamType streamType)229 int32_t VolumeDataMaintainer::GetDeviceVolume(DeviceType deviceType, AudioStreamType streamType)
230 {
231     std::lock_guard<ffrt::mutex> lock(volumeMutex_);
232     AudioStreamType streamForVolumeMap = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
233     return GetDeviceVolumeInternal(deviceType, streamForVolumeMap);
234 }
235 
GetDeviceVolumeInternal(DeviceType deviceType,AudioStreamType streamType)236 int32_t VolumeDataMaintainer::GetDeviceVolumeInternal(DeviceType deviceType, AudioStreamType streamType)
237 {
238     std::string volumeKey = GetVolumeKeyForDataShare(deviceType, streamType);
239     int32_t volumeValue = 0;
240     if (!volumeKey.compare("")) {
241         AUDIO_ERR_LOG("[device %{public}d, streamType %{public}d] is not supported for datashare",
242             deviceType, streamType);
243         return volumeValue;
244     }
245 
246     AudioSettingProvider& audioSettingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
247     ErrCode ret = audioSettingProvider.GetIntValue(volumeKey, volumeValue, "system");
248     if (ret != SUCCESS) {
249         AUDIO_ERR_LOG("Get streamType %{public}d, deviceType %{public}d, Volume FromDataBase volumeMap failed.",
250             streamType, deviceType);
251     } else {
252         AUDIO_PRERELEASE_LOGI("Get streamType %{public}d, deviceType %{public}d, "\
253             "Volume FromDataBase volumeMap from datashare %{public}d.", streamType, deviceType, volumeValue);
254     }
255 
256     return volumeValue;
257 }
258 
IsSetAppVolume(int32_t appUid)259 bool VolumeDataMaintainer::IsSetAppVolume(int32_t appUid)
260 {
261     std::lock_guard<ffrt::mutex> lock(volumeMutex_);
262     return appVolumeLevelMap_.find(appUid) != appVolumeLevelMap_.end();
263 }
264 
GetAppVolume(int32_t appUid)265 int32_t VolumeDataMaintainer::GetAppVolume(int32_t appUid)
266 {
267     std::lock_guard<ffrt::mutex> lock(volumeMutex_);
268     return appVolumeLevelMap_[appUid];
269 }
270 
271 
GetStreamVolumeInternal(AudioStreamType streamType)272 int32_t VolumeDataMaintainer::GetStreamVolumeInternal(AudioStreamType streamType)
273 {
274     AudioStreamType streamForVolumeMap = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
275     return volumeLevelMap_[streamForVolumeMap];
276 }
277 
GetVolumeMap()278 std::unordered_map<AudioStreamType, int32_t> VolumeDataMaintainer::GetVolumeMap()
279 {
280     std::lock_guard<ffrt::mutex> lock(volumeMutex_);
281     return volumeLevelMap_;
282 }
283 
SaveMuteStatus(DeviceType deviceType,AudioStreamType streamType,bool muteStatus)284 bool VolumeDataMaintainer::SaveMuteStatus(DeviceType deviceType, AudioStreamType streamType,
285     bool muteStatus)
286 {
287     std::lock_guard<ffrt::mutex> lock(volumeForDbMutex_);
288     if (streamType == STREAM_RING && VolumeUtils::GetVolumeTypeFromStreamType(streamType) == STREAM_RING) {
289         AUDIO_INFO_LOG("set ring stream mute status to all device.");
290         bool saveMuteResult = false;
291         for (auto &device : DEVICE_TYPE_LIST) {
292             // set ring stream mute status to device
293             saveMuteResult = SaveMuteStatusInternal(device, streamType, muteStatus);
294             if (!saveMuteResult) {
295                 AUDIO_INFO_LOG("save mute failed.");
296                 break;
297             }
298         }
299         return saveMuteResult;
300     }
301     AudioStreamType streamForVolumeMap = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
302     return SaveMuteStatusInternal(deviceType, streamForVolumeMap, muteStatus);
303 }
304 
SaveMuteStatusInternal(DeviceType deviceType,AudioStreamType streamType,bool muteStatus)305 bool VolumeDataMaintainer::SaveMuteStatusInternal(DeviceType deviceType, AudioStreamType streamType,
306     bool muteStatus)
307 {
308     std::string muteKey = GetMuteKeyForDataShare(deviceType, streamType);
309     if (!muteKey.compare("")) {
310         AUDIO_ERR_LOG("[device %{public}d, streamType %{public}d] is not supported for "\
311             "datashare", deviceType, streamType);
312         return false;
313     }
314 
315     AudioSettingProvider& audioSettingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
316     ErrCode ret = audioSettingProvider.PutBoolValue(muteKey, muteStatus, "system");
317     if (ret != SUCCESS) {
318         AUDIO_WARNING_LOG("Failed to write mutestatus: %{public}d to setting db! Err: %{public}d", muteStatus, ret);
319     } else {
320         AUDIO_DEBUG_LOG("muteKey:%{public}s, muteStatus:%{public}d", muteKey.c_str(), muteStatus);
321     }
322 
323     return true;
324 }
325 
SetStreamMuteStatus(AudioStreamType streamType,bool muteStatus)326 bool VolumeDataMaintainer::SetStreamMuteStatus(AudioStreamType streamType, bool muteStatus)
327 {
328     std::lock_guard<ffrt::mutex> lock(volumeMutex_);
329     AudioStreamType streamForVolumeMap = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
330     muteStatusMap_[streamForVolumeMap] = muteStatus;
331     return true;
332 }
333 
GetMuteStatus(DeviceType deviceType,AudioStreamType streamType)334 bool VolumeDataMaintainer::GetMuteStatus(DeviceType deviceType, AudioStreamType streamType)
335 {
336     std::lock_guard<ffrt::mutex> lock(volumeForDbMutex_);
337     AudioStreamType streamForVolumeMap = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
338     return GetMuteStatusInternal(deviceType, streamForVolumeMap);
339 }
340 
GetMuteStatusInternal(DeviceType deviceType,AudioStreamType streamType)341 bool VolumeDataMaintainer::GetMuteStatusInternal(DeviceType deviceType, AudioStreamType streamType)
342 {
343     std::string muteKey = GetMuteKeyForDataShare(deviceType, streamType);
344     if (!muteKey.compare("")) {
345         AUDIO_ERR_LOG("[device %{public}d, streamType %{public}d] is not supported for "\
346             "datashare", deviceType, streamType);
347         return false;
348     }
349 
350     AudioSettingProvider& audioSettingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
351     bool muteStatus = false;
352     ErrCode ret = audioSettingProvider.GetBoolValue(muteKey, muteStatus, "system");
353     if (ret != SUCCESS) {
354         AUDIO_ERR_LOG("Get MuteStatus From DataBase muteStatus failed");
355         return false;
356     } else {
357         muteStatusMap_[streamType] = muteStatus;
358         AUDIO_DEBUG_LOG("Get MuteStatus From DataBase muteStatus from datashare %{public}d", muteStatus);
359     }
360 
361     return true;
362 }
363 
GetStreamMute(AudioStreamType streamType)364 bool VolumeDataMaintainer::GetStreamMute(AudioStreamType streamType)
365 {
366     std::lock_guard<ffrt::mutex> lock(volumeMutex_);
367     return GetStreamMuteInternal(streamType);
368 }
369 
GetStreamMuteInternal(AudioStreamType streamType)370 bool VolumeDataMaintainer::GetStreamMuteInternal(AudioStreamType streamType)
371 {
372     AudioStreamType streamForVolumeMap = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
373     return muteStatusMap_[streamForVolumeMap];
374 }
375 
GetMuteAffected(int32_t & affected)376 bool VolumeDataMaintainer::GetMuteAffected(int32_t &affected)
377 {
378     AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
379     const std::string settingKey = "mute_streams_affected";
380     int32_t value = 0;
381     ErrCode ret = settingProvider.GetIntValue(settingKey, value, "system");
382     if (ret != SUCCESS) {
383         AUDIO_WARNING_LOG("Failed to get muteaffected failed Err: %{public}d", ret);
384         return false;
385     } else {
386         affected = value;
387     }
388     return true;
389 }
390 
GetMuteTransferStatus(bool & status)391 bool VolumeDataMaintainer::GetMuteTransferStatus(bool &status)
392 {
393     AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
394     const std::string settingKey = "need_mute_affected_transfer";
395     ErrCode ret = settingProvider.GetBoolValue(settingKey, status);
396     if (ret != SUCCESS) {
397         AUDIO_WARNING_LOG("Failed to get muteaffected failed Err: %{public}d", ret);
398         return false;
399     }
400     return true;
401 }
402 
SetMuteAffectedToMuteStatusDataBase(int32_t affected)403 bool VolumeDataMaintainer::SetMuteAffectedToMuteStatusDataBase(int32_t affected)
404 {
405     // transfer mute_streams_affected to mutestatus
406     for (auto &streamtype : VOLUME_MUTE_STREAM_TYPE) {
407         if (static_cast<uint32_t>(affected) & (1 << streamtype)) {
408             for (auto &device : DEVICE_TYPE_LIST) {
409                 // save mute status to database
410                 SaveMuteStatusInternal(device, AUDIO_STREAMTYPE_MAP[streamtype], true);
411             }
412         }
413     }
414     return true;
415 }
416 
SaveMuteTransferStatus(bool status)417 bool VolumeDataMaintainer::SaveMuteTransferStatus(bool status)
418 {
419     AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
420     const std::string settingKey = "need_mute_affected_transfer";
421     ErrCode ret = settingProvider.PutIntValue(settingKey, status);
422     if (ret != SUCCESS) {
423         AUDIO_WARNING_LOG("Failed to SaveMuteTransferStatus: %{public}d to setting db! Err: %{public}d", status, ret);
424         return false;
425     }
426     return true;
427 }
428 
SaveRingerMode(AudioRingerMode ringerMode)429 bool VolumeDataMaintainer::SaveRingerMode(AudioRingerMode ringerMode)
430 {
431     AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
432     const std::string settingKey = "ringer_mode";
433     ErrCode ret = settingProvider.PutIntValue(settingKey, static_cast<int32_t>(ringerMode));
434     if (ret != SUCCESS) {
435         AUDIO_WARNING_LOG("Failed to write ringer_mode: %{public}d to setting db! Err: %{public}d", ringerMode, ret);
436         return false;
437     }
438     return true;
439 }
440 
GetRingerMode(AudioRingerMode & ringerMode)441 bool VolumeDataMaintainer::GetRingerMode(AudioRingerMode &ringerMode)
442 {
443     AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
444     const std::string settingKey = "ringer_mode";
445     int32_t value = 0;
446     ErrCode ret = settingProvider.GetIntValue(settingKey, value);
447     if (ret != SUCCESS) {
448         AUDIO_WARNING_LOG("Failed to write ringer_mode: %{public}d to setting db! Err: %{public}d", ringerMode, ret);
449         return false;
450     } else {
451         ringerMode = static_cast<AudioRingerMode>(value);
452     }
453     return true;
454 }
455 
SaveSafeStatus(DeviceType deviceType,SafeStatus safeStatus)456 bool VolumeDataMaintainer::SaveSafeStatus(DeviceType deviceType, SafeStatus safeStatus)
457 {
458     AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
459     ErrCode ret = SUCCESS;
460     switch (deviceType) {
461         case DEVICE_TYPE_BLUETOOTH_A2DP:
462         case DEVICE_TYPE_BLUETOOTH_SCO:
463             ret = settingProvider.PutIntValue(AUDIO_SAFE_VOLUME_STATE_BT, static_cast<int32_t>(safeStatus));
464             break;
465         case DEVICE_TYPE_WIRED_HEADSET:
466         case DEVICE_TYPE_USB_HEADSET:
467         case DEVICE_TYPE_USB_ARM_HEADSET:
468             ret = settingProvider.PutIntValue(AUDIO_SAFE_VOLUME_STATE, static_cast<int32_t>(safeStatus));
469             break;
470         default:
471             AUDIO_WARNING_LOG("the device type not support safe volume");
472             return false;
473     }
474     if (ret != SUCCESS) {
475         AUDIO_ERR_LOG("device:%{public}d, insert failed, safe status:%{public}d", deviceType, safeStatus);
476         return false;
477     }
478     return true;
479 }
480 
GetSafeStatus(DeviceType deviceType,SafeStatus & safeStatus)481 bool VolumeDataMaintainer::GetSafeStatus(DeviceType deviceType, SafeStatus &safeStatus)
482 {
483     AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
484     ErrCode ret = SUCCESS;
485     int32_t value = 0;
486     switch (deviceType) {
487         case DEVICE_TYPE_BLUETOOTH_A2DP:
488         case DEVICE_TYPE_BLUETOOTH_SCO:
489             ret = settingProvider.GetIntValue(AUDIO_SAFE_VOLUME_STATE_BT, value);
490             break;
491         case DEVICE_TYPE_WIRED_HEADSET:
492         case DEVICE_TYPE_USB_HEADSET:
493         case DEVICE_TYPE_USB_ARM_HEADSET:
494             ret = settingProvider.GetIntValue(AUDIO_SAFE_VOLUME_STATE, value);
495             break;
496         default:
497             AUDIO_WARNING_LOG("the device type not support safe volume");
498             return false;
499     }
500     if (ret != SUCCESS) {
501         AUDIO_ERR_LOG("device:%{public}d, insert failed, safe status:%{public}d", deviceType, safeStatus);
502         return false;
503     }
504     if (value > static_cast<int32_t>(SAFE_ACTIVE)) {
505         value = value - MAX_SAFE_STATUS;
506         SaveSafeStatus(deviceType, static_cast<SafeStatus>(value));
507     }
508     safeStatus = static_cast<SafeStatus>(value);
509     return true;
510 }
511 
SaveSafeVolumeTime(DeviceType deviceType,int64_t time)512 bool VolumeDataMaintainer::SaveSafeVolumeTime(DeviceType deviceType, int64_t time)
513 {
514     AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
515     ErrCode ret = SUCCESS;
516     switch (deviceType) {
517         case DEVICE_TYPE_BLUETOOTH_A2DP:
518         case DEVICE_TYPE_BLUETOOTH_SCO:
519             ret = settingProvider.PutLongValue(UNSAFE_VOLUME_MUSIC_ACTIVE_MS_BT, time, "secure");
520             break;
521         case DEVICE_TYPE_WIRED_HEADSET:
522         case DEVICE_TYPE_USB_HEADSET:
523         case DEVICE_TYPE_USB_ARM_HEADSET:
524             ret = settingProvider.PutLongValue(UNSAFE_VOLUME_MUSIC_ACTIVE_MS, time, "secure");
525             break;
526         default:
527             AUDIO_WARNING_LOG("the device type not support safe volume");
528             return false;
529     }
530     if (ret != SUCCESS) {
531         AUDIO_ERR_LOG("device:%{public}d, insert failed", deviceType);
532         return false;
533     }
534 
535     return true;
536 }
537 
GetSafeVolumeTime(DeviceType deviceType,int64_t & time)538 bool VolumeDataMaintainer::GetSafeVolumeTime(DeviceType deviceType, int64_t &time)
539 {
540     AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
541     ErrCode ret = SUCCESS;
542     switch (deviceType) {
543         case DEVICE_TYPE_BLUETOOTH_A2DP:
544         case DEVICE_TYPE_BLUETOOTH_SCO:
545             ret = settingProvider.GetLongValue(UNSAFE_VOLUME_MUSIC_ACTIVE_MS_BT, time, "secure");
546             break;
547         case DEVICE_TYPE_WIRED_HEADSET:
548         case DEVICE_TYPE_USB_HEADSET:
549         case DEVICE_TYPE_USB_ARM_HEADSET:
550             ret = settingProvider.GetLongValue(UNSAFE_VOLUME_MUSIC_ACTIVE_MS, time, "secure");
551             break;
552         default:
553             AUDIO_WARNING_LOG("the device type not support safe mode");
554             return false;
555     }
556     if (ret != SUCCESS) {
557         AUDIO_ERR_LOG("device:%{public}d, get safe active time failed", deviceType);
558         return false;
559     }
560     return true;
561 }
562 
SetRestoreVolumeLevel(DeviceType deviceType,int32_t volume)563 bool VolumeDataMaintainer::SetRestoreVolumeLevel(DeviceType deviceType, int32_t volume)
564 {
565     AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
566     ErrCode ret = SUCCESS;
567     switch (deviceType) {
568         case DEVICE_TYPE_BLUETOOTH_A2DP:
569         case DEVICE_TYPE_BLUETOOTH_SCO:
570             ret = settingProvider.PutIntValue(UNSAFE_VOLUME_LEVEL_BT, volume);
571             break;
572         case DEVICE_TYPE_WIRED_HEADSET:
573         case DEVICE_TYPE_USB_HEADSET:
574         case DEVICE_TYPE_USB_ARM_HEADSET:
575         case DEVICE_TYPE_DP:
576             ret = settingProvider.PutIntValue(UNSAFE_VOLUME_LEVEL, volume);
577             break;
578         default:
579             AUDIO_WARNING_LOG("the device type not support safe volume");
580             return false;
581     }
582     if (ret != SUCCESS) {
583         AUDIO_ERR_LOG("device:%{public}d, insert failed", deviceType);
584         return false;
585     }
586 
587     return true;
588 }
589 
GetRestoreVolumeLevel(DeviceType deviceType,int32_t & volume)590 bool VolumeDataMaintainer::GetRestoreVolumeLevel(DeviceType deviceType, int32_t &volume)
591 {
592     AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
593     ErrCode ret = SUCCESS;
594     int32_t value = 0;
595     switch (deviceType) {
596         case DEVICE_TYPE_BLUETOOTH_A2DP:
597         case DEVICE_TYPE_BLUETOOTH_SCO:
598             ret = settingProvider.GetIntValue(UNSAFE_VOLUME_LEVEL_BT, value);
599             break;
600         case DEVICE_TYPE_WIRED_HEADSET:
601         case DEVICE_TYPE_USB_HEADSET:
602         case DEVICE_TYPE_USB_ARM_HEADSET:
603         case DEVICE_TYPE_DP:
604             ret = settingProvider.GetIntValue(UNSAFE_VOLUME_LEVEL, value);
605             break;
606         default:
607             AUDIO_WARNING_LOG("the device type not support safe volume");
608             return false;
609     }
610     if (ret != SUCCESS) {
611         AUDIO_ERR_LOG("device:%{public}d, insert failed", deviceType);
612         return false;
613     }
614     volume = value;
615     return true;
616 }
617 
SaveSystemSoundUrl(const std::string & key,const std::string & value)618 bool VolumeDataMaintainer::SaveSystemSoundUrl(const std::string &key, const std::string &value)
619 {
620     AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
621     ErrCode ret = settingProvider.PutStringValue(key, value);
622     if (ret != SUCCESS) {
623         AUDIO_WARNING_LOG("Failed to system sound url: %{public}s to setting db! Err: %{public}d", value.c_str(), ret);
624         return false;
625     }
626     return true;
627 }
628 
GetSystemSoundUrl(const std::string & key,std::string & value)629 bool VolumeDataMaintainer::GetSystemSoundUrl(const std::string &key, std::string &value)
630 {
631     AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
632     ErrCode ret = settingProvider.GetStringValue(key, value);
633     if (ret != SUCCESS) {
634         AUDIO_WARNING_LOG("Failed to get systemsoundurl failed Err: %{public}d", ret);
635         return false;
636     }
637     return true;
638 }
639 
RegisterCloned()640 void VolumeDataMaintainer::RegisterCloned()
641 {
642     AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
643     AudioSettingObserver::UpdateFunc updateFunc = [&](const std::string& key) {
644         int32_t value = INVALIAD_SETTINGS_CLONE_STATUS;
645         ErrCode result =
646             AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID).GetIntValue(SETTINGS_CLONED, value);
647         if (!isSettingsCloneHaveStarted_ && (value == SETTINGS_CLONING_STATUS) && (result == SUCCESS)) {
648             AUDIO_INFO_LOG("clone staring");
649             isSettingsCloneHaveStarted_ = true;
650         }
651 
652         if (isSettingsCloneHaveStarted_ && (value == SETTINGS_CLONED_STATUS) && (result == SUCCESS)) {
653             AUDIO_INFO_LOG("Get SETTINGS_CLONED success, clone done, restore.");
654             AudioPolicyManagerFactory::GetAudioPolicyManager().DoRestoreData();
655             isSettingsCloneHaveStarted_ = false;
656         }
657     };
658     sptr<AudioSettingObserver> observer = settingProvider.CreateObserver(SETTINGS_CLONED, updateFunc);
659     ErrCode ret = settingProvider.RegisterObserver(observer);
660     if (ret != ERR_OK) {
661         AUDIO_ERR_LOG("RegisterObserver failed");
662     }
663 }
664 
SaveMicMuteState(bool isMute)665 bool VolumeDataMaintainer::SaveMicMuteState(bool isMute)
666 {
667     AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
668     const std::string settingKey = "micmute_state";
669     ErrCode ret = settingProvider.PutBoolValue(settingKey, isMute, "secure");
670     if (ret != SUCCESS) {
671         AUDIO_ERR_LOG("Failed to saveMicMuteState: %{public}d to setting db! Err: %{public}d", isMute, ret);
672         return false;
673     }
674     return true;
675 }
676 
GetMicMuteState(bool & isMute)677 bool VolumeDataMaintainer::GetMicMuteState(bool &isMute)
678 {
679     AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
680     const std::string settingKey = "micmute_state";
681     ErrCode ret = settingProvider.GetBoolValue(settingKey, isMute, "secure");
682     if (ret != SUCCESS) {
683         AUDIO_WARNING_LOG("Failed to write micmute_state: %{public}d to setting db! Err: %{public}d", isMute, ret);
684         return false;
685     }
686 
687     return true;
688 }
GetDeviceTypeName(DeviceType deviceType)689 std::string VolumeDataMaintainer::GetDeviceTypeName(DeviceType deviceType)
690 {
691     std::string type = "";
692     switch (deviceType) {
693         case DEVICE_TYPE_EARPIECE:
694             type = "_earpiece";
695             return type;
696         case DEVICE_TYPE_SPEAKER:
697         case DEVICE_TYPE_DP:
698             type = "_builtin";
699             return type;
700         case DEVICE_TYPE_BLUETOOTH_A2DP:
701         case DEVICE_TYPE_BLUETOOTH_SCO:
702             type = "_wireless";
703             return type;
704         case DEVICE_TYPE_WIRED_HEADSET:
705         case DEVICE_TYPE_USB_HEADSET:
706         case DEVICE_TYPE_USB_ARM_HEADSET:
707             type = "_wired";
708             return type;
709         case DEVICE_TYPE_REMOTE_CAST:
710             type = "_remote_cast";
711             return type;
712         default:
713             AUDIO_ERR_LOG("device %{public}d is not supported for dataShare", deviceType);
714             return "";
715     }
716 }
717 
GetVolumeKeyForDataShare(DeviceType deviceType,AudioStreamType streamType)718 std::string VolumeDataMaintainer::GetVolumeKeyForDataShare(DeviceType deviceType, AudioStreamType streamType)
719 {
720     std::string type = "";
721     if (!AUDIO_STREAMTYPE_VOLUME_MAP.count(streamType)) {
722         return "";
723     }
724     type = AUDIO_STREAMTYPE_VOLUME_MAP[streamType];
725     if (type == "") {
726         AUDIO_ERR_LOG("streamType %{public}d is not supported for datashare", streamType);
727         return "";
728     }
729 
730     std::string deviceTypeName = GetDeviceTypeName(deviceType);
731     if (deviceTypeName == "") {
732         AUDIO_ERR_LOG("device %{public}d is not supported for datashare", deviceType);
733         return "";
734     }
735     if (VolumeUtils::IsPCVolumeEnable() && streamType == AudioStreamType::STREAM_MUSIC &&
736         deviceType == DeviceType::DEVICE_TYPE_BLUETOOTH_SCO) {
737         type = AUDIO_STREAMTYPE_VOLUME_MAP[STREAM_VOICE_CALL];
738     }
739     return type + deviceTypeName;
740 }
741 
GetMuteKeyForDataShare(DeviceType deviceType,AudioStreamType streamType)742 std::string VolumeDataMaintainer::GetMuteKeyForDataShare(DeviceType deviceType, AudioStreamType streamType)
743 {
744     std::string type = "";
745     if (!AUDIO_STREAMTYPE_MUTE_STATUS_MAP.count(streamType)) {
746         return "";
747     }
748     type = AUDIO_STREAMTYPE_MUTE_STATUS_MAP[streamType];
749     if (type == "") {
750         AUDIO_ERR_LOG("streamType %{public}d is not supported for datashare", streamType);
751         return "";
752     }
753 
754     std::string deviceTypeName = GetDeviceTypeName(deviceType);
755     if (deviceTypeName == "") {
756         AUDIO_ERR_LOG("device %{public}d is not supported for datashare", deviceType);
757         return "";
758     }
759     if (VolumeUtils::IsPCVolumeEnable() && streamType == AudioStreamType::STREAM_MUSIC &&
760         deviceType == DeviceType::DEVICE_TYPE_BLUETOOTH_SCO) {
761         type = AUDIO_STREAMTYPE_VOLUME_MAP[STREAM_VOICE_CALL];
762     }
763     return type + deviceTypeName;
764 }
765 
766 } // namespace AudioStandard
767 } // namespace OHOS
768