forked from github/cordova-android
Small fix to speed up contact retrieval
This commit is contained in:
parent
2bbf62c489
commit
8eaaa04746
@ -104,6 +104,7 @@
|
|||||||
var obj = new ContactFindOptions();
|
var obj = new ContactFindOptions();
|
||||||
obj.filter="";
|
obj.filter="";
|
||||||
obj.multiple=true;
|
obj.multiple=true;
|
||||||
|
obj.limit=5;
|
||||||
navigator.service.contacts.find(["displayName", "phoneNumbers", "emails"], count_contacts, fail, obj);
|
navigator.service.contacts.find(["displayName", "phoneNumbers", "emails"], count_contacts, fail, obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ public abstract class ContactAccessor {
|
|||||||
/**
|
/**
|
||||||
* Handles adding a JSON Contact object into the database.
|
* Handles adding a JSON Contact object into the database.
|
||||||
*/
|
*/
|
||||||
public abstract void save();
|
public abstract void save(JSONObject contact);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles searching through SDK-specific contacts API.
|
* Handles searching through SDK-specific contacts API.
|
||||||
|
@ -361,7 +361,7 @@ public class ContactAccessorSdk3_4 extends ContactAccessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save() {
|
public void save(JSONObject contact) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -107,14 +107,17 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
//dbMap.put("connected", null);
|
//dbMap.put("connected", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ContactAccessorSdk5(WebView view, Activity app)
|
public ContactAccessorSdk5(WebView view, Activity app) {
|
||||||
{
|
|
||||||
mApp = app;
|
mApp = app;
|
||||||
mView = view;
|
mView = view;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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;
|
||||||
@ -133,11 +136,19 @@ 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);
|
||||||
|
|
||||||
|
stop = System.currentTimeMillis();
|
||||||
|
Log.d(LOG_TAG, "Building contact ID's took = " + (stop-start));
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
|
||||||
Iterator<String> it = contactIds.iterator();
|
Iterator<String> it = contactIds.iterator();
|
||||||
|
|
||||||
JSONArray contacts = new JSONArray();
|
JSONArray contacts = new JSONArray();
|
||||||
@ -171,12 +182,28 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<String> buildSetOfContactIds(JSONArray filter, String searchTerm) {
|
private Set<String> buildSetOfContactIds(JSONArray filter, String searchTerm) {
|
||||||
Set<String> contactIds = new HashSet<String>();
|
Set<String> contactIds = new HashSet<String>();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Special case for when the user wants all the contacts
|
||||||
|
*/
|
||||||
|
if ("%".equals(searchTerm)) {
|
||||||
|
doQuery(searchTerm, contactIds,
|
||||||
|
ContactsContract.Contacts.CONTENT_URI,
|
||||||
|
ContactsContract.Contacts._ID,
|
||||||
|
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?",
|
||||||
|
new String[] {searchTerm});
|
||||||
|
return contactIds;
|
||||||
|
}
|
||||||
|
|
||||||
String key;
|
String key;
|
||||||
try {
|
try {
|
||||||
for (int i=0; i<filter.length(); i++) {
|
for (int i=0; i<filter.length(); i++) {
|
||||||
@ -308,7 +335,12 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
String[] orgWhereParams = new String[]{contactId,
|
String[] orgWhereParams = new String[]{contactId,
|
||||||
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
|
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
|
||||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||||
null, WHERE_STRING, orgWhereParams, null);
|
new String[] {ContactsContract.CommonDataKinds.Organization.DEPARTMENT,
|
||||||
|
ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION,
|
||||||
|
ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION,
|
||||||
|
ContactsContract.CommonDataKinds.Organization.COMPANY,
|
||||||
|
ContactsContract.CommonDataKinds.Organization.TITLE},
|
||||||
|
WHERE_STRING, orgWhereParams, null);
|
||||||
JSONArray organizations = new JSONArray();
|
JSONArray organizations = new JSONArray();
|
||||||
JSONObject organization = new JSONObject();
|
JSONObject organization = new JSONObject();
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
@ -335,7 +367,13 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
String[] addrWhereParams = new String[]{contactId,
|
String[] addrWhereParams = new String[]{contactId,
|
||||||
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
|
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
|
||||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||||
null, WHERE_STRING, addrWhereParams, null);
|
new String[] {ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
|
||||||
|
ContactsContract.CommonDataKinds.StructuredPostal.STREET,
|
||||||
|
ContactsContract.CommonDataKinds.StructuredPostal.CITY,
|
||||||
|
ContactsContract.CommonDataKinds.StructuredPostal.REGION,
|
||||||
|
ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE,
|
||||||
|
ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY},
|
||||||
|
WHERE_STRING, addrWhereParams, null);
|
||||||
JSONArray addresses = new JSONArray();
|
JSONArray addresses = new JSONArray();
|
||||||
JSONObject address = new JSONObject();
|
JSONObject address = new JSONObject();
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
@ -359,7 +397,12 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
String[] addrWhereParams = new String[]{contactId,
|
String[] addrWhereParams = new String[]{contactId,
|
||||||
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
|
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
|
||||||
Cursor name = cr.query(ContactsContract.Data.CONTENT_URI,
|
Cursor name = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||||
null, WHERE_STRING, addrWhereParams, null);
|
new String[] {ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
|
||||||
|
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
|
||||||
|
ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
|
||||||
|
ContactsContract.CommonDataKinds.StructuredName.PREFIX,
|
||||||
|
ContactsContract.CommonDataKinds.StructuredName.SUFFIX},
|
||||||
|
WHERE_STRING, addrWhereParams, null);
|
||||||
JSONObject contactName = new JSONObject();
|
JSONObject contactName = new JSONObject();
|
||||||
if (name.moveToFirst()) {
|
if (name.moveToFirst()) {
|
||||||
try {
|
try {
|
||||||
@ -394,7 +437,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
private JSONArray phoneQuery(ContentResolver cr, String contactId) {
|
private JSONArray phoneQuery(ContentResolver cr, String contactId) {
|
||||||
Cursor phones = cr.query(
|
Cursor phones = cr.query(
|
||||||
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
|
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
|
||||||
null,
|
new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.TYPE},
|
||||||
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
|
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
|
||||||
null, null);
|
null, null);
|
||||||
JSONArray phoneNumbers = new JSONArray();
|
JSONArray phoneNumbers = new JSONArray();
|
||||||
@ -416,7 +459,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
private JSONArray emailQuery(ContentResolver cr, String contactId) {
|
private JSONArray emailQuery(ContentResolver cr, String contactId) {
|
||||||
Cursor emails = cr.query(
|
Cursor emails = cr.query(
|
||||||
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
|
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
|
||||||
null,
|
new String[] {ContactsContract.CommonDataKinds.Email.DATA,ContactsContract.CommonDataKinds.Email.TYPE},
|
||||||
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
|
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
|
||||||
null, null);
|
null, null);
|
||||||
JSONArray emailAddresses = new JSONArray();
|
JSONArray emailAddresses = new JSONArray();
|
||||||
@ -439,7 +482,8 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
String[] addrWhereParams = new String[]{contactId,
|
String[] addrWhereParams = new String[]{contactId,
|
||||||
ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE};
|
ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE};
|
||||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||||
null, WHERE_STRING, addrWhereParams, null);
|
new String[] {ContactsContract.CommonDataKinds.Im.DATA,ContactsContract.CommonDataKinds.Im.TYPE},
|
||||||
|
WHERE_STRING, addrWhereParams, null);
|
||||||
JSONArray ims = new JSONArray();
|
JSONArray ims = new JSONArray();
|
||||||
JSONObject im = new JSONObject();
|
JSONObject im = new JSONObject();
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
@ -460,7 +504,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
String[] noteWhereParams = new String[]{contactId,
|
String[] noteWhereParams = new String[]{contactId,
|
||||||
ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
|
ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
|
||||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||||
null, WHERE_STRING, noteWhereParams, null);
|
new String[] {ContactsContract.CommonDataKinds.Note.NOTE}, WHERE_STRING, noteWhereParams, null);
|
||||||
String note = new String("");
|
String note = new String("");
|
||||||
if (cursor.moveToFirst()) {
|
if (cursor.moveToFirst()) {
|
||||||
note = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
|
note = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
|
||||||
@ -473,7 +517,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
String[] nicknameWhereParams = new String[]{contactId,
|
String[] nicknameWhereParams = new String[]{contactId,
|
||||||
ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE};
|
ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE};
|
||||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||||
null, WHERE_STRING, nicknameWhereParams, null);
|
new String[] {ContactsContract.CommonDataKinds.Nickname.NAME}, WHERE_STRING, nicknameWhereParams, null);
|
||||||
String nickname = new String("");
|
String nickname = new String("");
|
||||||
if (cursor.moveToFirst()) {
|
if (cursor.moveToFirst()) {
|
||||||
nickname = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME));
|
nickname = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME));
|
||||||
@ -486,7 +530,8 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
String[] websiteWhereParams = new String[]{contactId,
|
String[] websiteWhereParams = new String[]{contactId,
|
||||||
ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE};
|
ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE};
|
||||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||||
null, WHERE_STRING, websiteWhereParams, null);
|
new String[] {ContactsContract.CommonDataKinds.Website.URL,ContactsContract.CommonDataKinds.Website.TYPE},
|
||||||
|
WHERE_STRING, websiteWhereParams, null);
|
||||||
JSONArray websites = new JSONArray();
|
JSONArray websites = new JSONArray();
|
||||||
JSONObject website = new JSONObject();
|
JSONObject website = new JSONObject();
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
@ -507,7 +552,8 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
String[] relationshipWhereParams = new String[]{contactId,
|
String[] relationshipWhereParams = new String[]{contactId,
|
||||||
ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE};
|
ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE};
|
||||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||||
null, WHERE_STRING, relationshipWhereParams, null);
|
new String[] {ContactsContract.CommonDataKinds.Relation.NAME,ContactsContract.CommonDataKinds.Relation.TYPE},
|
||||||
|
WHERE_STRING, relationshipWhereParams, null);
|
||||||
JSONArray relationships = new JSONArray();
|
JSONArray relationships = new JSONArray();
|
||||||
JSONObject relationship = new JSONObject();
|
JSONObject relationship = new JSONObject();
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
@ -553,7 +599,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save() {
|
public void save(JSONObject contact) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,9 +64,6 @@ public class ContactManager implements Plugin {
|
|||||||
JSONArray res = contactAccessor.search(args.getJSONArray(0), args.getJSONObject(1));
|
JSONArray res = contactAccessor.search(args.getJSONArray(0), args.getJSONObject(1));
|
||||||
return new PluginResult(status, res);
|
return new PluginResult(status, res);
|
||||||
}
|
}
|
||||||
else if (action.equals("create")) {
|
|
||||||
// TODO Coming soon!
|
|
||||||
}
|
|
||||||
else if (action.equals("save")) {
|
else if (action.equals("save")) {
|
||||||
// TODO Coming soon!
|
// TODO Coming soon!
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user