Forráskód Böngészése

Add implementations of RewardedAdManager

GameJob 4 éve
szülő
commit
4223ee682a

+ 144 - 0
src/main/java/com/kdanmobile/admanager/admob/AdMobRewardedAdManager.kt

@@ -0,0 +1,144 @@
+package com.kdanmobile.admanager.admob
+
+import android.app.Activity
+import android.content.Context
+import com.google.android.gms.ads.MobileAds
+import com.google.android.gms.ads.reward.RewardItem
+import com.google.android.gms.ads.reward.RewardedVideoAd
+import com.google.android.gms.ads.reward.RewardedVideoAdListener
+import com.kdanmobile.admanager.RewardedAdListener
+import com.kdanmobile.admanager.RewardedAdManager
+import com.kdanmobile.admanager.toRewardedAdItem
+import java.util.concurrent.CopyOnWriteArrayList
+
+class AdMobRewardedAdManager: RewardedAdManager {
+    override var isPersonalized: Boolean = false
+    private val adListeners = CopyOnWriteArrayList<RewardedAdListener>()
+    private val rewardedVideoAdMap = HashMap<String, RewardedVideoAd>()
+    private val rewardedAdStatusMap = HashMap<String, RewardedAdStatus>()
+
+    private enum class RewardedAdStatus {
+        NOT_INIT,
+        LOADING,
+        LOADED,
+        FAILED
+    }
+
+    override fun request(activity: Activity, adUnitId: String) {
+        rewardedVideoAdMap[adUnitId] ?: kotlin.run {
+            rewardedAdStatusMap[adUnitId] = RewardedAdStatus.NOT_INIT
+
+            MobileAds.getRewardedVideoAdInstance(activity.application).apply {
+                rewardedVideoAdListener = object : RewardedVideoAdListener {
+                    override fun onRewardedVideoAdClosed() {
+                        adListeners.forEach {
+                            it.onRewardedAdClosed(adUnitId)
+                        }
+                    }
+
+                    override fun onRewardedVideoAdLeftApplication() {
+                        adListeners.forEach {
+                            it.onRewardedAdLeftApplication(adUnitId)
+                        }
+                    }
+
+                    override fun onRewardedVideoAdLoaded() {
+                        adListeners.forEach {
+                            it.onRewardedAdLoaded(adUnitId)
+                        }
+                        rewardedAdStatusMap[adUnitId] = RewardedAdStatus.LOADED
+                    }
+
+                    override fun onRewardedVideoAdOpened() {
+                        adListeners.forEach {
+                            it.onRewardedAdOpened(adUnitId)
+                        }
+                    }
+
+                    override fun onRewardedVideoCompleted() {
+                        adListeners.forEach {
+                            it.onRewardedAdCompleted(adUnitId)
+                        }
+                    }
+
+                    override fun onRewarded(rewardItem: RewardItem?) {
+                        val item = rewardItem?.toRewardedAdItem()
+                        adListeners.forEach {
+                            it.onRewarded(adUnitId, item)
+                        }
+                    }
+
+                    override fun onRewardedVideoStarted() {
+                        adListeners.forEach {
+                            it.onRewardedAdStarted(adUnitId)
+                        }
+                    }
+
+                    override fun onRewardedVideoAdFailedToLoad(errorCode: Int) {
+                        adListeners.forEach {
+                            it.onRewardedAdFailedToLoad(adUnitId, errorCode)
+                        }
+                        rewardedAdStatusMap[adUnitId] = RewardedAdStatus.FAILED
+                    }
+                }
+            }
+        }
+        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
+        }
+    }
+
+    override fun show(adUnitId: String): Boolean {
+        if (!isLoaded(adUnitId)) return false
+        return rewardedVideoAdMap[adUnitId]?.let {
+            it.show()
+            rewardedAdStatusMap[adUnitId] = RewardedAdStatus.NOT_INIT
+            request(adUnitId)
+            true
+        } ?: false
+    }
+
+    override fun registerListener(listener: RewardedAdListener) {
+        adListeners.add(listener)
+    }
+
+    override fun unregisterListener(listener: RewardedAdListener) {
+        adListeners.remove(listener)
+    }
+
+    override fun isLoading(adUnitId: String): Boolean {
+        return rewardedAdStatusMap[adUnitId] == RewardedAdStatus.LOADING
+    }
+
+    override fun isLoaded(adUnitId: String): Boolean {
+        return rewardedAdStatusMap[adUnitId] == RewardedAdStatus.LOADED
+    }
+
+    override fun pauseAll(context: Context) {
+        for (rewardedAd in rewardedVideoAdMap.values) {
+            rewardedAd.pause(context)
+        }
+    }
+
+    override fun resumeAll(context: Context) {
+        for (rewardedAd in rewardedVideoAdMap.values) {
+            rewardedAd.resume(context)
+        }
+    }
+
+    override fun destroyAll(context: Context) {
+        for (rewardedAd in rewardedVideoAdMap.values) {
+            rewardedAd.destroy(context)
+        }
+        rewardedVideoAdMap.clear()
+        rewardedAdStatusMap.clear()
+    }
+}

+ 51 - 0
src/main/java/com/kdanmobile/admanager/mopub/MoPubRewardedAdManager.kt

@@ -0,0 +1,51 @@
+package com.kdanmobile.admanager.mopub
+
+import android.app.Activity
+import android.content.Context
+import com.kdanmobile.admanager.RewardedAdListener
+import com.kdanmobile.admanager.RewardedAdManager
+
+class MoPubRewardedAdManager: RewardedAdManager {
+
+    override var isPersonalized: Boolean = false
+
+    override fun request(activity: Activity, adUnitId: String) {
+        // do nothing
+    }
+
+    override fun request(adUnitId: String) {
+        // do nothing
+    }
+
+    override fun show(adUnitId: String): Boolean {
+        return false
+    }
+
+    override fun registerListener(listener: RewardedAdListener) {
+        // do nothing
+    }
+
+    override fun unregisterListener(listener: RewardedAdListener) {
+        // do nothing
+    }
+
+    override fun isLoading(adUnitId: String): Boolean {
+        return false
+    }
+
+    override fun isLoaded(adUnitId: String): Boolean {
+        return false
+    }
+
+    override fun pauseAll(context: Context) {
+        // do nothing
+    }
+
+    override fun resumeAll(context: Context) {
+        // do nothing
+    }
+
+    override fun destroyAll(context: Context) {
+        // do nothing
+    }
+}