Skip to content

Commit

Permalink
Merge branch 'master' into fetch-websites
Browse files Browse the repository at this point in the history
  • Loading branch information
tamir7 authored May 21, 2017
2 parents 1f3e967 + e4b5324 commit bec2664
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 2 deletions.
152 changes: 152 additions & 0 deletions contacts/src/main/java/com/github/tamir7/contacts/Address.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
34 changes: 32 additions & 2 deletions contacts/src/main/java/com/github/tamir7/contacts/Contact.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public final class Contact {
private final Set<Email> emails = new HashSet<>();
private final Set<Event> events = new HashSet<>();
private final Set<String> websites = new HashSet<>();
private final Set<Address> addresses = new HashSet<>();

interface AbstractField {
String getMimeType();
Expand All @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -262,6 +284,14 @@ public Event getAnniversary() {
public List<String> getWebsites() {
return Arrays.asList(websites.toArray(new String[websites.size()]));
}
/**
* Gets the list of addresses
*
* @return A list of addresses
*/
public List<Address> getAddresses() {
return Arrays.asList(addresses.toArray(new Address[addresses.size()]));
}

private Event getEvent(Event.Type type) {
for (Event event: events) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions contacts/src/main/java/com/github/tamir7/contacts/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit bec2664

Please sign in to comment.