From 0e9c7bc15a5a906c50b981e974665c0e29871b47 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 24 Apr 2023 14:58:25 +0200 Subject: [PATCH] Start CustomTab from Activity --- .../android/features/login/impl/LoginFlowNode.kt | 13 ++++++++++++- .../login/impl/oidc/customtab/CustomTabHandler.kt | 5 +++-- .../login/impl/oidc/customtab/Extensions.kt | 8 ++------ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/LoginFlowNode.kt b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/LoginFlowNode.kt index 833bf4367b..d8f05832f9 100644 --- a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/LoginFlowNode.kt +++ b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/LoginFlowNode.kt @@ -16,9 +16,12 @@ package io.element.android.features.login.impl +import android.app.Activity import android.os.Parcelable import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext import com.bumble.appyx.core.composable.Children import com.bumble.appyx.core.modality.BuildContext import com.bumble.appyx.core.node.Node @@ -54,6 +57,8 @@ class LoginFlowNode @AssistedInject constructor( buildContext = buildContext, plugins = plugins, ) { + private var activity: Activity? = null + sealed interface NavTarget : Parcelable { @Parcelize object Root : NavTarget @@ -76,7 +81,7 @@ class LoginFlowNode @AssistedInject constructor( override fun onOidcDetails(oidcDetails: OidcDetails) { if (customTabAvailabilityChecker.supportCustomTab()) { // In this case open a Chrome Custom tab - customTabHandler.open(oidcDetails.url) + activity?.let { customTabHandler.open(it, oidcDetails.url) } } else { // Fallback to WebView mode backstack.push(NavTarget.OidcView(oidcDetails)) @@ -96,6 +101,12 @@ class LoginFlowNode @AssistedInject constructor( @Composable override fun View(modifier: Modifier) { + activity = LocalContext.current as? Activity + DisposableEffect(lifecycle) { + onDispose { + activity = null + } + } Children( navModel = backstack, modifier = modifier, diff --git a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/oidc/customtab/CustomTabHandler.kt b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/oidc/customtab/CustomTabHandler.kt index 4a84dc76a7..059757657d 100644 --- a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/oidc/customtab/CustomTabHandler.kt +++ b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/oidc/customtab/CustomTabHandler.kt @@ -16,6 +16,7 @@ package io.element.android.features.login.impl.oidc.customtab +import android.app.Activity import android.content.ComponentName import android.content.Context import android.net.Uri @@ -71,7 +72,7 @@ class CustomTabHandler @Inject constructor( customTabsServiceConnection = null } - fun open(url: String) { - context.openUrlInChromeCustomTab(customTabsSession, false, url) + fun open(activity: Activity, url: String) { + activity.openUrlInChromeCustomTab(customTabsSession, false, url) } } diff --git a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/oidc/customtab/Extensions.kt b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/oidc/customtab/Extensions.kt index 3321c8979a..be98566e7c 100644 --- a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/oidc/customtab/Extensions.kt +++ b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/oidc/customtab/Extensions.kt @@ -16,9 +16,8 @@ package io.element.android.features.login.impl.oidc.customtab +import android.app.Activity import android.content.ActivityNotFoundException -import android.content.Context -import android.content.Intent import android.net.Uri import androidx.browser.customtabs.CustomTabColorSchemeParams import androidx.browser.customtabs.CustomTabsIntent @@ -29,7 +28,7 @@ import androidx.browser.customtabs.CustomTabsSession * If several compatible browsers are installed, the user will be proposed to choose one. * Ref: https://developer.chrome.com/multidevice/android/customtabs. */ -fun Context.openUrlInChromeCustomTab( +fun Activity.openUrlInChromeCustomTab( session: CustomTabsSession?, darkTheme: Boolean, url: String @@ -54,9 +53,6 @@ fun Context.openUrlInChromeCustomTab( // .setExitAnimations(context, R.anim.enter_fade_in, R.anim.exit_fade_out) .apply { session?.let { setSession(it) } } .build() - .apply { - intent.flags += Intent.FLAG_ACTIVITY_NEW_TASK - } .launchUrl(this, Uri.parse(url)) } catch (activityNotFoundException: ActivityNotFoundException) { // TODO context.toast(R.string.error_no_external_application_found)