Start CustomTab from Activity

This commit is contained in:
Benoit Marty 2023-04-24 14:58:25 +02:00 committed by Benoit Marty
parent d33fbaf89f
commit dbc13a3a3c
3 changed files with 17 additions and 9 deletions

View file

@ -16,9 +16,12 @@
package io.element.android.features.login.impl package io.element.android.features.login.impl
import android.app.Activity
import android.os.Parcelable import android.os.Parcelable
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import com.bumble.appyx.core.composable.Children import com.bumble.appyx.core.composable.Children
import com.bumble.appyx.core.modality.BuildContext import com.bumble.appyx.core.modality.BuildContext
import com.bumble.appyx.core.node.Node import com.bumble.appyx.core.node.Node
@ -54,6 +57,8 @@ class LoginFlowNode @AssistedInject constructor(
buildContext = buildContext, buildContext = buildContext,
plugins = plugins, plugins = plugins,
) { ) {
private var activity: Activity? = null
sealed interface NavTarget : Parcelable { sealed interface NavTarget : Parcelable {
@Parcelize @Parcelize
object Root : NavTarget object Root : NavTarget
@ -76,7 +81,7 @@ class LoginFlowNode @AssistedInject constructor(
override fun onOidcDetails(oidcDetails: OidcDetails) { override fun onOidcDetails(oidcDetails: OidcDetails) {
if (customTabAvailabilityChecker.supportCustomTab()) { if (customTabAvailabilityChecker.supportCustomTab()) {
// In this case open a Chrome Custom tab // In this case open a Chrome Custom tab
customTabHandler.open(oidcDetails.url) activity?.let { customTabHandler.open(it, oidcDetails.url) }
} else { } else {
// Fallback to WebView mode // Fallback to WebView mode
backstack.push(NavTarget.OidcView(oidcDetails)) backstack.push(NavTarget.OidcView(oidcDetails))
@ -96,6 +101,12 @@ class LoginFlowNode @AssistedInject constructor(
@Composable @Composable
override fun View(modifier: Modifier) { override fun View(modifier: Modifier) {
activity = LocalContext.current as? Activity
DisposableEffect(lifecycle) {
onDispose {
activity = null
}
}
Children( Children(
navModel = backstack, navModel = backstack,
modifier = modifier, modifier = modifier,

View file

@ -16,6 +16,7 @@
package io.element.android.features.login.impl.oidc.customtab package io.element.android.features.login.impl.oidc.customtab
import android.app.Activity
import android.content.ComponentName import android.content.ComponentName
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
@ -71,7 +72,7 @@ class CustomTabHandler @Inject constructor(
customTabsServiceConnection = null customTabsServiceConnection = null
} }
fun open(url: String) { fun open(activity: Activity, url: String) {
context.openUrlInChromeCustomTab(customTabsSession, false, url) activity.openUrlInChromeCustomTab(customTabsSession, false, url)
} }
} }

View file

@ -16,9 +16,8 @@
package io.element.android.features.login.impl.oidc.customtab package io.element.android.features.login.impl.oidc.customtab
import android.app.Activity
import android.content.ActivityNotFoundException import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri import android.net.Uri
import androidx.browser.customtabs.CustomTabColorSchemeParams import androidx.browser.customtabs.CustomTabColorSchemeParams
import androidx.browser.customtabs.CustomTabsIntent 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. * If several compatible browsers are installed, the user will be proposed to choose one.
* Ref: https://developer.chrome.com/multidevice/android/customtabs. * Ref: https://developer.chrome.com/multidevice/android/customtabs.
*/ */
fun Context.openUrlInChromeCustomTab( fun Activity.openUrlInChromeCustomTab(
session: CustomTabsSession?, session: CustomTabsSession?,
darkTheme: Boolean, darkTheme: Boolean,
url: String url: String
@ -54,9 +53,6 @@ fun Context.openUrlInChromeCustomTab(
// .setExitAnimations(context, R.anim.enter_fade_in, R.anim.exit_fade_out) // .setExitAnimations(context, R.anim.enter_fade_in, R.anim.exit_fade_out)
.apply { session?.let { setSession(it) } } .apply { session?.let { setSession(it) } }
.build() .build()
.apply {
intent.flags += Intent.FLAG_ACTIVITY_NEW_TASK
}
.launchUrl(this, Uri.parse(url)) .launchUrl(this, Uri.parse(url))
} catch (activityNotFoundException: ActivityNotFoundException) { } catch (activityNotFoundException: ActivityNotFoundException) {
// TODO context.toast(R.string.error_no_external_application_found) // TODO context.toast(R.string.error_no_external_application_found)