Skip to content

Commit

Permalink
Support rendering controls in mediation ad units (#430)
Browse files Browse the repository at this point in the history
* feat: support rendering controls in mediation ad units #425

* fix: skip release tests in debug mode
  • Loading branch information
ValentinPostindustria authored Apr 27, 2022
1 parent 5f022f8 commit 25f5872
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,8 @@ public MobileSdkPassThrough getMobileSdkPassThrough() {
return mobileSdkPassThrough;
}

public void setMobileSdkPassThrough(@Nullable MobileSdkPassThrough mobileSdkPassThrough) {
this.mobileSdkPassThrough = mobileSdkPassThrough;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.prebid.mobile.rendering.bidding.interfaces.InterstitialViewListener;
import org.prebid.mobile.rendering.errors.AdException;
import org.prebid.mobile.rendering.models.AdDetails;
import org.prebid.mobile.rendering.models.openrtb.bidRequests.MobileSdkPassThrough;
import org.prebid.mobile.rendering.networking.WinNotifier;
import org.prebid.mobile.units.configuration.AdFormat;
import org.prebid.mobile.units.configuration.AdUnitConfiguration;
Expand Down Expand Up @@ -94,6 +95,7 @@ public InterstitialController(Context context, InterstitialControllerListener li
}

public void loadAd(AdUnitConfiguration adUnitConfiguration, BidResponse bidResponse) {
setRenderingControlSettings(adUnitConfiguration, bidResponse);
WinNotifier winNotifier = new WinNotifier();
winNotifier.notifyWin(bidResponse, () -> {
mAdUnitIdentifierType = bidResponse.isVideo()
Expand Down Expand Up @@ -132,12 +134,23 @@ public void show() {
break;
default:
LogUtil.error(TAG, "show: Failed. Did you specify correct AdUnitConfigurationType? "
+ "Supported types: VAST, INTERSTITIAL. "
+ "Provided type: " + mAdUnitIdentifierType);
+ "Supported types: VAST, INTERSTITIAL. "
+ "Provided type: " + mAdUnitIdentifierType);
}
}

public void destroy() {
mBidInterstitialView.destroy();
}

private void setRenderingControlSettings(
AdUnitConfiguration adUnitConfiguration,
BidResponse bidResponse
) {
MobileSdkPassThrough renderingControlSettings = bidResponse.getMobileSdkPassThrough();
if (renderingControlSettings != null) {
renderingControlSettings.modifyAdUnitConfiguration(adUnitConfiguration);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.prebid.mobile.rendering.bidding.display;

import android.content.Context;
import androidx.annotation.FloatRange;
import androidx.annotation.Nullable;
import org.prebid.mobile.AdSize;
import org.prebid.mobile.units.configuration.Position;

public abstract class MediationBaseFullScreenAdUnit extends MediationBaseAdUnit {

private static final String TAG = MediationBaseFullScreenAdUnit.class.getSimpleName();

protected MediationBaseFullScreenAdUnit(
Context context,
String configId,
AdSize adSize,
PrebidMediationDelegate mediationDelegate
) {
super(context, configId, adSize, mediationDelegate);
}

/**
* Sets max video duration. If the ad from server is bigger, it will be rejected.
*/
public void setMaxVideoDuration(int seconds) {
mAdUnitConfig.setMaxVideoDuration(seconds);
}

/**
* Sets delay in seconds to show skip or close button.
*/
public void setSkipDelay(int secondsDelay) {
mAdUnitConfig.setSkipDelay(secondsDelay);
}

/**
* Sets skip button percentage size in range from 0.05 to 1.
* If value less than 0.05, size will be default.
*/
public void setSkipButtonArea(@FloatRange(from = 0, to = 1.0) double buttonArea) {
mAdUnitConfig.setSkipButtonArea(buttonArea);
}

/**
* Sets skip button position on the screen. Suitable values TOP_LEFT and TOP_RIGHT.
* Default value TOP_RIGHT.
*/
public void setSkipButtonPosition(Position skipButtonPosition) {
mAdUnitConfig.setSkipButtonPosition(skipButtonPosition);
}

/**
* Sets close button percentage size in range from 0.05 to 1.
* If value less than 0.05, size will be default.
*/
public void setCloseButtonArea(@FloatRange(from = 0, to = 1.0) double closeButtonArea) {
mAdUnitConfig.setCloseButtonArea(closeButtonArea);
}

/**
* Sets close button position on the screen. Suitable values TOP_LEFT and TOP_RIGHT.
* Default value TOP_RIGHT.
*/
public void setCloseButtonPosition(@Nullable Position closeButtonPosition) {
mAdUnitConfig.setCloseButtonPosition(closeButtonPosition);
}

/**
* Sets desired is muted property.
*/
public void setIsMuted(boolean isMuted) {
mAdUnitConfig.setIsMuted(isMuted);
}

/**
* Makes sound button visible.
*/
public void setIsSoundButtonVisible(boolean isSoundButtonVisible) {
mAdUnitConfig.setIsSoundButtonVisible(isSoundButtonVisible);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@

import java.util.EnumSet;

public class MediationInterstitialAdUnit extends MediationBaseAdUnit {
public class MediationInterstitialAdUnit extends MediationBaseFullScreenAdUnit {

private static final String TAG = MediationInterstitialAdUnit.class.getSimpleName();

/**
* Constructor to fetch demand for a display interstitial ad with specified minHeightPercentage and minWidthPercentage
*/
public MediationInterstitialAdUnit(
Context context,
String configId,
AdSize minSizePercentage,
Context context,
String configId,
AdSize minSizePercentage,
PrebidMediationDelegate mediationDelegate
) {
super(context, configId, minSizePercentage, mediationDelegate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@
import org.prebid.mobile.rendering.models.AdPosition;
import org.prebid.mobile.units.configuration.AdFormat;

public class MediationRewardedVideoAdUnit extends MediationBaseAdUnit {
public class MediationRewardedVideoAdUnit extends MediationBaseFullScreenAdUnit {

private static final String TAG = "MediationRewardedVideoAdUnit";
private static final String TAG = "MediationRewardedAdUnit";

public MediationRewardedVideoAdUnit(Context context, String configId, PrebidMediationDelegate mediationDelegate) {
public MediationRewardedVideoAdUnit(
Context context,
String configId,
PrebidMediationDelegate mediationDelegate
) {
super(context, configId, null, mediationDelegate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.prebid.mobile.rendering.bidding.loader;

import android.content.Context;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import org.prebid.mobile.LogUtil;
import org.prebid.mobile.PrebidMobile;
import org.prebid.mobile.rendering.bidding.data.bid.BidResponse;
Expand Down Expand Up @@ -62,7 +62,7 @@ public void onResponse(BaseNetworkTask.GetUrlResult response) {
return;
}
checkTmax(response, bidResponse);
updateAdUnitConfiguration(bidResponse.getMobileSdkPassThrough());
updateAdUnitConfiguration(bidResponse);
if (mRequestListener != null) {
setupRefreshTimer();
mRequestListener.onFetchCompleted(bidResponse);
Expand Down Expand Up @@ -217,10 +217,16 @@ private void checkTmax(
}
}

private void updateAdUnitConfiguration(@Nullable MobileSdkPassThrough mobileSdkPassThrough) {
if (mobileSdkPassThrough != null) {
mobileSdkPassThrough.modifyAdUnitConfiguration(mAdConfiguration);
}
/**
* Gets mobile sdk pass through object, combines it with user's ad unit
* rendering controls settings in configuration, modifies ad unit
* configuration, sets combined parameters to bid response.
*/
private void updateAdUnitConfiguration(@NonNull BidResponse bidResponse) {
MobileSdkPassThrough serverParameters = bidResponse.getMobileSdkPassThrough();
MobileSdkPassThrough combinedParameters = MobileSdkPassThrough.combine(serverParameters, mAdConfiguration);
combinedParameters.modifyAdUnitConfiguration(mAdConfiguration);
bidResponse.setMobileSdkPassThrough(combinedParameters);
}

public interface BidRefreshListener {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.prebid.mobile.rendering.models.openrtb.bidRequests;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -56,8 +57,8 @@ public static MobileSdkPassThrough create(JSONObject extJson) {
*/
@Nullable
public static MobileSdkPassThrough combine(
@Nullable MobileSdkPassThrough fromBid,
@Nullable MobileSdkPassThrough fromRoot
@Nullable MobileSdkPassThrough fromBid,
@Nullable MobileSdkPassThrough fromRoot
) {
if (fromBid == null && fromRoot == null) {
return null;
Expand Down Expand Up @@ -91,6 +92,48 @@ public static MobileSdkPassThrough combine(
return fromBid;
}

/**
* Combines unified pass through object with rendering controls
* from ad unit configuration. Settings from ad unit configuration
* have lower priority.
*/
@NonNull
public static MobileSdkPassThrough combine(
@Nullable MobileSdkPassThrough unifiedPassThrough,
@NonNull AdUnitConfiguration configuration
) {
MobileSdkPassThrough result;
if (unifiedPassThrough == null) {
result = new MobileSdkPassThrough();
} else {
result = unifiedPassThrough;
}

if (result.isMuted == null) {
result.isMuted = configuration.isMuted();
}
if (result.maxVideoDuration == null) {
result.maxVideoDuration = configuration.getMaxVideoDuration();
}
if (result.skipDelay == null) {
result.skipDelay = configuration.getSkipDelay();
}
if (result.skipButtonArea == null) {
result.skipButtonArea = configuration.getSkipButtonArea();
}
if (result.skipButtonPosition == null) {
result.skipButtonPosition = configuration.getSkipButtonPosition();
}
if (result.closeButtonArea == null) {
result.closeButtonArea = configuration.getCloseButtonArea();
}
if (result.closeButtonPosition == null) {
result.closeButtonPosition = configuration.getCloseButtonPosition();
}
return result;
}


public Boolean isMuted;

public Integer maxVideoDuration;
Expand All @@ -104,6 +147,8 @@ public static MobileSdkPassThrough combine(

private JSONObject configuration;

private MobileSdkPassThrough() {}

private MobileSdkPassThrough(JSONObject passThrough) {
try {
if (passThrough.has("adconfiguration")) {
Expand Down Expand Up @@ -148,9 +193,9 @@ public void modifyAdUnitConfiguration(AdUnitConfiguration adUnitConfiguration) {
}

private <T> void getAndSave(
String key,
Class<T> classType,
AfterCast<T> afterCast
String key,
Class<T> classType,
AfterCast<T> afterCast
) {
try {
if (configuration.has(key)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.prebid.mobile.rendering.bidding.data.bid;

import org.junit.Test;
import org.prebid.mobile.core.BuildConfig;
import org.prebid.mobile.rendering.models.openrtb.bidRequests.MobileSdkPassThrough;
import org.prebid.mobile.test.utils.ResourceUtils;
import org.prebid.mobile.units.configuration.Position;
Expand Down Expand Up @@ -72,21 +73,23 @@ public void whenInstantiatedWithoutWinningKeywords_NoBidsError() throws IOExcept

@Test
public void testMobileSdkPassThrough_checkFieldsUnification_returnUnifiedFields() throws IOException {
String responseString = ResourceUtils.convertResourceToString("BidResponseTest/mobile_sdk_pass_through.json");
if (!BuildConfig.DEBUG) {
String responseString = ResourceUtils.convertResourceToString("BidResponseTest/mobile_sdk_pass_through.json");

BidResponse subject = new BidResponse(responseString);
MobileSdkPassThrough mobileSdkPassThrough = subject.getMobileSdkPassThrough();
BidResponse subject = new BidResponse(responseString);
MobileSdkPassThrough mobileSdkPassThrough = subject.getMobileSdkPassThrough();

assertNotNull(mobileSdkPassThrough);
assertTrue(mobileSdkPassThrough.isMuted);
assertEquals((Double) 0.1, mobileSdkPassThrough.closeButtonArea);
assertEquals(Position.TOP_LEFT, mobileSdkPassThrough.closeButtonPosition);
assertEquals((Double) 0.2, mobileSdkPassThrough.skipButtonArea);
assertEquals(Position.TOP_RIGHT, mobileSdkPassThrough.skipButtonPosition);
assertEquals((Integer) 15, mobileSdkPassThrough.skipDelay);
assertNotNull(mobileSdkPassThrough);
assertTrue(mobileSdkPassThrough.isMuted);
assertEquals((Double) 0.1, mobileSdkPassThrough.closeButtonArea);
assertEquals(Position.TOP_LEFT, mobileSdkPassThrough.closeButtonPosition);
assertEquals((Double) 0.2, mobileSdkPassThrough.skipButtonArea);
assertEquals(Position.TOP_RIGHT, mobileSdkPassThrough.skipButtonPosition);
assertEquals((Integer) 15, mobileSdkPassThrough.skipDelay);

/* This field presents in both MobileSdkPassThrough objects */
assertEquals((Integer) 11, mobileSdkPassThrough.maxVideoDuration);
/* This field presents in both MobileSdkPassThrough objects */
assertEquals((Integer) 11, mobileSdkPassThrough.maxVideoDuration);
}
}

}
Loading

0 comments on commit 25f5872

Please sign in to comment.