mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-19 15:12: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;
|
package com.phonegap.demo;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import android.provider.Contacts.ContactMethods;
|
||||||
import android.provider.Contacts.People;
|
import android.provider.Contacts.People;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.webkit.WebView;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.ContentUris;
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
import android.database.sqlite.SQLiteException;
|
||||||
|
|
||||||
public class ContactManager {
|
public class ContactManager {
|
||||||
|
|
||||||
|
private static final String LOG_TAG = "Contact Query";
|
||||||
Activity mApp;
|
Activity mApp;
|
||||||
|
WebView mView;
|
||||||
Uri mPeople = android.provider.Contacts.People.CONTENT_URI;
|
Uri mPeople = android.provider.Contacts.People.CONTENT_URI;
|
||||||
Uri mPhone = android.provider.Contacts.Phones.CONTENT_URI;
|
Uri mPhone = android.provider.Contacts.Phones.CONTENT_URI;
|
||||||
Uri mEmail = android.provider.Contacts.ContactMethods.CONTENT_EMAIL_URI;
|
Uri mEmail = android.provider.Contacts.ContactMethods.CONTENT_URI;
|
||||||
|
|
||||||
ContactManager(Activity app)
|
ContactManager(Activity app, WebView view)
|
||||||
{
|
{
|
||||||
mApp = app;
|
mApp = app;
|
||||||
|
mView = view;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getColumnData(Cursor cur){
|
private void processResults(Cursor cur){
|
||||||
|
|
||||||
ContentResolver cr = mApp.getContentResolver();
|
|
||||||
|
|
||||||
if (cur.moveToFirst()) {
|
if (cur.moveToFirst()) {
|
||||||
|
|
||||||
String name;
|
String name;
|
||||||
String email;
|
|
||||||
String phoneNumber;
|
String phoneNumber;
|
||||||
|
String email;
|
||||||
|
|
||||||
int nameColumn = cur.getColumnIndex(People.NAME);
|
int nameColumn = cur.getColumnIndex(People.NAME);
|
||||||
int phoneColumn = cur.getColumnIndex(People.NUMBER);
|
int phoneColumn = cur.getColumnIndex(People.NUMBER);
|
||||||
|
int emailColumn = cur.getColumnIndex(ContactMethods.DATA);
|
||||||
do {
|
do {
|
||||||
// Get the field values
|
// Get the field values
|
||||||
name = cur.getString(nameColumn);
|
name = cur.getString(nameColumn);
|
||||||
phoneNumber = cur.getString(phoneColumn);
|
phoneNumber = cur.getString(phoneColumn);
|
||||||
|
email = cur.getString(emailColumn);
|
||||||
|
|
||||||
|
mView.loadUrl("javascript:navigator.addressBook.droidFoundContact('" + name + "','" + phoneNumber + "','" + email +")");
|
||||||
|
|
||||||
} while (cur.moveToNext());
|
} while (cur.moveToNext());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void search(String rawdata)
|
||||||
public void findContacts()
|
|
||||||
{
|
{
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
// Form an array specifying which columns to return.
|
|
||||||
String[] projection = new String[] {
|
String[] projection = new String[] {
|
||||||
People._ID,
|
ContactMethods._ID,
|
||||||
People.NAME,
|
People.NAME,
|
||||||
People.NUMBER
|
People.NUMBER,
|
||||||
|
ContactMethods.DATA
|
||||||
};
|
};
|
||||||
|
|
||||||
// Make the query.
|
String[] params = new String[] { name, phone, email };
|
||||||
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");
|
|
||||||
|
|
||||||
this.getColumnData(managedCursor);
|
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);
|
geo = new GeoBroker(appView, this);
|
||||||
accel = new AccelListener(this, appView);
|
accel = new AccelListener(this, appView);
|
||||||
launcher = new CameraLauncher(appView, this);
|
launcher = new CameraLauncher(appView, this);
|
||||||
mContacts = new ContactManager(this);
|
mContacts = new ContactManager(this, appView);
|
||||||
// This creates the new javascript interfaces for PhoneGap
|
// This creates the new javascript interfaces for PhoneGap
|
||||||
appView.addJavascriptInterface(gap, "DroidGap");
|
appView.addJavascriptInterface(gap, "DroidGap");
|
||||||
appView.addJavascriptInterface(geo, "Geo");
|
appView.addJavascriptInterface(geo, "Geo");
|
||||||
|
Loading…
Reference in New Issue
Block a user