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

setInv function #95

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
46 changes: 1 addition & 45 deletions PrebidMobile/API1.0/src/main/java/org/prebid/mobile/AdUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,20 @@
import android.support.annotation.NonNull;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashSet;

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

private String configId;
private AdType adType;
private ArrayList<String> keywords;
private DemandFetcher fetcher;
private int periodMillis;

AdUnit(@NonNull String configId, @NonNull AdType adType) {
this.configId = configId;
this.adType = adType;
this.periodMillis = 0; // by default no auto refresh
this.keywords = new ArrayList<>();
}

public void setAutoRefreshPeriodMillis(@IntRange(from = MIN_AUTO_REFRESH_PERIOD_MILLIS) int periodMillis) {
Expand Down Expand Up @@ -107,7 +104,7 @@ public void fetchDemand(@NonNull Object adObj, @NonNull OnCompleteListener liste
}
if (Util.supportedAdObject(adObj)) {
fetcher = new DemandFetcher(adObj);
RequestParams requestParams = new RequestParams(configId, adType, sizes, keywords);
RequestParams requestParams = new RequestParams(configId, adType, sizes);
fetcher.setPeriodMillis(periodMillis);
fetcher.setRequestParams(requestParams);
fetcher.setListener(listener);
Expand All @@ -123,46 +120,5 @@ public void fetchDemand(@NonNull Object adObj, @NonNull OnCompleteListener liste

}


public void addUserKeyword(String key, String value) {
if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
keywords.add(key + "=" + value);
} else if (!TextUtils.isEmpty(key)) {
keywords.add(key);
}
}

public void addUserKeywords(String key, String[] values) {
if (!TextUtils.isEmpty(key) && values.length > 0) {
keywords.clear();
for (String value : values) {
keywords.add(key + "=" + value);
}
} else if (!TextUtils.isEmpty(key)) {
keywords.clear();
keywords.add(key);
}
}

public void removeUserKeyword(String key) {
ArrayList<String> toBeRemoved = new ArrayList<>();
for (String keyword : keywords) {
if (keyword.equals(key)) {
toBeRemoved.add(keyword);
} else {
String[] keyValuePair = keyword.split("=");
if (keyValuePair[0].equals(key)) {
toBeRemoved.add(keyword);
}
}
}
keywords.removeAll(toBeRemoved);
}

public void clearUserKeywords() {
keywords.clear();
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class PrebidMobile {

public static final int TIMEOUT_MILLIS = 2_000;

static int timeoutMillis = TIMEOUT_MILLIS; // by default use 10000 milliseconds as timeout
static int timeoutMillis = TIMEOUT_MILLIS; // by default use 20000 milliseconds as timeout
static boolean timeoutMillisUpdated = false;

private PrebidMobile() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,15 @@ private JSONObject getAppObject() {
if (!TextUtils.isEmpty(TargetingParams.getStoreUrl())) {
app.put("storeurl", TargetingParams.getStoreUrl());
}
StringBuilder builderInv = new StringBuilder();
ArrayList<String> invKeywords = TargetingParams.getInvKeywords();
for (String key : invKeywords) {
builderInv.append(key).append(",");
}
String finalInvKeywords = builderInv.toString();
if (!TextUtils.isEmpty(finalInvKeywords)) {
app.put("keywords", finalInvKeywords);
}
JSONObject publisher = new JSONObject();
publisher.put("id", PrebidMobile.getPrebidServerAccountId());
app.put("publisher", publisher);
Expand Down Expand Up @@ -721,14 +730,14 @@ private JSONObject getUserObject() {
break;
}
user.put("gender", g);
StringBuilder builder = new StringBuilder();
ArrayList<String> keywords = this.requestParams.getKeywords();
for (String key : keywords) {
builder.append(key).append(",");
StringBuilder builderUser = new StringBuilder();
ArrayList<String> userKeywords = TargetingParams.getUserKeywords();
for (String key : userKeywords) {
builderUser.append(key).append(",");
}
String finalKeywords = builder.toString();
if (!TextUtils.isEmpty(finalKeywords)) {
user.put("keywords", finalKeywords);
String finalUserKeywords = builderUser.toString();
if (!TextUtils.isEmpty(finalUserKeywords)) {
user.put("keywords", finalUserKeywords);
}
if (TargetingParams.isSubjectToGDPR() != null) {
JSONObject ext = new JSONObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ class RequestParams {
private String configId = "";
private AdType adType = AdType.BANNER;
private HashSet<AdSize> sizes = new HashSet<>();
private ArrayList<String> keywords;

RequestParams(String configId, AdType adType, HashSet<AdSize> sizes, ArrayList<String> keywords) {
RequestParams(String configId, AdType adType, HashSet<AdSize> sizes) {
this.configId = configId;
this.adType = adType;
this.sizes = sizes; // for Interstitial this will be null, will use screen width & height in the request
this.keywords = keywords;
}

String getConfigId() {
Expand All @@ -44,8 +42,4 @@ HashSet<AdSize> getAdSizes() {
return this.sizes;
}

ArrayList<String> getKeywords() {
return keywords;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.preference.PreferenceManager;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Calendar;


Expand All @@ -40,6 +41,8 @@ public class TargetingParams {
private static final String IABConsent_ConsentString = "IABConsent_ConsentString";
private static final String PREBID_GDPR_KEY = "Prebid_GDPR";
private static final String IABConsent_SubjectToGDPR = "IABConsent_SubjectToGDPR";
private static ArrayList<String> userKeywords = new ArrayList<String>();
private static ArrayList<String> invKeywords = new ArrayList<String>();
//endregion

//region Private Constructor
Expand Down Expand Up @@ -206,6 +209,102 @@ public static synchronized String getStoreUrl() {
return storeUrl;
}

/**
* Get the user keyword targeting of your app
*
* @return userKeywords
*/
public static synchronized ArrayList<String> getUserKeywords() {
return userKeywords;
}

/**
* Get the inventory keyword targeting of your app
*
* @return invKeywords
*/
public static synchronized ArrayList<String> getInvKeywords() {
return invKeywords;
}

public static void addUserKeyword(String key, String value) {
if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
TargetingParams.userKeywords.add(key + "=" + value);
} else if (!TextUtils.isEmpty(key)) {
TargetingParams.userKeywords.add(key);
}
}

public static void addInvKeyword(String key, String value) {
if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
TargetingParams.invKeywords.add(key + "=" + value);
} else if (!TextUtils.isEmpty(key)) {
TargetingParams.invKeywords.add(key);
}
}

public static void addUserKeywords(String key, String[] values) {
if (!TextUtils.isEmpty(key) && values.length > 0) {
TargetingParams.userKeywords.clear();
for (String value : values) {
TargetingParams.userKeywords.add(key + "=" + value);
}
} else if (!TextUtils.isEmpty(key)) {
TargetingParams.userKeywords.clear();
TargetingParams.userKeywords.add(key);
}
}

public static void addInvKeywords(String key, String[] values) {
if (!TextUtils.isEmpty(key) && values.length > 0) {
TargetingParams.invKeywords.clear();
for (String value : values) {
TargetingParams.invKeywords.add(key + "=" + value);
}
} else if (!TextUtils.isEmpty(key)) {
TargetingParams.invKeywords.clear();
TargetingParams.invKeywords.add(key);
}
}

public static void removeUserKeyword(String key) {
ArrayList<String> toBeRemoved = new ArrayList<>();
for (String keyword : TargetingParams.userKeywords) {
if (keyword.equals(key)) {
toBeRemoved.add(keyword);
} else {
String[] keyValuePair = keyword.split("=");
if (keyValuePair[0].equals(key)) {
toBeRemoved.add(keyword);
}
}
}
TargetingParams.userKeywords.removeAll(toBeRemoved);
}

public static void removeInvKeyword(String key) {
ArrayList<String> toBeRemoved = new ArrayList<>();
for (String keyword : TargetingParams.invKeywords) {
if (keyword.equals(key)) {
toBeRemoved.add(keyword);
} else {
String[] keyValuePair = keyword.split("=");
if (keyValuePair[0].equals(key)) {
toBeRemoved.add(keyword);
}
}
}
TargetingParams.invKeywords.removeAll(toBeRemoved);
}

public static void clearUserKeywords() {
TargetingParams.userKeywords.clear();
}

public static void clearInvKeywords() {
TargetingParams.invKeywords.clear();
}


//endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.ArrayList;

import static junit.framework.Assert.assertEquals;

@RunWith(RobolectricTestRunner.class)
Expand All @@ -48,42 +46,4 @@ public void testBannerAdUnitAddSize() throws Exception {
assertEquals(2, adUnit.getSizes().size());
}

@Test
public void testSetUserKeyword() throws Exception {
BannerAdUnit adUnit = new BannerAdUnit("123456", 320, 50);
adUnit.addUserKeyword("key", "value");
adUnit.addUserKeyword("key1", null);
@SuppressWarnings("unchecked")
ArrayList<String> keywords = (ArrayList<String>) FieldUtils.readField(adUnit, "keywords", true);
assertEquals(2, keywords.size());
assertEquals("key=value", keywords.get(0));
assertEquals("key1", keywords.get(1));
adUnit.addUserKeyword("key", "value2");
assertEquals(3, keywords.size());
assertEquals("key=value", keywords.get(0));
assertEquals("key1", keywords.get(1));
assertEquals("key=value2", keywords.get(2));
adUnit.removeUserKeyword("key");
assertEquals(1, keywords.size());
assertEquals("key1", keywords.get(0));
adUnit.clearUserKeywords();
assertEquals(0, keywords.size());
}

@Test
public void testSetUserKeywords() throws Exception {
BannerAdUnit adUnit = new BannerAdUnit("123456", 320, 50);
adUnit.addUserKeyword("key1", "value1");
String[] values = {"value1", "value2"};
adUnit.addUserKeywords("key2", values);
@SuppressWarnings("unchecked")
ArrayList<String> keywords = (ArrayList<String>) FieldUtils.readField(adUnit, "keywords", true);
assertEquals(2, keywords.size());
assertEquals("key2=value1", keywords.get(0));
assertEquals("key2=value2", keywords.get(1));
adUnit.addUserKeywords("key1", values);
assertEquals(2, keywords.size());
assertEquals("key1=value1", keywords.get(0));
assertEquals("key1=value2", keywords.get(1));
}
}
Loading