Commit cfd8ca08 authored by wanglei's avatar wanglei

...

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