Commit 126e5240 authored by wanglei's avatar wanglei

[商业化]优化广告事件上报链

parent e18d1f4d
......@@ -16,6 +16,11 @@ class AdState<T>() {
*/
var currentAd: T? = null
/**
* 当前缓存广告对应的event
*/
var currentAdEvent: AdEvent? = null
/**
* 是否正在缓存加载广告
*/
......@@ -43,6 +48,7 @@ class AdState<T>() {
*/
fun onAdDisplayed() {
currentAd = null
currentAdEvent = null
activityRef = null
adDialog?.dismiss()
......@@ -63,14 +69,18 @@ class AdState<T>() {
adDialog = null
currentAd = null
currentAdEvent = null
activityRef = null
}
fun onAdLoaded(ad: T?) {
fun onAdLoaded(ad: T?, adEvent: AdEvent?) {
//这里可能提前设置,所有可以不设置,max回调的类型可能不同
if (ad != null) {
currentAd = ad
}
if (adEvent != null) {
currentAdEvent = adEvent
}
loadingAd = false
lastLoadTime = System.currentTimeMillis()
}
......@@ -82,4 +92,7 @@ class AdState<T>() {
}
fun adAvailable() =
currentAd != null || ((System.currentTimeMillis() - lastLoadTime) / 1000 / 60).toInt() < 30
}
......@@ -183,13 +183,12 @@ object AdsMgr {
val from = activity::class.java.simpleName
if (adsConfigBean.adSwitch) {
val admobEvent = AdmobEvent("openAd", from)
admobEvent.isUnLimit = isUnLimit
if (isAdmobInit) {
adOpenMgr.show(activity, admobEvent, showCallBack)
adOpenMgr.show(activity, isUnLimit, admobEvent, showCallBack)
} else {
admobInitCallBack = {
}
adOpenMgr.show(activity, admobEvent, showCallBack)
adOpenMgr.show(activity, isUnLimit, admobEvent, showCallBack)
}
} else {
if (isMaxInit) {
......
......@@ -26,10 +26,13 @@ import java.lang.ref.WeakReference
*/
class AdInterMgr {
private val TAG="AdInterMgr"
private val TAG = "AdInterMgr"
private var adState = AdState<InterstitialAd>()
private var showCallBack: AdsShowCallBack? = null
//正在加载回调
private var loadingCallBack: (() -> Unit)? = null
fun show(
activity: Activity,
isUnLimit: Boolean,
......@@ -43,39 +46,56 @@ class AdInterMgr {
return
}
if (!isUnLimit) {
if (!LimitUtils.isAdShow(AdsType.INSERT, adEvent)) {
val nowAdEvent = adState.currentAdEvent ?: adEvent
nowAdEvent.from = adEvent.from
nowAdEvent.isUnLimit = isUnLimit
if (!nowAdEvent.isUnLimit) {
if (!LimitUtils.isAdShow(AdsType.INSERT, nowAdEvent)) {
showCallBack?.failed(2)
return
}
if (LimitUtils.isIntervalLimited(adEvent)) {
if (LimitUtils.isIntervalLimited(nowAdEvent)) {
showCallBack?.failed(3)
return
}
}
adEvent.adPrepareShow()
val needLoad = !adState.adAvailable()
adState.activityRef = WeakReference(activity)
this.showCallBack = showCallBack
if (adState.adDialog == null) {
adState.adDialog = activity.showAdCountDownDialog()
} else {
adState.adDialog?.dismiss()
}
val needLoad = adState.currentAd == null || !adAvailable()
nowAdEvent.adPrepareShow()
LogEx.logDebug(adEvent.TAG, "needLoad=$needLoad")
if (needLoad) {
if (!adState.loadingAd) {
loadAd(activity, adEvent, isUnLimit) {
showReadyAd(adEvent)
LogEx.logDebug(adEvent.TAG, "inter adState !loadingAd")
loadAd(activity, adEvent) {
showReadyAd()
}
} else {
LogEx.logDebug(adEvent.TAG, "inter adState is loadingAd")
loadingCallBack = {
showReadyAd()
}
}
} else {
showReadyAd(adEvent)
LogEx.logDebug(adEvent.TAG, "inter ad ready")
showReadyAd()
}
}
private fun showReadyAd(adEvent: AdEvent) {
private fun showReadyAd() {
val ac = adState.activityRef?.get()
if (ac == null || ac.isFinishing || ac.isDestroyed) {
......@@ -83,12 +103,15 @@ class AdInterMgr {
return
}
adState.currentAd?.run {
val adEvent = (adState.currentAdEvent as AdmobEvent?)
fullScreenContentCallback = object : FullScreenContentCallback() {
override fun onAdShowedFullScreenContent() {
super.onAdShowedFullScreenContent()
val ac = adState.activityRef?.get()
(adEvent as AdmobEvent).showAd(responseInfo, ac)
adEvent?.showAd(responseInfo, ac)
adState.onAdDisplayed()
showCallBack?.show()
......@@ -103,7 +126,7 @@ class AdInterMgr {
showCallBack?.adFailed()
showCallBack = null
adEvent.adShowError(adError)
adEvent?.adShowError(adError)
}
override fun onAdDismissedFullScreenContent() {
......@@ -118,7 +141,7 @@ class AdInterMgr {
override fun onAdClicked() {
super.onAdClicked()
(adEvent as AdmobEvent).clickAd(responseInfo)
adEvent?.clickAd(responseInfo)
//计数
LimitUtils.addClickNum()
}
......@@ -135,10 +158,9 @@ class AdInterMgr {
fun loadAd(
context: Context,
adEvent: AdEvent,
isUnLimit: Boolean = false,
loadCallBack: (() -> Unit)? = null
) {
if (!isUnLimit) {
if (!adEvent.isUnLimit) {
if (!LimitUtils.isAdShow(AdsType.INSERT, adEvent)) {
this.showCallBack?.close(4)
this.showCallBack = null
......@@ -147,14 +169,27 @@ class AdInterMgr {
}
}
//避免无效预加载
if (adState.loadingAd && loadCallBack == null && loadingCallBack == null) {
//容错机制
adState.loadingAd = false
return
}
adState.loadingAd = true
adState.currentAdEvent = adEvent
adEvent.adPulStart()
InterstitialAd.load(
context, GlobalConfig.ID_ADMOB_INTER, AdRequest.Builder().build(),
object : InterstitialAdLoadCallback() {
override fun onAdLoaded(ad: InterstitialAd) {
adState.onAdLoaded(ad)
adState.onAdLoaded(ad, adEvent)
loadCallBack?.invoke()
loadingCallBack?.invoke()
loadingCallBack = null
(adEvent as AdmobEvent).pullAd(ad.responseInfo)
LimitUtils.addRequestNum()
ad.onPaidEventListener = AdmobOnPaidEventListener(adEvent.scope)
......@@ -172,5 +207,4 @@ class AdInterMgr {
}
private fun adAvailable() = ((System.currentTimeMillis() - adState.lastLoadTime) / 1000 / 60).toInt() < 30
}
\ No newline at end of file
......@@ -27,43 +27,58 @@ class AdOpenMgr {
private val adState = AdState<AppOpenAd>()
private var showCallBack: AdsShowCallBack? = null
//正在加载回调
private var loadingCallBack: (() -> Unit)? = null
fun show(activity: Activity, adEvent: AdEvent, showCallBack: AdsShowCallBack?) {
fun show(
activity: Activity,
isUnLimit: Boolean,
adEvent: AdEvent,
showCallBack: AdsShowCallBack?
) {
if (activity.isFinishing || activity.isDestroyed) {
return
}
if (!adEvent.isUnLimit) {
if (!LimitUtils.isAdShow(AdsType.OPEN, adEvent)) {
val nowAdEvent = adState.currentAdEvent ?: adEvent
nowAdEvent.from = adEvent.from
nowAdEvent.isUnLimit = isUnLimit
if (!nowAdEvent.isUnLimit) {
if (!LimitUtils.isAdShow(AdsType.OPEN, nowAdEvent)) {
showCallBack?.failed()
return
}
if (LimitUtils.isIntervalLimited(adEvent)) {
if (LimitUtils.isIntervalLimited(nowAdEvent)) {
showCallBack?.failed()
return
}
}
adEvent.adPrepareShow()
val needLoad = !adState.adAvailable()
adState.activityRef = WeakReference(activity)
this.showCallBack = showCallBack
// if (adState.adDialog == null) {
// adState.adDialog = activity.showAdPreparingDialog(1)
// } else {
// adState.adDialog?.dismiss()
// }
val needLoad = adState.currentAd == null || !adAvailable()
nowAdEvent.adPrepareShow()
if (needLoad) {
if (!adState.loadingAd) {
LogEx.logDebug(adEvent.TAG, "open adState !loadingAd")
loadAd(activity, adEvent) {
showReadyAd(adEvent)
showReadyAd()
}
} else {
LogEx.logDebug(adEvent.TAG, "open adState is loadingAd")
loadingCallBack = {
showReadyAd()
}
}
} else {
showReadyAd(adEvent)
LogEx.logDebug(adEvent.TAG, "open ad ready")
showReadyAd()
}
}
private fun showReadyAd(adEvent: AdEvent) {
private fun showReadyAd() {
val ac = adState.activityRef?.get()
if (ac == null || ac.isFinishing || ac.isDestroyed) {
......@@ -72,10 +87,13 @@ class AdOpenMgr {
}
adState.currentAd?.run {
val adEvent = adState.currentAdEvent as AdmobEvent?
fullScreenContentCallback = object : FullScreenContentCallback() {
override fun onAdShowedFullScreenContent() {
(adEvent as AdmobEvent).showAd(this@run.responseInfo, ac)
adEvent?.showAd(this@run.responseInfo, ac)
showCallBack?.show()
......@@ -92,7 +110,7 @@ class AdOpenMgr {
showCallBack = null
adState.onAdDisplayFailed()
(adEvent as AdmobEvent).adShowError(adError)
adEvent?.adShowError(adError)
}
......@@ -110,7 +128,7 @@ class AdOpenMgr {
}
override fun onAdClicked() {
(adEvent as AdmobEvent).clickAd(this@run.responseInfo)
adEvent?.clickAd(this@run.responseInfo)
//计数
LimitUtils.addClickNum()
}
......@@ -135,6 +153,15 @@ class AdOpenMgr {
}
}
//避免无效预加载
if (adState.loadingAd && loadCallBack == null && loadingCallBack == null) {
//容错机制
adState.loadingAd = false
return
}
adState.loadingAd = true
adState.currentAdEvent = adEvent
adEvent.adPulStart()
AppOpenAd.load(
......@@ -143,8 +170,12 @@ class AdOpenMgr {
AdRequest.Builder().build(),
object : AppOpenAd.AppOpenAdLoadCallback() {
override fun onAdLoaded(appOpenAd: AppOpenAd) {
adState.onAdLoaded(appOpenAd)
adState.onAdLoaded(appOpenAd, adEvent)
loadCallBack?.invoke()
loadingCallBack?.invoke()
loadingCallBack = null
(adEvent as AdmobEvent).pullAd(appOpenAd.responseInfo)
appOpenAd.onPaidEventListener = AdmobOnPaidEventListener(appOpenAd, adEvent.scope)
LimitUtils.addRequestNum()
......@@ -160,5 +191,4 @@ class AdOpenMgr {
)
}
private fun adAvailable() = ((System.currentTimeMillis() - adState.lastLoadTime) / 1000 / 60).toInt() < 30
}
\ No newline at end of file
......@@ -27,7 +27,6 @@ import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.ktx.Firebase
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import org.json.JSONObject
......
......@@ -145,7 +145,7 @@ class MaxInsertMgr {
override fun onAdDisplayFailed(p0: MaxAd, p1: MaxError) = Unit
override fun onAdLoaded(ad: MaxAd) {
adState.onAdLoaded(null)
adState.onAdLoaded(null, adEvent)
val ac = adState.activityRef?.get()
if (ac != null) {
show(ac, isUnLimit, adEvent, null)
......
......@@ -126,7 +126,7 @@ class MaxOpenMgr {
adState.currentAd = MaxAppOpenAd(GlobalConfig.ID_MAX_OPEN, context)
adState.currentAd?.setListener(object : MaxAdListener {
override fun onAdLoaded(ad: MaxAd) {
adState.onAdLoaded(null)
adState.onAdLoaded(null, adEvent)
val ac = adState.activityRef?.get()
if (ac != null) {
show(ac, isUnLimit, adEvent, null)
......
......@@ -11,12 +11,14 @@ class GuideCleanActivity : BaseActivity<ActivityGuideCleanBinding>(ActivityGuide
override fun initView() {
super.initView()
changeLanguage(this)
val flag = changeLanguage(this)
if (!flag) {
changeNative()
}
}
override fun initListener() {
super.initListener()
changeNative()
binding.llPhoto.setOnClickListener {
binding.iv1.isSelected = !binding.iv1.isSelected
changeNative()
......@@ -48,8 +50,10 @@ class GuideCleanActivity : BaseActivity<ActivityGuideCleanBinding>(ActivityGuide
private fun changeNative() {
AdsMgr.showNative(binding.flAd, R.layout.layout_admob_native_custom) {
binding.flAd.removeAllViews()
binding.flAd.visibility = View.VISIBLE
if (it == null) {
binding.flAd.removeAllViews()
binding.flAd.visibility = View.VISIBLE
}
}
}
}
\ No newline at end of file
......@@ -71,7 +71,7 @@ class LanguageActivity : BaseActivity<ActivityLanguageBinding>(ActivityLanguageB
appLanguageSp = selectLanguageBean.language
appLanguageCountrySp = selectLanguageBean.country
AdsMgr.showInsert(this, showCallBack = object : AdsShowCallBack() {
AdsMgr.showInsert(this, true, showCallBack = object : AdsShowCallBack() {
override fun next() {
goToAc(GuideCleanActivity::class.java)
finish()
......
......@@ -92,7 +92,7 @@ class SplashActivity : BaseActivity<ActivitySplashBinding>(ActivitySplashBinding
jumpNext()
}
viewModel.onTick = { s, t, p ->
Log.e(TAG, "onTick $s $t")
// Log.e(TAG, "onTick $s $t")
binding.progressBar.setProgress(p.toInt(), false)
}
}
......
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