Skip to content
Permalink
Browse files

Add source code for mopub adapter

  • Loading branch information
Divya Kali Ranga Fnu
Divya Kali Ranga Fnu committed Sep 5, 2019
1 parent 36178ec commit 06e19b2055a372c5fbcd2f851d2ae59b1b6d86d2
@@ -0,0 +1,97 @@
package com.criteo.mediation.mopub;

import static com.mopub.common.logging.MoPubLog.AdapterLogEvent.LOAD_ATTEMPTED;
import static com.mopub.common.logging.MoPubLog.AdapterLogEvent.LOAD_FAILED;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import com.criteo.publisher.Criteo;
import com.criteo.publisher.CriteoBannerView;
import com.criteo.publisher.CriteoInitException;
import com.criteo.publisher.model.AdSize;
import com.criteo.publisher.model.AdUnit;
import com.criteo.publisher.model.BannerAdUnit;
import com.mopub.common.logging.MoPubLog;
import com.mopub.mobileads.CustomEventBanner;
import com.mopub.mobileads.MoPubErrorCode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CriteoBannerAdapter extends CustomEventBanner {

private static final String TAG = CriteoBannerAdapter.class.getSimpleName();
protected static final String ADUNIT_ID = "adUnitId";
protected static final String CRITEO_PUBLISHER_ID = "cpId";
protected static final String MOPUB_WIDTH = "com_mopub_ad_width";
protected static final String MOPUB_HEIGHT = "com_mopub_ad_height";
private CriteoBannerView bannerView;

@Override
protected void loadBanner(Context context, CustomEventBannerListener customEventBannerListener,
Map<String, Object> localExtras, Map<String, String> serverExtras) {

if (TextUtils.isEmpty(localExtras.toString()) || TextUtils.isEmpty(serverExtras.toString())) {
MoPubLog.log(LOAD_FAILED, TAG, "Server parameters are empty");
customEventBannerListener.onBannerFailed(MoPubErrorCode.ADAPTER_CONFIGURATION_ERROR);
return;
}

AdSize adSize = getAdSize(localExtras);
String criteoPublisherId = serverExtras.get(CRITEO_PUBLISHER_ID);

if (adSize == null || criteoPublisherId == null) {
MoPubLog.log(LOAD_FAILED, TAG, "CriteoPublisherId cannot be null");
customEventBannerListener.onBannerFailed(MoPubErrorCode.ADAPTER_CONFIGURATION_ERROR);
return;
}

String adUnitId = serverExtras.get(ADUNIT_ID);

if (adUnitId == null) {
MoPubLog.log(LOAD_FAILED, TAG, "Missing adUnit Id");
customEventBannerListener.onBannerFailed(MoPubErrorCode.MISSING_AD_UNIT_ID);
return;
}

try {
Criteo.getInstance();
BannerAdUnit bannerAdUnit = new BannerAdUnit(adUnitId, adSize);
bannerView = new CriteoBannerView(context, bannerAdUnit);
CriteoBannerEventListener listener = new CriteoBannerEventListener(customEventBannerListener);
bannerView.setCriteoBannerAdListener(listener);
bannerView.loadAd();
MoPubLog.log(LOAD_ATTEMPTED, TAG, "BannerView is loading");
} catch (Exception e) {
List<AdUnit> adUnits = new ArrayList<>();

try {
Criteo.init((Application) (context.getApplicationContext()), criteoPublisherId, adUnits);
} catch (CriteoInitException e1) {

}
MoPubLog.log(LOAD_FAILED, TAG, "Initialization failed");
customEventBannerListener.onBannerFailed(MoPubErrorCode.INTERNAL_ERROR);
}
}

@Override
protected void onInvalidate() {
if (bannerView != null) {
bannerView.destroy();
}
}

private AdSize getAdSize(Map<String, Object> localExtras) {
Object objHeight = localExtras.get(MOPUB_HEIGHT);
Object objWidth = localExtras.get(MOPUB_WIDTH);
if (objHeight == null || objWidth == null) {
return null;
}

Integer height = (Integer) objHeight;
Integer width = (Integer) objWidth;
return new AdSize(width, height);
}
}
@@ -0,0 +1,57 @@
package com.criteo.mediation.mopub;

