Java Code Examples for com.mopub.mobileads.MoPubView#setAdUnitId()

The following examples show how to use com.mopub.mobileads.MoPubView#setAdUnitId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MoPubBannerAdView.java    From mobile-sdk-android with Apache License 2.0 6 votes vote down vote up
@Override
public View requestAd(MediatedBannerAdViewController mBC, Activity activity, String parameter, String uid,
                      int width, int height, TargetingParameters targetingParameters) {
    mpListener = new MoPubListener(mBC, this.getClass().getSimpleName());
    adView = new MoPubView(activity);
    adView.setAdUnitId(uid);
    adView.setBannerAdListener(mpListener);
    adView.setAutorefreshEnabled(false);

    if (targetingParameters != null) {
        if (targetingParameters.getLocation() != null) {
            adView.setLocation(targetingParameters.getLocation());
        }

        String keywords = MoPubListener.keywordsFromTargetingParameters(targetingParameters);
        adView.setKeywords(keywords);
    }

    adView.setMinimumWidth(width);
    adView.setMinimumHeight(height);
    adView.loadAd(MoPubView.MoPubAdSize.valueOf(height));
    return adView;
}
 
Example 2
Source File: DemoActivity.java    From prebid-mobile-android with Apache License 2.0 5 votes vote down vote up
void setupMPBanner(String adUnitId, int width, int height) {
    FrameLayout adFrame = findViewById(R.id.adFrame);
    adFrame.removeAllViews();
    adView = new MoPubView(this);
    adView.setAdUnitId(adUnitId);

    adView.setMinimumWidth(width);
    adView.setMinimumHeight(height);
    adFrame.addView(adView);
}
 
Example 3
Source File: DemandFetcherTest.java    From prebid-mobile-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleRequestOneBidResponseForMoPubAdObject() throws Exception {

    HttpUrl httpUrl = server.url("/");
    PrebidMobile.setApplicationContext(activity);
    Host.CUSTOM.setHostUrl(httpUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.oneBidFromAppNexus()));
    MoPubView adView = new MoPubView(activity);
    adView.setAdUnitId("123456789");
    DemandFetcher demandFetcher = new DemandFetcher(adView);
    PrebidMobile.setTimeoutMillis(Integer.MAX_VALUE);
    demandFetcher.setPeriodMillis(0);
    HashSet<AdSize> sizes = new HashSet<>();
    sizes.add(new AdSize(300, 250));
    RequestParams requestParams = new RequestParams("12345", AdType.BANNER, sizes);
    demandFetcher.setRequestParams(requestParams);
    OnCompleteListener mockListener = mock(OnCompleteListener.class);
    demandFetcher.setListener(mockListener);
    assertEquals(DemandFetcher.STATE.STOPPED, FieldUtils.readField(demandFetcher, "state", true));
    demandFetcher.start();
    assertEquals(DemandFetcher.STATE.RUNNING, FieldUtils.readField(demandFetcher, "state", true));
    ShadowLooper fetcherLooper = Shadows.shadowOf(demandFetcher.getHandler().getLooper());
    fetcherLooper.runOneTask();
    ShadowLooper demandLooper = Shadows.shadowOf(demandFetcher.getDemandHandler().getLooper());
    demandLooper.runOneTask();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.SUCCESS);
    assertEquals(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
    String adViewKeywords = adView.getKeywords();
    assertEquals("hb_pb:0.50,hb_env:mobile-app,hb_pb_appnexus:0.50,hb_size:300x250,hb_bidder_appnexus:appnexus,hb_bidder:appnexus,hb_cache_id:df4aba04-5e69-44b8-8608-058ab21600b8,hb_env_appnexus:mobile-app,hb_size_appnexus:300x250,hb_cache_id_appnexus:df4aba04-5e69-44b8-8608-058ab21600b8,", adViewKeywords);
}
 
Example 4
Source File: DemandFetcherTest.java    From prebid-mobile-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleRequestOneBidRubiconResponseForMoPubAdObject() throws Exception {
    HttpUrl httpUrl = server.url("/");
    PrebidMobile.setApplicationContext(activity);
    Host.CUSTOM.setHostUrl(httpUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.oneBidFromRubicon()));
    MoPubView adView = new MoPubView(activity);
    adView.setAdUnitId("123456789");
    DemandFetcher demandFetcher = new DemandFetcher(adView);
    PrebidMobile.setTimeoutMillis(Integer.MAX_VALUE);
    demandFetcher.setPeriodMillis(0);
    HashSet<AdSize> sizes = new HashSet<>();
    sizes.add(new AdSize(300, 250));
    RequestParams requestParams = new RequestParams("12345", AdType.BANNER, sizes);
    demandFetcher.setRequestParams(requestParams);
    OnCompleteListener mockListener = mock(OnCompleteListener.class);
    demandFetcher.setListener(mockListener);
    assertEquals(DemandFetcher.STATE.STOPPED, FieldUtils.readField(demandFetcher, "state", true));
    demandFetcher.start();
    assertEquals(DemandFetcher.STATE.RUNNING, FieldUtils.readField(demandFetcher, "state", true));
    ShadowLooper fetcherLooper = Shadows.shadowOf(demandFetcher.getHandler().getLooper());
    fetcherLooper.runOneTask();
    ShadowLooper demandLooper = Shadows.shadowOf(demandFetcher.getDemandHandler().getLooper());
    demandLooper.runOneTask();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.SUCCESS);
    assertEquals(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
    String adViewKeywords = adView.getKeywords();
    assertEquals("hb_env:mobile-app,hb_cache_hostpath:https://prebid-cache-europe.rubiconproject.com/cache,hb_size_rubicon:300x250,hb_cache_id:a2f41588-4727-425c-9ef0-3b382debef1e,hb_cache_path_rubicon:/cache,hb_cache_host_rubicon:prebid-cache-europe.rubiconproject.com,hb_pb:1.20,hb_pb_rubicon:1.20,hb_cache_id_rubicon:a2f41588-4727-425c-9ef0-3b382debef1e,hb_cache_path:/cache,hb_size:300x250,hb_cache_hostpath_rubicon:https://prebid-cache-europe.rubiconproject.com/cache,hb_env_rubicon:mobile-app,hb_bidder:rubicon,hb_bidder_rubicon:rubicon,hb_cache_host:prebid-cache-europe.rubiconproject.com,", adViewKeywords);
}
 
