From a1fd175a46cda1453747e43781aba16e4eab3c52 Mon Sep 17 00:00:00 2001 From: Manuel Martin Date: Tue, 15 Oct 2019 11:31:40 +0200 Subject: [PATCH] Always sync after signing in and remember sync status Some refactoring too --- .../mozilla/vrbrowser/VRBrowserActivity.java | 4 +- .../vrbrowser/VRBrowserApplication.java | 7 ++ .../{AccountsManager.kt => Accounts.kt} | 16 +++- .../vrbrowser/browser/SettingsStore.java | 22 +++++ .../browser/engine/SessionStore.java | 15 ---- .../vrbrowser/ui/views/BookmarksView.java | 38 ++++---- .../vrbrowser/ui/views/HistoryView.java | 38 ++++---- .../mozilla/vrbrowser/ui/widgets/Windows.java | 14 +-- .../settings/FxAAccountOptionsView.java | 88 +++++++------------ .../ui/widgets/settings/SettingsWidget.java | 23 ++--- app/src/main/res/values/non_L10n.xml | 2 + 11 files changed, 139 insertions(+), 128 deletions(-) rename app/src/common/shared/org/mozilla/vrbrowser/browser/{AccountsManager.kt => Accounts.kt} (92%) diff --git a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java index 64522535e..8bba0837a 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java @@ -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); @@ -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(); } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserApplication.java b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserApplication.java index f5a1e792a..d42c638ad 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserApplication.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserApplication.java @@ -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; @@ -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() { @@ -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); } @@ -50,4 +53,8 @@ public Services getServices() { public Places getPlaces() { return mPlaces; } + + public Accounts getAccounts() { + return mAccounts; + } } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/browser/AccountsManager.kt b/app/src/common/shared/org/mozilla/vrbrowser/browser/Accounts.kt similarity index 92% rename from app/src/common/shared/org/mozilla/vrbrowser/browser/AccountsManager.kt rename to app/src/common/shared/org/mozilla/vrbrowser/browser/Accounts.kt index 49886f3d8..249b9ae33 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/browser/AccountsManager.kt +++ b/app/src/common/shared/org/mozilla/vrbrowser/browser/Accounts.kt @@ -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, @@ -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 { @@ -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) } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/browser/SettingsStore.java b/app/src/common/shared/org/mozilla/vrbrowser/browser/SettingsStore.java index c0a1e50ca..be4d09183 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/browser/SettingsStore.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/browser/SettingsStore.java @@ -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; @@ -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); + } + } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java index 038a0f99b..a30bc2108 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java @@ -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; @@ -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<>(); @@ -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); @@ -157,10 +151,6 @@ public void purgeSessionHistory() { } } - public AccountsManager getAccountsManager() { - return mAccountsManager; - } - public void onPause() { for (Map.Entry entry : mSessionStacks.entrySet()) { entry.getValue().setActive(false); @@ -186,11 +176,6 @@ public void onDestroy() { if (mHistoryStore != null) { mHistoryStore.removeAllListeners(); } - - if (mAccountsManager != null) { - mAccountsManager.removeAllAccountListeners(); - mAccountsManager.removeAllSyncListeners(); - } } public void onConfigurationChanged(Configuration newConfig) { diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/views/BookmarksView.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/views/BookmarksView.java index 83232018e..bbeec9176 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/views/BookmarksView.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/views/BookmarksView.java @@ -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; @@ -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 mBookmarksViewListeners; @@ -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); @@ -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() { @@ -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); } }); @@ -216,9 +217,10 @@ 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(); } @@ -226,17 +228,17 @@ public void onStarted() { @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(); } } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/views/HistoryView.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/views/HistoryView.java index b126db71f..5910becd7 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/views/HistoryView.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/views/HistoryView.java @@ -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; @@ -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 mHistoryViewListeners; @@ -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); @@ -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() { @@ -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); } }); @@ -213,9 +214,10 @@ 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(); } @@ -223,17 +225,17 @@ public void onStarted() { @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(); } } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/Windows.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/Windows.java index 009008da7..744e7f1b2 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/Windows.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/Windows.java @@ -13,11 +13,11 @@ import org.jetbrains.annotations.NotNull; import org.mozilla.geckoview.GeckoSession; 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.Media; import org.mozilla.vrbrowser.browser.SettingsStore; import org.mozilla.vrbrowser.browser.engine.SessionStack; -import org.mozilla.vrbrowser.browser.engine.SessionStore; import org.mozilla.vrbrowser.telemetry.TelemetryWrapper; import org.mozilla.vrbrowser.utils.SystemUtils; @@ -92,7 +92,7 @@ class WindowsState { private boolean mStoredCurvedMode = false; private boolean mForcedCurvedMode = false; private boolean mIsPaused = false; - private AccountsManager mAccountManager; + private Accounts mAccounts; public enum WindowPlacement{ FRONT(0), @@ -126,8 +126,8 @@ public Windows(Context aContext) { mStoredCurvedMode = SettingsStore.getInstance(mContext).getCylinderDensity() > 0.0f; - mAccountManager = SessionStore.get().getAccountsManager(); - mAccountManager.addAccountListener(mAccountObserver); + mAccounts = ((VRBrowserApplication)mContext.getApplicationContext()).getAccounts(); + mAccounts.addAccountListener(mAccountObserver); restoreWindows(); } @@ -422,7 +422,7 @@ public void onDestroy() { for (WindowWidget window: mPrivateWindows) { window.close(); } - mAccountManager.removeAccountListener(mAccountObserver); + mAccounts.removeAccountListener(mAccountObserver); } public boolean isInPrivateMode() { @@ -855,7 +855,7 @@ public void onLoggedOut() { @Override public void onAuthenticated(@NotNull OAuthAccount oAuthAccount, @NotNull AuthType authType) { - switch (mAccountManager.getLoginOrigin()) { + switch (mAccounts.getLoginOrigin()) { case BOOKMARKS: getFocusedWindow().switchBookmarks(); break; diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/FxAAccountOptionsView.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/FxAAccountOptionsView.java index afb7afe8e..d6787429b 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/FxAAccountOptionsView.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/FxAAccountOptionsView.java @@ -13,13 +13,16 @@ 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.browser.engine.SessionStore; +import org.mozilla.vrbrowser.VRBrowserApplication; +import org.mozilla.vrbrowser.browser.Accounts; +import org.mozilla.vrbrowser.browser.SettingsStore; import org.mozilla.vrbrowser.databinding.OptionsFxaAccountBinding; import org.mozilla.vrbrowser.ui.views.settings.SwitchSetting; import org.mozilla.vrbrowser.ui.widgets.WidgetManagerDelegate; import org.mozilla.vrbrowser.utils.SystemUtils; +import java.util.Objects; + import mozilla.components.concept.sync.AccountObserver; import mozilla.components.concept.sync.AuthType; import mozilla.components.concept.sync.OAuthAccount; @@ -32,10 +35,8 @@ class FxAAccountOptionsView extends SettingsView { private static final String LOGTAG = SystemUtils.createLogtag(FxAAccountOptionsView.class); - private static final long SYNC_REFRESH_TIME = 10L; - private OptionsFxaAccountBinding mBinding; - private AccountsManager mAccountManager; + private Accounts mAccounts; public FxAAccountOptionsView(Context aContext, WidgetManagerDelegate aWidgetManager) { super(aContext, aWidgetManager); @@ -53,9 +54,9 @@ private void initialize(Context aContext) { // Header mBinding.headerLayout.setBackClickListener(view -> onDismiss()); - mAccountManager = SessionStore.get().getAccountsManager(); + mAccounts = ((VRBrowserApplication)getContext().getApplicationContext()).getAccounts(); - mBinding.signButton.setOnClickListener(view -> mAccountManager.logoutAsync()); + mBinding.signButton.setOnClickListener(view -> mAccounts.logoutAsync()); mBinding.bookmarksSyncSwitch.setOnCheckedChangeListener(mBookmarksSyncListener); mBinding.historySyncSwitch.setOnCheckedChangeListener(mHistorySyncListener); @@ -70,68 +71,47 @@ private void initialize(Context aContext) { public void onShown() { super.onShown(); - mAccountManager.addAccountListener(mAccountListener); - mAccountManager.addSyncListener(mSyncListener); + mAccounts.addAccountListener(mAccountListener); + mAccounts.addSyncListener(mSyncListener); - setBookmarksSync(mAccountManager.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE), false); - setHistorySync(mAccountManager.isEngineEnabled(SyncEngine.History.INSTANCE), false); + mBinding.bookmarksSyncSwitch.setValue(mAccounts.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE), false); + mBinding.historySyncSwitch.setValue(mAccounts.isEngineEnabled(SyncEngine.History.INSTANCE), false); } @Override public void onHidden() { super.onHidden(); - mAccountManager.removeAccountListener(mAccountListener); - mAccountManager.removeSyncListener(mSyncListener); + mAccounts.removeAccountListener(mAccountListener); + mAccounts.removeSyncListener(mSyncListener); } - private SwitchSetting.OnCheckedChangeListener mBookmarksSyncListener = (compoundButton, value, apply) -> setBookmarksSync(value, apply); + private SwitchSetting.OnCheckedChangeListener mBookmarksSyncListener = (compoundButton, value, apply) -> { + mAccounts.setSyncStatus(SyncEngine.Bookmarks.INSTANCE, value); + mAccounts.syncNowAsync(SyncReason.EngineChange.INSTANCE, false); + }; - private SwitchSetting.OnCheckedChangeListener mHistorySyncListener = (compoundButton, value, apply) -> setHistorySync(value, apply); + private SwitchSetting.OnCheckedChangeListener mHistorySyncListener = (compoundButton, value, apply) -> { + mAccounts.setSyncStatus(SyncEngine.History.INSTANCE, value); + mAccounts.syncNowAsync(SyncReason.EngineChange.INSTANCE, false); + }; private void resetOptions() { - setBookmarksSync(true, true); - setHistorySync(true, true); - } - - private void setBookmarksSync(boolean value, boolean doApply) { - updateBookmarkSwitch(value); - if (doApply) { - mAccountManager.setSyncStatus(SyncEngine.Bookmarks.INSTANCE, value); - mAccountManager.syncNowAsync(SyncReason.EngineChange.INSTANCE, false); - } - } - - private void updateBookmarkSwitch(boolean value) { - mBinding.bookmarksSyncSwitch.setOnCheckedChangeListener(null); - mBinding.bookmarksSyncSwitch.setValue(value, false); - mBinding.bookmarksSyncSwitch.setOnCheckedChangeListener(mBookmarksSyncListener); - } - - private void setHistorySync(boolean value, boolean doApply) { - updateHistorySwitch(value); - if (doApply) { - mAccountManager.setSyncStatus(SyncEngine.History.INSTANCE, value); - mAccountManager.syncNowAsync(SyncReason.EngineChange.INSTANCE, false); - } - } - - private void updateHistorySwitch(boolean value) { - mBinding.historySyncSwitch.setOnCheckedChangeListener(null); - mBinding.historySyncSwitch.setValue(value, false); - mBinding.historySyncSwitch.setOnCheckedChangeListener(mHistorySyncListener); + mAccounts.setSyncStatus(SyncEngine.Bookmarks.INSTANCE, SettingsStore.BOOKMARKS_SYNC_DEFAULT); + mAccounts.setSyncStatus(SyncEngine.History.INSTANCE, SettingsStore.HISTORY_SYNC_DEFAULT); + mAccounts.syncNowAsync(SyncReason.EngineChange.INSTANCE, false); } private SyncStatusObserver mSyncListener = new SyncStatusObserver() { @Override public void onStarted() { - + mBinding.bookmarksSyncSwitch.setValue(mAccounts.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE), false); + mBinding.historySyncSwitch.setValue(mAccounts.isEngineEnabled(SyncEngine.History.INSTANCE), false); } @Override public void onIdle() { - mBinding.bookmarksSyncSwitch.setValue(mAccountManager.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE), false); - mBinding.historySyncSwitch.setValue(mAccountManager.isEngineEnabled(SyncEngine.History.INSTANCE), false); + } @Override @@ -141,19 +121,19 @@ public void onError(@Nullable Exception e) { }; void updateCurrentAccountState() { - switch(mAccountManager.getAccountStatus()) { + switch(mAccounts.getAccountStatus()) { case NEEDS_RECONNECT: mBinding.signButton.setButtonText(R.string.settings_fxa_account_reconnect); break; case SIGNED_IN: mBinding.signButton.setButtonText(R.string.settings_fxa_account_sign_out); - Profile profile = mAccountManager.accountProfile(); + Profile profile = mAccounts.accountProfile(); if (profile != null) { updateProfile(profile); } else { - mAccountManager.updateProfileAsync().thenAcceptAsync((u) -> updateProfile(mAccountManager.accountProfile())); + Objects.requireNonNull(mAccounts.updateProfileAsync()).thenAcceptAsync((u) -> updateProfile(mAccounts.accountProfile())); } break; @@ -162,7 +142,7 @@ void updateCurrentAccountState() { break; default: - throw new IllegalStateException("Unexpected value: " + mAccountManager.getAccountStatus()); + throw new IllegalStateException("Unexpected value: " + mAccounts.getAccountStatus()); } } @@ -186,12 +166,12 @@ public void onProfileUpdated(@NotNull Profile profile) { @Override public void onLoggedOut() { - post(() -> FxAAccountOptionsView.this.onDismiss()); + post(FxAAccountOptionsView.this::onDismiss); } @Override public void onAuthenticationProblems() { - post(() -> FxAAccountOptionsView.this.onDismiss()); + post(FxAAccountOptionsView.this::onDismiss); } }; diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/SettingsWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/SettingsWidget.java index 3dfbdf709..5d191e680 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/SettingsWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/SettingsWidget.java @@ -26,8 +26,9 @@ import org.mozilla.gecko.util.ThreadUtils; import org.mozilla.vrbrowser.BuildConfig; import org.mozilla.vrbrowser.R; +import org.mozilla.vrbrowser.VRBrowserApplication; import org.mozilla.vrbrowser.audio.AudioEngine; -import org.mozilla.vrbrowser.browser.AccountsManager; +import org.mozilla.vrbrowser.browser.Accounts; import org.mozilla.vrbrowser.browser.engine.SessionStack; import org.mozilla.vrbrowser.browser.engine.SessionStore; import org.mozilla.vrbrowser.databinding.SettingsBinding; @@ -58,7 +59,7 @@ public class SettingsWidget extends UIDialog implements WidgetManagerDelegate.Wo private int mViewMarginV; private int mRestartDialogHandle = -1; private int mAlertDialogHandle = -1; - private AccountsManager mAccountManager; + private Accounts mAccounts; class VersionGestureListener extends GestureDetector.SimpleOnGestureListener { @@ -98,8 +99,8 @@ private void initialize() { mWidgetManager.addWorldClickListener(this); - mAccountManager = SessionStore.get().getAccountsManager(); - mAccountManager.addAccountListener(mAccountObserver); + mAccounts = ((VRBrowserApplication)getContext().getApplicationContext()).getAccounts(); + mAccounts.addAccountListener(mAccountObserver); mBinding.backButton.setOnClickListener(v -> { if (mAudio != null) { @@ -203,7 +204,7 @@ private void initialize() { @Override public void releaseWidget() { mWidgetManager.removeWorldClickListener(this); - mAccountManager.removeAccountListener(mAccountObserver); + mAccounts.removeAccountListener(mAccountObserver); super.releaseWidget(); } @@ -254,13 +255,13 @@ private void onSettingsReportClick() { } private void manageAccount() { - switch(mAccountManager.getAccountStatus()) { + switch(mAccounts.getAccountStatus()) { case SIGNED_OUT: case NEEDS_RECONNECT: - mAccountManager.getAuthenticationUrlAsync().thenAcceptAsync((url) -> { + mAccounts.getAuthenticationUrlAsync().thenAcceptAsync((url) -> { if (url != null) { post(() -> { - mAccountManager.setLoginOrigin(AccountsManager.LoginOrigin.SETTINGS); + mAccounts.setLoginOrigin(Accounts.LoginOrigin.SETTINGS); SessionStore.get().getActiveStore().loadUri(url); hide(REMOVE_WIDGET); }); @@ -275,19 +276,19 @@ private void manageAccount() { } private void updateCurrentAccountState() { - switch(mAccountManager.getAccountStatus()) { + switch(mAccounts.getAccountStatus()) { case NEEDS_RECONNECT: mBinding.fxaButton.setText(R.string.settings_fxa_account_reconnect); break; case SIGNED_IN: mBinding.fxaButton.setText(R.string.settings_fxa_account_manage); - updateProfile(mAccountManager.accountProfile()); + updateProfile(mAccounts.accountProfile()); break; case SIGNED_OUT: mBinding.fxaButton.setText(R.string.settings_fxa_account_sign_in); - updateProfile(mAccountManager.accountProfile()); + updateProfile(mAccounts.accountProfile()); break; } } diff --git a/app/src/main/res/values/non_L10n.xml b/app/src/main/res/values/non_L10n.xml index 12b084491..febf0d844 100644 --- a/app/src/main/res/values/non_L10n.xml +++ b/app/src/main/res/values/non_L10n.xml @@ -43,6 +43,8 @@ settings_key_debug_logging settings_key_autoplay settings_key_pid + settings_key_bookmarks_sync + settings_key_history_sync https://github.com/MozillaReality/FirefoxReality/wiki/Environments https://www.mozilla.org/privacy/firefox/ https://mixedreality.mozilla.org/fxr/report?src=browser-fxr&label=browser-firefox-reality&url=%1$s