Adding queries for addresses and organization

This commit is contained in:
macdonst 2010-09-17 17:15:30 -04:00
parent c2bcc29cfb
commit ca4d7f7fd2
3 changed files with 207 additions and 58 deletions

View File

@ -104,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;

View File

@ -595,14 +595,46 @@ PhoneGap.addConstructor(function() {
*
* @constructor
*/
function Camera() {
Camera = function() {
this.successCallback = null;
this.errorCallback = null;
this.options = null;
};
/**
* Takes a photo and returns the image as a base64 encoded `String`.
* Format of image that returned from getPicture.
*
* Example: navigator.camera.getPicture(success, fail,
* { quality: 80,
* destinationType: Camera.DestinationType.DATA_URL,
* sourceType: Camera.PictureSourceType.PHOTOLIBRARY})
*/
Camera.DestinationType = {
DATA_URL: 0, // Return base64 encoded string
FILE_URI: 1 // Return file uri (content://media/external/images/media/2 for Android)
};
Camera.prototype.DestinationType = Camera.DestinationType;
/**
* Source to getPicture from.
*
* Example: navigator.camera.getPicture(success, fail,
* { quality: 80,
* destinationType: Camera.DestinationType.DATA_URL,
* sourceType: Camera.PictureSourceType.PHOTOLIBRARY})
*/
Camera.PictureSourceType = {
PHOTOLIBRARY : 0, // Choose image from picture library (same as SAVEDPHOTOALBUM for Android)
CAMERA : 1, // Take picture from camera
SAVEDPHOTOALBUM : 2 // Choose image from picture library (same as PHOTOLIBRARY for Android)
};
Camera.prototype.PictureSourceType = Camera.PictureSourceType;
/**
* Gets a picture from source defined by "options.sourceType", and returns the
* image as defined by the "options.destinationType" option.
* The defaults are sourceType=CAMERA and destinationType=DATA_URL.
*
* @param {Function} successCallback
* @param {Function} errorCallback
@ -625,15 +657,19 @@ Camera.prototype.getPicture = function(successCallback, errorCallback, options)
this.successCallback = successCallback;
this.errorCallback = errorCallback;
this.options = options;
var capturetype = "base64";
var quality = 80;
if (this.options.capturetype) {
capturetype = this.options.capturetype;
}
if (options.quality) {
quality = this.options.quality;
}
PhoneGap.execAsync(null, null, "Camera", "takePicture", [quality, capturetype]);
var destinationType = Camera.DestinationType.DATA_URL;
if (this.options.destinationType) {
destinationType = this.options.destinationType;
}
var sourceType = Camera.PictureSourceType.CAMERA;
if (typeof this.options.sourceType == "number") {
sourceType = this.options.sourceType;
}
PhoneGap.execAsync(null, null, "Camera", "takePicture", [quality, destinationType, sourceType]);
};
/**
@ -850,14 +886,6 @@ Contacts.prototype.find = function(fields, win, fail, options) {
PhoneGap.execAsync(null, null, "Contacts", "search", [fields, options]);
};
Contacts.prototype.droidFoundContact = function(contact) {
//console.log("this is what a contact looks like");
//console.log(contact);
//this.records.push(eval('(' + contact + ')'));
console.log("we should be called anymore.");
};
Contacts.prototype.droidDone = function(contacts) {
this.win(eval('(' + contacts + ')'));
};
@ -882,11 +910,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;

View File

@ -63,9 +63,18 @@ public class ContactAccessorSdk5 extends ContactAccessor {
@Override
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 = "%";
multiple = options.getBoolean("multiple");
if (multiple) {
limit = options.getInt("limit");
}
System.out.println("Limit = " + limit);
System.out.println("Multiple = " + multiple);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@ -81,7 +90,8 @@ public class ContactAccessorSdk5 extends ContactAccessor {
new String[] {searchTerm},
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
JSONArray contacts = new JSONArray();
while (cursor.moveToNext()) {
int pos = 0;
while (cursor.moveToNext() && (pos < limit)) {
JSONObject contact = new JSONObject();
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
@ -97,51 +107,160 @@ public class ContactAccessorSdk5 extends ContactAccessor {
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()) {
Log.d(LOG_TAG, "We found a phone!");
JSONArray phoneNumbers = new JSONArray();
JSONObject phoneNumber = new JSONObject();
try {
phoneNumber.put("primary", true);
phoneNumber.put("value", phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace('\'', '`'));
phoneNumbers.put(phoneNumber);
contact.put("phoneNumbers", phoneNumbers);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
phones.close();
//}
Cursor emails = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
null, null);
if (emails.moveToFirst()) {
Log.d(LOG_TAG, "We found an email!");
JSONArray emailAddresses = new JSONArray();
JSONObject email = new JSONObject();
try {
email.put("primary", true);
email.put("value", emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)).replace('\'', '`'));
emailAddresses.put(email);
contact.put("emails", emailAddresses);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
emails.close();
try {
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));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
contacts.put(contact);
pos++;
}
cursor.close();
mView.loadUrl("javascript:navigator.service.contacts.droidDone('" + contacts.toString() + "');");
}
private JSONArray organizationQuery(ContentResolver cr, String contactId) {
// TODO Fix the query URI
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null);
JSONArray organizations = new JSONArray();
JSONObject organization = new JSONObject();
while (cursor.moveToNext()) {
Log.d(LOG_TAG, "We found a phone!");
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
cursor.close();
return organizations;
}
private JSONArray addressQuery(ContentResolver cr, String contactId) {
// TODO Fix the query URI
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null);
JSONArray addresses = new JSONArray();
JSONObject address = new JSONObject();
while (cursor.moveToNext()) {
Log.d(LOG_TAG, "We found a phone!");
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
cursor.close();
return addresses;
}
private JSONObject nameQuery(ContentResolver cr, String contactId) {
// TODO Fix the query URI
Cursor name = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null);
JSONObject contactName = new JSONObject();
if (name.moveToFirst()) {
Log.d(LOG_TAG, "We found a name!");
try {
contactName.put("familyName", name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)));
contactName.put("givenName", name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)));
contactName.put("middleName", name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME)));
contactName.put("honorificPrefix", name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX)));
contactName.put("honorificSuffix", name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX)));
contactName.put("formatted", name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX))
+ " " + name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME))
+ " " + name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME))
+ " " + name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME))
+ " " + name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX)));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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()) {
Log.d(LOG_TAG, "We found a phone!");
try {
phoneNumber.put("primary", false); // Android does not store primary attribute
phoneNumber.put("value", phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace('\'', '`'));
phoneNumber.put("type", phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)));
phoneNumbers.put(phoneNumber);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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()) {
Log.d(LOG_TAG, "We found an email!");
try {
email.put("primary", false);
email.put("value", emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)).replace('\'', '`'));
email.put("type", emails.getInt(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)));
emailAddresses.put(email);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
emails.close();
return emailAddresses;
}
}