Add more api calls for billing integration.

This commit is contained in:
Alex Hart
2024-08-19 15:32:45 -03:00
committed by mtang-signal
parent 26e79db057
commit 478e3a7233
3 changed files with 104 additions and 12 deletions

View File

@@ -5,12 +5,22 @@
package org.thoughtcrime.securesms.billing
import android.app.Activity
/**
* Variant interface for the BillingApi.
*/
interface GooglePlayBillingApi {
fun isApiAvailable(): Boolean = false
suspend fun queryProducts() {}
suspend fun queryProducts() = Unit
/**
* Queries the user's current purchases. This enqueues a check and will
* propagate it to the normal callbacks in the api.
*/
suspend fun queryPurchases() = Unit
suspend fun launchBillingFlow(activity: Activity) = Unit
/**
* Empty implementation, to be used when play services are available but

View File

@@ -1,7 +1,10 @@
package org.thoughtcrime.securesms.billing
import android.app.Activity
import android.content.Context
import com.android.billingclient.api.BillingClient.BillingResponseCode
import com.android.billingclient.api.ProductDetailsResult
import com.android.billingclient.api.PurchasesUpdatedListener
import org.signal.billing.BillingApi
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.util.RemoteConfig
@@ -29,7 +32,25 @@ private class GooglePlayBillingApiImpl(context: Context) : GooglePlayBillingApi
val TAG = Log.tag(GooglePlayBillingApiImpl::class)
}
private val billingApi: BillingApi = BillingApi.getOrCreate(context)
private val purchasesUpdatedListener = PurchasesUpdatedListener { billingResult, purchases ->
when {
billingResult.responseCode == BillingResponseCode.OK && purchases != null -> {
Log.d(TAG, "purchasesUpdatedListener: ${purchases.size} purchases.")
purchases.forEach {
// Handle purchases.
}
}
billingResult.responseCode == BillingResponseCode.USER_CANCELED -> {
// Handle user cancelled
Log.d(TAG, "purchasesUpdatedListener: User cancelled.")
}
else -> {
Log.d(TAG, "purchasesUpdatedListener: No purchases.")
}
}
}
private val billingApi: BillingApi = BillingApi.getOrCreate(context, purchasesUpdatedListener)
override fun isApiAvailable(): Boolean = billingApi.areSubscriptionsSupported()
@@ -38,4 +59,15 @@ private class GooglePlayBillingApiImpl(context: Context) : GooglePlayBillingApi
Log.d(TAG, "queryProducts: ${products.billingResult.responseCode}, ${products.billingResult.debugMessage}")
}
override suspend fun queryPurchases() {
Log.d(TAG, "queryPurchases")
val purchaseResult = billingApi.queryPurchases()
purchasesUpdatedListener.onPurchasesUpdated(purchaseResult.billingResult, purchaseResult.purchasesList)
}
override suspend fun launchBillingFlow(activity: Activity) {
billingApi.launchBillingFlow(activity)
}
}