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();
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 rawContactValues.push_back(valuesBucket);
188 rawIds.push_back(rawContactMaxId + i + 1);
189 }
190 VCardRdbHelper::GetInstance().BatchInsertRawContact(rawContactValues);
191 }
192
BatchInsertContactData(std::vector<int32_t> & rawIds,const std::vector<std::shared_ptr<VCardContact>> & contactList,int32_t & errorCode)193 void VCardManager::BatchInsertContactData(
194 std::vector<int32_t> &rawIds, const std::vector<std::shared_ptr<VCardContact>> &contactList, int32_t &errorCode)
195 {
196 std::vector<DataShare::DataShareValuesBucket> contactDataValues;
197 for (size_t i = 0; i < rawIds.size(); i++) {
198 int32_t rawId = rawIds[i];
199 std::shared_ptr<VCardContact> contact = contactList[i];
200 if (contact == nullptr) {
201 errorCode = TELEPHONY_ERROR;
202 TELEPHONY_LOGE("contact is nullptr");
203 continue;
204 }
205 contact->BuildContactData(rawId, contactDataValues);
206 if (contactDataValues.empty()) {
207 TELEPHONY_LOGE("no contactData insert");
208 errorCode = TELEPHONY_ERROR;
209 }
210 }
211 int ret = VCardRdbHelper::GetInstance().BatchInsertContactData(contactDataValues);
212 if (ret == TELEPHONY_ERROR) {
213 TELEPHONY_LOGE("batch insert contactDatat failed");
214 errorCode = TELEPHONY_ERROR;
215 }
216 }
217
SplitContactsVector(std::vector<std::shared_ptr<VCardContact>> list,size_t step)218 std::vector<std::vector<std::shared_ptr<VCardContact>>> VCardManager::SplitContactsVector(
219 std::vector<std::shared_ptr<VCardContact>> list, size_t step)
220 {
221 std::vector<std::vector<std::shared_ptr<VCardContact>>> result;
222 if (step >= list.size()) {
223 result.push_back(list);
224 } else {
225 std::vector<std::shared_ptr<VCardContact>>::iterator curPtr = list.begin();
226 std::vector<std::shared_ptr<VCardContact>>::iterator endPtr = list.end();
227 std::vector<std::shared_ptr<VCardContact>>::iterator end;
228 while (curPtr < endPtr) {
229 end = static_cast<size_t>(endPtr - curPtr) > step ? (step + curPtr) : endPtr;
230 step = static_cast<size_t>(endPtr - curPtr) > step ? step : static_cast<size_t>(endPtr - curPtr);
231 result.push_back(std::vector<std::shared_ptr<VCardContact>>(curPtr, end));
232 curPtr += step;
233 }
234 }
235 return result;
236 }
237
InsertRawContact(int32_t accountId)238 int32_t VCardManager::InsertRawContact(int32_t accountId)
239 {
240 OHOS::DataShare::DataShareValuesBucket ValuesBucket;
241 ValuesBucket.Put(RawContact::ACCOUNT_ID, GetAccountId());
242 if (IsContactsIdExit(accountId)) {
243 ValuesBucket.Put(RawContact::CONTACT_ID, accountId);
244 }
245 return VCardRdbHelper::GetInstance().InsertRawContact(ValuesBucket);
246 }
247
IsContactsIdExit(int32_t accountId)248 bool VCardManager::IsContactsIdExit(int32_t accountId)
249 {
250 std::vector<std::string> columns;
251 OHOS::DataShare::DataSharePredicates predicates;
252 predicates.EqualTo(Contact::ID, std::to_string(accountId));
253 auto resultSet = VCardRdbHelper::GetInstance().QueryContact(columns, predicates);
254 if (resultSet == nullptr) {
255 return false;
256 }
257 bool result = (resultSet->GoToFirstRow() == DataShare::E_OK);
258 resultSet->Close();
259 return result;
260 }
261
GetAccountId()262 int32_t VCardManager::GetAccountId()
263 {
264 std::vector<std::string> columns;
265 OHOS::DataShare::DataSharePredicates predicates;
266 predicates.EqualTo(Account::ACCOUNT_TYPE, "com.ohos.contacts");
267 auto resultSet = VCardRdbHelper::GetInstance().QueryAccount(columns, predicates);
268 if (resultSet == nullptr) {
269 return -1;
270 }
271 resultSet->GoToFirstRow();
272 int32_t index = 0;
273 int32_t id = 0;
274 resultSet->GetColumnIndex(Account::ID, index);
275 resultSet->GetInt(index, id);
276 resultSet->Close();
277 return id;
278 }
279
IsAccountIdExit(int32_t accountId)280 bool VCardManager::IsAccountIdExit(int32_t accountId)
281 {
282 std::vector<std::string> columns;
283 OHOS::DataShare::DataSharePredicates predicates;
284 predicates.EqualTo(Account::ID, std::to_string(accountId));
285 auto resultSet = VCardRdbHelper::GetInstance().QueryAccount(columns, predicates);
286 if (resultSet == nullptr) {
287 return false;
288 }
289 bool result = (resultSet->GoToFirstRow() == DataShare::E_OK);
290 resultSet->Close();
291 return result;
292 }
293
InsertContactData(int32_t rawId,std::shared_ptr<VCardContact> contact)294 int32_t VCardManager::InsertContactData(int32_t rawId, std::shared_ptr<VCardContact> contact)
295 {
296 if (contact == nullptr) {
297 return TELEPHONY_ERROR;
298 }
299 std::vector<DataShare::DataShareValuesBucket> contactDataValues;
300 contact->BuildContactData(rawId, contactDataValues);
301 if (contactDataValues.empty()) {
302 TELEPHONY_LOGI("no data insert");
303 return TELEPHONY_ERROR;
304 }
305 int ret = VCardRdbHelper::GetInstance().InsertContactData(contactDataValues);
306 if (ret == TELEPHONY_ERROR) {
307 TELEPHONY_LOGE("insert failed");
308 return TELEPHONY_ERROR;
309 }
310 return TELEPHONY_SUCCESS;
311 }
312
ParameterTypeAndCharsetCheck(int32_t cardType,std::string charset,int32_t & errorCode)313 bool VCardManager::ParameterTypeAndCharsetCheck(int32_t cardType, std::string charset, int32_t &errorCode)
314 {
315 if (cardType < VERSION_21_NUM || cardType > VERSION_40_NUM) {
316 errorCode = TELEPHONY_ERR_ARGUMENT_INVALID;
317 return false;
318 }
319 if (!charset.empty() && !VCardUtils::EqualsIgnoreCase(DEFAULT_CHARSET, charset)) {
320 errorCode = TELEPHONY_ERR_ARGUMENT_INVALID;
321 return false;
322 }
323 errorCode = TELEPHONY_SUCCESS;
324 return true;
325 }
326
ExportLock(std::string & path,std::shared_ptr<DataShare::DataShareHelper> dataShareHelper,const DataShare::DataSharePredicates & predicates,int32_t cardType,const std::string & charset)327 int32_t VCardManager::ExportLock(std::string &path, std::shared_ptr<DataShare::DataShareHelper> dataShareHelper,
328 const DataShare::DataSharePredicates &predicates, int32_t cardType, const std::string &charset)
329 {
330 std::lock_guard<std::mutex> lock(mutex_);
331 if (dataShareHelper == nullptr) {
332 TELEPHONY_LOGE("DataShareHelper is nullptr");
333 return TELEPHONY_ERR_LOCAL_PTR_NULL;
334 }
335 SetDataHelper(dataShareHelper);
336 int32_t errorCode = Export(path, predicates, cardType, charset);
337 Release();
338 TELEPHONY_LOGI("ExportLock errorCode : %{public}d finish", errorCode);
339 return errorCode;
340 }
341
Export(std::string & path,const DataShare::DataSharePredicates & predicates,int32_t cardType,const std::string & charset)342 int32_t VCardManager::Export(
343 std::string &path, const DataShare::DataSharePredicates &predicates, int32_t cardType, const std::string &charset)
344 {
345 int32_t errorCode = TELEPHONY_SUCCESS;
346 if (!ParameterTypeAndCharsetCheck(cardType, charset, errorCode)) {
347 return errorCode;
348 }
349 std::vector<std::string> columns;
350 auto resultSet = VCardRdbHelper::GetInstance().QueryContact(columns, predicates);
351 if (resultSet == nullptr) {
352 TELEPHONY_LOGE("QueryContact failed");
353 return TELEPHONY_ERR_LOCAL_PTR_NULL;
354 }
355 int32_t resultSetNum = resultSet->GoToFirstRow();
356 std::string result = "";
357 VCardEncoder encoder { cardType, charset };
358 while (resultSetNum == 0 && errorCode == TELEPHONY_SUCCESS) {
359 result += encoder.ContructVCard(resultSet, errorCode);
360 resultSetNum = resultSet->GoToNextRow();
361 }
362 if (errorCode != TELEPHONY_SUCCESS) {
363 TELEPHONY_LOGE("Export data failed");
364 resultSet->Close();
365 return errorCode;
366 }
367 if (path.empty()) {
368 std::string fileName = VCardUtils::CreateFileName();
369 path = VCARD_EXPORT_FILE_PATH + fileName;
370 } else {
371 path = path + VCardUtils::CreateFileName();
372 }
373 if (!result.empty()) {
374 VCardUtils::SaveFile(result, path);
375 resultSet->Close();
376 } else {
377 resultSet->Close();
378 return TELEPHONY_ERROR;
379 }
380 return TELEPHONY_SUCCESS;
381 }
382
ExportToStr(std::string & str,const DataShare::DataSharePredicates & predicates,int32_t cardType,const std::string & charset)383 int32_t VCardManager::ExportToStr(
384 std::string &str, const DataShare::DataSharePredicates &predicates, int32_t cardType, const std::string &charset)
385 {
386 std::vector<std::string> columns;
387 auto resultSet = VCardRdbHelper::GetInstance().QueryContact(columns, predicates);
388 if (resultSet == nullptr) {
389 TELEPHONY_LOGE("QueryContact failed");
390 return TELEPHONY_ERR_LOCAL_PTR_NULL;
391 }
392 int32_t resultSetNum = resultSet->GoToFirstRow();
393 VCardEncoder encoder { cardType, charset };
394 int32_t errorCode = TELEPHONY_SUCCESS;
395 str = "";
396 while (resultSetNum == 0 && errorCode == TELEPHONY_SUCCESS) {
397 str += encoder.ContructVCard(resultSet, errorCode);
398 resultSetNum = resultSet->GoToNextRow();
399 }
400 if (errorCode != TELEPHONY_SUCCESS) {
401 TELEPHONY_LOGE("Export data failed");
402 resultSet->Close();
403 return errorCode;
404 }
405 resultSet->Close();
406 return TELEPHONY_SUCCESS;
407 }
408
SetDataHelper(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper)409 void VCardManager::SetDataHelper(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper)
410 {
411 VCardRdbHelper::GetInstance().SetDataHelper(dataShareHelper);
412 }
413
GetInstance()414 VCardManager &VCardManager::GetInstance()
415 {
416 static VCardManager instance;
417 return instance;
418 }
419
Release()420 void VCardManager::Release()
421 {
422 VCardRdbHelper::GetInstance().Release();
423 if (listener_ != nullptr) {
424 listener_->GetContacts().clear();
425 }
426 }
427 } // namespace Telephony
428 } // namespace OHOS
429