import android.view.View;
import com.criteo.publisher.CriteoBannerAdListener;
import com.criteo.publisher.CriteoErrorCode;
import com.mopub.mobileads.CustomEventBanner.CustomEventBannerListener;
import com.mopub.mobileads.MoPubErrorCode;

public class CriteoBannerEventListener implements CriteoBannerAdListener {

private CustomEventBannerListener customEventBannerListener;

public CriteoBannerEventListener(CustomEventBannerListener listener) {
customEventBannerListener = listener;
}

@Override
public void onAdReceived(View view) {
customEventBannerListener.onBannerLoaded(view);
}

@Override
public void onAdFailedToReceive(CriteoErrorCode code) {
switch (code) {
case ERROR_CODE_INTERNAL_ERROR:
customEventBannerListener.onBannerFailed(MoPubErrorCode.INTERNAL_ERROR);
break;
case ERROR_CODE_NETWORK_ERROR:
customEventBannerListener.onBannerFailed(MoPubErrorCode.NETWORK_TIMEOUT);
break;
case ERROR_CODE_INVALID_REQUEST:
customEventBannerListener.onBannerFailed(MoPubErrorCode.SERVER_ERROR);
break;
case ERROR_CODE_NO_FILL:
customEventBannerListener.onBannerFailed(MoPubErrorCode.NETWORK_NO_FILL);
break;
}
}

@Override
public void onAdLeftApplication() {
customEventBannerListener.onLeaveApplication();
}

@Override
public void onAdClicked() {
customEventBannerListener.onBannerClicked();
}

@Override
public void onAdOpened() {
}

@Override
public void onAdClosed() {
}
}
@@ -0,0 +1,53 @@
package com.criteo.mediation.mopub;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.mopub.common.BaseAdapterConfiguration;
import com.mopub.common.OnNetworkInitializationFinishedListener;
import com.mopub.mobileads.MoPubErrorCode;
import java.util.Map;

public class CriteoBaseAdapterConfiguration extends BaseAdapterConfiguration {

private String adapterVersion;
private String networkSdkVersion;
private String moPubNetworkName;


@NonNull
@Override
public String getAdapterVersion() {
return adapterVersion;
}

@Nullable
@Override
public String getBiddingToken(@NonNull Context context) {
return null;
}

@NonNull
@Override
public String getMoPubNetworkName() {
return moPubNetworkName;
}

@NonNull
@Override
public String getNetworkSdkVersion() {
return networkSdkVersion;
}

@Override
public void initializeNetwork(@NonNull Context context, @Nullable Map<String, String> configuration,
@NonNull OnNetworkInitializationFinishedListener listener) {

this.networkSdkVersion = "3.1.0";
this.adapterVersion = "3.1.0.1";
this.moPubNetworkName = "criteo";

listener.onNetworkInitializationFinished(CriteoBaseAdapterConfiguration.class,
MoPubErrorCode.ADAPTER_INITIALIZATION_SUCCESS);
}
}
@@ -0,0 +1,88 @@
package com.criteo.mediation.mopub;


