From 014198f9cb0d2e9fcc0fe13c370b9bd8d3a9e850 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 15 Aug 2026 10:40:55 +0000 Subject: [PATCH] Fix crash launching the Authress Custom Tab from Application context MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AuthressLoginClient is built once in AppContainer with the Application context, never an Activity, so CustomTabsIntent.launchUrl needs FLAG_ACTIVITY_NEW_TASK set explicitly on its intent — without it, starting the activity throws. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01XRMBgZXVCEVwBuBWapy5J7 --- .../email/data/auth/AuthressLoginClient.kt | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/ch/rhosys/email/data/auth/AuthressLoginClient.kt b/app/src/main/java/ch/rhosys/email/data/auth/AuthressLoginClient.kt index eea89e0..fa92e3a 100644 --- a/app/src/main/java/ch/rhosys/email/data/auth/AuthressLoginClient.kt +++ b/app/src/main/java/ch/rhosys/email/data/auth/AuthressLoginClient.kt @@ -1,6 +1,7 @@ package ch.rhosys.email.data.auth import android.content.Context +import android.content.Intent import android.net.Uri import androidx.browser.customtabs.CustomTabsIntent import ch.rhosys.email.BuildConfig @@ -154,7 +155,7 @@ class AuthressLoginClient( ) withContext(Dispatchers.Main) { - CustomTabsIntent.Builder().build().launchUrl(context, Uri.parse(authenticationUrl)) + launchAuthenticationUrl(authenticationUrl) } }.onFailure { logger.error("Authress", "authenticate() failed", it) } @@ -213,6 +214,19 @@ class AuthressLoginClient( fun isRedirect(uri: Uri?): Boolean = uri != null && uri.toString().startsWith(redirectUri) + /** + * [context] here is always the Application context (this client is built + * once in [ch.rhosys.email.di.AppContainer], never per-Activity), so the + * Custom Tab intent needs FLAG_ACTIVITY_NEW_TASK explicitly — launchUrl + * doesn't add it, and starting an activity from a non-Activity context + * without it throws. + */ + private fun launchAuthenticationUrl(url: String) { + val customTabsIntent = CustomTabsIntent.Builder().build() + customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + customTabsIntent.launchUrl(context, Uri.parse(url)) + } + // ── session ───────────────────────────────────────────────────────────── /** @@ -329,7 +343,7 @@ class AuthressLoginClient( ) withContext(Dispatchers.Main) { - CustomTabsIntent.Builder().build().launchUrl(context, Uri.parse(authenticationUrl)) + launchAuthenticationUrl(authenticationUrl) } AuthenticationResponse(authenticationUrl, authenticationRequestId) }.onFailure { logger.error("Authress", "linkIdentity() failed", it) }