#if UNITY_ANDROID using System; using BigoAds.Scripts.Api; using BigoAds.Scripts.Api.Constant; using BigoAds.Scripts.Common; using UnityEngine; namespace BigoAds.Scripts.Platforms.Android { class AndroidPopupAd : IPopupAd { private const string PopupAdLoaderClassName = AndroidPlatformTool.ClassPackage + ".api.popup.PopupAdLoader$Builder"; private const string PopupAdRequestClassName = AndroidPlatformTool.ClassPackage + ".api.popup.PopupAdRequest$Builder"; private const string PopupBuildMethod = "build"; private const string PopupAdLoaderBuildMethod = "withAdLoadListener"; private const string PopupAdLoaderExtMethod = "withExt"; private AndroidJavaObject PopupAd; public event Action OnLoad; public event Action OnLoadFailed; public event Action OnAdShowed; public event Action OnAdClicked; public event Action OnAdDismissed; public event Action OnAdError; public AndroidPopupAd() { OnAdLoad += ((ad) => { PopupAd = ad; OnLoad?.Invoke(); }); } private event Action OnAdLoad; public void Load(string slotId, BigoPopupRequest request) { if (request == null) { return; } var PopupLoaderBuilder = new AndroidJavaObject(PopupAdLoaderClassName); PopupLoaderBuilder?.Call(PopupAdLoaderExtMethod, request.ExtraInfoJson); PopupLoaderBuilder?.Call(PopupAdLoaderBuildMethod, new AdLoadCallback(OnAdLoad, OnLoadFailed)); var PopupLoader = PopupLoaderBuilder?.Call(PopupBuildMethod); var PopupRequestBuilder = new AndroidJavaObject(PopupAdRequestClassName); PopupRequestBuilder?.Call("withSlotId", slotId); PopupRequestBuilder?.Call("withAge", request.Age); PopupRequestBuilder?.Call("withGender", (int)(request.Gender)); PopupRequestBuilder?.Call("withActivatedTime", request.ActivatedTime); var PopupRequest = PopupRequestBuilder?.Call(PopupBuildMethod); PopupLoader?.Call("loadAd", PopupRequest); } public bool IsLoaded() { return PopupAd != null; } public void Show() { PopupAd?.Call("setAdInteractionListener", new AdInteractionCallback(OnAdShowed, OnAdClicked, OnAdDismissed, OnAdError)); AndroidPlatformTool.CallMethodOnMainThread(() => { PopupAd?.Call("show"); }); } public void Destroy() { //post to main AdHelper.DestroyAd(PopupAd); } public bool IsExpired() { return PopupAd != null && PopupAd.Call("isExpired"); } public bool IsClientBidding() { if (PopupAd == null) return false; AndroidJavaObject bid = PopupAd.Call("getBid"); return bid != null; } public string GetExtraInfo(string key) { if (PopupAd == null) return ""; return PopupAd.Call("getExtraInfo", key); } /// get price public double getPrice() { if (PopupAd == null) return 0; AndroidJavaObject bid = PopupAd.Call("getBid"); return bid == null ? 0 : bid.Call("getPrice"); } ///notify win public void notifyWin(double secPrice, string secBidder) { if (PopupAd == null) return; var secPriceDouble = new AndroidJavaClass("java.lang.Double").CallStatic ("valueOf", secPrice); PopupAd.Call("getBid")?.Call("notifyWin", secPriceDouble, secBidder); } ///notify loss public void notifyLoss(double firstPrice, string firstBidder, BGAdLossReason lossReason) { if (PopupAd == null) return; var firstPriceDouble = new AndroidJavaClass("java.lang.Double").CallStatic ("valueOf", firstPrice); PopupAd.Call("getBid")?.Call("notifyLoss", firstPriceDouble, firstBidder, (int)lossReason); } } } #endif