[admob][android] Implement VideoOptions + AdRequest into all advert types
This commit is contained in:
parent
7ef43d9c02
commit
5a9a7ea556
|
@ -9,22 +9,10 @@ import com.facebook.react.bridge.ReactApplicationContext;
|
|||
import com.facebook.react.uimanager.UIManagerModule;
|
||||
import com.facebook.react.uimanager.ViewManager;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import io.invertase.firebase.admob.RNFirebaseAdMobBanner;
|
||||
import io.invertase.firebase.auth.RNFirebaseAuth;
|
||||
import io.invertase.firebase.config.RNFirebaseRemoteConfig;
|
||||
import io.invertase.firebase.storage.RNFirebaseStorage;
|
||||
import io.invertase.firebase.database.RNFirebaseDatabase;
|
||||
import io.invertase.firebase.analytics.RNFirebaseAnalytics;
|
||||
import io.invertase.firebase.crash.RNFirebaseCrash;
|
||||
import io.invertase.firebase.messaging.RNFirebaseMessaging;
|
||||
import io.invertase.firebase.perf.RNFirebasePerformance;
|
||||
import io.invertase.firebase.admob.RNFirebaseAdMob;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class RNFirebasePackage implements ReactPackage {
|
||||
private Context mContext;
|
||||
|
@ -61,8 +49,6 @@ public class RNFirebasePackage implements ReactPackage {
|
|||
*/
|
||||
@Override
|
||||
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
|
||||
return Arrays.<ViewManager>asList(
|
||||
new RNFirebaseAdMobBanner()
|
||||
);
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ import java.util.Map;
|
|||
|
||||
public class RNFirebaseAdMob extends ReactContextBaseJavaModule {
|
||||
|
||||
private static final String TAG = "RNFirebaseAdmob";
|
||||
private static final String TAG = "RNFirebaseAdMob";
|
||||
|
||||
ReactApplicationContext getContext() {
|
||||
return getReactApplicationContext();
|
||||
|
@ -30,7 +30,8 @@ public class RNFirebaseAdMob extends ReactContextBaseJavaModule {
|
|||
}
|
||||
|
||||
private HashMap<String, RNFirebaseAdmobInterstitial> interstitials = new HashMap<>();
|
||||
private HashMap<String, RNFirebaseRewardedVideo> rewardedVideos = new HashMap<>();
|
||||
private HashMap<String, RNFirebaseAdMobRewardedVideo> rewardedVideos = new HashMap<>();
|
||||
private HashMap<String, RNFirebaseAdMobNativeExpress> nativeExpressAds = new HashMap<>();
|
||||
|
||||
public RNFirebaseAdMob(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
|
@ -45,7 +46,7 @@ public class RNFirebaseAdMob extends ReactContextBaseJavaModule {
|
|||
@ReactMethod
|
||||
public void interstitialLoadAd(String adUnit, ReadableMap request) {
|
||||
RNFirebaseAdmobInterstitial interstitial = getOrCreateInterstitial(adUnit);
|
||||
interstitial.loadAd(buildRequest(request).build());
|
||||
interstitial.loadAd(RNFirebaseAdMobUtils.buildRequest(request).build());
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
|
@ -56,13 +57,13 @@ public class RNFirebaseAdMob extends ReactContextBaseJavaModule {
|
|||
|
||||
@ReactMethod
|
||||
public void rewardedVideoLoadAd(String adUnit, ReadableMap request) {
|
||||
RNFirebaseRewardedVideo rewardedVideo = getOrCreateRewardedVideo(adUnit);
|
||||
rewardedVideo.loadAd(buildRequest(request).build());
|
||||
RNFirebaseAdMobRewardedVideo rewardedVideo = getOrCreateRewardedVideo(adUnit);
|
||||
rewardedVideo.loadAd(RNFirebaseAdMobUtils.buildRequest(request).build());
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void rewardedVideoShowAd(String adUnit) {
|
||||
RNFirebaseRewardedVideo rewardedVideo = getOrCreateRewardedVideo(adUnit);
|
||||
RNFirebaseAdMobRewardedVideo rewardedVideo = getOrCreateRewardedVideo(adUnit);
|
||||
rewardedVideo.show();
|
||||
}
|
||||
|
||||
|
@ -85,11 +86,11 @@ public class RNFirebaseAdMob extends ReactContextBaseJavaModule {
|
|||
* @param adUnit
|
||||
* @return
|
||||
*/
|
||||
private RNFirebaseRewardedVideo getOrCreateRewardedVideo(String adUnit) {
|
||||
private RNFirebaseAdMobRewardedVideo getOrCreateRewardedVideo(String adUnit) {
|
||||
if (rewardedVideos.containsKey(adUnit)) {
|
||||
return rewardedVideos.get(adUnit);
|
||||
}
|
||||
RNFirebaseRewardedVideo rewardedVideo = new RNFirebaseRewardedVideo(adUnit, this);
|
||||
RNFirebaseAdMobRewardedVideo rewardedVideo = new RNFirebaseAdMobRewardedVideo(adUnit, this);
|
||||
rewardedVideos.put(adUnit, rewardedVideo);
|
||||
return rewardedVideo;
|
||||
}
|
||||
|
@ -100,21 +101,4 @@ public class RNFirebaseAdMob extends ReactContextBaseJavaModule {
|
|||
constants.put("DEVICE_ID_EMULATOR", AdRequest.DEVICE_ID_EMULATOR);
|
||||
return constants;
|
||||
}
|
||||
|
||||
AdRequest.Builder buildRequest(ReadableMap request) {
|
||||
AdRequest.Builder requestBuilder = new AdRequest.Builder();
|
||||
|
||||
if (request.hasKey("testDevice")) {
|
||||
requestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
|
||||
}
|
||||
|
||||
ReadableArray keywords = request.getArray("keywords");
|
||||
List<Object> keywordsList = Utils.recursivelyDeconstructReadableArray(keywords);
|
||||
|
||||
for (Object word : keywordsList) {
|
||||
requestBuilder.addKeyword((String) word);
|
||||
}
|
||||
|
||||
return requestBuilder;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import android.support.annotation.Nullable;
|
|||
import android.view.View;
|
||||
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
import com.facebook.react.bridge.WritableMap;
|
||||
import com.facebook.react.common.MapBuilder;
|
||||
import com.facebook.react.uimanager.PixelUtil;
|
||||
|
@ -16,8 +17,11 @@ import com.google.android.gms.ads.AdListener;
|
|||
import com.google.android.gms.ads.AdRequest;
|
||||
import com.google.android.gms.ads.AdSize;
|
||||
import com.google.android.gms.ads.AdView;
|
||||
import com.google.android.gms.ads.NativeExpressAdView;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class RNFirebaseAdMobBanner extends SimpleViewManager<ReactViewGroup> implements View.OnLayoutChangeListener {
|
||||
|
||||
|
@ -30,7 +34,8 @@ public class RNFirebaseAdMobBanner extends SimpleViewManager<ReactViewGroup> imp
|
|||
EVENT_AD_FAILED_TO_LOAD("onAdFailedToLoad"),
|
||||
EVENT_AD_OPENED("onAdOpened"),
|
||||
EVENT_AD_CLOSED("onAdClosed"),
|
||||
EVENT_AD_LEFT_APPLICATION("onAdLeftApplication");
|
||||
EVENT_AD_LEFT_APPLICATION("onAdLeftApplication"),
|
||||
EVENT_AD_VIDEO_CONTENT("hasVideoContent");
|
||||
|
||||
private final String event;
|
||||
|
||||
|
@ -47,8 +52,8 @@ public class RNFirebaseAdMobBanner extends SimpleViewManager<ReactViewGroup> imp
|
|||
private ThemedReactContext context;
|
||||
private ReactViewGroup viewGroup;
|
||||
private RCTEventEmitter emitter;
|
||||
private AdRequest.Builder request = null;
|
||||
private String size;
|
||||
private Boolean testing = false;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
|
@ -66,10 +71,17 @@ public class RNFirebaseAdMobBanner extends SimpleViewManager<ReactViewGroup> imp
|
|||
viewGroup = new ReactViewGroup(themedReactContext);
|
||||
emitter = themedReactContext.getJSModule(RCTEventEmitter.class);
|
||||
|
||||
attachAdViewToViewGroup();
|
||||
AdView adView = new AdView(context);
|
||||
viewGroup.addView(adView);
|
||||
setAdListener();
|
||||
|
||||
return viewGroup;
|
||||
}
|
||||
|
||||
AdView getAdView() {
|
||||
return (AdView) viewGroup.getChildAt(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Declare custom events
|
||||
* @return
|
||||
|
@ -108,19 +120,19 @@ public class RNFirebaseAdMobBanner extends SimpleViewManager<ReactViewGroup> imp
|
|||
*/
|
||||
@ReactProp(name = "unitId")
|
||||
public void setUnitId(final ReactViewGroup view, final String value) {
|
||||
AdView adViewView = (AdView) view.getChildAt(0);
|
||||
AdView adViewView = getAdView();
|
||||
adViewView.setAdUnitId(value);
|
||||
requestAd();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle testing prop
|
||||
* Handle request prop
|
||||
* @param view
|
||||
* @param value
|
||||
* @param adRequest
|
||||
*/
|
||||
@ReactProp(name = "testing")
|
||||
public void setUnitId(final ReactViewGroup view, final Boolean value) {
|
||||
testing = value;
|
||||
@ReactProp(name = "request")
|
||||
public void setRequest(final ReactViewGroup view, final ReadableMap adRequest) {
|
||||
request = RNFirebaseAdMobUtils.buildRequest(adRequest);
|
||||
requestAd();
|
||||
}
|
||||
|
||||
|
@ -135,7 +147,7 @@ public class RNFirebaseAdMobBanner extends SimpleViewManager<ReactViewGroup> imp
|
|||
size = value;
|
||||
}
|
||||
|
||||
AdSize adSize = propToAdSize(size.toUpperCase());
|
||||
AdSize adSize = RNFirebaseAdMobUtils.stringToAdSize(size);
|
||||
AdView adViewView = (AdView) view.getChildAt(0);
|
||||
adViewView.setAdSize(adSize);
|
||||
|
||||
|
@ -159,49 +171,17 @@ public class RNFirebaseAdMobBanner extends SimpleViewManager<ReactViewGroup> imp
|
|||
requestAd();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new instance of the AdView and attaches it to the
|
||||
* current ReactViewGroup
|
||||
*/
|
||||
void attachAdViewToViewGroup() {
|
||||
removeAdFromViewGroup();
|
||||
|
||||
final AdView adView = new AdView(context);
|
||||
viewGroup.addView(adView);
|
||||
setAdListener();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the AdView from the ViewGroup
|
||||
*/
|
||||
void removeAdFromViewGroup() {
|
||||
AdView adView = (AdView) viewGroup.getChildAt(0);
|
||||
viewGroup.removeAllViews();
|
||||
|
||||
if (adView != null) {
|
||||
adView.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a new ad into a viewGroup
|
||||
*/
|
||||
void requestAd() {
|
||||
AdView adView = (AdView) viewGroup.getChildAt(0);
|
||||
AdView adView = getAdView();
|
||||
|
||||
if (adView.getAdSize() == null || adView.getAdUnitId() == null) {
|
||||
if (adView.getAdSize() == null || adView.getAdUnitId() == null || request == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
|
||||
|
||||
// If the prop testing is set, assign the emulators device ID
|
||||
if (testing) {
|
||||
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
|
||||
}
|
||||
|
||||
AdRequest adRequest = adRequestBuilder.build();
|
||||
AdRequest adRequest = request.build();
|
||||
adView.loadAd(adRequest);
|
||||
}
|
||||
|
||||
|
@ -209,7 +189,7 @@ public class RNFirebaseAdMobBanner extends SimpleViewManager<ReactViewGroup> imp
|
|||
* Listen to Ad events
|
||||
*/
|
||||
void setAdListener() {
|
||||
final AdView adView = (AdView) viewGroup.getChildAt(0);
|
||||
final AdView adView = getAdView();
|
||||
|
||||
adView.setAdListener(new AdListener() {
|
||||
@Override
|
||||
|
@ -223,7 +203,16 @@ public class RNFirebaseAdMobBanner extends SimpleViewManager<ReactViewGroup> imp
|
|||
adView.measure(width, height);
|
||||
adView.layout(left, top, left + width, top + height);
|
||||
|
||||
sendEvent(Events.EVENT_AD_LOADED.toString(), null);
|
||||
WritableMap payload = Arguments.createMap();
|
||||
|
||||
payload.putBoolean(RNFirebaseAdMobNativeExpress.Events.EVENT_AD_VIDEO_CONTENT.toString(), false);
|
||||
payload.putInt("width", width);
|
||||
payload.putInt("height", height);
|
||||
payload.putInt("left", left);
|
||||
payload.putInt("top", top);
|
||||
|
||||
|
||||
sendEvent(Events.EVENT_AD_LOADED.toString(), payload);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -264,27 +253,4 @@ public class RNFirebaseAdMobBanner extends SimpleViewManager<ReactViewGroup> imp
|
|||
|
||||
emitter.receiveEvent(viewGroup.getId(), BANNER_EVENT, event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the size prop to the AdSize
|
||||
* @param prop
|
||||
* @return
|
||||
*/
|
||||
AdSize propToAdSize(String prop) {
|
||||
switch (prop) {
|
||||
default:
|
||||
case "BANNER":
|
||||
return AdSize.BANNER;
|
||||
case "LARGE_BANNER":
|
||||
return AdSize.LARGE_BANNER;
|
||||
case "MEDIUM_RECTANGLE":
|
||||
return AdSize.MEDIUM_RECTANGLE;
|
||||
case "FULL_BANNER":
|
||||
return AdSize.FULL_BANNER;
|
||||
case "LEADERBOARD":
|
||||
return AdSize.LEADERBOARD;
|
||||
case "SMART_BANNER":
|
||||
return AdSize.SMART_BANNER;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,8 @@ public class RNFirebaseAdMobPackage implements ReactPackage {
|
|||
@Override
|
||||
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
|
||||
return Arrays.<ViewManager>asList(
|
||||
new RNFirebaseAdMobBanner()
|
||||
new RNFirebaseAdMobBanner(),
|
||||
new RNFirebaseAdMobNativeExpress()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,18 @@
|
|||
package io.invertase.firebase.admob;
|
||||
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
import com.facebook.react.bridge.ReadableArray;
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
import com.facebook.react.bridge.WritableMap;
|
||||
import com.google.android.gms.ads.AdRequest;
|
||||
import com.google.android.gms.ads.AdSize;
|
||||
import com.google.android.gms.ads.VideoOptions;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import io.invertase.firebase.Utils;
|
||||
|
||||
class RNFirebaseAdMobUtils {
|
||||
|
||||
|
@ -36,4 +46,93 @@ class RNFirebaseAdMobUtils {
|
|||
return map;
|
||||
}
|
||||
|
||||
static AdRequest.Builder buildRequest(ReadableMap request) {
|
||||
AdRequest.Builder requestBuilder = new AdRequest.Builder();
|
||||
|
||||
if (request.hasKey("testDevice")) {
|
||||
requestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
|
||||
}
|
||||
|
||||
if (request.hasKey("isDesignedForFamilies")) {
|
||||
requestBuilder.setIsDesignedForFamilies(request.getBoolean("isDesignedForFamilies"));
|
||||
}
|
||||
|
||||
if (request.hasKey("tagForChildDirectedTreatment")) {
|
||||
requestBuilder.tagForChildDirectedTreatment(request.getBoolean("tagForChildDirectedTreatment"));
|
||||
}
|
||||
|
||||
if (request.hasKey("contentUrl")) {
|
||||
requestBuilder.setContentUrl(request.getString("setContentUrl"));
|
||||
}
|
||||
|
||||
if (request.hasKey("requestAgent")) {
|
||||
requestBuilder.setRequestAgent(request.getString("requestAgent"));
|
||||
}
|
||||
|
||||
if (request.hasKey("gender")) {
|
||||
String gender = request.getString("gender");
|
||||
switch (gender) {
|
||||
case "male":
|
||||
requestBuilder.setGender(AdRequest.GENDER_MALE);
|
||||
break;
|
||||
case "female":
|
||||
requestBuilder.setGender(AdRequest.GENDER_FEMALE);
|
||||
break;
|
||||
case "unknown":
|
||||
requestBuilder.setGender(AdRequest.GENDER_UNKNOWN);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ReadableArray keywords = request.getArray("keywords");
|
||||
List<Object> keywordsList = Utils.recursivelyDeconstructReadableArray(keywords);
|
||||
|
||||
for (Object word : keywordsList) {
|
||||
requestBuilder.addKeyword((String) word);
|
||||
}
|
||||
|
||||
return requestBuilder;
|
||||
}
|
||||
|
||||
static VideoOptions.Builder buildVideoOptions(ReadableMap options) {
|
||||
VideoOptions.Builder optionsBuilder = new VideoOptions.Builder();
|
||||
|
||||
// Default true
|
||||
optionsBuilder.setStartMuted(options.getBoolean("startMuted"));
|
||||
|
||||
return optionsBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the size prop to the AdSize
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
static AdSize stringToAdSize(String value) {
|
||||
Pattern pattern = Pattern.compile("([0-9]+)x([0-9]+)");
|
||||
Matcher matcher = pattern.matcher(value);
|
||||
|
||||
// If size is "valXval"
|
||||
if (matcher.find()) {
|
||||
int width = Integer.parseInt(matcher.group(1));
|
||||
int height = Integer.parseInt(matcher.group(2));
|
||||
return new AdSize(width, height);
|
||||
}
|
||||
|
||||
switch (value.toUpperCase()) {
|
||||
default:
|
||||
case "BANNER":
|
||||
return AdSize.BANNER;
|
||||
case "LARGE_BANNER":
|
||||
return AdSize.LARGE_BANNER;
|
||||
case "MEDIUM_RECTANGLE":
|
||||
return AdSize.MEDIUM_RECTANGLE;
|
||||
case "FULL_BANNER":
|
||||
return AdSize.FULL_BANNER;
|
||||
case "LEADERBOARD":
|
||||
return AdSize.LEADERBOARD;
|
||||
case "SMART_BANNER":
|
||||
return AdSize.SMART_BANNER;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,135 +0,0 @@
|
|||
package io.invertase.firebase.admob;
|
||||
|
||||
|
||||
import android.app.Activity;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
import com.facebook.react.bridge.WritableMap;
|
||||
import com.google.android.gms.ads.AdRequest;
|
||||
import com.google.android.gms.ads.MobileAds;
|
||||
import com.google.android.gms.ads.reward.RewardItem;
|
||||
import com.google.android.gms.ads.reward.RewardedVideoAd;
|
||||
import com.google.android.gms.ads.reward.RewardedVideoAdListener;
|
||||
|
||||
import io.invertase.firebase.Utils;
|
||||
|
||||
public class RNFirebaseRewardedVideo implements RewardedVideoAdListener {
|
||||
|
||||
private RewardedVideoAd mAd;
|
||||
private String adUnit;
|
||||
private RNFirebaseAdMob adMob;
|
||||
private RewardedVideoAd rewardedVideo;
|
||||
|
||||
RNFirebaseRewardedVideo(final String adUnitString, final RNFirebaseAdMob adMobInstance) {
|
||||
adUnit = adUnitString;
|
||||
adMob = adMobInstance;
|
||||
|
||||
rewardedVideo = MobileAds.getRewardedVideoAdInstance(adMob.getContext());
|
||||
|
||||
Activity activity = adMob.getActivity();
|
||||
final RNFirebaseRewardedVideo _this = this;
|
||||
|
||||
if (activity != null) {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
rewardedVideo.setRewardedVideoAdListener(_this);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load an Ad with a AdRequest instance
|
||||
*
|
||||
* @param adRequest
|
||||
*/
|
||||
void loadAd(final AdRequest adRequest) {
|
||||
Activity activity = adMob.getActivity();
|
||||
if (activity != null) {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
rewardedVideo.loadAd(adUnit, adRequest);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the loaded interstitial, if it's loaded
|
||||
*/
|
||||
void show() {
|
||||
Activity activity = adMob.getActivity();
|
||||
if (activity != null) {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (rewardedVideo.isLoaded()) {
|
||||
rewardedVideo.show();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRewarded(RewardItem reward) {
|
||||
WritableMap payload = Arguments.createMap();
|
||||
payload.putInt("amount", reward.getAmount());
|
||||
payload.putString("type", reward.getType());
|
||||
sendEvent("onRewarded", payload);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRewardedVideoAdLeftApplication() {
|
||||
sendEvent("onAdLeftApplication", null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRewardedVideoAdClosed() {
|
||||
sendEvent("onAdClosed", null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRewardedVideoAdFailedToLoad(int errorCode) {
|
||||
WritableMap payload = RNFirebaseAdMobUtils.errorCodeToMap(errorCode);
|
||||
sendEvent("onAdFailedToLoad", payload);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRewardedVideoAdLoaded() {
|
||||
sendEvent("onAdLoaded", null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRewardedVideoAdOpened() {
|
||||
sendEvent("onAdOpened", null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRewardedVideoStarted() {
|
||||
sendEvent("onRewardedVideoStarted", null);
|
||||
}
|
||||
|
||||
// TODO onResume etc??? https://developers.google.com/admob/android/rewarded-video
|
||||
|
||||
/**
|
||||
* Send a native event over the bridge with a type and optional payload
|
||||
*
|
||||
* @param type
|
||||
* @param payload
|
||||
*/
|
||||
void sendEvent(String type, final @Nullable WritableMap payload) {
|
||||
WritableMap map = Arguments.createMap();
|
||||
map.putString("type", type);
|
||||
map.putString("adUnit", adUnit);
|
||||
|
||||
if (payload != null) {
|
||||
map.putMap("payload", payload);
|
||||
}
|
||||
|
||||
Utils.sendEvent(adMob.getContext(), "rewarded_video_event", map);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
import React from 'react';
|
||||
import { View, requireNativeComponent } from 'react-native';
|
||||
import PropTypes from 'prop-types';
|
||||
import { statics } from './';
|
||||
import { nativeToJSError } from '../../utils';
|
||||
|
||||
import AdRequest from './AdRequest';
|
||||
import VideoOptions from './VideoOptions';
|
||||
|
||||
class AdMobComponent extends React.Component {
|
||||
|
||||
static propTypes = {
|
||||
...View.propTypes,
|
||||
|
||||
// TODO ehesp: cant init this outside of the component; statics isn't defined
|
||||
...(() => {
|
||||
const eventProps = {};
|
||||
Object.keys(statics.EventTypes).forEach((key) => {
|
||||
eventProps[key] = PropTypes.func;
|
||||
});
|
||||
return eventProps;
|
||||
}),
|
||||
|
||||
...(() => {
|
||||
const eventProps = {};
|
||||
Object.keys(statics.NativeExpressEventTypes).forEach((key) => {
|
||||
eventProps[key] = PropTypes.func;
|
||||
});
|
||||
return eventProps;
|
||||
}),
|
||||
|
||||
size: PropTypes.string.isRequired,
|
||||
unitId: PropTypes.string.isRequired,
|
||||
request: PropTypes.object,
|
||||
video: PropTypes.object,
|
||||
};
|
||||
|
||||
static defaultProps = {
|
||||
request: new AdRequest().addTestDevice().build(),
|
||||
video: new VideoOptions().build(),
|
||||
};
|
||||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
};
|
||||
|
||||
this.nativeView = requireNativeComponent(props.class, AdMobComponent, {
|
||||
nativeOnly: {
|
||||
onSizeChange: true,
|
||||
},
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a single banner event and pass to
|
||||
* any props watching it
|
||||
* @param nativeEvent
|
||||
*/
|
||||
onBannerEvent = ({ nativeEvent }) => {
|
||||
console.log(nativeEvent)
|
||||
if (this.props[nativeEvent.type]) {
|
||||
if (nativeEvent.type === 'onAdFailedToLoad') {
|
||||
const { code, message } = nativeEvent.payload;
|
||||
this.props[nativeEvent.type](nativeToJSError(code, message));
|
||||
} else {
|
||||
this.props[nativeEvent.type](nativeEvent.payload || {});
|
||||
}
|
||||
}
|
||||
|
||||
if (nativeEvent.type === 'onSizeChange') this.updateSize(nativeEvent.payload);
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the JS size of the loaded banner
|
||||
* @param width
|
||||
* @param height
|
||||
*/
|
||||
updateSize = ({ width, height }) => {
|
||||
this.setState({ width, height });
|
||||
};
|
||||
|
||||
/**
|
||||
* Render the native component
|
||||
* @returns {XML}
|
||||
*/
|
||||
render() {
|
||||
console.log(this.props)
|
||||
return (
|
||||
<this.nativeView
|
||||
{...this.props}
|
||||
style={[this.props.style, { ...this.state }]}
|
||||
bannerEvent={this.onBannerEvent}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default AdMobComponent;
|
|
@ -3,9 +3,6 @@ export default class AdRequest {
|
|||
constructor() {
|
||||
this._props = {
|
||||
keywords: [],
|
||||
contentUrl: null,
|
||||
isDesignedForFamilies: null,
|
||||
tagForChildDirectedTreatment: null,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -29,18 +26,24 @@ export default class AdRequest {
|
|||
|
||||
setContentUrl(url: string) {
|
||||
this._props.contentUrl = url;
|
||||
return this;
|
||||
}
|
||||
|
||||
setGender() {
|
||||
// TODO
|
||||
setGender(gender: 'male | female | unknown') {
|
||||
const genders = ['male | female | unknown'];
|
||||
if (genders.includes(gender)) {
|
||||
this._props.gender = gender;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
setLocation() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
setRequestAgent() {
|
||||
// TODO
|
||||
setRequestAgent(requestAgent: string) {
|
||||
this._props.requestAgent = requestAgent;
|
||||
return this;
|
||||
}
|
||||
|
||||
setIsDesignedForFamilies(isDesignedForFamilies: boolean) {
|
||||
|
|
|
@ -1,82 +1,21 @@
|
|||
import React, { PropTypes } from 'react';
|
||||
import { requireNativeComponent, View } from 'react-native';
|
||||
import { statics } from './';
|
||||
import { nativeToJSError } from '../../utils';
|
||||
|
||||
class Banner extends React.Component {
|
||||
|
||||
static propTypes = {
|
||||
...View.propTypes,
|
||||
// TODO ehesp: cant init this outside of the component; statics isn't defined
|
||||
...(() => {
|
||||
const eventProps = {};
|
||||
Object.keys(statics.EventTypes).forEach((key) => {
|
||||
eventProps[key] = PropTypes.func;
|
||||
});
|
||||
return eventProps;
|
||||
}),
|
||||
size: PropTypes.string,
|
||||
unitId: PropTypes.string,
|
||||
testing: PropTypes.bool,
|
||||
};
|
||||
|
||||
static defaultProps = {
|
||||
size: 'SMART_BANNER',
|
||||
unitId: 'ca-app-pub-3940256099942544/6300978111', // Testing
|
||||
testing: true,
|
||||
};
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.state = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a single banner event and pass to
|
||||
* any props watching it
|
||||
* @param nativeEvent
|
||||
*/
|
||||
onBannerEvent = ({ nativeEvent }) => {
|
||||
if (this.props[nativeEvent.type]) {
|
||||
if (nativeEvent.type === 'onAdFailedToLoad') {
|
||||
const { code, message } = nativeEvent.payload;
|
||||
this.props[nativeEvent.type](nativeToJSError(code, message));
|
||||
} else {
|
||||
this.props[nativeEvent.type](nativeEvent.payload || {});
|
||||
}
|
||||
}
|
||||
|
||||
if (nativeEvent.type === 'onSizeChange') this.updateSize(nativeEvent.payload);
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle a native onSizeChange event
|
||||
* @param width
|
||||
* @param height
|
||||
*/
|
||||
updateSize = ({ width, height }) => {
|
||||
this.setState({ width, height });
|
||||
};
|
||||
|
||||
/**
|
||||
* Render the native component
|
||||
* @returns {XML}
|
||||
*/
|
||||
render() {
|
||||
return (
|
||||
<RNFirebaseAdMobBanner
|
||||
{...this.props}
|
||||
style={[this.props.style, { ...this.state }]}
|
||||
bannerEvent={this.onBannerEvent}
|
||||
/>
|
||||
);
|
||||
}
|
||||
import React from 'react';
|
||||
import { requireNativeComponent } from 'react-native';
|
||||
import AdMobComponent from './AdMobComponent';
|
||||
|
||||
function Banner({ ...props }) {
|
||||
return (
|
||||
<AdMobComponent
|
||||
{...props}
|
||||
class={'RNFirebaseAdMobBanner'}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const RNFirebaseAdMobBanner = requireNativeComponent('RNFirebaseAdMobBanner', Banner);
|
||||
Banner.propTypes = AdMobComponent.propTypes;
|
||||
|
||||
Banner.defaultProps = {
|
||||
unitId: 'ca-app-pub-3940256099942544/2177258514',
|
||||
size: 'SMART_BANNER',
|
||||
};
|
||||
|
||||
export default Banner;
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
import React from 'react';
|
||||
import { requireNativeComponent } from 'react-native';
|
||||
import AdMobComponent from './AdMobComponent';
|
||||
|
||||
function NativeExpress({ ...props }) {
|
||||
return (
|
||||
<AdMobComponent
|
||||
{...props}
|
||||
class={'RNFirebaseAdMobNativeExpress'}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
NativeExpress.propTypes = AdMobComponent.propTypes;
|
||||
|
||||
NativeExpress.defaultProps = {
|
||||
unitId: 'ca-app-pub-3940256099942544/2177258514',
|
||||
size: 'SMART_BANNER',
|
||||
};
|
||||
|
||||
export default NativeExpress;
|
|
@ -0,0 +1,17 @@
|
|||
export default class VideoOptions {
|
||||
|
||||
constructor() {
|
||||
this._props = {
|
||||
startMuted: true,
|
||||
};
|
||||
}
|
||||
|
||||
build() {
|
||||
return this._props;
|
||||
}
|
||||
|
||||
setStartMuted(muted: boolean = true) {
|
||||
this._props.startMuted = muted;
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -4,7 +4,9 @@ import { nativeSDKMissing } from './../../utils';
|
|||
import Interstitial from './Interstitial';
|
||||
import RewardedVideo from './RewardedVideo';
|
||||
import AdRequest from './AdRequest';
|
||||
import VideoOptions from './VideoOptions';
|
||||
import Banner from './Banner';
|
||||
import NativeExpress from './NativeExpress';
|
||||
import { Base } from './../base';
|
||||
|
||||
const FirebaseAdMob = NativeModules.RNFirebaseAdMob;
|
||||
|
@ -14,6 +16,7 @@ export default class Admob extends Base {
|
|||
|
||||
constructor() {
|
||||
super();
|
||||
console.log('>>', FirebaseAdMob)
|
||||
if (FirebaseAdMob.nativeSDKMissing) {
|
||||
return nativeSDKMissing('admob');
|
||||
}
|
||||
|
@ -59,7 +62,9 @@ export default class Admob extends Base {
|
|||
|
||||
export const statics = {
|
||||
Banner,
|
||||
NativeExpress,
|
||||
AdRequest,
|
||||
VideoOptions,
|
||||
EventTypes: {
|
||||
onAdLoaded: 'onAdLoaded',
|
||||
onAdOpened: 'onAdOpened',
|
||||
|
@ -71,4 +76,7 @@ export const statics = {
|
|||
onRewarded: 'onRewarded',
|
||||
onRewardedVideoStarted: 'onRewardedVideoStarted',
|
||||
},
|
||||
NativeExpressEventTypes: {
|
||||
onVideoEnd: 'onVideoEnd',
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue