| 1 | package com.plugin.admob.ads |
| 2 | |
| 3 | import com.plugin.admob.AdmobPlugin |
| 4 | import com.plugin.admob.ExecuteContext |
| 5 | import com.plugin.admob.Generated |
| 6 | import com.plugin.admob.core.Context |
| 7 | import com.plugin.admob.core.GenericAd |
| 8 | import com.plugin.admob.core.Helper.Companion.getParentView |
| 9 | import com.plugin.admob.core.Helper.Companion.removeFromParentView |
| 10 | import android.annotation.SuppressLint |
| 11 | import android.view.Gravity |
| 12 | import android.view.View |
| 13 | import android.view.ViewGroup |
| 14 | import android.webkit.WebView |
| 15 | import android.widget.LinearLayout |
| 16 | import com.google.android.gms.ads.AdListener |
| 17 | import com.google.android.gms.ads.AdSize |
| 18 | import com.google.android.gms.ads.AdView |
| 19 | import com.google.android.gms.ads.LoadAdError |
| 20 | import java.util.Objects |
| 21 | import androidx.core.view.isGone |
| 22 | |
| 23 | class Banner(ctx: ExecuteContext) : AdBase(ctx), GenericAd { |
| 24 | private val adSize: AdSize = AdSize.SMART_BANNER |
| 25 | private val gravity: Int = if ("top" == ctx.optPosition()) Gravity.TOP else Gravity.BOTTOM |
| 26 | private var adView: AdView? = null |
| 27 | |
| 28 | override val isLoaded: Boolean |
| 29 | get() = adView != null |
| 30 | |
| 31 | override fun load(ctx: Context?) { |
| 32 | if (adView == null) { |
| 33 | adView = AdView(activity) |
| 34 | adView!!.adUnitId = adUnitId |
| 35 | adView!!.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(activity, 360)) |
| 36 | adView!!.adListener = object : AdListener() { |
| 37 | override fun onAdClicked() { |
| 38 | emit(Generated.Events.BANNER_CLICK) |
| 39 | } |
| 40 | |
| 41 | override fun onAdClosed() { |
| 42 | emit(Generated.Events.BANNER_CLOSE) |
| 43 | } |
| 44 | |
| 45 | override fun onAdFailedToLoad(error: LoadAdError) { |
| 46 | emit(Generated.Events.BANNER_LOAD_FAIL, error) |
| 47 | } |
| 48 | |
| 49 | override fun onAdImpression() { |
| 50 | emit(Generated.Events.BANNER_IMPRESSION) |
| 51 | } |
| 52 | |
| 53 | override fun onAdLoaded() { |
| 54 | emit(Generated.Events.BANNER_LOAD) |
| 55 | } |
| 56 | |
| 57 | override fun onAdOpened() { |
| 58 | emit(Generated.Events.BANNER_OPEN) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | adView!!.loadAd(ctx!!.optAdRequest()) |
| 63 | ctx.resolve() |
| 64 | } |
| 65 | |
| 66 | override fun show(ctx: Context?) { |
| 67 | Objects.requireNonNull(adView) |
| 68 | val webView: WebView = ExecuteContext.Companion.plugin!!.webView!! |
| 69 | if (getParentView(adView) == null) { |
| 70 | addBannerView(ExecuteContext.Companion.plugin, adView) |
| 71 | } else if (adView!!.isGone) { |
| 72 | adView!!.resume() |
| 73 | adView!!.visibility = View.VISIBLE |
| 74 | } else { |
| 75 | val wvParentView = getParentView(webView) |
| 76 | if (parentView !== wvParentView) { |
| 77 | parentView!!.removeAllViews() |
| 78 | removeFromParentView(parentView) |
| 79 | addBannerView(ExecuteContext.Companion.plugin, adView) |
| 80 | } |
| 81 | } |
| 82 | ctx!!.resolve() |
| 83 | } |
| 84 | |
| 85 | override fun hide(ctx: Context?) { |
| 86 | if (adView != null) { |
| 87 | adView!!.pause() |
| 88 | adView!!.visibility = View.GONE |
| 89 | } |
| 90 | ctx!!.resolve() |
| 91 | } |
| 92 | |
| 93 | override fun destroy() { |
| 94 | if (adView != null) { |
| 95 | adView!!.destroy() |
| 96 | adView = null |
| 97 | } |
| 98 | super.destroy() |
| 99 | } |
| 100 | |
| 101 | private fun addBannerView(plugin: AdmobPlugin?, adView: AdView?) { |
| 102 | val webView = plugin!!.webView!! |
| 103 | val wvParentView = webView.parent as ViewGroup |
| 104 | if (parentView == null) { |
| 105 | parentView = LinearLayout(webView.context) |
| 106 | } |
| 107 | if (wvParentView != null && wvParentView !== parentView) { |
| 108 | if (getParentView(parentView) != null) { |
| 109 | parentView!!.removeAllViews() |
| 110 | removeFromParentView(parentView) |
| 111 | } |
| 112 | wvParentView.removeView(webView) |
| 113 | val content = parentView as LinearLayout? |
| 114 | content!!.orientation = LinearLayout.VERTICAL |
| 115 | content.setLayoutParams( |
| 116 | LinearLayout.LayoutParams( |
| 117 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 118 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 119 | 0.0f |
| 120 | ) |
| 121 | ) |
| 122 | webView.layoutParams = LinearLayout.LayoutParams( |
| 123 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 124 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 125 | 1.0f |
| 126 | ) |
| 127 | content.addView(webView) |
| 128 | wvParentView.addView(parentView) |
| 129 | } |
| 130 | if (gravity == Gravity.TOP) { |
| 131 | parentView!!.addView(adView, 0) |
| 132 | } else { |
| 133 | parentView!!.addView(adView) |
| 134 | } |
| 135 | parentView!!.bringToFront() |
| 136 | parentView!!.requestLayout() |
| 137 | parentView!!.requestFocus() |
| 138 | } |
| 139 | |
| 140 | companion object { |
| 141 | @SuppressLint("StaticFieldLeak") |
| 142 | private var parentView: ViewGroup? = null |
| 143 | } |
| 144 | } |