Skip to content

Commit

Permalink
Apply bid loader from rendering instead of demand fetcher (#381)
Browse files Browse the repository at this point in the history
* feat(unification): connect BidLoader from rendering #370

Add Native json object. Add saving Native ad to cache.

* feat(unification): configure auto refresh #370

Add pbs debug field to Bid request.

* feat(unification): add setter for auto refresh interval in seconds #370

* feat(unification): fix ad units types #370

* feat(unification): rebase conflicts resolves #370
  • Loading branch information
ValentinPostindustria authored Mar 23, 2022
1 parent 98b96ce commit a2f0996
Show file tree
Hide file tree
Showing 27 changed files with 418 additions and 86 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,21 @@ object AdTypesRepository {
}
)
)

)

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

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 @@ -55,6 +55,7 @@ class DemoActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_demo)

AdTypesRepository.useOpenXServer()
useFakeGDPR()
parseArguments()
initViews()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,64 +23,72 @@
import android.text.TextUtils;
import androidx.annotation.IntRange;
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;
import org.prebid.mobile.rendering.errors.AdException;
import org.prebid.mobile.tasksmanager.TasksManager;
import org.prebid.mobile.units.configuration.AdUnitConfiguration;

import java.util.*;

public abstract class AdUnit {
import static org.prebid.mobile.PrebidMobile.AUTO_REFRESH_DELAY_MAX;
import static org.prebid.mobile.PrebidMobile.AUTO_REFRESH_DELAY_MIN;

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

private int periodMillis = 0; // No auto refresh
private DemandFetcher fetcher;
protected AdUnitConfiguration configuration = new AdUnitConfiguration();

AdUnit(@NonNull String configId, @NonNull AdType adType) {
@Nullable
protected BidLoader bidLoader;
@Nullable
protected Object adObject;

AdUnit(@NonNull String configId, @NonNull AdUnitConfiguration.AdUnitIdentifierType adType) {
configuration.setConfigId(configId);
configuration.setAdType(adType);
configuration.setAdUnitIdentifierType(adType);
}

public void setAutoRefreshPeriodMillis(@IntRange(from = MIN_AUTO_REFRESH_PERIOD_MILLIS) int periodMillis) {
if (periodMillis < MIN_AUTO_REFRESH_PERIOD_MILLIS) {
LogUtil.warning("periodMillis less then:" + MIN_AUTO_REFRESH_PERIOD_MILLIS);
return;
}
this.periodMillis = periodMillis;
if (fetcher != null) {
fetcher.setPeriodMillis(periodMillis);
}
/**
* @deprecated Please use setAutoRefreshInterval() in seconds!
*/
@Deprecated
public void setAutoRefreshPeriodMillis(
@IntRange(from = AUTO_REFRESH_DELAY_MIN, to = AUTO_REFRESH_DELAY_MAX) int periodMillis
) {
configuration.setAutoRefreshDelay(periodMillis / 1000);
}

public void setAutoRefreshInterval(
@IntRange(from = AUTO_REFRESH_DELAY_MIN / 1000, to = AUTO_REFRESH_DELAY_MAX / 1000) int seconds
) {
configuration.setAutoRefreshDelay(seconds);
}

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

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

public void fetchDemand(@NonNull final OnCompleteListener2 listener) {

final Map<String, String> keywordsMap = new HashMap<>();

fetchDemand(keywordsMap, new OnCompleteListener() {
@Override
public void onComplete(final ResultCode resultCode) {
TasksManager.getInstance().executeOnMainThread(new Runnable() {
@Override
public void run() {
listener.onComplete(resultCode, keywordsMap.size() != 0 ? Collections.unmodifiableMap(keywordsMap) : null);
}
});
}
fetchDemand(keywordsMap, resultCode -> {
TasksManager.getInstance().executeOnMainThread(() ->
listener.onComplete(resultCode, keywordsMap.size() != 0 ? Collections.unmodifiableMap(keywordsMap) : null)
);
});
}

Expand Down Expand Up @@ -130,17 +138,25 @@ public void fetchDemand(@NonNull Object adObj, @NonNull OnCompleteListener liste
}

if (Util.supportedAdObject(adObj)) {
fetcher = new DemandFetcher(adObj);
fetcher.setPeriodMillis(periodMillis);
fetcher.setConfiguration(configuration);
fetcher.setListener(listener);
if (periodMillis >= 30000) {
LogUtil.verbose("Start fetching bids with auto refresh millis: " + periodMillis);
adObject = adObj;
bidLoader = new BidLoader(
context,
configuration,
createBidListener(listener)
);

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

bidLoader.load();
} else {
adObject = null;
listener.onComplete(ResultCode.INVALID_AD_OBJECT);
}

Expand Down Expand Up @@ -249,6 +265,52 @@ public void setPbAdSlot(String pbAdSlot) {
configuration.setPbAdSlot(pbAdSlot);
}


protected BidRequesterListener createBidListener(OnCompleteListener originalListener) {
return new BidRequesterListener() {
@Override
public void onFetchCompleted(BidResponse response) {
HashMap<String, String> keywords = response.getTargeting();
Util.apply(keywords, adObject);
originalListener.onComplete(ResultCode.SUCCESS);
}

@Override
public void onError(AdException exception) {
Util.apply(null, adObject);
originalListener.onComplete(convertToResultCode(exception));
}
};
}

protected ResultCode convertToResultCode(AdException renderingException) {
FetchDemandResult fetchDemandResult = FetchDemandResult.parseErrorMessage(renderingException.getMessage());
LogUtil.error("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() {
return configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
package org.prebid.mobile;

import androidx.annotation.NonNull;
import org.prebid.mobile.units.configuration.AdUnitConfiguration;

import java.util.HashSet;

public class BannerAdUnit extends BannerBaseAdUnit {

public BannerAdUnit(@NonNull String configId, int width, int height) {
super(configId, AdType.BANNER);
super(configId, AdUnitConfiguration.AdUnitIdentifierType.BANNER);
configuration.addSize(new AdSize(width, height));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.prebid.mobile.units.configuration.AdUnitConfiguration;

import java.util.List;

public abstract class BannerBaseAdUnit extends AdUnit {

BannerBaseAdUnit(@NonNull String configId, @NonNull AdType adType) {
BannerBaseAdUnit(@NonNull String configId, @NonNull AdUnitConfiguration.AdUnitIdentifierType adType) {
super(configId, adType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.prebid.mobile.rendering.models.AdPosition;
import org.prebid.mobile.units.configuration.AdUnitConfiguration;

public class InterstitialAdUnit extends BannerBaseAdUnit {

public InterstitialAdUnit(@NonNull String configId) {
super(configId, AdType.INTERSTITIAL);
super(configId, AdUnitConfiguration.AdUnitIdentifierType.INTERSTITIAL);
configuration.setAdPosition(AdPosition.FULLSCREEN);
}

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

0 comments on commit a2f0996

Please sign in to comment.