Search in sources :

Example 41 with MoPubView

use of com.mopub.mobileads.MoPubView in project prebid-mobile-android by prebid.

the class XandrNativeInBannerMoPubDemoActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_demo);
    PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
    PrebidMobile.setPrebidServerAccountId(Constants.PBS_ACCOUNT_ID_APPNEXUS);
    adUnit = new NativeAdUnit(Constants.PBS_CONFIG_ID_NATIVE_APPNEXUS);
    final FrameLayout adFrame = (FrameLayout) findViewById(R.id.adFrame);
    adFrame.removeAllViews();
    final MoPubView adView = new MoPubView(this);
    adView.setAdUnitId(Constants.MOPUB_IN_BANNER_NATIVE_ADUNIT_ID_APPNEXUS);
    adView.setBannerAdListener(new MoPubView.BannerAdListener() {

        @Override
        public void onBannerLoaded(final MoPubView banner) {
            adFrame.addView(banner);
        }

        @Override
        public void onBannerFailed(MoPubView banner, MoPubErrorCode errorCode) {
            LogUtil.d("Banner failed " + errorCode);
        }

        @Override
        public void onBannerClicked(MoPubView banner) {
        }

        @Override
        public void onBannerExpanded(MoPubView banner) {
        }

        @Override
        public void onBannerCollapsed(MoPubView banner) {
        }
    });
    adView.setAutorefreshEnabled(false);
    NativeAdUnit nativeAdUnit = (NativeAdUnit) adUnit;
    nativeAdUnit.setContextType(NativeAdUnit.CONTEXT_TYPE.SOCIAL_CENTRIC);
    nativeAdUnit.setPlacementType(NativeAdUnit.PLACEMENTTYPE.CONTENT_FEED);
    nativeAdUnit.setContextSubType(NativeAdUnit.CONTEXTSUBTYPE.GENERAL_SOCIAL);
    ArrayList<NativeEventTracker.EVENT_TRACKING_METHOD> methods = new ArrayList<>();
    methods.add(NativeEventTracker.EVENT_TRACKING_METHOD.IMAGE);
    methods.add(NativeEventTracker.EVENT_TRACKING_METHOD.JS);
    try {
        NativeEventTracker tracker = new NativeEventTracker(NativeEventTracker.EVENT_TYPE.IMPRESSION, methods);
        nativeAdUnit.addEventTracker(tracker);
    } catch (Exception e) {
        e.printStackTrace();
    }
    NativeTitleAsset title = new NativeTitleAsset();
    title.setLength(90);
    title.setRequired(true);
    nativeAdUnit.addAsset(title);
    NativeImageAsset icon = new NativeImageAsset();
    icon.setImageType(NativeImageAsset.IMAGE_TYPE.ICON);
    icon.setWMin(20);
    icon.setHMin(20);
    icon.setRequired(true);
    nativeAdUnit.addAsset(icon);
    NativeImageAsset image = new NativeImageAsset();
    image.setImageType(NativeImageAsset.IMAGE_TYPE.MAIN);
    image.setHMin(200);
    image.setWMin(200);
    image.setRequired(true);
    nativeAdUnit.addAsset(image);
    NativeDataAsset data = new NativeDataAsset();
    data.setLen(90);
    data.setDataType(NativeDataAsset.DATA_TYPE.SPONSORED);
    data.setRequired(true);
    nativeAdUnit.addAsset(data);
    NativeDataAsset body = new NativeDataAsset();
    body.setRequired(true);
    body.setDataType(NativeDataAsset.DATA_TYPE.DESC);
    nativeAdUnit.addAsset(body);
    NativeDataAsset cta = new NativeDataAsset();
    cta.setRequired(true);
    cta.setDataType(NativeDataAsset.DATA_TYPE.CTATEXT);
    nativeAdUnit.addAsset(cta);
    int millis = getIntent().getIntExtra(Constants.AUTO_REFRESH_NAME, 0);
    nativeAdUnit.setAutoRefreshPeriodMillis(millis);
    nativeAdUnit.fetchDemand(adView, new OnCompleteListener() {

        @Override
        public void onComplete(ResultCode resultCode) {
            adView.loadAd(MoPubView.MoPubAdSize.MATCH_VIEW);
        }
    });
}
Also used : MoPubErrorCode(com.mopub.mobileads.MoPubErrorCode) ArrayList(java.util.ArrayList) NativeTitleAsset(org.prebid.mobile.NativeTitleAsset) NativeDataAsset(org.prebid.mobile.NativeDataAsset) NativeAdUnit(org.prebid.mobile.NativeAdUnit) OnCompleteListener(org.prebid.mobile.OnCompleteListener) NativeImageAsset(org.prebid.mobile.NativeImageAsset) FrameLayout(android.widget.FrameLayout) NativeEventTracker(org.prebid.mobile.NativeEventTracker) ResultCode(org.prebid.mobile.ResultCode) MoPubView(com.mopub.mobileads.MoPubView)

