Only query what is required as passed by filter

This commit is contained in:
macdonst 2010-09-28 14:19:40 -04:00
parent 2098436a2c
commit c13c0c37e3
2 changed files with 374 additions and 39 deletions

View File

@ -17,6 +17,12 @@
package com.phonegap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -24,6 +30,7 @@ import org.json.JSONObject;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.Contacts.ContactMethods;
import android.provider.Contacts.ContactMethodsColumns;
@ -48,6 +55,23 @@ import android.webkit.WebView;
*/
@SuppressWarnings("deprecation")
public class ContactAccessorSdk3_4 extends ContactAccessor {
private static final Map<String, String> dbMap = new HashMap<String, String>();
static {
dbMap.put("id", People._ID);
dbMap.put("displayName", People.DISPLAY_NAME);
dbMap.put("phoneNumbers", Phones.NUMBER);
dbMap.put("phoneNumbers.value", Phones.NUMBER);
dbMap.put("emails", ContactMethods.DATA);
dbMap.put("emails.value", ContactMethods.DATA);
dbMap.put("addresses", ContactMethodsColumns.DATA);
dbMap.put("addresses.formatted", ContactMethodsColumns.DATA);
dbMap.put("ims", ContactMethodsColumns.DATA);
dbMap.put("ims.value", ContactMethodsColumns.DATA);
dbMap.put("organizations", Organizations.COMPANY);
dbMap.put("organizations.name", Organizations.COMPANY);
dbMap.put("organizations.title", Organizations.TITLE);
dbMap.put("note", People.NOTES);
}
public ContactAccessorSdk3_4(WebView view, Activity app)
{
@ -77,32 +101,51 @@ public class ContactAccessorSdk3_4 extends ContactAccessor {
}
JSONArray contacts = new JSONArray();
JSONObject contact;
ContentResolver cr = mApp.getContentResolver();
// Right now we are just querying the displayName
Cursor cur = cr.query(People.CONTENT_URI,
null,
People.DISPLAY_NAME + " LIKE ?",
new String[] {searchTerm},
People.DISPLAY_NAME + " ASC");
int pos = 0;
while (cur.moveToNext() && pos < limit) {
contact = new JSONObject();
try {
String contactId = cur.getString(cur.getColumnIndex(People._ID));
// name
// JSONArray contacts = new JSONArray();
// JSONObject contact;
//
// ContentResolver cr = mApp.getContentResolver();
//
// // Right now we are just querying the displayName
// Cursor cur = cr.query(People.CONTENT_URI,
// null,
// People.DISPLAY_NAME + " LIKE ?",
// new String[] {searchTerm},
// People.DISPLAY_NAME + " ASC");
//
//
// int pos = 0;
// while (cur.moveToNext() && pos < limit) {
// Get a cursor by creating the query.
ContentResolver cr = mApp.getContentResolver();
Set<String> contactIds = buildSetOfContactIds(filter, searchTerm);
Iterator<String> it = contactIds.iterator();
JSONArray contacts = new JSONArray();
JSONObject contact;
String contactId;
int pos = 0;
while (it.hasNext() && (pos < limit)) {
contact = new JSONObject();
try {
contactId = it.next();
contact.put("id", contactId);
contact.put("displayName", cur.getString(cur.getColumnIndex(People.DISPLAY_NAME)));
// Do query for name and note
// Right now we are just querying the displayName
Cursor cur = cr.query(People.CONTENT_URI,
null,
"people._id = ?",
new String[] {contactId},
null);
cur.moveToFirst();
// name
contact.put("displayName", cur.getString(cur.getColumnIndex(People.DISPLAY_NAME)));
// phone number
if (Integer.parseInt(cur.getString(cur.getColumnIndex(People.PRIMARY_PHONE_ID))) > 0) {
contact.put("phoneNumbers", phoneQuery(cr, contactId));
}
contact.put("phoneNumbers", phoneQuery(cr, contactId));
// email
contact.put("emails", emailQuery(cr, contactId));
// addresses
@ -120,15 +163,113 @@ public class ContactAccessorSdk3_4 extends ContactAccessor {
// anniversary
pos++;
cur.close();
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
}
contacts.put(contact);
}
cur.close();
mView.loadUrl("javascript:navigator.service.contacts.droidDone('" + contacts.toString() + "');");
}
private Set<String> buildSetOfContactIds(JSONArray filter, String searchTerm) {
Set<String> contactIds = new HashSet<String>();
String key;
try {
for (int i=0; i<filter.length(); i++) {
key = filter.getString(i);
if (key.startsWith("displayName")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
People.CONTENT_URI,
People._ID,
dbMap.get(key) + " LIKE ?",
new String[] {searchTerm});
}
// else if (key.startsWith("name")) {
// Log.d(LOG_TAG, "Doing " + key + " query");
// doQuery(searchTerm, contactIds,
// ContactsContract.Data.CONTENT_URI,
// ContactsContract.Data.CONTACT_ID,
// dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
// new String[] {searchTerm, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE});
// }
else if (key.startsWith("phoneNumbers")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
Phones.CONTENT_URI,
Phones.PERSON_ID,
dbMap.get(key) + " LIKE ?",
new String[] {searchTerm});
}
else if (key.startsWith("emails")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
ContactMethods.CONTENT_EMAIL_URI,
ContactMethods.PERSON_ID,
dbMap.get(key) + " LIKE ? AND " + ContactMethods.KIND + " = ?",
new String[] {searchTerm, ContactMethods.CONTENT_EMAIL_ITEM_TYPE});
}
else if (key.startsWith("addresses")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
ContactMethods.CONTENT_URI,
ContactMethods.PERSON_ID,
dbMap.get(key) + " LIKE ? AND " + ContactMethods.KIND + " = ?",
new String[] {searchTerm, ContactMethods.CONTENT_POSTAL_ITEM_TYPE});
}
else if (key.startsWith("ims")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
ContactMethods.CONTENT_URI,
ContactMethods.PERSON_ID,
dbMap.get(key) + " LIKE ? AND " + ContactMethods.KIND + " = ?",
new String[] {searchTerm, ContactMethods.CONTENT_IM_ITEM_TYPE});
}
else if (key.startsWith("organizations")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
Organizations.CONTENT_URI,
ContactMethods.PERSON_ID,
dbMap.get(key) + " LIKE ?",
new String[] {searchTerm});
}
else if (key.startsWith("note")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
People.CONTENT_URI,
People._ID,
dbMap.get(key) + " LIKE ?",
new String[] {searchTerm});
}
}
}
catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
}
return contactIds;
}
private void doQuery(String searchTerm, Set<String> contactIds,
Uri uri, String projection, String selection, String[] selectionArgs) {
ContentResolver cr = mApp.getContentResolver();
Cursor cursor = cr.query(
uri,
null,
selection,
selectionArgs,
null);
while (cursor.moveToNext()) {
Log.d(LOG_TAG, "ID = " + cursor.getString(cursor.getColumnIndex(projection)));
contactIds.add(cursor.getString(cursor.getColumnIndex(projection)));
}
cursor.close();
}
private JSONArray imQuery(ContentResolver cr, String contactId) {
String imWhere = ContactMethods.PERSON_ID
+ " = ? AND " + ContactMethods.KIND + " = ?";

View File

@ -17,6 +17,12 @@
package com.phonegap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -24,6 +30,7 @@ import org.json.JSONObject;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;
import android.webkit.WebView;
@ -49,8 +56,58 @@ 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<String, String> dbMap = new HashMap<String, String>();
static {
dbMap.put("id", ContactsContract.Contacts._ID);
dbMap.put("displayName", ContactsContract.Contacts.DISPLAY_NAME);
dbMap.put("name", ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
dbMap.put("name.formatted", ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
dbMap.put("name.familyName", ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
dbMap.put("name.givenName", ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
dbMap.put("name.middleName", ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME);
dbMap.put("name.honorificPrefix", ContactsContract.CommonDataKinds.StructuredName.PREFIX);
dbMap.put("name.honorificSuffix", ContactsContract.CommonDataKinds.StructuredName.SUFFIX);
dbMap.put("nickname", ContactsContract.CommonDataKinds.Nickname.NAME);
dbMap.put("phoneNumbers", ContactsContract.CommonDataKinds.Phone.NUMBER);
dbMap.put("phoneNumbers.value", ContactsContract.CommonDataKinds.Phone.NUMBER);
dbMap.put("emails", ContactsContract.CommonDataKinds.Email.DATA);
dbMap.put("emails.value", ContactsContract.CommonDataKinds.Email.DATA);
dbMap.put("addresses", ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
dbMap.put("addresses.formatted", ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
dbMap.put("addresses.streetAddress", ContactsContract.CommonDataKinds.StructuredPostal.STREET);
dbMap.put("addresses.locality", ContactsContract.CommonDataKinds.StructuredPostal.CITY);
dbMap.put("addresses.region", ContactsContract.CommonDataKinds.StructuredPostal.REGION);
dbMap.put("addresses.postalCode", ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE);
dbMap.put("addresses.country", ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY);
dbMap.put("ims", ContactsContract.CommonDataKinds.Im.DATA);
dbMap.put("ims.value", ContactsContract.CommonDataKinds.Im.DATA);
dbMap.put("organizations", ContactsContract.CommonDataKinds.Organization.COMPANY);
dbMap.put("organizations.name", ContactsContract.CommonDataKinds.Organization.COMPANY);
dbMap.put("organizations.department", ContactsContract.CommonDataKinds.Organization.DEPARTMENT);
dbMap.put("organizations.title", ContactsContract.CommonDataKinds.Organization.TITLE);
dbMap.put("organizations.location", ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION);
dbMap.put("organizations.description", ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION);
//dbMap.put("published", null);
//dbMap.put("updated", null);
dbMap.put("birthday", ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
dbMap.put("anniversary", ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
//dbMap.put("gender", null);
dbMap.put("note", ContactsContract.CommonDataKinds.Note.NOTE);
//dbMap.put("preferredUsername", null);
//dbMap.put("photos.value", null);
//dbMap.put("tags.value", null);
dbMap.put("relationships", ContactsContract.CommonDataKinds.Relation.NAME);
dbMap.put("relationships.value", ContactsContract.CommonDataKinds.Relation.NAME);
dbMap.put("urls", ContactsContract.CommonDataKinds.Website.URL);
dbMap.put("urls.value", ContactsContract.CommonDataKinds.Website.URL);
//dbMap.put("accounts.domain", null);
//dbMap.put("accounts.username", null);
//dbMap.put("accounts.userid", null);
//dbMap.put("utcOffset", null);
//dbMap.put("connected", null);
}
public ContactAccessorSdk5(WebView view, Activity app)
public ContactAccessorSdk5(WebView view, Activity app)
{
mApp = app;
mView = view;
@ -79,26 +136,22 @@ public class ContactAccessorSdk5 extends ContactAccessor {
// Get a cursor by creating the query.
ContentResolver cr = mApp.getContentResolver();
// Right now we are just querying the displayName
Cursor cursor = cr.query(
ContactsContract.Contacts.CONTENT_URI,
new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.HAS_PHONE_NUMBER, ContactsContract.Contacts.DISPLAY_NAME},
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?",
new String[] {searchTerm},
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
Set<String> contactIds = buildSetOfContactIds(filter, searchTerm);
Iterator<String> it = contactIds.iterator();
JSONArray contacts = new JSONArray();
JSONObject contact;
String contactId;
int pos = 0;
while (cursor.moveToNext() && (pos < limit)) {
while (it.hasNext() && (pos < limit)) {
contact = new JSONObject();
contactId = it.next();
Log.d(LOG_TAG, "Contact ID = " + contactId);
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (contactName.trim().length() == 0) continue;
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
try {
contact.put("id", contactId);
contact.put("displayName", contactName);
contact.put("displayName", displayNameQuery(cr, contactId));
contact.put("name", nameQuery(cr, contactId));
contact.put("phoneNumbers", phoneQuery(cr, contactId));
contact.put("emails", emailQuery(cr, contactId));
@ -118,9 +171,150 @@ public class ContactAccessorSdk5 extends ContactAccessor {
contacts.put(contact);
pos++;
}
cursor.close();
mView.loadUrl("javascript:navigator.service.contacts.droidDone('" + contacts.toString() + "');");
}
private Set<String> buildSetOfContactIds(JSONArray filter, String searchTerm) {
Set<String> contactIds = new HashSet<String>();
String key;
try {
for (int i=0; i<filter.length(); i++) {
key = filter.getString(i);
if (key.startsWith("displayName")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
ContactsContract.Contacts.CONTENT_URI,
ContactsContract.Contacts._ID,
dbMap.get(key) + " LIKE ?",
new String[] {searchTerm});
}
else if (key.startsWith("name")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
ContactsContract.Data.CONTENT_URI,
ContactsContract.Data.CONTACT_ID,
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {searchTerm, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE});
}
else if (key.startsWith("nickname")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
ContactsContract.Data.CONTENT_URI,
ContactsContract.Data.CONTACT_ID,
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {searchTerm, ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE});
}
else if (key.startsWith("phoneNumbers")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
dbMap.get(key) + " LIKE ?",
new String[] {searchTerm});
}
else if (key.startsWith("emails")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
ContactsContract.CommonDataKinds.Email.CONTACT_ID,
dbMap.get(key) + " LIKE ?",
new String[] {searchTerm});
}
else if (key.startsWith("addresses")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
ContactsContract.Data.CONTENT_URI,
ContactsContract.Data.CONTACT_ID,
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {searchTerm, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE});
}
else if (key.startsWith("ims")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
ContactsContract.Data.CONTENT_URI,
ContactsContract.Data.CONTACT_ID,
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {searchTerm, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE});
}
else if (key.startsWith("organizations")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
ContactsContract.Data.CONTENT_URI,
ContactsContract.Data.CONTACT_ID,
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {searchTerm, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE});
}
else if (key.startsWith("birthday")) {
}
else if (key.startsWith("anniversary")) {
}
else if (key.startsWith("note")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
ContactsContract.Data.CONTENT_URI,
ContactsContract.Data.CONTACT_ID,
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {searchTerm, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE});
}
else if (key.startsWith("relationships")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
ContactsContract.Data.CONTENT_URI,
ContactsContract.Data.CONTACT_ID,
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {searchTerm, ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE});
}
else if (key.startsWith("urls")) {
Log.d(LOG_TAG, "Doing " + key + " query");
doQuery(searchTerm, contactIds,
ContactsContract.Data.CONTENT_URI,
ContactsContract.Data.CONTACT_ID,
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {searchTerm, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE});
}
}
}
catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
}
return contactIds;
}
private void doQuery(String searchTerm, Set<String> contactIds,
Uri uri, String projection, String selection, String[] selectionArgs) {
// Get a cursor by creating the query.
ContentResolver cr = mApp.getContentResolver();
Cursor cursor = cr.query(
uri,
new String[] {projection},
selection,
selectionArgs,
null);
while (cursor.moveToNext()) {
Log.d(LOG_TAG, "ID = " + cursor.getString(cursor.getColumnIndex(projection)));
contactIds.add(cursor.getString(cursor.getColumnIndex(projection)));
}
cursor.close();
}
private String displayNameQuery(ContentResolver cr, String contactId) {
Cursor cursor = cr.query(
ContactsContract.Contacts.CONTENT_URI,
new String[] {ContactsContract.Contacts.DISPLAY_NAME},
ContactsContract.Contacts._ID + " = ?",
new String[] {contactId},
null);
cursor.moveToFirst();
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
cursor.close();
return displayName;
}
private JSONArray organizationQuery(ContentResolver cr, String contactId) {
String[] orgWhereParams = new String[]{contactId,