2019-02-15 23:44:43 +01:00
|
|
|
/* 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
|
|
|
|
|
2019-07-26 04:55:51 +02:00
|
|
|
import android.content.Context
|
|
|
|
import androidx.navigation.NavController
|
2020-01-02 19:08:11 +01:00
|
|
|
import androidx.preference.PreferenceManager
|
2019-08-02 00:40:43 +02:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import mozilla.components.feature.accounts.FirefoxAccountsAuthFeature
|
2019-12-20 14:27:19 +01:00
|
|
|
import mozilla.components.feature.app.links.AppLinksInterceptor
|
2019-05-08 18:04:06 +02:00
|
|
|
import mozilla.components.service.fxa.manager.FxaAccountManager
|
2019-07-26 04:55:51 +02:00
|
|
|
import mozilla.components.support.ktx.android.content.hasCamera
|
2020-01-13 19:38:32 +01:00
|
|
|
import org.mozilla.fenix.FeatureFlags
|
2019-07-26 04:55:51 +02:00
|
|
|
import org.mozilla.fenix.NavGraphDirections
|
2019-12-20 14:27:19 +01:00
|
|
|
import org.mozilla.fenix.R
|
2019-07-26 04:55:51 +02:00
|
|
|
import org.mozilla.fenix.components.metrics.Event
|
|
|
|
import org.mozilla.fenix.ext.components
|
2019-12-20 14:27:19 +01:00
|
|
|
import org.mozilla.fenix.ext.getPreferenceKey
|
2019-08-02 00:40:43 +02:00
|
|
|
import org.mozilla.fenix.settings.SupportUtils
|
2019-05-07 23:36:37 +02:00
|
|
|
import org.mozilla.fenix.test.Mockable
|
2019-02-15 23:44:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component group which encapsulates foreground-friendly services.
|
|
|
|
*/
|
2019-05-07 23:36:37 +02:00
|
|
|
@Mockable
|
2019-02-15 23:44:43 +01:00
|
|
|
class Services(
|
2019-08-23 01:56:13 +02:00
|
|
|
private val context: Context,
|
2019-06-05 00:57:24 +02:00
|
|
|
private val accountManager: FxaAccountManager
|
2019-02-15 23:44:43 +01:00
|
|
|
) {
|
2019-12-20 14:27:19 +01:00
|
|
|
|
2019-07-11 16:23:35 +02:00
|
|
|
val fxaRedirectUrl = FxaServer.redirectUrl(context)
|
|
|
|
|
2019-02-15 23:44:43 +01:00
|
|
|
val accountsAuthFeature by lazy {
|
|
|
|
FirefoxAccountsAuthFeature(
|
|
|
|
accountManager,
|
2019-07-11 16:23:35 +02:00
|
|
|
redirectUrl = fxaRedirectUrl
|
2019-08-02 00:40:43 +02:00
|
|
|
) { context, authUrl ->
|
|
|
|
CoroutineScope(Dispatchers.Main).launch {
|
|
|
|
val intent = SupportUtils.createAuthCustomTabIntent(context, authUrl)
|
|
|
|
context.startActivity(intent)
|
|
|
|
}
|
|
|
|
}
|
2019-02-15 23:44:43 +01:00
|
|
|
}
|
2019-07-26 04:55:51 +02:00
|
|
|
|
2019-12-20 14:27:19 +01:00
|
|
|
val appLinksInterceptor by lazy {
|
|
|
|
AppLinksInterceptor(
|
|
|
|
context,
|
|
|
|
interceptLinkClicks = true,
|
|
|
|
launchInApp = {
|
2020-01-02 19:08:11 +01:00
|
|
|
PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
|
2019-12-20 14:27:19 +01:00
|
|
|
context.getPreferenceKey(R.string.pref_key_open_links_in_external_app), false)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:55:51 +02:00
|
|
|
/**
|
|
|
|
* Launches the sign in and pairing custom tab from any screen in the app.
|
|
|
|
* @param context the current Context
|
|
|
|
* @param navController the navController to use for navigation
|
|
|
|
*/
|
|
|
|
fun launchPairingSignIn(context: Context, navController: NavController) {
|
|
|
|
// Do not navigate to pairing UI if camera not available or pairing is disabled
|
2020-01-13 19:38:32 +01:00
|
|
|
if (context.hasCamera() && !FeatureFlags.asFeatureFxAPairingDisabled) {
|
2019-07-26 04:55:51 +02:00
|
|
|
val directions = NavGraphDirections.actionGlobalTurnOnSync()
|
|
|
|
navController.navigate(directions)
|
|
|
|
} else {
|
|
|
|
context.components.services.accountsAuthFeature.beginAuthentication(context)
|
|
|
|
// TODO The sign-in web content populates session history,
|
|
|
|
// so pressing "back" after signing in won't take us back into the settings screen, but rather up the
|
|
|
|
// session history stack.
|
|
|
|
// We could auto-close this tab once we get to the end of the authentication process?
|
|
|
|
// Via an interceptor, perhaps.
|
|
|
|
context.components.analytics.metrics.track(Event.SyncAuthSignIn)
|
|
|
|
}
|
|
|
|
}
|
2019-02-15 23:44:43 +01:00
|
|
|
}
|