소스 검색

Add RewardedAdManager codes & refactor: add AbstractAdManager

GameJob 4 년 전
부모
커밋
64ab616e3c

+ 115 - 0
src/main/java/com/kdanmobile/admanager/AbstractAdManager.kt

@@ -0,0 +1,115 @@
+package com.kdanmobile.admanager
+
+import android.app.Activity
+import android.content.Context
+import android.view.View
+import java.util.concurrent.CopyOnWriteArrayList
+
+abstract class AbstractAdManager : AdManager {
+    protected lateinit var context: Context
+    protected lateinit var interstitialAdManager: InterstitialAdManager
+    protected lateinit var nativeAdManager: NativeAdManager
+    protected lateinit var rewardedAdManager: RewardedAdManager
+
+    override var isInitialized = false
+    override val onInitListeners = CopyOnWriteArrayList<Runnable>()
+    override var isPersonalized: Boolean = true
+        set(value) {
+            field = value
+            interstitialAdManager.isPersonalized = value
+            nativeAdManager.isPersonalized = value
+            rewardedAdManager.isPersonalized = value
+        }
+
+    override fun requestInterstitial(adUnitId: String) {
+        interstitialAdManager.request(adUnitId)
+    }
+
+    override fun requestInterstitial(activity: Activity, adUnitId: String) {
+        interstitialAdManager.request(activity, adUnitId)
+    }
+
+    override fun showInterstitial(adUnitId: String): Boolean {
+        return interstitialAdManager.show(adUnitId)
+    }
+
+    override fun destroyInterstitial(activity: Activity) {
+        interstitialAdManager.destroy(activity)
+    }
+
+    override fun registerInterstitialAdListener(interstitialAdListener: InterstitialAdListener) {
+        interstitialAdManager.registerListener(interstitialAdListener)
+    }
+
+    override fun unregisterInterstitialAdListener(interstitialAdListener: InterstitialAdListener) {
+        interstitialAdManager.unregisterListener(interstitialAdListener)
+    }
+
+    override fun isInterstitialLoading(adUnitId: String): Boolean {
+        return interstitialAdManager.isLoading(adUnitId)
+    }
+
+    override fun isInterstitialLoaded(adUnitId: String): Boolean {
+        return interstitialAdManager.isLoaded(adUnitId)
+    }
+
+    override fun registerNativeAdListener(nativeAdListener: NativeAdListener) {
+        nativeAdManager.listener += nativeAdListener
+    }
+
+    override fun unregisterNativeAdListener(nativeAdListener: NativeAdListener) {
+        nativeAdManager.listener -= nativeAdListener
+    }
+
+    override fun requestNative(adUnitId: String, nativeAdViewBinder: NativeAdViewBinder) {
+        nativeAdManager.request(adUnitId, nativeAdViewBinder)
+    }
+
+    override fun takeNativeAdView(adUnitId: String, nativeAdViewBinder: NativeAdViewBinder): View? {
+        return nativeAdManager.takeAdView(adUnitId, nativeAdViewBinder)
+    }
+
+    override fun isNativeLoading(adUnitId: String): Boolean {
+        return nativeAdManager.isLoading(adUnitId)
+    }
+
+    override fun requestRewardedAd(activity: Activity, adUnitId: String) {
+        rewardedAdManager.request(activity, adUnitId)
+    }
+
+    override fun requestRewardedAd(adUnitId: String) {
+        rewardedAdManager.request(adUnitId)
+    }
+
+    override fun showRewardedAd(adUnitId: String): Boolean {
+        return rewardedAdManager.show(adUnitId)
+    }
+
+    override fun registerRewardedAdListener(listener: RewardedAdListener) {
+        rewardedAdManager.registerListener(listener)
+    }
+
+    override fun unregisterRewardedAdListener(listener: RewardedAdListener) {
+        rewardedAdManager.unregisterListener(listener)
+    }
+
+    override fun isRewardedAdLoading(adUnitId: String): Boolean {
+        return rewardedAdManager.isLoading(adUnitId)
+    }
+
+    override fun isRewardedAdLoaded(adUnitId: String): Boolean {
+        return rewardedAdManager.isLoaded(adUnitId)
+    }
+
+    override fun pauseRewardedAds(context: Context) {
+        rewardedAdManager.pauseAll(context)
+    }
+
+    override fun resumeRewardedAds(context: Context) {
+        rewardedAdManager.resumeAll(context)
+    }
+
+    override fun destroyRewardedAds(context: Context) {
+        rewardedAdManager.destroyAll(context)
+    }
+}

