Skip to content

Commit

Permalink
feat: support legacy architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
wjaykim committed Dec 27, 2024
1 parent 7813c21 commit 02b83e7
Show file tree
Hide file tree
Showing 22 changed files with 287 additions and 91 deletions.
2 changes: 1 addition & 1 deletion RNGoogleMobileAdsExample/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ const NativeComponent = () => {
const [nativeAd, setNativeAd] = useState<NativeAd>();

useEffect(() => {
NativeAd.createForAdRequest(TestIds.GAM_NATIVE_VIDEO, {
NativeAd.createForAdRequest(TestIds.GAM_NATIVE, {
aspectRatio: NativeMediaAspectRatio.LANDSCAPE,
})
.then(setNativeAd)
Expand Down
2 changes: 1 addition & 1 deletion RNGoogleMobileAdsExample/android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64
# your application. You should enable this flag either if you want
# to write custom TurboModules/Fabric components OR use libraries that
# are providing them.
newArchEnabled=true
newArchEnabled=false

# Use this property to enable or disable the Hermes JS engine.
# If set to false, you will be using JSC instead.
Expand Down
10 changes: 10 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ android {
appJSONGoogleMobileAdsOptimizeAdLoading : appJSONGoogleMobileAdsOptimizeAdLoadingBool
]
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()

sourceSets {
main {
if (isNewArchitectureEnabled()) {
java.srcDirs += ['src/newarch']
} else {
java.srcDirs += ['src/oldarch']
}
}
}
}
lintOptions {
disable 'GradleCompatible'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ReactNativeGoogleMobileAdsMediaView(
private val context: ReactContext
): MediaView(context) {
fun setResponseId(responseId: String?) {
val nativeModule = context.getNativeModule(ReactNativeGoogleMobileAdsNativeModule.NAME) as ReactNativeGoogleMobileAdsNativeModule?
val nativeModule = context.getNativeModule(ReactNativeGoogleMobileAdsNativeModule::class.java)
nativeModule?.getNativeAd(responseId ?: "")?.let {
this.mediaContent = it.mediaContent
requestLayout()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ class ReactNativeGoogleMobileAdsMediaViewManager(
override fun createViewInstance(context: ThemedReactContext): ReactNativeGoogleMobileAdsMediaView = ReactNativeGoogleMobileAdsMediaView(context)

@ReactProp(name = "responseId")
override fun setResponseId(mediaView: ReactNativeGoogleMobileAdsMediaView, responseId: String?) {
mediaView.setResponseId(responseId)
override fun setResponseId(view: ReactNativeGoogleMobileAdsMediaView, responseId: String?) {
view.setResponseId(responseId)
}

@ReactProp(name = "resizeMode")
override fun setResizeMode(mediaView: ReactNativeGoogleMobileAdsMediaView, resizeMode: String?) {
mediaView.setResizeMode(resizeMode)
override fun setResizeMode(view: ReactNativeGoogleMobileAdsMediaView, resizeMode: String?) {
view.setResizeMode(resizeMode)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ import com.google.android.gms.ads.OnAdInspectorClosedListener

class ReactNativeGoogleMobileAdsModule(
reactContext: ReactApplicationContext
) : NativeGoogleMobileAdsModuleSpec(reactContext) {
) : ReactContextBaseJavaModule(reactContext) {

override fun getName() = NAME

override fun getTypedExportedConstants(): Map<String, Any> {
override fun getConstants(): Map<String, Any> {
return mapOf(
// Precision types in ad revenue events.
// See:
// https://developers.google.com/android/reference/com/google/android/gms/ads/AdValue.PrecisionType
"REVENUE_PRECISION_UNKNOWN" to AdValue.PrecisionType.UNKNOWN,
"REVENUE_PRECISION_ESTIMATED" to AdValue.PrecisionType.ESTIMATED,
"REVENUE_PRECISION_PUBLISHER_PROVIDED" to AdValue.PrecisionType.PUBLISHER_PROVIDED,
"REVENUE_PRECISION_PRECISE" to AdValue.PrecisionType.PRECISE
// Precision types in ad revenue events.
// See:
// https://developers.google.com/android/reference/com/google/android/gms/ads/AdValue.PrecisionType
"REVENUE_PRECISION_UNKNOWN" to AdValue.PrecisionType.UNKNOWN,
"REVENUE_PRECISION_ESTIMATED" to AdValue.PrecisionType.ESTIMATED,
"REVENUE_PRECISION_PUBLISHER_PROVIDED" to AdValue.PrecisionType.PUBLISHER_PROVIDED,
"REVENUE_PRECISION_PRECISE" to AdValue.PrecisionType.PRECISE
)
}

Expand Down Expand Up @@ -103,7 +103,8 @@ class ReactNativeGoogleMobileAdsModule(
return builder.build()
}

override fun initialize(promise: Promise) {
@ReactMethod
fun initialize(promise: Promise) {
MobileAds.initialize(
// in react-native, the Activity instance *may* go away, becoming null after initialize
// it is not clear if that can happen here without an initialize necessarily following the Activity lifecycle
Expand All @@ -124,15 +125,17 @@ class ReactNativeGoogleMobileAdsModule(
});
}

override fun setRequestConfiguration(
@ReactMethod
fun setRequestConfiguration(
requestConfiguration: ReadableMap?,
promise: Promise
) {
MobileAds.setRequestConfiguration(buildRequestConfiguration(requestConfiguration ?: Arguments.createMap()))
promise.resolve(null)
}

override fun openAdInspector(promise: Promise) {
@ReactMethod
fun openAdInspector(promise: Promise) {
val activity = currentActivity
if (activity == null) {
promise.reject("null-activity", "Ad Inspector attempted to open but the current Activity was null.")
Expand All @@ -159,18 +162,20 @@ class ReactNativeGoogleMobileAdsModule(
}
}


override fun openDebugMenu(adUnit: String) {
@ReactMethod
fun openDebugMenu(adUnit: String) {
currentActivity?.runOnUiThread {
MobileAds.openDebugMenu(currentActivity!!, adUnit)
}
}

override fun setAppVolume(volume: Double) {
@ReactMethod
fun setAppVolume(volume: Double) {
MobileAds.setAppVolume(volume.toFloat())
}

override fun setAppMuted(muted: Boolean) {
@ReactMethod
fun setAppMuted(muted: Boolean) {
MobileAds.setAppMuted(muted)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ReactNativeGoogleMobileAdsNativeAdView(
}

fun setResponseId(responseId: String?) {
val nativeModule = context.getNativeModule(ReactNativeGoogleMobileAdsNativeModule.NAME) as ReactNativeGoogleMobileAdsNativeModule?
val nativeModule = context.getNativeModule(ReactNativeGoogleMobileAdsNativeModule::class.java)
nativeModule?.getNativeAd(responseId ?: "")?.let {
if (nativeAd == it) {
return
Expand All @@ -59,7 +59,7 @@ class ReactNativeGoogleMobileAdsNativeAdView(
}

fun registerAsset(assetType: String, reactTag: Int) {
val uiManager = UIManagerHelper.getUIManager(context, UIManagerType.FABRIC)
val uiManager = UIManagerHelper.getUIManagerForReactTag(context, reactTag)
val assetView = uiManager?.resolveView(reactTag) ?: return
when (assetType) {
"advertiser" -> nativeAdView.advertiserView = assetView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package io.invertase.googlemobileads

import android.view.View
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.module.annotations.ReactModule
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewGroupManager
Expand Down Expand Up @@ -53,12 +54,13 @@ class ReactNativeGoogleMobileAdsNativeAdViewManager(
}

@ReactProp(name = "responseId")
override fun setResponseId(adView: ReactNativeGoogleMobileAdsNativeAdView, responseId: String?) {
adView.setResponseId(responseId)
override fun setResponseId(view: ReactNativeGoogleMobileAdsNativeAdView, responseId: String?) {
view.setResponseId(responseId)
}

override fun registerAsset(adView: ReactNativeGoogleMobileAdsNativeAdView, assetKey: String, reactTag: Int) {
adView.registerAsset(assetKey, reactTag)
@ReactMethod
override fun registerAsset(view: ReactNativeGoogleMobileAdsNativeAdView, assetKey: String, reactTag: Int) {
view.registerAsset(assetKey, reactTag)
}

override fun addView(parent: ReactNativeGoogleMobileAdsNativeAdView, child: View, index: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ package io.invertase.googlemobileads
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.module.annotations.ReactModule
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdLoader
import com.google.android.gms.ads.MediaAspectRatio
Expand All @@ -29,13 +31,15 @@ import com.google.android.gms.ads.VideoOptions
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdOptions

@ReactModule(ReactNativeGoogleMobileAdsNativeModule.NAME)
class ReactNativeGoogleMobileAdsNativeModule(
reactContext: ReactApplicationContext
) : NativeGoogleMobileAdsNativeModuleSpec(reactContext) {
private val adHolders = HashMap<String, NativeAdHolder>()

override fun getName() = NAME

@ReactMethod
override fun load(adUnitId: String, requestOptions: ReadableMap, promise: Promise) {
val holder = NativeAdHolder(adUnitId, requestOptions)
holder.loadAd { nativeAd ->
Expand Down Expand Up @@ -75,11 +79,20 @@ class ReactNativeGoogleMobileAdsNativeModule(
}
}

@ReactMethod
override fun destroy(responseId: String) {
adHolders[responseId]?.destroy()
adHolders.remove(responseId)
}

override fun invalidate() {
super.invalidate()
adHolders.values.forEach {
it.destroy()
}
adHolders.clear()
}

fun getNativeAd(responseId: String): NativeAd? {
return adHolders[responseId]?.nativeAd
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ReactNativeGoogleMobileAdsPackage : TurboReactPackage() {
false,
false,
false,
isTurboModule,
false,
)
moduleInfos[ReactNativeGoogleMobileAdsConsentModule.NAME] =
ReactModuleInfo(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.facebook.react.viewmanagers;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.react.uimanager.BaseViewManagerDelegate;
import com.facebook.react.uimanager.BaseViewManagerInterface;

public class RNGoogleMobileAdsMediaViewManagerDelegate<T extends View, U extends BaseViewManagerInterface<T> & RNGoogleMobileAdsMediaViewManagerInterface<T>> extends BaseViewManagerDelegate<T, U> {
public RNGoogleMobileAdsMediaViewManagerDelegate(U viewManager) {
super(viewManager);
}

@Override
public void setProperty(@NonNull T view, String propName, @Nullable Object value) {
assert propName != null;
switch (propName) {
case "responseId":
mViewManager.setResponseId(view, value == null ? null : (String) value);
break;
case "resizeMode":
mViewManager.setResizeMode(view, value == null ? null : (String) value);
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.facebook.react.viewmanagers;

import android.view.View;

import androidx.annotation.Nullable;

public interface RNGoogleMobileAdsMediaViewManagerInterface<T extends View> {
void setResponseId(T view, @Nullable String responseId);
void setResizeMode(T view, @Nullable String resizeMode);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.facebook.react.viewmanagers;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.uimanager.BaseViewManagerDelegate;
import com.facebook.react.uimanager.BaseViewManagerInterface;

public class RNGoogleMobileAdsNativeViewManagerDelegate<T extends View, U extends BaseViewManagerInterface<T> & RNGoogleMobileAdsNativeViewManagerInterface<T>> extends BaseViewManagerDelegate<T, U> {
public RNGoogleMobileAdsNativeViewManagerDelegate(U viewManager) {
super(viewManager);
}

@Override
public void setProperty(@NonNull T view, String propName, @Nullable Object value) {
assert propName != null;
if (propName.equals("responseId")) {
mViewManager.setResponseId(view, value == null ? null : (String) value);
}
}

@Override
public void receiveCommand(@NonNull T view, String commandName, ReadableArray args) {
assert commandName != null;
if (commandName.equals("registerAsset")) {
mViewManager.registerAsset(view, args.getString(0), args.getInt(1));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.facebook.react.viewmanagers;

import android.view.View;

import androidx.annotation.Nullable;

public interface RNGoogleMobileAdsNativeViewManagerInterface<T extends View> {
void setResponseId(T view, @Nullable String responseId);
void registerAsset(T view, String assetKey, int reactTag);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.invertase.googlemobileads

import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.modules.core.DeviceEventManagerModule

abstract class NativeGoogleMobileAdsNativeModuleSpec(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
abstract fun load(adUnitId: String, requestOptions: ReadableMap, promise: Promise)
abstract fun destroy(responseId: String)

fun emitOnAdEvent(params: ReadableMap) {
reactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit("RNGMANativeAdEvent", params)
}
}
3 changes: 3 additions & 0 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsMediaView.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithBridge:(RCTBridge *)bridge;
#endif

@property(nonatomic, copy) NSString *responseId;
@property(nonatomic, copy) NSString *resizeMode;

@end

@interface RNGoogleMobileAdsMediaViewManager : RCTViewManager
Expand Down
Loading

0 comments on commit 02b83e7

Please sign in to comment.