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