Open billing connection for the subscription sync service.

This commit is contained in:
sictiru 2022-01-08 22:21:27 -08:00
parent 2f18de918d
commit 5091814f84
2 changed files with 29 additions and 17 deletions

View file

@ -7,11 +7,11 @@ import android.app.job.JobService
import android.content.ComponentName
import android.content.Context
import com.newsblur.subscription.SubscriptionManagerImpl
import com.newsblur.subscription.SubscriptionsListener
import com.newsblur.util.AppConstants
import com.newsblur.util.Log
import com.newsblur.util.NBScope
import com.newsblur.util.PrefsUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
/**
@ -24,6 +24,8 @@ import kotlinx.coroutines.launch
*/
class SubscriptionSyncService : JobService() {
private val scope = NBScope
override fun onStartJob(params: JobParameters?): Boolean {
Log.d(this, "onStartJob")
if (!PrefsUtils.hasCookie(this)) {
@ -31,15 +33,22 @@ class SubscriptionSyncService : JobService() {
return false
}
NBScope.launch(Dispatchers.Default) {
val subscriptionManager = SubscriptionManagerImpl(this@SubscriptionSyncService, this)
val job = subscriptionManager.syncActiveSubscription()
job.invokeOnCompletion {
Log.d(this, "sync active subscription completed.")
// manually trigger jobFinished after work is done
val subscriptionManager = SubscriptionManagerImpl(this@SubscriptionSyncService, scope)
subscriptionManager.startBillingConnection(object : SubscriptionsListener {
override fun onBillingConnectionReady() {
scope.launch {
subscriptionManager.syncActiveSubscription()
Log.d(this, "sync active subscription completed.")
// manually call jobFinished after work is done
jobFinished(params, false)
}
}
override fun onBillingConnectionError(message: String?) {
// manually call jobFinished on error
jobFinished(params, false)
}
}
})
return true // returning true due to background thread work
}

View file

@ -25,7 +25,6 @@ import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.text.DateFormat
@ -58,7 +57,7 @@ interface SubscriptionManager {
/**
* Sync subscription state between NewsBlur and Play Store.
*/
fun syncActiveSubscription(): Job
suspend fun syncActiveSubscription()
/**
* Notify backend of active Play Store subscription.
@ -70,13 +69,13 @@ interface SubscriptionManager {
interface SubscriptionsListener {
fun onActiveSubscription(renewalMessage: String?)
fun onActiveSubscription(renewalMessage: String?) {}
fun onAvailableSubscription(skuDetails: SkuDetails)
fun onAvailableSubscription(skuDetails: SkuDetails) {}
fun onBillingConnectionReady()
fun onBillingConnectionReady() {}
fun onBillingConnectionError(message: String? = null)
fun onBillingConnectionError(message: String? = null) {}
}
class SubscriptionManagerImpl(
@ -90,7 +89,9 @@ class SubscriptionManagerImpl(
when (billingResult.responseCode) {
BillingClient.BillingResponseCode.OK -> {
Log.d(this, "acknowledgePurchaseResponseListener OK")
syncActiveSubscription()
scope.launch(Dispatchers.Default) {
syncActiveSubscription()
}
}
BillingClient.BillingResponseCode.BILLING_UNAVAILABLE -> {
// Billing API version is not supported for the type requested.
@ -174,7 +175,7 @@ class SubscriptionManagerImpl(
billingClient.launchBillingFlow(activity, billingFlowParams)
}
override fun syncActiveSubscription() = scope.launch(Dispatchers.Default) {
override suspend fun syncActiveSubscription() {
val hasNewsBlurSubscription = PrefsUtils.getIsPremium(context)
val activePlayStoreSubscription = getActiveSubscriptionAsync().await()
@ -258,7 +259,9 @@ class SubscriptionManagerImpl(
private fun handlePurchase(purchase: Purchase) {
Log.d(this, "handlePurchase: ${purchase.orderId}")
if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED && purchase.isAcknowledged) {
syncActiveSubscription()
scope.launch(Dispatchers.Default) {
syncActiveSubscription()
}
} else if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED && !purchase.isAcknowledged) {
// need to acknowledge first time sub otherwise it will void
Log.d(this, "acknowledge purchase: ${purchase.orderId}")