Example 5
Source File: DemandFetcherTest.java    From prebid-mobile-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testAutoRefreshForMoPubAdObject() throws Exception {
    HttpUrl httpUrl = server.url("/");
    PrebidMobile.setApplicationContext(activity);
    Host.CUSTOM.setHostUrl(httpUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.oneBidFromAppNexus()));
    server.enqueue(new MockResponse().setResponseCode(200).setBody("{}"));
    MoPubView adView = new MoPubView(activity);
    adView.setAdUnitId("123456789");
    DemandFetcher demandFetcher = new DemandFetcher(adView);
    PrebidMobile.setTimeoutMillis(Integer.MAX_VALUE);
    demandFetcher.setPeriodMillis(2000);
    HashSet<AdSize> sizes = new HashSet<>();
    sizes.add(new AdSize(300, 250));
    RequestParams requestParams = new RequestParams("12345", AdType.BANNER, sizes);
    demandFetcher.setRequestParams(requestParams);
    OnCompleteListener mockListener = mock(OnCompleteListener.class);
    demandFetcher.setListener(mockListener);
    assertEquals(DemandFetcher.STATE.STOPPED, FieldUtils.readField(demandFetcher, "state", true));
    demandFetcher.start();
    assertEquals(DemandFetcher.STATE.RUNNING, FieldUtils.readField(demandFetcher, "state", true));
    ShadowLooper fetcherLooper = Shadows.shadowOf(demandFetcher.getHandler().getLooper());
    fetcherLooper.runOneTask();
    ShadowLooper demandLooper = Shadows.shadowOf(demandFetcher.getDemandHandler().getLooper());
    demandLooper.runOneTask();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.SUCCESS);
    assertNotSame(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
    String adViewKeywords = adView.getKeywords();
    assertEquals("hb_pb:0.50,hb_env:mobile-app,hb_pb_appnexus:0.50,hb_size:300x250,hb_bidder_appnexus:appnexus,hb_bidder:appnexus,hb_cache_id:df4aba04-5e69-44b8-8608-058ab21600b8,hb_env_appnexus:mobile-app,hb_size_appnexus:300x250,hb_cache_id_appnexus:df4aba04-5e69-44b8-8608-058ab21600b8,", adViewKeywords);
    fetcherLooper.runOneTask();
    demandLooper.runOneTask();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.NO_BIDS);
    assertNotSame(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
    adViewKeywords = adView.getKeywords();
    assertEquals("", adViewKeywords);
}
 
Example 6
Source File: MoPubAdapter.java    From googleads-mobile-android-mediation with Apache License 2.0 4 votes vote down vote up
@Override
public void requestBannerAd(final Context context,
    MediationBannerListener mediationBannerListener,
    Bundle bundle,
    final AdSize adSize,
    MediationAdRequest mediationAdRequest,
    final Bundle mediationExtras) {
  mContext = context;
  mAdSize = adSize;
  mExtras = mediationExtras;

  String adUnit = bundle.getString(MOPUB_AD_UNIT_KEY);
  if (TextUtils.isEmpty(adUnit)) {
    String errorMessage = createAdapterError(ERROR_INVALID_SERVER_PARAMETERS,
        "Missing or Invalid MoPub Ad Unit ID.");
    Log.w(TAG, errorMessage);
    mediationBannerListener.onAdFailedToLoad(MoPubAdapter.this,
        ERROR_INVALID_SERVER_PARAMETERS);
    return;
  }

  mMoPubView = new MoPubView(context);
  mMoPubView.setBannerAdListener(new MBannerListener(mediationBannerListener));
  mMoPubView.setAdUnitId(adUnit);

  //If test mode is enabled
  if (mediationAdRequest.isTesting()) {
    mMoPubView.setTesting(true);
  }

  //If location is available
  if (mediationAdRequest.getLocation() != null) {
    mMoPubView.setLocation(mediationAdRequest.getLocation());
  }

  mMoPubView.setKeywords(getKeywords(mediationAdRequest, false));
  mMoPubView.setUserDataKeywords(getKeywords(mediationAdRequest, true));

  SdkConfiguration sdkConfiguration = new SdkConfiguration.Builder(adUnit).build();
  MoPubSingleton.getInstance().initializeMoPubSDK(context, sdkConfiguration,
      new SdkInitializationListener() {
        @Override
        public void onInitializationFinished() {
          mMoPubView.loadAd();
        }
      });
}