forked from upstream-mirrors/quik
Update to billing client v3
This commit is contained in:
@@ -9,6 +9,7 @@ buildscript {
|
||||
ext.androidx_testrunner_version = '1.1.0-alpha3'
|
||||
ext.androidx_viewpager_version = '1.0.0-beta05'
|
||||
ext.autodispose_version = '1.3.0'
|
||||
ext.billing_version = '3.0.2'
|
||||
ext.conductor_version = '2.1.5'
|
||||
ext.coroutines_version = '1.2.2'
|
||||
ext.dagger_version = "2.16"
|
||||
|
||||
@@ -38,6 +38,10 @@ interface BillingManager {
|
||||
val products: Observable<List<Product>>
|
||||
val upgradeStatus: Observable<Boolean>
|
||||
|
||||
fun initiatePurchaseFlow(activity: Activity, sku: String)
|
||||
suspend fun checkForPurchases()
|
||||
|
||||
suspend fun queryProducts()
|
||||
|
||||
suspend fun initiatePurchaseFlow(activity: Activity, sku: String)
|
||||
|
||||
}
|
||||
|
||||
@@ -187,8 +187,8 @@ dependencies {
|
||||
implementation project(':domain')
|
||||
|
||||
withAnalyticsImplementation 'com.google.firebase:firebase-crashlytics:17.3.0'
|
||||
withAnalyticsImplementation "com.android.billingclient:billing:1.0"
|
||||
|
||||
withAnalyticsImplementation "com.android.billingclient:billing:$billing_version"
|
||||
withAnalyticsImplementation "com.android.billingclient:billing-ktx:$billing_version"
|
||||
noAnalyticsDebug project(path: ':data', configuration: 'noAnalyticsDebug')
|
||||
noAnalyticsRelease project(path: ':data', configuration: 'noAnalyticsRelease')
|
||||
withAnalyticsDebug project(path: ':data', configuration: 'withAnalyticsDebug')
|
||||
|
||||
@@ -31,6 +31,7 @@ import com.moez.QKSMS.common.util.FileLoggingTree
|
||||
import com.moez.QKSMS.injection.AppComponentManager
|
||||
import com.moez.QKSMS.injection.appComponent
|
||||
import com.moez.QKSMS.manager.AnalyticsManager
|
||||
import com.moez.QKSMS.manager.BillingManager
|
||||
import com.moez.QKSMS.manager.ReferralManager
|
||||
import com.moez.QKSMS.migration.QkMigration
|
||||
import com.moez.QKSMS.migration.QkRealmMigration
|
||||
@@ -60,6 +61,7 @@ class QKApplication : Application(), HasActivityInjector, HasBroadcastReceiverIn
|
||||
@Suppress("unused")
|
||||
@Inject lateinit var qkMigration: QkMigration
|
||||
|
||||
@Inject lateinit var billingManager: BillingManager
|
||||
@Inject lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Activity>
|
||||
@Inject lateinit var dispatchingBroadcastReceiverInjector: DispatchingAndroidInjector<BroadcastReceiver>
|
||||
@Inject lateinit var dispatchingServiceInjector: DispatchingAndroidInjector<Service>
|
||||
@@ -85,6 +87,8 @@ class QKApplication : Application(), HasActivityInjector, HasBroadcastReceiverIn
|
||||
|
||||
GlobalScope.launch(Dispatchers.IO) {
|
||||
referralManager.trackReferrer()
|
||||
billingManager.checkForPurchases()
|
||||
billingManager.queryProducts()
|
||||
}
|
||||
|
||||
nightModeManager.updateCurrentTheme()
|
||||
|
||||
@@ -24,11 +24,11 @@ import androidx.core.view.children
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import com.jakewharton.rxbinding2.view.clicks
|
||||
import com.jakewharton.rxbinding2.view.enabled
|
||||
import com.moez.QKSMS.BuildConfig
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.common.base.QkThemedActivity
|
||||
import com.moez.QKSMS.common.util.FontProvider
|
||||
import com.moez.QKSMS.common.util.extensions.makeToast
|
||||
import com.moez.QKSMS.common.util.extensions.resolveThemeColor
|
||||
import com.moez.QKSMS.common.util.extensions.setBackgroundTint
|
||||
import com.moez.QKSMS.common.util.extensions.setTint
|
||||
@@ -37,10 +37,13 @@ import com.moez.QKSMS.common.widget.PreferenceView
|
||||
import com.moez.QKSMS.feature.plus.experiment.UpgradeButtonExperiment
|
||||
import com.moez.QKSMS.manager.BillingManager
|
||||
import dagger.android.AndroidInjection
|
||||
import io.reactivex.Observable
|
||||
import kotlinx.android.synthetic.main.collapsing_toolbar.*
|
||||
import kotlinx.android.synthetic.main.preference_view.view.*
|
||||
import kotlinx.android.synthetic.main.qksms_plus_activity.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
class PlusActivity : QkThemedActivity(), PlusView {
|
||||
@@ -113,7 +116,14 @@ class PlusActivity : QkThemedActivity(), PlusView {
|
||||
}
|
||||
|
||||
override fun initiatePurchaseFlow(billingManager: BillingManager, sku: String) {
|
||||
billingManager.initiatePurchaseFlow(this, sku)
|
||||
GlobalScope.launch(Dispatchers.Main) {
|
||||
try {
|
||||
billingManager.initiatePurchaseFlow(this@PlusActivity, sku)
|
||||
} catch (e: Exception) {
|
||||
Timber.w(e)
|
||||
makeToast(R.string.qksms_plus_error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -337,6 +337,7 @@
|
||||
<string name="qksms_plus_upgrade_donate">Unlock + donate for %1$s %2$s</string>
|
||||
<string name="qksms_plus_thanks_title">Thank you for supporting QKSMS!</string>
|
||||
<string name="qksms_plus_thanks_summary">You now have access to all QKSMS+ features</string>
|
||||
<string name="qksms_plus_error">An error has occurred, please try again</string>
|
||||
<string name="qksms_plus_free_title">QKSMS+ is free for F-Droid users! If you\'d like to support development, a donation would be highly appreciated.</string>
|
||||
<string name="qksms_plus_donate">Donate via PayPal</string>
|
||||
<string name="qksms_plus_coming_soon">Coming soon</string>
|
||||
|
||||
@@ -32,6 +32,8 @@ class BillingManagerImpl @Inject constructor(
|
||||
override val products: Observable<List<BillingManager.Product>> = BehaviorSubject.createDefault(listOf())
|
||||
override val upgradeStatus: Observable<Boolean> = BehaviorSubject.createDefault(true)
|
||||
|
||||
override fun initiatePurchaseFlow(activity: Activity, sku: String) = Unit
|
||||
override suspend fun checkForPurchases() = Unit
|
||||
override suspend fun queryProducts() = Unit
|
||||
override suspend fun initiatePurchaseFlow(activity: Activity, sku: String) = Unit
|
||||
|
||||
}
|
||||
|
||||
+101
-68
@@ -21,23 +21,32 @@ package com.moez.QKSMS.common.util
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import com.android.billingclient.api.AcknowledgePurchaseParams
|
||||
import com.android.billingclient.api.BillingClient
|
||||
import com.android.billingclient.api.BillingClientStateListener
|
||||
import com.android.billingclient.api.BillingFlowParams
|
||||
import com.android.billingclient.api.BillingResult
|
||||
import com.android.billingclient.api.Purchase
|
||||
import com.android.billingclient.api.PurchasesUpdatedListener
|
||||
import com.android.billingclient.api.SkuDetails
|
||||
import com.android.billingclient.api.SkuDetailsParams
|
||||
import com.android.billingclient.api.acknowledgePurchase
|
||||
import com.android.billingclient.api.queryPurchaseHistory
|
||||
import com.android.billingclient.api.querySkuDetails
|
||||
import com.moez.QKSMS.manager.AnalyticsManager
|
||||
import com.moez.QKSMS.manager.BillingManager
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import io.reactivex.subjects.BehaviorSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
|
||||
@Singleton
|
||||
class BillingManagerImpl @Inject constructor(
|
||||
@@ -45,96 +54,120 @@ class BillingManagerImpl @Inject constructor(
|
||||
private val analyticsManager: AnalyticsManager
|
||||
) : BillingManager, PurchasesUpdatedListener {
|
||||
|
||||
companion object {
|
||||
const val SKU_PLUS = "remove_ads"
|
||||
const val SKU_PLUS_DONATE = "qksms_plus_donate"
|
||||
}
|
||||
private val productsSubject: Subject<List<SkuDetails>> = BehaviorSubject.create()
|
||||
override val products: Observable<List<BillingManager.Product>> = productsSubject
|
||||
.map { skuDetailsList ->
|
||||
skuDetailsList.map { skuDetails ->
|
||||
BillingManager.Product(skuDetails.sku, skuDetails.price, skuDetails.priceCurrencyCode)
|
||||
}
|
||||
}
|
||||
|
||||
override val products: Observable<List<BillingManager.Product>> = BehaviorSubject.create()
|
||||
override val upgradeStatus: Observable<Boolean>
|
||||
private val purchaseListSubject = BehaviorSubject.create<List<Purchase>>()
|
||||
override val upgradeStatus: Observable<Boolean> = purchaseListSubject
|
||||
.map { purchases ->
|
||||
purchases
|
||||
.filter { it.purchaseState == Purchase.PurchaseState.PURCHASED }
|
||||
.any { it.sku in skus }
|
||||
}
|
||||
.distinctUntilChanged()
|
||||
.doOnNext { upgraded -> analyticsManager.setUserProperty("Upgraded", upgraded) }
|
||||
|
||||
private val skus = listOf(SKU_PLUS, SKU_PLUS_DONATE)
|
||||
private val purchaseListObservable = BehaviorSubject.create<List<Purchase>>()
|
||||
private val skus = listOf(BillingManager.SKU_PLUS, BillingManager.SKU_PLUS_DONATE)
|
||||
private val billingClient: BillingClient = BillingClient.newBuilder(context)
|
||||
.setListener(this)
|
||||
.enablePendingPurchases()
|
||||
.build()
|
||||
|
||||
private val billingClient: BillingClient = BillingClient.newBuilder(context).setListener(this).build()
|
||||
private var isServiceConnected = false
|
||||
|
||||
init {
|
||||
startServiceConnection {
|
||||
queryPurchases()
|
||||
querySkuDetailsAsync()
|
||||
}
|
||||
override suspend fun checkForPurchases() = executeServiceRequest {
|
||||
// Load the cached data
|
||||
queryPurchases()
|
||||
|
||||
upgradeStatus = purchaseListObservable
|
||||
.map { purchases -> purchases.any { it.sku == SKU_PLUS } || purchases.any { it.sku == SKU_PLUS_DONATE } }
|
||||
.doOnNext { upgraded -> analyticsManager.setUserProperty("Upgraded", upgraded) }
|
||||
// On a fresh device, the purchase might not be cached, and so we'll need to force a refresh
|
||||
billingClient.queryPurchaseHistory(BillingClient.SkuType.INAPP)
|
||||
queryPurchases()
|
||||
}
|
||||
|
||||
private fun queryPurchases() {
|
||||
executeServiceRequest {
|
||||
// Load the cached data
|
||||
purchaseListObservable.onNext(billingClient.queryPurchases(BillingClient.SkuType.INAPP).purchasesList.orEmpty())
|
||||
override suspend fun queryProducts() = executeServiceRequest {
|
||||
val params = SkuDetailsParams.newBuilder()
|
||||
.setSkusList(skus)
|
||||
.setType(BillingClient.SkuType.INAPP)
|
||||
|
||||
// On a fresh device, the purchase might not be cached, and so we'll need to force a refresh
|
||||
billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.INAPP) { _, _ ->
|
||||
purchaseListObservable.onNext(billingClient.queryPurchases(BillingClient.SkuType.INAPP).purchasesList.orEmpty())
|
||||
val (billingResult, skuDetailsList) = billingClient.querySkuDetails(params.build())
|
||||
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
|
||||
productsSubject.onNext(skuDetailsList.orEmpty())
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun initiatePurchaseFlow(activity: Activity, sku: String) = executeServiceRequest {
|
||||
val skuDetails = withContext(Dispatchers.IO) {
|
||||
val params = SkuDetailsParams.newBuilder()
|
||||
.setType(BillingClient.SkuType.INAPP)
|
||||
.setSkusList(listOf(sku))
|
||||
.build()
|
||||
|
||||
billingClient.querySkuDetails(params).skuDetailsList?.firstOrNull()!!
|
||||
}
|
||||
|
||||
val params = BillingFlowParams.newBuilder().setSkuDetails(skuDetails)
|
||||
billingClient.launchBillingFlow(activity, params.build())
|
||||
}
|
||||
|
||||
override fun onPurchasesUpdated(result: BillingResult, purchases: MutableList<Purchase>?) {
|
||||
if (result.responseCode == BillingClient.BillingResponseCode.OK) {
|
||||
GlobalScope.launch(Dispatchers.IO) {
|
||||
handlePurchases(purchases.orEmpty())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun startServiceConnection(onSuccess: () -> Unit) {
|
||||
val listener = object : BillingClientStateListener {
|
||||
override fun onBillingSetupFinished(@BillingClient.BillingResponse billingResponseCode: Int) {
|
||||
if (billingResponseCode == BillingClient.BillingResponse.OK) {
|
||||
isServiceConnected = true
|
||||
onSuccess()
|
||||
} else {
|
||||
Timber.w("Billing response: $billingResponseCode")
|
||||
purchaseListObservable.onNext(listOf())
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBillingServiceDisconnected() {
|
||||
isServiceConnected = false
|
||||
}
|
||||
}
|
||||
|
||||
Flowable.fromCallable { billingClient.startConnection(listener) }
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe()
|
||||
}
|
||||
|
||||
private fun querySkuDetailsAsync() {
|
||||
executeServiceRequest {
|
||||
val subParams = SkuDetailsParams.newBuilder().setSkusList(skus).setType(BillingClient.SkuType.INAPP)
|
||||
billingClient.querySkuDetailsAsync(subParams.build()) { responseCode, skuDetailsList ->
|
||||
if (responseCode == BillingClient.BillingResponse.OK) {
|
||||
(products as Subject).onNext(skuDetailsList.map { skuDetails ->
|
||||
BillingManager.Product(skuDetails.sku, skuDetails.price, skuDetails.priceCurrencyCode)
|
||||
})
|
||||
}
|
||||
}
|
||||
private suspend fun queryPurchases() {
|
||||
val result = billingClient.queryPurchases(BillingClient.SkuType.INAPP)
|
||||
if (result.responseCode == BillingClient.BillingResponseCode.OK) {
|
||||
handlePurchases(result.purchasesList.orEmpty())
|
||||
}
|
||||
}
|
||||
|
||||
override fun initiatePurchaseFlow(activity: Activity, sku: String) {
|
||||
executeServiceRequest {
|
||||
val params = BillingFlowParams.newBuilder().setSku(sku).setType(BillingClient.SkuType.INAPP)
|
||||
billingClient.launchBillingFlow(activity, params.build())
|
||||
private suspend fun handlePurchases(purchases: List<Purchase>) = executeServiceRequest {
|
||||
purchases.forEach { purchase ->
|
||||
if (!purchase.isAcknowledged) {
|
||||
val params = AcknowledgePurchaseParams.newBuilder()
|
||||
.setPurchaseToken(purchase.purchaseToken)
|
||||
.build()
|
||||
|
||||
Timber.i("Acknowledging purchase ${purchase.orderId}")
|
||||
val result = billingClient.acknowledgePurchase(params)
|
||||
Timber.i("Acknowledgement result: ${result.responseCode}, ${result.debugMessage}")
|
||||
}
|
||||
}
|
||||
|
||||
purchaseListSubject.onNext(purchases)
|
||||
}
|
||||
|
||||
private fun executeServiceRequest(runnable: () -> Unit) {
|
||||
when (isServiceConnected) {
|
||||
private suspend fun executeServiceRequest(runnable: suspend () -> Unit) {
|
||||
when (billingClient.isReady) {
|
||||
true -> runnable()
|
||||
false -> startServiceConnection(runnable)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPurchasesUpdated(resultCode: Int, purchases: List<Purchase>?) {
|
||||
if (resultCode == BillingClient.BillingResponse.OK) {
|
||||
purchaseListObservable.onNext(purchases.orEmpty())
|
||||
private suspend fun startServiceConnection(onSuccess: suspend () -> Unit) = withContext(Dispatchers.IO) {
|
||||
val result = suspendCoroutine<BillingResult> { cont ->
|
||||
val listener = object : BillingClientStateListener {
|
||||
override fun onBillingSetupFinished(result: BillingResult) = cont.resume(result)
|
||||
override fun onBillingServiceDisconnected() = Timber.i("Billing service disconnected")
|
||||
}
|
||||
|
||||
Timber.i("Starting billing service")
|
||||
billingClient.startConnection(listener)
|
||||
}
|
||||
|
||||
if (result.responseCode == BillingClient.BillingResponseCode.OK) {
|
||||
Timber.i("Billing service connected")
|
||||
onSuccess()
|
||||
} else {
|
||||
Timber.w("Billing response: ${result.responseCode}")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user