FileDocCategorySizeDatePackage
AbstractThreadedSyncAdapter.javaAPI DocAndroid 5.1 API14783Thu Mar 12 22:22:10 GMT 2015android.content

AbstractThreadedSyncAdapter

public abstract class AbstractThreadedSyncAdapter extends Object
An abstract implementation of a SyncAdapter that spawns a thread to invoke a sync operation. If a sync operation is already in progress when a startSync() request is received then an error will be returned to the new request and the existing request will be allowed to continue. When a startSync() is received and there is no sync operation in progress then a thread will be started to run the operation and {@link #onPerformSync} will be invoked on that thread. If a cancelSync() is received that matches an existing sync operation then the thread that is running that sync operation will be interrupted, which will indicate to the thread that the sync has been canceled.

In order to be a sync adapter one must extend this class, provide implementations for the abstract methods and write a service that returns the result of {@link #getSyncAdapterBinder()} in the service's {@link android.app.Service#onBind(android.content.Intent)} when invoked with an intent with action android.content.SyncAdapter. This service must specify the following intent filter and metadata tags in its AndroidManifest.xml file

<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter" />
The android:resource attribute must point to a resource that looks like:
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:contentAuthority="authority"
android:accountType="accountType"
android:userVisible="true|false"
android:supportsUploading="true|false"
android:allowParallelSyncs="true|false"
android:isAlwaysSyncable="true|false"
android:syncAdapterSettingsAction="ACTION_OF_SETTINGS_ACTIVITY"
/>
  • The android:contentAuthority and android:accountType attributes indicate which content authority and for which account types this sync adapter serves.
  • android:userVisible defaults to true and controls whether or not this sync adapter shows up in the Sync Settings screen.
  • android:supportsUploading defaults to true and if true an upload-only sync will be requested for all syncadapters associated with an authority whenever that authority's content provider does a {@link ContentResolver#notifyChange(android.net.Uri, android.database.ContentObserver, boolean)} with syncToNetwork set to true.
  • android:allowParallelSyncs defaults to false and if true indicates that the sync adapter can handle syncs for multiple accounts at the same time. Otherwise the SyncManager will wait until the sync adapter is not in use before requesting that it sync an account's data.
  • android:isAlwaysSyncable defaults to false and if true tells the SyncManager to intialize the isSyncable state to 1 for that sync adapter for each account that is added.
  • android:syncAdapterSettingsAction defaults to null and if supplied it specifies an Intent action of an activity that can be used to adjust the sync adapter's sync settings. The activity must live in the same package as the sync adapter.

Fields Summary
public static final int
LOG_SYNC_DETAILS
Kernel event log tag. Also listed in data/etc/event-log-tags.
private final Context
mContext
private final AtomicInteger
mNumSyncStarts
private final ISyncAdapterImpl
mISyncAdapterImpl
private final HashMap
mSyncThreads
private final Object
mSyncThreadLock
private final boolean
mAutoInitialize
private boolean
mAllowParallelSyncs
Constructors Summary
public AbstractThreadedSyncAdapter(Context context, boolean autoInitialize)
Creates an {@link AbstractThreadedSyncAdapter}.

param
context the {@link android.content.Context} that this is running within.
param
autoInitialize if true then sync requests that have {@link ContentResolver#SYNC_EXTRAS_INITIALIZE} set will be internally handled by {@link AbstractThreadedSyncAdapter} by calling {@link ContentResolver#setIsSyncable(android.accounts.Account, String, int)} with 1 if it is currently set to <0.


                                                         
         
        this(context, autoInitialize, false /* allowParallelSyncs */);
    
public AbstractThreadedSyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs)
Creates an {@link AbstractThreadedSyncAdapter}.

param
context the {@link android.content.Context} that this is running within.
param
autoInitialize if true then sync requests that have {@link ContentResolver#SYNC_EXTRAS_INITIALIZE} set will be internally handled by {@link AbstractThreadedSyncAdapter} by calling {@link ContentResolver#setIsSyncable(android.accounts.Account, String, int)} with 1 if it is currently set to <0.
param
allowParallelSyncs if true then allow syncs for different accounts to run at the same time, each in their own thread. This must be consistent with the setting in the SyncAdapter's configuration file.

        mContext = context;
        mISyncAdapterImpl = new ISyncAdapterImpl();
        mNumSyncStarts = new AtomicInteger(0);
        mAutoInitialize = autoInitialize;
        mAllowParallelSyncs = allowParallelSyncs;
    
Methods Summary
public ContextgetContext()

        return mContext;
    
public final android.os.IBindergetSyncAdapterBinder()

return
a reference to the IBinder of the SyncAdapter service.

        return mISyncAdapterImpl.asBinder();
    
public abstract voidonPerformSync(android.accounts.Account account, android.os.Bundle extras, java.lang.String authority, ContentProviderClient provider, SyncResult syncResult)
Perform a sync for this account. SyncAdapter-specific parameters may be specified in extras, which is guaranteed to not be null. Invocations of this method are guaranteed to be serialized.

param
account the account that should be synced
param
extras SyncAdapter-specific parameters
param
authority the authority of this sync request
param
provider a ContentProviderClient that points to the ContentProvider for this authority
param
syncResult SyncAdapter-specific parameters

public voidonSyncCanceled()
Indicates that a sync operation has been canceled. This will be invoked on a separate thread than the sync thread and so you must consider the multi-threaded implications of the work that you do in this method.

This will only be invoked when the SyncAdapter indicates that it doesn't support parallel syncs.

        final SyncThread syncThread;
        synchronized (mSyncThreadLock) {
            syncThread = mSyncThreads.get(null);
        }
        if (syncThread != null) {
            syncThread.interrupt();
        }
    
public voidonSyncCanceled(java.lang.Thread thread)
Indicates that a sync operation has been canceled. This will be invoked on a separate thread than the sync thread and so you must consider the multi-threaded implications of the work that you do in this method.

This will only be invoked when the SyncAdapter indicates that it does support parallel syncs.

param
thread the Thread of the sync that is to be canceled.

        thread.interrupt();
    
private android.accounts.AccounttoSyncKey(android.accounts.Account account)

        if (mAllowParallelSyncs) {
            return account;
        } else {
            return null;
        }