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
25 namespace OHOS {
26 namespace Telephony {
PreferencesUtil()27 PreferencesUtil::PreferencesUtil() {}
~PreferencesUtil()28 PreferencesUtil::~PreferencesUtil() {}
29
GetProfiles(const std::string &path, int &errCode)30 std::shared_ptr<NativePreferences::Preferences> PreferencesUtil::GetProfiles(const std::string &path, int &errCode)
31 {
32 return NativePreferences::PreferencesHelper::GetPreferences(path, errCode);
33 }
34
DeleteProfiles()35 int PreferencesUtil::DeleteProfiles()
36 {
37 return NativePreferences::PreferencesHelper::DeletePreferences(path_);
38 }
39
SaveString(const std::string &key, const std::string &value)40 int 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
ObtainString(const std::string &key, const std::string &defValue)51 std::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
SaveInt(const std::string &key, int value)60 int 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
ObtainInt(const std::string &key, int defValue)71 int 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
SaveBool(const std::string &key, bool value)80 int 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
ObtainBool(const std::string &key, bool defValue)91 bool 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
SaveLong(const std::string &key, int64_t value)100 int 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
ObtainLong(const std::string &key, int64_t defValue)111 int64_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
SaveFloat(const std::string &key, float value)120 int 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
ObtainFloat(const std::string &key, float defValue)131 float 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
IsExistKey(const std::string &key)140 bool 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
RemoveKey(const std::string &key)149 int 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
RemoveAll()158 int 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
Refresh()167 void PreferencesUtil::Refresh()
168 {
169 std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_);
170 if (ptr != nullptr) {
171 ptr->Flush();
172 }
173 }
174
RefreshSync()175 int 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