Modify native contact programmatically - android

I'm trying to modify contact first name and last name programmatically.
The code snippet that I've used in order to do the job is the following one:
operations.add( ContentProviderOperation.newUpdate( Data.CONTENT_URI )
.withSelection( RawContacts._ID + "=?",
new String[] { String.valueOf( mSmartphoneContactKey) } )
.withValue( ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
mContactName.getEditableText().toString() )
.withValue( ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
mContactLastName.getEditableText().toString() )
.build() );
The mSmartphoneContactKey is filled in with the data contained in the column
ContactsContract.Contacts._ID
which is sitting in my projection array when I read contacts using content provider.
The problem is that for some contacts the name and last name are not modified and the phone type is modified instead.
Actually I don't have any clue about the cause.
Any advice is appreciated.
I've read further the documentation the Data table is the one I have to use.
I've modified the code as below...still not working
operations.add( ContentProviderOperation.newUpdate( Data.CONTENT_URI )
.withSelection( Data._ID + " = ? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'",
new String[] { String.valueOf( mSmartphoneContactId ) } )
.withValue( ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, mContactName.getEditableText().toString() )
.withValue( ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, mContactLastName.getEditableText().toString() )
.build() );
Please help me!

Ok Solved!
Wrong ID passed.
Need to retrieve the ID along the data from the DATA table.

Related

Android Not able to update contact details programatically in local phonebook

I am having issue where i need to have update Multiple Mobile no's , Landline no's and Emails , websites , address in local phonebook contact.
If contact is already exist in phone book then i am trying to update it's details.
Below is the code i am trying. It is getting executed without error but no multiple mobile no , landline , email etc not reflecting in phone-book on that contact as the multiple data's are their.
I also referred few of the links but that didn't helped as well.
String whereMobile = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = ? ";
String[] paramsMobile = new String[]{String.valueOf(contactID),
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)};
if (userInfoMobileNos != null && !userInfoMobileNos.isEmpty()) {
for (int iUserMobile = 0; iUserMobile < userInfoMobileNos.size(); iUserMobile++) {
operationList.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(whereMobile, paramsMobile)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, userInfoMobileNos.get(iUserMobile).getPhoneNumber())
.build());
}
}
String whereGmail = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ? AND " +
ContactsContract.CommonDataKinds.Email.TYPE + " = ? ";
String[] paramsGmail = new String[]{String.valueOf(contactID),
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_WORK)};
if (userInfoGmails != null && !userInfoGmails.isEmpty()) {
for (int iUserGmail = 0; iUserGmail < userInfoGmails.size(); iUserGmail++) {
operationList.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(whereGmail, paramsGmail)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, userInfoGmails.get(iUserGmail).getEmail())
.build());
}
}
Updated code
private void updateNew(Context context, int rawContactID) {
ArrayList<ContentProviderOperation> operationList = new ArrayList<>();
String whereMobile = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = ? ";
String[] paramsMobile = new String[]{String.valueOf(rawContactID),
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)};
// First delete all the existing phones with type mobile, if any exist
operationList.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
.withSelection(whereMobile, paramsMobile)
.build());
// Next, insert all the updated phones:
for (MobileNumbersItem infoMobileNo : userInfoMobileNos) { // <== change the class to the one you're using in userInfoMobileNos
operationList.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) // <== insert not update!
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, infoMobileNo.getPhoneNumber())
.build());
}
String whereGmail = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ? AND " +
ContactsContract.CommonDataKinds.Email.TYPE + " = ? ";
String[] paramsGmail = new String[]{String.valueOf(rawContactID),
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_WORK)};
// First delete all the existing emails with type work, if any exist
operationList.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
.withSelection(whereGmail, paramsGmail)
.build());
// Next, insert all the updated emails:
for (GmailsItem userInfoGmail : userInfoGmails) { // <== change the class here too
operationList.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, userInfoGmail.getEmail())
.build());
}
try {
// don't forget to apply the operations now:
ContentProviderResult[] results = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList);
Log.d("UPDATE CONTACRT", "results=" + Arrays.toString(results));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("updateContactError", Objects.requireNonNull(e.getMessage()));
}
}
Any help will be appreciated here. I have tried to debug but can't able to find the issue.
one issue that can be either a bad parameter name, or an actual bug is that your selection is requesting a RAW_CONTACT_ID and supplying a parameter called contactId, I'm not sure what is stored in contactId but there's a big difference between a rawContactId and a contactId, so if that's indeed a contactId your operations will not find anything to update (or even worse, update the wrong contact).
The second issue is that your two loops (over userInfoMobileNos and userInfoGmails) keep updating (overriding) the same Data row, so eventually you should end up with just the last value in the contact details.
You're also not checking if there's a Data row in the contact details that fit your selection, if there's not, there will be no row to update, and therefore the code will simply do nothing.
Third issue, but it may be intentional, not sure how you expect your app to work, is that you assume the info item type, i.e. you only update a phone if it's TYPE_MOBILE, and you only update the email if it's TYPE_WORK, what if there are other phones and emails stored for that contact? your code will simply keep those untouched, not sure if that intentional or not.
To fix issue one, just make sure the value in contactId is a RawContactId and change the name of the param.
For the second issue, you should instead do a delete-and-insert, see code below.
For the third issue, you'll need to change the selection code (paramsMobile) to remove it's assumption of label.
Here's suggested code:
String whereMobile = Data.RAW_CONTACT_ID + " = ? AND " +
Data.MIMETYPE + " = ? AND " +
Phone.TYPE + " = ? ";
String[] paramsMobile = new String[]{String.valueOf(rawContactID),
Phone.CONTENT_ITEM_TYPE,
String.valueOf(Phone.TYPE_MOBILE)};
// First delete all the existing phones with type mobile, if any exist
operationList.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
.withSelection(whereMobile, paramsMobile)
.build());
// Next, insert all the updated phones:
for (UserInfo infoMobileNo : userInfoMobileNos) { // <== change the class to the one you're using in userInfoMobileNos
operationList.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) // <== insert not update!
operationList.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) // <== insert not update!
.withValue(Data.RAW_CONTACT_ID, rawContactID)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.TYPE, Phone.TYPE_MOBILE)
.withValue(Phone.NUMBER, infoMobileNo.getPhoneNumber())
.build());
}
String whereGmail = Data.RAW_CONTACT_ID + " = ? AND " +
Data.MIMETYPE + " = ? AND " +
Email.TYPE + " = ? ";
String[] paramsGmail = new String[]{String.valueOf(rawContactID),
Email.CONTENT_ITEM_TYPE,
String.valueOf(Email.TYPE_WORK)};
// First delete all the existing emails with type work, if any exist
operationList.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
.withSelection(whereGmail, paramsGmail)
.build());
// Next, insert all the updated emails:
for (UserInfo userInfoGmail : userInfoGmails) { // <== change the class here too
operationList.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValue(Data.RAW_CONTACT_ID, rawContactID)
.withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
.withValue(Email.TYPE, Email.TYPE_WORK)
.withValue(Email.DATA, userInfoGmail.getEmail())
.build());
}
// don't forget to apply the operations now:
ContentProviderResult[] results = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList);
Log.d("UPDATE CONTACRT", "results=" + Arrays.toString(results));

