|
|
|
|
@ -5,6 +5,7 @@ import android.content.Context |
|
|
|
|
import android.os.Handler |
|
|
|
|
import android.os.Looper |
|
|
|
|
import com.android.billingclient.api.* |
|
|
|
|
import com.android.billingclient.api.BillingClient.BillingResponseCode |
|
|
|
|
import net.pokeranalytics.android.BuildConfig |
|
|
|
|
import net.pokeranalytics.android.R |
|
|
|
|
import timber.log.Timber |
|
|
|
|
@ -99,7 +100,7 @@ object AppGuard : PurchasesUpdatedListener { |
|
|
|
|
*/ |
|
|
|
|
fun load(context: Context) { |
|
|
|
|
|
|
|
|
|
billingClient = BillingClient.newBuilder(context).setListener(this).build() |
|
|
|
|
billingClient = BillingClient.newBuilder(context).setListener(this).enablePendingPurchases().build() |
|
|
|
|
|
|
|
|
|
this.startConnection(Runnable { |
|
|
|
|
this.updatePurchases() |
|
|
|
|
@ -110,10 +111,9 @@ object AppGuard : PurchasesUpdatedListener { |
|
|
|
|
private fun startConnection(executeOnSuccess: Runnable) { |
|
|
|
|
|
|
|
|
|
billingClient.startConnection(object : BillingClientStateListener { |
|
|
|
|
override fun onBillingSetupFinished(@BillingClient.BillingResponse billingResponseCode: Int) { |
|
|
|
|
|
|
|
|
|
this@AppGuard.billingResponseCode = billingResponseCode |
|
|
|
|
if (billingResponseCode == BillingClient.BillingResponse.OK) { |
|
|
|
|
override fun onBillingSetupFinished(result: BillingResult) { |
|
|
|
|
this@AppGuard.billingResponseCode = result.responseCode |
|
|
|
|
if (billingResponseCode == BillingResponseCode.OK) { |
|
|
|
|
// The BillingClient is ready. You can query purchases here. |
|
|
|
|
billingClientAvailable = true |
|
|
|
|
executeOnSuccess.run() |
|
|
|
|
@ -125,6 +125,22 @@ object AppGuard : PurchasesUpdatedListener { |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// billingClient.startConnection(object : BillingClientStateListener { |
|
|
|
|
// override fun onBillingSetupFinished(@BillingClient.BillingResponse billingResponseCode: Int) { |
|
|
|
|
// |
|
|
|
|
// this@AppGuard.billingResponseCode = billingResponseCode |
|
|
|
|
// if (billingResponseCode == BillingClient.BillingResponse.OK) { |
|
|
|
|
// // The BillingClient is ready. You can query purchases here. |
|
|
|
|
// billingClientAvailable = true |
|
|
|
|
// executeOnSuccess.run() |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// override fun onBillingServiceDisconnected() { |
|
|
|
|
// billingClientAvailable = false |
|
|
|
|
// } |
|
|
|
|
// }) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun executeServiceRequest(runnable: Runnable) { |
|
|
|
|
@ -150,8 +166,8 @@ object AppGuard : PurchasesUpdatedListener { |
|
|
|
|
this.resetPurchases() |
|
|
|
|
// Automatically checks for purchases (when switching devices for example) |
|
|
|
|
val purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.SUBS) |
|
|
|
|
if (purchasesResult != null && purchasesResult.purchasesList != null) { |
|
|
|
|
purchasesResult.purchasesList.forEach { |
|
|
|
|
if (purchasesResult != null) { |
|
|
|
|
purchasesResult.purchasesList?.forEach { |
|
|
|
|
this.handlePurchase(it) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -206,23 +222,40 @@ object AppGuard : PurchasesUpdatedListener { |
|
|
|
|
|
|
|
|
|
// PurchasesUpdatedListener |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Purchase callback |
|
|
|
|
*/ |
|
|
|
|
override fun onPurchasesUpdated(responseCode: Int, purchases: MutableList<Purchase>?) { |
|
|
|
|
override fun onPurchasesUpdated(result: BillingResult, purchases: MutableList<Purchase>?) { |
|
|
|
|
|
|
|
|
|
if (responseCode == BillingClient.BillingResponse.OK && purchases != null) { |
|
|
|
|
if (result.responseCode == BillingResponseCode.OK && purchases != null) { |
|
|
|
|
for (purchase in purchases) { |
|
|
|
|
handlePurchase(purchase) |
|
|
|
|
} |
|
|
|
|
} else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) { |
|
|
|
|
} else if (result.responseCode == BillingResponseCode.USER_CANCELED) { |
|
|
|
|
Timber.d("purchase cancel message: ${result.debugMessage}") |
|
|
|
|
// Handle an error caused by a user cancelling the purchase flow. |
|
|
|
|
} else { |
|
|
|
|
Timber.d("purchase error message: ${result.debugMessage}") |
|
|
|
|
// Handle any other error codes. |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// /** |
|
|
|
|
// * Purchase callback |
|
|
|
|
// */ |
|
|
|
|
// override fun onPurchasesUpdated(responseCode: Int, purchases: MutableList<Purchase>?) { |
|
|
|
|
// |
|
|
|
|
// if (responseCode == BillingClient.BillingResponse.OK && purchases != null) { |
|
|
|
|
// for (purchase in purchases) { |
|
|
|
|
// handlePurchase(purchase) |
|
|
|
|
// } |
|
|
|
|
// } else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) { |
|
|
|
|
// // Handle an error caused by a user cancelling the purchase flow. |
|
|
|
|
// } else { |
|
|
|
|
// // Handle any other error codes. |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Method called when a [purchase] has been made |
|
|
|
|
*/ |
|
|
|
|
@ -232,11 +265,19 @@ object AppGuard : PurchasesUpdatedListener { |
|
|
|
|
when (purchase.sku) { |
|
|
|
|
IAPProducts.PRO.identifier -> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val date = Date(purchase.purchaseTime) |
|
|
|
|
Timber.d("*** Auto renewing = ${purchase.isAutoRenewing}") |
|
|
|
|
Timber.d("*** purchaseTime = $date") |
|
|
|
|
|
|
|
|
|
if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED) { |
|
|
|
|
|
|
|
|
|
if (!purchase.isAcknowledged) { |
|
|
|
|
val params = AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.purchaseToken).build() |
|
|
|
|
this.billingClient.acknowledgePurchase(params) { result -> |
|
|
|
|
Timber.d("Acknowledge result: ${result.responseCode}") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this._isProUser = true |
|
|
|
|
this.purchaseDelegate?.let { |
|
|
|
|
Handler(Looper.getMainLooper()).post { |
|
|
|
|
@ -245,6 +286,8 @@ object AppGuard : PurchasesUpdatedListener { |
|
|
|
|
this.purchaseDelegate = null |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
else -> { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|