|
|
|
|
@ -5,12 +5,32 @@ import android.content.Context |
|
|
|
|
import com.android.billingclient.api.* |
|
|
|
|
import net.pokeranalytics.android.ui.activity.IAPProducts |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* the AppGuard object is in charge of contacting the Billing services to retrieve products, |
|
|
|
|
* initiating purchases and verifying transactions |
|
|
|
|
*/ |
|
|
|
|
object AppGuard : PurchasesUpdatedListener { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* The Billing Client making requests with Google Billing services |
|
|
|
|
*/ |
|
|
|
|
lateinit private var billingClient: BillingClient |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Whether the billing client is available |
|
|
|
|
*/ |
|
|
|
|
private var billingClientAvailable: Boolean = false |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns whether the user has the pro subscription |
|
|
|
|
*/ |
|
|
|
|
var isProUser: Boolean = false |
|
|
|
|
private set |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Initialization of AppGuard |
|
|
|
|
* Connects to billing services and restores purchases |
|
|
|
|
*/ |
|
|
|
|
fun load(context: Context) { |
|
|
|
|
|
|
|
|
|
billingClient = BillingClient.newBuilder(context).setListener(this).build() |
|
|
|
|
@ -26,6 +46,7 @@ object AppGuard : PurchasesUpdatedListener { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onBillingServiceDisconnected() { |
|
|
|
|
billingClientAvailable = false |
|
|
|
|
|
|
|
|
|
@ -36,16 +57,22 @@ object AppGuard : PurchasesUpdatedListener { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Restore purchases |
|
|
|
|
*/ |
|
|
|
|
fun restorePurchases() { |
|
|
|
|
// Automatically checks for purchases (when switching devices for example) |
|
|
|
|
val purchasesResult= |
|
|
|
|
val purchasesResult = |
|
|
|
|
billingClient.queryPurchases(BillingClient.SkuType.SUBS) |
|
|
|
|
purchasesResult.purchasesList.forEach { |
|
|
|
|
this.handlePurchase(it) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun requestProducts(listener: SkuDetailsResponseListener) : Boolean { |
|
|
|
|
/** |
|
|
|
|
* Requests the product descriptions |
|
|
|
|
*/ |
|
|
|
|
fun requestProducts(listener: SkuDetailsResponseListener): Boolean { |
|
|
|
|
|
|
|
|
|
if (this.billingClientAvailable) { |
|
|
|
|
|
|
|
|
|
@ -60,6 +87,9 @@ object AppGuard : PurchasesUpdatedListener { |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Initiates purchase with the product [skuDetails] |
|
|
|
|
*/ |
|
|
|
|
fun initiatePurchase(activity: Activity, skuDetails: SkuDetails) { |
|
|
|
|
|
|
|
|
|
val flowParams = BillingFlowParams.newBuilder() |
|
|
|
|
@ -71,6 +101,9 @@ object AppGuard : PurchasesUpdatedListener { |
|
|
|
|
|
|
|
|
|
// PurchasesUpdatedListener |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Purchase callback |
|
|
|
|
*/ |
|
|
|
|
override fun onPurchasesUpdated(responseCode: Int, purchases: MutableList<Purchase>?) { |
|
|
|
|
|
|
|
|
|
if (responseCode == BillingClient.BillingResponse.OK && purchases != null) { |
|
|
|
|
@ -85,6 +118,9 @@ object AppGuard : PurchasesUpdatedListener { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Method called when a purchase has been made |
|
|
|
|
*/ |
|
|
|
|
private fun handlePurchase(purchase: Purchase) { |
|
|
|
|
|
|
|
|
|
val token = purchase.purchaseToken |
|
|
|
|
@ -93,9 +129,10 @@ object AppGuard : PurchasesUpdatedListener { |
|
|
|
|
if (this.verifyPurchase(purchase)) { |
|
|
|
|
when (purchase.sku) { |
|
|
|
|
IAPProducts.PRO.identifier -> { |
|
|
|
|
|
|
|
|
|
this.isProUser = true |
|
|
|
|
} |
|
|
|
|
else -> { |
|
|
|
|
} |
|
|
|
|
else -> {} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
// invalid purchase |
|
|
|
|
@ -103,7 +140,10 @@ object AppGuard : PurchasesUpdatedListener { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun verifyPurchase(purchase: Purchase) : Boolean { |
|
|
|
|
/** |
|
|
|
|
* Verifies the validity of a purchase |
|
|
|
|
*/ |
|
|
|
|
private fun verifyPurchase(purchase: Purchase): Boolean { |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|