+ 14 - 0
src/main/java/com/kdanmobile/admanager/AdManager.kt

@@ -27,4 +27,18 @@ interface AdManager {
     fun requestNative(adUnitId: String, nativeAdViewBinder: NativeAdViewBinder)
     fun takeNativeAdView(adUnitId: String, nativeAdViewBinder: NativeAdViewBinder): View?
     fun isNativeLoading(adUnitId: String): Boolean
+
+    fun requestRewardedAd(activity: Activity, adUnitId: String)
+    /**
+     * Use to re-request with same activity, must be called after request(activity, adUnitId)
+     */
+    fun requestRewardedAd(adUnitId: String)
+    fun showRewardedAd(adUnitId: String): Boolean
+    fun registerRewardedAdListener(listener: RewardedAdListener)
+    fun unregisterRewardedAdListener(listener: RewardedAdListener)
+    fun isRewardedAdLoading(adUnitId: String): Boolean
+    fun isRewardedAdLoaded(adUnitId: String): Boolean
+    fun pauseRewardedAds(context: Context)
+    fun resumeRewardedAds(context: Context)
+    fun destroyRewardedAds(context: Context)
 }

+ 3 - 69
src/main/java/com/kdanmobile/admanager/admob/AdmobAdManager.kt

@@ -1,25 +1,10 @@
 package com.kdanmobile.admanager.admob
 
-import android.app.Activity
 import android.content.Context
-import android.view.View
 import com.google.android.gms.ads.MobileAds
-import com.kdanmobile.admanager.*
-import java.util.concurrent.CopyOnWriteArrayList
+import com.kdanmobile.admanager.AbstractAdManager
 
