diff --git a/framework/src/com/phonegap/ContactAccessorSdk5.java b/framework/src/com/phonegap/ContactAccessorSdk5.java index b4ab52a6..55cbf5ee 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk5.java +++ b/framework/src/com/phonegap/ContactAccessorSdk5.java @@ -55,7 +55,6 @@ import android.webkit.WebView; */ public class ContactAccessorSdk5 extends ContactAccessor { - private static final String WHERE_STRING = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; private static final Map dbMap = new HashMap(); static { dbMap.put("id", ContactsContract.Contacts._ID); @@ -142,59 +141,109 @@ public class ContactAccessorSdk5 extends ContactAccessor { Iterator it = contactIds.iterator(); JSONArray contacts = new JSONArray(); - JSONObject contact; + String contactId; int pos = 0; - String[] events = null; + boolean firstRow = true; while (it.hasNext() && (pos < limit)) { - contact = new JSONObject(); + JSONObject contact = new JSONObject(); + JSONArray organizations = new JSONArray(); + JSONArray addresses = new JSONArray(); + JSONArray phones = new JSONArray(); + JSONArray emails = new JSONArray(); + JSONArray ims = new JSONArray(); + JSONArray websites = new JSONArray(); + JSONArray relationships = new JSONArray(); + contactId = it.next(); - try { - contact.put("id", contactId); - if (isRequired("displayName",populate)) { - contact.put("displayName", displayNameQuery(cr, contactId)); + Cursor c = cr.query(ContactsContract.Data.CONTENT_URI, + null, + ContactsContract.Data.CONTACT_ID + " = ?", + new String[] {contactId}, + null); + + String mimetype = ""; + while (c.moveToNext()) { + try { + if (firstRow) { + firstRow = false; + contact.put("id", contactId); + contact.put("displayName", c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))); + } + mimetype = c.getString(c.getColumnIndex(ContactsContract.Data.MIMETYPE)); + if (mimetype.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) + && isRequired("name",populate)) { + contact.put("name", nameQuery(c)); + } + if (mimetype.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) + && isRequired("phoneNumbers",populate)) { + phones.put(phoneQuery(c)); + } + if (mimetype.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) + && isRequired("emails",populate)) { + emails.put(emailQuery(c)); + } + if (mimetype.equals(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE) + && isRequired("addresses",populate)) { + addresses.put(addressQuery(c)); + } + if (mimetype.equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE) + && isRequired("organizations",populate)) { + organizations.put(organizationQuery(c)); + } + if (mimetype.equals(ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE) + && isRequired("ims",populate)) { + ims.put(imQuery(c)); + } + if (mimetype.equals(ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE) + && isRequired("note",populate)) { + contact.put("note",c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE))); + } + if (mimetype.equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE) + && isRequired("nickname",populate)) { + contact.put("nickname",c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME))); + } + if (mimetype.equals(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE) + && isRequired("urls",populate)) { + websites.put(websiteQuery(c)); + } + if (mimetype.equals(ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE) + && isRequired("relationships",populate)) { + relationships.put(relationshipQuery(c)); + } + if (mimetype.equals(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)) { + if (ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY == c.getInt(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE)) + && isRequired("anniversary",populate)) { + contact.put("anniversary", c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE))); + } + else if (ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY == c.getInt(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE)) + && isRequired("birthday",populate)) { + contact.put("birthday", c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE))); + } + } } - if (isRequired("name",populate)) { - contact.put("name", nameQuery(cr, contactId)); + catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage(),e); } - if (isRequired("phoneNumbers",populate)) { - contact.put("phoneNumbers", phoneQuery(cr, contactId)); - } - if (isRequired("emails",populate)) { - contact.put("emails", emailQuery(cr, contactId)); - } - if (isRequired("addresses",populate)) { - contact.put("addresses", addressQuery(cr, contactId)); - } - if (isRequired("organizations",populate)) { - contact.put("organizations", organizationQuery(cr, contactId)); - } - if (isRequired("ims",populate)) { - contact.put("ims",imQuery(cr, contactId)); - } - if (isRequired("note",populate)) { - contact.put("note",noteQuery(cr, contactId)); - } - if (isRequired("nickname",populate)) { - contact.put("nickname",nicknameQuery(cr, contactId)); - } - if (isRequired("urls",populate)) { - contact.put("urls",websiteQuery(cr, contactId)); - } - if (isRequired("relationships",populate)) { - contact.put("relationships",relationshipQuery(cr, contactId)); - } - if (isRequired("birthday",populate) || isRequired("anniversary",populate)) { - events = eventQuery(cr, contactId); - contact.put("birthday",events[0]); - contact.put("anniversary",events[1]); - } - } catch (JSONException e) { - Log.e(LOG_TAG, e.getMessage(), e); } - Log.d(LOG_TAG, "putting in contact ID = " + contactId); - + c.close(); + + firstRow = true; + + // Populate the Contact object with it's arrays + try { + contact.put("organizations", organizations); + contact.put("addresses", addresses); + contact.put("phoneNumbers", phones); + contact.put("emails", emails); + contact.put("ims", ims); + contact.put("websites", websites); + contact.put("relationships", relationships); + } + catch (JSONException e) { + Log.e(LOG_TAG,e.getMessage(),e); + } contacts.put(contact); pos++; } @@ -215,7 +264,7 @@ public class ContactAccessorSdk5 extends ContactAccessor { new String[] {searchTerm}); return contactIds; } - + String key; try { for (int i=0; i