Skip to content

Commit

Permalink
feat(unification): configure auto refresh #370
Browse files Browse the repository at this point in the history
Add pbs debug field to Bid request.
  • Loading branch information
ValentinPostindustria committed Mar 23, 2022
1 parent 0476ced commit 81f402d
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.prebid.mobile.prebidkotlindemo

import com.mopub.mobileads.MoPubView
import org.prebid.mobile.Host
import org.prebid.mobile.PrebidMobile
import org.prebid.mobile.prebidkotlindemo.ads.GamBanner
import org.prebid.mobile.prebidkotlindemo.ads.GamInterstitial
import org.prebid.mobile.prebidkotlindemo.ads.MoPubBanner
Expand All @@ -24,19 +26,22 @@ object AdTypesRepository {
AdType(
"Banner 320x50",
onCreate = { _, wrapper, autoRefreshTime ->
useTestServer()
PrebidMobile.setStoredAuctionResponse("response-prebid-banner-320-50")
GamBanner.create(
wrapper, autoRefreshTime,
320, 50,
// TODO: Problem with ids
"/5300653/pavliuchyk_test_adunit_1x1_puc",
"625c6125-f19e-4d5b-95c5-55501526b2a4"
"imp-prebid-banner-320-50"
)
},
onDestroy = { GamBanner.destroy() }
),
AdType(
"Banner 300x250",
onCreate = { _, wrapper, autoRefreshTime ->
useAppNexusServer()
GamBanner.create(
wrapper, autoRefreshTime,
300, 250,
Expand All @@ -49,6 +54,7 @@ object AdTypesRepository {
AdType(
"Interstitial",
onCreate = { activity, _, autoRefreshTime ->
useAppNexusServer()
GamInterstitial.create(
activity, autoRefreshTime,
// TODO: Problem with ids
Expand All @@ -64,18 +70,21 @@ object AdTypesRepository {
AdType(
"Banner 320x50",
onCreate = { _, wrapper, autoRefreshTime ->
useTestServer()
PrebidMobile.setStoredAuctionResponse("response-prebid-banner-320-50")
MoPubBanner.create(
wrapper, autoRefreshTime,
320, 50, MoPubView.MoPubAdSize.HEIGHT_50,
"42b99af979cd474ea32f497c044b5d71",
"625c6125-f19e-4d5b-95c5-55501526b2a4"
"imp-prebid-banner-320-50"
)
},
onDestroy = { MoPubBanner.destroy() }
),
AdType(
"Banner 300x250",
onCreate = { _, wrapper, autoRefreshTime ->
useAppNexusServer()
MoPubBanner.create(
wrapper, autoRefreshTime,
300, 250, MoPubView.MoPubAdSize.HEIGHT_250,
Expand All @@ -89,6 +98,7 @@ object AdTypesRepository {
AdType(
"Interstitial",
onCreate = { activity, _, autoRefreshTime ->
useAppNexusServer()
MoPubInterstitial.create(
activity, autoRefreshTime,
// TODO: Problem with ids
Expand Down Expand Up @@ -331,7 +341,16 @@ object AdTypesRepository {
}
)
)

)

private fun useTestServer() {
PrebidMobile.setPrebidServerAccountId("0689a263-318d-448b-a3d4-b02e8a709d9d")
PrebidMobile.setPrebidServerHost(Host.createCustomHost("https://prebid-server-test-j.prebid.org/openrtb2/auction"))
}

private fun useAppNexusServer() {
PrebidMobile.setPrebidServerAccountId("bfa84af2-bd16-4d35-96ad-31c6bb888df0")
PrebidMobile.setPrebidServerHost(Host.APPNEXUS)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import com.google.android.gms.ads.RequestConfiguration
import com.mopub.common.MoPub
import com.mopub.common.SdkConfiguration
import com.mopub.common.logging.MoPubLog
import org.prebid.mobile.Host
import org.prebid.mobile.PrebidMobile

class CustomApplication : Application() {
Expand All @@ -49,22 +48,16 @@ class CustomApplication : Application() {

private fun initMopubSDK() {
val sdkConfiguration = SdkConfiguration.Builder("42b99af979cd474ea32f497c044b5d71")
sdkConfiguration.withLogLevel(MoPubLog.LogLevel.DEBUG)
sdkConfiguration.withLogLevel(MoPubLog.LogLevel.NONE)
MoPub.initializeSdk(this, sdkConfiguration.build()) {
Log.d("MoPub", "Initialized successfully!")
}
}

private fun initPrebidSDK() {
PrebidMobile.setPrebidServerAccountId("bfa84af2-bd16-4d35-96ad-31c6bb888df0")
PrebidMobile.setPrebidServerHost(Host.APPNEXUS)
PrebidMobile.setShareGeoLocation(true)
// PrebidMobile.setPbsDebug(true)
PrebidMobile.setApplicationContext(applicationContext)

val host = Host.CUSTOM
host.hostUrl = "https://prebid.openx.net/openrtb2/auction"
PrebidMobile.setPrebidServerHost(host)
PrebidMobile.setPrebidServerAccountId("0689a263-318d-448b-a3d4-b02e8a709d9d")
PrebidMobile.setShareGeoLocation(true)
}

private fun initAdMob() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.preference.PreferenceManager
import org.prebid.mobile.Host
import org.prebid.mobile.PrebidMobile
import org.prebid.mobile.prebidkotlindemo.databinding.ActivityDemoBinding

class DemoActivity : AppCompatActivity() {
Expand Down Expand Up @@ -55,6 +57,7 @@ class DemoActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_demo)

useOpenXServer()
useFakeGDPR()
parseArguments()
initViews()
Expand Down Expand Up @@ -97,4 +100,9 @@ class DemoActivity : AppCompatActivity() {
}
}

private fun useOpenXServer() {
PrebidMobile.setPrebidServerHost(Host.createCustomHost("https://prebid.openx.net/openrtb2/auction"))
PrebidMobile.setPrebidServerAccountId("0689a263-318d-448b-a3d4-b02e8a709d9d")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import org.prebid.mobile.rendering.bidding.data.FetchDemandResult;
import org.prebid.mobile.rendering.bidding.data.bid.BidResponse;
import org.prebid.mobile.rendering.bidding.listeners.BidRequesterListener;
import org.prebid.mobile.rendering.bidding.loader.BidLoader;
Expand All @@ -38,10 +39,15 @@ public abstract class AdUnit {

private static final int MIN_AUTO_REFRESH_PERIOD_MILLIS = 30_000;

private int periodMillis = 0; // No auto refresh
private DemandFetcher fetcher;
/**
* Auto refresh time period in millis. 0 for disabling auto refresh.
* Can't be less than MIN_AUTO_REFRESH_PERIOD_MILLIS.
*/
private int periodMillis = 0;
protected AdUnitConfiguration configuration = new AdUnitConfiguration();

@Nullable
protected BidLoader bidLoader;
@Nullable
protected Object adObject;

Expand All @@ -52,26 +58,23 @@ public abstract class AdUnit {

public void setAutoRefreshPeriodMillis(@IntRange(from = MIN_AUTO_REFRESH_PERIOD_MILLIS) int periodMillis) {
if (periodMillis < MIN_AUTO_REFRESH_PERIOD_MILLIS) {
LogUtil.w("periodMillis less then:" + MIN_AUTO_REFRESH_PERIOD_MILLIS);
LogUtil.w("Auto refresh time can't be less then: " + MIN_AUTO_REFRESH_PERIOD_MILLIS);
return;
}
this.periodMillis = periodMillis;
if (fetcher != null) {
fetcher.setPeriodMillis(periodMillis);
}
}

public void resumeAutoRefresh() {
LogUtil.v("Resuming auto refresh...");
if (fetcher != null) {
fetcher.start();
if (bidLoader != null) {
bidLoader.setupRefreshTimer();
}
}

public void stopAutoRefresh() {
LogUtil.v("Stopping auto refresh...");
if (fetcher != null) {
fetcher.stop();
if (bidLoader != null) {
bidLoader.cancelRefresh();
}
}

Expand Down Expand Up @@ -132,23 +135,22 @@ public void fetchDemand(@NonNull Object adObj, @NonNull OnCompleteListener liste

if (Util.supportedAdObject(adObj)) {
adObject = adObj;
BidLoader loader = new BidLoader(
bidLoader = new BidLoader(
context,
configuration,
createBidListener(listener)
);
loader.load();
// TODO:Unification: Add auto refresh
// fetcher = new DemandFetcher(adObj);
// fetcher.setPeriodMillis(periodMillis);
// fetcher.setConfiguration(configuration);
// fetcher.setListener(listener);
// if (periodMillis >= 30000) {
// LogUtil.v("Start fetching bids with auto refresh millis: " + periodMillis);
// } else {
// LogUtil.v("Start a single fetching.");
// }
// fetcher.start();

if (periodMillis > 0) {
BidLoader.BidRefreshListener bidRefreshListener = () -> true;
bidLoader.setBidRefreshListener(bidRefreshListener);
LogUtil.v("Start fetching bids with auto refresh millis: " + periodMillis);
} else {
bidLoader.setBidRefreshListener(null);
LogUtil.v("Start a single fetching.");
}

bidLoader.load();
} else {
adObject = null;
listener.onComplete(ResultCode.INVALID_AD_OBJECT);
Expand Down Expand Up @@ -272,12 +274,38 @@ public void onFetchCompleted(BidResponse response) {
@Override
public void onError(AdException exception) {
Util.apply(null, adObject);
// TODO:Unification: Change code
originalListener.onComplete(ResultCode.NETWORK_ERROR);
originalListener.onComplete(convertToResultCode(exception));
}
};
}

protected ResultCode convertToResultCode(AdException renderingException) {
FetchDemandResult fetchDemandResult = FetchDemandResult.parseErrorMessage(renderingException.getMessage());
LogUtil.e("Prebid", "Can't download bids: " + fetchDemandResult);
switch (fetchDemandResult) {
case INVALID_ACCOUNT_ID:
return ResultCode.INVALID_ACCOUNT_ID;
case INVALID_CONFIG_ID:
return ResultCode.INVALID_CONFIG_ID;
case INVALID_SIZE:
return ResultCode.INVALID_SIZE;
case INVALID_CONTEXT:
return ResultCode.INVALID_CONTEXT;
case INVALID_AD_OBJECT:
return ResultCode.INVALID_AD_OBJECT;
case INVALID_HOST_URL:
return ResultCode.INVALID_HOST_URL;
case NETWORK_ERROR:
return ResultCode.NETWORK_ERROR;
case TIMEOUT:
return ResultCode.TIMEOUT;
case NO_BIDS:
return ResultCode.NO_BIDS;
default:
return ResultCode.PREBID_SERVER_ERROR;
}
}


@VisibleForTesting
public AdUnitConfiguration getConfiguration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public void onFetchCompleted(BidResponse response) {
@Override
public void onError(AdException exception) {
Util.apply(null, adObject);
// TODO:Unification: Change code
originalListener.onComplete(ResultCode.NETWORK_ERROR);
originalListener.onComplete(convertToResultCode(exception));
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.prebid.mobile.PrebidMobile;
import org.prebid.mobile.rendering.models.openrtb.bidRequests.*;
import org.prebid.mobile.rendering.models.openrtb.bidRequests.source.Source;

Expand Down Expand Up @@ -58,6 +59,7 @@ public JSONObject getJsonObject() throws JSONException {
toJSON(jsonObject, "user", (mUser != null) ? mUser.getJsonObject() : null);
toJSON(jsonObject, "source", mSource != null ? mSource.getJsonObject() : null);
toJSON(jsonObject, "ext", mExt != null ? mExt.getJsonObject() : null);
toJSON(jsonObject, "test", PrebidMobile.getPbsDebug() ? 1 : null);

return jsonObject;
}
Expand Down

0 comments on commit 81f402d

Please sign in to comment.