Commit cfd8ca08 authored by wanglei's avatar wanglei

...

parent 07a8a13e
...@@ -8,11 +8,14 @@ import android.view.LayoutInflater ...@@ -8,11 +8,14 @@ import android.view.LayoutInflater
import android.view.animation.LinearInterpolator import android.view.animation.LinearInterpolator
import com.base.locationsharewhite.databinding.DialogAdPreparingBinding import com.base.locationsharewhite.databinding.DialogAdPreparingBinding
import com.base.locationsharewhite.utils.DensityUtils import com.base.locationsharewhite.utils.DensityUtils
import com.base.locationsharewhite.utils.LogEx
object AdDialog { object AdDialog {
fun Context.showAdPreparingDialog(): AlertDialog { private val TAG = "AdDialog"
fun Context.showAdPreparingDialog(where: Int = 0): AlertDialog {
LogEx.logDebug(TAG, "where=$where")
val binding = DialogAdPreparingBinding.inflate(LayoutInflater.from(this)) val binding = DialogAdPreparingBinding.inflate(LayoutInflater.from(this))
val dialog = AlertDialog.Builder(this).create() val dialog = AlertDialog.Builder(this).create()
dialog.setView(binding.root) dialog.setView(binding.root)
......
package com.base.locationsharewhite.ads
import com.base.locationsharewhite.helper.EventUtils
import com.base.locationsharewhite.utils.LogEx
import org.json.JSONObject
import java.util.UUID
abstract class AdEvent {
abstract val TAG: String
var adUnit: String = ""
var from: String = ""
val reqId = UUID.randomUUID().toString()
fun adPrepareShow() {
val obj1 = JSONObject()
obj1.put("ad_unit", adUnit)
obj1.put("req_id", reqId)
obj1.put("from", from)
EventUtils.event("ad_prepare_show", ext = obj1)
LogEx.logDebug(TAG, "ad_prepare_show $obj1")
}
fun adPulStart() {
val obj = JSONObject()
obj.put("req_id", reqId)
obj.put("ad_unit", adUnit)
obj.put("ad_type", adUnit)
obj.put("from", from)
EventUtils.event("ad_pull_start", ext = obj)
LogEx.logDebug(TAG, "ad_pull_start $obj")
}
fun adShowError(reason: Any) {
val obj = JSONObject()
obj.put("ad_unit", adUnit)
obj.put("req_id", reqId)
obj.put("from", from)
obj.put("reason", reason.toString())
EventUtils.event("ad_show_error", ext = obj)
LogEx.logDebug(TAG, "ad_show_error $obj")
}
fun adLimited(value: String) {
val obj = JSONObject()
obj.put("ad_unit", adUnit)
obj.put("req_id", reqId)
EventUtils.event("ad_limit", value, obj)
LogEx.logDebug(TAG, "ad_limit $obj")
}
}
\ No newline at end of file
...@@ -12,7 +12,8 @@ import com.base.locationsharewhite.ads.admob.AdBannerMgr ...@@ -12,7 +12,8 @@ import com.base.locationsharewhite.ads.admob.AdBannerMgr
import com.base.locationsharewhite.ads.admob.AdInsertMgr import com.base.locationsharewhite.ads.admob.AdInsertMgr
import com.base.locationsharewhite.ads.admob.AdNativeMgr import com.base.locationsharewhite.ads.admob.AdNativeMgr
import com.base.locationsharewhite.ads.admob.AdOpenMgr import com.base.locationsharewhite.ads.admob.AdOpenMgr
import com.base.locationsharewhite.ads.admob.LimitUtils import com.base.locationsharewhite.ads.admob.AdmobEvent
import com.base.locationsharewhite.ads.applovin.AdMaxEvent
import com.base.locationsharewhite.ads.applovin.MaxInsertMgr import com.base.locationsharewhite.ads.applovin.MaxInsertMgr
import com.base.locationsharewhite.ads.applovin.MaxNativeMgr import com.base.locationsharewhite.ads.applovin.MaxNativeMgr
import com.base.locationsharewhite.ads.applovin.MaxOpenMgr import com.base.locationsharewhite.ads.applovin.MaxOpenMgr
...@@ -100,16 +101,16 @@ object AdsMgr { ...@@ -100,16 +101,16 @@ object AdsMgr {
} }
isAdmobInit = readyAdapter != null isAdmobInit = readyAdapter != null
EventUtils.event("AdmobInit", "AdmobInit") EventUtils.event("AdmobInit", "AdmobInit")
//if (isInit) {
//成功初始化就提前预加载开屏广告和插页广告
// }
context.toast("admob init") context.toast("admob init")
admobInitCallBack?.invoke()
admobInitCallBack = null if (adsConfigBean.adSwitch) {
admobInitCallBack?.invoke()
admobInitCallBack = null
adOpenMgr.loadAd(context, false, AdmobEvent("openAd", context::class.java.simpleName))
adInsertMgr.loadAd(context, false, AdmobEvent("interAd", context::class.java.simpleName))
}
} }
//据说可以初始化之前就预加载广告
adOpenMgr.loadAd(context)
adInsertMgr.loadAd(context)
} }
private fun initMax(context: Context) { private fun initMax(context: Context) {
...@@ -131,11 +132,12 @@ object AdsMgr { ...@@ -131,11 +132,12 @@ object AdsMgr {
AppLovinSdk.getInstance(context).initialize(initConfig) { AppLovinSdk.getInstance(context).initialize(initConfig) {
isMaxInit = true isMaxInit = true
// maxOpenMgr.loadAd(context) // maxOpenMgr.loadAd(context)
maxInsertMgr.loadAd(context) if (!adsConfigBean.adSwitch) {
maxInsertMgr.loadAd(context, false, AdMaxEvent("interAd", context::class.java.simpleName))
context.toast("max init") context.toast("max init")
maxInitCallBack?.invoke() maxInitCallBack?.invoke()
maxInitCallBack = null maxInitCallBack = null
}
} }
} }
...@@ -158,27 +160,25 @@ object AdsMgr { ...@@ -158,27 +160,25 @@ object AdsMgr {
showCallBack: AdsShowCallBack? = null, showCallBack: AdsShowCallBack? = null,
) { ) {
if (configBean.isInBlackList) { if (configBean.isInBlackList) {
activity.toast("isInBlackList")
EventUtils.event("isInBlackList", "isInBlackList=${configBean.isInBlackList}") EventUtils.event("isInBlackList", "isInBlackList=${configBean.isInBlackList}")
return return
} }
val from = activity::class.java.simpleName
if (adsConfigBean.adSwitch) { if (adsConfigBean.adSwitch) {
if (isAdmobInit) { if (isAdmobInit) {
adOpenMgr.show(activity, isUnLimit, showCallBack) adOpenMgr.show(activity, isUnLimit, AdmobEvent("openAd", from), showCallBack)
} else { } else {
admobInitCallBack = { admobInitCallBack = {
LogEx.logDebug("showOpen", "admobInitCallBack") adOpenMgr.show(activity, isUnLimit, AdmobEvent("openAd", from), showCallBack)
adOpenMgr.show(activity, isUnLimit, showCallBack)
} }
} }
} else { } else {
if (isMaxInit) { if (isMaxInit) {
maxOpenMgr.show(activity, isUnLimit, showCallBack) maxOpenMgr.show(activity, isUnLimit, AdMaxEvent("openAd", from), showCallBack)
} else { } else {
maxInitCallBack = { maxInitCallBack = {
LogEx.logDebug("showOpen", "maxInitCallBack") maxOpenMgr.show(activity, isUnLimit, AdMaxEvent("openAd", from), showCallBack)
maxOpenMgr.show(activity, isUnLimit, showCallBack)
} }
} }
} }
...@@ -201,11 +201,12 @@ object AdsMgr { ...@@ -201,11 +201,12 @@ object AdsMgr {
EventUtils.event("isInBlackList", configBean.isInBlackList.toString()) EventUtils.event("isInBlackList", configBean.isInBlackList.toString())
return return
} }
LogEx.logDebug("showInsert", "adSwitch=${adsConfigBean.adSwitch}") LogEx.logDebug("showAd", "adSwitch=${adsConfigBean.adSwitch}")
val from = activity::class.java.simpleName
if (adsConfigBean.adSwitch) { if (adsConfigBean.adSwitch) {
adInsertMgr.show(activity, isUnLimit, showCallBack) adInsertMgr.show(activity, isUnLimit, AdmobEvent("interAd", from), showCallBack)
} else { } else {
maxInsertMgr.show(activity, isUnLimit, showCallBack) maxInsertMgr.show(activity, isUnLimit, AdMaxEvent("interAd", from), showCallBack)
} }
} }
...@@ -227,14 +228,15 @@ object AdsMgr { ...@@ -227,14 +228,15 @@ object AdsMgr {
if (!isAdmobInit) return if (!isAdmobInit) return
if (adsConfigBean.adSwitch) { if (adsConfigBean.adSwitch) {
adNativeMgr.show(nativeView, layout, nativeCallBack) adNativeMgr.show(AdmobEvent("nativeAd", "nativeAd"), nativeView, layout, nativeCallBack)
} else { } else {
maxNativeMgr.show(nativeView, layout, nativeCallBack) maxNativeMgr.show(AdMaxEvent("nativeAd", "nativeAd"), nativeView, layout, nativeCallBack)
} }
} }
fun isNativeShow() = (isAdmobInit && !configBean.isInBlackList) && LimitUtils.isAdShow(AdsType.NATIVE) fun isNativeShow() = (isAdmobInit && !configBean.isInBlackList) && LimitUtils.isAdShow(AdsType.NATIVE, null)
/** /**
* 展示banner广告 * 展示banner广告
...@@ -246,7 +248,7 @@ object AdsMgr { ...@@ -246,7 +248,7 @@ object AdsMgr {
EventUtils.event("isInBlackList", configBean.isInBlackList.toString()) EventUtils.event("isInBlackList", configBean.isInBlackList.toString())
return return
} }
if (!isAdmobInit) { if (adsConfigBean.adSwitch) {
adBannerMgr.show(parent) adBannerMgr.show(parent)
} }
} }
......
...@@ -2,7 +2,7 @@ package com.base.locationsharewhite.ads ...@@ -2,7 +2,7 @@ package com.base.locationsharewhite.ads
abstract class AdsShowCallBack { abstract class AdsShowCallBack {
open fun show() {} open fun show() {}
abstract fun close() abstract fun close(where: Int = 0)
abstract fun failed() abstract fun failed(where: Int = 0)
abstract fun googleFailed() abstract fun googleFailed(where: Int = 0)
} }
\ No newline at end of file
package com.base.locationsharewhite.ads package com.base.locationsharewhite.ads
import android.app.Activity import android.app.Activity
import android.app.Dialog
import android.content.Context import android.content.Context
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
class adState<T>() {
var adDialog: Dialog? = null
/**
* 当前缓存的广告
*/
var currentAd: T? = null
/**
* 是否正在缓存加载广告
*/
var loadingAd: Boolean = false
/**
* 是否正在显示广告
*/
var showingAd: Boolean = false
/**
* 用于保存引用现有页面,在此页面显示广告(因为要等待广告加载完毕)
*/
var activityRef: WeakReference<Activity>? = null
/**
* 上一次的缓存成功时间
*/
var lastLoadTime: Long = 0
/**
* 上次展示时间
*/
var lastShowTime: Long = 0
fun onAdDisplayed() {
showingAd = true
adDialog?.dismiss()
adDialog = null
lastShowTime = System.currentTimeMillis()
currentAd = null
activityRef = null
}
fun onAdHidden() {
// 广告关闭,清空缓存数据,重新加载
showingAd = false
}
fun onAdDisplayFailed() {
adDialog?.dismiss()
adDialog = null
showingAd = false
currentAd = null
activityRef = null
}
fun onAdLoaded(ad: T?) {
//这里可能提前设置,所有可以不设置,max回调的类型可能不同
if (ad != null) {
currentAd = ad
}
loadingAd = false
lastLoadTime = System.currentTimeMillis()
}
fun onAdLoadFailed() {
adDialog?.dismiss()
adDialog = null
loadingAd = false
}
}
/** /**
* 特定广告管理基类 * 特定广告管理基类
* @param T 缓存广告的类 * @param T 缓存广告的类
...@@ -38,7 +120,7 @@ abstract class BaseAdMgr<T> { ...@@ -38,7 +120,7 @@ abstract class BaseAdMgr<T> {
/** /**
* 上一次的缓存成功时间 * 上一次的缓存成功时间
*/ */
protected var lastTime: Long = 0 protected var lastLoadTime: Long = 0
/** /**
...@@ -46,14 +128,14 @@ abstract class BaseAdMgr<T> { ...@@ -46,14 +128,14 @@ abstract class BaseAdMgr<T> {
* *
* @param context 加载所用的上下文,一般使用appContext * @param context 加载所用的上下文,一般使用appContext
*/ */
abstract fun loadAd(context: Context, isUnLimit: Boolean = true) abstract fun loadAd(context: Context, isUnLimit: Boolean = true, adEvent: AdEvent)
/** /**
* 广告显示 * 广告显示
* *
* @param activity 当前页面 * @param activity 当前页面
*/ */
abstract fun show(activity: Activity, isUnLimit: Boolean = true, showCallBack: AdsShowCallBack? = null) abstract fun show(activity: Activity, isUnLimit: Boolean = true, adEvent: AdEvent, showCallBack: AdsShowCallBack? = null)
/** /**
......
package com.base.locationsharewhite.ads.admob package com.base.locationsharewhite.ads
import com.base.locationsharewhite.ads.AdsMgr
import com.base.locationsharewhite.ads.AdsType
import com.base.locationsharewhite.helper.EventUtils
import com.base.locationsharewhite.utils.AppPreferences import com.base.locationsharewhite.utils.AppPreferences
import com.base.locationsharewhite.utils.KotlinExt.toFormatTime4 import com.base.locationsharewhite.utils.KotlinExt.toFormatTime4
import com.base.locationsharewhite.utils.LogEx
/** /**
...@@ -75,7 +71,7 @@ object LimitUtils { ...@@ -75,7 +71,7 @@ object LimitUtils {
* *
* @return true or false * @return true or false
*/ */
fun isAdShow(adsType: AdsType): Boolean { fun isAdShow(adsType: AdsType, adEvent: AdEvent?): Boolean {
val currentDate = System.currentTimeMillis().toFormatTime4() val currentDate = System.currentTimeMillis().toFormatTime4()
if (saveDate != currentDate) { if (saveDate != currentDate) {
//如果已经不是今天了,就重置个数 //如果已经不是今天了,就重置个数
...@@ -85,39 +81,27 @@ object LimitUtils { ...@@ -85,39 +81,27 @@ object LimitUtils {
AppPreferences.getInstance().put(NUM_CLICK, 0) AppPreferences.getInstance().put(NUM_CLICK, 0)
} }
if (isDisplayLimited) { if (isDisplayLimited) {
LogEx.logDebug("LimitUtils", "isDisplayLimited")
EventUtils.event( val value = "current${getAdEventMsg(adsType)} " +
"ad_limit", "show=${AppPreferences.getInstance().getInt(NUM_DISPLAY, 0)} " +
"current${getAdEventMsg(adsType)}Show=${ "${getAdEventMsg(adsType).lowercase()}_" + "max_show=${AdsMgr.adsConfigBean.numDisplayLimit}"
AppPreferences.getInstance()
.getInt(NUM_DISPLAY, 0) adEvent?.adLimited(value)
} ${getAdEventMsg(adsType).lowercase()}_max_show=${AdsMgr.adsConfigBean?.numDisplayLimit}"
)
} }
if (isClickLimited) { if (isClickLimited) {
LogEx.logDebug("LimitUtils", "isClickLimited") val value =
EventUtils.event( "current${getAdEventMsg(adsType)}Click=${AppPreferences.getInstance().getInt(NUM_CLICK, 0)} "
"ad_limit", "${getAdEventMsg(adsType).lowercase()}_max_click=${AdsMgr.adsConfigBean.numClickLimit}"
"current${getAdEventMsg(adsType)}Click=${
AppPreferences.getInstance() adEvent?.adLimited(value)
.getInt(NUM_CLICK, 0)
} ${getAdEventMsg(adsType).lowercase()}_max_click=${AdsMgr.adsConfigBean?.numClickLimit}"
)
} }
if (isRequestLimited) { if (isRequestLimited) {
LogEx.logDebug("LimitUtils", "isRequestLimited") val value = "current${getAdEventMsg(adsType)}Request=${AppPreferences.getInstance().getInt(NUM_REQUEST, 0)} " +
EventUtils.event( "${getAdEventMsg(adsType).lowercase()}_max_request=${AdsMgr.adsConfigBean.numRequestLimit}"
"ad_limit",
"current${getAdEventMsg(adsType)}Request=${ adEvent?.adLimited(value)
AppPreferences.getInstance()
.getInt(NUM_REQUEST, 0)
} ${getAdEventMsg(adsType).lowercase()}_max_request=${AdsMgr.adsConfigBean?.numRequestLimit}"
)
} }
LogEx.logDebug(
"LimitUtils",
"adsType=$adsType isDisplayLimited=$isDisplayLimited isClickLimited=$isClickLimited isRequestLimited=$isRequestLimited"
)
return !(isDisplayLimited || isClickLimited || isRequestLimited) return !(isDisplayLimited || isClickLimited || isRequestLimited)
} }
...@@ -152,7 +136,11 @@ object LimitUtils { ...@@ -152,7 +136,11 @@ object LimitUtils {
* *
* @param lastTime 上一次显示的时间 * @param lastTime 上一次显示的时间
*/ */
fun isIntervalLimited(lastTime: Long) = fun isIntervalLimited(lastTime: Long, adEvent: AdEvent): Boolean {
((System.currentTimeMillis() - lastTime) / 1000 / 60).toInt() < (AdsMgr.adsConfigBean?.timeInterval val flag = ((System.currentTimeMillis() - lastTime) / 1000).toInt() < (AdsMgr.adsConfigBean.timeInterval)
?: 1) if (flag) {
adEvent.adShowError("ad in timeInterval")
}
return flag
}
} }
\ No newline at end of file
...@@ -3,13 +3,16 @@ package com.base.locationsharewhite.ads.admob ...@@ -3,13 +3,16 @@ package com.base.locationsharewhite.ads.admob
import android.os.Bundle import android.os.Bundle
import android.view.ViewGroup import android.view.ViewGroup
import android.view.ViewTreeObserver import android.view.ViewTreeObserver
import com.base.locationsharewhite.ads.AdsMgr
import com.base.locationsharewhite.ads.AdsType import com.base.locationsharewhite.ads.AdsType
import com.base.locationsharewhite.ads.ConstConfig import com.base.locationsharewhite.ads.ConstConfig
import com.base.locationsharewhite.ads.LimitUtils
import com.google.ads.mediation.admob.AdMobAdapter import com.google.ads.mediation.admob.AdMobAdapter
import com.google.android.gms.ads.AdListener import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.LoadAdError
import java.util.UUID import java.util.UUID
/** /**
...@@ -22,7 +25,12 @@ class AdBannerMgr { ...@@ -22,7 +25,12 @@ class AdBannerMgr {
fun show(parent: ViewGroup, adClose: (() -> Unit)? = null) { fun show(parent: ViewGroup, adClose: (() -> Unit)? = null) {
if (!LimitUtils.isAdShow(AdsType.BANNER)) { if (!AdsMgr.adsConfigBean.adSwitch) {
return
}
val admobEvent = AdmobEvent("banner", "banner")
if (!LimitUtils.isAdShow(AdsType.BANNER, admobEvent)) {
adView = null adView = null
return return
} }
...@@ -43,14 +51,14 @@ class AdBannerMgr { ...@@ -43,14 +51,14 @@ class AdBannerMgr {
AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(parent.context, adWidth) AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(parent.context, adWidth)
adView?.adUnitId = ConstConfig.ADMOB_BANNER_UNIT_ID adView?.adUnitId = ConstConfig.ADMOB_BANNER_UNIT_ID
adView?.setAdSize(adSize) adView?.setAdSize(adSize)
loadAd(adClose) loadAd(admobEvent, adClose)
parent.viewTreeObserver.removeOnGlobalLayoutListener(listener) parent.viewTreeObserver.removeOnGlobalLayoutListener(listener)
} }
parent.viewTreeObserver.addOnGlobalLayoutListener(listener) parent.viewTreeObserver.addOnGlobalLayoutListener(listener)
} }
fun loadAd(adClose: (() -> Unit)?) { fun loadAd(admobEvent: AdmobEvent, adClose: (() -> Unit)?) {
val extras = Bundle() val extras = Bundle()
extras.putString("collapsible", "bottom") extras.putString("collapsible", "bottom")
extras.putString("collapsible_request_id", UUID.randomUUID().toString()) extras.putString("collapsible_request_id", UUID.randomUUID().toString())
...@@ -58,8 +66,13 @@ class AdBannerMgr { ...@@ -58,8 +66,13 @@ class AdBannerMgr {
AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter::class.java, extras).build() AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter::class.java, extras).build()
adView?.adListener = adView?.adListener =
object : AdListener() { object : AdListener() {
override fun onAdLoaded() { override fun onAdFailedToLoad(loadAdError: LoadAdError) {
super.onAdFailedToLoad(loadAdError)
admobEvent.pullAd(loadAdError.responseInfo, loadAdError)
}
override fun onAdLoaded() {
admobEvent.pullAd(adView?.responseInfo)
} }
override fun onAdOpened() { override fun onAdOpened() {
......
package com.base.locationsharewhite.ads.admob package com.base.locationsharewhite.ads.admob
import android.content.Context
import android.view.ViewGroup
import androidx.core.view.isVisible
import com.base.locationsharewhite.ads.NativeParentView import com.base.locationsharewhite.ads.NativeParentView
import com.base.locationsharewhite.ads.AdsType import com.base.locationsharewhite.ads.AdsType
import com.base.locationsharewhite.ads.ConstConfig import com.base.locationsharewhite.ads.ConstConfig
import com.base.locationsharewhite.helper.EventUtils import com.base.locationsharewhite.ads.LimitUtils
import com.base.locationsharewhite.utils.LogEx
import com.google.android.gms.ads.AdListener import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdLoader import com.google.android.gms.ads.AdLoader
import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.LoadAdError import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.nativead.NativeAd import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdOptions import com.google.android.gms.ads.nativead.NativeAdOptions
import org.json.JSONObject
import java.util.UUID
import java.util.concurrent.ConcurrentLinkedDeque import java.util.concurrent.ConcurrentLinkedDeque
/** /**
...@@ -33,35 +27,37 @@ class AdNativeMgr { ...@@ -33,35 +27,37 @@ class AdNativeMgr {
*/ */
private val cacheItems = ConcurrentLinkedDeque<NativeAd>() private val cacheItems = ConcurrentLinkedDeque<NativeAd>()
fun loadAd( private fun loadAd(
context: Context, admobEvent: AdmobEvent,
parent: NativeParentView? = null, parent: NativeParentView,
layout: Int? = null layout: Int
) { ) {
if (!LimitUtils.isAdShow(AdsType.NATIVE)) return admobEvent.adPulStart()
if (!LimitUtils.isAdShow(AdsType.NATIVE, admobEvent)) return
val reqId = UUID.randomUUID().toString() var currentNativeAd: NativeAd? = null
val obj = JSONObject()
obj.put("req_id", reqId)
obj.put("ad_type", "nativeAd")
val adLoader = AdLoader.Builder( val adLoader = AdLoader.Builder(
context, parent.context,
ConstConfig.ADMOB_NATIVE_UNIT_ID ConstConfig.ADMOB_NATIVE_UNIT_ID
).forNativeAd { nativeAd -> ).forNativeAd { nativeAd ->
currentNativeAd = nativeAd
cacheItems.offer(nativeAd) cacheItems.offer(nativeAd)
lastTime = System.currentTimeMillis() lastTime = System.currentTimeMillis()
nativeAd.setOnPaidEventListener(AdmobEvent.EventOnPaidEventListener(nativeAd)) nativeAd.setOnPaidEventListener(AdmobEvent.EventOnPaidEventListener(nativeAd))
AdmobEvent.pullAd(nativeAd.responseInfo, "nativeAd", reqId = reqId)
if (parent != null && layout != null) show(parent, layout) admobEvent.pullAd(nativeAd.responseInfo)
show(admobEvent, parent, layout)
}.withAdListener(object : AdListener() { }.withAdListener(object : AdListener() {
override fun onAdFailedToLoad(error: LoadAdError) { override fun onAdFailedToLoad(error: LoadAdError) {
AdmobEvent.pullAd(error.responseInfo, "nativeAd", error.message, reqId = reqId) admobEvent.pullAd(error.responseInfo, error)
} }
override fun onAdClicked() { override fun onAdClicked() {
super.onAdClicked() super.onAdClicked()
AdmobEvent.clickAd(cacheItems.lastOrNull()?.responseInfo, "nativeAd") admobEvent.clickAd(currentNativeAd?.responseInfo)
} }
override fun onAdClosed() { override fun onAdClosed() {
...@@ -75,45 +71,30 @@ class AdNativeMgr { ...@@ -75,45 +71,30 @@ class AdNativeMgr {
} }
fun show( fun show(
admobEvent: AdmobEvent,
parent: NativeParentView, parent: NativeParentView,
layout: Int, layout: Int,
nativeCallBack: ((Any?) -> Unit)? = null nativeCallBack: ((Any?) -> Unit)? = null
) { ) {
if (!LimitUtils.isAdShow(AdsType.NATIVE)) {
admobEvent.adPrepareShow()
if (!LimitUtils.isAdShow(AdsType.NATIVE, admobEvent)) {
cacheItems.clear() cacheItems.clear()
LogEx.logDebug("AdNativeMgr", "0")
return return
} }
val nativeAd = cacheItems.peek() val nativeAd = cacheItems.peek()
if ((nativeAd == null).also { if (nativeAd == null || !adAvailable()) {
if (it) {
LogEx.logDebug("AdNativeMgr", "1")
val obj2 = JSONObject()
obj2.put("reason", "no_ad")
obj2.put("ad_unit", "nativeAd")
EventUtils.event("ad_show_error", ext = obj2)
}
} || (!adAvailable()).also {
if (it) {
LogEx.logDebug("AdNativeMgr", "2")
val obj2 = JSONObject()
obj2.put("ad_unit", "nativeAd")
EventUtils.event("ad_expire", ext = obj2)
}
}) {
//缓存过期了就清空 //缓存过期了就清空
cacheItems.clear() cacheItems.clear()
LogEx.logDebug("AdNativeMgr", "loadAd") loadAd(admobEvent, parent, layout)
loadAd(parent.context.applicationContext, parent, layout)
return return
} }
nativeCallBack?.invoke(nativeAd) nativeCallBack?.invoke(nativeAd)
LogEx.logDebug("AdNativeMgr", "3") parent.setNativeAd(nativeAd, layout)
val obj = JSONObject() admobEvent.showAd(nativeAd.responseInfo)
obj.put("ad_unit", "nativeAd")
EventUtils.event("ad_prepare_show_native", ext = obj)
parent.setNativeAd(nativeAd!!, layout)
AdmobEvent.showAd(nativeAd.responseInfo, "nativeAd")
} }
private fun adAvailable(): Boolean { private fun adAvailable(): Boolean {
......
...@@ -2,13 +2,16 @@ package com.base.locationsharewhite.ads.admob ...@@ -2,13 +2,16 @@ package com.base.locationsharewhite.ads.admob
import android.app.Activity import android.app.Activity
import android.os.Bundle import android.os.Bundle
import com.base.locationsharewhite.ads.AdEvent
import com.base.locationsharewhite.helper.EventUtils import com.base.locationsharewhite.helper.EventUtils
import com.base.locationsharewhite.helper.MyApplication import com.base.locationsharewhite.helper.MyApplication
import com.base.locationsharewhite.utils.LogEx import com.base.locationsharewhite.utils.LogEx
import com.facebook.appevents.AppEventsConstants import com.facebook.appevents.AppEventsConstants
import com.facebook.appevents.AppEventsLogger import com.facebook.appevents.AppEventsLogger
import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.AdValue import com.google.android.gms.ads.AdValue
import com.google.android.gms.ads.AdView import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.OnPaidEventListener import com.google.android.gms.ads.OnPaidEventListener
import com.google.android.gms.ads.ResponseInfo import com.google.android.gms.ads.ResponseInfo
import com.google.android.gms.ads.appopen.AppOpenAd import com.google.android.gms.ads.appopen.AppOpenAd
...@@ -20,23 +23,31 @@ import com.google.firebase.analytics.ktx.analytics ...@@ -20,23 +23,31 @@ import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.ktx.Firebase import com.google.firebase.ktx.Firebase
import org.json.JSONObject import org.json.JSONObject
object AdmobEvent { private val taichiPref by lazy {
private val TAG = "AdmobEvent" MyApplication.appContext.getSharedPreferences("TaichiTroasCache", 0)
}
fun adPrepareShow(adUnit: String) { private val taichiSharedPreferencesEditor by lazy {
val obj1 = JSONObject() taichiPref.edit()
obj1.put("ad_unit", adUnit) }
EventUtils.event("ad_prepare_show", ext = obj1)
class AdmobEvent : AdEvent {
override val TAG: String = "AdmobEvent"
constructor(adUnit: String, from: String) : super() {
this.adUnit = adUnit
this.from = from
} }
fun pullAd( fun pullAd(
responseInfo: ResponseInfo?, responseInfo: ResponseInfo?,
adUnit: String, error: LoadAdError? = null,
error: String? = null,
reqId: String? = null
) { ) {
val obj = JSONObject() val obj = JSONObject()
obj.put("req_id", reqId)
if (responseInfo != null) { if (responseInfo != null) {
val response = responseInfo.adapterResponses.getOrNull(0) val response = responseInfo.adapterResponses.getOrNull(0)
if (response != null) { if (response != null) {
...@@ -53,23 +64,63 @@ object AdmobEvent { ...@@ -53,23 +64,63 @@ object AdmobEvent {
} }
obj.put("networkname", responseInfo?.mediationAdapterClassName) obj.put("networkname", responseInfo?.mediationAdapterClassName)
obj.put("ad_unit", adUnit) obj.put("ad_unit", adUnit)
obj.put("req_id", reqId) obj.put("from", from)
if (error == null) { if (error == null) {
obj.put("status", "1") obj.put("status", "1")
} else { } else {
obj.put("errMsg", error) obj.put("errMsg", error.toString())
obj.put("status", "2") obj.put("status", "2")
} }
LogEx.logDebug(TAG, "obj=$obj")
EventUtils.event("ad_pull", ext = obj) EventUtils.event("ad_pull", ext = obj)
LogEx.logDebug(TAG, "ad_pull obj=$obj")
} }
private val taichiPref by lazy {
MyApplication.appContext.getSharedPreferences("TaichiTroasCache", 0) fun clickAd(responseInfo: ResponseInfo?) {
val response = responseInfo?.adapterResponses?.getOrNull(0)
val obj = JSONObject()
obj.put("req_id", reqId)
obj.put("source", response?.adSourceName)
obj.put("ad_unit", adUnit)
val credentials = mapOf(
"placementid" to response?.credentials?.get("placementid"),
"appid" to response?.credentials?.get("appid"),
"pubid" to response?.credentials?.get("pubid")
)
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)
} else {
EventUtils.event("ad_click", ext = obj)
}
} }
private val taichiSharedPreferencesEditor by lazy { fun showAd(responseInfo: ResponseInfo?, activity: Activity? = null) {
taichiPref.edit() val response = responseInfo?.adapterResponses?.getOrNull(0)
val obj = JSONObject()
obj.put("req_id", reqId)
obj.put("source", response?.adSourceName)
obj.put("ad_unit", adUnit)
obj.put("networkname", responseInfo?.mediationAdapterClassName)
val credentials = mapOf(
"placementid" to response?.credentials?.get("placementid"),
"appid" to response?.credentials?.get("appid"),
"pubid" to response?.credentials?.get("pubid")
)
obj.put("credentials", credentials.toString())
obj.put("session_id", responseInfo?.responseId)
obj.put("from", activity?.javaClass?.simpleName)
if (adUnit != "nativeAd") {
EventUtils.event("ad_show", ext = obj)
} else {
EventUtils.event("ad_show", ext = obj)
}
LogEx.logDebug(TAG, "ad_show $obj")
} }
class EventOnPaidEventListener(private val ad: Any?) : OnPaidEventListener { class EventOnPaidEventListener(private val ad: Any?) : OnPaidEventListener {
...@@ -248,46 +299,16 @@ object AdmobEvent { ...@@ -248,46 +299,16 @@ object AdmobEvent {
} }
} }
fun clickAd(responseInfo: ResponseInfo?, adUnit: String) { fun adShowError(adError: AdError) {
val response = responseInfo?.adapterResponses?.getOrNull(0)
val obj = JSONObject() val obj = JSONObject()
obj.put("source", response?.adSourceName) obj.put("req_id", reqId)
obj.put("ad_unit", adUnit) obj.put("reason", adError.message)
obj.put("code", adError.code)
val credentials = mapOf( obj.put("from", from)
"placementid" to response?.credentials?.get("placementid"), obj.put("ad_unit", "openAd")
"appid" to response?.credentials?.get("appid"), EventUtils.event("ad_show_error", ext = obj)
"pubid" to response?.credentials?.get("pubid") LogEx.logDebug(TAG, "ad_show_error $obj")
)
obj.put("credentials", credentials.toString())
obj.put("session_id", responseInfo?.responseId)
obj.put("networkname", responseInfo?.mediationAdapterClassName)
if (adUnit != "nativeAd") {
EventUtils.event("ad_click", ext = obj)
} else {
EventUtils.event("bigimage_ad_click", ext = obj)
}
} }
}
fun showAd(responseInfo: ResponseInfo?, adUnit: String, activity: Activity? = null) {
val response = responseInfo?.adapterResponses?.getOrNull(0)
val obj = JSONObject()
obj.put("source", response?.adSourceName)
obj.put("ad_unit", adUnit)
obj.put("networkname", responseInfo?.mediationAdapterClassName)
val credentials = mapOf(
"placementid" to response?.credentials?.get("placementid"),
"appid" to response?.credentials?.get("appid"),
"pubid" to response?.credentials?.get("pubid")
)
obj.put("credentials", credentials.toString())
obj.put("session_id", responseInfo?.responseId)
obj.put("from", activity?.javaClass?.simpleName)
if (adUnit != "nativeAd") {
EventUtils.event("ad_show", ext = obj)
} else {
EventUtils.event("ad_show", ext = obj)
}
}
}
\ No newline at end of file
...@@ -5,8 +5,10 @@ import com.applovin.mediation.MaxAd ...@@ -5,8 +5,10 @@ import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxAdRevenueListener import com.applovin.mediation.MaxAdRevenueListener
import com.applovin.mediation.MaxError import com.applovin.mediation.MaxError
import com.applovin.sdk.AppLovinSdk import com.applovin.sdk.AppLovinSdk
import com.base.locationsharewhite.ads.AdEvent
import com.base.locationsharewhite.helper.EventUtils import com.base.locationsharewhite.helper.EventUtils
import com.base.locationsharewhite.helper.MyApplication import com.base.locationsharewhite.helper.MyApplication
import com.base.locationsharewhite.utils.LogEx
import com.facebook.FacebookSdk import com.facebook.FacebookSdk
import com.facebook.appevents.AppEventsConstants import com.facebook.appevents.AppEventsConstants
import com.facebook.appevents.AppEventsLogger import com.facebook.appevents.AppEventsLogger
...@@ -14,19 +16,22 @@ import com.google.firebase.analytics.FirebaseAnalytics ...@@ -14,19 +16,22 @@ import com.google.firebase.analytics.FirebaseAnalytics
import org.json.JSONObject import org.json.JSONObject
object AdMaxEvent { private val taichiPref = FacebookSdk.getApplicationContext()
.getSharedPreferences("TaichiTroasCache", 0)
fun adPrepareShow(adUnit: String) { private val taichiSharedPreferencesEditor = taichiPref.edit()
val obj1 = JSONObject()
obj1.put("ad_unit", adUnit) class AdMaxEvent : AdEvent {
EventUtils.event("ad_prepare_show", ext = obj1)
} override val TAG: String = "AdMaxEvent"
constructor(adUnit: String, from: String) : super() {
this.adUnit = adUnit
this.from = from
}
fun pullAd( fun pullAd(
ad: MaxAd?, ad: MaxAd?,
adUnit: String,
reqId: String? = null,
error: MaxError? = null error: MaxError? = null
) { ) {
val obj = JSONObject() val obj = JSONObject()
...@@ -37,6 +42,7 @@ object AdMaxEvent { ...@@ -37,6 +42,7 @@ object AdMaxEvent {
ad?.creativeId ad?.creativeId
) )
obj.put("req_id", reqId) obj.put("req_id", reqId)
obj.put("from", from)
obj.put("status", if (ad == null) "0" else "1") obj.put("status", if (ad == null) "0" else "1")
obj.put("networkname", ad?.networkName) obj.put("networkname", ad?.networkName)
obj.put("placement", ad?.placement) obj.put("placement", ad?.placement)
...@@ -50,9 +56,10 @@ object AdMaxEvent { ...@@ -50,9 +56,10 @@ object AdMaxEvent {
obj.put("status", "2") obj.put("status", "2")
} }
EventUtils.event("ad_pull", ext = obj) EventUtils.event("ad_pull", ext = obj)
LogEx.run { logDebug(TAG, "ad_pull $obj") }
} }
fun clickAd(ad: MaxAd?, adUnit: String) { fun clickAd(ad: MaxAd?) {
val obj = JSONObject() val obj = JSONObject()
obj.put("UnitId", ad?.adUnitId) obj.put("UnitId", ad?.adUnitId)
...@@ -74,7 +81,7 @@ object AdMaxEvent { ...@@ -74,7 +81,7 @@ object AdMaxEvent {
} }
fun showAd(ad: MaxAd?, adUnit: String, activity: String?) { fun showAd(ad: MaxAd?, activity: String?) {
val obj = JSONObject() val obj = JSONObject()
obj.put("UnitId", ad?.adUnitId) obj.put("UnitId", ad?.adUnitId)
obj.put("ad_unit", adUnit) obj.put("ad_unit", adUnit)
...@@ -97,9 +104,6 @@ object AdMaxEvent { ...@@ -97,9 +104,6 @@ object AdMaxEvent {
} }
private val taichiPref = FacebookSdk.getApplicationContext()
.getSharedPreferences("TaichiTroasCache", 0)
private val taichiSharedPreferencesEditor = taichiPref.edit()
class EventOnPaidEventListener : MaxAdRevenueListener { class EventOnPaidEventListener : MaxAdRevenueListener {
override fun onAdRevenuePaid(ad: MaxAd) { override fun onAdRevenuePaid(ad: MaxAd) {
...@@ -152,6 +156,9 @@ object AdMaxEvent { ...@@ -152,6 +156,9 @@ object AdMaxEvent {
obj.put("networkPlacement", networkPlacement) obj.put("networkPlacement", networkPlacement)
EventUtils.event("ad_price", ext = obj) EventUtils.event("ad_price", ext = obj)
} }
}
fun adShowError(reason: String) {
} }
} }
\ No newline at end of file
...@@ -10,7 +10,7 @@ import com.applovin.mediation.nativeAds.MaxNativeAdView ...@@ -10,7 +10,7 @@ import com.applovin.mediation.nativeAds.MaxNativeAdView
import com.base.locationsharewhite.ads.AdsType import com.base.locationsharewhite.ads.AdsType
import com.base.locationsharewhite.ads.ConstConfig import com.base.locationsharewhite.ads.ConstConfig
import com.base.locationsharewhite.ads.NativeParentView import com.base.locationsharewhite.ads.NativeParentView
import com.base.locationsharewhite.ads.admob.LimitUtils import com.base.locationsharewhite.ads.LimitUtils
import com.base.locationsharewhite.helper.EventUtils import com.base.locationsharewhite.helper.EventUtils
import org.json.JSONObject import org.json.JSONObject
import java.util.UUID import java.util.UUID
...@@ -32,15 +32,20 @@ class MaxNativeMgr { ...@@ -32,15 +32,20 @@ class MaxNativeMgr {
private var currentLoader: MaxNativeAdLoader? = null private var currentLoader: MaxNativeAdLoader? = null
fun loadAd(context: Context, parent: NativeParentView? = null, @LayoutRes layout: Int? = null) { private fun loadAd(
if (!LimitUtils.isAdShow(AdsType.NATIVE)) return adMaxEvent: AdMaxEvent,
parent: NativeParentView,
@LayoutRes layout: Int
) {
if (!LimitUtils.isAdShow(AdsType.NATIVE, adMaxEvent)) return
val reqId = UUID.randomUUID().toString() val reqId = UUID.randomUUID().toString()
val obj = JSONObject() val obj = JSONObject()
obj.put("req_id", reqId) obj.put("req_id", reqId)
obj.put("ad_type", "nativeAd") obj.put("ad_type", "nativeAd")
val nativeAdLoader = MaxNativeAdLoader(ConstConfig.MAX_NATIVE_UNIT_ID, context) val nativeAdLoader = MaxNativeAdLoader(ConstConfig.MAX_NATIVE_UNIT_ID, parent.context)
nativeAdLoader.setNativeAdListener(object : MaxNativeAdListener() { nativeAdLoader.setNativeAdListener(object : MaxNativeAdListener() {
...@@ -48,13 +53,13 @@ class MaxNativeMgr { ...@@ -48,13 +53,13 @@ class MaxNativeMgr {
currentLoader = nativeAdLoader currentLoader = nativeAdLoader
currentAd = ad currentAd = ad
lastTime = System.currentTimeMillis() lastTime = System.currentTimeMillis()
AdMaxEvent.pullAd(ad, "nativeAd", reqId = reqId) adMaxEvent.pullAd(ad)
nativeAdLoader.setRevenueListener(AdMaxEvent.EventOnPaidEventListener()) nativeAdLoader.setRevenueListener(AdMaxEvent.EventOnPaidEventListener())
if (parent != null && layout != null) show(parent, layout) show(adMaxEvent, parent, layout)
} }
override fun onNativeAdLoadFailed(adUnitId: String, error: MaxError) { override fun onNativeAdLoadFailed(adUnitId: String, error: MaxError) {
AdMaxEvent.pullAd(null, "nativeAd", reqId = reqId, error) adMaxEvent.pullAd(null, error)
} }
override fun onNativeAdClicked(ad: MaxAd) { override fun onNativeAdClicked(ad: MaxAd) {
...@@ -66,11 +71,12 @@ class MaxNativeMgr { ...@@ -66,11 +71,12 @@ class MaxNativeMgr {
} }
fun show( fun show(
adMaxEvent: AdMaxEvent,
parent: NativeParentView, parent: NativeParentView,
@LayoutRes layout: Int, @LayoutRes layout: Int,
nativeCallBack: ((Any?) -> Unit)? = null nativeCallBack: ((Any?) -> Unit)? = null
) { ) {
if (!LimitUtils.isAdShow(AdsType.NATIVE)) { if (!LimitUtils.isAdShow(AdsType.NATIVE, adMaxEvent)) {
currentLoader = null currentLoader = null
currentAd = null currentAd = null
return return
...@@ -94,7 +100,7 @@ class MaxNativeMgr { ...@@ -94,7 +100,7 @@ class MaxNativeMgr {
//缓存过期了就清空 //缓存过期了就清空
currentLoader = null currentLoader = null
currentAd = null currentAd = null
loadAd(parent.context.applicationContext, parent, layout) loadAd(AdMaxEvent("nativeAd", "preload"), parent, layout)
return return
} }
val obj = JSONObject() val obj = JSONObject()
......
package com.base.locationsharewhite.config package com.base.locationsharewhite.config
class AdConfigBean( class AdConfigBean(
var adSwitch: Boolean = true, var adSwitch: Boolean = true,//true 走admob,false走max
var numDisplayLimit: Int = -1, var numDisplayLimit: Int = -1,
var numRequestLimit: Int = -1, var numRequestLimit: Int = -1,
var numClickLimit: Int = -1, var numClickLimit: Int = -1,
......
...@@ -103,7 +103,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnMapReadyCallback { ...@@ -103,7 +103,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnMapReadyCallback {
showNotificationDialog() showNotificationDialog()
if (!bannerShowed.get()) { if (!bannerShowed.get()) {
bannerShowed.set(true) bannerShowed.set(true)
AdBannerMgr().show(binding.flAd) AdsMgr.showBanner(binding.flAd)
} }
} }
...@@ -209,15 +209,18 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnMapReadyCallback { ...@@ -209,15 +209,18 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnMapReadyCallback {
private fun showAdJump(jumpAction: () -> Unit) { private fun showAdJump(jumpAction: () -> Unit) {
AdsMgr.showInsert(this, false, object : AdsShowCallBack() { AdsMgr.showInsert(this, false, object : AdsShowCallBack() {
override fun close() { override fun close(where:Int) {
LogEx.logDebug(TAG,"close where=$where")
jumpAction.invoke() jumpAction.invoke()
} }
override fun failed() { override fun failed(where:Int) {
LogEx.logDebug(TAG,"failed where=$where")
jumpAction.invoke() jumpAction.invoke()
} }
override fun googleFailed() { override fun googleFailed(where:Int) {
LogEx.logDebug(TAG,"googleFailed where=$where")
jumpAction.invoke() jumpAction.invoke()
} }
}) })
......
...@@ -70,17 +70,17 @@ class SplashActivity : BaseActivity<ActivitySplashBinding>(), SplashView { ...@@ -70,17 +70,17 @@ class SplashActivity : BaseActivity<ActivitySplashBinding>(), SplashView {
cancelProgressJob() cancelProgressJob()
} }
override fun close() { override fun close(where:Int) {
LogEx.logDebug(TAG, "AdsShowCallBack close") LogEx.logDebug(TAG, "AdsShowCallBack close")
jumpNext() jumpNext()
} }
override fun failed() { override fun failed(where:Int) {
LogEx.logDebug(TAG, "AdsShowCallBack failed") LogEx.logDebug(TAG, "AdsShowCallBack failed")
jumpNext() jumpNext()
} }
override fun googleFailed() { override fun googleFailed(where:Int) {
LogEx.logDebug(TAG, "AdsShowCallBack googleFailed") LogEx.logDebug(TAG, "AdsShowCallBack googleFailed")
jumpNext() jumpNext()
} }
......
...@@ -81,7 +81,7 @@ object DialogView { ...@@ -81,7 +81,7 @@ object DialogView {
binding.ivClose.setOnClickListener { binding.ivClose.setOnClickListener {
dialog.dismiss() dialog.dismiss()
} }
AdsMgr.showNative(binding.flAd, R.layout.layout_admob_app_exit) AdsMgr.showNative( binding.flAd, R.layout.layout_admob_app_exit)
} }
......
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