4 コミット e8a31f58d3 ... 64ab616e3c

作者 SHA1 メッセージ 日付
  GameJob 64ab616e3c Add RewardedAdManager codes & refactor: add AbstractAdManager 4 年 前
  GameJob 4223ee682a Add implementations of RewardedAdManager 4 年 前
  GameJob 47df0b0003 Add RewardedAdManager 4 年 前
  GameJob b4c9532650 Add RewardedAdListener & RewardedAdItem 4 年 前

+ 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)
 }

+ 9 - 0
src/main/java/com/kdanmobile/admanager/RewardedAdItem.kt

@@ -0,0 +1,9 @@
+package com.kdanmobile.admanager
+
+import com.google.android.gms.ads.reward.RewardItem
+
+data class RewardedAdItem(val type: String?, val amount: Int)
+
+fun RewardItem.toRewardedAdItem(): RewardedAdItem {
+    return RewardedAdItem(type, amount)
+}

+ 12 - 0
src/main/java/com/kdanmobile/admanager/RewardedAdListener.kt

@@ -0,0 +1,12 @@
+package com.kdanmobile.admanager
+
+interface RewardedAdListener {
+    fun onRewardedAdLoaded(adUnitId: String)
+    fun onRewardedAdOpened(adUnitId: String)
+    fun onRewardedAdStarted(adUnitId: String)
+    fun onRewardedAdClosed(adUnitId: String)
+    fun onRewardedAdCompleted(adUnitId: String)
+    fun onRewardedAdLeftApplication(adUnitId: String)
+    fun onRewardedAdFailedToLoad(adUnitId: String, errorCode: Int)
+    fun onRewarded(adUnitId: String, item: RewardedAdItem?)
+}

+ 22 - 0
src/main/java/com/kdanmobile/admanager/RewardedAdManager.kt

@@ -0,0 +1,22 @@
+package com.kdanmobile.admanager
+
+import android.app.Activity
+import android.content.Context
+
+interface RewardedAdManager {
+    var isPersonalized: Boolean
+    fun request(activity: Activity, adUnitId: String)
+    /**
+     * Use to re-request with same activity, must be called after request(activity, adUnitId)
+     */
+    fun request(adUnitId: String)
+    fun show(adUnitId: String): Boolean
+    fun registerListener(listener: RewardedAdListener)
+    fun unregisterListener(listener: RewardedAdListener)
+    fun isLoading(adUnitId: String): Boolean
+    fun isLoaded(adUnitId: String): Boolean
+
+    fun pauseAll(context: Context)
+    fun resumeAll(context: Context)
+    fun destroyAll(context: Context)
+}

+ 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()
+    }
+}

+ 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)
-    }
 }

+ 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
+    }
+}