#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 AndroidNativeAd : INativeAd { private const string NativeAdLoaderClassName = AndroidPlatformTool.ClassPackage + ".api.NativeAdLoader$Builder"; private const string NativeAdRequestClassName = AndroidPlatformTool.ClassPackage + ".api.NativeAdRequest$Builder"; private const string NativeBuildMethod = "build"; private const string NativeAdLoaderBuildMethod = "withAdLoadListener"; private const string NativeAdLoaderExtMethod = "withExt"; private AndroidJavaObject NativeAd; 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 AndroidNativeAd() { OnAdLoad += ((ad) => { NativeAd = ad; OnLoad?.Invoke(); }); } private event Action OnAdLoad; public void Load(string slotId, BigoNativeRequest request) { if (request == null) { return; } var nativeLoaderBuilder = new AndroidJavaObject(NativeAdLoaderClassName); nativeLoaderBuilder?.Call(NativeAdLoaderExtMethod, request.ExtraInfoJson); nativeLoaderBuilder?.Call(NativeAdLoaderBuildMethod, new AdLoadCallback(OnAdLoad, OnLoadFailed)); var nativeLoader = nativeLoaderBuilder?.Call(NativeBuildMethod); var nativeRequestBuilder = new AndroidJavaObject(NativeAdRequestClassName); nativeRequestBuilder?.Call("withSlotId", slotId); nativeRequestBuilder?.Call("withAge", request.Age); nativeRequestBuilder?.Call("withGender", (int)(request.Gender)); nativeRequestBuilder?.Call("withActivatedTime", request.ActivatedTime); var nativeRequest = nativeRequestBuilder?.Call(NativeBuildMethod); nativeLoader?.Call("loadAd", nativeRequest); } public bool IsLoaded() { return NativeAd != null; } public void Show() { NativeAd?.Call("setAdInteractionListener", new AdInteractionCallback(OnAdShowed, OnAdClicked, OnAdDismissed, OnAdError)); AndroidPlatformTool.CallMethodOnMainThread(() => { AdHelper.ShowNativeAd(NativeAd); }); } public void Destroy() { AdHelper.DestroyAd(NativeAd); } public bool IsExpired() { return NativeAd != null && NativeAd.Call("isExpired"); } public void SetPosition(BigoPosition position) { AndroidPlatformTool.CallMethodOnMainThread(() => { AdHelper.ShowNativeAd(NativeAd, position); }); } public bool IsClientBidding() { if (NativeAd == null) return false; AndroidJavaObject bid = NativeAd.Call("getBid"); return bid != null; } public string GetExtraInfo(string key) { if (NativeAd == null) return ""; return NativeAd.Call("getExtraInfo", key); } /// get price public double getPrice() { if (NativeAd == null) return 0; AndroidJavaObject bid = NativeAd.Call("getBid"); return bid == null ? 0 : bid.Call("getPrice"); } ///notify win public void notifyWin(double secPrice, string secBidder) { if (NativeAd == null) return; var secPriceDouble = new AndroidJavaClass("java.lang.Double").CallStatic ("valueOf", secPrice); NativeAd.Call("getBid")?.Call("notifyWin", secPriceDouble, secBidder); } ///notify loss public void notifyLoss(double firstPrice, string firstBidder, BGAdLossReason lossReason) { if (NativeAd == null) return; var firstPriceDouble = new AndroidJavaClass("java.lang.Double").CallStatic ("valueOf", firstPrice); NativeAd.Call("getBid")?.Call("notifyLoss", firstPriceDouble, firstBidder, (int)lossReason); } } } #endif