cordova-android/src/com/phonegap/demo/ContactManager.java

163 lines
4.0 KiB
Java
Raw Normal View History

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