Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 234 additions & 6 deletions source/plugin/Assets/GoogleMobileAds/Api/AdManagerBannerView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,55 @@ namespace GoogleMobileAds.Api.AdManager
/// Banner view that works with Google Ad Manager. Banner views occupy a spot within an app's
/// layout. They stay on screen while users are interacting with the app.
/// </summary>
public class AdManagerBannerView : BannerView
public class AdManagerBannerView
{
/// <summary>
/// Raised when an ad is loaded into the banner view.
/// </summary>
public event Action OnBannerAdLoaded;

/// <summary>
/// Raised when an ad fails to load into the banner view.
/// </summary>
public event Action<LoadAdError> OnBannerAdLoadFailed;

/// <summary>
/// Raised when the ad is estimated to have earned money.
/// </summary>
public event Action<AdValue> OnAdPaid;

/// <summary>
/// Raised when an ad is clicked.
/// </summary>
public event Action OnAdClicked;

/// <summary>
/// Raised when an impression is recorded for an ad.
/// </summary>
public event Action OnAdImpressionRecorded;

/// <summary>
/// Raised when an ad opened full-screen content.
/// </summary>
public event Action OnAdFullScreenContentOpened;

/// <summary>
/// Raised when the ad closed full-screen content.
/// On iOS, this event is only raised when an ad opens an overlay, not when opening a new
/// application such as Safari or the App Store.
/// </summary>
public event Action OnAdFullScreenContentClosed;

/// <summary>
/// Raised when the app receives an event from the banner ad.
/// </summary>
public event Action<AppEvent> OnAppEventReceived;

/// <summary>
/// Returns true if Destroy() has been called.
/// </summary>
public bool IsDestroyed { get { return _client == null; } }

/// <summary>
/// Sets the supported sizes of the banner ad. In most cases, only one ad size will be
/// specified. Use one of the predefined standard ad sizes (such as AdSize.MediumRectangle),
Expand All @@ -50,10 +92,12 @@ public class AdManagerBannerView : BannerView
/// </summary>
public List<AdSize> ValidAdSizes
{
get { return ((IAdManagerBannerClient)_client).ValidAdSizes; }
set { ((IAdManagerBannerClient)_client).ValidAdSizes = value; }
get { return _client.ValidAdSizes; }
set { _client.ValidAdSizes = value; }
}

internal IAdManagerBannerClient _client;

/// <summary>
/// Creates an Ad Manager banner view with a standard position.
/// </summary>
Expand All @@ -74,11 +118,195 @@ public AdManagerBannerView(string adUnitId, AdSize adSize, int x, int y)
ConfigureBannerEvents();
}

protected internal override void ConfigureBannerEvents()
/// <summary>
/// Destroys the banner view.
/// </summary>
public void Destroy()
{
if (_client != null)
{
_client.DestroyBannerView();
_client = null;
}
}

/// <summary>
/// Returns the ad unit ID.
/// </summary>
public string GetAdUnitID()
{
return _client != null ? _client.GetAdUnitID() : null;
}

/// <summary>
/// Returns the ad request response info or null if the ad is not loaded.
/// </summary>
public ResponseInfo GetResponseInfo()
{
return _client != null ? new ResponseInfo(_client.GetResponseInfoClient()) : null;
}

/// <summary>
/// Returns the height of the banner view in pixels.
/// </summary>
public float GetHeightInPixels()
{
return _client != null ? _client.GetHeightInPixels() : 0;
}

/// <summary>
/// Returns the width of the banner view in pixels.
/// </summary>
public float GetWidthInPixels()
{
return _client != null ? _client.GetWidthInPixels() : 0;
}

/// <summary>
/// Loads an ad into the banner view.
/// </summary>
public void LoadAd(AdRequest request)
{
if (_client != null)
{
_client.LoadAd(request);
}
}

/// <summary>
/// Shows the banner view.
/// </summary>
public void Show()
{
if (_client != null)
{
_client.ShowBannerView();
}
}

/// <summary>
/// Hides the banner view.
/// </summary>
public void Hide()
{
if (_client != null)
{
_client.HideBannerView();
}
}

/// <summary>
/// Sets the position of the banner view using standard position.
/// </summary>
public void SetPosition(AdPosition adPosition)
{
base.ConfigureBannerEvents();
if (_client != null)
{
_client.SetPosition(adPosition);
}
}

/// <summary>
/// Sets the position of the banner view using custom position.
/// </summary>
public void SetPosition(int x, int y)
{
if (_client != null)
{
_client.SetPosition(x, y);
}
}

/// <summary>
/// Indicates whether the last loaded ad is a collapsible banner.
/// </summary>
public bool IsCollapsible()
{
return _client == null ? false : _client.IsCollapsible();
}

private void ConfigureBannerEvents()
{

_client.OnAdLoaded += (sender, args) =>
{
MobileAds.RaiseAction(() =>
{
if (OnBannerAdLoaded != null)
{
OnBannerAdLoaded();
}
});
};

_client.OnAdFailedToLoad += (sender, args) =>
{
LoadAdError loadAdError = new LoadAdError(args.LoadAdErrorClient);
MobileAds.RaiseAction(() =>
{
if (OnBannerAdLoadFailed != null)
{
OnBannerAdLoadFailed(loadAdError);
}
});
};

_client.OnAdOpening += (sender, args) =>
{
MobileAds.RaiseAction(() =>
{
if (OnAdFullScreenContentOpened != null)
{
OnAdFullScreenContentOpened();
}
});
};

_client.OnAdClosed += (sender, args) =>
{
MobileAds.RaiseAction(() =>
{
if (OnAdFullScreenContentClosed != null)
{
OnAdFullScreenContentClosed();
}
});
};

_client.OnPaidEvent += (adValue) =>
{
MobileAds.RaiseAction(() =>
{
if (OnAdPaid != null)
{
OnAdPaid(adValue);
}
});
};

_client.OnAdClicked += () =>
{
MobileAds.RaiseAction(() =>
{
if (OnAdClicked != null)
{
OnAdClicked();
}
});
};

_client.OnAdImpressionRecorded += () =>
{
MobileAds.RaiseAction(() =>
{
if (OnAdImpressionRecorded != null)
{
OnAdImpressionRecorded();
}
});
};

((IAdManagerBannerClient)_client).OnAppEvent += (appEvent) =>
_client.OnAppEvent += (appEvent) =>
{
MobileAds.RaiseAction(() =>
{
Expand Down
Loading