Search in sources :

Example 16 with ResultCode

use of org.prebid.mobile.ResultCode in project prebid-mobile-android by prebid.

the class AdManagerComplexTest method testRubiconDFPBannerResizeSanityAppCheckTest.

// 30x250 -> 728x90
@Test
public void testRubiconDFPBannerResizeSanityAppCheckTest() throws Exception {
    final CountDownLatch lock = new CountDownLatch(1);
    PrebidMobile.setPrebidServerHost(Host.RUBICON);
    PrebidMobile.setPrebidServerAccountId(Constants.PBS_ACCOUNT_ID_RUBICON);
    PrebidMobile.setStoredAuctionResponse("1001-rubicon-300x250");
    TestActivity activity = testActivityRule.getActivity();
    final IntegerWrapper firstTransactionCount = new IntegerWrapper();
    final IntegerWrapper secondTransactionCount = new IntegerWrapper();
    final int transactionFailRepeatCount = 5;
    final int screenshotDelayMillis = 3_000;
    final int transactionFailDelayMillis = 3_000;
    final FrameLayout adFrame = activity.findViewById(R.id.adFrame);
    activity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            adFrame.removeAllViews();
        }
    });
    final AdManagerAdView dfpAdView = new AdManagerAdView(activity);
    // Programmatic fix
    dfpAdView.setAdUnitId("/5300653/test_adunit_pavliuchyk_300x250_puc_ucTagData_prebid-server.rubiconproject.com");
    dfpAdView.setAdSizes(new AdSize(300, 250));
    // Targeting creative
    /*
        dfpAdView.setAdUnitId("/5300653/Banner_PUC_b397711");
        dfpAdView.setAdSizes(new AdSize(300, 250), new AdSize(728, 90));
        */
    final BannerAdUnit bannerAdUnit = new BannerAdUnit(Constants.PBS_CONFIG_ID_300x250_RUBICON, 300, 250);
    final AdManagerAdRequest request = new AdManagerAdRequest.Builder().build();
    final OnCompleteListener completeListener = new OnCompleteListener() {

        @Override
        public void onComplete(ResultCode resultCode) {
            dfpAdView.loadAd(request);
        }
    };
    dfpAdView.setAdListener(new AdListener() {

        private void notifyResult() {
            lock.countDown();
        }

        private void update(boolean isSuccess) {
            if (isSuccess) {
                if (firstTransactionCount.value != -1) {
                    firstTransactionCount.value = -1;
                    PrebidMobile.setStoredAuctionResponse("1001-rubicon-300x50");
                    bannerAdUnit.fetchDemand(request, completeListener);
                } else if (secondTransactionCount.value != -1) {
                    secondTransactionCount.value = -1;
                    notifyResult();
                }
            } else {
                try {
                    Thread.sleep(transactionFailDelayMillis);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (firstTransactionCount.value != -1) {
                    if (firstTransactionCount.value > transactionFailRepeatCount - 2) {
                        fail("first Transaction Count == " + transactionFailRepeatCount);
                    } else {
                        // repeat
                        firstTransactionCount.value++;
                        bannerAdUnit.fetchDemand(request, completeListener);
                    }
                } else if (secondTransactionCount.value != -1) {
                    if (secondTransactionCount.value > transactionFailRepeatCount - 2) {
                        fail("second Transaction Count == " + transactionFailRepeatCount);
                    } else {
                        // repeat
                        secondTransactionCount.value++;
                        bannerAdUnit.fetchDemand(request, completeListener);
                    }
                } else {
                    fail("Unexpected");
                }
            }
        }

        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            // Programmatic fix
            AdViewUtils.findPrebidCreativeSize(dfpAdView, new AdViewUtils.PbFindSizeListener() {

                @Override
                public void success(final int width, final int height) {
                    dfpAdView.setAdSizes(new AdSize(width, height));
                    final View child = dfpAdView.getChildAt(0);
                    child.setBackgroundColor(Color.RED);
                    dfpAdView.post(new Runnable() {

                        @Override
                        public void run() {
                            float density = dfpAdView.getResources().getDisplayMetrics().density;
                            double dpW = Math.ceil(child.getMinimumWidth() / density);
                            double dpH = Math.ceil(child.getMinimumHeight() / density);
                            try {
                                Thread.sleep(screenshotDelayMillis);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            assertEquals((int) dpW, width);
                            assertEquals((int) dpH, height);
                            update(true);
                        }
                    });
                }

                @Override
                public void failure(PbFindSizeError error) {
                    LogUtil.w("failure:" + error.getDescription());
                    update(false);
                }
            });
        // Targeting creative
        /*
                try {
                    Thread.sleep(screenshotDelayMillis);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                update(true);
                */
        }

        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
            LogUtil.w("onAdFailedToLoad:" + loadAdError);
            update(false);
        }
    });
    activity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            adFrame.addView(dfpAdView);
        }
    });
    bannerAdUnit.fetchDemand(request, completeListener);
    // TravisCI fix
    Thread.sleep(2 * transactionFailRepeatCount * transactionFailDelayMillis + 2 * screenshotDelayMillis);
    try {
        Thread.sleep(screenshotDelayMillis);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertEquals(-1, firstTransactionCount.value);
    assertEquals(-1, secondTransactionCount.value);
}
Also used : PbFindSizeError(org.prebid.mobile.addendum.PbFindSizeError) LoadAdError(com.google.android.gms.ads.LoadAdError) CountDownLatch(java.util.concurrent.CountDownLatch) View(android.view.View) AdManagerAdView(com.google.android.gms.ads.admanager.AdManagerAdView) BannerAdUnit(org.prebid.mobile.BannerAdUnit) AdListener(com.google.android.gms.ads.AdListener) OnCompleteListener(org.prebid.mobile.OnCompleteListener) AdManagerAdView(com.google.android.gms.ads.admanager.AdManagerAdView) FrameLayout(android.widget.FrameLayout) AdSize(com.google.android.gms.ads.AdSize) AdManagerAdRequest(com.google.android.gms.ads.admanager.AdManagerAdRequest) ResultCode(org.prebid.mobile.ResultCode) Test(org.junit.Test)

