123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package cn.yyxx.eyuangame.core.impl.iab
- import android.app.Activity
- import android.content.Context
- import cn.yyxx.eyuangame.base.utils.Logger
- import cn.yyxx.eyuangame.core.impl.SdkBridgeImpl
- import cn.yyxx.eyuangame.core.ui.dialog.ScaleLoadingDialog
- import com.android.billingclient.api.BillingClient
- import com.android.billingclient.api.BillingClientStateListener
- import com.android.billingclient.api.BillingResult
- import com.android.billingclient.api.Purchase
- import com.google.android.gms.common.ConnectionResult
- import com.google.android.gms.common.GoogleApiAvailability
- abstract class InAppBilling {
- protected var billingClient: BillingClient? = null
- private var loadingDialog: ScaleLoadingDialog? = null
- protected fun checkGoogleApiAvailability(activity: Activity): Boolean {
- return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity) == ConnectionResult.SUCCESS
- }
-
- protected fun initializeBillingClient(activity: Activity) {
- billingClient = BillingClient.newBuilder(activity).setListener { billingResult, list ->
-
- if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
- if (list.isNullOrEmpty()) {
- purchasesUpdatedFailed()
- } else {
- for (purchase in list) {
- Logger.d(purchase.toString())
- purchase.skus.forEach {
- if (it.equals(SdkBridgeImpl.initBean.rewardId)) {
-
- preRewardPurchasesUpdated(activity, purchase)
- } else {
-
- chargePurchasesUpdated(activity, purchase)
- }
- }
- }
- }
- } else {
- purchasesUpdatedFailed()
- }
- }.enablePendingPurchases().build()
- }
-
- protected fun connectGooglePlay(activity: Activity, isPreReward: Boolean = false) {
- billingClient?.apply {
- if (!isReady) {
- Logger.d("start connection Google Play ...")
- startConnection(object : BillingClientStateListener {
- override fun onBillingSetupFinished(billingResult: BillingResult) {
- logBillingResult("onBillingSetupFinished", billingResult)
- if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
- if (isPreReward) {
- queryRewardInfo(activity)
- } else {
- queryChargeInfo(activity)
- }
- } else {
- connectGooglePlayFailed()
- }
- }
- override fun onBillingServiceDisconnected() {
- Logger.e("onBillingServiceDisconnected")
- connectGooglePlay(activity, isPreReward)
- }
- })
- } else {
- dismissDialog()
- disConnection()
- }
- }
- }
- protected fun disConnection() {
- billingClient?.apply {
- if (isReady) {
- Logger.d("断开谷歌收银台连接,以清空被消耗或者失败的缓存订单")
- endConnection()
- }
- }
- }
- protected fun logBillingResult(callbackFuncName: String, billingResult: BillingResult) {
- val code = billingResult.responseCode
- val msg = billingResult.debugMessage
- Logger.d("$callbackFuncName , code : $code , msg : $msg")
- }
- protected fun showDialog(context: Context) {
- loadingDialog?.apply {
- dismiss()
- loadingDialog = null
- }
- loadingDialog = ScaleLoadingDialog(context, "")
- loadingDialog?.show()
- }
- protected fun dismissDialog() {
- loadingDialog?.apply {
- dismiss()
- loadingDialog = null
- }
- }
-
- protected abstract fun chargePurchasesUpdated(activity: Activity, purchase: Purchase)
-
- protected abstract fun preRewardPurchasesUpdated(activity: Activity, purchase: Purchase)
- protected abstract fun queryRewardInfo(activity: Activity)
- protected abstract fun queryChargeInfo(activity: Activity)
- protected abstract fun purchasesUpdatedFailed()
- protected abstract fun connectGooglePlayFailed()
- }
|