Implements billing service disconnection logic

dev
Laurent 7 years ago
parent ff8e2cdbae
commit 7448d73b19
  1. 34
      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)
})
}

Loading…
Cancel
Save