Example 17 with ResultCode

use of org.prebid.mobile.ResultCode in project prebid-mobile-android by prebid.

the class XandrInterstitialGamDemoActivity 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 InterstitialAdUnit(Constants.PBS_CONFIG_ID_INTERSTITIAL_APPNEXUS);
    int millis = getIntent().getIntExtra(Constants.AUTO_REFRESH_NAME, 0);
    adUnit.setAutoRefreshPeriodMillis(millis);
    final AdManagerAdRequest.Builder builder = new AdManagerAdRequest.Builder();
    adUnit.fetchDemand(builder, new OnCompleteListener() {

        @Override
        public void onComplete(ResultCode resultCode) {
            AdManagerAdRequest request = builder.build();
            AdManagerInterstitialAd.load(XandrInterstitialGamDemoActivity.this, Constants.DFP_INTERSTITIAL_ADUNIT_ID_APPNEXUS, request, new AdManagerInterstitialAdLoadCallback() {

                @Override
                public void onAdLoaded(@NonNull AdManagerInterstitialAd adManagerInterstitialAd) {
                    super.onAdLoaded(adManagerInterstitialAd);
                    adManagerInterstitialAd.show(XandrInterstitialGamDemoActivity.this);
                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                    super.onAdFailedToLoad(loadAdError);
                    AlertDialog.Builder builder;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        builder = new AlertDialog.Builder(XandrInterstitialGamDemoActivity.this, android.R.style.Theme_Material_Dialog_Alert);
                    } else {
                        builder = new AlertDialog.Builder(XandrInterstitialGamDemoActivity.this);
                    }
                    builder.setTitle("Failed to load AdManager interstitial ad").setMessage("Error: " + loadAdError.toString()).setIcon(android.R.drawable.ic_dialog_alert).show();
                }
            });
        }
    });
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) LoadAdError(com.google.android.gms.ads.LoadAdError) AdManagerInterstitialAdLoadCallback(com.google.android.gms.ads.admanager.AdManagerInterstitialAdLoadCallback) AdManagerInterstitialAd(com.google.android.gms.ads.admanager.AdManagerInterstitialAd) OnCompleteListener(org.prebid.mobile.OnCompleteListener) NonNull(androidx.annotation.NonNull) AdManagerAdRequest(com.google.android.gms.ads.admanager.AdManagerAdRequest) ResultCode(org.prebid.mobile.ResultCode) InterstitialAdUnit(org.prebid.mobile.InterstitialAdUnit)

