From e592609d8380ee24861e5139bc151a2395c0cc5d Mon Sep 17 00:00:00 2001 From: pranaysashank Date: Mon, 2 Oct 2023 18:56:42 +0530 Subject: [PATCH] Do not get the fingerprint on the main thread --- .../main/java/io/kronor/example/MainActivity.kt | 14 +++++++++++++- .../io/kronor/component/swish/SwishComponent.kt | 11 +++++++++-- .../WebviewGatewayComponent.kt | 14 ++++++++++++-- .../WebviewGatewayViewModel.kt | 1 - 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/example-app/src/main/java/io/kronor/example/MainActivity.kt b/example-app/src/main/java/io/kronor/example/MainActivity.kt index cf89517..b0ba79f 100644 --- a/example-app/src/main/java/io/kronor/example/MainActivity.kt +++ b/example-app/src/main/java/io/kronor/example/MainActivity.kt @@ -4,6 +4,8 @@ import android.content.Intent import android.net.Uri import android.os.Build import android.os.Bundle +import android.os.StrictMode +import android.os.StrictMode.ThreadPolicy import android.util.Log import androidx.activity.ComponentActivity import androidx.activity.compose.setContent @@ -32,7 +34,6 @@ import androidx.navigation.navArgument import io.kronor.api.Environment import io.kronor.api.PaymentConfiguration import io.kronor.api.PaymentEvent -import io.kronor.api.PaymentMethod import io.kronor.component.credit_card.CreditCardComponent import io.kronor.component.credit_card.creditCardViewModel import io.kronor.component.mobilepay.MobilePayComponent @@ -49,12 +50,23 @@ import io.kronor.example.ui.theme.KronorSDKTheme import kotlinx.coroutines.* import java.util.* + class MainActivity : ComponentActivity() { private val viewModel: MainViewModel by viewModels() @RequiresApi(Build.VERSION_CODES.O) override fun onCreate(savedInstanceState: Bundle?) { + StrictMode.setThreadPolicy( + ThreadPolicy.Builder() + .detectDiskReads() + .detectDiskWrites() + .detectAll() //for all detectable problems + + .penaltyLog() + .build() + ) + StrictMode.noteSlowCall("SlowCall") super.onCreate(savedInstanceState) Log.d("onCreate", "${viewModel.paymentSessionToken}") setContent { diff --git a/kronor_swish/src/main/java/io/kronor/component/swish/SwishComponent.kt b/kronor_swish/src/main/java/io/kronor/component/swish/SwishComponent.kt index 6d2c479..ca7de85 100644 --- a/kronor_swish/src/main/java/io/kronor/component/swish/SwishComponent.kt +++ b/kronor_swish/src/main/java/io/kronor/component/swish/SwishComponent.kt @@ -73,7 +73,10 @@ import com.google.zxing.qrcode.QRCodeWriter import io.kronor.api.KronorError import io.kronor.api.PaymentConfiguration import io.kronor.api.PaymentStatusSubscription +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.newSingleThreadContext +import kotlinx.coroutines.withContext import java.security.MessageDigest import java.util.UUID @@ -90,7 +93,9 @@ fun SwishComponent( if (!LocalInspectionMode.current) { LaunchedEffect(Unit) { - viewModel.setDeviceFingerPrint(getWeakFingerprint(context)) + withContext(Dispatchers.Default) { + viewModel.setDeviceFingerPrint(getWeakFingerprint(context)) + } } } @@ -99,7 +104,9 @@ fun SwishComponent( LaunchedEffect(viewModel.subscribe) { if (viewModel.subscribe) { lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { - viewModel.subscription() + withContext(Dispatchers.IO) { + viewModel.subscription() + } } } } diff --git a/kronor_webview_payment_gateway/src/main/java/io/kronor/component/webview_payment_gateway/WebviewGatewayComponent.kt b/kronor_webview_payment_gateway/src/main/java/io/kronor/component/webview_payment_gateway/WebviewGatewayComponent.kt index 396bcc5..b83f92f 100644 --- a/kronor_webview_payment_gateway/src/main/java/io/kronor/component/webview_payment_gateway/WebviewGatewayComponent.kt +++ b/kronor_webview_payment_gateway/src/main/java/io/kronor/component/webview_payment_gateway/WebviewGatewayComponent.kt @@ -9,6 +9,7 @@ import android.database.Cursor import android.media.MediaDrm import android.net.Uri import android.os.Build +import android.os.Looper import android.provider.Settings import android.util.Log import android.view.ViewGroup @@ -33,8 +34,13 @@ import androidx.lifecycle.Lifecycle.Event.* import androidx.lifecycle.repeatOnLifecycle import io.kronor.api.KronorError import io.kronor.api.PaymentMethod +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.newSingleThreadContext +import kotlinx.coroutines.withContext +import okhttp3.Dispatcher import java.lang.Exception +import java.lang.Thread.sleep import java.security.MessageDigest import java.util.UUID @@ -47,7 +53,9 @@ fun WebviewGatewayComponent( if (!LocalInspectionMode.current) { LaunchedEffect(Unit) { - viewModel.setDeviceFingerPrint(getWeakFingerprint(context)) + withContext(Dispatchers.Default) { + viewModel.setDeviceFingerPrint(getWeakFingerprint(context)) + } } val lifecycle = LocalLifecycleOwner.current.lifecycle @@ -55,7 +63,9 @@ fun WebviewGatewayComponent( LaunchedEffect(viewModel.subscribe) { if (viewModel.subscribe) { lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { - viewModel.subscription() + withContext(Dispatchers.IO) { + viewModel.subscription() + } } } } diff --git a/kronor_webview_payment_gateway/src/main/java/io/kronor/component/webview_payment_gateway/WebviewGatewayViewModel.kt b/kronor_webview_payment_gateway/src/main/java/io/kronor/component/webview_payment_gateway/WebviewGatewayViewModel.kt index 3d8b89f..85c1003 100644 --- a/kronor_webview_payment_gateway/src/main/java/io/kronor/component/webview_payment_gateway/WebviewGatewayViewModel.kt +++ b/kronor_webview_payment_gateway/src/main/java/io/kronor/component/webview_payment_gateway/WebviewGatewayViewModel.kt @@ -81,7 +81,6 @@ class WebviewGatewayViewModel( viewModelScope.launch { withContext(Dispatchers.IO) { _transition(event) - } } }