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