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 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 31 namespace OHOS { 32 namespace AudioStandard { 33 constexpr int32_t MAX_STRING_LENGTH = 10; 34 constexpr int32_t MIN_USER_ACCOUNT = 100; 35 36 class AudioSettingObserver : public AAFwk::DataAbilityObserverStub { 37 public: 38 AudioSettingObserver() = default; 39 ~AudioSettingObserver() = default; 40 void OnChange() override; 41 void SetKey(const std::string& key); 42 const std::string& GetKey(); 43 44 using UpdateFunc = std::function<void(const std::string&)>; 45 void SetUpdateFunc(UpdateFunc& func); 46 47 private: 48 std::string key_ {}; 49 UpdateFunc update_ = nullptr; 50 }; 51 52 class AudioSettingProvider : public NoCopyable { 53 public: 54 static constexpr int32_t INVALID_ACCOUNT_ID = -1; 55 static constexpr int32_t MAIN_USER_ID = 100; 56 57 static AudioSettingProvider& GetInstance(int32_t systemAbilityId); 58 static int32_t GetCurrentUserId(int32_t specificUserId = INVALID_ACCOUNT_ID); 59 static bool CheckOsAccountReady(); 60 ErrCode GetStringValue(const std::string &key, std::string &value, std::string tableType = "", 61 int32_t userId = INVALID_ACCOUNT_ID); 62 ErrCode GetIntValue(const std::string &key, int32_t &value, std::string tableType = ""); 63 ErrCode GetLongValue(const std::string &key, int64_t &value, std::string tableType = ""); 64 ErrCode GetFloatValue(const std::string &key, float &value, std::string tableType = ""); 65 ErrCode GetBoolValue(const std::string &key, bool &value, std::string tableType = "", 66 int32_t userId = INVALID_ACCOUNT_ID); 67 ErrCode GetMapValue(const std::string &key, std::vector<std::map<std::string, std::string>> &value, 68 std::string tableType = ""); 69 ErrCode PutStringValue(const std::string &key, const std::string &value, 70 std::string tableType = "", bool needNotify = true, int32_t userId = INVALID_ACCOUNT_ID); 71 ErrCode PutIntValue(const std::string &key, int32_t value, std::string tableType = "", bool needNotify = true); 72 ErrCode PutLongValue(const std::string &key, int64_t value, std::string tableType = "", bool needNotify = true); 73 ErrCode PutBoolValue(const std::string &key, bool value, std::string tableType = "", bool needNotify = true, 74 int32_t userId = INVALID_ACCOUNT_ID); 75 bool IsValidKey(const std::string &key); 76 void SetDataShareReady(std::atomic<bool> isDataShareReady); 77 sptr<AudioSettingObserver> CreateObserver(const std::string &key, AudioSettingObserver::UpdateFunc &func); 78 static void ExecRegisterCb(const sptr<AudioSettingObserver> &observer); 79 ErrCode RegisterObserver(const sptr<AudioSettingObserver> &observer, std::string tableType = ""); 80 ErrCode UnregisterObserver(const sptr<AudioSettingObserver> &observer, std::string tableType = ""); 81 std::vector<std::map<std::string, std::string>> ParseJsonArray(const std::string& input); 82 std::string ParseFirstOfKey(size_t &pos, size_t len, std::string input); 83 std::string ParseSecondOfValue(size_t &pos, size_t len, std::string input); 84 85 protected: 86 ~AudioSettingProvider() override; 87 88 private: 89 static std::atomic<bool> isDataShareReady_; 90 static void Initialize(int32_t systemAbilityId); 91 static std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(std::string tableType = "", 92 int32_t userId = INVALID_ACCOUNT_ID); 93 static bool ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> &helper); 94 static Uri AssembleUri(const std::string &key, std::string tableType = "", int32_t userId = INVALID_ACCOUNT_ID); 95 96 static std::mutex mutex_; 97 static sptr<IRemoteObject> remoteObj_; 98 static std::string SettingSystemUrlProxy_; 99 }; 100 } // namespace AudioStandard 101 } // namespace OHOS 102 #endif // AUDIO_SETTING_PROVIDER_H 103