Speeding up contacts.find

This commit is contained in:
macdonst 2010-10-07 00:43:20 +08:00
parent f20e5cf943
commit 3138178fea
3 changed files with 126 additions and 45 deletions

View File

@ -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<String,Boolean> map) {
Boolean retVal = map.get(key);
return (retVal == null) ? false : retVal.booleanValue();
}
protected HashMap<String,Boolean> buildPopulationSet(JSONArray filter) {
HashMap<String,Boolean> map = new HashMap<String,Boolean>();
String key;
try {
for (int i=0; i<filter.length(); i++) {
key = filter.getString(i);
if (key.startsWith("displayName")) {
map.put("displayName", true);
}
else if (key.startsWith("name")) {
map.put("name", true);
}
else if (key.startsWith("nickname")) {
map.put("nickname", true);
}
else if (key.startsWith("phoneNumbers")) {
map.put("phoneNumbers", true);
}
else if (key.startsWith("emails")) {
map.put("emails", true);
}
else if (key.startsWith("addresses")) {
map.put("addresses", true);
}
else if (key.startsWith("ims")) {
map.put("ims", true);
}
else if (key.startsWith("organizations")) {
map.put("organizations", true);
}
else if (key.startsWith("birthday")) {
map.put("birthday", true);
}
else if (key.startsWith("anniversary")) {
map.put("anniversary", true);
}
else if (key.startsWith("note")) {
map.put("note", true);
}
else if (key.startsWith("relationships")) {
map.put("relationships", true);
}
else if (key.startsWith("urls")) {
map.put("urls", true);
}
}
}
catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
}
return map;
}
/**
* Handles adding a JSON Contact object into the database.

View File

@ -102,7 +102,8 @@ public class ContactAccessorSdk3_4 extends ContactAccessor {
ContentResolver cr = mApp.getContentResolver();
Set<String> contactIds = buildSetOfContactIds(filter, searchTerm);
HashMap<String,Boolean> populate = buildPopulationSet(filter);
Iterator<String> 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

View File

@ -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<String> contactIds = buildSetOfContactIds(filter, searchTerm);
stop = System.currentTimeMillis();
Log.d(LOG_TAG, "Building contact ID's took = " + (stop-start));
HashMap<String,Boolean> populate = buildPopulationSet(filter);
Iterator<String> 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<String> buildSetOfContactIds(JSONArray filter, String searchTerm) {
Set<String> contactIds = new HashSet<String>();