Closed alexneighbournew closed 1 year ago
A new major of this plugin just got released as a beta version. The plugin is refactored quite a bit. Among others, the API and TypeScript definitions got improved.
It also ships your request: you will now be able to add a type
and label
field with each, phone, email or postal address object created.
Check out the newly added docs here: https://capacitor-community.github.io/contacts
Mind you that the new version (v3 and v4) are currently still in beta. However, the beta is tested quite extensively and fairly stable. If you're willing to test it out in your project, it would be much appreciated!
When saving a contact that contains emails, phone numbers, or addresses, the contact is saved with a custom type and not with android's own constants, for example TYPE_HOME, TYPE_WORK, etc.
In addition to this, if a website is sent, the information is not saved and in the case of the address it assumes that street, city, state, zip code, country is sent.
Given this, I made changes to the Contacts.java file that allows you to save the contact information in a better way, I hope it will help you. I show places where I made the changes:
`import android.provider.ContactsContract.CommonDataKinds.Email; import android.provider.ContactsContract.CommonDataKinds.Event; import android.provider.ContactsContract.CommonDataKinds.Organization; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.provider.ContactsContract.CommonDataKinds.Photo; import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; import android.provider.ContactsContract.CommonDataKinds.Website; import android.text.TextUtils; import android.util.Base64; import android.util.Log; import com.getcapacitor.JSArray; import com.getcapacitor.JSObject; import com.getcapacitor.Plugin; import com.getcapacitor.PluginCall; import com.getcapacitor.PluginMethod; import com.getcapacitor.annotation.CapacitorPlugin; import com.getcapacitor.annotation.Permission; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set;
public void saveContact(PluginCall call) throws JSONException { // TODO: allow more than these three email types if (emailAddresses.size() > 0) { JSObject primaryEmail = JSObject.fromJSONObject((JSONObject) emailAddresses.get(0)); intent.putExtra(ContactsContract.Intents.Insert.EMAIL, primaryEmail.getString("address", "")); intent.putExtra(ContactsContract.Intents.Insert.EMAIL_TYPE, primaryEmail.getInteger("label", Email.TYPE_HOME)); intent.putExtra(ContactsContract.Intents.Insert.EMAIL_ISPRIMARY, true);
// TODO: allow more than these three phone number types if (phoneNumbers.size() > 0) { JSObject primaryNumber = JSObject.fromJSONObject((JSONObject) phoneNumbers.get(0)); intent.putExtra(ContactsContract.Intents.Insert.PHONE, primaryNumber.getString("number", "")); intent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, primaryNumber.getInteger("label", Phone.TYPE_HOME)); intent.putExtra(ContactsContract.Intents.Insert.PHONE_ISPRIMARY, true);
}`
export interface PhoneNumber { label?: number; number?: string; } export interface EmailAddress { label?: number; address?: string; } export interface PostalAddress { label?: number; address?: { street?: string; city?: string; state?: string; postalCode?: string; country?: string; }; }
Result with the changes applied in Contacts.java: