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

Use Future instead of SyncListener to sync History/Bookmarks sync states #2171

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 @@ -13,7 +13,9 @@

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mozilla.geckoview.GeckoSessionSettings;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.VRBrowserActivity;
import org.mozilla.vrbrowser.VRBrowserApplication;
import org.mozilla.vrbrowser.browser.Accounts;
import org.mozilla.vrbrowser.browser.SettingsStore;
Expand Down Expand Up @@ -79,8 +81,6 @@ public void onShown() {
super.onShown();

mAccounts.addAccountListener(mAccountListener);
mAccounts.addSyncListener(mSyncListener);

updateSyncState();
}

Expand All @@ -89,64 +89,48 @@ public void onHidden() {
super.onHidden();

mAccounts.removeAccountListener(mAccountListener);
mAccounts.removeSyncListener(mSyncListener);
}

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

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

private void resetOptions() {
mAccounts.setSyncStatus(SyncEngine.Bookmarks.INSTANCE, SettingsStore.BOOKMARKS_SYNC_DEFAULT);
mAccounts.setSyncStatus(SyncEngine.History.INSTANCE, SettingsStore.HISTORY_SYNC_DEFAULT);
mBinding.historySyncSwitch.setValue(SettingsStore.HISTORY_SYNC_DEFAULT, false);
mBinding.bookmarksSyncSwitch.setValue(SettingsStore.BOOKMARKS_SYNC_DEFAULT, false);
sync();
}

private SyncStatusObserver mSyncListener = new SyncStatusObserver() {
@Override
public void onStarted() {
mSyncing = true;
Log.d(LOGTAG, "SyncStatusObserver sync started");
}

@Override
public void onIdle() {
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 ex) {
Log.d(LOGTAG, "SyncStatusObserver sync failed");
if (ex != null) {
ex.printStackTrace();
}
// Resync if needed
onIdle();
}
};

private void sync() {
if (mSyncing) {
mResyncNeeded = true;
} else {
Log.d(LOGTAG, "SyncStatusObserver sync started");
mSyncing = true;
mAccounts.syncNowAsync(SyncReason.EngineChange.INSTANCE, false);
mAccounts.setSyncStatus(SyncEngine.Bookmarks.INSTANCE, mBinding.bookmarksSyncSwitch.isChecked());
mAccounts.setSyncStatus(SyncEngine.History.INSTANCE, mBinding.historySyncSwitch.isChecked());
mAccounts.syncNowAsync(SyncReason.EngineChange.INSTANCE, false).thenAcceptAsync((value) -> {
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();
}
}, mUIThreadExecutor).exceptionally(throwable -> {
Log.d(LOGTAG, "SyncStatusObserver sync failed");
mSyncing = false;
throwable.printStackTrace();
return null;
});
}
}

Expand Down