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

Commit

Permalink
Get rid of a complex getAuthicationUrlAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Nov 4, 2019
1 parent 6ef86e1 commit c23f74e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 89 deletions.
50 changes: 0 additions & 50 deletions app/src/common/shared/org/mozilla/vrbrowser/browser/Accounts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ import mozilla.components.service.fxa.manager.SyncEnginesStorage
import mozilla.components.service.fxa.sync.SyncReason
import mozilla.components.service.fxa.sync.SyncStatusObserver
import mozilla.components.service.fxa.sync.getLastSynced
import mozilla.components.support.base.log.logger.Logger
import org.mozilla.vrbrowser.VRBrowserApplication
import org.mozilla.vrbrowser.utils.SystemUtils
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ExecutionException
import java.util.concurrent.Executors

class Accounts constructor(val context: Context) {

Expand Down Expand Up @@ -258,53 +255,6 @@ class Accounts constructor(val context: Context) {
}
}

fun getAuthenticationUrlAsync(): CompletableFuture<String> {
val future: CompletableFuture<String> = CompletableFuture()

// If we're already logged-in, and not in a "need to reconnect" state, logout.
if (services.accountManager.authenticatedAccount() != null && !services.accountManager.accountNeedsReauth()) {
services.accountManager.logoutAsync()
future.complete(null)
}

// Otherwise, obtain an authentication URL and load it in the gecko session.
// Recovering from "need to reconnect" state is treated the same as just logging in.
val futureUrl = authUrlAsync()
if (futureUrl == null) {
Logger(LOGTAG).debug("Got a 'null' futureUrl")
services.accountManager.logoutAsync()
future.complete(null)
}

Executors.newSingleThreadExecutor().submit {
try {
val url = futureUrl!!.get()
if (url == null) {
Logger(LOGTAG).debug("Got a 'null' url after resolving futureUrl")
services.accountManager.logoutAsync()
future.complete(null)
}
Logger(LOGTAG).debug("Got an auth url: " + url!!)
// Actually process the url on the main thread.
Handler(Looper.getMainLooper()).post {
Logger(LOGTAG).debug("We got an authentication url, we can continue...")
future.complete(url)
}
} catch (e: ExecutionException) {
Logger(LOGTAG).debug("Error obtaining auth url", e)
future.complete(null)
} catch (e: InterruptedException) {
Logger(LOGTAG).debug("Error obtaining auth url", e)
future.complete(null)
}
}

return future
}

