@capgo/capacitor-admob 8.0.15 → 8.1.1

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.
@@ -13,6 +13,6 @@ Pod::Spec.new do |s|
13
13
  s.source_files = 'ios/Sources/**/*.{swift,h,m,c,cc,mm,cpp}'
14
14
  s.ios.deployment_target = '15.0'
15
15
  s.dependency 'Capacitor'
16
- s.dependency 'Google-Mobile-Ads-SDK', '~> 11.0'
16
+ s.dependency 'Google-Mobile-Ads-SDK', '~> 13.1'
17
17
  s.swift_version = '5.1'
18
18
  end
package/Package.swift CHANGED
@@ -11,7 +11,7 @@ let package = Package(
11
11
  ],
12
12
  dependencies: [
13
13
  .package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "8.0.0"),
14
- .package(url: "https://github.com/googleads/swift-package-manager-google-mobile-ads.git", from: "11.13.0")
14
+ .package(url: "https://github.com/googleads/swift-package-manager-google-mobile-ads.git", from: "13.2.0")
15
15
  ],
16
16
  targets: [
17
17
  .target(
package/README.md CHANGED
@@ -7,6 +7,8 @@
7
7
  </div>
8
8
  AdMob SDK bridge for Capacitor apps
9
9
 
10
+ Android now uses the Google Mobile Ads Next Gen SDK, and iOS is aligned to Google Mobile Ads SDK `13.1.x`, while preserving the existing Capacitor-facing API.
11
+
10
12
  ## Documentation
11
13
 
12
14
  The most complete doc is available here: https://capgo.app/docs/plugins/admob/
@@ -5,13 +5,13 @@ ext {
5
5
  androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.1'
6
6
  androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.3.0'
7
7
  androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.7.0'
8
- googlePlayServicesAdsVersion = project.hasProperty('googlePlayServicesAdsVersion') ? rootProject.ext.googlePlayServicesAdsVersion : '23.0.0'
8
+ // Next Gen is currently published as a beta artifact; apps can override this property when pinning a newer vetted release.
9
+ googleMobileAdsNextGenVersion = project.hasProperty('googleMobileAdsNextGenVersion') ? rootProject.ext.googleMobileAdsNextGenVersion : '0.25.0-beta01'
9
10
  }
10
11
 
11
12
  buildscript {
12
13
  repositories {
13
14
  google()
14
- jcenter()
15
15
  mavenCentral()
16
16
  }
17
17
  dependencies {
@@ -56,7 +56,6 @@ kotlin {
56
56
 
57
57
  repositories {
58
58
  google()
59
- jcenter()
60
59
  mavenCentral()
61
60
  }
62
61
 
@@ -69,5 +68,5 @@ dependencies {
69
68
  testImplementation "junit:junit:$junitVersion"
70
69
  androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
71
70
  androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
72
- implementation "com.google.android.gms:play-services-ads:$googlePlayServicesAdsVersion"
71
+ implementation "com.google.android.libraries.ads.mobile.sdk:ads-mobile-sdk:$googleMobileAdsNextGenVersion"
73
72
  }
@@ -7,24 +7,30 @@ import admob.plus.capacitor.ads.RewardedInterstitial
7
7
  import admob.plus.core.GenericAd
8
8
  import admob.plus.core.Helper
9
9
  import android.app.Activity
10
+ import android.content.pm.PackageManager
10
11
  import com.getcapacitor.JSObject
11
12
  import com.getcapacitor.Plugin
12
13
  import com.getcapacitor.PluginCall
13
14
  import com.getcapacitor.PluginMethod
14
15
  import com.getcapacitor.annotation.CapacitorPlugin
15
- import com.google.android.gms.ads.MobileAds
16
- import com.google.android.gms.ads.initialization.InitializationStatus
16
+ import com.google.android.libraries.ads.mobile.sdk.MobileAds
17
+ import com.google.android.libraries.ads.mobile.sdk.common.RequestConfiguration
18
+ import com.google.android.libraries.ads.mobile.sdk.initialization.InitializationConfig
17
19
  import org.json.JSONException
18
20
  import org.json.JSONObject
19
21
 
20
22
  @CapacitorPlugin(name = "AdMobPlus")
21
23
  class AdMobPlusPlugin : Plugin(), Helper.Adapter {
22
- private val pluginVersion = "7.4.1"
24
+ private val pluginVersion = "8.0.15"
23
25
  private var helper: Helper? = null
26
+ @Volatile
27
+ private var mobileAdsInitialized = false
28
+ private var requestConfigurationOverride: RequestConfiguration? = null
29
+
24
30
  override fun load() {
25
31
  super.load()
26
32
  helper = Helper(this)
27
- ExecuteContext.Companion.plugin = this
33
+ ExecuteContext.plugin = this
28
34
  }
29
35
 
30
36
  @PluginMethod
@@ -47,7 +53,22 @@ class AdMobPlusPlugin : Plugin(), Helper.Adapter {
47
53
 
48
54
  @PluginMethod
49
55
  fun start(call: PluginCall) {
50
- MobileAds.initialize(context) { status: InitializationStatus? ->
56
+ if (mobileAdsInitialized) {
57
+ call.resolve()
58
+ return
59
+ }
60
+
61
+ val appId = getAppIdFromManifest()
62
+ if (appId == null) {
63
+ call.reject("AdMob App ID missing")
64
+ return
65
+ }
66
+
67
+ val initializationConfigBuilder = InitializationConfig.Builder(appId)
68
+ requestConfigurationOverride?.let(initializationConfigBuilder::setRequestConfiguration)
69
+
70
+ MobileAds.initialize(context, initializationConfigBuilder.build()) {
71
+ mobileAdsInitialized = true
51
72
  helper!!.configForTestLab()
52
73
  call.resolve()
53
74
  }
@@ -56,33 +77,44 @@ class AdMobPlusPlugin : Plugin(), Helper.Adapter {
56
77
  @PluginMethod
57
78
  fun configure(call: PluginCall) {
58
79
  val ctx = ExecuteContext(call)
80
+ if (rejectIfNotInitialized { ctx.reject(it) }) {
81
+ return
82
+ }
59
83
  ctx.configure(helper!!)
60
84
  }
61
85
 
62
86
  @PluginMethod
63
87
  fun configRequest(call: PluginCall) {
64
88
  val ctx = ExecuteContext(call)
65
- MobileAds.setRequestConfiguration(ctx.optRequestConfiguration())
66
- helper!!.configForTestLab()
89
+ val requestConfiguration = ctx.optRequestConfiguration()
90
+ requestConfigurationOverride = requestConfiguration
91
+ if (mobileAdsInitialized) {
92
+ MobileAds.setRequestConfiguration(requestConfiguration)
93
+ helper!!.configForTestLab()
94
+ }
67
95
  ctx.resolve()
68
96
  }
69
97
 
70
98
  @PluginMethod
71
99
  fun adCreate(call: PluginCall) {
72
100
  val ctx = ExecuteContext(call)
73
- getBridge().executeOnMainThread {
101
+ bridge.executeOnMainThread {
74
102
  val adClass = ctx.optString("cls")
75
103
  if (adClass == null) {
76
104
  ctx.reject("ad cls is missing")
77
105
  } else {
78
- when (adClass) {
106
+ val created = when (adClass) {
79
107
  "BannerAd" -> Banner(ctx)
80
108
  "InterstitialAd" -> Interstitial(ctx)
81
109
  "RewardedAd" -> Rewarded(ctx)
82
110
  "RewardedInterstitialAd" -> RewardedInterstitial(ctx)
83
- else -> ctx.reject("ad cls is not supported: $adClass")
111
+ else -> null
112
+ }
113
+ if (created == null) {
114
+ ctx.reject("ad cls is not supported: $adClass")
115
+ } else {
116
+ ctx.resolve()
84
117
  }
85
- ctx.resolve()
86
118
  }
87
119
  }
88
120
  }
@@ -90,7 +122,7 @@ class AdMobPlusPlugin : Plugin(), Helper.Adapter {
90
122
  @PluginMethod
91
123
  fun adIsLoaded(call: PluginCall) {
92
124
  val ctx = ExecuteContext(call)
93
- getBridge().executeOnMainThread {
125
+ bridge.executeOnMainThread {
94
126
  val ad = ctx.optAdOrError() as GenericAd?
95
127
  if (ad != null) {
96
128
  ctx.resolve(ad.isLoaded)
@@ -101,7 +133,10 @@ class AdMobPlusPlugin : Plugin(), Helper.Adapter {
101
133
  @PluginMethod
102
134
  fun adLoad(call: PluginCall) {
103
135
  val ctx = ExecuteContext(call)
104
- getBridge().executeOnMainThread {
136
+ if (rejectIfNotInitialized { ctx.reject(it) }) {
137
+ return
138
+ }
139
+ bridge.executeOnMainThread {
105
140
  val ad = ctx.optAdOrError() as GenericAd?
106
141
  ad?.load(ctx)
107
142
  }
@@ -110,7 +145,10 @@ class AdMobPlusPlugin : Plugin(), Helper.Adapter {
110
145
  @PluginMethod
111
146
  fun adShow(call: PluginCall) {
112
147
  val ctx = ExecuteContext(call)
113
- getBridge().executeOnMainThread {
148
+ if (rejectIfNotInitialized { ctx.reject(it) }) {
149
+ return
150
+ }
151
+ bridge.executeOnMainThread {
114
152
  val ad = ctx.optAdOrError() as GenericAd?
115
153
  if (ad != null) {
116
154
  if (ad.isLoaded) {
@@ -125,7 +163,7 @@ class AdMobPlusPlugin : Plugin(), Helper.Adapter {
125
163
  @PluginMethod
126
164
  fun adHide(call: PluginCall) {
127
165
  val ctx = ExecuteContext(call)
128
- getBridge().executeOnMainThread {
166
+ bridge.executeOnMainThread {
129
167
  val ad = ctx.optAdOrError() as GenericAd?
130
168
  ad?.hide(ctx)
131
169
  }
@@ -139,11 +177,13 @@ class AdMobPlusPlugin : Plugin(), Helper.Adapter {
139
177
  get() = getActivity()
140
178
 
141
179
  override fun emit(eventName: String?, data: Map<String?, Any?>?) {
142
- try {
143
- emit(eventName, JSObject.fromJSONObject(JSONObject(data)))
144
- } catch (e: JSONException) {
145
- e.printStackTrace()
180
+ val payload = JSObject()
181
+ data?.forEach { (key, value) ->
182
+ if (key != null) {
183
+ payload.put(key, value)
184
+ }
146
185
  }
186
+ emit(eventName, payload)
147
187
  }
148
188
 
149
189
  @PluginMethod
@@ -156,4 +196,31 @@ class AdMobPlusPlugin : Plugin(), Helper.Adapter {
156
196
  call.reject("Could not get plugin version", e)
157
197
  }
158
198
  }
199
+
200
+ private fun getAppIdFromManifest(): String? {
201
+ return try {
202
+ val appContext = context.applicationContext
203
+ val applicationInfo = appContext.packageManager.getApplicationInfo(
204
+ appContext.packageName,
205
+ PackageManager.GET_META_DATA
206
+ )
207
+ val metaData = applicationInfo.metaData ?: return null
208
+ metaData.getString(ADMOB_APP_ID_KEY)?.takeIf { it.isNotBlank() }
209
+ ?: metaData.getInt(ADMOB_APP_ID_KEY).takeIf { it != 0 }?.let(appContext::getString)
210
+ } catch (_: Exception) {
211
+ null
212
+ }
213
+ }
214
+
215
+ private fun rejectIfNotInitialized(reject: (String) -> Unit): Boolean {
216
+ if (mobileAdsInitialized) {
217
+ return false
218
+ }
219
+ reject("AdMob is not initialized. Call start() and wait for it to resolve.")
220
+ return true
221
+ }
222
+
223
+ private companion object {
224
+ private const val ADMOB_APP_ID_KEY = "com.google.android.gms.ads.APPLICATION_ID"
225
+ }
159
226
  }
@@ -64,7 +64,7 @@ class ExecuteContext internal constructor(private val call: PluginCall) : Contex
64
64
  return call.getString(name)
65
65
  }
66
66
 
67
- override fun optStringList(name: String): List<String?> {
67
+ override fun optStringList(name: String): List<String> {
68
68
  return jsonArray2stringList(call.getArray(name))
69
69
  }
70
70
 
@@ -77,12 +77,9 @@ class ExecuteContext internal constructor(private val call: PluginCall) : Contex
77
77
  }
78
78
 
79
79
  override fun resolve(data: Boolean) {
80
- try {
81
- call.resolve(JSObject("true"))
82
- } catch (e: JSONException) {
83
- e.printStackTrace()
84
- reject()
85
- }
80
+ val result = JSObject()
81
+ result.put("value", data)
82
+ call.resolve(result)
86
83
  }
87
84
 
88
85
  override fun reject(msg: String?) {
@@ -8,63 +8,74 @@ import admob.plus.core.GenericAd
8
8
  import admob.plus.core.Helper.Companion.getParentView
9
9
  import admob.plus.core.Helper.Companion.removeFromParentView
10
10
  import android.annotation.SuppressLint
11
+ import android.util.DisplayMetrics
11
12
  import android.view.Gravity
12
13
  import android.view.View
13
14
  import android.view.ViewGroup
14
15
  import android.webkit.WebView
15
16
  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
17
+ import com.google.android.libraries.ads.mobile.sdk.banner.AdSize
18
+ import com.google.android.libraries.ads.mobile.sdk.banner.AdView
19
+ import com.google.android.libraries.ads.mobile.sdk.banner.BannerAd
20
+ import com.google.android.libraries.ads.mobile.sdk.banner.BannerAdEventCallback
21
+ import com.google.android.libraries.ads.mobile.sdk.common.AdLoadCallback
20
22
  import java.util.Objects
21
23
 
22
24
  class Banner(ctx: ExecuteContext) : AdBase(ctx), GenericAd {
23
- private val adSize: AdSize
24
25
  private val gravity: Int
25
26
  private var adView: AdView? = null
27
+ private var loaded = false
26
28
 
27
29
  init {
28
- adSize = AdSize.SMART_BANNER
29
30
  gravity = if ("top" == ctx.optPosition()) Gravity.TOP else Gravity.BOTTOM
30
31
  }
31
32
 
32
33
  override val isLoaded: Boolean
33
- get() = adView != null
34
+ get() = loaded
34
35
 
35
36
  override fun load(ctx: Context?) {
37
+ val requestContext = ctx ?: return
38
+
36
39
  if (adView == null) {
37
40
  adView = AdView(activity)
38
- adView!!.adUnitId = adUnitId
39
- adView!!.setAdSize(adSize)
40
- adView!!.adListener = object : AdListener() {
41
- override fun onAdClicked() {
42
- emit(Generated.Events.BANNER_CLICK)
43
- }
41
+ }
44
42
 
45
- override fun onAdClosed() {
46
- emit(Generated.Events.BANNER_CLOSE)
47
- }
43
+ loaded = false
44
+ val adSize = resolveAdSize()
48
45
 
49
- override fun onAdFailedToLoad(error: LoadAdError) {
50
- emit(Generated.Events.BANNER_LOAD_FAIL, error)
51
- }
46
+ adView!!.loadAd(
47
+ requestContext.optBannerAdRequest(adUnitId, adSize, null),
48
+ object : AdLoadCallback<BannerAd> {
49
+ override fun onAdLoaded(bannerAd: BannerAd) {
50
+ loaded = true
51
+ bannerAd.adEventCallback = object : BannerAdEventCallback {
52
+ override fun onAdClicked() {
53
+ emit(Generated.Events.BANNER_CLICK)
54
+ }
52
55
 
53
- override fun onAdImpression() {
54
- emit(Generated.Events.BANNER_IMPRESSION)
55
- }
56
+ override fun onAdDismissedFullScreenContent() {
57
+ emit(Generated.Events.BANNER_CLOSE)
58
+ }
59
+
60
+ override fun onAdImpression() {
61
+ emit(Generated.Events.BANNER_IMPRESSION)
62
+ }
56
63
 
57
- override fun onAdLoaded() {
64
+ override fun onAdShowedFullScreenContent() {
65
+ emit(Generated.Events.BANNER_OPEN)
66
+ }
67
+ }
58
68
  emit(Generated.Events.BANNER_LOAD)
69
+ requestContext.resolve()
59
70
  }
60
71
 
61
- override fun onAdOpened() {
62
- emit(Generated.Events.BANNER_OPEN)
72
+ override fun onAdFailedToLoad(loadAdError: com.google.android.libraries.ads.mobile.sdk.common.LoadAdError) {
73
+ loaded = false
74
+ emit(Generated.Events.BANNER_LOAD_FAIL, loadAdError)
75
+ requestContext.reject(loadAdError)
63
76
  }
64
77
  }
65
- }
66
- adView!!.loadAd(ctx!!.optAdRequest())
67
- ctx.resolve()
78
+ )
68
79
  }
69
80
 
70
81
  override fun show(ctx: Context?) {
@@ -73,7 +84,6 @@ class Banner(ctx: ExecuteContext) : AdBase(ctx), GenericAd {
73
84
  if (getParentView(adView) == null) {
74
85
  addBannerView(ExecuteContext.Companion.plugin, adView)
75
86
  } else if (adView!!.visibility == View.GONE) {
76
- adView!!.resume()
77
87
  adView!!.visibility = View.VISIBLE
78
88
  } else {
79
89
  val wvParentView = getParentView(webView)
@@ -88,7 +98,6 @@ class Banner(ctx: ExecuteContext) : AdBase(ctx), GenericAd {
88
98
 
89
99
  override fun hide(ctx: Context?) {
90
100
  if (adView != null) {
91
- adView!!.pause()
92
101
  adView!!.visibility = View.GONE
93
102
  }
94
103
  ctx!!.resolve()
@@ -96,6 +105,7 @@ class Banner(ctx: ExecuteContext) : AdBase(ctx), GenericAd {
96
105
 
97
106
  override fun destroy() {
98
107
  if (adView != null) {
108
+ loaded = false
99
109
  adView!!.destroy()
100
110
  adView = null
101
111
  }
@@ -108,7 +118,7 @@ class Banner(ctx: ExecuteContext) : AdBase(ctx), GenericAd {
108
118
  if (parentView == null) {
109
119
  parentView = LinearLayout(webView.context)
110
120
  }
111
- if (wvParentView != null && wvParentView !== parentView) {
121
+ if (wvParentView !== parentView) {
112
122
  if (getParentView(parentView) != null) {
113
123
  parentView!!.removeAllViews()
114
124
  removeFromParentView(parentView)
@@ -141,6 +151,12 @@ class Banner(ctx: ExecuteContext) : AdBase(ctx), GenericAd {
141
151
  parentView!!.requestFocus()
142
152
  }
143
153
 
154
+ private fun resolveAdSize(): AdSize {
155
+ val displayMetrics: DisplayMetrics = activity.resources.displayMetrics
156
+ val adWidth = (displayMetrics.widthPixels / displayMetrics.density).toInt().coerceAtLeast(1)
157
+ return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(activity, adWidth)
158
+ }
159
+
144
160
  companion object {
145
161
  @SuppressLint("StaticFieldLeak")
146
162
  private var parentView: ViewGroup? = null
@@ -4,11 +4,9 @@ import admob.plus.capacitor.ExecuteContext
4
4
  import admob.plus.capacitor.Generated
5
5
  import admob.plus.core.Context
6
6
  import admob.plus.core.GenericAd
7
- import com.google.android.gms.ads.AdError
8
- import com.google.android.gms.ads.FullScreenContentCallback
9
- import com.google.android.gms.ads.LoadAdError
10
- import com.google.android.gms.ads.interstitial.InterstitialAd
11
- import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
7
+ import com.google.android.libraries.ads.mobile.sdk.common.AdLoadCallback
8
+ import com.google.android.libraries.ads.mobile.sdk.interstitial.InterstitialAd
9
+ import com.google.android.libraries.ads.mobile.sdk.interstitial.InterstitialAdEventCallback
12
10
 
13
11
  class Interstitial(ctx: ExecuteContext?) : AdBase(ctx), GenericAd {
14
12
  private var mAd: InterstitialAd? = null
@@ -18,22 +16,22 @@ class Interstitial(ctx: ExecuteContext?) : AdBase(ctx), GenericAd {
18
16
  }
19
17
 
20
18
  override fun load(ctx: Context?) {
19
+ val requestContext = ctx ?: return
21
20
  clear()
22
21
  InterstitialAd.load(
23
- activity,
24
- adUnitId,
25
- ctx!!.optAdRequest(),
26
- object : InterstitialAdLoadCallback() {
22
+ requestContext.optAdRequest(adUnitId),
23
+ object : AdLoadCallback<InterstitialAd> {
27
24
  override fun onAdLoaded(interstitialAd: InterstitialAd) {
28
25
  mAd = interstitialAd
29
- mAd!!.fullScreenContentCallback = object : FullScreenContentCallback() {
26
+ mAd!!.adEventCallback = object : InterstitialAdEventCallback {
30
27
  override fun onAdDismissedFullScreenContent() {
31
28
  clear()
32
29
  emit(Generated.Events.INTERSTITIAL_DISMISS)
33
30
  }
34
31
 
35
- override fun onAdFailedToShowFullScreenContent(adError: AdError) {
36
- emit(Generated.Events.INTERSTITIAL_SHOW_FAIL, adError)
32
+ override fun onAdFailedToShowFullScreenContent(error: com.google.android.libraries.ads.mobile.sdk.common.FullScreenContentError) {
33
+ clear()
34
+ emit(Generated.Events.INTERSTITIAL_SHOW_FAIL, error)
37
35
  }
38
36
 
39
37
  override fun onAdShowedFullScreenContent() {
@@ -43,15 +41,19 @@ class Interstitial(ctx: ExecuteContext?) : AdBase(ctx), GenericAd {
43
41
  override fun onAdImpression() {
44
42
  emit(Generated.Events.INTERSTITIAL_IMPRESSION)
45
43
  }
44
+
45
+ override fun onAdClicked() {
46
+ emit(Generated.Events.AD_CLICK)
47
+ }
46
48
  }
47
49
  emit(Generated.Events.INTERSTITIAL_LOAD)
48
- ctx.resolve()
50
+ requestContext.resolve()
49
51
  }
50
52
 
51
- override fun onAdFailedToLoad(loadAdError: LoadAdError) {
53
+ override fun onAdFailedToLoad(loadAdError: com.google.android.libraries.ads.mobile.sdk.common.LoadAdError) {
52
54
  clear()
53
55
  emit(Generated.Events.INTERSTITIAL_LOAD_FAIL, loadAdError)
54
- ctx.reject(loadAdError.message)
56
+ requestContext.reject(loadAdError)
55
57
  }
56
58
  })
57
59
  }
@@ -61,13 +63,10 @@ class Interstitial(ctx: ExecuteContext?) : AdBase(ctx), GenericAd {
61
63
 
62
64
  override fun show(ctx: Context?) {
63
65
  mAd!!.show(activity)
64
- ctx!!.resolve()
66
+ ctx?.resolve()
65
67
  }
66
68
 
67
69
  private fun clear() {
68
- if (mAd != null) {
69
- mAd!!.fullScreenContentCallback = null
70
- mAd = null
71
- }
70
+ mAd = null
72
71
  }
73
72
  }
@@ -2,14 +2,13 @@ package admob.plus.capacitor.ads
2
2
 
3
3
  import admob.plus.capacitor.ExecuteContext
4
4
  import admob.plus.capacitor.Generated
5
+ import android.util.Log
5
6
  import admob.plus.core.Context
6
7
  import admob.plus.core.GenericAd
7
- import com.google.android.gms.ads.AdError
8
- import com.google.android.gms.ads.FullScreenContentCallback
9
- import com.google.android.gms.ads.LoadAdError
10
- import com.google.android.gms.ads.rewarded.RewardItem
11
- import com.google.android.gms.ads.rewarded.RewardedAd
12
- import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback
8
+ import com.google.android.libraries.ads.mobile.sdk.common.AdLoadCallback
9
+ import com.google.android.libraries.ads.mobile.sdk.rewarded.RewardItem
10
+ import com.google.android.libraries.ads.mobile.sdk.rewarded.RewardedAd
11
+ import com.google.android.libraries.ads.mobile.sdk.rewarded.RewardedAdEventCallback
13
12
 
14
13
  class Rewarded(ctx: ExecuteContext?) : AdBase(ctx), GenericAd {
15
14
  private var mAd: RewardedAd? = null
@@ -19,32 +18,32 @@ class Rewarded(ctx: ExecuteContext?) : AdBase(ctx), GenericAd {
19
18
  }
20
19
 
21
20
  override fun load(ctx: Context?) {
21
+ val requestContext = ctx ?: return
22
22
  clear()
23
23
  RewardedAd.load(
24
- activity,
25
- adUnitId,
26
- ctx!!.optAdRequest(),
27
- object : RewardedAdLoadCallback() {
28
- override fun onAdFailedToLoad(loadAdError: LoadAdError) {
24
+ requestContext.optAdRequest(adUnitId),
25
+ object : AdLoadCallback<RewardedAd> {
26
+ override fun onAdFailedToLoad(loadAdError: com.google.android.libraries.ads.mobile.sdk.common.LoadAdError) {
29
27
  clear()
30
28
  emit(Generated.Events.REWARDED_LOAD_FAIL, loadAdError)
31
- ctx.reject(loadAdError)
29
+ requestContext.reject(loadAdError)
32
30
  }
33
31
 
34
32
  override fun onAdLoaded(rewardedAd: RewardedAd) {
35
33
  mAd = rewardedAd
36
- val ssv = ctx.optServerSideVerificationOptions()
34
+ val ssv = requestContext.optServerSideVerificationOptions()
37
35
  if (ssv != null) {
38
36
  mAd!!.setServerSideVerificationOptions(ssv)
39
37
  }
40
- mAd!!.fullScreenContentCallback = object : FullScreenContentCallback() {
38
+ mAd!!.adEventCallback = object : RewardedAdEventCallback {
41
39
  override fun onAdDismissedFullScreenContent() {
42
40
  clear()
43
41
  emit(Generated.Events.REWARDED_DISMISS)
44
42
  }
45
43
 
46
- override fun onAdFailedToShowFullScreenContent(adError: AdError) {
47
- emit(Generated.Events.REWARDED_SHOW_FAIL, adError)
44
+ override fun onAdFailedToShowFullScreenContent(error: com.google.android.libraries.ads.mobile.sdk.common.FullScreenContentError) {
45
+ clear()
46
+ emit(Generated.Events.REWARDED_SHOW_FAIL, error)
48
47
  }
49
48
 
50
49
  override fun onAdShowedFullScreenContent() {
@@ -54,9 +53,13 @@ class Rewarded(ctx: ExecuteContext?) : AdBase(ctx), GenericAd {
54
53
  override fun onAdImpression() {
55
54
  emit(Generated.Events.REWARDED_IMPRESSION)
56
55
  }
56
+
57
+ override fun onAdClicked() {
58
+ emit(Generated.Events.AD_CLICK)
59
+ }
57
60
  }
58
61
  emit(Generated.Events.REWARDED_LOAD)
59
- ctx.resolve()
62
+ requestContext.resolve()
60
63
  }
61
64
  })
62
65
  }
@@ -65,19 +68,21 @@ class Rewarded(ctx: ExecuteContext?) : AdBase(ctx), GenericAd {
65
68
  get() = mAd != null
66
69
 
67
70
  override fun show(ctx: Context?) {
68
- mAd!!.show(activity) { rewardItem: RewardItem? ->
69
- emit(
70
- Generated.Events.REWARDED_REWARD,
71
- rewardItem!!
72
- )
71
+ val ad = mAd ?: run {
72
+ ctx?.reject("ad is not loaded")
73
+ return
73
74
  }
74
- ctx!!.resolve()
75
+ ad.show(activity) { rewardItem: RewardItem? ->
76
+ if (rewardItem != null) {
77
+ emit(Generated.Events.REWARDED_REWARD, rewardItem)
78
+ } else {
79
+ Log.w("Rewarded", "Reward callback invoked with null rewardItem for adUnitId=$adUnitId")
80
+ }
81
+ }
82
+ ctx?.resolve()
75
83
  }
76
84
 
77
85
  private fun clear() {
78
- if (mAd != null) {
79
- mAd!!.fullScreenContentCallback = null
80
- mAd = null
81
- }
86
+ mAd = null
82
87
  }
83
88
  }