//
// FairBid Unity SDK
//
// Copyright (c) 2019 Fyber. All rights reserved.
//
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System;
namespace Fyber
{
///
/// Class responsible for managing Interstitial ads.
///
public class Interstitial : MonoBehaviour
{
private static InterstitialListener interstitialListener;
private static Interstitial _instance;
///
/// Requests an ad from the ad server.
///
/// The placement identifier for the particular ad.
///
///
public static void Request(string placementId)
{
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
#if UNITY_ANDROID
InterstitialAndroid.Fetch(placementId);
#elif UNITY_IOS
InterstitialIOS.Fetch(placementId);
#endif
#else
UnityEngine.Debug.LogWarning("Call received to fetch an Interstitial, but the SDK does not function in the editor. You must use a device/emulator to fetch/show ads.");
#endif
}
///
/// Displays an ad. If the placement has not been requested before, no ad will be shown
///
/// The placement identifier for the particular ad.
///
///
public static void Show(string placementId)
{
Show(placementId, null);
}
///
/// Displays an ad. If the placement has not been requested before, no ad will be shown
///
/// The placement identifier for the particular ad.
/// The options for the ad.
///
///
public static void Show(string placementId, ShowOptions options)
{
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
string optionsString = null;
if (options != null && options.CustomParameters != null)
{
optionsString = Utils.DictToJson(options.CustomParameters);
}
#if UNITY_ANDROID
InterstitialAndroid.Show(placementId, optionsString);
#elif UNITY_IOS
InterstitialIOS.Show(placementId, optionsString);
#endif
#else
UnityEngine.Debug.LogWarning("Call received to show an Interstitial, but the SDK does not function in the editor. You must use a device/emulator to fetch/show ads.");
#endif
}
///
/// Checks if an ad is immediately available to show.
///
/// true if the ad is completely loaded and ready to be shown.
/// The placement identifier for the particular ad.
///
///
public static bool IsAvailable(string placementId)
{
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
#if UNITY_ANDROID
return InterstitialAndroid.IsAvailable(placementId);
#elif UNITY_IOS
return InterstitialIOS.IsAvailable(placementId);
#endif
#else
return false;
#endif
}
///
/// Returns revenue and demand source information associated to the ad, if one is available.
///
/// An instance of if an ad is available, otherwise null
/// The placement identifier for the particular ad.
///
public static ImpressionData GetImpressionData(string placementId)
{
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
#if UNITY_ANDROID
return InterstitialAndroid.GetImpressionData(placementId);
#elif UNITY_IOS
return InterstitialIOS.GetImpressionData(placementId);
#endif
#else
return null;
#endif
}
///
/// Registers an ad callback to notify about every lifecycle events of an interstitial ad.
///
/// The listener which implements .
///
public static void SetInterstitialListener(InterstitialListener listener)
{
Interstitial.interstitialListener = listener;
}
///
/// Enables the auto–requesting behaviour for a given placement.
///
/// The placement id for which the auto–requesting should be enabled.
///
///
public static void EnableAutoRequesting(string placementId)
{
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
#if UNITY_ANDROID
InterstitialAndroid.EnableAutoRequesting(placementId);
#elif UNITY_IOS
InterstitialIOS.EnableAutoRequesting(placementId);
#endif
#else
UnityEngine.Debug.LogWarning("Call received to enable auto-requesting an Interstitial, but the SDK does not function in the editor. You must use a device/emulator.");
#endif
}
///
/// Disables auto–requesting for the given placement.
///
/// The placement id for which the auto–requesting should be disabled.
///
///
public static void DisableAutoRequesting(string placementId)
{
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
#if UNITY_ANDROID
InterstitialAndroid.DisableAutoRequesting(placementId);
#elif UNITY_IOS
InterstitialIOS.DisableAutoRequesting(placementId);
#endif
#else
UnityEngine.Debug.LogWarning("Call received to disable auto-requesting an Interstitial, but the SDK does not function in the editor. You must use a device/emulator.");
#endif
}
///
/// The amount of Interstitial impressions for this session
///
/// the amount of impressions
public static int GetImpressionDepth()
{
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
#if UNITY_ANDROID
return InterstitialAndroid.GetImpressionDepth();
#elif UNITY_IOS
return InterstitialIOS.GetImpressionDepth();
#endif
#else
return 0;
#endif
}
///
/// Summary of what +notifyLoss:reason: does
/// The placement id for notification loss.
/// LossNotificationReason reason type .
///
public static void NotifyLoss(string placementId, LossNotificationReason reason)
{
if (System.Enum.IsDefined(typeof(LossNotificationReason), reason))
{
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
#if UNITY_ANDROID
InterstitialAndroid.NotifyLoss(placementId, reason);
#elif UNITY_IOS
InterstitialIOS.NotifyLoss(placementId, reason);
#endif
#else
UnityEngine.Debug.LogWarning("Call received to notify loss on Interstitial, but the SDK does not function in the editor. You must use a device/emulator.");
#endif
}
}
#region Internal methods
internal static void InitReceiver()
{
if (_instance == null)
{
GameObject receiverObject = new GameObject("FairBidInterstitial");
DontDestroyOnLoad(receiverObject);
_instance = receiverObject.AddComponent();
}
}
public void InvokeCallback(string message)
{
CallbackInfo callbackInfo = CallbackInfo.FromJson(message);
Interstitial.InvokeCallback(callbackInfo);
}
private static void InvokeCallback(CallbackInfo callbackInfo)
{
if (Interstitial.interstitialListener != null)
{
string placementId = callbackInfo.placement_id;
switch (callbackInfo.callback)
{
case CallbackInfo.CallbackShow:
Interstitial.interstitialListener.OnShow(placementId, callbackInfo.impressionData);
break;
case CallbackInfo.CallbackFailed:
Interstitial.interstitialListener.OnShowFailure(placementId, callbackInfo.impressionData);
break;
case CallbackInfo.CallbackAvailable:
Interstitial.interstitialListener.OnAvailable(placementId);
break;
case CallbackInfo.CallbackUnavailable:
Interstitial.interstitialListener.OnUnavailable(placementId);
break;
case CallbackInfo.CallbackClick:
Interstitial.interstitialListener.OnClick(placementId);
break;
case CallbackInfo.CallbackHide:
Interstitial.interstitialListener.OnHide(placementId);
break;
case CallbackInfo.CallbackRequestStart:
Interstitial.interstitialListener.OnRequestStart(placementId, callbackInfo.request_id);
break;
default:
Console.WriteLine("Unknown callback for Interstitial");
break;
}
}
}
#endregion
}
#region Platform-specific translations
#if UNITY_IOS && !UNITY_EDITOR
public class InterstitialIOS
{
[DllImport ("__Internal")]
private static extern void fyb_sdk_show_interstitial(string placementId, string optionsString);
[DllImport ("__Internal")]
private static extern void fyb_sdk_fetch_interstitial(string placementId);
[DllImport ("__Internal")]
private static extern bool fyb_sdk_interstitial_is_available(string placementId);
[DllImport ("__Internal")]
private static extern string fyb_sdk_interstitial_get_impression_data(string placementId);
[DllImport ("__Internal")]
private static extern void fyb_sdk_interstitial_enable_auto_requesting(string placementId);
[DllImport ("__Internal")]
private static extern void fyb_sdk_interstitial_disable_auto_requesting(string placementId);
[DllImport ("__Internal")]
private static extern int fyb_sdk_impression_depth_interstitial();
[DllImport ("__Internal")]
private static extern void fyb_sdk_interstitial_notifyLoss(string placementId, string reason);
public static void Show(string placementId, string optionsString)
{
fyb_sdk_show_interstitial(placementId, optionsString);
}
public static void Fetch(string placementId)
{
fyb_sdk_fetch_interstitial(placementId);
}
public static bool IsAvailable(string placementId)
{
return fyb_sdk_interstitial_is_available(placementId);
}
public static ImpressionData GetImpressionData(string placementId)
{
string impressionDataString = fyb_sdk_interstitial_get_impression_data(placementId);
if (impressionDataString != null) {
return new ImpressionData(impressionDataString);
}
return null;
}
public static void EnableAutoRequesting(string placementId)
{
fyb_sdk_interstitial_enable_auto_requesting(placementId);
}
public static void DisableAutoRequesting(string placementId)
{
fyb_sdk_interstitial_disable_auto_requesting(placementId);
}
public static int GetImpressionDepth()
{
return fyb_sdk_impression_depth_interstitial();
}
public static void NotifyLoss(string placementId, LossNotificationReason reason)
{
fyb_sdk_interstitial_notifyLoss(placementId, reason.ToString());
}
}
#endif
#if UNITY_ANDROID && !UNITY_EDITOR
public class InterstitialAndroid
{
public static void Show(string placementId, string optionsString)
{
if(Application.platform != RuntimePlatform.Android) return;
AndroidJNIHelper.debug = false;
using (AndroidJavaClass jc = new AndroidJavaClass("com.fyber.fairbid.sdk.extensions.unity3d.UnityHelper"))
{
jc.CallStatic("showInterstitial", placementId, optionsString);
}
}
public static void Fetch(string placementId)
{
if(Application.platform != RuntimePlatform.Android) return;
AndroidJNIHelper.debug = false;
using (AndroidJavaClass jc = new AndroidJavaClass("com.fyber.fairbid.ads.Interstitial"))
{
jc.CallStatic("request", placementId);
}
}
public static void EnableAutoRequesting(string placementId)
{
if(Application.platform != RuntimePlatform.Android) return;
AndroidJNIHelper.debug = false;
using (AndroidJavaClass jc = new AndroidJavaClass("com.fyber.fairbid.ads.Interstitial"))
{
jc.CallStatic("enableAutoRequesting", placementId);
}
}
public static void DisableAutoRequesting(string placementId)
{
if(Application.platform != RuntimePlatform.Android) return;
AndroidJNIHelper.debug = false;
using (AndroidJavaClass jc = new AndroidJavaClass("com.fyber.fairbid.ads.Interstitial"))
{
jc.CallStatic("disableAutoRequesting", placementId);
}
}
public static Boolean IsAvailable(string placementId)
{
if(Application.platform != RuntimePlatform.Android) return false;
AndroidJNIHelper.debug = false;
using (AndroidJavaClass jc = new AndroidJavaClass("com.fyber.fairbid.ads.Interstitial"))
{
return jc.CallStatic("isAvailable", placementId);
}
}
public static ImpressionData GetImpressionData(string placementId)
{
if(Application.platform != RuntimePlatform.Android) return null;
AndroidJNIHelper.debug = false;
using (AndroidJavaClass jc = new AndroidJavaClass("com.fyber.fairbid.sdk.extensions.unity3d.UnityHelper"))
{
string impressionDataString = jc.CallStatic("getImpressionData", "interstitial", placementId);
if (impressionDataString != null) {
return new ImpressionData(impressionDataString);
}
return null;
}
}
public static int GetImpressionDepth()
{
if(Application.platform != RuntimePlatform.Android) return 0;
AndroidJNIHelper.debug = false;
using (AndroidJavaClass jc = new AndroidJavaClass("com.fyber.fairbid.ads.Interstitial"))
{
return jc.CallStatic("getImpressionDepth");
}
}
public static void NotifyLoss(string placementId, LossNotificationReason reason)
{
if(Application.platform != RuntimePlatform.Android) return;
AndroidJNIHelper.debug = false;
using (AndroidJavaClass jc = new AndroidJavaClass("com.fyber.fairbid.sdk.extensions.unity3d.UnityHelper"))
{
jc.CallStatic("notifyLoss", "INTERSTITIAL", placementId, reason.ToString());
}
}
}
#endif
#endregion
}