Background
Suppose I have a contact on the device (or I'm about to create one), which I wish to save into Google account, so that it will be synced with it. I also wish to be able to let the user choose which account to save to.
This is what a Samsung device shows upon creating a new contact:
If the device also had a Samsung account, it will also be shown in this dialog.
The problem
I've found out how to insert a new one (here), but as I remember, modifying an existing contact is only allowed in specific cases (maybe only if the contact is in the device/sim itself, and not of other accounts).
I also can't find a way to get the list of accounts to save to (device, sim, google,...)
What I've found
I've ran this code on a Samsung device that has contacts on sim, storage and Google accounts:
final Map<String, String> result = new HashMap<>();
final Cursor cursor = getContentResolver()
.query(ContactsContract.RawContacts.CONTENT_URI, new String[]{RawContacts.ACCOUNT_TYPE, RawContacts.ACCOUNT_NAME},
null, null,
null);
final int accountNameIdx = cursor.getColumnIndex(RawContacts.ACCOUNT_NAME);
final int accountTypeIdx = cursor.getColumnIndex(RawContacts.ACCOUNT_TYPE);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
result.put(cursor.getString(accountTypeIdx), cursor.getString(accountNameIdx));
}
cursor.close();
Log.d("AppLog", "accounts found:");
for (Entry<String, String> account : result.entrySet())
Log.d("AppLog", account.getKey() + ":" + account.getValue());
And the result was :
vnd.sec.contact.sim:primary.sim.account_name
com.google:somegmailaddress@gmail.com
vnd.sec.contact.phone:vnd.sec.contact.phone
But, as I've found (here), the sim/device accounts might be different on various devices.
The questions
What's the correct way to modify an existing contact, so that its ACCOUNT_TYPE will be of Google's ?
Does using the first item of the next code get the main Google account of the user, so that I should consider using it:
AccountManager.get(context).getAccountsByType("com.google")
?
Is there a way to check that the account that was found has auto-synced enabled, and if not , use an intent to go to the screen to make it sync? Is it possible to trigger a sync once the contact modification was done, in case auto-sync isn't enabled?
Is there a way to get all available accounts that can be set for a contact (including sim/local), so that I could present the user with a dialog to choose in which account to save the contact? Will "getAccounts" suffice, or should I filter the results somehow?
EDIT: for #4, it seems "getAccounts" doesn't provide me sim&local as the possible accounts to save into. Still, I want to know how to get them, so that I could offer the user to choose them.