How do I update a email address from specific Email._id?

I have this problem .
I have a listview that is created depending on how many emails address have a contact .
Now I wish I could change this email address in a specific way .
If for example one of my contact has four email address ( one#gmail.com,
two#gmail.com,three#gmail.com,four#gmail.com )and modify one of these emails address for the second example ( two#yahoo.com ) how can I change this only leaving the other ?
For every email address I have a bean that includes : Email._id , Email.Type , Email.Data .
Thank you all for the help .
P.S.
Sorry for my English ;)
The problem is not in the listview , but in a specific point of contact email address .
This is the code that I used when I was a single email address to edit.
String emailData = "example#email.com";
String contactId="123456";
int typeEmail=1;
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE
+ "=?",
new String[] { contactId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE })
.withValue(ContactsContract.CommonDataKinds.Email.DATA, emailData)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, typeEmail).build());
I solved it :
I simply changed the column to ContactsContract.Data.CONTACT_ID to ContactsContract.Data._ID
for (int i = 0; i < listEmail.size(); i++) {
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.Data._ID + "=? AND "
+ COLUMN_MIMETYPE + "=?",
new String[] { listEmail.get(i).getId(),
MIMETYPE_STRING_EMAIL })
.withValue(COLUMN_EMAIL, listEmail.get(i).getName())
.withValue(COLUMN_EMAIL_TYPE,
listEmail.get(i).getType()).build());
}

Cannot update contact with new fields

I have a problem when try to update existing contact with field which is not exist.
Example:
I successfully create new contact which contain name and email address fields. During create action I use ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) only for fields which are not empty.
Code for inserting phone field during contact creation (works fine):
if (isNewContact){
if (!phone.equals("")){
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, id)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, type)
.build());
}
}
So, now I try to run update process and append to my contact the phone number:
if (isUpdateContact){
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data.RAW_CONTACT_ID + "=? AND "
+ ContactsContract.CommonDataKinds.Phone.TYPE
+ "=? AND " + ContactsContract.Data.MIMETYPE
+ "=?",
new String[] {"" + id, "" + type,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE })
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, type)
.build());
}
In case if during contact creation the phone field was inserted, then update works fine. In case if phone was not inserted, then update does nothing - the field will not be updated. No warnings neither errors in LogCat.
The reason why I do not want to create empty fields on contact creation is because of empty fields crashes Phone application in Samsung Nexus device when I switch to contacts tab with null pointer exception in com.android.contacts.list.ContactListItemView.onMeasure(ContactListItemView.java:350).
So, my question is:
How during update process to insert a new field if not exist (and how to detect it) OR delete the field if exist but new update value is empty? Is it possible at all or there is another solution to avoid crash of Nexus phone app if fields are empty?
P.S. tried to run newInsert during for already created contact, getting:
mType: 1, mUri: content://com.android.contacts/data, mSelection: null, mExpectedCount: null, mYieldAllowed: false, mValues: data1= mimetype=vnd.android.cursor.item/phone_v2 data2=1, mValuesBackReferences: raw_contact_id=2962, mSelectionArgsBackReferences: null
java.lang.ArrayIndexOutOfBoundsException: asked for back ref 2962 but there are only 1 back refs
at android.content.ContentProviderOperation.backRefToValue(ContentProviderOperation.java:362)
Ok, I found the fix:
(this will do right update operation - in case if not found, create; in case if new value empty, just remove the field; in case if exists, do update.)
if (isUpdateContact){
ops.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data.RAW_CONTACT_ID + "=? AND "
+ ContactsContract.CommonDataKinds.Phone.TYPE
+ "=? AND " + ContactsContract.Data.MIMETYPE
+ "=?",
new String[] {"" + id, "" + type,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE })
.build());
if (!phone.equals("")){
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, id)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, type)
.build());
}
}

