emi-indo-cordova-plugin-admob 1.4.7 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1843 @@
1
+ package emi.indo.cordova.plugin.admob
2
+ import android.R
3
+ import android.annotation.SuppressLint
4
+ import android.app.Activity
5
+ import android.content.Context
6
+ import android.content.Intent
7
+ import android.content.SharedPreferences
8
+ import android.content.res.Configuration
9
+ import android.os.Bundle
10
+ import android.provider.Settings
11
+ import android.util.DisplayMetrics
12
+ import android.util.Log
13
+ import android.view.View
14
+ import android.view.ViewGroup
15
+ import android.widget.RelativeLayout
16
+ import androidx.preference.PreferenceManager
17
+ import com.google.ads.mediation.admob.AdMobAdapter
18
+ import com.google.android.gms.ads.AdError
19
+ import com.google.android.gms.ads.AdListener
20
+ import com.google.android.gms.ads.AdRequest
21
+ import com.google.android.gms.ads.AdSize
22
+ import com.google.android.gms.ads.AdValue
23
+ import com.google.android.gms.ads.AdView
24
+ import com.google.android.gms.ads.FullScreenContentCallback
25
+ import com.google.android.gms.ads.LoadAdError
26
+ import com.google.android.gms.ads.MobileAds
27
+ import com.google.android.gms.ads.OnPaidEventListener
28
+ import com.google.android.gms.ads.RequestConfiguration
29
+ import com.google.android.gms.ads.RequestConfiguration.PublisherPrivacyPersonalizationState
30
+ import com.google.android.gms.ads.admanager.AdManagerAdRequest
31
+ import com.google.android.gms.ads.appopen.AppOpenAd
32
+ import com.google.android.gms.ads.appopen.AppOpenAd.AppOpenAdLoadCallback
33
+ import com.google.android.gms.ads.initialization.InitializationStatus
34
+ import com.google.android.gms.ads.interstitial.InterstitialAd
35
+ import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
36
+ import com.google.android.gms.ads.rewarded.RewardItem
37
+ import com.google.android.gms.ads.rewarded.RewardedAd
38
+ import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback
39
+ import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd
40
+ import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback
41
+ import com.google.android.ump.ConsentDebugSettings
42
+ import com.google.android.ump.ConsentForm
43
+ import com.google.android.ump.ConsentInformation
44
+ import com.google.android.ump.ConsentRequestParameters
45
+ import com.google.android.ump.FormError
46
+ import com.google.android.ump.UserMessagingPlatform
47
+ import org.apache.cordova.CallbackContext
48
+ import org.apache.cordova.CordovaInterface
49
+ import org.apache.cordova.CordovaPlugin
50
+ import org.apache.cordova.CordovaWebView
51
+ import org.json.JSONArray
52
+ import org.json.JSONException
53
+ import org.json.JSONObject
54
+ import java.security.MessageDigest
55
+ import java.security.NoSuchAlgorithmException
56
+ import java.util.Locale
57
+ import java.util.concurrent.atomic.AtomicBoolean
58
+
59
+ /**
60
+ * Created by EMI INDO So on Apr 2, 2023
61
+ */
62
+ class emiAdmobPlugin : CordovaPlugin() {
63
+ private var PUBLIC_CALLBACKS: CallbackContext? = null
64
+
65
+ private var rewardedAd: RewardedAd? = null
66
+ private var rewardedInterstitialAd: RewardedInterstitialAd? = null
67
+ private var cWebView: CordovaWebView? = null
68
+ private var isAppOpenAdShow = false
69
+ private var isInterstitialLoad = false
70
+ private var isRewardedInterstitialLoad = false
71
+ private var isRewardedLoad = false
72
+ private var isBannerPause = 0
73
+ private var Position: String? = null
74
+ private var Size: String? = null
75
+
76
+ private var paddingInPx = 0
77
+ private var marginsInPx = 0
78
+ var ResponseInfo: Boolean = false
79
+ private var isAdSkip = 0
80
+ private var SetTagForChildDirectedTreatment: Boolean = false
81
+ private var SetTagForUnderAgeOfConsent: Boolean = false
82
+ private var SetMaxAdContentRating: String = "G"
83
+ private var bannerAdUnitId: String? = null
84
+
85
+ private var consentInformation: ConsentInformation? = null
86
+ private var bannerViewLayout: RelativeLayout? = null
87
+ private var bannerView: AdView? = null
88
+ var isBannerLoad: Boolean = false
89
+ var isBannerShow: Boolean = false
90
+
91
+ var isBannerShows: Boolean = true
92
+ private var bannerAutoShow = false
93
+ private var isAutoResize: Boolean = false
94
+ var appOpenAutoShow: Boolean = false
95
+ var intAutoShow: Boolean = false
96
+ var rewardedAutoShow: Boolean = false
97
+ var rIntAutoShow: Boolean = false
98
+ private var isCollapsible: Boolean = false
99
+ var lock: Boolean = true
100
+ private var setDebugGeography: Boolean = false
101
+ private var mActivity: Activity? = null
102
+ private var mContext: Context? = null
103
+ private var mInterstitialAd: InterstitialAd? = null
104
+ private var appOpenAd: AppOpenAd? = null
105
+ private var isOrientation: Int = 1
106
+
107
+ private var mPreferences: SharedPreferences? = null
108
+ var mBundleExtra: Bundle? = null
109
+ private var collapsiblePos: String? = null
110
+
111
+ // only isUsingAdManagerRequest = true
112
+ private var customTargetingEnabled: Boolean = false
113
+ private var customTargetingList: MutableList<String>? = null
114
+ private var categoryExclusionsEnabled: Boolean = false
115
+ private var cExclusionsValue: String = ""
116
+ private var ppIdEnabled: Boolean = false
117
+ private var ppIdVl: String = ""
118
+ private var ppsEnabled: Boolean = false
119
+ private var ppsVl: String = ""
120
+ private var ppsArrayList: MutableList<Int>? = null
121
+ private var contentURLEnabled: Boolean = false
122
+ private var cURLVl: String = ""
123
+ private var brandSafetyEnabled: Boolean = false
124
+ private var brandSafetyUrls: MutableList<String>? = null
125
+
126
+
127
+ private val isMobileAdsInitializeCalled = AtomicBoolean(false)
128
+
129
+ private var isUsingAdManagerRequest = false
130
+
131
+
132
+ override fun initialize(cordova: CordovaInterface, webView: CordovaWebView) {
133
+ super.initialize(cordova, webView)
134
+ cWebView = webView
135
+ mActivity = this.cordova.activity
136
+ mActivity?.let {
137
+ mContext = it.applicationContext
138
+ }
139
+
140
+ mPreferences = mContext?.let { PreferenceManager.getDefaultSharedPreferences(it) }
141
+ }
142
+
143
+ override fun onConfigurationChanged(newConfig: Configuration) {
144
+ super.onConfigurationChanged(newConfig)
145
+ val orientation = newConfig.orientation
146
+ if (orientation != isOrientation) {
147
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.screen.rotated');")
148
+ isOrientation = orientation
149
+ if (this.isAutoResize) {
150
+ mActivity!!.runOnUiThread {
151
+ try {
152
+ if (bannerViewLayout != null && bannerView != null) {
153
+ val parentView = bannerViewLayout!!.parent as ViewGroup
154
+ parentView?.removeView(bannerViewLayout)
155
+ bannerViewLayout = RelativeLayout(mActivity)
156
+ val params = RelativeLayout.LayoutParams(
157
+ RelativeLayout.LayoutParams.MATCH_PARENT,
158
+ RelativeLayout.LayoutParams.WRAP_CONTENT
159
+ )
160
+ val rootView =
161
+ mActivity!!.window.decorView.findViewById<View>(R.id.content)
162
+ if (rootView is ViewGroup) {
163
+ rootView.addView(bannerViewLayout, params)
164
+ } else {
165
+ mActivity!!.addContentView(bannerViewLayout, params)
166
+ }
167
+ bannerView = AdView(mContext!!)
168
+ setBannerPosition(this.Position)
169
+ setBannerSiz(this.Size)
170
+ bannerView!!.adUnitId = bannerAdUnitId!!
171
+ bannerView!!.adListener = bannerAdListener
172
+ bannerView!!.loadAd(buildAdRequest())
173
+ bannerViewLayout!!.addView(bannerView)
174
+ bannerViewLayout!!.bringToFront()
175
+ }
176
+ } catch (e: Exception) {
177
+ PUBLIC_CALLBACKS!!.error("Error adjusting banner size: " + e.message)
178
+ }
179
+ }
180
+ }
181
+
182
+ if (orientation == Configuration.ORIENTATION_PORTRAIT) {
183
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.orientation.portrait');")
184
+ } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
185
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.orientation.landscape');")
186
+ } else if (orientation == Configuration.ORIENTATION_UNDEFINED) {
187
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.orientation.undefined');")
188
+ }
189
+ }
190
+ }
191
+
192
+
193
+ @Throws(JSONException::class)
194
+ override fun execute(
195
+ action: String,
196
+ args: JSONArray,
197
+ callbackContext: CallbackContext
198
+ ): Boolean {
199
+ PUBLIC_CALLBACKS = callbackContext
200
+
201
+ if (action == "initialize") {
202
+ val options = args.getJSONObject(0)
203
+ mActivity!!.runOnUiThread {
204
+ val setAdRequest = options.optBoolean("isUsingAdManagerRequest")
205
+ val responseInfo = options.optBoolean("isResponseInfo")
206
+ val setDebugGeography = options.optBoolean("isConsentDebug")
207
+ setUsingAdManagerRequest(setAdRequest)
208
+ this.ResponseInfo = responseInfo
209
+ this.setDebugGeography = setDebugGeography
210
+ val params: ConsentRequestParameters
211
+ if (this.setDebugGeography) {
212
+ val debugSettings = mActivity?.let {
213
+ deviceId?.let { it1 ->
214
+ ConsentDebugSettings.Builder(it)
215
+ .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
216
+ .addTestDeviceHashedId(it1).build()
217
+ }
218
+ }
219
+ params = ConsentRequestParameters.Builder()
220
+ .setConsentDebugSettings(debugSettings).build()
221
+ } else {
222
+ params = ConsentRequestParameters.Builder()
223
+ .setTagForUnderAgeOfConsent(this.SetTagForUnderAgeOfConsent).build()
224
+ }
225
+
226
+ consentInformation = mContext?.let { UserMessagingPlatform.getConsentInformation(it) }
227
+ mActivity?.let {
228
+ consentInformation?.requestConsentInfoUpdate(
229
+ it,
230
+ params,
231
+ {
232
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.consent.info.update');")
233
+ when (consentInformation!!.getConsentStatus()) {
234
+ ConsentInformation.ConsentStatus.NOT_REQUIRED -> cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.consent.status.not_required');")
235
+ ConsentInformation.ConsentStatus.OBTAINED -> cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.consent.status.obtained');")
236
+ ConsentInformation.ConsentStatus.REQUIRED -> {
237
+ handleConsentForm()
238
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.consent.status.required');")
239
+ }
240
+
241
+ ConsentInformation.ConsentStatus.UNKNOWN -> cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.consent.status.unknown');")
242
+ }
243
+ },
244
+ { formError: FormError ->
245
+ if (consentInformation!!.canRequestAds()) {
246
+ initializeMobileAdsSdk()
247
+ }
248
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.consent.info.update.failed', { message: '" + formError.message + "' });")
249
+ })
250
+ }
251
+ if (consentInformation?.canRequestAds()!!) {
252
+ initializeMobileAdsSdk()
253
+ }
254
+ }
255
+
256
+ return true
257
+ } else if (action == "targeting") {
258
+ val options = args.getJSONObject(0)
259
+ mActivity!!.runOnUiThread {
260
+ try {
261
+ val childDirectedTreatment = options.optBoolean("childDirectedTreatment")
262
+ val underAgeOfConsent = options.optBoolean("underAgeOfConsent")
263
+ val contentRating = options.optString("contentRating")
264
+ this.SetTagForChildDirectedTreatment = childDirectedTreatment
265
+ this.SetTagForUnderAgeOfConsent = underAgeOfConsent
266
+ this.SetMaxAdContentRating = contentRating
267
+ _Targeting(childDirectedTreatment, underAgeOfConsent, contentRating)
268
+ // callbackContext.success();
269
+ } catch (e: Exception) {
270
+ callbackContext.error("Error: " + e.message)
271
+ }
272
+ }
273
+ return true
274
+ } else if (action == "targetingAdRequest") {
275
+ val options = args.getJSONObject(0)
276
+ mActivity!!.runOnUiThread {
277
+ val customTargetingEnabled = options.optBoolean("customTargetingEnabled")
278
+ val categoryExclusionsEnabled = options.optBoolean("categoryExclusionsEnabled")
279
+ val ppIdEnabled = options.optBoolean("ppIdEnabled")
280
+ val contentURLEnabled = options.optBoolean("contentURLEnabled")
281
+ val brandSafetyEnabled = options.optBoolean("brandSafetyEnabled")
282
+
283
+ val customTargeting = options.optJSONArray("customTargetingValue")
284
+ val categoryExclusions = options.optString("categoryExclusionsValue")
285
+ val ppId = options.optString("ppIdValue")
286
+ val ctURL = options.optString("contentURLValue")
287
+ val brandSafetyArr = options.optJSONArray("brandSafetyArr")
288
+ try {
289
+ this.customTargetingEnabled = customTargetingEnabled
290
+ this.categoryExclusionsEnabled = categoryExclusionsEnabled
291
+ this.ppIdEnabled = ppIdEnabled
292
+ this.contentURLEnabled = contentURLEnabled
293
+ this.brandSafetyEnabled = brandSafetyEnabled
294
+ this.cExclusionsValue = categoryExclusions
295
+ this.ppIdVl = ppId
296
+ this.cURLVl = ctURL
297
+ targetingAdRequest(
298
+ customTargeting,
299
+ categoryExclusions,
300
+ ppId,
301
+ ctURL,
302
+ brandSafetyArr
303
+ )
304
+ callbackContext.success()
305
+ } catch (e: Exception) {
306
+ callbackContext.error(e.toString())
307
+ }
308
+ }
309
+ return true
310
+ } else if (action == "setPersonalizationState") {
311
+ val options = args.getJSONObject(0)
312
+ mActivity!!.runOnUiThread {
313
+ val setPPT = options.optString("setPersonalizationState")
314
+ try {
315
+ setPersonalizationState(setPPT)
316
+ callbackContext.success()
317
+ } catch (e: Exception) {
318
+ callbackContext.error(e.toString())
319
+ }
320
+ }
321
+ return true
322
+ } else if (action == "setPPS") {
323
+ val options = args.getJSONObject(0)
324
+ mActivity!!.runOnUiThread {
325
+ val ppsEnabled = options.optBoolean("ppsEnabled")
326
+ val iabContent = options.optString("iabContent")
327
+ val ppsArrValue = options.optJSONArray("ppsArrValue")
328
+ try {
329
+ this.ppsEnabled = ppsEnabled
330
+ this.ppsVl = iabContent
331
+ setPublisherProvidedSignals(ppsArrValue)
332
+ callbackContext.success()
333
+ } catch (e: Exception) {
334
+ callbackContext.error(e.toString())
335
+ }
336
+ }
337
+ return true
338
+ } else if (action == "globalSettings") {
339
+ val options = args.getJSONObject(0)
340
+ mActivity!!.runOnUiThread {
341
+ val setAppMuted = options.optBoolean("setAppMuted")
342
+ val setAppVolume = options.optInt("setAppVolume").toFloat()
343
+ val pubIdEnabled = options.optBoolean("pubIdEnabled")
344
+ try {
345
+ _globalSettings(setAppMuted, setAppVolume, pubIdEnabled)
346
+ // callbackContext.success();
347
+ } catch (e: Exception) {
348
+ callbackContext.error(e.toString())
349
+ }
350
+ }
351
+ return true
352
+ } else if (action == "loadAppOpenAd") {
353
+ val options = args.getJSONObject(0)
354
+ mActivity!!.runOnUiThread {
355
+ val adUnitId = options.optString("adUnitId")
356
+ val autoShow = options.optBoolean("autoShow")
357
+ try {
358
+ this.appOpenAutoShow = autoShow
359
+ AppOpenAd.load(
360
+ mActivity!!, adUnitId, buildAdRequest(),
361
+ object : AppOpenAdLoadCallback() {
362
+ override fun onAdLoaded(ad: AppOpenAd) {
363
+ appOpenAd = ad
364
+ isAppOpenAdShow = true
365
+
366
+ if (appOpenAutoShow) {
367
+ OpenAutoShow()
368
+ }
369
+
370
+ _appOpenAdLoadCallback(callbackContext)
371
+
372
+ appOpenAd!!.onPaidEventListener =
373
+ OnPaidEventListener { adValue: AdValue ->
374
+ val valueMicros = adValue.valueMicros
375
+ val currencyCode = adValue.currencyCode
376
+ val precision = adValue.precisionType
377
+ val appOpenAdAdUnitId = appOpenAd!!.adUnitId
378
+ val result = JSONObject()
379
+ try {
380
+ result.put("micros", valueMicros)
381
+ result.put("currency", currencyCode)
382
+ result.put("precision", precision)
383
+ result.put("adUnitId", appOpenAdAdUnitId)
384
+ callbackContext.success(result)
385
+ cWebView!!.loadUrl(
386
+ "javascript:cordova.fireDocumentEvent('on.appOpenAd.revenue');"
387
+ )
388
+ } catch (e: JSONException) {
389
+ callbackContext.error(e.message)
390
+ }
391
+ }
392
+ cWebView!!.loadUrl(
393
+ "javascript:cordova.fireDocumentEvent('on.appOpenAd.loaded');"
394
+ )
395
+ if (ResponseInfo) {
396
+ val result = JSONObject()
397
+ val responseInfo = ad.responseInfo
398
+ try {
399
+ result.put("getResponseId", responseInfo.responseId)
400
+ result.put(
401
+ "getAdapterResponses",
402
+ responseInfo.adapterResponses
403
+ )
404
+ result.put("getResponseExtras", responseInfo.responseExtras)
405
+ result.put(
406
+ "getMediationAdapterClassName",
407
+ responseInfo.mediationAdapterClassName
408
+ )
409
+ result.put("getBundleExtra", mBundleExtra.toString())
410
+ callbackContext.success(result)
411
+ } catch (e: JSONException) {
412
+ callbackContext.error(e.message)
413
+ }
414
+ }
415
+ }
416
+
417
+ private fun OpenAutoShow() {
418
+ try {
419
+ if (isAppOpenAdShow && appOpenAd != null) {
420
+ mActivity!!.runOnUiThread { appOpenAd!!.show(mActivity!!) }
421
+ }
422
+ } catch (e: Exception) {
423
+ PUBLIC_CALLBACKS!!.error(e.toString())
424
+ }
425
+ }
426
+
427
+ override fun onAdFailedToLoad(loadAdError: LoadAdError) {
428
+ isAppOpenAdShow = false
429
+ callbackContext.error(loadAdError.toString())
430
+ cWebView!!.loadUrl(
431
+ "javascript:cordova.fireDocumentEvent('on.appOpenAd.failed.loaded');"
432
+ )
433
+ }
434
+ })
435
+ } catch (e: Exception) {
436
+ callbackContext.error(e.toString())
437
+ }
438
+ }
439
+ return true
440
+ } else if (action == "showAppOpenAd") {
441
+ try {
442
+ if (isAppOpenAdShow && appOpenAd != null) {
443
+ mActivity!!.runOnUiThread { appOpenAd!!.show(mActivity!!) }
444
+ _appOpenAdLoadCallback(callbackContext)
445
+ } else {
446
+ callbackContext.error("The App Open Ad wasn't ready yet")
447
+ }
448
+ } catch (e: Exception) {
449
+ PUBLIC_CALLBACKS!!.error(e.toString())
450
+ }
451
+
452
+ return true
453
+ } else if (action == "loadInterstitialAd") {
454
+ val options = args.getJSONObject(0)
455
+ mActivity!!.runOnUiThread {
456
+ val adUnitId = options.optString("adUnitId")
457
+ val autoShow = options.optBoolean("autoShow")
458
+ try {
459
+ this.intAutoShow = autoShow
460
+ InterstitialAd.load(
461
+ mActivity!!, adUnitId, buildAdRequest(),
462
+ object : InterstitialAdLoadCallback() {
463
+ override fun onAdLoaded(interstitialAd: InterstitialAd) {
464
+ isInterstitialLoad = true
465
+ mInterstitialAd = interstitialAd
466
+
467
+ if (intAutoShow) {
468
+ isIntAutoShow
469
+ }
470
+
471
+ _interstitialAdLoadCallback(callbackContext)
472
+
473
+ cWebView!!.loadUrl(
474
+ "javascript:cordova.fireDocumentEvent('on.interstitial.loaded');"
475
+ )
476
+ if (ResponseInfo) {
477
+ val result = JSONObject()
478
+ val responseInfo = mInterstitialAd!!.responseInfo
479
+ try {
480
+ result.put("getResponseId", responseInfo.responseId)
481
+ result.put(
482
+ "getAdapterResponses",
483
+ responseInfo.adapterResponses
484
+ )
485
+ result.put("getResponseExtras", responseInfo.responseExtras)
486
+ result.put(
487
+ "getMediationAdapterClassName",
488
+ responseInfo.mediationAdapterClassName
489
+ )
490
+ result.put("getBundleExtra", mBundleExtra.toString())
491
+ callbackContext.success(result)
492
+ } catch (e: JSONException) {
493
+ callbackContext.error(e.message)
494
+ }
495
+ }
496
+ mInterstitialAd!!.onPaidEventListener =
497
+ OnPaidEventListener { adValue: AdValue ->
498
+ val valueMicros = adValue.valueMicros
499
+ val currencyCode = adValue.currencyCode
500
+ val precision = adValue.precisionType
501
+ val interstitialAdUnitId = mInterstitialAd!!.adUnitId
502
+ val result = JSONObject()
503
+ try {
504
+ result.put("micros", valueMicros)
505
+ result.put("currency", currencyCode)
506
+ result.put("precision", precision)
507
+ result.put("adUnitId", interstitialAdUnitId)
508
+ callbackContext.success(result)
509
+ } catch (e: JSONException) {
510
+ callbackContext.error(e.message)
511
+ }
512
+ cWebView!!.loadUrl(
513
+ "javascript:cordova.fireDocumentEvent('on.interstitial.revenue');"
514
+ )
515
+ }
516
+ }
517
+
518
+ private val isIntAutoShow: Unit
519
+ get() {
520
+ if (isInterstitialLoad && mInterstitialAd != null) {
521
+ mActivity!!.runOnUiThread { mInterstitialAd!!.show(mActivity!!) }
522
+ }
523
+ }
524
+
525
+ override fun onAdFailedToLoad(loadAdError: LoadAdError) {
526
+ mInterstitialAd = null
527
+ isInterstitialLoad = false
528
+ cWebView!!.loadUrl(
529
+ "javascript:cordova.fireDocumentEvent('on.interstitial.failed.load');"
530
+ )
531
+ callbackContext.error(loadAdError.toString())
532
+ }
533
+ })
534
+ } catch (e: Exception) {
535
+ callbackContext.error(e.toString())
536
+ }
537
+ }
538
+ return true
539
+ } else if (action == "showInterstitialAd") {
540
+ if (isInterstitialLoad && mInterstitialAd != null) {
541
+ mActivity!!.runOnUiThread { mInterstitialAd!!.show(mActivity!!) }
542
+ _interstitialAdLoadCallback(callbackContext)
543
+ } else {
544
+ callbackContext.error("The Interstitial ad wasn't ready yet")
545
+ }
546
+ return true
547
+ } else if (action == "loadRewardedAd") {
548
+ val options = args.getJSONObject(0)
549
+ mActivity!!.runOnUiThread {
550
+ val adUnitId = options.optString("adUnitId")
551
+ val autoShow = options.optBoolean("autoShow")
552
+ try {
553
+ this.rewardedAutoShow = autoShow
554
+ RewardedAd.load(
555
+ mActivity!!, adUnitId, buildAdRequest(),
556
+ object : RewardedAdLoadCallback() {
557
+ override fun onAdFailedToLoad(loadAdError: LoadAdError) {
558
+ rewardedAd = null
559
+ isRewardedLoad = false
560
+ cWebView!!.loadUrl(
561
+ "javascript:cordova.fireDocumentEvent('on.rewarded.failed.load');"
562
+ )
563
+ callbackContext.error(loadAdError.toString())
564
+ }
565
+
566
+ override fun onAdLoaded(ad: RewardedAd) {
567
+ rewardedAd = ad
568
+ isRewardedLoad = true
569
+ isAdSkip = 0
570
+ if (rewardedAutoShow) {
571
+ isRewardedAutoShow
572
+ }
573
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.rewarded.loaded');")
574
+ _rewardedAdLoadCallback(callbackContext)
575
+ if (ResponseInfo) {
576
+ val result = JSONObject()
577
+ val responseInfo = ad.responseInfo
578
+ try {
579
+ result.put("getResponseId", responseInfo.responseId)
580
+ result.put(
581
+ "getAdapterResponses",
582
+ responseInfo.adapterResponses
583
+ )
584
+ result.put("getResponseExtras", responseInfo.responseExtras)
585
+ result.put(
586
+ "getMediationAdapterClassName",
587
+ responseInfo.mediationAdapterClassName
588
+ )
589
+ result.put("getBundleExtra", mBundleExtra.toString())
590
+ callbackContext.success(result)
591
+ } catch (e: JSONException) {
592
+ callbackContext.error(e.message)
593
+ }
594
+ }
595
+
596
+ rewardedAd!!.onPaidEventListener =
597
+ OnPaidEventListener { adValue: AdValue ->
598
+ val valueMicros = adValue.valueMicros
599
+ val currencyCode = adValue.currencyCode
600
+ val precision = adValue.precisionType
601
+ val rewardedAdAdUnitId = rewardedAd!!.adUnitId
602
+ val result = JSONObject()
603
+ try {
604
+ result.put("micros", valueMicros)
605
+ result.put("currency", currencyCode)
606
+ result.put("precision", precision)
607
+ result.put("adUnitId", rewardedAdAdUnitId)
608
+ callbackContext.success(result)
609
+ } catch (e: JSONException) {
610
+ callbackContext.error(e.message)
611
+ }
612
+ cWebView!!.loadUrl(
613
+ "javascript:cordova.fireDocumentEvent('on.rewarded.revenue');"
614
+ )
615
+ }
616
+ }
617
+
618
+ private val isRewardedAutoShow: Unit
619
+ get() {
620
+ if (isRewardedLoad && rewardedAd != null) {
621
+ isAdSkip = 1
622
+ rewardedAd!!.show(mActivity!!) { rewardItem: RewardItem ->
623
+ isAdSkip = 2
624
+ val rewardAmount = rewardItem.amount
625
+ val rewardType = rewardItem.type
626
+ val result = JSONObject()
627
+ try {
628
+ result.put("rewardAmount", rewardAmount)
629
+ result.put("rewardType", rewardType)
630
+ callbackContext.success(result)
631
+ } catch (e: JSONException) {
632
+ callbackContext.error(e.message)
633
+ }
634
+ cWebView!!.loadUrl(
635
+ "javascript:cordova.fireDocumentEvent('on.reward.userEarnedReward');"
636
+ )
637
+ }
638
+ }
639
+ }
640
+ })
641
+ } catch (e: Exception) {
642
+ callbackContext.error(e.toString())
643
+ }
644
+ }
645
+ return true
646
+ } else if (action == "showRewardedAd") {
647
+ mActivity!!.runOnUiThread {
648
+ if (isRewardedLoad && rewardedAd != null) {
649
+ isAdSkip = 1
650
+ rewardedAd!!.show(mActivity!!) { rewardItem: RewardItem ->
651
+ isAdSkip = 2
652
+ val rewardAmount = rewardItem.amount
653
+ val rewardType = rewardItem.type
654
+ val result = JSONObject()
655
+ try {
656
+ result.put("rewardAmount", rewardAmount)
657
+ result.put("rewardType", rewardType)
658
+ callbackContext.success(result)
659
+ } catch (e: JSONException) {
660
+ callbackContext.error(e.message)
661
+ }
662
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.reward.userEarnedReward');")
663
+ }
664
+ _rewardedAdLoadCallback(callbackContext)
665
+ } else {
666
+ callbackContext.error("The rewarded ad wasn't ready yet")
667
+ }
668
+ }
669
+ return true
670
+ } else if (action == "loadRewardedInterstitialAd") {
671
+ val options = args.getJSONObject(0)
672
+ mActivity!!.runOnUiThread {
673
+ val adUnitId = options.optString("adUnitId")
674
+ val autoShow = options.optBoolean("autoShow")
675
+ try {
676
+ this.rIntAutoShow = autoShow
677
+ RewardedInterstitialAd.load(
678
+ mActivity!!, adUnitId, buildAdRequest(),
679
+ object : RewardedInterstitialAdLoadCallback() {
680
+ override fun onAdLoaded(ad: RewardedInterstitialAd) {
681
+ rewardedInterstitialAd = ad
682
+ isRewardedInterstitialLoad = true
683
+ isAdSkip = 0
684
+
685
+ if (rIntAutoShow) {
686
+ isRIntAutoShow
687
+ }
688
+ _rewardedInterstitialAdLoadCallback(callbackContext)
689
+ responseAdLoad()
690
+ revenueAd()
691
+
692
+
693
+ cWebView!!.loadUrl(
694
+ "javascript:cordova.fireDocumentEvent('on.rewardedInt.loaded');"
695
+ )
696
+ }
697
+
698
+ private fun revenueAd() {
699
+ rewardedInterstitialAd!!.onPaidEventListener =
700
+ OnPaidEventListener { adValue: AdValue ->
701
+ val valueMicros = adValue.valueMicros
702
+ val currencyCode = adValue.currencyCode
703
+ val precision = adValue.precisionType
704
+ val rewardedIntAdUnitId = rewardedInterstitialAd!!.adUnitId
705
+ val result = JSONObject()
706
+ try {
707
+ result.put("micros", valueMicros)
708
+ result.put("currency", currencyCode)
709
+ result.put("precision", precision)
710
+ result.put("adUnitId", rewardedIntAdUnitId)
711
+ callbackContext.success(result)
712
+ } catch (e: JSONException) {
713
+ callbackContext.error(e.message)
714
+ }
715
+ cWebView!!.loadUrl(
716
+ "javascript:cordova.fireDocumentEvent('on.rewardedInt.revenue');"
717
+ )
718
+ }
719
+ }
720
+
721
+ private fun responseAdLoad() {
722
+ if (ResponseInfo) {
723
+ val result = JSONObject()
724
+ val responseInfo = rewardedInterstitialAd!!.responseInfo
725
+ try {
726
+ result.put("getResponseId", responseInfo.responseId)
727
+ result.put(
728
+ "getAdapterResponses",
729
+ responseInfo.adapterResponses
730
+ )
731
+ result.put("getResponseExtras", responseInfo.responseExtras)
732
+ result.put(
733
+ "getMediationAdapterClassName",
734
+ responseInfo.mediationAdapterClassName
735
+ )
736
+ result.put("getBundleExtra", mBundleExtra.toString())
737
+ callbackContext.success(result)
738
+ } catch (e: JSONException) {
739
+ callbackContext.error(e.message)
740
+ }
741
+ }
742
+ }
743
+
744
+ private val isRIntAutoShow: Unit
745
+ get() {
746
+ if (isRewardedInterstitialLoad && rewardedInterstitialAd != null) {
747
+ isAdSkip = 1
748
+ rewardedInterstitialAd!!.show(mActivity!!) { rewardItem: RewardItem ->
749
+ isAdSkip = 2
750
+ val rewardAmount = rewardItem.amount
751
+ val rewardType = rewardItem.type
752
+ val result = JSONObject()
753
+ try {
754
+ result.put("rewardAmount", rewardAmount)
755
+ result.put("rewardType", rewardType)
756
+ callbackContext.success(result)
757
+ } catch (e: JSONException) {
758
+ callbackContext.error(e.message)
759
+ }
760
+ cWebView!!.loadUrl(
761
+ "javascript:cordova.fireDocumentEvent('on.rewardedInt.userEarnedReward');"
762
+ )
763
+ }
764
+ }
765
+ }
766
+
767
+ override fun onAdFailedToLoad(loadAdError: LoadAdError) {
768
+ rewardedInterstitialAd = null
769
+ isRewardedInterstitialLoad = false
770
+ cWebView!!.loadUrl(
771
+ "javascript:cordova.fireDocumentEvent('on.rewardedInt.failed.load');"
772
+ )
773
+ callbackContext.error(loadAdError.toString())
774
+ }
775
+ })
776
+ } catch (e: Exception) {
777
+ callbackContext.error(e.toString())
778
+ }
779
+ }
780
+ return true
781
+ } else if (action == "showRewardedInterstitialAd") {
782
+ mActivity!!.runOnUiThread {
783
+ if (isRewardedInterstitialLoad && rewardedInterstitialAd != null) {
784
+ isAdSkip = 1
785
+ rewardedInterstitialAd!!.show(mActivity!!) { rewardItem: RewardItem ->
786
+ isAdSkip = 2
787
+ val rewardAmount = rewardItem.amount
788
+ val rewardType = rewardItem.type
789
+ val result = JSONObject()
790
+ try {
791
+ result.put("rewardAmount", rewardAmount)
792
+ result.put("rewardType", rewardType)
793
+ callbackContext.success(result)
794
+ } catch (e: JSONException) {
795
+ callbackContext.error(e.message)
796
+ }
797
+ cWebView!!.loadUrl(
798
+ "javascript:cordova.fireDocumentEvent('on.rewardedInt.userEarnedReward');"
799
+ )
800
+ }
801
+
802
+ _rewardedInterstitialAdLoadCallback(callbackContext)
803
+ } else {
804
+ callbackContext.error("The rewarded ad wasn't ready yet")
805
+ }
806
+ }
807
+ return true
808
+ } else if (action == "showPrivacyOptionsForm") {
809
+ mActivity!!.runOnUiThread {
810
+ try {
811
+ val params: ConsentRequestParameters
812
+ if (this.setDebugGeography) {
813
+ val debugSettings = deviceId?.let {
814
+ mActivity?.let { it1 ->
815
+ ConsentDebugSettings.Builder(it1)
816
+ .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
817
+ .addTestDeviceHashedId(it).build()
818
+ }
819
+ }
820
+ params = ConsentRequestParameters.Builder()
821
+ .setConsentDebugSettings(debugSettings).build()
822
+ } else {
823
+ params = ConsentRequestParameters.Builder()
824
+ .setTagForUnderAgeOfConsent(this.SetTagForUnderAgeOfConsent).build()
825
+ }
826
+ consentInformation = mContext?.let {
827
+ UserMessagingPlatform.getConsentInformation(
828
+ it
829
+ )
830
+ }
831
+ mActivity?.let {
832
+ consentInformation?.requestConsentInfoUpdate(
833
+ it,
834
+ params,
835
+ {
836
+ mActivity?.let { it1 ->
837
+ UserMessagingPlatform.loadAndShowConsentFormIfRequired(
838
+ it1
839
+ ) { loadAndShowError: FormError? ->
840
+ if (loadAndShowError != null) {
841
+ cordova.activity.runOnUiThread {
842
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.consent.failed.show', { message: '" + loadAndShowError.message + "' });")
843
+ }
844
+ }
845
+ if (isPrivacyOptionsRequired == ConsentInformation.PrivacyOptionsRequirementStatus.REQUIRED) {
846
+ mActivity?.let { it2 ->
847
+ UserMessagingPlatform.showPrivacyOptionsForm(
848
+ it2
849
+ ) { formError: FormError? ->
850
+ if (formError != null) {
851
+ cordova.activity.runOnUiThread {
852
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.consent.failed.show.options', { message: '" + formError.message + "' });")
853
+ }
854
+ }
855
+ }
856
+ }
857
+ }
858
+ }
859
+ }
860
+ },
861
+ { requestConsentError: FormError ->
862
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.consent.info.update.failed', { message: '" + requestConsentError.message + "' });")
863
+ })
864
+ }
865
+ } catch (e: Exception) {
866
+ callbackContext.error(e.toString())
867
+ }
868
+ }
869
+ return true
870
+ } else if (action == "consentReset") {
871
+ mActivity!!.runOnUiThread {
872
+ try {
873
+ consentInformation!!.reset()
874
+ } catch (e: Exception) {
875
+ callbackContext.error(e.toString())
876
+ }
877
+ }
878
+ return true
879
+ } else if (action == "getIabTfc") {
880
+ mActivity!!.runOnUiThread {
881
+ val gdprApplies = mPreferences!!.getInt("IABTCF_gdprApplies", 0)
882
+ val purposeConsents = mPreferences!!.getString("IABTCF_PurposeConsents", "")
883
+ val vendorConsents = mPreferences!!.getString("IABTCF_VendorConsents", "")
884
+ val consentString = mPreferences!!.getString("IABTCF_TCString", "")
885
+ val userInfoJson = JSONObject()
886
+ try {
887
+ userInfoJson.put("IABTCF_gdprApplies", gdprApplies)
888
+ userInfoJson.put("IABTCF_PurposeConsents", purposeConsents)
889
+ userInfoJson.put("IABTCF_VendorConsents", vendorConsents)
890
+ userInfoJson.put("IABTCF_TCString", consentString)
891
+ val editor = mPreferences!!.edit()
892
+ editor.putString("IABTCF_TCString", consentString)
893
+ editor.putLong(LAST_ACCESS_SUFFIX, System.currentTimeMillis())
894
+ editor.apply()
895
+ val key = "IABTCF_TCString"
896
+ getString(key)
897
+ callbackContext.success(userInfoJson)
898
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.getIabTfc');")
899
+ } catch (e: Exception) {
900
+ callbackContext.error(e.toString())
901
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.getIabTfc.error');")
902
+ }
903
+ }
904
+ return true
905
+ } else if (action == "loadBannerAd") {
906
+ val options = args.getJSONObject(0)
907
+ mActivity!!.runOnUiThread {
908
+ val adUnitId = options.optString("adUnitId")
909
+ val position = options.optString("position")
910
+ val collapsible = options.optString("collapsible")
911
+ val size = options.optString("size")
912
+ val autoResize = options.optBoolean("autoResize")
913
+ val autoShow = options.optBoolean("autoShow")
914
+ this.bannerAdUnitId = adUnitId
915
+ this.Position = position
916
+ this.Size = size
917
+ this.bannerAutoShow = autoShow
918
+ this.isAutoResize = autoResize
919
+ this.collapsiblePos = collapsible
920
+
921
+
922
+ isCollapsible = if (collapsible.isEmpty()) {
923
+ false
924
+ } else {
925
+ true
926
+ }
927
+ try {
928
+ _loadBannerAd(adUnitId, position, size)
929
+ } catch (e: Exception) {
930
+ callbackContext.error(e.toString())
931
+ }
932
+ }
933
+ return true
934
+ } else if (action == "showBannerAd") {
935
+ mActivity!!.runOnUiThread {
936
+ if (isBannerPause == 0) {
937
+ isShowBannerAds
938
+ } else if (isBannerPause == 1) {
939
+ try {
940
+ bannerView!!.visibility = View.VISIBLE
941
+ bannerView!!.resume()
942
+ } catch (e: Exception) {
943
+ callbackContext.error(e.toString())
944
+ }
945
+ }
946
+ }
947
+ return true
948
+ } else if (action == "styleBannerAd") {
949
+ val options = args.getJSONObject(0)
950
+ mActivity!!.runOnUiThread {
951
+ val paddingPx = options.optInt("padding")
952
+ val marginsPx = options.optInt("margins")
953
+ // final boolean autoResize = options.optBoolean("autoResize");
954
+ try {
955
+ this.paddingInPx = paddingPx
956
+ this.marginsInPx = marginsPx
957
+ // this.isAutoResize = autoResize;
958
+ } catch (e: Exception) {
959
+ callbackContext.error(e.toString())
960
+ }
961
+ }
962
+ return true
963
+ } else if (action == "collapsibleBannerAd") {
964
+ val options = args.getJSONObject(0)
965
+
966
+ mActivity!!.runOnUiThread {
967
+ val enableCollapsible = options.optBoolean("enabledBannerCollapsible")
968
+ val collapsible = options.optString("collapsiblePosition")
969
+ try {
970
+ this.isCollapsible = enableCollapsible
971
+ this.collapsiblePos = collapsible
972
+ } catch (e: Exception) {
973
+ callbackContext.error(e.toString())
974
+ }
975
+ }
976
+ return true
977
+ } else if (action == "hideBannerAd") {
978
+ cordova.activity.runOnUiThread {
979
+ if (isBannerShow) {
980
+ try {
981
+ bannerView!!.visibility = View.GONE
982
+ bannerView!!.pause()
983
+ isBannerLoad = false
984
+ isBannerPause = 1
985
+ } catch (e: Exception) {
986
+ callbackContext.error(e.toString())
987
+ }
988
+ }
989
+ }
990
+ return true
991
+ } else if (action == "removeBannerAd") {
992
+ mActivity!!.runOnUiThread {
993
+ try {
994
+ if (bannerViewLayout != null && bannerView != null) {
995
+ bannerViewLayout!!.removeView(bannerView)
996
+ bannerView!!.destroy()
997
+ bannerView = null
998
+ bannerViewLayout = null
999
+ isBannerLoad = false
1000
+ isBannerShow = false
1001
+ isBannerPause = 2
1002
+ lock = true
1003
+ }
1004
+ } catch (e: Exception) {
1005
+ PUBLIC_CALLBACKS!!.error("Error removing banner: " + e.message)
1006
+ }
1007
+ }
1008
+
1009
+ return true
1010
+ }
1011
+ return false
1012
+ }
1013
+
1014
+
1015
+ private fun handleConsentForm() {
1016
+ if (consentInformation!!.isConsentFormAvailable) {
1017
+ mContext?.let {
1018
+ UserMessagingPlatform.loadConsentForm(it,
1019
+ { consentForm: ConsentForm ->
1020
+ mActivity?.let { it1 ->
1021
+ consentForm.show(
1022
+ it1
1023
+ ) { formError: FormError? ->
1024
+ if (formError != null) {
1025
+ cordova.activity.runOnUiThread {
1026
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.consent.failed.show', { message: '" + formError.message + "' });")
1027
+ }
1028
+ }
1029
+ }
1030
+ }
1031
+ },
1032
+ { formError: FormError ->
1033
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.consent.failed.load.from', { message: '" + formError.message + "' });")
1034
+ }
1035
+ )
1036
+ }
1037
+ }
1038
+ }
1039
+
1040
+
1041
+ private fun _loadBannerAd(adUnitId: String, position: String, size: String) {
1042
+ try {
1043
+ if (bannerViewLayout == null) {
1044
+ bannerViewLayout = RelativeLayout(mActivity)
1045
+
1046
+ val params = RelativeLayout.LayoutParams(
1047
+ RelativeLayout.LayoutParams.MATCH_PARENT,
1048
+ RelativeLayout.LayoutParams.WRAP_CONTENT
1049
+ )
1050
+
1051
+ val rootView = mActivity!!.window.decorView.findViewById<View>(R.id.content)
1052
+ if (rootView is ViewGroup) {
1053
+ rootView.addView(bannerViewLayout, params)
1054
+ } else {
1055
+ mActivity!!.addContentView(bannerViewLayout, params)
1056
+ }
1057
+
1058
+ bannerView = AdView(mContext!!)
1059
+ setBannerPosition(position)
1060
+ setBannerSiz(size)
1061
+ bannerView!!.adUnitId = adUnitId
1062
+ bannerView!!.adListener = bannerAdListener
1063
+ bannerView!!.loadAd(buildAdRequest())
1064
+ } else {
1065
+ Log.d(TAG, "Banner view layout already exists.")
1066
+ }
1067
+ } catch (e: Exception) {
1068
+ PUBLIC_CALLBACKS!!.error("Error showing banner: " + e.message)
1069
+ Log.d(TAG, "Error showing banner: " + e.message)
1070
+ }
1071
+ }
1072
+
1073
+
1074
+ private fun isBannerAutoShow() {
1075
+ try {
1076
+ if (bannerView != null && bannerViewLayout != null) {
1077
+ if (lock) {
1078
+ bannerViewLayout!!.addView(bannerView)
1079
+ bannerViewLayout!!.bringToFront()
1080
+ lock = false
1081
+ }
1082
+ isBannerPause = 0
1083
+ isBannerLoad = true
1084
+ } else {
1085
+ val errorMessage = "Error showing banner: bannerView or bannerViewLayout is null."
1086
+ Log.e("isBannerAutoShow", errorMessage)
1087
+ PUBLIC_CALLBACKS!!.error(errorMessage)
1088
+ }
1089
+ } catch (e: Exception) {
1090
+ val errorMessage = "Error showing banner: " + e.message
1091
+ Log.e("isBannerAutoShow", errorMessage, e)
1092
+ PUBLIC_CALLBACKS!!.error(errorMessage)
1093
+ }
1094
+ }
1095
+
1096
+
1097
+ private val isShowBannerAds: Unit
1098
+ get() {
1099
+ if (isBannerLoad && bannerView != null) {
1100
+ try {
1101
+ if (lock) {
1102
+ bannerViewLayout!!.addView(bannerView)
1103
+ bannerViewLayout!!.bringToFront()
1104
+ lock = false
1105
+ }
1106
+ isBannerShow = true
1107
+ } catch (e: Exception) {
1108
+ lock = true
1109
+ PUBLIC_CALLBACKS!!.error(e.toString())
1110
+ }
1111
+ }
1112
+ }
1113
+
1114
+
1115
+ private val bannerAdListener: AdListener = object : AdListener() {
1116
+ override fun onAdClicked() {
1117
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.banner.click');")
1118
+ }
1119
+
1120
+ override fun onAdClosed() {
1121
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.banner.close');")
1122
+ }
1123
+
1124
+ override fun onAdFailedToLoad(adError: LoadAdError) {
1125
+ mActivity!!.runOnUiThread {
1126
+ try {
1127
+ if (bannerViewLayout != null && bannerView != null) {
1128
+ bannerViewLayout!!.removeView(bannerView)
1129
+ bannerView!!.destroy()
1130
+ bannerView = null
1131
+ bannerViewLayout = null
1132
+ isBannerLoad = false
1133
+ isBannerShow = false
1134
+ isBannerPause = 2
1135
+ lock = true
1136
+ }
1137
+ } catch (e: Exception) {
1138
+ PUBLIC_CALLBACKS!!.error("Error removing banner: " + e.message)
1139
+ }
1140
+ }
1141
+
1142
+ PUBLIC_CALLBACKS!!.error(adError.toString())
1143
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.banner.failed.load');")
1144
+ }
1145
+
1146
+ override fun onAdImpression() {
1147
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.banner.impression');")
1148
+ }
1149
+
1150
+
1151
+ override fun onAdLoaded() {
1152
+ // Log.d(TAG, "onAdLoaded: Ad finished loading successfully.");
1153
+ isBannerLoad = true
1154
+ isBannerPause = 0
1155
+
1156
+ if (bannerAutoShow) {
1157
+ isBannerAutoShow()
1158
+ }
1159
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.banner.load');")
1160
+
1161
+
1162
+ val eventData = String.format(
1163
+ "{\"collapsible\": \"%s\"}",
1164
+ if (bannerView!!.isCollapsible) "collapsible" else "not collapsible"
1165
+ )
1166
+
1167
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.is.collapsible', $eventData)")
1168
+
1169
+
1170
+ bannerView!!.onPaidEventListener = bannerPaidAdListener
1171
+
1172
+ if (ResponseInfo) {
1173
+ val result = JSONObject()
1174
+ val responseInfo = bannerView!!.responseInfo
1175
+ try {
1176
+ checkNotNull(responseInfo)
1177
+ result.put("getResponseId", responseInfo.responseId)
1178
+ result.put("getAdapterResponses", responseInfo.adapterResponses)
1179
+ result.put("getResponseExtras", responseInfo.responseExtras)
1180
+ result.put(
1181
+ "getMediationAdapterClassName",
1182
+ responseInfo.mediationAdapterClassName
1183
+ )
1184
+
1185
+ if (mBundleExtra != null) {
1186
+ result.put("getBundleExtra", mBundleExtra.toString())
1187
+ } else {
1188
+ result.put("getBundleExtra", JSONObject.NULL)
1189
+ }
1190
+
1191
+ PUBLIC_CALLBACKS!!.success(result)
1192
+ } catch (e: JSONException) {
1193
+ PUBLIC_CALLBACKS!!.error(e.toString())
1194
+ }
1195
+ }
1196
+ }
1197
+
1198
+
1199
+ override fun onAdOpened() {
1200
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.banner.open');")
1201
+ isBannerShows = false
1202
+ }
1203
+ }
1204
+
1205
+
1206
+ private val bannerPaidAdListener = OnPaidEventListener { adValue ->
1207
+ val valueMicros = adValue.valueMicros
1208
+ val currencyCode = adValue.currencyCode
1209
+ val precision = adValue.precisionType
1210
+ val adUnitId = bannerView!!.adUnitId
1211
+ val result = JSONObject()
1212
+ try {
1213
+ result.put("micros", valueMicros)
1214
+ result.put("currency", currencyCode)
1215
+ result.put("precision", precision)
1216
+ result.put("adUnitId", adUnitId)
1217
+ isBannerLoad = false
1218
+ isBannerShow = true
1219
+ PUBLIC_CALLBACKS!!.success(result)
1220
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.banner.revenue');")
1221
+ } catch (e: JSONException) {
1222
+ PUBLIC_CALLBACKS!!.error(e.message)
1223
+ }
1224
+ }
1225
+
1226
+
1227
+ private fun setBannerSiz(size: String?) {
1228
+ when (size) {
1229
+ "responsive_adaptive" -> bannerView!!.setAdSize(adSize)
1230
+ "anchored_adaptive" -> bannerView!!.setAdSize(
1231
+ AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
1232
+ mContext!!, adWidth
1233
+ )
1234
+ )
1235
+
1236
+ "full_width_adaptive" -> bannerView!!.setAdSize(
1237
+ AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
1238
+ mContext!!, AdSize.FULL_WIDTH
1239
+ )
1240
+ )
1241
+
1242
+ "in_line_adaptive" -> bannerView!!.setAdSize(
1243
+ AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(
1244
+ mContext!!, adWidth
1245
+ )
1246
+ )
1247
+
1248
+ "banner" -> bannerView!!.setAdSize(AdSize.BANNER)
1249
+ "large_banner" -> bannerView!!.setAdSize(AdSize.LARGE_BANNER)
1250
+ "medium_rectangle" -> bannerView!!.setAdSize(AdSize.MEDIUM_RECTANGLE)
1251
+ "full_banner" -> bannerView!!.setAdSize(AdSize.FULL_BANNER)
1252
+ "leaderboard" -> bannerView!!.setAdSize(AdSize.LEADERBOARD)
1253
+ "fluid" -> bannerView!!.setAdSize(AdSize.FLUID)
1254
+ }
1255
+ }
1256
+
1257
+
1258
+ private val adSize: AdSize
1259
+ get() {
1260
+ val display = mActivity!!.windowManager.defaultDisplay
1261
+ val outMetrics = DisplayMetrics()
1262
+ display.getMetrics(outMetrics)
1263
+ val density = outMetrics.density
1264
+ val adWidthPixels =
1265
+ if (bannerViewLayout != null && bannerViewLayout!!.width > 0) bannerViewLayout!!.width else outMetrics.widthPixels
1266
+ val adWidth = (adWidthPixels / density).toInt()
1267
+ return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(mContext!!, adWidth)
1268
+ }
1269
+
1270
+ private val adWidth: Int
1271
+ get() {
1272
+ val display = mActivity!!.windowManager.defaultDisplay
1273
+ val outMetrics = DisplayMetrics()
1274
+ display.getMetrics(outMetrics)
1275
+ val density = outMetrics.density
1276
+ val adWidthPixels =
1277
+ (if (bannerViewLayout != null && bannerViewLayout!!.width > 0) bannerViewLayout!!.width else outMetrics.widthPixels).toFloat()
1278
+ return (adWidthPixels / density).toInt()
1279
+ }
1280
+
1281
+
1282
+ private fun setBannerPosition(position: String?) {
1283
+ val bannerParams = RelativeLayout.LayoutParams(
1284
+ RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT
1285
+ )
1286
+
1287
+ when (position) {
1288
+ "top-right" -> {
1289
+ bannerParams.addRule(RelativeLayout.ALIGN_PARENT_TOP)
1290
+ bannerParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
1291
+ bannerParams.setMargins(0, marginsInPx, marginsInPx, 0)
1292
+ bannerViewLayout!!.setPadding(0, paddingInPx, paddingInPx, 0)
1293
+ }
1294
+
1295
+ "top-center" -> {
1296
+ bannerParams.addRule(RelativeLayout.ALIGN_PARENT_TOP)
1297
+ bannerParams.addRule(RelativeLayout.CENTER_HORIZONTAL)
1298
+ bannerParams.setMargins(0, marginsInPx, 0, 0)
1299
+ bannerViewLayout!!.setPadding(0, paddingInPx, 0, 0)
1300
+ }
1301
+
1302
+ "left" -> {
1303
+ bannerParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
1304
+ bannerParams.addRule(RelativeLayout.CENTER_VERTICAL)
1305
+ bannerParams.setMargins(marginsInPx, 0, 0, 0)
1306
+ bannerViewLayout!!.setPadding(paddingInPx, 0, 0, 0)
1307
+ }
1308
+
1309
+ "center" -> {
1310
+ bannerParams.addRule(RelativeLayout.CENTER_HORIZONTAL)
1311
+ bannerParams.addRule(RelativeLayout.CENTER_VERTICAL)
1312
+ }
1313
+
1314
+ "right" -> {
1315
+ bannerParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
1316
+ bannerParams.addRule(RelativeLayout.CENTER_VERTICAL)
1317
+ bannerParams.setMargins(0, 0, marginsInPx, 0)
1318
+ bannerViewLayout!!.setPadding(0, 0, paddingInPx, 0)
1319
+ }
1320
+
1321
+ "bottom-center" -> {
1322
+ bannerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
1323
+ bannerParams.addRule(RelativeLayout.CENTER_HORIZONTAL)
1324
+ bannerParams.setMargins(0, 0, 0, marginsInPx)
1325
+ bannerViewLayout!!.setPadding(0, 0, 0, paddingInPx)
1326
+ }
1327
+
1328
+ "bottom-right" -> {
1329
+ bannerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
1330
+ bannerParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
1331
+ bannerParams.setMargins(0, 0, marginsInPx, marginsInPx)
1332
+ bannerViewLayout!!.setPadding(0, 0, paddingInPx, paddingInPx)
1333
+ }
1334
+
1335
+ else -> {
1336
+ bannerParams.addRule(RelativeLayout.CENTER_HORIZONTAL)
1337
+ bannerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
1338
+ bannerParams.setMargins(marginsInPx, 0, 0, marginsInPx)
1339
+ bannerViewLayout!!.setPadding(paddingInPx, 0, 0, paddingInPx)
1340
+ }
1341
+ }
1342
+ bannerView!!.layoutParams = bannerParams
1343
+ }
1344
+
1345
+
1346
+ fun setUsingAdManagerRequest(isUsingAdManagerRequest: Boolean) {
1347
+ this.isUsingAdManagerRequest = isUsingAdManagerRequest
1348
+ }
1349
+
1350
+
1351
+ private fun targetingAdRequest(
1352
+ customTargeting: JSONArray?,
1353
+ categoryExclusions: String,
1354
+ ppId: String,
1355
+ ctURL: String,
1356
+ brandSafetyArr: JSONArray?
1357
+ ) {
1358
+ try {
1359
+ customTargetingList = ArrayList()
1360
+
1361
+ if (customTargeting != null) {
1362
+ for (i in 0 until customTargeting.length()) {
1363
+ (customTargetingList as ArrayList<String>).add(customTargeting.getString(i))
1364
+ }
1365
+ }
1366
+
1367
+
1368
+ brandSafetyUrls = ArrayList()
1369
+ if (brandSafetyArr != null) {
1370
+ for (i in 0 until brandSafetyArr.length()) {
1371
+ try {
1372
+ (brandSafetyUrls as ArrayList<String>).add(brandSafetyArr.getString(i))
1373
+ } catch (e: JSONException) {
1374
+ // e.printStackTrace();
1375
+ }
1376
+ }
1377
+ }
1378
+
1379
+ this.cExclusionsValue = categoryExclusions
1380
+ this.ppIdVl = ppId
1381
+ this.cURLVl = ctURL
1382
+ } catch (e: JSONException) {
1383
+ // e.printStackTrace();
1384
+ }
1385
+ }
1386
+
1387
+
1388
+ private fun setPublisherProvidedSignals(ppsArrValue: JSONArray?) {
1389
+ try {
1390
+ ppsArrayList = ArrayList()
1391
+ if (ppsArrValue != null) {
1392
+ for (i in 0 until ppsArrValue.length()) {
1393
+ (ppsArrayList as ArrayList<Int>).add(ppsArrValue.getInt(i))
1394
+ }
1395
+ }
1396
+ } catch (e: JSONException) {
1397
+ // e.printStackTrace();
1398
+ }
1399
+ }
1400
+
1401
+
1402
+ @SuppressLint("DefaultLocale")
1403
+ private fun initializeMobileAdsSdk() {
1404
+ if (isMobileAdsInitializeCalled.getAndSet(true)) {
1405
+ return
1406
+ }
1407
+
1408
+ MobileAds.initialize(mContext!!) { initializationStatus: InitializationStatus ->
1409
+ val statusMap = initializationStatus.adapterStatusMap
1410
+ for (adapterClass in statusMap.keys) {
1411
+ val status = statusMap[adapterClass]
1412
+ if (status != null) {
1413
+ Log.d(
1414
+ TAG, String.format(
1415
+ "Adapter name:%s, Description:%s, Latency:%d", adapterClass,
1416
+ status.description, status.latency
1417
+ )
1418
+ )
1419
+ } else {
1420
+ PUBLIC_CALLBACKS!!.error(MobileAds.ERROR_DOMAIN)
1421
+ }
1422
+ }
1423
+ val sdkVersion = MobileAds.getVersion().toString()
1424
+ val mStatus = consentInformation!!.consentStatus.toString()
1425
+
1426
+ val adapterInfo = StringBuilder()
1427
+ for (adapterClass in statusMap.keys) {
1428
+ val status = statusMap[adapterClass]
1429
+ if (status != null) {
1430
+ adapterInfo.append(
1431
+ String.format(
1432
+ "Adapter name:%s, Description:%s, Latency:%d\n",
1433
+ adapterClass, status.description, status.latency
1434
+ )
1435
+ )
1436
+ }
1437
+ }
1438
+
1439
+ val gdprApplies = mPreferences!!.getInt("IABTCF_gdprApplies", 0)
1440
+ val purposeConsents = mPreferences!!.getString("IABTCF_PurposeConsents", "")
1441
+ val vendorConsents = mPreferences!!.getString("IABTCF_VendorConsents", "")
1442
+ val consentTCString = mPreferences!!.getString("IABTCF_TCString", "")
1443
+ val additionalConsent = mPreferences!!.getString("IABTCF_AddtlConsent", "")
1444
+
1445
+ val eventData = String.format(
1446
+ "{ version: '%s', adapters: '%s', consentStatus: '%s', gdprApplies: '%d', purposeConsents: '%s', vendorConsents: '%s', consentTCString: '%s', additionalConsent: '%s' }",
1447
+ sdkVersion,
1448
+ adapterInfo,
1449
+ mStatus,
1450
+ gdprApplies,
1451
+ purposeConsents,
1452
+ vendorConsents,
1453
+ consentTCString,
1454
+ additionalConsent
1455
+ )
1456
+ Log.d(TAG, "Google Mobile Ads SDK: this.SetTagForUnderAgeOfConsent $eventData")
1457
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.sdkInitialization', $eventData)")
1458
+ }
1459
+ }
1460
+
1461
+
1462
+ @SuppressLint("DefaultLocale")
1463
+ private fun buildAdRequest(): AdRequest {
1464
+ if (isUsingAdManagerRequest) {
1465
+ val builder = AdManagerAdRequest.Builder()
1466
+
1467
+ if (this.customTargetingEnabled) {
1468
+ if (customTargetingList!!.isEmpty()) {
1469
+ Log.d(TAG, "List is empty")
1470
+ PUBLIC_CALLBACKS?.error("List is empty")
1471
+ } else {
1472
+ builder.addCustomTargeting("age", customTargetingList!!)
1473
+ }
1474
+ }
1475
+
1476
+ if (this.categoryExclusionsEnabled) {
1477
+ if (cExclusionsValue != "") {
1478
+ builder.addCategoryExclusion(this.cExclusionsValue)
1479
+ }
1480
+ }
1481
+
1482
+ if (this.ppIdEnabled) {
1483
+ if (ppIdVl != "") {
1484
+ builder.setPublisherProvidedId(this.ppIdVl)
1485
+ }
1486
+ }
1487
+
1488
+
1489
+ if (this.contentURLEnabled) {
1490
+ if (cURLVl != "") {
1491
+ builder.setPublisherProvidedId(this.cURLVl)
1492
+ }
1493
+ }
1494
+
1495
+
1496
+ if (this.brandSafetyEnabled) {
1497
+ if (brandSafetyUrls!!.isEmpty()) {
1498
+ Log.d(TAG, "List is empty")
1499
+ PUBLIC_CALLBACKS?.error("List is empty")
1500
+ } else {
1501
+ builder.setNeighboringContentUrls(brandSafetyUrls!!)
1502
+ }
1503
+ }
1504
+
1505
+
1506
+ val bundleExtra = Bundle()
1507
+ // bundleExtra.putString("npa", this.Npa); DEPRECATED Beginning January 16, 2024
1508
+ if (isCollapsible) {
1509
+ bundleExtra.putString("collapsible", this.collapsiblePos)
1510
+ }
1511
+ bundleExtra.putBoolean("is_designed_for_families", this.SetTagForChildDirectedTreatment)
1512
+ bundleExtra.putBoolean("under_age_of_consent", this.SetTagForUnderAgeOfConsent)
1513
+ bundleExtra.putString("max_ad_content_rating", this.SetMaxAdContentRating)
1514
+ builder.addNetworkExtrasBundle(AdMobAdapter::class.java, bundleExtra)
1515
+
1516
+ if (this.ppsEnabled) {
1517
+ when (this.ppsVl) {
1518
+ "IAB_AUDIENCE_1_1" -> bundleExtra.putIntegerArrayList(
1519
+ "IAB_AUDIENCE_1_1",
1520
+ ppsArrayList as ArrayList<Int>?
1521
+ )
1522
+
1523
+ "IAB_CONTENT_2_2" -> bundleExtra.putIntegerArrayList(
1524
+ "IAB_CONTENT_2_2",
1525
+ ppsArrayList as ArrayList<Int>?
1526
+ )
1527
+ }
1528
+ }
1529
+ mBundleExtra = bundleExtra
1530
+
1531
+ return builder.build()
1532
+ } else {
1533
+ val builder = AdRequest.Builder()
1534
+ val bundleExtra = Bundle()
1535
+ // bundleExtra.putString("npa", this.Npa); DEPRECATED Beginning January 16, 2024
1536
+ if (isCollapsible) {
1537
+ bundleExtra.putString("collapsible", this.collapsiblePos)
1538
+ }
1539
+ bundleExtra.putBoolean("is_designed_for_families", this.SetTagForChildDirectedTreatment)
1540
+ bundleExtra.putBoolean("under_age_of_consent", this.SetTagForUnderAgeOfConsent)
1541
+ bundleExtra.putString("max_ad_content_rating", this.SetMaxAdContentRating)
1542
+ builder.addNetworkExtrasBundle(AdMobAdapter::class.java, bundleExtra)
1543
+ mBundleExtra = bundleExtra
1544
+
1545
+ return builder.build()
1546
+ }
1547
+ }
1548
+
1549
+
1550
+ val deviceId: String?
1551
+ get() {
1552
+ var algorithm = "SHA-256"
1553
+ try {
1554
+ val messageDigest = MessageDigest.getInstance(algorithm)
1555
+ val contentResolver = mContext!!.contentResolver
1556
+ @SuppressLint("HardwareIds") val androidId =
1557
+ Settings.Secure.getString(contentResolver, "android_id")
1558
+ messageDigest.update(androidId.toByteArray())
1559
+ val by = messageDigest.digest()
1560
+ val sb = StringBuilder()
1561
+ for (b in by) {
1562
+ val emi = StringBuilder(Integer.toHexString((255 and b.toInt())))
1563
+ while (emi.length < 2) {
1564
+ emi.insert(0, "0")
1565
+ }
1566
+ sb.append(emi)
1567
+ }
1568
+ return sb.toString().uppercase(Locale.getDefault())
1569
+ } catch (e: NoSuchAlgorithmException) {
1570
+ algorithm = "SHA-1"
1571
+ try {
1572
+ val messageDigest = MessageDigest.getInstance(algorithm)
1573
+ val contentResolver = mContext!!.contentResolver
1574
+ @SuppressLint("HardwareIds") val androidId =
1575
+ Settings.Secure.getString(contentResolver, "android_id")
1576
+ messageDigest.update(androidId.toByteArray())
1577
+ val by = messageDigest.digest()
1578
+ val sb = StringBuilder()
1579
+ for (b in by) {
1580
+ val emi = StringBuilder(Integer.toHexString((255 and b.toInt())))
1581
+ while (emi.length < 2) {
1582
+ emi.insert(0, "0")
1583
+ }
1584
+ sb.append(emi)
1585
+ }
1586
+ return sb.toString().uppercase(Locale.getDefault())
1587
+ } catch (ex: NoSuchAlgorithmException) {
1588
+ // ex.printStackTrace();
1589
+ return null
1590
+ }
1591
+ }
1592
+ }
1593
+
1594
+
1595
+ fun getString(key: String) {
1596
+ val lastAccessTime = mPreferences!!.getLong(key + LAST_ACCESS_SUFFIX, 0)
1597
+ val currentTime = System.currentTimeMillis()
1598
+ if (currentTime - lastAccessTime > EXPIRATION_TIME) {
1599
+ removeKey(key)
1600
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.TCString.expired');")
1601
+ }
1602
+ val editor = mPreferences!!.edit()
1603
+ editor.putLong(key + LAST_ACCESS_SUFFIX, currentTime)
1604
+ editor.apply()
1605
+ }
1606
+
1607
+ private fun removeKey(key: String) {
1608
+ val editor = mPreferences!!.edit()
1609
+ editor.remove(key)
1610
+ editor.remove(key + LAST_ACCESS_SUFFIX)
1611
+ editor.apply()
1612
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.TCString.remove');")
1613
+ }
1614
+
1615
+
1616
+
1617
+
1618
+
1619
+
1620
+ private fun _appOpenAdLoadCallback(callbackContext: CallbackContext) {
1621
+ appOpenAd!!.fullScreenContentCallback = object : FullScreenContentCallback() {
1622
+ override fun onAdDismissedFullScreenContent() {
1623
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.appOpenAd.dismissed');")
1624
+
1625
+ // Akses langsung ke properti view tanpa menggunakan 'this'
1626
+ val mainView: View? = view
1627
+ if (mainView != null) {
1628
+ mainView.requestFocus()
1629
+ }
1630
+ }
1631
+
1632
+ override fun onAdFailedToShowFullScreenContent(adError: AdError) {
1633
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.appOpenAd.failed.show');")
1634
+ callbackContext.error(adError.toString())
1635
+ appOpenAd = null
1636
+ }
1637
+
1638
+ override fun onAdShowedFullScreenContent() {
1639
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.appOpenAd.show');")
1640
+ }
1641
+ }
1642
+ }
1643
+
1644
+
1645
+ private fun _interstitialAdLoadCallback(callbackContext: CallbackContext) {
1646
+ mInterstitialAd!!.fullScreenContentCallback = object : FullScreenContentCallback() {
1647
+ override fun onAdClicked() {
1648
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.interstitial.click');")
1649
+ }
1650
+
1651
+ override fun onAdDismissedFullScreenContent() {
1652
+ mInterstitialAd = null
1653
+ isInterstitialLoad = false
1654
+ val mainView: View? = view
1655
+ mainView?.requestFocus()
1656
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.interstitial.dismissed');")
1657
+ }
1658
+
1659
+ override fun onAdFailedToShowFullScreenContent(adError: AdError) {
1660
+ mInterstitialAd = null
1661
+ isInterstitialLoad = false
1662
+ callbackContext.error(adError.toString())
1663
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.interstitial.failed.show');")
1664
+ }
1665
+
1666
+ override fun onAdImpression() {
1667
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.interstitial.impression');")
1668
+ }
1669
+
1670
+ override fun onAdShowedFullScreenContent() {
1671
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.interstitial.show');")
1672
+ }
1673
+ }
1674
+ }
1675
+
1676
+ private fun _rewardedAdLoadCallback(callbackContext: CallbackContext) {
1677
+ rewardedAd!!.fullScreenContentCallback = object : FullScreenContentCallback() {
1678
+ override fun onAdClicked() {
1679
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.rewarded.click');")
1680
+ }
1681
+
1682
+ override fun onAdDismissedFullScreenContent() {
1683
+ if (isAdSkip != 2) {
1684
+ rewardedAd = null
1685
+ isRewardedLoad = false
1686
+ val mainView: View? = view
1687
+ mainView?.requestFocus()
1688
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.rewarded.ad.skip');")
1689
+ }
1690
+ rewardedAd = null
1691
+ isRewardedLoad = false
1692
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.rewarded.dismissed');")
1693
+ }
1694
+
1695
+ override fun onAdFailedToShowFullScreenContent(adError: AdError) {
1696
+ rewardedAd = null
1697
+ isRewardedLoad = false
1698
+ callbackContext.error(adError.toString())
1699
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.rewarded.failed.show');")
1700
+ }
1701
+
1702
+ override fun onAdImpression() {
1703
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.rewarded.impression');")
1704
+ }
1705
+
1706
+ override fun onAdShowedFullScreenContent() {
1707
+ isAdSkip = 1
1708
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.rewarded.show');")
1709
+ }
1710
+ }
1711
+ }
1712
+
1713
+ private fun _rewardedInterstitialAdLoadCallback(callbackContext: CallbackContext) {
1714
+ rewardedInterstitialAd!!.fullScreenContentCallback = object : FullScreenContentCallback() {
1715
+ override fun onAdClicked() {
1716
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.rewardedInt.click');")
1717
+ }
1718
+
1719
+ override fun onAdDismissedFullScreenContent() {
1720
+ if (isAdSkip != 2) {
1721
+ rewardedInterstitialAd = null
1722
+ isRewardedInterstitialLoad = false
1723
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.rewardedInt.ad.skip');")
1724
+ }
1725
+ rewardedInterstitialAd = null
1726
+ isRewardedInterstitialLoad = false
1727
+ val mainView: View? = view
1728
+ mainView?.requestFocus()
1729
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.rewardedInt.dismissed');")
1730
+ }
1731
+
1732
+ override fun onAdFailedToShowFullScreenContent(adError: AdError) {
1733
+ rewardedInterstitialAd = null
1734
+ isRewardedInterstitialLoad = false
1735
+ callbackContext.error(adError.toString())
1736
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.rewardedInt.failed.show');")
1737
+ }
1738
+
1739
+ override fun onAdImpression() {
1740
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.rewardedInt.impression');")
1741
+ }
1742
+
1743
+ override fun onAdShowedFullScreenContent() {
1744
+ isAdSkip = 1
1745
+ Log.d(TAG, "Ad showed fullscreen content.")
1746
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.rewardedInt.showed');")
1747
+ }
1748
+ }
1749
+ }
1750
+
1751
+ fun _globalSettings(setAppMuted: Boolean, setAppVolume: Float, pubIdEnabled: Boolean) {
1752
+ MobileAds.setAppMuted(setAppMuted)
1753
+ MobileAds.setAppVolume(setAppVolume)
1754
+ MobileAds.putPublisherFirstPartyIdEnabled(pubIdEnabled)
1755
+ }
1756
+
1757
+ fun _Targeting(
1758
+ childDirectedTreatment: Boolean,
1759
+ underAgeOfConsent: Boolean,
1760
+ contentRating: String?
1761
+ ) {
1762
+ val requestConfiguration = MobileAds.getRequestConfiguration().toBuilder()
1763
+ requestConfiguration.setTagForChildDirectedTreatment(if (childDirectedTreatment) RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_TRUE else RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_FALSE)
1764
+ requestConfiguration.setTagForUnderAgeOfConsent(if (underAgeOfConsent) RequestConfiguration.TAG_FOR_UNDER_AGE_OF_CONSENT_TRUE else RequestConfiguration.TAG_FOR_UNDER_AGE_OF_CONSENT_FALSE)
1765
+ when (contentRating) {
1766
+ "T" -> requestConfiguration.setMaxAdContentRating(RequestConfiguration.MAX_AD_CONTENT_RATING_T)
1767
+ "PG" -> requestConfiguration.setMaxAdContentRating(RequestConfiguration.MAX_AD_CONTENT_RATING_PG)
1768
+ "MA" -> requestConfiguration.setMaxAdContentRating(RequestConfiguration.MAX_AD_CONTENT_RATING_MA)
1769
+ "G" -> requestConfiguration.setMaxAdContentRating(RequestConfiguration.MAX_AD_CONTENT_RATING_G)
1770
+ else -> requestConfiguration.setMaxAdContentRating(RequestConfiguration.MAX_AD_CONTENT_RATING_UNSPECIFIED)
1771
+ }
1772
+ MobileAds.setRequestConfiguration(requestConfiguration.build())
1773
+ }
1774
+
1775
+ val isPrivacyOptionsRequired: ConsentInformation.PrivacyOptionsRequirementStatus
1776
+ get() = consentInformation
1777
+ ?.getPrivacyOptionsRequirementStatus() ?: ConsentInformation.PrivacyOptionsRequirementStatus.REQUIRED
1778
+
1779
+
1780
+
1781
+ private fun setPersonalizationState(setPPT: String) {
1782
+ val state = when (setPPT) {
1783
+ "disabled" -> PublisherPrivacyPersonalizationState.DISABLED
1784
+ "enabled" -> PublisherPrivacyPersonalizationState.ENABLED
1785
+ else -> PublisherPrivacyPersonalizationState.DEFAULT
1786
+ }
1787
+ val requestConfiguration = MobileAds.getRequestConfiguration()
1788
+ .toBuilder()
1789
+ .setPublisherPrivacyPersonalizationState(state)
1790
+ .build()
1791
+ MobileAds.setRequestConfiguration(requestConfiguration)
1792
+ }
1793
+
1794
+
1795
+ private val view: View?
1796
+ get() {
1797
+ if (View::class.java.isAssignableFrom(CordovaWebView::class.java)) {
1798
+ return cWebView as View?
1799
+ }
1800
+ return mActivity!!.window.decorView.findViewById(R.id.content)
1801
+ }
1802
+
1803
+ override fun onPause(multitasking: Boolean) {
1804
+ if (bannerView != null) {
1805
+ bannerView!!.pause()
1806
+ }
1807
+
1808
+ super.onPause(multitasking)
1809
+ }
1810
+
1811
+ override fun onResume(multitasking: Boolean) {
1812
+ super.onResume(multitasking)
1813
+ if (bannerView != null) {
1814
+ bannerView!!.resume() // ini berfungsi
1815
+ }
1816
+ }
1817
+
1818
+ override fun onNewIntent(intent: Intent) {
1819
+ super.onNewIntent(intent)
1820
+ }
1821
+
1822
+ override fun onDestroy() {
1823
+ if (bannerView != null) {
1824
+ bannerView!!.destroy()
1825
+ bannerView = null
1826
+ }
1827
+ if (bannerViewLayout != null) {
1828
+ val parentView = bannerViewLayout!!.parent as ViewGroup
1829
+ parentView?.removeView(bannerViewLayout)
1830
+ bannerViewLayout = null
1831
+ }
1832
+ super.onDestroy()
1833
+ }
1834
+
1835
+ companion object {
1836
+ private const val TAG = "emiAdmobPlugin"
1837
+
1838
+ // Consent status will automatically reset after 12 months
1839
+ // https://support.google.com/admanager/answer/9999955?hl=en
1840
+ private const val LAST_ACCESS_SUFFIX = "_last_access"
1841
+ private const val EXPIRATION_TIME = 360L * 24 * 60 * 60 * 1000
1842
+ }
1843
+ }