diff --git a/framework/src/com/phonegap/ContactAccessor.java b/framework/src/com/phonegap/ContactAccessor.java index b2a1843c..e276118c 100644 --- a/framework/src/com/phonegap/ContactAccessor.java +++ b/framework/src/com/phonegap/ContactAccessor.java @@ -19,11 +19,14 @@ package com.phonegap; import java.lang.reflect.Constructor; +import java.util.HashMap; import android.app.Activity; +import android.util.Log; import android.webkit.WebView; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; /** @@ -82,6 +85,65 @@ public abstract class ContactAccessor { return sInstance; } + + protected boolean isRequired(String key, HashMap map) { + Boolean retVal = map.get(key); + return (retVal == null) ? false : retVal.booleanValue(); + } + + protected HashMap buildPopulationSet(JSONArray filter) { + HashMap map = new HashMap(); + + String key; + try { + for (int i=0; i contactIds = buildSetOfContactIds(filter, searchTerm); - + HashMap populate = buildPopulationSet(filter); + Iterator it = contactIds.iterator(); JSONArray contacts = new JSONArray(); @@ -124,20 +125,27 @@ public class ContactAccessorSdk3_4 extends ContactAccessor { null); cur.moveToFirst(); - // name - contact.put("displayName", cur.getString(cur.getColumnIndex(People.DISPLAY_NAME))); - // phone number - contact.put("phoneNumbers", phoneQuery(cr, contactId)); - // email - contact.put("emails", emailQuery(cr, contactId)); - // addresses - contact.put("addresses", addressQuery(cr, contactId)); - // organizations - contact.put("organizations", organizationQuery(cr, contactId)); - // ims - contact.put("ims", imQuery(cr, contactId)); - // note - contact.put("note", cur.getString(cur.getColumnIndex(People.NOTES))); + if (isRequired("displayName",populate)) { + contact.put("displayName", cur.getString(cur.getColumnIndex(People.DISPLAY_NAME))); + } + 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", cur.getString(cur.getColumnIndex(People.NOTES))); + } // nickname // urls // relationship diff --git a/framework/src/com/phonegap/ContactAccessorSdk5.java b/framework/src/com/phonegap/ContactAccessorSdk5.java index 8aa2b067..b4ab52a6 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk5.java +++ b/framework/src/com/phonegap/ContactAccessorSdk5.java @@ -114,10 +114,6 @@ public class ContactAccessorSdk5 extends ContactAccessor { @Override public JSONArray search(JSONArray filter, JSONObject options) { - long totalEnd; - long totalStart = System.currentTimeMillis(); - long start = System.currentTimeMillis(); - long stop; String searchTerm = ""; int limit = Integer.MAX_VALUE; boolean multiple = true; @@ -136,18 +132,13 @@ public class ContactAccessorSdk5 extends ContactAccessor { } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); } - stop = System.currentTimeMillis(); - Log.d(LOG_TAG, "Parsing parameters took = " + (stop-start)); - start = System.currentTimeMillis(); // Get a cursor by creating the query. ContentResolver cr = mApp.getContentResolver(); Set contactIds = buildSetOfContactIds(filter, searchTerm); - - stop = System.currentTimeMillis(); - Log.d(LOG_TAG, "Building contact ID's took = " + (stop-start)); - + HashMap populate = buildPopulationSet(filter); + Iterator it = contactIds.iterator(); JSONArray contacts = new JSONArray(); @@ -161,20 +152,44 @@ public class ContactAccessorSdk5 extends ContactAccessor { try { contact.put("id", contactId); - contact.put("displayName", displayNameQuery(cr, contactId)); - contact.put("name", nameQuery(cr, contactId)); - contact.put("phoneNumbers", phoneQuery(cr, contactId)); - contact.put("emails", emailQuery(cr, contactId)); - contact.put("addresses", addressQuery(cr, contactId)); - contact.put("organizations", organizationQuery(cr, contactId)); - contact.put("ims",imQuery(cr, contactId)); - contact.put("note",noteQuery(cr, contactId)); - contact.put("nickname",nicknameQuery(cr, contactId)); - contact.put("urls",websiteQuery(cr, contactId)); - contact.put("relationships",relationshipQuery(cr, contactId)); - events = eventQuery(cr, contactId); - contact.put("birthday",events[0]); - contact.put("anniversary",events[1]); + if (isRequired("displayName",populate)) { + contact.put("displayName", displayNameQuery(cr, contactId)); + } + if (isRequired("name",populate)) { + contact.put("name", nameQuery(cr, contactId)); + } + 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); } @@ -183,13 +198,9 @@ public class ContactAccessorSdk5 extends ContactAccessor { contacts.put(contact); pos++; } - stop = System.currentTimeMillis(); - totalEnd = System.currentTimeMillis(); - Log.d(LOG_TAG, "Populating contact Array took = " + (stop - start)); - Log.d(LOG_TAG, "Total search took = " + (totalEnd - totalStart)); return contacts; } - + private Set buildSetOfContactIds(JSONArray filter, String searchTerm) { Set contactIds = new HashSet();