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