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 AUDIO_SETTING_PROVIDER_H
16 #define AUDIO_SETTING_PROVIDER_H
17 
18 #include <list>
19 #include <unordered_map>
20 #include <cinttypes>
21 
22 #include "os_account_manager.h"
23 #include "ipc_skeleton.h"
24 #include "datashare_helper.h"
25 #include "errors.h"
26 #include "mutex"
27 #include "data_ability_observer_stub.h"
28 
29 #include "audio_policy_log.h"
30 #include "audio_info.h"
31 
32 namespace OHOS {
33 namespace AudioStandard {
34 constexpr int32_t MAX_STRING_LENGTH = 10;
35 constexpr int32_t MIN_USER_ACCOUNT = 100;
36 
37 class AudioSettingObserver : public AAFwk::DataAbilityObserverStub {
38 public:
39     AudioSettingObserver() = default;
40     ~AudioSettingObserver() = default;
41     void OnChange() override;
42     void SetKey(const std::string& key);
43     const std::string& GetKey();
44 
45     using UpdateFunc = std::function<void(const std::string&)>;
46     void SetUpdateFunc(UpdateFunc& func);
47 
48 private:
49     std::string key_ {};
50     UpdateFunc update_ = nullptr;
51 };
52 
53 class AudioSettingProvider : public NoCopyable {
54 public:
55     static AudioSettingProvider& GetInstance(int32_t systemAbilityId);
56     ErrCode GetStringValue(const std::string &key, std::string &value, std::string tableType = "");
57     ErrCode GetIntValue(const std::string &key, int32_t &value, std::string tableType = "");
58     ErrCode GetLongValue(const std::string &key, int64_t &value, std::string tableType = "");
59     ErrCode GetBoolValue(const std::string &key, bool &value, std::string tableType = "");
60     ErrCode PutStringValue(const std::string &key, const std::string &value,
61         std::string tableType = "", bool needNotify = true);
62     ErrCode PutIntValue(const std::string &key, int32_t value, std::string tableType = "", bool needNotify = true);
63     ErrCode PutLongValue(const std::string &key, int64_t value, std::string tableType = "", bool needNotify = true);
64     ErrCode PutBoolValue(const std::string &key, bool value, std::string tableType = "", bool needNotify = true);
65     bool IsValidKey(const std::string &key);
66     void SetDataShareReady(std::atomic<bool> isDataShareReady);
67     sptr<AudioSettingObserver> CreateObserver(const std::string &key, AudioSettingObserver::UpdateFunc &func);
68     static void ExecRegisterCb(const sptr<AudioSettingObserver> &observer);
69     ErrCode RegisterObserver(const sptr<AudioSettingObserver> &observer);
70     ErrCode UnregisterObserver(const sptr<AudioSettingObserver> &observer);
71 
72 protected:
73     ~AudioSettingProvider() override;
74 
75 private:
76     static bool isDataShareReady_;
77     static void Initialize(int32_t systemAbilityId);
78     static std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(std::string tableType = "");
79     static bool ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> &helper);
80     static Uri AssembleUri(const std::string &key, std::string tableType = "");
81     static int32_t GetCurrentUserId();
82 
83     static AudioSettingProvider *instance_;
84     static std::mutex mutex_;
85     static sptr<IRemoteObject> remoteObj_;
86     static std::string SettingSystemUrlProxy_;
87 };
88 } // namespace AudioStandard
89 } // namespace OHOS
90 #endif // AUDIO_SETTING_PROVIDER_H
91