-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
149 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
billing/src/main/java/de/charlex/billing/BillingClientExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package de.charlex.billing | ||
|
||
import android.util.Log | ||
import com.android.billingclient.api.BillingClient | ||
import com.android.billingclient.api.BillingClientStateListener | ||
import com.android.billingclient.api.BillingResult | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import kotlinx.coroutines.withContext | ||
import kotlin.coroutines.resume | ||
|
||
/** | ||
* Starts up BillingClient setup process suspended if necessary. | ||
* | ||
* @return Boolean | ||
* | ||
* true: The billing client is ready. You can query purchases. | ||
* | ||
* false: The billing client is NOT ready or disconnected. | ||
*/ | ||
suspend fun BillingClient.startConnectionIfNecessary() = suspendCancellableCoroutine<Boolean> { continuation -> | ||
if (!isReady) { | ||
startConnection(object : BillingClientStateListener { | ||
override fun onBillingServiceDisconnected() { | ||
Log.d("BillingHelper", "The billing client is disconnected.") | ||
if (continuation.isActive) { | ||
continuation.resume(false) | ||
} | ||
} | ||
|
||
override fun onBillingSetupFinished(billingResult: BillingResult) { | ||
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) { | ||
Log.d("BillingHelper", "The billing client is ready. You can query purchases.") | ||
continuation.resume(true) | ||
} else { | ||
Log.d("BillingHelper", "The billing client is NOT ready. ${billingResult.debugMessage}") | ||
continuation.resume(false) | ||
} | ||
} | ||
}) | ||
} else { | ||
Log.d("BillingHelper", "The billing client is still ready") | ||
continuation.resume(true) | ||
} | ||
} | ||
|
||
/** | ||
* Closes the connection and releases all held resources such as service connections. | ||
* | ||
* Call this method once you are done with this BillingClient reference. | ||
*/ | ||
suspend fun BillingClient.endConnection() = withContext(Dispatchers.Main) { | ||
Log.d("BillingHelper", "The billing client is still ready") | ||
endConnection() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters