1/** 2 * Copyright (c) 2022 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 16import MorandiColor from '../model/bean/MorandiColor'; 17import dataShare from '@ohos.data.dataShare'; 18import dataSharePredicates from "@ohos.data.dataSharePredicates"; 19import { ContactVo } from '../model/bean/ContactVo'; 20import { ContactInfo } from "../model/bean/ContactInfo"; 21import { ContactRepository } from '../../../../../feature/contact/src/main/ets/repo/ContactRepository'; 22import { DataItem } from '../../../../../feature/contact/src/main/ets/entity/DataItem'; 23import { DataItemType } from '../../../../../feature/contact/src/main/ets/contract/DataType'; 24import { Data } from '../../../../../feature/contact/src/main/ets/contract/Data'; 25import { RawContacts } from '../../../../../feature/contact/src/main/ets/contract/RawContacts'; 26import { Contacts } from '../../../../../feature/contact/src/main/ets/contract/Contacts'; 27import { HiLog } from '../../../../../common/src/main/ets/util/HiLog'; 28import { StringUtil } from '../../../../../common/src/main/ets/util/StringUtil'; 29import { ArrayUtil } from '../../../../../common/src/main/ets/util/ArrayUtil'; 30import { CallLogRepository } from '../../../../../feature/call/src/main/ets/repo/CallLogRepository'; 31import { PhoneNumber } from '../../../../../feature/phonenumber/src/main/ets/PhoneNumber'; 32 33const TAG = "ContactAbility: "; 34const PROFILE_CONTACT_DATA_URI = 'datashare:///com.ohos.contactsdataability/profile/contact_data'; 35 36export default { 37 /** 38 * Add Contact 39 * 40 * @param {Object} addParams Contact Information 41 * @param {Object} callBack 42 */ 43 addContact: async function (addParams: ContactInfo, callBack?, context?) { 44 HiLog.i(TAG, 'Start to create a new contact.'); 45 if (addParams == undefined || addParams == null) { 46 HiLog.e(TAG, "The addParams of parameter is NULL"); 47 return ""; 48 } 49 let DAHelper = await dataShare.createDataShareHelper(context ? context : globalThis.context, Contacts.CONTENT_URI); 50 let insertValues = { 51 'display_name': this.getDisplayName(addParams), 52 }; 53 DAHelper.insert( 54 RawContacts.CONTENT_URI, 55 insertValues 56 ).then(data => { 57 if (data == -1 || data == undefined) { 58 HiLog.e(TAG, 'Data inserted failed'); 59 return; 60 } 61 addParams.id = data.toString(); 62 this.dealParam(DAHelper, addParams, false, callBack); 63 }).catch(error => { 64 HiLog.e(TAG, 'logMessage insert error: ' + JSON.stringify(error.message)); 65 }); 66 HiLog.i(TAG, 'End of creating a contact.'); 67 }, 68 69 /** 70 * Read the name, which needs to be optimized. 71 * Edit Contact 72 * 73 * @param {Object} addParams Contact Information 74 * @return {string} Contact Name 75 */ 76 getDisplayName: function (addParams: ContactInfo): String { 77 let displayName = ''; 78 if (addParams.display_name != undefined && addParams.display_name.length > 0) { 79 displayName = addParams.display_name; 80 } else if (addParams.nickname != undefined && addParams.nickname.length > 0) { 81 displayName = addParams.nickname; 82 } else if (addParams.hasOwnProperty('company') && addParams.company.length > 0) { 83 displayName = addParams.company; 84 } else if (addParams.hasOwnProperty('position') && addParams.position.length > 0) { 85 displayName = addParams.position; 86 } else if (addParams.hasOwnProperty('phones') && addParams.phones.length > 0) { 87 for (let element of addParams.phones) { 88 if (StringUtil.isEmpty(element.num)) { 89 continue; 90 } 91 displayName = PhoneNumber.fromString(element.num).getNumber(); 92 break; 93 } 94 } 95 return displayName; 96 }, 97 98 /** 99 * Convert the data to the database. 100 * 101 * @param {string} DAHelper Database path 102 * @param {Object} addParams Contact Information 103 * @param {boolean} isCard Indicates whether the information is a business card. 104 */ 105 dealParam: function (DAHelper, addParams: ContactInfo, isCard, callBack) { 106 let result = addParams.id; 107 let uri = isCard ? PROFILE_CONTACT_DATA_URI : Data.CONTENT_URI; 108 109 this.dataContact(addParams, DAHelper, result, uri); 110 this.organizationContact(addParams, DAHelper, result, uri); 111 this.noteContact(addParams, DAHelper, result, uri); 112 this.phoneContact(addParams, DAHelper, result, uri); 113 this.emailContact(addParams, DAHelper, result, uri); 114 this.postalContact(addParams, DAHelper, result, uri); 115 this.eventContact(addParams, DAHelper, result, uri); 116 this.imContact(addParams, DAHelper, result, uri); 117 this.groupsContact(addParams, DAHelper, result, uri); 118 this.websiteContact(addParams, DAHelper, result, uri); 119 this.nickContact(addParams, DAHelper, result, uri); 120 this.relationsContact(addParams, DAHelper, result, uri); 121 122 if (callBack != undefined) { 123 HiLog.d(TAG, 'Start the callback function.'); 124 callBack(addParams.id); 125 } 126 127 ContactRepository.getInstance().notifyChange(); 128 CallLogRepository.getInstance().notifyChange(); 129 }, 130 131 /** 132 * Save the contact name to the database. 133 * 134 * @param {Object} addParams Contact Information 135 * @param {string} DAHelper Database path 136 * @param {string} result Contact ID 137 * @param {string} uri Database address 138 */ 139 dataContact: function (addParams, DAHelper, result, uri) { 140 let displayName = ''; 141 let updateFlag = 0; 142 if (addParams.display_name != undefined && addParams.display_name.length > 0) { 143 displayName = addParams.display_name; 144 } else { 145 updateFlag = 1; 146 displayName = this.getDisplayName(addParams); 147 } 148 149 if (displayName.length > 0) { 150 let dataContact = { 151 'raw_contact_id': result, 152 'detail_info': displayName, 153 'alpha_name': displayName, 154 'phonetic_name': addParams.hasOwnProperty('phonetic_name') ? (addParams.phonetic_name) : "", 155 'other_lan_last_name': addParams.hasOwnProperty('other_lan_last_name') ? (addParams.other_lan_last_name) : "", 156 'other_lan_first_name': addParams.hasOwnProperty('other_lan_first_name') ? (addParams.other_lan_first_name) : "", 157 'type_id': 6, 158 'extend7': updateFlag 159 }; 160 DAHelper.insert( 161 uri, 162 dataContact 163 ).then(data => { 164 if (data == -1) { 165 HiLog.e(TAG, 'name-insert data failed!'); 166 } 167 HiLog.d(TAG, 'name-insert data success!'); 168 }).catch(err => { 169 HiLog.e(TAG, 'name-insert failed. Cause: ' + JSON.stringify(err.message)); 170 }); 171 } 172 }, 173 174 /** 175 * Save the contact nickname information to the database. 176 * 177 * @param {Object} addParams Contact Information 178 * @param {string} DAHelper Database path 179 * @param {number} result Contact ID 180 * @param {string} uri Database address 181 */ 182 nickContact: function (addParams, DAHelper, result, uri) { 183 if (addParams.nickname != undefined && addParams.nickname.length > 0) { 184 let nickContact = { 185 'raw_contact_id': result, 186 'detail_info': addParams.nickname, 187 'type_id': 9, 188 }; 189 DAHelper.insert( 190 uri, 191 nickContact 192 ).then(data => { 193 if (data == -1) { 194 HiLog.e(TAG, 'nickname-insert data failed!'); 195 } 196 HiLog.d(TAG, 'nickname-insert data success!'); 197 }).catch(err => { 198 HiLog.e(TAG, 'nickname-insert failed. Cause: ' + JSON.stringify(err.message)); 199 }); 200 } 201 }, 202 /** 203 * Save the position information of the contact company to the database. 204 * 205 * @param {Object} addParams Contact Information 206 * @param {string} DAHelper Database path 207 * @param {string} result Contact ID 208 * @param {string} uri Database address 209 */ 210 organizationContact: function (addParams, DAHelper, result, uri) { 211 let company = ''; 212 let position = ''; 213 if (addParams.company != undefined && addParams.company.length > 0) { 214 company = addParams.company; 215 } 216 if (addParams.position != undefined && addParams.position.length > 0) { 217 position = addParams.position; 218 } 219 if (addParams.company != undefined && addParams.company.length > 0 220 || addParams.position != undefined && addParams.position.length > 0) { 221 let organizationContact = { 222 'raw_contact_id': result, 223 'detail_info': company, 224 'position': position, 225 'type_id': 4, 226 }; 227 DAHelper.insert( 228 uri, 229 organizationContact 230 ).then(data => { 231 if (data == -1) { 232 HiLog.e(TAG, 'organizationContact-insert data failed!'); 233 } 234 HiLog.d(TAG, 'organizationContact-insert data success!'); 235 }).catch(error => { 236 HiLog.e(TAG, 'organizationContact-insert failed. Cause: ' + JSON.stringify(error.message)); 237 }); 238 } 239 }, 240 241 /** 242 * Save the contact remarks to the database. 243 * 244 * @param {Object} addParams Contact Information 245 * @param {string} DAHelper Database path 246 * @param {number} result Contact ID 247 * @param {string} uri Database address 248 */ 249 noteContact: function (addParams, DAHelper, result, uri) { 250 if (addParams.remarks != undefined && addParams.remarks.length > 0) { 251 let noteContact = { 252 'raw_contact_id': result, 253 'detail_info': addParams.remarks, 254 'type_id': 10, 255 }; 256 DAHelper.insert( 257 uri, 258 noteContact 259 ).then(data => { 260 if (data == -1) { 261 HiLog.e(TAG, 'noteContact-insert data failed!'); 262 } 263 HiLog.d(TAG, 'noteContact-insert insert data success!'); 264 }).catch(error => { 265 HiLog.e(TAG, 'noteContact-insert failed. Cause: ' + JSON.stringify(error.message)); 266 }); 267 } 268 }, 269 270 /** 271 * The contact mobile number information is saved to the database. 272 * 273 * @param {Object} addParams Contact Information 274 * @param {string} DAHelper Database path 275 * @param {number} result Contact ID 276 * @param {string} uri Database address 277 */ 278 phoneContact: function (addParams, DAHelper, result, uri) { 279 if (addParams.phones != undefined && addParams.phones.length > 0) { 280 let index = 1; 281 addParams.phones.forEach(element => { 282 if (StringUtil.isEmpty(element.num)) { 283 return; 284 } 285 let phoneContact = { 286 'raw_contact_id': result, 287 'detail_info': element.num, 288 'extend7': (index++).toString(), 289 'custom_data': element.numType, 290 'type_id': 5, 291 }; 292 DAHelper.insert( 293 uri, 294 phoneContact 295 ).then(data => { 296 if (data == -1) { 297 HiLog.e(TAG, 'phoneContact-insert data failed!'); 298 } 299 HiLog.d(TAG, 'phoneContact-insert data success!'); 300 }).catch(error => { 301 HiLog.e(TAG, 'phoneContact-insert failed. Cause: ' + JSON.stringify(error.message)); 302 }); 303 }); 304 } 305 }, 306 307 /** 308 * Save the contact email information to the database. 309 * 310 * @param {Object} addParams Contact Information 311 * @param {string} DAHelper Database path 312 * @param {number} result Contact ID 313 * @param {string} uri Database address 314 */ 315 emailContact: function (addParams, DAHelper, result, uri) { 316 if (addParams.emails != undefined && addParams.emails.length > 0) { 317 let index = 1; 318 addParams.emails.forEach(element => { 319 if (StringUtil.isEmpty(element.address)) { 320 return; 321 } 322 let emailContact = { 323 'raw_contact_id': result, 324 'detail_info': element.address, 325 'extend7': (index++).toString(), 326 'custom_data': element.emailType, 327 'type_id': 1, 328 }; 329 DAHelper.insert( 330 uri, 331 emailContact 332 ).then(data => { 333 if (data == -1) { 334 HiLog.e(TAG, 'emailContact-insert data failed!'); 335 } 336 HiLog.d(TAG, 'emailContact-insert data success!'); 337 }).catch(error => { 338 HiLog.e(TAG, 'emailContact-insert failed. Cause: ' + JSON.stringify(error.message)); 339 }); 340 }); 341 } 342 }, 343 344 /** 345 * The contact address information is saved to the database. 346 * 347 * @param {Object} addParams Contact Information 348 * @param {string} DAHelper Database path 349 * @param {number} result Contact ID 350 * @param {string} uri Database address 351 */ 352 postalContact: function (addParams, DAHelper, result, uri) { 353 if (addParams.houses != undefined && addParams.houses.length > 0) { 354 let index = 1; 355 addParams.houses.forEach(element => { 356 if (StringUtil.isEmpty(element.houseName)) { 357 return; 358 } 359 let postalContact = { 360 'raw_contact_id': result, 361 'detail_info': element.houseName, 362 'extend7': (index++).toString(), 363 'custom_data': element.houseType, 364 'type_id': 7, 365 }; 366 DAHelper.insert( 367 uri, 368 postalContact 369 ).then(data => { 370 if (data == -1) { 371 HiLog.e(TAG, 'postalContact-insert data failed!'); 372 } 373 HiLog.d(TAG, 'postalContact-insert data success!'); 374 }).catch(error => { 375 HiLog.e(TAG, 'postalContact-insert failed. Cause: ' + JSON.stringify(error.message)); 376 }); 377 }); 378 } 379 }, 380 381 382 /** 383 * Save the contact special date information to the database. 384 * 385 * @param {Object} addParams Contact Information 386 * @param {string} DAHelper Database path 387 * @param {number} result Contact ID 388 * @param {string} uri Database address 389 */ 390 eventContact: function (addParams, DAHelper, result, uri) { 391 if (addParams.events != undefined && addParams.events.length > 0) { 392 let index = 1; 393 addParams.events.forEach(element => { 394 if (StringUtil.isEmpty(element.data)) { 395 return; 396 } 397 let eventContact = { 398 'raw_contact_id': result, 399 'detail_info': element.data, 400 'extend7': (index++).toString(), 401 'custom_data': element.eventType, 402 'type_id': 11 403 }; 404 DAHelper.insert( 405 uri, 406 eventContact 407 ).then(data => { 408 if (data == -1) { 409 HiLog.e(TAG, 'eventContact-insert data failed!'); 410 } 411 HiLog.d(TAG, 'eventContact-insert data success!'); 412 }).catch(error => { 413 HiLog.e(TAG, 'eventContact-insert failed. Cause: ' + JSON.stringify(error.message)); 414 }); 415 }); 416 } 417 }, 418 419 /** 420 * Save the contact IMA information to the database. 421 * 422 * @param {Object} addParams Contact Information 423 * @param {string} DAHelper Database path 424 * @param {number} result Contact ID 425 * @param {string} uri Database address 426 */ 427 imContact: function (addParams, DAHelper, result, uri) { 428 if (addParams.aims != undefined && addParams.aims.length > 0) { 429 let index = 1; 430 addParams.aims.forEach(element => { 431 if (StringUtil.isEmpty(element.aimName)) { 432 return; 433 } 434 let imContact = { 435 'raw_contact_id': result, 436 'detail_info': element.aimName, 437 'extend7': (index++).toString(), 438 'custom_data': element.aimType, 439 'type_id': 2, 440 }; 441 DAHelper.insert( 442 uri, 443 imContact 444 ).then(data => { 445 if (data == -1) { 446 HiLog.e(TAG, 'imContact-insert data failed!'); 447 } 448 HiLog.d(TAG, 'imContact-insert data success!'); 449 }).catch(error => { 450 HiLog.e(TAG, 'imContact-insert failed. Cause: ' + JSON.stringify(error.message)); 451 }); 452 }); 453 } 454 }, 455 456 /** 457 * Save the contact group information to the database. 458 * 459 * @param {Object} addParams Contact Information 460 * @param {string} DAHelper Database path 461 * @param {number} result Contact ID 462 * @param {string} uri Database address 463 */ 464 groupsContact: function (addParams, DAHelper, result, uri) { 465 if (addParams.groups != undefined && addParams.groups.length > 0) { 466 addParams.groups.forEach(element => { 467 let groupsContact = { 468 'raw_contact_id': result, 469 'detail_info': element.groupName, 470 'extend7': element.groupId + '', 471 'custom_data': element.groupType, 472 'type_id': 9, 473 }; 474 DAHelper.insert( 475 uri, 476 groupsContact 477 ).then(data => { 478 if (data == -1) { 479 HiLog.e(TAG, 'groupsContact-insert data failed!'); 480 } 481 HiLog.d(TAG, 'groupsContact-insert data success!'); 482 }).catch(error => { 483 HiLog.e(TAG, 'groupsContact-insert failed. Cause: %s', JSON.stringify(error.message)); 484 }); 485 }); 486 } 487 }, 488 489 /** 490 * Save the contact website information to the database. 491 * 492 * @param {Object} addParams Contact Information 493 * @param {string} DAHelper Database path 494 * @param {number} result Contact ID 495 * @param {string} uri Database address 496 */ 497 websiteContact: function (addParams, DAHelper, result, uri) { 498 if (!ArrayUtil.isEmpty(addParams.websites)) { 499 addParams.websites.forEach(element => { 500 if (StringUtil.isEmpty(element)) 501 return; 502 let websiteContact = { 503 'raw_contact_id': result, 504 'detail_info': element, 505 'type_id': 12, 506 }; 507 DAHelper.insert( 508 uri, 509 websiteContact 510 ).then(data => { 511 if (data == -1) { 512 HiLog.e(TAG, 'websiteContact-insert data failed!'); 513 } 514 HiLog.d(TAG, 'websiteContact-insert data success!'); 515 }).catch(error => { 516 HiLog.e(TAG, 'websiteContact-insert failed. Cause: %s', JSON.stringify(error.message)); 517 }); 518 }); 519 } 520 }, 521 522 /** 523 * The relation information of the contact is saved to the database. 524 * 525 * @param {Object} addParams Contact Information 526 * @param {string} DAHelper Database path 527 * @param {number} result Contact ID 528 * @param {string} uri Database address 529 */ 530 relationsContact: function (addParams, DAHelper, result, uri) { 531 if (!ArrayUtil.isEmpty(addParams.relationships)) { 532 let index = 1; 533 addParams.relationships.forEach(element => { 534 if (StringUtil.isEmpty(element.name)) { 535 return; 536 } 537 let relationsContact = { 538 'raw_contact_id': result, 539 'detail_info': element.name, 540 'extend7': (index++).toString(), 541 'custom_data': element.associatedType, 542 'type_id': 13, 543 }; 544 DAHelper.insert( 545 uri, 546 relationsContact 547 ).then(data => { 548 if (data == -1) { 549 HiLog.e(TAG, 'relationsContact-insert data failed!'); 550 } 551 HiLog.d(TAG, 'relationsContact-insert data success!'); 552 }).catch(error => { 553 HiLog.e(TAG, 'relationsContact-insert failed. Cause: %s', JSON.stringify(error.message)); 554 }); 555 }); 556 } 557 }, 558 559 /** 560 * Querying the Mobile Numbers of All Contacts 561 * 562 * @param {string} DAHelper 563 * @param {Object} callBack 564 */ 565 getAllContact: async function (actionData, callBack, context?) { 566 HiLog.i(TAG, 'Start to query all contacts without PhoneNumbers'); 567 if (context) { 568 ContactRepository.getInstance().init(context); 569 } 570 ContactRepository.getInstance().findAll(actionData, contactList => { 571 if (ArrayUtil.isEmpty(contactList)) { 572 HiLog.i(TAG, 'queryContacts-SelectcontactsModel queryContact resultSet is empty!'); 573 let emptyResult: ContactVo[] = []; 574 callBack(emptyResult); 575 return; 576 } 577 let resultList = []; 578 for (let contactItem of contactList) { 579 let jsonObj: ContactVo = new ContactVo("", "", "", "", "", "", true, "", ""); 580 jsonObj.contactId = contactItem.id.toString(); 581 jsonObj.emptyNameData = contactItem.displayName; 582 jsonObj.namePrefix = contactItem.sortFirstLetter; 583 jsonObj.nameSuffix = contactItem.photoFirstName; 584 jsonObj.company = contactItem.company; 585 jsonObj.position = contactItem.position; 586 jsonObj.portraitColor = MorandiColor.Color[Math.abs(parseInt(jsonObj.contactId)) % 6]; 587 jsonObj.show = false; 588 jsonObj.setShowName(); 589 resultList.push(jsonObj); 590 } 591 callBack(resultList); 592 HiLog.i(TAG, 'End of querying all contacts'); 593 }); 594 }, 595 596 /** 597 * 查询所有联系人手机号 598 * 599 * @param {string} DAHelper 数据库地址 600 * @param {Object} callBack 回调 601 */ 602 getAllContactWithPhoneNumbers: function (callBack, context?) { 603 HiLog.i(TAG, 'Start to query all contacts with PhoneNumbers'); 604 if (context) { 605 ContactRepository.getInstance().init(context); 606 } 607 ContactRepository.getInstance().findByPhoneIsNotNull(contactList => { 608 if (ArrayUtil.isEmpty(contactList)) { 609 HiLog.i(TAG, 'getAllContactWithPhoneNumbers-SelectcontactsModel queryContact resultSet is empty!'); 610 let emptyResult: ContactVo[] = []; 611 callBack(emptyResult); 612 return; 613 } 614 let resultList = []; 615 for (let contactItem of contactList) { 616 let jsonObj: ContactVo = new ContactVo("", "", "", "", "", "", true, "", ""); 617 jsonObj.contactId = contactItem.id.toString(); 618 jsonObj.emptyNameData = contactItem.displayName; 619 jsonObj.namePrefix = contactItem.sortFirstLetter; 620 jsonObj.nameSuffix = contactItem.photoFirstName; 621 jsonObj.company = contactItem.company; 622 jsonObj.position = contactItem.position; 623 jsonObj.portraitColor = MorandiColor.Color[Math.abs(parseInt(jsonObj.contactId)) % 6]; 624 jsonObj.show = false; 625 jsonObj.phoneNumbers = contactItem.phoneNumbers; 626 jsonObj.setShowName(); 627 resultList.push(jsonObj); 628 } 629 callBack(resultList); 630 HiLog.i(TAG, 'End of querying all contacts'); 631 }); 632 }, 633 634 /** 635 * Obtain contact details. 636 * 637 * @param {Object} contactId Contact data ID. 638 * @param {Object} callback Contact details 639 */ 640 getContactById: function (contactId, callback, context?) { 641 HiLog.i(TAG, 'Start to query contact by id.'); 642 if (context) { 643 ContactRepository.getInstance().init(context); 644 } 645 ContactRepository.getInstance().findById(contactId, contact => { 646 let res = { 647 'data': {} 648 }; 649 if (contact == undefined || ArrayUtil.isEmpty(contact.rowContacts)) { 650 HiLog.e(TAG, 'Query contact by id failed.'); 651 callback(res); 652 return; 653 } 654 let contactDetailInfo: any = {}; 655 for (let dataItem of contact.rowContacts[0].dataItems) { 656 this.dealResult(dataItem, contactDetailInfo); 657 } 658 contactDetailInfo.id = contact.rowContacts[0].id; 659 contactDetailInfo.photoFirstName = contact.rowContacts[0].values.get(RawContacts.PHOTO_FIRST_NAME); 660 res.data = contactDetailInfo; 661 callback(res); 662 HiLog.i(TAG, 'End of querying contacts by ID.'); 663 }); 664 }, 665 666 /** 667 * Process contact details 668 * 669 * @param {Object} resultSet 670 * @param {Object} contactDetailInfo Contact details data 671 * @param {Object} actionData Contact data 672 */ 673 dealResult: function (dataItem: DataItem, contactDetailInfo) { 674 switch (dataItem.getContentTypeId()) { 675 case DataItemType.NAME: 676 contactDetailInfo.display_name = dataItem.getData(); 677 contactDetailInfo.nameUpdate = dataItem.getLabelId(); 678 break; 679 case DataItemType.PHONE: 680 let phone_element = { 681 'num': dataItem.getData(), 682 'id': dataItem.getLabelId(), 683 'numType': dataItem.getLabelName() 684 }; 685 if (contactDetailInfo.phones) { 686 contactDetailInfo.phones.push(phone_element); 687 } else { 688 contactDetailInfo.phones = [phone_element]; 689 } 690 break; 691 case DataItemType.EMAIL: 692 let email_element = { 693 'address': dataItem.getData(), 694 'id': dataItem.getLabelId(), 695 'emailType': dataItem.getLabelName() 696 }; 697 if (contactDetailInfo.emails) { 698 contactDetailInfo.emails.push(email_element); 699 } else { 700 contactDetailInfo.emails = [email_element]; 701 } 702 break; 703 case DataItemType.NOTE: 704 contactDetailInfo.remarks = dataItem.getData(); 705 break; 706 case DataItemType.ORGANIZATION: 707 contactDetailInfo.position = dataItem.values.get(Data.POSITION); 708 contactDetailInfo.company = dataItem.getData(); 709 break; 710 case DataItemType.IM: 711 let aim_element = { 712 'aimName': dataItem.getData(), 713 'aimId': dataItem.getLabelId(), 714 'aimType': dataItem.getLabelName() 715 }; 716 if (contactDetailInfo.aims) { 717 contactDetailInfo.aims.push(aim_element); 718 } else { 719 contactDetailInfo.aims = [aim_element]; 720 } 721 break; 722 case DataItemType.STRUCTURED_POSTAL: 723 let house_element = { 724 'houseName': dataItem.getData(), 725 'houseId': dataItem.getLabelId(), 726 'houseType': dataItem.getLabelName() 727 }; 728 if (contactDetailInfo.houses) { 729 contactDetailInfo.houses.push(house_element); 730 } else { 731 contactDetailInfo.houses = [house_element]; 732 } 733 break; 734 case DataItemType.GROUP_MEMBERSHIP: 735 contactDetailInfo.nickname = dataItem.getData(); 736 break; 737 case DataItemType.EVENT: 738 let event_element = { 739 'id': dataItem.getLabelId(), 740 'data': dataItem.getData(), 741 'eventType': dataItem.getLabelName(), 742 'eventName': "" 743 }; 744 if (contactDetailInfo.events) { 745 contactDetailInfo.events.push(event_element); 746 } else { 747 contactDetailInfo.events = [event_element]; 748 } 749 break; 750 case DataItemType.WEBSITE: 751 let website_element = dataItem.getData(); 752 if (contactDetailInfo.websites) { 753 contactDetailInfo.websites.push(website_element); 754 } else { 755 contactDetailInfo.websites = [website_element]; 756 } 757 break; 758 case DataItemType.RELATION: 759 let relation_element = { 760 'id': dataItem.getLabelId(), 761 'associatedPersonId': '', 762 'name': dataItem.getData(), 763 'associatedType': dataItem.getLabelName(), 764 }; 765 if (contactDetailInfo.relationships) { 766 contactDetailInfo.relationships.push(relation_element); 767 } else { 768 contactDetailInfo.relationships = [relation_element]; 769 } 770 break; 771 } 772 }, 773 774 /** 775 * Obtains the contact ID of the number by phone number 776 * 777 * @param {string} DAHelper 778 * @param {string} number Mobile number 779 * @param {Object} callBack Contact ID 780 */ 781 getContactIdByNumber: async function (DAHelper, number, callBack) { 782 HiLog.i(TAG, 'Start to query contacts by phone number.'); 783 if (DAHelper == undefined || DAHelper.length == 0) { 784 DAHelper = await dataShare.createDataShareHelper(globalThis.context, Contacts.CONTENT_URI); 785 } 786 if (StringUtil.isEmpty(number)) { 787 return; 788 } 789 let resultColumns = [ 790 'raw_contact_id', 791 ]; 792 let cleanNumber = StringUtil.removeSpace(number); 793 let condition = new dataSharePredicates.DataSharePredicates(); 794 condition.equalTo('detail_info', cleanNumber); 795 condition.and(); 796 condition.equalTo('is_deleted', 0); 797 condition.and(); 798 condition.equalTo('type_id', '5'); 799 let resultSet = await DAHelper.query(Data.CONTENT_URI, resultColumns, condition); 800 if (StringUtil.isEmpty(resultSet) || resultSet.rowCount == 0) { 801 HiLog.i(TAG, 'getContactIdByNumber-contactId resultSet is empty!'); 802 callBack(); 803 return; 804 } 805 resultSet.goToFirstRow(); 806 let contactId = resultSet.getString(0); 807 resultSet.close(); 808 callBack(contactId); 809 HiLog.i(TAG, 'End of querying contacts by phone number.'); 810 }, 811 812 /** 813 * Edit Contact Information 814 * 815 * @param {string} DAHelper 816 * @param {Object} addParams Contact Information 817 * @param {Object} callBack Contact ID 818 */ 819 updateContact: async function (DAHelper, addParams, callBack, context?) { 820 HiLog.i(TAG, 'Start to update contacts.'); 821 try { 822 if (DAHelper == undefined || DAHelper.length == 0 || DAHelper == null) { 823 DAHelper = await dataShare.createDataShareHelper(context ? context : globalThis.context, Contacts.CONTENT_URI); 824 } 825 let condition = new dataSharePredicates.DataSharePredicates(); 826 condition.equalTo('raw_contact_id', addParams.id); 827 DAHelper.delete( 828 Data.CONTENT_URI, 829 condition, 830 ).then(data => { 831 this.dealParam(DAHelper, addParams, false, callBack); 832 }).catch(error => { 833 HiLog.e(TAG, 'updateContact-update contact error: %s', JSON.stringify(error.message)); 834 }); 835 HiLog.i(TAG, 'End to update contacts.'); 836 } catch(err) { 837 HiLog.e(TAG, 'updateContact err : ' + JSON.stringify(err)); 838 } 839 }, 840 /** 841 * Querying IDs by Phone Number 842 * 843 * @param {string} DAHelper 844 * @param {string} addParams Phone number 845 * @param {Object} callBack Contact ID array 846 */ 847 getIdByTelephone: async function (number, callBack, context?) { 848 HiLog.i(TAG, 'Start to query contactID by phone number.'); 849 let DAHelper = await dataShare.createDataShareHelper(context ?context : globalThis.context, Contacts.CONTENT_URI); 850 let condition = new dataSharePredicates.DataSharePredicates(); 851 condition.equalTo('detail_info', number) 852 .and() 853 .equalTo('type_id', 5) 854 .and() 855 .equalTo('is_deleted', 0); 856 let columns = ["raw_contact_id"]; 857 let data_row = await DAHelper.query(Data.CONTENT_URI, condition, columns); 858 let resultList = []; 859 if (data_row.rowCount <= 0) { 860 data_row.close(); 861 callBack(resultList); 862 return; 863 } 864 data_row.goToFirstRow(); 865 let maxRows = data_row.rowCount; 866 let tempIndex = data_row.rowCount; 867 let index = 0; 868 do { 869 resultList.push(data_row.getString(data_row.getColumnIndex("raw_contact_id"))); 870 if ((++index) >= maxRows) { 871 data_row.close(); 872 callBack(resultList); 873 } 874 if (!data_row.goToNextRow()) { 875 break; 876 } 877 } while ((--tempIndex) >= 0) 878 data_row.close(); 879 HiLog.i(TAG, 'End to query contactID by phone number.'); 880 }, 881}