From e724ccb5ddb6212905a63df0895884b3c1639771 Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Tue, 3 Nov 2009 16:51:40 -0800 Subject: [PATCH] Initial Implementation of the W3C Device Contacts API --- src/com/phonegap/demo/ContactManager.java | 149 ++++++++++++++++++---- src/com/phonegap/demo/DroidGap.java | 2 +- 2 files changed, 124 insertions(+), 27 deletions(-) diff --git a/src/com/phonegap/demo/ContactManager.java b/src/com/phonegap/demo/ContactManager.java index ece818c1..56ee57ac 100644 --- a/src/com/phonegap/demo/ContactManager.java +++ b/src/com/phonegap/demo/ContactManager.java @@ -1,65 +1,162 @@ package com.phonegap.demo; +import org.json.JSONException; +import org.json.JSONObject; + +import android.provider.Contacts.ContactMethods; import android.provider.Contacts.People; +import android.util.Log; +import android.webkit.WebView; import android.app.Activity; import android.content.ContentResolver; -import android.content.ContentUris; import android.net.Uri; import android.database.Cursor; +import android.database.sqlite.SQLiteException; public class ContactManager { + private static final String LOG_TAG = "Contact Query"; Activity mApp; + WebView mView; Uri mPeople = android.provider.Contacts.People.CONTENT_URI; - Uri mPhone = android.provider.Contacts.Phones.CONTENT_URI; - Uri mEmail = android.provider.Contacts.ContactMethods.CONTENT_EMAIL_URI; + Uri mPhone = android.provider.Contacts.Phones.CONTENT_URI; + Uri mEmail = android.provider.Contacts.ContactMethods.CONTENT_URI; - ContactManager(Activity app) + ContactManager(Activity app, WebView view) { mApp = app; + mView = view; } - - private void getColumnData(Cursor cur){ - - ContentResolver cr = mApp.getContentResolver(); + private void processResults(Cursor cur){ + if (cur.moveToFirst()) { String name; + String phoneNumber; String email; - String phoneNumber; + int nameColumn = cur.getColumnIndex(People.NAME); int phoneColumn = cur.getColumnIndex(People.NUMBER); + int emailColumn = cur.getColumnIndex(ContactMethods.DATA); do { // Get the field values name = cur.getString(nameColumn); phoneNumber = cur.getString(phoneColumn); + email = cur.getString(emailColumn); + + mView.loadUrl("javascript:navigator.addressBook.droidFoundContact('" + name + "','" + phoneNumber + "','" + email +")"); } while (cur.moveToNext()); } } - - public void findContacts() + public void search(String rawdata) { + String conditions = ""; + String name = ""; + String phone = ""; + String email = ""; - // Form an array specifying which columns to return. - String[] projection = new String[] { - People._ID, - People.NAME, - People.NUMBER - }; - - // Make the query. - Cursor managedCursor = mApp.managedQuery(mPeople, - projection, // Which columns to return - null, // Which rows to return (all rows) - null, // Selection arguments (none) - // Put the results in ascending order by name - People.DISPLAY_NAME + " ASC"); + try { + JSONObject data = new JSONObject(rawdata); - this.getColumnData(managedCursor); + if (data.has("givenName")) + name += data.getString("givenName"); + if (data.has("familyName")) + name += data.getString("familyName"); + + if (name.length() > 0) + { + conditions += "people.name = ?"; + } + + if (data.has("phone")) + { + phone = data.getString("phone"); + if(conditions.length() > 0) + conditions += "AND "; + conditions += "people.number LIKE ?"; + } + if (data.has("email")) + { + email = data.getString("email"); + if(conditions.length() > 0) + conditions += "AND "; + conditions += "contact_methods.data = ?"; + } + + conditions += "AND contact_methods.kind = 1"; + + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + String[] projection = new String[] { + ContactMethods._ID, + People.NAME, + People.NUMBER, + ContactMethods.DATA + }; + + String[] params = new String[] { name, phone, email }; + + Cursor myCursor = mApp.managedQuery(mEmail, projection, + conditions, params , ContactMethods.DATA + " ASC"); + + processResults(myCursor); } + + private String getPhoneColumnData(Cursor cur){ + + ContentResolver cr = mApp.getContentResolver(); + + String email = ""; + String kind; + if (cur.moveToFirst()) { + + int emailColumn = cur.getColumnIndex(ContactMethods.DATA); + int kindColumn = cur.getColumnIndex(ContactMethods.KIND); + do { + // Get the field values + email = cur.getString(emailColumn); + kind = cur.getString(kindColumn); + + } while (cur.moveToNext()); + + } + return email; + } + + + private String getEmail(String id) + { + String email = ""; + + String[] projection = new String[] { + ContactMethods._ID, + ContactMethods.DATA, + ContactMethods.KIND + }; + String[] variables = new String[] { + id + }; + + try{ + Cursor myCursor = mApp.managedQuery(mEmail, projection, + "contact_methods." + ContactMethods._ID + " = ?" + "AND contact_methods.kind = 1", variables , ContactMethods.DATA + " ASC"); + email = getPhoneColumnData(myCursor); + } + catch (SQLiteException ex) + { + Log.d(LOG_TAG, ex.getMessage()); + } + + return email; + } + + } diff --git a/src/com/phonegap/demo/DroidGap.java b/src/com/phonegap/demo/DroidGap.java index 14eba964..0ad6767b 100644 --- a/src/com/phonegap/demo/DroidGap.java +++ b/src/com/phonegap/demo/DroidGap.java @@ -99,7 +99,7 @@ public class DroidGap extends Activity { geo = new GeoBroker(appView, this); accel = new AccelListener(this, appView); launcher = new CameraLauncher(appView, this); - mContacts = new ContactManager(this); + mContacts = new ContactManager(this, appView); // This creates the new javascript interfaces for PhoneGap appView.addJavascriptInterface(gap, "DroidGap"); appView.addJavascriptInterface(geo, "Geo");