mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-20 23:56:20 +08:00
Merge remote branch 'macdonst-contactSpec/contactSpec'
This commit is contained in:
commit
36984f4697
@ -101,11 +101,10 @@
|
||||
|
||||
function get_contacts()
|
||||
{
|
||||
var obj = new Contact();
|
||||
var name = new ContactName();
|
||||
name.givenName = '';
|
||||
obj.name = name;
|
||||
navigator.service.contacts.find(obj, count_contacts, fail);
|
||||
var obj = new ContactFindOptions();
|
||||
obj.filter="";
|
||||
obj.multiple=true;
|
||||
navigator.service.contacts.find(["name", "phone", "email"], count_contacts, fail, obj);
|
||||
}
|
||||
|
||||
function count_contacts(contacts)
|
||||
|
@ -2,117 +2,86 @@
|
||||
var Contact = function(id, displayName, name, nickname, phoneNumbers, emails, addresses,
|
||||
ims, organizations, published, updated, birthday, anniversary, gender, note,
|
||||
preferredUsername, photos, tags, relationships, urls, accounts, utcOffset, connected) {
|
||||
this.id = id || '';
|
||||
this.displayName = displayName || '';
|
||||
this.id = id || null;
|
||||
this.displayName = displayName || null;
|
||||
this.name = name || null; // ContactName
|
||||
this.nickname = nickname || '';
|
||||
this.nickname = nickname || null;
|
||||
this.phoneNumbers = phoneNumbers || null; // ContactField[]
|
||||
this.emails = emails || null; // ContactField[]
|
||||
this.addresses = addresses || null; // ContactAddress[]
|
||||
this.ims = ims || null; // ContactField[]
|
||||
this.organizations = organizations || null; // ContactOrganization[]
|
||||
this.published = published || '';
|
||||
this.updated = updated || '';
|
||||
this.birthday = birthday || '';
|
||||
this.anniversary = anniversary || '';
|
||||
this.gender = gender || '';
|
||||
this.note = note || '';
|
||||
this.preferredUsername = preferredUsername || '';
|
||||
this.published = published || null;
|
||||
this.updated = updated || null;
|
||||
this.birthday = birthday || null;
|
||||
this.anniversary = anniversary || null;
|
||||
this.gender = gender || null;
|
||||
this.note = note || null;
|
||||
this.preferredUsername = preferredUsername || null;
|
||||
this.photos = photos || null; // ContactField[]
|
||||
this.tags = tags || null; // ContactField[]
|
||||
this.relationships = relationships || null; // ContactField[]
|
||||
this.urls = urls || null; // ContactField[]
|
||||
this.accounts = accounts || null; // ContactAccount[]
|
||||
this.utcOffset = utcOffset || '';
|
||||
this.connected = connected || '';
|
||||
this.utcOffset = utcOffset || null;
|
||||
this.connected = connected || null;
|
||||
};
|
||||
|
||||
var ContactName = function(formatted, familyName, givenName, middle, prefix, suffix) {
|
||||
this.formatted = formatted || '';
|
||||
this.familyName = familyName || '';
|
||||
this.givenName = givenName || '';
|
||||
this.middleName = middle || '';
|
||||
this.honorificPrefix = prefix || '';
|
||||
this.honorificSuffix = suffix || '';
|
||||
this.formatted = formatted || null;
|
||||
this.familyName = familyName || null;
|
||||
this.givenName = givenName || null;
|
||||
this.middleName = middle || null;
|
||||
this.honorificPrefix = prefix || null;
|
||||
this.honorificSuffix = suffix || null;
|
||||
};
|
||||
|
||||
var ContactField = function(type, value, primary) {
|
||||
this.type = type || '';
|
||||
this.value = value || '';
|
||||
this.primary = primary || '';
|
||||
this.type = type || null;
|
||||
this.value = value || null;
|
||||
this.primary = primary || null;
|
||||
};
|
||||
|
||||
var ContactAddress = function(formatted, streetAddress, locality, region, postalCode, country) {
|
||||
this.formatted = formatted || '';
|
||||
this.streetAddress = streetAddress || '';
|
||||
this.locality = locality || '';
|
||||
this.region = region || '';
|
||||
this.postalCode = postalCode || '';
|
||||
this.country = country || '';
|
||||
this.formatted = formatted || null;
|
||||
this.streetAddress = streetAddress || null;
|
||||
this.locality = locality || null;
|
||||
this.region = region || null;
|
||||
this.postalCode = postalCode || null;
|
||||
this.country = country || null;
|
||||
};
|
||||
|
||||
var ContactOrganization = function(name, dept, title, startDate, endDate, location, desc) {
|
||||
this.name = name || '';
|
||||
this.department = dept || '';
|
||||
this.title = title || '';
|
||||
this.startDate = startDate || '';
|
||||
this.endDate = endDate || '';
|
||||
this.location = location || '';
|
||||
this.description = desc || '';
|
||||
this.name = name || null;
|
||||
this.department = dept || null;
|
||||
this.title = title || null;
|
||||
this.startDate = startDate || null;
|
||||
this.endDate = endDate || null;
|
||||
this.location = location || null;
|
||||
this.description = desc || null;
|
||||
};
|
||||
|
||||
var ContactAccount = function(domain, username, userid) {
|
||||
this.domain = domain || '';
|
||||
this.username = username || '';
|
||||
this.userid = userid || '';
|
||||
this.domain = domain || null;
|
||||
this.username = username || null;
|
||||
this.userid = userid || null;
|
||||
}
|
||||
|
||||
var Contacts = function() {
|
||||
this.inProgress = false;
|
||||
this.records = [];
|
||||
this.records = new Array();
|
||||
}
|
||||
|
||||
Contacts.prototype.find = function(obj, win, fail) {
|
||||
// Contacts.prototype.find = function(obj, win, fail) {
|
||||
Contacts.prototype.find = function(fields, win, fail, options) {
|
||||
this.win = win;
|
||||
this.fail = fail;
|
||||
if(obj.name != null) {
|
||||
// Build up the search term that we'll use in SQL, based on the structure/contents of the contact object passed into find.
|
||||
var searchTerm = '';
|
||||
if (obj.name.givenName && obj.name.givenName.length > 0) {
|
||||
searchTerm = obj.name.givenName.split(' ').join('%');
|
||||
}
|
||||
if (obj.name.familyName && obj.name.familyName.length > 0) {
|
||||
searchTerm += obj.name.familyName.split(' ').join('%');
|
||||
}
|
||||
if (!obj.name.familyName && !obj.name.givenName && obj.name.formatted) {
|
||||
searchTerm = obj.name.formatted;
|
||||
}
|
||||
PhoneGap.execAsync(null, null, "Contacts", "search", [searchTerm, "", ""]);
|
||||
}
|
||||
|
||||
PhoneGap.execAsync(null, null, "Contacts", "search", [fields, options]);
|
||||
};
|
||||
|
||||
Contacts.prototype.droidFoundContact = function(name, npa, email) {
|
||||
this.records = new Array();
|
||||
var contact = new Contact();
|
||||
contact.name = new ContactName();
|
||||
contact.name.formatted = name;
|
||||
contact.name.givenName = name;
|
||||
contact.emails = new Array();
|
||||
var mail = new ContactField();
|
||||
mail.type = "home";
|
||||
mail.value = email;
|
||||
mail.primary = true;
|
||||
contact.emails.push(mail);
|
||||
contact.phones = new Array();
|
||||
phone = new ContactField();
|
||||
phone.type = "home";
|
||||
phone.value = npa;
|
||||
contact.phones.push(phone);
|
||||
this.records.push(contact);
|
||||
};
|
||||
|
||||
Contacts.prototype.droidDone = function() {
|
||||
this.win(this.records);
|
||||
Contacts.prototype.droidDone = function(contacts) {
|
||||
this.win(eval('(' + contacts + ')'));
|
||||
};
|
||||
|
||||
Contacts.prototype.remove = function(contact) {
|
||||
@ -135,11 +104,12 @@ Contacts.prototype.m_foundContacts = function(win, contacts) {
|
||||
var ContactFindOptions = function(filter, multiple, limit, updatedSince) {
|
||||
this.filter = filter || '';
|
||||
this.multiple = multiple || true;
|
||||
this.limit = limit || 0;
|
||||
this.limit = limit || Number.MAX_VALUE;
|
||||
this.updatedSince = updatedSince || '';
|
||||
};
|
||||
|
||||
var ContactError = function() {
|
||||
this.code=null;
|
||||
};
|
||||
|
||||
ContactError.INVALID_ARGUMENT_ERROR = 0;
|
||||
@ -152,6 +122,6 @@ ContactError.TIMEOUT_ERROR = 6;
|
||||
ContactError.UNKNOWN_ERROR = 7;
|
||||
|
||||
PhoneGap.addConstructor(function() {
|
||||
if(typeof navigator.service == "undefined") navigator.service = new Object();
|
||||
if(typeof navigator.service == "undefined") navigator.service = new Object();
|
||||
if(typeof navigator.service.contacts == "undefined") navigator.service.contacts = new Contacts();
|
||||
});
|
||||
|
@ -290,6 +290,28 @@ PhoneGap.stringify = function(args) {
|
||||
if ((type == "number") || (type == "boolean")) {
|
||||
s = s + args[i];
|
||||
}
|
||||
else if (args[i] instanceof Array) {
|
||||
s = s + "[" + args[i] + "]";
|
||||
}
|
||||
else if (args[i] instanceof Object) {
|
||||
var start = true;
|
||||
s = s + '{';
|
||||
for (var name in args[i]) {
|
||||
if (!start) {
|
||||
s = s + ',';
|
||||
}
|
||||
s = s + '"' + name + '":';
|
||||
var nameType = typeof args[i][name];
|
||||
if ((nameType == "number") || (nameType == "boolean")) {
|
||||
s = s + args[i][name];
|
||||
}
|
||||
else {
|
||||
s = s + '"' + args[i][name] + '"';
|
||||
}
|
||||
start=false;
|
||||
}
|
||||
s = s + '}';
|
||||
}
|
||||
else {
|
||||
s = s + '"' + args[i] + '"';
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -21,12 +21,11 @@ package com.phonegap;
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.webkit.WebView;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* This abstract class defines SDK-independent API for communication with
|
||||
* Contacts Provider. The actual implementation used by the application depends
|
||||
@ -36,13 +35,6 @@ import android.webkit.WebView;
|
||||
*/
|
||||
public abstract class ContactAccessor {
|
||||
|
||||
public class ContactTriplet
|
||||
{
|
||||
public String name = "";
|
||||
public String email = "";
|
||||
public String phone = "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Static singleton instance of {@link ContactAccessor} holding the
|
||||
* SDK-specific implementation of the class.
|
||||
@ -94,5 +86,5 @@ public abstract class ContactAccessor {
|
||||
/**
|
||||
* Handles searching through SDK-specific contacts API.
|
||||
*/
|
||||
public abstract void search(String name, String npa, String email);
|
||||
public abstract void search(JSONArray filter, JSONObject options);
|
||||
}
|
@ -17,13 +17,25 @@
|
||||
|
||||
package com.phonegap;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.AsyncQueryHandler;
|
||||
import android.content.ContentResolver;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteException;
|
||||
import android.net.Uri;
|
||||
import android.provider.Contacts.ContactMethods;
|
||||
import android.provider.Contacts.ContactMethodsColumns;
|
||||
import android.provider.Contacts.Organizations;
|
||||
import android.provider.Contacts.People;
|
||||
import android.provider.Contacts.Phones;
|
||||
import android.util.Log;
|
||||
import android.webkit.WebView;
|
||||
|
||||
@ -42,10 +54,23 @@ import android.webkit.WebView;
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class ContactAccessorSdk3_4 extends ContactAccessor {
|
||||
|
||||
private Uri mPeople = android.provider.Contacts.People.CONTENT_URI;
|
||||
private Uri mPhone = android.provider.Contacts.Phones.CONTENT_URI;
|
||||
private Uri mEmail = android.provider.Contacts.ContactMethods.CONTENT_URI;
|
||||
private static final Map<String, String> dbMap = new HashMap<String, String>();
|
||||
static {
|
||||
dbMap.put("id", People._ID);
|
||||
dbMap.put("displayName", People.DISPLAY_NAME);
|
||||
dbMap.put("phoneNumbers", Phones.NUMBER);
|
||||
dbMap.put("phoneNumbers.value", Phones.NUMBER);
|
||||
dbMap.put("emails", ContactMethods.DATA);
|
||||
dbMap.put("emails.value", ContactMethods.DATA);
|
||||
dbMap.put("addresses", ContactMethodsColumns.DATA);
|
||||
dbMap.put("addresses.formatted", ContactMethodsColumns.DATA);
|
||||
dbMap.put("ims", ContactMethodsColumns.DATA);
|
||||
dbMap.put("ims.value", ContactMethodsColumns.DATA);
|
||||
dbMap.put("organizations", Organizations.COMPANY);
|
||||
dbMap.put("organizations.name", Organizations.COMPANY);
|
||||
dbMap.put("organizations.title", Organizations.TITLE);
|
||||
dbMap.put("note", People.NOTES);
|
||||
}
|
||||
|
||||
public ContactAccessorSdk3_4(WebView view, Activity app)
|
||||
{
|
||||
@ -54,234 +79,292 @@ public class ContactAccessorSdk3_4 extends ContactAccessor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void search(String name, String npa, String email) {
|
||||
if (email.length() > 0)
|
||||
searchByEmail(email);
|
||||
else
|
||||
searchPeople(name, npa);
|
||||
public void search(JSONArray filter, JSONObject options) {
|
||||
String searchTerm = "";
|
||||
int limit = Integer.MAX_VALUE;
|
||||
boolean multiple = true;
|
||||
try {
|
||||
searchTerm = options.getString("filter");
|
||||
if (searchTerm.length()==0) {
|
||||
searchTerm = "%";
|
||||
}
|
||||
else {
|
||||
searchTerm = "%" + searchTerm + "%";
|
||||
}
|
||||
multiple = options.getBoolean("multiple");
|
||||
if (multiple) {
|
||||
limit = options.getInt("limit");
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
|
||||
ContentResolver cr = mApp.getContentResolver();
|
||||
|
||||
Set<String> contactIds = buildSetOfContactIds(filter, searchTerm);
|
||||
|
||||
Iterator<String> it = contactIds.iterator();
|
||||
|
||||
JSONArray contacts = new JSONArray();
|
||||
JSONObject contact;
|
||||
String contactId;
|
||||
int pos = 0;
|
||||
while (it.hasNext() && (pos < limit)) {
|
||||
contact = new JSONObject();
|
||||
try {
|
||||
contactId = it.next();
|
||||
contact.put("id", contactId);
|
||||
|
||||
// Do query for name and note
|
||||
// Right now we are just querying the displayName
|
||||
Cursor cur = cr.query(People.CONTENT_URI,
|
||||
null,
|
||||
"people._id = ?",
|
||||
new String[] {contactId},
|
||||
null);
|
||||
cur.moveToFirst();
|
||||
|
||||
// name
|
||||
contact.put("displayName", cur.getString(cur.getColumnIndex(People.DISPLAY_NAME)));
|
||||
// phone number
|
||||
contact.put("phoneNumbers", phoneQuery(cr, contactId));
|
||||
// email
|
||||
contact.put("emails", emailQuery(cr, contactId));
|
||||
// addresses
|
||||
contact.put("addresses", addressQuery(cr, contactId));
|
||||
// organizations
|
||||
contact.put("organizations", organizationQuery(cr, contactId));
|
||||
// ims
|
||||
contact.put("ims", imQuery(cr, contactId));
|
||||
// note
|
||||
contact.put("note", cur.getString(cur.getColumnIndex(People.NOTES)));
|
||||
// nickname
|
||||
// urls
|
||||
// relationship
|
||||
// birthdays
|
||||
// anniversary
|
||||
|
||||
pos++;
|
||||
cur.close();
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
contacts.put(contact);
|
||||
}
|
||||
mView.loadUrl("javascript:navigator.service.contacts.droidDone('" + contacts.toString() + "');");
|
||||
}
|
||||
|
||||
private void searchByEmail(String email)
|
||||
{
|
||||
String[] projection = new String[] {
|
||||
ContactMethods._ID,
|
||||
ContactMethods.DATA,
|
||||
ContactMethods.KIND,
|
||||
ContactMethods.PERSON_ID
|
||||
};
|
||||
String[] variables = new String[] {
|
||||
email
|
||||
};
|
||||
private Set<String> buildSetOfContactIds(JSONArray filter, String searchTerm) {
|
||||
Set<String> contactIds = new HashSet<String>();
|
||||
|
||||
try{
|
||||
Cursor myCursor = mApp.managedQuery(mEmail, projection,
|
||||
"contact_methods." + ContactMethods.DATA + " = ?" + "AND contact_methods.kind = 1", variables , ContactMethods.DATA + " ASC");
|
||||
getMethodData(myCursor);
|
||||
|
||||
String key;
|
||||
try {
|
||||
for (int i=0; i<filter.length(); i++) {
|
||||
key = filter.getString(i);
|
||||
if (key.startsWith("displayName")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
People.CONTENT_URI,
|
||||
People._ID,
|
||||
dbMap.get(key) + " LIKE ?",
|
||||
new String[] {searchTerm});
|
||||
}
|
||||
// else if (key.startsWith("name")) {
|
||||
// Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
// doQuery(searchTerm, contactIds,
|
||||
// ContactsContract.Data.CONTENT_URI,
|
||||
// ContactsContract.Data.CONTACT_ID,
|
||||
// dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
|
||||
// new String[] {searchTerm, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE});
|
||||
// }
|
||||
else if (key.startsWith("phoneNumbers")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
Phones.CONTENT_URI,
|
||||
Phones.PERSON_ID,
|
||||
dbMap.get(key) + " LIKE ?",
|
||||
new String[] {searchTerm});
|
||||
}
|
||||
else if (key.startsWith("emails")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
ContactMethods.CONTENT_EMAIL_URI,
|
||||
ContactMethods.PERSON_ID,
|
||||
dbMap.get(key) + " LIKE ? AND " + ContactMethods.KIND + " = ?",
|
||||
new String[] {searchTerm, ContactMethods.CONTENT_EMAIL_ITEM_TYPE});
|
||||
}
|
||||
else if (key.startsWith("addresses")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
ContactMethods.CONTENT_URI,
|
||||
ContactMethods.PERSON_ID,
|
||||
dbMap.get(key) + " LIKE ? AND " + ContactMethods.KIND + " = ?",
|
||||
new String[] {searchTerm, ContactMethods.CONTENT_POSTAL_ITEM_TYPE});
|
||||
}
|
||||
else if (key.startsWith("ims")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
ContactMethods.CONTENT_URI,
|
||||
ContactMethods.PERSON_ID,
|
||||
dbMap.get(key) + " LIKE ? AND " + ContactMethods.KIND + " = ?",
|
||||
new String[] {searchTerm, ContactMethods.CONTENT_IM_ITEM_TYPE});
|
||||
}
|
||||
else if (key.startsWith("organizations")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
Organizations.CONTENT_URI,
|
||||
ContactMethods.PERSON_ID,
|
||||
dbMap.get(key) + " LIKE ?",
|
||||
new String[] {searchTerm});
|
||||
}
|
||||
else if (key.startsWith("note")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
People.CONTENT_URI,
|
||||
People._ID,
|
||||
dbMap.get(key) + " LIKE ?",
|
||||
new String[] {searchTerm});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SQLiteException ex)
|
||||
{
|
||||
Log.d(this.LOG_TAG, ex.getMessage());
|
||||
catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
|
||||
|
||||
return contactIds;
|
||||
}
|
||||
|
||||
private void doQuery(String searchTerm, Set<String> contactIds,
|
||||
Uri uri, String projection, String selection, String[] selectionArgs) {
|
||||
ContentResolver cr = mApp.getContentResolver();
|
||||
|
||||
Cursor cursor = cr.query(
|
||||
uri,
|
||||
null,
|
||||
selection,
|
||||
selectionArgs,
|
||||
null);
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
Log.d(LOG_TAG, "ID = " + cursor.getString(cursor.getColumnIndex(projection)));
|
||||
contactIds.add(cursor.getString(cursor.getColumnIndex(projection)));
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
private JSONArray imQuery(ContentResolver cr, String contactId) {
|
||||
String imWhere = ContactMethods.PERSON_ID
|
||||
+ " = ? AND " + ContactMethods.KIND + " = ?";
|
||||
String[] imWhereParams = new String[]{contactId, ContactMethods.CONTENT_IM_ITEM_TYPE};
|
||||
Cursor cursor = cr.query(ContactMethods.CONTENT_URI,
|
||||
null, imWhere, imWhereParams, null);
|
||||
JSONArray ims = new JSONArray();
|
||||
JSONObject im;
|
||||
while (cursor.moveToNext()) {
|
||||
im = new JSONObject();
|
||||
try{
|
||||
Cursor myCursor = mApp.managedQuery(mPeople, projection,
|
||||
conditions, variables , People.NAME + " ASC");
|
||||
processResults(myCursor, false);
|
||||
im.put("primary", false);
|
||||
im.put("value", cursor.getString(
|
||||
cursor.getColumnIndex(ContactMethodsColumns.DATA)));
|
||||
im.put("type", cursor.getString(
|
||||
cursor.getColumnIndex(ContactMethodsColumns.TYPE)));
|
||||
ims.put(im);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
catch (SQLiteException ex)
|
||||
{
|
||||
Log.d(this.LOG_TAG, ex.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
cursor.close();
|
||||
return null;
|
||||
}
|
||||
|
||||
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)
|
||||
mView.loadUrl("javascript:navigator.service.ContactManager.droidAddContact('" + name + "','" + phoneNumber + "','" + email +"')");
|
||||
else
|
||||
mView.loadUrl("javascript:navigator.service.contacts.droidFoundContact('" + name + "','" + phoneNumber + "','" + email +"')");
|
||||
|
||||
} while (cur.moveToNext());
|
||||
if (all)
|
||||
mView.loadUrl("javascript:navigator.service.ContactManager.droidDone()");
|
||||
else
|
||||
mView.loadUrl("javascript:navigator.service.contacts.droidDone();");
|
||||
}
|
||||
else
|
||||
{
|
||||
if(all)
|
||||
mView.loadUrl("javascript:navigator.service.ContactManager.fail()");
|
||||
else
|
||||
mView.loadUrl("javascript:navigator.service.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;
|
||||
mView.loadUrl("javascript:navigator.service.Contacts.droidFoundContact('" + data.name + "','" + data.phone + "','" + data.email +"')");
|
||||
}
|
||||
} while (cur.moveToNext());
|
||||
mView.loadUrl("javascript:navigator.service.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 = mApp.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 JSONArray organizationQuery(ContentResolver cr, String contactId) {
|
||||
String orgWhere = ContactMethods.PERSON_ID + " = ?";
|
||||
String[] orgWhereParams = new String[]{contactId};
|
||||
Cursor cursor = cr.query(Organizations.CONTENT_URI,
|
||||
null, orgWhere, orgWhereParams, null);
|
||||
JSONArray organizations = new JSONArray();
|
||||
JSONObject organization;
|
||||
while (cursor.moveToNext()) {
|
||||
organization = new JSONObject();
|
||||
try{
|
||||
organization.put("name", cursor.getString(cursor.getColumnIndex(Organizations.COMPANY)));
|
||||
organization.put("title", cursor.getString(cursor.getColumnIndex(Organizations.TITLE)));
|
||||
// organization.put("department", cursor.getString(cursor.getColumnIndex(Organizations)));
|
||||
// organization.put("description", cursor.getString(cursor.getColumnIndex(Organizations)));
|
||||
// organization.put("endDate", cursor.getString(cursor.getColumnIndex(Organizations)));
|
||||
// organization.put("location", cursor.getString(cursor.getColumnIndex(Organizations)));
|
||||
// organization.put("startDate", cursor.getString(cursor.getColumnIndex(Organizations)));
|
||||
organizations.put(organization);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
return organizations;
|
||||
}
|
||||
|
||||
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 = getEmailColumnData(myCursor);
|
||||
}
|
||||
catch (SQLiteException ex)
|
||||
{
|
||||
Log.d(LOG_TAG, ex.getMessage());
|
||||
}
|
||||
|
||||
return email;
|
||||
private JSONArray addressQuery(ContentResolver cr, String contactId) {
|
||||
String addrWhere = ContactMethods.PERSON_ID
|
||||
+ " = ? AND " + ContactMethods.KIND + " = ?";
|
||||
String[] addrWhereParams = new String[]{contactId,
|
||||
ContactMethods.CONTENT_POSTAL_ITEM_TYPE};
|
||||
Cursor cursor = cr.query(ContactMethods.CONTENT_URI,
|
||||
null, addrWhere, addrWhereParams, null);
|
||||
JSONArray addresses = new JSONArray();
|
||||
JSONObject address;
|
||||
while (cursor.moveToNext()) {
|
||||
address = new JSONObject();
|
||||
try{
|
||||
address.put("formatted", cursor.getString(cursor.getColumnIndex(ContactMethodsColumns.DATA)));
|
||||
addresses.put(address);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
return addresses;
|
||||
}
|
||||
|
||||
private JSONArray phoneQuery(ContentResolver cr, String contactId) {
|
||||
Cursor cursor = cr.query(
|
||||
Phones.CONTENT_URI,
|
||||
null,
|
||||
Phones.PERSON_ID +" = ?",
|
||||
new String[]{contactId}, null);
|
||||
JSONArray phones = new JSONArray();
|
||||
JSONObject phone;
|
||||
while (cursor.moveToNext()) {
|
||||
phone = new JSONObject();
|
||||
try{
|
||||
phone.put("primary", false);
|
||||
phone.put("value", cursor.getString(cursor.getColumnIndex(Phones.NUMBER)));
|
||||
phone.put("type", cursor.getString(cursor.getColumnIndex(Phones.TYPE)));
|
||||
phones.put(phone);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
return phones;
|
||||
}
|
||||
|
||||
private JSONArray emailQuery(ContentResolver cr, String contactId) {
|
||||
Cursor cursor = cr.query(
|
||||
ContactMethods.CONTENT_EMAIL_URI,
|
||||
null,
|
||||
ContactMethods.PERSON_ID +" = ?",
|
||||
new String[]{contactId}, null);
|
||||
JSONArray emails = new JSONArray();
|
||||
JSONObject email;
|
||||
while (cursor.moveToNext()) {
|
||||
email = new JSONObject();
|
||||
try{
|
||||
email.put("primary", false);
|
||||
email.put("value", cursor.getString(cursor.getColumnIndex(ContactMethods.DATA)));
|
||||
// TODO Find out why adding an email type throws and exception
|
||||
//email.put("type", cursor.getString(cursor.getColumnIndex(ContactMethods.TYPE)));
|
||||
emails.put(email);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
return emails;
|
||||
}
|
||||
}
|
@ -17,16 +17,21 @@
|
||||
|
||||
package com.phonegap;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteException;
|
||||
import android.net.Uri;
|
||||
import android.provider.ContactsContract;
|
||||
import android.provider.Contacts.People;
|
||||
import android.provider.ContactsContract.Contacts;
|
||||
import android.provider.ContactsContract.CommonDataKinds.Phone;
|
||||
import android.util.Log;
|
||||
import android.webkit.WebView;
|
||||
|
||||
@ -50,59 +55,512 @@ import android.webkit.WebView;
|
||||
*/
|
||||
public class ContactAccessorSdk5 extends ContactAccessor {
|
||||
|
||||
public ContactAccessorSdk5(WebView view, Activity app)
|
||||
private static final String WHERE_STRING = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
|
||||
private static final Map<String, String> dbMap = new HashMap<String, String>();
|
||||
static {
|
||||
dbMap.put("id", ContactsContract.Contacts._ID);
|
||||
dbMap.put("displayName", ContactsContract.Contacts.DISPLAY_NAME);
|
||||
dbMap.put("name", ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
|
||||
dbMap.put("name.formatted", ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
|
||||
dbMap.put("name.familyName", ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
|
||||
dbMap.put("name.givenName", ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
|
||||
dbMap.put("name.middleName", ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME);
|
||||
dbMap.put("name.honorificPrefix", ContactsContract.CommonDataKinds.StructuredName.PREFIX);
|
||||
dbMap.put("name.honorificSuffix", ContactsContract.CommonDataKinds.StructuredName.SUFFIX);
|
||||
dbMap.put("nickname", ContactsContract.CommonDataKinds.Nickname.NAME);
|
||||
dbMap.put("phoneNumbers", ContactsContract.CommonDataKinds.Phone.NUMBER);
|
||||
dbMap.put("phoneNumbers.value", ContactsContract.CommonDataKinds.Phone.NUMBER);
|
||||
dbMap.put("emails", ContactsContract.CommonDataKinds.Email.DATA);
|
||||
dbMap.put("emails.value", ContactsContract.CommonDataKinds.Email.DATA);
|
||||
dbMap.put("addresses", ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
|
||||
dbMap.put("addresses.formatted", ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
|
||||
dbMap.put("addresses.streetAddress", ContactsContract.CommonDataKinds.StructuredPostal.STREET);
|
||||
dbMap.put("addresses.locality", ContactsContract.CommonDataKinds.StructuredPostal.CITY);
|
||||
dbMap.put("addresses.region", ContactsContract.CommonDataKinds.StructuredPostal.REGION);
|
||||
dbMap.put("addresses.postalCode", ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE);
|
||||
dbMap.put("addresses.country", ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY);
|
||||
dbMap.put("ims", ContactsContract.CommonDataKinds.Im.DATA);
|
||||
dbMap.put("ims.value", ContactsContract.CommonDataKinds.Im.DATA);
|
||||
dbMap.put("organizations", ContactsContract.CommonDataKinds.Organization.COMPANY);
|
||||
dbMap.put("organizations.name", ContactsContract.CommonDataKinds.Organization.COMPANY);
|
||||
dbMap.put("organizations.department", ContactsContract.CommonDataKinds.Organization.DEPARTMENT);
|
||||
dbMap.put("organizations.title", ContactsContract.CommonDataKinds.Organization.TITLE);
|
||||
dbMap.put("organizations.location", ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION);
|
||||
dbMap.put("organizations.description", ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION);
|
||||
//dbMap.put("published", null);
|
||||
//dbMap.put("updated", null);
|
||||
dbMap.put("birthday", ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
|
||||
dbMap.put("anniversary", ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
|
||||
//dbMap.put("gender", null);
|
||||
dbMap.put("note", ContactsContract.CommonDataKinds.Note.NOTE);
|
||||
//dbMap.put("preferredUsername", null);
|
||||
//dbMap.put("photos.value", null);
|
||||
//dbMap.put("tags.value", null);
|
||||
dbMap.put("relationships", ContactsContract.CommonDataKinds.Relation.NAME);
|
||||
dbMap.put("relationships.value", ContactsContract.CommonDataKinds.Relation.NAME);
|
||||
dbMap.put("urls", ContactsContract.CommonDataKinds.Website.URL);
|
||||
dbMap.put("urls.value", ContactsContract.CommonDataKinds.Website.URL);
|
||||
//dbMap.put("accounts.domain", null);
|
||||
//dbMap.put("accounts.username", null);
|
||||
//dbMap.put("accounts.userid", null);
|
||||
//dbMap.put("utcOffset", null);
|
||||
//dbMap.put("connected", null);
|
||||
}
|
||||
|
||||
public ContactAccessorSdk5(WebView view, Activity app)
|
||||
{
|
||||
mApp = app;
|
||||
mView = view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void search(String name, String npa, String email) {
|
||||
if (name.length()==0) name = "%";
|
||||
// Get a cursor by creating the query.
|
||||
// TODO: parse name/number/email and dispatch to different query types.
|
||||
// Right now assumption is only name search. Lame but I'm on time constraints.
|
||||
ContentResolver cr = mApp.getContentResolver();
|
||||
Cursor cursor = cr.query(
|
||||
ContactsContract.Contacts.CONTENT_URI,
|
||||
new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.HAS_PHONE_NUMBER, ContactsContract.Contacts.DISPLAY_NAME},
|
||||
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?",
|
||||
new String[] {name},
|
||||
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
|
||||
while (cursor.moveToNext()) {
|
||||
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
|
||||
if (contactName.trim().length() == 0) continue;
|
||||
String phoneNumber = "null";
|
||||
String emailAddress = "null";
|
||||
|
||||
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
|
||||
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
|
||||
if (Boolean.parseBoolean(hasPhone)) {
|
||||
Cursor phones = cr.query(
|
||||
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
|
||||
null,
|
||||
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
|
||||
null, null);
|
||||
if (phones.moveToFirst()) {
|
||||
phoneNumber = "'" + phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace('\'', '`') + "'";
|
||||
}
|
||||
phones.close();
|
||||
public void search(JSONArray filter, JSONObject options) {
|
||||
String searchTerm = "";
|
||||
int limit = Integer.MAX_VALUE;
|
||||
boolean multiple = true;
|
||||
try {
|
||||
searchTerm = options.getString("filter");
|
||||
if (searchTerm.length()==0) {
|
||||
searchTerm = "%";
|
||||
}
|
||||
Cursor emails = cr.query(
|
||||
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
|
||||
null,
|
||||
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
|
||||
null, null);
|
||||
if (emails.moveToFirst()) {
|
||||
// This would allow you get several email addresses
|
||||
emailAddress = "'" + emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)).replace('\'', '`') + "'";
|
||||
}
|
||||
emails.close();
|
||||
String contactAddJS = "javascript:navigator.service.contacts.droidFoundContact('" + contactName.replace('\'', '`') + "'," + phoneNumber + "," + emailAddress +")";
|
||||
mView.loadUrl(contactAddJS);
|
||||
else {
|
||||
searchTerm = "%" + searchTerm + "%";
|
||||
}
|
||||
multiple = options.getBoolean("multiple");
|
||||
if (multiple) {
|
||||
limit = options.getInt("limit");
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
// Get a cursor by creating the query.
|
||||
ContentResolver cr = mApp.getContentResolver();
|
||||
|
||||
Set<String> contactIds = buildSetOfContactIds(filter, searchTerm);
|
||||
|
||||
Iterator<String> it = contactIds.iterator();
|
||||
|
||||
JSONArray contacts = new JSONArray();
|
||||
JSONObject contact;
|
||||
String contactId;
|
||||
int pos = 0;
|
||||
while (it.hasNext() && (pos < limit)) {
|
||||
contact = new JSONObject();
|
||||
contactId = it.next();
|
||||
Log.d(LOG_TAG, "Contact ID = " + contactId);
|
||||
|
||||
try {
|
||||
contact.put("id", contactId);
|
||||
contact.put("displayName", displayNameQuery(cr, contactId));
|
||||
contact.put("name", nameQuery(cr, contactId));
|
||||
contact.put("phoneNumbers", phoneQuery(cr, contactId));
|
||||
contact.put("emails", emailQuery(cr, contactId));
|
||||
contact.put("addresses", addressQuery(cr, contactId));
|
||||
contact.put("organizations", organizationQuery(cr, contactId));
|
||||
contact.put("ims",imQuery(cr, contactId));
|
||||
contact.put("note",noteQuery(cr, contactId));
|
||||
contact.put("nickname",nicknameQuery(cr, contactId));
|
||||
contact.put("urls",websiteQuery(cr, contactId));
|
||||
contact.put("relationships",relationshipQuery(cr, contactId));
|
||||
contact.put("birthday",birthdayQuery(cr, contactId));
|
||||
contact.put("anniversary",anniversaryQuery(cr, contactId));
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
|
||||
contacts.put(contact);
|
||||
pos++;
|
||||
}
|
||||
cursor.close();
|
||||
mView.loadUrl("javascript:navigator.service.contacts.droidDone();");
|
||||
mView.loadUrl("javascript:navigator.service.contacts.droidDone('" + contacts.toString() + "');");
|
||||
}
|
||||
|
||||
private Set<String> buildSetOfContactIds(JSONArray filter, String searchTerm) {
|
||||
Set<String> contactIds = new HashSet<String>();
|
||||
|
||||
String key;
|
||||
try {
|
||||
for (int i=0; i<filter.length(); i++) {
|
||||
key = filter.getString(i);
|
||||
if (key.startsWith("displayName")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
ContactsContract.Contacts.CONTENT_URI,
|
||||
ContactsContract.Contacts._ID,
|
||||
dbMap.get(key) + " LIKE ?",
|
||||
new String[] {searchTerm});
|
||||
}
|
||||
else if (key.startsWith("name")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
ContactsContract.Data.CONTENT_URI,
|
||||
ContactsContract.Data.CONTACT_ID,
|
||||
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
|
||||
new String[] {searchTerm, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE});
|
||||
}
|
||||
else if (key.startsWith("nickname")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
ContactsContract.Data.CONTENT_URI,
|
||||
ContactsContract.Data.CONTACT_ID,
|
||||
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
|
||||
new String[] {searchTerm, ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE});
|
||||
}
|
||||
else if (key.startsWith("phoneNumbers")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
|
||||
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
|
||||
dbMap.get(key) + " LIKE ?",
|
||||
new String[] {searchTerm});
|
||||
}
|
||||
else if (key.startsWith("emails")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
|
||||
ContactsContract.CommonDataKinds.Email.CONTACT_ID,
|
||||
dbMap.get(key) + " LIKE ?",
|
||||
new String[] {searchTerm});
|
||||
}
|
||||
else if (key.startsWith("addresses")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
ContactsContract.Data.CONTENT_URI,
|
||||
ContactsContract.Data.CONTACT_ID,
|
||||
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
|
||||
new String[] {searchTerm, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE});
|
||||
}
|
||||
else if (key.startsWith("ims")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
ContactsContract.Data.CONTENT_URI,
|
||||
ContactsContract.Data.CONTACT_ID,
|
||||
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
|
||||
new String[] {searchTerm, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE});
|
||||
}
|
||||
else if (key.startsWith("organizations")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
ContactsContract.Data.CONTENT_URI,
|
||||
ContactsContract.Data.CONTACT_ID,
|
||||
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
|
||||
new String[] {searchTerm, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE});
|
||||
}
|
||||
else if (key.startsWith("birthday")) {
|
||||
|
||||
}
|
||||
else if (key.startsWith("anniversary")) {
|
||||
|
||||
}
|
||||
else if (key.startsWith("note")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
ContactsContract.Data.CONTENT_URI,
|
||||
ContactsContract.Data.CONTACT_ID,
|
||||
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
|
||||
new String[] {searchTerm, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE});
|
||||
}
|
||||
else if (key.startsWith("relationships")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
ContactsContract.Data.CONTENT_URI,
|
||||
ContactsContract.Data.CONTACT_ID,
|
||||
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
|
||||
new String[] {searchTerm, ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE});
|
||||
}
|
||||
else if (key.startsWith("urls")) {
|
||||
Log.d(LOG_TAG, "Doing " + key + " query");
|
||||
doQuery(searchTerm, contactIds,
|
||||
ContactsContract.Data.CONTENT_URI,
|
||||
ContactsContract.Data.CONTACT_ID,
|
||||
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
|
||||
new String[] {searchTerm, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
|
||||
return contactIds;
|
||||
}
|
||||
|
||||
private void doQuery(String searchTerm, Set<String> contactIds,
|
||||
Uri uri, String projection, String selection, String[] selectionArgs) {
|
||||
// Get a cursor by creating the query.
|
||||
ContentResolver cr = mApp.getContentResolver();
|
||||
|
||||
Cursor cursor = cr.query(
|
||||
uri,
|
||||
new String[] {projection},
|
||||
selection,
|
||||
selectionArgs,
|
||||
null);
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
Log.d(LOG_TAG, "ID = " + cursor.getString(cursor.getColumnIndex(projection)));
|
||||
contactIds.add(cursor.getString(cursor.getColumnIndex(projection)));
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
private String displayNameQuery(ContentResolver cr, String contactId) {
|
||||
Cursor cursor = cr.query(
|
||||
ContactsContract.Contacts.CONTENT_URI,
|
||||
new String[] {ContactsContract.Contacts.DISPLAY_NAME},
|
||||
ContactsContract.Contacts._ID + " = ?",
|
||||
new String[] {contactId},
|
||||
null);
|
||||
cursor.moveToFirst();
|
||||
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
|
||||
cursor.close();
|
||||
return displayName;
|
||||
}
|
||||
|
||||
private JSONArray organizationQuery(ContentResolver cr, String contactId) {
|
||||
String[] orgWhereParams = new String[]{contactId,
|
||||
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
|
||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||
null, WHERE_STRING, orgWhereParams, null);
|
||||
JSONArray organizations = new JSONArray();
|
||||
JSONObject organization = new JSONObject();
|
||||
while (cursor.moveToNext()) {
|
||||
try {
|
||||
organization.put("department", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DEPARTMENT)));
|
||||
organization.put("description", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION)));
|
||||
// TODO No endDate
|
||||
// organization.put("endDate", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization)));
|
||||
organization.put("location", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION)));
|
||||
organization.put("name", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY)));
|
||||
// TODO no startDate
|
||||
// organization.put("startDate", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization)));
|
||||
organization.put("title", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE)));
|
||||
organizations.put(organization);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
cursor.close();
|
||||
return organizations;
|
||||
}
|
||||
|
||||
private JSONArray addressQuery(ContentResolver cr, String contactId) {
|
||||
String[] addrWhereParams = new String[]{contactId,
|
||||
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
|
||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||
null, WHERE_STRING, addrWhereParams, null);
|
||||
JSONArray addresses = new JSONArray();
|
||||
JSONObject address = new JSONObject();
|
||||
while (cursor.moveToNext()) {
|
||||
try {
|
||||
address.put("formatted", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS)));
|
||||
address.put("streetAddress", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)));
|
||||
address.put("locality", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)));
|
||||
address.put("region", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION)));
|
||||
address.put("postalCode", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE)));
|
||||
address.put("country", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY)));
|
||||
addresses.put(address);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
cursor.close();
|
||||
return addresses;
|
||||
}
|
||||
|
||||
private JSONObject nameQuery(ContentResolver cr, String contactId) {
|
||||
String[] addrWhereParams = new String[]{contactId,
|
||||
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
|
||||
Cursor name = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||
null, WHERE_STRING, addrWhereParams, null);
|
||||
JSONObject contactName = new JSONObject();
|
||||
if (name.moveToFirst()) {
|
||||
try {
|
||||
String familyName = name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
|
||||
String givenName = name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
|
||||
String middleName = name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));
|
||||
String honorificPrefix = name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX));
|
||||
String honorificSuffix = name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX));
|
||||
|
||||
// Create the formatted name
|
||||
StringBuffer formatted = new StringBuffer("");
|
||||
if (honorificPrefix != null) { formatted.append(honorificPrefix + " "); }
|
||||
if (givenName != null) { formatted.append(givenName + " "); }
|
||||
if (middleName != null) { formatted.append(middleName + " "); }
|
||||
if (familyName != null) { formatted.append(familyName + " "); }
|
||||
if (honorificSuffix != null) { formatted.append(honorificSuffix + " "); }
|
||||
|
||||
contactName.put("familyName", familyName);
|
||||
contactName.put("givenName", givenName);
|
||||
contactName.put("middleName", middleName);
|
||||
contactName.put("honorificPrefix", honorificPrefix);
|
||||
contactName.put("honorificSuffix", honorificSuffix);
|
||||
contactName.put("formatted", formatted);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
name.close();
|
||||
return contactName;
|
||||
}
|
||||
|
||||
private JSONArray phoneQuery(ContentResolver cr, String contactId) {
|
||||
Cursor phones = cr.query(
|
||||
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
|
||||
null,
|
||||
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
|
||||
null, null);
|
||||
JSONArray phoneNumbers = new JSONArray();
|
||||
JSONObject phoneNumber = new JSONObject();
|
||||
while (phones.moveToNext()) {
|
||||
try {
|
||||
phoneNumber.put("primary", false); // Android does not store primary attribute
|
||||
phoneNumber.put("value", phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
|
||||
phoneNumber.put("type", phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)));
|
||||
phoneNumbers.put(phoneNumber);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
phones.close();
|
||||
return phoneNumbers;
|
||||
}
|
||||
|
||||
private JSONArray emailQuery(ContentResolver cr, String contactId) {
|
||||
Cursor emails = cr.query(
|
||||
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
|
||||
null,
|
||||
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
|
||||
null, null);
|
||||
JSONArray emailAddresses = new JSONArray();
|
||||
JSONObject email = new JSONObject();
|
||||
while (emails.moveToNext()) {
|
||||
try {
|
||||
email.put("primary", false); // Android does not store primary attribute
|
||||
email.put("value", emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
|
||||
email.put("type", emails.getInt(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)));
|
||||
emailAddresses.put(email);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
emails.close();
|
||||
return emailAddresses;
|
||||
}
|
||||
|
||||
private JSONArray imQuery(ContentResolver cr, String contactId) {
|
||||
String[] addrWhereParams = new String[]{contactId,
|
||||
ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE};
|
||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||
null, WHERE_STRING, addrWhereParams, null);
|
||||
JSONArray ims = new JSONArray();
|
||||
JSONObject im = new JSONObject();
|
||||
while (cursor.moveToNext()) {
|
||||
try {
|
||||
im.put("primary", false); // Android does not store primary attribute
|
||||
im.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
|
||||
im.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE)));
|
||||
ims.put(im);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
cursor.close();
|
||||
return ims;
|
||||
}
|
||||
|
||||
private String noteQuery(ContentResolver cr, String contactId) {
|
||||
String[] noteWhereParams = new String[]{contactId,
|
||||
ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
|
||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||
null, WHERE_STRING, noteWhereParams, null);
|
||||
String note = new String("");
|
||||
if (cursor.moveToFirst()) {
|
||||
note = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
|
||||
}
|
||||
cursor.close();
|
||||
return note;
|
||||
}
|
||||
|
||||
private String nicknameQuery(ContentResolver cr, String contactId) {
|
||||
String[] nicknameWhereParams = new String[]{contactId,
|
||||
ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE};
|
||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||
null, WHERE_STRING, nicknameWhereParams, null);
|
||||
String nickname = new String("");
|
||||
if (cursor.moveToFirst()) {
|
||||
nickname = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME));
|
||||
}
|
||||
cursor.close();
|
||||
return nickname;
|
||||
}
|
||||
|
||||
private JSONArray websiteQuery(ContentResolver cr, String contactId) {
|
||||
String[] websiteWhereParams = new String[]{contactId,
|
||||
ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE};
|
||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||
null, WHERE_STRING, websiteWhereParams, null);
|
||||
JSONArray websites = new JSONArray();
|
||||
JSONObject website = new JSONObject();
|
||||
while (cursor.moveToNext()) {
|
||||
try {
|
||||
website.put("primary", false); // Android does not store primary attribute
|
||||
website.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL)));
|
||||
website.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE)));
|
||||
websites.put(website);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
cursor.close();
|
||||
return websites;
|
||||
}
|
||||
|
||||
private JSONArray relationshipQuery(ContentResolver cr, String contactId) {
|
||||
String[] relationshipWhereParams = new String[]{contactId,
|
||||
ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE};
|
||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||
null, WHERE_STRING, relationshipWhereParams, null);
|
||||
JSONArray relationships = new JSONArray();
|
||||
JSONObject relationship = new JSONObject();
|
||||
while (cursor.moveToNext()) {
|
||||
try {
|
||||
relationship.put("primary", false); // Android does not store primary attribute
|
||||
relationship.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Relation.NAME)));
|
||||
relationship.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Relation.TYPE)));
|
||||
relationships.put(relationship);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
cursor.close();
|
||||
return relationships;
|
||||
}
|
||||
|
||||
private String birthdayQuery(ContentResolver cr, String contactId) {
|
||||
String birthday = conditionalStringQuery(cr, contactId, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE,
|
||||
ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY, ContactsContract.CommonDataKinds.Event.TYPE,
|
||||
ContactsContract.CommonDataKinds.Event.START_DATE);
|
||||
return birthday;
|
||||
}
|
||||
|
||||
private String anniversaryQuery(ContentResolver cr, String contactId) {
|
||||
String anniversary = conditionalStringQuery(cr, contactId, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE,
|
||||
ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY, ContactsContract.CommonDataKinds.Event.TYPE,
|
||||
ContactsContract.CommonDataKinds.Event.START_DATE);
|
||||
return anniversary;
|
||||
}
|
||||
|
||||
private String conditionalStringQuery(ContentResolver cr, String contactId, String dataType, int type, String label, String data) {
|
||||
String[] whereParams = new String[]{contactId, dataType};
|
||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||
null, WHERE_STRING, whereParams, null);
|
||||
String retVal = new String("");
|
||||
while (cursor.moveToNext()) {
|
||||
if (type == cursor.getInt(cursor.getColumnIndex(label))) {
|
||||
retVal = cursor.getString(cursor.getColumnIndex(data));
|
||||
}
|
||||
}
|
||||
cursor.close();
|
||||
return retVal;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user