Example 18 with ResultCode

use of org.prebid.mobile.ResultCode in project prebid-mobile-android by prebid.

the class RubiconInterstitialGamDemoActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_demo);
    PrebidMobile.setPrebidServerHost(Host.RUBICON);
    PrebidMobile.setPrebidServerAccountId(Constants.PBS_ACCOUNT_ID_RUBICON);
    PrebidMobile.setStoredAuctionResponse(Constants.PBS_STORED_RESPONSE_300x250_RUBICON);
    adUnit = new InterstitialAdUnit(Constants.PBS_CONFIG_ID_INTERSTITIAL_RUBICON);
    int millis = getIntent().getIntExtra(Constants.AUTO_REFRESH_NAME, 0);
    adUnit.setAutoRefreshPeriodMillis(millis);
    final AdManagerAdRequest.Builder builder = new AdManagerAdRequest.Builder();
    adUnit.fetchDemand(builder, new OnCompleteListener() {

        @Override
        public void onComplete(ResultCode resultCode) {
            AdManagerAdRequest request = builder.build();
            AdManagerInterstitialAd.load(RubiconInterstitialGamDemoActivity.this, Constants.DFP_INTERSTITIAL_ADUNIT_ID_RUBICON, request, new AdManagerInterstitialAdLoadCallback() {

                @Override
                public void onAdLoaded(@NonNull AdManagerInterstitialAd adManagerInterstitialAd) {
                    super.onAdLoaded(adManagerInterstitialAd);
                    adManagerInterstitialAd.show(RubiconInterstitialGamDemoActivity.this);
                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                    super.onAdFailedToLoad(loadAdError);
                    AlertDialog.Builder builder;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        builder = new AlertDialog.Builder(RubiconInterstitialGamDemoActivity.this, android.R.style.Theme_Material_Dialog_Alert);
                    } else {
                        builder = new AlertDialog.Builder(RubiconInterstitialGamDemoActivity.this);
                    }
                    builder.setTitle("Failed to load AdManager interstitial ad").setMessage("Error: " + loadAdError.toString()).setIcon(android.R.drawable.ic_dialog_alert).show();
                }
            });
            refreshCount++;
            RubiconInterstitialGamDemoActivity.this.resultCode = resultCode;
        }
    });
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) LoadAdError(com.google.android.gms.ads.LoadAdError) AdManagerInterstitialAdLoadCallback(com.google.android.gms.ads.admanager.AdManagerInterstitialAdLoadCallback) AdManagerInterstitialAd(com.google.android.gms.ads.admanager.AdManagerInterstitialAd) OnCompleteListener(org.prebid.mobile.OnCompleteListener) NonNull(androidx.annotation.NonNull) AdManagerAdRequest(com.google.android.gms.ads.admanager.AdManagerAdRequest) ResultCode(org.prebid.mobile.ResultCode) InterstitialAdUnit(org.prebid.mobile.InterstitialAdUnit)

Example 19 with ResultCode

use of org.prebid.mobile.ResultCode in project prebid-mobile-android by prebid.

