2009-10-01 06:34:28 +08:00
|
|
|
package com.phonegap.demo;
|
|
|
|
|
2009-11-04 08:51:40 +08:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import android.provider.Contacts.ContactMethods;
|
2009-10-01 06:34:28 +08:00
|
|
|
import android.provider.Contacts.People;
|
2009-11-04 08:51:40 +08:00
|
|
|
import android.util.Log;
|
|
|
|
import android.webkit.WebView;
|
2009-11-03 07:43:09 +08:00
|
|
|
import android.app.Activity;
|
2009-10-10 01:23:21 +08:00
|
|
|
import android.content.ContentResolver;
|
2009-10-01 06:34:28 +08:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.database.Cursor;
|
2009-11-04 08:51:40 +08:00
|
|
|
import android.database.sqlite.SQLiteException;
|
2009-10-01 06:34:28 +08:00
|
|
|
|
|
|
|
public class ContactManager {
|
2009-11-03 07:43:09 +08:00
|
|
|
|
2009-11-04 08:51:40 +08:00
|
|
|
private static final String LOG_TAG = "Contact Query";
|
2009-11-03 07:43:09 +08:00
|
|
|
Activity mApp;
|
2009-11-04 08:51:40 +08:00
|
|
|
WebView mView;
|
2009-11-03 07:43:09 +08:00
|
|
|
Uri mPeople = android.provider.Contacts.People.CONTENT_URI;
|
2009-11-04 08:51:40 +08:00
|
|
|
Uri mPhone = android.provider.Contacts.Phones.CONTENT_URI;
|
|
|
|
Uri mEmail = android.provider.Contacts.ContactMethods.CONTENT_URI;
|
2009-11-03 07:43:09 +08:00
|
|
|
|
2009-11-04 08:51:40 +08:00
|
|
|
ContactManager(Activity app, WebView view)
|
2009-10-10 01:23:21 +08:00
|
|
|
{
|
2009-11-03 07:43:09 +08:00
|
|
|
mApp = app;
|
2009-11-04 08:51:40 +08:00
|
|
|
mView = view;
|
2009-10-10 01:23:21 +08:00
|
|
|
}
|
2009-11-03 07:43:09 +08:00
|
|
|
|
2009-11-04 08:51:40 +08:00
|
|
|
private void processResults(Cursor cur){
|
|
|
|
|
2009-10-01 06:34:28 +08:00
|
|
|
if (cur.moveToFirst()) {
|
|
|
|
|
|
|
|
String name;
|
2009-11-04 08:51:40 +08:00
|
|
|
String phoneNumber;
|
2009-11-03 07:43:09 +08:00
|
|
|
String email;
|
2009-11-04 08:51:40 +08:00
|
|
|
|
2009-11-03 07:43:09 +08:00
|
|
|
int nameColumn = cur.getColumnIndex(People.NAME);
|
2009-10-01 06:34:28 +08:00
|
|
|
int phoneColumn = cur.getColumnIndex(People.NUMBER);
|
2009-11-04 08:51:40 +08:00
|
|
|
int emailColumn = cur.getColumnIndex(ContactMethods.DATA);
|
2009-10-01 06:34:28 +08:00
|
|
|
do {
|
|
|
|
// Get the field values
|
2009-11-03 07:43:09 +08:00
|
|
|
name = cur.getString(nameColumn);
|
2009-10-01 06:34:28 +08:00
|
|
|
phoneNumber = cur.getString(phoneColumn);
|
2009-11-04 08:51:40 +08:00
|
|
|
email = cur.getString(emailColumn);
|
|
|
|
|
|
|
|
mView.loadUrl("javascript:navigator.addressBook.droidFoundContact('" + name + "','" + phoneNumber + "','" + email +")");
|
2009-11-03 07:43:09 +08:00
|
|
|
|
2009-10-01 06:34:28 +08:00
|
|
|
} while (cur.moveToNext());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-04 08:51:40 +08:00
|
|
|
public void search(String rawdata)
|
2009-11-03 07:43:09 +08:00
|
|
|
{
|
2009-11-04 08:51:40 +08:00
|
|
|
String conditions = "";
|
|
|
|
String name = "";
|
|
|
|
String phone = "";
|
|
|
|
String email = "";
|
|
|
|
|
|
|
|
try {
|
|
|
|
JSONObject data = new JSONObject(rawdata);
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2009-11-03 07:43:09 +08:00
|
|
|
|
|
|
|
String[] projection = new String[] {
|
2009-11-04 08:51:40 +08:00
|
|
|
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){
|
2009-11-03 07:43:09 +08:00
|
|
|
|
2009-11-04 08:51:40 +08:00
|
|
|
ContentResolver cr = mApp.getContentResolver();
|
|
|
|
|
|
|
|
String email = "";
|
|
|
|
String kind;
|
|
|
|
if (cur.moveToFirst()) {
|
2009-11-03 07:43:09 +08:00
|
|
|
|
2009-11-04 08:51:40 +08:00
|
|
|
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;
|
2009-11-03 07:43:09 +08:00
|
|
|
}
|
|
|
|
|
2009-11-04 08:51:40 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-01 06:34:28 +08:00
|
|
|
}
|