Mulgard
Mulgard

Reputation: 10569

Multiple SyncAdapter with different Names in Android Account Manager

I need to have multiple SyncAdapter. I now have exactly seven of them to synchronize differnt data between App and Server.

To start my adapters i do the following:

public class SyncUtils {
    public static final String ACCOUNT_NAME = "My Account";
    public static final String ACCOUNT_TYPE = "de.my.package";
    public static final long SECONDS_PER_MINUTE = 60L;
    public static final long SYNC_INTERVAL_IN_MINUTES = 2L;
    public static final long SYNC_INTERVAL = SYNC_INTERVAL_IN_MINUTES * SECONDS_PER_MINUTE;
    private static boolean newAccount = false;

    public static void createSyncAccount(Context context, String email) {
        boolean setupComplete = PreferencesSync.getSetupComplete(context, email);

        AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

        setUpSyncOne(accountManager);
        setUpSyncTwo(accountManager);
        setUpSyncThree(accountManager);
        setUpSyncFour(accountManager);
        setUpSyncFive(accountManager);
        setUpSyncSix(accountManager);
        setUpSyncSeven(accountManager);

        if (newAccount || !setupComplete) {
            syncRefreshImmediatly(ContentProviderOne.AUTHORITY);
            syncRefreshImmediatly(ContentProviderTwo.AUTHORITY);
            syncRefreshImmediatly(ContentProviderThree.AUTHORITY);
            syncRefreshImmediatly(ContentProviderFour.AUTHORITY);
            syncRefreshImmediatly(ContentProviderFive.AUTHORITY);
            syncRefreshImmediatly(ContentProviderSix.AUTHORITY);
            syncRefreshImmediatly(ContentProviderSeven.AUTHORITY);

            PreferencesSync.setSetupComplete(context, email, true);
        }
    }

    private static void setUpSyncOne(AccountManager accountManager) {
        Account account = getAccount();

        if (accountManager.addAccountExplicitly(account, null, null)) {
            ContentResolver.addPeriodicSync(account, ContentProviderOne.AUTHORITY, new Bundle(), SYNC_INTERVAL);

            newAccount = true;
        }
    }

    private static void setUpSyncTwo(AccountManager accountManager) {
        Account account = getAccount();

        if (accountManager.addAccountExplicitly(account, null, null)) {
            ContentResolver.addPeriodicSync(account, ContentProviderTwo.AUTHORITY, new Bundle(), SYNC_INTERVAL);

            newAccount = true;
        }
    }

    private static void setUpSyncThree(AccountManager accountManager) {
        Account account = getAccount();

        if (accountManager.addAccountExplicitly(account, null, null)) {
            ContentResolver.addPeriodicSync(account, ContentProviderThree.AUTHORITY, new Bundle(), SYNC_INTERVAL);

            newAccount = true;
        }
    }

    private static void setUpSyncFour(AccountManager accountManager) {
        Account account = getAccount();

        if (accountManager.addAccountExplicitly(account, null, null)) {
            ContentResolver.addPeriodicSync(account, ContentProviderFour.AUTHORITY, new Bundle(), SYNC_INTERVAL);

            newAccount = true;
        }
    }

    private static void setUpSyncFive(AccountManager accountManager) {
        Account account = getAccount();

        if (accountManager.addAccountExplicitly(account, null, null)) {
            ContentResolver.addPeriodicSync(account, ContentProviderFive.AUTHORITY, new Bundle(), SYNC_INTERVAL);

            newAccount = true;
        }
    }

    private static void setUpSyncSix(AccountManager accountManager) {
        Account account = getAccount();

        if (accountManager.addAccountExplicitly(account, null, null)) {
            ContentResolver.addPeriodicSync(account, ContentProviderSix.AUTHORITY, new Bundle(), SYNC_INTERVAL);

            newAccount = true;
        }
    }

    private static void setUpSyncSeven(AccountManager accountManager) {
        Account account = getAccount();

        if (accountManager.addAccountExplicitly(account, null, null)) {
            ContentResolver.addPeriodicSync(account, ContentProviderSeven.AUTHORITY, new Bundle(), SYNC_INTERVAL);

            newAccount = true;
        }
    }

    public static void syncRefresh(String contentAuthority, Bundle bundle) {
        ContentResolver.requestSync(getAccount(), contentAuthority, bundle);
    }

    public static void syncRefreshImmediatly(String contentAuthority) {
        Bundle bundle = new Bundle();

        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);

        syncRefresh(contentAuthority, bundle);
    }

    public static void syncRefreshGCM(String contentAuthority, String gcm, long addition) {
        Bundle bundle = new Bundle();

        bundle.putString(Globals.KEY_GCM, gcm);
        bundle.putLong(Globals.KEY_ADDITION, addition);
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);

        syncRefresh(contentAuthority, bundle);
    }

    public static Account getAccount() {
        return new Account(ACCOUNT_NAME, ACCOUNT_TYPE);
    }
}

When i now install my app the account manager of my android device shows me the account for my app together with all seven syncadapters. My problem now is, that there displayed names are all the same:

My App

My App
Touch to start synchronization
Synchronization: Disabled

My App
Touch to start synchronization
Synchronization: Disabled

My App
Touch to start synchronization
Synchronization: Disabled

My App
Touch to start synchronization
Synchronization: Disabled

My App
Touch to start synchronization
Synchronization: Disabled

My App
Touch to start synchronization
Synchronization: Disabled

My App
Touch to start synchronization
Synchronization: Disabled

But i actually want it to be:

My App 

SycAdapter One
Touch to start synchronization
Synchronization: Disabled

SycAdapter Two
Touch to start synchronization
Synchronization: Disabled

SycAdapter Thre
Touch to start synchronization
Synchronization: Disabled

SycAdapter Four
Touch to start synchronization
Synchronization: Disabled

SycAdapter Five
Touch to start synchronization
Synchronization: Disabled

SycAdapter Six
Touch to start synchronization
Synchronization: Disabled

SycAdapter Seven
Touch to start synchronization
Synchronization: Disabled

How can i do that?

EDIT

The SyncAdapters shown in the Account Manager are all correct! It means when i click on the first entry with the label My App my SyncAdapter One gest started. The second entry with the label My App starts my SyncAdapter Two and so on. So the Adapter are all there correctly they just have the same Label. So how can i change the labels?

Upvotes: 1

Views: 602

Answers (2)

Mulgard
Mulgard

Reputation: 10569

Got it! The answer was hidden:

    <provider
        android:name="de.hof.ime_dc.idia_move.provider.ContentProviderMeasure"
        android:authorities="de.hof.ime_dc.idia_move.measures.contentprovider"
        android:exported="false"
        android:label="@string/provider_measures"
        android:syncable="true" />

I had to set the label of my providers! I did not define them so android took my app name automatically!

Upvotes: 1

UMESH0492
UMESH0492

Reputation: 1731

For each SyncAdapter you need one AuthenticatorService with authenticator.xml and different account names and types

Upvotes: 1

Related Questions