the class XandrInterstitialMoPubDemoActivity 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 InterstitialAdUnit(Constants.PBS_CONFIG_ID_INTERSTITIAL_APPNEXUS);
    final MoPubInterstitial mpInterstitial = new MoPubInterstitial(this, Constants.MOPUB_INTERSTITIAL_ADUNIT_ID_APPNEXUS);
    mpInterstitial.setInterstitialAdListener(new MoPubInterstitial.InterstitialAdListener() {

        @Override
        public void onInterstitialLoaded(MoPubInterstitial interstitial) {
            interstitial.show();
        }

        @Override
        public void onInterstitialFailed(MoPubInterstitial interstitial, MoPubErrorCode errorCode) {
            AlertDialog.Builder builder;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                builder = new AlertDialog.Builder(XandrInterstitialMoPubDemoActivity.this, android.R.style.Theme_Material_Dialog_Alert);
            } else {
                builder = new AlertDialog.Builder(XandrInterstitialMoPubDemoActivity.this);
            }
            builder.setTitle("Failed to load MoPub interstitial ad").setMessage("Error code: " + errorCode.toString()).setIcon(android.R.drawable.ic_dialog_alert).show();
        }

        @Override
        public void onInterstitialShown(MoPubInterstitial interstitial) {
        }

        @Override
        public void onInterstitialClicked(MoPubInterstitial interstitial) {
        }

        @Override
        public void onInterstitialDismissed(MoPubInterstitial interstitial) {
        }
    });
    int millis = getIntent().getIntExtra(Constants.AUTO_REFRESH_NAME, 0);
    adUnit.setAutoRefreshPeriodMillis(millis);
    adUnit.fetchDemand(mpInterstitial, new OnCompleteListener() {

        @Override
        public void onComplete(ResultCode resultCode) {
            mpInterstitial.load();
        }
    });
}
Also used : MoPubErrorCode(com.mopub.mobileads.MoPubErrorCode) OnCompleteListener(org.prebid.mobile.OnCompleteListener) MoPubInterstitial(com.mopub.mobileads.MoPubInterstitial) ResultCode(org.prebid.mobile.ResultCode) InterstitialAdUnit(org.prebid.mobile.InterstitialAdUnit)

Example 20 with ResultCode

use of org.prebid.mobile.ResultCode in project prebid-mobile-android by prebid.

the class XandrNativeInAppMoPubDemoActivity method loadInAppNativeMopub.

