Skip to content

Commit

Permalink
Merge branch 'master' into fetch-company-infos
Browse files Browse the repository at this point in the history
  • Loading branch information
tamir7 committed May 21, 2017
2 parents de527a1 + 14fe800 commit 31d5a1b
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 3 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;
}

}
55 changes: 52 additions & 3 deletions contacts/src/main/java/com/github/tamir7/contacts/Contact.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,21 @@ public final class Contact {
private final Set<Event> events = new HashSet<>();
private String companyName;
private String companyTitle;
private final Set<String> websites = new HashSet<>();
private final Set<Address> 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,
Expand All @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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<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 @@ -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) {
Expand Down
10 changes: 10 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 @@ -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);
}
}
}

Expand Down

0 comments on commit 31d5a1b

Please sign in to comment.