mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-19 07:02:51 +08:00
Initial Implementation of the W3C Device Contacts API
This commit is contained in:
parent
7ead5ec2d8
commit
e724ccb5dd
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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");
|
||||
|
Loading…
Reference in New Issue
Block a user