Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple interstitial #550

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file not shown.
Binary file added android/.gradle/6.5/fileChanges/last-build.bin
Binary file not shown.
Binary file added android/.gradle/6.5/fileHashes/fileHashes.bin
Binary file not shown.
Binary file added android/.gradle/6.5/fileHashes/fileHashes.lock
Binary file not shown.
Empty file.
Binary file not shown.
2 changes: 2 additions & 0 deletions android/.gradle/buildOutputCleanup/cache.properties
@@ -0,0 +1,2 @@
#Sat Nov 28 16:35:28 IRST 2020
gradle.version=6.5
Binary file not shown.
Binary file added android/.gradle/checksums/checksums.lock
Binary file not shown.
Binary file added android/.gradle/checksums/md5-checksums.bin
Binary file not shown.
Binary file added android/.gradle/checksums/sha1-checksums.bin
Binary file not shown.
Empty file.
36 changes: 33 additions & 3 deletions android/build.gradle
Expand Up @@ -4,13 +4,28 @@ def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

buildscript {
if (project == rootProject) {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.1'
}
}
repositories {
mavenCentral()
}
}

android {
compileSdkVersion safeExtGet('compileSdkVersion', 23)
buildToolsVersion safeExtGet('buildToolsVersion', "23.0.1")
compileSdkVersion safeExtGet('compileSdkVersion', 29)
buildToolsVersion safeExtGet('buildToolsVersion', "29.0.2")

defaultConfig {
minSdkVersion 16
targetSdkVersion safeExtGet('targetSdkVersion', 22)
targetSdkVersion safeExtGet('targetSdkVersion', 29)
}
buildTypes {
release {
Expand All @@ -20,6 +35,21 @@ android {
}
}

repositories {
// ref: https://www.baeldung.com/maven-local-repository
mavenLocal()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
maven {
// Android JSC is installed from npm
url "$rootDir/../node_modules/jsc-android/dist"
}
google()
jcenter()
}

dependencies {
implementation 'com.facebook.react:react-native:+'
implementation 'com.google.android.gms:play-services-ads:+'
Expand Down
2 changes: 2 additions & 0 deletions android/src/main/AndroidManifest.xml
@@ -1,3 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sbugert.rnadmob">

<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Expand Up @@ -3,6 +3,7 @@
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.util.Pair;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
Expand Down Expand Up @@ -33,10 +34,11 @@ public class RNAdMobInterstitialAdModule extends ReactContextBaseJavaModule {
public static final String EVENT_AD_CLOSED = "interstitialAdClosed";
public static final String EVENT_AD_LEFT_APPLICATION = "interstitialAdLeftApplication";

InterstitialAd mInterstitialAd;
ReactApplicationContext mContext;
Map<String, InterstitialAd> mInterstitialAds;
String[] testDevices;

private Promise mRequestAdPromise;
private final Map<String, Promise> mRequestAdPromises;

@Override
public String getName() {
Expand All @@ -45,75 +47,89 @@ public String getName() {

public RNAdMobInterstitialAdModule(ReactApplicationContext reactContext) {
super(reactContext);
mInterstitialAd = new InterstitialAd(reactContext);

new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
sendEvent(EVENT_AD_CLOSED, null);
}
@Override
public void onAdFailedToLoad(int errorCode) {
String errorString = "ERROR_UNKNOWN";
String errorMessage = "Unknown error";
switch (errorCode) {
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
errorString = "ERROR_CODE_INTERNAL_ERROR";
errorMessage = "Internal error, an invalid response was received from the ad server.";
break;
case AdRequest.ERROR_CODE_INVALID_REQUEST:
errorString = "ERROR_CODE_INVALID_REQUEST";
errorMessage = "Invalid ad request, possibly an incorrect ad unit ID was given.";
break;
case AdRequest.ERROR_CODE_NETWORK_ERROR:
errorString = "ERROR_CODE_NETWORK_ERROR";
errorMessage = "The ad request was unsuccessful due to network connectivity.";
break;
case AdRequest.ERROR_CODE_NO_FILL:
errorString = "ERROR_CODE_NO_FILL";
errorMessage = "The ad request was successful, but no ad was returned due to lack of ad inventory.";
break;
}
WritableMap event = Arguments.createMap();
WritableMap error = Arguments.createMap();
event.putString("message", errorMessage);
sendEvent(EVENT_AD_FAILED_TO_LOAD, event);
if (mRequestAdPromise != null) {
mRequestAdPromise.reject(errorString, errorMessage);
mRequestAdPromise = null;
}
}
@Override
public void onAdLeftApplication() {
sendEvent(EVENT_AD_LEFT_APPLICATION, null);
}
@Override
public void onAdLoaded() {
sendEvent(EVENT_AD_LOADED, null);
if (mRequestAdPromise != null) {
mRequestAdPromise.resolve(null);
mRequestAdPromise = null;
}
}
@Override
public void onAdOpened() {
sendEvent(EVENT_AD_OPENED, null);
}
});
}
});
mContext = reactContext;
mInterstitialAds = new HashMap<>();
mRequestAdPromises = new HashMap<>();
}

private void sendEvent(String eventName, @Nullable WritableMap params) {
getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
}

