Commit c4bbfecc authored by wanglei's avatar wanglei

[修改]广告代码修改

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