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,28 +26,19 @@ class AdInsertMgr { ...@@ -25,28 +26,19 @@ 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)
return adState.onAdDisplayFailed()
}
if (adState.showingAd) {
showCallBack?.failed(1)
return 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)
...@@ -58,26 +50,38 @@ class AdInsertMgr { ...@@ -58,26 +50,38 @@ class AdInsertMgr {
} }
} }
if (adState.currentAd == null) { adEvent.adPrepareShow()
loadAd(activity, isUnLimit, adEvent) adState.activityRef = WeakReference(activity)
return this.showCallBack = showCallBack
if (adState.adDialog == null) {
adState.adDialog = activity.showAdPreparingDialog(1)
} else {
adState.adDialog?.dismiss()
} }
if (!adAvailable()) { 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,32 +112,34 @@ class AdInsertMgr { ...@@ -108,32 +112,34 @@ 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) {
adState.loadingAd = true
adEvent.adPulStart() adEvent.adPulStart()
InterstitialAd.load(context, GlobalConfig.ID_ADMOB_INTER, AdRequest.Builder().build(), InterstitialAd.load(context, GlobalConfig.ID_ADMOB_INTER, AdRequest.Builder().build(),
object : InterstitialAdLoadCallback() { object : InterstitialAdLoadCallback() {
override fun onAdLoaded(ad: InterstitialAd) { override fun onAdLoaded(ad: InterstitialAd) {
adState.onAdLoaded(ad) adState.onAdLoaded(ad)
val ac = adState.activityRef?.get() if (this@AdInsertMgr.showCallBack != null) {
if (ac != null) { showReadyAd(adEvent)
show(ac, isUnLimit, adEvent, null)
} }
(adEvent as AdmobEvent).pullAd(ad.responseInfo) (adEvent as AdmobEvent).pullAd(ad.responseInfo)
LimitUtils.addRequestNum() LimitUtils.addRequestNum()
...@@ -148,7 +154,6 @@ class AdInsertMgr { ...@@ -148,7 +154,6 @@ class AdInsertMgr {
} }
} }
) )
}
} }
......
...@@ -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,19 +29,6 @@ class AdOpenMgr { ...@@ -28,19 +29,6 @@ class AdOpenMgr {
if (activity.isFinishing || activity.isDestroyed) { if (activity.isFinishing || activity.isDestroyed) {
return return
} }
if (adState.showingAd) {
showCallBack?.failed()
adEvent.adShowError("showingAd")
adState.showingAd = false
return
}
if (showCallBack != null) {
this.showCallBack = showCallBack
adState.activityRef = WeakReference(activity)
adEvent.adPrepareShow()
}
if (!adState.loadingAd) {
if (!isUnLimit) { if (!isUnLimit) {
if (!LimitUtils.isAdShow(AdsType.OPEN, adEvent)) { if (!LimitUtils.isAdShow(AdsType.OPEN, adEvent)) {
showCallBack?.failed() showCallBack?.failed()
...@@ -51,27 +39,38 @@ class AdOpenMgr { ...@@ -51,27 +39,38 @@ class AdOpenMgr {
return return
} }
} }
if (!adAvailable() || adState.currentAd == null) {
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() {
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,12 +114,12 @@ class AdOpenMgr { ...@@ -112,12 +114,12 @@ 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() adEvent.adPulStart()
AppOpenAd.load( AppOpenAd.load(
...@@ -127,13 +129,13 @@ class AdOpenMgr { ...@@ -127,13 +129,13 @@ class AdOpenMgr {
object : AppOpenAd.AppOpenAdLoadCallback() { object : AppOpenAd.AppOpenAdLoadCallback() {
override fun onAdLoaded(appOpenAd: AppOpenAd) { override fun onAdLoaded(appOpenAd: AppOpenAd) {
adState.onAdLoaded(appOpenAd) adState.onAdLoaded(appOpenAd)
val ac = adState.activityRef?.get() if (this@AdOpenMgr.showCallBack != null) {
if (ac != null) { showReadyAd(adEvent)
show(ac, isUnLimit, adEvent, null)
} }
(adEvent as AdmobEvent).pullAd(appOpenAd.responseInfo) (adEvent as AdmobEvent).pullAd(appOpenAd.responseInfo)
appOpenAd.onPaidEventListener = AdmobEvent.EventOnPaidEventListener(appOpenAd) appOpenAd.onPaidEventListener = AdmobEvent.EventOnPaidEventListener(appOpenAd)
LimitUtils.addRequestNum() LimitUtils.addRequestNum()
} }
override fun onAdFailedToLoad(loadAdError: LoadAdError) { override fun onAdFailedToLoad(loadAdError: LoadAdError) {
...@@ -145,7 +147,6 @@ class AdOpenMgr { ...@@ -145,7 +147,6 @@ class AdOpenMgr {
} }
) )
} }
}
private fun adAvailable() = ((System.currentTimeMillis() - adState.lastLoadTime) / 1000 / 60).toInt() < 30 private fun adAvailable() = ((System.currentTimeMillis() - adState.lastLoadTime) / 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