Skip to content

Commit

Permalink
added ToChartboostMediationBannerSize extension to AndroidJavaObject
Browse files Browse the repository at this point in the history
  • Loading branch information
kushG committed Oct 20, 2023
1 parent dd5fd95 commit 51d892c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,53 +195,23 @@ class BannerAdWrapper(private val ad: HeliumBannerAd) {
}
}

fun getAdSize(): String {
val size = ad.getSize()
val creativeSize = partnerAd?.let {
Size((it.width / displayDensity).toInt(), (it.height / displayDensity).toInt())
}

// if partnerAd is not available then adSize is unknown
val sizeType = partnerAd?.let {
when (size?.name) {
"STANDARD" -> 0
"MEDIUM" -> 1
"LEADERBOARD" -> 2
"ADAPTIVE" -> 3
else -> -1
fun getAdSize(): HeliumBannerAd.HeliumBannerSize {
val width = partnerAd?.let { (it.width/displayDensity).toInt() } ?: run { 0 }
val height = partnerAd?.let { (it.height/displayDensity).toInt() } ?: run { 0 }
return when (ad.getSize()?.name) {
"STANDARD" -> HeliumBannerAd.HeliumBannerSize.STANDARD
"MEDIUM" -> HeliumBannerAd.HeliumBannerSize.MEDIUM
"LEADERBOARD" -> HeliumBannerAd.HeliumBannerSize.LEADERBOARD
"ADAPTIVE" -> HeliumBannerAd.HeliumBannerSize.bannerSize(width, height)
else -> {
Log.w(TAG, "Size not defined, set to ADAPTIVE(0x0) by default")
HeliumBannerAd.HeliumBannerSize.bannerSize(0,0)
}
}?: run { -1 } // -1 => Unknown

val json = JSONObject()
json.put("sizeType", sizeType)
json.put("aspectRatio", size?.aspectRatio)
json.put("width", creativeSize?.width ?: 0)
json.put("height", creativeSize?.height ?: 0)
json.put("type", size?.isAdaptive)

return json.toString()
}

fun getContainerSize(): String {
val size = ad.getSize()
val sizeType = when (size?.name) {
"STANDARD" -> 0
"MEDIUM" -> 1
"LEADERBOARD" -> 2
"ADAPTIVE" -> 3
else -> -1
}

val json = JSONObject()
json.put("sizeType", sizeType)
json.put("aspectRatio", size?.aspectRatio)
json.put("width", (size?.width ?: 0) / displayDensity)
json.put("height", (size?.height ?: 0) / displayDensity)
json.put("type", size?.isAdaptive)

return json.toString()
}

fun getContainerSize(): HeliumBannerAd.HeliumBannerSize? = ad.getSize()

fun resizeToFit(axis: Int, pivotX: Float, pivotY: Float) {
runTaskOnUiThread {
partnerAd?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public enum ChartboostMediationBannerSizeType
public struct ChartboostMediationBannerSize
{
[JsonProperty("sizeType")]
public ChartboostMediationBannerSizeType SizeType { get; private set; }
public ChartboostMediationBannerSizeType SizeType { get; internal set; }
[JsonProperty("aspectRatio")]
public float AspectRatio;
[JsonProperty("width")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,14 @@ protected set { }
/// <inheritdoc cref="ChartboostMediationBannerViewBase.AdSize"/>
public override ChartboostMediationBannerSize? AdSize
{
get
{
var sizeJson = _bannerAd.Call<string>("getAdSize");
if (string.IsNullOrEmpty(sizeJson))
return null;
return JsonConvert.DeserializeObject<ChartboostMediationBannerSize>(sizeJson);
}
get => _bannerAd.Call<AndroidJavaObject>("getAdSize").ToChartboostMediationBannerSize();
protected set { }
}

/// <inheritdoc cref="ChartboostMediationBannerViewBase.ContainerSize"/>
public override ChartboostMediationBannerSize? ContainerSize
{
get
{
var sizeJson = _bannerAd.Call<string>("getContainerSize");
if (string.IsNullOrEmpty(sizeJson))
return null;
return JsonConvert.DeserializeObject<ChartboostMediationBannerSize>(sizeJson);
}
get => _bannerAd.Call<AndroidJavaObject>("getContainerSize").ToChartboostMediationBannerSize();
protected set { }
}

Expand Down
Binary file not shown.
26 changes: 26 additions & 0 deletions com.chartboost.mediation/Runtime/Utilities/AndroidExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#if UNITY_ANDROID
using System;
using System.Collections.Generic;
using Chartboost.AdFormats.Banner;
using Chartboost.Events;
using Chartboost.Platforms.Android;
using Newtonsoft.Json;
Expand Down Expand Up @@ -36,6 +38,30 @@ public static BidInfo MapToWinningBidInfo(this AndroidJavaObject map)
return biddingInfo;
}

public static ChartboostMediationBannerSize ToChartboostMediationBannerSize(this AndroidJavaObject source)
{
if (source == null)
return new ChartboostMediationBannerSize();

var name = source.Get<string>("name");
var width = source.Get<int>("width");
var height = source.Get<int>("height");
var size = name switch
{
"STANDARD" => ChartboostMediationBannerSize.Standard,
"MEDIUM" => ChartboostMediationBannerSize.MediumRect,
"LEADERBOARD" => ChartboostMediationBannerSize.Leaderboard,
"ADAPTIVE" => ChartboostMediationBannerSize.Adaptive(width, height),
_ => throw new ArgumentOutOfRangeException()
};

// if we get adaptive size of size 0X0 then it is undefined/unknown
if (size is { SizeType: ChartboostMediationBannerSizeType.Adaptive, Width: 0, Height: 0 })
size.SizeType = ChartboostMediationBannerSizeType.Unknown;

return size;
}

public static string ImpressionDataToJsonString(this AndroidJavaObject impressionData)
{
var placementName = impressionData.Get<string>("placementId");
Expand Down

0 comments on commit 51d892c

Please sign in to comment.