From 00f798bffcfb860df6a7a65426a3b03457461202 Mon Sep 17 00:00:00 2001 From: Nixsm Date: Wed, 17 May 2017 10:44:29 -0300 Subject: [PATCH 1/9] Add Address class --- .../src/main/java/com/github/tamir7/contacts/Address.java | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 contacts/src/main/java/com/github/tamir7/contacts/Address.java 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..d44a184 --- /dev/null +++ b/contacts/src/main/java/com/github/tamir7/contacts/Address.java @@ -0,0 +1,8 @@ +package com.github.tamir7.contacts; + +/** + * Created by nicholas on 17/05/17. + */ + +public class Address { +} From e321ff4889688e9888278cb03827c76b8b5c5005 Mon Sep 17 00:00:00 2001 From: Nixsm Date: Wed, 17 May 2017 10:45:40 -0300 Subject: [PATCH 2/9] Implement Address class --- .../com/github/tamir7/contacts/Address.java | 148 +++++++++++++++++- 1 file changed, 146 insertions(+), 2 deletions(-) diff --git a/contacts/src/main/java/com/github/tamir7/contacts/Address.java b/contacts/src/main/java/com/github/tamir7/contacts/Address.java index d44a184..1fb5111 100644 --- a/contacts/src/main/java/com/github/tamir7/contacts/Address.java +++ b/contacts/src/main/java/com/github/tamir7/contacts/Address.java @@ -1,8 +1,152 @@ package com.github.tamir7.contacts; +import android.provider.ContactsContract; + /** - * Created by nicholas on 17/05/17. + * 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; + } + } From dd660e9c6ade68bb2ebbc497cf6b9a9cb4fb6391 Mon Sep 17 00:00:00 2001 From: Nixsm Date: Wed, 17 May 2017 10:49:37 -0300 Subject: [PATCH 3/9] Add Addresses fields and address field --- .../com/github/tamir7/contacts/Contact.java | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) 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 c27b4f6..57f4855 100644 --- a/contacts/src/main/java/com/github/tamir7/contacts/Contact.java +++ b/contacts/src/main/java/com/github/tamir7/contacts/Contact.java @@ -36,18 +36,20 @@ public final class Contact { private String photoUri; private final Set emails = new HashSet<>(); private final Set events = 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, @@ -69,7 +71,23 @@ public enum Field implements AbstractField { EventType(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Event.TYPE), EventLabel(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, - ContactsContract.CommonDataKinds.Event.LABEL); + ContactsContract.CommonDataKinds.Event.LABEL), + 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; @@ -154,6 +172,11 @@ Contact addEvent(Event event) { return this; } + Contact addAddress(Address address) { + addresses.add(address); + return this; + } + /** * Gets a the phone contact id. * @@ -245,6 +268,16 @@ public Event getAnniversary() { } + /** + * 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) { if (type.equals(event.getType())) { From 2ea5d14b347afee4221ef6eaff5c3d5cb03444af Mon Sep 17 00:00:00 2001 From: Nixsm Date: Wed, 17 May 2017 10:51:56 -0300 Subject: [PATCH 4/9] Add getAddress method --- .../github/tamir7/contacts/CursorHelper.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) 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 68c7d0b..f0cd1f9 100644 --- a/contacts/src/main/java/com/github/tamir7/contacts/CursorHelper.java +++ b/contacts/src/main/java/com/github/tamir7/contacts/CursorHelper.java @@ -46,6 +46,30 @@ String getFamilyName() { return getString(c, ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME); } + 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) { From 309702e796c8ac1724df6e30ee505c1dcceb68a7 Mon Sep 17 00:00:00 2001 From: Nixsm Date: Wed, 17 May 2017 10:53:39 -0300 Subject: [PATCH 5/9] Add address fetch to updateContact method --- contacts/src/main/java/com/github/tamir7/contacts/Query.java | 5 +++++ 1 file changed, 5 insertions(+) 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 700bd5c..6378d45 100644 --- a/contacts/src/main/java/com/github/tamir7/contacts/Query.java +++ b/contacts/src/main/java/com/github/tamir7/contacts/Query.java @@ -301,6 +301,11 @@ private void updateContact(Contact contact, CursorHelper helper) { if (familyName != null) { contact.addFamilyName(familyName); } + } else if (mimeType.equals(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) { + Address address = helper.getAddress(); + if (address != null) { + contact.addAddress(address); + } } } From d388e299e7cfe7ed4ff9271987cf53d01979cf4d Mon Sep 17 00:00:00 2001 From: Nixsm Date: Wed, 17 May 2017 11:27:39 -0300 Subject: [PATCH 6/9] Add website field to Field enum --- .../src/main/java/com/github/tamir7/contacts/Contact.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 c27b4f6..210591c 100644 --- a/contacts/src/main/java/com/github/tamir7/contacts/Contact.java +++ b/contacts/src/main/java/com/github/tamir7/contacts/Contact.java @@ -69,7 +69,9 @@ public enum Field implements AbstractField { EventType(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Event.TYPE), EventLabel(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, - ContactsContract.CommonDataKinds.Event.LABEL); + ContactsContract.CommonDataKinds.Event.LABEL), + Website(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE, + ContactsContract.CommonDataKinds.Website.URL); private final String column; private final String mimeType; From 13a877b5b89a20083334c79e09fe3387eff7ad77 Mon Sep 17 00:00:00 2001 From: Nixsm Date: Wed, 17 May 2017 11:40:03 -0300 Subject: [PATCH 7/9] Add website field plus getters and setters --- .../java/com/github/tamir7/contacts/Contact.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 210591c..faf00b5 100644 --- a/contacts/src/main/java/com/github/tamir7/contacts/Contact.java +++ b/contacts/src/main/java/com/github/tamir7/contacts/Contact.java @@ -36,9 +36,11 @@ public final class Contact { private String photoUri; private final Set emails = new HashSet<>(); private final Set events = new HashSet<>(); + private final Set websites = new HashSet<>(); interface AbstractField { String getMimeType(); + String getColumn(); } @@ -156,6 +158,11 @@ Contact addEvent(Event event) { return this; } + Contact addWebsite(String website) { + websites.add(website); + return this; + } + /** * Gets a the phone contact id. * @@ -247,6 +254,15 @@ public Event getAnniversary() { } + /** + * 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()])); + } + private Event getEvent(Event.Type type) { for (Event event: events) { if (type.equals(event.getType())) { From 71cb989c1fab8c324461f211cf107b7bcba3198c Mon Sep 17 00:00:00 2001 From: Nixsm Date: Wed, 17 May 2017 11:40:38 -0300 Subject: [PATCH 8/9] Add getWebsite method --- .../main/java/com/github/tamir7/contacts/CursorHelper.java | 4 ++++ 1 file changed, 4 insertions(+) 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 68c7d0b..9b35059 100644 --- a/contacts/src/main/java/com/github/tamir7/contacts/CursorHelper.java +++ b/contacts/src/main/java/com/github/tamir7/contacts/CursorHelper.java @@ -46,6 +46,10 @@ String getFamilyName() { return getString(c, ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME); } + String getWebsite() { + return getString(c, ContactsContract.CommonDataKinds.Website.URL); + } + PhoneNumber getPhoneNumber() { String number = getString(c, ContactsContract.CommonDataKinds.Phone.NUMBER); if (number == null) { From 1f3e96788f9a98abe8c7c8f53222d7b5509d931d Mon Sep 17 00:00:00 2001 From: Nixsm Date: Wed, 17 May 2017 11:45:52 -0300 Subject: [PATCH 9/9] Add website fetch to updateContact method --- contacts/src/main/java/com/github/tamir7/contacts/Query.java | 5 +++++ 1 file changed, 5 insertions(+) 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 700bd5c..4968d62 100644 --- a/contacts/src/main/java/com/github/tamir7/contacts/Query.java +++ b/contacts/src/main/java/com/github/tamir7/contacts/Query.java @@ -301,6 +301,11 @@ private void updateContact(Contact contact, CursorHelper helper) { if (familyName != null) { contact.addFamilyName(familyName); } + } else if (mimeType.equals(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)) { + String website = helper.getWebsite(); + if (website != null) { + contact.addWebsite(website); + } } }