Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switching to use startActivity with flags instead of startActivityForResult #262

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,4 @@ interface AuthProviding {

/** Handles the authentication code received from the SSO flow via deeplink. */
fun handleAuthCode(authCode: String)

/** Checks if the authentication is in progress. */
fun isAuthInProgress(): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ class AuthActivity : AppCompatActivity() {
authProvider = AuthProvider(this, authContext)
}

private fun init() {
private fun startAuth() {
authProvider?.let {
authStarted = true
lifecycleScope.launch(Dispatchers.Main) {
when (val authResult = it.authenticate()) {
is AuthResult.Success -> {
Expand All @@ -76,32 +77,39 @@ class AuthActivity : AppCompatActivity() {

override fun onResume() {
super.onResume()

if (!authStarted) {
init()
authStarted = true
return
}
// Check if the intent has the auth code. This happens when user has authenticated using custom
// tabs
intent?.data?.let {
val authCode = it.getQueryParameter(KEY_AUTHENTICATION_CODE)
if (!authCode.isNullOrEmpty()) {
authProvider?.handleAuthCode(authCode)
} else {
// If the intent does not have the auth code, then the user denied the authentication
val error = it.getQueryParameter(KEY_ERROR) ?: CANCELED
finishAuthWithError(error)
}
}
// Check if the intent has the auth code.
intent?.data?.let { handleResponse(it) }
?: run {
if (authProvider?.isAuthInProgress() == true) {
// if intent does not have auth code and auth has not started then start the auth flow
if (!authStarted) {
startAuth()
return
}
// otherwise finish the auth flow with error
finishAuthWithError()
}
}

private fun handleResponse(uri: Uri) {
// This happens when user has authenticated using custom tabs
val authCode =
if (uri.getQueryParameter(KEY_AUTHENTICATION_CODE).isNullOrEmpty() == false) {
uri.getQueryParameter(KEY_AUTHENTICATION_CODE)
} else if (uri.fragment?.isNotEmpty() == true) {
// This happens when user has authenticated using 1p app
Uri.Builder().encodedQuery(uri.fragment).build().getQueryParameter(KEY_AUTHENTICATION_CODE)
} else {
""
}
if (!authCode.isNullOrEmpty()) {
authProvider?.handleAuthCode(authCode)
} else {
// If the intent does not have the auth code, then the user denied the authentication
val error = uri.getQueryParameter(KEY_ERROR) ?: CANCELED
finishAuthWithError(error)
}
}

private fun finishAuthWithError(error: String = CANCELED) {
// If the intent does not have the auth code, then the user has cancelled the authentication
intent.putExtra("EXTRA_ERROR", error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,4 @@ class AuthProvider(
override fun handleAuthCode(authCode: String) {
ssoLink.handleAuthCode(authCode)
}

override fun isAuthInProgress(): Boolean {
return ssoLink.isAuthInProgress()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ package com.uber.sdk2.auth.internal.sso
import android.content.Intent
import android.net.Uri
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatActivity.RESULT_CANCELED
Expand Down Expand Up @@ -60,17 +59,6 @@ internal class UniversalSsoLink(
) : SsoLink {

@VisibleForTesting val resultDeferred = CompletableDeferred<String>()
private var isAuthInProgress: Boolean = false

private val launcher =
activity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
try {
resultDeferred.complete(handleResult(result))
} catch (e: AuthException) {
resultDeferred.completeExceptionally(e)
}
isAuthInProgress = true
}

override suspend fun execute(optionalQueryParams: Map<String, String>): String {
val uri =
Expand All @@ -96,7 +84,9 @@ internal class UniversalSsoLink(
val intent = Intent()
intent.`package` = packageName
intent.data = uri
launcher.launch(intent)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
intent.putExtra(CALLING_PACKAGE, activity.packageName)
activity.startActivity(intent)
} ?: loadCustomtab(getSecureWebviewUri(uri))
}
is AuthDestination.InApp -> loadCustomtab(getSecureWebviewUri(uri))
Expand Down Expand Up @@ -129,10 +119,6 @@ internal class UniversalSsoLink(
}
}

override fun isAuthInProgress(): Boolean {
return isAuthInProgress
}

private fun loadCustomtab(uri: Uri) {
customTabsLauncher.launch(uri)
}
Expand All @@ -143,5 +129,6 @@ internal class UniversalSsoLink(

private const val EXTRA_CODE_RECEIVED = "CODE_RECEIVED"
private const val EXTRA_ERROR = "ERROR"
private const val CALLING_PACKAGE = "CALLING_APPLICATION_ID"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,4 @@ interface SsoLink {

/** Handles the authentication code received from the SSO flow via deeplink. */
fun handleAuthCode(authCode: String)

/** Checks if the authentication is in progress. */
fun isAuthInProgress(): Boolean
}
Loading