Merging Fil's contact changes with Bryce's Plugins

This commit is contained in:
macdonst
2010-09-10 15:09:40 -04:00
parent 1c0de5ad8d
commit 633100a3ce
5 changed files with 772 additions and 269 deletions
+274 -268
View File
@@ -18,13 +18,14 @@ import android.database.sqlite.SQLiteException;
@SuppressWarnings("deprecation")
public class ContactManager implements Plugin {
public class ContactTriplet
{
public String name = "";
public String email = "";
public String phone = "";
}
// public class ContactTriplet
// {
// public String name = "";
// public String email = "";
// public String phone = "";
// }
private static ContactAccessor contactAccessor;
WebView webView; // WebView object
DroidGap ctx; // DroidGap object
@@ -67,15 +68,20 @@ public class ContactManager implements Plugin {
* @return A CommandResult object with a status and message.
*/
public PluginResult execute(String action, JSONArray args) {
if (contactAccessor == null) {
contactAccessor = ContactAccessor.getInstance(webView, ctx);
}
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
if (action.equals("getContactsAndSendBack")) {
this.getContactsAndSendBack();
}
else if (action.equals("search")) {
this.search(args.getString(0), args.getString(1), args.getString(2));
//if (action.equals("getContactsAndSendBack")) {
// contactAccessor.getContactsAndSendBack();
//}
//else if (action.equals("search")) {
if (action.equals("search")) {
Log.d(LOG_TAG, "Executing search using accessor");
contactAccessor.search(args.getString(0), args.getString(1), args.getString(2));
}
return new PluginResult(status, result);
} catch (JSONException e) {
@@ -129,263 +135,263 @@ public class ContactManager implements Plugin {
//--------------------------------------------------------------------------
// 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 = this.ctx.managedQuery(mPeople, projection,
null, null , People.NAME + " ASC");
processResults(myCursor, true);
}
catch (SQLiteException ex)
{
Log.d(LOG_TAG, ex.getMessage());
}
}
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 = this.ctx.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());
}
}
private void searchPeople(String name, String number)
{
String conditions = "";
if (name.length() == 0)
{
name = "%";
conditions += People.NAME + " LIKE ? AND ";
}
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 = this.ctx.managedQuery(mPeople, projection,
conditions, variables , People.NAME + " ASC");
processResults(myCursor, false);
}
catch (SQLiteException ex)
{
Log.d(LOG_TAG, ex.getMessage());
}
}
private void processResults(Cursor cur, boolean all){
if (cur.moveToFirst()) {
String name;
String phoneNumber;
String email_id;
String email;
int nameColumn = cur.getColumnIndex(People.NAME);
int phoneColumn = cur.getColumnIndex(People.NUMBER);
int emailIdColumn = cur.getColumnIndex(People.PRIMARY_EMAIL_ID);
do {
// Get the field values
name = cur.getString(nameColumn);
phoneNumber = cur.getString(phoneColumn);
email_id = cur.getString(emailIdColumn);
if (email_id != null && email_id.length() > 0)
email = getEmail(email_id);
else
email = "";
// Code for backwards compatibility with the OLD Contacts API
if (all) {
this.ctx.sendJavascript("navigator.ContactManager.droidAddContact('" + name + "','" + phoneNumber + "','" + email +"');");
}
else {
this.ctx.sendJavascript("navigator.contacts.droidFoundContact('" + name + "','" + phoneNumber + "','" + email +"');");
}
} while (cur.moveToNext());
if (all) {
this.ctx.sendJavascript("navigator.ContactManager.droidDone();");
}
else {
this.ctx.sendJavascript("navigator.contacts.droidDone();");
}
}
else
{
if (all) {
this.ctx.sendJavascript("navigator.ContactManager.fail();");
}
else {
this.ctx.sendJavascript("navigator.contacts.fail('None found!');");
}
}
}
private void getMethodData(Cursor cur)
{
ContactTriplet data = new ContactTriplet();
String id;
String email;
if (cur.moveToFirst()) {
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;
this.ctx.sendJavascript("navigator.Contacts.droidFoundContact('" + data.name + "','" + data.phone + "','" + data.email +"');");
}
} while (cur.moveToNext());
this.ctx.sendJavascript("navigator.contacts.droidDoneContacts();");
}
}
private ContactTriplet getContactData(String id) {
ContactTriplet data = null;
String[] projection = new String[] {
People._ID,
People.NAME,
People.NUMBER,
People.PRIMARY_EMAIL_ID
};
String[] variables = new String[] {
id
};
try{
Cursor myCursor = this.ctx.managedQuery(mPeople, projection,
People.PRIMARY_EMAIL_ID + " = ?", variables , People.NAME + " ASC");
data = getTriplet(myCursor);
}
catch (SQLiteException ex)
{
Log.d(LOG_TAG, ex.getMessage());
}
return data;
}
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 = "";
if (cur != null && cur.moveToFirst()) {
int emailColumn = cur.getColumnIndex(ContactMethods.DATA);
do {
// Get the field values
email = cur.getString(emailColumn);
} 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 = this.ctx.managedQuery(mEmail, projection,
"contact_methods." + ContactMethods._ID + " = ?" + " AND contact_methods.kind = 1", variables , ContactMethods.DATA + " ASC");
email = getEmailColumnData(myCursor);
}
catch (SQLiteException ex)
{
Log.d(LOG_TAG, ex.getMessage());
}
return email;
}
// public void getContactsAndSendBack()
// {
// String[] projection = new String[] {
// People._ID,
// People.NAME,
// People.NUMBER,
// People.PRIMARY_EMAIL_ID
// };
//
// try{
// Cursor myCursor = this.ctx.managedQuery(mPeople, projection,
// null, null , People.NAME + " ASC");
// processResults(myCursor, true);
// }
// catch (SQLiteException ex)
// {
// Log.d(LOG_TAG, ex.getMessage());
// }
// }
//
// 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 = this.ctx.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());
// }
//
// }
//
// private void searchPeople(String name, String number)
// {
// String conditions = "";
//
// if (name.length() == 0)
// {
// name = "%";
// conditions += People.NAME + " LIKE ? AND ";
// }
// 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 = this.ctx.managedQuery(mPeople, projection,
// conditions, variables , People.NAME + " ASC");
// processResults(myCursor, false);
// }
// catch (SQLiteException ex)
// {
// Log.d(LOG_TAG, ex.getMessage());
// }
//
// }
//
// private void processResults(Cursor cur, boolean all){
//
// if (cur.moveToFirst()) {
//
// String name;
// String phoneNumber;
// String email_id;
// String email;
//
// int nameColumn = cur.getColumnIndex(People.NAME);
// int phoneColumn = cur.getColumnIndex(People.NUMBER);
// int emailIdColumn = cur.getColumnIndex(People.PRIMARY_EMAIL_ID);
//
// do {
// // Get the field values
// name = cur.getString(nameColumn);
// phoneNumber = cur.getString(phoneColumn);
// email_id = cur.getString(emailIdColumn);
// if (email_id != null && email_id.length() > 0)
// email = getEmail(email_id);
// else
// email = "";
//
// // Code for backwards compatibility with the OLD Contacts API
// if (all) {
// this.ctx.sendJavascript("navigator.ContactManager.droidAddContact('" + name + "','" + phoneNumber + "','" + email +"');");
// }
// else {
// this.ctx.sendJavascript("navigator.contacts.droidFoundContact('" + name + "','" + phoneNumber + "','" + email +"');");
// }
// } while (cur.moveToNext());
// if (all) {
// this.ctx.sendJavascript("navigator.ContactManager.droidDone();");
// }
// else {
// this.ctx.sendJavascript("navigator.contacts.droidDone();");
// }
// }
// else
// {
// if (all) {
// this.ctx.sendJavascript("navigator.ContactManager.fail();");
// }
// else {
// this.ctx.sendJavascript("navigator.contacts.fail('None found!');");
// }
// }
// }
//
// private void getMethodData(Cursor cur)
// {
// ContactTriplet data = new ContactTriplet();
// String id;
// String email;
//
// if (cur.moveToFirst()) {
//
// 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;
// this.ctx.sendJavascript("navigator.Contacts.droidFoundContact('" + data.name + "','" + data.phone + "','" + data.email +"');");
// }
// } while (cur.moveToNext());
// this.ctx.sendJavascript("navigator.contacts.droidDoneContacts();");
// }
// }
//
// private ContactTriplet getContactData(String id) {
// ContactTriplet data = null;
// String[] projection = new String[] {
// People._ID,
// People.NAME,
// People.NUMBER,
// People.PRIMARY_EMAIL_ID
// };
//
// String[] variables = new String[] {
// id
// };
//
// try{
// Cursor myCursor = this.ctx.managedQuery(mPeople, projection,
// People.PRIMARY_EMAIL_ID + " = ?", variables , People.NAME + " ASC");
// data = getTriplet(myCursor);
// }
// catch (SQLiteException ex)
// {
// Log.d(LOG_TAG, ex.getMessage());
// }
//
// return data;
// }
//
// 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 = "";
// if (cur != null && cur.moveToFirst()) {
// int emailColumn = cur.getColumnIndex(ContactMethods.DATA);
// do {
// // Get the field values
// email = cur.getString(emailColumn);
// } 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 = this.ctx.managedQuery(mEmail, projection,
// "contact_methods." + ContactMethods._ID + " = ?" + " AND contact_methods.kind = 1", variables , ContactMethods.DATA + " ASC");
// email = getEmailColumnData(myCursor);
// }
// catch (SQLiteException ex)
// {
// Log.d(LOG_TAG, ex.getMessage());
// }
//
// return email;
// }
}