Skip to content

Commit

Permalink
Merge pull request #66 from garymathews/TIMOB-24473
Browse files Browse the repository at this point in the history
[TIMOB-24473] Android: Maintain API parity with iOS
  • Loading branch information
garymathews authored Nov 5, 2018
2 parents ba7b5c1 + bdf1c40 commit 6342686
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 43 deletions.
3 changes: 3 additions & 0 deletions android/documentation/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Change Log

<pre>

v4.4.0 Maintain API parity with iOS module and introduce Interstitial ads support on Android.

v4.3.0 Support the Facebook Audience Network adapter
Android integration guide: https://developers.google.com/admob/android/mediation/facebook

Expand Down
106 changes: 99 additions & 7 deletions android/documentation/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ parameters[object]: a dictionary object of properties.
#### Example:

var adMobView = Admob.createView({
publisherId: '<<YOUR PUBLISHER ID HERE>>',
testing: false, // default is false
adUnitId: 'ENTER_YOUR_AD_UNIT_ID_HERE',
testing:false, // default is false
top: 0, // optional
left: 0, // optional
right: 0, // optional
Expand All @@ -79,12 +79,41 @@ returns the constant for AD_RECEIVED -- for use in an event listener

### `Admob.AD_NOT_RECEIVED`

returns the constant for AD_NOT_RECEIVED -- for use in an event listener
returns whenever the ad was not successfully loaded. The callback contains the
error code in its parameter under the key `errorCode`
Error codes for Android can be checked here:
https://developers.google.com/android/reference/com/google/android/gms/ads/AdRequest#ERROR_CODE_INTERNAL_ERROR

#### Example:

adMobView.addEventListener(Admob.AD_NOT_RECEIVED, function () {
alert('ad was not received');
adMobView.addEventListener(Admob.AD_NOT_RECEIVED, function (e) {
alert('ad was not received. error code is ' + e.errorCode);
});

### `Admob.AD_OPENED`

returns the constant for AD_OPENED -- for use in an event listener

#### Example:

adMobView.addEventListener(Admob.AD_OPENED, function () {
alert('ad was just opened');
});

### `Admob.AD_CLOSED`

#### Example:

adMobView.addEventListener(Admob.AD_CLOSED, function () {
alert('ad was just closed');
});

### `Admob.AD_LEFT_APPLICATION`

#### Example:

adMobView.addEventListener(Admob.AD_LEFT_APPLICATION, function () {
alert('user just left the application through the ad');
});

### `AdMobView.requestAd(args)`
Expand Down Expand Up @@ -231,13 +260,76 @@ View the [change log](changelog.html) for this module.

Please direct all questions, feedback, and concerns to [info@appcelerator.com](mailto:info@appcelerator.com?subject=Android%20Admob%20Module).

### Interstitial ads

Starting from 4.4.0 this module now supports Interstitial ads for Android.

Interstitial ads are full screen ads that are usually shown between natural steps of an application's interface flow.
For instance doing different tasks in your application or between reading different articles.

For best user experience Interstitial ads should be loaded prior showing the to the user. Interstitial ad instances can
be used for showing one ad per loading, but they can be used multiple times. A good way of reusing an Interstitial ad is
to show an ad, load a new after it has been closed one, and await for the proper time to show the recently loaded.

#### Properties

##### adUnitId

Id for this add. This property can be set in the creation dictionary or after creating the Interstitial ad instance.

#### Methods

##### setAdUnitId(String id)

Sets the adUnitId property.

##### getAdUnitId()

Gets the adUnitId property.

##### load()

Loads an ad for this Interstitial ad instance.

##### show()

Shows an Interstitial ad if there is one successfully loaded.

#### Example:

// Create an Interstitial ad with a testing AdUnitId
var interstitialAd = Admob.createInterstitialAd({ adUnitId:"ca-app-pub-3940256099942544/1033173712" });

// Add all listeners for the add.
interstitialAd.addEventListener(Admob.AD_CLOSED, function () {
Ti.API.info('Interstitial Ad closed!');
});
interstitialAd.addEventListener(Admob.AD_RECEIVED, function () {
// When a new Interstitial ad is loaded, show it.
Ti.API.info('Interstitial Ad loaded!');
interstitialAd.show();
});
interstitialAd.addEventListener(Admob.AD_CLICKED, function () {
Ti.API.info('Interstitial Ad clicked!');
});
interstitialAd.addEventListener(Admob.AD_NOT_RECEIVED, function (e) {
Ti.API.info('Interstitial Ad not received! Error code = ' + e.errorCode);
});
interstitialAd.addEventListener(Admob.AD_OPENED, function () {
Ti.API.info('Interstitial Ad opened!');
});
interstitialAd.addEventListener(Admob.AD_LEFT_APPLICATION, function () {
Ti.API.info('Interstitial Ad left application!');
});
interstitialAd.load();

## Author

Brian Kurzius | bkurzius@gmail.com
Axway Appcelerator

## License
Copyright 2011 Brian Kurzius, Studio Classics.
Copyright 2017-present, Axway Appcelerator.
Copyright 2011, Brian Kurzius, Studio Classics.
Copyright 2014 - Present, Appcelerator.

Please see the LICENSE file included in the distribution for further details.
58 changes: 53 additions & 5 deletions android/example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

var win = Titanium.UI.createWindow({
backgroundColor: "#FFFFFF"
backgroundColor: "#FFFFFF"
});

// require AdMob
Expand All @@ -20,7 +20,7 @@ if (code != Admob.SUCCESS) {

// then create an adMob view
var adMobView = Admob.createView({
publisherId:"<<YOUR PUBLISHER ID HERE>>",
adUnitId:"ca-app-pub-3940256099942544/6300978111",
testing:false, // default is false
//top: 10, //optional
//left: 0, // optional
Expand All @@ -37,13 +37,49 @@ var adMobView = Admob.createView({

});

// Create an Interstitial ad with a testing AdUnitId
var interstitialAd = Admob.createInterstitialAd({ adUnitId:"ca-app-pub-3940256099942544/1033173712" });

// Add all listeners for the add.
interstitialAd.addEventListener(Admob.AD_CLOSED, function () {
Ti.API.info('Interstitial Ad closed!');
// Once the Interstitial ad is closed disable the button
// until another add is successfully loaded.
showInterstitialAdButton.enabled = false;
showInterstitialAdButton.touchEnabled = false;
interstitialAd.load();
});
interstitialAd.addEventListener(Admob.AD_RECEIVED, function () {
// When a new Interstitial ad is loaded, enabled the button.
Ti.API.info('Interstitial Ad loaded!');
showInterstitialAdButton.enabled = true;
showInterstitialAdButton.touchEnabled = true;
});
interstitialAd.addEventListener(Admob.AD_CLICKED, function () {
Ti.API.info('Interstitial Ad clicked!');
});
interstitialAd.addEventListener(Admob.AD_NOT_RECEIVED, function (e) {
Ti.API.info('Interstitial Ad not received! Error code = ' + e.errorCode);
});
interstitialAd.addEventListener(Admob.AD_OPENED, function () {
Ti.API.info('Interstitial Ad opened!');
});
interstitialAd.addEventListener(Admob.AD_LEFT_APPLICATION, function () {
Ti.API.info('Interstitial Ad left application!');
});
interstitialAd.load();

//listener for adReceived
adMobView.addEventListener(Admob.AD_RECEIVED,function(){
// alert("ad received");
Ti.API.info("ad received");
});

adMobView.addEventListener('ad_clicked', function(){
// alert("ad received");
Ti.API.info("ad clicked");
});

//listener for adNotReceived
adMobView.addEventListener(Admob.AD_NOT_RECEIVED,function(){
//alert("ad not received");
Expand Down Expand Up @@ -73,16 +109,23 @@ adRequestBtn2.addEventListener("click",function(){
adMobView.requestTestAd();
});

var showInterstitialAdButton = Ti.UI.createButton({
title: "Show Interstitial",
top: "40%",
height: "10%",
width: "80%"
})

var getAAID = Ti.UI.createButton({
title: "Get AAID",
top: "40%",
top: "55%",
height: "10%",
width: "80%"
});

var getIsAdTrackingLimited = Ti.UI.createButton({
title: "Is Ad tracking limited",
top: "55%",
top: "70%",
height: "10%",
width: "80%"
});
Expand All @@ -97,11 +140,16 @@ getIsAdTrackingLimited.addEventListener('click', function() {
Admob.isLimitAdTrackingEnabled(function (data) {
Ti.API.info('Ad tracking is limited: ' + data.isLimitAdTrackingEnabled);
});
})
});

showInterstitialAdButton.addEventListener('click', function () {
interstitialAd.show();
});

win.add(adMobView);
win.add(adRequestBtn);
win.add(adRequestBtn2);
win.add(showInterstitialAdButton);
win.add(getAAID);
win.add(getIsAdTrackingLimited)
win.open();
2 changes: 1 addition & 1 deletion android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# during compilation, packaging, distribution, etc.
#

version: 4.3.0
version: 4.4.0
apiversion: 4
architectures: arm64-v8a armeabi-v7a x86
description: Titanium Admob module for Android
Expand Down
13 changes: 11 additions & 2 deletions android/src/ti/admob/AdmobModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,17 @@ public class AdmobModule extends KrollModule

public static String PUBLISHER_ID;

// *
// properties
public static String PROPERTY_AD_UNIT_ID = "adUnitId";
public static String PROPERTY_DEBUG_ENABLED = "debugEnabled";
public static String PROPERTY_TESTING = "testing";
public static String PROPERTY_PUBLISHER_ID = "publisherId";
public static String PROPERTY_COLOR_BG = "adBackgroundColor";
public static String PROPERTY_COLOR_BG_TOP = "backgroundTopColor";
public static String PROPERTY_COLOR_BORDER = "borderColor";
public static String PROPERTY_COLOR_TEXT = "textColor";
public static String PROPERTY_COLOR_LINK = "linkColor";
public static String PROPERTY_COLOR_URL = "urlColor";

public static String PROPERTY_COLOR_TEXT_DEPRECATED = "primaryTextColor";
public static String PROPERTY_COLOR_LINK_DEPRECATED = "secondaryTextColor";

Expand All @@ -87,6 +90,12 @@ public AdmobModule()
public static final String AD_RECEIVED = "ad_received";
@Kroll.constant
public static final String AD_NOT_RECEIVED = "ad_not_received";
@Kroll.constant
public static final String AD_CLOSED = "ad_closed";
@Kroll.constant
public static final String AD_OPENED = "ad_opened";
@Kroll.constant
public static final String AD_LEFT_APPLICATION = "ad_left_application";

// Response from "isGooglePlayServicesAvailable()""
@Kroll.constant
Expand Down
59 changes: 59 additions & 0 deletions android/src/ti/admob/CommonAdListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (c) 2011 by Studio Classics. All Rights Reserved.
* Copyright (c) 2017-present by Axway Appcelerator. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/

package ti.admob;

import com.google.android.gms.ads.AdListener;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.common.Log;

public class CommonAdListener extends AdListener {

private KrollProxy sourceProxy;
private String sourceTag;

public CommonAdListener(KrollProxy sourceProxy, String tag) {
this.sourceProxy = sourceProxy;
this.sourceTag = tag;
}

@Override
public void onAdLoaded() {
Log.d(this.sourceTag, "onAdLoaded()");
this.sourceProxy.fireEvent(AdmobModule.AD_RECEIVED, new KrollDict());
}

@Override
public void onAdFailedToLoad(int errorCode)
{
super.onAdFailedToLoad(errorCode);
Log.d(this.sourceTag, "onAdFailedToLoad(): " + errorCode);
KrollDict eventData = new KrollDict();
eventData.put("errorCode", String.valueOf(errorCode));
this.sourceProxy.fireEvent(AdmobModule.AD_NOT_RECEIVED, eventData);
}

@Override
public void onAdClosed() {
super.onAdClosed();
this.sourceProxy.fireEvent(AdmobModule.AD_CLOSED, new KrollDict());
}

@Override
public void onAdOpened() {
super.onAdOpened();
this.sourceProxy.fireEvent(AdmobModule.AD_OPENED, new KrollDict());
}

@Override
public void onAdLeftApplication() {
super.onAdLeftApplication();
this.sourceProxy.fireEvent(AdmobModule.AD_LEFT_APPLICATION, new KrollDict());
}
}
Loading

0 comments on commit 6342686

Please sign in to comment.