[admob][android] Handle all AdListener events

This commit is contained in:
Elliot Hesp 2017-05-26 17:53:12 +01:00
parent cf18f85720
commit 5605610ba6
1 changed files with 64 additions and 1 deletions

View File

@ -2,23 +2,66 @@ package io.invertase.firebase.admob;
import android.app.Activity; import android.app.Activity;
import android.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableMap;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd; import com.google.android.gms.ads.InterstitialAd;
import io.invertase.firebase.Utils;
class RNFirebaseAdmobInterstitial { class RNFirebaseAdmobInterstitial {
private InterstitialAd interstitialAd; private InterstitialAd interstitialAd;
private RNFirebaseAdMob adMob; private RNFirebaseAdMob adMob;
private AdListener adListener;
private String adUnit;
RNFirebaseAdmobInterstitial(String adUnit, RNFirebaseAdMob adMobInstance) { RNFirebaseAdmobInterstitial(final String adUnitString, final RNFirebaseAdMob adMobInstance) {
adUnit = adUnitString;
adMob = adMobInstance; adMob = adMobInstance;
interstitialAd = new InterstitialAd(adMob.getContext()); interstitialAd = new InterstitialAd(adMob.getContext());
interstitialAd.setAdUnitId(adUnit); interstitialAd.setAdUnitId(adUnit);
adListener = new AdListener() {
@Override
public void onAdLoaded() {
sendEvent("onAdLoaded", null);
} }
@Override
public void onAdOpened() {
sendEvent("onAdOpened", null);
}
@Override
public void onAdLeftApplication() {
sendEvent("onAdLeftApplication", null);
}
@Override
public void onAdClosed() {
sendEvent("onAdClosed", null);
}
@Override
public void onAdFailedToLoad(int errorCode) {
WritableMap payload = RNFirebaseAdMobUtils.errorCodeToMap(errorCode);
sendEvent("onAdFailedToLoad", payload);
}
};
interstitialAd.setAdListener(adListener);
}
/**
* Load an Ad with a AdRequest instance
* @param adRequest
*/
void loadAd(final AdRequest adRequest) { void loadAd(final AdRequest adRequest) {
Activity activity = adMob.getActivity(); Activity activity = adMob.getActivity();
if (activity != null) { if (activity != null) {
@ -31,6 +74,9 @@ class RNFirebaseAdmobInterstitial {
} }
} }
/**
* Show the loaded interstitial, if it's loaded
*/
void show() { void show() {
Activity activity = adMob.getActivity(); Activity activity = adMob.getActivity();
if (activity != null) { if (activity != null) {
@ -44,4 +90,21 @@ class RNFirebaseAdmobInterstitial {
}); });
} }
} }
/**
* 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(), "interstitial_event", map);
}
} }