diff --git a/src/android/ContactAccessor.java b/src/android/ContactAccessor.java index 38ac5078..5e9f4749 100644 --- a/src/android/ContactAccessor.java +++ b/src/android/ContactAccessor.java @@ -147,7 +147,8 @@ protected String getJsonString(JSONObject obj, String property) { */ public abstract String save(JSONObject contact); - public abstract String save(JSONObject contact, String accountType, String accountName, boolean callerIsSyncAdapter); + public abstract String save(JSONObject contact, String accountType, + String accountName, boolean callerIsSyncAdapter, boolean resetFields); /** * Handles searching through SDK-specific contacts API. */ diff --git a/src/android/ContactAccessorSdk5.java b/src/android/ContactAccessorSdk5.java index a1c1b000..f0a914dc 100644 --- a/src/android/ContactAccessorSdk5.java +++ b/src/android/ContactAccessorSdk5.java @@ -1056,7 +1056,7 @@ private JSONObject photoQuery(Cursor cursor, String contactId) { * @returns the id if the contact is successfully saved, null otherwise. */ public String save(JSONObject contact) { - return save(contact, null, null, false); + return save(contact, null, null, false, false); } @Override @@ -1069,7 +1069,7 @@ public String save(JSONObject contact) { * @param callerIsSyncAdapter should set the flag during requests ? * @return the id if the contact is successfully saved, null otherwise. */ - public String save(JSONObject contact, String accountType, String accountName, boolean callerIsSyncAdapter) { + public String save(JSONObject contact, String accountType, String accountName, boolean callerIsSyncAdapter, boolean resetFields) { if (accountType == null || accountName == null) { AccountManager mgr = AccountManager.get(mApp.getActivity()); Account[] accounts = mgr.getAccounts(); @@ -1116,7 +1116,7 @@ else if (accounts.length > 1) { return createNewContact(contact, accountType, accountName, callerIsSyncAdapter); } else { // Modify existing contact - return modifyContact(id, contact, accountType, accountName, callerIsSyncAdapter); + return modifyContact(id, contact, accountType, accountName, callerIsSyncAdapter, resetFields); } } @@ -1127,7 +1127,7 @@ else if (accounts.length > 1) { * @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, boolean callerIsSyncAdapter) { + private String modifyContact(String id, JSONObject contact, String accountType, String accountName, boolean callerIsSyncAdapter, boolean resetFields) { // 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 = (Integer.valueOf(getJsonString(contact, "rawId"))).intValue(); @@ -1201,7 +1201,7 @@ private String modifyContact(String id, JSONObject contact, String accountType, phones = contact.getJSONArray("phoneNumbers"); if (phones != null) { // Delete all the phones - if (phones.length() == 0) { + if (phones.length() == 0 || resetFields) { ops.add(ContentProviderOperation.newDelete(contentUri) .withSelection(ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?", @@ -1209,31 +1209,29 @@ private String modifyContact(String id, JSONObject contact, String accountType, .build()); } // Modify or add a phone - else { - 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( - contentUri).withValues(contentValues).build()); - } - // This is an existing phone so do a DB update - else { - ops.add(ContentProviderOperation.newUpdate(contentUri) - .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()); - } + 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 || resetFields) { + 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( + contentUri).withValues(contentValues).build()); + } + // This is an existing phone so do a DB update + else { + ops.add(ContentProviderOperation.newUpdate(contentUri) + .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()); } } } @@ -1247,7 +1245,7 @@ private String modifyContact(String id, JSONObject contact, String accountType, emails = contact.getJSONArray("emails"); if (emails != null) { // Delete all the emails - if (emails.length() == 0) { + if (emails.length() == 0 || resetFields) { ops.add(ContentProviderOperation.newDelete(contentUri) .withSelection(ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?", @@ -1255,39 +1253,37 @@ private String modifyContact(String id, JSONObject contact, String accountType, .build()); } // Modify or add a email - else { - 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( - contentUri).withValues(contentValues).build()); - } - // This is an existing email so do a DB update - else { - String emailValue=getJsonString(email, "value"); - if(!emailValue.isEmpty()) { - ops.add(ContentProviderOperation.newUpdate(contentUri) + 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 || resetFields) { + 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( + contentUri).withValues(contentValues).build()); + } + // This is an existing email so do a DB update + else { + String emailValue=getJsonString(email, "value"); + if(!emailValue.isEmpty()) { + ops.add(ContentProviderOperation.newUpdate(contentUri) + .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()); + } else { + ops.add(ContentProviderOperation.newDelete(contentUri) .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()); - } else { - ops.add(ContentProviderOperation.newDelete(contentUri) - .withSelection(ContactsContract.CommonDataKinds.Email._ID + "=? AND " + - ContactsContract.Data.MIMETYPE + "=?", - new String[] { emailId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE }) - .build()); - } } } } @@ -1302,7 +1298,7 @@ private String modifyContact(String id, JSONObject contact, String accountType, addresses = contact.getJSONArray("addresses"); if (addresses != null) { // Delete all the addresses - if (addresses.length() == 0) { + if (addresses.length() == 0 || resetFields) { ops.add(ContentProviderOperation.newDelete(contentUri) .withSelection(ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?", @@ -1310,41 +1306,39 @@ private String modifyContact(String id, JSONObject contact, String accountType, .build()); } // Modify or add a address - else { - 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( - contentUri).withValues(contentValues).build()); - } - // This is an existing address so do a DB update - else { - ops.add(ContentProviderOperation.newUpdate(contentUri) - .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()); - } + 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 || resetFields) { + 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( + contentUri).withValues(contentValues).build()); + } + // This is an existing address so do a DB update + else { + ops.add(ContentProviderOperation.newUpdate(contentUri) + .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()); } } } @@ -1358,7 +1352,7 @@ private String modifyContact(String id, JSONObject contact, String accountType, organizations = contact.getJSONArray("organizations"); if (organizations != null) { // Delete all the organizations - if (organizations.length() == 0) { + if (organizations.length() == 0 || resetFields) { ops.add(ContentProviderOperation.newDelete(contentUri) .withSelection(ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?", @@ -1366,35 +1360,33 @@ private String modifyContact(String id, JSONObject contact, String accountType, .build()); } // Modify or add a organization - else { - 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( - contentUri).withValues(contentValues).build()); - } - // This is an existing organization so do a DB update - else { - ops.add(ContentProviderOperation.newUpdate(contentUri) - .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()); - } + 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 || resetFields) { + 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( + contentUri).withValues(contentValues).build()); + } + // This is an existing organization so do a DB update + else { + ops.add(ContentProviderOperation.newUpdate(contentUri) + .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()); } } } @@ -1408,7 +1400,7 @@ private String modifyContact(String id, JSONObject contact, String accountType, ims = contact.getJSONArray("ims"); if (ims != null) { // Delete all the ims - if (ims.length() == 0) { + if (ims.length() == 0 || resetFields) { ops.add(ContentProviderOperation.newDelete(contentUri) .withSelection(ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?", @@ -1416,31 +1408,29 @@ private String modifyContact(String id, JSONObject contact, String accountType, .build()); } // Modify or add a im - else { - 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, getImType(getJsonString(im, "type"))); - - ops.add(ContentProviderOperation.newInsert( - contentUri).withValues(contentValues).build()); - } - // This is an existing IM so do a DB update - else { - ops.add(ContentProviderOperation.newUpdate(contentUri) - .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()); - } + 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 || resetFields) { + 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, getImType(getJsonString(im, "type"))); + + ops.add(ContentProviderOperation.newInsert( + contentUri).withValues(contentValues).build()); + } + // This is an existing IM so do a DB update + else { + ops.add(ContentProviderOperation.newUpdate(contentUri) + .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()); } } } @@ -1474,7 +1464,7 @@ private String modifyContact(String id, JSONObject contact, String accountType, websites = contact.getJSONArray("urls"); if (websites != null) { // Delete all the websites - if (websites.length() == 0) { + if (websites.length() == 0 || resetFields) { Log.d(LOG_TAG, "This means we should be deleting all the phone numbers."); ops.add(ContentProviderOperation.newDelete(contentUri) .withSelection(ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + @@ -1483,31 +1473,29 @@ private String modifyContact(String id, JSONObject contact, String accountType, .build()); } // Modify or add a website - else { - 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( - contentUri).withValues(contentValues).build()); - } - // This is an existing website so do a DB update - else { - ops.add(ContentProviderOperation.newUpdate(contentUri) - .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()); - } + 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 || resetFields) { + 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( + contentUri).withValues(contentValues).build()); + } + // This is an existing website so do a DB update + else { + ops.add(ContentProviderOperation.newUpdate(contentUri) + .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()); } } } @@ -1534,7 +1522,7 @@ private String modifyContact(String id, JSONObject contact, String accountType, photos = contact.getJSONArray("photos"); if (photos != null) { // Delete all the photos - if (photos.length() == 0) { + if (photos.length() == 0 || resetFields) { ops.add(ContentProviderOperation.newDelete(contentUri) .withSelection(ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?", @@ -1542,32 +1530,30 @@ private String modifyContact(String id, JSONObject contact, String accountType, .build()); } // Modify or add a photo - else { - for (int i = 0; i < photos.length(); i++) { - JSONObject photo = (JSONObject) photos.get(i); - String photoId = getJsonString(photo, "id"); - byte[] bytes = getPhotoBytes(photo); - // 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( - contentUri).withValues(contentValues).build()); - } - // This is an existing photo so do a DB update - else { - ops.add(ContentProviderOperation.newUpdate(contentUri) - .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()); - } + for (int i = 0; i < photos.length(); i++) { + JSONObject photo = (JSONObject) photos.get(i); + String photoId = getJsonString(photo, "id"); + byte[] bytes = getPhotoBytes(photo); + // This is a new photo so do a DB insert + if (photoId == null || resetFields) { + 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( + contentUri).withValues(contentValues).build()); + } + // This is an existing photo so do a DB update + else { + ops.add(ContentProviderOperation.newUpdate(contentUri) + .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()); } } } diff --git a/src/android/ContactManager.java b/src/android/ContactManager.java index 01e29949..63d34124 100644 --- a/src/android/ContactManager.java +++ b/src/android/ContactManager.java @@ -107,11 +107,12 @@ else if (action.equals("save")) { final String accountType = args.optString(1, null); final String accountName = args.optString(2, null); final Boolean callerIsSyncAdapter = args.optBoolean(3, false); + final Boolean resetFields = args.optBoolean(4, false); this.cordova.getThreadPool().execute(new Runnable(){ public void run() { JSONObject res = null; String id = contactAccessor.save(contact, accountType, - accountName, callerIsSyncAdapter); + accountName, callerIsSyncAdapter, resetFields); Log.d(LOG_TAG, "Saved id: " + id); if (id != null) { try { diff --git a/www/Contact.js b/www/Contact.js index 952490b9..2d8ced23 100644 --- a/www/Contact.js +++ b/www/Contact.js @@ -189,7 +189,9 @@ Contact.prototype.save = function(successCB, errorCB, options) { var accountType = options.accountType; var accountName = options.accountName; var callerIsSyncAdapter = options.callerIsSyncAdapter == true; - exec(success, fail, "Contacts", "save", [dupContact, accountType, accountName, callerIsSyncAdapter]); + var resetFields = options.resetFields == true; + exec(success, fail, "Contacts", "save", + [dupContact, accountType, accountName, callerIsSyncAdapter, resetFields]); };