-class AdmobAdManager : AdManager {
-    private lateinit var context: Context
-    private lateinit var interstitialAdManager: InterstitialAdManager
-    private lateinit var nativeAdManager: NativeAdManager
-
-    override var isInitialized = false
-    override val onInitListeners = CopyOnWriteArrayList<Runnable>()
-    override var isPersonalized: Boolean = true
-        set(value) {
-            field = value
-            interstitialAdManager.isPersonalized = value
-            nativeAdManager.isPersonalized = value
-        }
+class AdmobAdManager : AbstractAdManager() {
 
     override fun init(context: Context, adAppId: String, adUnitId: String, volume: Float) {
         this.context = context
@@ -29,60 +14,9 @@ class AdmobAdManager : AdManager {
 
         interstitialAdManager = AdmobInterstitialAdManager()
         nativeAdManager = AdmobNativeAdManager(context)
+        rewardedAdManager = AdMobRewardedAdManager()
 
         isInitialized = true
         onInitListeners.forEach { it.run() }
     }
-
-    override fun requestInterstitial(adUnitId: String) {
-        interstitialAdManager.request(adUnitId)
-    }
-
-    override fun requestInterstitial(activity: Activity, adUnitId: String) {
-        interstitialAdManager.request(activity, adUnitId)
-    }
-
-    override fun showInterstitial(adUnitId: String): Boolean {
-        return interstitialAdManager.show(adUnitId)
-    }
-
-    override fun destroyInterstitial(activity: Activity) {
-        interstitialAdManager.destroy(activity)
-    }
-
-    override fun registerInterstitialAdListener(interstitialAdListener: InterstitialAdListener) {
-        interstitialAdManager.registerListener(interstitialAdListener)
-    }
-
-    override fun unregisterInterstitialAdListener(interstitialAdListener: InterstitialAdListener) {
-        interstitialAdManager.unregisterListener(interstitialAdListener)
-    }
-
-    override fun isInterstitialLoading(adUnitId: String): Boolean {
-        return interstitialAdManager.isLoading(adUnitId)
-    }
-
-    override fun isInterstitialLoaded(adUnitId: String): Boolean {
-        return interstitialAdManager.isLoaded(adUnitId)
-    }
-
-    override fun registerNativeAdListener(nativeAdListener: NativeAdListener) {
-        nativeAdManager.listener += nativeAdListener
-    }
-
-    override fun unregisterNativeAdListener(nativeAdListener: NativeAdListener) {
-        nativeAdManager.listener -= nativeAdListener
-    }
-
-    override fun requestNative(adUnitId: String, nativeAdViewBinder: NativeAdViewBinder) {
-        nativeAdManager.request(adUnitId, nativeAdViewBinder)
-    }
-
-    override fun takeNativeAdView(adUnitId: String, nativeAdViewBinder: NativeAdViewBinder): View? {
-        return nativeAdManager.takeAdView(adUnitId, nativeAdViewBinder)
-    }
-
-    override fun isNativeLoading(adUnitId: String): Boolean {
-        return nativeAdManager.isLoading(adUnitId)
-    }
 }

+ 3 - 69
src/main/java/com/kdanmobile/admanager/mopub/MoPubAdManager.kt

@@ -1,26 +1,11 @@
 package com.kdanmobile.admanager.mopub
 
-import android.app.Activity
 import android.content.Context
-import android.view.View
-import com.kdanmobile.admanager.*
+import com.kdanmobile.admanager.AbstractAdManager
 import com.mopub.common.MoPub
 import com.mopub.common.SdkConfiguration
-import java.util.concurrent.CopyOnWriteArrayList
 
-class MoPubAdManager : AdManager {
-    private lateinit var context: Context
-    private lateinit var interstitialAdManager: InterstitialAdManager
-    private lateinit var nativeAdManager: NativeAdManager
-
-    override var isInitialized = false
-    override val onInitListeners = CopyOnWriteArrayList<Runnable>()
-    override var isPersonalized: Boolean = true
-        set(value) {
-            field = value
-            interstitialAdManager.isPersonalized = value
-            nativeAdManager.isPersonalized = value
-        }
+class MoPubAdManager : AbstractAdManager() {
 
     override fun init(context: Context, adAppId: String, adUnitId: String, volume: Float) {
         this.context = context
@@ -35,59 +20,8 @@ class MoPubAdManager : AdManager {
 
         interstitialAdManager = MoPubInterstitialAdManager()
         nativeAdManager = MoPubNativeAdManager(context)
+        rewardedAdManager = MoPubRewardedAdManager()
 
         onInitListeners.forEach { it.run() }
     }
-
-    override fun requestInterstitial(adUnitId: String) {
-        interstitialAdManager.request(adUnitId)
-    }
-
-    override fun requestInterstitial(activity: Activity, adUnitId: String) {
-        interstitialAdManager.request(activity, adUnitId)
-    }
-
-    override fun showInterstitial(adUnitId: String): Boolean {
-        return interstitialAdManager.show(adUnitId)
-    }
-
-    override fun destroyInterstitial(activity: Activity) {
-        interstitialAdManager.destroy(activity)
-    }
-
-    override fun registerInterstitialAdListener(interstitialAdListener: InterstitialAdListener) {
-        interstitialAdManager.registerListener(interstitialAdListener)
-    }
-
-    override fun unregisterInterstitialAdListener(interstitialAdListener: InterstitialAdListener) {
-        interstitialAdManager.unregisterListener(interstitialAdListener)
-    }
-
-    override fun isInterstitialLoading(adUnitId: String): Boolean {
-        return interstitialAdManager.isLoading(adUnitId)
-    }
-
-    override fun isInterstitialLoaded(adUnitId: String): Boolean {
-        return interstitialAdManager.isLoaded(adUnitId)
-    }
-
-    override fun registerNativeAdListener(nativeAdListener: NativeAdListener) {
-        nativeAdManager.listener += nativeAdListener
-    }
-
-    override fun unregisterNativeAdListener(nativeAdListener: NativeAdListener) {
-        nativeAdManager.listener -= nativeAdListener
-    }
-
-    override fun requestNative(adUnitId: String, nativeAdViewBinder: NativeAdViewBinder) {
-        nativeAdManager.request(adUnitId, nativeAdViewBinder)
-    }
-
-    override fun takeNativeAdView(adUnitId: String, nativeAdViewBinder: NativeAdViewBinder): View? {
-        return nativeAdManager.takeAdView(adUnitId, nativeAdViewBinder)
-    }
-
-    override fun isNativeLoading(adUnitId: String): Boolean {
-        return nativeAdManager.isLoading(adUnitId)
-    }
 }