Skip to content

Commit

Permalink
Merge pull request #127 from yoalex5/feature/interstitial_advanced
Browse files Browse the repository at this point in the history
Advanced interstitial support
  • Loading branch information
yoalex5 authored Aug 1, 2019
2 parents 7ca0b4c + c4acbc7 commit e5094e9
Show file tree
Hide file tree
Showing 9 changed files with 405 additions and 1 deletion.
10 changes: 9 additions & 1 deletion PrebidMobile/API1.0/src/main/java/org/prebid/mobile/AdUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ public void fetchDemand(@NonNull Object adObj, @NonNull OnCompleteListener liste
}
}
}
AdSize minSizePerc = null;
if (this instanceof InterstitialAdUnit) {
InterstitialAdUnit interstitialAdUnit = (InterstitialAdUnit) this;

minSizePerc = interstitialAdUnit.getMinSizePerc();
}

Context context = PrebidMobile.getApplicationContext();
if (context != null) {
ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Expand All @@ -107,7 +114,8 @@ 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, keywords, minSizePerc);
fetcher.setPeriodMillis(periodMillis);
fetcher.setRequestParams(requestParams);
fetcher.setListener(listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,25 @@
package org.prebid.mobile;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

public class InterstitialAdUnit extends AdUnit {

@Nullable
private AdSize minSizePerc = null;

public InterstitialAdUnit(@NonNull String configId) {
super(configId, AdType.INTERSTITIAL);
}

public InterstitialAdUnit(@NonNull String configId, int minWidthPerc, int minHeightPerc) {

this(configId);
minSizePerc = new AdSize(minWidthPerc, minHeightPerc);
}

@Nullable
AdSize getMinSizePerc() {
return minSizePerc;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,33 @@ private JSONObject getDeviceObject() {
if (!TextUtils.isEmpty(Locale.getDefault().getLanguage())) {
device.put(PrebidServerSettings.REQUEST_LANGUAGE, Locale.getDefault().getLanguage());
}

if (requestParams.getAdType().equals(AdType.INTERSTITIAL)) {

Integer minSizePercWidth = null;
Integer minSizePercHeight = null;

AdSize minSizePerc = requestParams.getMinSizePerc();
if (minSizePerc != null) {

minSizePercWidth = minSizePerc.getWidth();
minSizePercHeight = minSizePerc.getHeight();
}

JSONObject deviceExt = new JSONObject();
JSONObject deviceExtPrebid = new JSONObject();
JSONObject deviceExtPrebidInstl = new JSONObject();

device.put("ext", deviceExt);
deviceExt.put("prebid", deviceExtPrebid);
deviceExtPrebid.put("interstitial", deviceExtPrebidInstl);
deviceExtPrebidInstl.put("minwidthperc", minSizePercWidth);
deviceExtPrebidInstl.put("minheightperc", minSizePercHeight);

JSONObject deviceExtWithoutEmptyValues = Util.getObjectWithoutEmptyValues(deviceExt);
device.put("ext", deviceExtWithoutEmptyValues);
}

// POST data that requires context
Context context = PrebidMobile.getApplicationContext();
if (context != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@

package org.prebid.mobile;

import android.support.annotation.Nullable;

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

class RequestParams {

private String configId = "";
private AdType adType = AdType.BANNER;
private HashSet<AdSize> sizes = new HashSet<>();
private ArrayList<String> keywords;
@Nullable
private AdSize minSizePerc; //non null only for InterstitialAdUnit(String, int, int)

RequestParams(String configId, AdType adType, HashSet<AdSize> sizes, ArrayList<String> keywords) {
this.configId = configId;
Expand All @@ -32,6 +37,11 @@ class RequestParams {
this.keywords = keywords;
}

RequestParams(String configId, AdType adType, HashSet<AdSize> sizes, ArrayList<String> keywords, @Nullable AdSize minSizePerc) {
this(configId, adType, sizes, keywords);
this.minSizePerc = minSizePerc;
}

String getConfigId() {
return this.configId;
}
Expand All @@ -48,4 +58,8 @@ ArrayList<String> getKeywords() {
return keywords;
}

@Nullable
AdSize getMinSizePerc() {
return minSizePerc;
}
}
103 changes: 103 additions & 0 deletions PrebidMobile/API1.0/src/main/java/org/prebid/mobile/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.prebid.mobile;

import android.os.Bundle;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
Expand All @@ -25,12 +26,17 @@
import org.prebid.mobile.addendum.AdViewUtils;
import org.prebid.mobile.addendum.PbFindSizeError;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;

Expand Down Expand Up @@ -73,6 +79,103 @@ public void failure(@NonNull PbFindSizeError error) {

}

@Nullable
static JSONObject getObjectWithoutEmptyValues(@NonNull JSONObject jsonObject) {

JSONObject result = null;
try {
JSONObject clone = new JSONObject(jsonObject.toString());
removeEntryWithoutValue(clone);

if (clone.length() > 0) {
result = clone;
}

} catch (JSONException e) {
LogUtil.e("message:" + e.getMessage());
}

return result;
}

private static void removeEntryWithoutValue(@NonNull JSONObject map) throws JSONException {
Iterator<String> iterator = map.keys();

while (iterator.hasNext()) {
String key = iterator.next();

Object value = map.opt(key);
if (value != null) {

if (value instanceof JSONObject) {

JSONObject mapValue = (JSONObject)value;
removeEntryWithoutValue(mapValue);

if (mapValue.length() == 0) {
iterator.remove();
}
} else if (value instanceof JSONArray) {

JSONArray arrayValue = (JSONArray)value;
arrayValue = removeEntryWithoutValue(arrayValue);

map.put(key, arrayValue);

if (arrayValue.length() == 0) {
iterator.remove();
}
}
}
}
}

@CheckResult
private static JSONArray removeEntryWithoutValue(@NonNull JSONArray array) throws JSONException {

for (int i = 0; i < array.length(); i++) {

Object value = array.opt(i);
if (value != null) {

if (value instanceof JSONObject) {

JSONObject mapValue = (JSONObject)value;
removeEntryWithoutValue(mapValue);

if (mapValue.length() == 0) {
array = getJsonArrayWithoutEntryByIndex(array, i);
}
} else if (value instanceof JSONArray) {
JSONArray arrayValue = (JSONArray)value;
arrayValue = removeEntryWithoutValue(arrayValue);

array.put(i, arrayValue);

if (arrayValue.length() == 0) {
array = getJsonArrayWithoutEntryByIndex(array, i);
}
}
}

}

return array;
}

@CheckResult
private static JSONArray getJsonArrayWithoutEntryByIndex(JSONArray jsonArray, int pos) throws JSONException {
JSONArray result = new JSONArray();

for (int i = 0; i < jsonArray.length(); i++) {
if (i != pos) {
result.put(jsonArray.get(i));
}
}

return result;
}

static Class getClassFromString(String className) {
try {
return Class.forName(className);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.ArrayList;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = BaseSetup.testSDK)
Expand All @@ -37,6 +39,16 @@ public void testInterstitialAdUnitCreation() throws Exception {
assertEquals(AdType.INTERSTITIAL, FieldUtils.readField(adUnit, "adType", true));
}


@Test
public void testAdvancedInterstitialAdUnitCreation() throws Exception {
InterstitialAdUnit adUnit = new InterstitialAdUnit("12345", 50, 70);
assertEquals(AdType.INTERSTITIAL, FieldUtils.readField(adUnit, "adType", true));

assertNotNull(adUnit.getMinSizePerc());
assertTrue(adUnit.getMinSizePerc().getWidth() == 50 && adUnit.getMinSizePerc().getHeight() == 70);
}

@Test
public void testSetUserKeyword() throws Exception {
InterstitialAdUnit adUnit = new InterstitialAdUnit("12345");
Expand Down Expand Up @@ -75,4 +87,5 @@ public void testSetUserKeywords() throws Exception {
assertEquals("key1=value1", keywords.get(0));
assertEquals("key1=value2", keywords.get(1));
}

}
Loading

0 comments on commit e5094e9

Please sign in to comment.