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