diff options
| author | David Göransson <david.goransson@mullvad.net> | 2024-09-10 15:52:53 +0200 |
|---|---|---|
| committer | David Göransson <david.goransson@mullvad.net> | 2024-09-10 15:52:53 +0200 |
| commit | 409b4ce7ee6c4bb101e39c6593b3a008a588c22a (patch) | |
| tree | 5304948fcb24f1ba1dc5a6f98a620a61e23485bc | |
| parent | 7434494335236e97687b8cd29e4da939950c196a (diff) | |
| parent | 232d74757613a5d3e8ddfcb1bd478957b1b4b732 (diff) | |
| download | mullvadvpn-409b4ce7ee6c4bb101e39c6593b3a008a588c22a.tar.xz mullvadvpn-409b4ce7ee6c4bb101e39c6593b3a008a588c22a.zip | |
Merge branch 'illegalstateexception-in-droid-887'
| -rw-r--r-- | android/lib/billing/src/main/kotlin/net/mullvad/mullvadvpn/lib/billing/BillingRepository.kt | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/android/lib/billing/src/main/kotlin/net/mullvad/mullvadvpn/lib/billing/BillingRepository.kt b/android/lib/billing/src/main/kotlin/net/mullvad/mullvadvpn/lib/billing/BillingRepository.kt index 544c348170..9ccd1d77ff 100644 --- a/android/lib/billing/src/main/kotlin/net/mullvad/mullvadvpn/lib/billing/BillingRepository.kt +++ b/android/lib/billing/src/main/kotlin/net/mullvad/mullvadvpn/lib/billing/BillingRepository.kt @@ -16,12 +16,12 @@ import com.android.billingclient.api.QueryProductDetailsParams.Product import com.android.billingclient.api.QueryPurchasesParams import com.android.billingclient.api.queryProductDetails import com.android.billingclient.api.queryPurchasesAsync -import kotlin.coroutines.Continuation import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException -import kotlin.coroutines.suspendCoroutine +import kotlinx.coroutines.CancellableContinuation import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.asSharedFlow +import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import net.mullvad.mullvadvpn.lib.billing.model.BillingException @@ -71,19 +71,21 @@ class BillingRepository(context: Context) { private suspend fun ensureConnected() = ensureConnectedMutex.withLock { - suspendCoroutine { + suspendCancellableCoroutine { if ( billingClient.isReady && billingClient.connectionState == BillingClient.ConnectionState.CONNECTED ) { - it.resume(Unit) + if (it.isActive) { + it.resume(Unit) + } } else { startConnection(it) } } } - private fun startConnection(continuation: Continuation<Unit>) { + private fun startConnection(continuation: CancellableContinuation<Unit>) { billingClient.startConnection( object : BillingClientStateListener { override fun onBillingServiceDisconnected() { @@ -92,11 +94,15 @@ class BillingRepository(context: Context) { override fun onBillingSetupFinished(result: BillingResult) { if (result.responseCode == BillingResponseCode.OK) { - continuation.resume(Unit) + if (continuation.isActive) { + continuation.resume(Unit) + } } else { - continuation.resumeWithException( - BillingException(result.responseCode, result.debugMessage) - ) + if (continuation.isActive) { + continuation.resumeWithException( + BillingException(result.responseCode, result.debugMessage) + ) + } } } } |
