This source file includes following definitions.
- get
- openSigninDialog
- openSignOutDialog
- signIn
- onStart
- setupSessionSyncId
- refreshSyncState
- start
- stop
- syncStateChanged
- syncSettingsChanged
package org.chromium.chrome.shell.sync;
import android.accounts.Account;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;
import android.util.Log;
import org.chromium.base.ThreadUtils;
import org.chromium.chrome.browser.identity.UniqueIdentificationGeneratorFactory;
import org.chromium.chrome.browser.identity.UuidBasedUniqueIdentificationGenerator;
import org.chromium.chrome.browser.invalidation.InvalidationController;
import org.chromium.chrome.browser.signin.SigninManager;
import org.chromium.chrome.browser.sync.ProfileSyncService;
import org.chromium.sync.notifier.SyncStatusHelper;
import org.chromium.sync.signin.AccountManagerHelper;
import org.chromium.sync.signin.ChromeSigninController;
public class SyncController implements ProfileSyncService.SyncStateChangedListener,
SyncStatusHelper.SyncSettingsChangedObserver {
private static final String TAG = "SyncController";
private static final String SESSIONS_UUID_PREF_KEY = "chromium.sync.sessions.id";
private static SyncController sInstance;
private final Context mContext;
private final ChromeSigninController mChromeSigninController;
private final SyncStatusHelper mSyncStatusHelper;
private final ProfileSyncService mProfileSyncService;
private SyncController(Context context) {
mContext = context;
mChromeSigninController = ChromeSigninController.get(mContext);
mSyncStatusHelper = SyncStatusHelper.get(context);
mSyncStatusHelper.registerSyncSettingsChangedObserver(this);
mProfileSyncService = ProfileSyncService.get(mContext);
mProfileSyncService.addSyncStateChangedListener(this);
setupSessionSyncId();
mChromeSigninController.ensureGcmIsInitialized();
}
public static SyncController get(Context context) {
ThreadUtils.assertOnUiThread();
if (sInstance == null) {
sInstance = new SyncController(context.getApplicationContext());
}
return sInstance;
}
public static void openSigninDialog(FragmentManager fragmentManager) {
AccountChooserFragment chooserFragment = new AccountChooserFragment();
chooserFragment.show(fragmentManager, null);
}
public static void openSignOutDialog(FragmentManager fragmentManager) {
SignoutFragment signoutFragment = new SignoutFragment();
signoutFragment.show(fragmentManager, null);
}
public void signIn(Activity activity, String accountName) {
final Account account = AccountManagerHelper.createAccountFromName(accountName);
SigninManager signinManager = SigninManager.get(mContext);
signinManager.onFirstRunCheckDone();
final boolean passive = false;
signinManager.startSignIn(activity, account, passive, new SigninManager.Observer() {
@Override
public void onSigninComplete() {
SigninManager.get(mContext).logInSignedInUser();
mProfileSyncService.setSetupInProgress(false);
mProfileSyncService.syncSignIn();
start();
}
@Override
public void onSigninCancelled() {
stop();
}
});
}
public void onStart() {
refreshSyncState();
}
private void setupSessionSyncId() {
UuidBasedUniqueIdentificationGenerator generator =
new UuidBasedUniqueIdentificationGenerator(mContext, SESSIONS_UUID_PREF_KEY);
UniqueIdentificationGeneratorFactory.registerGenerator(
UuidBasedUniqueIdentificationGenerator.GENERATOR_ID, generator, false);
mProfileSyncService.setSessionsId(UniqueIdentificationGeneratorFactory
.getInstance(UuidBasedUniqueIdentificationGenerator.GENERATOR_ID));
}
private void refreshSyncState() {
if (mSyncStatusHelper.isSyncEnabled())
start();
else
stop();
}
private void start() {
ThreadUtils.assertOnUiThread();
if (mSyncStatusHelper.isMasterSyncAutomaticallyEnabled()) {
Log.d(TAG, "Enabling sync");
Account account = mChromeSigninController.getSignedInUser();
InvalidationController.get(mContext).start();
mProfileSyncService.enableSync();
mSyncStatusHelper.enableAndroidSync(account);
}
}
private void stop() {
ThreadUtils.assertOnUiThread();
if (mChromeSigninController.isSignedIn()) {
Log.d(TAG, "Disabling sync");
Account account = mChromeSigninController.getSignedInUser();
InvalidationController.get(mContext).stop();
mProfileSyncService.disableSync();
mSyncStatusHelper.disableAndroidSync(account);
}
}
@Override
public void syncStateChanged() {
ThreadUtils.assertOnUiThread();
Account account = mChromeSigninController.getSignedInUser();
boolean isSyncSuppressStart = mProfileSyncService.isStartSuppressed();
boolean isSyncEnabled = mSyncStatusHelper.isSyncEnabled(account);
if (account != null && isSyncSuppressStart && isSyncEnabled)
stop();
}
@Override
public void syncSettingsChanged() {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
refreshSyncState();
}
});
}
}