|
@@ -12,10 +12,20 @@ import com.kdanmobile.admanager.toRewardedAdItem
|
|
|
import java.util.concurrent.CopyOnWriteArrayList
|
|
|
|
|
|
class AdMobRewardedAdManager: RewardedAdManager {
|
|
|
+
|
|
|
+ companion object {
|
|
|
+ private const val MAX_KEEPING_AD_DURATION = 60 * 60 * 1000L
|
|
|
+ }
|
|
|
+
|
|
|
override var isPersonalized: Boolean = false
|
|
|
private val adListeners = CopyOnWriteArrayList<RewardedAdListener>()
|
|
|
- private val rewardedVideoAdMap = HashMap<String, RewardedVideoAd>()
|
|
|
- private val rewardedAdStatusMap = HashMap<String, RewardedAdStatus>()
|
|
|
+ private val rewardedAdDataMap = HashMap<String, RewardedAdData>()
|
|
|
+
|
|
|
+ private data class RewardedAdData(
|
|
|
+ var ad: RewardedVideoAd,
|
|
|
+ var status: RewardedAdStatus,
|
|
|
+ var time: Long = 0L
|
|
|
+ )
|
|
|
|
|
|
private enum class RewardedAdStatus {
|
|
|
NOT_INIT,
|
|
@@ -25,9 +35,9 @@ class AdMobRewardedAdManager: RewardedAdManager {
|
|
|
}
|
|
|
|
|
|
override fun request(activity: Activity, adUnitId: String) {
|
|
|
- rewardedVideoAdMap[adUnitId] ?: run {
|
|
|
- rewardedAdStatusMap[adUnitId] = RewardedAdStatus.NOT_INIT
|
|
|
- rewardedVideoAdMap[adUnitId] = MobileAds.getRewardedVideoAdInstance(activity.application).apply {
|
|
|
+ if (rewardedAdDataMap[adUnitId] == null) {
|
|
|
+ val status = RewardedAdStatus.NOT_INIT
|
|
|
+ val ad = MobileAds.getRewardedVideoAdInstance(activity.application).apply {
|
|
|
rewardedVideoAdListener = object : RewardedVideoAdListener {
|
|
|
override fun onRewardedVideoAdClosed() {
|
|
|
adListeners.forEach {
|
|
@@ -42,10 +52,10 @@ class AdMobRewardedAdManager: RewardedAdManager {
|
|
|
}
|
|
|
|
|
|
override fun onRewardedVideoAdLoaded() {
|
|
|
+ rewardedAdDataMap[adUnitId]?.status = RewardedAdStatus.LOADED
|
|
|
adListeners.forEach {
|
|
|
it.onRewardedAdLoaded(adUnitId)
|
|
|
}
|
|
|
- rewardedAdStatusMap[adUnitId] = RewardedAdStatus.LOADED
|
|
|
}
|
|
|
|
|
|
override fun onRewardedVideoAdOpened() {
|
|
@@ -74,32 +84,35 @@ class AdMobRewardedAdManager: RewardedAdManager {
|
|
|
}
|
|
|
|
|
|
override fun onRewardedVideoAdFailedToLoad(errorCode: Int) {
|
|
|
+ rewardedAdDataMap[adUnitId]?.status = RewardedAdStatus.FAILED
|
|
|
adListeners.forEach {
|
|
|
it.onRewardedAdFailedToLoad(adUnitId, errorCode)
|
|
|
}
|
|
|
- rewardedAdStatusMap[adUnitId] = RewardedAdStatus.FAILED
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ rewardedAdDataMap[adUnitId] = RewardedAdData(ad, status)
|
|
|
}
|
|
|
request(adUnitId)
|
|
|
}
|
|
|
|
|
|
override fun request(adUnitId: String) {
|
|
|
- rewardedVideoAdMap[adUnitId]?.let {
|
|
|
- val status = rewardedAdStatusMap[adUnitId]
|
|
|
- if (status == RewardedAdStatus.LOADING) return
|
|
|
- if (status == RewardedAdStatus.LOADED) return
|
|
|
- it.loadAd(adUnitId, RequestFactory.create(isPersonalized))
|
|
|
- rewardedAdStatusMap[adUnitId] = RewardedAdStatus.LOADING
|
|
|
+ rewardedAdDataMap[adUnitId]?.apply {
|
|
|
+ if (System.currentTimeMillis() - time <= MAX_KEEPING_AD_DURATION) {
|
|
|
+ if (status == RewardedAdStatus.LOADING) return
|
|
|
+ if (status == RewardedAdStatus.LOADED) return
|
|
|
+ }
|
|
|
+ ad.loadAd(adUnitId, RequestFactory.create(isPersonalized))
|
|
|
+ status = RewardedAdStatus.LOADING
|
|
|
+ time = System.currentTimeMillis()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
override fun show(adUnitId: String): Boolean {
|
|
|
if (!isLoaded(adUnitId)) return false
|
|
|
- return rewardedVideoAdMap[adUnitId]?.let {
|
|
|
- it.show()
|
|
|
- rewardedAdStatusMap[adUnitId] = RewardedAdStatus.NOT_INIT
|
|
|
+ return rewardedAdDataMap[adUnitId]?.let {
|
|
|
+ it.ad.show()
|
|
|
+ it.status = RewardedAdStatus.NOT_INIT
|
|
|
true
|
|
|
} ?: false
|
|
|
}
|
|
@@ -113,30 +126,35 @@ class AdMobRewardedAdManager: RewardedAdManager {
|
|
|
}
|
|
|
|
|
|
override fun isLoading(adUnitId: String): Boolean {
|
|
|
- return rewardedAdStatusMap[adUnitId] == RewardedAdStatus.LOADING
|
|
|
+ return rewardedAdDataMap[adUnitId]?.status == RewardedAdStatus.LOADING
|
|
|
}
|
|
|
|
|
|
override fun isLoaded(adUnitId: String): Boolean {
|
|
|
- return rewardedAdStatusMap[adUnitId] == RewardedAdStatus.LOADED
|
|
|
+ val data = rewardedAdDataMap[adUnitId]
|
|
|
+ return when {
|
|
|
+ data == null -> false
|
|
|
+ data.status != RewardedAdStatus.LOADED -> false
|
|
|
+ System.currentTimeMillis() - data.time > MAX_KEEPING_AD_DURATION -> false
|
|
|
+ else -> true
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
override fun pauseAll(context: Context) {
|
|
|
- for (rewardedAd in rewardedVideoAdMap.values) {
|
|
|
+ for (rewardedAd in rewardedAdDataMap.values.map { it.ad }) {
|
|
|
rewardedAd.pause(context)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
override fun resumeAll(context: Context) {
|
|
|
- for (rewardedAd in rewardedVideoAdMap.values) {
|
|
|
+ for (rewardedAd in rewardedAdDataMap.values.map { it.ad }) {
|
|
|
rewardedAd.resume(context)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
override fun destroyAll(context: Context) {
|
|
|
- for (rewardedAd in rewardedVideoAdMap.values) {
|
|
|
+ for (rewardedAd in rewardedAdDataMap.values.map { it.ad }) {
|
|
|
rewardedAd.destroy(context)
|
|
|
}
|
|
|
- rewardedVideoAdMap.clear()
|
|
|
- rewardedAdStatusMap.clear()
|
|
|
+ rewardedAdDataMap.clear()
|
|
|
}
|
|
|
}
|