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 16#include "preferences_util.h" 17 18#include "preferences.h" 19#include "preferences_helper.h" 20#include "preferences_observer.h" 21#include "string" 22 23namespace OHOS { 24namespace ScreenLock { 25PreferencesUtil::PreferencesUtil() {} 26PreferencesUtil::~PreferencesUtil() {} 27 28std::shared_ptr<NativePreferences::Preferences> PreferencesUtil::GetProfiles(const std::string &path, int &errCode) 29{ 30 return NativePreferences::PreferencesHelper::GetPreferences(path, errCode); 31} 32 33int PreferencesUtil::DeleteProfiles() 34{ 35 return NativePreferences::PreferencesHelper::DeletePreferences(path_); 36} 37 38int PreferencesUtil::SaveString(const std::string &key, const std::string &value) 39{ 40 std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_); 41 if (ptr == nullptr) { 42 return NativePreferences::E_ERROR; 43 } 44 int ret = ptr->PutString(key, value); 45 ptr->Flush(); 46 return ret; 47} 48 49std::string PreferencesUtil::ObtainString(const std::string &key, const std::string &defValue) 50{ 51 std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_); 52 if (ptr == nullptr) { 53 return defValue; 54 } 55 return ptr->GetString(key, defValue); 56} 57 58int PreferencesUtil::SaveInt(const std::string &key, int value) 59{ 60 std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_); 61 if (ptr == nullptr) { 62 return NativePreferences::E_ERROR; 63 } 64 int ret = ptr->PutInt(key, value); 65 ptr->Flush(); 66 return ret; 67} 68 69int PreferencesUtil::ObtainInt(const std::string &key, int defValue) 70{ 71 std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_); 72 if (ptr == nullptr) { 73 return defValue; 74 } 75 return ptr->GetInt(key, defValue); 76} 77 78int PreferencesUtil::SaveBool(const std::string &key, bool value) 79{ 80 std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_); 81 if (ptr == nullptr) { 82 return NativePreferences::E_ERROR; 83 } 84 int ret = ptr->PutBool(key, value); 85 ptr->Flush(); 86 return ret; 87} 88 89bool PreferencesUtil::ObtainBool(const std::string &key, bool defValue) 90{ 91 std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_); 92 if (ptr == nullptr) { 93 return defValue; 94 } 95 return ptr->GetBool(key, defValue); 96} 97 98int PreferencesUtil::SaveLong(const std::string &key, int64_t value) 99{ 100 std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_); 101 if (ptr == nullptr) { 102 return NativePreferences::E_ERROR; 103 } 104 int ret = ptr->PutLong(key, value); 105 ptr->Flush(); 106 return ret; 107} 108 109int64_t PreferencesUtil::ObtainLong(const std::string &key, int64_t defValue) 110{ 111 std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_); 112 if (ptr == nullptr) { 113 return defValue; 114 } 115 return ptr->GetLong(key, defValue); 116} 117 118int PreferencesUtil::SaveFloat(const std::string &key, float value) 119{ 120 std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_); 121 if (ptr == nullptr) { 122 return NativePreferences::E_ERROR; 123 } 124 int ret = ptr->PutFloat(key, value); 125 ptr->Flush(); 126 return ret; 127} 128 129float PreferencesUtil::ObtainFloat(const std::string &key, float defValue) 130{ 131 std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_); 132 if (ptr == nullptr) { 133 return defValue; 134 } 135 return ptr->GetFloat(key, defValue); 136} 137 138bool PreferencesUtil::IsExistKey(const std::string &key) 139{ 140 std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_); 141 if (ptr == nullptr) { 142 return NativePreferences::E_ERROR; 143 } 144 return ptr->HasKey(key); 145} 146 147int PreferencesUtil::RemoveKey(const std::string &key) 148{ 149 std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_); 150 if (ptr == nullptr) { 151 return NativePreferences::E_ERROR; 152 } 153 return ptr->Delete(key); 154} 155 156int PreferencesUtil::RemoveAll() 157{ 158 std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_); 159 if (ptr == nullptr) { 160 return NativePreferences::E_ERROR; 161 } 162 return ptr->Clear(); 163} 164 165void PreferencesUtil::Refresh() 166{ 167 std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_); 168 if (ptr != nullptr) { 169 ptr->Flush(); 170 } 171} 172 173int PreferencesUtil::RefreshSync() 174{ 175 std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_); 176 if (ptr == nullptr) { 177 return NativePreferences::E_ERROR; 178 } 179 return ptr->FlushSync(); 180} 181} // namespace Telephony 182} // namespace OHOS