You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by bo...@apache.org on 2012/06/06 23:36:05 UTC

[2/10] Massive Merge Commit of DEATH TO SPACES

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/81195db8/framework/src/org/apache/cordova/ContactAccessorSdk5.java
----------------------------------------------------------------------
diff --cc framework/src/org/apache/cordova/ContactAccessorSdk5.java
index ec7b39f,52cf599..fea9983
--- a/framework/src/org/apache/cordova/ContactAccessorSdk5.java
+++ b/framework/src/org/apache/cordova/ContactAccessorSdk5.java
@@@ -25,10 -25,10 +25,7 @@@ import java.io.FileNotFoundException
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.URL;
--import java.text.ParseException;
--import java.text.SimpleDateFormat;
  import java.util.ArrayList;
--import java.util.Date;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Iterator;
@@@ -76,51 -74,51 +73,51 @@@ 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;
 +    /**
 +     * 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>*/
 +    private static final String EMAIL_REGEXP = ".+@.+\\.+.+"; /* <anything>@<anything>.<anything>*/
  
 -  /**
 -   * A static map that converts the JavaScript property name to Android database column name.
 -   */
 +    /**
 +     * 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.START_DATE);
--      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);
      }
  
      /**
@@@ -539,103 -539,114 +536,99 @@@
                      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")) {
-             try {
-                 SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
-                 Date searchDate = format.parse(searchTerm.substring(1, searchTerm.length()-1));
-                 // Have to subtract one from the month as JavaScript's January is 01
-                 // while Java's January is 00.
-                 searchDate.setMonth(searchDate.getMonth()-1);
-                 SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd");
-                 
-                 where.add("(" + dbMap.get(key) + " = ? AND " 
-                     + ContactsContract.Data.MIMETYPE + " = ? )");                 
-                 whereArgs.add(newFormat.format(searchDate));
-                 whereArgs.add(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
-             }
-             catch (ParseException e) {
-                 Log.d(LOG_TAG, "Bad romance format");
 -          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("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("urls")) {
++                    where.add("(" + dbMap.get(key) + " LIKE ? AND "
++                            + ContactsContract.Data.MIMETYPE + " = ? )");
++                    whereArgs.add(searchTerm);
++                    whereArgs.add(ContactsContract.CommonDataKinds.Website.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);
++        } catch (JSONException e) {
++            Log.e(LOG_TAG, e.getMessage(), e);
          }
 -        else if (key.startsWith("birthday")) {
 -            try {
 -                SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
 -                Date searchDate = format.parse(searchTerm.substring(1, searchTerm.length()-1));
 -                // Have to subtract one from the month as JavaScript's January is 01
 -                // while Java's January is 00.
 -                searchDate.setMonth(searchDate.getMonth()-1);
 -                SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd");
  
 -                where.add("(" + dbMap.get(key) + " = ? AND "
 -                    + ContactsContract.Data.MIMETYPE + " = ? )");
 -                whereArgs.add(newFormat.format(searchDate));
 -                whereArgs.add(ContactsContract.CommonDataKinds.Event.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 (ParseException e) {
 -                Log.d(LOG_TAG, "Bad romance format");
 -            }
 -        }
 -        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);
 -    }
 +        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;
                  }
@@@ -871,765 -872,751 +864,736 @@@
                  }
              }
          }
 -    }
  
 -    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);
 +        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);
 -        }
 +    /**
 +     * 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);
 -        }
 +                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");
 -    }
 +                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());
 -          }
 +        // 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 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 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");
          }
 -      }
 -    }
 -    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 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 + "=?",
 +                                        new String[] { addressId, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE })
                                  .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, getAddressType(getJsonString(address, "type")))
 -                    .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, getJsonString(address, "formatted"))
 -                    .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, getJsonString(address, "streetAddress"))
 -                    .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, getJsonString(address, "locality"))
 -                    .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, getJsonString(address, "region"))
 -                    .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, getJsonString(address, "postalCode"))
 -                    .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, getJsonString(address, "country"))
 -                    .build());
 -          }
 +                                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, getJsonString(address, "formatted"))
 +                                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, getJsonString(address, "streetAddress"))
 +                                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, getJsonString(address, "locality"))
 +                                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, getJsonString(address, "region"))
 +                                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, getJsonString(address, "postalCode"))
 +                                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, getJsonString(address, "country"))
 +                                .build());
 +                    }
 +                }
 +            }
 +        } catch (JSONException e) {
 +            Log.d(LOG_TAG, "Could not get addresses");
          }
 -      }
 -    }
 -    catch (JSONException e) {
 -      Log.d(LOG_TAG, "Could not get addresses");
 -    }
  
 -    // Modify organizations
 -    JSONArray organizations = null;
 -    try {
 -      organizations = contact.getJSONArray("organizations");
 -      if (organizations != null) {
 -        for (int i=0; i<organizations.length(); i++) {
 -          JSONObject org = (JSONObject)organizations.get(i);
 -          String orgId = getJsonString(org, "id");
 -          // This is a new organization so do a DB insert
 -          if (orgId==null) {
 -            ContentValues contentValues = new ContentValues();
 -              contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
 -              contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
 +        // Modify organizations
 +        JSONArray organizations = null;
 +        try {
 +            organizations = contact.getJSONArray("organizations");
 +            if (organizations != null) {
 +                for (int i = 0; i < organizations.length(); i++) {
 +                    JSONObject org = (JSONObject) organizations.get(i);
 +                    String orgId = getJsonString(org, "id");
 +                    // This is a new organization so do a DB insert
 +                    if (orgId == null) {
 +                        ContentValues contentValues = new ContentValues();
 +                        contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
 +                        contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
                          contentValues.put(ContactsContract.CommonDataKinds.Organization.TYPE, getOrgType(getJsonString(org, "type")));
 -              contentValues.put(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, getJsonString(org, "department"));
 -                contentValues.put(ContactsContract.CommonDataKinds.Organization.COMPANY, getJsonString(org, "name"));
 -                contentValues.put(ContactsContract.CommonDataKinds.Organization.TITLE, getJsonString(org, "title"));
 -
 -              ops.add(ContentProviderOperation.newInsert(
 -                      ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());
 -          }
 -          // This is an existing organization so do a DB update
 -          else {
 -            ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
 -                    .withSelection(ContactsContract.CommonDataKinds.Organization._ID + "=? AND " +
 -                    ContactsContract.Data.MIMETYPE + "=?",
 -                    new String[]{orgId, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE})
 +                        contentValues.put(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, getJsonString(org, "department"));
 +                        contentValues.put(ContactsContract.CommonDataKinds.Organization.COMPANY, getJsonString(org, "name"));
 +                        contentValues.put(ContactsContract.CommonDataKinds.Organization.TITLE, getJsonString(org, "title"));
 +
 +                        ops.add(ContentProviderOperation.newInsert(
 +                                ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());
 +                    }
 +                    // This is an existing organization so do a DB update
 +                    else {
 +                        ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
 +                                .withSelection(ContactsContract.CommonDataKinds.Organization._ID + "=? AND " +
 +                                        ContactsContract.Data.MIMETYPE + "=?",
 +                                        new String[] { orgId, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE })
                                  .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, getOrgType(getJsonString(org, "type")))
 -                    .withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, getJsonString(org, "department"))
 -                    .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, getJsonString(org, "name"))
 -                    .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, getJsonString(org, "title"))
 -                    .build());
 -          }
 +                                .withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, getJsonString(org, "department"))
 +                                .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, getJsonString(org, "name"))
 +                                .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, getJsonString(org, "title"))
 +                                .build());
 +                    }
 +                }
 +            }
 +        } catch (JSONException e) {
 +            Log.d(LOG_TAG, "Could not get organizations");
          }
 -      }
 -    }
 -    catch (JSONException e) {
 -      Log.d(LOG_TAG, "Could not get organizations");
 -    }
  
 -    // Modify IMs
 -    JSONArray ims = null;
 -    try {
 -      ims = contact.getJSONArray("ims");
 -      if (ims != null) {
 -        for (int i=0; i<ims.length(); i++) {
 -          JSONObject im = (JSONObject)ims.get(i);
 -          String imId = getJsonString(im, "id");
 -          // This is a new IM so do a DB insert
 -          if (imId==null) {
 -            ContentValues contentValues = new ContentValues();
 -              contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
 -              contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
 -              contentValues.put(ContactsContract.CommonDataKinds.Im.DATA, getJsonString(im, "value"));
 -                contentValues.put(ContactsContract.CommonDataKinds.Im.TYPE, getContactType(getJsonString(im, "type")));
 -
 -              ops.add(ContentProviderOperation.newInsert(
 -                      ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());
 -          }
 -          // This is an existing IM so do a DB update
 -          else {
 +        // Modify IMs
 +        JSONArray ims = null;
 +        try {
 +            ims = contact.getJSONArray("ims");
 +            if (ims != null) {
 +                for (int i = 0; i < ims.length(); i++) {
 +                    JSONObject im = (JSONObject) ims.get(i);
 +                    String imId = getJsonString(im, "id");
 +                    // This is a new IM so do a DB insert
 +                    if (imId == null) {
 +                        ContentValues contentValues = new ContentValues();
 +                        contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
 +                        contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
 +                        contentValues.put(ContactsContract.CommonDataKinds.Im.DATA, getJsonString(im, "value"));
 +                        contentValues.put(ContactsContract.CommonDataKinds.Im.TYPE, getContactType(getJsonString(im, "type")));
 +
 +                        ops.add(ContentProviderOperation.newInsert(
 +                                ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());
 +                    }
 +                    // This is an existing IM so do a DB update
 +                    else {
 +                        ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
 +                                .withSelection(ContactsContract.CommonDataKinds.Im._ID + "=? AND " +
 +                                        ContactsContract.Data.MIMETYPE + "=?",
 +                                        new String[] { imId, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE })
 +                                .withValue(ContactsContract.CommonDataKinds.Im.DATA, getJsonString(im, "value"))
 +                                .withValue(ContactsContract.CommonDataKinds.Im.TYPE, getContactType(getJsonString(im, "type")))
 +                                .build());
 +                    }
 +                }
 +            }
 +        } catch (JSONException e) {
 +            Log.d(LOG_TAG, "Could not get emails");
 +        }
 +
-     // Modify note
-     String note = getJsonString(contact, "note");
-     ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
-         .withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " + 
-             ContactsContract.Data.MIMETYPE + "=?", 
-             new String[]{id,ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE})
-             .withValue(ContactsContract.CommonDataKinds.Note.NOTE, note)
-         .build());
- 
-     // Modify nickname
-     String nickname = getJsonString(contact, "nickname");
-     if (nickname != null) {
-       ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
-           .withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " + 
-               ContactsContract.Data.MIMETYPE + "=?", 
-               new String[]{id,ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE})
-               .withValue(ContactsContract.CommonDataKinds.Nickname.NAME, nickname)
-               .build());
-     }
-       
-     // Modify urls  
-     JSONArray websites = null;
-     try {
-       websites = contact.getJSONArray("urls");
-       if (websites != null) {
-         for (int i=0; i<websites.length(); i++) {
-           JSONObject website = (JSONObject)websites.get(i);
-           String websiteId = getJsonString(website, "id");
-           // This is a new website so do a DB insert
-           if (websiteId==null) {
-             ContentValues contentValues = new ContentValues();
-               contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
-               contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
-               contentValues.put(ContactsContract.CommonDataKinds.Website.DATA, getJsonString(website, "value"));
-                 contentValues.put(ContactsContract.CommonDataKinds.Website.TYPE, getContactType(getJsonString(website, "type")));
- 
-               ops.add(ContentProviderOperation.newInsert(
-                       ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());            
-           }
-           // This is an existing website so do a DB update
-           else {
++        // Modify note
++        String note = getJsonString(contact, "note");
++        ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
++                .withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " +
++                        ContactsContract.Data.MIMETYPE + "=?",
++                        new String[] { id, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE })
++                .withValue(ContactsContract.CommonDataKinds.Note.NOTE, note)
++                .build());
++
++        // Modify nickname
++        String nickname = getJsonString(contact, "nickname");
++        if (nickname != null) {
              ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
 -                    .withSelection(ContactsContract.CommonDataKinds.Im._ID + "=? AND " +
 -                    ContactsContract.Data.MIMETYPE + "=?",
 -                    new String[]{imId, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE})
 -                    .withValue(ContactsContract.CommonDataKinds.Im.DATA, getJsonString(im, "value"))
 -                    .withValue(ContactsContract.CommonDataKinds.Im.TYPE, getContactType(getJsonString(im, "type")))
 +                    .withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " +
 +                            ContactsContract.Data.MIMETYPE + "=?",
 +                            new String[] { id, ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE })
 +                    .withValue(ContactsContract.CommonDataKinds.Nickname.NAME, nickname)
                      .build());
 -          }
          }
 -      }
 -    }
 -    catch (JSONException e) {
 -      Log.d(LOG_TAG, "Could not get emails");
 -    }
  
 -    // Modify note
 -    String note = getJsonString(contact, "note");
 -    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
 -        .withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " +
 -            ContactsContract.Data.MIMETYPE + "=?",
 -            new String[]{id,ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE})
 -            .withValue(ContactsContract.CommonDataKinds.Note.NOTE, note)
 -        .build());
 -
 -    // Modify nickname
 -    String nickname = getJsonString(contact, "nickname");
 -    if (nickname != null) {
 -      ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
 -          .withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " +
 -              ContactsContract.Data.MIMETYPE + "=?",
 -              new String[]{id,ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE})
 -              .withValue(ContactsContract.CommonDataKinds.Nickname.NAME, nickname)
 -              .build());
 -    }
 +        // Modify urls  
 +        JSONArray websites = null;
 +        try {
 +            websites = contact.getJSONArray("websites");
 +            if (websites != null) {
 +                for (int i = 0; i < websites.length(); i++) {
 +                    JSONObject website = (JSONObject) websites.get(i);
 +                    String websiteId = getJsonString(website, "id");
 +                    // This is a new website so do a DB insert
 +                    if (websiteId == null) {
 +                        ContentValues contentValues = new ContentValues();
 +                        contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
 +                        contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
 +                        contentValues.put(ContactsContract.CommonDataKinds.Website.DATA, getJsonString(website, "value"));
 +                        contentValues.put(ContactsContract.CommonDataKinds.Website.TYPE, getContactType(getJsonString(website, "type")));
 +
 +                        ops.add(ContentProviderOperation.newInsert(
 +                                ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());
 +                    }
 +                    // This is an existing website so do a DB update
 +                    else {
 +                        ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
 +                                .withSelection(ContactsContract.CommonDataKinds.Website._ID + "=? AND " +
 +                                        ContactsContract.Data.MIMETYPE + "=?",
 +                                        new String[] { websiteId, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE })
 +                                .withValue(ContactsContract.CommonDataKinds.Website.DATA, getJsonString(website, "value"))
 +                                .withValue(ContactsContract.CommonDataKinds.Website.TYPE, getContactType(getJsonString(website, "type")))
 +                                .build());
 +                    }
 +                }
 +            }
 +        } catch (JSONException e) {
 +            Log.d(LOG_TAG, "Could not get websites");
 +        }
  
 -    // Modify urls
 -    JSONArray websites = null;
 -    try {
 -      websites = contact.getJSONArray("urls");
 -      if (websites != null) {
 -        for (int i=0; i<websites.length(); i++) {
 -          JSONObject website = (JSONObject)websites.get(i);
 -          String websiteId = getJsonString(website, "id");
 -          // This is a new website so do a DB insert
 -          if (websiteId==null) {
 -            ContentValues contentValues = new ContentValues();
 -              contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
 -              contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
 -              contentValues.put(ContactsContract.CommonDataKinds.Website.DATA, getJsonString(website, "value"));
 -                contentValues.put(ContactsContract.CommonDataKinds.Website.TYPE, getContactType(getJsonString(website, "type")));
 -
 -              ops.add(ContentProviderOperation.newInsert(
 -                      ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());
 -          }
 -          // This is an existing website so do a DB update
 -          else {
 +        // Modify birthday
 +        String birthday = getJsonString(contact, "birthday");
 +        if (birthday != null) {
              ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
 -                    .withSelection(ContactsContract.CommonDataKinds.Website._ID + "=? AND " +
 -                    ContactsContract.Data.MIMETYPE + "=?",
 -                    new String[]{websiteId, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE})
 -                    .withValue(ContactsContract.CommonDataKinds.Website.DATA, getJsonString(website, "value"))
 -                    .withValue(ContactsContract.CommonDataKinds.Website.TYPE, getContactType(getJsonString(website, "type")))
 +                    .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_BIRTHDAY) })
 +                    .withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
 +                    .withValue(ContactsContract.CommonDataKinds.Event.START_DATE, birthday)
                      .build());
 -          }
          }
 -      }
 -    }
 -    catch (JSONException e) {
 -      Log.d(LOG_TAG, "Could not get websites");
 -    }
  
 -    // Modify birthday
 -    String birthday = getJsonString(contact, "birthday");
 -    if (birthday != 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_BIRTHDAY)})
 -              .withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
 -              .withValue(ContactsContract.CommonDataKinds.Event.START_DATE, birthday)
 -              .build());
 -    }
 +        // Modify photos
 +        JSONArray photos = null;
 +        try {
 +            photos = contact.getJSONArray("photos");
 +            if (photos != null) {
 +                for (int i = 0; i < photos.length(); i++) {
 +                    JSONObject photo = (JSONObject) photos.get(i);
 +                    String photoId = getJsonString(photo, "id");
 +                    byte[] bytes = getPhotoBytes(getJsonString(photo, "value"));
 +                    // This is a new photo so do a DB insert
 +                    if (photoId == null) {
 +                        ContentValues contentValues = new ContentValues();
 +                        contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
 +                        contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
 +                        contentValues.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
 +                        contentValues.put(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes);
 +
 +                        ops.add(ContentProviderOperation.newInsert(
 +                                ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());
 +                    }
 +                    // This is an existing photo so do a DB update
 +                    else {
 +                        ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
 +                                .withSelection(ContactsContract.CommonDataKinds.Photo._ID + "=? AND " +
 +                                        ContactsContract.Data.MIMETYPE + "=?",
 +                                        new String[] { photoId, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE })
 +                                .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
 +                                .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes)
 +                                .build());
 +                    }
 +                }
 +            }
 +        } catch (JSONException e) {
 +            Log.d(LOG_TAG, "Could not get photos");
 +        }
  
 -    // Modify photos
 -    JSONArray photos = null;
 -    try {
 -      photos = contact.getJSONArray("photos");
 -      if (photos != null) {
 -        for (int i=0; i<photos.length(); i++) {
 -          JSONObject photo = (JSONObject)photos.get(i);
 -          String photoId = getJsonString(photo, "id");
 -          byte[] bytes = getPhotoBytes(getJsonString(photo, "value"));
 -          // This is a new photo so do a DB insert
 -          if (photoId==null) {
 -            ContentValues contentValues = new ContentValues();
 -              contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
 -              contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
 -              contentValues.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
 -                contentValues.put(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes);
 -
 -              ops.add(ContentProviderOperation.newInsert(
 -                      ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());
 -          }
 -          // This is an existing photo so do a DB update
 -          else {
 -            ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
 -                    .withSelection(ContactsContract.CommonDataKinds.Photo._ID + "=? AND " +
 -                    ContactsContract.Data.MIMETYPE + "=?",
 -                    new String[]{photoId, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE})
 -                    .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
 -                    .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes)
 -                    .build());
 -          }
 +        boolean retVal = true;
 +
 +        //Modify contact
 +        try {
 +            mApp.getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
 +        } catch (RemoteException e) {
 +            Log.e(LOG_TAG, e.getMessage(), e);
 +            Log.e(LOG_TAG, Log.getStackTraceString(e), e);
 +            retVal = false;
 +        } catch (OperationApplicationException e) {
 +            Log.e(LOG_TAG, e.getMessage(), e);
 +            Log.e(LOG_TAG, Log.getStackTraceString(e), e);
 +            retVal = false;
 +        }
 +
 +        // if the save was a succes return the contact ID
 +        if (retVal) {
 +            return id;
 +        } else {
 +            return null;
          }
 -      }
 -    }
 -    catch (JSONException e) {
 -      Log.d(LOG_TAG, "Could not get photos");
      }
  
 -    boolean retVal = true;
 -
 -    //Modify contact
 -    try {
 -        mApp.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
 -    } catch (RemoteException e) {
 -      Log.e(LOG_TAG, e.getMessage(), e);
 -      Log.e(LOG_TAG, Log.getStackTraceString(e), e);
 -      retVal = false;
 -    } catch (OperationApplicationException e) {
 -      Log.e(LOG_TAG, e.getMessage(), e);
 -      Log.e(LOG_TAG, Log.getStackTraceString(e), e);
 -      retVal = false;
 +    /**
 +     * Add a website to a list of database actions to be performed
 +     * 
 +     * @param ops the list of database actions
 +     * @param website the item to be inserted
 +     */
 +    private void insertWebsite(ArrayList<ContentProviderOperation> ops,
 +            JSONObject website) {
 +        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 +                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 +                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)
 +                .withValue(ContactsContract.CommonDataKinds.Website.DATA, getJsonString(website, "value"))
 +                .withValue(ContactsContract.CommonDataKinds.Website.TYPE, getContactType(getJsonString(website, "type")))
 +                .build());
      }
  
 -    // if the save was a succes return the contact ID
 -    if (retVal) {
 -        return id;
 -    } else {
 -        return null;
 +    /**
 +     * Add an im to a list of database actions to be performed
 +     * 
 +     * @param ops the list of database actions
 +     * @param im the item to be inserted
 +     */
 +    private void insertIm(ArrayList<ContentProviderOperation> ops, JSONObject im) {
 +        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 +                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 +                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
 +                .withValue(ContactsContract.CommonDataKinds.Im.DATA, getJsonString(im, "value"))
 +                .withValue(ContactsContract.CommonDataKinds.Im.TYPE, getContactType(getJsonString(im, "type")))
 +                .build());
      }
 -  }
 -
 -  /**
 -   * Add a website to a list of database actions to be performed
 -   *
 -   * @param ops the list of database actions
 -   * @param website the item to be inserted
 -   */
 -  private void insertWebsite(ArrayList<ContentProviderOperation> ops,
 -      JSONObject website) {
 -    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 -            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 -            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)
 -            .withValue(ContactsContract.CommonDataKinds.Website.DATA, getJsonString(website, "value"))
 -            .withValue(ContactsContract.CommonDataKinds.Website.TYPE, getContactType(getJsonString(website, "type")))
 -            .build());
 -  }
 -
 -  /**
 -   * Add an im to a list of database actions to be performed
 -   *
 -   * @param ops the list of database actions
 -   * @param im the item to be inserted
 -   */
 -  private void insertIm(ArrayList<ContentProviderOperation> ops, JSONObject im) {
 -    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 -            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 -            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
 -            .withValue(ContactsContract.CommonDataKinds.Im.DATA, getJsonString(im, "value"))
 -            .withValue(ContactsContract.CommonDataKinds.Im.TYPE, getContactType(getJsonString(im, "type")))
 -            .build());
 -  }
  
 -  /**
 -   * Add an organization to a list of database actions to be performed
 -   *
 -   * @param ops the list of database actions
 -   * @param org the item to be inserted
 -   */
 -  private void insertOrganization(ArrayList<ContentProviderOperation> ops,
 -      JSONObject org) {
 -    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 -            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 -            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
 -            .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, getOrgType(getJsonString(org, "type")))
 +    /**
 +     * Add an organization to a list of database actions to be performed
 +     * 
 +     * @param ops the list of database actions
 +     * @param org the item to be inserted
 +     */
 +    private void insertOrganization(ArrayList<ContentProviderOperation> ops,
 +            JSONObject org) {
 +        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 +                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 +                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
 +                .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, getOrgType(getJsonString(org, "type")))
                  .withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, getJsonString(org, "department"))
 -            .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, getJsonString(org, "name"))
 -            .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, getJsonString(org, "title"))
 -            .build());
 -  }
 +                .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, getJsonString(org, "name"))
 +                .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, getJsonString(org, "title"))
 +                .build());
 +    }
  
 -  /**
 -   * Add an address to a list of database actions to be performed
 -   *
 -   * @param ops the list of database actions
 -   * @param address the item to be inserted
 -   */
 -  private void insertAddress(ArrayList<ContentProviderOperation> ops,
 -      JSONObject address) {
 -    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 -            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 -            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
 +    /**
 +     * Add an address to a list of database actions to be performed
 +     * 
 +     * @param ops the list of database actions
 +     * @param address the item to be inserted
 +     */
 +    private void insertAddress(ArrayList<ContentProviderOperation> ops,
 +            JSONObject address) {
 +        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 +                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 +                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
                  .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, getAddressType(getJsonString(address, "type")))
 -            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, getJsonString(address, "formatted"))
 -            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, getJsonString(address, "streetAddress"))
 -            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, getJsonString(address, "locality"))
 -            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, getJsonString(address, "region"))
 -            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, getJsonString(address, "postalCode"))
 -            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, getJsonString(address, "country"))
 -            .build());
 -  }
 -
 -  /**
 -   * Add an email to a list of database actions to be performed
 -   *
 -   * @param ops the list of database actions
 -   * @param email the item to be inserted
 -   */
 -  private void insertEmail(ArrayList<ContentProviderOperation> ops,
 -      JSONObject email) {
 -    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 -            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 -            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
 -            .withValue(ContactsContract.CommonDataKinds.Email.DATA, getJsonString(email, "value"))
 -            .withValue(ContactsContract.CommonDataKinds.Email.TYPE, getPhoneType(getJsonString(email, "type")))
 -            .build());
 -  }
 -
 -  /**
 -   * Add a phone to a list of database actions to be performed
 -   *
 -   * @param ops the list of database actions
 -   * @param phone the item to be inserted
 -   */
 -  private void insertPhone(ArrayList<ContentProviderOperation> ops,
 -      JSONObject phone) {
 -    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 -            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 -            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
 -            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, getJsonString(phone, "value"))
 -            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, getPhoneType(getJsonString(phone, "type")))
 -            .build());
 -  }
 -
 -  /**
 -   * Add a phone to a list of database actions to be performed
 -   *
 -   * @param ops the list of database actions
 -   * @param phone the item to be inserted
 -   */
 -  private void insertPhoto(ArrayList<ContentProviderOperation> ops,
 -      JSONObject photo) {
 -    byte[] bytes = getPhotoBytes(getJsonString(photo, "value"));
 -    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 -            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 -            .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
 -            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
 -            .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes)
 -            .build());
 -  }
 -
 -  /**
 -   * Gets the raw bytes from the supplied filename
 -   *
 -   * @param filename the file to read the bytes from
 -   * @return a byte array
 -   * @throws IOException
 -   */
 -  private byte[] getPhotoBytes(String filename) {
 -    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
 -    try {
 -      int bytesRead = 0;
 -      long totalBytesRead = 0;
 -      byte[] data = new byte[8192];
 -      InputStream in = getPathFromUri(filename);
 -
 -      while ((bytesRead = in.read(data, 0, data.length)) != -1 && totalBytesRead <= MAX_PHOTO_SIZE) {
 -        buffer.write(data, 0, bytesRead);
 -        totalBytesRead += bytesRead;
 -      }
 -
 -      in.close();
 -      buffer.flush();
 -    } catch (FileNotFoundException e) {
 -      Log.e(LOG_TAG, e.getMessage(), e);
 -    } catch (IOException e) {
 -      Log.e(LOG_TAG, e.getMessage(), e);
 +                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, getJsonString(address, "formatted"))
 +                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, getJsonString(address, "streetAddress"))
 +                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, getJsonString(address, "locality"))
 +                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, getJsonString(address, "region"))
 +                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, getJsonString(address, "postalCode"))
 +                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, getJsonString(address, "country"))
 +                .build());
      }
 -    return buffer.toByteArray();
 -  }
 -  /**
 -     * Get an input stream based on file path or uri content://, http://, file://
 -     *
 -     * @param path
 -     * @return an input stream
 -   * @throws IOException
 +
 +    /**
 +     * Add an email to a list of database actions to be performed
 +     * 
 +     * @param ops the list of database actions
 +     * @param email the item to be inserted
       */
 -    private InputStream getPathFromUri(String path) throws IOException {
 -      if (path.startsWith("content:")) {
 -        Uri uri = Uri.parse(path);
 -        return mApp.getContentResolver().openInputStream(uri);
 -      }
 -      if (path.startsWith("http:") || path.startsWith("file:")) {
 -        URL url = new URL(path);
 -        return url.openStream();
 -      }
 -      else {
 -        return new FileInputStream(path);
 -      }
 +    private void insertEmail(ArrayList<ContentProviderOperation> ops,
 +            JSONObject email) {
 +        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 +                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 +                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
 +                .withValue(ContactsContract.CommonDataKinds.Email.DATA, getJsonString(email, "value"))
 +                .withValue(ContactsContract.CommonDataKinds.Email.TYPE, getPhoneType(getJsonString(email, "type")))
 +                .build());
      }
  
 -  /**
 -   * Creates a new contact and stores it in the database
 -   *
 -   * @param contact the contact to be saved
 -   * @param account the account to be saved under
 -   */
 -  private String createNewContact(JSONObject contact, String accountType, String accountName) {
 -    // Create a list of attributes to add to the contact database
 -    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
 -
 -    //Add contact type
 -    ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
 -            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
 -            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName)
 -            .build());
 -
 -    // Add name
 -    try {
 -      JSONObject name = contact.optJSONObject("name");
 -      String displayName = contact.getString("displayName");
 -      if (displayName != null || name != null) {
 +    /**
 +     * Add a phone to a list of database actions to be performed
 +     * 
 +     * @param ops the list of database actions
 +     * @param phone the item to be inserted
 +     */
 +    private void insertPhone(ArrayList<ContentProviderOperation> ops,
 +            JSONObject phone) {
          ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 -            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 -            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
 -            .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName)
 -            .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, getJsonString(name, "familyName"))
 -            .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, getJsonString(name, "middleName"))
 -            .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, getJsonString(name, "givenName"))
 -            .withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, getJsonString(name, "honorificPrefix"))
 -            .withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, getJsonString(name, "honorificSuffix"))
 -            .build());
 -      }
 +                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 +                .withValue(ContactsContract.Data.MIMETYPE, 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 name object");
 +
 +    /**
 +     * Add a phone to a list of database actions to be performed
 +     * 
 +     * @param ops the list of database actions
 +     * @param phone the item to be inserted
 +     */
 +    private void insertPhoto(ArrayList<ContentProviderOperation> ops,
 +            JSONObject photo) {
 +        byte[] bytes = getPhotoBytes(getJsonString(photo, "value"));
 +        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 +                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 +                .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
 +                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
 +                .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes)
 +                .build());
      }
  
 -    //Add 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);
 -          insertPhone(ops, phone);
 +    /**
 +     * Gets the raw bytes from the supplied filename
 +     * 
 +     * @param filename the file to read the bytes from
 +     * @return a byte array
 +     * @throws IOException 
 +     */
 +    private byte[] getPhotoBytes(String filename) {
 +        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
 +        try {
 +            int bytesRead = 0;
 +            long totalBytesRead = 0;
 +            byte[] data = new byte[8192];
 +            InputStream in = getPathFromUri(filename);
 +
 +            while ((bytesRead = in.read(data, 0, data.length)) != -1 && totalBytesRead <= MAX_PHOTO_SIZE) {
 +                buffer.write(data, 0, bytesRead);
 +                totalBytesRead += bytesRead;
 +            }
 +
 +            in.close();
 +            buffer.flush();
 +        } catch (FileNotFoundException e) {
 +            Log.e(LOG_TAG, e.getMessage(), e);
 +        } catch (IOException e) {
 +            Log.e(LOG_TAG, e.getMessage(), e);
          }
 -      }
 -    }
 -    catch (JSONException e) {
 -      Log.d(LOG_TAG, "Could not get phone numbers");
 +        return buffer.toByteArray();
      }
  
 -    // Add 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);
 -          insertEmail(ops, email);
 +    /**
 +       * Get an input stream based on file path or uri content://, http://, file://
 +       * 
 +       * @param path
 +       * @return an input stream
 +     * @throws IOException 
 +       */
 +    private InputStream getPathFromUri(String path) throws IOException {
 +        if (path.startsWith("content:")) {
 +            Uri uri = Uri.parse(path);
 +            return mApp.getActivity().getContentResolver().openInputStream(uri);
 +        }
 +        if (path.startsWith("http:") || path.startsWith("file:")) {
 +            URL url = new URL(path);
 +            return url.openStream();
 +        }
 +        else {
 +            return new FileInputStream(path);
          }
 -      }
 -    }
 -    catch (JSONException e) {
 -      Log.d(LOG_TAG, "Could not get emails");
      }
  
 -    // Add 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);
 -          insertAddress(ops, address);
 +    /**
 +     * Creates a new contact and stores it in the database
 +     * 
 +     * @param contact the contact to be saved
 +     * @param account the account to be saved under
 +     */
 +    private String createNewContact(JSONObject contact, String accountType, String accountName) {
 +        // Create a list of attributes to add to the contact database
 +        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
 +
 +        //Add contact type
 +        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
 +                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
 +                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName)
 +                .build());
 +
 +        // Add name
 +        try {
 +            JSONObject name = contact.optJSONObject("name");
 +            String displayName = contact.getString("displayName");
 +            if (displayName != null || name != null) {
 +                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 +                        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 +                        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
 +                        .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName)
 +                        .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, getJsonString(name, "familyName"))
 +                        .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, getJsonString(name, "middleName"))
 +                        .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, getJsonString(name, "givenName"))
 +                        .withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, getJsonString(name, "honorificPrefix"))
 +                        .withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, getJsonString(name, "honorificSuffix"))
 +                        .build());
 +            }
 +        } catch (JSONException e) {
 +            Log.d(LOG_TAG, "Could not get name object");
          }
 -      }
 -

<TRUNCATED>
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/81195db8/framework/src/org/apache/cordova/ContactManager.java
----------------------------------------------------------------------