emi-indo-cordova-plugin-admob 2.0.3 → 2.0.4

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.
@@ -83,11 +83,14 @@ document.addEventListener("deviceready", function () {
83
83
  }
84
84
 
85
85
 
86
- // (Optional) deactivate Google's consent
86
+ // (Optional)
87
87
  /*
88
88
  cordova.plugins.emiAdmobPlugin.metaData({
89
89
 
90
- useCustomConsentManager: false // Default false only android
90
+ useCustomConsentManager: false, // deactivate Google's consent Default false only android
91
+
92
+ isEnabledKeyword: false, // Default false only android
93
+ setKeyword: "" // https://github.com/EMI-INDO/emi-indo-cordova-plugin-admob/discussions/54
91
94
 
92
95
  });
93
96
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "emi-indo-cordova-plugin-admob",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "description": "Cordova/Quasar/Capacitor Plugin Admob Android IOS",
5
5
  "cordova": {
6
6
  "id": "emi-indo-cordova-plugin-admob",
package/plugin.xml CHANGED
@@ -1,6 +1,6 @@
1
1
  <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
2
2
  xmlns:android="http://schemas.android.com/apk/res/android"
3
- id="emi-indo-cordova-plugin-admob" version="2.0.3">
3
+ id="emi-indo-cordova-plugin-admob" version="2.0.4">
4
4
 
5
5
  <name>emiAdmobPlugin</name>
6
6
  <description>Cordova/Quasar/Capacitor Plugin Admob Android IOS</description>
@@ -144,6 +144,8 @@ class emiAdmobPlugin : CordovaPlugin() {
144
144
  private var isUsingAdManagerRequest = false
145
145
 
146
146
  private var isCustomConsentManager = false
147
+ private var isEnabledKeyword: Boolean = false
148
+ private var setKeyword: String = ""
147
149
 
148
150
  private var mActivity: Activity? = null
149
151
  private var mContext: Context? = null
@@ -1136,58 +1138,59 @@ class emiAdmobPlugin : CordovaPlugin() {
1136
1138
  } else if (action == "styleBannerAd") {
1137
1139
  val options = args.getJSONObject(0)
1138
1140
  if (mActivity != null) {
1139
- mActivity?.runOnUiThread {
1140
- val screenHeight: Int
1141
- val usableHeight: Int
1142
-
1143
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
1144
- val windowMetrics = cordova.activity.windowManager.currentWindowMetrics
1145
- val insets = windowMetrics.windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.navigationBars())
1146
- screenHeight = windowMetrics.bounds.height()
1147
- usableHeight = screenHeight - insets.bottom
1148
- } else {
1149
- val display = cordova.activity.windowManager.defaultDisplay
1150
- val size = Point()
1151
- val realSize = Point()
1152
- display.getSize(size)
1153
- display.getRealSize(realSize)
1154
-
1155
- usableHeight = size.y
1156
- screenHeight = realSize.y
1157
- }
1158
1141
 
1159
- val navBarHeight = maxOf(0, screenHeight - usableHeight)
1142
+ val screenHeight: Int
1143
+ val usableHeight: Int
1144
+
1145
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
1146
+ val windowMetrics = cordova.activity.windowManager.currentWindowMetrics
1147
+ val insets = windowMetrics.windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.navigationBars())
1148
+ screenHeight = windowMetrics.bounds.height()
1149
+ usableHeight = screenHeight - insets.bottom
1150
+ } else {
1151
+ val display = cordova.activity.windowManager.defaultDisplay
1152
+ val size = Point()
1153
+ val realSize = Point()
1154
+ display.getSize(size)
1155
+ display.getRealSize(realSize)
1156
+
1157
+ usableHeight = size.y
1158
+ screenHeight = realSize.y
1159
+ }
1160
+
1161
+ val navBarHeight = maxOf(0, screenHeight - usableHeight)
1160
1162
 
1161
- val isOverlapping = options.optBoolean("isOverlapping", false)
1162
- val setStatusBarShow = options.optBoolean("isStatusBarShow", true)
1163
- val overlappingHeight = options.optInt("overlappingHeight", navBarHeight)
1164
- val paddingPx = options.optInt("padding", 0)
1165
- val marginsPx = options.optInt("margins", navBarHeight)
1163
+ val isOverlapping = options.optBoolean("isOverlapping", false)
1164
+ val setStatusBarShow = options.optBoolean("isStatusBarShow", true)
1165
+ val overlappingHeight = options.optInt("overlappingHeight", navBarHeight)
1166
+ val paddingPx = options.optInt("padding", 0)
1167
+ val marginsPx = options.optInt("margins", navBarHeight)
1168
+
1169
+ this.isOverlapping = isOverlapping
1170
+ this.isStatusBarShow = setStatusBarShow
1171
+ this.overlappingHeight = if (overlappingHeight > 0) overlappingHeight else navBarHeight
1172
+ this.paddingInPx = paddingPx
1173
+ this.marginsInPx = if (marginsPx > 0) marginsPx else navBarHeight
1174
+
1175
+ cordova.getThreadPool().execute {
1166
1176
 
1167
- val result = JSONObject()
1168
1177
  try {
1169
- // Make sure to only set variables if they have the correct values
1170
- this.isOverlapping = isOverlapping
1171
- this.isStatusBarShow = setStatusBarShow
1172
- this.overlappingHeight = if (overlappingHeight > 0) overlappingHeight else navBarHeight
1173
- this.paddingInPx = paddingPx
1174
- this.marginsInPx = if (marginsPx > 0) marginsPx else navBarHeight
1175
-
1176
- // Add more useful data to the result
1177
- result.put("navBarHeight", navBarHeight)
1178
- result.put("screenHeight", screenHeight)
1179
- result.put("usableHeight", usableHeight)
1180
- result.put("isOverlapping", isOverlapping)
1181
- result.put("overlappingHeight", this.overlappingHeight)
1182
- result.put("paddingInPx", paddingPx)
1183
- result.put("marginsInPx", this.marginsInPx)
1184
-
1185
- cWebView?.let {
1186
- it.loadUrl("javascript:cordova.fireDocumentEvent('on.style.banner.ad', ${result});")
1187
- } ?: run {
1188
- callbackContext.error("Error: cWebView is null.")
1189
- }
1190
1178
 
1179
+ val eventData = """
1180
+ {
1181
+ "navBarHeight": "$navBarHeight",
1182
+ "screenHeight": "$screenHeight",
1183
+ "usableHeight": "$usableHeight",
1184
+ "isOverlapping": $isOverlapping,
1185
+ "overlappingHeight": "$overlappingHeight",
1186
+ "paddingInPx": "$paddingPx",
1187
+ "marginsInPx": "$marginsInPx"
1188
+ }
1189
+ """.trimIndent()
1190
+
1191
+ mActivity?.runOnUiThread {
1192
+ cWebView?.loadUrl("javascript:cordova.fireDocumentEvent('on.style.banner.ad', $eventData)")
1193
+ }
1191
1194
  } catch (e: Exception) {
1192
1195
  callbackContext.error("Error in styleBannerAd: ${e.message}")
1193
1196
  }
@@ -1198,8 +1201,12 @@ class emiAdmobPlugin : CordovaPlugin() {
1198
1201
  } else if (action == "metaData") {
1199
1202
  val options = args.getJSONObject(0)
1200
1203
  val useCustomConsentManager = options.optBoolean("useCustomConsentManager")
1204
+ val useCustomKeyword = options.optBoolean("isEnabledKeyword")
1205
+ val keywordValue = options.optString("setKeyword")
1201
1206
  if (mActivity != null) {
1202
1207
  this.isCustomConsentManager = useCustomConsentManager
1208
+ this.isEnabledKeyword = useCustomKeyword
1209
+ this.setKeyword = keywordValue
1203
1210
  }
1204
1211
  return true
1205
1212
  } else if (action == "hideBannerAd") {
@@ -1637,31 +1644,31 @@ class emiAdmobPlugin : CordovaPlugin() {
1637
1644
 
1638
1645
 
1639
1646
 
1640
-
1641
-
1642
-
1643
-
1644
- private val bannerPaidAdListener = OnPaidEventListener { adValue ->
1645
- val valueMicros = adValue.valueMicros.takeIf { it > 0 } ?: 0L
1646
- val currencyCode = adValue.currencyCode.ifBlank { "UNKNOWN" }
1647
- val precision = adValue.precisionType.takeIf { it >= 0 } ?: AdValue.PrecisionType.UNKNOWN
1648
- val adUnitId = bannerView?.adUnitId ?: "null"
1649
- val result = JSONObject()
1650
- try {
1651
- result.put("micros", valueMicros)
1652
- result.put("currency", currencyCode)
1653
- result.put("precision", precision)
1654
- result.put("adUnitId", adUnitId)
1655
- isBannerLoad = false
1656
- isBannerShow = true
1657
- cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.banner.revenue', ${result});")
1658
- } catch (e: JSONException) {
1659
- PUBLIC_CALLBACKS!!.error(e.message)
1647
+ private val bannerPaidAdListener = OnPaidEventListener { adValue ->
1648
+ val valueMicros = adValue.valueMicros.takeIf { it > 0 } ?: 0L
1649
+ val currencyCode = adValue.currencyCode.ifBlank { "UNKNOWN" }
1650
+ val precision = adValue.precisionType.takeIf { it >= 0 } ?: AdValue.PrecisionType.UNKNOWN
1651
+ val adUnitId = bannerView?.adUnitId ?: "null"
1652
+ val result = JSONObject()
1653
+ try {
1654
+ result.put("micros", valueMicros)
1655
+ result.put("currency", currencyCode)
1656
+ result.put("precision", precision)
1657
+ result.put("adUnitId", adUnitId)
1658
+ isBannerLoad = false
1659
+ isBannerShow = true
1660
+ cWebView!!.loadUrl("javascript:cordova.fireDocumentEvent('on.banner.revenue', ${result});")
1661
+ } catch (e: JSONException) {
1662
+ PUBLIC_CALLBACKS!!.error(e.message)
1663
+ }
1660
1664
  }
1661
- }
1662
-
1663
1665
 
1664
1666
  private fun setBannerSize(size: String?) {
1667
+ if (mActivity == null || bannerView == null) {
1668
+ Log.e("AdBanner", "mActivity or bannerView is null. Cannot set banner size.")
1669
+ return
1670
+ }
1671
+
1665
1672
  when (size) {
1666
1673
  "responsive_adaptive" -> bannerView?.setAdSize(adSize)
1667
1674
  "anchored_adaptive" -> bannerView?.setAdSize(
@@ -1669,41 +1676,42 @@ class emiAdmobPlugin : CordovaPlugin() {
1669
1676
  mActivity!!, adWidth
1670
1677
  )
1671
1678
  )
1672
-
1673
1679
  "full_width_adaptive" -> bannerView?.setAdSize(
1674
1680
  AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
1675
1681
  mActivity!!, adWidth
1676
1682
  )
1677
1683
  )
1678
-
1679
1684
  "in_line_adaptive" -> bannerView?.setAdSize(
1680
1685
  AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(
1681
1686
  mActivity!!, adWidth
1682
1687
  )
1683
1688
  )
1684
-
1685
1689
  "banner" -> bannerView?.setAdSize(AdSize.BANNER)
1686
1690
  "large_banner" -> bannerView?.setAdSize(AdSize.LARGE_BANNER)
1687
1691
  "medium_rectangle" -> bannerView?.setAdSize(AdSize.MEDIUM_RECTANGLE)
1688
1692
  "full_banner" -> bannerView?.setAdSize(AdSize.FULL_BANNER)
1689
1693
  "leaderboard" -> bannerView?.setAdSize(AdSize.LEADERBOARD)
1690
1694
  "fluid" -> bannerView?.setAdSize(AdSize.FLUID)
1695
+ else -> Log.e("AdBanner", "Unknown banner size: $size")
1691
1696
  }
1692
1697
  }
1693
1698
 
1694
-
1695
1699
  private val adSize: AdSize
1696
1700
  get() {
1697
- val outMetrics = DisplayMetrics()
1701
+ if (mActivity == null) {
1702
+ throw IllegalStateException("mActivity is null. Cannot get adSize.")
1703
+ }
1698
1704
 
1705
+ val outMetrics = DisplayMetrics()
1699
1706
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
1700
- val windowMetrics = mActivity!!.windowManager.currentWindowMetrics
1701
- val insets = windowMetrics.windowInsets
1702
- .getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
1703
- val bounds = windowMetrics.bounds
1704
- val widthPixels = bounds.width() - insets.left - insets.right
1705
- outMetrics.widthPixels = widthPixels
1706
- outMetrics.density = mActivity!!.resources.displayMetrics.density
1707
+ mActivity?.windowManager?.currentWindowMetrics?.let { windowMetrics ->
1708
+ val insets = windowMetrics.windowInsets
1709
+ .getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
1710
+ val bounds = windowMetrics.bounds
1711
+ val widthPixels = bounds.width() - insets.left - insets.right
1712
+ outMetrics.widthPixels = widthPixels
1713
+ outMetrics.density = mActivity!!.resources.displayMetrics.density
1714
+ }
1707
1715
  } else {
1708
1716
  @Suppress("DEPRECATION")
1709
1717
  mActivity?.windowManager?.defaultDisplay?.getMetrics(outMetrics)
@@ -1711,37 +1719,39 @@ class emiAdmobPlugin : CordovaPlugin() {
1711
1719
 
1712
1720
  val density = outMetrics.density
1713
1721
  val adWidthPixels =
1714
- if (bannerViewLayout != null && bannerViewLayout!!.width > 0) bannerViewLayout!!.width else outMetrics.widthPixels
1722
+ if ((bannerViewLayout?.width ?: 0) > 0) bannerViewLayout!!.width else outMetrics.widthPixels
1715
1723
  val adWidth = (adWidthPixels / density).toInt()
1716
1724
  return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(mActivity!!, adWidth)
1717
1725
  }
1718
1726
 
1719
-
1720
1727
  private val adWidth: Int
1721
1728
  get() {
1722
- val outMetrics = DisplayMetrics()
1729
+ if (mActivity == null) {
1730
+ throw IllegalStateException("mActivity is null. Cannot calculate adWidth.")
1731
+ }
1723
1732
 
1733
+ val outMetrics = DisplayMetrics()
1724
1734
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
1725
- val windowMetrics = mActivity!!.windowManager.currentWindowMetrics
1726
- val insets = windowMetrics.windowInsets
1727
- .getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
1728
- val bounds = windowMetrics.bounds
1729
- val widthPixels = bounds.width() - insets.left - insets.right
1730
- outMetrics.widthPixels = widthPixels
1731
- outMetrics.density = mActivity!!.resources.displayMetrics.density
1735
+ mActivity?.windowManager?.currentWindowMetrics?.let { windowMetrics ->
1736
+ val insets = windowMetrics.windowInsets
1737
+ .getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
1738
+ val bounds = windowMetrics.bounds
1739
+ val widthPixels = bounds.width() - insets.left - insets.right
1740
+ outMetrics.widthPixels = widthPixels
1741
+ outMetrics.density = mActivity!!.resources.displayMetrics.density
1742
+ }
1732
1743
  } else {
1733
1744
  @Suppress("DEPRECATION")
1734
- mActivity!!.windowManager.defaultDisplay.getMetrics(outMetrics)
1745
+ mActivity?.windowManager?.defaultDisplay?.getMetrics(outMetrics)
1735
1746
  }
1736
1747
 
1737
1748
  val density = outMetrics.density
1738
1749
  val adWidthPixels =
1739
- (if (bannerViewLayout != null && bannerViewLayout!!.width > 0) bannerViewLayout!!.width else outMetrics.widthPixels).toFloat()
1750
+ (bannerViewLayout?.width?.takeIf { it > 0 } ?: outMetrics.widthPixels).toFloat()
1740
1751
  return (adWidthPixels / density).toInt()
1741
1752
  }
1742
1753
 
1743
1754
 
1744
-
1745
1755
  private fun handleConsentForm() {
1746
1756
  if(mActivity != null) {
1747
1757
  if (consentInformation!!.isConsentFormAvailable) {
@@ -1935,6 +1945,11 @@ class emiAdmobPlugin : CordovaPlugin() {
1935
1945
  }
1936
1946
  }
1937
1947
 
1948
+ if (isEnabledKeyword) {
1949
+ setKeyword.split(",").forEach { keyword ->
1950
+ builder.addKeyword(keyword.trim())
1951
+ }
1952
+ }
1938
1953
 
1939
1954
  val bundleExtra = Bundle()
1940
1955
  // bundleExtra.putString("npa", this.Npa); DEPRECATED Beginning January 16, 2024
@@ -1969,6 +1984,13 @@ class emiAdmobPlugin : CordovaPlugin() {
1969
1984
  if (isCollapsible) {
1970
1985
  bundleExtra.putString("collapsible", this.collapsiblePos)
1971
1986
  }
1987
+
1988
+ if (isEnabledKeyword) {
1989
+ setKeyword.split(",").forEach { keyword ->
1990
+ builder.addKeyword(keyword.trim())
1991
+ }
1992
+ }
1993
+
1972
1994
  bundleExtra.putBoolean("is_designed_for_families", this.isSetTagForChildDirectedTreatment)
1973
1995
  bundleExtra.putBoolean("under_age_of_consent", this.isSetTagForUnderAgeOfConsent)
1974
1996
  bundleExtra.putString("max_ad_content_rating", this.isSetMaxAdContentRating)
@@ -2241,6 +2263,7 @@ class emiAdmobPlugin : CordovaPlugin() {
2241
2263
  }
2242
2264
 
2243
2265
 
2266
+
2244
2267
  private val view: View?
2245
2268
  get() {
2246
2269
  if (View::class.java.isAssignableFrom(CordovaWebView::class.java)) {