Browse Source

Merge branch 'rewardedAd'

Wayne 4 years ago
parent
commit
9cc7a1716d

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

+ 15 - 1
src/main/java/com/kdanmobile/admanager/AdManager.kt

@@ -26,5 +26,19 @@ interface AdManager {
     fun unregisterNativeAdListener(nativeAdListener: NativeAdListener)
     fun requestNative(adUnitId: String, nativeAdViewBinder: NativeAdViewBinder)
     fun takeNativeAdView(adUnitId: String, nativeAdViewBinder: NativeAdViewBinder): View?
-    fun isNatvieLoading(adUnitId: String): Boolean
+    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)
+}

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

@@ -0,0 +1,160 @@
+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 {
+
+    companion object {
+        private const val MAX_KEEPING_AD_DURATION = 60 * 60 * 1000L
+    }
+
+    override var isPersonalized: Boolean = false
+    private val adListeners = CopyOnWriteArrayList<RewardedAdListener>()
+    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,
+        LOADING,
+        LOADED,
+        FAILED
+    }
+
+    override fun request(activity: Activity, adUnitId: String) {
+        if (rewardedAdDataMap[adUnitId] == null) {
+            val status = RewardedAdStatus.NOT_INIT
+            val ad = 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() {
+                        rewardedAdDataMap[adUnitId]?.status = RewardedAdStatus.LOADED
+                        adListeners.forEach {
+                            it.onRewardedAdLoaded(adUnitId)
+                        }
+                    }
+
+                    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) {
+                        rewardedAdDataMap[adUnitId]?.status = RewardedAdStatus.FAILED
+                        adListeners.forEach {
+                            it.onRewardedAdFailedToLoad(adUnitId, errorCode)
+                        }
+                    }
+                }
+            }
+            rewardedAdDataMap[adUnitId] = RewardedAdData(ad, status)
+        }
+        request(adUnitId)
+    }
+
+    override fun request(adUnitId: String) {
+        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 rewardedAdDataMap[adUnitId]?.let {
+            it.ad.show()
+            it.status = RewardedAdStatus.NOT_INIT
+            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 rewardedAdDataMap[adUnitId]?.status == RewardedAdStatus.LOADING
+    }
+
+    override fun isLoaded(adUnitId: String): Boolean {
+        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 rewardedAdDataMap.values.map { it.ad }) {
+            rewardedAd.pause(context)
+        }
+    }
+
+    override fun resumeAll(context: Context) {
+        for (rewardedAd in rewardedAdDataMap.values.map { it.ad }) {
+            rewardedAd.resume(context)
+        }
+    }
+
+    override fun destroyAll(context: Context) {
+        for (rewardedAd in rewardedAdDataMap.values.map { it.ad }) {
+            rewardedAd.destroy(context)
+        }
+        rewardedAdDataMap.clear()
+    }
+}

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

@@ -1,23 +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) {
-            nativeAdManager?.isPersonalized = value
-            interstitialAdManager?.isPersonalized = value
-        }
+class AdmobAdManager : AbstractAdManager() {
 
     override fun init(context: Context, adAppId: String, adUnitId: String, volume: Float) {
         this.context = context
@@ -27,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 isNatvieLoading(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 isNatvieLoading(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
+    }
+}