Commit 126e5240 authored by wanglei's avatar wanglei

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

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