capacitor-community / contacts

Contacts Plugin for Capacitor
https://capacitor-community.github.io/contacts/
119 stars 52 forks source link

(Save Contact) support android types of email, phone, address and save website #66

Closed alexneighbournew closed 1 year ago

alexneighbournew commented 2 years ago

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);

        if (emailAddresses.size() > 1) {
            JSObject secondaryEmail = JSObject.fromJSONObject((JSONObject) emailAddresses.get(1));
            intent.putExtra(ContactsContract.Intents.Insert.SECONDARY_EMAIL, secondaryEmail.getString("address", ""));
            intent.putExtra(ContactsContract.Intents.Insert.SECONDARY_EMAIL_TYPE, secondaryEmail.getInteger("label", Email.TYPE_HOME));

            if (emailAddresses.size() > 2) {
                JSObject tertiaryEmail = JSObject.fromJSONObject((JSONObject) emailAddresses.get(2));
                intent.putExtra(ContactsContract.Intents.Insert.TERTIARY_EMAIL, tertiaryEmail.getString("address", ""));
                intent.putExtra(ContactsContract.Intents.Insert.TERTIARY_EMAIL_TYPE, tertiaryEmail.getInteger("label", Email.TYPE_HOME));
            }
        }
    }

// 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);

        if (phoneNumbers.size() > 1) {
            JSObject secondaryNumber = JSObject.fromJSONObject((JSONObject) phoneNumbers.get(1));
            intent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, secondaryNumber.getString("number", ""));
            intent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE_TYPE, secondaryNumber.getInteger("label", Phone.TYPE_HOME));

            if (phoneNumbers.size() > 2) {
                JSObject tertiaryNumber = JSObject.fromJSONObject((JSONObject) phoneNumbers.get(2));
                intent.putExtra(ContactsContract.Intents.Insert.TERTIARY_PHONE, tertiaryNumber.getString("number", ""));
                intent.putExtra(ContactsContract.Intents.Insert.TERTIARY_PHONE_TYPE, tertiaryNumber.getInteger("label", Phone.TYPE_HOME));
            }
        }
    }

    // TODO: allow more than one address
    if (addresses.size() > 0) {
        JSObject address = JSObject.fromJSONObject((JSONObject) addresses.get(0));
        JSObject postalObject = address.getJSObject("address", new JSObject());
        ArrayList<String> postalArray = new ArrayList<String>();
        postalArray.add(postalObject.getString("street", ""));
        postalArray.add(postalObject.getString("city", ""));
        postalArray.add(postalObject.getString("state", ""));
        postalArray.add(postalObject.getString("postalCode", ""));
        postalArray.add(postalObject.getString("country", ""));
        postalArray.removeAll(Arrays.asList("", null));
        String postal = TextUtils.join(", ", postalArray);
        intent.putExtra(ContactsContract.Intents.Insert.POSTAL, postal);
        intent.putExtra(ContactsContract.Intents.Insert.POSTAL_TYPE, address.getInteger("label", StructuredPostal.TYPE_HOME));
        intent.putExtra(ContactsContract.Intents.Insert.POSTAL_ISPRIMARY, true);
    }

    // UrlAddresses

  JSArray urlAddressesArray = call.getArray("urlAddresses", new JSArray());
  List<Object> urlAddresses = urlAddressesArray.toList();

  if (urlAddresses.size() > 0) {
    ArrayList<ContentValues> data = new ArrayList<ContentValues>();
    ContentValues rowUrlAddress = new ContentValues();
    JSObject urlAddress = JSObject.fromJSONObject((JSONObject) urlAddresses.get(0));
    rowUrlAddress.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
    rowUrlAddress.put(ContactsContract.CommonDataKinds.Website.URL, urlAddress.getString("url", ""));
    rowUrlAddress.put(ContactsContract.CommonDataKinds.Website.TYPE, urlAddress.getInteger("label", Website.TYPE_HOME));
    data.add(rowUrlAddress);
    intent.putExtra(ContactsContract.Intents.Insert.DATA, data);
  }

}`

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:

Screenshot_20220211_181809 Screenshot_20220211_181844

tafelnl commented 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!