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 faf00b5..9c28d02 100644 --- a/contacts/src/main/java/com/github/tamir7/contacts/Contact.java +++ b/contacts/src/main/java/com/github/tamir7/contacts/Contact.java @@ -37,6 +37,7 @@ public final class Contact { private final Set emails = new HashSet<>(); private final Set events = new HashSet<>(); private final Set websites = new HashSet<>(); + private final Set
addresses = new HashSet<>(); interface AbstractField { String getMimeType(); @@ -47,9 +48,9 @@ interface AbstractField { 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, @@ -74,6 +75,22 @@ public enum Field implements AbstractField { ContactsContract.CommonDataKinds.Event.LABEL), 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; @@ -163,6 +180,11 @@ Contact addWebsite(String website) { return this; } + Contact addAddress(Address address) { + addresses.add(address); + return this; + } + /** * Gets a the phone contact id. * @@ -262,6 +284,14 @@ public Event getAnniversary() { 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 9b35059..bdad1db 100644 --- a/contacts/src/main/java/com/github/tamir7/contacts/CursorHelper.java +++ b/contacts/src/main/java/com/github/tamir7/contacts/CursorHelper.java @@ -50,6 +50,29 @@ 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 4968d62..62b6d6d 100644 --- a/contacts/src/main/java/com/github/tamir7/contacts/Query.java +++ b/contacts/src/main/java/com/github/tamir7/contacts/Query.java @@ -306,6 +306,11 @@ private void updateContact(Contact contact, CursorHelper helper) { 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); + } } }