diff options
author | Allan Wang <me@allanwang.ca> | 2017-08-14 20:48:39 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-14 20:48:39 -0700 |
commit | 5d9a3fd7fb8f2f9d0f592c89446824980c9841c6 (patch) | |
tree | a770b2564b67280fcc9fcc65144bd0b8bd8e2881 /app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABBinder.kt | |
parent | ab7ec131b62ac1567e983c846c921bd3ada11dd4 (diff) | |
download | frost-5d9a3fd7fb8f2f9d0f592c89446824980c9841c6.tar.gz frost-5d9a3fd7fb8f2f9d0f592c89446824980c9841c6.tar.bz2 frost-5d9a3fd7fb8f2f9d0f592c89446824980c9841c6.zip |
v1.4.5 (#174)v1.4.5
* Update/kau (#125)
* Update logger
* Clean imports and bring back reactive libs
* Update dependencies and make billing async
* Misc (#128)
* Update null
* Attempt to improve transparent theme backgrounds
* Update menu
* Move injections to visible method and reduce offset
* Update searchview and logging
* Clean temp strings and add network states
* Move console blacklist to web state
* Change some logs to info
* Move glide loader to onCreate (#135)
* Remove commit number increments (#139)
* Fix/misc (#140)
* Add canadian locale to toLowerCase
* Add try catch to JsAssets
* Disable error throwing for bad search subject
* Log more throwables quietly
* Check internet connection before fetching username
* Remove name check in frost notifications
* Add activity lifecycle logger
* Add rxjava to lib showcase
* Move network checker to io thread (#150)
* Update dependency
* Blank
* Feature/jsoup debugger (#152)
* Create debugger
* Update debugger content
* Create debugging logic
* Finalize and test debugger
* Add reload listener
* Fix/pro crash without play store (#155)
* Update changelog
* Check if iab service exists
* Add checker before launching play store request
* Separate strings
* Enhancement/message notifications (#157)
* Map message notifs to the headless html extractor
* Update strings
* Bring im notifs out of alpha
* Update changelog
* Remove confirmation dialog (#159)
* Separate message notifications and add click intents (#171)
* Separate message notifications and add click intent for group notifications
* Add comments and finalize
* Feature/scroll down on message thread (#172)
* Add hook for scroll
* Update changelog
* Add custom navdrawer layout (#173)
* Add faq for auto play
* Update changelog
* Fix page banner bg (#163)
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 | 102 |
1 files changed, 68 insertions, 34 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 bad7f8fd..7f6e8a6d 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,6 +9,13 @@ import com.pitchedapps.frost.BuildConfig import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.frostAnswers +import com.pitchedapps.frost.utils.logFrostAnswers +import org.jetbrains.anko.doAsync +import org.jetbrains.anko.onComplete +import org.jetbrains.anko.uiThread +import java.lang.ref.WeakReference +import java.math.BigDecimal +import java.util.* /** * Created by Allan Wang on 2017-07-22. @@ -33,31 +40,48 @@ interface FrostBilling : BillingProcessor.IBillingHandler { abstract class IABBinder : FrostBilling { var bp: BillingProcessor? = null - var activity: Activity? = null - - override fun Activity.onCreateBilling() { - activity = this - bp = BillingProcessor.newBillingProcessor(this, PUBLIC_BILLING_KEY, this@IABBinder) - bp?.initialize() + lateinit var activityRef: WeakReference<Activity> + val activity + get() = activityRef.get() + + override final fun Activity.onCreateBilling() { + activityRef = WeakReference(this) + doAsync { + bp = BillingProcessor.newBillingProcessor(this@onCreateBilling, PUBLIC_BILLING_KEY, this@IABBinder) + bp?.initialize() + } } override fun onDestroyBilling() { + activityRef.clear() bp?.release() bp = null - activity = null } - override fun onBillingInitialized() = L.d("IAB initialized") + override fun onBillingInitialized() = L.i("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) - ) + bp.doAsync { + L.i("IAB $productId purchased") + val listing = weakRef.get()?.getPurchaseListingDetails(productId) ?: return@doAsync + val currency = try { + Currency.getInstance(listing.currency) + } catch (e: Exception) { + null + } + frostAnswers { + logPurchase(PurchaseEvent().apply { + putItemId(productId) + putSuccess(true) + if (currency != null) { + putCurrency(Currency.getInstance(Locale.getDefault())) + putItemType(productId) + putItemPrice(BigDecimal.valueOf(listing.priceValue)) + } + }) + } } } @@ -67,13 +91,14 @@ abstract class IABBinder : FrostBilling { .putCustomAttribute("result", errorCode.toString()) .putSuccess(false)) } - L.e(error, "IAB error $errorCode") + error.logFrostAnswers("IAB error $errorCode") } override fun onActivityResultBilling(requestCode: Int, resultCode: Int, data: Intent?): Boolean = bp?.handleActivityResult(requestCode, resultCode, data) ?: false override fun purchasePro() { + val bp = this.bp if (bp == null) { frostAnswers { logPurchase(PurchaseEvent() @@ -83,10 +108,12 @@ abstract class IABBinder : FrostBilling { L.eThrow("IAB null bp on purchase attempt") return } - if (!(bp?.isOneTimePurchaseSupported ?: false)) - activity?.playStorePurchaseUnsupported() + val a = activity ?: return + + if (!BillingProcessor.isIabServiceAvailable(a) || !bp.isOneTimePurchaseSupported) + a.playStorePurchaseUnsupported() else - bp?.purchase(activity, FROST_PRO) + bp.purchase(a, FROST_PRO) } } @@ -107,15 +134,18 @@ class IABSettings : IABBinder() { * 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() ?: return - L.d("IAB settings load from google $load") - if (!(bp?.isPurchased(FROST_PRO) ?: return)) { - if (Prefs.pro) activity.playStoreNoLongerPro() - else purchasePro() - } else { - if (!Prefs.pro) activity.playStoreFoundPro() - else activity?.purchaseRestored() + bp.doAsync { + val load = weakRef.get()?.loadOwnedPurchasesFromGoogle() ?: return@doAsync + L.d("IAB settings load from google $load") + uiThread { + if (!(weakRef.get()?.isPurchased(FROST_PRO) ?: return@uiThread)) { + if (Prefs.pro) activity.playStoreNoLongerPro() + else purchasePro() + } else { + if (!Prefs.pro) activity.playStoreFoundPro() + else activity?.purchaseRestored() + } + } } } } @@ -142,13 +172,17 @@ class IABMain : IABBinder() { override fun restorePurchases() { if (restored || bp == null) return restored = true - val load = bp?.loadOwnedPurchasesFromGoogle() ?: false - L.d("IAB main load from google $load") - if (!(bp?.isPurchased(FROST_PRO) ?: false)) { - if (Prefs.pro) activity.playStoreNoLongerPro() - } else { - if (!Prefs.pro) activity.playStoreFoundPro() + bp.doAsync { + val load = weakRef.get()?.loadOwnedPurchasesFromGoogle() ?: false + L.d("IAB main load from google $load") + onComplete { + if (!(weakRef.get()?.isPurchased(FROST_PRO) ?: false)) { + if (Prefs.pro) activity.playStoreNoLongerPro() + } else { + if (!Prefs.pro) activity.playStoreFoundPro() + } + onDestroyBilling() + } } - onDestroyBilling() } }
\ No newline at end of file |