@nativescript/google-mobile-ads
Version:
NativeScript Google mobile Ads
636 lines • 25.3 kB
JavaScript
import { Application, Utils } from '@nativescript/core';
import lazy from '@nativescript/core/utils/lazy';
import { AdEventType, BannerAdBase, RewardedAdEventType, MaxAdContentRating, unitIdProperty, BannerAdSizeBase, sizeProperty } from './common';
export { MaxAdContentRating, AdEventType };
import { MobileAdsError } from './error';
export { MobileAdsError };
export * from './adsconsent';
export * from './nativead';
var AdListener = /** @class */ (function (_super) {
__extends(AdListener, _super);
function AdListener(owner) {
var _this = _super.call(this) || this;
_this._owner = owner;
return global.__native(_this);
}
AdListener.prototype.onAdLoaded = function () {
var _a, _b;
var owner = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a);
if (!owner) {
return;
}
owner.notify({
eventName: BannerAd.onAdLoadedEvent,
object: owner,
});
};
AdListener.prototype.onAdClicked = function () {
var _a, _b;
var owner = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a);
if (!owner) {
return;
}
owner.notify({
eventName: BannerAd.onAdClickedEvent,
object: owner,
});
};
AdListener.prototype.onAdFailedToLoad = function (error) {
var _a, _b;
var owner = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a);
if (!owner) {
return;
}
owner.notify({
eventName: BannerAd.onAdFailedToLoadEvent,
object: owner,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
error: MobileAdsError.fromNative(error),
});
};
AdListener.prototype.onAdClosed = function () {
var _a, _b;
var owner = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a);
if (!owner) {
return;
}
owner.notify({
eventName: BannerAd.onAdClosedEvent,
object: owner,
});
};
AdListener.prototype.onAdImpression = function () {
var _a, _b;
var owner = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a);
if (!owner) {
return;
}
owner.notify({
eventName: BannerAd.onAdImpression,
object: owner,
});
};
AdListener.prototype.onAdOpened = function () {
var _a, _b;
var owner = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a);
if (!owner) {
return;
}
owner.notify({
eventName: BannerAd.onAdOpenedEvent,
object: owner,
});
};
return AdListener;
}(com.google.android.gms.ads.AdListener));
export class AdRequest {
static fromNative(request) {
if (request instanceof com.google.android.gms.ads.AdRequest) {
const ret = new AdRequest();
ret._native = request;
return ret;
}
return null;
}
get contentUrl() {
return this.native.getContentUrl();
}
get keywords() {
const kw = this.native.getKeywords().toArray();
const count = kw.length;
const ret = [];
for (let i = 0; i < count; i++) {
ret.push(kw[i]);
}
return ret;
}
get neighboringContentUrls() {
const urls = this.native.getNeighboringContentUrls();
const count = urls.size();
const ret = [];
for (let i = 0; i < count; i++) {
ret.push(urls.get(i));
}
return ret;
}
isTestDevice() {
return this.native.isTestDevice(Utils.android.getApplicationContext());
}
get native() {
return this._native;
}
get android() {
return this.native;
}
}
export class InterstitialAd {
constructor() {
this._loaded = false;
}
static createForAdRequest(adUnitId, requestOptions) {
const ad = new InterstitialAd();
ad._adUnitId = adUnitId;
ad._requestOptions = requestOptions;
return ad;
}
get adUnitId() {
return this._adUnitId;
}
_setNative(value) {
this._native = value;
}
_setLoaded(value) {
this._loaded = value;
}
get loaded() {
return this._loaded;
}
load() {
const ref = new WeakRef(this);
this._nativeRequest = org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.InterstitialAd.load(Application.android.foregroundActivity || Application.android.startActivity, this._adUnitId, JSON.stringify(this._requestOptions || {}), new org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.AdCallback({
onEvent(event, dataOrError) {
const owner = ref.get?.();
switch (event) {
case AdEventType.LOADED:
owner._setNative?.(dataOrError);
owner?._onAdEvent(AdEventType.LOADED, null, owner);
owner?._setLoaded(true);
break;
case AdEventType.CLOSED:
owner?._onAdEvent(AdEventType.CLOSED, null, owner);
owner?._setLoaded(false);
break;
case AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
owner?._onAdEvent(AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT, MobileAdsError.fromNative(dataOrError), owner);
break;
case AdEventType.IMPRESSION:
owner?._onAdEvent(AdEventType.IMPRESSION, null, owner);
break;
case AdEventType.OPENED:
owner?._onAdEvent(AdEventType.OPENED, null, owner);
break;
case AdEventType.FAILED_TO_LOAD_EVENT:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
owner?._onAdEvent(AdEventType.FAILED_TO_LOAD_EVENT, MobileAdsError.fromNative(dataOrError), owner);
break;
}
},
}));
}
onAdEvent(listener) {
this._onAdEvent = listener;
}
show(showOptions) {
if (typeof showOptions?.immersiveModeEnabled === 'boolean') {
this.native.setImmersiveMode(showOptions?.immersiveModeEnabled);
}
this.native.show(Application.android.foregroundActivity || Application.android.startActivity);
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get request() {
return AdRequest.fromNative(this._nativeRequest);
}
}
export class RewardedInterstitialAd {
constructor() {
this._loaded = false;
}
static createForAdRequest(adUnitId, requestOptions) {
const ad = new RewardedInterstitialAd();
ad._adUnitId = adUnitId;
ad._requestOptions = requestOptions;
return ad;
}
get adUnitId() {
return this._adUnitId;
}
_setNative(value) {
this._native = value;
}
_setLoaded(value) {
this._loaded = value;
}
get loaded() {
return this._loaded;
}
load() {
const ref = new WeakRef(this);
this._nativeRequest = org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.RewardedInterstitialAd.load(Application.android.foregroundActivity || Application.android.startActivity, this._adUnitId, JSON.stringify(this._requestOptions || {}), new org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.AdCallback({
onEvent(event, dataOrError) {
const owner = ref.get?.();
switch (event) {
case AdEventType.LOADED:
owner._setNative?.(dataOrError);
owner?._onAdEvent(AdEventType.LOADED, null, owner);
owner?._setLoaded(true);
break;
case AdEventType.CLOSED:
owner?._onAdEvent(AdEventType.CLOSED, null, owner);
owner?._setLoaded(false);
break;
case AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
owner?._onAdEvent(AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT, MobileAdsError.fromNative(dataOrError), owner);
break;
case AdEventType.IMPRESSION:
owner?._onAdEvent(AdEventType.IMPRESSION, null, owner);
break;
case AdEventType.OPENED:
owner?._onAdEvent(AdEventType.OPENED, null, owner);
break;
case AdEventType.FAILED_TO_LOAD_EVENT:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
owner?._onAdEvent(AdEventType.FAILED_TO_LOAD_EVENT, MobileAdsError.fromNative(dataOrError), owner);
break;
}
},
}));
}
onAdEvent(listener) {
this._onAdEvent = listener;
}
show(showOptions) {
if (typeof showOptions?.immersiveModeEnabled === 'boolean') {
this.native.setImmersiveMode(showOptions?.immersiveModeEnabled);
}
const ref = new WeakRef(this);
org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.RewardedInterstitialAd.show(Application.android.foregroundActivity || Application.android.startActivity, this.native, new org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.Callback({
onSuccess(reward) {
ref.get()?._onAdEvent?.(RewardedAdEventType.EARNED_REWARD, null, RewardedItem.fromNative(reward));
},
onError(error) {
// noop
},
}));
}
setServerSideVerificationOptions(options) {
if (this.native) {
const ssvo = new com.google.android.gms.ads.rewarded.ServerSideVerificationOptions.Builder();
if (options.customData) {
ssvo.setCustomData(options.customData);
}
if (options.userId) {
ssvo.setUserId(options.userId);
}
this.native.setServerSideVerificationOptions(ssvo.build());
}
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get request() {
return AdRequest.fromNative(this._nativeRequest);
}
}
export class RewardedAd {
constructor() {
this._loaded = false;
}
static createForAdRequest(adUnitId, requestOptions) {
const reward = new RewardedAd();
reward._adUnitId = adUnitId;
reward._requestOptions = requestOptions;
return reward;
}
get adUnitId() {
return this._adUnitId;
}
get loaded() {
return this._loaded;
}
_setNative(value) {
this._native = value;
}
_setLoaded(value) {
this._loaded = value;
}
load() {
const ref = new WeakRef(this);
this._nativeRequest = org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.RewardedAd.load(Application.android.foregroundActivity || Application.android.startActivity, this.adUnitId, JSON.stringify(this._requestOptions || {}), new org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.AdCallback({
onEvent(event, dataOrError) {
const owner = ref.get?.();
if (!owner) {
return;
}
switch (event) {
case AdEventType.LOADED:
owner._setNative(dataOrError);
owner._onAdEvent(AdEventType.LOADED, null, owner);
owner._setLoaded(true);
break;
case AdEventType.CLOSED:
owner._onAdEvent(AdEventType.CLOSED, null, owner);
owner._setLoaded(false);
break;
case AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
owner._onAdEvent(AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT, MobileAdsError.fromNative(dataOrError), owner);
break;
case AdEventType.IMPRESSION:
owner._onAdEvent(AdEventType.IMPRESSION, null, owner);
break;
case AdEventType.OPENED:
owner._onAdEvent(AdEventType.OPENED, null, owner);
break;
case AdEventType.FAILED_TO_LOAD_EVENT:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
owner._onAdEvent(AdEventType.FAILED_TO_LOAD_EVENT, MobileAdsError.fromNative(dataOrError), owner);
break;
}
},
}));
}
onAdEvent(listener) {
this._onAdEvent = listener;
}
show(showOptions) {
if (typeof showOptions?.immersiveModeEnabled === 'boolean') {
this.native.setImmersiveMode(showOptions?.immersiveModeEnabled);
}
const ref = new WeakRef(this);
org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.RewardedAd.show(Application.android.foregroundActivity || Application.android.startActivity, this.native, new org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.Callback({
onSuccess(reward) {
ref.get()?._onAdEvent?.(RewardedAdEventType.EARNED_REWARD, null, RewardedItem.fromNative(reward));
},
onError(error) {
// noop
},
}));
}
setServerSideVerificationOptions(options) {
if (this.native) {
const ssvo = new com.google.android.gms.ads.rewarded.ServerSideVerificationOptions.Builder();
if (options.customData) {
ssvo.setCustomData(options.customData);
}
if (options.userId) {
ssvo.setUserId(options.userId);
}
this.native.setServerSideVerificationOptions(ssvo.build());
}
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get request() {
return AdRequest.fromNative(this._nativeRequest);
}
}
export class RewardedItem {
static fromNative(reward) {
if (reward instanceof com.google.android.gms.ads.rewarded.RewardItem) {
const item = new RewardedItem();
item._native = reward;
return item;
}
return null;
}
get amount() {
return this.native?.getAmount?.();
}
get type() {
return this.native?.getType();
}
get native() {
return this._native;
}
get android() {
return this.native;
}
}
const BANNER = lazy(() => com.google.android.gms.ads.AdSize.BANNER);
const FULL_BANNER = lazy(() => com.google.android.gms.ads.AdSize.FULL_BANNER);
const LARGE_BANNER = lazy(() => com.google.android.gms.ads.AdSize.LARGE_BANNER);
const LEADERBOARD = lazy(() => com.google.android.gms.ads.AdSize.LEADERBOARD);
const MEDIUM_RECTANGLE = lazy(() => com.google.android.gms.ads.AdSize.MEDIUM_RECTANGLE);
const FLUID = lazy(() => com.google.android.gms.ads.AdSize.FLUID);
const WIDE_SKYSCRAPER = lazy(() => com.google.android.gms.ads.AdSize.WIDE_SKYSCRAPER);
const INVALID = lazy(() => com.google.android.gms.ads.AdSize.INVALID);
const FULL_WIDTH = lazy(() => com.google.android.gms.ads.AdSize.FULL_WIDTH);
const AUTO_HEIGHT = lazy(() => com.google.android.gms.ads.AdSize.AUTO_HEIGHT);
export class BannerAdSize extends BannerAdSizeBase {
constructor(width, height) {
super();
if (width && height) {
this._native = new com.google.android.gms.ads.AdSize(width, height);
}
}
static fromNative(size) {
const banner = new BannerAdSize();
if (size instanceof com.google.android.gms.ads.AdSize) {
banner._native = size;
}
else {
banner._native = com.google.android.gms.ads.AdSize.INVALID;
}
return banner;
}
static get BANNER() {
return BannerAdSize.fromNative(BANNER());
}
static get FULL_BANNER() {
return BannerAdSize.fromNative(FULL_BANNER());
}
static get LARGE_BANNER() {
return BannerAdSize.fromNative(LARGE_BANNER());
}
static get LEADERBOARD() {
return BannerAdSize.fromNative(LEADERBOARD());
}
static get MEDIUM_RECTANGLE() {
return BannerAdSize.fromNative(MEDIUM_RECTANGLE());
}
static createAnchoredAdaptiveBanner(width, orientation = 'device') {
switch (orientation) {
case 'portrait':
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getPortraitAnchoredAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, width));
case 'landscape':
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getLandscapeAnchoredAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, width));
default:
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, width));
}
}
static createInLineAdaptiveBanner(width, maxHeight = 0, orientation = 'device') {
if (maxHeight > 0) {
BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getInlineAdaptiveBannerAdSize(width, maxHeight));
}
switch (orientation) {
case 'portrait':
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getPortraitInlineAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, width));
case 'landscape':
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getLandscapeInlineAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, width));
default:
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, width));
}
}
static get FLUID() {
return BannerAdSize.fromNative(FLUID());
}
static get WIDE_SKYSCRAPER() {
return BannerAdSize.fromNative(WIDE_SKYSCRAPER());
}
static get INVALID() {
return BannerAdSize.fromNative(INVALID());
}
get native() {
return this._native;
}
get android() {
return this.native;
}
}
export class BannerAd extends BannerAdBase {
[sizeProperty.setNative](value) {
if (this._native) {
this._native.setAdSize(value?.native);
}
}
[unitIdProperty.setNative](value) {
if (this._native) {
this._native.setAdUnitId(value);
}
}
createNativeView() {
this._native = new com.google.android.gms.ads.AdView(this._context);
return this._native;
}
initNativeView() {
super.initNativeView();
this._listener = new AdListener(new WeakRef(this));
this._native.setAdListener(this._listener);
}
load(options) {
if (this._native) {
this._nativeRequest = org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.BannerAd.load(JSON.stringify(options || {}), this._native);
}
}
isLoading() {
return this._native?.isLoading?.();
}
get request() {
return AdRequest.fromNative(this._nativeRequest);
}
}
export class MobileAds {
static init() {
return new Promise((resolve, reject) => {
com.google.android.gms.ads.MobileAds.initialize(Utils.android.getApplicationContext(), new com.google.android.gms.ads.initialization.OnInitializationCompleteListener({
onInitializationComplete(status) {
let data = {};
try {
data = JSON.parse(org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.toJSONStatusMap(status.getAdapterStatusMap()));
}
catch (e) {
// noop
}
resolve(data);
},
}));
});
}
static getInstance() {
return this._instance || (this._instance = new MobileAds());
}
set requestConfiguration(requestConfiguration) {
try {
const parsedConfiguration = { ...requestConfiguration };
if (typeof parsedConfiguration.tagForChildDirectedTreatment === 'boolean') {
parsedConfiguration.tagForChildDirectedTreatment = String(parsedConfiguration.tagForChildDirectedTreatment);
}
else {
parsedConfiguration.tagForChildDirectedTreatment = 'unspecified';
}
if (typeof parsedConfiguration.tagForUnderAgeOfConsent === 'boolean') {
parsedConfiguration.tagForUnderAgeOfConsent = String(parsedConfiguration.tagForUnderAgeOfConsent);
}
else {
parsedConfiguration.tagForUnderAgeOfConsent = 'unspecified';
}
org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.setRequestConfiguration(JSON.stringify(requestConfiguration));
}
catch (e) {
// noop
}
}
get requestConfiguration() {
const ret = {};
const config = com.google.android.gms.ads.MobileAds.getRequestConfiguration();
switch (config.getTagForChildDirectedTreatment()) {
case com.google.android.gms.ads.RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_TRUE:
ret.tagForChildDirectedTreatment = true;
break;
case com.google.android.gms.ads.RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_FALSE:
ret.tagForChildDirectedTreatment = false;
break;
default:
// noop
break;
}
switch (config.getTagForUnderAgeOfConsent()) {
case com.google.android.gms.ads.RequestConfiguration.TAG_FOR_UNDER_AGE_OF_CONSENT_TRUE:
ret.tagForUnderAgeOfConsent = true;
break;
case com.google.android.gms.ads.RequestConfiguration.TAG_FOR_UNDER_AGE_OF_CONSENT_FALSE:
ret.tagForUnderAgeOfConsent = false;
break;
default:
// noop
break;
}
switch (config.getMaxAdContentRating()) {
case com.google.android.gms.ads.RequestConfiguration.MAX_AD_CONTENT_RATING_G:
ret.maxAdContentRating = MaxAdContentRating.G;
break;
case com.google.android.gms.ads.RequestConfiguration.MAX_AD_CONTENT_RATING_MA:
ret.maxAdContentRating = MaxAdContentRating.MA;
break;
case com.google.android.gms.ads.RequestConfiguration.MAX_AD_CONTENT_RATING_PG:
ret.maxAdContentRating = MaxAdContentRating.PG;
break;
case com.google.android.gms.ads.RequestConfiguration.MAX_AD_CONTENT_RATING_T:
ret.maxAdContentRating = MaxAdContentRating.T;
break;
default:
// noop
break;
}
ret.testDevices = [];
const devices = config.getTestDeviceIds();
if (devices) {
const count = devices.size();
for (let i = 0; i < count; i++) {
ret.testDevices.push(devices.get(i));
}
}
return ret;
}
setRequestConfiguration(requestConfiguration) {
this.requestConfiguration = requestConfiguration;
}
getRequestConfiguration() {
return this.requestConfiguration;
}
get app() {
return global?.__defaultFirebaseApp;
}
}
//# sourceMappingURL=index.android.js.map