Commit c4bbfecc authored by wanglei's avatar wanglei

[修改]广告代码修改

parent 30eda931
...@@ -23,6 +23,7 @@ class NativeParentView(context: Context, attrs: AttributeSet? = null) : ...@@ -23,6 +23,7 @@ class NativeParentView(context: Context, attrs: AttributeSet? = null) :
FrameLayout(context, attrs) { FrameLayout(context, attrs) {
private val TAG = "NativeParentView" private val TAG = "NativeParentView"
private var removeAll = true private var removeAll = true
var isAdShowed = false
init { init {
// 获取XML属性 // 获取XML属性
...@@ -78,6 +79,7 @@ class NativeParentView(context: Context, attrs: AttributeSet? = null) : ...@@ -78,6 +79,7 @@ class NativeParentView(context: Context, attrs: AttributeSet? = null) :
removeAllViews() removeAllViews()
} }
addView(adView) addView(adView)
isAdShowed = true
} }
fun setNativeAd( fun setNativeAd(
...@@ -102,5 +104,6 @@ class NativeParentView(context: Context, attrs: AttributeSet? = null) : ...@@ -102,5 +104,6 @@ class NativeParentView(context: Context, attrs: AttributeSet? = null) :
setBackgroundResource(0) setBackgroundResource(0)
removeAllViews() removeAllViews()
addView(adView) addView(adView)
isAdShowed = true
} }
} }
package com.base.appzxhy.ads.admob package com.base.appzxhy.ads.admob
import android.content.Context
import android.util.Log
import com.base.appzxhy.GlobalConfig import com.base.appzxhy.GlobalConfig
import com.base.appzxhy.MyApplication
import com.base.appzxhy.ads.AdsType import com.base.appzxhy.ads.AdsType
import com.base.appzxhy.ads.LimitUtils import com.base.appzxhy.ads.LimitUtils
import com.base.appzxhy.ads.NativeParentView import com.base.appzxhy.ads.NativeParentView
...@@ -17,6 +20,8 @@ import java.util.concurrent.ConcurrentLinkedDeque ...@@ -17,6 +20,8 @@ import java.util.concurrent.ConcurrentLinkedDeque
*/ */
class AdNativeMgr { class AdNativeMgr {
private val TAG = "AdNativeMgr"
/** /**
* 上一次的缓存成功时间 * 上一次的缓存成功时间
*/ */
...@@ -27,27 +32,37 @@ class AdNativeMgr { ...@@ -27,27 +32,37 @@ class AdNativeMgr {
*/ */
private val cacheItems = ConcurrentLinkedDeque<NativeAd>() private val cacheItems = ConcurrentLinkedDeque<NativeAd>()
private fun loadAd( fun loadAd(
context: Context,
admobEvent: AdmobEvent, admobEvent: AdmobEvent,
parent: NativeParentView, showAction: ((ad: NativeAd) -> Unit)? = null
layout: Int
) { ) {
admobEvent.adPulStart() admobEvent.adPulStart()
if (!LimitUtils.isAdShow(AdsType.NATIVE, admobEvent)) return if (!LimitUtils.isAdShow(AdsType.NATIVE, admobEvent)) {
// Log.e(TAG, "loadAd return")
return
}
var currentNativeAd: NativeAd? = null var currentNativeAd: NativeAd? = null
val adLoader = AdLoader.Builder( val adLoader = AdLoader.Builder(
parent.context, context,
GlobalConfig.ID_ADMOB_NATIVE GlobalConfig.ID_ADMOB_NATIVE
).forNativeAd { nativeAd -> ).forNativeAd { nativeAd ->
currentNativeAd = nativeAd
cacheItems.offer(nativeAd)
lastTime = System.currentTimeMillis() lastTime = System.currentTimeMillis()
nativeAd.setOnPaidEventListener(AdmobEvent.EventOnPaidEventListener(nativeAd)) nativeAd.setOnPaidEventListener(AdmobEvent.EventOnPaidEventListener(nativeAd))
currentNativeAd = nativeAd
admobEvent.pullAd(nativeAd.responseInfo) admobEvent.pullAd(nativeAd.responseInfo)
show(admobEvent, parent, layout)
if (showAction != null) {
showAction.invoke(nativeAd)
} else {
// Log.e(TAG, "offer ad")
cacheItems.offer(nativeAd)
}
}.withAdListener(object : AdListener() { }.withAdListener(object : AdListener() {
...@@ -64,8 +79,7 @@ class AdNativeMgr { ...@@ -64,8 +79,7 @@ class AdNativeMgr {
super.onAdClosed() super.onAdClosed()
} }
}).withNativeAdOptions( }).withNativeAdOptions(
NativeAdOptions.Builder() NativeAdOptions.Builder().build()
.build()
).build() ).build()
adLoader.loadAds(AdRequest.Builder().build(), 1) adLoader.loadAds(AdRequest.Builder().build(), 1)
} }
...@@ -80,24 +94,44 @@ class AdNativeMgr { ...@@ -80,24 +94,44 @@ class AdNativeMgr {
admobEvent.adPrepareShow() admobEvent.adPrepareShow()
if (!LimitUtils.isAdShow(AdsType.NATIVE, admobEvent)) { if (!LimitUtils.isAdShow(AdsType.NATIVE, admobEvent)) {
Log.e(TAG, "!isAdShow")
cacheItems.clear() cacheItems.clear()
return return
} }
Log.e(TAG, "adNative can show")
val nativeAd = cacheItems.peek() if (!adAvailable()) {
if (nativeAd == null || !adAvailable()) {
//缓存过期了就清空 //缓存过期了就清空
Log.e(TAG, "cacheItems clear")
cacheItems.clear() cacheItems.clear()
loadAd(admobEvent, parent, layout)
return
} }
nativeCallBack?.invoke(nativeAd) parent.isAdShowed = true
parent.setNativeAd(nativeAd, layout) val nativeAd = cacheItems.peek()
admobEvent.showAd(nativeAd.responseInfo) if (nativeAd == null) {
loadAd(parent.context, admobEvent) { ad ->
Log.e(TAG, "load show")
nativeCallBack?.invoke(ad)
parent.setNativeAd(ad, layout)
admobEvent.showAd(ad.responseInfo)
loadAd(MyApplication.appContext, AdmobEvent("nativeAd", "preload"), null)
}
} else {
val flag = cacheItems.remove(nativeAd)
Log.e(TAG, "ready show remove=$flag size=${cacheItems.size}")
nativeCallBack?.invoke(nativeAd)
parent.setNativeAd(nativeAd, layout)
admobEvent.showAd(nativeAd.responseInfo)
loadAd(MyApplication.appContext, AdmobEvent("nativeAd", "preload"), null)
}
} }
private fun adAvailable(): Boolean { private fun adAvailable(): Boolean {
return ((System.currentTimeMillis() - lastTime) / 1000 / 60).toInt() < 30 return (lastTime == 0L) || ((System.currentTimeMillis() - lastTime) / 1000 / 60).toInt() < 30
} }
} }
\ No newline at end of file
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