Example 42 with MoPubView

use of com.mopub.mobileads.MoPubView in project prebid-mobile-android by prebid.

the class AdServerTest method startTest.

public void startTest() {
    GeneralSettings generalSettings = SettingsManager.getInstance(mContext).getGeneralSettings();
    AdServerSettings adServerSettings = SettingsManager.getInstance(mContext).getAdServerSettings();
    PrebidServerSettings prebidServerSettings = SettingsManager.getInstance(mContext).getPrebidServerSettings();
    switch(generalSettings.getAdSize()) {
        case BANNER_300x250:
            break;
        case BANNER_300x600:
            break;
        case BANNER_320x50:
            break;
        case BANNER_320x100:
            break;
        case BANNER_320x480:
            break;
        case BANNER_728x90:
            break;
    }
    mKeywords = createMapKeywords(adServerSettings.getBidPrice(), generalSettings.getAdSize(), generalSettings.getAdFormat(), prebidServerSettings.getPrebidServer());
    if (adServerSettings.getAdServer() == AdServer.MOPUB) {
        String keywords = createStringKeywords();
        if (generalSettings.getAdFormat() == AdFormat.BANNER) {
            AdSize adSize = generalSettings.getAdSize();
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(DimenUtil.convertPxToDp(mContext, adSize.getWidth()), DimenUtil.convertPxToDp(mContext, adSize.getHeight()));
            mMoPubAd = new MoPubView(mContext);
            mMoPubAd.setLayoutParams(layoutParams);
            mMoPubAd.setAdUnitId(adServerSettings.getAdUnitId());
            mMoPubAd.setAutorefreshEnabled(false);
            mMoPubAd.setBannerAdListener(this);
            mMoPubAd.setKeywords(keywords);
            Networking.getRequestQueue(mContext).addRequestFinishedListener(mMoPubRequestFinishedListener);
            mMoPubAd.loadAd();
        } else if (generalSettings.getAdFormat() == AdFormat.INTERSTITIAL) {
            mMoPubInterstitial = new MoPubInterstitial(mContext, adServerSettings.getAdUnitId());
            mMoPubInterstitial.setInterstitialAdListener(this);
            mMoPubInterstitial.setKeywords(keywords);
            Networking.getRequestQueue(mContext).addRequestFinishedListener(mMoPubRequestFinishedListener);
            mMoPubInterstitial.load();
        }
    } else if (adServerSettings.getAdServer() == AdServer.GOOGLE_AD_MANAGER) {
        PublisherAdRequest adRequest = null;
        if (generalSettings.getAdFormat() == AdFormat.BANNER) {
            mGoogleAd = new PublisherAdView(mContext);
            AdSize adSize = generalSettings.getAdSize();
            mGoogleAd.setAdSizes(new com.google.android.gms.ads.AdSize(adSize.getWidth(), adSize.getHeight()));
            mGoogleAd.setAdUnitId(adServerSettings.getAdUnitId());
            mGoogleAd.setAdListener(mGoogleBannerListener);
        } else if (generalSettings.getAdFormat() == AdFormat.INTERSTITIAL) {
            mGoogleInterstitial = new PublisherInterstitialAd(mContext);
            mGoogleInterstitial.setAdUnitId(adServerSettings.getAdUnitId());
            mGoogleInterstitial.setAdListener(mGoogleInterstitialListener);
        }
        PublisherAdRequest.Builder adRequestBuilder = new PublisherAdRequest.Builder();
        for (String key : mKeywords.keySet()) {
            if (mKeywords.containsKey(key)) {
                adRequestBuilder.addCustomTargeting(key, mKeywords.get(key));
            }
        }
        adRequest = adRequestBuilder.build();
        if (generalSettings.getAdFormat() == AdFormat.BANNER) {
            mGoogleAd.loadAd(adRequest);
        } else if (generalSettings.getAdFormat() == AdFormat.INTERSTITIAL) {
            mGoogleInterstitial.loadAd(adRequest);
        }
        checkRequestForKeywordsAM(adRequest);
    }
}
Also used : PrebidServerSettings(org.prebid.mobile.drprebid.model.PrebidServerSettings) MoPubInterstitial(com.mopub.mobileads.MoPubInterstitial) AdServerSettings(org.prebid.mobile.drprebid.model.AdServerSettings) GeneralSettings(org.prebid.mobile.drprebid.model.GeneralSettings) PublisherAdView(com.google.android.gms.ads.doubleclick.PublisherAdView) AdSize(org.prebid.mobile.drprebid.model.AdSize) FrameLayout(android.widget.FrameLayout) PublisherAdRequest(com.google.android.gms.ads.doubleclick.PublisherAdRequest) PublisherInterstitialAd(com.google.android.gms.ads.doubleclick.PublisherInterstitialAd) MoPubView(com.mopub.mobileads.MoPubView)

