diff options
author | Allan Wang <me@allanwang.ca> | 2017-07-22 23:36:19 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-07-22 23:36:19 -0700 |
commit | 728da420b4fe57bb6fafa57001f30350b2690214 (patch) | |
tree | 3fef2d5f10745ad4ee5cb710930851c58f544588 | |
parent | 6a9304900636a0b597c9ac8b85b9c96b50c7addf (diff) | |
download | frost-728da420b4fe57bb6fafa57001f30350b2690214.tar.gz frost-728da420b4fe57bb6fafa57001f30350b2690214.tar.bz2 frost-728da420b4fe57bb6fafa57001f30350b2690214.zip |
Fix double calling issue
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/activities/SettingsActivity.kt | 2 | ||||
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABBinder.kt | 81 |
2 files changed, 57 insertions, 26 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/SettingsActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/SettingsActivity.kt index a7ca6ce5..182c99f7 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/SettingsActivity.kt @@ -68,7 +68,7 @@ class SettingsActivity : KPrefActivity(), FrostBilling by IABSettings() { plainText(R.string.restore_purchases) { descRes = R.string.restore_purchases_desc iicon = GoogleMaterial.Icon.gmd_refresh - onClick = { _, _, _ -> restorePurchases(false); true } + onClick = { _, _, _ -> restorePurchases(); true } } plainText(R.string.about_frost) { 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..b597717b 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 @@ -24,18 +24,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() } @@ -76,32 +76,21 @@ 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() { @@ -123,17 +112,59 @@ class IABSettings : IABBinder() { super.onBillingError(errorCode, error) activity?.playStoreGenericError(null) } + + override fun restorePurchases() { + if (bp == null) return + doAsync { + val load = bp!!.loadOwnedPurchasesFromGoogle() + L.d("IAB settings load from google $load") + uiThread { + 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) return + synchronized(this) { + if (restored || bp == null) return + restored = true + doAsync { + val load = bp!!.loadOwnedPurchasesFromGoogle() + L.d("IAB main load from google $load") + if (!bp!!.isPurchased(FROST_PRO)) { + if (Prefs.pro) uiThread { activity!!.playStoreNoLongerPro() } + } else { + if (!Prefs.pro) uiThread { activity!!.playStoreFoundPro() } + } + onDestroyBilling() + } + } } }
\ No newline at end of file |