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 "vcard_manager.h"
17
18 #include "telephony_errors.h"
19 #include "telephony_log_wrapper.h"
20 #include "vcard_constant.h"
21 #include "vcard_encoder.h"
22 #include "vcard_file_utils.h"
23 #include "vcard_rdb_helper.h"
24 #include "vcard_utils.h"
25
26 namespace OHOS {
27 namespace Telephony {
VCardManager()28 VCardManager::VCardManager()
29 {
30 listener_ = std::make_shared<VCardManager::DecodeListener>();
31 }
32
GetContacts()33 std::vector<std::shared_ptr<VCardContact>> &VCardManager::DecodeListener::GetContacts()
34 {
35 return contacts_;
36 }
37
OnStarted()38 void VCardManager::DecodeListener::OnStarted()
39 {
40 contacts_.clear();
41 }
42
OnEnded()43 void VCardManager::DecodeListener::OnEnded()
44 {
45 TELEPHONY_LOGI("OnEnded contact size %{public}d", static_cast<int32_t>(contacts_.size()));
46 VCardDecoder::Close();
47 }
48
OnOneContactStarted()49 void VCardManager::DecodeListener::OnOneContactStarted()
50 {
51 TELEPHONY_LOGI("OnOneContactStarted index %{public}d", static_cast<int32_t>(contacts_.size()));
52 currentContact_ = std::make_shared<VCardContact>();
53 }
54
OnOneContactEnded()55 void VCardManager::DecodeListener::OnOneContactEnded()
56 {
57 TELEPHONY_LOGI("OnOneContactEnded index %{public}d", static_cast<int32_t>(contacts_.size()));
58 contacts_.push_back(currentContact_);
59 currentContact_ = nullptr;
60 }
61
OnRawDataCreated(std::shared_ptr<VCardRawData> rawData)62 void VCardManager::DecodeListener::OnRawDataCreated(std::shared_ptr<VCardRawData> rawData)
63 {
64 if (rawData == nullptr || currentContact_ == nullptr) {
65 return;
66 }
67 int32_t errorCode = TELEPHONY_SUCCESS;
68 currentContact_->AddRawData(rawData, errorCode);
69 }
70
ImportLock(const std::string & path,std::shared_ptr<DataShare::DataShareHelper> dataShareHelper,int32_t accountId)71 int32_t VCardManager::ImportLock(
72 const std::string &path, std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, int32_t accountId)
73 {
74 std::lock_guard<std::mutex> lock(mutex_);
75 if (dataShareHelper == nullptr) {
76 TELEPHONY_LOGE("DataShareHelper is nullptr");
77 return TELEPHONY_ERR_LOCAL_PTR_NULL;
78 }
79 SetDataHelper(dataShareHelper);
80 int32_t errorCode = Import(path, accountId);
81 Release();
82 TELEPHONY_LOGI("ImportLock errorCode : %{public}d finish", errorCode);
83 return errorCode;
84 }
85
Import(const std::string & path,int32_t accountId)86 int32_t VCardManager::Import(const std::string &path, int32_t accountId)
87 {
88 int32_t errorCode = TELEPHONY_SUCCESS;
89 Decode(path, errorCode);
90 if (errorCode != TELEPHONY_SUCCESS) {
91 TELEPHONY_LOGE("Failed to decode");
92 return errorCode;
93 }
94 BatchInsertContactDbAbility(accountId, errorCode);
95 if (errorCode != TELEPHONY_SUCCESS) {
96 TELEPHONY_LOGE("Failed to insert ability");
97 return errorCode;
98 }
99 TELEPHONY_LOGI("Import size %{public}d success", static_cast<int32_t>(listener_->GetContacts().size()));
100 return errorCode;
101 }
102
Decode(const std::string & path,int32_t & errorCode)103 void VCardManager::Decode(const std::string &path, int32_t &errorCode)
104 {
105 std::shared_ptr<VCardDecoder> decoder = VCardDecoder::Create(path, errorCode);
106 if (decoder == nullptr || errorCode != TELEPHONY_SUCCESS) {
107 TELEPHONY_LOGE("Failed to get decoder");
108 return;
109 }
110 decoder->AddVCardDecodeListener(listener_);
111 decoder->Decode(errorCode);
112 if (errorCode != TELEPHONY_SUCCESS) {
113 TELEPHONY_LOGE("Failed to decode");
114 }
115 }
116
InsertContactDbAbility(int32_t accountId,int32_t & errorCode)117 void VCardManager::InsertContactDbAbility(int32_t accountId, int32_t &errorCode)
118 {
119 if (listener_ == nullptr) {
120 errorCode = TELEPHONY_ERR_LOCAL_PTR_NULL;
121 return;
122 }
123 if (listener_->GetContacts().size() == 0) {
124 errorCode = TELEPHONY_ERR_VCARD_FILE_INVALID;
125 return;
126 }
127 for (std::shared_ptr<VCardContact> contact : listener_->GetContacts()) {
128 auto rawId = InsertRawContact(accountId);
129 if (rawId <= 0) {
130 TELEPHONY_LOGE("Failed to insert raw contact");
131 errorCode = TELEPHONY_ERR_LOCAL_PTR_NULL;
132 continue;
133 }
134 if (InsertContactData(rawId, contact) == TELEPHONY_ERROR) {
135 TELEPHONY_LOGE("Insert contactData failed");
136 errorCode = TELEPHONY_ERR_LOCAL_PTR_NULL;
137 }
138 }
139 }
140
BatchInsertContactDbAbility(int32_t accountId,int32_t & errorCode)141 void VCardManager::BatchInsertContactDbAbility(int32_t accountId, int32_t &errorCode)
142 {
143 if (listener_ == nullptr) {
144 errorCode = TELEPHONY_ERR_LOCAL_PTR_NULL;
145 return;
146 }
147 if (listener_->GetContacts().size() < BATCH_INSERT_MAX_SIZE) {
148 TELEPHONY_LOGI("contactData < BATCH_INSERT_MAX_SIZE");
149 InsertContactDbAbility(accountId, errorCode);
150 return;
151 }
152 if (listener_->GetContacts().size() == 0) {
153 errorCode = TELEPHONY_ERR_VCARD_FILE_INVALID;
154 return;
155 }
156 std::vector<std::vector<std::shared_ptr<VCardContact>>> splitList =
157 SplitContactsVector(listener_->GetContacts(), BATCH_INSERT_MAX_SIZE);
158 TELEPHONY_LOGI(
159 "contactData > BATCH_INSERT_MAX_SIZE, split List size %{public}d", static_cast<int32_t>(splitList.size()));
160 for (std::vector<std::shared_ptr<VCardContact>> list : splitList) {
161 TELEPHONY_LOGI("List size %{public}d", static_cast<int32_t>(list.size()));
162 std::vector<int32_t> rawIds;
163 BatchInsertRawContact(accountId, list.size(), rawIds, errorCode);
164 if (errorCode == TELEPHONY_ERROR) {
165 TELEPHONY_LOGE("Failed to batch insert raw contact");
166 continue;
167 }
168 BatchInsertContactData(rawIds, list, errorCode);
169 if (errorCode == TELEPHONY_ERROR) {
170 TELEPHONY_LOGE("Failed to batch insert contactData");
171 continue;
172 }
173 }
174 }
175
BatchInsertRawContact(int32_t accountId,uint32_t size,std::vector<int32_t> & rawIds,int32_t & errorCode)176 void VCardManager::BatchInsertRawContact(
177 int32_t accountId, uint32_t size, std::vector<int32_t> &rawIds, int32_t &errorCode)
178 {
179 int32_t rawContactMaxId = VCardRdbHelper::GetInstance().QueryRawContactMaxId(static_cast<int32_t>(size));
180 std::vector<DataShare::DataShareValuesBucket> rawContactValues;
181 for (uint32_t i = 0; i < size; i++) {
182 OHOS::DataShare::DataShareValuesBucket valuesBucket;
183 valuesBucket.Put(RawContact::ACCOUNT_ID, GetAccountId());
184 if (IsContactsIdExit(accountId)) {
185 valuesBucket.Put(RawContact::CONTACT_ID, accountId);
186 }
187 int32_t newRawId = rawContactMaxId + i + 1;
188 valuesBucket.Put(RawContact::ID, newRawId);
189 rawContactValues.push_back(valuesBucket);
190 rawIds.push_back(newRawId);
191 }
192 VCardRdbHelper::GetInstance().BatchInsertRawContact(rawContactValues);
193 }
194
BatchInsertContactData(std::vector<int32_t> & rawIds,const std::vector<std::shared_ptr<VCardContact>> & contactList,int32_t & errorCode)195 void VCardManager::BatchInsertContactData(
196 std::vector<int32_t> &rawIds, const std::vector<std::shared_ptr<VCardContact>> &contactList, int32_t &errorCode)
197 {
198 std::vector<DataShare::DataShareValuesBucket> contactDataValues;
199 for (size_t i = 0; i < rawIds.size(); i++) {
200 int32_t rawId = rawIds[i];
201 std::shared_ptr<VCardContact> contact = contactList[i];
202 if (contact == nullptr) {
203 errorCode = TELEPHONY_ERROR;
204 TELEPHONY_LOGE("contact is nullptr");
205 continue;
206 }
207 contact->BuildContactData(rawId, contactDataValues);
208 if (contactDataValues.empty()) {
209 TELEPHONY_LOGE("no contactData insert");
210 errorCode = TELEPHONY_ERROR;
211 }
212 }
213 int ret = VCardRdbHelper::GetInstance().BatchInsertContactData(contactDataValues);
214 if (ret == TELEPHONY_ERROR) {
215 TELEPHONY_LOGE("batch insert contactDatat failed");
216 errorCode = TELEPHONY_ERROR;
217 }
218 }
219
SplitContactsVector(std::vector<std::shared_ptr<VCardContact>> list,size_t step)220 std::vector<std::vector<std::shared_ptr<VCardContact>>> VCardManager::SplitContactsVector(
221 std::vector<std::shared_ptr<VCardContact>> list, size_t step)
222 {
223 std::vector<std::vector<std::shared_ptr<VCardContact>>> result;
224 if (step >= list.size()) {
225 result.push_back(list);
226 } else {
227 std::vector<std::shared_ptr<VCardContact>>::iterator curPtr = list.begin();
228 std::vector<std::shared_ptr<VCardContact>>::iterator endPtr = list.end();
229 std::vector<std::shared_ptr<VCardContact>>::iterator end;
230 while (curPtr < endPtr) {
231 end = static_cast<size_t>(endPtr - curPtr) > step ? (step + curPtr) : endPtr;
232 step = static_cast<size_t>(endPtr - curPtr) > step ? step : static_cast<size_t>(endPtr - curPtr);
233 result.push_back(std::vector<std::shared_ptr<VCardContact>>(curPtr, end));
234 curPtr += step;
235 }
236 }
237 return result;
238 }
239
InsertRawContact(int32_t accountId)240 int32_t VCardManager::InsertRawContact(int32_t accountId)
241 {
242 OHOS::DataShare::DataShareValuesBucket ValuesBucket;
243 ValuesBucket.Put(RawContact::ACCOUNT_ID, GetAccountId());
244 if (IsContactsIdExit(accountId)) {
245 ValuesBucket.Put(RawContact::CONTACT_ID, accountId);
246 }
247 return VCardRdbHelper::GetInstance().InsertRawContact(ValuesBucket);
248 }
249
IsContactsIdExit(int32_t accountId)250 bool VCardManager::IsContactsIdExit(int32_t accountId)
251 {
252 std::vector<std::string> columns;
253 OHOS::DataShare::DataSharePredicates predicates;
254 predicates.EqualTo(Contact::ID, std::to_string(accountId));
255 auto resultSet = VCardRdbHelper::GetInstance().QueryContact(columns, predicates);
256 if (resultSet == nullptr) {
257 return false;
258 }
259 bool result = (resultSet->GoToFirstRow() == DataShare::E_OK);
260 resultSet->Close();
261 return result;
262 }
263
GetAccountId()264 int32_t VCardManager::GetAccountId()
265 {
266 std::vector<std::string> columns;
267 OHOS::DataShare::DataSharePredicates predicates;
268 predicates.EqualTo(Account::ACCOUNT_TYPE, "com.ohos.contacts");
269 auto resultSet = VCardRdbHelper::GetInstance().QueryAccount(columns, predicates);
270 if (resultSet == nullptr) {
271 return -1;
272 }
273 resultSet->GoToFirstRow();
274 int32_t index = 0;
275 int32_t id = 0;
276 resultSet->GetColumnIndex(Account::ID, index);
277 resultSet->GetInt(index, id);
278 resultSet->Close();
279 return id;
280 }
281
IsAccountIdExit(int32_t accountId)282 bool VCardManager::IsAccountIdExit(int32_t accountId)
283 {
284 std::vector<std::string> columns;
285 OHOS::DataShare::DataSharePredicates predicates;
286 predicates.EqualTo(Account::ID, std::to_string(accountId));
287 auto resultSet = VCardRdbHelper::GetInstance().QueryAccount(columns, predicates);
288 if (resultSet == nullptr) {
289 return false;
290 }
291 bool result = (resultSet->GoToFirstRow() == DataShare::E_OK);
292 resultSet->Close();
293 return result;
294 }
295
InsertContactData(int32_t rawId,std::shared_ptr<VCardContact> contact)296 int32_t VCardManager::InsertContactData(int32_t rawId, std::shared_ptr<VCardContact> contact)
297 {
298 if (contact == nullptr) {
299 return TELEPHONY_ERROR;
300 }
301 std::vector<DataShare::DataShareValuesBucket> contactDataValues;
302 contact->BuildContactData(rawId, contactDataValues);
303 if (contactDataValues.empty()) {
304 TELEPHONY_LOGI("no data insert");
305 return TELEPHONY_ERROR;
306 }
307 int ret = VCardRdbHelper::GetInstance().InsertContactData(contactDataValues);
308 if (ret == TELEPHONY_ERROR) {
309 TELEPHONY_LOGE("insert failed");
310 return TELEPHONY_ERROR;
311 }
312 return TELEPHONY_SUCCESS;
313 }
314
ParameterTypeAndCharsetCheck(int32_t cardType,std::string charset,int32_t & errorCode)315 bool VCardManager::ParameterTypeAndCharsetCheck(int32_t cardType, std::string charset, int32_t &errorCode)
316 {
317 if (cardType < VERSION_21_NUM || cardType > VERSION_40_NUM) {
318 errorCode = TELEPHONY_ERR_ARGUMENT_INVALID;
319 return false;
320 }
321 if (!charset.empty() && !VCardUtils::EqualsIgnoreCase(DEFAULT_CHARSET, charset)) {
322 errorCode = TELEPHONY_ERR_ARGUMENT_INVALID;
323 return false;
324 }
325 errorCode = TELEPHONY_SUCCESS;
326 return true;
327 }
328
ExportLock(std::string & path,std::shared_ptr<DataShare::DataShareHelper> dataShareHelper,const DataShare::DataSharePredicates & predicates,int32_t cardType,const std::string & charset)329 int32_t VCardManager::ExportLock(std::string &path, std::shared_ptr<DataShare::DataShareHelper> dataShareHelper,
330 const DataShare::DataSharePredicates &predicates, int32_t cardType, const std::string &charset)
331 {
332 std::lock_guard<std::mutex> lock(mutex_);
333 if (dataShareHelper == nullptr) {
334 TELEPHONY_LOGE("DataShareHelper is nullptr");
335 return TELEPHONY_ERR_LOCAL_PTR_NULL;
336 }
337 SetDataHelper(dataShareHelper);
338 int32_t errorCode = Export(path, predicates, cardType, charset);
339 Release();
340 TELEPHONY_LOGI("ExportLock errorCode : %{public}d finish", errorCode);
341 return errorCode;
342 }
343
Export(std::string & path,const DataShare::DataSharePredicates & predicates,int32_t cardType,const std::string & charset)344 int32_t VCardManager::Export(
345 std::string &path, const DataShare::DataSharePredicates &predicates, int32_t cardType, const std::string &charset)
346 {
347 int32_t errorCode = TELEPHONY_SUCCESS;
348 if (!ParameterTypeAndCharsetCheck(cardType, charset, errorCode)) {
349 return errorCode;
350 }
351 std::vector<std::string> columns;
352 auto resultSet = VCardRdbHelper::GetInstance().QueryContact(columns, predicates);
353 if (resultSet == nullptr) {
354 TELEPHONY_LOGE("QueryContact failed");
355 return TELEPHONY_ERR_LOCAL_PTR_NULL;
356 }
357 int32_t resultSetNum = resultSet->GoToFirstRow();
358 std::string result = "";
359 VCardEncoder encoder { cardType, charset };
360 while (resultSetNum == 0 && errorCode == TELEPHONY_SUCCESS) {
361 result += encoder.ContructVCard(resultSet, errorCode);
362 resultSetNum = resultSet->GoToNextRow();
363 }
364 if (errorCode != TELEPHONY_SUCCESS) {
365 TELEPHONY_LOGE("Export data failed");
366 resultSet->Close();
367 return errorCode;
368 }
369 if (path.empty()) {
370 std::string fileName = VCardUtils::CreateFileName();
371 path = VCARD_EXPORT_FILE_PATH + fileName;
372 } else {
373 path = path + VCardUtils::CreateFileName();
374 }
375 if (!result.empty()) {
376 VCardUtils::SaveFile(result, path);
377 resultSet->Close();
378 } else {
379 resultSet->Close();
380 return TELEPHONY_ERROR;
381 }
382 return TELEPHONY_SUCCESS;
383 }
384
ExportToStr(std::string & str,const DataShare::DataSharePredicates & predicates,int32_t cardType,const std::string & charset)385 int32_t VCardManager::ExportToStr(
386 std::string &str, const DataShare::DataSharePredicates &predicates, int32_t cardType, const std::string &charset)
387 {
388 std::vector<std::string> columns;
389 auto resultSet = VCardRdbHelper::GetInstance().QueryContact(columns, predicates);
390 if (resultSet == nullptr) {
391 TELEPHONY_LOGE("QueryContact failed");
392 return TELEPHONY_ERR_LOCAL_PTR_NULL;
393 }
394 int32_t resultSetNum = resultSet->GoToFirstRow();
395 VCardEncoder encoder { cardType, charset };
396 int32_t errorCode = TELEPHONY_SUCCESS;
397 str = "";
398 while (resultSetNum == 0 && errorCode == TELEPHONY_SUCCESS) {
399 str += encoder.ContructVCard(resultSet, errorCode);
400 resultSetNum = resultSet->GoToNextRow();
401 }
402 if (errorCode != TELEPHONY_SUCCESS) {
403 TELEPHONY_LOGE("Export data failed");
404 resultSet->Close();
405 return errorCode;
406 }
407 resultSet->Close();
408 return TELEPHONY_SUCCESS;
409 }
410
SetDataHelper(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper)411 void VCardManager::SetDataHelper(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper)
412 {
413 VCardRdbHelper::GetInstance().SetDataHelper(dataShareHelper);
414 }
415
GetInstance()416 VCardManager &VCardManager::GetInstance()
417 {
418 static VCardManager instance;
419 return instance;
420 }
421
Release()422 void VCardManager::Release()
423 {
424 VCardRdbHelper::GetInstance().Release();
425 if (listener_ != nullptr) {
426 listener_->GetContacts().clear();
427 }
428 }
429 } // namespace Telephony
430 } // namespace OHOS
431