diff --git a/contacts/src/main/java/com/github/tamir7/contacts/Address.java b/contacts/src/main/java/com/github/tamir7/contacts/Address.java new file mode 100644 index 0000000..1fb5111 --- /dev/null +++ b/contacts/src/main/java/com/github/tamir7/contacts/Address.java @@ -0,0 +1,152 @@ +package com.github.tamir7.contacts; + +import android.provider.ContactsContract; + +/** + * Represents an Address + */ +public class Address { + private final String formattedAddress; + private final Type type; + private final String street; + private final String city; + private final String region; + private final String postcode; + private final String country; + private final String label; + + + public enum Type { + CUSTOM, + HOME, + WORK, + OTHER, + UNKNOWN; + + static Type fromValue(int value) { + switch (value) { + case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_CUSTOM: + return CUSTOM; + case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME: + return HOME; + case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK: + return WORK; + case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_OTHER: + return OTHER; + default: + return UNKNOWN; + } + } + } + + private Address(String address, + String street, + String city, + String region, + String postcode, + String country, + Type type, + String label) { + this.formattedAddress = address; + this.street = street; + this.city = city; + this.region = region; + this.postcode = postcode; + this.country = country; + this.type = type; + this.label = label; + } + + Address(String address, + String street, + String city, + String region, + String postcode, + String country, + Type type) { + this(address, street, city, region, postcode, country, type, null); + } + + Address(String address, + String street, + String city, + String region, + String postcode, + String country, + String label) { + this(address, street, city, region, postcode, country, Type.CUSTOM, label); + } + + /** + * Gets the formatted address + * + * @return Formatted address + */ + public String getFormattedAddress() { + return formattedAddress; + } + + /** + * Gets the type of the address + * + * @return type of address + */ + public Type getType() { + return type; + } + + /** + * Gets the street name + * + * @return street name + */ + public String getStreet() { + return street; + } + + /** + * Gets the city name + * + * @return city name + */ + public String getCity() { + return city; + } + + /** + * Gets the region name + * + * @return region name + */ + public String getRegion() { + return region; + } + + /** + * Gets the post code + * + * @return post code + */ + public String getPostcode() { + return postcode; + } + + /** + * Gets the country of the address + * + * @return country of the address + */ + public String getCountry() { + return country; + } + + /** + * The label of the address. (null unless type = TYPE_CUSTOM) + * + * @return the label of the address + */ + public String getLabel() { + return label; + } + +} diff --git a/contacts/src/main/java/com/github/tamir7/contacts/Contact.java b/contacts/src/main/java/com/github/tamir7/contacts/Contact.java index 0b1218d..18af308 100644 --- a/contacts/src/main/java/com/github/tamir7/contacts/Contact.java +++ b/contacts/src/main/java/com/github/tamir7/contacts/Contact.java @@ -39,18 +39,21 @@ public final class Contact { private final Set events = new HashSet<>(); private String companyName; private String companyTitle; + private final Set websites = new HashSet<>(); + private final Set
addresses = new HashSet<>(); interface AbstractField { String getMimeType(); + String getColumn(); } public enum Field implements AbstractField { DisplayName(null, ContactsContract.Data.DISPLAY_NAME), GivenName(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, - ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME), + ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME), FamilyName(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, - ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME), + ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME), PhoneNumber(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Phone.NUMBER), PhoneType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, @@ -76,7 +79,25 @@ public enum Field implements AbstractField { CompanyName(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Organization.COMPANY), CompanyTitle(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE, - ContactsContract.CommonDataKinds.Organization.TITLE); + ContactsContract.CommonDataKinds.Organization.TITLE), + Website(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE, + ContactsContract.CommonDataKinds.Website.URL); + Address(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, + ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS), + AddressType(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, + ContactsContract.CommonDataKinds.StructuredPostal.TYPE), + AddressStreet(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, + ContactsContract.CommonDataKinds.StructuredPostal.STREET), + AddressCity(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, + ContactsContract.CommonDataKinds.StructuredPostal.CITY), + AddressRegion(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, + ContactsContract.CommonDataKinds.StructuredPostal.REGION), + AddressPostcode(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, + ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE), + AddressCountry(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, + ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY), + AddressLabel(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, + ContactsContract.CommonDataKinds.StructuredPostal.LABEL); private final String column; private final String mimeType; @@ -171,6 +192,16 @@ Contact addCompanyTitle(String companyTitle) { return this; } + Contact addWebsite(String website) { + websites.add(website); + return this; + } + + Contact addAddress(Address address) { + addresses.add(address); + return this; + } + /** * Gets a the phone contact id. * @@ -279,6 +310,24 @@ public String getCompanyName() { public String getCompanyTitle() { return companyTitle; } + + /** + * Gets the list of all websites the contact has + * + * @return A list of websites + */ + public List getWebsites() { + return Arrays.asList(websites.toArray(new String[websites.size()])); + } + + /** + * Gets the list of addresses + * + * @return A list of addresses + */ + public List
getAddresses() { + return Arrays.asList(addresses.toArray(new Address[addresses.size()])); + } private Event getEvent(Event.Type type) { for (Event event: events) { diff --git a/contacts/src/main/java/com/github/tamir7/contacts/CursorHelper.java b/contacts/src/main/java/com/github/tamir7/contacts/CursorHelper.java index 5015e2a..4b0542d 100644 --- a/contacts/src/main/java/com/github/tamir7/contacts/CursorHelper.java +++ b/contacts/src/main/java/com/github/tamir7/contacts/CursorHelper.java @@ -54,6 +54,33 @@ String getCompanyTitle() { return getString(c, ContactsContract.CommonDataKinds.Organization.TITLE); } + String getWebsite() { + return getString(c, ContactsContract.CommonDataKinds.Website.URL); + } + + Address getAddress() { + String address = getString(c, ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS); + if (address == null) { + return null; + } + + Integer typeValue = getInt(c, ContactsContract.CommonDataKinds.StructuredPostal.TYPE); + Address.Type type = typeValue == null ? Address.Type.UNKNOWN : Address.Type.fromValue(typeValue); + + String street = getString(c, ContactsContract.CommonDataKinds.StructuredPostal.STREET); + String city = getString(c, ContactsContract.CommonDataKinds.StructuredPostal.CITY); + String region = getString(c, ContactsContract.CommonDataKinds.StructuredPostal.REGION); + String postcode = getString(c, ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE); + String country = getString(c, ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY); + + if (!type.equals(Address.Type.CUSTOM)) { + return new Address(address, street, city, region, postcode, country, type); + } + + String label = getString(c, ContactsContract.CommonDataKinds.StructuredPostal.LABEL); + return new Address(address, street, city, region, postcode, country, label); + } + PhoneNumber getPhoneNumber() { String number = getString(c, ContactsContract.CommonDataKinds.Phone.NUMBER); if (number == null) { diff --git a/contacts/src/main/java/com/github/tamir7/contacts/Query.java b/contacts/src/main/java/com/github/tamir7/contacts/Query.java index 3241d03..77e5deb 100644 --- a/contacts/src/main/java/com/github/tamir7/contacts/Query.java +++ b/contacts/src/main/java/com/github/tamir7/contacts/Query.java @@ -313,6 +313,16 @@ private void updateContact(Contact contact, CursorHelper helper) { if (companyTitle != null) { contact.addCompanyTitle(companyTitle); } + } else if (mimeType.equals(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)) { + String website = helper.getWebsite(); + if (website != null) { + contact.addWebsite(website); + } + } else if (mimeType.equals(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) { + Address address = helper.getAddress(); + if (address != null) { + contact.addAddress(address); + } } }