Commit 70df2429 authored by wanglei's avatar wanglei

[AI重构]AI重构插页广告

parent a22342fc
...@@ -21,32 +21,44 @@ import com.google.android.gms.ads.interstitial.InterstitialAd ...@@ -21,32 +21,44 @@ import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
import com.simplecleaner.app.business.ads.admob.AdmobEvent.AdmobOnPaidEventListener import com.simplecleaner.app.business.ads.admob.AdmobEvent.AdmobOnPaidEventListener
/** /**
*插屏广告加载显示管理类 * 插屏广告加载显示管理类(重构版)
*/ */
class AdInterMgr { class AdInterMgr {
// 内部状态管理
private data class InterAdState(
var currentAd: InterstitialAd? = null,
var loading: Boolean = false,
var adDialog: android.app.Dialog? = null,
var showCallBack: AdsShowCallBack? = null,
var loadingCallBack: ((Boolean) -> Unit)? = null,
var currentAdEvent: AdEvent? = null
) {
fun reset() {
currentAd = null
loading = false
adDialog?.dismiss()
adDialog = null
showCallBack = null
loadingCallBack = null
currentAdEvent = null
}
}
private var adState = AdState<InterstitialAd>() private val state = InterAdState()
private var showCallBack: AdsShowCallBack? = null
//正在加载回调
private var loadingCallBack: ((flag: Boolean) -> Unit)? = null
/**
* 展示插屏广告
*/
fun show( fun show(
activity: Activity, activity: Activity,
adEvent: AdEvent, adEvent: AdEvent,
showCallBack: AdsShowCallBack? = null showCallBack: AdsShowCallBack? = null
) { ) {
if (activity.isFinishing || activity.isDestroyed) return
if (activity.isFinishing || activity.isDestroyed) { // 关联reqId
return state.currentAdEvent?.let { adEvent.reqId = it.reqId }
} // 限流判断
//currentAdEvent!=null 表示有缓存广告,关联reqId
adState.currentAdEvent?.let { adEvent.reqId = it.reqId }
if (!adEvent.isUnLimit) { if (!adEvent.isUnLimit) {
if (!LimitUtils.isAdShow(AdsType.INSERT, adEvent)) { if (!LimitUtils.isAdShow(AdsType.INSERT, adEvent)) {
showCallBack?.failed(2) showCallBack?.failed(2)
...@@ -57,166 +69,115 @@ class AdInterMgr { ...@@ -57,166 +69,115 @@ class AdInterMgr {
return return
} }
} }
state.showCallBack = showCallBack
// 弹窗管理
val needLoad = adState.needLoad() state.adDialog?.dismiss()
this.showCallBack = showCallBack state.adDialog = activity.showAdCountDownDialog()
if (adState.adDialog == null) {
adState.adDialog = activity.showAdCountDownDialog()
} else {
adState.adDialog?.dismiss()
}
adEvent.adPrepareShow() adEvent.adPrepareShow()
// 加载或直接展示
LogEx.logDebug(adEvent.TAG, "needLoad=$needLoad") if (state.currentAd == null) {
if (needLoad) { if (!state.loading) {
loadAd(activity, adEvent) { success ->
if (!adState.loadingAd) { if (success) showReadyAd(activity, adEvent)
LogEx.logDebug(adEvent.TAG, "inter adState !loadingAd") else state.showCallBack?.adFailed()
loadAd(activity, adEvent) {
if (it) {
showReadyAd(activity, adEvent)
} else {
showCallBack?.adFailed()
}
} }
} else { } else {
LogEx.logDebug(adEvent.TAG, "inter adState is loadingAd") state.loadingCallBack = { showReadyAd(activity, adEvent) }
loadingCallBack = {
showReadyAd(activity, adEvent)
}
} }
} else { } else {
LogEx.logDebug(adEvent.TAG, "inter ad ready")
showReadyAd(activity, adEvent) showReadyAd(activity, adEvent)
} }
} }
/**
private fun showReadyAd(ac: Activity, adEvent: AdEvent) { * 展示已准备好的广告
*/
val admobEvent = (adEvent as AdmobEvent) private fun showReadyAd(activity: Activity, adEvent: AdEvent) {
val tag = adEvent.TAG val admobEvent = adEvent as? AdmobEvent ?: return
val ad = state.currentAd
LogEx.logDebug(tag, "inter showReadyAd ac=${ac.javaClass.simpleName} currentAd=${adState.currentAd}") if (activity.isFinishing || activity.isDestroyed || ad == null) {
if (ac.isFinishing || ac.isDestroyed || adState.currentAd == null) { state.showCallBack?.failed()
LogEx.logDebug(tag, "inter showReadyAd ac=null isFinishing isDestroyed") state.reset()
showCallBack?.failed()
adState.onAdDisplayFailed()
return return
} }
ad.fullScreenContentCallback = object : FullScreenContentCallback() {
adState.currentAd?.run {
fullScreenContentCallback = object : FullScreenContentCallback() {
override fun onAdShowedFullScreenContent() { override fun onAdShowedFullScreenContent() {
super.onAdShowedFullScreenContent() state.currentAd = null
state.showCallBack?.show()
adState.onAdDisplayed(AdsType.INSERT)
showCallBack?.show()
LimitUtils.addDisplayNum() LimitUtils.addDisplayNum()
admobEvent.showAd(responseInfo, ac) admobEvent.showAd(ad.responseInfo, activity)
} }
override fun onAdFailedToShowFullScreenContent(adError: AdError) { override fun onAdFailedToShowFullScreenContent(adError: AdError) {
super.onAdFailedToShowFullScreenContent(adError) state.showCallBack?.adFailed()
adState.onAdDisplayFailed()
showCallBack?.adFailed()
showCallBack = null
admobEvent.adShowError(adError) admobEvent.adShowError(adError)
state.reset()
} }
override fun onAdDismissedFullScreenContent() { override fun onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent() state.showCallBack?.close()
state.reset()
adState.onAdHidden(AdsType.INSERT) // 自动预加载
showCallBack?.close()
showCallBack = null
loadAd(MyApplication.appContext, AdmobEvent("interAd", "preload")) loadAd(MyApplication.appContext, AdmobEvent("interAd", "preload"))
} }
override fun onAdClicked() { override fun onAdClicked() {
super.onAdClicked() admobEvent.clickAd(ad.responseInfo)
admobEvent.clickAd(responseInfo)
//计数
LimitUtils.addClickNum() LimitUtils.addClickNum()
} }
} }
if (AdConfigBean.adsConfigBean.showCountdown) { if (AdConfigBean.adsConfigBean.showCountdown) {
createUICountdownTimer(adState.adDialog) { createUICountdownTimer(state.adDialog) { ad.show(activity) }
show(ac)
}
} else { } else {
show(ac) ad.show(activity)
} }
} }
}
/**
* 加载插屏广告
*/
fun loadAd( fun loadAd(
context: Context, context: Context,
adEvent: AdEvent, adEvent: AdEvent,
loadCallBack: ((flag: Boolean) -> Unit)? = null loadCallBack: ((Boolean) -> Unit)? = null
) { ) {
if (!adEvent.isUnLimit) { if (!adEvent.isUnLimit && !LimitUtils.isAdShow(AdsType.INSERT, adEvent)) {
if (!LimitUtils.isAdShow(AdsType.INSERT, adEvent)) { state.showCallBack?.close(4)
this.showCallBack?.close(4) state.reset()
this.showCallBack = null
adState.onAdLoadFailed()
return
}
}
//避免无效预加载
if (adState.loadingAd) {
return return
} }
if (state.loading) return
state.loading = true
adEvent.adPulStart() adEvent.adPulStart()
adState.loadStart(adEvent) state.currentAdEvent = adEvent
InterstitialAd.load( InterstitialAd.load(
context, GlobalConfig.ID_ADMOB_INTER, AdRequest.Builder().build(), context,
GlobalConfig.ID_ADMOB_INTER,
AdRequest.Builder().build(),
object : InterstitialAdLoadCallback() { object : InterstitialAdLoadCallback() {
override fun onAdLoaded(ad: InterstitialAd) { override fun onAdLoaded(ad: InterstitialAd) {
val event = (adEvent as AdmobEvent)
ad.onPaidEventListener = AdmobOnPaidEventListener(ad, adEvent.scope) ad.onPaidEventListener = AdmobOnPaidEventListener(ad, adEvent.scope)
adState.onAdLoaded(ad) state.currentAd = ad
state.loading = false
loadCallBack?.invoke(true) loadCallBack?.invoke(true)
loadingCallBack?.invoke(true) state.loadingCallBack?.invoke(true)
loadingCallBack = null state.loadingCallBack = null
LimitUtils.addRequestNum() LimitUtils.addRequestNum()
event.pullAd(ad.responseInfo) (adEvent as? AdmobEvent)?.pullAd(ad.responseInfo)
}
override fun onAdFailedToLoad(loadAdError: LoadAdError) {
adState.onAdLoadFailed()
if (loadCallBack != null) {
adState.onAdDisplayFailed()
} }
override fun onAdFailedToLoad(error: LoadAdError) {
state.currentAd = null
state.loading = false
loadCallBack?.invoke(false) loadCallBack?.invoke(false)
loadingCallBack?.invoke(false) state.loadingCallBack?.invoke(false)
loadingCallBack = null state.loadingCallBack = null
(adEvent as? AdmobEvent)?.pullAd(error.responseInfo, error)
(adEvent as AdmobEvent).pullAd(loadAdError.responseInfo, loadAdError)
} }
} }
) )
} }
/**
* 主动释放资源
*/
fun clear() {
state.reset()
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment