diff --git a/android/.gradle/6.5/executionHistory/executionHistory.lock b/android/.gradle/6.5/executionHistory/executionHistory.lock new file mode 100644 index 000000000..81785218e Binary files /dev/null and b/android/.gradle/6.5/executionHistory/executionHistory.lock differ diff --git a/android/.gradle/6.5/fileChanges/last-build.bin b/android/.gradle/6.5/fileChanges/last-build.bin new file mode 100644 index 000000000..f76dd238a Binary files /dev/null and b/android/.gradle/6.5/fileChanges/last-build.bin differ diff --git a/android/.gradle/6.5/fileHashes/fileHashes.bin b/android/.gradle/6.5/fileHashes/fileHashes.bin new file mode 100644 index 000000000..15e293aa9 Binary files /dev/null and b/android/.gradle/6.5/fileHashes/fileHashes.bin differ diff --git a/android/.gradle/6.5/fileHashes/fileHashes.lock b/android/.gradle/6.5/fileHashes/fileHashes.lock new file mode 100644 index 000000000..d378023f4 Binary files /dev/null and b/android/.gradle/6.5/fileHashes/fileHashes.lock differ diff --git a/android/.gradle/6.5/gc.properties b/android/.gradle/6.5/gc.properties new file mode 100644 index 000000000..e69de29bb diff --git a/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock new file mode 100644 index 000000000..abf17a032 Binary files /dev/null and b/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/android/.gradle/buildOutputCleanup/cache.properties b/android/.gradle/buildOutputCleanup/cache.properties new file mode 100644 index 000000000..f53a01494 --- /dev/null +++ b/android/.gradle/buildOutputCleanup/cache.properties @@ -0,0 +1,2 @@ +#Sat Nov 28 16:35:28 IRST 2020 +gradle.version=6.5 diff --git a/android/.gradle/buildOutputCleanup/outputFiles.bin b/android/.gradle/buildOutputCleanup/outputFiles.bin new file mode 100644 index 000000000..a3f9135f6 Binary files /dev/null and b/android/.gradle/buildOutputCleanup/outputFiles.bin differ diff --git a/android/.gradle/checksums/checksums.lock b/android/.gradle/checksums/checksums.lock new file mode 100644 index 000000000..6ccdf527c Binary files /dev/null and b/android/.gradle/checksums/checksums.lock differ diff --git a/android/.gradle/checksums/md5-checksums.bin b/android/.gradle/checksums/md5-checksums.bin new file mode 100644 index 000000000..c3201dc7b Binary files /dev/null and b/android/.gradle/checksums/md5-checksums.bin differ diff --git a/android/.gradle/checksums/sha1-checksums.bin b/android/.gradle/checksums/sha1-checksums.bin new file mode 100644 index 000000000..7f2107761 Binary files /dev/null and b/android/.gradle/checksums/sha1-checksums.bin differ diff --git a/android/.gradle/vcs-1/gc.properties b/android/.gradle/vcs-1/gc.properties new file mode 100644 index 000000000..e69de29bb diff --git a/android/build.gradle b/android/build.gradle index 0b89985e2..ba1f9d5f1 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -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 { @@ -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:+' diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index 40190d307..85f2402fa 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -1,3 +1,5 @@ + + diff --git a/android/src/main/java/com/sbugert/rnadmob/RNAdMobInterstitialAdModule.java b/android/src/main/java/com/sbugert/rnadmob/RNAdMobInterstitialAdModule.java index bad5a0aec..7f6e8bf95 100644 --- a/android/src/main/java/com/sbugert/rnadmob/RNAdMobInterstitialAdModule.java +++ b/android/src/main/java/com/sbugert/rnadmob/RNAdMobInterstitialAdModule.java @@ -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; @@ -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 mInterstitialAds; String[] testDevices; - private Promise mRequestAdPromise; + private final Map mRequestAdPromises; @Override public String getName() { @@ -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); + } + }); + } + }); } } @@ -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."); @@ -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); + } } }); }