react-native-google-mobile-ads 14.7.1 → 14.8.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.
Files changed (147) hide show
  1. package/README.md +86 -1
  2. package/android/build.gradle +10 -0
  3. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeAppModule.java +2 -2
  4. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsConsentModule.java +2 -2
  5. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsMediaView.kt +57 -0
  6. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsMediaViewManager.kt +55 -0
  7. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsModule.kt +5 -4
  8. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsNativeAdView.kt +107 -0
  9. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsNativeAdViewManager.kt +85 -0
  10. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsNativeModule.kt +211 -0
  11. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsPackage.kt +102 -13
  12. package/android/src/oldarch/com/facebook/react/viewmanagers/RNGoogleMobileAdsMediaViewManagerDelegate.java +29 -0
  13. package/android/src/oldarch/com/facebook/react/viewmanagers/RNGoogleMobileAdsMediaViewManagerInterface.java +10 -0
  14. package/android/src/oldarch/com/facebook/react/viewmanagers/RNGoogleMobileAdsNativeViewManagerDelegate.java +33 -0
  15. package/android/src/oldarch/com/facebook/react/viewmanagers/RNGoogleMobileAdsNativeViewManagerInterface.java +10 -0
  16. package/android/src/oldarch/io/invertase/googlemobileads/NativeGoogleMobileAdsNativeModuleSpec.kt +18 -0
  17. package/docs/displaying-ads.mdx +4 -0
  18. package/docs/european-user-consent.mdx +2 -0
  19. package/docs/index.mdx +5 -4
  20. package/docs/native-ads.mdx +365 -0
  21. package/docs.json +1 -0
  22. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsMediaView.h +49 -0
  23. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsMediaView.mm +152 -0
  24. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsNativeModule.h +35 -0
  25. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsNativeModule.mm +290 -0
  26. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsNativeView.h +48 -0
  27. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsNativeView.mm +218 -0
  28. package/jest.setup.ts +7 -0
  29. package/lib/commonjs/NativeAdEventType.js +19 -0
  30. package/lib/commonjs/NativeAdEventType.js.map +1 -0
  31. package/lib/commonjs/TestIds.js +15 -8
  32. package/lib/commonjs/TestIds.js.map +1 -1
  33. package/lib/commonjs/ads/native-ad/NativeAd.js +122 -0
  34. package/lib/commonjs/ads/native-ad/NativeAd.js.map +1 -0
  35. package/lib/commonjs/ads/native-ad/NativeAdContext.js +26 -0
  36. package/lib/commonjs/ads/native-ad/NativeAdContext.js.map +1 -0
  37. package/lib/commonjs/ads/native-ad/NativeAdView.js +48 -0
  38. package/lib/commonjs/ads/native-ad/NativeAdView.js.map +1 -0
  39. package/lib/commonjs/ads/native-ad/NativeAsset.js +71 -0
  40. package/lib/commonjs/ads/native-ad/NativeAsset.js.map +1 -0
  41. package/lib/commonjs/ads/native-ad/NativeMediaView.js +58 -0
  42. package/lib/commonjs/ads/native-ad/NativeMediaView.js.map +1 -0
  43. package/lib/commonjs/common/ref.js +45 -0
  44. package/lib/commonjs/common/ref.js.map +1 -0
  45. package/lib/commonjs/index.js +47 -0
  46. package/lib/commonjs/index.js.map +1 -1
  47. package/lib/commonjs/specs/components/GoogleMobileAdsBannerViewNativeComponent.js +17 -0
  48. package/lib/commonjs/specs/components/GoogleMobileAdsBannerViewNativeComponent.js.map +1 -1
  49. package/lib/commonjs/specs/components/GoogleMobileAdsMediaViewNativeComponent.js +26 -0
  50. package/lib/commonjs/specs/components/GoogleMobileAdsMediaViewNativeComponent.js.map +1 -0
  51. package/lib/commonjs/specs/components/GoogleMobileAdsNativeViewNativeComponent.js +31 -0
  52. package/lib/commonjs/specs/components/GoogleMobileAdsNativeViewNativeComponent.js.map +1 -0
  53. package/lib/commonjs/specs/modules/NativeConsentModule.js.map +1 -1
  54. package/lib/commonjs/specs/modules/NativeGoogleMobileAdsModule.js +1 -1
  55. package/lib/commonjs/specs/modules/NativeGoogleMobileAdsNativeModule.js +25 -0
  56. package/lib/commonjs/specs/modules/NativeGoogleMobileAdsNativeModule.js.map +1 -0
  57. package/lib/commonjs/types/NativeAdRequestOptions.js +37 -0
  58. package/lib/commonjs/types/NativeAdRequestOptions.js.map +1 -0
  59. package/lib/commonjs/types/index.js +11 -0
  60. package/lib/commonjs/types/index.js.map +1 -1
  61. package/lib/commonjs/version.js +1 -1
  62. package/lib/module/NativeAdEventType.js +13 -0
  63. package/lib/module/NativeAdEventType.js.map +1 -0
  64. package/lib/module/TestIds.js +15 -8
  65. package/lib/module/TestIds.js.map +1 -1
  66. package/lib/module/ads/native-ad/NativeAd.js +115 -0
  67. package/lib/module/ads/native-ad/NativeAd.js.map +1 -0
  68. package/lib/module/ads/native-ad/NativeAdContext.js +20 -0
  69. package/lib/module/ads/native-ad/NativeAdContext.js.map +1 -0
  70. package/lib/module/ads/native-ad/NativeAdView.js +40 -0
  71. package/lib/module/ads/native-ad/NativeAdView.js.map +1 -0
  72. package/lib/module/ads/native-ad/NativeAsset.js +63 -0
  73. package/lib/module/ads/native-ad/NativeAsset.js.map +1 -0
  74. package/lib/module/ads/native-ad/NativeMediaView.js +50 -0
  75. package/lib/module/ads/native-ad/NativeMediaView.js.map +1 -0
  76. package/lib/module/common/ref.js +38 -0
  77. package/lib/module/common/ref.js.map +1 -0
  78. package/lib/module/index.js +5 -0
  79. package/lib/module/index.js.map +1 -1
  80. package/lib/module/specs/components/GoogleMobileAdsBannerViewNativeComponent.js +17 -0
  81. package/lib/module/specs/components/GoogleMobileAdsBannerViewNativeComponent.js.map +1 -1
  82. package/lib/module/specs/components/GoogleMobileAdsMediaViewNativeComponent.js +20 -0
  83. package/lib/module/specs/components/GoogleMobileAdsMediaViewNativeComponent.js.map +1 -0
  84. package/lib/module/specs/components/GoogleMobileAdsNativeViewNativeComponent.js +24 -0
  85. package/lib/module/specs/components/GoogleMobileAdsNativeViewNativeComponent.js.map +1 -0
  86. package/lib/module/specs/modules/NativeConsentModule.js.map +1 -1
  87. package/lib/module/specs/modules/NativeGoogleMobileAdsModule.js +1 -1
  88. package/lib/module/specs/modules/NativeGoogleMobileAdsNativeModule.js +20 -0
  89. package/lib/module/specs/modules/NativeGoogleMobileAdsNativeModule.js.map +1 -0
  90. package/lib/module/types/NativeAdRequestOptions.js +32 -0
  91. package/lib/module/types/NativeAdRequestOptions.js.map +1 -0
  92. package/lib/module/types/index.js +1 -0
  93. package/lib/module/types/index.js.map +1 -1
  94. package/lib/module/version.js +1 -1
  95. package/lib/typescript/NativeAdEventType.d.ts +39 -0
  96. package/lib/typescript/NativeAdEventType.d.ts.map +1 -0
  97. package/lib/typescript/TestIds.d.ts +3 -0
  98. package/lib/typescript/TestIds.d.ts.map +1 -1
  99. package/lib/typescript/ads/native-ad/NativeAd.d.ts +46 -0
  100. package/lib/typescript/ads/native-ad/NativeAd.d.ts.map +1 -0
  101. package/lib/typescript/ads/native-ad/NativeAdContext.d.ts +10 -0
  102. package/lib/typescript/ads/native-ad/NativeAdContext.d.ts.map +1 -0
  103. package/lib/typescript/ads/native-ad/NativeAdView.d.ts +8 -0
  104. package/lib/typescript/ads/native-ad/NativeAdView.d.ts.map +1 -0
  105. package/lib/typescript/ads/native-ad/NativeAsset.d.ts +18 -0
  106. package/lib/typescript/ads/native-ad/NativeAsset.d.ts.map +1 -0
  107. package/lib/typescript/ads/native-ad/NativeMediaView.d.ts +7 -0
  108. package/lib/typescript/ads/native-ad/NativeMediaView.d.ts.map +1 -0
  109. package/lib/typescript/common/ref.d.ts +13 -0
  110. package/lib/typescript/common/ref.d.ts.map +1 -0
  111. package/lib/typescript/index.d.ts +6 -1
  112. package/lib/typescript/index.d.ts.map +1 -1
  113. package/lib/typescript/specs/components/GoogleMobileAdsBannerViewNativeComponent.d.ts.map +1 -1
  114. package/lib/typescript/specs/components/GoogleMobileAdsMediaViewNativeComponent.d.ts +9 -0
  115. package/lib/typescript/specs/components/GoogleMobileAdsMediaViewNativeComponent.d.ts.map +1 -0
  116. package/lib/typescript/specs/components/GoogleMobileAdsNativeViewNativeComponent.d.ts +14 -0
  117. package/lib/typescript/specs/components/GoogleMobileAdsNativeViewNativeComponent.d.ts.map +1 -0
  118. package/lib/typescript/specs/modules/NativeConsentModule.d.ts +3 -4
  119. package/lib/typescript/specs/modules/NativeConsentModule.d.ts.map +1 -1
  120. package/lib/typescript/specs/modules/NativeGoogleMobileAdsModule.d.ts +1 -17
  121. package/lib/typescript/specs/modules/NativeGoogleMobileAdsModule.d.ts.map +1 -1
  122. package/lib/typescript/specs/modules/NativeGoogleMobileAdsNativeModule.d.ts +37 -0
  123. package/lib/typescript/specs/modules/NativeGoogleMobileAdsNativeModule.d.ts.map +1 -0
  124. package/lib/typescript/types/NativeAdRequestOptions.d.ts +36 -0
  125. package/lib/typescript/types/NativeAdRequestOptions.d.ts.map +1 -0
  126. package/lib/typescript/types/index.d.ts +1 -0
  127. package/lib/typescript/types/index.d.ts.map +1 -1
  128. package/lib/typescript/version.d.ts +1 -1
  129. package/package.json +1 -1
  130. package/src/NativeAdEventType.ts +46 -0
  131. package/src/TestIds.ts +13 -6
  132. package/src/ads/native-ad/NativeAd.ts +152 -0
  133. package/src/ads/native-ad/NativeAdContext.ts +26 -0
  134. package/src/ads/native-ad/NativeAdView.tsx +43 -0
  135. package/src/ads/native-ad/NativeAsset.tsx +66 -0
  136. package/src/ads/native-ad/NativeMediaView.tsx +44 -0
  137. package/src/common/ref.ts +44 -0
  138. package/src/index.ts +5 -0
  139. package/src/specs/components/GoogleMobileAdsBannerViewNativeComponent.ts +17 -0
  140. package/src/specs/components/GoogleMobileAdsMediaViewNativeComponent.ts +30 -0
  141. package/src/specs/components/GoogleMobileAdsNativeViewNativeComponent.ts +44 -0
  142. package/src/specs/modules/NativeConsentModule.ts +3 -4
  143. package/src/specs/modules/NativeGoogleMobileAdsModule.ts +2 -2
  144. package/src/specs/modules/NativeGoogleMobileAdsNativeModule.ts +64 -0
  145. package/src/types/NativeAdRequestOptions.ts +55 -0
  146. package/src/types/index.ts +1 -0
  147. package/src/version.ts +1 -1
