Skip to content

Commit

Permalink
Enable the return of photos in a Contact object
Browse files Browse the repository at this point in the history
  • Loading branch information
macdonst committed Jan 19, 2011
1 parent f090f9a commit 03ea8a0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
3 changes: 3 additions & 0 deletions framework/src/com/phonegap/ContactAccessor.java
Expand Up @@ -155,6 +155,9 @@ else if (key.startsWith("relationships")) {
else if (key.startsWith("urls")) {
map.put("urls", true);
}
else if (key.startsWith("photos")) {
map.put("photos", true);
}
}
}
catch (JSONException e) {
Expand Down
39 changes: 34 additions & 5 deletions framework/src/com/phonegap/ContactAccessorSdk5.java
Expand Up @@ -39,9 +39,11 @@
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.ContentProviderOperation;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.util.Log;
Expand Down Expand Up @@ -108,7 +110,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
//dbMap.put("gender", null);
dbMap.put("note", ContactsContract.CommonDataKinds.Note.NOTE);
//dbMap.put("preferredUsername", null);
//dbMap.put("photos.value", null);
dbMap.put("photos.value", ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
//dbMap.put("tags.value", null);
dbMap.put("relationships", ContactsContract.CommonDataKinds.Relation.NAME);
dbMap.put("relationships.value", ContactsContract.CommonDataKinds.Relation.NAME);
Expand Down Expand Up @@ -213,7 +215,8 @@ public JSONArray search(JSONArray fields, JSONObject options) {
JSONArray emails = new JSONArray();
JSONArray ims = new JSONArray();
JSONArray websites = new JSONArray();
JSONArray relationships = new JSONArray();
JSONArray relationships = new JSONArray();
JSONArray photos = new JSONArray();

if (c.getCount() > 0) {
while (c.moveToNext() && (contacts.length() <= (limit-1))) {
Expand All @@ -232,7 +235,7 @@ public JSONArray search(JSONArray fields, JSONObject options) {
// Populate the Contact object with it's arrays
// and push the contact into the contacts array
contacts.put(populateContact(contact, organizations, addresses, phones,
emails, ims, websites, relationships));
emails, ims, websites, relationships, photos));

// Clean up the objects
contact = new JSONObject();
Expand All @@ -243,6 +246,7 @@ public JSONArray search(JSONArray fields, JSONObject options) {
ims = new JSONArray();
websites = new JSONArray();
relationships = new JSONArray();
photos = new JSONArray();

// Set newContact to true as we are starting to populate a new contact
newContact = true;
Expand Down Expand Up @@ -310,6 +314,10 @@ && isRequired("birthday",populate)) {
contact.put("birthday", c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE)));
}
}
else if (mimetype.equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
&& isRequired("photos",populate)) {
photos.put(photoQuery(contactId));
}
}
catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(),e);
Expand All @@ -321,7 +329,7 @@ && isRequired("birthday",populate)) {

// Push the last contact into the contacts array
contacts.put(populateContact(contact, organizations, addresses, phones,
emails, ims, websites, relationships));
emails, ims, websites, relationships, photos));
}
c.close();

Expand Down Expand Up @@ -376,11 +384,12 @@ private WhereOptions buildIdClause(Set<String> contactIds, String searchTerm) {
* @param ims array of instant messenger addresses
* @param websites array of websites
* @param relationships array of relationships
* @param photos
* @return
*/
private JSONObject populateContact(JSONObject contact, JSONArray organizations,
JSONArray addresses, JSONArray phones, JSONArray emails,
JSONArray ims, JSONArray websites, JSONArray relationships) {
JSONArray ims, JSONArray websites, JSONArray relationships, JSONArray photos) {
try {
contact.put("organizations", organizations);
contact.put("addresses", addresses);
Expand All @@ -389,6 +398,7 @@ private JSONObject populateContact(JSONObject contact, JSONArray organizations,
contact.put("ims", ims);
contact.put("websites", websites);
contact.put("relationships", relationships);
contact.put("photos", photos);
}
catch (JSONException e) {
Log.e(LOG_TAG,e.getMessage(),e);
Expand Down Expand Up @@ -696,6 +706,25 @@ private JSONObject relationshipQuery(Cursor cursor) {
return relationship;
}

/**
* Create a ContactField JSONObject
* @param contactId
* @return a JSONObject representing a ContactField
*/
private JSONObject photoQuery(String contactId) {
JSONObject photo = new JSONObject();
try {
photo.put("id", contactId);
photo.put("primary", false);
Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, (new Long(contactId)));
Uri photoUri = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
photo.put("value", photoUri.toString());
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
}
return photo;
}

@Override
/**
* This method will save a contact object into the devices contacts database.
Expand Down

0 comments on commit 03ea8a0

Please sign in to comment.