Skip to content

Commit

Permalink
[HB-6597] Adaptive Banner Fixes (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
kushG authored Oct 7, 2023
1 parent 9c6d055 commit 17c2198
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package com.chartboost.mediation.unity
import android.app.Activity
import android.graphics.Color
import android.graphics.PointF
import android.os.Build
import android.util.DisplayMetrics
import android.util.Log
import android.util.Size
import android.view.DisplayCutout
import android.view.Gravity
import android.view.View
import android.view.View.OnLayoutChangeListener
Expand Down Expand Up @@ -353,6 +355,8 @@ class BannerAdWrapper(private val ad: HeliumBannerAd) {
layout.gravity = bannerGravityPosition
usesGravity = true

keepWithinSafeArea(screenLocation)

// Attach the banner layout to the activity.
val density = displayDensity
try {
Expand Down Expand Up @@ -448,6 +452,55 @@ class BannerAdWrapper(private val ad: HeliumBannerAd) {
return ViewGroup.LayoutParams((pixels * width).toInt(), (pixels * height).toInt())
}

private fun keepWithinSafeArea(screenLocation: Int) {
ad.setOnApplyWindowInsetsListener { _, windowInsets ->
run {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val displayCutout: DisplayCutout? = windowInsets.displayCutout
if (displayCutout != null) {
var x = 0
var y = 0
when(screenLocation) {
// Top-left
0 -> {
x= displayCutout.safeInsetLeft
y = displayCutout.safeInsetTop
}
// Top-center
1 -> {
y = displayCutout.safeInsetTop
}
// Top-right
2 -> {
x= displayCutout.safeInsetRight
y = displayCutout.safeInsetTop
}
// center
3 -> {}
// bottom-left
4 -> {
x= displayCutout.safeInsetLeft
y = displayCutout.safeInsetBottom
}
// bottom-center
5 -> {
y = displayCutout.safeInsetBottom
}
// bottom-right
6 -> {
x= displayCutout.safeInsetRight
y = displayCutout.safeInsetBottom
}
}
ad.x = x.toFloat()
ad.y = y.toFloat()
}
}
windowInsets
}
}
}

private val displayDensity: Float
get() {
return activity?.resources?.displayMetrics?.density ?: DisplayMetrics.DENSITY_DEFAULT.toFloat()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import android.os.Build
import android.util.DisplayMetrics
import android.view.DisplayCutout
import android.view.MotionEvent
import android.view.WindowInsets
import android.widget.RelativeLayout
import com.chartboost.heliumsdk.ad.HeliumBannerAd
import com.unity3d.player.UnityPlayer
import kotlin.math.pow
import kotlin.math.sqrt

class BannerLayout
(
context: Context,
Expand Down Expand Up @@ -44,26 +45,25 @@ class BannerLayout
}
else {
val metrics = DisplayMetrics();
UnityPlayer.currentActivity.windowManager.defaultDisplay.getRealMetrics(metrics);
screenWidth = metrics.widthPixels;
UnityPlayer.currentActivity.windowManager.defaultDisplay.getRealMetrics(metrics)
screenWidth = metrics.widthPixels
screenHeight = metrics.heightPixels
}
}

override fun onApplyWindowInsets(insets: WindowInsets?): WindowInsets {
// Get safe area insets
setOnApplyWindowInsetsListener { _, windowInsets -> run {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val displayCutout: DisplayCutout? = windowInsets.displayCutout
if (displayCutout != null) {
// Get safe area insets
safeAreaTop = displayCutout.safeInsetTop
safeAreaLeft = displayCutout.safeInsetLeft
safeAreaRight = displayCutout.safeInsetRight
safeAreaBottom = displayCutout.safeInsetBottom
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val displayCutout: DisplayCutout? = insets?.displayCutout
if (displayCutout != null) {
// Get safe area insets
safeAreaTop = displayCutout.safeInsetTop
safeAreaLeft = displayCutout.safeInsetLeft
safeAreaRight = displayCutout.safeInsetRight
safeAreaBottom = displayCutout.safeInsetBottom
}
windowInsets
}
}
return super.onApplyWindowInsets(insets)
}

override fun onInterceptTouchEvent(event: MotionEvent?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,9 @@ private void onAdCached(AndroidJavaObject ad, string error)
return;
}

ChartboostMediationBannerAdLoadResult loadResult;
if (!string.IsNullOrEmpty(error))
{
loadResult = new ChartboostMediationBannerAdLoadResult(new ChartboostMediationError(error));
}
else
{
loadResult = new ChartboostMediationBannerAdLoadResult(bannerView.LoadId, null, null);
EventProcessor.ProcessChartboostMediationBannerEvent(ad.HashCode(),
(int)EventProcessor.BannerAdEvents.Load);
}
var loadResult = !string.IsNullOrEmpty(error)
? new ChartboostMediationBannerAdLoadResult(new ChartboostMediationError(error))
: new ChartboostMediationBannerAdLoadResult(bannerView.LoadId, null, null);

androidBannerView.LoadRequest.Complete(loadResult);
}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

typedef void (*ChartboostMediationBannerAdDragEvent)(void* uniqueId, float x, float y);

@interface ChartboostMediationBannerAdWrapper : NSObject
@interface ChartboostMediationBannerAdWrapper : NSObject<UIGestureRecognizerDelegate>

@property ChartboostMediationBannerView* bannerView;
@property ChartboostMediationBannerAdDragEvent dragListener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ - (instancetype)initWithBannerView:(ChartboostMediationBannerView *)bannerView a
self.bannerView = bannerView;

self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
self.panGesture.delegate = self;

[self.bannerView addGestureRecognizer:self.panGesture];

self.dragListener = dragListener;
Expand Down Expand Up @@ -131,4 +133,8 @@ - (void)handlePan:(UIPanGestureRecognizer *)gr{

self.dragListener((__bridge void*)self, x, y);
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return true;
}
@end

0 comments on commit 17c2198

Please sign in to comment.