package/README.md CHANGED
@@ -30,22 +30,107 @@ React Native Google Mobile Ads is built with three key principals in mind;
30
30
  - 📄 **Well documented**
31
31
  - full reference & installation documentation alongside detailed guides and FAQs
32
32
 
33
+ ## Ad formats
34
+
35
+ ### App open
36
+
37
+ App open ads are a special ad format intended for publishers wishing to monetize their app load screens.
38
+
39
+ <img width="200" src="https://developers.google.com/static/admob/images/app-open-ad.png" alt="App open ad">
40
+
41
+ [Learn More](https://docs.page/invertase/react-native-google-mobile-ads/displaying-ads#app-open-ads)
42
+
43
+ ### Banner
44
+
45
+ Banner ad units display rectangular ads that occupy a portion of an app's layout.
46
+ They stay on screen while users are interacting with the app, either anchored at the top or bottom of the screen or inline with content as the user scrolls.
47
+ Banner ads can refresh automatically after a certain period of time.
48
+
49
+ #### Anchored adaptive
50
+
51
+ A dynamically sized banner that is full-width and auto-height. Anchored adaptive banners are expected to be always on-screen, locked to the screen’s top or bottom.
52
+
53
+ <img width="200" src="https://developers.google.com/static/admob/images/Android_adaptive.png" alt="Anchored adaptive">
54
+
55
+ [Learn More](https://docs.page/invertase/react-native-google-mobile-ads/displaying-ads#banner-ads-component)
56
+
57
+ #### Inline adaptive
58
+
59
+ Inline adaptive banners are larger, taller banners compared to anchored adaptive banners.
60
+ They are of variable height, and can be as tall as the device screen.
61
+ They are intended to be placed in scrolling content.
62
+
63
+ <img width="600" src="https://developers.google.com/static/admob/images/inline-adaptive.png" alt="Inline adaptive">
64
+
65
+ [Learn More](https://docs.page/invertase/react-native-google-mobile-ads/displaying-ads#banner-ads-component)
66
+
67
+ #### Collapsible
68
+
69
+ Collapsible banner ads are intended to improve performance of anchored ads that are otherwise a smaller size.
70
+
71
+ <img width="400" src="https://developers.google.com/static/admob/images/collapsible-banner.png" alt="Collapsible banner">
72
+
73
+ [Learn More](https://docs.page/invertase/react-native-google-mobile-ads/displaying-ads#collapsible-banner-ads)
74
+
75
+ #### Fixed size (legacy)
76
+
77
+ The Google Mobile Ads SDK supports fixed ad sizes for situations where adaptive banners ads don't meet your needs.
78
+ Banner (320x50), Large banner (320x100), Medium rectangle (300x250), full banner (468x60) and leaderboard (728x90).
79
+
80
+ [Learn More](https://docs.page/invertase/react-native-google-mobile-ads/displaying-ads#banner-ads-component)
81
+
82
+ ### Native
83
+
84
+ Native ads allow you to customize the look and feel of the ads that appear in your app.
85
+ You decide how and where they're placed, so the layout is more consistent your app's design.
86
+
87
+ <img width="300" src="https://developers.google.com/static/admob/images/format-native.svg" alt="Native">
88
+
89
+ [Learn More](https://docs.page/invertase/react-native-google-mobile-ads/native-ads)
90
+
91
+ ### Interstitial
92
+
93
+ Interstitial ad units show full-page ads in your app. Place them at natural breaks & transitions in your app's interface, such as after level completion in a gaming app.
94
+
95
+ <img width="300" src="https://developers.google.com/static/admob/images/format-interstitial.svg" alt="Interstitial">
96
+
97
+ [Learn More](https://docs.page/invertase/react-native-google-mobile-ads/displaying-ads#interstitial-ads)
98
+
99
+ ### Rewarded
100
+
101
+ AdMob rewarded ad units allow you to reward users with in-app items for interacting with video ads, playable ads, and surveys.
102
+
103
+ <img width="300" src="https://developers.google.com/static/admob/images/format-rewarded.svg" alt="Rewarded">
104
+
105
+ [Learn More](https://docs.page/invertase/react-native-google-mobile-ads/displaying-ads#rewarded-ads)
106
+
107
+ ### Rewarded Interstitial
108
+
109
+ Rewarded interstitial is a type of incentivized ad format that allows you offer rewards for ads that appear automatically during natural app transitions.
110
+ Unlike rewarded ads, users aren't required to opt-in to view a rewarded interstitial.
111
+
112
+ <img width="300" src="https://developers.google.com/static/admob/images/format-rewarded-interstitial.svg" alt="Rewarded interstitial">
113
+
114
+ [Learn More](https://docs.page/invertase/react-native-google-mobile-ads/displaying-ads#rewarded-interstitial-ads)
115
+
33
116
  ## Migrating to the New Architecture Status (backwards compatible)
34
117
 
35
118
  This package can be used in both The Old and [The New Architecture](https://reactnative.dev/docs/the-new-architecture/landing-page).
36
119
  When using The New Architecture, some legacy code will still be used though. See status below:
37
120
 
38
121
  | Platform | Feature | Status |
39
- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- |
122
+ | -------- |----------------------------------------------------------------------------------------------------------------------------------------------------------------| ----------- |
40
123
  | iOS | Mobile Ads SDK Methods (Turbo Native Module) | ✅ Complete |
41
124
  | iOS | Banners (Fabric Native Component) | ✅ Complete |
42
125
  | iOS | Full Screen Ads (Turbo Native Module) | ✅ Complete |
126
+ | iOS | Native Ads (Turbo Native Module, Fabric Native Component) | ✅ Complete |
43
127
  | iOS | User Messaging Platform (Turbo Native Module) | ✅ Complete |
44
128
  | iOS | [EventEmitter](https://github.com/reactwg/react-native-new-architecture/blob/main/docs/turbo-modules.md#add-event-emitting-capabilities) (Turbo Native Module) | ⏳ To-Do |
45
129
  | iOS | Revenue Precision Constants (Turbo Native Module) | ✅ Complete |
46
130
  | Android | Mobile Ads SDK Methods (Turbo Native Module) | ⏳ To-Do |
47
131
  | Android | Banners (Fabric Native Component) | ⏳ To-Do |
48
132
  | Android | Full Screen Ads (Turbo Native Module) | ⏳ To-Do |
133
+ | Android | Native Ads (Turbo Native Module, Fabric Native Component) | ✅ Complete |
49
134
  | Android | User Messaging Platform (Turbo Native Module) | ⏳ To-Do |
50
135
  | Android | [EventEmitter](https://github.com/reactwg/react-native-new-architecture/blob/main/docs/turbo-modules.md#add-event-emitting-capabilities) (Turbo Native Module) | ⏳ To-Do |
51
136
  | Android | Revenue Precision Constants (Turbo Native Module) | ⏳ To-Do |
@@ -108,6 +108,16 @@ android {
108
108
  appJSONGoogleMobileAdsOptimizeAdLoading : appJSONGoogleMobileAdsOptimizeAdLoadingBool
109
109
  ]
110
110
  buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
111
+
112
+ sourceSets {
113
+ main {
114
+ if (isNewArchitectureEnabled()) {
115
+ java.srcDirs += ['src/newarch']
116
+ } else {
117
+ java.srcDirs += ['src/oldarch']
118
+ }
119
+ }
120
+ }
111
121
  }
112
122
  lintOptions {
113
123
  disable 'GradleCompatible'
@@ -30,10 +30,10 @@ import io.invertase.googlemobileads.common.ReactNativeModule;
30
30
  import io.invertase.googlemobileads.common.ReactNativePreferences;
31
31
 
32
32
  public class ReactNativeAppModule extends ReactNativeModule {
33
- private static final String TAG = "RNAppModule";
33
+ static final String NAME = "RNAppModule";
34
34
 
35
35
  ReactNativeAppModule(ReactApplicationContext reactContext) {
36
- super(reactContext, TAG);
36
+ super(reactContext, NAME);
37
37
  }
38
38
 
39
39
  @Override
@@ -35,11 +35,11 @@ import javax.annotation.Nonnull;
35
35
 
36
36
  public class ReactNativeGoogleMobileAdsConsentModule extends ReactNativeModule {
37
37
 
38
- private static final String TAG = "RNGoogleMobileAdsConsentModule";
38
+ static final String NAME = "RNGoogleMobileAdsConsentModule";
39
39
  private ConsentInformation consentInformation;
40
40
 
41
41
  public ReactNativeGoogleMobileAdsConsentModule(ReactApplicationContext reactContext) {
42
- super(reactContext, TAG);
42
+ super(reactContext, NAME);
43
43
  consentInformation = UserMessagingPlatform.getConsentInformation(reactContext);
44
44
  }
45
45
 
@@ -0,0 +1,57 @@
1
+ package io.invertase.googlemobileads
2
+
3
+ /*
4
+ * Copyright (c) 2016-present Invertase Limited & Contributors
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this library except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ *
18
+ */
19
+
20
+ import android.annotation.SuppressLint
21
+ import android.widget.ImageView
22
+ import com.facebook.react.bridge.ReactContext
23
+ import com.google.android.gms.ads.nativead.MediaView
24
+
25
+ @SuppressLint("ViewConstructor")
26
+ class ReactNativeGoogleMobileAdsMediaView(
27
+ private val context: ReactContext
28
+ ): MediaView(context) {
29
+ fun setResponseId(responseId: String?) {
30
+ val nativeModule = context.getNativeModule(ReactNativeGoogleMobileAdsNativeModule::class.java)
31
+ nativeModule?.getNativeAd(responseId ?: "")?.let {
32
+ this.mediaContent = it.mediaContent
33
+ requestLayout()
34
+ }
35
+ }
36
+
37
+ fun setResizeMode(resizeMode: String?) {
38
+ when (resizeMode) {
39
+ "cover" -> setImageScaleType(ImageView.ScaleType.CENTER_CROP)
40
+ "contain" -> setImageScaleType(ImageView.ScaleType.CENTER_INSIDE)
41
+ "stretch" -> setImageScaleType(ImageView.ScaleType.FIT_XY)
42
+ }
43
+ }
44
+
45
+ override fun requestLayout() {
46
+ super.requestLayout()
47
+ post(measureAndLayout)
48
+ }
49
+
50
+ private val measureAndLayout = Runnable {
51
+ measure(
52
+ MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
53
+ MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
54
+ )
55
+ layout(left, top, right, bottom)
56
+ }
57
+ }
@@ -0,0 +1,55 @@
1
+ package io.invertase.googlemobileads
2
+
3
+ /*
4
+ * Copyright (c) 2016-present Invertase Limited & Contributors
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this library except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ *
18
+ */
19
+
20
+ import com.facebook.react.bridge.ReactApplicationContext
21
+ import com.facebook.react.module.annotations.ReactModule
22
+ import com.facebook.react.uimanager.ThemedReactContext
23
+ import com.facebook.react.uimanager.ViewGroupManager
24
+ import com.facebook.react.uimanager.ViewManagerDelegate
25
+ import com.facebook.react.uimanager.annotations.ReactProp
26
+ import com.facebook.react.viewmanagers.RNGoogleMobileAdsMediaViewManagerDelegate
27
+ import com.facebook.react.viewmanagers.RNGoogleMobileAdsMediaViewManagerInterface
28
+
29
+ @ReactModule(name = ReactNativeGoogleMobileAdsMediaViewManager.NAME)
30
+ class ReactNativeGoogleMobileAdsMediaViewManager(
31
+ reactContext: ReactApplicationContext
32
+ ) : ViewGroupManager<ReactNativeGoogleMobileAdsMediaView>(reactContext),
33
+ RNGoogleMobileAdsMediaViewManagerInterface<ReactNativeGoogleMobileAdsMediaView> {
34
+ private val delegate: ViewManagerDelegate<ReactNativeGoogleMobileAdsMediaView> = RNGoogleMobileAdsMediaViewManagerDelegate(this)
35
+
36
+ override fun getDelegate(): ViewManagerDelegate<ReactNativeGoogleMobileAdsMediaView> = delegate
37
+
38
+ override fun getName(): String = NAME
39
+
40
+ override fun createViewInstance(context: ThemedReactContext): ReactNativeGoogleMobileAdsMediaView = ReactNativeGoogleMobileAdsMediaView(context)
41
+
42
+ @ReactProp(name = "responseId")
43
+ override fun setResponseId(view: ReactNativeGoogleMobileAdsMediaView, responseId: String?) {
44
+ view.setResponseId(responseId)
45
+ }
46
+
47
+ @ReactProp(name = "resizeMode")
48
+ override fun setResizeMode(view: ReactNativeGoogleMobileAdsMediaView, resizeMode: String?) {
49
+ view.setResizeMode(resizeMode)
50
+ }
51
+
52
+ companion object {
53
+ const val NAME = "RNGoogleMobileAdsMediaView"
54
+ }
55
+ }
@@ -26,13 +26,11 @@ import com.google.android.gms.ads.AdRequest
26
26
  import com.google.android.gms.ads.AdValue;
27
27
  import com.google.android.gms.ads.OnAdInspectorClosedListener
28
28
 
29
- private const val SERVICE = "RNGoogleMobileAdsModule";
30
-
31
29
  class ReactNativeGoogleMobileAdsModule(
32
30
  reactContext: ReactApplicationContext
33
31
  ) : ReactContextBaseJavaModule(reactContext) {
34
32
 
35
- override fun getName() = SERVICE
33
+ override fun getName() = NAME
36
34
 
37
35
  override fun getConstants(): Map<String, Any> {
38
36
  return mapOf(
@@ -164,7 +162,6 @@ class ReactNativeGoogleMobileAdsModule(
164
162
  }
165
163
  }
166
164
 
167
-
168
165
  @ReactMethod
169
166
  fun openDebugMenu(adUnit: String) {
170
167
  currentActivity?.runOnUiThread {
@@ -181,4 +178,8 @@ class ReactNativeGoogleMobileAdsModule(
181
178
  fun setAppMuted(muted: Boolean) {
182
179
  MobileAds.setAppMuted(muted)
183
180
  }
181
+
182
+ companion object {
183
+ const val NAME = "RNGoogleMobileAdsModule"
184
+ }
184
185
  }
@@ -0,0 +1,107 @@
1
+ package io.invertase.googlemobileads
2
+
3
+ /*
4
+ * Copyright (c) 2016-present Invertase Limited & Contributors
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this library except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ *
18
+ */
19
+
20
+ import android.annotation.SuppressLint
21
+ import android.widget.FrameLayout
22
+ import com.facebook.react.bridge.ReactContext
23
+ import com.facebook.react.uimanager.UIManagerHelper
24
+ import com.facebook.react.uimanager.common.UIManagerType
25
+ import com.facebook.react.views.view.ReactViewGroup
26
+ import com.google.android.gms.ads.nativead.MediaView
27
+ import com.google.android.gms.ads.nativead.NativeAd
28
+ import com.google.android.gms.ads.nativead.NativeAdView
29
+ import kotlinx.coroutines.CoroutineScope
30
+ import kotlinx.coroutines.Dispatchers
31
+ import kotlinx.coroutines.Job
32
+ import kotlinx.coroutines.delay
33
+ import kotlinx.coroutines.launch
34
+
35
+
36
+ @SuppressLint("ViewConstructor")
37
+ class ReactNativeGoogleMobileAdsNativeAdView(
38
+ private val context: ReactContext
39
+ ): FrameLayout(context) {
40
+ val viewGroup = ReactViewGroup(context)
41
+ private val nativeAdView = NativeAdView(context)
42
+ private var nativeAd: NativeAd? = null
43
+ private var reloadJob: Job? = null
44
+
45
+ init {
46
+ nativeAdView.addView(viewGroup)
47
+ addView(nativeAdView)
48
+ }
49
+
50
+ fun setResponseId(responseId: String?) {
51
+ val nativeModule = context.getNativeModule(ReactNativeGoogleMobileAdsNativeModule::class.java)
52
+ nativeModule?.getNativeAd(responseId ?: "")?.let {
53
+ if (nativeAd == it) {
54
+ return
55
+ }
56
+ nativeAd = it
57
+ reloadAd()
58
+ }
59
+ }
60
+
61
+ fun registerAsset(assetType: String, reactTag: Int) {
62
+ val uiManager = UIManagerHelper.getUIManagerForReactTag(context, reactTag)
63
+ val assetView = uiManager?.resolveView(reactTag) ?: return
64
+ when (assetType) {
65
+ "advertiser" -> nativeAdView.advertiserView = assetView
66
+ "body" -> nativeAdView.bodyView = assetView
67
+ "callToAction" -> nativeAdView.callToActionView = assetView
68
+ "headline" -> nativeAdView.headlineView = assetView
69
+ "price" -> nativeAdView.priceView = assetView
70
+ "store" -> nativeAdView.storeView = assetView
71
+ "starRating" -> nativeAdView.starRatingView = assetView
72
+ "icon" -> nativeAdView.iconView = assetView
73
+ "image" -> nativeAdView.imageView = assetView
74
+ "media" -> nativeAdView.mediaView = assetView as MediaView
75
+ }
76
+ reloadAd()
77
+ }
78
+
79
+ private fun reloadAd() {
80
+ reloadJob?.cancel()
81
+ reloadJob = CoroutineScope(Dispatchers.Main).launch {
82
+ delay(100)
83
+ nativeAd?.let { nativeAdView.setNativeAd(it) }
84
+ nativeAdView.rootView.requestLayout()
85
+ }
86
+ }
87
+
88
+ override fun requestLayout() {
89
+ super.requestLayout()
90
+ post(measureAndLayout)
91
+ }
92
+
93
+ fun destroy() {
94
+ reloadJob?.cancel()
95
+ reloadJob = null
96
+ nativeAdView.removeView(viewGroup)
97
+ nativeAdView.destroy()
98
+ }
99
+
100
+ private val measureAndLayout = Runnable {
101
+ measure(
102
+ MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
103
+ MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
104
+ )
105
+ layout(left, top, right, bottom)
106
+ }
107
+ }
@@ -0,0 +1,85 @@
1
+ package io.invertase.googlemobileads
2
+
3
+ /*
4
+ * Copyright (c) 2016-present Invertase Limited & Contributors
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this library except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ *
18
+ */
19
+
20
+ import android.view.View
21
+ import com.facebook.react.bridge.ReactApplicationContext
22
+ import com.facebook.react.bridge.ReactMethod
23
+ import com.facebook.react.module.annotations.ReactModule
24
+ import com.facebook.react.uimanager.ThemedReactContext
25
+ import com.facebook.react.uimanager.ViewGroupManager
26
+ import com.facebook.react.uimanager.ViewManagerDelegate
27
+ import com.facebook.react.uimanager.annotations.ReactProp
28
+ import com.facebook.react.viewmanagers.RNGoogleMobileAdsNativeViewManagerDelegate
29
+ import com.facebook.react.viewmanagers.RNGoogleMobileAdsNativeViewManagerInterface
30
+
31
+ @ReactModule(name = ReactNativeGoogleMobileAdsNativeAdViewManager.NAME)
32
+ class ReactNativeGoogleMobileAdsNativeAdViewManager(
33
+ reactContext: ReactApplicationContext
34
+ ) : ViewGroupManager<ReactNativeGoogleMobileAdsNativeAdView>(reactContext),
35
+ RNGoogleMobileAdsNativeViewManagerInterface<ReactNativeGoogleMobileAdsNativeAdView> {
36
+ private val delegate: ViewManagerDelegate<ReactNativeGoogleMobileAdsNativeAdView> = RNGoogleMobileAdsNativeViewManagerDelegate(this)
37
+
38
+ override fun getDelegate(): ViewManagerDelegate<ReactNativeGoogleMobileAdsNativeAdView> = delegate
39
+
40
+ override fun getName(): String = NAME
41
+
42
+ override fun createViewInstance(context: ThemedReactContext): ReactNativeGoogleMobileAdsNativeAdView = ReactNativeGoogleMobileAdsNativeAdView(context)
43
+
44
+ override fun onDropViewInstance(adView: ReactNativeGoogleMobileAdsNativeAdView) {
45
+ super.onDropViewInstance(adView)
46
+ adView.destroy()
47
+ }
48
+
49
+ override fun prepareToRecycleView(
50
+ reactContext: ThemedReactContext,
51
+ view: ReactNativeGoogleMobileAdsNativeAdView
52
+ ): ReactNativeGoogleMobileAdsNativeAdView? {
53
+ return null
54
+ }
55
+
56
+ @ReactProp(name = "responseId")
57
+ override fun setResponseId(view: ReactNativeGoogleMobileAdsNativeAdView, responseId: String?) {
58
+ view.setResponseId(responseId)
59
+ }
60
+
61
+ @ReactMethod
62
+ override fun registerAsset(view: ReactNativeGoogleMobileAdsNativeAdView, assetKey: String, reactTag: Int) {
63
+ view.registerAsset(assetKey, reactTag)
64
+ }
65
+
66
+ override fun addView(parent: ReactNativeGoogleMobileAdsNativeAdView, child: View, index: Int) {
67
+ parent.viewGroup.addView(child, index)
68
+ }
69
+
70
+ override fun getChildCount(parent: ReactNativeGoogleMobileAdsNativeAdView): Int {
71
+ return parent.viewGroup.childCount
72
+ }
73
+
74
+ override fun getChildAt(parent: ReactNativeGoogleMobileAdsNativeAdView, index: Int): View? {
75
+ return parent.viewGroup.getChildAt(index)
76
+ }
77
+
78
+ override fun removeViewAt(parent: ReactNativeGoogleMobileAdsNativeAdView, index: Int) {
79
+ parent.viewGroup.removeViewAt(index)
80
+ }
81
+
82
+ companion object {
83
+ const val NAME = "RNGoogleMobileAdsNativeView"
84
+ }
85
+ }