diff options
Diffstat (limited to 'core/java/android/content/AbstractThreadedSyncAdapter.java')
| -rw-r--r-- | core/java/android/content/AbstractThreadedSyncAdapter.java | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/core/java/android/content/AbstractThreadedSyncAdapter.java b/core/java/android/content/AbstractThreadedSyncAdapter.java index 2629929e91ce..b528e397906f 100644 --- a/core/java/android/content/AbstractThreadedSyncAdapter.java +++ b/core/java/android/content/AbstractThreadedSyncAdapter.java @@ -16,11 +16,17 @@ package android.content; +import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; + import android.accounts.Account; +import android.annotation.MainThread; +import android.annotation.NonNull; import android.os.Build; import android.os.Bundle; +import android.os.Handler; import android.os.IBinder; import android.os.Process; +import android.os.RemoteException; import android.os.Trace; import android.util.Log; @@ -166,6 +172,13 @@ public abstract class AbstractThreadedSyncAdapter { private class ISyncAdapterImpl extends ISyncAdapter.Stub { @Override + public void onUnsyncableAccount(ISyncAdapterUnsyncableAccountCallback cb) { + Handler.getMain().sendMessage(obtainMessage( + AbstractThreadedSyncAdapter::handleOnUnsyncableAccount, + AbstractThreadedSyncAdapter.this, cb)); + } + + @Override public void startSync(ISyncContext syncContext, String authority, Account account, Bundle extras) { if (ENABLE_LOG) { @@ -374,6 +387,54 @@ public abstract class AbstractThreadedSyncAdapter { } /** + * Handle a call of onUnsyncableAccount. + * + * @param cb The callback to report the return value to + */ + private void handleOnUnsyncableAccount(@NonNull ISyncAdapterUnsyncableAccountCallback cb) { + boolean doSync; + try { + doSync = onUnsyncableAccount(); + } catch (RuntimeException e) { + Log.e(TAG, "Exception while calling onUnsyncableAccount, assuming 'true'", e); + doSync = true; + } + + try { + cb.onUnsyncableAccountDone(doSync); + } catch (RemoteException e) { + Log.e(TAG, "Could not report result of onUnsyncableAccount", e); + } + } + + /** + * Allows to defer syncing until all accounts are properly set up. + * + * <p>Called when a account / authority pair + * <ul> + * <li>that can be handled by this adapter</li> + * <li>{@link ContentResolver#requestSync(SyncRequest) is synced}</li> + * <li>and the account/provider {@link ContentResolver#getIsSyncable(Account, String) has + * unknown state (<0)}.</li> + * </ul> + * + * <p>This might be called on a different service connection as {@link #onPerformSync}. + * + * <p>The system expects this method to immediately return. If the call stalls the system + * behaves as if this method returned {@code true}. If it is required to perform a longer task + * (such as interacting with the user), return {@code false} and proceed in a difference + * context, such as an {@link android.app.Activity}, or foreground service. The sync can then be + * rescheduled once the account becomes syncable. + * + * @return If {@code false} syncing is deferred. Returns {@code true} by default, i.e. by + * default syncing starts immediately. + */ + @MainThread + public boolean onUnsyncableAccount() { + return true; + } + + /** * 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. |
