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

Commit

Permalink
Do not overlap History/Bookmarks FxA SyncStatus change operations
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro committed Nov 5, 2019
1 parent 5cff5e3 commit 05fbf08
Showing 1 changed file with 40 additions and 10 deletions.
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

0 comments on commit 05fbf08

Please sign in to comment.