1 /* 2 * Copyright (c) 2023 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 16 #ifndef SETTING_DATASHARE_H 17 #define SETTING_DATASHARE_H 18 19 #include "datashare_helper.h" 20 #include "setting_observer.h" 21 22 namespace OHOS { 23 namespace MMI { 24 class SettingDataShare : public NoCopyable { 25 public: 26 ~SettingDataShare() override; 27 static SettingDataShare& GetInstance(int32_t systemAbilityId); 28 ErrCode GetStringValue(const std::string& key, std::string& value, const std::string &strUri = std::string()); 29 ErrCode GetIntValue(const std::string& key, int32_t& value, const std::string &strUri = std::string()); 30 ErrCode GetLongValue(const std::string& key, int64_t& value, const std::string &strUri = std::string()); 31 ErrCode GetBoolValue(const std::string& key, bool& value, const std::string &strUri = std::string()); 32 ErrCode PutStringValue(const std::string& key, const std::string& value, 33 bool needNotify = true, const std::string &strUri = std::string()); 34 ErrCode PutIntValue(const std::string& key, int32_t value, 35 bool needNotify = true, const std::string &strUri = std::string()); 36 ErrCode PutLongValue(const std::string& key, int64_t value, 37 bool needNotify = true, const std::string &strUri = std::string()); 38 ErrCode PutBoolValue(const std::string& key, bool value, 39 bool needNotify = true, const std::string &strUri = std::string()); 40 bool IsValidKey(const std::string& key, const std::string &strUri = std::string()); 41 sptr<SettingObserver> CreateObserver(const std::string& key, SettingObserver::UpdateFunc& func); 42 static void ExecRegisterCb(const sptr<SettingObserver>& observer); 43 ErrCode RegisterObserver(const sptr<SettingObserver>& observer, const std::string &strUri = std::string()); 44 ErrCode UnregisterObserver(const sptr<SettingObserver>& observer, const std::string &strUri = std::string()); 45 bool CheckIfSettingsDataReady(); 46 47 private: 48 static std::shared_ptr<SettingDataShare> instance_; 49 static std::mutex mutex_; 50 static sptr<IRemoteObject> remoteObj_; 51 bool isDataShareReady_ = false; 52 53 static std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(const std::string &strUri); 54 static bool ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper>& helper); 55 static Uri AssembleUri(const std::string& key, const std::string &strUri); 56 }; 57 } 58 } // namespace OHOS 59 #endif // SETTING_DATASHARE_H