@nativescript/google-mobile-ads
Version:
NativeScript Google mobile Ads
481 lines • 15.5 kB
JavaScript
import { Application, ImageSource } from '@nativescript/core';
import { MobileAdsError } from '../error';
import { AdEventType } from '../common';
import { NativeAdViewBase, MediaViewBase, mediaContentProperty, stretchProperty, MediaAspectRatio, AdChoicesPlacement, VideoStatus, NativeAdEventType } from './common';
export { AdChoicesPlacement, AdEventType, VideoStatus, MediaAspectRatio, NativeAdEventType };
const NATIVE_AD_LOADED_EVENT = 'adNativeAdLoaded';
const AD_MANAGER_AD_VIEW_LOADED_EVENT = 'adManagerAdViewLoaded';
export class NativeAdView extends NativeAdViewBase {
createNativeView() {
this._native = new com.google.android.gms.ads.nativead.NativeAdView(this._context);
return this._native;
}
_addChildFromBuilder(name, value) {
this.content = value;
}
get adChoicesView() {
return this._adChoicesView;
}
set adChoicesView(value) {
this._adChoicesView = value;
this._native.setAdChoicesView(value?.nativeView);
}
get advertiserView() {
return this._advertiserView;
}
set advertiserView(value) {
this._advertiserView = value;
this._native.setAdvertiserView(value?.nativeView);
}
get bodyView() {
return this._bodyView;
}
set bodyView(value) {
this._bodyView = value;
this._native.setBodyView(value?.nativeView);
}
get callToActionView() {
return this._callToActionView;
}
set callToActionView(value) {
this._callToActionView = value;
this._native.setCallToActionView(value?.nativeView);
}
get headlineView() {
return this._headlineView;
}
set headlineView(value) {
this._headlineView = value;
this._native.setHeadlineView(value?.nativeView);
}
get iconView() {
return this._iconView;
}
set iconView(value) {
this._iconView = value;
this._native.setIconView(value?.nativeView);
}
get imageView() {
return this._imageView;
}
set imageView(value) {
this.imageView = value;
this._native.setImageView(value?.nativeView);
}
get mediaView() {
return this._mediaView;
}
set mediaView(value) {
this._native.setMediaView(value?.native);
this._mediaView = value;
}
get nativeAd() {
return this._nativeAd;
}
set nativeAd(value) {
this._native.setNativeAd(value?.native || null);
this._nativeAd = value;
}
get priceView() {
return this._priceView;
}
set priceView(value) {
this._priceView = value;
this._native.setPriceView(value?.nativeView);
}
get starRatingView() {
return this._starRatingView;
}
set starRatingView(value) {
this._starRatingView = value;
this._native.setStarRatingView(value?.nativeView);
}
get storeView() {
return this._storeView;
}
set storeView(value) {
this._priceView = value;
this._native.setStoreView(value?.nativeView);
}
}
export class NativeAdLoader {
constructor(adUnitId, requestOptions = {}, nativeAdOptions = {}) {
this._adUnitId = adUnitId;
this._requestOptions = requestOptions;
this._nativeAdOptions = nativeAdOptions;
}
isLoading() {
return this.native?.isLoading?.() ?? false;
}
load(arg) {
const ref = new WeakRef(this);
this._native = org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.NativeAd.createLoader(Application.android.foregroundActivity || Application.android.startActivity, this._adUnitId, JSON.stringify(this._nativeAdOptions || {}), new org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.AdLoaderCallback({
onEvent(event, param1) {
const owner = ref.get();
let nativeAd;
switch (event) {
case AdEventType.CLICKED:
nativeAd?._listener?.(AdEventType.CLICKED, null, null);
break;
case AdEventType.CLOSED:
nativeAd?._listener?.(AdEventType.CLOSED, null, null);
break;
case AdEventType.FAILED_TO_LOAD_EVENT:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
owner?._listener?.(AdEventType.FAILED_TO_LOAD_EVENT, MobileAdsError.fromNative(param1), null);
break;
case AdEventType.IMPRESSION:
nativeAd?._listener?.(AdEventType.IMPRESSION, null, null);
break;
case AdEventType.LOADED:
owner?._listener?.(AdEventType.LOADED, null, null);
break;
case AdEventType.OPENED:
owner?._listener?.(AdEventType.OPENED, null, null);
break;
case NATIVE_AD_LOADED_EVENT:
nativeAd = NativeAd.fromNative(param1);
owner?._listener?.(NativeAdEventType.LOADED, null, nativeAd);
break;
case AD_MANAGER_AD_VIEW_LOADED_EVENT:
// returns adManagerAdView view
break;
}
},
}));
if (arg) {
if (typeof arg === 'number') {
org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.NativeAd.load(this._native, JSON.stringify(this._requestOptions), arg);
}
if (typeof arg === 'object') {
org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.NativeAd.load(this._native, JSON.stringify(arg), true);
}
}
else {
org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.NativeAd.load(this._native, JSON.stringify(this._requestOptions), false);
}
}
onAdEvent(listener) {
this._listener = listener;
}
get native() {
return this._native;
}
get android() {
return this.native;
}
}
export class NativeAd {
static fromNative(nativeAd) {
if (nativeAd instanceof com.google.android.gms.ads.nativead.NativeAd) {
const ad = new NativeAd();
ad._native = nativeAd;
return ad;
}
return null;
}
get adUnitId() {
return this._adUnitId;
}
onAdEvent(listener) {
this._listener = listener;
}
destroy() {
this.native?.destroy?.();
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get body() {
return this.native?.getBody();
}
get callToAction() {
return this.native?.getCallToAction?.();
}
get headline() {
return this.native?.getHeadline?.();
}
get price() {
return this.native?.getPrice?.();
}
get mediaContent() {
return MediaContent.fromNative(this.native?.getMediaContent?.());
}
get customMuteThisAdAvailable() {
return this.native?.isCustomMuteThisAdEnabled?.();
}
isCustomClickGestureEnabled() {
return this.native?.isCustomClickGestureEnabled?.();
}
enableCustomClickGesture() {
this.native?.enableCustomClickGesture?.();
}
get advertiser() {
return this.native?.getAdvertiser?.();
}
recordCustomClickGesture() {
this.native?.recordCustomClickGesture?.();
}
setUnconfirmedClickListener(listener) {
if (this._unconfirmedClickListener) {
this.native.setUnconfirmedClickListener(null);
this._unconfirmedClickListener = null;
}
if (listener) {
const ref = new WeakRef(this);
this._unconfirmedClickListenerNative = new com.google.android.gms.ads.nativead.NativeAd.UnconfirmedClickListener({
onUnconfirmedClickReceived(param0) {
const owner = ref.get();
if (owner) {
owner._unconfirmedClickListener?.unconfirmedClickReceived?.(param0);
}
},
onUnconfirmedClickCancelled() {
const owner = ref.get();
if (owner) {
owner._unconfirmedClickListener?.unconfirmedClickCancelled?.();
}
},
});
this._unconfirmedClickListener = listener;
this.native?.setUnconfirmedClickListener(this._unconfirmedClickListenerNative);
}
}
get icon() {
return NativeAdImage.fromNative(this.native?.getIcon?.());
}
get store() {
return this.native?.getStore?.();
}
get starRating() {
return this.native?.getStarRating?.().doubleValue();
}
get muteThisAdReasons() {
const result = [];
const reasons = this.native?.getMuteThisAdReasons?.();
if (reasons) {
const size = reasons.size();
for (let i = 0; i < size; i++) {
result.push(MuteThisAdReason.fromNative(reasons.get(i)));
}
}
return result;
}
muteThisAdWithReason(reason) {
if (reason?.native) {
this.native?.muteThisAd?.(reason.native);
}
}
get images() {
const result = [];
const images = this.native?.getImages?.();
if (images) {
const size = images.size();
for (let i = 0; i < size; i++) {
result.push(NativeAdImage.fromNative(images.get(i)));
}
}
return result;
}
}
export class MediaContent {
static fromNative(content) {
if (content instanceof com.google.android.gms.ads.MediaContent) {
const mediaContent = new MediaContent();
mediaContent._native = content;
return mediaContent;
}
return null;
}
get aspectRatio() {
return this.native?.getAspectRatio?.();
}
get currentTime() {
return this.native?.getCurrentTime?.();
}
get duration() {
return this.native?.getDuration?.();
}
get mainImage() {
return this.native?.getMainImage?.();
}
set mainImage(value) {
this.native.setMainImage(value);
}
get hasVideoContent() {
return this.native?.hasVideoContent();
}
get native() {
return this._native;
}
get android() {
return this.native;
}
}
let VideoLifecycleCallbacks;
function ensureVideoLifecycleCallbacks() {
if (VideoLifecycleCallbacks) {
return;
}
class VideoLifecycleCallbacksImpl extends com.google.android.gms.ads.VideoController.VideoLifecycleCallbacks {
constructor(owner) {
super();
this._owner = owner;
return global.__native(this);
}
onVideoPlay() {
const owner = this._owner?.get?.();
if (owner) {
owner._status = VideoStatus.Playing;
}
}
onVideoStart() {
const owner = this._owner?.get?.();
if (owner) {
owner._status = VideoStatus.Playing;
}
}
onVideoPause() {
const owner = this._owner?.get?.();
if (owner) {
owner._status = VideoStatus.Paused;
}
}
onVideoMute(param0) { }
onVideoEnd() {
const owner = this._owner?.get?.();
if (owner) {
owner._status = VideoStatus.Ended;
}
}
}
VideoLifecycleCallbacks = VideoLifecycleCallbacksImpl;
}
export class VideoController {
constructor() {
this._status = VideoStatus.Unstarted;
}
static fromNative(controller) {
if (controller instanceof com.google.android.gms.ads.VideoController) {
ensureVideoLifecycleCallbacks();
const ctrl = new VideoController();
ctrl._native = controller;
controller.setVideoLifecycleCallbacks(new VideoLifecycleCallbacks(new WeakRef(this)));
return ctrl;
}
return null;
}
clickToExpandEnabled() {
return this.native?.isClickToExpandEnabled?.();
}
customControlsEnabled() {
return this.native?.isCustomControlsEnabled?.();
}
pause() {
this.native?.pause?.();
}
play() {
this.native?.play?.();
}
get mute() {
return this.native?.isMuted?.();
}
set mute(value) {
this.native?.mute?.(value);
}
stop() {
this.native?.stop?.();
}
get status() {
return this._status;
}
get native() {
return this._native;
}
get android() {
return this.native;
}
}
export class MediaView extends MediaViewBase {
createNativeView() {
this._contentView = new com.google.android.gms.ads.nativead.MediaView(this._context);
return this._contentView;
}
[mediaContentProperty.setNative](content) {
if (this._contentView) {
this._contentView?.setMediaContent(content?.native || null);
}
}
[stretchProperty.setNative](value) {
if (!this._contentView) {
return;
}
switch (value) {
case 'aspectFit':
this.nativeViewProtected.setScaleType(android.widget.ImageView.ScaleType.FIT_CENTER);
break;
case 'aspectFill':
this.nativeViewProtected.setScaleType(android.widget.ImageView.ScaleType.CENTER_CROP);
break;
case 'fill':
this.nativeViewProtected.setScaleType(android.widget.ImageView.ScaleType.FIT_XY);
break;
case 'none':
default:
this.nativeViewProtected.setScaleType(android.widget.ImageView.ScaleType.MATRIX);
break;
}
}
get native() {
return this._contentView;
}
}
export class NativeAdImage {
static fromNative(image) {
if (image instanceof com.google.android.gms.ads.nativead.NativeAd.Image) {
const nativeAdImage = new NativeAdImage();
nativeAdImage._native = image;
return nativeAdImage;
}
return null;
}
get image() {
return new ImageSource(org.nativescript.plugins.google_mobile_ads.GoogleMobileAds.getBitmap(this.native?.getDrawable?.() || null));
}
get url() {
return this.native?.getUri?.().toString?.();
}
get scale() {
return this.native?.getScale?.();
}
get native() {
return this._native;
}
get android() {
return this.native;
}
}
export class MuteThisAdReason {
static fromNative(reason) {
if (reason instanceof com.google.android.gms.ads.MuteThisAdReason) {
const muteThisAdReason = new MuteThisAdReason();
muteThisAdReason._native = reason;
return muteThisAdReason;
}
return null;
}
get description() {
return this.native?.getDescription?.();
}
get native() {
return this._native;
}
get android() {
return this.native;
}
}
//# sourceMappingURL=index.android.js.map