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