Commit 4498ed5c authored by wanglei's avatar wanglei

...

parent aaaaec39
...@@ -107,7 +107,7 @@ object AdsMgr { ...@@ -107,7 +107,7 @@ object AdsMgr {
if (adsConfigBean.adSwitch) { if (adsConfigBean.adSwitch) {
admobInitCallBack?.invoke() admobInitCallBack?.invoke()
admobInitCallBack = null admobInitCallBack = null
adOpenMgr.loadAd(context, false, AdmobEvent("openAd", context::class.java.simpleName)) // adOpenMgr.loadAd(context, false, AdmobEvent("openAd", context::class.java.simpleName))
adInsertMgr.loadAd(context, false, AdmobEvent("interAd", context::class.java.simpleName)) adInsertMgr.loadAd(context, false, AdmobEvent("interAd", context::class.java.simpleName))
} }
} }
......
...@@ -9,6 +9,7 @@ import com.base.locationsharewhite.ads.AdsShowCallBack ...@@ -9,6 +9,7 @@ import com.base.locationsharewhite.ads.AdsShowCallBack
import com.base.locationsharewhite.ads.AdsType import com.base.locationsharewhite.ads.AdsType
import com.base.locationsharewhite.ads.LimitUtils import com.base.locationsharewhite.ads.LimitUtils
import com.base.locationsharewhite.ads.adState import com.base.locationsharewhite.ads.adState
import com.base.locationsharewhite.helper.MyApplication
import com.google.android.gms.ads.AdError import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.FullScreenContentCallback import com.google.android.gms.ads.FullScreenContentCallback
...@@ -25,59 +26,62 @@ class AdInsertMgr { ...@@ -25,59 +26,62 @@ class AdInsertMgr {
private var adState = adState<InterstitialAd>() private var adState = adState<InterstitialAd>()
private var showCallBack: AdsShowCallBack? = null private var showCallBack: AdsShowCallBack? = null
fun show(activity: Activity, isUnLimit: Boolean, adEvent: AdEvent, showCallBack: AdsShowCallBack?) { fun show(
activity: Activity,
isUnLimit: Boolean,
adEvent: AdEvent,
showCallBack: AdsShowCallBack? = null
) {
if (activity.isFinishing || activity.isDestroyed) { if (activity.isFinishing || activity.isDestroyed) {
showCallBack?.failed(0) showCallBack?.failed(0)
adState.onAdDisplayFailed()
return return
} }
if (adState.showingAd) {
showCallBack?.failed(1)
return
}
if (showCallBack != null) {
this.showCallBack = showCallBack
adState.activityRef = WeakReference(activity)
if (adState.adDialog == null) {
adState.adDialog = activity.showAdPreparingDialog(1)
}
adEvent.adPrepareShow()
}
if (!adState.loadingAd) {
if (!isUnLimit) { if (!isUnLimit) {
if (!LimitUtils.isAdShow(AdsType.INSERT, adEvent)) { if (!LimitUtils.isAdShow(AdsType.INSERT, adEvent)) {
showCallBack?.failed(2) showCallBack?.failed(2)
return return
}
if (LimitUtils.isIntervalLimited(adState.lastShowTime, adEvent)) {
showCallBack?.failed(3)
return
}
} }
if (LimitUtils.isIntervalLimited(adState.lastShowTime, adEvent)) {
if (adState.currentAd == null) { showCallBack?.failed(3)
loadAd(activity, isUnLimit, adEvent)
return return
} }
if (!adAvailable()) { }
adEvent.adPrepareShow()
adState.activityRef = WeakReference(activity)
this.showCallBack = showCallBack
if (adState.adDialog == null) {
adState.adDialog = activity.showAdPreparingDialog(1)
} else {
adState.adDialog?.dismiss()
}
val needLoad = adState.currentAd == null || !adAvailable()
if (needLoad) {
if (!adState.loadingAd) {
loadAd(activity, isUnLimit, adEvent) loadAd(activity, isUnLimit, adEvent)
return
} }
showReadyAd(adEvent, activity) } else {
showReadyAd(adEvent)
} }
} }
private fun showReadyAd(adEvent: AdEvent, activity: Activity) { private fun showReadyAd(adEvent: AdEvent) {
adState.currentAd?.run { adState.currentAd?.run {
fullScreenContentCallback = object : FullScreenContentCallback() { fullScreenContentCallback = object : FullScreenContentCallback() {
override fun onAdShowedFullScreenContent() { override fun onAdShowedFullScreenContent() {
super.onAdShowedFullScreenContent() super.onAdShowedFullScreenContent()
val ac = adState.activityRef?.get()
(adEvent as AdmobEvent).showAd(responseInfo, ac)
adState.onAdDisplayed() adState.onAdDisplayed()
showCallBack?.show() showCallBack?.show()
(adEvent as AdmobEvent).showAd(responseInfo, activity)
LimitUtils.addDisplayNum() LimitUtils.addDisplayNum()
} }
...@@ -98,7 +102,7 @@ class AdInsertMgr { ...@@ -98,7 +102,7 @@ class AdInsertMgr {
showCallBack?.close() showCallBack?.close()
showCallBack = null showCallBack = null
loadAd(activity.applicationContext, false, AdmobEvent("interAd", "preload")) loadAd(MyApplication.appContext, false, AdmobEvent("interAd", "preload"))
} }
override fun onAdClicked() { override fun onAdClicked() {
...@@ -108,47 +112,48 @@ class AdInsertMgr { ...@@ -108,47 +112,48 @@ class AdInsertMgr {
LimitUtils.addClickNum() LimitUtils.addClickNum()
} }
} }
show(activity) val activity = adState.activityRef?.get()
activity?.let { show(it) }
} }
} }
fun loadAd(context: Context, isUnLimit: Boolean, adEvent: AdEvent) { fun loadAd(
context: Context,
isUnLimit: Boolean,
adEvent: AdEvent
) {
if (!isUnLimit) { if (!isUnLimit) {
if (!LimitUtils.isAdShow(AdsType.INSERT, adEvent)) { if (!LimitUtils.isAdShow(AdsType.INSERT, adEvent)) {
this.showCallBack?.close(4) this.showCallBack?.close(4)
this.showCallBack = null this.showCallBack = null
adState.onAdLoadFailed()
return return
} }
} }
if (!adState.loadingAd) { adEvent.adPulStart()
adState.loadingAd = true
adEvent.adPulStart()
InterstitialAd.load(context, GlobalConfig.ID_ADMOB_INTER, AdRequest.Builder().build(),
object : InterstitialAdLoadCallback() {
override fun onAdLoaded(ad: InterstitialAd) {
adState.onAdLoaded(ad)
val ac = adState.activityRef?.get()
if (ac != null) {
show(ac, isUnLimit, adEvent, null)
}
(adEvent as AdmobEvent).pullAd(ad.responseInfo)
LimitUtils.addRequestNum()
ad.onPaidEventListener = AdmobEvent.EventOnPaidEventListener(ad)
}
override fun onAdFailedToLoad(loadAdError: LoadAdError) { InterstitialAd.load(context, GlobalConfig.ID_ADMOB_INTER, AdRequest.Builder().build(),
adState.onAdLoadFailed() object : InterstitialAdLoadCallback() {
(adEvent as AdmobEvent).pullAd(loadAdError.responseInfo, loadAdError) override fun onAdLoaded(ad: InterstitialAd) {
showCallBack?.googleFailed() adState.onAdLoaded(ad)
showCallBack = null if (this@AdInsertMgr.showCallBack != null) {
showReadyAd(adEvent)
} }
(adEvent as AdmobEvent).pullAd(ad.responseInfo)
LimitUtils.addRequestNum()
ad.onPaidEventListener = AdmobEvent.EventOnPaidEventListener(ad)
} }
)
} override fun onAdFailedToLoad(loadAdError: LoadAdError) {
adState.onAdLoadFailed()
(adEvent as AdmobEvent).pullAd(loadAdError.responseInfo, loadAdError)
showCallBack?.googleFailed()
showCallBack = null
}
}
)
} }
......
...@@ -8,6 +8,7 @@ import com.base.locationsharewhite.ads.AdsShowCallBack ...@@ -8,6 +8,7 @@ import com.base.locationsharewhite.ads.AdsShowCallBack
import com.base.locationsharewhite.ads.AdsType import com.base.locationsharewhite.ads.AdsType
import com.base.locationsharewhite.ads.LimitUtils import com.base.locationsharewhite.ads.LimitUtils
import com.base.locationsharewhite.ads.adState import com.base.locationsharewhite.ads.adState
import com.base.locationsharewhite.helper.MyApplication
import com.google.android.gms.ads.AdError import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.FullScreenContentCallback import com.google.android.gms.ads.FullScreenContentCallback
...@@ -28,50 +29,48 @@ class AdOpenMgr { ...@@ -28,50 +29,48 @@ class AdOpenMgr {
if (activity.isFinishing || activity.isDestroyed) { if (activity.isFinishing || activity.isDestroyed) {
return return
} }
if (adState.showingAd) { if (!isUnLimit) {
showCallBack?.failed() if (!LimitUtils.isAdShow(AdsType.OPEN, adEvent)) {
adEvent.adShowError("showingAd") showCallBack?.failed()
adState.showingAd = false return
return
}
if (showCallBack != null) {
this.showCallBack = showCallBack
adState.activityRef = WeakReference(activity)
adEvent.adPrepareShow()
}
if (!adState.loadingAd) {
if (!isUnLimit) {
if (!LimitUtils.isAdShow(AdsType.OPEN, adEvent)) {
showCallBack?.failed()
return
}
if (LimitUtils.isIntervalLimited(adState.lastShowTime, adEvent)) {
showCallBack?.failed()
return
}
} }
if (!adAvailable() || adState.currentAd == null) { if (LimitUtils.isIntervalLimited(adState.lastShowTime, adEvent)) {
loadAd(activity, isUnLimit, adEvent) showCallBack?.failed()
return return
} }
showReadyAd(adEvent, activity)
} }
adEvent.adPrepareShow()
adState.activityRef = WeakReference(activity)
this.showCallBack = showCallBack
// if (adState.adDialog == null) {
// adState.adDialog = activity.showAdPreparingDialog(1)
// } else {
// adState.adDialog?.dismiss()
// }
val needLoad = adState.currentAd == null || !adAvailable()
if (needLoad) {
if (!adState.loadingAd) {
loadAd(activity, isUnLimit, adEvent)
}
} else {
showReadyAd(adEvent)
}
} }
private fun showReadyAd(adEvent: AdEvent, activity: Activity) { private fun showReadyAd(adEvent: AdEvent) {
adState.currentAd?.run { adState.currentAd?.run {
fullScreenContentCallback = object : FullScreenContentCallback() { fullScreenContentCallback = object : FullScreenContentCallback() {
override fun onAdShowedFullScreenContent() { override fun onAdShowedFullScreenContent() {
val ac = adState.activityRef?.get()
(adEvent as AdmobEvent).showAd(this@run.responseInfo, ac)
showCallBack?.show() showCallBack?.show()
(adEvent as AdmobEvent).showAd(this@run.responseInfo, activity)
adState.onAdDisplayed() adState.onAdDisplayed()
//计数 //计数
LimitUtils.addDisplayNum() LimitUtils.addDisplayNum()
} }
override fun onAdFailedToShowFullScreenContent(adError: AdError) { override fun onAdFailedToShowFullScreenContent(adError: AdError) {
...@@ -93,7 +92,9 @@ class AdOpenMgr { ...@@ -93,7 +92,9 @@ class AdOpenMgr {
adState.onAdHidden() adState.onAdHidden()
loadAd(activity.applicationContext, false, AdmobEvent("openAd", "preload")) //预加载,“Timeout for show call succeed.”预加载的广告大概率,
loadAd(MyApplication.appContext, false, AdmobEvent("openAd", "preload"))
} }
override fun onAdClicked() { override fun onAdClicked() {
...@@ -102,7 +103,8 @@ class AdOpenMgr { ...@@ -102,7 +103,8 @@ class AdOpenMgr {
LimitUtils.addClickNum() LimitUtils.addClickNum()
} }
} }
show(activity) val activity = adState.activityRef?.get()
activity?.let { show(it) }
} }
} }
...@@ -112,39 +114,38 @@ class AdOpenMgr { ...@@ -112,39 +114,38 @@ class AdOpenMgr {
if (!LimitUtils.isAdShow(AdsType.OPEN, adEvent)) { if (!LimitUtils.isAdShow(AdsType.OPEN, adEvent)) {
this.showCallBack?.close() this.showCallBack?.close()
this.showCallBack = null this.showCallBack = null
adState.onAdLoadFailed()
return return
} }
} }
if (!adState.loadingAd) {
adState.loadingAd = true
adEvent.adPulStart()
AppOpenAd.load(
context,
GlobalConfig.ID_ADMOB_OPEN,
AdRequest.Builder().build(),
object : AppOpenAd.AppOpenAdLoadCallback() {
override fun onAdLoaded(appOpenAd: AppOpenAd) {
adState.onAdLoaded(appOpenAd)
val ac = adState.activityRef?.get()
if (ac != null) {
show(ac, isUnLimit, adEvent, null)
}
(adEvent as AdmobEvent).pullAd(appOpenAd.responseInfo)
appOpenAd.onPaidEventListener = AdmobEvent.EventOnPaidEventListener(appOpenAd)
LimitUtils.addRequestNum()
}
override fun onAdFailedToLoad(loadAdError: LoadAdError) { adEvent.adPulStart()
showCallBack?.googleFailed()
showCallBack = null AppOpenAd.load(
adState.onAdLoadFailed() context,
(adEvent as AdmobEvent).pullAd(loadAdError.responseInfo, loadAdError) GlobalConfig.ID_ADMOB_OPEN,
AdRequest.Builder().build(),
object : AppOpenAd.AppOpenAdLoadCallback() {
override fun onAdLoaded(appOpenAd: AppOpenAd) {
adState.onAdLoaded(appOpenAd)
if (this@AdOpenMgr.showCallBack != null) {
showReadyAd(adEvent)
} }
(adEvent as AdmobEvent).pullAd(appOpenAd.responseInfo)
appOpenAd.onPaidEventListener = AdmobEvent.EventOnPaidEventListener(appOpenAd)
LimitUtils.addRequestNum()
} }
)
} override fun onAdFailedToLoad(loadAdError: LoadAdError) {
showCallBack?.googleFailed()
showCallBack = null
adState.onAdLoadFailed()
(adEvent as AdmobEvent).pullAd(loadAdError.responseInfo, loadAdError)
}
}
)
} }
private fun adAvailable() = ((System.currentTimeMillis() - adState.lastLoadTime) / 1000 / 60).toInt() < 30 private fun adAvailable() = ((System.currentTimeMillis() - adState.lastLoadTime) / 1000 / 60).toInt() < 30
......
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