import static com.mopub.common.logging.MoPubLog.AdapterLogEvent.LOAD_ATTEMPTED;
import static com.mopub.common.logging.MoPubLog.AdapterLogEvent.LOAD_FAILED;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import com.criteo.publisher.Criteo;
import com.criteo.publisher.CriteoInitException;
import com.criteo.publisher.CriteoInterstitial;
import com.criteo.publisher.model.AdUnit;
import com.criteo.publisher.model.InterstitialAdUnit;
import com.mopub.common.logging.MoPubLog;
import com.mopub.mobileads.CustomEventInterstitial;
import com.mopub.mobileads.MoPubErrorCode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CriteoInterstitialAdapter extends CustomEventInterstitial {

private static final String TAG = CriteoInterstitialAdapter.class.getSimpleName();
protected static final String ADUNIT_ID = "adUnitId";
protected static final String CRITEO_PUBLISHER_ID = "cpId";
private CriteoInterstitial criteoInterstitial;

@Override
protected void loadInterstitial(Context context, CustomEventInterstitialListener customEventInterstitialListener,
Map<String, Object> localExtras, Map<String, String> serverExtras) {

if (TextUtils.isEmpty(serverExtras.toString())) {
MoPubLog.log(LOAD_FAILED, TAG, "Server parameters are empty");
customEventInterstitialListener.onInterstitialFailed(MoPubErrorCode.ADAPTER_CONFIGURATION_ERROR);
return;
}

String criteoPublisherId = serverExtras.get(CRITEO_PUBLISHER_ID);

if (criteoPublisherId == null) {
MoPubLog.log(LOAD_FAILED, TAG, "CriteoPublisherId cannot be null");
customEventInterstitialListener.onInterstitialFailed(MoPubErrorCode.ADAPTER_CONFIGURATION_ERROR);
return;
}

String adUnitId = serverExtras.get(ADUNIT_ID);

if (adUnitId == null) {
MoPubLog.log(LOAD_FAILED, TAG, "Missing adunit Id");
customEventInterstitialListener.onInterstitialFailed(MoPubErrorCode.MISSING_AD_UNIT_ID);
return;
}

try {
Criteo.getInstance();
InterstitialAdUnit interstitialAdUnit = new InterstitialAdUnit(adUnitId);
criteoInterstitial = new CriteoInterstitial(context, interstitialAdUnit);
CriteoInterstitialEventListener listener = new CriteoInterstitialEventListener(
customEventInterstitialListener);
criteoInterstitial.setCriteoInterstitialAdListener(listener);
criteoInterstitial.setCriteoInterstitialAdDisplayListener(listener);
criteoInterstitial.loadAd();
MoPubLog.log(LOAD_ATTEMPTED, TAG, "Criteo Interstitial is loading");
} catch (Exception e) {
List<AdUnit> adUnits = new ArrayList<>();

try {
Criteo.init((Application) (context.getApplicationContext()), criteoPublisherId, adUnits);
} catch (CriteoInitException e1) {
}
MoPubLog.log(LOAD_FAILED, TAG, "Initialization failed");
customEventInterstitialListener.onInterstitialFailed(MoPubErrorCode.INTERNAL_ERROR);
}
}

@Override
protected void showInterstitial() {
if (criteoInterstitial != null) {
criteoInterstitial.show();
}
}

@Override
protected void onInvalidate() {

}
}
@@ -0,0 +1,70 @@
package com.criteo.mediation.mopub;

import com.criteo.publisher.CriteoErrorCode;
import com.criteo.publisher.CriteoInterstitialAdDisplayListener;
import com.criteo.publisher.CriteoInterstitialAdListener;
import com.mopub.mobileads.CustomEventInterstitial.CustomEventInterstitialListener;
import com.mopub.mobileads.MoPubErrorCode;

public class CriteoInterstitialEventListener implements CriteoInterstitialAdListener,
CriteoInterstitialAdDisplayListener {

private CustomEventInterstitialListener customEventInterstitialListener;

public CriteoInterstitialEventListener(CustomEventInterstitialListener listener) {
customEventInterstitialListener = listener;
}

@Override
public void onAdOpened() {
customEventInterstitialListener.onInterstitialShown();
}

@Override
public void onAdClosed() {
customEventInterstitialListener.onInterstitialDismissed();
}

@Override
public void onAdFailedToReceive(CriteoErrorCode code) {
switch (code) {
case ERROR_CODE_INTERNAL_ERROR:
customEventInterstitialListener.onInterstitialFailed(MoPubErrorCode.INTERNAL_ERROR);
break;
case ERROR_CODE_NETWORK_ERROR:
customEventInterstitialListener.onInterstitialFailed(MoPubErrorCode.NETWORK_TIMEOUT);
break;
case ERROR_CODE_INVALID_REQUEST:
customEventInterstitialListener.onInterstitialFailed(MoPubErrorCode.SERVER_ERROR);
break;
case ERROR_CODE_NO_FILL:
customEventInterstitialListener.onInterstitialFailed(MoPubErrorCode.NETWORK_NO_FILL);
break;
}
}

@Override
public void onAdLeftApplication() {
customEventInterstitialListener.onLeaveApplication();
}

@Override
public void onAdClicked() {
customEventInterstitialListener.onInterstitialClicked();
}

@Override
public void onAdReadyToDisplay() {
customEventInterstitialListener.onInterstitialLoaded();
}

@Override
public void onAdFailedToDisplay(CriteoErrorCode code) {
customEventInterstitialListener.onInterstitialFailed(MoPubErrorCode.NETWORK_TIMEOUT);
}

@Override
public void onAdReceived() {
}

}

0 comments on commit 06e19b2

Please sign in to comment.
You can’t perform that action at this time.