mirror of
https://github.com/apache/cordova-android.git
synced 2025-04-22 08:36:25 +08:00
Speeding up contacts.find
This commit is contained in:
parent
f20e5cf943
commit
3138178fea
@ -19,11 +19,14 @@
|
|||||||
package com.phonegap;
|
package com.phonegap;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.util.Log;
|
||||||
import android.webkit.WebView;
|
import android.webkit.WebView;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -83,6 +86,65 @@ public abstract class ContactAccessor {
|
|||||||
return sInstance;
|
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.
|
* Handles adding a JSON Contact object into the database.
|
||||||
*/
|
*/
|
||||||
|
@ -102,6 +102,7 @@ public class ContactAccessorSdk3_4 extends ContactAccessor {
|
|||||||
ContentResolver cr = mApp.getContentResolver();
|
ContentResolver cr = mApp.getContentResolver();
|
||||||
|
|
||||||
Set<String> contactIds = buildSetOfContactIds(filter, searchTerm);
|
Set<String> contactIds = buildSetOfContactIds(filter, searchTerm);
|
||||||
|
HashMap<String,Boolean> populate = buildPopulationSet(filter);
|
||||||
|
|
||||||
Iterator<String> it = contactIds.iterator();
|
Iterator<String> it = contactIds.iterator();
|
||||||
|
|
||||||
@ -124,20 +125,27 @@ public class ContactAccessorSdk3_4 extends ContactAccessor {
|
|||||||
null);
|
null);
|
||||||
cur.moveToFirst();
|
cur.moveToFirst();
|
||||||
|
|
||||||
// name
|
if (isRequired("displayName",populate)) {
|
||||||
contact.put("displayName", cur.getString(cur.getColumnIndex(People.DISPLAY_NAME)));
|
contact.put("displayName", cur.getString(cur.getColumnIndex(People.DISPLAY_NAME)));
|
||||||
// phone number
|
}
|
||||||
contact.put("phoneNumbers", phoneQuery(cr, contactId));
|
if (isRequired("phoneNumbers",populate)) {
|
||||||
// email
|
contact.put("phoneNumbers", phoneQuery(cr, contactId));
|
||||||
contact.put("emails", emailQuery(cr, contactId));
|
}
|
||||||
// addresses
|
if (isRequired("emails",populate)) {
|
||||||
contact.put("addresses", addressQuery(cr, contactId));
|
contact.put("emails", emailQuery(cr, contactId));
|
||||||
// organizations
|
}
|
||||||
contact.put("organizations", organizationQuery(cr, contactId));
|
if (isRequired("addresses",populate)) {
|
||||||
// ims
|
contact.put("addresses", addressQuery(cr, contactId));
|
||||||
contact.put("ims", imQuery(cr, contactId));
|
}
|
||||||
// note
|
if (isRequired("organizations",populate)) {
|
||||||
contact.put("note", cur.getString(cur.getColumnIndex(People.NOTES)));
|
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
|
// nickname
|
||||||
// urls
|
// urls
|
||||||
// relationship
|
// relationship
|
||||||
|
@ -114,10 +114,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JSONArray search(JSONArray filter, JSONObject options) {
|
public JSONArray search(JSONArray filter, JSONObject options) {
|
||||||
long totalEnd;
|
|
||||||
long totalStart = System.currentTimeMillis();
|
|
||||||
long start = System.currentTimeMillis();
|
|
||||||
long stop;
|
|
||||||
String searchTerm = "";
|
String searchTerm = "";
|
||||||
int limit = Integer.MAX_VALUE;
|
int limit = Integer.MAX_VALUE;
|
||||||
boolean multiple = true;
|
boolean multiple = true;
|
||||||
@ -136,17 +132,12 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
Log.e(LOG_TAG, e.getMessage(), 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.
|
// Get a cursor by creating the query.
|
||||||
ContentResolver cr = mApp.getContentResolver();
|
ContentResolver cr = mApp.getContentResolver();
|
||||||
|
|
||||||
Set<String> contactIds = buildSetOfContactIds(filter, searchTerm);
|
Set<String> contactIds = buildSetOfContactIds(filter, searchTerm);
|
||||||
|
HashMap<String,Boolean> populate = buildPopulationSet(filter);
|
||||||
stop = System.currentTimeMillis();
|
|
||||||
Log.d(LOG_TAG, "Building contact ID's took = " + (stop-start));
|
|
||||||
|
|
||||||
Iterator<String> it = contactIds.iterator();
|
Iterator<String> it = contactIds.iterator();
|
||||||
|
|
||||||
@ -161,20 +152,44 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
contact.put("id", contactId);
|
contact.put("id", contactId);
|
||||||
contact.put("displayName", displayNameQuery(cr, contactId));
|
if (isRequired("displayName",populate)) {
|
||||||
contact.put("name", nameQuery(cr, contactId));
|
contact.put("displayName", displayNameQuery(cr, contactId));
|
||||||
contact.put("phoneNumbers", phoneQuery(cr, contactId));
|
}
|
||||||
contact.put("emails", emailQuery(cr, contactId));
|
if (isRequired("name",populate)) {
|
||||||
contact.put("addresses", addressQuery(cr, contactId));
|
contact.put("name", nameQuery(cr, contactId));
|
||||||
contact.put("organizations", organizationQuery(cr, contactId));
|
}
|
||||||
contact.put("ims",imQuery(cr, contactId));
|
if (isRequired("phoneNumbers",populate)) {
|
||||||
contact.put("note",noteQuery(cr, contactId));
|
contact.put("phoneNumbers", phoneQuery(cr, contactId));
|
||||||
contact.put("nickname",nicknameQuery(cr, contactId));
|
}
|
||||||
contact.put("urls",websiteQuery(cr, contactId));
|
if (isRequired("emails",populate)) {
|
||||||
contact.put("relationships",relationshipQuery(cr, contactId));
|
contact.put("emails", emailQuery(cr, contactId));
|
||||||
events = eventQuery(cr, contactId);
|
}
|
||||||
contact.put("birthday",events[0]);
|
if (isRequired("addresses",populate)) {
|
||||||
contact.put("anniversary",events[1]);
|
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) {
|
} catch (JSONException e) {
|
||||||
Log.e(LOG_TAG, e.getMessage(), e);
|
Log.e(LOG_TAG, e.getMessage(), e);
|
||||||
}
|
}
|
||||||
@ -183,10 +198,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
contacts.put(contact);
|
contacts.put(contact);
|
||||||
pos++;
|
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;
|
return contacts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user