1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chrome.browser.sync;
7 import android.accounts.Account;
8 import android.content.ContentResolver;
9 import android.content.Context;
10 import android.content.SharedPreferences;
11 import android.os.AsyncTask;
12 import android.os.Bundle;
13 import android.preference.PreferenceManager;
14 import android.util.Log;
16 import org.chromium.base.ApplicationStatus;
17 import org.chromium.base.VisibleForTesting;
18 import org.chromium.sync.notifier.SyncStatusHelper;
19 import org.chromium.sync.signin.AccountManagerHelper;
22 * A class for controlling when a sync should be performed immediately, and when it should be
23 * delayed until Chrome comes to the foreground again.
25 public class DelayedSyncController {
26 private static final String TAG = "DelayedSyncController";
27 private static final String DELAYED_ACCOUNT_NAME = "delayed_account";
29 private static class LazyHolder {
30 private static final DelayedSyncController INSTANCE = new DelayedSyncController();
33 public static DelayedSyncController getInstance() {
34 return LazyHolder.INSTANCE;
38 DelayedSyncController() {}
41 * Resume any syncs that were delayed while Chromium was backgrounded.
43 public boolean resumeDelayedSyncs(final Context context) {
44 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
45 String accountName = prefs.getString(DELAYED_ACCOUNT_NAME, null);
46 if (accountName == null) {
47 Log.d(TAG, "No delayed sync.");
50 Log.d(TAG, "Handling delayed sync.");
51 Account account = AccountManagerHelper.createAccountFromName(accountName);
52 requestSyncOnBackgroundThread(context, account);
58 * Calls ContentResolver.requestSync() in a separate thread as it performs some blocking
62 void requestSyncOnBackgroundThread(final Context context, final Account account) {
63 new AsyncTask<Void, Void, Void>() {
65 protected Void doInBackground(Void... unused) {
66 String contractAuthority =
67 SyncStatusHelper.get(context).getContractAuthority();
68 ContentResolver.requestSync(account, contractAuthority, new Bundle());
75 * Stores preferences to indicate that an invalidation has arrived, but dropped on the floor.
77 void setDelayedSync(Context ctx, String accountName) {
78 SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
79 editor.putString(DELAYED_ACCOUNT_NAME, accountName);
84 * If there is a delayed sync, it will be cleared.
87 void clearDelayedSyncs(Context context) {
88 setDelayedSync(context, null);
92 boolean shouldPerformSync(Context ctx, Bundle extras, Account account) {
93 boolean manualSync = isManualSync(extras);
95 if (manualSync || ApplicationStatus.hasVisibleActivities()) {
96 clearDelayedSyncs(ctx);
99 Log.d(TAG, "Delaying sync.");
100 setDelayedSync(ctx, account.name);
105 private static boolean isManualSync(Bundle extras) {
106 boolean manualSync = false;
107 if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false)) {
109 Log.d(TAG, "Manual sync requested.");