4

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:

enter image description here

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

  1. What's the correct way to modify an existing contact, so that its ACCOUNT_TYPE will be of Google's ?

  2. 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")
    

    ?

  3. 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?

  4. 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.

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270

1 Answers1

1

So Many questions here. I might be able to answer a few. Let's see

1) You cannot modify the Account_Type of an existing user. This is explained here This clearly states that Account Type can only be changed once. this is most likely to be used so that you can add an account type to each row for a Contact.

2) The code that you provided will return to you a list of all the Accounts of type com.google. So if a user has logged into their phone with 3 google accounts you get a list of all 3. Now an ideal user experience in this case is to show the user a list of all these accounts for them to choose and you perform your operations after the user selection on that particular account. For more code Check here

3) To check if sync is enabled use the flag SHOULD_SYNC from ContactContracts.Settings .Rather than send an intent I reckon you just set this value, and the data source would have implemented the relevant content observers to do the job for you.

4) I am not sure if AccountManager holds detailsof SIM contacts at all. Atleast the documentation does not mention this. you can debug using a rooted device and extracting the sqlite database of the same, but in case you only want to fetch the details I think this stackoverflow question has certain approaches worth trying and look like they might work on the face of it

Hopefully this answers your questions.

Community
  • 1
  • 1
ichthyocentaurs
  • 2,173
  • 21
  • 35
  • 1. I meant changing it for the contact that's in the address book. how do I do it (in code), and how can I know that it can still be changed? Also, I don't see any explanation in the link about what you wrote. 2. will the first one in the list always be of the main user account? 3. Can you please show how (code) ? 4. so how can contacts apps show a list of where to store contacts (including sim)? – android developer May 15 '16 at 07:25
  • About 4, it can't get the accounts of SIM&local, but still, I need to get the list of accounts that I can save to, including SIM&local. the link you provided is for query of existing contacts, so it won't work in case they don't exist there yet. – android developer May 15 '16 at 11:26