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
SetDataShareReady(std::atomic<bool> isDataShareReady)122 void VolumeDataMaintainer::SetDataShareReady(std::atomic<bool> isDataShareReady)
123 {
124 AudioSettingProvider& audioSettingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
125 audioSettingProvider.SetDataShareReady(std::atomic_load(&isDataShareReady));
126 }
127
SaveVolume(DeviceType type,AudioStreamType streamType,int32_t volumeLevel)128 bool VolumeDataMaintainer::SaveVolume(DeviceType type, AudioStreamType streamType, int32_t volumeLevel)
129 {
130 std::lock_guard<std::mutex> lock(volumeForDbMutex_);
131 std::string volumeKey = GetVolumeKeyForDataShare(type, streamType);
132 if (!volumeKey.compare("")) {
133 AUDIO_ERR_LOG("[device %{public}d, streamType %{public}d] is not supported for datashare",
134 type, streamType);
135 return false;
136 }
137
138 AudioSettingProvider& audioSettingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
139 ErrCode ret = audioSettingProvider.PutIntValue(volumeKey, volumeLevel, "system");
140 if (ret != SUCCESS) {
141 AUDIO_ERR_LOG("Save Volume To DataBase volumeMap failed");
142 return false;
143 }
144 return true;
145 }
146
GetVolume(DeviceType deviceType,AudioStreamType streamType)147 bool VolumeDataMaintainer::GetVolume(DeviceType deviceType, AudioStreamType streamType)
148 {
149 std::lock_guard<std::mutex> lock(volumeForDbMutex_);
150 return GetVolumeInternal(deviceType, streamType);
151 }
152
GetVolumeInternal(DeviceType deviceType,AudioStreamType streamType)153 bool VolumeDataMaintainer::GetVolumeInternal(DeviceType deviceType, AudioStreamType streamType)
154 {
155 // Voice call assistant stream is full volume by default
156 if (streamType == STREAM_VOICE_CALL_ASSISTANT) {
157 return true;
158 }
159 std::string volumeKey = GetVolumeKeyForDataShare(deviceType, streamType);
160 if (!volumeKey.compare("")) {
161 AUDIO_ERR_LOG("[device %{public}d, streamType %{public}d] is not supported for datashare",
162 deviceType, streamType);
163 return false;
164 }
165
166 AudioSettingProvider& audioSettingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
167 int32_t volumeValue = 0;
168 ErrCode ret = audioSettingProvider.GetIntValue(volumeKey, volumeValue, "system");
169 if (ret != SUCCESS) {
170 AUDIO_ERR_LOG("Get Volume FromDataBase volumeMap failed");
171 return false;
172 } else {
173 volumeLevelMap_[streamType] = volumeValue;
174 AUDIO_PRERELEASE_LOGI("Get streamType %{public}d Volume FromDataBase volumeMap from datashare %{public}d",
175 streamType, volumeValue);
176 }
177
178 return true;
179 }
180
SetStreamVolume(AudioStreamType streamType,int32_t volumeLevel)181 void VolumeDataMaintainer::SetStreamVolume(AudioStreamType streamType, int32_t volumeLevel)
182 {
183 std::lock_guard<std::mutex> lock(volumeMutex_);
184 SetStreamVolumeInternal(streamType, volumeLevel);
185 }
186
SetStreamVolumeInternal(AudioStreamType streamType,int32_t volumeLevel)187 void VolumeDataMaintainer::SetStreamVolumeInternal(AudioStreamType streamType, int32_t volumeLevel)
188 {
189 AudioStreamType streamForVolumeMap = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
190 volumeLevelMap_[streamForVolumeMap] = volumeLevel;
191 }
192
GetStreamVolume(AudioStreamType streamType)193 int32_t VolumeDataMaintainer::GetStreamVolume(AudioStreamType streamType)
194 {
195 std::lock_guard<std::mutex> lock(volumeMutex_);
196 return GetStreamVolumeInternal(streamType);
197 }
198
GetStreamVolumeInternal(AudioStreamType streamType)199 int32_t VolumeDataMaintainer::GetStreamVolumeInternal(AudioStreamType streamType)
200 {
201 AudioStreamType streamForVolumeMap = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
202 return volumeLevelMap_[streamForVolumeMap];
203 }
204
GetVolumeMap()205 std::unordered_map<AudioStreamType, int32_t> VolumeDataMaintainer::GetVolumeMap()
206 {
207 std::lock_guard<std::mutex> lock(volumeMutex_);
208 return volumeLevelMap_;
209 }
210
SaveMuteStatus(DeviceType deviceType,AudioStreamType streamType,bool muteStatus)211 bool VolumeDataMaintainer::SaveMuteStatus(DeviceType deviceType, AudioStreamType streamType,
212 bool muteStatus)
213 {
214 std::lock_guard<std::mutex> lock(volumeForDbMutex_);
215 if (streamType == STREAM_RING) {
216 AUDIO_INFO_LOG("set ring stream mute status to all device.");
217 bool saveMuteResult = false;
218 for (auto &device : DEVICE_TYPE_LIST) {
219 // set ring stream mute status to device
220 saveMuteResult = SaveMuteStatusInternal(device, streamType, muteStatus);
221 if (!saveMuteResult) {
222 AUDIO_INFO_LOG("save mute failed.");
223 break;
224 }
225 }
226 return saveMuteResult;
227 }
228 return SaveMuteStatusInternal(deviceType, streamType, muteStatus);
229 }
230
SaveMuteStatusInternal(DeviceType deviceType,AudioStreamType streamType,bool muteStatus)231 bool VolumeDataMaintainer::SaveMuteStatusInternal(DeviceType deviceType, AudioStreamType streamType,
232 bool muteStatus)
233 {
234 std::string muteKey = GetMuteKeyForDataShare(deviceType, streamType);
235 if (!muteKey.compare("")) {
236 AUDIO_ERR_LOG("[device %{public}d, streamType %{public}d] is not supported for "\
237 "datashare", deviceType, streamType);
238 return false;
239 }
240
241 AudioSettingProvider& audioSettingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
242 ErrCode ret = audioSettingProvider.PutBoolValue(muteKey, muteStatus, "system");
243 if (ret != SUCCESS) {
244 AUDIO_WARNING_LOG("Failed to write mutestatus: %{public}d to setting db! Err: %{public}d", muteStatus, ret);
245 } else {
246 AUDIO_DEBUG_LOG("muteKey:%{public}s, muteStatus:%{public}d", muteKey.c_str(), muteStatus);
247 }
248
249 return true;
250 }
251
SetStreamMuteStatus(AudioStreamType streamType,bool muteStatus)252 bool VolumeDataMaintainer::SetStreamMuteStatus(AudioStreamType streamType, bool muteStatus)
253 {
254 std::lock_guard<std::mutex> lock(volumeMutex_);
255 AudioStreamType streamForVolumeMap = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
256 muteStatusMap_[streamForVolumeMap] = muteStatus;
257 return true;
258 }
259
GetMuteStatus(DeviceType deviceType,AudioStreamType streamType)260 bool VolumeDataMaintainer::GetMuteStatus(DeviceType deviceType, AudioStreamType streamType)
261 {
262 std::lock_guard<std::mutex> lock(volumeForDbMutex_);
263 return GetMuteStatusInternal(deviceType, streamType);
264 }
265
GetMuteStatusInternal(DeviceType deviceType,AudioStreamType streamType)266 bool VolumeDataMaintainer::GetMuteStatusInternal(DeviceType deviceType, AudioStreamType streamType)
267 {
268 std::string muteKey = GetMuteKeyForDataShare(deviceType, streamType);
269 if (!muteKey.compare("")) {
270 AUDIO_ERR_LOG("[device %{public}d, streamType %{public}d] is not supported for "\
271 "datashare", deviceType, streamType);
272 return false;
273 }
274
275 AudioSettingProvider& audioSettingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
276 bool muteStatus = false;
277 ErrCode ret = audioSettingProvider.GetBoolValue(muteKey, muteStatus, "system");
278 if (ret != SUCCESS) {
279 AUDIO_ERR_LOG("Get MuteStatus From DataBase muteStatus failed");
280 return false;
281 } else {
282 muteStatusMap_[streamType] = muteStatus;
283 AUDIO_DEBUG_LOG("Get MuteStatus From DataBase muteStatus from datashare %{public}d", muteStatus);
284 }
285
286 return true;
287 }
288
GetStreamMute(AudioStreamType streamType)289 bool VolumeDataMaintainer::GetStreamMute(AudioStreamType streamType)
290 {
291 std::lock_guard<std::mutex> lock(volumeMutex_);
292 return GetStreamMuteInternal(streamType);
293 }
294
GetStreamMuteInternal(AudioStreamType streamType)295 bool VolumeDataMaintainer::GetStreamMuteInternal(AudioStreamType streamType)
296 {
297 AudioStreamType streamForVolumeMap = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
298 return muteStatusMap_[streamForVolumeMap];
299 }
300
GetMuteAffected(int32_t & affected)301 bool VolumeDataMaintainer::GetMuteAffected(int32_t &affected)
302 {
303 AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
304 const std::string settingKey = "mute_streams_affected";
305 int32_t value = 0;
306 ErrCode ret = settingProvider.GetIntValue(settingKey, value, "system");
307 if (ret != SUCCESS) {
308 AUDIO_WARNING_LOG("Failed to get muteaffected failed Err: %{public}d", ret);
309 return false;
310 } else {
311 affected = value;
312 }
313 return true;
314 }
315
GetMuteTransferStatus(bool & status)316 bool VolumeDataMaintainer::GetMuteTransferStatus(bool &status)
317 {
318 AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
319 const std::string settingKey = "need_mute_affected_transfer";
320 ErrCode ret = settingProvider.GetBoolValue(settingKey, status);
321 if (ret != SUCCESS) {
322 AUDIO_WARNING_LOG("Failed to get muteaffected failed Err: %{public}d", ret);
323 return false;
324 }
325 return true;
326 }
327
SetMuteAffectedToMuteStatusDataBase(int32_t affected)328 bool VolumeDataMaintainer::SetMuteAffectedToMuteStatusDataBase(int32_t affected)
329 {
330 // transfer mute_streams_affected to mutestatus
331 for (auto &streamtype : VOLUME_MUTE_STREAM_TYPE) {
332 if (static_cast<uint32_t>(affected) & (1 << streamtype)) {
333 for (auto &device : DEVICE_TYPE_LIST) {
334 // save mute status to database
335 SaveMuteStatusInternal(device, AUDIO_STREAMTYPE_MAP[streamtype], true);
336 }
337 }
338 }
339 return true;
340 }
341
SaveMuteTransferStatus(bool status)342 bool VolumeDataMaintainer::SaveMuteTransferStatus(bool status)
343 {
344 AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
345 const std::string settingKey = "need_mute_affected_transfer";
346 ErrCode ret = settingProvider.PutIntValue(settingKey, status);
347 if (ret != SUCCESS) {
348 AUDIO_WARNING_LOG("Failed to SaveMuteTransferStatus: %{public}d to setting db! Err: %{public}d", status, ret);
349 return false;
350 }
351 return true;
352 }
353
SaveRingerMode(AudioRingerMode ringerMode)354 bool VolumeDataMaintainer::SaveRingerMode(AudioRingerMode ringerMode)
355 {
356 AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
357 const std::string settingKey = "ringer_mode";
358 ErrCode ret = settingProvider.PutIntValue(settingKey, static_cast<int32_t>(ringerMode));
359 if (ret != SUCCESS) {
360 AUDIO_WARNING_LOG("Failed to write ringer_mode: %{public}d to setting db! Err: %{public}d", ringerMode, ret);
361 return false;
362 }
363 return true;
364 }
365
GetRingerMode(AudioRingerMode & ringerMode)366 bool VolumeDataMaintainer::GetRingerMode(AudioRingerMode &ringerMode)
367 {
368 AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
369 const std::string settingKey = "ringer_mode";
370 int32_t value = 0;
371 ErrCode ret = settingProvider.GetIntValue(settingKey, value);
372 if (ret != SUCCESS) {
373 AUDIO_WARNING_LOG("Failed to write ringer_mode: %{public}d to setting db! Err: %{public}d", ringerMode, ret);
374 return false;
375 } else {
376 ringerMode = static_cast<AudioRingerMode>(value);
377 }
378 return true;
379 }
380
SaveSafeStatus(DeviceType deviceType,SafeStatus safeStatus)381 bool VolumeDataMaintainer::SaveSafeStatus(DeviceType deviceType, SafeStatus safeStatus)
382 {
383 AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
384 ErrCode ret = SUCCESS;
385 switch (deviceType) {
386 case DEVICE_TYPE_BLUETOOTH_A2DP:
387 case DEVICE_TYPE_BLUETOOTH_SCO:
388 ret = settingProvider.PutIntValue(AUDIO_SAFE_VOLUME_STATE_BT, static_cast<int32_t>(safeStatus));
389 break;
390 case DEVICE_TYPE_WIRED_HEADSET:
391 case DEVICE_TYPE_USB_HEADSET:
392 case DEVICE_TYPE_USB_ARM_HEADSET:
393 case DEVICE_TYPE_DP:
394 ret = settingProvider.PutIntValue(AUDIO_SAFE_VOLUME_STATE, static_cast<int32_t>(safeStatus));
395 break;
396 default:
397 AUDIO_WARNING_LOG("the device type not support safe volume");
398 return false;
399 }
400 if (ret != SUCCESS) {
401 AUDIO_ERR_LOG("device:%{public}d, insert failed, safe status:%{public}d", deviceType, safeStatus);
402 return false;
403 }
404 return true;
405 }
406
GetSafeStatus(DeviceType deviceType,SafeStatus & safeStatus)407 bool VolumeDataMaintainer::GetSafeStatus(DeviceType deviceType, SafeStatus &safeStatus)
408 {
409 AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
410 ErrCode ret = SUCCESS;
411 int32_t value = 0;
412 switch (deviceType) {
413 case DEVICE_TYPE_BLUETOOTH_A2DP:
414 case DEVICE_TYPE_BLUETOOTH_SCO:
415 ret = settingProvider.GetIntValue(AUDIO_SAFE_VOLUME_STATE_BT, value);
416 break;
417 case DEVICE_TYPE_WIRED_HEADSET:
418 case DEVICE_TYPE_USB_HEADSET:
419 case DEVICE_TYPE_USB_ARM_HEADSET:
420 case DEVICE_TYPE_DP:
421 ret = settingProvider.GetIntValue(AUDIO_SAFE_VOLUME_STATE, value);
422 break;
423 default:
424 AUDIO_WARNING_LOG("the device type not support safe volume");
425 return false;
426 }
427 if (ret != SUCCESS) {
428 AUDIO_ERR_LOG("device:%{public}d, insert failed, safe status:%{public}d", deviceType, safeStatus);
429 return false;
430 }
431 if (value > static_cast<int32_t>(SAFE_ACTIVE)) {
432 value = value - MAX_SAFE_STATUS;
433 SaveSafeStatus(deviceType, static_cast<SafeStatus>(value));
434 }
435 safeStatus = static_cast<SafeStatus>(value);
436 return true;
437 }
438
SaveSafeVolumeTime(DeviceType deviceType,int64_t time)439 bool VolumeDataMaintainer::SaveSafeVolumeTime(DeviceType deviceType, int64_t time)
440 {
441 AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
442 ErrCode ret = SUCCESS;
443 switch (deviceType) {
444 case DEVICE_TYPE_BLUETOOTH_A2DP:
445 case DEVICE_TYPE_BLUETOOTH_SCO:
446 ret = settingProvider.PutLongValue(UNSAFE_VOLUME_MUSIC_ACTIVE_MS_BT, time, "secure");
447 break;
448 case DEVICE_TYPE_WIRED_HEADSET:
449 case DEVICE_TYPE_USB_HEADSET:
450 case DEVICE_TYPE_USB_ARM_HEADSET:
451 case DEVICE_TYPE_DP:
452 ret = settingProvider.PutLongValue(UNSAFE_VOLUME_MUSIC_ACTIVE_MS, time, "secure");
453 break;
454 default:
455 AUDIO_WARNING_LOG("the device type not support safe volume");
456 return false;
457 }
458 if (ret != SUCCESS) {
459 AUDIO_ERR_LOG("device:%{public}d, insert failed", deviceType);
460 return false;
461 }
462
463 return true;
464 }
465
GetSafeVolumeTime(DeviceType deviceType,int64_t & time)466 bool VolumeDataMaintainer::GetSafeVolumeTime(DeviceType deviceType, int64_t &time)
467 {
468 AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
469 ErrCode ret = SUCCESS;
470 switch (deviceType) {
471 case DEVICE_TYPE_BLUETOOTH_A2DP:
472 case DEVICE_TYPE_BLUETOOTH_SCO:
473 ret = settingProvider.GetLongValue(UNSAFE_VOLUME_MUSIC_ACTIVE_MS_BT, time, "secure");
474 break;
475 case DEVICE_TYPE_WIRED_HEADSET:
476 case DEVICE_TYPE_USB_HEADSET:
477 case DEVICE_TYPE_USB_ARM_HEADSET:
478 case DEVICE_TYPE_DP:
479 ret = settingProvider.GetLongValue(UNSAFE_VOLUME_MUSIC_ACTIVE_MS, time, "secure");
480 break;
481 default:
482 AUDIO_WARNING_LOG("the device type not support safe mode");
483 return false;
484 }
485 if (ret != SUCCESS) {
486 AUDIO_ERR_LOG("device:%{public}d, get safe active time failed", deviceType);
487 return false;
488 }
489 return true;
490 }
491
SaveSystemSoundUrl(const std::string & key,const std::string & value)492 bool VolumeDataMaintainer::SaveSystemSoundUrl(const std::string &key, const std::string &value)
493 {
494 AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
495 ErrCode ret = settingProvider.PutStringValue(key, value);
496 if (ret != SUCCESS) {
497 AUDIO_WARNING_LOG("Failed to system sound url: %{public}s to setting db! Err: %{public}d", value.c_str(), ret);
498 return false;
499 }
500 return true;
501 }
502
GetSystemSoundUrl(const std::string & key,std::string & value)503 bool VolumeDataMaintainer::GetSystemSoundUrl(const std::string &key, std::string &value)
504 {
505 AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
506 ErrCode ret = settingProvider.GetStringValue(key, value);
507 if (ret != SUCCESS) {
508 AUDIO_WARNING_LOG("Failed to get systemsoundurl failed Err: %{public}d", ret);
509 return false;
510 }
511 return true;
512 }
513
RegisterCloned()514 void VolumeDataMaintainer::RegisterCloned()
515 {
516 AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
517 AudioSettingObserver::UpdateFunc updateFunc = [&](const std::string& key) {
518 int32_t value = INVALIAD_SETTINGS_CLONE_STATUS;
519 ErrCode result =
520 AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID).GetIntValue(SETTINGS_CLONED, value);
521 if (!isSettingsCloneHaveStarted_ && (value == SETTINGS_CLONING_STATUS) && (result == SUCCESS)) {
522 AUDIO_INFO_LOG("clone staring.");
523 isSettingsCloneHaveStarted_ = true;
524 }
525
526 if (isSettingsCloneHaveStarted_ && (value == SETTINGS_CLONED_STATUS) && (result == SUCCESS)) {
527 AUDIO_INFO_LOG("Get SETTINGS_CLONED success, clone done, restore.");
528 AudioPolicyManagerFactory::GetAudioPolicyManager().DoRestoreData();
529 isSettingsCloneHaveStarted_ = false;
530 }
531 };
532 sptr<AudioSettingObserver> observer = settingProvider.CreateObserver(SETTINGS_CLONED, updateFunc);
533 ErrCode ret = settingProvider.RegisterObserver(observer);
534 if (ret != ERR_OK) {
535 AUDIO_ERR_LOG("RegisterObserver failed");
536 }
537 }
538
SaveMicMuteState(bool isMute)539 bool VolumeDataMaintainer::SaveMicMuteState(bool isMute)
540 {
541 AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
542 const std::string settingKey = "micmute_state";
543 ErrCode ret = settingProvider.PutBoolValue(settingKey, isMute, "secure");
544 if (ret != SUCCESS) {
545 AUDIO_ERR_LOG("Failed to saveMicMuteState: %{public}d to setting db! Err: %{public}d", isMute, ret);
546 return false;
547 }
548 return true;
549 }
550
GetMicMuteState(bool & isMute)551 bool VolumeDataMaintainer::GetMicMuteState(bool &isMute)
552 {
553 AudioSettingProvider& settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
554 const std::string settingKey = "micmute_state";
555 ErrCode ret = settingProvider.GetBoolValue(settingKey, isMute, "secure");
556 if (ret != SUCCESS) {
557 AUDIO_WARNING_LOG("Failed to write micmute_state: %{public}d to setting db! Err: %{public}d", isMute, ret);
558 return false;
559 }
560
561 return true;
562 }
GetDeviceTypeName(DeviceType deviceType)563 std::string VolumeDataMaintainer::GetDeviceTypeName(DeviceType deviceType)
564 {
565 std::string type = "";
566 switch (deviceType) {
567 case DEVICE_TYPE_EARPIECE:
568 type = "_earpiece";
569 return type;
570 case DEVICE_TYPE_SPEAKER:
571 type = "_builtin";
572 return type;
573 case DEVICE_TYPE_BLUETOOTH_A2DP:
574 case DEVICE_TYPE_BLUETOOTH_SCO:
575 type = "_wireless";
576 return type;
577 case DEVICE_TYPE_WIRED_HEADSET:
578 case DEVICE_TYPE_USB_HEADSET:
579 case DEVICE_TYPE_USB_ARM_HEADSET:
580 case DEVICE_TYPE_DP:
581 type = "_wired";
582 return type;
583 case DEVICE_TYPE_REMOTE_CAST:
584 type = "_remote_cast";
585 return type;
586 default:
587 AUDIO_ERR_LOG("device %{public}d is not supported for dataShare", deviceType);
588 return "";
589 }
590 }
591
GetVolumeKeyForDataShare(DeviceType deviceType,AudioStreamType streamType)592 std::string VolumeDataMaintainer::GetVolumeKeyForDataShare(DeviceType deviceType, AudioStreamType streamType)
593 {
594 std::string type = "";
595 if (!AUDIO_STREAMTYPE_VOLUME_MAP.count(streamType)) {
596 return "";
597 }
598 type = AUDIO_STREAMTYPE_VOLUME_MAP[streamType];
599 if (type == "") {
600 AUDIO_ERR_LOG("streamType %{public}d is not supported for datashare", streamType);
601 return "";
602 }
603
604 std::string deviceTypeName = GetDeviceTypeName(deviceType);
605 if (deviceTypeName == "") {
606 AUDIO_ERR_LOG("device %{public}d is not supported for datashare", deviceType);
607 return "";
608 }
609 return type + deviceTypeName;
610 }
611
GetMuteKeyForDataShare(DeviceType deviceType,AudioStreamType streamType)612 std::string VolumeDataMaintainer::GetMuteKeyForDataShare(DeviceType deviceType, AudioStreamType streamType)
613 {
614 std::string type = "";
615 if (!AUDIO_STREAMTYPE_MUTE_STATUS_MAP.count(streamType)) {
616 return "";
617 }
618 type = AUDIO_STREAMTYPE_MUTE_STATUS_MAP[streamType];
619 if (type == "") {
620 AUDIO_ERR_LOG("streamType %{public}d is not supported for datashare", streamType);
621 return "";
622 }
623
624 std::string deviceTypeName = GetDeviceTypeName(deviceType);
625 if (deviceTypeName == "") {
626 AUDIO_ERR_LOG("device %{public}d is not supported for datashare", deviceType);
627 return "";
628 }
629 return type + deviceTypeName;
630 }
631
632 } // namespace AudioStandard
633 } // namespace OHOS
634