1
+ package org.prebid.mobile.renderingtestapp.plugplay.bidding.testing
2
+
3
+ import android.os.Bundle
4
+ import android.util.Log
5
+ import android.view.View
6
+ import android.view.ViewGroup
7
+ import android.webkit.WebSettings
8
+ import android.webkit.WebView
9
+ import android.widget.Toast
10
+ import com.google.android.gms.ads.AdListener
11
+ import com.google.android.gms.ads.AdSize
12
+ import com.google.android.gms.ads.LoadAdError
13
+ import com.google.android.gms.ads.admanager.AdManagerAdRequest
14
+ import com.google.android.gms.ads.admanager.AdManagerAdView
15
+ import org.prebid.mobile.renderingtestapp.AdFragment
16
+ import org.prebid.mobile.renderingtestapp.R
17
+ import org.prebid.mobile.renderingtestapp.databinding.FragmentEmptyBinding
18
+ import org.prebid.mobile.renderingtestapp.databinding.FragmentPucTestingGamBinding
19
+ import org.prebid.mobile.renderingtestapp.plugplay.config.AdConfiguratorDialogFragment
20
+
21
+ /* *
22
+ * Example for testing memory leaks with original API ad units.
23
+ * It doesn't use Google ad view because it causes another memory leak after loadAd().
24
+ */
25
+ open class PrebidUniversalCreativeTestingGamFragment : AdFragment () {
26
+
27
+ override val layoutRes = R .layout.fragment_puc_testing_gam
28
+
29
+ private lateinit var adView: AdManagerAdView
30
+
31
+ private val binding: FragmentPucTestingGamBinding
32
+ get() = getBinding()
33
+
34
+ override fun configuratorMode () = AdConfiguratorDialogFragment .AdConfiguratorMode .BANNER
35
+
36
+ override fun initUi (view : View , savedInstanceState : Bundle ? ) {
37
+ super .initUi(view, savedInstanceState)
38
+ }
39
+
40
+ override fun initAd (): Any {
41
+ val adView = AdManagerAdView (requireContext()).apply { adView = this }
42
+ adView.adUnitId = " /21808260008/prebid_puc_testing"
43
+ adView.setAdSizes(AdSize (300 , 250 ))
44
+ adView.adListener = createListener()
45
+ adView.loadAd(AdManagerAdRequest .Builder ().build())
46
+ binding.container.addView(adView)
47
+ return adView
48
+ }
49
+
50
+ private fun createListener (): AdListener = object : AdListener () {
51
+
52
+ override fun onAdLoaded () {
53
+ super .onAdLoaded()
54
+
55
+ val webView = WebViewSearcher .findIn(adView)
56
+ if (webView == null ) {
57
+ Log .e(" TESTV" , " WebView is null" )
58
+ return
59
+ }
60
+
61
+ webView.getSettings().mixedContentMode = WebSettings .MIXED_CONTENT_ALWAYS_ALLOW
62
+ }
63
+
64
+ override fun onAdFailedToLoad (error : LoadAdError ) {
65
+ Toast .makeText(requireContext(), " Error loading GAM ad: ${error.message} " , Toast .LENGTH_SHORT ).show()
66
+ }
67
+ }
68
+
69
+
70
+ override fun loadAd () {}
71
+
72
+ internal object WebViewSearcher {
73
+
74
+ fun findIn (root : View ): WebView ? {
75
+ if (root is WebView ) return root
76
+
77
+ if (root is ViewGroup ) return findRecursively(root)
78
+
79
+ return null
80
+ }
81
+
82
+ private fun findRecursively (root : ViewGroup ): WebView ? {
83
+ for (i in 0 until root.childCount) {
84
+ val child = root.getChildAt(i)
85
+ if (child is WebView ) return child
86
+
87
+ if (child is ViewGroup ) {
88
+ val result = findRecursively(child)
89
+ if (result != null ) return result
90
+ }
91
+ }
92
+
93
+ return null
94
+ }
95
+
96
+ }
97
+
98
+ }
0 commit comments