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