2009-11-10 09:45:02 +08:00
|
|
|
package com.phonegap;
|
2009-10-01 06:34:28 +08:00
|
|
|
|
2009-11-04 08:51:40 +08:00
|
|
|
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
|
|
|
|
2009-12-02 03:55:15 +08:00
|
|
|
@SuppressWarnings("deprecation")
|
2009-10-01 06:34:28 +08:00
|
|
|
public class ContactManager {
|
2009-11-03 07:43:09 +08:00
|
|
|
|
2009-11-05 06:27:19 +08:00
|
|
|
public class ContactTriplet
|
|
|
|
{
|
|
|
|
public String name = "";
|
|
|
|
public String email = "";
|
|
|
|
public String phone = "";
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2010-06-05 04:32:00 +08:00
|
|
|
ContactManager(WebView view, Activity app)
|
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
|
|
|
}
|
2010-06-05 04:32:00 +08:00
|
|
|
|
2009-11-10 09:22:36 +08:00
|
|
|
// This is to add backwards compatibility to the OLD Contacts API\
|
|
|
|
public void getContactsAndSendBack()
|
|
|
|
{
|
|
|
|
String[] projection = new String[] {
|
|
|
|
People._ID,
|
|
|
|
People.NAME,
|
|
|
|
People.NUMBER,
|
|
|
|
People.PRIMARY_EMAIL_ID
|
|
|
|
};
|
|
|
|
|
|
|
|
try{
|
|
|
|
Cursor myCursor = mApp.managedQuery(mPeople, projection,
|
|
|
|
null, null , People.NAME + " ASC");
|
|
|
|
processResults(myCursor, true);
|
|
|
|
}
|
|
|
|
catch (SQLiteException ex)
|
|
|
|
{
|
|
|
|
Log.d(LOG_TAG, ex.getMessage());
|
|
|
|
}
|
|
|
|
}
|
2009-11-05 06:27:19 +08:00
|
|
|
|
|
|
|
public void search(String name, String npa, String email)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (email.length() > 0)
|
|
|
|
searchByEmail(email);
|
|
|
|
else
|
|
|
|
searchPeople(name, npa);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void searchByEmail(String email)
|
|
|
|
{
|
|
|
|
String[] projection = new String[] {
|
|
|
|
ContactMethods._ID,
|
|
|
|
ContactMethods.DATA,
|
|
|
|
ContactMethods.KIND,
|
|
|
|
ContactMethods.PERSON_ID
|
|
|
|
};
|
|
|
|
String[] variables = new String[] {
|
|
|
|
email
|
|
|
|
};
|
|
|
|
|
|
|
|
try{
|
|
|
|
Cursor myCursor = mApp.managedQuery(mEmail, projection,
|
|
|
|
"contact_methods." + ContactMethods.DATA + " = ?" + "AND contact_methods.kind = 1", variables , ContactMethods.DATA + " ASC");
|
|
|
|
getMethodData(myCursor);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (SQLiteException ex)
|
|
|
|
{
|
|
|
|
Log.d(LOG_TAG, ex.getMessage());
|
|
|
|
}
|
2009-11-04 08:51:40 +08:00
|
|
|
|
2009-11-05 06:27:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void searchPeople(String name, String number)
|
|
|
|
{
|
|
|
|
String conditions = "";
|
|
|
|
|
|
|
|
if (name.length() == 0)
|
|
|
|
{
|
|
|
|
name = "%";
|
2009-12-02 06:57:02 +08:00
|
|
|
conditions += People.NAME + " LIKE ? AND ";
|
2009-11-05 06:27:19 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
conditions += People.NAME + " = ? AND ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (number.length() == 0)
|
|
|
|
number = "%";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
number = number.replace('+', '%');
|
|
|
|
number = number.replace('.', '%');
|
|
|
|
number = number.replace('-', '%');
|
|
|
|
}
|
|
|
|
|
|
|
|
conditions += People.NUMBER + " LIKE ? ";
|
|
|
|
|
|
|
|
String[] projection = new String[] {
|
|
|
|
People._ID,
|
|
|
|
People.NAME,
|
|
|
|
People.NUMBER,
|
|
|
|
People.PRIMARY_EMAIL_ID
|
|
|
|
};
|
|
|
|
|
|
|
|
String[] variables = new String[] {
|
|
|
|
name, number
|
|
|
|
};
|
|
|
|
|
|
|
|
try{
|
|
|
|
Cursor myCursor = mApp.managedQuery(mPeople, projection,
|
|
|
|
conditions, variables , People.NAME + " ASC");
|
2009-11-10 09:22:36 +08:00
|
|
|
processResults(myCursor, false);
|
2009-11-05 06:27:19 +08:00
|
|
|
}
|
|
|
|
catch (SQLiteException ex)
|
|
|
|
{
|
|
|
|
Log.d(LOG_TAG, ex.getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-11-10 09:22:36 +08:00
|
|
|
private void processResults(Cursor cur, boolean all){
|
2009-11-05 06:27:19 +08:00
|
|
|
|
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-05 06:27:19 +08:00
|
|
|
String email_id;
|
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-05 06:27:19 +08:00
|
|
|
int emailIdColumn = cur.getColumnIndex(People.PRIMARY_EMAIL_ID);
|
|
|
|
|
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-05 06:27:19 +08:00
|
|
|
email_id = cur.getString(emailIdColumn);
|
2009-11-20 02:34:49 +08:00
|
|
|
if (email_id != null && email_id.length() > 0)
|
2009-11-05 06:27:19 +08:00
|
|
|
email = getEmail(email_id);
|
|
|
|
else
|
|
|
|
email = "";
|
2009-11-04 08:51:40 +08:00
|
|
|
|
2009-11-10 09:22:36 +08:00
|
|
|
// Code for backwards compatibility with the OLD Contacts API
|
|
|
|
if (all)
|
2009-11-13 08:10:25 +08:00
|
|
|
mView.loadUrl("javascript:navigator.ContactManager.droidAddContact('" + name + "','" + phoneNumber + "','" + email +"')");
|
2009-11-10 09:22:36 +08:00
|
|
|
else
|
2009-12-16 02:38:38 +08:00
|
|
|
mView.loadUrl("javascript:navigator.contacts.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-10 09:22:36 +08:00
|
|
|
if (all)
|
|
|
|
mView.loadUrl("javascript:navigator.ContactManager.droidDone()");
|
2009-11-11 05:53:33 +08:00
|
|
|
else
|
2009-12-16 02:38:38 +08:00
|
|
|
mView.loadUrl("javascript:navigator.contacts.droidDone();");
|
2009-10-01 06:34:28 +08:00
|
|
|
}
|
2009-11-05 06:27:19 +08:00
|
|
|
else
|
|
|
|
{
|
2009-11-13 04:33:11 +08:00
|
|
|
if(all)
|
|
|
|
mView.loadUrl("javascript:navigator.ContactManager.fail()");
|
|
|
|
else
|
2009-12-16 02:38:38 +08:00
|
|
|
mView.loadUrl("javascript:navigator.contacts.fail('None found!')");
|
2009-11-05 06:27:19 +08:00
|
|
|
}
|
|
|
|
}
|
2009-10-01 06:34:28 +08:00
|
|
|
|
2009-11-05 06:27:19 +08:00
|
|
|
private void getMethodData(Cursor cur)
|
|
|
|
{
|
|
|
|
ContactTriplet data = new ContactTriplet();
|
|
|
|
String id;
|
|
|
|
String email;
|
|
|
|
|
|
|
|
if (cur.moveToFirst()) {
|
2009-11-04 08:51:40 +08:00
|
|
|
|
2009-11-05 06:27:19 +08:00
|
|
|
int idColumn = cur.getColumnIndex(ContactMethods._ID);
|
|
|
|
int emailColumn = cur.getColumnIndex(ContactMethods.DATA);
|
|
|
|
do {
|
|
|
|
// Get the field values
|
|
|
|
id = cur.getString(idColumn);
|
|
|
|
email = cur.getString(emailColumn);
|
|
|
|
|
|
|
|
data = getContactData(id);
|
|
|
|
if(data != null)
|
|
|
|
{
|
|
|
|
data.email = email;
|
2009-12-02 03:55:15 +08:00
|
|
|
mView.loadUrl("javascript:navigator.Contacts.droidFoundContact('" + data.name + "','" + data.phone + "','" + data.email +"')");
|
2009-11-05 06:27:19 +08:00
|
|
|
}
|
|
|
|
} while (cur.moveToNext());
|
2009-12-16 02:38:38 +08:00
|
|
|
mView.loadUrl("javascript:navigator.contacts.droidDoneContacts();");
|
2009-11-05 06:27:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private ContactTriplet getContactData(String id) {
|
|
|
|
ContactTriplet data = null;
|
2009-11-03 07:43:09 +08:00
|
|
|
String[] projection = new String[] {
|
2009-11-05 06:27:19 +08:00
|
|
|
People._ID,
|
2009-11-04 08:51:40 +08:00
|
|
|
People.NAME,
|
|
|
|
People.NUMBER,
|
2009-11-05 06:27:19 +08:00
|
|
|
People.PRIMARY_EMAIL_ID
|
2009-11-04 08:51:40 +08:00
|
|
|
};
|
2009-11-05 06:27:19 +08:00
|
|
|
|
|
|
|
String[] variables = new String[] {
|
|
|
|
id
|
|
|
|
};
|
|
|
|
|
|
|
|
try{
|
|
|
|
Cursor myCursor = mApp.managedQuery(mPeople, projection,
|
|
|
|
People.PRIMARY_EMAIL_ID + " = ?", variables , People.NAME + " ASC");
|
|
|
|
data = getTriplet(myCursor);
|
|
|
|
}
|
|
|
|
catch (SQLiteException ex)
|
|
|
|
{
|
|
|
|
Log.d(LOG_TAG, ex.getMessage());
|
|
|
|
}
|
2009-11-04 08:51:40 +08:00
|
|
|
|
2009-11-05 06:27:19 +08:00
|
|
|
return data;
|
2009-11-04 08:51:40 +08:00
|
|
|
}
|
2009-11-05 06:27:19 +08:00
|
|
|
|
|
|
|
private ContactTriplet getTriplet(Cursor cur) {
|
|
|
|
ContactTriplet data = new ContactTriplet();
|
|
|
|
if (cur.moveToFirst()) {
|
|
|
|
|
|
|
|
int nameColumn = cur.getColumnIndex(People.NAME);
|
|
|
|
int numberColumn = cur.getColumnIndex(People.NUMBER);
|
|
|
|
do {
|
|
|
|
|
|
|
|
data.name = cur.getString(nameColumn);
|
|
|
|
data.phone = cur.getString(numberColumn);
|
|
|
|
|
|
|
|
} while (cur.moveToNext());
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getEmailColumnData(Cursor cur)
|
|
|
|
{
|
|
|
|
String email = "";
|
2009-11-20 02:34:49 +08:00
|
|
|
if (cur != null && cur.moveToFirst()) {
|
2009-11-05 06:27:19 +08:00
|
|
|
int emailColumn = cur.getColumnIndex(ContactMethods.DATA);
|
2009-11-04 08:51:40 +08:00
|
|
|
do {
|
|
|
|
// Get the field values
|
2009-11-05 06:27:19 +08:00
|
|
|
email = cur.getString(emailColumn);
|
|
|
|
} while (cur.moveToNext());
|
2009-11-04 08:51:40 +08:00
|
|
|
}
|
|
|
|
return email;
|
2009-11-03 07:43:09 +08:00
|
|
|
}
|
|
|
|
|
2009-11-04 08:51:40 +08:00
|
|
|
private String getEmail(String id)
|
|
|
|
{
|
2009-11-05 06:27:19 +08:00
|
|
|
String email = "";
|
2009-11-04 08:51:40 +08:00
|
|
|
String[] projection = new String[] {
|
|
|
|
ContactMethods._ID,
|
|
|
|
ContactMethods.DATA,
|
|
|
|
ContactMethods.KIND
|
|
|
|
};
|
|
|
|
String[] variables = new String[] {
|
|
|
|
id
|
|
|
|
};
|
|
|
|
|
2009-11-05 06:27:19 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Cursor myCursor = mApp.managedQuery(mEmail, projection,
|
|
|
|
"contact_methods." + ContactMethods._ID + " = ?" + " AND contact_methods.kind = 1", variables , ContactMethods.DATA + " ASC");
|
|
|
|
email = getEmailColumnData(myCursor);
|
2009-11-04 08:51:40 +08:00
|
|
|
}
|
|
|
|
catch (SQLiteException ex)
|
|
|
|
{
|
|
|
|
Log.d(LOG_TAG, ex.getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return email;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-01 06:34:28 +08:00
|
|
|
}
|