export function assertAreClose(actual: number, expected: number, message: string): void { const density = utils.layout.getDisplayDensity(); const delta = Math.floor(density) !== density ? 1.1 : DELTA; TKUnit.assertAreClose(actual, expected, delta, message); }
return new Promise((resolve, reject) => { try { const settings = firebase.merge(arg, BANNER_DEFAULTS); // always close a previously opened banner if (firebase.admob.adView !== null && firebase.admob.adView !== undefined) { const parent = firebase.admob.adView.getParent(); if (parent !== null) { parent.removeView(firebase.admob.adView); } } firebase.admob.adView = new com.google.android.gms.ads.AdView(appModule.android.foregroundActivity); firebase.admob.adView.setAdUnitId(settings.androidBannerId); const bannerType = _getBannerType(settings.size); firebase.admob.adView.setAdSize(bannerType); // need these to support showing a banner more than once this.resolve = resolve; this.reject = reject; const BannerAdListener = com.google.android.gms.ads.AdListener.extend({ resolve: null, reject: null, onAdLoaded: () => { this.resolve(); }, onAdFailedToLoad: errorCode => { this.reject(errorCode); } }); firebase.admob.adView.setAdListener(new BannerAdListener()); const ad = _buildAdRequest(settings); firebase.admob.adView.loadAd(ad); const density = layout.getDisplayDensity(), top = settings.margins.top * density, bottom = settings.margins.bottom * density; const relativeLayoutParams = new android.widget.RelativeLayout.LayoutParams( android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT); if (bottom > -1) { relativeLayoutParams.bottomMargin = bottom; relativeLayoutParams.addRule(android.widget.RelativeLayout.ALIGN_PARENT_BOTTOM); } else { if (top > -1) { relativeLayoutParams.topMargin = top; } relativeLayoutParams.addRule(android.widget.RelativeLayout.ALIGN_PARENT_TOP); } const adViewLayout = new android.widget.RelativeLayout(appModule.android.foregroundActivity); adViewLayout.addView(firebase.admob.adView, relativeLayoutParams); const 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(() => { const top = topmost(); if (top !== undefined && top.currentPage && top.currentPage.android && top.currentPage.android.getParent()) { top.currentPage.android.getParent().addView(adViewLayout, relativeLayoutParamsOuter); } else if (appModule.android && appModule.android.foregroundActivity) { appModule.android.foregroundActivity.getWindow().getDecorView().addView(adViewLayout, relativeLayoutParamsOuter); } else { console.log("Could not find a view to add the banner to"); } }, 100); } catch (ex) { console.log("Error in firebase.admob.showBanner: " + ex); reject(ex); } });