diff --git a/framework/src/com/phonegap/ContactAccessorSdk3_4.java b/framework/src/com/phonegap/ContactAccessorSdk3_4.java index acdbd8af..f84fc0b4 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk3_4.java +++ b/framework/src/com/phonegap/ContactAccessorSdk3_4.java @@ -18,15 +18,19 @@ package com.phonegap; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; -import android.content.AsyncQueryHandler; +import android.content.ContentResolver; import android.database.Cursor; import android.database.sqlite.SQLiteException; import android.net.Uri; import android.provider.Contacts.ContactMethods; +import android.provider.Contacts.ContactMethodsColumns; +import android.provider.Contacts.Organizations; import android.provider.Contacts.People; +import android.provider.Contacts.Phones; import android.util.Log; import android.webkit.WebView; @@ -58,13 +62,177 @@ public class ContactAccessorSdk3_4 extends ContactAccessor { @Override public void search(JSONArray filter, JSONObject options) { - //if (email.length() > 0) - // searchByEmail(email); - //else - // searchPeople(name, npa); - searchPeople("", ""); + String searchTerm = ""; + int limit = Integer.MAX_VALUE; + boolean multiple = true; + try { + searchTerm = options.getString("filter"); + if (searchTerm.length()==0) searchTerm = "%"; + multiple = options.getBoolean("multiple"); + if (multiple) { + limit = options.getInt("limit"); + } + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage(), e); + } + + + JSONArray contacts = new JSONArray(); + JSONObject contact; + + ContentResolver cr = mApp.getContentResolver(); + Cursor cur = cr.query(People.CONTENT_URI, + null, null, null, null); + + int pos = 0; + while (cur.moveToNext() && pos < limit) { + contact = new JSONObject(); + try { + String contactId = cur.getString(cur.getColumnIndex(People._ID)); + // name + contact.put("id", contactId); + 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)); + } + // 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 + cur.getString(cur.getColumnIndex(People.NOTES)); + // nickname + // urls + // relationship + // birthdays + // anniversary + + pos++; + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage(), e); + } + contacts.put(contact); + } } + private JSONArray imQuery(ContentResolver cr, String contactId) { + String imWhere = ContactMethods.PERSON_ID + + " = ? AND " + ContactMethods.KIND + " = ?"; + String[] imWhereParams = new String[]{contactId, ContactMethods.CONTENT_IM_ITEM_TYPE}; + Cursor cursor = cr.query(ContactMethods.CONTENT_URI, + null, imWhere, imWhereParams, null); + JSONArray ims = new JSONArray(); + JSONObject im; + while (cursor.moveToNext()) { + im = new JSONObject(); + try{ + im.put("value", cursor.getString( + cursor.getColumnIndex(ContactMethodsColumns.DATA))); + im.put("type", cursor.getString( + cursor.getColumnIndex(ContactMethodsColumns.TYPE))); + ims.put(im); + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage(), e); + } + } + cursor.close(); + return null; + } + + private JSONArray organizationQuery(ContentResolver cr, String contactId) { + String orgWhere = ContactMethods.PERSON_ID + " = ?"; + String[] orgWhereParams = new String[]{contactId}; + Cursor cursor = cr.query(Organizations.CONTENT_URI, + null, orgWhere, orgWhereParams, null); + JSONArray organizations = new JSONArray(); + JSONObject organization; + while (cursor.moveToNext()) { + organization = new JSONObject(); + try{ + organization.put("name", cursor.getString(cursor.getColumnIndex(Organizations.COMPANY))); + organization.put("title", cursor.getString(cursor.getColumnIndex(Organizations.TITLE))); + // organization.put("department", cursor.getString(cursor.getColumnIndex(Organizations))); + // organization.put("description", cursor.getString(cursor.getColumnIndex(Organizations))); + // organization.put("endDate", cursor.getString(cursor.getColumnIndex(Organizations))); + // organization.put("location", cursor.getString(cursor.getColumnIndex(Organizations))); + // organization.put("startDate", cursor.getString(cursor.getColumnIndex(Organizations))); + organizations.put(organization); + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage(), e); + } + } + return organizations; + } + + private JSONArray addressQuery(ContentResolver cr, String contactId) { + String addrWhere = ContactMethods.PERSON_ID + + " = ? AND " + ContactMethods.KIND + " = ?"; + String[] addrWhereParams = new String[]{contactId, + ContactMethods.CONTENT_POSTAL_ITEM_TYPE}; + Cursor cursor = cr.query(ContactMethods.CONTENT_URI, + null, addrWhere, addrWhereParams, null); + JSONArray addresses = new JSONArray(); + JSONObject address; + while (cursor.moveToNext()) { + address = new JSONObject(); + try{ + address.put("formatted", cursor.getString(cursor.getColumnIndex(ContactMethodsColumns.DATA))); + addresses.put(address); + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage(), e); + } + } + return addresses; + } + + private JSONArray phoneQuery(ContentResolver cr, String contactId) { + Cursor cursor = cr.query( + Phones.CONTENT_URI, + null, + Phones.PERSON_ID +" = ?", + new String[]{contactId}, null); + JSONArray phones = new JSONArray(); + JSONObject phone; + while (cursor.moveToNext()) { + phone = new JSONObject(); + try{ + phone.put("value", cursor.getString(cursor.getColumnIndex(Phones.NUMBER))); + phone.put("type", cursor.getString(cursor.getColumnIndex(Phones.TYPE))); + phones.put(phone); + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage(), e); + } + } + return phones; + } + + private JSONArray emailQuery(ContentResolver cr, String contactId) { + Cursor cursor = cr.query( + ContactMethods.CONTENT_EMAIL_URI, + null, + ContactMethods.PERSON_ID +" = ?", + new String[]{contactId}, null); + JSONArray emails = new JSONArray(); + JSONObject email; + while (cursor.moveToNext()) { + email = new JSONObject(); + try{ + email.put("value", cursor.getString(cursor.getColumnIndex(ContactMethods.DATA))); + email.put("type", cursor.getString(cursor.getColumnIndex(ContactMethods.TYPE))); + emails.put(email); + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage(), e); + } + } + return emails; + } + private void searchByEmail(String email) { String[] projection = new String[] {