public abstract class BaseSyncManager extends Object implements OnSyncTaskStateChangeListener
Account that should be picked by a specific BaseSyncManager implementation
whenever pickAccountForSync() is called.
To request synchronization for a specific SyncTask call requestSync(SyncTask)
method. If a global synchronization should be performed, call requestGlobalSync().
Base synchronization manager class implements also some configuration methods like to start/stop
automatic synchronization via startAutomaticSync() and stopAutomaticSync() or
to check whether there are some synchronization operations pending or active via isSyncPedning()
and isSyncPedning(). Any additional methods may be freely implemented by the inheritance
hierarchies.
| Modifier and Type | Field and Description |
|---|---|
protected String |
mAuthority
Content authority with which has been this manager created.
|
protected Context |
mContext
Context with which has been this manager created.
|
| Constructor and Description |
|---|
BaseSyncManager(Context context,
String authority)
Creates a new instance of BaseSyncManager with the given context and authority.
|
| Modifier and Type | Method and Description |
|---|---|
void |
cancelSync()
Cancels any active or pending synchronizations that match the content authority specified for
this manager and account picked for synchronization by this manager implementation.
|
boolean |
isSyncActive()
Checks whether there is currently a synchronization being processed for the content authority
specified for this manager and account picked for synchronization by this manager implementation.
|
boolean |
isSyncPedning()
Checks whether there are any pending synchronizations for the content authority specified for
this manager and account picked for synchronization by this manager implementation.
|
protected void |
onCancelSync(Account account)
Invoked whenever
cancelSync() is called. |
void |
onSyncTaskStateChanged(SyncTask syncTask,
Account account)
Invoked whenever a state of the specified syncTask has been changed.
|
protected abstract Account |
pickAccountForSync()
Called to pick account for the current synchronization related request dispatched to this
manager.
|
void |
requestGlobalSync()
Same as
requestSync(SyncTask) with sync task with SyncTask.DEFAULT_ID id. |
void |
requestSync(SyncTask syncTask)
Requests synchronization operation to be performed for the specified syncTask.
|
protected boolean |
shouldRequestSync(SyncTask syncTask,
Account account)
Called from
requestSync(SyncTask) to check whether a synchronization should be requested
for the specified syncTask and account. |
void |
startAutomaticSync()
Starts automatic synchronization for the content authority specified for this manager and
account picked for synchronization by this manager implementation.
|
void |
stopAutomaticSync()
Stops automatic synchronization for the content authority specified for this manager and
account picked for synchronization by this manager implementation.
|
protected final Context mContext
protected final String mAuthority
ContentResolver.public BaseSyncManager(@NonNull Context context, @NonNull String authority)
context - Context that may be used by inheritance hierarchies to access application
data and services needed to perform requested synchronization.authority - The content authority for which the manager should request synchronization
via ContentResolver.requestSync(Account, String, Bundle).public void startAutomaticSync()
public void stopAutomaticSync()
public void requestGlobalSync()
requestSync(SyncTask) with sync task with SyncTask.DEFAULT_ID id.public void requestSync(@NonNull SyncTask syncTask)
If there is successfully picked synchronization account, a Bundle for sync adapter
is created with the following extras:
SYNC_EXTRAS_MANUAL: trueSYNC_EXTRAS_EXPEDITED: trueSyncTask put into the extras BundlesyncTask - The desired task for which to request synchronization. This task will be put
into Bundle along with other sync extras for the sync adapter registered
for this Android application.requestGlobalSync(),
ContentResolver.requestSync(Account, String, Bundle)protected boolean shouldRequestSync(@NonNull SyncTask syncTask, @NonNull Account account)
requestSync(SyncTask) to check whether a synchronization should be requested
for the specified syncTask and account.
This implementation returns always true.
syncTask - The sync task for which to check if synchronization should be requested.account - The account for which to request synchronization.True if requestSync(SyncTask) should proceed and request synchronization
via ContentResolver.requestSync(Account, String, Bundle) for the task and account,
false otherwise.pickAccountForSync()@CallSuper public void onSyncTaskStateChanged(@NonNull SyncTask syncTask, @NonNull Account account)
OnSyncTaskStateChangeListeneronSyncTaskStateChanged in interface OnSyncTaskStateChangeListenersyncTask - The task of which state has changed.account - The account for which has been requested synchronization associated with the
sync task.SyncTask.getState()public boolean isSyncActive()
True if synchronization is active at this time, false otherwise.ContentResolver.isSyncActive(Account, String),
isSyncPedning(),
cancelSync()public boolean isSyncPedning()
True if there are some synchronizations pending, false otherwise.ContentResolver.isSyncPending(Account, String),
isSyncActive(),
cancelSync()public void cancelSync()
protected void onCancelSync(@NonNull Account account)
cancelSync() is called.
Default implementation cancels current sync via ContentResolver.cancelSync(Account, String)
for the specified account and authority specified for this manager.
account - The account picked via pickAccountForSync().@Nullable protected abstract Account pickAccountForSync()
startAutomaticSync(),
stopAutomaticSync(), requestGlobalSync(), requestSync(SyncTask),
isSyncActive(), isSyncPedning(), cancelSync().