From 8df7c19e84cc49599ebd4576dc5588946fb48002 Mon Sep 17 00:00:00 2001 From: Matthias Urhahn Date: Mon, 13 Mar 2023 17:28:01 +0100 Subject: [PATCH] Fix billing client disconnection handling (#117) --- .../client/BillingClientConnectionProvider.kt | 48 +++++++++++-------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/app/src/gplay/java/eu/darken/capod/common/upgrade/core/client/BillingClientConnectionProvider.kt b/app/src/gplay/java/eu/darken/capod/common/upgrade/core/client/BillingClientConnectionProvider.kt index 1009e324..80b9cf4f 100644 --- a/app/src/gplay/java/eu/darken/capod/common/upgrade/core/client/BillingClientConnectionProvider.kt +++ b/app/src/gplay/java/eu/darken/capod/common/upgrade/core/client/BillingClientConnectionProvider.kt @@ -56,26 +56,30 @@ class BillingClientConnectionProvider @Inject constructor( "onBillingSetupFinished(code=${result.responseCode}, message=${result.debugMessage})" } - val billingClientConnection = when (result.responseCode) { - BillingResponseCode.OK -> BillingClientConnection(client, purchasePublisher) - else -> throw BillingClientException(result) - } + when (result.responseCode) { + BillingResponseCode.OK -> { + val connection = BillingClientConnection(client, purchasePublisher) - trySendBlocking(billingClientConnection) + trySendBlocking(connection) - launch { - try { - purchasePublisher.value = billingClientConnection.queryPurchases() - log(TAG) { "Initial IAP query successful." } - } catch (e: Exception) { - log(TAG, ERROR) { "Initial IAP query failed:\n${e.asLog()}" } + launch { + try { + purchasePublisher.value = connection.queryPurchases() + log(TAG) { "Initial IAP query successful." } + } catch (e: Exception) { + log(TAG, ERROR) { "Initial IAP query failed:\n${e.asLog()}" } + } + } + } + else -> { + close(BillingClientException(result)) } } } override fun onBillingServiceDisconnected() { log(TAG, VERBOSE) { "onBillingServiceDisconnected() " } - error(BillingException("Billing service disconnected")) + close(BillingException("Billing service disconnected")) } }) @@ -89,26 +93,28 @@ class BillingClientConnectionProvider @Inject constructor( val connection: Flow = connectionProvider .setupCommonEventHandlers(TAG) { "connection" } .retryWhen { cause, attempt -> + log(TAG) { "Billing client connection error: ${cause.asLog()}" } + if (cause is CancellationException) { log(TAG) { "BillingClient connection cancelled." } return@retryWhen false } - if (attempt > 5) { - log(TAG, WARN) { "Reached attempt limit: $attempt due to $cause" } + + if (cause !is BillingException) { + log(TAG, WARN) { "Unknown exception type: $cause" } return@retryWhen false } - if (cause !is BillingClientException) { - log(TAG, WARN) { "Unknown BillingClient exception type: $cause" } - return@retryWhen false - } else { - log(TAG) { "BillingClient exception: $cause; ${cause.result}" } - } - if (cause.result.responseCode == BillingResponseCode.BILLING_UNAVAILABLE) { + if (cause is BillingClientException && cause.result.responseCode == BillingResponseCode.BILLING_UNAVAILABLE) { log(TAG) { "Got BILLING_UNAVAILABLE while trying to connect client." } return@retryWhen false } + if (attempt > 5) { + log(TAG, WARN) { "Reached attempt limit: $attempt due to $cause" } + return@retryWhen false + } + log(TAG) { "Will retry BillingClient connection... *sigh*" } delay(3000 * attempt) true