diff options
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/utils')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IAB.kt | 63 | ||||
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABDialogs.kt | 50 |
2 files changed, 77 insertions, 36 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IAB.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IAB.kt index 19b9b6f7..0fd10c5b 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IAB.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IAB.kt @@ -16,17 +16,20 @@ object IAB { var helper: IabHelper? = null - fun setupAsync(context: Context) { - if (!context.isFromGooglePlay) return + fun setupAsync(activity: Activity) { if (helper == null) { + L.d("IAB setup async") + if (!activity.isFromGooglePlay && !BuildConfig.DEBUG) return L.d("IAB not from google play") try { - helper = IabHelper(context.applicationContext, PUBLIC_BILLING_KEY) + helper = IabHelper(activity.applicationContext, PUBLIC_BILLING_KEY) helper!!.startSetup { result -> + L.d("IAB result ${result.message}") if (!result.isSuccess) L.eThrow("IAB Setup error: $result") } } catch (e: Exception) { L.e(e, "IAB error") + activity.playStoreNoLongerPro() } } } @@ -35,7 +38,7 @@ object IAB { private const val FROST_PRO = "frost_pro" val IS_FROST_PRO: Boolean - get() = (BuildConfig.DEBUG && Prefs.debugPro) || (IAB.helper?.queryInventory()?.getSkuDetails(FROST_PRO) != null) + get() = (BuildConfig.DEBUG && Prefs.debugPro) || Prefs.previouslyPro private fun Context.checkFromPlay(): Boolean { val isPlay = isFromGooglePlay || BuildConfig.DEBUG @@ -52,44 +55,32 @@ private fun Context.checkFromPlay(): Boolean { fun Activity.openPlayProPurchase(code: Int) = openPlayPurchase(FROST_PRO, code) fun Activity.openPlayPurchase(key: String, code: Int) { + L.d("Open play purchase $key $code") if (!checkFromPlay()) return frostAnswersCustom("PLAY_PURCHASE") { putCustomAttribute("Key", key) } - IAB.helper?.flagEndAsync() ?: playStoreErrorDialog() - IAB.helper?.queryInventoryAsync { + L.d("IAB flag end async") + IAB.helper?.flagEndAsync() ?: return playStoreGenericError("Null flag end async") + L.d("IAB query inv async") + IAB.helper!!.queryInventoryAsync { res, inv -> - if (res.isFailure) { - L.e("IAB error: ${res.message}") - playStoreErrorDialog() - } else if (inv == null) { - playStoreErrorDialog("Empty inventory") - } else { - val donation = inv.getSkuDetails(key) - if (donation != null) { - IAB.helper?.launchPurchaseFlow(this@openPlayPurchase, donation.sku, code) { - result, _ -> - if (result.isSuccess) materialDialogThemed { - title(R.string.play_thank_you) - content(R.string.play_purchased_pro) - positiveText(R.string.kau_ok) - } else playStoreErrorDialog("Result: ${result.message}") - frostAnswers { - logPurchase(PurchaseEvent() - .putItemId(key) - .putSuccess(result.isSuccess)) - } - } ?: playStoreErrorDialog("Launch Purchase Flow") + if (res.isFailure) return@queryInventoryAsync playStoreGenericError("Query res error") + if (inv == null) return@queryInventoryAsync playStoreGenericError("Empty inventory") + L.d("IAB: inventory ${inv.allOwnedSkus}") + val donation = inv.getSkuDetails(key) ?: return@queryInventoryAsync playStoreGenericError("Donation null") + IAB.helper!!.launchPurchaseFlow(this@openPlayPurchase, donation.sku, code) { + result, _ -> + if (result.isSuccess) materialDialogThemed { + title(R.string.play_thank_you) + content(R.string.play_purchased_pro) + positiveText(R.string.kau_ok) + } else playStoreGenericError("Result: ${result.message}") + frostAnswers { + logPurchase(PurchaseEvent() + .putItemId(key) + .putSuccess(result.isSuccess)) } } } -} - -private fun Context.playStoreErrorDialog(s: String = "Play Store Error") { - materialDialogThemed { - title(R.string.uh_oh) - content(R.string.play_store_billing_error) - positiveText(R.string.kau_ok) - } - L.e(Throwable(s), "Play Store Error") }
\ 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 new file mode 100644 index 00000000..e855138f --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABDialogs.kt @@ -0,0 +1,50 @@ +package com.pitchedapps.frost.utils.iab + +import android.app.Activity +import ca.allanwang.kau.utils.restart +import ca.allanwang.kau.utils.startPlayStoreLink +import com.pitchedapps.frost.R +import com.pitchedapps.frost.utils.L +import com.pitchedapps.frost.utils.Prefs +import com.pitchedapps.frost.utils.materialDialogThemed + +/** + * Created by Allan Wang on 2017-06-30. + */ +private fun playStoreLog(text: String) { + L.e(Throwable(text), "Play Store Exception") +} + +fun Activity.playStoreNoLongerPro() { + if (!Prefs.previouslyPro) return //never pro to begin with + Prefs.previouslyPro = false + playStoreLog("No Longer Pro") + materialDialogThemed { + title(R.string.uh_oh) + content(R.string.play_store_not_pro) + positiveText(R.string.reload) + dismissListener { + this@playStoreNoLongerPro.restart() + } + } +} + +fun Activity.playStoreNotAvailable() { + playStoreLog("Store not available") + materialDialogThemed { + title(R.string.uh_oh) + content(R.string.play_store_not_found) + positiveText(R.string.kau_ok) + neutralText(R.string.kau_play_store) + onNeutral { _, _ -> startPlayStoreLink(R.string.play_store_package_id) } + } +} + +fun Activity.playStoreGenericError(text: String = "Store generic error") { + playStoreLog("IAB: $text") + materialDialogThemed { + title(R.string.uh_oh) + content(R.string.play_store_billing_error) + positiveText(R.string.kau_ok) + } +}
\ No newline at end of file |