Skip to content
This repository has been archived by the owner on May 19, 2021. It is now read-only.

Commit

Permalink
Include adType for ad events, fix #10
Browse files Browse the repository at this point in the history
  • Loading branch information
ratson committed Oct 16, 2016
1 parent 581179d commit 89ac13a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
58 changes: 47 additions & 11 deletions src/android/name/ratson/cordova/admob/AdMob.java
Original file line number Diff line number Diff line change
Expand Up @@ -633,47 +633,83 @@ public void run() {
* document.addEventListener('onDismissAd', function());
* document.addEventListener('onLeaveToAd', function());
*/
public class BasicListener extends AdListener {
abstract class BasicListener extends AdListener {
abstract String getAdType();

void fireAdEvent(String eventName) {
new CordovaEventBuilder(eventName).fire(webView);
}

void fireAdEvent(String eventName, JSONObject data) {
new CordovaEventBuilder(eventName).withData(data).fire(webView);
}

@Override
public void onAdFailedToLoad(int errorCode) {
webView.loadUrl(String.format(
"javascript:cordova.fireDocumentEvent('onFailedToReceiveAd', { 'error': %d, 'reason':'%s' });",
errorCode, getErrorReason(errorCode)));
JSONObject data = new JSONObject();
try {
data.put("error", errorCode);
data.put("reason", getErrorReason(errorCode));
data.put("adType", this.getAdType());
} catch (JSONException e) {
e.printStackTrace();
this.fireAdEvent("onFailedToReceiveAd");
return;
}
this.fireAdEvent("onFailedToReceiveAd", data);
}

@Override
public void onAdLeftApplication() {
webView.loadUrl("javascript:cordova.fireDocumentEvent('onLeaveToAd');");
JSONObject data = new JSONObject();
try {
data.put("adType", this.getAdType());
} catch (JSONException e) {
e.printStackTrace();
this.fireAdEvent("onLeaveToAd");
return;
}
this.fireAdEvent("onLeaveToAd", data);
}
}

private class BannerListener extends BasicListener {
@Override
String getAdType() {
return "banner";
}

@Override
public void onAdLoaded() {
Log.w("AdMob", "BannerAdLoaded");
if (autoShowBanner && !bannerVisible) {
executeShowAd(true, null);
}
webView.loadUrl("javascript:cordova.fireDocumentEvent('onReceiveAd');");
this.fireAdEvent("onReceiveAd");
}

@Override
public void onAdOpened() {
webView.loadUrl("javascript:cordova.fireDocumentEvent('onPresentAd');");
this.fireAdEvent("onPresentAd");
}

@Override
public void onAdClosed() {
webView.loadUrl("javascript:cordova.fireDocumentEvent('onDismissAd');");
this.fireAdEvent("onDismissAd");
}

}

private class InterstitialListener extends BasicListener {
@Override
String getAdType() {
return "interstitial";
}

@Override
public void onAdLoaded() {
Log.w("AdMob", "InterstitialAdLoaded");
webView.loadUrl("javascript:cordova.fireDocumentEvent('onReceiveInterstitialAd');");
this.fireAdEvent("onReceiveInterstitialAd");

if (autoShowInterstitial) {
executeShowInterstitialAd(true, null);
Expand All @@ -685,12 +721,12 @@ public void onAdLoaded() {

@Override
public void onAdOpened() {
webView.loadUrl("javascript:cordova.fireDocumentEvent('onPresentInterstitialAd');");
this.fireAdEvent("onPresentInterstitialAd");
}

@Override
public void onAdClosed() {
webView.loadUrl("javascript:cordova.fireDocumentEvent('onDismissInterstitialAd');");
this.fireAdEvent("onDismissInterstitialAd");
clearInterstitial();
}

Expand Down
12 changes: 9 additions & 3 deletions src/ios/CDVAdMob.m
Original file line number Diff line number Diff line change
Expand Up @@ -593,12 +593,13 @@ - (void)adViewDidReceiveAd:(GADBannerView *)adView {
}

- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error {
NSString* jsonData = [NSString stringWithFormat:@"{ 'error': '%@' }", [error localizedFailureReason]];
NSString* jsonData = [NSString stringWithFormat:@"{ 'error': '%@', 'adType':'banner' }", [error localizedFailureReason]];
[self fireEvent:@"" event:@"onFailedToReceiveAd" withData:jsonData];
}

- (void)adViewWillLeaveApplication:(GADBannerView *)adView {
[self fireEvent:@"" event:@"onLeaveToAd" withData:nil];
NSString* jsonData = @"{ 'adType':'banner' }";
[self fireEvent:@"" event:@"onLeaveToAd" withData:jsonData];
}

- (void)adViewWillPresentScreen:(GADBannerView *)adView {
Expand All @@ -613,10 +614,15 @@ - (void)adViewDidDismissScreen:(GADBannerView *)adView {

- (void)interstitial:(GADInterstitial *)ad
didFailToReceiveAdWithError:(GADRequestError *)error {
NSString* jsonData = [NSString stringWithFormat:@"{ 'error': '%@' }", [error localizedFailureReason]];
NSString* jsonData = [NSString stringWithFormat:@"{ 'error': '%@', 'adType':'interstitial' }", [error localizedFailureReason]];
[self fireEvent:@"" event:@"onFailedToReceiveAd" withData:jsonData];
}

- (void)interstitialWillLeaveApplication:(GADInterstitial *)interstitial {
NSString* jsonData = @"{ 'adType':'interstitial' }";
[self fireEvent:@"" event:@"onLeaveToAd" withData:jsonData];
}

- (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial {
[self fireEvent:@"" event:@"onReceiveInterstitialAd" withData:nil];
if (self.interstitialView){
Expand Down

0 comments on commit 89ac13a

Please sign in to comment.