Can't add a HOME email has been specified yet to an existing ANDROID contacs

I have a proplem when I make a app "contacts manager" for android.
I have processed creating and deleting a contact with name, and phone
but when I want to add more a home email info to an existing contacts but can't do that.
here my code:
ArrayList ops = new ArrayList();
String selectName = ContactsContract.Data.CONTACT_ID + "= ? AND " + ContactsContract.Data.MIMETYPE + "= ? ";
String[] NameArgs = new String[]{strId, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
String selectPhone = ContactsContract.Data.CONTACT_ID + "= ? AND " + ContactsContract.Data.MIMETYPE + "= ? ";
String[] phoneArgs = new String[]{strId, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};
String selectEmail = ContactsContract.Data.CONTACT_ID + "= ? AND " + ContactsContract.Data.MIMETYPE + "= ? ";
String[] emailArgs = new String[]{strId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
String selectPostAdd = ContactsContract.Data.CONTACT_ID + "= ? AND " + ContactsContract.Data.MIMETYPE + "= ? AND " + ContactsContract.CommonDataKinds.StructuredPostal.TYPE + "= ? ";
String[] postAddArgs = new String[]{strId, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, String.valueOf(ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)};
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectName, NameArgs)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, updContact_name.getText().toString())
.build());
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, updContact_phone.getText().toString())
.build());
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectEmail, emailArgs)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, updContact_textMail.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPostAdd, postAddArgs)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.DATA, updContact_textPostAdd.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
this code is available for update a contacts but an Item Info(here is email, StructruedPostal) has been created once. but when I create it with contacts app of android with only name, number (don't touch email, structuedPostal) and save, I don't update it in my android app.
somebody have experience about this pls help me! tks
sorry about my grammar english! It have more mistake and wrong grammar. :(. Tks all
Updating will not insert field which doesn't exist, so follow these steps.
First query for the raw Contact Id of your Contact.
use new Insert method to insert data that has not been inserted to the contact previously when created (in the oppt case newUpdate() will work fine).
A piece of code goes like this
String[] params = new String[] {"Give your Contact"};// you need query and get this too.
String[] proj = {ContactsContract.RawContacts._ID};
Cursor c = context.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,proj,
ContactsContract.Data._ID +"= ?", params, null);
long rawContactId = -1;
while(c.moveToNext()){
rawContactId = c.getLong(0);
}
c.close();
After getting those values,
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, data.getEmail2Address())
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME)
.build());

How to update Android contact company?

I recently created a Sync adapter for my app, It will sync contacts I am getting via a web request with the contacts in the phone. I have no problem adding the contact, however I cannot get the contact information to correctly update when contact information has changed. For example the Company Name field on the contact. Here is some example queries I have tried that did not work or only partially worked(ie - some contacts updated but not correctly):
ContentValues values = new ContentValues();
values.put(ContactsContract.CommonDataKinds.Organization.COMPANY, "New Company");
context.getContentResolver().update(Uri.parse("content://com.android.contacts/data/"), values, BaseColumns._ID + "=?", new String[] { String.valueOf(id) } );
I have also tried doing this in batch as suggested by the android documentation:
builder = ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(BaseColumns._ID + " =?", new String[]{String.valueOf(id)});
builder.withValue(
ContactsContract.CommonDataKinds.Organization.COMPANY,
"New Company Name!");
operationList.add(builder.build());
I have read the ContactContracts Documentation and originally was following this tutorial. I also checked into the AuthenticatorActivity example in the api's to no avail. Any help is greatly appreciated.
After spending an exhausting amount of time trying to figure out the correct query, I believe I have found the answer. It looks like i needed to change the BaseColumns._ID to ContactsContract.Data.CONTACT_ID and for each update I made, I also had to supply the mime-type also I did not see this anywhere in the android documentation. Much help was found on this write-up: Working With Android Contacts
String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] orgWhereParams = new String[]{String.valueOf(id),
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
operationList
.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(orgWhere, orgWhereParams)
.withValue(
ContactsContract.CommonDataKinds.Organization.DATA,
guCon.getCompany()).build());

Categories

Resources