diff --git a/framework/src/com/phonegap/ContactAccessorSdk3_4.java b/framework/src/com/phonegap/ContactAccessorSdk3_4.java index 704139ed..b4803aeb 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk3_4.java +++ b/framework/src/com/phonegap/ContactAccessorSdk3_4.java @@ -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 dbMap = new HashMap(); + 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 contactIds = buildSetOfContactIds(filter, searchTerm); + + Iterator 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 buildSetOfContactIds(JSONArray filter, String searchTerm) { + Set contactIds = new HashSet(); + + String key; + try { + for (int i=0; i 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 + " = ?"; diff --git a/framework/src/com/phonegap/ContactAccessorSdk5.java b/framework/src/com/phonegap/ContactAccessorSdk5.java index ed49c5e5..8cbcafb8 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk5.java +++ b/framework/src/com/phonegap/ContactAccessorSdk5.java @@ -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 dbMap = new HashMap(); + 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 contactIds = buildSetOfContactIds(filter, searchTerm); + + Iterator 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 buildSetOfContactIds(JSONArray filter, String searchTerm) { + Set contactIds = new HashSet(); + + String key; + try { + for (int i=0; i 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,