Example 43 with MoPubView

use of com.mopub.mobileads.MoPubView in project prebid-mobile-android by prebid.

the class KeywordsSetupTest method setResponseToMoPubLocalExtrasWithMoPubBannerOrInterstitialView_ResponseStoredInLocalExtras.

@Test
public void setResponseToMoPubLocalExtrasWithMoPubBannerOrInterstitialView_ResponseStoredInLocalExtras() {
    MoPubView moPubView = new MoPubView(mActivity);
    MoPubBannerMediationUtils mediationUtils = new MoPubBannerMediationUtils(moPubView);
    BidResponse response = new BidResponse("{\"response\":\"test\", \"id\":\"1234\"}");
    Map<String, Object> expectedLocalExtras = Collections.singletonMap(KEY_BID_RESPONSE, response.getId());
    mediationUtils.setResponseToLocalExtras(response);
    assertEquals(expectedLocalExtras, moPubView.getLocalExtras());
}
Also used : BidResponse(org.prebid.mobile.rendering.bidding.data.bid.BidResponse) MoPubBannerMediationUtils(com.mopub.mediation.MoPubBannerMediationUtils) MoPubView(com.mopub.mobileads.MoPubView) Test(org.junit.Test)

Example 44 with MoPubView

use of com.mopub.mobileads.MoPubView in project prebid-mobile-android by prebid.

the class ResultCodeTest method testNoBids.

@Test
public void testNoBids() throws Exception {
    HttpUrl httpUrl = server.url("/");
    Host.CUSTOM.setHostUrl(httpUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    PrebidMobile.setPrebidServerAccountId("123456");
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBid()));
    BannerAdUnit adUnit = new BannerAdUnit("123456", 300, 250);
    MoPubView testView = new MoPubView(activity);
    OnCompleteListener mockListener = mock(OnCompleteListener.class);
    adUnit.fetchDemand(testView, mockListener);
    DemandFetcher fetcher = (DemandFetcher) FieldUtils.readField(adUnit, "fetcher", true);
    PrebidMobile.setTimeoutMillis(Integer.MAX_VALUE);
    ShadowLooper fetcherLooper = shadowOf(fetcher.getHandler().getLooper());
    fetcherLooper.runToEndOfTasks();
    ShadowLooper demandLooper = shadowOf(fetcher.getDemandHandler().getLooper());
    demandLooper.runToEndOfTasks();
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.NO_BIDS);
    assertEquals(null, testView.getKeywords());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) HttpUrl(okhttp3.HttpUrl) MoPubView(com.mopub.mobileads.MoPubView) Test(org.junit.Test)

Example 45 with MoPubView

use of com.mopub.mobileads.MoPubView in project prebid-mobile-android by prebid.

the class ResultCodeTest method testEmptyHostUrl.

@Test
public void testEmptyHostUrl() throws Exception {
    PrebidMobile.setPrebidServerAccountId("123456");
    Host.CUSTOM.setHostUrl("");
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    BannerAdUnit adUnit = new BannerAdUnit("123456", 320, 50);
    MoPubView testView = new MoPubView(activity);
    OnCompleteListener mockListener = mock(OnCompleteListener.class);
    adUnit.fetchDemand(testView, mockListener);
    verify(mockListener).onComplete(ResultCode.INVALID_HOST_URL);
}
Also used : MoPubView(com.mopub.mobileads.MoPubView) Test(org.junit.Test)

Aggregations

MoPubView (com.mopub.mobileads.MoPubView)48 Test (org.junit.Test)38 FrameLayout (android.widget.FrameLayout)26 BannerAdUnit (org.prebid.mobile.BannerAdUnit)23 OnCompleteListener (org.prebid.mobile.OnCompleteListener)23 ResultCode (org.prebid.mobile.ResultCode)23 MockResponse (okhttp3.mockwebserver.MockResponse)15 HttpUrl (okhttp3.HttpUrl)8 Dispatcher (okhttp3.mockwebserver.Dispatcher)8 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)8 Matchers.containsString (org.hamcrest.Matchers.containsString)7 BackgroundThreadExecutor (org.prebid.mobile.tasksmanager.BackgroundThreadExecutor)7 ShadowLooper (org.robolectric.shadows.ShadowLooper)7 MoPubInterstitial (com.mopub.mobileads.MoPubInterstitial)5 JSONException (org.json.JSONException)5 ArrayList (java.util.ArrayList)4 JSONObject (org.json.JSONObject)4 AdUnit (org.prebid.mobile.AdUnit)4 InterstitialAdUnit (org.prebid.mobile.InterstitialAdUnit)4 MoPubErrorCode (com.mopub.mobileads.MoPubErrorCode)3