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