Search code examples
androidandroid-contactsandroid-syncadapter

How to make new contacts exported from an app to Phonebook as EDITABLE


I have an application which has it's own sets of contacts which is coming from a server. I am exporting this contact to the native contacts database. So that i can access it from the phonebook. However, i want to make these contacts as editable in native app.

I am using account authenticator and syncadapter approach for this.

Below is my sync-adapter xml

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="com.android.contacts"
    android:supportsUploading="true"
    android:userVisible="true"
    android:accountType="com.example.myapp"
/>

Setting android:supportsUploading="true"

Also, exporting raw contacts and data fields as non-read only by setting below flag as 0 android.provider.ContactsContract.RawContacts.RAW_CONTACT_IS_READ_ONLY android.provider.ContactsContract.RawContacts.DATA_IS_READ_ONLY

Still i am not seeing option to edit my contacts in native app. What's missing here?

Thanks in advance

[UPDATE] I am using below code to check which account type is supported by contacts and is editable.

final SyncAdapterType[] syncs = ContentResolver.getSyncAdapterTypes();
for (SyncAdapterType sync : syncs) {
    Log.d(TAG, "found SyncAdapter: " + sync.accountType);
    if (ContactsContract.AUTHORITY.equals(sync.authority)) {
        Log.d(TAG, "found SyncAdapter that supports contacts: " + sync.accountType);
        if (sync.supportsUploading()) {
            Log.d(TAG, "found SyncAdapter that supports contacts and is not read-only: " + sync.accountType);
            // we'll now get a list of all accounts under that accountType:
            Account[] accounts = AccountManager.get(this).getAccountsByType(sync.accountType);
            for (Account account : accounts) {
               Log.d(TAG, account.type + " / " + account.name);
            }
        }
    }
}

My apps account type is non-read only but still don't see an edit option. Please help


Solution

  • A couple of things you need to do:

    1. Implement a SyncService and declare it on AndroidManifest
    2. create a contacts.xml file that will that the contact editor how to parse and edit contacts related to your account

    See: https://github.com/xwiki-contrib/android-authenticator/blob/master/app/src/main/AndroidManifest.xml#L81-L94 https://github.com/xwiki-contrib/android-authenticator/blob/master/app/src/main/res/xml/syncadapter.xml https://github.com/xwiki-contrib/android-authenticator/blob/master/app/src/main/res/xml/contacts.xml

    as examples.