Commit 1d1246a4 authored by leichao.gao's avatar leichao.gao

广告dialog优化

parent aac77a51
......@@ -61,24 +61,24 @@ object AdmobUtils {
obj.put("ad_type", "openAd")
EventUtils.event("ad_pull_start", ext = obj)
AppOpenAd.load(
BaseApplication.context,
ConfigHelper.openAdmobId,
mRequest,
object : AppOpenAd.AppOpenAdLoadCallback() {
override fun onAdLoaded(ad: AppOpenAd) {
openLoadTime = System.currentTimeMillis()
mOpenAd = ad
onLoad?.invoke()
pull(ad.responseInfo, "openAd", reqId = reqId)
ad.onPaidEventListener = EventOnPaidEventListener(ad)
}
BaseApplication.context,
ConfigHelper.openAdmobId,
mRequest,
object : AppOpenAd.AppOpenAdLoadCallback() {
override fun onAdLoaded(ad: AppOpenAd) {
openLoadTime = System.currentTimeMillis()
mOpenAd = ad
onLoad?.invoke()
pull(ad.responseInfo, "openAd", reqId = reqId)
ad.onPaidEventListener = EventOnPaidEventListener(ad)
}
override fun onAdFailedToLoad(p0: LoadAdError) {
mOpenAd = null
onLoad?.invoke()
pull(p0.responseInfo, "openAd", p0.message, reqId = reqId)
}
})
override fun onAdFailedToLoad(p0: LoadAdError) {
mOpenAd = null
onLoad?.invoke()
pull(p0.responseInfo, "openAd", p0.message, reqId = reqId)
}
})
}
fun isOpenAdLoaded() = mOpenAd != null
......@@ -202,7 +202,7 @@ object AdmobUtils {
obj.put("ad_type", "nativeAd")
val adLoader = AdLoader.Builder(
BaseApplication.context, ConfigHelper.nativeAdmobId
BaseApplication.context, ConfigHelper.nativeAdmobId
).forNativeAd {
nativeLoadTime = System.currentTimeMillis()
nativeAd = it
......@@ -244,32 +244,32 @@ object AdmobUtils {
obj.put("from", activity.javaClass.simpleName)
EventUtils.event("ad_pull_start", ext = obj)
InterstitialAd.load(
activity,
ConfigHelper.interAdmobId,
mRequest,
object : InterstitialAdLoadCallback() {
override fun onAdFailedToLoad(p0: LoadAdError) {
interAd = null
onLoad?.invoke()
pull(p0.responseInfo, "interAd", p0.message, reqId = reqId)
if (BuildConfig.DEBUG) {
Toast.makeText(
BaseApplication.context,
"拉取失败" + p0.message,
Toast.LENGTH_SHORT
).show()
}
activity,
ConfigHelper.interAdmobId,
mRequest,
object : InterstitialAdLoadCallback() {
override fun onAdFailedToLoad(p0: LoadAdError) {
interAd = null
onLoad?.invoke()
pull(p0.responseInfo, "interAd", p0.message, reqId = reqId)
if (BuildConfig.DEBUG) {
Toast.makeText(
BaseApplication.context,
"拉取失败" + p0.message,
Toast.LENGTH_SHORT
).show()
}
}
}
override fun onAdLoaded(ad: InterstitialAd) {
interAd = ad
onLoad?.invoke()
interLoadTime = System.currentTimeMillis()
pull(ad.responseInfo, "interAd", reqId = reqId)
ad.onPaidEventListener = EventOnPaidEventListener(ad)
}
})
override fun onAdLoaded(ad: InterstitialAd) {
interAd = ad
onLoad?.invoke()
interLoadTime = System.currentTimeMillis()
pull(ad.responseInfo, "interAd", reqId = reqId)
ad.onPaidEventListener = EventOnPaidEventListener(ad)
}
})
}
private fun isAdExpired(): Boolean {
......@@ -280,9 +280,9 @@ object AdmobUtils {
var adLastDisplayTime: Long = 0
fun showInterstitialAd(
activity: Activity,
isLoadAdNow: Boolean = false,
onHidden: (() -> Unit)? = null
activity: Activity,
isLoadAdNow: Boolean = false,
onHidden: (() -> Unit)? = null
) {
if (activity.isFinishing || activity.isDestroyed) {
return
......@@ -314,34 +314,36 @@ object AdmobUtils {
}
}
private fun showIntervalDialogAndShowAd(
activity: Activity,
isLoadAdNow: Boolean,
onHidden: (() -> Unit)?,
interval: Int
) {
val customDialog = CustomDialog(activity, R.layout.dialog_ad_loading)
customDialog.setCountdownText(R.id.dialog_ad_loading_text)
private var customDialog: CustomDialog? = null
private fun showIntervalDialogAndShowAd(activity: Activity, isLoadAdNow: Boolean, onHidden: (() -> Unit)?, interval: Int) {
if (customDialog != null && customDialog?.isShowing == true) {
return // 如果对话框已经显示,则不再显示
}
customDialog = CustomDialog(activity, R.layout.dialog_ad_loading)
customDialog?.setCountdownText(R.id.dialog_ad_loading_text)
val countdownTimer = object : CountDownTimer((interval * 1000).toLong(), 1000) {
override fun onTick(millisUntilFinished: Long) {
val seconds = (millisUntilFinished / 1000).toInt()
customDialog.updateCountdownText("Advertising in preparation ($seconds" + "s)...")
customDialog?.updateCountdownText("Advertising in preparation ($seconds" + "s)...")
}
override fun onFinish() {
showCachedInterstitialAd(activity, isLoadAdNow, onHidden)
customDialog?.dismiss()
customDialog.dismiss()
customDialog = null
}
}
countdownTimer.start()
customDialog.show()
customDialog?.show()
}
private fun showCachedInterstitialAd(
activity: Activity,
isLoadAdNow: Boolean,
onHidden: (() -> Unit)?
activity: Activity,
isLoadAdNow: Boolean,
onHidden: (() -> Unit)?
) {
if (interAd != null) {
displayInterstitialAd(activity, onHidden)
......@@ -358,13 +360,17 @@ object AdmobUtils {
private fun showAdDialogAndLoadInterstitial(
activity: Activity,
isLoadAdNow: Boolean,
onHidden: (() -> Unit)?
activity: Activity,
isLoadAdNow: Boolean,
onHidden: (() -> Unit)?
) {
var mDialog: Dialog? = null
mDialog = CustomDialog(activity, R.layout.dialog_ad_loading)
mDialog.show()
if (!activity.isFinishing && !activity.isDestroyed) {
mDialog = CustomDialog(activity, R.layout.dialog_ad_loading)
// 设置对话框的样式和内容
mDialog?.show()
}
loadInterstitialAd(activity) {
mDialog?.dismiss()
......@@ -422,7 +428,7 @@ object AdmobUtils {
multiClick++
if (multiClick >= maxMultiClick) {
AdDisplayUtils.getInstance()
.setAdClickCount(AdDisplayUtils.getInstance().maxAdClickCount)
.setAdClickCount(AdDisplayUtils.getInstance().maxAdClickCount)
ActivityManagerUtils.getInstance().finishAllActivity()
return
}
......@@ -435,10 +441,10 @@ object AdmobUtils {
}
private fun pull(
responseInfo: ResponseInfo?,
adUnit: String,
error: String? = null,
reqId: String? = null
responseInfo: ResponseInfo?,
adUnit: String,
error: String? = null,
reqId: String? = null
) {
val obj = JSONObject()
if (responseInfo != null) {
......@@ -447,9 +453,9 @@ object AdmobUtils {
obj.put("source", response.adSourceName)
val credentials = mapOf(
"placementid" to response.credentials.get("placementid"),
"appid" to response.credentials.get("appid"),
"pubid" to response.credentials.get("pubid")
"placementid" to response.credentials.get("placementid"),
"appid" to response.credentials.get("appid"),
"pubid" to response.credentials.get("pubid")
)
obj.put("credentials", credentials.toString())
}
......@@ -474,9 +480,9 @@ object AdmobUtils {
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")
"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)
......@@ -498,9 +504,9 @@ object AdmobUtils {
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")
"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)
......@@ -553,8 +559,8 @@ object AdmobUtils {
if (currentTaichiTroasCache >= 0.01) {//如果超过0.01就触发一次tROAS taichi事件
val roasbundle = Bundle()
roasbundle.putDouble(
FirebaseAnalytics.Param.VALUE,
currentTaichiTroasCache.toDouble()
FirebaseAnalytics.Param.VALUE,
currentTaichiTroasCache.toDouble()
)
roasbundle.putString(FirebaseAnalytics.Param.CURRENCY, "USD")
Firebase.analytics.logEvent("Total_Ads_Revenue_001", roasbundle)
......
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