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