Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AdMob adapters #353

Merged
merged 6 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Example/PrebidDemoKotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ dependencies {
implementation project(':PrebidMobile')
implementation project(':PrebidMobile-gamEventHandlers')
implementation project(':PrebidMobile-mopubAdapters')
implementation project(':PrebidMobile-admobAdapters')

// For testing staging releases
// implementation 'org.prebid:prebid-mobile-sdk:1.13.0-beta2'
// implementation 'org.prebid:prebid-mobile-sdk-gam-event-handlers:1.13.0-beta2'
// implementation 'org.prebid:prebid-mobile-sdk-mopub-adapters:1.13.0-beta2'
// implementation 'org.prebid:prebid-mobile-sdk-admob-adapters:1.13.0-beta2'

// Standard libraries
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
Expand Down
5 changes: 5 additions & 0 deletions Example/PrebidDemoKotlin/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<meta-data
android:name="com.google.android.gms.ads.AD_MANAGER_APP"
android:value="true"/>

<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-1875909575462531~6255590079"/>

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import org.prebid.mobile.prebidkotlindemo.ads.GamInterstitial
import org.prebid.mobile.prebidkotlindemo.ads.MoPubBanner
import org.prebid.mobile.prebidkotlindemo.ads.MoPubInterstitial
import org.prebid.mobile.prebidkotlindemo.ads.inapp.*
import org.prebid.mobile.prebidkotlindemo.ads.inappadmob.InAppAdMobBanner
import org.prebid.mobile.prebidkotlindemo.ads.inappadmob.InAppAdMobInterstitial
import org.prebid.mobile.prebidkotlindemo.ads.inappadmob.InAppAdMobNative
import org.prebid.mobile.prebidkotlindemo.ads.inappadmob.InAppAdMobRewarded
import org.prebid.mobile.prebidkotlindemo.ads.inappgam.*
import org.prebid.mobile.prebidkotlindemo.ads.inappmopub.InAppMoPubBanner
import org.prebid.mobile.prebidkotlindemo.ads.inappmopub.InAppMoPubInterstitial
Expand Down Expand Up @@ -100,7 +104,7 @@ object AdTypesRepository {
"Banner 320x50",
onCreate = { _, wrapper, autoRefreshTime ->
InAppBanner.create(
wrapper, autoRefreshTime,
wrapper, autoRefreshTime / 1000,
320, 50,
"50699c03-0910-477c-b4a4-911dbe2b9d42"
)
Expand All @@ -122,7 +126,7 @@ object AdTypesRepository {
"Video Banner",
onCreate = { _, wrapper, autoRefreshTime ->
InAppVideoBanner.create(
wrapper, autoRefreshTime,
wrapper, autoRefreshTime / 1000,
300, 250,
"9007b76d-c73c-49c6-b0a8-1c7890a84b33"
)
Expand Down Expand Up @@ -156,7 +160,7 @@ object AdTypesRepository {
"Banner 320x50",
onCreate = { _, wrapper, autoRefreshTime ->
InAppGamBanner.create(
wrapper, autoRefreshTime,
wrapper, autoRefreshTime / 1000,
320, 50,
"/21808260008/prebid_oxb_320x50_banner",
"50699c03-0910-477c-b4a4-911dbe2b9d42"
Expand All @@ -180,7 +184,7 @@ object AdTypesRepository {
"Video Banner",
onCreate = { _, wrapper, autoRefreshTime ->
InAppGamVideoBanner.create(
wrapper, autoRefreshTime,
wrapper, autoRefreshTime / 1000,
300, 250,
"/21808260008/prebid_oxb_300x250_banner",
"9007b76d-c73c-49c6-b0a8-1c7890a84b33"
Expand Down Expand Up @@ -217,7 +221,7 @@ object AdTypesRepository {
"Banner 320x50",
onCreate = { _, wrapper, autoRefreshTime ->
InAppMoPubBanner.create(
wrapper, autoRefreshTime,
wrapper, autoRefreshTime / 1000,
320, 50,
"093eef131ec7455b9bda52b7eb456c51",
"50699c03-0910-477c-b4a4-911dbe2b9d42"
Expand Down Expand Up @@ -260,7 +264,69 @@ object AdTypesRepository {
},
onDestroy = { InAppMoPubRewardedInterstitial.destroy() }
)
),

"In-App + AdMob" to listOf(
AdType(
"Banner",
onCreate = { activity, wrapper, autoRefreshTime ->
InAppAdMobBanner.create(
activity, wrapper, autoRefreshTime,
320, 50,
"ca-app-pub-1875909575462531/3793078260",
"50699c03-0910-477c-b4a4-911dbe2b9d42"
)
},
onDestroy = { InAppAdMobBanner.destroy() }
),
AdType(
"Display Interstitial",
onCreate = { activity, _, _ ->
InAppAdMobInterstitial.create(
activity,
"ca-app-pub-1875909575462531/6393291067",
"5a4b8dcf-f984-4b04-9448-6529908d6cb6"
)
},
onDestroy = { InAppAdMobInterstitial.destroy() }
),
AdType(
"Video Interstitial",
onCreate = { activity, _, _ ->
InAppAdMobInterstitial.create(
activity,
"ca-app-pub-1875909575462531/6393291067",
"12f58bc2-b664-4672-8d19-638bcc96fd5c"
)
},
onDestroy = { InAppAdMobInterstitial.destroy() }
),
AdType(
"Rewarded",
onCreate = { activity, _, _ ->
InAppAdMobRewarded.create(
activity,
"ca-app-pub-1875909575462531/1908212572",
"9007b76d-c73c-49c6-b0a8-1c7890a84b33"
)
},
onDestroy = { InAppAdMobRewarded.destroy() }
),
AdType(
"Native",
onCreate = { _, wrapper, _ ->
InAppAdMobNative.create(
wrapper,
"ca-app-pub-1875909575462531/9720985924",
"51fe68ba-aff2-401e-9e15-f3ed89d5c036"
)
},
onDestroy = {
InAppAdMobNative.destroy()
}
)
)

)

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import android.util.Log
import android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
import android.webkit.WebView
import com.google.android.gms.ads.MobileAds
import com.google.android.gms.ads.RequestConfiguration
import com.mopub.common.MoPub
import com.mopub.common.SdkConfiguration
import com.mopub.common.SdkInitializationListener
import com.mopub.common.logging.MoPubLog
import org.prebid.mobile.Host
import org.prebid.mobile.PrebidMobile
Expand All @@ -39,6 +39,7 @@ class CustomApplication : Application() {
super.onCreate()
initMopubSDK()
initPrebidSDK()
initAdMob()
if (BuildConfig.DEBUG) {
activateKeepScreenOnFlag()
}
Expand Down Expand Up @@ -67,6 +68,16 @@ class CustomApplication : Application() {
PrebidRenderingSettings.setAccountId("0689a263-318d-448b-a3d4-b02e8a709d9d")
}

private fun initAdMob() {
MobileAds.initialize(this) { status ->
Log.d("MobileAds", "Initialization complete.")
}
val configuration = RequestConfiguration.Builder().setTestDeviceIds(
listOf("38250D98D8E3A07A2C03CD3552013B29")
).build()
MobileAds.setRequestConfiguration(configuration)
}

private fun activateKeepScreenOnFlag() {
sendBroadcast(Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
this.registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.prebid.mobile.prebidkotlindemo.ads.inappadmob

import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import com.google.android.gms.ads.*
import com.google.android.gms.ads.initialization.InitializationStatus
import org.prebid.mobile.admob.AdMobBannerMediationUtils
import org.prebid.mobile.admob.PrebidBannerAdapter
import org.prebid.mobile.rendering.bidding.display.MediationBannerAdUnit

object InAppAdMobBanner {

private const val TAG = "InAppAdMobBanner"

private var bannerView: AdView? = null
private var adUnit: MediationBannerAdUnit? = null

fun create(
activity: Activity,
wrapper: ViewGroup,
autoRefreshTime: Int,
width: Int,
height: Int,
adUnitId: String,
configId: String
) {
MobileAds.initialize(wrapper.context) { status ->
Log.d("MobileAds", "Initialization complete.")
logAdaptersInitializationStatus(status)
}

/** Google recommends put activity for mediation ad networks */
bannerView = AdView(activity)
bannerView?.adSize = AdSize.BANNER
bannerView?.adUnitId = adUnitId
bannerView?.adListener = object : AdListener() {
override fun onAdLoaded() {
Log.d("AdListener", "Won ad network: ${bannerView?.responseInfo?.mediationAdapterClassName}")
}

override fun onAdFailedToLoad(p0: LoadAdError) {
Log.d("AdListener", "Failed to load ad $p0")
}
}
wrapper.addView(bannerView)

val extras = Bundle()
val request = AdRequest
.Builder()
.addCustomEventExtrasBundle(PrebidBannerAdapter::class.java, extras)
.build()
val mediationUtils = AdMobBannerMediationUtils(extras, bannerView)

adUnit = MediationBannerAdUnit(
wrapper.context,
configId,
org.prebid.mobile.rendering.bidding.data.AdSize(width, height),
mediationUtils
)
adUnit?.setRefreshInterval(autoRefreshTime / 1000)
adUnit?.fetchDemand { result ->
Log.d("Prebid", "Fetch demand result: $result")
bannerView?.loadAd(request)
}
}

fun destroy() {
bannerView?.destroy()
bannerView = null

adUnit?.destroy()
adUnit = null
}

private fun logAdaptersInitializationStatus(status: InitializationStatus) {
val statusMap = status.adapterStatusMap
for (adapterClass in statusMap.keys) {
val adapterStatus = statusMap[adapterClass]
val adapterDescription = adapterStatus?.description ?: ""
if (adapterDescription.isNotBlank()) {
Log.e("MobileAds", "Mediation adapter: $adapterClass $adapterDescription")
} else {
Log.d("MobileAds", "Mediation adapter: $adapterClass")
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.prebid.mobile.prebidkotlindemo.ads.inappadmob

import android.app.Activity
import android.os.Bundle
import android.util.Log
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
import org.prebid.mobile.admob.AdMobInterstitialMediationUtils
import org.prebid.mobile.admob.PrebidInterstitialAdapter
import org.prebid.mobile.rendering.bidding.display.MediationInterstitialAdUnit
import org.prebid.mobile.rendering.bidding.enums.AdUnitFormat

object InAppAdMobInterstitial {

private var adUnit: MediationInterstitialAdUnit? = null
private var interstitialAd: InterstitialAd? = null

fun create(
activity: Activity,
adUnitId: String,
configId: String
) {
val extras = Bundle()
val request = AdRequest
.Builder()
.addCustomEventExtrasBundle(PrebidInterstitialAdapter::class.java, extras)
.build()
val mediationUtils = AdMobInterstitialMediationUtils(extras)

adUnit = MediationInterstitialAdUnit(
activity,
configId,
AdUnitFormat.DISPLAY,
mediationUtils
)
adUnit?.fetchDemand { result ->
Log.d("Prebid", "Fetch demand result: $result")

InterstitialAd.load(activity, adUnitId, request, object : InterstitialAdLoadCallback() {
override fun onAdLoaded(interstitial: InterstitialAd) {
interstitialAd = interstitial
interstitialAd?.show(activity)
}

override fun onAdFailedToLoad(error: LoadAdError) {
interstitialAd = null
}
})
}
}

fun destroy() {
adUnit?.destroy()
adUnit = null

interstitialAd = null
}

}
Loading