aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABBinder.kt
blob: 53d3e058a457b881687983110fa6c04437b907dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.pitchedapps.frost.utils.iab

import android.app.Activity
import android.content.Intent
import com.anjlab.android.iab.v3.BillingProcessor
import com.anjlab.android.iab.v3.TransactionDetails
import com.crashlytics.android.answers.PurchaseEvent
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.
 */
private const val FROST_PRO = "frost_pro"

val IS_FROST_PRO: Boolean
    get() = (BuildConfig.DEBUG && Prefs.debugPro) || Prefs.pro

interface FrostBilling : BillingProcessor.IBillingHandler {
    fun Activity.onCreateBilling()
    fun onDestroyBilling()
    fun purchasePro()
    fun restorePurchases(once: Boolean)
    fun onActivityResultBilling(requestCode: Int, resultCode: Int, data: Intent?): Boolean
}

open 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!!.initialize()
    }

    override fun onDestroyBilling() {
        bp?.release()
        bp = null
        activity = null
    }

    override fun onBillingInitialized() {
        L.d("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)
            )
        }
    }

    override fun onBillingError(errorCode: Int, error: Throwable) {
        frostAnswers {
            logPurchase(PurchaseEvent()
                    .putCustomAttribute("result", errorCode.toString())
                    .putSuccess(false))
        }
        L.e(error, "IAB error $errorCode")
    }

    override fun onActivityResultBilling(requestCode: Int, resultCode: Int, data: Intent?): Boolean
            = bp?.handleActivityResult(requestCode, resultCode, data) ?: false

    override fun purchasePro() {
        if (bp == null) 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) {
        super.onProductPurchased(productId, details)
    }

    override fun onBillingError(errorCode: Int, error: Throwable) {
        super.onBillingError(errorCode, error)
        activity?.playStoreGenericError(null)
    }
}

class IABMain : IABBinder() {

    override fun onBillingInitialized() {
        super.onBillingInitialized()
        restorePurchases(true)
    }

    override fun onPurchaseHistoryRestored() {
        super.onPurchaseHistoryRestored()
        restorePurchases(true)
    }
}