Просмотр исходного кода

AdMobRewardedAdManager::isLoaded return false when the rewarded ad is requested long time ago

cooperku_kdanmobile 4 лет назад
Родитель
Сommit
f9d3a004c6
1 измененных файлов с 41 добавлено и 23 удалено
  1. 41 23
      src/main/java/com/kdanmobile/admanager/admob/AdMobRewardedAdManager.kt

+ 41 - 23
src/main/java/com/kdanmobile/admanager/admob/AdMobRewardedAdManager.kt

@@ -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 {
@@ -45,7 +55,7 @@ class AdMobRewardedAdManager: RewardedAdManager {
                         adListeners.forEach {
                             it.onRewardedAdLoaded(adUnitId)
                         }
-                        rewardedAdStatusMap[adUnitId] = RewardedAdStatus.LOADED
+                        rewardedAdDataMap[adUnitId]?.status = RewardedAdStatus.LOADED
                     }
 
                     override fun onRewardedVideoAdOpened() {
@@ -77,29 +87,32 @@ class AdMobRewardedAdManager: RewardedAdManager {
                         adListeners.forEach {
                             it.onRewardedAdFailedToLoad(adUnitId, errorCode)
                         }
-                        rewardedAdStatusMap[adUnitId] = RewardedAdStatus.FAILED
+                        rewardedAdDataMap[adUnitId]?.status = 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()
     }
 }