|
| 1 | +// Copyright (C) 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#if GMA_PREVIEW_FEATURES |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using UnityEngine; |
| 20 | + |
| 21 | +using GoogleMobileAds.Api; |
| 22 | +using GoogleMobileAds.Common; |
| 23 | + |
| 24 | +namespace GoogleMobileAds.Android { |
| 25 | + public class NextGenInterstitialAdPreloaderClient : AndroidJavaProxy, |
| 26 | + IInterstitialAdPreloaderClient { |
| 27 | + private readonly AndroidJavaObject _unityInterstitialAdPreloader; |
| 28 | + |
| 29 | + private Action<string, IResponseInfoClient> _onAdPreloaded; |
| 30 | + private Action<string, IAdErrorClient> _onAdFailedToPreload; |
| 31 | + private Action<string> _onAdsExhausted; |
| 32 | + |
| 33 | + public NextGenInterstitialAdPreloaderClient() : base(NextGenUtils.UnityPreloadCallbackClassName) { |
| 34 | + var playerClass = new AndroidJavaClass(Utils.UnityActivityClassName); |
| 35 | + var activity = playerClass.GetStatic<AndroidJavaObject>("currentActivity"); |
| 36 | + _unityInterstitialAdPreloader = |
| 37 | + new AndroidJavaObject(NextGenUtils.UnityInterstitialAdPreloaderClassName, activity, this); |
| 38 | + } |
| 39 | + |
| 40 | + public bool Preload(string preloadId, PreloadConfiguration preloadConfiguration, |
| 41 | + Action<string, IResponseInfoClient> onAdPreloaded, |
| 42 | + Action<string, IAdErrorClient> onAdFailedToPreload, |
| 43 | + Action<string> onAdsExhausted) { |
| 44 | + _onAdFailedToPreload = onAdFailedToPreload; |
| 45 | + _onAdPreloaded = onAdPreloaded; |
| 46 | + _onAdsExhausted = onAdsExhausted; |
| 47 | + return _unityInterstitialAdPreloader.Call<bool>( |
| 48 | + "start", preloadId, NextGenUtils.GetPreloadConfigurationJavaObject(preloadConfiguration)); |
| 49 | + } |
| 50 | + |
| 51 | + public bool IsAdAvailable(string preloadId) { |
| 52 | + return _unityInterstitialAdPreloader.Call<bool>("isAdAvailable", preloadId); |
| 53 | + } |
| 54 | + |
| 55 | + public IInterstitialClient DequeueAd(string preloadId) { |
| 56 | + var interstitialAdClient = new NextGenInterstitialAdClient(); |
| 57 | + var unityInterstitialAd = _unityInterstitialAdPreloader.Call<AndroidJavaObject>( |
| 58 | + "pollAd", preloadId, interstitialAdClient); |
| 59 | + if (unityInterstitialAd == null) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + interstitialAdClient.androidInterstitialAd = unityInterstitialAd; |
| 63 | + return interstitialAdClient; |
| 64 | + } |
| 65 | + |
| 66 | + public int GetNumAdsAvailable(string preloadId) { |
| 67 | + return _unityInterstitialAdPreloader.Call<int>("getNumAdsAvailable", preloadId); |
| 68 | + } |
| 69 | + |
| 70 | + public PreloadConfiguration GetConfiguration(string preloadId) { |
| 71 | + return NextGenUtils.GetPreloadConfiguration( |
| 72 | + _unityInterstitialAdPreloader.Call<AndroidJavaObject>("getConfiguration", preloadId)); |
| 73 | + } |
| 74 | + |
| 75 | + public Dictionary<string, PreloadConfiguration> GetConfigurations() { |
| 76 | + var configurations = new Dictionary<string, PreloadConfiguration>(); |
| 77 | + var androidConfigurations = |
| 78 | + _unityInterstitialAdPreloader.Call<AndroidJavaObject>("getConfigurations"); |
| 79 | + var keySet = androidConfigurations.Call<AndroidJavaObject>("keySet"); |
| 80 | + var iterator = keySet.Call<AndroidJavaObject>("iterator"); |
| 81 | + |
| 82 | + while (iterator.Call<bool>("hasNext")) { |
| 83 | + var key = iterator.Call<AndroidJavaObject>("next"); |
| 84 | + var keyString = key.Call<string>("toString"); |
| 85 | + var androidPreloadConfiguration = androidConfigurations.Call<AndroidJavaObject>("get", key); |
| 86 | + configurations.Add(keyString, |
| 87 | + NextGenUtils.GetPreloadConfiguration(androidPreloadConfiguration)); |
| 88 | + } |
| 89 | + |
| 90 | + return configurations; |
| 91 | + } |
| 92 | + |
| 93 | + public void Destroy(string preloadId) { |
| 94 | + _unityInterstitialAdPreloader.Call("destroy", preloadId); |
| 95 | + } |
| 96 | + |
| 97 | + public void DestroyAll() { |
| 98 | + _unityInterstitialAdPreloader.Call("destroyAll"); |
| 99 | + } |
| 100 | + |
| 101 | +#region Callbacks from UnityPreloadCallback |
| 102 | + |
| 103 | + void onAdPreloaded(string preloadId, AndroidJavaObject responseInfo) { |
| 104 | + if (_onAdPreloaded != null) { |
| 105 | + _onAdPreloaded(preloadId, |
| 106 | + new ResponseInfoClient(ResponseInfoClientType.AdLoaded, responseInfo)); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + void onAdFailedToPreload(string preloadId, AndroidJavaObject error) { |
| 111 | + if (_onAdFailedToPreload != null) { |
| 112 | + _onAdFailedToPreload(preloadId, new AdErrorClient(error)); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + void onAdsExhausted(string preloadId) { |
| 117 | + if (_onAdsExhausted != null) { |
| 118 | + _onAdsExhausted(preloadId); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | +#endregion |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +#endif // GMA_PREVIEW_FEATURES |
0 commit comments