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

Commit

Permalink
Always sync after signing in and remember sync status
Browse files Browse the repository at this point in the history
Some refactoring too
  • Loading branch information
keianhzo committed Oct 21, 2019
1 parent a2894c2 commit b779b93
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@ protected void onCreate(Bundle savedInstanceState) {
SessionStore.get().setContext(this, extras);
SessionStore.get().initializeStores(this);

SessionStore.get().initializeAccounts(this);

// Create broadcast receiver for getting crash messages from crash process
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(CrashReporterService.CRASH_ACTION);
Expand Down Expand Up @@ -390,7 +388,7 @@ protected void onResume() {

// If we're signed-in, poll for any new device events (e.g. received tabs) on activity resume.
// There's no push support right now, so this helps with the perception of speedy tab delivery.
SessionStore.get().getAccountsManager().refreshDevicesAsync();
((VRBrowserApplication)getApplicationContext()).getAccounts().refreshDevicesAsync();

super.onResume();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.content.Context;
import android.content.res.Configuration;

import org.mozilla.vrbrowser.browser.Accounts;
import org.mozilla.vrbrowser.browser.Places;
import org.mozilla.vrbrowser.browser.Services;
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
Expand All @@ -19,6 +20,7 @@ public class VRBrowserApplication extends Application {
private AppExecutors mAppExecutors;
private Services mServices;
private Places mPlaces;
private Accounts mAccounts;

@Override
public void onCreate() {
Expand All @@ -27,6 +29,7 @@ public void onCreate() {
mAppExecutors = new AppExecutors();
mPlaces = new Places(this);
mServices = new Services(this, mPlaces);
mAccounts = new Accounts(this);

TelemetryWrapper.init(this);
}
Expand All @@ -50,4 +53,8 @@ public Services getServices() {
public Places getPlaces() {
return mPlaces;
}

public Accounts getAccounts() {
return mAccounts;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import java.util.concurrent.CompletableFuture
import java.util.concurrent.ExecutionException
import java.util.concurrent.Executors

class AccountsManager constructor(val context: Context) {
class Accounts constructor(val context: Context) {

private val LOGTAG = SystemUtils.createLogtag(AccountsManager::class.java)
private val LOGTAG = SystemUtils.createLogtag(Accounts::class.java)

enum class AccountStatus {
SIGNED_IN,
Expand Down Expand Up @@ -86,6 +86,12 @@ class AccountsManager constructor(val context: Context) {
private val accountObserver = object : AccountObserver {
override fun onAuthenticated(account: OAuthAccount, authType: AuthType) {
accountStatus = AccountStatus.SIGNED_IN

// Enable syncing after signing in
syncStorage.setStatus(SyncEngine.Bookmarks, SettingsStore.getInstance(context).isBookmarksSyncEnabled)
syncStorage.setStatus(SyncEngine.History, SettingsStore.getInstance(context).isHistorySyncEnabled)
services.accountManager.syncNowAsync(SyncReason.EngineChange, false)

account.deviceConstellation().refreshDevicesAsync()
accountListeners.toMutableList().forEach {
Handler(Looper.getMainLooper()).post {
Expand Down Expand Up @@ -194,6 +200,12 @@ class AccountsManager constructor(val context: Context) {
}

fun setSyncStatus(engine: SyncEngine, value: Boolean) {

when(engine) {
SyncEngine.Bookmarks -> SettingsStore.getInstance(context).isBookmarksSyncEnabled = value
SyncEngine.History -> SettingsStore.getInstance(context).isHistorySyncEnabled = value
}

syncStorage.setStatus(engine, value)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ SettingsStore getInstance(final @NonNull Context aContext) {
private final static long CRASH_RESTART_DELTA = 2000;
public final static boolean AUTOPLAY_ENABLED = false;
public final static boolean DEBUG_LOGGING_DEFAULT = false;
public final static boolean BOOKMARKS_SYNC_DEFAULT = true;
public final static boolean HISTORY_SYNC_DEFAULT = true;

// Enable telemetry by default (opt-out).
public final static boolean CRASH_REPORTING_DEFAULT = false;
Expand Down Expand Up @@ -560,5 +562,25 @@ public int getPid() {
return mPrefs.getInt(mContext.getString(R.string.settings_key_pid), 0);
}

public void setBookmarksSyncEnabled(boolean isEnabled) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(mContext.getString(R.string.settings_key_bookmarks_sync), isEnabled);
editor.commit();
}

public boolean isBookmarksSyncEnabled() {
return mPrefs.getBoolean(mContext.getString(R.string.settings_key_bookmarks_sync), BOOKMARKS_SYNC_DEFAULT);
}

public void setHistorySyncEnabled(boolean isEnabled) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(mContext.getString(R.string.settings_key_history_sync), isEnabled);
editor.commit();
}

public boolean isHistorySyncEnabled() {
return mPrefs.getBoolean(mContext.getString(R.string.settings_key_history_sync), HISTORY_SYNC_DEFAULT);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.mozilla.vrbrowser.browser.PermissionDelegate;
import org.mozilla.vrbrowser.browser.SettingsStore;
import org.mozilla.vrbrowser.crashreporting.CrashReporterService;
import org.mozilla.vrbrowser.browser.AccountsManager;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -49,7 +48,6 @@ public static SessionStore get() {
private PermissionDelegate mPermissionDelegate;
private BookmarksStore mBookmarksStore;
private HistoryStore mHistoryStore;
private AccountsManager mAccountsManager;

private SessionStore() {
mSessionStacks = new HashMap<>();
Expand Down Expand Up @@ -107,10 +105,6 @@ public void initializeStores(Context context) {
mHistoryStore = new HistoryStore(context);
}

public void initializeAccounts(Context context) {
mAccountsManager = new AccountsManager(context);
}

public SessionStack createSessionStack(int storeId, boolean privateMode) {
SessionStack store = new SessionStack(mContext, mRuntime, privateMode);
store.setPermissionDelegate(this);
Expand Down Expand Up @@ -157,10 +151,6 @@ public void purgeSessionHistory() {
}
}

public AccountsManager getAccountsManager() {
return mAccountsManager;
}

public void onPause() {
for (Map.Entry<Integer, SessionStack> entry : mSessionStacks.entrySet()) {
entry.getValue().setActive(false);
Expand All @@ -186,11 +176,6 @@ public void onDestroy() {
if (mHistoryStore != null) {
mHistoryStore.removeAllListeners();
}

if (mAccountsManager != null) {
mAccountsManager.removeAllAccountListeners();
mAccountsManager.removeAllSyncListeners();
}
}

public void onConfigurationChanged(Configuration newConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.browser.AccountsManager;
import org.mozilla.vrbrowser.VRBrowserApplication;
import org.mozilla.vrbrowser.browser.Accounts;
import org.mozilla.vrbrowser.browser.BookmarksStore;
import org.mozilla.vrbrowser.browser.SettingsStore;
import org.mozilla.vrbrowser.browser.engine.SessionStack;
Expand Down Expand Up @@ -52,7 +53,7 @@ public class BookmarksView extends FrameLayout implements BookmarksStore.Bookmar

private BookmarksBinding mBinding;
private ObjectAnimator mSyncingAnimation;
private AccountsManager mAccountManager;
private Accounts mAccounts;
private BookmarkAdapter mBookmarkAdapter;
private boolean mIgnoreNextListener;
private ArrayList<BookmarksCallback> mBookmarksViewListeners;
Expand Down Expand Up @@ -101,12 +102,12 @@ private void initialize(Context aContext) {
mSyncingAnimation = ObjectAnimator.ofInt(drawables[0], "level", 0, 10000);
mSyncingAnimation.setRepeatCount(ObjectAnimator.INFINITE);

mAccountManager = SessionStore.get().getAccountsManager();
mAccountManager.addAccountListener(mAccountListener);
mAccountManager.addSyncListener(mSyncListener);
mAccounts = ((VRBrowserApplication)getContext().getApplicationContext()).getAccounts();
mAccounts.addAccountListener(mAccountListener);
mAccounts.addSyncListener(mSyncListener);

mBinding.setIsSignedIn(mAccountManager.isSignedIn());
mBinding.setIsSyncEnabled(mAccountManager.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE));
mBinding.setIsSignedIn(mAccounts.isSignedIn());
mBinding.setIsSyncEnabled(mAccounts.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE));

updateBookmarks();
SessionStore.get().getBookmarkStore().addListener(this);
Expand All @@ -121,8 +122,8 @@ private void initialize(Context aContext) {

public void onDestroy() {
SessionStore.get().getBookmarkStore().removeListener(this);
mAccountManager.removeAccountListener(mAccountListener);
mAccountManager.removeSyncListener(mSyncListener);
mAccounts.removeAccountListener(mAccountListener);
mAccounts.removeSyncListener(mSyncListener);
}

private final BookmarkItemCallback mBookmarkItemCallback = new BookmarkItemCallback() {
Expand Down Expand Up @@ -184,14 +185,14 @@ public void onClearBookmarks(@NonNull View view) {

@Override
public void onSyncBookmarks(@NonNull View view) {
mAccountManager.syncNowAsync(SyncReason.User.INSTANCE, false);
mAccounts.syncNowAsync(SyncReason.User.INSTANCE, false);
}

@Override
public void onFxALogin(@NonNull View view) {
mAccountManager.getAuthenticationUrlAsync().thenAcceptAsync((url) -> {
mAccounts.getAuthenticationUrlAsync().thenAcceptAsync((url) -> {
if (url != null) {
mAccountManager.setLoginOrigin(AccountsManager.LoginOrigin.HISTORY);
mAccounts.setLoginOrigin(Accounts.LoginOrigin.BOOKMARKS);
SessionStore.get().getActiveStore().loadUri(url);
}
});
Expand All @@ -216,27 +217,28 @@ public void removeBookmarksListener(@NonNull BookmarksCallback listener) {
private SyncStatusObserver mSyncListener = new SyncStatusObserver() {
@Override
public void onStarted() {
mBinding.setIsSyncEnabled(mAccountManager.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE));
boolean isSyncEnabled = mAccounts.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE);
mBinding.setIsSyncEnabled(isSyncEnabled);
mBinding.executePendingBindings();
if (mAccountManager.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE)) {
if (isSyncEnabled) {
mSyncingAnimation.setDuration(500);
mSyncingAnimation.start();
}
}

@Override
public void onIdle() {
if (mAccountManager.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE)) {
if (mAccounts.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE)) {
mSyncingAnimation.cancel();
mBinding.setLastSync(mAccountManager.getLastSync());
mBinding.setLastSync(mAccounts.getLastSync());
}
}

@Override
public void onError(@Nullable Exception e) {
mBinding.setIsSyncEnabled(mAccountManager.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE));
mBinding.setIsSyncEnabled(mAccounts.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE));
mBinding.executePendingBindings();
if (mAccountManager.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE)) {
if (mAccounts.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE)) {
mSyncingAnimation.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.browser.AccountsManager;
import org.mozilla.vrbrowser.VRBrowserApplication;
import org.mozilla.vrbrowser.browser.Accounts;
import org.mozilla.vrbrowser.browser.HistoryStore;
import org.mozilla.vrbrowser.browser.SettingsStore;
import org.mozilla.vrbrowser.browser.engine.SessionStack;
Expand Down Expand Up @@ -58,7 +59,7 @@ public class HistoryView extends FrameLayout implements HistoryStore.HistoryList

private HistoryBinding mBinding;
private ObjectAnimator mSyncingAnimation;
private AccountsManager mAccountManager;
private Accounts mAccounts;
private HistoryAdapter mHistoryAdapter;
private boolean mIgnoreNextListener;
private ArrayList<HistoryCallback> mHistoryViewListeners;
Expand Down Expand Up @@ -104,12 +105,12 @@ private void initialize(Context aContext) {
mSyncingAnimation = ObjectAnimator.ofInt(drawables[0], "level", 0, 10000);
mSyncingAnimation.setRepeatCount(ObjectAnimator.INFINITE);

mAccountManager = SessionStore.get().getAccountsManager();
mAccountManager.addAccountListener(mAccountListener);
mAccountManager.addSyncListener(mSyncListener);
mAccounts = ((VRBrowserApplication)getContext().getApplicationContext()).getAccounts();
mAccounts.addAccountListener(mAccountListener);
mAccounts.addSyncListener(mSyncListener);

mBinding.setIsSignedIn(mAccountManager.isSignedIn());
mBinding.setIsSyncEnabled(mAccountManager.isEngineEnabled(SyncEngine.History.INSTANCE));
mBinding.setIsSignedIn(mAccounts.isSignedIn());
mBinding.setIsSyncEnabled(mAccounts.isEngineEnabled(SyncEngine.History.INSTANCE));

updateHistory();
SessionStore.get().getHistoryStore().addListener(this);
Expand All @@ -124,8 +125,8 @@ private void initialize(Context aContext) {

public void onDestroy() {
SessionStore.get().getHistoryStore().removeListener(this);
mAccountManager.removeAccountListener(mAccountListener);
mAccountManager.removeSyncListener(mSyncListener);
mAccounts.removeAccountListener(mAccountListener);
mAccounts.removeSyncListener(mSyncListener);
}

private final HistoryItemCallback mHistoryItemCallback = new HistoryItemCallback() {
Expand Down Expand Up @@ -181,14 +182,14 @@ public void onClearHistory(@NonNull View view) {

@Override
public void onSyncHistory(@NonNull View view) {
mAccountManager.syncNowAsync(SyncReason.User.INSTANCE, false);
mAccounts.syncNowAsync(SyncReason.User.INSTANCE, false);
}

@Override
public void onFxALogin(@NonNull View view) {
mAccountManager.getAuthenticationUrlAsync().thenAcceptAsync((url) -> {
mAccounts.getAuthenticationUrlAsync().thenAcceptAsync((url) -> {
if (url != null) {
mAccountManager.setLoginOrigin(AccountsManager.LoginOrigin.HISTORY);
mAccounts.setLoginOrigin(Accounts.LoginOrigin.HISTORY);
SessionStore.get().getActiveStore().loadUri(url);
}
});
Expand All @@ -213,27 +214,28 @@ public void removeHistoryListener(@NonNull HistoryCallback listener) {
private SyncStatusObserver mSyncListener = new SyncStatusObserver() {
@Override
public void onStarted() {
mBinding.setIsSyncEnabled(mAccountManager.isEngineEnabled(SyncEngine.History.INSTANCE));
boolean isSyncEnabled = mAccounts.isEngineEnabled(SyncEngine.History.INSTANCE);
mBinding.setIsSyncEnabled(isSyncEnabled);
mBinding.executePendingBindings();
if (mAccountManager.isEngineEnabled(SyncEngine.History.INSTANCE)) {
if (isSyncEnabled) {
mSyncingAnimation.setDuration(500);
mSyncingAnimation.start();
}
}

@Override
public void onIdle() {
if (mAccountManager.isEngineEnabled(SyncEngine.History.INSTANCE)) {
if (mAccounts.isEngineEnabled(SyncEngine.History.INSTANCE)) {
mSyncingAnimation.cancel();
mBinding.setLastSync(mAccountManager.getLastSync());
mBinding.setLastSync(mAccounts.getLastSync());
}
}

@Override
public void onError(@Nullable Exception e) {
mBinding.setIsSyncEnabled(mAccountManager.isEngineEnabled(SyncEngine.History.INSTANCE));
mBinding.setIsSyncEnabled(mAccounts.isEngineEnabled(SyncEngine.History.INSTANCE));
mBinding.executePendingBindings();
if (mAccountManager.isEngineEnabled(SyncEngine.History.INSTANCE)) {
if (mAccounts.isEngineEnabled(SyncEngine.History.INSTANCE)) {
mSyncingAnimation.cancel();
}
}
Expand Down
Loading

0 comments on commit b779b93

Please sign in to comment.