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 "sms_persist_helper.h"
17
18 #include "ability_manager_interface.h"
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "system_ability.h"
22 #include "system_ability_definition.h"
23 #include "resource_manager.h"
24 #include "parameter.h"
25
26 #include "telephony_log_wrapper.h"
27 #include "string_utils.h"
28
29 namespace OHOS {
30 namespace Telephony {
31 constexpr static uint8_t SMS_TYPE_CDMA = 2;
32
SmsPersistHelper()33 SmsPersistHelper::SmsPersistHelper() {}
34
~SmsPersistHelper()35 SmsPersistHelper::~SmsPersistHelper() {}
36
CreateDataShareHelper(const std::string & uri)37 std::shared_ptr<DataShare::DataShareHelper> SmsPersistHelper::CreateDataShareHelper(const std::string &uri)
38 {
39 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
40 if (saManager == nullptr) {
41 TELEPHONY_LOGE(" Get system ability mgr failed.");
42 return nullptr;
43 }
44 auto remoteObj = saManager->GetSystemAbility(TELEPHONY_SMS_MMS_SYS_ABILITY_ID);
45 if (remoteObj == nullptr) {
46 TELEPHONY_LOGE("GetSystemAbility Service Failed.");
47 return nullptr;
48 }
49 return DataShare::DataShareHelper::Creator(remoteObj, uri);
50 }
51
Insert(DataShare::DataShareValuesBucket & values,uint16_t & dataBaseId)52 bool SmsPersistHelper::Insert(DataShare::DataShareValuesBucket &values, uint16_t &dataBaseId)
53 {
54 std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(SMS_URI);
55 if (helper == nullptr) {
56 TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
57 return false;
58 }
59 Uri uri(SMS_SUBSECTION);
60 int ret = helper->Insert(uri, values);
61 helper->Release();
62 dataBaseId = ret;
63 return ret >= 0 ? true : false;
64 }
65
Insert(std::string tableUri,DataShare::DataShareValuesBucket & values)66 bool SmsPersistHelper::Insert(std::string tableUri, DataShare::DataShareValuesBucket &values)
67 {
68 std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(SMS_URI);
69 if (helper == nullptr) {
70 TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
71 return false;
72 }
73 Uri uri(tableUri);
74 int ret = helper->Insert(uri, values);
75 helper->Release();
76 return ret >= 0 ? true : false;
77 }
78
QuerySession(DataShare::DataSharePredicates & predicates,uint16_t & sessionId,uint16_t & messageCount)79 bool SmsPersistHelper::QuerySession(DataShare::DataSharePredicates &predicates, uint16_t &sessionId,
80 uint16_t &messageCount)
81 {
82 std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(SMS_URI);
83 if (helper == nullptr) {
84 TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
85 return false;
86 }
87 Uri uri(SMS_SESSION);
88 std::vector<std::string> columns;
89 auto resultSet = helper->Query(uri, predicates, columns);
90 if (resultSet == nullptr) {
91 TELEPHONY_LOGE("Query Result Set nullptr Failed.");
92 helper->Release();
93 return false;
94 }
95 resultSet->GoToFirstRow();
96 int32_t columnInt;
97 int columnIndex;
98 resultSet->GetColumnIndex("id", columnIndex);
99 if (resultSet->GetInt(columnIndex, columnInt) == 0) {
100 sessionId = columnInt;
101 }
102 resultSet->GetColumnIndex("message_count", columnIndex);
103 if (resultSet->GetInt(columnIndex, columnInt) == 0) {
104 messageCount = columnInt;
105 resultSet->Close();
106 helper->Release();
107 return true;
108 }
109 resultSet->Close();
110 helper->Release();
111 return false;
112 }
113
Update(DataShare::DataSharePredicates & predicates,DataShare::DataShareValuesBucket & values)114 bool SmsPersistHelper::Update(DataShare::DataSharePredicates &predicates, DataShare::DataShareValuesBucket &values)
115 {
116 std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(SMS_URI);
117 if (helper == nullptr) {
118 TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
119 return false;
120 }
121 Uri uri(SMS_SESSION);
122 int ret = helper->Update(uri, predicates, values);
123 helper->Release();
124 return ret >= 0 ? true : false;
125 }
126
Query(DataShare::DataSharePredicates & predicates,std::vector<SmsReceiveIndexer> & indexers)127 bool SmsPersistHelper::Query(DataShare::DataSharePredicates &predicates, std::vector<SmsReceiveIndexer> &indexers)
128 {
129 std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(SMS_URI);
130 if (helper == nullptr) {
131 TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
132 return false;
133 }
134 Uri uri(SMS_SUBSECTION);
135 std::vector<std::string> columns;
136 auto resultSet = helper->Query(uri, predicates, columns);
137 if (resultSet == nullptr) {
138 TELEPHONY_LOGE("Query Result Set nullptr Failed.");
139 helper->Release();
140 return false;
141 }
142
143 int resultSetNum = resultSet->GoToFirstRow();
144 while (resultSetNum == 0) {
145 SmsReceiveIndexer indexer;
146 ResultSetConvertToIndexer(indexer, resultSet);
147 indexers.push_back(indexer);
148 resultSetNum = resultSet->GoToNextRow();
149 }
150 resultSet->Close();
151 helper->Release();
152 helper = nullptr;
153 return true;
154 }
155
QueryMaxGroupId(DataShare::DataSharePredicates & predicates,uint16_t & maxGroupId)156 bool SmsPersistHelper::QueryMaxGroupId(DataShare::DataSharePredicates &predicates, uint16_t &maxGroupId)
157 {
158 std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(SMS_URI);
159 if (helper == nullptr) {
160 TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
161 return false;
162 }
163 Uri uri(SMS_MMS_INFO);
164 std::vector<std::string> columns;
165 auto resultSet = helper->Query(uri, predicates, columns);
166 if (resultSet == nullptr) {
167 TELEPHONY_LOGE("Query Result Set nullptr Failed.");
168 helper->Release();
169 return false;
170 }
171
172 int32_t columnInt;
173 int columnIndex;
174 resultSet->GoToLastRow();
175 resultSet->GetColumnIndex(SmsMmsInfo::GROUP_ID, columnIndex);
176 if (resultSet->GetInt(columnIndex, columnInt) == 0) {
177 maxGroupId = columnInt;
178 }
179 resultSet->Close();
180 helper->Release();
181 helper = nullptr;
182 return true;
183 }
184
Delete(DataShare::DataSharePredicates & predicates)185 bool SmsPersistHelper::Delete(DataShare::DataSharePredicates &predicates)
186 {
187 std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(SMS_URI);
188 if (helper == nullptr) {
189 TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
190 return false;
191 }
192 Uri uri(SMS_SUBSECTION);
193 int ret = helper->Delete(uri, predicates);
194 helper->Release();
195 return ret >= 0 ? true : false;
196 }
197
QueryBlockPhoneNumber(const std::string & phoneNum)198 bool SmsPersistHelper::QueryBlockPhoneNumber(const std::string &phoneNum)
199 {
200 bool result = false;
201 int count = 0;
202 const std::string phoneNumber = "phone_number";
203 if (phoneNum.empty()) {
204 return result;
205 }
206 std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(CONTACT_URI);
207 if (helper == nullptr) {
208 TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
209 return false;
210 }
211 Uri uri(CONTACT_BLOCK);
212 std::vector<std::string> columns;
213 DataShare::DataSharePredicates predicates;
214 predicates.EqualTo(phoneNumber, phoneNum);
215 auto resultSet = helper->Query(uri, predicates, columns);
216 if (resultSet == nullptr) {
217 TELEPHONY_LOGE("Query Result Set nullptr Failed.");
218 helper->Release();
219 return result;
220 }
221 if (resultSet->GetRowCount(count) == 0 && count != 0) {
222 result = true;
223 }
224 resultSet->Close();
225 helper->Release();
226 helper = nullptr;
227 return result;
228 }
229
QueryParamBoolean(const std::string key,bool defValue)230 bool SmsPersistHelper::QueryParamBoolean(const std::string key, bool defValue)
231 {
232 const int PARAM_SIZE = 64;
233 char paramOutBuff[PARAM_SIZE] = {0};
234 std::string defStrValue = defValue ? "1" : "0";
235 std::string paramValue(defStrValue);
236
237 int retLen = GetParameter(key.c_str(), defStrValue.c_str(), paramOutBuff, PARAM_SIZE);
238 if (retLen > 0) {
239 paramOutBuff[retLen] = '\0';
240 paramValue.assign(paramOutBuff, retLen);
241 }
242
243 TELEPHONY_LOGI("QueryParamBoolean: %{public}zu : %{public}s", paramValue.size(), paramValue.c_str());
244 if ((paramValue == std::string("1")) || (paramValue == std::string("y")) || (paramValue == std::string("yes")) ||
245 (paramValue == std::string("on")) || (paramValue == std::string("true"))) {
246 return true;
247 } else if ((paramValue == std::string("0")) || (paramValue == std::string("n")) ||
248 (paramValue == std::string("no")) || (paramValue == std::string("off")) ||
249 (paramValue == std::string("false"))) {
250 return false;
251 }
252 return defValue;
253 }
254
ConvertIntToIndexer(SmsReceiveIndexer & info,const std::shared_ptr<DataShare::DataShareResultSet> & resultSet)255 void SmsPersistHelper::ConvertIntToIndexer(
256 SmsReceiveIndexer &info, const std::shared_ptr<DataShare::DataShareResultSet> &resultSet)
257 {
258 int32_t columnInt;
259 int columnIndex;
260 resultSet->GetColumnIndex(SmsSubsection::FORMAT, columnIndex);
261 if (resultSet->GetInt(columnIndex, columnInt) == 0) {
262 info.SetIsCdma(columnInt == SMS_TYPE_CDMA);
263 }
264
265 resultSet->GetColumnIndex(SmsSubsection::SMS_SUBSECTION_ID, columnIndex);
266 if (resultSet->GetInt(columnIndex, columnInt) == 0) {
267 info.SetMsgRefId(columnInt);
268 }
269
270 resultSet->GetColumnIndex(SmsSubsection::SIZE, columnIndex);
271 if (resultSet->GetInt(columnIndex, columnInt) == 0) {
272 info.SetMsgCount(columnInt);
273 }
274
275 resultSet->GetColumnIndex(SmsSubsection::SUBSECTION_INDEX, columnIndex);
276 if (resultSet->GetInt(columnIndex, columnInt) == 0) {
277 info.SetMsgSeqId(columnInt);
278 }
279
280 resultSet->GetColumnIndex(SmsSubsection::DEST_PORT, columnIndex);
281 if (resultSet->GetInt(columnIndex, columnInt) == 0) {
282 info.SetDestPort(columnInt);
283 }
284
285 resultSet->GetColumnIndex(SmsSubsection::ID, columnIndex);
286 if (resultSet->GetInt(columnIndex, columnInt) == 0) {
287 info.SetDataBaseId(columnInt);
288 }
289 }
290
ConvertStringToIndexer(SmsReceiveIndexer & info,const std::shared_ptr<DataShare::DataShareResultSet> & resultSet)291 void SmsPersistHelper::ConvertStringToIndexer(
292 SmsReceiveIndexer &info, const std::shared_ptr<DataShare::DataShareResultSet> &resultSet)
293 {
294 int columnIndex;
295 std::string columnValue;
296 resultSet->GetColumnIndex(SmsSubsection::RECEIVER_NUMBER, columnIndex);
297 if (resultSet->GetString(columnIndex, columnValue) == 0) {
298 info.SetVisibleAddress(columnValue);
299 }
300 resultSet->GetColumnIndex(SmsSubsection::SENDER_NUMBER, columnIndex);
301 if (resultSet->GetString(columnIndex, columnValue) == 0) {
302 info.SetOriginatingAddress(columnValue);
303 }
304
305 resultSet->GetColumnIndex(SmsSubsection::START_TIME, columnIndex);
306 if (resultSet->GetString(columnIndex, columnValue) == 0) {
307 info.SetTimestamp(std::stol(columnValue));
308 }
309 resultSet->GetColumnIndex(SmsSubsection::END_TIME, columnIndex);
310 if (resultSet->GetString(columnIndex, columnValue) == 0) {
311 info.SetTimestamp(std::stol(columnValue));
312 }
313
314 resultSet->GetColumnIndex(SmsSubsection::REW_PUD, columnIndex);
315 if (resultSet->GetString(columnIndex, columnValue) == 0) {
316 info.SetPdu(StringUtils::HexToByteVector(columnValue));
317 }
318 }
319
ResultSetConvertToIndexer(SmsReceiveIndexer & info,const std::shared_ptr<DataShare::DataShareResultSet> & resultSet)320 void SmsPersistHelper::ResultSetConvertToIndexer(
321 SmsReceiveIndexer &info, const std::shared_ptr<DataShare::DataShareResultSet> &resultSet)
322 {
323 ConvertIntToIndexer(info, resultSet);
324 ConvertStringToIndexer(info, resultSet);
325 }
326 } // namespace Telephony
327 } // namespace OHOS
328