Skip to content

Commit

Permalink
Unification of configuration and targeting (#374)
Browse files Browse the repository at this point in the history
* feat(unification): create ad unit configuration interfaces and classes #370

* feat(unification): replace internal attributes in ad units to AdUnitConfiguration #370

* feat(unification): replace internal references to interface #370

* feat(unification): sort targeting params by categories #370

Sort variables by categories: user, context, metadata, consents data.

* feat(unification): move targeting params from rendering #370

* feat(unification): use original targeting in rendering #370

* feat(unification): fix test cases for configuration and targeting #370

* feat(unification): fix native tests #370

* feat(unification): remove duplicate methods for external ids #370
  • Loading branch information
ValentinPostindustria authored Mar 10, 2022
1 parent cd2400f commit 8d4d03d
Show file tree
Hide file tree
Showing 42 changed files with 2,019 additions and 1,912 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.json.JSONArray
import org.prebid.mobile.rendering.networking.targeting.Targeting
import org.prebid.mobile.TargetingParams
import org.prebid.mobile.rendering.sdk.deviceData.listeners.SdkInitListener
import org.prebid.mobile.renderingtestapp.plugplay.utilities.consent.ConsentUpdateManager
import org.prebid.mobile.renderingtestapp.utils.OpenRtbConfigs
Expand Down Expand Up @@ -223,7 +223,7 @@ class MainActivity : AppCompatActivity(), SdkInitListener {
private fun extractEidsExtras() {
val eidsJsonString = intent.extras?.getString(EXTRA_EIDS)
val eidsJsonArray = JSONArray(eidsJsonString)
Targeting.setEids(eidsJsonArray)
TargetingParams.setExtendedUserIds(eidsJsonArray)
}

private fun handleConsentExtra() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
package org.prebid.mobile.renderingtestapp.utils

import com.google.gson.Gson
import org.prebid.mobile.rendering.models.openrtb.bidRequests.Ext
import org.prebid.mobile.rendering.networking.parameters.UserParameters
import org.prebid.mobile.rendering.networking.targeting.Targeting
import org.prebid.mobile.ExtObject
import org.prebid.mobile.TargetingParams
import java.lang.reflect.InvocationTargetException

object OpenRtbConfigs {
Expand All @@ -28,51 +27,53 @@ object OpenRtbConfigs {

fun setTargeting(openRtbExtra: OpenRtbExtra) {
if (openRtbExtra.age != 0) {
Targeting.setUserAge(openRtbExtra.age)
TargetingParams.setUserAge(openRtbExtra.age)
}
if (openRtbExtra.appStoreUrl != null) {
Targeting.setAppStoreMarketUrl(openRtbExtra.appStoreUrl)
TargetingParams.setStoreUrl(openRtbExtra.appStoreUrl)
}
if (openRtbExtra.userId != null) {
Targeting.setUserId(openRtbExtra.userId)
TargetingParams.setUserId(openRtbExtra.userId)
}
if (openRtbExtra.gender != null) {
Targeting.setUserGender(UserParameters.Gender.valueOf(openRtbExtra.gender))
TargetingParams.setGender(TargetingParams.GENDER.genderByKey(openRtbExtra.gender))
}
if (openRtbExtra.buyerId != null) {
Targeting.setBuyerUid(openRtbExtra.buyerId)
TargetingParams.setBuyerId(openRtbExtra.buyerId)
}
if (openRtbExtra.customData != null) {
Targeting.setUserCustomData(openRtbExtra.customData)
TargetingParams.setUserCustomData(openRtbExtra.customData)
}
if (openRtbExtra.keywords != null) {
Targeting.setUserKeywords(openRtbExtra.keywords)
openRtbExtra.keywords.split(",").forEach {
TargetingParams.addUserKeyword(it)
}
}
if (openRtbExtra.geo != null) {
Targeting.setUserLatLng(openRtbExtra.geo.lat, openRtbExtra.geo.lon)
TargetingParams.setUserLatLng(openRtbExtra.geo.lat, openRtbExtra.geo.lon)
}
if (openRtbExtra.publisherName != null) {
Targeting.setPublisherName(openRtbExtra.publisherName)
TargetingParams.setPublisherName(openRtbExtra.publisherName)
}
if (openRtbExtra.userExt?.isNotEmpty() == true) {
val ext = Ext()
val ext = ExtObject()
val gson = Gson()
for (key in openRtbExtra.userExt.keys) {
ext.put(key, gson.toJson(openRtbExtra.userExt[key]))
}
Targeting.setUserExt(ext)
TargetingParams.setUserExt(ext)
}
if (openRtbExtra.accessControl?.isNotEmpty() == true) {
for (bidder in openRtbExtra.accessControl) {
Targeting.addBidderToAccessControlList(bidder)
TargetingParams.addBidderToAccessControlList(bidder)
}
}
if (openRtbExtra.userData?.isNotEmpty() == true) {
for (key in openRtbExtra.userData.keys) {
val dataList = openRtbExtra.userData[key]
if (dataList != null) {
for (data in dataList) {
Targeting.addUserData(key, data)
TargetingParams.addUserData(key, data)
}
}
}
Expand All @@ -82,7 +83,7 @@ object OpenRtbConfigs {
val dataList = openRtbExtra.appContextData[key]
if (dataList != null) {
for (data in dataList) {
Targeting.addContextData(key, data)
TargetingParams.addContextData(key, data)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.prebid.mobile;

enum AdType {
public enum AdType {
BANNER,
INTERSTITIAL,
NATIVE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,25 @@
import android.text.TextUtils;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import org.prebid.mobile.tasksmanager.TasksManager;
import org.prebid.mobile.unification.AdUnitConfiguration;
import org.prebid.mobile.unification.BaseAdUnitConfiguration;
import org.prebid.mobile.unification.BaseAdUnitConfigurationInterface;

import java.util.*;

public abstract class AdUnit {
private static final int MIN_AUTO_REFRESH_PERIOD_MILLIS = 30_000;

private String configId;
private AdType adType;
private static final int MIN_AUTO_REFRESH_PERIOD_MILLIS = 30_000;

private int periodMillis = 0; // No auto refresh
private DemandFetcher fetcher;
private int periodMillis;

private final Map<String, Set<String>> contextDataDictionary;
private final Set<String> contextKeywordsSet;
private ContentObject content;
private final ArrayList<DataObject> userDataObjects = new ArrayList<>();

private String pbAdSlot;
protected BaseAdUnitConfigurationInterface configuration = createConfiguration();

AdUnit(@NonNull String configId, @NonNull AdType adType) {
this.configId = configId;
this.adType = adType;
this.periodMillis = 0; // by default no auto refresh

this.contextDataDictionary = new HashMap<>();
this.contextKeywordsSet = new HashSet<>();

configuration.setConfigId(configId);
configuration.setAdType(adType);
}

public void setAutoRefreshPeriodMillis(@IntRange(from = MIN_AUTO_REFRESH_PERIOD_MILLIS) int periodMillis) {
Expand Down Expand Up @@ -101,7 +92,7 @@ public void fetchDemand(@NonNull Object adObj, @NonNull OnCompleteListener liste
listener.onComplete(ResultCode.INVALID_ACCOUNT_ID);
return;
}
if (TextUtils.isEmpty(configId)) {
if (TextUtils.isEmpty(configuration.getConfigId())) {
LogUtil.e("Empty config id.");
listener.onComplete(ResultCode.INVALID_CONFIG_ID);
return;
Expand All @@ -114,34 +105,14 @@ public void fetchDemand(@NonNull Object adObj, @NonNull OnCompleteListener liste
}
}

HashSet<AdSize> sizes = null;
if (adType == AdType.BANNER) {
sizes = ((BannerAdUnit) this).getSizes();
for (AdSize size : sizes) {
if (size.getWidth() < 0 || size.getHeight() < 0) {
listener.onComplete(ResultCode.INVALID_SIZE);
return;
}
}
} else if (adType == AdType.VIDEO) {

VideoAdUnit videoAdUnit = (VideoAdUnit) this;

sizes = new HashSet<>(1);
sizes.add(videoAdUnit.getAdSize());
if (configuration.getAdType() == AdType.BANNER || configuration.getAdType() == AdType.VIDEO) {
HashSet<AdSize> sizes = configuration.castToOriginal().getSizes();
for (AdSize size : sizes) {
if (size.getWidth() < 0 || size.getHeight() < 0) {
listener.onComplete(ResultCode.INVALID_SIZE);
return;
}
}

}
AdSize minSizePerc = null;
if (this instanceof InterstitialAdUnit) {
InterstitialAdUnit interstitialAdUnit = (InterstitialAdUnit) this;

minSizePerc = interstitialAdUnit.getMinSizePerc();
}

Context context = PrebidMobile.getApplicationContext();
Expand All @@ -160,26 +131,10 @@ public void fetchDemand(@NonNull Object adObj, @NonNull OnCompleteListener liste
return;
}

BannerBaseAdUnit.Parameters bannerParameters = null;
if (this instanceof BannerBaseAdUnit) {
BannerBaseAdUnit bannerBaseAdUnit = (BannerBaseAdUnit) this;
bannerParameters = bannerBaseAdUnit.parameters;
}

VideoBaseAdUnit.Parameters videoParameters = null;
if (this instanceof VideoBaseAdUnit) {
VideoBaseAdUnit videoBaseAdUnit = (VideoBaseAdUnit) this;
videoParameters = videoBaseAdUnit.parameters;
}

if (Util.supportedAdObject(adObj)) {
fetcher = new DemandFetcher(adObj);
RequestParams requestParams = new RequestParams(configId, adType, sizes, contextDataDictionary, contextKeywordsSet, minSizePerc, pbAdSlot, bannerParameters, videoParameters, content, userDataObjects);
if (this.adType.equals(AdType.NATIVE)) {
requestParams.setNativeRequestParams(((NativeAdUnit) this).params);
}
fetcher.setPeriodMillis(periodMillis);
fetcher.setRequestParams(requestParams);
fetcher.setConfiguration(configuration);
fetcher.setListener(listener);
if (periodMillis >= 30000) {
LogUtil.v("Start fetching bids with auto refresh millis: " + periodMillis);
Expand All @@ -200,33 +155,33 @@ public void fetchDemand(@NonNull Object adObj, @NonNull OnCompleteListener liste
* if the key already exists the value will be appended to the list. No duplicates will be added
*/
public void addContextData(String key, String value) {
Util.addValue(contextDataDictionary, key, value);
configuration.addContextData(key, value);
}

/**
* This method obtains the context data keyword & values for adunit context targeting
* the values if the key already exist will be replaced with the new set of values
*/
public void updateContextData(String key, Set<String> value) {
contextDataDictionary.put(key, value);
configuration.addContextData(key, value);
}

/**
* This method allows to remove specific context data keyword & values set from adunit context targeting
*/
public void removeContextData(String key) {
contextDataDictionary.remove(key);
configuration.removeContextData(key);
}

/**
* This method allows to remove all context data set from adunit context targeting
*/
public void clearContextData() {
contextDataDictionary.clear();
configuration.clearContextData();
}

Map<String, Set<String>> getContextDataDictionary() {
return contextDataDictionary;
return configuration.getContextDataDictionary();
}

// MARK: - adunit context keywords (imp[].ext.context.keywords)
Expand All @@ -236,64 +191,74 @@ Map<String, Set<String>> getContextDataDictionary() {
* Inserts the given element in the set if it is not already present.
*/
public void addContextKeyword(String keyword) {
contextKeywordsSet.add(keyword);
configuration.addContextKeyword(keyword);
}

/**
* This method obtains the context keyword set for adunit context targeting
* Adds the elements of the given set to the set.
*/
public void addContextKeywords(Set<String> keywords) {
contextKeywordsSet.addAll(keywords);
configuration.addContextKeywords(keywords);
}

/**
* This method allows to remove specific context keyword from adunit context targeting
*/
public void removeContextKeyword(String keyword) {
configuration.removeContextKeyword(keyword);
}

/**
* This method allows to remove all keywords from the set of adunit context targeting
*/
public void clearContextKeywords() {
configuration.clearContextKeywords();
}

Set<String> getContextKeywordsSet() {
return configuration.getContextKeywordsSet();
}

/**
* This method obtains the content for adunit, content, in which impression will appear
*/
public void setAppContent(ContentObject content) {
this.content = content;
configuration.setAppContent(content);
}

public ContentObject getAppContent() {
return content;
return configuration.getAppContent();
}

public void addUserData(DataObject dataObject) {
userDataObjects.add(dataObject);
configuration.addUserData(dataObject);
}

public ArrayList<DataObject> getUserData() {
return userDataObjects;
return configuration.getUserData();
}

public void clearUserData() {
userDataObjects.clear();
configuration.clearUserData();
}

/**
* This method allows to remove specific context keyword from adunit context targeting
*/
public void removeContextKeyword(String keyword) {
contextKeywordsSet.remove(keyword);
public String getPbAdSlot() {
return configuration.getPbAdSlot();
}

/**
* This method allows to remove all keywords from the set of adunit context targeting
*/
public void clearContextKeywords() {
contextKeywordsSet.clear();
public void setPbAdSlot(String pbAdSlot) {
configuration.setPbAdSlot(pbAdSlot);
}

Set<String> getContextKeywordsSet() {
return contextKeywordsSet;
protected BaseAdUnitConfiguration createConfiguration() {
return new AdUnitConfiguration();
}

public String getPbAdSlot() {
return pbAdSlot;
@VisibleForTesting
public BaseAdUnitConfiguration getConfiguration() {
return (BaseAdUnitConfiguration) configuration;
}

public void setPbAdSlot(String pbAdSlot) {
this.pbAdSlot = pbAdSlot;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,17 @@

public class BannerAdUnit extends BannerBaseAdUnit {

private HashSet<AdSize> sizes;

public BannerAdUnit(@NonNull String configId, int width, int height) {
super(configId, AdType.BANNER);
this.sizes = new HashSet<>();
this.sizes.add(new AdSize(width, height));
configuration.castToOriginal().addSize(new AdSize(width, height));
}

public void addAdditionalSize(int width, int height) {
sizes.add(new AdSize(width, height));
configuration.castToOriginal().addSize(new AdSize(width, height));
}

HashSet<AdSize> getSizes() {
return this.sizes;
return configuration.castToOriginal().getSizes();
}

}
Loading

0 comments on commit 8d4d03d

Please sign in to comment.