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