Commit 80344ba4 authored by maxbogue's avatar maxbogue Committed by Commit bot

[Sync] Make it impossible to get a reference to AndroidSyncSettings.

This change is motivated by some complex test flakiness issues that were
discovered in http://crrev.com/1118833002. Making it impossible to store
a reference means that if we overwrite it for tests, we know everyone is
then using the overwritten version.

The approach here is to make every public method static and take the
context as an argument, so it can initialize the inner object if
necessary.

This CL is part 1/3 and leaves in deprecated versions of all the methods.
In part 2/3 the downstream uses of AndroidSyncSettings will be changed,
and in part 3/3 the deprecated methods will be removed upstream.

BUG=480604

Review URL: https://codereview.chromium.org/1138013008

Cr-Commit-Position: refs/heads/master@{#330570}
parent 08f8097c
...@@ -630,7 +630,7 @@ public class ChromeBrowserProvider extends ContentProvider { ...@@ -630,7 +630,7 @@ public class ChromeBrowserProvider extends ContentProvider {
// Don't allow going up the hierarchy if sync is disabled and the requested node // Don't allow going up the hierarchy if sync is disabled and the requested node
// is the Mobile Bookmarks folder. // is the Mobile Bookmarks folder.
if (getParent && nodeId == getMobileBookmarksFolderId() if (getParent && nodeId == getMobileBookmarksFolderId()
&& !AndroidSyncSettings.get(getContext()).isSyncEnabled()) { && !AndroidSyncSettings.isSyncEnabled(getContext())) {
getParent = false; getParent = false;
} }
......
...@@ -104,7 +104,7 @@ public class InvalidationController implements ApplicationStatus.ApplicationStat ...@@ -104,7 +104,7 @@ public class InvalidationController implements ApplicationStatus.ApplicationStat
@Override @Override
public void onApplicationStateChange(int newState) { public void onApplicationStateChange(int newState) {
if (AndroidSyncSettings.get(mContext).isSyncEnabled()) { if (AndroidSyncSettings.isSyncEnabled(mContext)) {
if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) { if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) {
start(); start();
} else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) { } else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) {
......
...@@ -393,7 +393,7 @@ public class AccountManagementFragment extends PreferenceFragment ...@@ -393,7 +393,7 @@ public class AccountManagementFragment extends PreferenceFragment
ProfileAccountManagementMetrics.CLICK_PRIMARY_ACCOUNT, ProfileAccountManagementMetrics.CLICK_PRIMARY_ACCOUNT,
mGaiaServiceType); mGaiaServiceType);
if (AndroidSyncSettings.get(activity).isMasterSyncEnabled()) { if (AndroidSyncSettings.isMasterSyncEnabled(activity)) {
AccountManagementFragmentDelegate delegate = getDelegate(); AccountManagementFragmentDelegate delegate = getDelegate();
// The delegate is set as part of deferred startup, so it might be null. // The delegate is set as part of deferred startup, so it might be null.
if (delegate == null) return false; if (delegate == null) return false;
...@@ -480,7 +480,6 @@ public class AccountManagementFragment extends PreferenceFragment ...@@ -480,7 +480,6 @@ public class AccountManagementFragment extends PreferenceFragment
private static String getSyncStatusSummary(Activity activity) { private static String getSyncStatusSummary(Activity activity) {
if (!ChromeSigninController.get(activity).isSignedIn()) return ""; if (!ChromeSigninController.get(activity).isSignedIn()) return "";
AndroidSyncSettings androidSyncSettings = AndroidSyncSettings.get(activity);
ProfileSyncService profileSyncService = ProfileSyncService.get(activity); ProfileSyncService profileSyncService = ProfileSyncService.get(activity);
Resources res = activity.getResources(); Resources res = activity.getResources();
...@@ -488,7 +487,7 @@ public class AccountManagementFragment extends PreferenceFragment ...@@ -488,7 +487,7 @@ public class AccountManagementFragment extends PreferenceFragment
return res.getString(R.string.kids_account); return res.getString(R.string.kids_account);
} }
if (!androidSyncSettings.isMasterSyncEnabled()) { if (!AndroidSyncSettings.isMasterSyncEnabled(activity)) {
return res.getString(R.string.sync_android_master_sync_disabled); return res.getString(R.string.sync_android_master_sync_disabled);
} }
...@@ -496,7 +495,7 @@ public class AccountManagementFragment extends PreferenceFragment ...@@ -496,7 +495,7 @@ public class AccountManagementFragment extends PreferenceFragment
return res.getString(profileSyncService.getAuthError().getMessage()); return res.getString(profileSyncService.getAuthError().getMessage());
} }
if (androidSyncSettings.isSyncEnabled()) { if (AndroidSyncSettings.isSyncEnabled(activity)) {
if (!profileSyncService.isSyncInitialized()) { if (!profileSyncService.isSyncInitialized()) {
return res.getString(R.string.sync_setup_progress); return res.getString(R.string.sync_setup_progress);
} }
...@@ -506,7 +505,7 @@ public class AccountManagementFragment extends PreferenceFragment ...@@ -506,7 +505,7 @@ public class AccountManagementFragment extends PreferenceFragment
} }
} }
return androidSyncSettings.isSyncEnabled() return AndroidSyncSettings.isSyncEnabled(activity)
? res.getString(R.string.sync_is_enabled) ? res.getString(R.string.sync_is_enabled)
: res.getString(R.string.sync_is_disabled); : res.getString(R.string.sync_is_disabled);
} }
......
...@@ -414,7 +414,7 @@ public class SigninManager { ...@@ -414,7 +414,7 @@ public class SigninManager {
// Sign-in to sync. // Sign-in to sync.
ProfileSyncService profileSyncService = ProfileSyncService.get(mContext); ProfileSyncService profileSyncService = ProfileSyncService.get(mContext);
if (AndroidSyncSettings.get(mContext).isSyncEnabled() if (AndroidSyncSettings.isSyncEnabled(mContext)
&& !profileSyncService.hasSyncSetupCompleted()) { && !profileSyncService.hasSyncSetupCompleted()) {
profileSyncService.setSetupInProgress(true); profileSyncService.setSetupInProgress(true);
profileSyncService.syncSignIn(); profileSyncService.syncSignIn();
......
...@@ -63,8 +63,7 @@ public class DelayedSyncController { ...@@ -63,8 +63,7 @@ public class DelayedSyncController {
new AsyncTask<Void, Void, Void>() { new AsyncTask<Void, Void, Void>() {
@Override @Override
protected Void doInBackground(Void... unused) { protected Void doInBackground(Void... unused) {
String contractAuthority = String contractAuthority = AndroidSyncSettings.getContractAuthority(context);
AndroidSyncSettings.get(context).getContractAuthority();
ContentResolver.requestSync(account, contractAuthority, new Bundle()); ContentResolver.requestSync(account, contractAuthority, new Bundle());
return null; return null;
} }
......
...@@ -63,7 +63,6 @@ public class SyncController implements ApplicationStateListener, ...@@ -63,7 +63,6 @@ public class SyncController implements ApplicationStateListener,
private final Context mContext; private final Context mContext;
private final ChromeSigninController mChromeSigninController; private final ChromeSigninController mChromeSigninController;
private final AndroidSyncSettings mAndroidSyncSettings;
private final ProfileSyncService mProfileSyncService; private final ProfileSyncService mProfileSyncService;
private final SyncNotificationController mSyncNotificationController; private final SyncNotificationController mSyncNotificationController;
...@@ -77,8 +76,7 @@ public class SyncController implements ApplicationStateListener, ...@@ -77,8 +76,7 @@ public class SyncController implements ApplicationStateListener,
private SyncController(Context context) { private SyncController(Context context) {
mContext = context; mContext = context;
mChromeSigninController = ChromeSigninController.get(mContext); mChromeSigninController = ChromeSigninController.get(mContext);
mAndroidSyncSettings = AndroidSyncSettings.get(context); AndroidSyncSettings.registerObserver(context, this);
mAndroidSyncSettings.registerObserver(this);
mProfileSyncService = ProfileSyncService.get(mContext); mProfileSyncService = ProfileSyncService.get(mContext);
mProfileSyncService.addSyncStateChangedListener(this); mProfileSyncService.addSyncStateChangedListener(this);
...@@ -149,7 +147,7 @@ public class SyncController implements ApplicationStateListener, ...@@ -149,7 +147,7 @@ public class SyncController implements ApplicationStateListener,
* Updates sync to reflect the state of the Android sync settings. * Updates sync to reflect the state of the Android sync settings.
*/ */
public void updateSyncStateFromAndroid() { public void updateSyncStateFromAndroid() {
if (mAndroidSyncSettings.isSyncEnabled()) { if (AndroidSyncSettings.isSyncEnabled(mContext)) {
start(); start();
} else { } else {
stop(); stop();
...@@ -163,11 +161,11 @@ public class SyncController implements ApplicationStateListener, ...@@ -163,11 +161,11 @@ public class SyncController implements ApplicationStateListener,
*/ */
public void start() { public void start() {
ThreadUtils.assertOnUiThread(); ThreadUtils.assertOnUiThread();
if (mAndroidSyncSettings.isMasterSyncEnabled()) { if (AndroidSyncSettings.isMasterSyncEnabled(mContext)) {
Log.d(TAG, "Enabling sync"); Log.d(TAG, "Enabling sync");
InvalidationController.get(mContext).start(); InvalidationController.get(mContext).start();
mProfileSyncService.enableSync(); mProfileSyncService.enableSync();
mAndroidSyncSettings.enableChromeSync(); AndroidSyncSettings.enableChromeSync(mContext);
} }
} }
...@@ -182,11 +180,11 @@ public class SyncController implements ApplicationStateListener, ...@@ -182,11 +180,11 @@ public class SyncController implements ApplicationStateListener,
Log.d(TAG, "Disabling sync"); Log.d(TAG, "Disabling sync");
InvalidationController.get(mContext).stop(); InvalidationController.get(mContext).stop();
mProfileSyncService.disableSync(); mProfileSyncService.disableSync();
if (mAndroidSyncSettings.isMasterSyncEnabled()) { if (AndroidSyncSettings.isMasterSyncEnabled(mContext)) {
// Only disable Android's Chrome sync setting if we weren't disabled // Only disable Android's Chrome sync setting if we weren't disabled
// by the master sync setting. This way, when master sync is enabled // by the master sync setting. This way, when master sync is enabled
// they will both be on and sync will start again. // they will both be on and sync will start again.
mAndroidSyncSettings.disableChromeSync(); AndroidSyncSettings.disableChromeSync(mContext);
} }
} }
} }
...@@ -204,12 +202,12 @@ public class SyncController implements ApplicationStateListener, ...@@ -204,12 +202,12 @@ public class SyncController implements ApplicationStateListener,
// Make the Java state match the native state. // Make the Java state match the native state.
if (isSyncActive) { if (isSyncActive) {
InvalidationController.get(mContext).start(); InvalidationController.get(mContext).start();
mAndroidSyncSettings.enableChromeSync(); AndroidSyncSettings.enableChromeSync(mContext);
} else { } else {
InvalidationController.get(mContext).stop(); InvalidationController.get(mContext).stop();
if (mAndroidSyncSettings.isMasterSyncEnabled()) { if (AndroidSyncSettings.isMasterSyncEnabled(mContext)) {
// See comment in stop(). // See comment in stop().
mAndroidSyncSettings.disableChromeSync(); AndroidSyncSettings.disableChromeSync(mContext);
} }
} }
} }
...@@ -263,7 +261,7 @@ public class SyncController implements ApplicationStateListener, ...@@ -263,7 +261,7 @@ public class SyncController implements ApplicationStateListener,
} }
private void onFirstStart() { private void onFirstStart() {
if (mAndroidSyncSettings.isSyncEnabled()) { if (AndroidSyncSettings.isSyncEnabled(mContext)) {
InvalidationController controller = InvalidationController.get(mContext); InvalidationController controller = InvalidationController.get(mContext);
controller.refreshRegisteredTypes(mProfileSyncService.getPreferredDataTypes()); controller.refreshRegisteredTypes(mProfileSyncService.getPreferredDataTypes());
} }
......
...@@ -27,7 +27,6 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC ...@@ -27,7 +27,6 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC
private static final String TAG = "SyncNotificationController"; private static final String TAG = "SyncNotificationController";
private final Context mApplicationContext; private final Context mApplicationContext;
private final GoogleServicesNotificationController mNotificationController; private final GoogleServicesNotificationController mNotificationController;
private final AndroidSyncSettings mAndroidSyncSettings;
private final Class<? extends Activity> mPassphraseRequestActivity; private final Class<? extends Activity> mPassphraseRequestActivity;
private final Class<? extends Fragment> mAccountManagementFragment; private final Class<? extends Fragment> mAccountManagementFragment;
private final ProfileSyncService mProfileSyncService; private final ProfileSyncService mProfileSyncService;
...@@ -38,7 +37,6 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC ...@@ -38,7 +37,6 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC
mApplicationContext = context.getApplicationContext(); mApplicationContext = context.getApplicationContext();
mNotificationController = GoogleServicesNotificationController.get(context); mNotificationController = GoogleServicesNotificationController.get(context);
mProfileSyncService = ProfileSyncService.get(context); mProfileSyncService = ProfileSyncService.get(context);
mAndroidSyncSettings = AndroidSyncSettings.get(context);
mPassphraseRequestActivity = passphraseRequestActivity; mPassphraseRequestActivity = passphraseRequestActivity;
mAccountManagementFragment = accountManagementFragment; mAccountManagementFragment = accountManagementFragment;
} }
...@@ -62,7 +60,7 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC ...@@ -62,7 +60,7 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC
Intent intent; Intent intent;
// Auth errors take precedence over passphrase errors. // Auth errors take precedence over passphrase errors.
if (!mAndroidSyncSettings.isSyncEnabled()) { if (!AndroidSyncSettings.isSyncEnabled(mApplicationContext)) {
mNotificationController.cancelNotification(NotificationConstants.NOTIFICATION_ID_SYNC); mNotificationController.cancelNotification(NotificationConstants.NOTIFICATION_ID_SYNC);
return; return;
} }
......
...@@ -89,7 +89,6 @@ public class SyncCustomizationFragment extends PreferenceFragment implements ...@@ -89,7 +89,6 @@ public class SyncCustomizationFragment extends PreferenceFragment implements
private static final int ERROR_COLOR = Color.RED; private static final int ERROR_COLOR = Color.RED;
private ChromeSwitchPreference mSyncSwitchPreference; private ChromeSwitchPreference mSyncSwitchPreference;
private AndroidSyncSettings mAndroidSyncSettings;
private boolean mIsSyncInitialized; private boolean mIsSyncInitialized;
@VisibleForTesting @VisibleForTesting
...@@ -120,7 +119,6 @@ public class SyncCustomizationFragment extends PreferenceFragment implements ...@@ -120,7 +119,6 @@ public class SyncCustomizationFragment extends PreferenceFragment implements
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
mAndroidSyncSettings = AndroidSyncSettings.get(getActivity());
mProfileSyncService = ProfileSyncService.get(getActivity()); mProfileSyncService = ProfileSyncService.get(getActivity());
mIsSyncInitialized = mProfileSyncService.isSyncInitialized(); mIsSyncInitialized = mProfileSyncService.isSyncInitialized();
...@@ -242,7 +240,8 @@ public class SyncCustomizationFragment extends PreferenceFragment implements ...@@ -242,7 +240,8 @@ public class SyncCustomizationFragment extends PreferenceFragment implements
if (!getActivity().isChangingConfigurations()) { if (!getActivity().isChangingConfigurations()) {
// Only save state if the switch and external state match. If a stop and clear comes // Only save state if the switch and external state match. If a stop and clear comes
// while the dialog is open, this will be false and settings won't be saved. // while the dialog is open, this will be false and settings won't be saved.
if (mSyncSwitchPreference.isChecked() && mAndroidSyncSettings.isSyncEnabled()) { if (mSyncSwitchPreference.isChecked()
&& AndroidSyncSettings.isSyncEnabled(getActivity())) {
// Save the new data type state. // Save the new data type state.
configureSyncDataTypes(); configureSyncDataTypes();
// Inform sync that the user has finished setting up sync at least once. // Inform sync that the user has finished setting up sync at least once.
...@@ -260,7 +259,7 @@ public class SyncCustomizationFragment extends PreferenceFragment implements ...@@ -260,7 +259,7 @@ public class SyncCustomizationFragment extends PreferenceFragment implements
* updateSyncStateFromSwitch, which uses that as its source of truth. * updateSyncStateFromSwitch, which uses that as its source of truth.
*/ */
private void updateSyncState() { private void updateSyncState() {
boolean isSyncEnabled = mAndroidSyncSettings.isSyncEnabled(); boolean isSyncEnabled = AndroidSyncSettings.isSyncEnabled(getActivity());
mSyncSwitchPreference.setChecked(isSyncEnabled); mSyncSwitchPreference.setChecked(isSyncEnabled);
mSyncSwitchPreference.setEnabled(canDisableSync()); mSyncSwitchPreference.setEnabled(canDisableSync());
updateSyncStateFromSwitch(); updateSyncStateFromSwitch();
......
...@@ -117,11 +117,10 @@ public class InvalidationControllerTest extends InstrumentationTestCase { ...@@ -117,11 +117,10 @@ public class InvalidationControllerTest extends InstrumentationTestCase {
Account account = AccountManagerHelper.createAccountFromName("test@gmail.com"); Account account = AccountManagerHelper.createAccountFromName("test@gmail.com");
ChromeSigninController chromeSigninController = ChromeSigninController.get(mContext); ChromeSigninController chromeSigninController = ChromeSigninController.get(mContext);
chromeSigninController.setSignedInAccountName(account.name); chromeSigninController.setSignedInAccountName(account.name);
AndroidSyncSettings androidSyncSettings = AndroidSyncSettings.get(mContext);
if (syncEnabled) { if (syncEnabled) {
androidSyncSettings.enableChromeSync(); AndroidSyncSettings.enableChromeSync(mContext);
} else { } else {
androidSyncSettings.disableChromeSync(); AndroidSyncSettings.disableChromeSync(mContext);
} }
} }
......
...@@ -72,7 +72,7 @@ public class ChromiumSyncAdapterTest extends ChromeShellTestBase { ...@@ -72,7 +72,7 @@ public class ChromiumSyncAdapterTest extends ChromeShellTestBase {
public void performSyncWithBundle(Bundle bundle) { public void performSyncWithBundle(Bundle bundle) {
mSyncAdapter.onPerformSync(TEST_ACCOUNT, bundle, mSyncAdapter.onPerformSync(TEST_ACCOUNT, bundle,
AndroidSyncSettings.get(getActivity()).getContractAuthority(), AndroidSyncSettings.getContractAuthority(getActivity()),
null, new SyncResult()); null, new SyncResult());
} }
......
...@@ -54,12 +54,10 @@ public class SyncCustomizationFragmentTest extends SyncTestBase { ...@@ -54,12 +54,10 @@ public class SyncCustomizationFragmentTest extends SyncTestBase {
} }
private Activity mActivity; private Activity mActivity;
private AndroidSyncSettings mAndroidSyncSettings;
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
mAndroidSyncSettings = AndroidSyncSettings.get(mContext);
mActivity = getActivity(); mActivity = getActivity();
} }
...@@ -72,13 +70,13 @@ public class SyncCustomizationFragmentTest extends SyncTestBase { ...@@ -72,13 +70,13 @@ public class SyncCustomizationFragmentTest extends SyncTestBase {
final SwitchPreference syncSwitch = getSyncSwitch(fragment); final SwitchPreference syncSwitch = getSyncSwitch(fragment);
assertTrue(syncSwitch.isChecked()); assertTrue(syncSwitch.isChecked());
assertTrue(mAndroidSyncSettings.isChromeSyncEnabled()); assertTrue(AndroidSyncSettings.isChromeSyncEnabled(mContext));
togglePreference(syncSwitch); togglePreference(syncSwitch);
assertFalse(syncSwitch.isChecked()); assertFalse(syncSwitch.isChecked());
assertFalse(mAndroidSyncSettings.isChromeSyncEnabled()); assertFalse(AndroidSyncSettings.isChromeSyncEnabled(mContext));
togglePreference(syncSwitch); togglePreference(syncSwitch);
assertTrue(syncSwitch.isChecked()); assertTrue(syncSwitch.isChecked());
assertTrue(mAndroidSyncSettings.isChromeSyncEnabled()); assertTrue(AndroidSyncSettings.isChromeSyncEnabled(mContext));
} }
/** /**
...@@ -91,7 +89,7 @@ public class SyncCustomizationFragmentTest extends SyncTestBase { ...@@ -91,7 +89,7 @@ public class SyncCustomizationFragmentTest extends SyncTestBase {
stopSync(); stopSync();
SyncCustomizationFragment fragment = startSyncCustomizationFragment(); SyncCustomizationFragment fragment = startSyncCustomizationFragment();
closeFragment(fragment); closeFragment(fragment);
assertFalse(mAndroidSyncSettings.isChromeSyncEnabled()); assertFalse(AndroidSyncSettings.isChromeSyncEnabled(mContext));
} }
@SmallTest @SmallTest
...@@ -139,7 +137,7 @@ public class SyncCustomizationFragmentTest extends SyncTestBase { ...@@ -139,7 +137,7 @@ public class SyncCustomizationFragmentTest extends SyncTestBase {
} }
getInstrumentation().waitForIdleSync(); getInstrumentation().waitForIdleSync();
assertDefaultSyncOffState(fragment); assertDefaultSyncOffState(fragment);
assertFalse(mAndroidSyncSettings.isChromeSyncEnabled()); assertFalse(AndroidSyncSettings.isChromeSyncEnabled(mContext));
} }
/** /**
......
...@@ -152,7 +152,7 @@ public class SyncTest extends SyncTestBase { ...@@ -152,7 +152,7 @@ public class SyncTest extends SyncTestBase {
Account account = Account account =
AccountManagerHelper.createAccountFromName(SyncTestUtil.DEFAULT_TEST_ACCOUNT); AccountManagerHelper.createAccountFromName(SyncTestUtil.DEFAULT_TEST_ACCOUNT);
String authority = AndroidSyncSettings.get(mContext).getContractAuthority(); String authority = AndroidSyncSettings.getContractAuthority(mContext);
// Disabling Android sync should turn Chrome sync engine off. // Disabling Android sync should turn Chrome sync engine off.
mSyncContentResolver.setSyncAutomatically(account, authority, false); mSyncContentResolver.setSyncAutomatically(account, authority, false);
......
...@@ -60,7 +60,7 @@ public class SyncTestBase extends ChromeShellTestBase { ...@@ -60,7 +60,7 @@ public class SyncTestBase extends ChromeShellTestBase {
public void run() { public void run() {
mSyncController = SyncController.get(mContext); mSyncController = SyncController.get(mContext);
// Ensure SyncController is registered with the new AndroidSyncSettings. // Ensure SyncController is registered with the new AndroidSyncSettings.
AndroidSyncSettings.get(mContext).registerObserver(mSyncController); AndroidSyncSettings.registerObserver(mContext, mSyncController);
mFakeServerHelper = FakeServerHelper.get(); mFakeServerHelper = FakeServerHelper.get();
} }
}); });
......
...@@ -444,7 +444,7 @@ public class InvalidationClientService extends AndroidListener { ...@@ -444,7 +444,7 @@ public class InvalidationClientService extends AndroidListener {
bundle.putString("payload", (payload == null) ? "" : payload); bundle.putString("payload", (payload == null) ? "" : payload);
} }
Account account = ChromeSigninController.get(this).getSignedInUser(); Account account = ChromeSigninController.get(this).getSignedInUser();
String contractAuthority = AndroidSyncSettings.get(this).getContractAuthority(); String contractAuthority = AndroidSyncSettings.getContractAuthority(this);
requestSyncFromContentResolver(bundle, account, contractAuthority); requestSyncFromContentResolver(bundle, account, contractAuthority);
} }
...@@ -472,7 +472,7 @@ public class InvalidationClientService extends AndroidListener { ...@@ -472,7 +472,7 @@ public class InvalidationClientService extends AndroidListener {
/** Returns whether sync is enabled. LLocal method so it can be overridden in tests. */ /** Returns whether sync is enabled. LLocal method so it can be overridden in tests. */
@VisibleForTesting @VisibleForTesting
boolean isSyncEnabled() { boolean isSyncEnabled() {
return AndroidSyncSettings.get(getApplicationContext()).isSyncEnabled(); return AndroidSyncSettings.isSyncEnabled(getApplicationContext());
} }
/** /**
......
...@@ -21,7 +21,9 @@ import javax.annotation.concurrent.ThreadSafe; ...@@ -21,7 +21,9 @@ import javax.annotation.concurrent.ThreadSafe;
* *
* It also provides an observer to be used whenever Android sync settings change. * It also provides an observer to be used whenever Android sync settings change.
* *
* To retrieve an instance of this class, call AndroidSyncSettings.get(someContext). * This class is a collection of static methods so that no references to its object can be
* stored. This is important because tests need to be able to overwrite the object with a
* mock content resolver and know that no references to the old one are cached.
* *
* This class must be initialized via updateAccount() on startup if the user is signed in. * This class must be initialized via updateAccount() on startup if the user is signed in.
*/ */
...@@ -35,7 +37,7 @@ public class AndroidSyncSettings { ...@@ -35,7 +37,7 @@ public class AndroidSyncSettings {
*/ */
private static final Object CLASS_LOCK = new Object(); private static final Object CLASS_LOCK = new Object();
private static AndroidSyncSettings sAndroidSyncSettings; private static AndroidSyncSettings sInstance;
private final Object mLock = new Object(); private final Object mLock = new Object();
...@@ -63,37 +65,26 @@ public class AndroidSyncSettings { ...@@ -63,37 +65,26 @@ public class AndroidSyncSettings {
public void androidSyncSettingsChanged(); public void androidSyncSettingsChanged();
} }
/** private static void ensureInitialized(Context context) {
* A factory method for the AndroidSyncSettings.
*
* It is possible to override the {@link SyncContentResolverDelegate} to use in tests for the
* instance of the AndroidSyncSettings by calling overrideForTests(...) with
* your {@link SyncContentResolverDelegate}.
*
* @param context the ApplicationContext is retrieved from the context used as an argument.
* @return a singleton instance of the AndroidSyncSettings
*/
public static AndroidSyncSettings get(Context context) {
synchronized (CLASS_LOCK) { synchronized (CLASS_LOCK) {
if (sAndroidSyncSettings == null) { if (sInstance == null) {
SyncContentResolverDelegate contentResolver = SyncContentResolverDelegate contentResolver =
new SystemSyncContentResolverDelegate(); new SystemSyncContentResolverDelegate();
sAndroidSyncSettings = new AndroidSyncSettings(context, contentResolver); sInstance = new AndroidSyncSettings(context, contentResolver);
} }
} }
return sAndroidSyncSettings;
} }
@VisibleForTesting @VisibleForTesting
public static void overrideForTests(Context context, public static void overrideForTests(Context context,
SyncContentResolverDelegate contentResolver) { SyncContentResolverDelegate contentResolver) {
synchronized (CLASS_LOCK) { synchronized (CLASS_LOCK) {
sAndroidSyncSettings = new AndroidSyncSettings(context, contentResolver); sInstance = new AndroidSyncSettings(context, contentResolver);
} }
} }
/** /**
* @param context the context * @param context the context the ApplicationContext will be retrieved from.
* @param syncContentResolverDelegate an implementation of {@link SyncContentResolverDelegate}. * @param syncContentResolverDelegate an implementation of {@link SyncContentResolverDelegate}.
*/ */
private AndroidSyncSettings(Context context, private AndroidSyncSettings(Context context,
...@@ -117,8 +108,9 @@ public class AndroidSyncSettings { ...@@ -117,8 +108,9 @@ public class AndroidSyncSettings {
* *
* @return true if sync is on, false otherwise * @return true if sync is on, false otherwise
*/ */
public boolean isSyncEnabled() { public static boolean isSyncEnabled(Context context) {
return mMasterSyncEnabled && mChromeSyncEnabled; ensureInitialized(context);
return sInstance.mMasterSyncEnabled && sInstance.mChromeSyncEnabled;
} }
/** /**
...@@ -129,66 +121,74 @@ public class AndroidSyncSettings { ...@@ -129,66 +121,74 @@ public class AndroidSyncSettings {
* *
* @return true if sync is on, false otherwise * @return true if sync is on, false otherwise
*/ */
public boolean isChromeSyncEnabled() { public static boolean isChromeSyncEnabled(Context context) {
return mChromeSyncEnabled; ensureInitialized(context);
return sInstance.mChromeSyncEnabled;
} }
/** /**
* Checks whether the master sync flag for Android is currently enabled. * Checks whether the master sync flag for Android is currently enabled.
*/ */
public boolean isMasterSyncEnabled() { public static boolean isMasterSyncEnabled(Context context) {
return mMasterSyncEnabled; ensureInitialized(context);
return sInstance.mMasterSyncEnabled;
} }
/** /**
* Make sure Chrome is syncable, and enable sync. * Make sure Chrome is syncable, and enable sync.
*/ */
public void enableChromeSync() { public static void enableChromeSync(Context context) {
setChromeSyncEnabled(true); ensureInitialized(context);
sInstance.setChromeSyncEnabled(true);
} }
/** /**
* Disables Android Chrome sync * Disables Android Chrome sync
*/ */
public void disableChromeSync() { public static void disableChromeSync(Context context) {
setChromeSyncEnabled(false); ensureInitialized(context);
sInstance.setChromeSyncEnabled(false);
} }
/** /**
* Must be called when a new account is signed in. * Must be called when a new account is signed in.
*/ */
public void updateAccount(Account account) { public static void updateAccount(Context context, Account account) {
synchronized (mLock) { ensureInitialized(context);
mAccount = account; synchronized (sInstance.mLock) {
updateSyncability(); sInstance.mAccount = account;
sInstance.updateSyncability();
} }
if (updateCachedSettings()) { if (sInstance.updateCachedSettings()) {
notifyObservers(); sInstance.notifyObservers();
} }
} }
/** /**
* Returns the contract authority to use when requesting sync. * Returns the contract authority to use when requesting sync.
*/ */
public String getContractAuthority() { public static String getContractAuthority(Context context) {
return mApplicationContext.getPackageName(); ensureInitialized(context);
return sInstance.getContractAuthority();
} }
/** /**
* Add a new AndroidSyncSettingsObserver. * Add a new AndroidSyncSettingsObserver.
*/ */
public void registerObserver(AndroidSyncSettingsObserver observer) { public static void registerObserver(Context context, AndroidSyncSettingsObserver observer) {
synchronized (mLock) { ensureInitialized(context);
mObservers.addObserver(observer); synchronized (sInstance.mLock) {
sInstance.mObservers.addObserver(observer);
} }
} }
/** /**
* Remove an AndroidSyncSettingsObserver that was previously added. * Remove an AndroidSyncSettingsObserver that was previously added.
*/ */
public void unregisterObserver(AndroidSyncSettingsObserver observer) { public static void unregisterObserver(Context context, AndroidSyncSettingsObserver observer) {
synchronized (mLock) { ensureInitialized(context);
mObservers.removeObserver(observer); synchronized (sInstance.mLock) {
sInstance.mObservers.removeObserver(observer);
} }
} }
...@@ -290,4 +290,68 @@ public class AndroidSyncSettings { ...@@ -290,4 +290,68 @@ public class AndroidSyncSettings {
observer.androidSyncSettingsChanged(); observer.androidSyncSettingsChanged();
} }
} }
// TODO(maxbogue): make private once downstream uses are removed.
@Deprecated
public String getContractAuthority() {
return mApplicationContext.getPackageName();
}
// Deprecated section; to be removed once downstream no longer uses them.
@Deprecated
public static AndroidSyncSettings get(Context context) {
ensureInitialized(context);
return sInstance;
}
@Deprecated
public boolean isSyncEnabled() {
return mMasterSyncEnabled && mChromeSyncEnabled;
}
@Deprecated
public boolean isChromeSyncEnabled() {
return mChromeSyncEnabled;
}
@Deprecated
public boolean isMasterSyncEnabled() {
return mMasterSyncEnabled;
}
@Deprecated
public void enableChromeSync() {
setChromeSyncEnabled(true);
}
@Deprecated
public void disableChromeSync() {
setChromeSyncEnabled(false);
}
@Deprecated
public void updateAccount(Account account) {
synchronized (mLock) {
mAccount = account;
updateSyncability();
}
if (updateCachedSettings()) {
notifyObservers();
}
}
@Deprecated
public void registerObserver(AndroidSyncSettingsObserver observer) {
synchronized (mLock) {
mObservers.addObserver(observer);
}
}
@Deprecated
public void unregisterObserver(AndroidSyncSettingsObserver observer) {
synchronized (mLock) {
mObservers.removeObserver(observer);
}
}
} }
...@@ -44,14 +44,11 @@ public class ChromeSigninController { ...@@ -44,14 +44,11 @@ public class ChromeSigninController {
private final ObserverList<Listener> mListeners = new ObserverList<Listener>(); private final ObserverList<Listener> mListeners = new ObserverList<Listener>();
private final AndroidSyncSettings mAndroidSyncSettings;
private boolean mGcmInitialized; private boolean mGcmInitialized;
private ChromeSigninController(Context context) { private ChromeSigninController(Context context) {
mApplicationContext = context.getApplicationContext(); mApplicationContext = context.getApplicationContext();
mAndroidSyncSettings = AndroidSyncSettings.get(context); AndroidSyncSettings.updateAccount(context, getSignedInUser());
mAndroidSyncSettings.updateAccount(getSignedInUser());
} }
/** /**
...@@ -86,7 +83,7 @@ public class ChromeSigninController { ...@@ -86,7 +83,7 @@ public class ChromeSigninController {
.putString(SIGNED_IN_ACCOUNT_KEY, accountName) .putString(SIGNED_IN_ACCOUNT_KEY, accountName)
.apply(); .apply();
// TODO(maxbogue): Move this to SigninManager. // TODO(maxbogue): Move this to SigninManager.
mAndroidSyncSettings.updateAccount(getSignedInUser()); AndroidSyncSettings.updateAccount(mApplicationContext, getSignedInUser());
} }
public void clearSignedInUser() { public void clearSignedInUser() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment