Commit 813e956b authored by wanglei's avatar wanglei

[同步]同步广告问题

parent 285684f1
......@@ -170,21 +170,28 @@ dependencies {
implementation("com.google.firebase:firebase-crashlytics")
implementation("com.google.firebase:firebase-config")
//广告
//admob渠道
implementation(libs.vungle)
implementation(libs.facebook)
implementation(libs.mintegral)
implementation(libs.pangle)
//applovin sdk
implementation(libs.applovin)
//applovin渠道
implementation(libs.applovin.google)
implementation(libs.applovin.admob)
implementation(libs.applovin.facebook) //meta
implementation(libs.applovin.mintegral)//mintegral
implementation(libs.applovin.pangle) //pangle
implementation(libs.applovin.vungle) //vungle
// //广告
// //admob渠道
// implementation(libs.vungle)
// implementation(libs.facebook)
// implementation(libs.mintegral)
// implementation(libs.pangle)
// //applovin sdk
// implementation(libs.applovin)
// //applovin渠道
// implementation(libs.applovin.google)
// implementation(libs.applovin.admob)
// implementation(libs.applovin.facebook) //meta
// implementation(libs.applovin.mintegral)//mintegral
// implementation(libs.applovin.pangle) //pangle
// implementation(libs.applovin.vungle) //vungle
implementation("com.google.android.gms:play-services-ads:23.5.0")
implementation("com.google.ads.mediation:applovin:13.0.1.0")
implementation("com.google.ads.mediation:facebook:6.18.0.0")
implementation("com.google.ads.mediation:mintegral:16.8.61.0")
implementation("com.google.ads.mediation:pangle:6.3.0.4.0")
implementation("com.google.ads.mediation:vungle:7.4.2.0")
val work_version = "2.8.1"
implementation("androidx.work:work-runtime-ktx:$work_version")
......
......@@ -4,20 +4,22 @@ package com.rgt.superr.bean.config
class AdConfigBean(
var isAdShow: Boolean = true,//广告开关
var adSwitch: Boolean = true,//true 走admob,false走max
// var adSwitch: Boolean = true,//true 走admob,false走max
var taichiAdValue: Int = 1,//价值上报阀值
var adRatio: Int = 100,//价值上报随机控制
var numDisplayLimit: Int = -1,//展示次数限制
var numRequestLimit: Int = -1,//请求次数限制
var numClickLimit: Int = -1,//点击次数限制
var timeInterval: Int = 10,//广告间隔秒
var timeInterval: Int = 10,//插页广告间隔秒
var timeIntervalOpen: Int = 0,//开屏广告间隔
var openAdLoading: Int = 15,//开屏广告拉取时间
var numNativeDisplayLimit: Int = -1,//原生广告展示限制
var fullNativeShow: Boolean = true,//显示全屏原生
var functionBackShowAd: Boolean = true,//功能退出显示广告
var functionInShowAd: Boolean = true,//功能进入显示广告
var rvEmptyShowNative: Boolean = true,//列表空的显示原生不
// var rvEmptyShowNative: Boolean = true,//列表空的显示原生不
var requestUmp: Boolean = false,//是否需要请求ump
var showCountdown: Boolean = false,//是否显示倒计时
......
......@@ -6,7 +6,7 @@ package com.rgt.superr.bean.config
*/
data class ConfigBean(
var isInBlackList: Boolean = false,
// var isInBlackList: Boolean = false,
val ut: Int = 0,
var vpnCanUse: Boolean = true,
val adConfigBean: AdConfigBean = AdConfigBean(),
......
package com.rgt.superr.business.ads
import android.app.Dialog
import com.rgt.superr.business.ads.LimitUtils.openInterLastShowTime
import com.rgt.superr.business.ads.AdsType.Companion.INSERT
import com.rgt.superr.business.ads.AdsType.Companion.OPEN
import com.rgt.superr.business.ads.LimitUtils.interLastShowTime
import com.rgt.superr.business.ads.LimitUtils.openLastShowTime
class AdState<T>() {
var adDialog: Dialog? = null
......@@ -24,41 +28,41 @@ class AdState<T>() {
*/
var loadingAd: Boolean = false
/**
* 用于保存引用现有页面,在此页面显示广告(因为要等待广告加载完毕)
*/
// var activityRef: WeakReference<Activity>? = null
/**
* 上一次的缓存成功时间
*/
var lastLoadTime: Long = 0
/**
* 上次展示时间
*/
// var lastShowTime: Long = 0
/**
* 广告已经展示
*/
fun onAdDisplayed() {
fun onAdDisplayed(adsType: AdsType) {
currentAd = null
currentAdEvent = null
// activityRef = null
adDialog?.dismiss()
adDialog = null
openInterLastShowTime = System.currentTimeMillis()
if (adsType == OPEN) {
openLastShowTime = System.currentTimeMillis()
}
if (adsType == INSERT) {
interLastShowTime = System.currentTimeMillis()
}
}
fun onAdHidden() {
fun onAdHidden(adsType: AdsType) {
//重置下上次展示的时间,避免看广告的时间算入间隔
openInterLastShowTime = System.currentTimeMillis()
if (adsType == OPEN) {
openLastShowTime = System.currentTimeMillis()
}
if (adsType == INSERT) {
interLastShowTime = System.currentTimeMillis()
}
}
......@@ -68,17 +72,16 @@ class AdState<T>() {
currentAd = null
currentAdEvent = null
// activityRef = null
}
fun onAdLoaded(ad: T?, adEvent: AdEvent?) {
//这里可能提前设置,所有可以不设置,max回调的类型可能不同
if (ad != null) {
currentAd = ad
}
if (adEvent != null) {
currentAdEvent = adEvent
}
fun loadStart(adEvent: AdEvent) {
loadingAd = true
currentAdEvent = adEvent
}
fun onAdLoaded(ad: T?) {
currentAd = ad
loadingAd = false
lastLoadTime = System.currentTimeMillis()
}
......@@ -90,7 +93,6 @@ class AdState<T>() {
}
fun adAvailable() =
currentAd != null || ((System.currentTimeMillis() - lastLoadTime) / 1000 / 60).toInt() < 30
fun needLoad() = currentAd == null || ((System.currentTimeMillis() - lastLoadTime) / 1000 / 60).toInt() > 30
}
......@@ -17,10 +17,10 @@ import com.rgt.superr.business.ads.admob.AdInterMgr
import com.rgt.superr.business.ads.admob.AdNativeMgr
import com.rgt.superr.business.ads.admob.AdOpenMgr
import com.rgt.superr.business.ads.admob.AdmobEvent
import com.rgt.superr.business.ads.applovin.AdMaxEvent
import com.rgt.superr.business.ads.applovin.MaxInsertMgr
import com.rgt.superr.business.ads.applovin.MaxNativeMgr
import com.rgt.superr.business.ads.applovin.MaxOpenMgr
//import com.rgt.superr.business.ads.applovin.AdMaxEvent
//import com.rgt.superr.business.ads.applovin.MaxInsertMgr
//import com.rgt.superr.business.ads.applovin.MaxNativeMgr
//import com.rgt.superr.business.ads.applovin.MaxOpenMgr
import com.rgt.superr.business.helper.EventUtils
import com.rgt.superr.utils.AppPreferences
import com.rgt.superr.utils.LogEx
......@@ -49,15 +49,15 @@ object AdsMgr {
AdBannerMgr()
}
private val maxOpenMgr by lazy {
MaxOpenMgr()
}
private val maxInsertMgr by lazy {
MaxInsertMgr()
}
private val maxNativeMgr by lazy {
MaxNativeMgr()
}
// private val maxOpenMgr by lazy {
// MaxOpenMgr()
// }
// private val maxInsertMgr by lazy {
// MaxInsertMgr()
// }
// private val maxNativeMgr by lazy {
// MaxNativeMgr()
// }
/**
* 是否初始化
......@@ -87,12 +87,12 @@ object AdsMgr {
*/
fun init(context: Context) {
if (configBean.isInBlackList) {
EventUtils.event("isInBlackList", value = "isInBlackList=${configBean.isInBlackList}")
return
}
// if (configBean.isInBlackList) {
// EventUtils.event("isInBlackList", value = "isInBlackList=${configBean.isInBlackList}")
// return
// }
initAdmob(context)
initMax(context)
// initMax(context)
}
......@@ -106,7 +106,7 @@ object AdsMgr {
EventUtils.event("AdmobInit", "AdmobInit=$isAdmobInit")
// context.toast("admob init")
if (adsConfigBean.adSwitch) {
if (true) {
admobInitCallBack?.invoke()
admobInitCallBack = null
adNativeMgr.loadAd(context, AdmobEvent("nativeAd", context::class.java.simpleName))
......@@ -117,38 +117,38 @@ object AdsMgr {
}
private fun initMax(context: Context) = kotlin.runCatching {
if (isMaxInit) return@runCatching
val executor = Executors.newSingleThreadExecutor()
executor.execute {
val currentGaid = AdvertisingIdClient.getAdvertisingIdInfo(context).id
AppPreferences.getInstance().getString("gid", currentGaid)
val build = AppLovinSdkInitializationConfiguration
.builder(GlobalConfig.KEY_MAX, context)
build.mediationProvider = AppLovinMediationProvider.MAX
if (BuildConfig.DEBUG) {
build.testDeviceAdvertisingIds = Collections.singletonList(currentGaid)
}
val initConfig = build.build()
runCatching {
AppLovinSdk.getInstance(context).initialize(initConfig) {
isMaxInit = true
// maxOpenMgr.loadAd(context)
if (!adsConfigBean.adSwitch) {
maxInsertMgr.loadAd(context, AdMaxEvent("interAd", context::class.java.simpleName))
context.toast("max init")
maxInitCallBack?.invoke()
maxInitCallBack = null
}
}
}
}
}
// private fun initMax(context: Context) = kotlin.runCatching {
// if (isMaxInit) return@runCatching
// val executor = Executors.newSingleThreadExecutor()
//
// executor.execute {
// val currentGaid = AdvertisingIdClient.getAdvertisingIdInfo(context).id
// AppPreferences.getInstance().getString("gid", currentGaid)
//
// val build = AppLovinSdkInitializationConfiguration
// .builder(GlobalConfig.KEY_MAX, context)
//
// build.mediationProvider = AppLovinMediationProvider.MAX
// if (BuildConfig.DEBUG) {
// build.testDeviceAdvertisingIds = Collections.singletonList(currentGaid)
// }
// val initConfig = build.build()
// runCatching {
// AppLovinSdk.getInstance(context).initialize(initConfig) {
// isMaxInit = true
//// maxOpenMgr.loadAd(context)
// if (!adsConfigBean.adSwitch) {
// maxInsertMgr.loadAd(context, AdMaxEvent("interAd", context::class.java.simpleName))
// context.toast("max init")
// maxInitCallBack?.invoke()
// maxInitCallBack = null
// }
// }
// }
//
// }
//
// }
var admobInitCallBack: (() -> Unit)? = null
var maxInitCallBack: (() -> Unit)? = null
......@@ -172,14 +172,14 @@ object AdsMgr {
return
}
if (configBean.isInBlackList) {
EventUtils.event("isInBlackList", "isInBlackList=${configBean.isInBlackList}")
showCallBack?.failed()
return
}
// if (configBean.isInBlackList) {
// EventUtils.event("isInBlackList", "isInBlackList=${configBean.isInBlackList}")
// showCallBack?.failed()
// return
// }
val from = activity::class.java.simpleName
if (adsConfigBean.adSwitch) {
if (true) {
val admobEvent = AdmobEvent("openAd", from).apply { this.isUnLimit = isUnLimit }
if (isAdmobInit) {
adOpenMgr.show(activity, admobEvent, showCallBack)
......@@ -189,13 +189,13 @@ object AdsMgr {
adOpenMgr.show(activity, admobEvent, showCallBack)
}
} else {
if (isMaxInit) {
maxOpenMgr.show(activity, isUnLimit, AdMaxEvent("openAd", from), showCallBack)
} else {
maxInitCallBack = {
maxOpenMgr.show(activity, isUnLimit, AdMaxEvent("openAd", from), showCallBack)
}
}
// if (isMaxInit) {
// maxOpenMgr.show(activity, isUnLimit, AdMaxEvent("openAd", from), showCallBack)
// } else {
// maxInitCallBack = {
// maxOpenMgr.show(activity, isUnLimit, AdMaxEvent("openAd", from), showCallBack)
// }
// }
}
}
......@@ -219,17 +219,16 @@ object AdsMgr {
return
}
if (configBean.isInBlackList) {
EventUtils.event("isInBlackList", configBean.isInBlackList.toString())
showCallBack?.failed()
return
}
LogEx.logDebug("showAd", "adSwitch=${adsConfigBean.adSwitch}")
// if (configBean.isInBlackList) {
// EventUtils.event("isInBlackList", configBean.isInBlackList.toString())
// showCallBack?.failed()
// return
// }
val from = activity::class.java.simpleName
if (adsConfigBean.adSwitch) {
if (true) {
adInterMgr.show(activity, AdmobEvent("interAd", from).apply { this.isUnLimit = isUnLimit }, showCallBack)
} else {
maxInsertMgr.show(activity, isUnLimit, AdMaxEvent("interAd", from), showCallBack)
// maxInsertMgr.show(activity, isUnLimit, AdMaxEvent("interAd", from), showCallBack)
}
}
......@@ -250,16 +249,16 @@ object AdsMgr {
return
}
nativeView.visibility = View.VISIBLE
if (configBean.isInBlackList) {
EventUtils.event("isInBlackList", configBean.isInBlackList.toString())
return
}
// if (configBean.isInBlackList) {
// EventUtils.event("isInBlackList", configBean.isInBlackList.toString())
// return
// }
val showNative = {
if (adsConfigBean.adSwitch) {
if (true) {
adNativeMgr.show(AdmobEvent("nativeAd", "nativeAd"), nativeView, layout, nativeCallBack)
} else {
maxNativeMgr.show(AdMaxEvent("nativeAd", "nativeAd"), nativeView, layout, nativeCallBack)
// maxNativeMgr.show(AdMaxEvent("nativeAd", "nativeAd"), nativeView, layout, nativeCallBack)
}
}
......@@ -282,11 +281,11 @@ object AdsMgr {
return
}
parent.visibility = View.VISIBLE
if (configBean.isInBlackList) {
EventUtils.event("isInBlackList", configBean.isInBlackList.toString())
return
}
if (adsConfigBean.adSwitch) {
// if (configBean.isInBlackList) {
// EventUtils.event("isInBlackList", configBean.isInBlackList.toString())
// return
// }
if (true) {
adBannerMgr.show(parent, collapsible, adClose)
}
}
......
......@@ -4,6 +4,7 @@ import com.rgt.superr.BuildConfig
import com.rgt.superr.bean.config.AdConfigBean
import com.rgt.superr.utils.AppPreferences
import com.rgt.superr.utils.KotlinExt.toFormatTime4
import com.rgt.superr.utils.LogEx
/**
......@@ -11,6 +12,8 @@ import com.rgt.superr.utils.KotlinExt.toFormatTime4
*
*/
object LimitUtils {
const val NUM_NATIVE_DISPLAY = "local_numNativeDisplayLimit"
const val NUM_DISPLAY = "local_numDisplayLimit"
const val NUM_REQUEST = "local_numRequestLimit"
......@@ -143,24 +146,94 @@ object LimitUtils {
addNum(NUM_CLICK)
}
fun addNativeDisplayNum() {
addNum(NUM_NATIVE_DISPLAY)
}
/**
* 开屏限制
*/
fun isIntervalOpenLimit(adEvent: AdEvent): Boolean {
val passTime = ((System.currentTimeMillis() - openLastShowTime) / 1000).toInt()
val interval = AdConfigBean.adsConfigBean.timeIntervalOpen
val flag = passTime < interval
LogEx.logDebug(adEvent.TAG, "open isIntervalOpenLimit=$flag passTime=$passTime interval=$interval")
if (flag) {
adEvent.adShowError("ad in timeInterval")
}
return flag
}
/**
* 开屏和插页广告的显示间隔限制
* 插屏限制
*/
fun isIntervalLimited(adEvent: AdEvent?): Boolean {
val flag = ((System.currentTimeMillis() - openInterLastShowTime) / 1000).toInt() < (AdConfigBean.adsConfigBean.timeInterval)
fun isIntervalInterLimit(adEvent: AdEvent): Boolean {
val passTime = ((System.currentTimeMillis() - interLastShowTime) / 1000).toInt()
val interval = AdConfigBean.adsConfigBean.timeInterval
val flag = passTime < interval
LogEx.logDebug(
adEvent.TAG,
"inter isIntervalInterLimit=$flag interLastShowTime=$interLastShowTime passTime=$passTime interval=$interval"
)
if (flag) {
adEvent?.adLimited("ad in timeInterval")
adEvent.adShowError("ad in timeInterval")
}
return flag
}
//开屏和插页上一次展示时间共用,避免开屏插页连弹
var openInterLastShowTime = 0L
//开屏上次展示时间
var openLastShowTime = 0L
get() {
return AppPreferences.getInstance().getLong("openLastShowTime", field)
}
set(value) {
field = value
AppPreferences.getInstance().put("openLastShowTime", value, true)
}
//插屏上次展示时间
var interLastShowTime = 0L
get() {
return AppPreferences.getInstance().getLong("openInterLastShowTime", field)
return AppPreferences.getInstance().getLong("interLastShowTime", field)
}
set(value) {
field = value
AppPreferences.getInstance().put("openInterLastShowTime", value, true)
AppPreferences.getInstance().put("interLastShowTime", value, true)
}
}
\ No newline at end of file
/**
* 原生广告是否到达限制
*/
private inline val isNativeLimited: Boolean
get() {
val maxCount = AdConfigBean.adsConfigBean.numNativeDisplayLimit
return maxCount > -1 && AppPreferences.getInstance()
.getInt(NUM_NATIVE_DISPLAY, 0) >= maxCount
}
fun isShowNative(adsType: AdsType, adEvent: AdEvent?): Boolean {
val currentDate = System.currentTimeMillis().toFormatTime4()
if (saveDate != currentDate) {
//如果已经不是今天了,就重置个数
saveDate = currentDate
AppPreferences.getInstance().put(NUM_NATIVE_DISPLAY, 0)
}
val flag = isNativeLimited
val todayNumber = AppPreferences.getInstance().getInt(NUM_NATIVE_DISPLAY, 0)
val max = AdConfigBean.adsConfigBean.numNativeDisplayLimit
LogEx.logDebug(adEvent?.TAG ?: "", "native todayNumber=$todayNumber max=$max ")
if (flag) {
val value = "current${getAdEventMsg(adsType)} " +
"show=${todayNumber} " +
"${getAdEventMsg(adsType).lowercase()}_" + "max_show=${max}"
adEvent?.adLimited(value)
}
return !flag
}
}
......@@ -25,9 +25,9 @@ class AdBannerMgr {
fun show(parent: ViewGroup, collapsible: Boolean, adClose: (() -> Unit)? = null) {
if (!AdConfigBean.adsConfigBean.adSwitch) {
return
}
// if (!AdConfigBean.adsConfigBean.adSwitch) {
// return
// }
val admobEvent = AdmobEvent("banner", "banner")
if (!LimitUtils.isAdShow(AdsType.BANNER, admobEvent)) {
......
......@@ -19,6 +19,7 @@ import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
import com.rgt.superr.business.ads.AdsType.Companion.INSERT
/**
......@@ -26,12 +27,12 @@ import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
*/
class AdInterMgr {
private val TAG = "AdInterMgr"
private var adState = AdState<InterstitialAd>()
private var showCallBack: AdsShowCallBack? = null
//正在加载回调
private var loadingCallBack: (() -> Unit)? = null
private var loadingCallBack: ((flag: Boolean) -> Unit)? = null
fun show(
activity: Activity,
......@@ -43,24 +44,22 @@ class AdInterMgr {
return
}
val nowAdEvent = adEvent
//currentAdEvent!=null 表示有缓存广告,关联reqId
adState.currentAdEvent?.let { nowAdEvent.reqId = it.reqId }
adState.currentAdEvent?.let { adEvent.reqId = it.reqId }
if (!nowAdEvent.isUnLimit) {
if (!LimitUtils.isAdShow(AdsType.INSERT, nowAdEvent)) {
if (!adEvent.isUnLimit) {
if (!LimitUtils.isAdShow(AdsType.INSERT, adEvent)) {
showCallBack?.failed(2)
return
}
if (LimitUtils.isIntervalLimited(nowAdEvent)) {
if (LimitUtils.isIntervalInterLimit(adEvent)) {
showCallBack?.failed(3)
return
}
}
val needLoad = !adState.adAvailable()
// adState.activityRef = WeakReference(activity)
val needLoad = adState.needLoad()
this.showCallBack = showCallBack
if (adState.adDialog == null) {
......@@ -69,24 +68,29 @@ class AdInterMgr {
adState.adDialog?.dismiss()
}
nowAdEvent.adPrepareShow()
adEvent.adPrepareShow()
LogEx.logDebug(adEvent.TAG, "needLoad=$needLoad")
if (needLoad) {
if (!adState.loadingAd) {
LogEx.logDebug(adEvent.TAG, "inter adState !loadingAd")
loadAd(activity, nowAdEvent) {
showReadyAd(activity, nowAdEvent)
loadAd(activity, adEvent) {
if (it) {
showReadyAd(activity, adEvent)
} else {
showCallBack?.adFailed()
}
}
} else {
LogEx.logDebug(adEvent.TAG, "inter adState is loadingAd")
loadingCallBack = {
showReadyAd(activity, nowAdEvent)
showReadyAd(activity, adEvent)
}
}
} else {
LogEx.logDebug(adEvent.TAG, "inter ad ready")
showReadyAd(activity, nowAdEvent)
showReadyAd(activity, adEvent)
}
}
......@@ -94,13 +98,12 @@ class AdInterMgr {
private fun showReadyAd(ac: Activity, adEvent: AdEvent) {
// val ac = adState.activityRef?.get()
val admobEvent = (adEvent as AdmobEvent)
val tag = adEvent.TAG
LogEx.logDebug(tag, "showReadyAd ac=$ac currentAd=${adState.currentAd}")
LogEx.logDebug(tag, "inter showReadyAd ac=${ac.javaClass.simpleName} currentAd=${adState.currentAd}")
if (ac.isFinishing || ac.isDestroyed || adState.currentAd == null) {
LogEx.logDebug(tag, "showReadyAd ac=null isFinishing isDestroyed")
LogEx.logDebug(tag, "inter showReadyAd ac=null isFinishing isDestroyed")
showCallBack?.failed()
adState.onAdDisplayFailed()
return
......@@ -112,12 +115,12 @@ class AdInterMgr {
override fun onAdShowedFullScreenContent() {
super.onAdShowedFullScreenContent()
admobEvent.showAd(responseInfo, ac)
adState.onAdDisplayed()
adState.onAdDisplayed(INSERT)
showCallBack?.show()
LimitUtils.addDisplayNum()
admobEvent.showAd(responseInfo, ac)
}
override fun onAdFailedToShowFullScreenContent(adError: AdError) {
......@@ -133,10 +136,11 @@ class AdInterMgr {
override fun onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent()
adState.onAdHidden()
adState.onAdHidden(INSERT)
showCallBack?.close()
showCallBack = null
loadAd(MyApplication.appContext, AdmobEvent("interAd", "preload"))
}
......@@ -147,7 +151,6 @@ class AdInterMgr {
LimitUtils.addClickNum()
}
}
// val activity = adState.activityRef?.get()
if (AdConfigBean.adsConfigBean.showCountdown) {
createUICountdownTimer(adState.adDialog) {
show(ac)
......@@ -158,10 +161,11 @@ class AdInterMgr {
}
}
fun loadAd(
context: Context,
adEvent: AdEvent,
loadCallBack: (() -> Unit)? = null
loadCallBack: ((flag: Boolean) -> Unit)? = null
) {
if (!adEvent.isUnLimit) {
if (!LimitUtils.isAdShow(AdsType.INSERT, adEvent)) {
......@@ -173,14 +177,12 @@ class AdInterMgr {
}
//避免无效预加载
if (adState.loadingAd && loadCallBack == null && loadingCallBack == null) {
//容错机制
adState.loadingAd = false
if (adState.loadingAd) {
return
}
adState.loadingAd = true
adEvent.adPulStart()
adState.loadStart(adEvent)
InterstitialAd.load(
context, GlobalConfig.ID_ADMOB_INTER, AdRequest.Builder().build(),
......@@ -188,10 +190,10 @@ class AdInterMgr {
override fun onAdLoaded(ad: InterstitialAd) {
val event = (adEvent as AdmobEvent)
ad.onPaidEventListener = AdmobOnPaidEventListener(ad, adEvent.scope)
adState.onAdLoaded(ad, adEvent)
adState.onAdLoaded(ad)
loadCallBack?.invoke()
loadingCallBack?.invoke()
loadCallBack?.invoke(true)
loadingCallBack?.invoke(true)
loadingCallBack = null
LimitUtils.addRequestNum()
......@@ -200,11 +202,14 @@ class AdInterMgr {
override fun onAdFailedToLoad(loadAdError: LoadAdError) {
adState.onAdLoadFailed()
if (loadCallBack != null) {
adState.onAdDisplayFailed()
}
showCallBack?.adFailed()
showCallBack = null
loadCallBack?.invoke(false)
loadingCallBack?.invoke(false)
loadingCallBack = null
(adEvent as AdmobEvent).pullAd(loadAdError.responseInfo, loadAdError)
}
......@@ -214,4 +219,4 @@ class AdInterMgr {
}
}
\ No newline at end of file
}
......@@ -13,6 +13,7 @@ import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdOptions
import com.rgt.superr.business.ads.AdsType.Companion.NATIVE
import java.util.concurrent.ConcurrentLinkedDeque
/**
......@@ -40,7 +41,6 @@ class AdNativeMgr {
admobEvent.adPulStart()
if (!LimitUtils.isAdShow(AdsType.NATIVE, admobEvent)) {
// Log.e(TAG, "loadAd return")
return
}
......@@ -60,7 +60,6 @@ class AdNativeMgr {
if (showAction != null) {
showAction.invoke(nativeAd)
} else {
// Log.e(TAG, "offer ad")
cacheItems.offer(nativeAd)
}
......@@ -93,16 +92,15 @@ class AdNativeMgr {
admobEvent.adPrepareShow()
if (!LimitUtils.isAdShow(AdsType.NATIVE, admobEvent)) {
Log.e(TAG, "!isAdShow")
if (!LimitUtils.isAdShow(NATIVE, admobEvent) || !LimitUtils.isShowNative(NATIVE, admobEvent)) {
cacheItems.clear()
return
}
Log.e(TAG, "adNative can show")
Log.e(admobEvent.TAG, "native can show")
if (!adAvailable()) {
//缓存过期了就清空
Log.e(TAG, "cacheItems clear")
Log.e(admobEvent.TAG, "native cacheItems clear")
cacheItems.clear()
}
......@@ -111,7 +109,7 @@ class AdNativeMgr {
if (nativeAd == null) {
loadAd(parent.context, admobEvent) { ad ->
Log.e(TAG, "load show")
Log.e(admobEvent.TAG, "native load show")
nativeCallBack?.invoke(ad)
parent.setNativeAd(ad, layout)
admobEvent.showAd(ad.responseInfo)
......@@ -121,7 +119,7 @@ class AdNativeMgr {
} else {
val flag = cacheItems.remove(nativeAd)
Log.e(TAG, "ready show remove=$flag size=${cacheItems.size}")
Log.e(admobEvent.TAG, "native ready show remove=$flag size=${cacheItems.size}")
nativeCallBack?.invoke(nativeAd)
parent.setNativeAd(nativeAd, layout)
......
......@@ -52,7 +52,7 @@ class AdOpenMgr {
}
}
val needLoad = !adState.adAvailable()
val needLoad = !adState.needLoad()
this.showCallBack = showCallBack
nowAdEvent.adPrepareShow()
......
......@@ -77,16 +77,17 @@ class AdmobEvent : AdEvent {
key = "ad_pull_error"
}
EventUtils.event(key, ext = obj)
LogEx.logDebug(TAG, "$key obj=$obj")
LogEx.logDebug(TAG, "${key}_$adUnit obj=$obj")
}
fun clickAd(responseInfo: ResponseInfo?) = scope.launch {
val response = responseInfo?.adapterResponses?.getOrNull(0)
val obj = JSONObject()
obj.put("ad_unit", adUnit)
obj.put("from", from)
obj.put("req_id", reqId)
obj.put("source", response?.adSourceName)
obj.put("ad_unit", adUnit)
val credentials = mapOf(
"placementid" to response?.credentials?.get("placementid"),
......@@ -95,7 +96,6 @@ class AdmobEvent : AdEvent {
)
obj.put("credentials", credentials.toString())
obj.put("session_id", responseInfo?.responseId)
obj.put("from", from)
obj.put("networkname", responseInfo?.mediationAdapterClassName)
if (adUnit != "nativeAd") {
EventUtils.event("ad_click", ext = obj)
......@@ -129,11 +129,11 @@ class AdmobEvent : AdEvent {
fun adShowError(adError: AdError) = scope.launch {
val obj = JSONObject()
obj.put("ad_unit", "openAd")
obj.put("from", from)
obj.put("req_id", reqId)
obj.put("reason", adError.message)
obj.put("code", adError.code)
obj.put("from", from)
obj.put("ad_unit", "openAd")
EventUtils.event("ad_show_error", ext = obj)
LogEx.logDebug(TAG, "ad_show_error $obj")
}
......
package com.rgt.superr.business.ads.applovin
import androidx.annotation.LayoutRes
import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxError
import com.applovin.mediation.nativeAds.MaxNativeAdListener
import com.applovin.mediation.nativeAds.MaxNativeAdLoader
import com.applovin.mediation.nativeAds.MaxNativeAdView
import com.rgt.superr.GlobalConfig
import com.rgt.superr.business.ads.AdsType
import com.rgt.superr.business.ads.LimitUtils
import com.rgt.superr.business.ads.NativeParentView
import com.rgt.superr.business.helper.EventUtils
import org.json.JSONObject
import java.util.UUID
/**
*原生广告加载显示管理类
*/
class MaxNativeMgr {
/**
* 上一次的缓存成功时间
*/
protected var lastTime: Long = 0
/**
* 原生广告
*/
private var currentAd: MaxAd? = null
private var currentLoader: MaxNativeAdLoader? = null
private fun loadAd(
adMaxEvent: AdMaxEvent,
parent: NativeParentView,
@LayoutRes layout: Int
) {
if (!LimitUtils.isAdShow(AdsType.NATIVE, adMaxEvent)) return
val reqId = UUID.randomUUID().toString()
val obj = JSONObject()
obj.put("req_id", reqId)
obj.put("ad_type", "nativeAd")
val nativeAdLoader = MaxNativeAdLoader(GlobalConfig.ID_MAX_NATIVE, parent.context)
nativeAdLoader.setNativeAdListener(object : MaxNativeAdListener() {
override fun onNativeAdLoaded(nativeAdView: MaxNativeAdView?, ad: MaxAd) {
currentLoader = nativeAdLoader
currentAd = ad
lastTime = System.currentTimeMillis()
adMaxEvent.pullAd(ad)
nativeAdLoader.setRevenueListener(AdMaxEvent.EventOnPaidEventListener())
show(adMaxEvent, parent, layout)
}
override fun onNativeAdLoadFailed(adUnitId: String, error: MaxError) {
adMaxEvent.pullAd(null, error)
}
override fun onNativeAdClicked(ad: MaxAd) {
}
})
nativeAdLoader.loadAd()
}
fun show(
adMaxEvent: AdMaxEvent,
parent: NativeParentView,
@LayoutRes layout: Int,
nativeCallBack: ((Any?) -> Unit)? = null
) {
if (!LimitUtils.isAdShow(AdsType.NATIVE, adMaxEvent)) {
currentLoader = null
currentAd = null
return
}
val nativeAd = currentAd
val nativeLoader = currentLoader
if ((nativeAd == null || nativeLoader == null).also {
if (it) {
val obj2 = JSONObject()
obj2.put("reason", "no_ad")
obj2.put("ad_unit", "nativeAd")
EventUtils.event("ad_show_error", ext = obj2)
}
} || (!adAvailable()).also {
if (it) {
val obj2 = JSONObject()
obj2.put("ad_unit", "nativeAd")
EventUtils.event("ad_expire", ext = obj2)
}
}) {
//缓存过期了就清空
currentLoader = null
currentAd = null
loadAd(AdMaxEvent("nativeAd", "preload"), parent, layout)
return
}
val obj = JSONObject()
obj.put("ad_unit", "nativeAd")
EventUtils.event("ad_prepare_show", ext = obj)
parent.setNativeAd(nativeLoader!!, nativeAd!!, layout)
nativeCallBack?.invoke(nativeAd)
}
private fun adAvailable(): Boolean {
return ((System.currentTimeMillis() - lastTime) / 1000 / 60).toInt() < 30
}
}
\ No newline at end of file
//package com.rgt.superr.business.ads.applovin
//
//import androidx.annotation.LayoutRes
//import com.applovin.mediation.MaxAd
//import com.applovin.mediation.MaxError
//import com.applovin.mediation.nativeAds.MaxNativeAdListener
//import com.applovin.mediation.nativeAds.MaxNativeAdLoader
//import com.applovin.mediation.nativeAds.MaxNativeAdView
//import com.rgt.superr.GlobalConfig
//import com.rgt.superr.business.ads.AdsType
//import com.rgt.superr.business.ads.LimitUtils
//import com.rgt.superr.business.ads.NativeParentView
//import com.rgt.superr.business.helper.EventUtils
//import org.json.JSONObject
//import java.util.UUID
//
///**
// *原生广告加载显示管理类
// */
//class MaxNativeMgr {
//
// /**
// * 上一次的缓存成功时间
// */
// protected var lastTime: Long = 0
//
// /**
// * 原生广告
// */
// private var currentAd: MaxAd? = null
// private var currentLoader: MaxNativeAdLoader? = null
//
//
// private fun loadAd(
// adMaxEvent: AdMaxEvent,
// parent: NativeParentView,
// @LayoutRes layout: Int
// ) {
//
// if (!LimitUtils.isAdShow(AdsType.NATIVE, adMaxEvent)) return
//
// val reqId = UUID.randomUUID().toString()
// val obj = JSONObject()
// obj.put("req_id", reqId)
// obj.put("ad_type", "nativeAd")
//
// val nativeAdLoader = MaxNativeAdLoader(GlobalConfig.ID_MAX_NATIVE, parent.context)
//
// nativeAdLoader.setNativeAdListener(object : MaxNativeAdListener() {
//
// override fun onNativeAdLoaded(nativeAdView: MaxNativeAdView?, ad: MaxAd) {
// currentLoader = nativeAdLoader
// currentAd = ad
// lastTime = System.currentTimeMillis()
// adMaxEvent.pullAd(ad)
// nativeAdLoader.setRevenueListener(AdMaxEvent.EventOnPaidEventListener())
// show(adMaxEvent, parent, layout)
// }
//
// override fun onNativeAdLoadFailed(adUnitId: String, error: MaxError) {
// adMaxEvent.pullAd(null, error)
// }
//
// override fun onNativeAdClicked(ad: MaxAd) {
//
// }
//
// })
// nativeAdLoader.loadAd()
// }
//
// fun show(
// adMaxEvent: AdMaxEvent,
// parent: NativeParentView,
// @LayoutRes layout: Int,
// nativeCallBack: ((Any?) -> Unit)? = null
// ) {
// if (!LimitUtils.isAdShow(AdsType.NATIVE, adMaxEvent)) {
// currentLoader = null
// currentAd = null
// return
// }
// val nativeAd = currentAd
// val nativeLoader = currentLoader
// if ((nativeAd == null || nativeLoader == null).also {
// if (it) {
// val obj2 = JSONObject()
// obj2.put("reason", "no_ad")
// obj2.put("ad_unit", "nativeAd")
// EventUtils.event("ad_show_error", ext = obj2)
// }
// } || (!adAvailable()).also {
// if (it) {
// val obj2 = JSONObject()
// obj2.put("ad_unit", "nativeAd")
// EventUtils.event("ad_expire", ext = obj2)
// }
// }) {
// //缓存过期了就清空
// currentLoader = null
// currentAd = null
// loadAd(AdMaxEvent("nativeAd", "preload"), parent, layout)
// return
// }
// val obj = JSONObject()
// obj.put("ad_unit", "nativeAd")
// EventUtils.event("ad_prepare_show", ext = obj)
// parent.setNativeAd(nativeLoader!!, nativeAd!!, layout)
// nativeCallBack?.invoke(nativeAd)
// }
//
// private fun adAvailable(): Boolean {
// return ((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