You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by bc...@apache.org on 2012/05/15 06:38:01 UTC
[11/15] Updates.
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/c8fafa6b/framework/src/org/apache/cordova/ContactAccessorSdk5.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/ContactAccessorSdk5.java b/framework/src/org/apache/cordova/ContactAccessorSdk5.java
index 6adcf00..cb673e4 100644
--- a/framework/src/org/apache/cordova/ContactAccessorSdk5.java
+++ b/framework/src/org/apache/cordova/ContactAccessorSdk5.java
@@ -32,18 +32,19 @@ import java.util.Iterator;
import java.util.Map;
import java.util.Set;
+import org.apache.cordova.api.CordovaInterface;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.accounts.Account;
import android.accounts.AccountManager;
-import android.app.Activity;
+//import android.app.Activity;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentUris;
import android.content.ContentValues;
-import android.content.Context;
+//import android.content.Context;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
@@ -72,355 +73,354 @@ import android.webkit.WebView;
*/
public class ContactAccessorSdk5 extends ContactAccessor {
- /**
- * Keep the photo size under the 1 MB blog limit.
- */
- private static final long MAX_PHOTO_SIZE = 1048576;
-
- private static final String EMAIL_REGEXP = ".+@.+\\.+.+"; /* <anything>@<anything>.<anything>*/
+ /**
+ * Keep the photo size under the 1 MB blog limit.
+ */
+ private static final long MAX_PHOTO_SIZE = 1048576;
- /**
- * A static map that converts the JavaScript property name to Android database column name.
- */
+ private static final String EMAIL_REGEXP = ".+@.+\\.+.+"; /* <anything>@<anything>.<anything>*/
+
+ /**
+ * A static map that converts the JavaScript property name to Android database column name.
+ */
private static final Map<String, String> dbMap = new HashMap<String, String>();
static {
- dbMap.put("id", ContactsContract.Data.CONTACT_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("birthday", ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
- dbMap.put("note", ContactsContract.CommonDataKinds.Note.NOTE);
- dbMap.put("photos.value", ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
- //dbMap.put("categories.value", null);
- dbMap.put("urls", ContactsContract.CommonDataKinds.Website.URL);
- dbMap.put("urls.value", ContactsContract.CommonDataKinds.Website.URL);
+ dbMap.put("id", ContactsContract.Data.CONTACT_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("birthday", ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
+ dbMap.put("note", ContactsContract.CommonDataKinds.Note.NOTE);
+ dbMap.put("photos.value", ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
+ //dbMap.put("categories.value", null);
+ dbMap.put("urls", ContactsContract.CommonDataKinds.Website.URL);
+ dbMap.put("urls.value", ContactsContract.CommonDataKinds.Website.URL);
}
/**
* Create an contact accessor.
*/
- public ContactAccessorSdk5(WebView view, Context context) {
- mApp = context;
- mView = view;
- }
-
- /**
- * This method takes the fields required and search options in order to produce an
- * array of contacts that matches the criteria provided.
- * @param fields an array of items to be used as search criteria
- * @param options that can be applied to contact searching
- * @return an array of contacts
- */
- @Override
- public JSONArray search(JSONArray fields, JSONObject options) {
- // Get the find options
- String searchTerm = "";
- int limit = Integer.MAX_VALUE;
- boolean multiple = true;
-
- if (options != null) {
- searchTerm = options.optString("filter");
- if (searchTerm.length()==0) {
- searchTerm = "%";
- }
- else {
- searchTerm = "%" + searchTerm + "%";
- }
- try {
- multiple = options.getBoolean("multiple");
- if (!multiple) {
- limit = 1;
- }
- } catch (JSONException e) {
- // Multiple was not specified so we assume the default is true.
- }
+ public ContactAccessorSdk5(WebView view, CordovaInterface context) {
+ mApp = context;
+ mView = view;
}
- else {
- searchTerm = "%";
- }
-
- //Log.d(LOG_TAG, "Search Term = " + searchTerm);
- //Log.d(LOG_TAG, "Field Length = " + fields.length());
- //Log.d(LOG_TAG, "Fields = " + fields.toString());
-
- // Loop through the fields the user provided to see what data should be returned.
- HashMap<String,Boolean> populate = buildPopulationSet(fields);
-
- // Build the ugly where clause and where arguments for one big query.
- WhereOptions whereOptions = buildWhereClause(fields, searchTerm);
-
- // Get all the id's where the search term matches the fields passed in.
- Cursor idCursor = mApp.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
- new String[] { ContactsContract.Data.CONTACT_ID },
- whereOptions.getWhere(),
- whereOptions.getWhereArgs(),
- ContactsContract.Data.CONTACT_ID + " ASC");
-
- // Create a set of unique ids
- //Log.d(LOG_TAG, "ID cursor query returns = " + idCursor.getCount());
- Set<String> contactIds = new HashSet<String>();
- while (idCursor.moveToNext()) {
- contactIds.add(idCursor.getString(idCursor.getColumnIndex(ContactsContract.Data.CONTACT_ID)));
+
+ /**
+ * This method takes the fields required and search options in order to produce an
+ * array of contacts that matches the criteria provided.
+ * @param fields an array of items to be used as search criteria
+ * @param options that can be applied to contact searching
+ * @return an array of contacts
+ */
+ @Override
+ public JSONArray search(JSONArray fields, JSONObject options) {
+ // Get the find options
+ String searchTerm = "";
+ int limit = Integer.MAX_VALUE;
+ boolean multiple = true;
+
+ if (options != null) {
+ searchTerm = options.optString("filter");
+ if (searchTerm.length() == 0) {
+ searchTerm = "%";
+ }
+ else {
+ searchTerm = "%" + searchTerm + "%";
+ }
+ try {
+ multiple = options.getBoolean("multiple");
+ if (!multiple) {
+ limit = 1;
+ }
+ } catch (JSONException e) {
+ // Multiple was not specified so we assume the default is true.
+ }
+ }
+ else {
+ searchTerm = "%";
+ }
+
+ //Log.d(LOG_TAG, "Search Term = " + searchTerm);
+ //Log.d(LOG_TAG, "Field Length = " + fields.length());
+ //Log.d(LOG_TAG, "Fields = " + fields.toString());
+
+ // Loop through the fields the user provided to see what data should be returned.
+ HashMap<String, Boolean> populate = buildPopulationSet(fields);
+
+ // Build the ugly where clause and where arguments for one big query.
+ WhereOptions whereOptions = buildWhereClause(fields, searchTerm);
+
+ // Get all the id's where the search term matches the fields passed in.
+ Cursor idCursor = mApp.getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
+ new String[] { ContactsContract.Data.CONTACT_ID },
+ whereOptions.getWhere(),
+ whereOptions.getWhereArgs(),
+ ContactsContract.Data.CONTACT_ID + " ASC");
+
+ // Create a set of unique ids
+ //Log.d(LOG_TAG, "ID cursor query returns = " + idCursor.getCount());
+ Set<String> contactIds = new HashSet<String>();
+ while (idCursor.moveToNext()) {
+ contactIds.add(idCursor.getString(idCursor.getColumnIndex(ContactsContract.Data.CONTACT_ID)));
+ }
+ idCursor.close();
+
+ // Build a query that only looks at ids
+ WhereOptions idOptions = buildIdClause(contactIds, searchTerm);
+
+ // Do the id query
+ Cursor c = mApp.getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
+ null,
+ idOptions.getWhere(),
+ idOptions.getWhereArgs(),
+ ContactsContract.Data.CONTACT_ID + " ASC");
+
+ JSONArray contacts = populateContactArray(limit, populate, c);
+ return contacts;
}
- idCursor.close();
-
- // Build a query that only looks at ids
- WhereOptions idOptions = buildIdClause(contactIds, searchTerm);
-
- // Do the id query
- Cursor c = mApp.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
- null,
- idOptions.getWhere(),
- idOptions.getWhereArgs(),
- ContactsContract.Data.CONTACT_ID + " ASC");
-
- JSONArray contacts = populateContactArray(limit, populate, c);
- return contacts;
- }
-
- /**
- * A special search that finds one contact by id
- *
- * @param id contact to find by id
- * @return a JSONObject representing the contact
- * @throws JSONException
- */
- public JSONObject getContactById(String id) throws JSONException {
+
+ /**
+ * A special search that finds one contact by id
+ *
+ * @param id contact to find by id
+ * @return a JSONObject representing the contact
+ * @throws JSONException
+ */
+ public JSONObject getContactById(String id) throws JSONException {
// Do the id query
- Cursor c = mApp.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
+ Cursor c = mApp.getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID + " = ? ",
new String[] { id },
- ContactsContract.Data.CONTACT_ID + " ASC");
+ ContactsContract.Data.CONTACT_ID + " ASC");
JSONArray fields = new JSONArray();
fields.put("*");
-
- HashMap<String,Boolean> populate = buildPopulationSet(fields);
-
+
+ HashMap<String, Boolean> populate = buildPopulationSet(fields);
+
JSONArray contacts = populateContactArray(1, populate, c);
-
+
if (contacts.length() == 1) {
return contacts.getJSONObject(0);
} else {
return null;
}
- }
-
- /**
- * Creates an array of contacts from the cursor you pass in
- *
- * @param limit max number of contacts for the array
- * @param populate whether or not you should populate a certain value
- * @param c the cursor
- * @return a JSONArray of contacts
- */
+ }
+
+ /**
+ * Creates an array of contacts from the cursor you pass in
+ *
+ * @param limit max number of contacts for the array
+ * @param populate whether or not you should populate a certain value
+ * @param c the cursor
+ * @return a JSONArray of contacts
+ */
private JSONArray populateContactArray(int limit,
HashMap<String, Boolean> populate, Cursor c) {
-
- String contactId = "";
- String rawId = "";
- String oldContactId = "";
- boolean newContact = true;
- String mimetype = "";
-
- JSONArray contacts = new JSONArray();
- JSONObject contact = new JSONObject();
- JSONArray organizations = new JSONArray();
- JSONArray addresses = new JSONArray();
- JSONArray phones = new JSONArray();
- JSONArray emails = new JSONArray();
- JSONArray ims = new JSONArray();
- JSONArray websites = new JSONArray();
- JSONArray photos = new JSONArray();
-
- if (c.getCount() > 0) {
- while (c.moveToNext() && (contacts.length() <= (limit-1))) {
- try {
- contactId = c.getString(c.getColumnIndex(ContactsContract.Data.CONTACT_ID));
- rawId = c.getString(c.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
-
- // If we are in the first row set the oldContactId
- if (c.getPosition() == 0) {
- oldContactId = contactId;
- }
-
- // When the contact ID changes we need to push the Contact object
- // to the array of contacts and create new objects.
- if (!oldContactId.equals(contactId)) {
- // Populate the Contact object with it's arrays
- // and push the contact into the contacts array
- contacts.put(populateContact(contact, organizations, addresses, phones,
- emails, ims, websites, photos));
-
- // Clean up the objects
- contact = new JSONObject();
- organizations = new JSONArray();
- addresses = new JSONArray();
- phones = new JSONArray();
- emails = new JSONArray();
- ims = new JSONArray();
- websites = new JSONArray();
- photos = new JSONArray();
-
- // Set newContact to true as we are starting to populate a new contact
- newContact = true;
- }
-
- // When we detect a new contact set the ID and display name.
- // These fields are available in every row in the result set returned.
- if (newContact) {
- newContact = false;
- contact.put("id", contactId);
- contact.put("rawId", rawId);
- }
-
- // Grab the mimetype of the current row as it will be used in a lot of comparisons
- mimetype = c.getString(c.getColumnIndex(ContactsContract.Data.MIMETYPE));
-
+
+ String contactId = "";
+ String rawId = "";
+ String oldContactId = "";
+ boolean newContact = true;
+ String mimetype = "";
+
+ JSONArray contacts = new JSONArray();
+ JSONObject contact = new JSONObject();
+ JSONArray organizations = new JSONArray();
+ JSONArray addresses = new JSONArray();
+ JSONArray phones = new JSONArray();
+ JSONArray emails = new JSONArray();
+ JSONArray ims = new JSONArray();
+ JSONArray websites = new JSONArray();
+ JSONArray photos = new JSONArray();
+
+ if (c.getCount() > 0) {
+ while (c.moveToNext() && (contacts.length() <= (limit - 1))) {
+ try {
+ contactId = c.getString(c.getColumnIndex(ContactsContract.Data.CONTACT_ID));
+ rawId = c.getString(c.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
+
+ // If we are in the first row set the oldContactId
+ if (c.getPosition() == 0) {
+ oldContactId = contactId;
+ }
+
+ // When the contact ID changes we need to push the Contact object
+ // to the array of contacts and create new objects.
+ if (!oldContactId.equals(contactId)) {
+ // Populate the Contact object with it's arrays
+ // and push the contact into the contacts array
+ contacts.put(populateContact(contact, organizations, addresses, phones,
+ emails, ims, websites, photos));
+
+ // Clean up the objects
+ contact = new JSONObject();
+ organizations = new JSONArray();
+ addresses = new JSONArray();
+ phones = new JSONArray();
+ emails = new JSONArray();
+ ims = new JSONArray();
+ websites = new JSONArray();
+ photos = new JSONArray();
+
+ // Set newContact to true as we are starting to populate a new contact
+ newContact = true;
+ }
+
+ // When we detect a new contact set the ID and display name.
+ // These fields are available in every row in the result set returned.
+ if (newContact) {
+ newContact = false;
+ contact.put("id", contactId);
+ contact.put("rawId", rawId);
+ }
+
+ // Grab the mimetype of the current row as it will be used in a lot of comparisons
+ mimetype = c.getString(c.getColumnIndex(ContactsContract.Data.MIMETYPE));
+
if (mimetype.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)) {
contact.put("displayName", c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME)));
}
- if (mimetype.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
- && isRequired("name",populate)) {
+ if (mimetype.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
+ && isRequired("name", populate)) {
contact.put("name", nameQuery(c));
}
- else if (mimetype.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
- && isRequired("phoneNumbers",populate)) {
- phones.put(phoneQuery(c));
- }
- else if (mimetype.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
- && isRequired("emails",populate)) {
- emails.put(emailQuery(c));
- }
- else if (mimetype.equals(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
- && isRequired("addresses",populate)) {
- addresses.put(addressQuery(c));
- }
- else if (mimetype.equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
- && isRequired("organizations",populate)) {
- organizations.put(organizationQuery(c));
- }
- else if (mimetype.equals(ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
- && isRequired("ims",populate)) {
- ims.put(imQuery(c));
- }
- else if (mimetype.equals(ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE)
- && isRequired("note",populate)) {
- contact.put("note",c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE)));
- }
- else if (mimetype.equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE)
- && isRequired("nickname",populate)) {
- contact.put("nickname",c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME)));
- }
- else if (mimetype.equals(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)
- && isRequired("urls",populate)) {
- websites.put(websiteQuery(c));
- }
- else if (mimetype.equals(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)) {
- if (ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY == c.getInt(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE))
- && isRequired("birthday",populate)) {
- contact.put("birthday", c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE)));
- }
- }
- else if (mimetype.equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
- && isRequired("photos",populate)) {
- photos.put(photoQuery(c, contactId));
- }
- }
- catch (JSONException e) {
- Log.e(LOG_TAG, e.getMessage(),e);
+ else if (mimetype.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
+ && isRequired("phoneNumbers", populate)) {
+ phones.put(phoneQuery(c));
+ }
+ else if (mimetype.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
+ && isRequired("emails", populate)) {
+ emails.put(emailQuery(c));
+ }
+ else if (mimetype.equals(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
+ && isRequired("addresses", populate)) {
+ addresses.put(addressQuery(c));
+ }
+ else if (mimetype.equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
+ && isRequired("organizations", populate)) {
+ organizations.put(organizationQuery(c));
+ }
+ else if (mimetype.equals(ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
+ && isRequired("ims", populate)) {
+ ims.put(imQuery(c));
+ }
+ else if (mimetype.equals(ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE)
+ && isRequired("note", populate)) {
+ contact.put("note", c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE)));
+ }
+ else if (mimetype.equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE)
+ && isRequired("nickname", populate)) {
+ contact.put("nickname", c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME)));
+ }
+ else if (mimetype.equals(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)
+ && isRequired("urls", populate)) {
+ websites.put(websiteQuery(c));
+ }
+ else if (mimetype.equals(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)) {
+ if (ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY == c.getInt(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE))
+ && isRequired("birthday", populate)) {
+ contact.put("birthday", c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE)));
+ }
+ }
+ else if (mimetype.equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
+ && isRequired("photos", populate)) {
+ photos.put(photoQuery(c, contactId));
+ }
+ } catch (JSONException e) {
+ Log.e(LOG_TAG, e.getMessage(), e);
+ }
+
+ // Set the old contact ID
+ oldContactId = contactId;
+ }
+
+ // Push the last contact into the contacts array
+ if (contacts.length() < limit) {
+ contacts.put(populateContact(contact, organizations, addresses, phones,
+ emails, ims, websites, photos));
+ }
}
-
- // Set the old contact ID
- oldContactId = contactId;
- }
-
- // Push the last contact into the contacts array
- if (contacts.length() < limit) {
- contacts.put(populateContact(contact, organizations, addresses, phones,
- emails, ims, websites, photos));
- }
- }
- c.close();
+ c.close();
return contacts;
}
- /**
- * Builds a where clause all all the ids passed into the method
- * @param contactIds a set of unique contact ids
- * @param searchTerm what to search for
- * @return an object containing the selection and selection args
- */
- private WhereOptions buildIdClause(Set<String> contactIds, String searchTerm) {
- WhereOptions options = new WhereOptions();
-
- // If the user is searching for every contact then short circuit the method
- // and return a shorter where clause to be searched.
- if (searchTerm.equals("%")) {
- options.setWhere("(" + ContactsContract.Data.CONTACT_ID + " LIKE ? )");
- options.setWhereArgs(new String[] {searchTerm});
- return options;
- }
+ /**
+ * Builds a where clause all all the ids passed into the method
+ * @param contactIds a set of unique contact ids
+ * @param searchTerm what to search for
+ * @return an object containing the selection and selection args
+ */
+ private WhereOptions buildIdClause(Set<String> contactIds, String searchTerm) {
+ WhereOptions options = new WhereOptions();
+
+ // If the user is searching for every contact then short circuit the method
+ // and return a shorter where clause to be searched.
+ if (searchTerm.equals("%")) {
+ options.setWhere("(" + ContactsContract.Data.CONTACT_ID + " LIKE ? )");
+ options.setWhereArgs(new String[] { searchTerm });
+ return options;
+ }
+
+ // This clause means that there are specific ID's to be populated
+ Iterator<String> it = contactIds.iterator();
+ StringBuffer buffer = new StringBuffer("(");
+
+ while (it.hasNext()) {
+ buffer.append("'" + it.next() + "'");
+ if (it.hasNext()) {
+ buffer.append(",");
+ }
+ }
+ buffer.append(")");
- // This clause means that there are specific ID's to be populated
- Iterator<String> it = contactIds.iterator();
- StringBuffer buffer = new StringBuffer("(");
-
- while (it.hasNext()) {
- buffer.append("'" + it.next() + "'");
- if (it.hasNext()) {
- buffer.append(",");
- }
+ options.setWhere(ContactsContract.Data.CONTACT_ID + " IN " + buffer.toString());
+ options.setWhereArgs(null);
+
+ return options;
}
- buffer.append(")");
-
- options.setWhere(ContactsContract.Data.CONTACT_ID + " IN " + buffer.toString());
- options.setWhereArgs(null);
-
- return options;
- }
-
- /**
- * Create a new contact using a JSONObject to hold all the data.
- * @param contact
- * @param organizations array of organizations
- * @param addresses array of addresses
- * @param phones array of phones
- * @param emails array of emails
- * @param ims array of instant messenger addresses
- * @param websites array of websites
- * @param photos
- * @return
- */
- private JSONObject populateContact(JSONObject contact, JSONArray organizations,
- JSONArray addresses, JSONArray phones, JSONArray emails,
- JSONArray ims, JSONArray websites, JSONArray photos) {
- try {
- // Only return the array if it has at least one entry
+
+ /**
+ * Create a new contact using a JSONObject to hold all the data.
+ * @param contact
+ * @param organizations array of organizations
+ * @param addresses array of addresses
+ * @param phones array of phones
+ * @param emails array of emails
+ * @param ims array of instant messenger addresses
+ * @param websites array of websites
+ * @param photos
+ * @return
+ */
+ private JSONObject populateContact(JSONObject contact, JSONArray organizations,
+ JSONArray addresses, JSONArray phones, JSONArray emails,
+ JSONArray ims, JSONArray websites, JSONArray photos) {
+ try {
+ // Only return the array if it has at least one entry
if (organizations.length() > 0) {
contact.put("organizations", organizations);
}
@@ -442,26 +442,25 @@ public class ContactAccessorSdk5 extends ContactAccessor {
if (photos.length() > 0) {
contact.put("photos", photos);
}
+ } catch (JSONException e) {
+ Log.e(LOG_TAG, e.getMessage(), e);
+ }
+ return contact;
}
- catch (JSONException e) {
- Log.e(LOG_TAG,e.getMessage(),e);
- }
- return contact;
- }
-
- /**
- * Take the search criteria passed into the method and create a SQL WHERE clause.
- * @param fields the properties to search against
- * @param searchTerm the string to search for
- * @return an object containing the selection and selection args
- */
- private WhereOptions buildWhereClause(JSONArray fields, String searchTerm) {
-
- ArrayList<String> where = new ArrayList<String>();
- ArrayList<String> whereArgs = new ArrayList<String>();
-
- WhereOptions options = new WhereOptions();
-
+
+ /**
+ * Take the search criteria passed into the method and create a SQL WHERE clause.
+ * @param fields the properties to search against
+ * @param searchTerm the string to search for
+ * @return an object containing the selection and selection args
+ */
+ private WhereOptions buildWhereClause(JSONArray fields, String searchTerm) {
+
+ ArrayList<String> where = new ArrayList<String>();
+ ArrayList<String> whereArgs = new ArrayList<String>();
+
+ WhereOptions options = new WhereOptions();
+
/*
* Special case where the user wants all fields returned
*/
@@ -469,1329 +468,1323 @@ public class ContactAccessorSdk5 extends ContactAccessor {
// Get all contacts with all properties
if ("%".equals(searchTerm)) {
options.setWhere("(" + ContactsContract.Contacts.DISPLAY_NAME + " LIKE ? )");
- options.setWhereArgs(new String[] {searchTerm});
+ options.setWhereArgs(new String[] { searchTerm });
return options;
} else {
// Get all contacts that match the filter but return all properties
where.add("(" + dbMap.get("displayName") + " LIKE ? )");
whereArgs.add(searchTerm);
- where.add("(" + dbMap.get("name") + " LIKE ? AND "
+ where.add("(" + dbMap.get("name") + " LIKE ? AND "
+ ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
- where.add("(" + dbMap.get("nickname") + " LIKE ? AND "
- + ContactsContract.Data.MIMETYPE + " = ? )");
+ where.add("(" + dbMap.get("nickname") + " LIKE ? AND "
+ + ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE);
- where.add("(" + dbMap.get("phoneNumbers") + " LIKE ? AND "
- + ContactsContract.Data.MIMETYPE + " = ? )");
+ where.add("(" + dbMap.get("phoneNumbers") + " LIKE ? AND "
+ + ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
- where.add("(" + dbMap.get("emails") + " LIKE ? AND "
- + ContactsContract.Data.MIMETYPE + " = ? )");
+ where.add("(" + dbMap.get("emails") + " LIKE ? AND "
+ + ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
- where.add("(" + dbMap.get("addresses") + " LIKE ? AND "
- + ContactsContract.Data.MIMETYPE + " = ? )");
+ where.add("(" + dbMap.get("addresses") + " LIKE ? AND "
+ + ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
- where.add("(" + dbMap.get("ims") + " LIKE ? AND "
- + ContactsContract.Data.MIMETYPE + " = ? )");
+ where.add("(" + dbMap.get("ims") + " LIKE ? AND "
+ + ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
- where.add("(" + dbMap.get("organizations") + " LIKE ? AND "
- + ContactsContract.Data.MIMETYPE + " = ? )");
+ where.add("(" + dbMap.get("organizations") + " LIKE ? AND "
+ + ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
- where.add("(" + dbMap.get("note") + " LIKE ? AND "
- + ContactsContract.Data.MIMETYPE + " = ? )");
+ where.add("(" + dbMap.get("note") + " LIKE ? AND "
+ + ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE);
- where.add("(" + dbMap.get("urls") + " LIKE ? AND "
- + ContactsContract.Data.MIMETYPE + " = ? )");
+ where.add("(" + dbMap.get("urls") + " LIKE ? AND "
+ + ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
}
}
- /*
- * Special case for when the user wants all the contacts but
- */
- if ("%".equals(searchTerm)) {
- options.setWhere("(" + ContactsContract.Contacts.DISPLAY_NAME + " LIKE ? )");
- options.setWhereArgs(new String[] {searchTerm});
- return options;
- }
-
- String key;
- try {
- //Log.d(LOG_TAG, "How many fields do we have = " + fields.length());
- for (int i=0; i<fields.length(); i++) {
- key = fields.getString(i);
+ /*
+ * Special case for when the user wants all the contacts but
+ */
+ if ("%".equals(searchTerm)) {
+ options.setWhere("(" + ContactsContract.Contacts.DISPLAY_NAME + " LIKE ? )");
+ options.setWhereArgs(new String[] { searchTerm });
+ return options;
+ }
+
+ String key;
+ try {
+ //Log.d(LOG_TAG, "How many fields do we have = " + fields.length());
+ for (int i = 0; i < fields.length(); i++) {
+ key = fields.getString(i);
if (key.equals("id")) {
where.add("(" + dbMap.get(key) + " = ? )");
- whereArgs.add(searchTerm.substring(1, searchTerm.length()-1));
+ whereArgs.add(searchTerm.substring(1, searchTerm.length() - 1));
}
else if (key.startsWith("displayName")) {
where.add("(" + dbMap.get(key) + " LIKE ? )");
whereArgs.add(searchTerm);
}
- else if (key.startsWith("name")) {
- 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("nickname")) {
- where.add("(" + dbMap.get(key) + " LIKE ? AND "
- + ContactsContract.Data.MIMETYPE + " = ? )");
- whereArgs.add(searchTerm);
- whereArgs.add(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE);
- }
- else if (key.startsWith("phoneNumbers")) {
- where.add("(" + dbMap.get(key) + " LIKE ? AND "
- + ContactsContract.Data.MIMETYPE + " = ? )");
- whereArgs.add(searchTerm);
- whereArgs.add(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
- }
- else if (key.startsWith("emails")) {
- where.add("(" + dbMap.get(key) + " LIKE ? AND "
- + ContactsContract.Data.MIMETYPE + " = ? )");
- whereArgs.add(searchTerm);
- whereArgs.add(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
- }
- else if (key.startsWith("addresses")) {
- where.add("(" + dbMap.get(key) + " LIKE ? AND "
- + ContactsContract.Data.MIMETYPE + " = ? )");
- whereArgs.add(searchTerm);
- whereArgs.add(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
- }
- else if (key.startsWith("ims")) {
- where.add("(" + dbMap.get(key) + " LIKE ? AND "
- + ContactsContract.Data.MIMETYPE + " = ? )");
- whereArgs.add(searchTerm);
- whereArgs.add(ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
- }
- else if (key.startsWith("organizations")) {
- where.add("(" + dbMap.get(key) + " LIKE ? AND "
- + ContactsContract.Data.MIMETYPE + " = ? )");
- whereArgs.add(searchTerm);
- whereArgs.add(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
- }
-// else if (key.startsWith("birthday")) {
+ else if (key.startsWith("name")) {
+ 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("nickname")) {
+ where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ + ContactsContract.Data.MIMETYPE + " = ? )");
+ whereArgs.add(searchTerm);
+ whereArgs.add(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE);
+ }
+ else if (key.startsWith("phoneNumbers")) {
+ where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ + ContactsContract.Data.MIMETYPE + " = ? )");
+ whereArgs.add(searchTerm);
+ whereArgs.add(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
+ }
+ else if (key.startsWith("emails")) {
+ where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ + ContactsContract.Data.MIMETYPE + " = ? )");
+ whereArgs.add(searchTerm);
+ whereArgs.add(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
+ }
+ else if (key.startsWith("addresses")) {
+ where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ + ContactsContract.Data.MIMETYPE + " = ? )");
+ whereArgs.add(searchTerm);
+ whereArgs.add(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
+ }
+ else if (key.startsWith("ims")) {
+ where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ + ContactsContract.Data.MIMETYPE + " = ? )");
+ whereArgs.add(searchTerm);
+ whereArgs.add(ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
+ }
+ else if (key.startsWith("organizations")) {
+ where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ + ContactsContract.Data.MIMETYPE + " = ? )");
+ whereArgs.add(searchTerm);
+ whereArgs.add(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
+ }
+ // else if (key.startsWith("birthday")) {
// where.add("(" + dbMap.get(key) + " LIKE ? AND "
// + ContactsContract.Data.MIMETYPE + " = ? )");
// }
- else if (key.startsWith("note")) {
- where.add("(" + dbMap.get(key) + " LIKE ? AND "
- + ContactsContract.Data.MIMETYPE + " = ? )");
- whereArgs.add(searchTerm);
- whereArgs.add(ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE);
+ else if (key.startsWith("note")) {
+ where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ + ContactsContract.Data.MIMETYPE + " = ? )");
+ whereArgs.add(searchTerm);
+ whereArgs.add(ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE);
+ }
+ else if (key.startsWith("urls")) {
+ where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ + ContactsContract.Data.MIMETYPE + " = ? )");
+ whereArgs.add(searchTerm);
+ whereArgs.add(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
+ }
+ }
+ } catch (JSONException e) {
+ Log.e(LOG_TAG, e.getMessage(), e);
}
- else if (key.startsWith("urls")) {
- where.add("(" + dbMap.get(key) + " LIKE ? AND "
- + ContactsContract.Data.MIMETYPE + " = ? )");
- whereArgs.add(searchTerm);
- whereArgs.add(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
+
+ // Creating the where string
+ StringBuffer selection = new StringBuffer();
+ for (int i = 0; i < where.size(); i++) {
+ selection.append(where.get(i));
+ if (i != (where.size() - 1)) {
+ selection.append(" OR ");
+ }
}
- }
- }
- catch (JSONException e) {
- Log.e(LOG_TAG, e.getMessage(), e);
- }
+ options.setWhere(selection.toString());
- // Creating the where string
- StringBuffer selection = new StringBuffer();
- for (int i=0; i<where.size(); i++) {
- selection.append(where.get(i));
- if (i != (where.size()-1)) {
- selection.append(" OR ");
- }
- }
- options.setWhere(selection.toString());
+ // Creating the where args array
+ String[] selectionArgs = new String[whereArgs.size()];
+ for (int i = 0; i < whereArgs.size(); i++) {
+ selectionArgs[i] = whereArgs.get(i);
+ }
+ options.setWhereArgs(selectionArgs);
- // Creating the where args array
- String[] selectionArgs = new String[whereArgs.size()];
- for (int i=0; i<whereArgs.size(); i++) {
- selectionArgs[i] = whereArgs.get(i);
+ return options;
}
- options.setWhereArgs(selectionArgs);
-
- return options;
- }
-
- /**
- * If the user passes in the '*' wildcard character for search then they want all fields for each contact
- *
- * @param fields
- * @return true if wildcard search requested, false otherwise
- */
- private boolean isWildCardSearch(JSONArray fields) {
- // Only do a wildcard search if we are passed ["*"]
- if (fields.length() == 1) {
- try {
+
+ /**
+ * If the user passes in the '*' wildcard character for search then they want all fields for each contact
+ *
+ * @param fields
+ * @return true if wildcard search requested, false otherwise
+ */
+ private boolean isWildCardSearch(JSONArray fields) {
+ // Only do a wildcard search if we are passed ["*"]
+ if (fields.length() == 1) {
+ try {
if ("*".equals(fields.getString(0))) {
return true;
- }
+ }
} catch (JSONException e) {
return false;
}
- }
+ }
return false;
}
/**
- * Create a ContactOrganization JSONObject
- * @param cursor the current database row
- * @return a JSONObject representing a ContactOrganization
- */
- private JSONObject organizationQuery(Cursor cursor) {
- JSONObject organization = new JSONObject();
- try {
- organization.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization._ID)));
- organization.put("pref", false); // Android does not store pref attribute
+ * Create a ContactOrganization JSONObject
+ * @param cursor the current database row
+ * @return a JSONObject representing a ContactOrganization
+ */
+ private JSONObject organizationQuery(Cursor cursor) {
+ JSONObject organization = new JSONObject();
+ try {
+ organization.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization._ID)));
+ organization.put("pref", false); // Android does not store pref attribute
organization.put("type", getOrgType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TYPE))));
- organization.put("department", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DEPARTMENT)));
- organization.put("name", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY)));
- organization.put("title", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE)));
- } catch (JSONException e) {
- Log.e(LOG_TAG, e.getMessage(), e);
+ organization.put("department", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DEPARTMENT)));
+ organization.put("name", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY)));
+ organization.put("title", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE)));
+ } catch (JSONException e) {
+ Log.e(LOG_TAG, e.getMessage(), e);
+ }
+ return organization;
}
- return organization;
- }
-
- /**
- * Create a ContactAddress JSONObject
- * @param cursor the current database row
- * @return a JSONObject representing a ContactAddress
- */
- private JSONObject addressQuery(Cursor cursor) {
- JSONObject address = new JSONObject();
- try {
- address.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal._ID)));
- address.put("pref", false); // Android does not store pref attribute
+
+ /**
+ * Create a ContactAddress JSONObject
+ * @param cursor the current database row
+ * @return a JSONObject representing a ContactAddress
+ */
+ private JSONObject addressQuery(Cursor cursor) {
+ JSONObject address = new JSONObject();
+ try {
+ address.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal._ID)));
+ address.put("pref", false); // Android does not store pref attribute
address.put("type", getAddressType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TYPE))));
- 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)));
- } catch (JSONException e) {
- Log.e(LOG_TAG, e.getMessage(), e);
- }
- return address;
- }
-
- /**
- * Create a ContactName JSONObject
- * @param cursor the current database row
- * @return a JSONObject representing a ContactName
- */
- private JSONObject nameQuery(Cursor cursor) {
- JSONObject contactName = new JSONObject();
- try {
- String familyName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
- String givenName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
- String middleName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));
- String honorificPrefix = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX));
- String honorificSuffix = cursor.getString(cursor.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);
+ 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)));
+ } catch (JSONException e) {
+ Log.e(LOG_TAG, e.getMessage(), e);
+ }
+ return address;
}
- return contactName;
- }
-
- /**
- * Create a ContactField JSONObject
- * @param cursor the current database row
- * @return a JSONObject representing a ContactField
- */
- private JSONObject phoneQuery(Cursor cursor) {
- JSONObject phoneNumber = new JSONObject();
- try {
- phoneNumber.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID)));
- phoneNumber.put("pref", false); // Android does not store pref attribute
- phoneNumber.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
- phoneNumber.put("type", getPhoneType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))));
- } catch (JSONException e) {
- Log.e(LOG_TAG, e.getMessage(), e);
+
+ /**
+ * Create a ContactName JSONObject
+ * @param cursor the current database row
+ * @return a JSONObject representing a ContactName
+ */
+ private JSONObject nameQuery(Cursor cursor) {
+ JSONObject contactName = new JSONObject();
+ try {
+ String familyName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
+ String givenName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
+ String middleName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));
+ String honorificPrefix = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX));
+ String honorificSuffix = cursor.getString(cursor.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);
+ }
+ return contactName;
}
- catch (Exception excp) {
- Log.e(LOG_TAG, excp.getMessage(), excp);
- }
- return phoneNumber;
- }
-
- /**
- * Create a ContactField JSONObject
- * @param cursor the current database row
- * @return a JSONObject representing a ContactField
- */
- private JSONObject emailQuery(Cursor cursor) {
- JSONObject email = new JSONObject();
- try {
- email.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email._ID)));
- email.put("pref", false); // Android does not store pref attribute
- email.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
- email.put("type", getContactType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE))));
- } catch (JSONException e) {
- Log.e(LOG_TAG, e.getMessage(), e);
+
+ /**
+ * Create a ContactField JSONObject
+ * @param cursor the current database row
+ * @return a JSONObject representing a ContactField
+ */
+ private JSONObject phoneQuery(Cursor cursor) {
+ JSONObject phoneNumber = new JSONObject();
+ try {
+ phoneNumber.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID)));
+ phoneNumber.put("pref", false); // Android does not store pref attribute
+ phoneNumber.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
+ phoneNumber.put("type", getPhoneType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))));
+ } catch (JSONException e) {
+ Log.e(LOG_TAG, e.getMessage(), e);
+ } catch (Exception excp) {
+ Log.e(LOG_TAG, excp.getMessage(), excp);
+ }
+ return phoneNumber;
}
- return email;
- }
-
- /**
- * Create a ContactField JSONObject
- * @param cursor the current database row
- * @return a JSONObject representing a ContactField
- */
- private JSONObject imQuery(Cursor cursor) {
- JSONObject im = new JSONObject();
- try {
- im.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im._ID)));
- im.put("pref", false); // Android does not store pref attribute
- im.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
- im.put("type", getContactType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE))));
- } catch (JSONException e) {
- Log.e(LOG_TAG, e.getMessage(), e);
+
+ /**
+ * Create a ContactField JSONObject
+ * @param cursor the current database row
+ * @return a JSONObject representing a ContactField
+ */
+ private JSONObject emailQuery(Cursor cursor) {
+ JSONObject email = new JSONObject();
+ try {
+ email.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email._ID)));
+ email.put("pref", false); // Android does not store pref attribute
+ email.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
+ email.put("type", getContactType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE))));
+ } catch (JSONException e) {
+ Log.e(LOG_TAG, e.getMessage(), e);
+ }
+ return email;
}
- return im;
- }
-
- /**
- * Create a ContactField JSONObject
- * @param cursor the current database row
- * @return a JSONObject representing a ContactField
- */
- private JSONObject websiteQuery(Cursor cursor) {
- JSONObject website = new JSONObject();
- try {
- website.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website._ID)));
- website.put("pref", false); // Android does not store pref attribute
- website.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL)));
- website.put("type", getContactType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE))));
- } catch (JSONException e) {
- Log.e(LOG_TAG, e.getMessage(), e);
+
+ /**
+ * Create a ContactField JSONObject
+ * @param cursor the current database row
+ * @return a JSONObject representing a ContactField
+ */
+ private JSONObject imQuery(Cursor cursor) {
+ JSONObject im = new JSONObject();
+ try {
+ im.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im._ID)));
+ im.put("pref", false); // Android does not store pref attribute
+ im.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
+ im.put("type", getContactType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE))));
+ } catch (JSONException e) {
+ Log.e(LOG_TAG, e.getMessage(), e);
+ }
+ return im;
}
- return website;
- }
-
- /**
- * Create a ContactField JSONObject
- * @param contactId
- * @return a JSONObject representing a ContactField
- */
- private JSONObject photoQuery(Cursor cursor, String contactId) {
- JSONObject photo = new JSONObject();
- try {
- photo.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo._ID)));
- photo.put("pref", false);
- photo.put("type", "url");
- Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, (new Long(contactId)));
- Uri photoUri = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
- photo.put("value", photoUri.toString());
- } catch (JSONException e) {
- Log.e(LOG_TAG, e.getMessage(), e);
+
+ /**
+ * Create a ContactField JSONObject
+ * @param cursor the current database row
+ * @return a JSONObject representing a ContactField
+ */
+ private JSONObject websiteQuery(Cursor cursor) {
+ JSONObject website = new JSONObject();
+ try {
+ website.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website._ID)));
+ website.put("pref", false); // Android does not store pref attribute
+ website.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL)));
+ website.put("type", getContactType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE))));
+ } catch (JSONException e) {
+ Log.e(LOG_TAG, e.getMessage(), e);
+ }
+ return website;
}
- return photo;
- }
-
- @Override
- /**
- * This method will save a contact object into the devices contacts database.
- *
- * @param contact the contact to be saved.
- * @returns the id if the contact is successfully saved, null otherwise.
- */
- public String save(JSONObject contact) {
- AccountManager mgr = AccountManager.get(mApp);
- Account[] accounts = mgr.getAccounts();
- String accountName = null;
- String accountType = null;
-
- if (accounts.length == 1) {
- accountName = accounts[0].name;
- accountType = accounts[0].type;
+
+ /**
+ * Create a ContactField JSONObject
+ * @param contactId
+ * @return a JSONObject representing a ContactField
+ */
+ private JSONObject photoQuery(Cursor cursor, String contactId) {
+ JSONObject photo = new JSONObject();
+ try {
+ photo.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo._ID)));
+ photo.put("pref", false);
+ photo.put("type", "url");
+ Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, (new Long(contactId)));
+ Uri photoUri = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
+ photo.put("value", photoUri.toString());
+ } catch (JSONException e) {
+ Log.e(LOG_TAG, e.getMessage(), e);
+ }
+ return photo;
}
- else if (accounts.length > 1) {
- for(Account a : accounts) {
- if(a.type.contains("eas")&& a.name.matches(EMAIL_REGEXP)) /*Exchange ActiveSync*/ {
- accountName = a.name;
- accountType = a.type;
- break;
- }
- }
- if(accountName == null){
- for(Account a : accounts){
- if(a.type.contains("com.google") && a.name.matches(EMAIL_REGEXP)) /*Google sync provider*/ {
+
+ @Override
+ /**
+ * This method will save a contact object into the devices contacts database.
+ *
+ * @param contact the contact to be saved.
+ * @returns the id if the contact is successfully saved, null otherwise.
+ */
+ public String save(JSONObject contact) {
+ AccountManager mgr = AccountManager.get(mApp.getActivity());
+ Account[] accounts = mgr.getAccounts();
+ String accountName = null;
+ String accountType = null;
+
+ if (accounts.length == 1) {
+ accountName = accounts[0].name;
+ accountType = accounts[0].type;
+ }
+ else if (accounts.length > 1) {
+ for (Account a : accounts) {
+ if (a.type.contains("eas") && a.name.matches(EMAIL_REGEXP)) /*Exchange ActiveSync*/{
accountName = a.name;
accountType = a.type;
break;
}
}
- }
- if(accountName == null){
- for(Account a : accounts){
- if(a.name.matches(EMAIL_REGEXP)) /*Last resort, just look for an email address...*/ {
- accountName = a.name;
- accountType = a.type;
- break;
+ if (accountName == null) {
+ for (Account a : accounts) {
+ if (a.type.contains("com.google") && a.name.matches(EMAIL_REGEXP)) /*Google sync provider*/{
+ accountName = a.name;
+ accountType = a.type;
+ break;
+ }
+ }
+ }
+ if (accountName == null) {
+ for (Account a : accounts) {
+ if (a.name.matches(EMAIL_REGEXP)) /*Last resort, just look for an email address...*/{
+ accountName = a.name;
+ accountType = a.type;
+ break;
+ }
}
}
}
- }
- String id = getJsonString(contact, "id");
- // Create new contact
- if (id == null) {
- return createNewContact(contact, accountType, accountName);
- }
- // Modify existing contact
- else {
- return modifyContact(id, contact, accountType, accountName);
- }
- }
-
- /**
- * Creates a new contact and stores it in the database
- *
- * @param id the raw contact id which is required for linking items to the contact
- * @param contact the contact to be saved
- * @param account the account to be saved under
- */
- private String modifyContact(String id, JSONObject contact, String accountType, String accountName) {
- // Get the RAW_CONTACT_ID which is needed to insert new values in an already existing contact.
- // But not needed to update existing values.
- int rawId = (new Integer(getJsonString(contact,"rawId"))).intValue();
-
- // Create a list of attributes to add to the contact database
- ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
-
- //Add contact type
- ops.add(ContentProviderOperation.newUpdate(ContactsContract.RawContacts.CONTENT_URI)
- .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
- .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName)
- .build());
-
- // Modify name
- JSONObject name;
- try {
- String displayName = getJsonString(contact, "displayName");
- name = contact.getJSONObject("name");
- if (displayName != null || name != null) {
- ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
- .withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " +
- ContactsContract.Data.MIMETYPE + "=?",
- new String[]{id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE});
-
- if (displayName != null) {
- builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName);
- }
-
- String familyName = getJsonString(name, "familyName");
- if (familyName != null) {
- builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, familyName);
- }
- String middleName = getJsonString(name, "middleName");
- if (middleName != null) {
- builder.withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName);
- }
- String givenName = getJsonString(name, "givenName");
- if (givenName != null) {
- builder.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, givenName);
+ String id = getJsonString(contact, "id");
+ // Create new contact
+ if (id == null) {
+ return createNewContact(contact, accountType, accountName);
}
- String honorificPrefix = getJsonString(name, "honorificPrefix");
- if (honorificPrefix != null) {
- builder.withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, honorificPrefix);
+ // Modify existing contact
+ else {
+ return modifyContact(id, contact, accountType, accountName);
}
- String honorificSuffix = getJsonString(name, "honorificSuffix");
- if (honorificSuffix != null) {
- builder.withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, honorificSuffix);
- }
-
- ops.add(builder.build());
- }
- } catch (JSONException e1) {
- Log.d(LOG_TAG, "Could not get name");
}
-
- // Modify phone numbers
- JSONArray phones = null;
- try {
- phones = contact.getJSONArray("phoneNumbers");
- if (phones != null) {
- for (int i=0; i<phones.length(); i++) {
- JSONObject phone = (JSONObject)phones.get(i);
- String phoneId = getJsonString(phone, "id");
- // This is a new phone so do a DB insert
- if (phoneId == null) {
- ContentValues contentValues = new ContentValues();
- contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
- contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
- contentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, getJsonString(phone, "value"));
- contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, getPhoneType(getJsonString(phone, "type")));
-
- ops.add(ContentProviderOperation.newInsert(
- ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());
- }
- // This is an existing phone so do a DB update
- else {
- ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
- .withSelection(ContactsContract.CommonDataKinds.Phone._ID + "=? AND " +
- ContactsContract.Data.MIMETYPE + "=?",
- new String[]{phoneId, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE})
- .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, getJsonString(phone, "value"))
- .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, getPhoneType(getJsonString(phone, "type")))
- .build());
- }
+
+ /**
+ * Creates a new contact and stores it in the database
+ *
+ * @param id the raw contact id which is required for linking items to the contact
+ * @param contact the contact to be saved
+ * @param account the account to be saved under
+ */
+ private String modifyContact(String id, JSONObject contact, String accountType, String accountName) {
+ // Get the RAW_CONTACT_ID which is needed to insert new values in an already existing contact.
+ // But not needed to update existing values.
+ int rawId = (new Integer(getJsonString(contact, "rawId"))).intValue();
+
+ // Create a list of attributes to add to the contact database
+ ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
+
+ //Add contact type
+ ops.add(ContentProviderOperation.newUpdate(ContactsContract.RawContacts.CONTENT_URI)
+ .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
+ .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName)
+ .build());
+
+ // Modify name
+ JSONObject name;
+ try {
+ String displayName = getJsonString(contact, "displayName");
+ name = contact.getJSONObject("name");
+ if (displayName != null || name != null) {
+ ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
+ .withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " +
+ ContactsContract.Data.MIMETYPE + "=?",
+ new String[] { id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE });
+
+ if (displayName != null) {
+ builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName);
+ }
+
+ String familyName = getJsonString(name, "familyName");
+ if (familyName != null) {
+ builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, familyName);
+ }
+ String middleName = getJsonString(name, "middleName");
+ if (middleName != null) {
+ builder.withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName);
+ }
+ String givenName = getJsonString(name, "givenName");
+ if (givenName != null) {
+ builder.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, givenName);
+ }
+ String honorificPrefix = getJsonString(name, "honorificPrefix");
+ if (honorificPrefix != null) {
+ builder.withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, honorificPrefix);
+ }
+ String honorificSuffix = getJsonString(name, "honorificSuffix");
+ if (honorificSuffix != null) {
+ builder.withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, honorificSuffix);
+ }
+
+ ops.add(builder.build());
+ }
+ } catch (JSONException e1) {
+ Log.d(LOG_TAG, "Could not get name");
}
- }
- }
- catch (JSONException e) {
- Log.d(LOG_TAG, "Could not get phone numbers");
- }
-
- // Modify emails
- JSONArray emails = null;
- try {
- emails = contact.getJSONArray("emails");
- if (emails != null) {
- for (int i=0; i<emails.length(); i++) {
- JSONObject email = (JSONObject)emails.get(i);
- String emailId = getJsonString(email, "id");
- // This is a new email so do a DB insert
- if (emailId==null) {
- ContentValues contentValues = new ContentValues();
- contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
- contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
- contentValues.put(ContactsContract.CommonDataKinds.Email.DATA, getJsonString(email, "value"));
- contentValues.put(ContactsContract.CommonDataKinds.Email.TYPE, getContactType(getJsonString(email, "type")));
-
- ops.add(ContentProviderOperation.newInsert(
- ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());
- }
- // This is an existing email so do a DB update
- else {
- ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
- .withSelection(ContactsContract.CommonDataKinds.Email._ID + "=? AND " +
- ContactsContract.Data.MIMETYPE + "=?",
- new String[]{emailId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE})
- .withValue(ContactsContract.CommonDataKinds.Email.DATA, getJsonString(email, "value"))
- .withValue(ContactsContract.CommonDataKinds.Email.TYPE, getContactType(getJsonString(email, "type")))
- .build());
- }
+
+ // Modify phone numbers
+ JSONArray phones = null;
+ try {
+ phones = contact.getJSONArray("phoneNumbers");
+ if (phones != null) {
+ for (int i = 0; i < phones.length(); i++) {
+ JSONObject phone = (JSONObject) phones.get(i);
+ String phoneId = getJsonString(phone, "id");
+ // This is a new phone so do a DB insert
+ if (phoneId == null) {
+ ContentValues contentValues = new ContentValues();
+ contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
+ contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
+ contentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, getJsonString(phone, "value"));
+ contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, getPhoneType(getJsonString(phone, "type")));
+
+ ops.add(ContentProviderOperation.newInsert(
+ ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());
+ }
+ // This is an existing phone so do a DB update
+ else {
+ ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
+ .withSelection(ContactsContract.CommonDataKinds.Phone._ID + "=? AND " +
+ ContactsContract.Data.MIMETYPE + "=?",
+ new String[] { phoneId, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE })
+ .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, getJsonString(phone, "value"))
+ .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, getPhoneType(getJsonString(phone, "type")))
+ .build());
+ }
+ }
+ }
+ } catch (JSONException e) {
+ Log.d(LOG_TAG, "Could not get phone numbers");
}
- }
- }
- catch (JSONException e) {
- Log.d(LOG_TAG, "Could not get emails");
- }
- // Modify addresses
- JSONArray addresses = null;
- try {
- addresses = contact.getJSONArray("addresses");
- if (addresses != null) {
- for (int i=0; i<addresses.length(); i++) {
- JSONObject address = (JSONObject)addresses.get(i);
- String addressId = getJsonString(address, "id");
- // This is a new address so do a DB insert
- if (addressId==null) {
- ContentValues contentValues = new ContentValues();
- contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
- contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
+ // Modify emails
+ JSONArray emails = null;
+ try {
+ emails = contact.getJSONArray("emails");
+ if (emails != null) {
+ for (int i = 0; i < emails.length(); i++) {
+ JSONObject email = (JSONObject) emails.get(i);
+ String emailId = getJsonString(email, "id");
+ // This is a new email so do a DB insert
+ if (emailId == null) {
+ ContentValues contentValues = new ContentValues();
+ contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
+ contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
+ contentValues.put(ContactsContract.CommonDataKinds.Email.DATA, getJsonString(email, "value"));
+ contentValues.put(ContactsContract.CommonDataKinds.Email.TYPE, getContactType(getJsonString(email, "type")));
+
+ ops.add(ContentProviderOperation.newInsert(
+ ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());
+ }
+ // This is an existing email so do a DB update
+ else {
+ ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
+ .withSelection(ContactsContract.CommonDataKinds.Email._ID + "=? AND " +
+ ContactsContract.Data.MIMETYPE + "=?",
+ new String[] { emailId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE })
+ .withValue(ContactsContract.CommonDataKinds.Email.DATA, getJsonString(email, "value"))
+ .withValue(ContactsContract.CommonDataKinds.Email.TYPE, getContactType(getJsonString(email, "type")))
+ .build());
+ }
+ }
+ }
+ } catch (JSONException e) {
+ Log.d(LOG_TAG, "Could not get emails");
+ }
+
+ // Modify addresses
+ JSONArray addresses = null;
+ try {
+ addresses = contact.getJSONArray("addresses");
+ if (addresses != null) {
+ for (int i = 0; i < addresses.length(); i++) {
+ JSONObject address = (JSONObject) addresses.get(i);
+ String addressId = getJsonString(address, "id");
+ // This is a new address so do a DB insert
+ if (addressId == null) {
+ ContentValues contentValues = new ContentValues();
+ contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
+ contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, getAddressType(getJsonString(address, "type")));
- contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, getJsonString(address, "formatted"));
- contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, getJsonString(address, "streetAddress"));
- contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, getJsonString(address, "locality"));
- contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.REGION, getJsonString(address, "region"));
- contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, getJsonString(address, "postalCode"));
- contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, getJsonString(address, "country"));
-
- ops.add(ContentProviderOperation.newInsert(
- ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());
- }
- // This is an existing address so do a DB update
- else {
- ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
- .withSelection(ContactsContract.CommonDataKinds.StructuredPostal._ID + "=? AND " +
- ContactsContract.Data.MIMETYPE + "=?",
- new String[]{addressId, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE})
+ contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, getJsonString(address, "formatted"));
+ contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, getJsonString(address, "streetAddress"));
+ contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, getJsonString(address, "locality"));
+ contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.REGION, getJsonString(address, "region"));
+ contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, getJsonString(address, "postalCode"));
+ contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, getJsonString(address, "country"));
+
+ ops.add(ContentProviderOperation.newInsert(
+ ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());
+ }
+ // This is an existing address so do a DB update
+ else {
+ ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
+ .withSelection(ContactsContract.CommonDataKinds.StructuredPostal._ID + "=? AND " +
+ ContactsContract.Data.MIMETYPE + "=?
<TRUNCATED>