From f23f3b74895eb07d13bb5412a8709c220524d38d Mon Sep 17 00:00:00 2001 From: Grisha Kruglov Date: Thu, 1 Aug 2019 15:40:43 -0700 Subject: [PATCH] Closes #4467: Use a-c version of FirefoxAccountsAuthFeature --- .../org/mozilla/fenix/components/Services.kt | 13 ++- .../features/FirefoxAccountsAuthFeature.kt | 81 ------------------- 2 files changed, 11 insertions(+), 83 deletions(-) delete mode 100644 app/src/main/java/org/mozilla/fenix/components/features/FirefoxAccountsAuthFeature.kt diff --git a/app/src/main/java/org/mozilla/fenix/components/Services.kt b/app/src/main/java/org/mozilla/fenix/components/Services.kt index f070d038d..8d066b1f6 100644 --- a/app/src/main/java/org/mozilla/fenix/components/Services.kt +++ b/app/src/main/java/org/mozilla/fenix/components/Services.kt @@ -6,14 +6,18 @@ package org.mozilla.fenix.components import android.content.Context import androidx.navigation.NavController +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import mozilla.components.feature.accounts.FirefoxAccountsAuthFeature import mozilla.components.service.fxa.manager.FxaAccountManager import mozilla.components.support.ktx.android.content.hasCamera import org.mozilla.fenix.Experiments import org.mozilla.fenix.NavGraphDirections -import org.mozilla.fenix.components.features.FirefoxAccountsAuthFeature import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.ext.components import org.mozilla.fenix.isInExperiment +import org.mozilla.fenix.settings.SupportUtils import org.mozilla.fenix.test.Mockable /** @@ -27,7 +31,12 @@ class Services( FirefoxAccountsAuthFeature( accountManager, redirectUrl = BackgroundServices.REDIRECT_URL - ) + ) { context, authUrl -> + CoroutineScope(Dispatchers.Main).launch { + val intent = SupportUtils.createAuthCustomTabIntent(context, authUrl) + context.startActivity(intent) + } + } } /** diff --git a/app/src/main/java/org/mozilla/fenix/components/features/FirefoxAccountsAuthFeature.kt b/app/src/main/java/org/mozilla/fenix/components/features/FirefoxAccountsAuthFeature.kt deleted file mode 100644 index 33c5dc5df..000000000 --- a/app/src/main/java/org/mozilla/fenix/components/features/FirefoxAccountsAuthFeature.kt +++ /dev/null @@ -1,81 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package org.mozilla.fenix.components.features - -import android.content.Context -import android.net.Uri -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import mozilla.components.concept.engine.EngineSession -import mozilla.components.concept.engine.request.RequestInterceptor -import mozilla.components.service.fxa.manager.FxaAccountManager -import org.mozilla.fenix.settings.SupportUtils -import kotlin.coroutines.CoroutineContext - -// The FirefoxAccountsAuthFeature provided by android components only worked with -// the tabs use case. We want to use our custom tab feature to improve the UX for both -// signing in through settings and onboarding. - -// We're temporarily copying and creating our own: -// https://github.com/mozilla-mobile/android-components/issues/3272 -class FirefoxAccountsAuthFeature( - private val accountManager: FxaAccountManager, - private val redirectUrl: String, - private val coroutineContext: CoroutineContext = Dispatchers.Main -) { - fun beginAuthentication(context: Context) { - beginAuthenticationAsync(context) { - accountManager.beginAuthenticationAsync().await() - } - } - - fun beginPairingAuthentication(context: Context, pairingUrl: String) { - beginAuthenticationAsync(context) { - accountManager.beginAuthenticationAsync(pairingUrl).await() - } - } - - private fun beginAuthenticationAsync(context: Context, beginAuthentication: suspend () -> String?) { - CoroutineScope(coroutineContext).launch { - // FIXME return a fallback URL provided by Config... - // https://github.com/mozilla-mobile/android-components/issues/2496 - val authUrl = beginAuthentication() ?: FALLBACK_URL - - // TODO - // We may fail to obtain an authentication URL, for example due to transient network errors. - // If that happens, open up a fallback URL in order to present some kind of a "no network" - // UI to the user. - // It's possible that the underlying problem will go away by the time the tab actually - // loads, resulting in a confusing experience. - val intent = SupportUtils.createAuthCustomTabIntent(context, authUrl) - context.startActivity(intent) - } - } - - val interceptor = object : RequestInterceptor { - override fun onLoadRequest(session: EngineSession, uri: String): RequestInterceptor.InterceptionResponse? { - if (uri.startsWith(redirectUrl)) { - val parsedUri = Uri.parse(uri) - val code = parsedUri.getQueryParameter("code") - - if (code != null) { - val state = parsedUri.getQueryParameter("state") as String - - // Notify the state machine about our success. - accountManager.finishAuthenticationAsync(code, state) - - return RequestInterceptor.InterceptionResponse.Url(redirectUrl) - } - } - - return null - } - } - - companion object { - private const val FALLBACK_URL = "https://accounts.firefox.com/signin" - } -}