// Mopub
private void loadInAppNativeMopub() {
    {
        removePreviousAds();
        PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
        PrebidMobile.setPrebidServerAccountId(Constants.PBS_ACCOUNT_ID_APPNEXUS);
        final NativeAdUnit nativeAdUnit = new NativeAdUnit(Constants.PBS_CONFIG_ID_NATIVE_APPNEXUS);
        nativeAdUnit.setContextType(NativeAdUnit.CONTEXT_TYPE.SOCIAL_CENTRIC);
        nativeAdUnit.setPlacementType(NativeAdUnit.PLACEMENTTYPE.CONTENT_FEED);
        nativeAdUnit.setContextSubType(NativeAdUnit.CONTEXTSUBTYPE.GENERAL_SOCIAL);
        adUnit = nativeAdUnit;
        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);
        moPubNative = new MoPubNative(XandrNativeInAppMoPubDemoActivity.this, "2674981035164b2db5ef4b4546bf3d49", new MoPubNative.MoPubNativeNetworkListener() {

            @Override
            public void onNativeLoad(final NativeAd nativeAd) {
                Log.d("Prebid", "MoPub native ad loaded");
                XandrNativeInAppMoPubDemoActivity.this.ad = nativeAd;
                AdViewUtils.findNative(nativeAd, new PrebidNativeAdListener() {

                    @Override
                    public void onPrebidNativeLoaded(final PrebidNativeAd ad) {
                        inflatePrebidNativeAd(ad);
                    }

                    @Override
                    public void onPrebidNativeNotFound() {
                        inflateMoPubNativeAd(nativeAd);
                    }

                    @Override
                    public void onPrebidNativeNotValid() {
                        Log.e("ERROR", "onPrebidNativeNotValid");
                    // should not show the NativeAd on the screen, do something else
                    }
                });
            }

            @Override
            public void onNativeFail(NativeErrorCode errorCode) {
                Log.d("Prebid", "MoPub native failed to load: " + errorCode.toString());
            }
        });
        moPubNative.registerAdRenderer(new MoPubStaticNativeAdRenderer(null));
        RequestParameters.Builder mRP = new RequestParameters.Builder();
        // Fetching the demannd using OnCompleteListener
        nativeAdUnit.fetchDemand(mRP, new OnCompleteListener() {

            @Override
            public void onComplete(ResultCode resultCode) {
                if (resultCode == ResultCode.SUCCESS) {
                    moPubNative.makeRequest(mRP.build());
                } else {
                    Toast.makeText(XandrNativeInAppMoPubDemoActivity.this, "Native Ad Unit: " + resultCode.name(), Toast.LENGTH_SHORT).show();
                }
                refreshCount++;
                XandrNativeInAppMoPubDemoActivity.this.resultCode = resultCode;
                request = mRP.build();
            }
        });
    // ================================================================================
    // SAMPLE CODE: Fetching the demand using OnCompleteListener2
    // ================================================================================
    /*
            nativeAdUnit.fetchDemand(new OnCompleteListener2() {
                @Override
                public void onComplete(ResultCode resultCode, Map<String, String> unmodifiableMap) {
                    Log.e("MAP", unmodifiableMap.toString());
                    if (resultCode == ResultCode.SUCCESS) {
                        String keywords = "";
                        for (String key: unmodifiableMap.keySet()) {
                            keywords += key + ":" + unmodifiableMap.get(key) + ",";
                        }
                        // removing last ","
                        keywords = keywords.substring(0, keywords.length()-1);
                        RequestParameters mRP = new RequestParameters.Builder().keywords(keywords).build();
                        Log.d("Prebid", mRP.getKeywords());
                        mMoPubNative.makeRequest(mRP);
                    }
                    Toast.makeText(XandrNativeInAppMoPubDemoActivity.this, "Native Ad Unit: " + resultCode.name(), Toast.LENGTH_SHORT).show();
                }
            });
            */
    // ================================================================================
    // SAMPLE CODE: END
    // ================================================================================
    }
}
Also used : MoPubNative(com.mopub.nativeads.MoPubNative) ArrayList(java.util.ArrayList) NativeTitleAsset(org.prebid.mobile.NativeTitleAsset) NativeDataAsset(org.prebid.mobile.NativeDataAsset) NativeAdUnit(org.prebid.mobile.NativeAdUnit) NativeErrorCode(com.mopub.nativeads.NativeErrorCode) MoPubStaticNativeAdRenderer(com.mopub.nativeads.MoPubStaticNativeAdRenderer) RequestParameters(com.mopub.nativeads.RequestParameters) OnCompleteListener(org.prebid.mobile.OnCompleteListener) StaticNativeAd(com.mopub.nativeads.StaticNativeAd) PrebidNativeAd(org.prebid.mobile.PrebidNativeAd) NativeAd(com.mopub.nativeads.NativeAd) NativeImageAsset(org.prebid.mobile.NativeImageAsset) NativeEventTracker(org.prebid.mobile.NativeEventTracker) PrebidNativeAdListener(org.prebid.mobile.PrebidNativeAdListener) PrebidNativeAd(org.prebid.mobile.PrebidNativeAd) ResultCode(org.prebid.mobile.ResultCode)

Aggregations

ResultCode (org.prebid.mobile.ResultCode)42 OnCompleteListener (org.prebid.mobile.OnCompleteListener)41 BannerAdUnit (org.prebid.mobile.BannerAdUnit)29 FrameLayout (android.widget.FrameLayout)27 Test (org.junit.Test)25 MoPubView (com.mopub.mobileads.MoPubView)23 AdManagerAdRequest (com.google.android.gms.ads.admanager.AdManagerAdRequest)14 MockResponse (okhttp3.mockwebserver.MockResponse)9 NonNull (androidx.annotation.NonNull)7 ArrayList (java.util.ArrayList)7 Matchers.containsString (org.hamcrest.Matchers.containsString)7 AdUnit (org.prebid.mobile.AdUnit)7 InterstitialAdUnit (org.prebid.mobile.InterstitialAdUnit)7 AdManagerAdView (com.google.android.gms.ads.admanager.AdManagerAdView)6 Dispatcher (okhttp3.mockwebserver.Dispatcher)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)6 AdListener (com.google.android.gms.ads.AdListener)5 LoadAdError (com.google.android.gms.ads.LoadAdError)5 VideoBaseAdUnit (org.prebid.mobile.VideoBaseAdUnit)5 AdSize (com.google.android.gms.ads.AdSize)4