forked from bidmachine/BidMachine-Android-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestsKotlinExample.kt
More file actions
465 lines (380 loc) · 16.5 KB
/
Copy pathRequestsKotlinExample.kt
File metadata and controls
465 lines (380 loc) · 16.5 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package io.bidmachine.examples
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import io.bidmachine.AdPlacementConfig
import io.bidmachine.BidMachine
import io.bidmachine.BannerAdSize
import io.bidmachine.banner.BannerRequest
import io.bidmachine.banner.BannerView
import io.bidmachine.banner.SimpleBannerListener
import io.bidmachine.examples.base.BaseKotlinExampleActivity
import io.bidmachine.examples.base.Status
import io.bidmachine.examples.databinding.ActivityRequestsBinding
import io.bidmachine.interstitial.InterstitialAd
import io.bidmachine.interstitial.InterstitialRequest
import io.bidmachine.interstitial.SimpleInterstitialListener
import io.bidmachine.models.AuctionResult
import io.bidmachine.MediaAssetType
import io.bidmachine.nativead.NativeAd
import io.bidmachine.nativead.NativeRequest
import io.bidmachine.nativead.SimpleNativeListener
import io.bidmachine.nativead.view.NativeAdContentLayout
import io.bidmachine.rewarded.RewardedAd
import io.bidmachine.rewarded.RewardedRequest
import io.bidmachine.rewarded.SimpleRewardedListener
import io.bidmachine.utils.BMError
class RequestsKotlinExample : BaseKotlinExampleActivity<ActivityRequestsBinding>() {
private var bannerView: BannerView? = null
private var bannerRequest: BannerRequest? = null
private var interstitialAd: InterstitialAd? = null
private var interstitialRequest: InterstitialRequest? = null
private var rewardedAd: RewardedAd? = null
private var rewardedRequest: RewardedRequest? = null
private var nativeAd: NativeAd? = null
private var nativeRequest: NativeRequest? = null
override fun inflate(inflater: LayoutInflater) = ActivityRequestsBinding.inflate(inflater)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Initialise SDK
BidMachine.initialize(this, SOURCE_ID)
// Enable logs
BidMachine.setLoggingEnabled(true)
// Set listener to perform Ads request
binding.bRequestBanner.setOnClickListener {
requestBanner()
}
// Set listener to perform Ads show
binding.bShowRequestedBanner.setOnClickListener {
showRequestedBanner()
}
// Set listener to perform Interstitial Ads request
binding.bRequestInterstitial.setOnClickListener {
requestInterstitial()
}
// Set listener to perform Interstitial Ads show
binding.bShowRequestedInterstitial.setOnClickListener {
showRequestedInterstitial()
}
// Set listener to perform Rewarded Ads request
binding.bRequestRewarded.setOnClickListener {
requestRewarded()
}
// Set listener to perform Rewarded Ads show
binding.bShowRequestedRewarded.setOnClickListener {
showRequestedRewarded()
}
// Set listener to perform Native Ads request
binding.bRequestNative.setOnClickListener {
requestNative()
}
// Set listener to perform Native Ads show
binding.bShowRequestedNative.setOnClickListener {
showRequestedNative()
}
}
override fun onDestroy() {
super.onDestroy()
// Destroy Ads when you finish with it
destroyCurrentBannerView()
destroyCurrentInterstitialAd()
destroyCurrentRewardedAd()
destroyCurrentNativeAd()
}
private fun showAdView(view: View) {
binding.adContainer.removeAllViews()
binding.adContainer.addView(view)
}
/**
* Make note, that AdRequest listeners will be notified in background thread
*/
private fun requestBanner() {
setDebugState(Status.Requesting)
// Create placement configuration
val config = AdPlacementConfig.bannerBuilder(BannerAdSize.Banner).build()
// Create new Banner Ads request
bannerRequest = BannerRequest.Builder(config)
// Set Banner Ads request listener
.setListener(object : BannerRequest.AdRequestListener {
override fun onRequestSuccess(request: BannerRequest, auctionResult: AuctionResult) {
runOnUiThread {
setDebugState(Status.Requested, "Banner Ad Requested")
}
}
override fun onRequestFailed(request: BannerRequest, error: BMError) {
runOnUiThread {
setDebugState(Status.RequestFail, "Banner Request Failed: ${error.message}")
}
}
override fun onRequestExpired(request: BannerRequest) {
runOnUiThread {
setDebugState(Status.Expired, "Banner Request Expired")
}
}
})
.build().apply {
// Perform Banner Ads request
request(this@RequestsKotlinExample)
}
}
private fun showRequestedBanner() {
when {
bannerRequest == null -> {
toast("Please request Banner first")
}
bannerRequest?.isExpired == true -> {
toast("BannerRequest expired, request new one please")
}
bannerRequest?.auctionResult == null -> {
toast("BannerRequest not requested or requested unsuccessfully")
}
else -> {
// Destroy previous BannerView object
destroyCurrentBannerView()
bannerView = BannerView(this).apply {
// Set listener ot change BannerView visibility when Ads loaded
setListener(object : SimpleBannerListener() {
override fun onAdLoaded(ad: BannerView) {
setDebugState(Status.Loaded, "Banner Ads Loaded")
// Show Banner Ad
showAdView(ad)
}
override fun onAdLoadFailed(ad: BannerView, error: BMError) {
setDebugState(Status.LoadFail, "Banner Ads load failed: ${error.message}")
// Destroy loaded ad since it not required any more
destroyCurrentBannerView()
}
})
// Perform BannerAd load
load(bannerRequest)
}
}
}
}
private fun destroyCurrentBannerView() {
bannerView?.destroy()
bannerView = null
}
private fun requestInterstitial() {
setDebugState(Status.Requesting)
// Create placement configuration
val config = AdPlacementConfig.interstitialBuilder().build()
// Create new Interstitial Ads request
interstitialRequest = InterstitialRequest.Builder(config)
// Set Interstitial Ads request listener
.setListener(object : InterstitialRequest.AdRequestListener {
override fun onRequestSuccess(request: InterstitialRequest, auctionResult: AuctionResult) {
runOnUiThread {
setDebugState(Status.Requested, "Interstitial Ad Requested")
}
}
override fun onRequestFailed(request: InterstitialRequest, bmError: BMError) {
runOnUiThread {
setDebugState(Status.RequestFail, "Interstitial Request Failed: ${bmError.message}")
}
}
override fun onRequestExpired(request: InterstitialRequest) {
runOnUiThread {
setDebugState(Status.Expired, "Interstitial Request Expired")
}
}
})
.build().apply {
// Perform Interstitial Ads request
request(this@RequestsKotlinExample)
}
}
private fun showRequestedInterstitial() {
when {
interstitialRequest == null -> {
toast("Please request Interstitial first")
}
interstitialRequest?.isExpired == true -> {
toast("InterstitialRequest expired, request new one please")
}
interstitialRequest?.auctionResult == null -> {
toast("InterstitialRequest not requested or requested unsuccessfully")
}
else -> {
// Destroy previous InterstitialAd object
destroyCurrentInterstitialAd()
// Create new InterstitialAd
interstitialAd = InterstitialAd(this).apply {
setListener(object : SimpleInterstitialListener() {
override fun onAdLoaded(ad: InterstitialAd) {
setDebugState(Status.Loaded, "Interstitial Ads Loaded")
// Show Interstitial Ads
ad.show()
}
override fun onAdLoadFailed(ad: InterstitialAd, error: BMError) {
setDebugState(Status.LoadFail, "Interstitial Ads load failed: ${error.message}")
// Destroy current Interstitial ad since we don't need it anymore
destroyCurrentInterstitialAd()
}
override fun onAdClosed(ad: InterstitialAd, finished: Boolean) {
setDebugState(Status.Closed)
// Destroy current Interstitial ad since we don't need it anymore
destroyCurrentInterstitialAd()
}
})
// Perform InterstitialAd load
load(interstitialRequest)
}
}
}
}
private fun destroyCurrentInterstitialAd() {
interstitialAd?.destroy()
interstitialAd = null
}
private fun requestRewarded() {
setDebugState(Status.Requesting)
// Create placement configuration
val config = AdPlacementConfig.rewardedBuilder().build()
// Create new Rewarded Ads request
rewardedRequest = RewardedRequest.Builder(config)
// Set Rewarded Ads request listener
.setListener(object : RewardedRequest.AdRequestListener {
override fun onRequestSuccess(request: RewardedRequest, auctionResult: AuctionResult) {
runOnUiThread {
setDebugState(Status.Requested, "Rewarded Ad Requested")
}
}
override fun onRequestFailed(request: RewardedRequest, bmError: BMError) {
runOnUiThread {
setDebugState(Status.RequestFail, "Rewarded Request Failed: ${bmError.message}")
}
}
override fun onRequestExpired(request: RewardedRequest) {
runOnUiThread {
setDebugState(Status.Expired, "Rewarded Request Expired")
}
}
})
.build().apply {
// Perform Rewarded Ads request
request(this@RequestsKotlinExample)
}
}
private fun showRequestedRewarded() {
when {
rewardedRequest == null -> {
toast("Please request Rewarded first")
}
rewardedRequest?.isExpired == true -> {
toast("RewardedRequest expired, request new one please")
}
rewardedRequest?.auctionResult == null -> {
toast("RewardedRequest not requested or requested unsuccessfully")
}
else -> {
// Destroy previous RewardedAd object
destroyCurrentRewardedAd()
// Create new RewardedAd
rewardedAd = RewardedAd(this).apply {
setListener(object : SimpleRewardedListener() {
override fun onAdLoaded(ad: RewardedAd) {
setDebugState(Status.Loaded, "Rewarded Ads Loaded")
// Show Rewarded Ads
ad.show()
}
override fun onAdLoadFailed(ad: RewardedAd, error: BMError) {
setDebugState(Status.LoadFail, "Rewarded Ads load failed: ${error.message}")
// Destroy current Rewarded ad since we don't need it anymore
destroyCurrentRewardedAd()
}
override fun onAdClosed(ad: RewardedAd, finished: Boolean) {
setDebugState(Status.Closed)
// Destroy current Rewarded ad since we don't need it anymore
destroyCurrentRewardedAd()
}
})
// Perform RewardedAd load
load(rewardedRequest)
}
}
}
}
private fun destroyCurrentRewardedAd() {
rewardedAd?.destroy()
rewardedAd = null
}
private fun requestNative() {
setDebugState(Status.Requesting)
// Create media types list
val mediaTypes = listOf(MediaAssetType.Icon, MediaAssetType.Image)
// Create placement configuration
val config = AdPlacementConfig.nativeBuilder(mediaTypes).build()
// Create new Native Ads request
nativeRequest = NativeRequest.Builder(config)
// Set Native Ads request listener
.setListener(object : NativeRequest.AdRequestListener {
override fun onRequestSuccess(request: NativeRequest, auctionResult: AuctionResult) {
runOnUiThread {
setDebugState(Status.Requested, "Native Ad Requested")
}
}
override fun onRequestFailed(request: NativeRequest, bmError: BMError) {
runOnUiThread {
setDebugState(Status.RequestFail, "Native Request Failed: ${bmError.message}")
}
}
override fun onRequestExpired(request: NativeRequest) {
runOnUiThread {
setDebugState(Status.Expired, "Native Request Expired")
}
}
})
.build().apply {
// Perform Native Ads request
request(this@RequestsKotlinExample)
}
}
private fun showRequestedNative() {
when {
nativeRequest == null -> {
toast("Please request Native first")
}
nativeRequest?.isExpired == true -> {
toast("NativeRequest expired, request new one please")
}
nativeRequest?.auctionResult == null -> {
toast("NativeRequest not requested or requested unsuccessfully")
}
else -> {
// Destroy previous NativeAd object
destroyCurrentNativeAd()
// Create new NativeAd
nativeAd = NativeAd(this).apply {
setListener(object : SimpleNativeListener() {
override fun onAdLoaded(ad: NativeAd) {
setDebugState(Status.Loaded, "Native Ads Loaded")
// Show native Ads
val nativeView = createNativeAdView(ad)
showAdView(nativeView)
}
override fun onAdLoadFailed(ad: NativeAd, error: BMError) {
setDebugState(Status.LoadFail, "Native Ads load failed: ${error.message}")
// Destroy current Native ad since we don't need it anymore
destroyCurrentNativeAd()
}
})
// Perform NativeAd load
load(nativeRequest)
}
}
}
}
private fun createNativeAdView(nativeAd: NativeAd): View {
val nativeView = LayoutInflater.from(this)
.inflate(R.layout.native_ad, binding.adContainer, false) as NativeAdContentLayout
nativeView.bind(nativeAd)
nativeView.registerViewForInteraction(nativeAd)
nativeView.visibility = View.VISIBLE
return nativeView
}
private fun destroyCurrentNativeAd() {
nativeAd?.destroy()
nativeAd = null
}
}