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 AudioSettingProvider& GetInstance(int32_t systemAbilityId); 55 static int32_t GetCurrentUserId(); 56 static bool CheckOsAccountReady(); 57 ErrCode GetStringValue(const std::string &key, std::string &value, std::string tableType = ""); 58 ErrCode GetIntValue(const std::string &key, int32_t &value, std::string tableType = ""); 59 ErrCode GetLongValue(const std::string &key, int64_t &value, std::string tableType = ""); 60 ErrCode GetFloatValue(const std::string &key, float &value, std::string tableType = ""); 61 ErrCode GetBoolValue(const std::string &key, bool &value, std::string tableType = ""); 62 ErrCode PutStringValue(const std::string &key, const std::string &value, 63 std::string tableType = "", bool needNotify = true); 64 ErrCode PutIntValue(const std::string &key, int32_t value, std::string tableType = "", bool needNotify = true); 65 ErrCode PutLongValue(const std::string &key, int64_t value, std::string tableType = "", bool needNotify = true); 66 ErrCode PutBoolValue(const std::string &key, bool value, std::string tableType = "", bool needNotify = true); 67 bool IsValidKey(const std::string &key); 68 void SetDataShareReady(std::atomic<bool> isDataShareReady); 69 sptr<AudioSettingObserver> CreateObserver(const std::string &key, AudioSettingObserver::UpdateFunc &func); 70 static void ExecRegisterCb(const sptr<AudioSettingObserver> &observer); 71 ErrCode RegisterObserver(const sptr<AudioSettingObserver> &observer, std::string tableType = ""); 72 ErrCode UnregisterObserver(const sptr<AudioSettingObserver> &observer, std::string tableType = ""); 73 74 protected: 75 ~AudioSettingProvider() override; 76 77 private: 78 static std::atomic<bool> isDataShareReady_; 79 static void Initialize(int32_t systemAbilityId); 80 static std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(std::string tableType = ""); 81 static bool ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> &helper); 82 static Uri AssembleUri(const std::string &key, std::string tableType = ""); 83 84 static AudioSettingProvider *instance_; 85 static std::mutex mutex_; 86 static sptr<IRemoteObject> remoteObj_; 87 static std::string SettingSystemUrlProxy_; 88 }; 89 } // namespace AudioStandard 90 } // namespace OHOS 91 #endif // AUDIO_SETTING_PROVIDER_H 92