Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HB-6639] Adds ContainerSize to BannerView API #194

Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,26 @@ class BannerAdWrapper(private val ad: HeliumBannerAd) {
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)
kushG marked this conversation as resolved.
Show resolved Hide resolved

return json.toString()
}

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 @@ -93,6 +93,19 @@ public override ChartboostMediationBannerSize? AdSize
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);
}
protected set { }
}

/// <inheritdoc cref="ChartboostMediationBannerViewBase.HorizontalAlignment"/>
public override ChartboostMediationBannerHorizontalAlignment HorizontalAlignment
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ protected ChartboostMediationBannerViewBase(IntPtr uniqueId)
/// <inheritdoc cref="IChartboostMediationBannerView.AdSize"/>
public abstract ChartboostMediationBannerSize? AdSize { get; protected set; }

/// <inheritdoc cref="IChartboostMediationBannerView.ContainerSize"/>
public abstract ChartboostMediationBannerSize? ContainerSize { get; protected set; }

/// <inheritdoc cref="IChartboostMediationBannerView.HorizontalAlignment"/>
public abstract ChartboostMediationBannerHorizontalAlignment HorizontalAlignment { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,26 @@ public override ChartboostMediationBannerSize? AdSize
{
get
{
var sizeJson = _chartboostMediationBannerViewGetSize(UniqueId);
var sizeJson = _chartboostMediationBannerViewGetAdSize(UniqueId);
var size = string.IsNullOrEmpty(sizeJson) ? new ChartboostMediationBannerSize(): JsonConvert.DeserializeObject<ChartboostMediationBannerSize>(sizeJson);
return size;
}
protected set { }
}

/// <inheritdoc cref="ChartboostMediationBannerViewBase.ContainerSize"/>
public override ChartboostMediationBannerSize? ContainerSize
{
get
{
var sizeJson = _chartboostMediationBannerViewGetContainerSize(UniqueId);
var size = string.IsNullOrEmpty(sizeJson) ? new ChartboostMediationBannerSize(): JsonConvert.DeserializeObject<ChartboostMediationBannerSize>(sizeJson);
size.BannerType = Request.Size.BannerType;
return size;
}
protected set { }
}

/// <inheritdoc cref="ChartboostMediationBannerViewBase.HorizontalAlignment"/>
public override ChartboostMediationBannerHorizontalAlignment HorizontalAlignment
{
Expand Down Expand Up @@ -199,7 +212,10 @@ internal override void MoveTo(float x, float y)
private static extern void _chartboostMediationBannerViewSetKeywords(IntPtr uniqueId, string keywords);

[DllImport("__Internal")]
private static extern string _chartboostMediationBannerViewGetSize(IntPtr uniqueId);
private static extern string _chartboostMediationBannerViewGetAdSize(IntPtr uniqueId);

[DllImport("__Internal")]
private static extern string _chartboostMediationBannerViewGetContainerSize(IntPtr uniqueId);

[DllImport("__Internal")] [CanBeNull]
private static extern string _chartboostMediationBannerViewGetWinningBidInfo(IntPtr uniqueId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public ChartboostMediationBannerViewUnsupported()
/// <inheritdoc cref="ChartboostMediationBannerViewBase.AdSize"/>
public override ChartboostMediationBannerSize? AdSize { get; protected set; }

/// <inheritdoc cref="ChartboostMediationBannerViewBase.ContainerSize"/>
public override ChartboostMediationBannerSize? ContainerSize { get; protected set; }

/// <inheritdoc cref="ChartboostMediationBannerViewBase.HorizontalAlignment"/>
public override ChartboostMediationBannerHorizontalAlignment HorizontalAlignment { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public interface IChartboostMediationBannerView
/// </summary>
abstract ChartboostMediationBannerSize? AdSize { get; }

/// <summary>
/// The size of container. Note that this may change if <see cref="ResizeToFit"/> is called
/// </summary>
abstract ChartboostMediationBannerSize? ContainerSize { get; }

/// <summary>
/// The horizontal alignment of the ad within its container.
/// </summary>
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ TObj objFromJsonString(const char* jsonString) {
return toJSON(data);
}

const char * sizeToJSON(ChartboostMediationBannerSize* size)
{
NSString * aspectRatioKey =@"aspectRatio"; NSString * aspectRatioValue = [NSString stringWithFormat:@"%f", size.aspectRatio];
NSString * widthKey = @"width"; NSString * widthValue = [NSString stringWithFormat:@"%f", size.size.width];
NSString * heightKey = @"height"; NSString * heightValue = [NSString stringWithFormat:@"%f", size.size.height];
NSString * typeKey = @"type"; NSString * typeValue = [NSString stringWithFormat:@"%d", (int)size.type];

NSString *sizeTypeKey = @"sizeType";
NSString *sizeTypeValue = @"";
if(size.type == 0) { // Fixed
int width = size.size.width;
switch (width) {
case 320: sizeTypeValue = [NSString stringWithFormat:@"%d", 0]; break; // Standard
case 300: sizeTypeValue = [NSString stringWithFormat:@"%d", 1]; break; // Medium
case 728: sizeTypeValue = [NSString stringWithFormat:@"%d", 2]; break; // Leaderboard
default: sizeTypeValue = [NSString stringWithFormat:@"%d", -1];break; // Unknown
}
}
else{
sizeTypeValue = [NSString stringWithFormat:@"%d", 3]; // Adaptive
}
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:sizeTypeValue,sizeTypeKey,aspectRatioValue,aspectRatioKey,widthValue,widthKey,heightValue,heightKey,typeValue, typeKey, nil];

return dictionaryToJSON(dict);
}

const void serializeEvent(ChartboostMediationError *error, ChartboostMediationEvent event)
{
if (event == nil)
Expand Down Expand Up @@ -368,7 +394,6 @@ - (UIViewController*) getBannerViewController: (ChartboostMediationBannerView*)
return unityVC;
}


#pragma mark HeliumSdkDelegate
- (void)heliumDidStartWithError:(ChartboostMediationError *)error;
{
Expand Down Expand Up @@ -1049,31 +1074,22 @@ void _chartboostMediationBannerViewSetKeywords(const void* uniqueId, const char
ad.keywords = formattedKeywords;
}

const char * _chartboostMediationBannerViewGetSize(const void* uniqueId){
const char * _chartboostMediationBannerViewGetAdSize(const void* uniqueId){
ChartboostMediationBannerView *bannerView = _getBannerView(uniqueId);
return sizeToJSON(bannerView.size);
}

NSString * aspectRatioKey =@"aspectRatio"; NSString * aspectRatioValue = [NSString stringWithFormat:@"%f", bannerView.size.aspectRatio];
NSString * widthKey = @"width"; NSString * widthValue = [NSString stringWithFormat:@"%f", bannerView.size.size.width];
NSString * heightKey = @"height"; NSString * heightValue = [NSString stringWithFormat:@"%f", bannerView.size.size.height];
NSString * typeKey = @"type"; NSString * typeValue = [NSString stringWithFormat:@"%d", (int)bannerView.size.type];
const char * _chartboostMediationBannerViewGetContainerSize(const void* uniqueId){
ChartboostMediationBannerView *bannerView = _getBannerView(uniqueId);

NSString *sizeTypeKey = @"sizeType";
NSString *sizeTypeValue = @"";
if(bannerView.size.type == 0) { // Fixed
int width = bannerView.size.size.width;
switch (width) {
case 320: sizeTypeValue = [NSString stringWithFormat:@"%d", 0]; break; // Standard
case 300: sizeTypeValue = [NSString stringWithFormat:@"%d", 1]; break; // Medium
case 728: sizeTypeValue = [NSString stringWithFormat:@"%d", 2]; break; // Leaderboard
default: sizeTypeValue = [NSString stringWithFormat:@"%d", -1];break; // Unknown
}
}
else{
sizeTypeValue = [NSString stringWithFormat:@"%d", 3]; // Adaptive
}
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:sizeTypeValue,sizeTypeKey,aspectRatioValue,aspectRatioKey,widthValue,widthKey,heightValue,heightKey,typeValue, typeKey, nil];

return dictionaryToJSON(dict);
// Note : `bannerView.size.type` is always 0 even if load request was made with adaptive size
// Therefore, we always set it to adaptive here and let Unity updated it based on size type at load request
// if(bannerView.size.type == 0) // Fixed
// return sizeToJSON(bannerView.request.size);

// Adaptive
ChartboostMediationBannerSize *size = [ChartboostMediationBannerSize adaptiveWithWidth:bannerView.frame.size.width maxHeight:bannerView.frame.size.height];
return sizeToJSON(size);
}

const char * _chartboostMediationBannerViewGetWinningBidInfo(const void* uniqueId){
Expand Down
Loading