From 906b8eaeeff18ed80d88443ca2df05a59c79c71c Mon Sep 17 00:00:00 2001 From: Mobile Ads Developer Relations Date: Tue, 8 Oct 2024 17:21:55 -0700 Subject: [PATCH] Add `isAdAvailable` and `pollAd` methods to the Interstitial, UnityRewardedAd and UnityAppOpenAd classes in android library. PiperOrigin-RevId: 683818191 --- .../com/google/unity/ads/Interstitial.java | 25 +++++++++++++------ .../com/google/unity/ads/UnityAppOpenAd.java | 13 ++++++++++ .../com/google/unity/ads/UnityRewardedAd.java | 19 ++++++++++---- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/source/android-library/app/src/main/java/com/google/unity/ads/Interstitial.java b/source/android-library/app/src/main/java/com/google/unity/ads/Interstitial.java index 8a08bd9c9..06877622f 100644 --- a/source/android-library/app/src/main/java/com/google/unity/ads/Interstitial.java +++ b/source/android-library/app/src/main/java/com/google/unity/ads/Interstitial.java @@ -42,15 +42,11 @@ public class Interstitial { */ private InterstitialAd interstitialAd; - /** - * The {@code Activity} on which the interstitial will display. - */ - private Activity activity; + /** The {@code Activity} on which the interstitial will display. */ + private final Activity activity; - /** - * A listener implemented in Unity via {@code AndroidJavaProxy} to receive ad events. - */ - private UnityInterstitialAdCallback callback; + /** A listener implemented in Unity via {@code AndroidJavaProxy} to receive ad events. */ + private final UnityInterstitialAdCallback callback; public Interstitial(Activity activity, UnityInterstitialAdCallback callback) { this.activity = activity; @@ -197,6 +193,19 @@ public void run() { }); } + /** Returns {@code true} if there is an interstitial ad available in the pre-load queue. */ + public boolean isAdAvailable(@NonNull String adUnitId) { + return InterstitialAd.isAdAvailable(activity, adUnitId); + } + + /** + * Retrieves the next interstitial ad available in pre-load queue, or {@code null} if no ad is + * available. + */ + public void pollAd(@NonNull String adUnitId) { + interstitialAd = InterstitialAd.pollAd(activity, adUnitId); + } + /** Returns the {@link InterstitialAd} ad unit ID. */ @Nullable public String getAdUnitId() { diff --git a/source/android-library/app/src/main/java/com/google/unity/ads/UnityAppOpenAd.java b/source/android-library/app/src/main/java/com/google/unity/ads/UnityAppOpenAd.java index af4312d96..97b348f31 100644 --- a/source/android-library/app/src/main/java/com/google/unity/ads/UnityAppOpenAd.java +++ b/source/android-library/app/src/main/java/com/google/unity/ads/UnityAppOpenAd.java @@ -159,6 +159,19 @@ public void onAdFailedToLoad(@NonNull final LoadAdError error) { })); } + /** + * Retrieves the next app open ad available in Preload queue, or {@code null} if no ad is + * available. + */ + public void pollAd(@NonNull String adUnitId) { + appOpenAd = AppOpenAd.pollAd(activity, adUnitId); + } + + /** Returns {@code true} if there is an interstitial ad available in the pre-load queue. */ + public boolean isAdAvailable(@NonNull String adUnitId) { + return AppOpenAd.isAdAvailable(activity, adUnitId); + } + public void show() { if (appOpenAd == null) { Log.e(PluginUtils.LOGTAG, "Tried to show app open ad before it was ready. This should " diff --git a/source/android-library/app/src/main/java/com/google/unity/ads/UnityRewardedAd.java b/source/android-library/app/src/main/java/com/google/unity/ads/UnityRewardedAd.java index a7cd0a679..93434560e 100644 --- a/source/android-library/app/src/main/java/com/google/unity/ads/UnityRewardedAd.java +++ b/source/android-library/app/src/main/java/com/google/unity/ads/UnityRewardedAd.java @@ -30,17 +30,14 @@ public class UnityRewardedAd { */ private RewardedAd rewardedAd; - /** - * The {@code Activity} on which the rewarded ad will display. - */ - private Activity activity; + /** The {@code Activity} on which the rewarded ad will display. */ + private final Activity activity; /** * A callback implemented in Unity via {@code AndroidJavaProxy} to receive ad events. */ private UnityRewardedAdCallback callback; - public UnityRewardedAd(Activity activity, UnityRewardedAdCallback callback) { this.activity = activity; this.callback = callback; @@ -178,6 +175,18 @@ public void run() { }); } + /** + * Retrieves the next rewarded ad available in Preload queue, or {@code null} if no ad is + * available. + */ + public void pollAd(@NonNull String adUnitId) { + rewardedAd = RewardedAd.pollAd(activity, adUnitId); + } + + /** Returns {@code true} if there is an interstitial ad available in the pre-load queue. */ + public boolean isAdAvailable(@NonNull String adUnitId) { + return RewardedAd.isAdAvailable(activity, adUnitId); + } /** * Shows the rewarded ad if it has loaded.