-
Notifications
You must be signed in to change notification settings - Fork 948
[PM-33510] feat: Add Play Billing Library dependency and PlayBillingManager #6680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.x8bit.bitwarden.data.billing.manager | ||
|
|
||
| import android.content.Context | ||
| import com.bitwarden.annotation.OmitFromCoverage | ||
| import com.bitwarden.core.data.manager.dispatcher.DispatcherManager | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
|
|
||
| /** | ||
| * F-Droid implementation of [PlayBillingManager]. Always returns `true` since | ||
| * F-Droid users are eligible for the premium upgrade flow. | ||
| */ | ||
| @OmitFromCoverage | ||
| @Suppress("UnusedParameter") | ||
| class PlayBillingManagerImpl( | ||
| context: Context, | ||
| dispatcherManager: DispatcherManager, | ||
| ) : PlayBillingManager { | ||
|
|
||
| override val isInAppBillingSupportedFlow: StateFlow<Boolean> = | ||
| MutableStateFlow(true) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.x8bit.bitwarden.data.billing.manager | ||
|
|
||
| import kotlinx.coroutines.flow.StateFlow | ||
|
|
||
| /** | ||
| * Manages interactions with the Google Play Billing system. | ||
| */ | ||
| interface PlayBillingManager { | ||
|
|
||
| /** | ||
| * Emits `true` when in-app billing is supported, or `false` otherwise. | ||
| */ | ||
| val isInAppBillingSupportedFlow: StateFlow<Boolean> | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package com.x8bit.bitwarden.data.billing.manager | ||
|
|
||
| import android.content.Context | ||
| import com.android.billingclient.api.BillingClient | ||
| import com.android.billingclient.api.BillingClientStateListener | ||
| import com.android.billingclient.api.BillingConfig | ||
| import com.android.billingclient.api.BillingResult | ||
| import com.android.billingclient.api.GetBillingConfigParams | ||
| import com.android.billingclient.api.PendingPurchasesParams | ||
| import com.bitwarden.annotation.OmitFromCoverage | ||
| import com.bitwarden.core.data.manager.dispatcher.DispatcherManager | ||
| import com.bitwarden.core.data.util.asFailure | ||
| import com.bitwarden.core.data.util.asSuccess | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.suspendCancellableCoroutine | ||
| import kotlin.coroutines.resume | ||
|
|
||
| private const val SUPPORTED_BILLING_COUNTRY = "US" | ||
|
|
||
| /** | ||
| * Standard implementation of [PlayBillingManager] using the Google Play Billing Library. | ||
| * | ||
| * Uses a connect-per-call lifecycle: a new [BillingClient] is created, connected, queried, | ||
| * and disconnected for each call. | ||
| */ | ||
| @OmitFromCoverage | ||
| class PlayBillingManagerImpl( | ||
| private val context: Context, | ||
| dispatcherManager: DispatcherManager, | ||
| ) : PlayBillingManager { | ||
|
|
||
| private val unconfinedScope = | ||
| CoroutineScope(dispatcherManager.unconfined) | ||
|
|
||
| private val mutableIsInAppBillingSupportedFlow = MutableStateFlow(false) | ||
|
|
||
| override val isInAppBillingSupportedFlow: StateFlow<Boolean> = | ||
| mutableIsInAppBillingSupportedFlow.asStateFlow() | ||
|
|
||
| init { | ||
| unconfinedScope.launch { | ||
| mutableIsInAppBillingSupportedFlow.value = | ||
| queryBillingCountry().getOrNull() == SUPPORTED_BILLING_COUNTRY | ||
| } | ||
| } | ||
|
|
||
| private suspend fun queryBillingCountry(): Result<String> { | ||
| val billingClient = BillingClient | ||
| .newBuilder(context) | ||
| .setListener { _, _ -> | ||
| // No-op: we don't handle purchases. | ||
| } | ||
| .enablePendingPurchases( | ||
| PendingPurchasesParams | ||
| .newBuilder() | ||
| .enableOneTimeProducts() | ||
| .build(), | ||
| ) | ||
| .build() | ||
|
|
||
| return billingClient.useConnection { | ||
| if (responseCode != BillingClient.BillingResponseCode.OK) { | ||
| return@useConnection BillingException("Connection failed: $debugMessage") | ||
| .asFailure() | ||
david-livefront marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| val (configResult, billingConfig) = | ||
| billingClient.getBillingConfig() | ||
| if (configResult.responseCode != BillingClient.BillingResponseCode.OK || | ||
| billingConfig == null | ||
| ) { | ||
| BillingException("Config query failed: ${configResult.debugMessage}").asFailure() | ||
| } else { | ||
| billingConfig.countryCode.asSuccess() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Connects to the [BillingClient], executes [block] with the [BillingResult], and guarantees | ||
| * [BillingClient.endConnection] is called when finished. Catches [IllegalStateException] thrown | ||
| * by [BillingClient.startConnection] if the client is already connected or mid-connection. | ||
| */ | ||
| private suspend fun <T> BillingClient.useConnection( | ||
| block: suspend BillingResult.() -> Result<T>, | ||
| ): Result<T> = try { | ||
| block( | ||
| suspendCancellableCoroutine { continuation -> | ||
| startConnection( | ||
| object : BillingClientStateListener { | ||
| override fun onBillingSetupFinished( | ||
| billingResult: BillingResult, | ||
| ) { | ||
| if (continuation.isActive) { | ||
| continuation.resume(billingResult) | ||
| } | ||
| } | ||
|
|
||
| override fun onBillingServiceDisconnected() { | ||
| // No-op: connect-per-call lifecycle, no reconnection. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we don't want to cancel the continuation here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, we don't need it now that we have |
||
| } | ||
| }, | ||
| ) | ||
| }, | ||
| ) | ||
| } catch (e: IllegalStateException) { | ||
| e.asFailure() | ||
| } finally { | ||
| endConnection() | ||
| } | ||
|
|
||
| /** | ||
| * Wraps [BillingClient.getBillingConfigAsync] in a suspend function using | ||
| * [suspendCancellableCoroutine]. | ||
| */ | ||
| private suspend fun BillingClient.getBillingConfig(): Pair<BillingResult, BillingConfig?> = | ||
| suspendCancellableCoroutine { continuation -> | ||
| val params = GetBillingConfigParams.newBuilder().build() | ||
| getBillingConfigAsync(params) { billingResult, billingConfig -> | ||
| if (continuation.isActive) { | ||
| continuation.resume(billingResult to billingConfig) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Exception type for billing-specific errors. | ||
| */ | ||
| private class BillingException(message: String) : Exception(message) | ||
Uh oh!
There was an error while loading. Please reload this page.