Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Do not overlap History/Bookmarks FxA SyncStatus change operations #2168

Merged
merged 1 commit into from
Nov 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class FxAAccountOptionsView extends SettingsView {
private OptionsFxaAccountBinding mBinding;
private Accounts mAccounts;
private Executor mUIThreadExecutor;
private boolean mSyncing;
private boolean mResyncNeeded;

public FxAAccountOptionsView(Context aContext, WidgetManagerDelegate aWidgetManager) {
super(aContext, aWidgetManager);
Expand Down Expand Up @@ -79,8 +81,7 @@ public void onShown() {
mAccounts.addAccountListener(mAccountListener);
mAccounts.addSyncListener(mSyncListener);

mBinding.bookmarksSyncSwitch.setValue(mAccounts.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE), false);
mBinding.historySyncSwitch.setValue(mAccounts.isEngineEnabled(SyncEngine.History.INSTANCE), false);
updateSyncState();
}

@Override
Expand All @@ -93,38 +94,67 @@ public void onHidden() {

private SwitchSetting.OnCheckedChangeListener mBookmarksSyncListener = (compoundButton, value, apply) -> {
mAccounts.setSyncStatus(SyncEngine.Bookmarks.INSTANCE, value);
mAccounts.syncNowAsync(SyncReason.EngineChange.INSTANCE, false);
sync();
};

private SwitchSetting.OnCheckedChangeListener mHistorySyncListener = (compoundButton, value, apply) -> {
mAccounts.setSyncStatus(SyncEngine.History.INSTANCE, value);
mAccounts.syncNowAsync(SyncReason.EngineChange.INSTANCE, false);
sync();
};

private void resetOptions() {
mAccounts.setSyncStatus(SyncEngine.Bookmarks.INSTANCE, SettingsStore.BOOKMARKS_SYNC_DEFAULT);
mAccounts.setSyncStatus(SyncEngine.History.INSTANCE, SettingsStore.HISTORY_SYNC_DEFAULT);
mAccounts.syncNowAsync(SyncReason.EngineChange.INSTANCE, false);
sync();
}

private SyncStatusObserver mSyncListener = new SyncStatusObserver() {
@Override
public void onStarted() {

mSyncing = true;
Log.d(LOGTAG, "SyncStatusObserver sync started");
}

@Override
public void onIdle() {
mBinding.bookmarksSyncSwitch.setValue(mAccounts.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE), false);
mBinding.historySyncSwitch.setValue(mAccounts.isEngineEnabled(SyncEngine.History.INSTANCE), false);
mSyncing = false;
if (mResyncNeeded) {
Log.d(LOGTAG, "SyncStatusObserver resync: The user has changed the settings while it was syncing");
mResyncNeeded = false;
mAccounts.setSyncStatus(SyncEngine.Bookmarks.INSTANCE, mBinding.bookmarksSyncSwitch.isChecked());
mAccounts.setSyncStatus(SyncEngine.History.INSTANCE, mBinding.historySyncSwitch.isChecked());
sync();
} else {
Log.d(LOGTAG, "SyncStatusObserver sync completed");
updateSyncState();
}
}

@Override
public void onError(@Nullable Exception e) {

public void onError(@Nullable Exception ex) {
Log.d(LOGTAG, "SyncStatusObserver sync failed");
if (ex != null) {
ex.printStackTrace();
}
// Resync if needed
onIdle();
}
};

private void sync() {
if (mSyncing) {
mResyncNeeded = true;
} else {
mSyncing = true;
mAccounts.syncNowAsync(SyncReason.EngineChange.INSTANCE, false);
}
}

private void updateSyncState() {
mBinding.bookmarksSyncSwitch.setValue(mAccounts.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE), false);
mBinding.historySyncSwitch.setValue(mAccounts.isEngineEnabled(SyncEngine.History.INSTANCE), false);
}

void updateCurrentAccountState() {
switch(mAccounts.getAccountStatus()) {
case NEEDS_RECONNECT:
Expand Down