Add comments to the class

dev
Laurent 7 years ago
parent 8cfcedd4c0
commit ff8e2cdbae
  1. 50
      app/src/main/java/net/pokeranalytics/android/util/billing/AppGuard.kt

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

Loading…
Cancel
Save