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 */ 15const featureAbility = requireNapi('ability.featureAbility'); 16const contact = requireInternal('contact'); 17 18async function contactsPickerSelect() { 19 if (arguments.length == 2 && typeof arguments[1] != "function") { 20 console.log("[picker] contactsPickerSelect callback invalid"); 21 throw Error("invalid callback"); 22 } 23 24 try { 25 let result; 26 if (globalThis.abilityContext) { 27 let config = { 28 parameters: { 29 'pageFlag': "page_flag_single_choose" 30 }, 31 bundleName: "com.ohos.contacts", 32 abilityName: "com.ohos.contacts.MainAbility", 33 } 34 if (arguments.length > 0 && typeof arguments[0] == 'object') { 35 let option = arguments[0]; 36 if (option.isMultiSelect != undefined) { 37 config.parameters.pageFlag = (option.isMultiSelect == true) ? "page_flag_multi_choose" : "page_flag_single_choose"; 38 } 39 } 40 console.log("[picker] contactsPickerSelect config: " + JSON.stringify(config)); 41 result = await globalThis.abilityContext.startAbilityForResult(config, {windowMode: 1}); 42 } else { 43 let parameter = { 44 want: { 45 parameters: { 46 'pageFlag': "page_flag_single_choose" 47 }, 48 bundleName: "com.ohos.contacts", 49 abilityName: "com.ohos.contacts.MainAbility", 50 }, 51 } 52 if (arguments.length > 0 && typeof arguments[0] == 'object') { 53 let option = arguments[0]; 54 if (option.isMultiSelect != undefined) { 55 parameter.want.parameters.pageFlag = (option.isMultiSelect == true) ? "page_flag_multi_choose" : "page_flag_single_choose"; 56 } 57 } 58 console.log("[picker] contactsPickerSelect parameter: " + JSON.stringify(parameter)); 59 result = await featureAbility.startAbilityForResult(parameter); 60 } 61 let contactObject = result.want.parameters.contactObjects; 62 let params = []; 63 try { 64 params = JSON.parse(contactObject); 65 } catch (Error) { 66 console.log(`[picker] JSON.parse Error: ${Error}`); 67 } 68 let contacts = []; 69 for (let object of params) { 70 let contact = { 71 name: { 72 fullName: object.contactName 73 }, 74 phoneNumbers: [{ 75 phoneNumber: object.telephone 76 }] 77 }; 78 contacts.push(contact); 79 } 80 if (arguments.length == 2 && typeof arguments[1] == "function") { 81 if (result.resultCode == 0) { 82 return arguments[1](undefined, contacts); 83 } else { 84 return arguments[1](result.resultCode, undefined); 85 } 86 } else if (arguments.length == 1 && typeof arguments[0] == "function") { 87 if (result.resultCode == 0) { 88 return arguments[0](undefined, contacts); 89 } else { 90 return arguments[0](result.resultCode, undefined); 91 } 92 } 93 return new Promise((resolve, reject) => { 94 if (result.resultCode == 0) { 95 resolve(contacts); 96 } else { 97 console.log("[picker] contactsPickerSelect err, result.resultCode = : " + result.resultCode); 98 reject(result.resultCode); 99 } 100 }) 101 } catch (error) { 102 console.log("[picker] contactsPickerSelect error: " + error); 103 } 104} 105 106export default { 107 selectContact: contactsPickerSelect, 108 addContact: contact.addContact, 109 deleteContact: contact.deleteContact, 110 queryContact: contact.queryContact, 111 queryContacts: contact.queryContacts, 112 queryContactsByEmail: contact.queryContactsByEmail, 113 queryContactsByPhoneNumber: contact.queryContactsByPhoneNumber, 114 queryGroups: contact.queryGroups, 115 queryHolders: contact.queryHolders, 116 queryKey: contact.queryKey, 117 queryMyCard: contact.queryMyCard, 118 updateContact: contact.updateContact, 119 isLocalContact: contact.isLocalContact, 120 isMyCard: contact.isMyCard, 121 122 Contact: contact.Contact, 123 ContactAttributes: contact.ContactAttributes, 124 Attribute: contact.Attribute, 125 Email: contact.Email, 126 Event: contact.Event, 127 Group: contact.Group, 128 Holder: contact.Holder, 129 ImAddress: contact.ImAddress, 130 Name: contact.Name, 131 NickName: contact.NickName, 132 Note: contact.Note, 133 Organization: contact.Organization, 134 PhoneNumber: contact.PhoneNumber, 135 Portrait: contact.Portrait, 136 PostalAddress: contact.PostalAddress, 137 Relation: contact.Relation, 138 SipAddress: contact.SipAddress, 139 Website: contact.Website 140} 141