diff options
author | Allan Wang <me@allanwang.ca> | 2017-07-22 20:27:22 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-22 20:27:22 -0700 |
commit | 138824065679d3cd88f7f80d48728ffdc777704a (patch) | |
tree | 1b2262da7499f226aede68bb26adec6833263d5d /app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABBinder.kt | |
parent | 37a9f9057d7879080b7b982f987bf0b82d0d774c (diff) | |
download | frost-1.3.5.tar.gz frost-1.3.5.tar.bz2 frost-1.3.5.zip |
Test new billing logic (#86)v1.3.5
* Add lint
* Add new libs
* Update libs and add friends tab
* Aggressively hide nonrecent posts
* Update dependencies
* Add php to most recents
* Add full size image downloader
* Fix css cleaner
* Fix notification and circle
* Bring back regex
* Update kau, optimize imports, and remove string ambiguity
* Bring back anjlab iab and move to alpha
* Create initial billing test
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABBinder.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABBinder.kt | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABBinder.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABBinder.kt new file mode 100644 index 00000000..53d3e058 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABBinder.kt @@ -0,0 +1,139 @@ +package com.pitchedapps.frost.utils.iab + +import android.app.Activity +import android.content.Intent +import com.anjlab.android.iab.v3.BillingProcessor +import com.anjlab.android.iab.v3.TransactionDetails +import com.crashlytics.android.answers.PurchaseEvent +import com.pitchedapps.frost.BuildConfig +import com.pitchedapps.frost.utils.L +import com.pitchedapps.frost.utils.Prefs +import com.pitchedapps.frost.utils.frostAnswers +import org.jetbrains.anko.doAsync +import org.jetbrains.anko.uiThread + +/** + * Created by Allan Wang on 2017-07-22. + */ +private const val FROST_PRO = "frost_pro" + +val IS_FROST_PRO: Boolean + get() = (BuildConfig.DEBUG && Prefs.debugPro) || Prefs.pro + +interface FrostBilling : BillingProcessor.IBillingHandler { + fun Activity.onCreateBilling() + fun onDestroyBilling() + fun purchasePro() + fun restorePurchases(once: Boolean) + fun onActivityResultBilling(requestCode: Int, resultCode: Int, data: Intent?): Boolean +} + +open class IABBinder : FrostBilling { + + var bp: BillingProcessor? = null + var activity: Activity? = null + + override fun Activity.onCreateBilling() { + bp = BillingProcessor.newBillingProcessor(this, PUBLIC_BILLING_KEY, this@IABBinder) + activity = this + bp!!.initialize() + } + + override fun onDestroyBilling() { + bp?.release() + bp = null + activity = null + } + + override fun onBillingInitialized() { + L.d("IAB initialized") + } + + override fun onPurchaseHistoryRestored() { + L.d("IAB restored") + } + + override fun onProductPurchased(productId: String, details: TransactionDetails) { + L.d("IAB $productId purchased") + frostAnswers { + logPurchase(PurchaseEvent() + .putItemId(productId) + .putSuccess(true) + ) + } + } + + override fun onBillingError(errorCode: Int, error: Throwable) { + frostAnswers { + logPurchase(PurchaseEvent() + .putCustomAttribute("result", errorCode.toString()) + .putSuccess(false)) + } + L.e(error, "IAB error $errorCode") + } + + override fun onActivityResultBilling(requestCode: Int, resultCode: Int, data: Intent?): Boolean + = bp?.handleActivityResult(requestCode, resultCode, data) ?: false + + override fun purchasePro() { + if (bp == null) return + if (!bp!!.isOneTimePurchaseSupported) + activity!!.playStorePurchaseUnsupported() + else + bp!!.purchase(activity, FROST_PRO) + } + + override fun restorePurchases(once: Boolean) { + if (bp == null) return + doAsync { + bp?.loadOwnedPurchasesFromGoogle() + if (bp?.isPurchased(FROST_PRO) ?: false) { + uiThread { + if (Prefs.pro) activity!!.playStoreNoLongerPro() + else if (!once) purchasePro() + if (once) onDestroyBilling() + } + } else { + uiThread { + if (!Prefs.pro) activity!!.playStoreFoundPro() + else if (!once) activity!!.purchaseRestored() + if (once) onDestroyBilling() + } + } + } + } +} + +class IABSettings : IABBinder() { + + override fun onBillingInitialized() { + super.onBillingInitialized() + + } + + override fun onPurchaseHistoryRestored() { + super.onPurchaseHistoryRestored() + } + + override fun onProductPurchased(productId: String, details: TransactionDetails) { + super.onProductPurchased(productId, details) + } + + override fun onBillingError(errorCode: Int, error: Throwable) { + super.onBillingError(errorCode, error) + activity?.playStoreGenericError(null) + } +} + +class IABMain : IABBinder() { + + override fun onBillingInitialized() { + super.onBillingInitialized() + restorePurchases(true) + } + + override fun onPurchaseHistoryRestored() { + super.onPurchaseHistoryRestored() + restorePurchases(true) + } +}
\ No newline at end of file |