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