Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fixing admob ios native script 4 #40

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 84 additions & 66 deletions src/admob.android.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var utils = require("utils/utils");
var application = require("application");
var frame = require("ui/frame");
var admob = require("./admob-common");
var utils = require('utils/utils');
var application = require('application');
var frame = require('ui/frame');
var admob = require('./admob-common');

admob._getBannerType = function (size) {
admob._getBannerType = function(size) {
if (size == admob.AD_SIZE.BANNER) {
return com.google.android.gms.ads.AdSize.BANNER;
} else if (size == admob.AD_SIZE.LARGE_BANNER) {
Expand All @@ -16,7 +16,7 @@ admob._getBannerType = function (size) {
return com.google.android.gms.ads.AdSize.FLUID;
} else if (size == admob.AD_SIZE.LEADERBOARD) {
// doesn't seem to work on Android - using large instead
//return com.google.android.gms.ads.AdSize.LEADERBOARD;
// return com.google.android.gms.ads.AdSize.LEADERBOARD;
return com.google.android.gms.ads.AdSize.LARGE_BANNER;
} else if (size == admob.AD_SIZE.SKYSCRAPER) {
return com.google.android.gms.ads.AdSize.WIDE_SKYSCRAPER;
Expand All @@ -27,9 +27,9 @@ admob._getBannerType = function (size) {
}
};

admob._md5 = function (input) {
admob._md5 = function(input) {
try {
var digest = java.security.MessageDigest.getInstance("MD5");
var digest = java.security.MessageDigest.getInstance('MD5');
var bytes = [];
for (var j = 0; j < input.length; ++j) {
bytes.push(input.charCodeAt(j));
Expand All @@ -38,40 +38,44 @@ admob._md5 = function (input) {
var s = new java.lang.String(input);
digest.update(s.getBytes());
var messageDigest = digest.digest();
var hexString = "";
var hexString = '';
for (var i = 0; i < messageDigest.length; i++) {
var h = java.lang.Integer.toHexString(0xFF & messageDigest[i]);
while (h.length < 2)
h = "0" + h;
while (h.length < 2) h = '0' + h;
hexString += h;
}
return hexString;

} catch (noSuchAlgorithmException) {
console.log("error generating md5: " + noSuchAlgorithmException);
console.log('error generating md5: ' + noSuchAlgorithmException);
return null;
}
};

// need to cache this baby since after an Interstitial was shown a second won't resolve the activity
// need to cache this baby since after an Interstitial was shown a second won't
// resolve the activity
admob.activity = null;
admob._getActivity = function () {
admob._getActivity = function() {
if (admob.activity === null) {
admob.activity = application.android.foregroundActivity;
}
return admob.activity;
};

admob._buildAdRequest = function (settings) {
admob._buildAdRequest = function(settings) {
var builder = new com.google.android.gms.ads.AdRequest.Builder();
if (settings.testing) {
builder.addTestDevice(com.google.android.gms.ads.AdRequest.DEVICE_ID_EMULATOR);
// This will request test ads on the emulator and device by passing this hashed device ID.
var ANDROID_ID = android.provider.Settings.Secure.getString(admob._getActivity().getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
builder.addTestDevice(
com.google.android.gms.ads.AdRequest.DEVICE_ID_EMULATOR);
// This will request test ads on the emulator and device by passing this
// hashed device ID.
var ANDROID_ID = android.provider.Settings.Secure.getString(
admob._getActivity().getContentResolver(),
android.provider.Settings.Secure.ANDROID_ID);
var deviceId = admob._md5(ANDROID_ID);
if (deviceId !== null) {
deviceId = deviceId.toUpperCase();
console.log("Treating this deviceId as testdevice: " + deviceId);
console.log('Treating this deviceId as testdevice: ' + deviceId);
builder.addTestDevice(deviceId);
}
}
Expand All @@ -83,14 +87,15 @@ admob._buildAdRequest = function (settings) {
}

var bundle = new android.os.Bundle();
bundle.putInt("nativescript", 1);
var adextras = new com.google.android.gms.ads.mediation.admob.AdMobExtras(bundle);
//builder = builder.addNetworkExtras(adextras);
bundle.putInt('nativescript', 1);
var adextras =
new com.google.android.gms.ads.mediation.admob.AdMobExtras(bundle);
// builder = builder.addNetworkExtras(adextras);
return builder.build();
};

admob.createBanner = function (arg) {
return new Promise(function (resolve, reject) {
admob.createBanner = function(arg) {
return new Promise(function(resolve, reject) {
try {
// always close a previous opened banner
if (admob.adView !== null && admob.adView !== undefined) {
Expand All @@ -100,12 +105,14 @@ admob.createBanner = function (arg) {
}
}
var settings = admob.merge(arg, admob.defaults);
admob.adView = new com.google.android.gms.ads.AdView(admob._getActivity());
admob.adView =
new com.google.android.gms.ads.AdView(admob._getActivity());
admob.adView.setAdUnitId(settings.androidBannerId);
var bannerType = admob._getBannerType(settings.size);
admob.adView.setAdSize(bannerType);
// TODO consider implementing events, after v1
//admob.adView.setAdListener(new com.google.android.gms.ads.BannerListener());
// admob.adView.setAdListener(new
// com.google.android.gms.ads.BannerListener());

var ad = admob._buildAdRequest(settings);
admob.adView.loadAd(ad);
Expand All @@ -120,81 +127,92 @@ admob.createBanner = function (arg) {

if (bottom > -1) {
relativeLayoutParams.bottomMargin = bottom;
relativeLayoutParams.addRule(android.widget.RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayoutParams.addRule(
android.widget.RelativeLayout.ALIGN_PARENT_BOTTOM);
} else {
if (top > -1) {
relativeLayoutParams.topMargin = top;
}
relativeLayoutParams.addRule(android.widget.RelativeLayout.ALIGN_PARENT_TOP);
relativeLayoutParams.addRule(
android.widget.RelativeLayout.ALIGN_PARENT_TOP);
}

var adViewLayout = new android.widget.RelativeLayout(admob._getActivity());
var adViewLayout =
new android.widget.RelativeLayout(admob._getActivity());
adViewLayout.addView(admob.adView, relativeLayoutParams);

var relativeLayoutParamsOuter = new android.widget.RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT);
var relativeLayoutParamsOuter =
new android.widget.RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT);

// Wrapping it in a timeout makes sure that when this function is loaded from a Page.loaded event 'frame.topmost()' doesn't resolve to 'undefined'.
// Also, in NativeScript 4+ it may be undefined anyway.. so using the appModule in that case.
setTimeout(function () {
// Wrapping it in a timeout makes sure that when this function is loaded
// from a Page.loaded event 'frame.topmost()' doesn't resolve to
// 'undefined'. Also, in NativeScript 4+ it may be undefined anyway.. so
// using the appModule in that case.
setTimeout(function() {
var topmost = frame.topmost();
if (topmost !== undefined) {
topmost.currentPage &&
topmost.currentPage.android &&
topmost.currentPage.android.getParent() &&
topmost.currentPage.android.getParent().addView(adViewLayout, relativeLayoutParamsOuter);
topmost.currentPage && topmost.currentPage.android &&
topmost.currentPage.android.getParent() &&
topmost.currentPage.android.getParent().addView(
adViewLayout, relativeLayoutParamsOuter);
} else {
application.android &&
application.android.foregroundActivity &&
application.android.foregroundActivity.getWindow() &&
application.android.foregroundActivity.getWindow().getDecorView() &&
application.android.foregroundActivity.getWindow().getDecorView().addView(adViewLayout, relativeLayoutParamsOuter);
application.android && application.android.foregroundActivity &&
application.android.foregroundActivity.getWindow() &&
application.android.foregroundActivity.getWindow()
.getDecorView() &&
application.android.foregroundActivity.getWindow()
.getDecorView()
.addView(adViewLayout, relativeLayoutParamsOuter);
}
}, 0);

resolve();
} catch (ex) {
console.log("Error in admob.createBanner: " + ex);
console.log('Error in admob.createBanner: ' + ex);
reject(ex);
}
});
};

admob.createInterstitial = function (arg) {
return new Promise(function (resolve, reject) {
admob.createInterstitial = function(arg) {
return new Promise(function(resolve, reject) {
try {
var settings = admob.merge(arg, admob.defaults);
admob.interstitialView = new com.google.android.gms.ads.InterstitialAd(admob._getActivity());
admob.interstitialView =
new com.google.android.gms.ads.InterstitialAd(admob._getActivity());
admob.interstitialView.setAdUnitId(settings.androidInterstitialId);

// Interstitial ads must be loaded before they can be shown, so adding a listener
var InterstitialAdListener = com.google.android.gms.ads.AdListener.extend({
onAdLoaded: function () {
admob.interstitialView.show();
resolve();
},
onAdFailedToLoad: function (errorCode) {
reject(errorCode);
},
onAdClosed: function () {
admob.interstitialView.setAdListener(null);
admob.interstitialView = null;
}
});
// Interstitial ads must be loaded before they can be shown, so adding a
// listener
var InterstitialAdListener =
com.google.android.gms.ads.AdListener.extend({
onAdLoaded: function() {
admob.interstitialView.show();
resolve();
},
onAdFailedToLoad: function(errorCode) {
reject(errorCode);
},
onAdClosed: function() {
admob.interstitialView.setAdListener(null);
admob.interstitialView = null;
}
});
admob.interstitialView.setAdListener(new InterstitialAdListener());

var ad = admob._buildAdRequest(settings);
admob.interstitialView.loadAd(ad);
} catch (ex) {
console.log("Error in admob.createBanner: " + ex);
console.log('Error in admob.createBanner: ' + ex);
reject(ex);
}
});
};

admob.hideBanner = function (arg) {
return new Promise(function (resolve, reject) {
admob.hideBanner = function(arg) {
return new Promise(function(resolve, reject) {
try {
if (admob.adView != null) {
var parent = admob && admob.adView && admob.adView.getParent();
Expand All @@ -205,7 +223,7 @@ admob.hideBanner = function (arg) {
}
resolve();
} catch (ex) {
console.log("Error in admob.hideBanner: " + ex);
console.log('Error in admob.hideBanner: ' + ex);
reject(ex);
}
});
Expand Down
Loading