Skip to content

Commit

Permalink
added AdsListener
Browse files Browse the repository at this point in the history
  • Loading branch information
chen-fishbein committed Mar 23, 2015
1 parent 8e1073e commit 147ca9c
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/nbproject/private/
/build/
Binary file modified dist/AdmobFullScreen.cn1lib
Binary file not shown.
38 changes: 36 additions & 2 deletions native/android/com/codename1/admob/AdMobNativeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,42 @@ public boolean loadAd() {
final CodenameOneActivity activity = (CodenameOneActivity)AndroidNativeUtil.getActivity();
interstitial = new InterstitialAd(activity);
interstitial.setAdUnitId(adID);
System.err.println("inside AdMobFullPageImpl.startLoadingAd");
interstitial.setAdListener(new AdListener() {

@Override
public void onAdClosed() {
super.onAdClosed();
Callback.onAdClosed();
}

@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
Callback.onAdFailedToLoad(errorCode);
}

@Override
public void onAdLeftApplication() {
super.onAdLeftApplication();
Callback.onAdLeftApplication();
}

@Override
public void onAdLoaded() {
super.onAdLoaded();
Callback.onAdLoaded();
}

@Override
public void onAdOpened() {
super.onAdOpened();
Callback.onAdOpened();
}



});

try {
// Create ad request.
final AdRequest adRequest = new AdRequest.Builder().build();
Expand All @@ -74,7 +109,6 @@ public boolean loadAd() {

public void run() {
interstitial.loadAd(adRequest);
System.err.println("start loading ad");
}
});

Expand Down
3 changes: 2 additions & 1 deletion native/ios/com_codename1_admob_AdMobNativeImpl.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#import <Foundation/Foundation.h>

#import "GADInterstitial.h"
#import "GADInterstitialDelegate.h"

@interface com_codename1_admob_AdMobNativeImpl : NSObject {
@interface com_codename1_admob_AdMobNativeImpl : NSObject<GADInterstitialDelegate> {
// Declare one as an instance variable
GADInterstitial *interstitial_;
BOOL loaded;
Expand Down
45 changes: 43 additions & 2 deletions native/ios/com_codename1_admob_AdMobNativeImpl.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#import "com_codename1_admob_AdMobNativeImpl.h"
#import "CodenameOne_GLViewController.h"
#import "com_codename1_admob_Callback.h"

@implementation com_codename1_admob_AdMobNativeImpl

-(void)init:(NSString*)param{
NSLog(@"inside native init");

adID = param;
id del = self;

[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
interstitial_ = [[GADInterstitial alloc] init];
interstitial_.adUnitID = adID;
interstitial_.delegate = del;
}];
}

Expand All @@ -37,11 +40,12 @@ -(BOOL)isLoaded{

-(BOOL)loadAd{
NSLog(@"inside native startLoadingAd");

id del = self;
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
interstitial_ = [[GADInterstitial alloc] init];
interstitial_.adUnitID = adID;

interstitial_.delegate = del;

// Your code goes in here
NSLog(@"start loading Ad");
[interstitial_ loadRequest:[GADRequest request]];
Expand All @@ -65,4 +69,41 @@ -(BOOL)isSupported{
return YES;
}

/// Called when an interstitial ad request succeeded.
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad {
NSLog(@"interstitialDidReceiveAd");
com_codename1_admob_Callback_onAdLoaded__(CN1_THREAD_GET_STATE_PASS_SINGLE_ARG);
}

/// Called when an interstitial ad request failed.
- (void)interstitial:(GADInterstitial *)ad didFailToReceiveAdWithError:(GADRequestError *)error {
NSLog(@"interstitialDidFailToReceiveAdWithError: %@", [error localizedDescription]);
com_codename1_admob_Callback_onAdFailedToLoad___int(CN1_THREAD_GET_STATE_PASS_ARG -1);
}

/// Called just before presenting an interstitial.
- (void)interstitialWillPresentScreen:(GADInterstitial *)ad {
NSLog(@"interstitialWillPresentScreen");
com_codename1_admob_Callback_onAdOpened__(CN1_THREAD_GET_STATE_PASS_SINGLE_ARG);
}

/// Called before the interstitial is to be animated off the screen.
- (void)interstitialWillDismissScreen:(GADInterstitial *)ad {
NSLog(@"interstitialWillDismissScreen");
}

/// Called just after dismissing an interstitial and it has animated off the screen.
- (void)interstitialDidDismissScreen:(GADInterstitial *)ad {
NSLog(@"interstitialDidDismissScreen");
com_codename1_admob_Callback_onAdClosed__(CN1_THREAD_GET_STATE_PASS_SINGLE_ARG);
}

/// Called just before the application will background or terminate because the user clicked on an
/// ad that will launch another application (such as the App Store).
- (void)interstitialWillLeaveApplication:(GADInterstitial *)ad {
NSLog(@"interstitialWillLeaveApplication");
com_codename1_admob_Callback_onAdLeftApplication__(CN1_THREAD_GET_STATE_PASS_SINGLE_ARG);
}


@end
8 changes: 8 additions & 0 deletions src/com/codename1/admob/AdMobManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,12 @@ public void run() {
});
admob.showAd();
}

/**
* Sets an AdsListener
*/
public void setAdsListener(AdsListener l) {
Callback.setListener(l);
}

}
39 changes: 39 additions & 0 deletions src/com/codename1/admob/AdsListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.codename1.admob;

/**
*
* @author Chen
*/
public interface AdsListener {

/**
* Called when the user is about to return to the application after clicking
* on an ad.
*/
public void onAdClosed();

/**
* Called when an ad request failed.
*/
public void onAdFailedToLoad(int errorCode);

/**
* Called when an ad leaves the application (e.g., to go to the browser).
*/
public void onAdLeftApplication();

/**
* Called when an ad is received.
*/
public void onAdLoaded();

/**
* Called when an ad opens an overlay that covers the screen.
*/
public void onAdOpened();
}
78 changes: 78 additions & 0 deletions src/com/codename1/admob/Callback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.codename1.admob;

import com.codename1.ui.Display;

/**
*
* @author Chen
*/
public class Callback {

private static AdsListener l;

public static void onAdClosed(){
if(l != null){
Display.getInstance().callSerially(new Runnable(){

public void run() {
l.onAdClosed();
}
});
}
}

public static void onAdLeftApplication(){
if(l != null){
Display.getInstance().callSerially(new Runnable(){

public void run() {
l.onAdLeftApplication();
}
});
}
}

public static void onAdOpened(){
if(l != null){
Display.getInstance().callSerially(new Runnable(){

public void run() {
l.onAdOpened();
}
});
}
}

public static void onAdFailedToLoad(final int err){
if(l != null){
Display.getInstance().callSerially(new Runnable(){

public void run() {
l.onAdFailedToLoad(err);
}
});
}
}


public static void onAdLoaded(){
if(l != null){
Display.getInstance().callSerially(new Runnable(){

public void run() {
l.onAdLoaded();
}
});
}
}

static void setListener(AdsListener listener){
l = listener;
}

}

0 comments on commit 147ca9c

Please sign in to comment.