diff options
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 | 88 |
1 files changed, 59 insertions, 29 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..47331833 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,12 @@ 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.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,18 +39,22 @@ 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() { bp?.release() bp = null - activity = null + activityRef.clear() } override fun onBillingInitialized() = L.d("IAB initialized") @@ -52,12 +62,25 @@ abstract class IABBinder : FrostBilling { 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.d("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)) + } + }) + } } } @@ -107,15 +130,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 +168,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 |