From e7e26395ef48698f4c1b91ccafdc13ec31269630 Mon Sep 17 00:00:00 2001 From: Michael Shafrir Date: Wed, 10 Jun 2020 11:10:03 -0400 Subject: [PATCH] Fix StrictMode DiskReadViolation in StripePaymentController `transaction.authenticationRequestParameters` should be called off the main thread because it reads from disk --- .../stripe/android/StripePaymentController.kt | 65 ++++++++++++------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/stripe/src/main/java/com/stripe/android/StripePaymentController.kt b/stripe/src/main/java/com/stripe/android/StripePaymentController.kt index a56719cf375..439739d4372 100644 --- a/stripe/src/main/java/com/stripe/android/StripePaymentController.kt +++ b/stripe/src/main/java/com/stripe/android/StripePaymentController.kt @@ -38,6 +38,7 @@ import java.security.cert.CertificateException import java.util.concurrent.TimeUnit import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch /** * A controller responsible for confirming and authenticating payment (typically through resolving @@ -595,31 +596,45 @@ internal class StripePaymentController internal constructor( config.stripe3ds2Config.uiCustomization.uiCustomization ) - val areqParams = transaction.authenticationRequestParameters - val timeout = config.stripe3ds2Config.timeout - val authParams = Stripe3ds2AuthParams( - stripe3ds2Fingerprint.source, - areqParams.sdkAppId, - areqParams.sdkReferenceNumber, - areqParams.sdkTransactionId, - areqParams.deviceData, - areqParams.sdkEphemeralPublicKey, - areqParams.messageVersion, - timeout, - // We do not currently have a fallback url - // TODO(smaskell-stripe): Investigate more robust error handling - returnUrl = null - ) - stripeRepository.start3ds2Auth( - authParams, - stripeIntent.id.orEmpty(), - requestOptions, - Stripe3ds2AuthCallback( - host, stripeRepository, transaction, timeout, - stripeIntent, stripe3ds2Fingerprint.source, requestOptions, - analyticsRequestExecutor, analyticsDataFactory, - challengeFlowStarter, enableLogging) - ) + workScope.launch { + // call `authenticationRequestParameters` on background thread to avoid StrictMode + // DiskReadViolation + val areqParams = transaction.authenticationRequestParameters + + val timeout = config.stripe3ds2Config.timeout + val authParams = Stripe3ds2AuthParams( + stripe3ds2Fingerprint.source, + areqParams.sdkAppId, + areqParams.sdkReferenceNumber, + areqParams.sdkTransactionId, + areqParams.deviceData, + areqParams.sdkEphemeralPublicKey, + areqParams.messageVersion, + timeout, + // We do not currently have a fallback url + // TODO(smaskell-stripe): Investigate more robust error handling + returnUrl = null + ) + + stripeRepository.start3ds2Auth( + authParams, + stripeIntent.id.orEmpty(), + requestOptions, + Stripe3ds2AuthCallback( + host, + stripeRepository, + transaction, + timeout, + stripeIntent, + stripe3ds2Fingerprint.source, + requestOptions, + analyticsRequestExecutor, + analyticsDataFactory, + challengeFlowStarter, + enableLogging + ) + ) + } } private class ConfirmStripeIntentTask(