Skip to content

Commit

Permalink
feat: make ad format as array #393
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinPostindustria committed Mar 24, 2022
1 parent c64ae8b commit 5a1ebc8
Show file tree
Hide file tree
Showing 28 changed files with 99 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void requestBannerAd(
}

AdUnitConfiguration adConfiguration = new AdUnitConfiguration();
adConfiguration.setAdUnitIdentifierType(AdFormat.BANNER);
adConfiguration.setAdFormat(AdFormat.BANNER);
DisplayViewListener listener = getListener(adMobListener);
adView = new DisplayView(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public abstract class AdUnit {

AdUnit(@NonNull String configId, @NonNull AdFormat adType) {
configuration.setConfigId(configId);
configuration.setAdUnitIdentifierType(adType);
configuration.setAdFormat(adType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void loadAd(AdUnitConfiguration adUnitConfiguration, BidResponse bidRespo
mAdUnitIdentifierType = bidResponse.isVideo()
? AdFormat.VAST
: AdFormat.INTERSTITIAL;
adUnitConfiguration.setAdUnitIdentifierType(mAdUnitIdentifierType);
adUnitConfiguration.setAdFormat(mAdUnitIdentifierType);
mBidInterstitialView.loadAd(adUnitConfiguration, bidResponse);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public MediationBannerAdUnit(Context context, String configId, AdSize size, Preb
protected final void initAdConfig(String configId, AdSize adSize) {
mAdUnitConfig.addSize(adSize);
mAdUnitConfig.setConfigId(configId);
mAdUnitConfig.setAdUnitIdentifierType(AdFormat.BANNER);
mAdUnitConfig.setAdFormat(AdFormat.BANNER);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ public final void fetchDemand(
protected final void initAdConfig(String configId, AdSize minSizePercentage) {
mAdUnitConfig.setMinSizePercentage(minSizePercentage);
mAdUnitConfig.setConfigId(configId);
mAdUnitConfig.setAdUnitIdentifierType(AdFormat.INTERSTITIAL);
mAdUnitConfig.setAdFormat(AdFormat.INTERSTITIAL);
mAdUnitConfig.setAdPosition(AdPosition.FULLSCREEN);
}

private void setAdUnitType(AdUnitFormat adUnitFormat) {
switch (adUnitFormat) {
case DISPLAY:
mAdUnitConfig.setAdUnitIdentifierType(AdFormat.INTERSTITIAL);
mAdUnitConfig.setAdFormat(AdFormat.INTERSTITIAL);
break;
case VIDEO:
mAdUnitConfig.setAdUnitIdentifierType(AdFormat.VAST);
mAdUnitConfig.setAdFormat(AdFormat.VAST);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void fetchDemand(@NonNull OnFetchCompleteListener listener) {
@Override
protected final void initAdConfig(String configId, AdSize adSize) {
mAdUnitConfig.setConfigId(configId);
mAdUnitConfig.setAdUnitIdentifierType(AdFormat.VAST);
mAdUnitConfig.setAdFormat(AdFormat.VAST);
mAdUnitConfig.setRewarded(true);
mAdUnitConfig.setAdPosition(AdPosition.FULLSCREEN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public void setBannerListener(BannerViewListener bannerListener) {
}

public void setVideoPlacementType(VideoPlacementType videoPlacement) {
mAdUnitConfig.setAdUnitIdentifierType(AdFormat.VAST);
mAdUnitConfig.setAdFormat(AdFormat.VAST);

final PlacementType placementType = VideoPlacementType.mapToPlacementType(videoPlacement);
mAdUnitConfig.setPlacementType(placementType);
Expand Down Expand Up @@ -438,7 +438,7 @@ private void initAdConfiguration() {
mAdUnitConfig.setConfigId(mConfigId);
mAdUnitConfig.setAutoRefreshDelay(mRefreshIntervalSec);
mEventHandler.setBannerEventListener(mBannerEventListener);
mAdUnitConfig.setAdUnitIdentifierType(AdFormat.BANNER);
mAdUnitConfig.setAdFormat(AdFormat.BANNER);
mAdUnitConfig.addSizes(mEventHandler.getAdSizeArray());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private InterstitialAdUnit(Context context, String configId,
AdUnitConfiguration adUnitConfiguration = new AdUnitConfiguration();
adUnitConfiguration.setConfigId(configId);
adUnitConfiguration.setMinSizePercentage(minSizePercentage);
adUnitConfiguration.setAdUnitIdentifierType(mapPrebidAdUnitTypeToAdConfigAdUnitType(adUnitFormat));
adUnitConfiguration.setAdFormat(mapPrebidAdUnitTypeToAdConfigAdUnitType(adUnitFormat));

init(adUnitConfiguration);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public RewardedAdUnit(Context context, String configId, RewardedEventHandler eve

AdUnitConfiguration adUnitConfiguration = new AdUnitConfiguration();
adUnitConfiguration.setConfigId(configId);
adUnitConfiguration.setAdUnitIdentifierType(AdFormat.VAST);
adUnitConfiguration.setAdFormat(AdFormat.VAST);
adUnitConfiguration.setRewarded(true);

init(adUnitConfiguration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.prebid.mobile.rendering.video.vast.VASTErrorCodes;
import org.prebid.mobile.rendering.views.interstitial.InterstitialManager;
import org.prebid.mobile.units.configuration.AdFormat;
import org.prebid.mobile.units.configuration.AdUnitConfiguration;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
Expand All @@ -63,7 +64,7 @@ public CreativeFactory(Context context,
Listener listener,
OmAdSessionManager omAdSessionManager,
InterstitialManager interstitialManager)
throws AdException {
throws AdException {
if (context == null) {
throw new AdException(AdException.INTERNAL_ERROR, "Context is null");
}
Expand All @@ -85,25 +86,19 @@ public CreativeFactory(Context context,

public void start() {
try {
AdFormat adUnitIdentifierType = mCreativeModel.getAdConfiguration().getAdUnitIdentifierType();
switch (adUnitIdentifierType) {
case BANNER:
case INTERSTITIAL:
attemptAuidCreative();
break;
case VAST:
attemptVastCreative();
break;
default:
String msg = "Unable to start creativeFactory. adConfig.adUnitIdentifierType doesn't match supported types"
+ "adConfig.adUnitIdentifierType: " + adUnitIdentifierType;
LogUtil.error(TAG, msg);
AdException adException = new AdException(AdException.INTERNAL_ERROR, msg);
mListener.onFailure(adException);
break;
AdUnitConfiguration configuration = mCreativeModel.getAdConfiguration();

if (configuration.isAdType(AdFormat.BANNER) || configuration.isAdType(AdFormat.INTERSTITIAL)) {
attemptAuidCreative();
} else if (configuration.isAdType(AdFormat.VAST)) {
attemptVastCreative();
} else {
String msg = "Unable to start creativeFactory. adConfig.adUnitIdentifierType doesn't match supported types adConfig.adFormat: " + configuration.getAdFormats();
LogUtil.error(TAG, msg);
AdException adException = new AdException(AdException.INTERNAL_ERROR, msg);
mListener.onFailure(adException);
}
}
catch (Exception exception) {
} catch (Exception exception) {
String message = "Creative Factory failed: " + exception.getMessage();
LogUtil.error(TAG, message + Log.getStackTraceString(exception));
AdException adException = new AdException(AdException.INTERNAL_ERROR, message);
Expand Down Expand Up @@ -142,8 +137,7 @@ private void attemptAuidCreative() throws Exception {
rcUrls.add(mCreativeModel.getClickUrl());
mCreativeModel.registerTrackingEvent(TrackingEvent.Events.CLICK, rcUrls);
}
}
else {
} else {
mListener.onFailure(new AdException(AdException.INTERNAL_ERROR, "Tracking info not found"));
}
markWorkStart(BANNER_TIMEOUT);
Expand All @@ -167,17 +161,15 @@ private void attemptVastCreative() {
try {
if (mCreativeModel.getAdConfiguration().isRewarded()) {
newCreative = new RewardedVideoCreative(mContextReference.get(), videoCreativeModel, mOmAdSessionManager, mInterstitialManager);
}
else {
} else {
newCreative = new VideoCreative(mContextReference.get(), videoCreativeModel, mOmAdSessionManager, mInterstitialManager);
}

newCreative.setResolutionListener(new CreativeFactoryCreativeResolutionListener(this));
mCreative = newCreative;
markWorkStart(VAST_TIMEOUT);
newCreative.load();
}
catch (Exception exception) {
} catch (Exception exception) {
LogUtil.error(TAG, "VideoCreative creation failed: " + Log.getStackTraceString(exception));
mListener.onFailure(new AdException(AdException.INTERNAL_ERROR, "VideoCreative creation failed: " + exception.getMessage()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void makeModels(AdUnitConfiguration adConfiguration, BidResponse bidRespo

public void makeVideoModels(AdUnitConfiguration adConfiguration, String vast) {
mAdConfiguration = adConfiguration;
mAdConfiguration.setAdUnitIdentifierType(AdFormat.VAST);
mAdConfiguration.setAdFormat(AdFormat.VAST);
mParserExtractor.extract(vast);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private void makeModelsContinued() {
endCardModel.setWidth(Integer.parseInt(companionAd.getWidth()));
endCardModel.setHeight(Integer.parseInt(companionAd.getHeight()));
endCardModel.setAdConfiguration(new AdUnitConfiguration());
endCardModel.getAdConfiguration().setAdUnitIdentifierType(AdFormat.INTERSTITIAL);
endCardModel.getAdConfiguration().setAdFormat(AdFormat.INTERSTITIAL);
endCardModel.setRequireImpressionUrl(false);
result.creativeModels.add(endCardModel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import org.prebid.mobile.units.configuration.AdFormat;
import org.prebid.mobile.units.configuration.AdUnitConfiguration;

import java.util.ArrayList;

public class HTMLCreative extends AbstractCreative
implements WebViewDelegate, InterstitialManagerDisplayDelegate, Comparable {

Expand Down Expand Up @@ -77,15 +79,18 @@ public void load() throws AdException {
throw new AdException(AdException.INTERNAL_ERROR, "Context is null. Could not load adHtml");
}
CreativeModel model = getCreativeModel();
AdFormat adType = model.getAdConfiguration().getAdUnitIdentifierType();

ArrayList<AdFormat> adFormats = model.getAdConfiguration().getAdFormats();
if (adFormats.isEmpty()) {
throw new AdException(AdException.INTERNAL_ERROR, "Can't create a WebView for a null adtype");
}

AdFormat adType = adFormats.get(0);

if (model.getAdConfiguration().isBuiltInVideo()) {
adType = AdFormat.BANNER;
}

if (adType == null) {
throw new AdException(AdException.INTERNAL_ERROR, "Can't create a WebView for a null adtype");
}
//create a webview here
PrebidWebViewBase prebidWebView = null;
if (adType == AdFormat.BANNER) {
//do all banner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public AdUnitConfiguration getAdConfiguration() {
}

public boolean isAutoDisplayOnLoad() {
return mAdConfiguration.getAdUnitIdentifierType() == AdFormat.BANNER;
return mAdConfiguration.isAdType(AdFormat.BANNER);
}

public void destroy() {
Expand Down Expand Up @@ -388,7 +388,7 @@ private void handleCreativeDisplay() {
return;
}

if (mAdConfiguration.getAdUnitIdentifierType() == AdFormat.BANNER) {
if (mAdConfiguration.isAdType(AdFormat.BANNER)) {
if (!mCurrentCreative.equals(mLastCreativeShown)) {
displayCreative(creativeView);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class AdUnitConfiguration {
private String pbAdSlot;
private String interstitialSize;

private AdFormat adFormat;
private ArrayList<AdFormat> adFormats = new ArrayList<>();
private AdSize minSizePercentage;
private PlacementType placementType;
private AdPosition adPosition;
Expand Down Expand Up @@ -217,20 +217,21 @@ public int getVideoSkipOffset() {
return videoSkipOffset;
}

public void setAdUnitIdentifierType(@Nullable AdFormat adFormat) {
public void setAdFormat(@Nullable AdFormat adFormat) {
if (adFormat == AdFormat.NATIVE) {
nativeConfiguration = new NativeAdUnitConfiguration();
}
this.adFormat = adFormat;
adFormats.clear();
adFormats.add(adFormat);
}

@Nullable
public AdFormat getAdUnitIdentifierType() {
return adFormat;
@NonNull
public ArrayList<AdFormat> getAdFormats() {
return adFormats;
}

public boolean isAdType(AdFormat type) {
return adFormat == type;
return adFormats.contains(type);
}

public void setRewarded(boolean rewarded) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.robolectric.annotation.Config;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static junit.framework.Assert.assertEquals;
Expand Down Expand Up @@ -72,7 +73,7 @@ public void testBannerAdUnitCreation() {

assertEquals(1, configuration.getSizes().size());
assertEquals(testConfigId, configuration.getConfigId());
assertEquals(AdFormat.BANNER, configuration.getAdUnitIdentifierType());
assertEquals(Collections.singletonList(AdFormat.BANNER), configuration.getAdFormats());

assertEquals(0, adUnit.configuration.getAutoRefreshDelay());
adUnit.setAutoRefreshInterval(30);
Expand Down Expand Up @@ -112,7 +113,7 @@ public void testInterstitialAdUnitCreation() throws Exception {
InterstitialAdUnit adUnit = new InterstitialAdUnit(testConfigId);
AdUnitConfiguration configuration = adUnit.getConfiguration();
assertEquals(testConfigId, configuration.getConfigId());
assertEquals(AdFormat.INTERSTITIAL, configuration.getAdUnitIdentifierType());
assertEquals(Collections.singletonList(AdFormat.INTERSTITIAL), configuration.getAdFormats());
}


Expand All @@ -121,7 +122,7 @@ public void testAdvancedInterstitialAdUnitCreation() {
InterstitialAdUnit adUnit = new InterstitialAdUnit(testConfigId, height, 70);
AdUnitConfiguration configuration = (AdUnitConfiguration) adUnit.getConfiguration();

assertEquals(AdFormat.INTERSTITIAL, configuration.getAdUnitIdentifierType());
assertEquals(Collections.singletonList(AdFormat.INTERSTITIAL), configuration.getAdFormats());
assertEquals(testConfigId, configuration.getConfigId());
assertEquals(height, configuration.getMinSizePercentage().getWidth());
assertEquals(70, configuration.getMinSizePercentage().getHeight());
Expand All @@ -136,7 +137,7 @@ public void testVideoAdUnitCreation() {
assertEquals(width, size.getWidth());
assertEquals(height, size.getHeight());
assertEquals(testConfigId, configuration.getConfigId());
assertEquals(AdFormat.VAST, configuration.getAdUnitIdentifierType());
assertEquals(Collections.singletonList(AdFormat.VAST), configuration.getAdFormats());
}

@Test
Expand All @@ -145,15 +146,15 @@ public void testVideoInterstitialAdUnitCreation() {
AdUnitConfiguration configuration = (AdUnitConfiguration) adUnit.getConfiguration();

assertEquals(testConfigId, configuration.getConfigId());
assertEquals(AdFormat.VAST, configuration.getAdUnitIdentifierType());
assertEquals(Collections.singletonList(AdFormat.VAST), configuration.getAdFormats());
}

@Test
public void testRewardedVideoAdUnitCreation() {
RewardedVideoAdUnit adUnit = new RewardedVideoAdUnit(testConfigId);
AdUnitConfiguration configuration = adUnit.getConfiguration();
assertEquals(testConfigId, configuration.getConfigId());
assertEquals(AdFormat.VAST, configuration.getAdUnitIdentifierType());
assertEquals(Collections.singletonList(AdFormat.VAST), configuration.getAdFormats());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void putUsersDataListToRequestParams_addElementsToOutsideList_elementsAdd

AdUnitConfiguration configuration = new AdUnitConfiguration();
configuration.setConfigId("configId");
configuration.setAdUnitIdentifierType(AdFormat.BANNER);
configuration.setAdFormat(AdFormat.BANNER);
for (DataObject data : userDataObjects) {
configuration.addUserData(data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.robolectric.annotation.Config;

import java.util.ArrayList;
import java.util.Collections;

import static junit.framework.Assert.*;

Expand All @@ -32,7 +33,7 @@ public void testNativeAdUnitCreation() throws Exception {

assertNotNull(nativeUnit);
assertEquals(PBS_CONFIG_ID_NATIVE_APPNEXUS, configuration.getConfigId());
assertEquals(AdFormat.NATIVE, configuration.getAdUnitIdentifierType());
assertEquals(Collections.singletonList(AdFormat.NATIVE), configuration.getAdFormats());
assertNotNull(configuration.getNativeConfiguration());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void setup() throws Exception {
mContext = Robolectric.buildActivity(Activity.class).create().get();

mAdUnitConfiguration = new AdUnitConfiguration();
mAdUnitConfiguration.setAdUnitIdentifierType(AdFormat.BANNER);
mAdUnitConfiguration.setAdFormat(AdFormat.BANNER);

BidResponse mockResponse = mock(BidResponse.class);
Bid mockBid = mock(Bid.class);
Expand Down
Loading

0 comments on commit 5a1ebc8

Please sign in to comment.