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