aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/utils/iab
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/utils/iab')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABBinder.kt103
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABDialogs.kt11
2 files changed, 66 insertions, 48 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
index 53d3e058..ab9e37d1 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABBinder.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABBinder.kt
@@ -9,8 +9,6 @@ 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.
@@ -24,18 +22,18 @@ interface FrostBilling : BillingProcessor.IBillingHandler {
fun Activity.onCreateBilling()
fun onDestroyBilling()
fun purchasePro()
- fun restorePurchases(once: Boolean)
+ fun restorePurchases()
fun onActivityResultBilling(requestCode: Int, resultCode: Int, data: Intent?): Boolean
}
-open class IABBinder : FrostBilling {
+abstract 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 = BillingProcessor.newBillingProcessor(this, PUBLIC_BILLING_KEY, this@IABBinder)
bp!!.initialize()
}
@@ -45,15 +43,11 @@ open class IABBinder : FrostBilling {
activity = null
}
- override fun onBillingInitialized() {
- L.d("IAB initialized")
- }
+ override fun onBillingInitialized() = L.d("IAB initialized")
- override fun onPurchaseHistoryRestored() {
- L.d("IAB restored")
- }
+ override fun onPurchaseHistoryRestored() = L.d("IAB restored")
- override fun onProductPurchased(productId: String, details: TransactionDetails) {
+ override fun onProductPurchased(productId: String, details: TransactionDetails?) {
L.d("IAB $productId purchased")
frostAnswers {
logPurchase(PurchaseEvent()
@@ -63,7 +57,7 @@ open class IABBinder : FrostBilling {
}
}
- override fun onBillingError(errorCode: Int, error: Throwable) {
+ override fun onBillingError(errorCode: Int, error: Throwable?) {
frostAnswers {
logPurchase(PurchaseEvent()
.putCustomAttribute("result", errorCode.toString())
@@ -76,64 +70,81 @@ open class IABBinder : FrostBilling {
= bp?.handleActivityResult(requestCode, resultCode, data) ?: false
override fun purchasePro() {
- if (bp == null) return
+ if (bp == null) {
+ frostAnswers {
+ logPurchase(PurchaseEvent()
+ .putCustomAttribute("result", "null bp")
+ .putSuccess(false))
+ }
+ L.eThrow("IAB null bp on purchase attempt")
+ 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) {
+ override fun onProductPurchased(productId: String, details: TransactionDetails?) {
super.onProductPurchased(productId, details)
+ activity?.playStorePurchasedSuccessfully(productId)
}
- override fun onBillingError(errorCode: Int, error: Throwable) {
+ override fun onBillingError(errorCode: Int, error: Throwable?) {
super.onBillingError(errorCode, error)
activity?.playStoreGenericError(null)
}
+
+ /**
+ * Attempts to get pro, or launch purchase flow if user doesn't have it
+ */
+ override fun restorePurchases() {
+ if (bp == null) return
+ val load = bp!!.loadOwnedPurchasesFromGoogle()
+ L.d("IAB settings load from google $load")
+ if (!bp!!.isPurchased(FROST_PRO)) {
+ if (Prefs.pro) activity!!.playStoreNoLongerPro()
+ else purchasePro()
+ } else {
+ if (!Prefs.pro) activity!!.playStoreFoundPro()
+ else activity!!.purchaseRestored()
+ }
+ }
}
class IABMain : IABBinder() {
override fun onBillingInitialized() {
super.onBillingInitialized()
- restorePurchases(true)
+ restorePurchases()
}
override fun onPurchaseHistoryRestored() {
super.onPurchaseHistoryRestored()
- restorePurchases(true)
+ restorePurchases()
+ }
+
+ private var restored = false
+
+ /**
+ * Checks for pro and only does so once
+ * A null check is added but it should never happen
+ * given that this is only called with bp is ready
+ */
+ override fun restorePurchases() {
+ if (restored || bp == null) return
+ restored = true
+ val load = bp!!.loadOwnedPurchasesFromGoogle()
+ L.d("IAB main load from google $load")
+ if (!bp!!.isPurchased(FROST_PRO)) {
+ if (Prefs.pro) activity!!.playStoreNoLongerPro()
+ } else {
+ if (!Prefs.pro) activity!!.playStoreFoundPro()
+ }
+ onDestroyBilling()
}
} \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABDialogs.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABDialogs.kt
index 4f750b6b..d2f22829 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABDialogs.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABDialogs.kt
@@ -4,11 +4,13 @@ import android.app.Activity
import ca.allanwang.kau.utils.restart
import ca.allanwang.kau.utils.startPlayStoreLink
import ca.allanwang.kau.utils.string
+import com.crashlytics.android.answers.PurchaseEvent
import com.pitchedapps.frost.R
import com.pitchedapps.frost.activities.MainActivity
import com.pitchedapps.frost.activities.SettingsActivity
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.Prefs
+import com.pitchedapps.frost.utils.frostAnswers
import com.pitchedapps.frost.utils.materialDialogThemed
/**
@@ -16,7 +18,7 @@ import com.pitchedapps.frost.utils.materialDialogThemed
*/
private fun playStoreLog(text: String) {
- L.e(Throwable(text), "Play Store Exception")
+ L.e(Throwable(text), "IAB Play Store Exception")
}
/**
@@ -31,7 +33,12 @@ private fun Activity.playRestart() {
fun Activity.playStoreNoLongerPro() {
Prefs.pro = false
- playStoreLog("No Longer Pro")
+ L.d("IAB No longer pro")
+ frostAnswers {
+ logPurchase(PurchaseEvent()
+ .putCustomAttribute("result", "no longer pro")
+ .putSuccess(false))
+ }
materialDialogThemed {
title(R.string.uh_oh)
content(R.string.play_store_not_pro)