Updating to latest W3C spec

This commit is contained in:
macdonst 2011-01-26 00:13:02 +08:00
parent cda154209d
commit b353f3608d
3 changed files with 44 additions and 344 deletions

View File

@ -17,24 +17,19 @@
* @param {ContactAddress[]} addresses array of addresses * @param {ContactAddress[]} addresses array of addresses
* @param {ContactField[]} ims instant messaging user ids * @param {ContactField[]} ims instant messaging user ids
* @param {ContactOrganization[]} organizations * @param {ContactOrganization[]} organizations
* @param {DOMString} published date contact was first created * @param {DOMString} revision date contact was last updated
* @param {DOMString} updated date contact was last updated
* @param {DOMString} birthday contact's birthday * @param {DOMString} birthday contact's birthday
* @param (DOMString} anniversary contact's anniversary
* @param {DOMString} gender contact's gender * @param {DOMString} gender contact's gender
* @param {DOMString} note user notes about contact * @param {DOMString} note user notes about contact
* @param {DOMString} preferredUsername
* @param {ContactField[]} photos * @param {ContactField[]} photos
* @param {ContactField[]} tags * @param {ContactField[]} categories
* @param {ContactField[]} relationships
* @param {ContactField[]} urls contact's web sites * @param {ContactField[]} urls contact's web sites
* @param {ContactAccounts[]} accounts contact's online accounts * @param {ContactAccounts[]} accounts contact's online accounts
* @param {DOMString} utcOffset UTC time zone offset * @param {DOMString} utcOffset UTC time zone offset
* @param {DOMString} connected * @param {DOMString} connected
*/ */
var Contact = function(id, displayName, name, nickname, phoneNumbers, emails, addresses, var Contact = function(id, displayName, name, nickname, phoneNumbers, emails, addresses,
ims, organizations, published, updated, birthday, anniversary, gender, note, ims, organizations, revision, birthday, gender, note, photos, categories, urls, timezone) {
preferredUsername, photos, tags, relationships, urls, accounts, utcOffset, connected) {
this.id = id || null; this.id = id || null;
this.rawId = null; this.rawId = null;
this.displayName = displayName || null; this.displayName = displayName || null;
@ -45,20 +40,14 @@ var Contact = function(id, displayName, name, nickname, phoneNumbers, emails, ad
this.addresses = addresses || null; // ContactAddress[] this.addresses = addresses || null; // ContactAddress[]
this.ims = ims || null; // ContactField[] this.ims = ims || null; // ContactField[]
this.organizations = organizations || null; // ContactOrganization[] this.organizations = organizations || null; // ContactOrganization[]
this.published = published || null; this.revision = revision || null;
this.updated = updated || null;
this.birthday = birthday || null; this.birthday = birthday || null;
this.anniversary = anniversary || null;
this.gender = gender || null; this.gender = gender || null;
this.note = note || null; this.note = note || null;
this.preferredUsername = preferredUsername || null;
this.photos = photos || null; // ContactField[] this.photos = photos || null; // ContactField[]
this.tags = tags || null; // ContactField[] this.categories = categories || null; // ContactField[]
this.relationships = relationships || null; // ContactField[]
this.urls = urls || null; // ContactField[] this.urls = urls || null; // ContactField[]
this.accounts = accounts || null; // ContactAccount[] this.timezone = timezone || null;
this.utcOffset = utcOffset || null;
this.connected = connected || null;
}; };
/** /**
@ -164,11 +153,11 @@ var ContactName = function(formatted, familyName, givenName, middle, prefix, suf
* @param value * @param value
* @param primary * @param primary
*/ */
var ContactField = function(type, value, primary) { var ContactField = function(type, value, pref) {
this.id = null; this.id = null;
this.type = type || null; this.type = type || null;
this.value = value || null; this.value = value || null;
this.primary = primary || null; this.pref = pref || null;
}; };
/** /**
@ -202,31 +191,13 @@ var ContactAddress = function(formatted, streetAddress, locality, region, postal
* @param location * @param location
* @param desc * @param desc
*/ */
var ContactOrganization = function(name, dept, title, startDate, endDate, location, desc) { var ContactOrganization = function(name, dept, title) {
this.id = null; this.id = null;
this.name = name || null; this.name = name || null;
this.department = dept || null; this.department = dept || null;
this.title = title || null; this.title = title || null;
this.startDate = startDate || null;
this.endDate = endDate || null;
this.location = location || null;
this.description = desc || null;
}; };
/**
* Contact account.
* @param {DOMString} id unique identifier, should only be set by native code
* @param domain
* @param username
* @param userid
*/
var ContactAccount = function(domain, username, userid) {
this.id = null;
this.domain = domain || null;
this.username = username || null;
this.userid = userid || null;
}
/** /**
* Represents a group of Contacts. * Represents a group of Contacts.
*/ */
@ -284,13 +255,11 @@ Contacts.prototype.cast = function(pluginResult) {
* ContactFindOptions. * ContactFindOptions.
* @param filter used to match contacts against * @param filter used to match contacts against
* @param multiple boolean used to determine if more than one contact should be returned * @param multiple boolean used to determine if more than one contact should be returned
* @param limit maximum number of results to return from the contacts search
* @param updatedSince return only contact records that have been updated on or after the given time * @param updatedSince return only contact records that have been updated on or after the given time
*/ */
var ContactFindOptions = function(filter, multiple, limit, updatedSince) { var ContactFindOptions = function(filter, multiple, updatedSince) {
this.filter = filter || ''; this.filter = filter || '';
this.multiple = multiple || false; this.multiple = multiple || true;
this.limit = limit || 1;
this.updatedSince = updatedSince || ''; this.updatedSince = updatedSince || '';
}; };

View File

@ -104,8 +104,8 @@ public class ContactAccessorSdk3_4 extends ContactAccessor {
*/ */
public JSONArray search(JSONArray fields, JSONObject options) { public JSONArray search(JSONArray fields, JSONObject options) {
String searchTerm = ""; String searchTerm = "";
int limit = 1; int limit = Integer.MAX_VALUE;
boolean multiple = false; boolean multiple = true;
if (options != null) { if (options != null) {
searchTerm = options.optString("filter"); searchTerm = options.optString("filter");
@ -115,10 +115,13 @@ public class ContactAccessorSdk3_4 extends ContactAccessor {
else { else {
searchTerm = "%" + searchTerm + "%"; searchTerm = "%" + searchTerm + "%";
} }
multiple = options.optBoolean("multiple"); try {
if (multiple) { multiple = options.getBoolean("multiple");
limit = options.optInt("limit"); if (!multiple) {
limit = limit > 0 ? limit : 1; limit = 1;
}
} catch (JSONException e) {
// Multiple was not specified so we assume the default is true.
} }
} }
else { else {
@ -312,7 +315,7 @@ public class ContactAccessorSdk3_4 extends ContactAccessor {
try{ try{
im.put("id", cursor.getString( im.put("id", cursor.getString(
cursor.getColumnIndex(ContactMethods._ID))); cursor.getColumnIndex(ContactMethods._ID)));
im.put("primary", false); im.put("perf", false);
im.put("value", cursor.getString( im.put("value", cursor.getString(
cursor.getColumnIndex(ContactMethodsColumns.DATA))); cursor.getColumnIndex(ContactMethodsColumns.DATA)));
im.put("type", getContactType(cursor.getInt( im.put("type", getContactType(cursor.getInt(
@ -346,10 +349,6 @@ public class ContactAccessorSdk3_4 extends ContactAccessor {
organization.put("name", cursor.getString(cursor.getColumnIndex(Organizations.COMPANY))); organization.put("name", cursor.getString(cursor.getColumnIndex(Organizations.COMPANY)));
organization.put("title", cursor.getString(cursor.getColumnIndex(Organizations.TITLE))); organization.put("title", cursor.getString(cursor.getColumnIndex(Organizations.TITLE)));
// organization.put("department", cursor.getString(cursor.getColumnIndex(Organizations))); // 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); organizations.put(organization);
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
@ -404,7 +403,7 @@ public class ContactAccessorSdk3_4 extends ContactAccessor {
phone = new JSONObject(); phone = new JSONObject();
try{ try{
phone.put("id", cursor.getString(cursor.getColumnIndex(Phones._ID))); phone.put("id", cursor.getString(cursor.getColumnIndex(Phones._ID)));
phone.put("primary", false); phone.put("perf", false);
phone.put("value", cursor.getString(cursor.getColumnIndex(Phones.NUMBER))); phone.put("value", cursor.getString(cursor.getColumnIndex(Phones.NUMBER)));
phone.put("type", getPhoneType(cursor.getInt(cursor.getColumnIndex(Phones.TYPE)))); phone.put("type", getPhoneType(cursor.getInt(cursor.getColumnIndex(Phones.TYPE))));
phones.put(phone); phones.put(phone);
@ -433,7 +432,7 @@ public class ContactAccessorSdk3_4 extends ContactAccessor {
email = new JSONObject(); email = new JSONObject();
try{ try{
email.put("id", cursor.getString(cursor.getColumnIndex(ContactMethods._ID))); email.put("id", cursor.getString(cursor.getColumnIndex(ContactMethods._ID)));
email.put("primary", false); email.put("perf", false);
email.put("value", cursor.getString(cursor.getColumnIndex(ContactMethods.DATA))); email.put("value", cursor.getString(cursor.getColumnIndex(ContactMethods.DATA)));
// TODO Find out why adding an email type throws and exception // TODO Find out why adding an email type throws and exception
//email.put("type", cursor.getString(cursor.getColumnIndex(ContactMethods.TYPE))); //email.put("type", cursor.getString(cursor.getColumnIndex(ContactMethods.TYPE)));

View File

@ -115,26 +115,14 @@ public class ContactAccessorSdk5 extends ContactAccessor {
dbMap.put("organizations.name", ContactsContract.CommonDataKinds.Organization.COMPANY); dbMap.put("organizations.name", ContactsContract.CommonDataKinds.Organization.COMPANY);
dbMap.put("organizations.department", ContactsContract.CommonDataKinds.Organization.DEPARTMENT); dbMap.put("organizations.department", ContactsContract.CommonDataKinds.Organization.DEPARTMENT);
dbMap.put("organizations.title", ContactsContract.CommonDataKinds.Organization.TITLE); dbMap.put("organizations.title", ContactsContract.CommonDataKinds.Organization.TITLE);
dbMap.put("organizations.location", ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION); //dbMap.put("revision", null);
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("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("note", ContactsContract.CommonDataKinds.Note.NOTE);
//dbMap.put("preferredUsername", null);
dbMap.put("photos.value", ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE); dbMap.put("photos.value", ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
//dbMap.put("tags.value", null); //dbMap.put("categories.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", ContactsContract.CommonDataKinds.Website.URL);
dbMap.put("urls.value", ContactsContract.CommonDataKinds.Website.URL); dbMap.put("urls.value", ContactsContract.CommonDataKinds.Website.URL);
//dbMap.put("accounts.domain", null); //dbMap.put("timezone", null);
//dbMap.put("accounts.username", null);
//dbMap.put("accounts.userid", null);
//dbMap.put("utcOffset", null);
//dbMap.put("connected", null);
} }
/** /**
@ -159,8 +147,8 @@ public class ContactAccessorSdk5 extends ContactAccessor {
// Get the find options // Get the find options
String searchTerm = ""; String searchTerm = "";
int limit = 1; int limit = Integer.MAX_VALUE;
boolean multiple = false; boolean multiple = true;
if (options != null) { if (options != null) {
searchTerm = options.optString("filter"); searchTerm = options.optString("filter");
@ -170,10 +158,13 @@ public class ContactAccessorSdk5 extends ContactAccessor {
else { else {
searchTerm = "%" + searchTerm + "%"; searchTerm = "%" + searchTerm + "%";
} }
multiple = options.optBoolean("multiple"); try {
if (multiple) { multiple = options.getBoolean("multiple");
limit = options.optInt("limit"); if (!multiple) {
limit = limit > 0 ? limit : 1; limit = 1;
}
} catch (JSONException e) {
// Multiple was not specified so we assume the default is true.
} }
} }
else { else {
@ -232,7 +223,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
JSONArray emails = new JSONArray(); JSONArray emails = new JSONArray();
JSONArray ims = new JSONArray(); JSONArray ims = new JSONArray();
JSONArray websites = new JSONArray(); JSONArray websites = new JSONArray();
JSONArray relationships = new JSONArray();
JSONArray photos = new JSONArray(); JSONArray photos = new JSONArray();
if (c.getCount() > 0) { if (c.getCount() > 0) {
@ -252,7 +242,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
// Populate the Contact object with it's arrays // Populate the Contact object with it's arrays
// and push the contact into the contacts array // and push the contact into the contacts array
contacts.put(populateContact(contact, organizations, addresses, phones, contacts.put(populateContact(contact, organizations, addresses, phones,
emails, ims, websites, relationships, photos)); emails, ims, websites, photos));
// Clean up the objects // Clean up the objects
contact = new JSONObject(); contact = new JSONObject();
@ -262,7 +252,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
emails = new JSONArray(); emails = new JSONArray();
ims = new JSONArray(); ims = new JSONArray();
websites = new JSONArray(); websites = new JSONArray();
relationships = new JSONArray();
photos = new JSONArray(); photos = new JSONArray();
// Set newContact to true as we are starting to populate a new contact // Set newContact to true as we are starting to populate a new contact
@ -317,16 +306,8 @@ public class ContactAccessorSdk5 extends ContactAccessor {
&& isRequired("urls",populate)) { && isRequired("urls",populate)) {
websites.put(websiteQuery(c)); websites.put(websiteQuery(c));
} }
else if (mimetype.equals(ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE)
&& isRequired("relationships",populate)) {
relationships.put(relationshipQuery(c));
}
else if (mimetype.equals(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)) { else if (mimetype.equals(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)) {
if (ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY == c.getInt(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE)) if (ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY == c.getInt(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE))
&& isRequired("anniversary",populate)) {
contact.put("anniversary", c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE)));
}
else if (ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY == c.getInt(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE))
&& isRequired("birthday",populate)) { && isRequired("birthday",populate)) {
contact.put("birthday", c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE))); contact.put("birthday", c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE)));
} }
@ -347,7 +328,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
// Push the last contact into the contacts array // Push the last contact into the contacts array
if (contacts.length() < limit) { if (contacts.length() < limit) {
contacts.put(populateContact(contact, organizations, addresses, phones, contacts.put(populateContact(contact, organizations, addresses, phones,
emails, ims, websites, relationships, photos)); emails, ims, websites, photos));
} }
} }
c.close(); c.close();
@ -402,13 +383,12 @@ public class ContactAccessorSdk5 extends ContactAccessor {
* @param emails array of emails * @param emails array of emails
* @param ims array of instant messenger addresses * @param ims array of instant messenger addresses
* @param websites array of websites * @param websites array of websites
* @param relationships array of relationships
* @param photos * @param photos
* @return * @return
*/ */
private JSONObject populateContact(JSONObject contact, JSONArray organizations, private JSONObject populateContact(JSONObject contact, JSONArray organizations,
JSONArray addresses, JSONArray phones, JSONArray emails, JSONArray addresses, JSONArray phones, JSONArray emails,
JSONArray ims, JSONArray websites, JSONArray relationships, JSONArray photos) { JSONArray ims, JSONArray websites, JSONArray photos) {
try { try {
contact.put("organizations", organizations); contact.put("organizations", organizations);
contact.put("addresses", addresses); contact.put("addresses", addresses);
@ -416,7 +396,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
contact.put("emails", emails); contact.put("emails", emails);
contact.put("ims", ims); contact.put("ims", ims);
contact.put("websites", websites); contact.put("websites", websites);
contact.put("relationships", relationships);
contact.put("photos", photos); contact.put("photos", photos);
} }
catch (JSONException e) { catch (JSONException e) {
@ -502,12 +481,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
// else if (key.startsWith("birthday")) { // else if (key.startsWith("birthday")) {
// where.add("(" + dbMap.get(key) + " LIKE ? AND " // where.add("(" + dbMap.get(key) + " LIKE ? AND "
// + ContactsContract.Data.MIMETYPE + " = ? )"); // + ContactsContract.Data.MIMETYPE + " = ? )");
// }
// else if (key.startsWith("anniversary")) {
// where.add("(" + dbMap.get(key) + " LIKE ? AND "
// + ContactsContract.Data.MIMETYPE + " = ? )");
// whereArgs.add(searchTerm);
// whereArgs.add(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
// } // }
else if (key.startsWith("note")) { else if (key.startsWith("note")) {
where.add("(" + dbMap.get(key) + " LIKE ? AND " where.add("(" + dbMap.get(key) + " LIKE ? AND "
@ -515,12 +488,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
whereArgs.add(searchTerm); whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE); whereArgs.add(ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE);
} }
else if (key.startsWith("relationships")) {
where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE);
}
else if (key.startsWith("urls")) { else if (key.startsWith("urls")) {
where.add("(" + dbMap.get(key) + " LIKE ? AND " where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ ContactsContract.Data.MIMETYPE + " = ? )"); + ContactsContract.Data.MIMETYPE + " = ? )");
@ -563,13 +530,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
try { try {
organization.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization._ID))); organization.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization._ID)));
organization.put("department", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DEPARTMENT))); 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))); 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))); organization.put("title", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE)));
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
@ -641,7 +602,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
JSONObject phoneNumber = new JSONObject(); JSONObject phoneNumber = new JSONObject();
try { try {
phoneNumber.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID))); phoneNumber.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID)));
phoneNumber.put("primary", false); // Android does not store primary attribute phoneNumber.put("pref", false); // Android does not store pref attribute
phoneNumber.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); phoneNumber.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
phoneNumber.put("type", getPhoneType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)))); phoneNumber.put("type", getPhoneType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))));
} catch (JSONException e) { } catch (JSONException e) {
@ -662,7 +623,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
JSONObject email = new JSONObject(); JSONObject email = new JSONObject();
try { try {
email.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email._ID))); email.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email._ID)));
email.put("primary", false); // Android does not store primary attribute email.put("pref", false); // Android does not store pref attribute
email.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA))); email.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
email.put("type", getContactType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)))); email.put("type", getContactType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE))));
} catch (JSONException e) { } catch (JSONException e) {
@ -680,7 +641,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
JSONObject im = new JSONObject(); JSONObject im = new JSONObject();
try { try {
im.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im._ID))); im.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im._ID)));
im.put("primary", false); // Android does not store primary attribute im.put("pref", false); // Android does not store pref attribute
im.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA))); im.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
im.put("type", getContactType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE)))); im.put("type", getContactType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE))));
} catch (JSONException e) { } catch (JSONException e) {
@ -698,7 +659,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
JSONObject website = new JSONObject(); JSONObject website = new JSONObject();
try { try {
website.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website._ID))); website.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website._ID)));
website.put("primary", false); // Android does not store primary attribute website.put("pref", false); // Android does not store pref attribute
website.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL))); website.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL)));
website.put("type", getContactType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE)))); website.put("type", getContactType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE))));
} catch (JSONException e) { } catch (JSONException e) {
@ -707,24 +668,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
return website; return website;
} }
/**
* Create a ContactField JSONObject
* @param cursor the current database row
* @return a JSONObject representing a ContactField
*/
private JSONObject relationshipQuery(Cursor cursor) {
JSONObject relationship = new JSONObject();
try {
relationship.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Relation._ID)));
relationship.put("primary", false); // Android does not store primary attribute
relationship.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Relation.NAME)));
relationship.put("type", getRelationshipType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Relation.TYPE))));
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
}
return relationship;
}
/** /**
* Create a ContactField JSONObject * Create a ContactField JSONObject
* @param contactId * @param contactId
@ -734,7 +677,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
JSONObject photo = new JSONObject(); JSONObject photo = new JSONObject();
try { try {
photo.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo._ID))); photo.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo._ID)));
photo.put("primary", false); photo.put("pref", false);
Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, (new Long(contactId))); Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, (new Long(contactId)));
Uri photoUri = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); Uri photoUri = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
photo.put("value", photoUri.toString()); photo.put("value", photoUri.toString());
@ -1004,8 +947,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId); contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE); contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, getJsonString(org, "department")); contentValues.put(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, getJsonString(org, "department"));
contentValues.put(ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION, getJsonString(org, "description"));
contentValues.put(ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION, getJsonString(org, "location"));
contentValues.put(ContactsContract.CommonDataKinds.Organization.COMPANY, getJsonString(org, "name")); contentValues.put(ContactsContract.CommonDataKinds.Organization.COMPANY, getJsonString(org, "name"));
contentValues.put(ContactsContract.CommonDataKinds.Organization.TITLE, getJsonString(org, "title")); contentValues.put(ContactsContract.CommonDataKinds.Organization.TITLE, getJsonString(org, "title"));
@ -1019,8 +960,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
ContactsContract.Data.MIMETYPE + "=?", ContactsContract.Data.MIMETYPE + "=?",
new String[]{orgId, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}) new String[]{orgId, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE})
.withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, getJsonString(org, "department")) .withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, getJsonString(org, "department"))
.withValue(ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION, getJsonString(org, "description"))
.withValue(ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION, getJsonString(org, "location"))
.withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, getJsonString(org, "name")) .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, getJsonString(org, "name"))
.withValue(ContactsContract.CommonDataKinds.Organization.TITLE, getJsonString(org, "title")) .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, getJsonString(org, "title"))
.build()); .build());
@ -1124,42 +1063,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
Log.d(LOG_TAG, "Could not get websites"); Log.d(LOG_TAG, "Could not get websites");
} }
// Modify relationships
JSONArray relationships = null;
try {
relationships = contact.getJSONArray("relationships");
if (relationships != null) {
for (int i=0; i<relationships.length(); i++) {
JSONObject relationship = (JSONObject)relationships.get(i);
String relationshipId = getJsonString(relationship, "id");;
// This is a new relationship so do a DB insert
if (relationshipId==null) {
ContentValues contentValues = new ContentValues();
contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.CommonDataKinds.Relation.NAME, getJsonString(relationship, "name"));
contentValues.put(ContactsContract.CommonDataKinds.Relation.TYPE, getRelationshipType(getJsonString(relationship, "type")));
ops.add(ContentProviderOperation.newInsert(
ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());
}
// This is an existing relationship so do a DB update
else {
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.CommonDataKinds.Relation._ID + "=? AND " +
ContactsContract.Data.MIMETYPE + "=?",
new String[]{relationshipId, ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE})
.withValue(ContactsContract.CommonDataKinds.Relation.NAME, getJsonString(relationship, "value"))
.withValue(ContactsContract.CommonDataKinds.Relation.TYPE, getRelationshipType(getJsonString(relationship, "type")))
.build());
}
}
}
}
catch (JSONException e) {
Log.d(LOG_TAG, "Could not get relationships");
}
// Modify birthday // Modify birthday
String birthday = getJsonString(contact, "birthday"); String birthday = getJsonString(contact, "birthday");
if (birthday != null) { if (birthday != null) {
@ -1173,18 +1076,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
.build()); .build());
} }
// Modify anniversary
String anniversary = getJsonString(contact, "anniversary");
if (anniversary != null) {
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " +
ContactsContract.Data.MIMETYPE + "=? AND " +
ContactsContract.CommonDataKinds.Event.TYPE + "=?",
new String[]{id,ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, new String(""+ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY)})
.withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY)
.withValue(ContactsContract.CommonDataKinds.Event.START_DATE, anniversary)
.build());
}
// Modify photos // Modify photos
JSONArray photos = null; JSONArray photos = null;
try { try {
@ -1240,22 +1131,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
return retVal; return retVal;
} }
/**
* Add a relationship to a list of database actions to be performed
*
* @param ops the list of database actions
* @param relationship the item to be inserted
*/
private void insertRelationship(ArrayList<ContentProviderOperation> ops,
JSONObject relationship) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Relation.NAME, getJsonString(relationship, "value"))
.withValue(ContactsContract.CommonDataKinds.Relation.TYPE, getRelationshipType(getJsonString(relationship, "type")))
.build());
}
/** /**
* Add a website to a list of database actions to be performed * Add a website to a list of database actions to be performed
* *
@ -1299,8 +1174,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, getJsonString(org, "department")) .withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, getJsonString(org, "department"))
.withValue(ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION, getJsonString(org, "description"))
.withValue(ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION, getJsonString(org, "location"))
.withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, getJsonString(org, "name")) .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, getJsonString(org, "name"))
.withValue(ContactsContract.CommonDataKinds.Organization.TITLE, getJsonString(org, "title")) .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, getJsonString(org, "title"))
.build()); .build());
@ -1573,21 +1446,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
Log.d(LOG_TAG, "Could not get websites"); Log.d(LOG_TAG, "Could not get websites");
} }
// Add relationships
JSONArray relationships = null;
try {
relationships = contact.getJSONArray("relationships");
if (relationships != null) {
for (int i=0; i<relationships.length(); i++) {
JSONObject relationship = (JSONObject)relationships.get(i);
insertRelationship(ops, relationship);
}
}
}
catch (JSONException e) {
Log.d(LOG_TAG, "Could not get relationships");
}
// Add birthday // Add birthday
String birthday = getJsonString(contact, "birthday"); String birthday = getJsonString(contact, "birthday");
if (birthday != null) { if (birthday != null) {
@ -1599,17 +1457,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
.build()); .build());
} }
// Add anniversary
String anniversary = getJsonString(contact, "anniversary");
if (anniversary != null) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY)
.withValue(ContactsContract.CommonDataKinds.Event.START_DATE, anniversary)
.build());
}
// Add photos // Add photos
JSONArray photos = null; JSONArray photos = null;
try { try {
@ -1862,119 +1709,4 @@ public class ContactAccessorSdk5 extends ContactAccessor {
} }
return stringType; return stringType;
} }
/**
* Converts a string from the W3C Contact API to it's Android int value.
* @param string
* @return Android int value
*/
private int getRelationshipType(String string) {
int type = ContactsContract.CommonDataKinds.Relation.TYPE_CUSTOM;
if (string!=null) {
if ("assistant".equals(string.toLowerCase())) {
return ContactsContract.CommonDataKinds.Relation.TYPE_ASSISTANT;
}
else if ("brother".equals(string.toLowerCase())) {
return ContactsContract.CommonDataKinds.Relation.TYPE_BROTHER;
}
else if ("child".equals(string.toLowerCase())) {
return ContactsContract.CommonDataKinds.Relation.TYPE_CHILD;
}
else if ("domestic partner".equals(string.toLowerCase())) {
return ContactsContract.CommonDataKinds.Relation.TYPE_DOMESTIC_PARTNER;
}
else if ("father".equals(string.toLowerCase())) {
return ContactsContract.CommonDataKinds.Relation.TYPE_FATHER;
}
else if ("friend".equals(string.toLowerCase())) {
return ContactsContract.CommonDataKinds.Relation.TYPE_FRIEND;
}
else if ("manager".equals(string.toLowerCase())) {
return ContactsContract.CommonDataKinds.Relation.TYPE_MANAGER;
}
else if ("mother".equals(string.toLowerCase())) {
return ContactsContract.CommonDataKinds.Relation.TYPE_MOTHER;
}
else if ("parent".equals(string.toLowerCase())) {
return ContactsContract.CommonDataKinds.Relation.TYPE_PARENT;
}
else if ("partner".equals(string.toLowerCase())) {
return ContactsContract.CommonDataKinds.Relation.TYPE_PARTNER;
}
else if ("referred by".equals(string.toLowerCase())) {
return ContactsContract.CommonDataKinds.Relation.TYPE_REFERRED_BY;
}
else if ("relative".equals(string.toLowerCase())) {
return ContactsContract.CommonDataKinds.Relation.TYPE_RELATIVE;
}
else if ("sister".equals(string.toLowerCase())) {
return ContactsContract.CommonDataKinds.Relation.TYPE_SISTER;
}
else if ("spouse".equals(string.toLowerCase())) {
return ContactsContract.CommonDataKinds.Relation.TYPE_SPOUSE;
}
else if ("custom".equals(string.toLowerCase())) {
return ContactsContract.CommonDataKinds.Relation.TYPE_CUSTOM;
}
}
return type;
}
/**
* getPhoneType converts an Android phone type into a string
* @param type
* @return phone type as string.
*/
private String getRelationshipType(int type) {
String stringType;
switch (type) {
case ContactsContract.CommonDataKinds.Relation.TYPE_ASSISTANT:
stringType = "assistant";
break;
case ContactsContract.CommonDataKinds.Relation.TYPE_BROTHER:
stringType = "brother";
break;
case ContactsContract.CommonDataKinds.Relation.TYPE_CHILD:
stringType = "child";
break;
case ContactsContract.CommonDataKinds.Relation.TYPE_DOMESTIC_PARTNER:
stringType = "domestic partner";
break;
case ContactsContract.CommonDataKinds.Relation.TYPE_FATHER:
stringType = "father";
break;
case ContactsContract.CommonDataKinds.Relation.TYPE_FRIEND:
stringType = "friend";
break;
case ContactsContract.CommonDataKinds.Relation.TYPE_MANAGER:
stringType = "manager";
break;
case ContactsContract.CommonDataKinds.Relation.TYPE_MOTHER:
stringType = "mother";
break;
case ContactsContract.CommonDataKinds.Relation.TYPE_PARENT:
stringType = "parent";
break;
case ContactsContract.CommonDataKinds.Relation.TYPE_PARTNER:
stringType = "partner";
break;
case ContactsContract.CommonDataKinds.Relation.TYPE_REFERRED_BY:
stringType = "referred by";
break;
case ContactsContract.CommonDataKinds.Relation.TYPE_RELATIVE:
stringType = "relative";
break;
case ContactsContract.CommonDataKinds.Relation.TYPE_SISTER:
stringType = "sister";
break;
case ContactsContract.CommonDataKinds.Relation.TYPE_SPOUSE:
stringType = "spouse";
break;
case ContactsContract.CommonDataKinds.Relation.TYPE_CUSTOM:
default:
stringType = "custom";
break;
}
return stringType;
}
} }