fun isEngineEnabled(engine: SyncEngine): Boolean {
return syncStorage.getStatus()[engine]?: false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,23 @@ public void onSyncBookmarks(@NonNull View view) {

@Override
public void onFxALogin(@NonNull View view) {
mAccounts.getAuthenticationUrlAsync().thenAcceptAsync((url) -> {
if (url != null) {
mAccounts.setLoginOrigin(Accounts.LoginOrigin.BOOKMARKS);
WidgetManagerDelegate widgetManager = ((VRBrowserActivity)getContext());
widgetManager.openNewTabForeground(url);
widgetManager.getFocusedWindow().getSession().setUaMode(GeckoSessionSettings.USER_AGENT_MODE_MOBILE);
}
}, new UIThreadExecutor());
if (mAccounts.getAccountStatus() == Accounts.AccountStatus.SIGNED_IN) {
mAccounts.logoutAsync();

} else {
mAccounts.authUrlAsync().thenAcceptAsync((url) -> {
if (url == null) {
mAccounts.logoutAsync();

} else {
mAccounts.setLoginOrigin(Accounts.LoginOrigin.BOOKMARKS);
WidgetManagerDelegate widgetManager = ((VRBrowserActivity)getContext());
widgetManager.openNewTabForeground(url);
widgetManager.getFocusedWindow().getSession().setUaMode(GeckoSessionSettings.USER_AGENT_MODE_MOBILE);
}

}, new UIThreadExecutor());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,23 @@ public void onSyncHistory(@NonNull View view) {

@Override
public void onFxALogin(@NonNull View view) {
mAccounts.getAuthenticationUrlAsync().thenAcceptAsync((url) -> {
if (url != null) {
mAccounts.setLoginOrigin(Accounts.LoginOrigin.HISTORY);
WidgetManagerDelegate widgetManager = ((VRBrowserActivity)getContext());
widgetManager.openNewTabForeground(url);
widgetManager.getFocusedWindow().getSession().setUaMode(GeckoSessionSettings.USER_AGENT_MODE_MOBILE);
}
}, new UIThreadExecutor());
if (mAccounts.getAccountStatus() == Accounts.AccountStatus.SIGNED_IN) {
mAccounts.logoutAsync();

} else {
mAccounts.authUrlAsync().thenAcceptAsync((url) -> {
if (url == null) {
mAccounts.logoutAsync();

} else {
mAccounts.setLoginOrigin(Accounts.LoginOrigin.HISTORY);
WidgetManagerDelegate widgetManager = ((VRBrowserActivity)getContext());
widgetManager.openNewTabForeground(url);
widgetManager.getFocusedWindow().getSession().setUaMode(GeckoSessionSettings.USER_AGENT_MODE_MOBILE);
}

}, new UIThreadExecutor());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

Expand Down Expand Up @@ -95,19 +94,23 @@ public void hide(@HideFlags int aHideFlags) {
}

private void signIn(View view) {
mAccounts.getAuthenticationUrlAsync().thenAcceptAsync((url) -> {
if (url != null) {
mAccounts.setLoginOrigin(mLoginOrigin);
mWidgetManager.openNewTabForeground(url);
mWidgetManager.getFocusedWindow().getSession().setUaMode(GeckoSessionSettings.USER_AGENT_MODE_VR);
mWidgetManager.getFocusedWindow().getSession().loadUri(url);
}

if (mSignInCallback != null) {
mSignInCallback.run();
}

}, new UIThreadExecutor());
if (mAccounts.getAccountStatus() == Accounts.AccountStatus.SIGNED_IN) {
mAccounts.logoutAsync();

} else {
mAccounts.authUrlAsync().thenAcceptAsync((url) -> {
if (url == null) {
mAccounts.logoutAsync();

} else {
mAccounts.setLoginOrigin(mLoginOrigin);
mWidgetManager.openNewTabForeground(url);
mWidgetManager.getFocusedWindow().getSession().setUaMode(GeckoSessionSettings.USER_AGENT_MODE_VR);
mWidgetManager.getFocusedWindow().getSession().loadUri(url);
}

}, new UIThreadExecutor());
}
}

private void startBrowsing(View view) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,24 @@ private void manageAccount() {
switch(mAccounts.getAccountStatus()) {
case SIGNED_OUT:
case NEEDS_RECONNECT:
mAccounts.getAuthenticationUrlAsync().thenAcceptAsync((url) -> {
if (url != null) {
mAccounts.setLoginOrigin(Accounts.LoginOrigin.SETTINGS);
WidgetManagerDelegate widgetManager = ((VRBrowserActivity)getContext());
widgetManager.openNewTabForeground(url);
widgetManager.getFocusedWindow().getSession().setUaMode(GeckoSessionSettings.USER_AGENT_MODE_MOBILE);
hide(REMOVE_WIDGET);
}
}, new UIThreadExecutor());
if (mAccounts.getAccountStatus() == Accounts.AccountStatus.SIGNED_IN) {
mAccounts.logoutAsync();

} else {
mAccounts.authUrlAsync().thenAcceptAsync((url) -> {
if (url == null) {
mAccounts.logoutAsync();

} else {
mAccounts.setLoginOrigin(Accounts.LoginOrigin.SETTINGS);
WidgetManagerDelegate widgetManager = ((VRBrowserActivity)getContext());
widgetManager.openNewTabForeground(url);
widgetManager.getFocusedWindow().getSession().setUaMode(GeckoSessionSettings.USER_AGENT_MODE_MOBILE);
hide(REMOVE_WIDGET);
}

}, new UIThreadExecutor());
}
break;

case SIGNED_IN:
Expand Down

0 comments on commit c23f74e

Please sign in to comment.