From e98838fea3d80ce6a489b1c5bf4b2e59dd6a0d7a Mon Sep 17 00:00:00 2001 From: Robert Mengual Date: Sat, 27 Sep 2025 15:35:00 +0200 Subject: [PATCH 1/2] Fix banners on android 15 --- .../admob/banner/BannerExecutor.java | 310 +++++++++--------- 1 file changed, 155 insertions(+), 155 deletions(-) diff --git a/android/src/main/java/com/getcapacitor/community/admob/banner/BannerExecutor.java b/android/src/main/java/com/getcapacitor/community/admob/banner/BannerExecutor.java index 3671a6ba..7be98d08 100644 --- a/android/src/main/java/com/getcapacitor/community/admob/banner/BannerExecutor.java +++ b/android/src/main/java/com/getcapacitor/community/admob/banner/BannerExecutor.java @@ -1,7 +1,14 @@ package com.getcapacitor.community.admob.banner; +import androidx.annotation.NonNull; +import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.core.util.Supplier; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; + import android.app.Activity; import android.content.Context; +import android.content.res.Configuration; import android.util.DisplayMetrics; import android.util.Log; import android.view.Gravity; @@ -9,9 +16,7 @@ import android.view.ViewGroup; import android.view.WindowManager; import android.widget.RelativeLayout; -import androidx.annotation.NonNull; -import androidx.coordinatorlayout.widget.CoordinatorLayout; -import androidx.core.util.Supplier; + import com.getcapacitor.JSObject; import com.getcapacitor.PluginCall; import com.getcapacitor.community.admob.helpers.AdViewIdHelper; @@ -43,18 +48,19 @@ public BannerExecutor( } public void initialize() { + // The first child of android.R.id.content in a Capacitor activity is a CoordinatorLayout. mViewGroup = (ViewGroup) ((ViewGroup) activitySupplier.get().findViewById(android.R.id.content)).getChildAt(0); } public void showBanner(final PluginCall call) { final AdOptions adOptions = AdOptions.getFactory().createBannerOptions(call); - float density = contextSupplier.get().getResources().getDisplayMetrics().density; + final float density = contextSupplier.get().getResources().getDisplayMetrics().density; - int defaultWidthPixels = contextSupplier.get().getResources().getDisplayMetrics().widthPixels; + final int defaultWidthPixels = contextSupplier.get().getResources().getDisplayMetrics().widthPixels; - DisplayMetrics metrics = new DisplayMetrics(); + final DisplayMetrics metrics = new DisplayMetrics(); activitySupplier.get().getWindowManager().getDefaultDisplay().getRealMetrics(metrics); - int realWidthPixels = metrics.widthPixels; + final int realWidthPixels = metrics.widthPixels; boolean fullscreen = false; if ((activitySupplier.get().getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0) { @@ -63,10 +69,10 @@ public void showBanner(final PluginCall call) { if (mAdView != null) { updateExistingAdView(adOptions); + call.resolve(); return; } - // Why a try catch block? try { mAdView = new AdView(contextSupplier.get()); @@ -75,70 +81,73 @@ public void showBanner(final PluginCall call) { } else { // ADAPTIVE BANNER mAdView.setAdSize( - AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(contextSupplier.get(), (int) (defaultWidthPixels / density)) + AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize( + contextSupplier.get(), + (int) (defaultWidthPixels / density) + ) ); } - // Setup AdView Layout + // ---- Container that will consume navigation bar insets ---- mAdViewLayout = new RelativeLayout(contextSupplier.get()); mAdViewLayout.setHorizontalGravity(Gravity.CENTER_HORIZONTAL); mAdViewLayout.setVerticalGravity(Gravity.BOTTOM); - final CoordinatorLayout.LayoutParams mAdViewLayoutParams = new CoordinatorLayout.LayoutParams( + final boolean isLandscape = + contextSupplier.get().getResources().getConfiguration().orientation + == Configuration.ORIENTATION_LANDSCAPE; + + // Parent is CoordinatorLayout → use its LayoutParams + final CoordinatorLayout.LayoutParams lp = new CoordinatorLayout.LayoutParams( CoordinatorLayout.LayoutParams.WRAP_CONTENT, CoordinatorLayout.LayoutParams.WRAP_CONTENT ); - // TODO: Make an enum like the AdSizeEnum? - switch (adOptions.position) { - case "TOP_CENTER": - mAdViewLayoutParams.gravity = Gravity.TOP; - break; - case "CENTER": - mAdViewLayoutParams.gravity = Gravity.CENTER; - break; - default: - mAdViewLayoutParams.gravity = Gravity.BOTTOM; - break; + // Gravity per position, but left-align in LANDSCAPE (unless explicitly CENTER) + if ("CENTER".equals(adOptions.position)) { + lp.gravity = Gravity.CENTER; + } else if ("TOP_CENTER".equals(adOptions.position)) { + lp.gravity = isLandscape ? (Gravity.TOP | Gravity.START) : Gravity.TOP; + } else { + // default (bottom) + lp.gravity = isLandscape ? (Gravity.BOTTOM | Gravity.START) : Gravity.BOTTOM; } - // set Safe Area - View rootView = activitySupplier.get().getWindow().getDecorView(); - rootView.setOnApplyWindowInsetsListener((v, insets) -> { - int bottomInset = insets.getSystemWindowInsetBottom(); - int topInset = insets.getSystemWindowInsetTop(); + // Margins + final int densityMargin = (int) (adOptions.margin * density); + final int configuredAdWidthPx = (int) (adOptions.adSize.getSize().getWidth() * density); - if ("TOP_CENTER".equals(adOptions.position)) { - mAdViewLayoutParams.setMargins(0, topInset, 0, 0); + if (isLandscape) { + // Left-align: no centering—just use regular margins + lp.setMargins(densityMargin, densityMargin, densityMargin, densityMargin); + } else { + // PORTRAIT: keep your existing centering logic + if (configuredAdWidthPx <= 0 || adOptions.adSize.toString().equals("ADAPTIVE_BANNER")) { + int sideMargin = 0; + if (fullscreen) { + sideMargin = (realWidthPixels - defaultWidthPixels) / 2; + } + lp.setMargins(sideMargin, densityMargin, sideMargin, densityMargin); } else { - mAdViewLayoutParams.setMargins(0, 0, 0, bottomInset); + int sideMargin = (defaultWidthPixels - configuredAdWidthPx) / 2; + if (fullscreen) { + sideMargin = (realWidthPixels - configuredAdWidthPx) / 2; + } + lp.setMargins(sideMargin, densityMargin, sideMargin, densityMargin); } + } + + mAdViewLayout.setLayoutParams(lp); - mAdViewLayout.setLayoutParams(mAdViewLayoutParams); + // Consume the navigation bar inset so the banner clears the gesture/3-button bar + ViewCompat.setOnApplyWindowInsetsListener(mAdViewLayout, (v, insets) -> { + final int navBottom = insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom; + // keep existing paddings; only adjust bottom + v.setPadding(v.getPaddingLeft(), v.getPaddingTop(), v.getPaddingRight(), navBottom); return insets; }); - mAdViewLayout.setLayoutParams(mAdViewLayoutParams); - - int densityMargin = (int) (adOptions.margin * density); - - // Center Banner Ads - int adWidth = (int) (adOptions.adSize.getSize().getWidth() * density); - - if (adWidth <= 0 || adOptions.adSize.toString().equals("ADAPTIVE_BANNER")) { - int margin = 0; - if (fullscreen) { - margin = (realWidthPixels - defaultWidthPixels) / 2; - } - mAdViewLayoutParams.setMargins(margin, densityMargin, margin, densityMargin); - } else { - int sideMargin = ((int) defaultWidthPixels - adWidth) / 2; - if (fullscreen) { - sideMargin = (realWidthPixels - adWidth) / 2; - } - mAdViewLayoutParams.setMargins(sideMargin, densityMargin, sideMargin, densityMargin); - } - + // Proceed to create and attach the ad view createNewAdView(adOptions); call.resolve(); @@ -154,20 +163,17 @@ public void hideBanner(final PluginCall call) { } try { - activitySupplier - .get() - .runOnUiThread(() -> { - if (mAdViewLayout != null) { - mAdViewLayout.setVisibility(View.GONE); - mAdView.pause(); + activitySupplier.get().runOnUiThread(() -> { + if (mAdViewLayout != null) { + mAdViewLayout.setVisibility(View.GONE); + mAdView.pause(); - final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(0, 0); + final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(0, 0); + notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); - notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); - - call.resolve(); - } - }); + call.resolve(); + } + }); } catch (Exception ex) { call.reject(ex.getLocalizedMessage(), ex); } @@ -175,19 +181,17 @@ public void hideBanner(final PluginCall call) { public void resumeBanner(final PluginCall call) { try { - activitySupplier - .get() - .runOnUiThread(() -> { - if (mAdViewLayout != null && mAdView != null) { - mAdViewLayout.setVisibility(View.VISIBLE); - mAdView.resume(); - - final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(mAdView); - notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); + activitySupplier.get().runOnUiThread(() -> { + if (mAdViewLayout != null && mAdView != null) { + mAdViewLayout.setVisibility(View.VISIBLE); + mAdView.resume(); - Log.d(logTag, "Banner AD Resumed"); - } - }); + final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(mAdView); + notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); + + Log.d(logTag, "Banner AD Resumed"); + } + }); call.resolve(); } catch (Exception ex) { @@ -198,19 +202,17 @@ public void resumeBanner(final PluginCall call) { public void removeBanner(final PluginCall call) { try { if (mAdView != null) { - activitySupplier - .get() - .runOnUiThread(() -> { - if (mAdView != null) { - mViewGroup.removeView(mAdViewLayout); - mAdViewLayout.removeView(mAdView); - mAdView.destroy(); - mAdView = null; - Log.d(logTag, "Banner AD Removed"); - final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(0, 0); - notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); - } - }); + activitySupplier.get().runOnUiThread(() -> { + if (mAdView != null) { + mViewGroup.removeView(mAdViewLayout); + mAdViewLayout.removeView(mAdView); + mAdView.destroy(); + mAdView = null; + Log.d(logTag, "Banner AD Removed"); + final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(0, 0); + notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); + } + }); } call.resolve(); @@ -220,12 +222,10 @@ public void removeBanner(final PluginCall call) { } private void updateExistingAdView(AdOptions adOptions) { - activitySupplier - .get() - .runOnUiThread(() -> { - final AdRequest adRequest = RequestHelper.createRequest(adOptions); - mAdView.loadAd(adRequest); - }); + activitySupplier.get().runOnUiThread(() -> { + final AdRequest adRequest = RequestHelper.createRequest(adOptions); + mAdView.loadAd(adRequest); + }); } /** @@ -233,68 +233,68 @@ private void updateExistingAdView(AdOptions adOptions) { * https://developers.google.com/admob/ios/banner?hl=ja */ private void createNewAdView(AdOptions adOptions) { - // Run AdMob In Main UI Thread - activitySupplier - .get() - .runOnUiThread(() -> { - final AdRequest adRequest = RequestHelper.createRequest(adOptions); - // Assign the correct id needed - AdViewIdHelper.assignIdToAdView(mAdView, adOptions, adRequest, logTag, contextSupplier.get()); - // Add the AdView to the view hierarchy. - mAdViewLayout.addView(mAdView); - // Start loading the ad. - mAdView.loadAd(adRequest); - mAdView.setAdListener( - new AdListener() { - @Override - public void onAdLoaded() { - final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(mAdView); - - notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); - notifyListeners(BannerAdPluginEvents.Loaded.getWebEventName(), emptyObject); - super.onAdLoaded(); - } - - @Override - public void onAdFailedToLoad(@NonNull LoadAdError adError) { - if (mAdView != null) { - mViewGroup.removeView(mAdViewLayout); - mAdViewLayout.removeView(mAdView); - mAdView.destroy(); - mAdView = null; - } - - final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(0, 0); - notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); - - final AdMobPluginError adMobPluginError = new AdMobPluginError(adError); - notifyListeners(BannerAdPluginEvents.FailedToLoad.getWebEventName(), adMobPluginError); - - super.onAdFailedToLoad(adError); - } - - @Override - public void onAdOpened() { - notifyListeners(BannerAdPluginEvents.Opened.getWebEventName(), emptyObject); - super.onAdOpened(); - } - - @Override - public void onAdClosed() { - notifyListeners(BannerAdPluginEvents.Closed.getWebEventName(), emptyObject); - super.onAdClosed(); - } - - @Override - public void onAdImpression() { - notifyListeners(BannerAdPluginEvents.AdImpression.getWebEventName(), emptyObject); - super.onAdImpression(); - } + activitySupplier.get().runOnUiThread(() -> { + final AdRequest adRequest = RequestHelper.createRequest(adOptions); + + // Assign the correct id needed + AdViewIdHelper.assignIdToAdView(mAdView, adOptions, adRequest, logTag, contextSupplier.get()); + + // Add the AdView to the container + mAdViewLayout.addView(mAdView); + + // Add container above WebView and request insets now that it's attached + mViewGroup.addView(mAdViewLayout); + mAdViewLayout.bringToFront(); + ViewCompat.requestApplyInsets(mAdViewLayout); + + // Start loading the ad. + mAdView.setAdListener(new AdListener() { + @Override + public void onAdLoaded() { + final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(mAdView); + notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); + notifyListeners(BannerAdPluginEvents.Loaded.getWebEventName(), emptyObject); + super.onAdLoaded(); + } + + @Override + public void onAdFailedToLoad(@NonNull LoadAdError adError) { + if (mAdView != null) { + mViewGroup.removeView(mAdViewLayout); + mAdViewLayout.removeView(mAdView); + mAdView.destroy(); + mAdView = null; } - ); - // Add AdViewLayout top of the WebView - mViewGroup.addView(mAdViewLayout); + final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(0, 0); + notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); + + final AdMobPluginError adMobPluginError = new AdMobPluginError(adError); + notifyListeners(BannerAdPluginEvents.FailedToLoad.getWebEventName(), adMobPluginError); + + super.onAdFailedToLoad(adError); + } + + @Override + public void onAdOpened() { + notifyListeners(BannerAdPluginEvents.Opened.getWebEventName(), emptyObject); + super.onAdOpened(); + } + + @Override + public void onAdClosed() { + notifyListeners(BannerAdPluginEvents.Closed.getWebEventName(), emptyObject); + super.onAdClosed(); + } + + @Override + public void onAdImpression() { + notifyListeners(BannerAdPluginEvents.AdImpression.getWebEventName(), emptyObject); + super.onAdImpression(); + } }); + + mAdView.loadAd(adRequest); + }); } } From 52365ceb9651b3c1d327c726501eea92131f558a Mon Sep 17 00:00:00 2001 From: Robert Mengual Date: Wed, 8 Oct 2025 10:55:29 +0200 Subject: [PATCH 2/2] Ran fmt command --- .../admob/banner/BannerExecutor.java | 220 +++++++++--------- .../admob/consent/AdConsentExecutor.java | 6 +- .../admob/consent/AdConsentExecutorTest.java | 2 +- .../AdInterstitialExecutorTest.java | 3 +- 4 files changed, 119 insertions(+), 112 deletions(-) diff --git a/android/src/main/java/com/getcapacitor/community/admob/banner/BannerExecutor.java b/android/src/main/java/com/getcapacitor/community/admob/banner/BannerExecutor.java index 7be98d08..6f35a291 100644 --- a/android/src/main/java/com/getcapacitor/community/admob/banner/BannerExecutor.java +++ b/android/src/main/java/com/getcapacitor/community/admob/banner/BannerExecutor.java @@ -1,11 +1,5 @@ package com.getcapacitor.community.admob.banner; -import androidx.annotation.NonNull; -import androidx.coordinatorlayout.widget.CoordinatorLayout; -import androidx.core.util.Supplier; -import androidx.core.view.ViewCompat; -import androidx.core.view.WindowInsetsCompat; - import android.app.Activity; import android.content.Context; import android.content.res.Configuration; @@ -16,7 +10,11 @@ import android.view.ViewGroup; import android.view.WindowManager; import android.widget.RelativeLayout; - +import androidx.annotation.NonNull; +import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.core.util.Supplier; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; import com.getcapacitor.JSObject; import com.getcapacitor.PluginCall; import com.getcapacitor.community.admob.helpers.AdViewIdHelper; @@ -81,10 +79,7 @@ public void showBanner(final PluginCall call) { } else { // ADAPTIVE BANNER mAdView.setAdSize( - AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize( - contextSupplier.get(), - (int) (defaultWidthPixels / density) - ) + AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(contextSupplier.get(), (int) (defaultWidthPixels / density)) ); } @@ -94,8 +89,7 @@ public void showBanner(final PluginCall call) { mAdViewLayout.setVerticalGravity(Gravity.BOTTOM); final boolean isLandscape = - contextSupplier.get().getResources().getConfiguration().orientation - == Configuration.ORIENTATION_LANDSCAPE; + contextSupplier.get().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE; // Parent is CoordinatorLayout → use its LayoutParams final CoordinatorLayout.LayoutParams lp = new CoordinatorLayout.LayoutParams( @@ -163,17 +157,19 @@ public void hideBanner(final PluginCall call) { } try { - activitySupplier.get().runOnUiThread(() -> { - if (mAdViewLayout != null) { - mAdViewLayout.setVisibility(View.GONE); - mAdView.pause(); + activitySupplier + .get() + .runOnUiThread(() -> { + if (mAdViewLayout != null) { + mAdViewLayout.setVisibility(View.GONE); + mAdView.pause(); - final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(0, 0); - notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); + final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(0, 0); + notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); - call.resolve(); - } - }); + call.resolve(); + } + }); } catch (Exception ex) { call.reject(ex.getLocalizedMessage(), ex); } @@ -181,17 +177,19 @@ public void hideBanner(final PluginCall call) { public void resumeBanner(final PluginCall call) { try { - activitySupplier.get().runOnUiThread(() -> { - if (mAdViewLayout != null && mAdView != null) { - mAdViewLayout.setVisibility(View.VISIBLE); - mAdView.resume(); - - final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(mAdView); - notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); + activitySupplier + .get() + .runOnUiThread(() -> { + if (mAdViewLayout != null && mAdView != null) { + mAdViewLayout.setVisibility(View.VISIBLE); + mAdView.resume(); + + final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(mAdView); + notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); - Log.d(logTag, "Banner AD Resumed"); - } - }); + Log.d(logTag, "Banner AD Resumed"); + } + }); call.resolve(); } catch (Exception ex) { @@ -202,17 +200,19 @@ public void resumeBanner(final PluginCall call) { public void removeBanner(final PluginCall call) { try { if (mAdView != null) { - activitySupplier.get().runOnUiThread(() -> { - if (mAdView != null) { - mViewGroup.removeView(mAdViewLayout); - mAdViewLayout.removeView(mAdView); - mAdView.destroy(); - mAdView = null; - Log.d(logTag, "Banner AD Removed"); - final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(0, 0); - notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); - } - }); + activitySupplier + .get() + .runOnUiThread(() -> { + if (mAdView != null) { + mViewGroup.removeView(mAdViewLayout); + mAdViewLayout.removeView(mAdView); + mAdView.destroy(); + mAdView = null; + Log.d(logTag, "Banner AD Removed"); + final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(0, 0); + notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); + } + }); } call.resolve(); @@ -222,10 +222,12 @@ public void removeBanner(final PluginCall call) { } private void updateExistingAdView(AdOptions adOptions) { - activitySupplier.get().runOnUiThread(() -> { - final AdRequest adRequest = RequestHelper.createRequest(adOptions); - mAdView.loadAd(adRequest); - }); + activitySupplier + .get() + .runOnUiThread(() -> { + final AdRequest adRequest = RequestHelper.createRequest(adOptions); + mAdView.loadAd(adRequest); + }); } /** @@ -233,68 +235,72 @@ private void updateExistingAdView(AdOptions adOptions) { * https://developers.google.com/admob/ios/banner?hl=ja */ private void createNewAdView(AdOptions adOptions) { - activitySupplier.get().runOnUiThread(() -> { - final AdRequest adRequest = RequestHelper.createRequest(adOptions); - - // Assign the correct id needed - AdViewIdHelper.assignIdToAdView(mAdView, adOptions, adRequest, logTag, contextSupplier.get()); - - // Add the AdView to the container - mAdViewLayout.addView(mAdView); - - // Add container above WebView and request insets now that it's attached - mViewGroup.addView(mAdViewLayout); - mAdViewLayout.bringToFront(); - ViewCompat.requestApplyInsets(mAdViewLayout); - - // Start loading the ad. - mAdView.setAdListener(new AdListener() { - @Override - public void onAdLoaded() { - final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(mAdView); - notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); - notifyListeners(BannerAdPluginEvents.Loaded.getWebEventName(), emptyObject); - super.onAdLoaded(); - } - - @Override - public void onAdFailedToLoad(@NonNull LoadAdError adError) { - if (mAdView != null) { - mViewGroup.removeView(mAdViewLayout); - mAdViewLayout.removeView(mAdView); - mAdView.destroy(); - mAdView = null; + activitySupplier + .get() + .runOnUiThread(() -> { + final AdRequest adRequest = RequestHelper.createRequest(adOptions); + + // Assign the correct id needed + AdViewIdHelper.assignIdToAdView(mAdView, adOptions, adRequest, logTag, contextSupplier.get()); + + // Add the AdView to the container + mAdViewLayout.addView(mAdView); + + // Add container above WebView and request insets now that it's attached + mViewGroup.addView(mAdViewLayout); + mAdViewLayout.bringToFront(); + ViewCompat.requestApplyInsets(mAdViewLayout); + + // Start loading the ad. + mAdView.setAdListener( + new AdListener() { + @Override + public void onAdLoaded() { + final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(mAdView); + notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); + notifyListeners(BannerAdPluginEvents.Loaded.getWebEventName(), emptyObject); + super.onAdLoaded(); + } + + @Override + public void onAdFailedToLoad(@NonNull LoadAdError adError) { + if (mAdView != null) { + mViewGroup.removeView(mAdViewLayout); + mAdViewLayout.removeView(mAdView); + mAdView.destroy(); + mAdView = null; + } + + final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(0, 0); + notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); + + final AdMobPluginError adMobPluginError = new AdMobPluginError(adError); + notifyListeners(BannerAdPluginEvents.FailedToLoad.getWebEventName(), adMobPluginError); + + super.onAdFailedToLoad(adError); + } + + @Override + public void onAdOpened() { + notifyListeners(BannerAdPluginEvents.Opened.getWebEventName(), emptyObject); + super.onAdOpened(); + } + + @Override + public void onAdClosed() { + notifyListeners(BannerAdPluginEvents.Closed.getWebEventName(), emptyObject); + super.onAdClosed(); + } + + @Override + public void onAdImpression() { + notifyListeners(BannerAdPluginEvents.AdImpression.getWebEventName(), emptyObject); + super.onAdImpression(); + } } + ); - final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(0, 0); - notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); - - final AdMobPluginError adMobPluginError = new AdMobPluginError(adError); - notifyListeners(BannerAdPluginEvents.FailedToLoad.getWebEventName(), adMobPluginError); - - super.onAdFailedToLoad(adError); - } - - @Override - public void onAdOpened() { - notifyListeners(BannerAdPluginEvents.Opened.getWebEventName(), emptyObject); - super.onAdOpened(); - } - - @Override - public void onAdClosed() { - notifyListeners(BannerAdPluginEvents.Closed.getWebEventName(), emptyObject); - super.onAdClosed(); - } - - @Override - public void onAdImpression() { - notifyListeners(BannerAdPluginEvents.AdImpression.getWebEventName(), emptyObject); - super.onAdImpression(); - } + mAdView.loadAd(adRequest); }); - - mAdView.loadAd(adRequest); - }); } } diff --git a/android/src/main/java/com/getcapacitor/community/admob/consent/AdConsentExecutor.java b/android/src/main/java/com/getcapacitor/community/admob/consent/AdConsentExecutor.java index 6c5b438a..b4555c18 100644 --- a/android/src/main/java/com/getcapacitor/community/admob/consent/AdConsentExecutor.java +++ b/android/src/main/java/com/getcapacitor/community/admob/consent/AdConsentExecutor.java @@ -71,7 +71,7 @@ public void requestConsentInfo(final PluginCall call, BiConsumer call.reject(formError.getMessage()) + (formError) -> call.reject(formError.getMessage()) ); } catch (Exception ex) { call.reject(ex.getLocalizedMessage(), ex); @@ -88,7 +88,7 @@ public void showPrivacyOptionsForm(final PluginCall call, BiConsumer - UserMessagingPlatform.showPrivacyOptionsForm(activity, formError -> { + UserMessagingPlatform.showPrivacyOptionsForm(activity, (formError) -> { if (formError != null) { call.reject("Error when show privacy form", formError.getMessage()); } else { @@ -112,7 +112,7 @@ public void showConsentForm(final PluginCall call, BiConsumer ensureConsentInfo(); activity.runOnUiThread(() -> - UserMessagingPlatform.loadAndShowConsentFormIfRequired(activity, formError -> { + UserMessagingPlatform.loadAndShowConsentFormIfRequired(activity, (formError) -> { if (formError != null) { call.reject("Error when show consent form", formError.getMessage()); return; diff --git a/android/src/test/java/com/getcapacitor/community/admob/consent/AdConsentExecutorTest.java b/android/src/test/java/com/getcapacitor/community/admob/consent/AdConsentExecutorTest.java index 91f8989f..e4ddfd92 100644 --- a/android/src/test/java/com/getcapacitor/community/admob/consent/AdConsentExecutorTest.java +++ b/android/src/test/java/com/getcapacitor/community/admob/consent/AdConsentExecutorTest.java @@ -62,7 +62,7 @@ void beforeEach() { adConsentExecutor = new AdConsentExecutor(() -> contextMock, () -> activityMock, notifierMock, LOG_TAG); - doAnswer(invocation -> { + doAnswer((invocation) -> { Runnable runnable = invocation.getArgument(0); runnable.run(); return null; diff --git a/android/src/test/java/com/getcapacitor/community/admob/interstitial/AdInterstitialExecutorTest.java b/android/src/test/java/com/getcapacitor/community/admob/interstitial/AdInterstitialExecutorTest.java index 70ffd497..6447fa1a 100644 --- a/android/src/test/java/com/getcapacitor/community/admob/interstitial/AdInterstitialExecutorTest.java +++ b/android/src/test/java/com/getcapacitor/community/admob/interstitial/AdInterstitialExecutorTest.java @@ -146,7 +146,8 @@ void usesIdHelper() { Runnable uiThreadRunnable = runnableArgumentCaptor.getValue(); uiThreadRunnable.run(); - interstitialAdMockedStatic.verify(() -> InterstitialAd.load(any(), idArgumentCaptor.capture(), adRequestCaptor.capture(), any()) + interstitialAdMockedStatic.verify(() -> + InterstitialAd.load(any(), idArgumentCaptor.capture(), adRequestCaptor.capture(), any()) ); assertEquals(idFromViewHelper, idArgumentCaptor.getValue());