Преглед изворни кода

Show "Ad" or "Sponsored" text on native ad

Wayne пре 4 година
родитељ
комит
f4b5572b50

+ 1 - 0
build.gradle

@@ -37,6 +37,7 @@ dependencies {
     api ('com.google.firebase:firebase-ads:18.2.0')
     api 'com.mopub.mediation:admob:17.1.2.1'
     implementation 'com.google.android.ads.consent:consent-library:1.0.6'
+    implementation 'com.google.ads.mediation:facebook:5.1.1.0'
     implementation 'com.mopub.mediation:facebookaudiencenetwork:5.0.0.0'
     implementation('com.mopub:mopub-sdk:5.3.0@aar') {
         transitive = true

+ 1 - 0
src/main/java/com/kdanmobile/admanager/NativeAdViewBinder.kt

@@ -12,4 +12,5 @@ data class NativeAdViewBinder(val layout: Int) {
     var textTextViewId = 0
     var ctaButtonId = 0
     var privacyInformationIcon = 0
+    var sponsoredTextView = 0
 }

+ 10 - 0
src/main/java/com/kdanmobile/admanager/admob/AdmobNativeAdManager.kt

@@ -6,6 +6,7 @@ import android.view.View
 import android.widget.Button
 import android.widget.ImageView
 import android.widget.TextView
+import com.google.ads.mediation.facebook.FacebookAdapter
 import com.google.android.gms.ads.AdListener
 import com.google.android.gms.ads.AdLoader
 import com.google.android.gms.ads.formats.MediaView
@@ -15,6 +16,7 @@ import com.google.android.gms.ads.formats.UnifiedNativeAdView
 import com.kdanmobile.admanager.NativeAdListener
 import com.kdanmobile.admanager.NativeAdManager
 import com.kdanmobile.admanager.NativeAdViewBinder
+import com.kdanmobile.admanager.R
 import java.util.concurrent.ConcurrentHashMap
 import java.util.concurrent.CopyOnWriteArrayList
 
@@ -109,6 +111,14 @@ class AdmobNativeAdManager(private val context: Context) : NativeAdManager {
                 ad.callToAction?.apply { it.text = this }
                 callToActionView = it
             }
+            findViewById<TextView>(nativeAdViewBinder.sponsoredTextView)?.let {
+                it.setText(when {
+                    ad.extras.containsKey(FacebookAdapter.KEY_SOCIAL_CONTEXT_ASSET) -> {
+                        R.string.advertisement_sponsored_text_fb
+                    }
+                    else -> R.string.advertisement_sponsored_text_admob
+                })
+            }
             post {
                 setNativeAd(ad)
             }

+ 2 - 0
src/main/res/values/strings.xml

@@ -1,3 +1,5 @@
 <resources>
     <string name="app_name">AdManager</string>
+    <string name="advertisement_sponsored_text_admob" translatable="false">Ad</string>
+    <string name="advertisement_sponsored_text_fb" translatable="false">Sponsored</string>
 </resources>