Fix billing client disconnection handling (#117)

This commit is contained in:
Matthias Urhahn
2023-03-13 17:28:01 +01:00
committed by GitHub
parent 88b0069b94
commit 8df7c19e84
@@ -56,26 +56,30 @@ class BillingClientConnectionProvider @Inject constructor(
"onBillingSetupFinished(code=${result.responseCode}, message=${result.debugMessage})" "onBillingSetupFinished(code=${result.responseCode}, message=${result.debugMessage})"
} }
val billingClientConnection = when (result.responseCode) { when (result.responseCode) {
BillingResponseCode.OK -> BillingClientConnection(client, purchasePublisher) BillingResponseCode.OK -> {
else -> throw BillingClientException(result) val connection = BillingClientConnection(client, purchasePublisher)
}
trySendBlocking(billingClientConnection) trySendBlocking(connection)
launch { launch {
try { try {
purchasePublisher.value = billingClientConnection.queryPurchases() purchasePublisher.value = connection.queryPurchases()
log(TAG) { "Initial IAP query successful." } log(TAG) { "Initial IAP query successful." }
} catch (e: Exception) { } catch (e: Exception) {
log(TAG, ERROR) { "Initial IAP query failed:\n${e.asLog()}" } log(TAG, ERROR) { "Initial IAP query failed:\n${e.asLog()}" }
}
}
}
else -> {
close(BillingClientException(result))
} }
} }
} }
override fun onBillingServiceDisconnected() { override fun onBillingServiceDisconnected() {
log(TAG, VERBOSE) { "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<BillingClientConnection> = connectionProvider val connection: Flow<BillingClientConnection> = connectionProvider
.setupCommonEventHandlers(TAG) { "connection" } .setupCommonEventHandlers(TAG) { "connection" }
.retryWhen { cause, attempt -> .retryWhen { cause, attempt ->
log(TAG) { "Billing client connection error: ${cause.asLog()}" }
if (cause is CancellationException) { if (cause is CancellationException) {
log(TAG) { "BillingClient connection cancelled." } log(TAG) { "BillingClient connection cancelled." }
return@retryWhen false 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 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." } log(TAG) { "Got BILLING_UNAVAILABLE while trying to connect client." }
return@retryWhen false 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*" } log(TAG) { "Will retry BillingClient connection... *sigh*" }
delay(3000 * attempt) delay(3000 * attempt)
true true