forked from bidmachine/BidMachine-Android-Examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHeaderBiddingKotlinActivity.kt
244 lines (202 loc) · 9.33 KB
/
HeaderBiddingKotlinActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package io.bidmachine.examples
import android.os.Bundle
import android.view.View
import io.bidmachine.AdsFormat
import io.bidmachine.BidMachine
import io.bidmachine.ads.networks.AmazonConfig
import io.bidmachine.ads.networks.adcolony.AdColonyConfig
import io.bidmachine.ads.networks.criteo.CriteoConfig
import io.bidmachine.ads.networks.facebook.FacebookConfig
import io.bidmachine.ads.networks.mintegral.MintegralConfig
import io.bidmachine.ads.networks.my_target.MyTargetConfig
import io.bidmachine.ads.networks.tapjoy.TapjoyConfig
import io.bidmachine.banner.BannerRequest
import io.bidmachine.banner.BannerSize
import io.bidmachine.banner.BannerView
import io.bidmachine.banner.SimpleBannerListener
import io.bidmachine.examples.base.BaseKotlinExampleActivity
import io.bidmachine.interstitial.InterstitialAd
import io.bidmachine.interstitial.InterstitialRequest
import io.bidmachine.interstitial.SimpleInterstitialListener
import io.bidmachine.rewarded.RewardedAd
import io.bidmachine.rewarded.RewardedRequest
import io.bidmachine.rewarded.SimpleRewardedListener
import io.bidmachine.utils.BMError
import kotlinx.android.synthetic.main.activity_header_bidding.*
class HeaderBiddingKotlinActivity : BaseKotlinExampleActivity() {
private var bannerView: BannerView? = null
private var interstitialAd: InterstitialAd? = null
private var rewardedAd: RewardedAd? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//3rd party networks should be configure before SDK initialization
configureHeaderBiddingNetworks()
//Initialise SDK
BidMachine.initialize(this, "1")
//Enable logs
BidMachine.setLoggingEnabled(true)
//Set activity content view
setContentView(R.layout.activity_header_bidding)
//Set listeners to perform Ads load
btnShowBanner.setOnClickListener { showBanner() }
btnShowInterstitial.setOnClickListener { showInterstitial() }
btnShowRewarded.setOnClickListener { showRewarded() }
}
private fun configureHeaderBiddingNetworks() {
BidMachine.registerNetworks(
//Configure AdColony network
AdColonyConfig("app185a7e71e1714831a49ec7")
.withMediationConfig(AdsFormat.InterstitialVideo, "vz06e8c32a037749699e7050")
.withMediationConfig(AdsFormat.RewardedVideo, "vz1fd5a8b2bf6841a0a4b826"),
//Configure myTarget network
MyTargetConfig()
.withMediationConfig(AdsFormat.Banner, "437933")
.withMediationConfig(AdsFormat.Banner_320x50, "437933")
.withMediationConfig(AdsFormat.Banner_300x250, "64526")
.withMediationConfig(AdsFormat.Banner_728x90, "81620")
.withMediationConfig(AdsFormat.InterstitialStatic, "365991")
.withMediationConfig(AdsFormat.RewardedVideo, "482205"),
//Configure Tapjoy network
TapjoyConfig("tmyN5ZcXTMyjeJNJmUD5ggECAbnEGtJREmLDd0fvqKBXcIr7e1dvboNKZI4y")
.withMediationConfig(AdsFormat.InterstitialVideo, "video_without_cap_pb")
.withMediationConfig(AdsFormat.RewardedVideo, "rewarded_video_without_cap_pb"),
//Configure Facebook network
FacebookConfig("1525692904128549")
.withMediationConfig(AdsFormat.Banner, "1525692904128549_2386746951356469")
.withMediationConfig(AdsFormat.Banner_300x250, "1525692904128549_2386746951356469")
.withMediationConfig(AdsFormat.InterstitialStatic, "1525692904128549_2386743441356820")
.withMediationConfig(AdsFormat.RewardedVideo, "1525692904128549_2386753464689151"),
//Configure Mintegral network
MintegralConfig("117852", "936dcbdd57fe235fd7cf61c2e93da3c4")
.withMediationConfig(AdsFormat.InterstitialVideo, "140146")
.withMediationConfig(AdsFormat.RewardedVideo, "140144", "12817"),
//Configure Amazon network
AmazonConfig("a9_onboarding_app_id")
.withMediationConfig(AdsFormat.Banner_320x50, "5ab6a4ae-4aa5-43f4-9da4-e30755f2b295")
.withMediationConfig(AdsFormat.Banner_300x250, "54fb2d08-c222-40b1-8bbe-4879322dc04b")
.withMediationConfig(AdsFormat.Banner_728x90, "bed17ec3-b185-453e-b2a8-4a3c6bb9234d")
.withMediationConfig(AdsFormat.InterstitialStatic, "4e918ac0-5c68-4fe1-8d26-4e76e8f74831")
.withMediationConfig(AdsFormat.InterstitialVideo, "4acc26e6-3ada-4ee8-bae0-753c1e0ad278"),
//Configure Criteo
CriteoConfig("3703")
)
}
override fun onDestroy() {
super.onDestroy()
//Destroy Ads when you finish with it
if (bannerView != null) {
bannerView!!.destroy()
}
destroyCurrentInterstitialAd()
destroyCurrentRewardedAd()
}
private fun showBanner() {
if (bannerView == null) {
bannerView = findViewById<BannerView>(R.id.bannerView).apply {
//Set Banner Ads events listener
setListener(object : SimpleBannerListener() {
override fun onAdLoaded(ad: BannerView) {
setDebugState(Status.Loaded, "Banner Ads loaded")
//Make BannerView visible
ad.visibility = View.VISIBLE
}
override fun onAdLoadFailed(ad: BannerView, error: BMError) {
setDebugState(Status.LoadFail, "Banner Ads load failed")
}
})
}
}
if (bannerView == null) {
setDebugState(Status.LoadFail, "BannerView not found")
return
}
setDebugState(Status.Loading)
//Create banner request
val request = BannerRequest.Builder()
.setSize(BannerSize.Size_320x50)
.build()
//Load Banner Ads
bannerView!!.load(request)
}
private fun showInterstitial() {
setDebugState(Status.Loading)
//Destroy previous loaded Interstitial Ads
destroyCurrentInterstitialAd()
//Create new InterstitialRequest
val interstitialRequest = InterstitialRequest.Builder()
.build()
//Create new InterstitialAd
interstitialAd = InterstitialAd(this).apply {
//Set InterstitialAd events listener
setListener(object : SimpleInterstitialListener() {
override fun onAdLoaded(ad: InterstitialAd) {
super.onAdLoaded(ad)
setDebugState(Status.Loaded, "Interstitial Ads Loaded")
//Show Interstitial Ad
ad.show()
}
override fun onAdLoadFailed(ad: InterstitialAd, error: BMError) {
super.onAdLoadFailed(ad, error)
setDebugState(Status.LoadFail, "Interstitial Ads Load Failed")
//Destroy loaded ad since it not required any more
destroyCurrentInterstitialAd()
}
override fun onAdClosed(ad: InterstitialAd, finished: Boolean) {
super.onAdClosed(ad, finished)
setDebugState(Status.Closed, "Interstitial Ads Closed")
//Destroy loaded ad since it not required any more
destroyCurrentInterstitialAd()
}
})
//Load InterstitialAd
load(interstitialRequest)
}
}
private fun destroyCurrentInterstitialAd() {
interstitialAd?.apply {
destroy()
interstitialAd = null
}
}
private fun showRewarded() {
setDebugState(Status.Loading)
//Destroy previous loaded RewardedAd
destroyCurrentRewardedAd()
//Create new RewardedRequest
val rewardedRequest = RewardedRequest.Builder()
.build()
//Create new RewardedAd
rewardedAd = RewardedAd(this).apply {
//Set RewardedAd events listener
setListener(object : SimpleRewardedListener() {
override fun onAdLoaded(ad: RewardedAd) {
setDebugState(Status.Loaded, "Rewarded Ads Loaded")
//Show RewardedAd
ad.show()
}
override fun onAdLoadFailed(ad: RewardedAd, error: BMError) {
setDebugState(Status.LoadFail, "Rewarded Ads Load Failed")
//Destroy loaded ad since it not required any more
destroyCurrentRewardedAd()
}
override fun onAdClosed(ad: RewardedAd, finished: Boolean) {
setDebugState(Status.Closed, "Rewarded Ads Closed")
//Destroy loaded ad since it not required any more
destroyCurrentRewardedAd()
}
override fun onAdRewarded(ad: RewardedAd) {
setDebugState(Status.Rewarded, "Rewarded Ads Rewarded")
//Here you can start you reward process
}
})
//Load RewardedAd
load(rewardedRequest)
}
}
private fun destroyCurrentRewardedAd() {
rewardedAd?.apply {
destroy()
rewardedAd = null
}
}
}