@ReactMethod
public void setAdUnitID(String adUnitID) {
if (mInterstitialAd.getAdUnitId() == null) {
mInterstitialAd.setAdUnitId(adUnitID);
public void setAdUnitID(final String adUnitID) {
if (!mInterstitialAds.containsKey(adUnitID)) {
mInterstitialAds.put(adUnitID, new InterstitialAd(mContext));
mInterstitialAds.get(adUnitID).setAdUnitId(adUnitID);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
mInterstitialAds.get(adUnitID).setAdListener(new AdListener() {
@Override
public void onAdClosed() {
WritableMap params = Arguments.createMap();
params.putString("adUnitId", adUnitID);
sendEvent(EVENT_AD_CLOSED, params);
}
@Override
public void onAdFailedToLoad(int errorCode) {
String errorString = "ERROR_UNKNOWN";
String errorMessage = "Unknown error";
switch (errorCode) {
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
errorString = "ERROR_CODE_INTERNAL_ERROR";
errorMessage = "Internal error, an invalid response was received from the ad server.";
break;
case AdRequest.ERROR_CODE_INVALID_REQUEST:
errorString = "ERROR_CODE_INVALID_REQUEST";
errorMessage = "Invalid ad request, possibly an incorrect ad unit ID was given.";
break;
case AdRequest.ERROR_CODE_NETWORK_ERROR:
errorString = "ERROR_CODE_NETWORK_ERROR";
errorMessage = "The ad request was unsuccessful due to network connectivity.";
break;
case AdRequest.ERROR_CODE_NO_FILL:
errorString = "ERROR_CODE_NO_FILL";
errorMessage = "The ad request was successful, but no ad was returned due to lack of ad inventory.";
break;
}
WritableMap event = Arguments.createMap();
WritableMap error = Arguments.createMap();
event.putString("message", errorMessage);
event.putString("adUnitId", adUnitID);
sendEvent(EVENT_AD_FAILED_TO_LOAD, event);
if (mRequestAdPromises.get(adUnitID) != null) {
mRequestAdPromises.get(adUnitID).reject(errorString, errorMessage);
// todo:: check how to set promise to null
mRequestAdPromises.put(adUnitID, null);
}
}
@Override
public void onAdLeftApplication() {
WritableMap params = Arguments.createMap();
params.putString("adUnitId", adUnitID);
sendEvent(EVENT_AD_LEFT_APPLICATION, params);
}
@Override
public void onAdLoaded() {
WritableMap params = Arguments.createMap();
params.putString("adUnitId", adUnitID);
sendEvent(EVENT_AD_LOADED, params);
if (mRequestAdPromises.get(adUnitID) != null) {
mRequestAdPromises.get(adUnitID).resolve(null);
// todo:: check how to set promise to null
mRequestAdPromises.put(adUnitID, null);
}
}
@Override
public void onAdOpened() {
WritableMap params = Arguments.createMap();
params.putString("adUnitId", adUnitID);
sendEvent(EVENT_AD_OPENED, params);
}
});
}
});
}
}

Expand All @@ -125,38 +141,40 @@ public void setTestDevices(ReadableArray testDevices) {
}

@ReactMethod
public void requestAd(final Promise promise) {
public void requestAd(final String adUnitId, final Promise promise) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run () {
if (mInterstitialAd.isLoaded() || mInterstitialAd.isLoading()) {
promise.reject("E_AD_ALREADY_LOADED", "Ad is already loaded.");
if (mInterstitialAds.containsKey(adUnitId)) {
if (mInterstitialAds.get(adUnitId).isLoaded() || mInterstitialAds.get(adUnitId).isLoading()) {
promise.reject("E_AD_ALREADY_LOADED", "Ad is already loaded.");
} else {
mRequestAdPromise = promise;
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
if (testDevices != null) {
for (int i = 0; i < testDevices.length; i++) {
String testDevice = testDevices[i];
if (testDevice == "SIMULATOR") {
testDevice = AdRequest.DEVICE_ID_EMULATOR;
}
adRequestBuilder.addTestDevice(testDevice);
}
mRequestAdPromises.put(adUnitId, promise);
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
if (testDevices != null) {
for (int i = 0; i < testDevices.length; i++) {
String testDevice = testDevices[i];
if (testDevice == "SIMULATOR") {
testDevice = AdRequest.DEVICE_ID_EMULATOR;
}
adRequestBuilder.addTestDevice(testDevice);
}
AdRequest adRequest = adRequestBuilder.build();
mInterstitialAd.loadAd(adRequest);
}
AdRequest adRequest = adRequestBuilder.build();
mInterstitialAds.get(adUnitId).loadAd(adRequest);
}
}
}
});
}

@ReactMethod
public void showAd(final Promise promise) {
public void showAd(final String adUnitId, final Promise promise) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run () {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
if (mInterstitialAds.get(adUnitId).isLoaded()) {
mInterstitialAds.get(adUnitId).show();
promise.resolve(null);
} else {
promise.reject("E_AD_NOT_READY", "Ad is not ready.");
Expand All @@ -166,11 +184,15 @@ public void run () {
}

@ReactMethod
public void isReady(final Callback callback) {
public void isReady(final String adUnitId, final Callback callback) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run () {
callback.invoke(mInterstitialAd.isLoaded());
if (mInterstitialAds.containsKey(adUnitId)){
callback.invoke(mInterstitialAds.get(adUnitId).isLoaded());
} else {
callback.invoke(false);
}
}
});
}
Expand Down