From 7448d73b19d2ef7a44f05e94697b42cb1bfecd31 Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 3 May 2019 15:29:09 +0200 Subject: [PATCH] Implements billing service disconnection logic --- .../android/util/billing/AppGuard.kt | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/net/pokeranalytics/android/util/billing/AppGuard.kt b/app/src/main/java/net/pokeranalytics/android/util/billing/AppGuard.kt index 93106650..0fb29408 100644 --- a/app/src/main/java/net/pokeranalytics/android/util/billing/AppGuard.kt +++ b/app/src/main/java/net/pokeranalytics/android/util/billing/AppGuard.kt @@ -35,28 +35,40 @@ object AppGuard : PurchasesUpdatedListener { billingClient = BillingClient.newBuilder(context).setListener(this).build() - this.restorePurchases() + this.startConnection(Runnable { + this.restorePurchases() + }) + + } + + private fun startConnection(executeOnSuccess: Runnable) { billingClient.startConnection(object : BillingClientStateListener { override fun onBillingSetupFinished(@BillingClient.BillingResponse billingResponseCode: Int) { if (billingResponseCode == BillingClient.BillingResponse.OK) { // The BillingClient is ready. You can query purchases here. - billingClientAvailable = true - + executeOnSuccess.run() } } override fun onBillingServiceDisconnected() { billingClientAvailable = false - - // Try to restart the connection on the next request to - // Google Play by calling the startConnection() method. } }) } + private fun executeServiceRequest(runnable: Runnable) { + + if (billingClientAvailable) { + runnable.run() + } else { + this.startConnection(runnable) + } + + } + /** * Restore purchases */ @@ -81,7 +93,10 @@ object AppGuard : PurchasesUpdatedListener { val params = SkuDetailsParams.newBuilder() params.setSkusList(skuList).setType(BillingClient.SkuType.SUBS) - billingClient.querySkuDetailsAsync(params.build(), listener) + this.executeServiceRequest(Runnable { + billingClient.querySkuDetailsAsync(params.build(), listener) + }) + return true } return false @@ -95,7 +110,10 @@ object AppGuard : PurchasesUpdatedListener { val flowParams = BillingFlowParams.newBuilder() .setSkuDetails(skuDetails) .build() - val responseCode = billingClient.launchBillingFlow(activity, flowParams) + + this.executeServiceRequest(Runnable { + val responseCode = billingClient.launchBillingFlow(activity, flowParams) + }) }