Java 类com.google.android.gms.ads.mediation.NativeMediationAdRequest 实例源码

项目:googleads-mobile-android-mediation    文件:MyTargetNativeAdapter.java   
MyTargetNativeAdListener(final @NonNull NativeAd nativeAd,
                         final @Nullable NativeMediationAdRequest nativeMediationAdRequest,
                         final @NonNull Context context) {
    this.nativeAd = nativeAd;
    this.nativeMediationAdRequest = nativeMediationAdRequest;
    this.context = context;
}
项目:googleads-mobile-android-mediation    文件:FacebookAdapter.java   
@Override
public void requestNativeAd(Context context,
                            MediationNativeListener listener,
                            Bundle serverParameters,
                            NativeMediationAdRequest mediationAdRequest,
                            Bundle mediationExtras) {
    mNativeListener = listener;
    if (!isValidRequestParameters(context, serverParameters)) {
        mNativeListener.onAdFailedToLoad(this, AdRequest.ERROR_CODE_INVALID_REQUEST);
        return;
    }

    // Verify that the request is for both app install and content ads.
    if (!(mediationAdRequest.isAppInstallAdRequested()
            && mediationAdRequest.isContentAdRequested())) {
        Log.w(TAG, "Failed to request native ad. Both app install and content ad should be "
                + "requested");
        mNativeListener.onAdFailedToLoad(this, AdRequest.ERROR_CODE_INVALID_REQUEST);
        return;
    }

    String placementId = serverParameters.getString(PLACEMENT_PARAMETER);

    // Get the optional extras if set by the publisher.
    if (mediationExtras != null) {
        mIsAdChoicesIconExpandable = mediationExtras.getBoolean(
                FacebookExtrasBundleBuilder.KEY_EXPANDABLE_ICON, true);
    }

    mMediaView = new MediaView(context);

    mNativeAd = new NativeAd(context, placementId);
    mNativeAd.setAdListener(new NativeListener(mNativeAd, mediationAdRequest));
    buildAdRequest(mediationAdRequest);
    mNativeAd.loadAd();
}
项目:SAS-DFP-Adapter-Android    文件:SASCustomEventNative.java   
/**
 * Implementation of CustomEventNative interface.
 * Delegates the banner ad call to Smart AdServer SDK
 */
@Override
public void requestNativeAd(final Context context, final CustomEventNativeListener customEventNativeListener, String s, NativeMediationAdRequest nativeMediationAdRequest, Bundle bundle) {
    // get smart placement object
    SASCustomEventUtil.SASAdPlacement adPlacement = SASCustomEventUtil.getPlacementFromString(s,nativeMediationAdRequest);

    if (adPlacement == null) {
        // incorrect smart placement : exit in error
        customEventNativeListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INVALID_REQUEST);
    } else {
        if (sasNativeAdManager == null) {
            // instantiate the AdResponseHandler to handle Smart ad call outcome
            nativeAdResponseHandler = new SASNativeAdManager.NativeAdResponseHandler() {
                @Override
                public void nativeAdLoadingCompleted(SASNativeAdElement nativeAdElement) {

                    // TODO native ad mapper
                    NativeAdMapper adMapper = new NativeAdMapper() {
                    };
                    customEventNativeListener.onAdLoaded(adMapper);

                    nativeAdElement.setClickHandler(new SASNativeAdElement.ClickHandler() {

                        @Override
                        public boolean handleClick(String clickUrl, SASNativeAdElement nativeAdElement) {
                            customEventNativeListener.onAdClicked();
                            customEventNativeListener.onAdOpened();
                            customEventNativeListener.onAdLeftApplication();
                            return false;
                        }
                    });
                }

                @Override
                public void nativeAdLoadingFailed(Exception e) {
                    int errorCode = AdRequest.ERROR_CODE_INTERNAL_ERROR;
                    if (e instanceof SASNoAdToDeliverException) {
                        // no ad to deliver
                        errorCode = AdRequest.ERROR_CODE_NO_FILL;
                    } else if (e instanceof SASAdTimeoutException) {
                        // ad request timeout translates to admob network error
                        errorCode = AdRequest.ERROR_CODE_NETWORK_ERROR;
                    }
                    customEventNativeListener.onAdFailedToLoad(errorCode);
                }
            };

            SASNativeAdPlacement nativeAdPlacement = new SASNativeAdPlacement(SASConstants.DEFAULT_BASE_URL,
                    adPlacement.siteId,adPlacement.pageId,adPlacement.formatId,adPlacement.targeting);

            // instantiate SASNativeAdManager that will perform the Smart ad call
            sasNativeAdManager = new SASNativeAdManager(context,nativeAdPlacement);


            // pass received location on to SASNativeAdManager
            sasNativeAdManager.setLocation(nativeMediationAdRequest.getLocation());

            // Now request ad for this SASNativeAdManager
            sasNativeAdManager.requestNativeAd(nativeAdResponseHandler,10000);
        }
    }
}
项目:mytarget-android    文件:MyTargetAdmobCustomEventNative.java   
@Override
public void requestNativeAd(@Nullable Context context,
                            @Nullable CustomEventNativeListener customEventNativeListener,
                            @Nullable String serverParameter,
                            @Nullable NativeMediationAdRequest nativeMediationAdRequest,
                            @Nullable
                                    Bundle customEventExtras)
{
    if (context == null)
    {
        Log.d(TAG, "unable to request native ad, context is null");
        if (customEventNativeListener != null)
        {
            customEventNativeListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INTERNAL_ERROR);
        }
        return;
    }

    this.resources = context.getResources();

    this.customEventNativeListener = customEventNativeListener;

    NativeAdOptions options = null;
    int gender = 0;
    Date birthday = null;
    if (nativeMediationAdRequest != null)
    {
        options = nativeMediationAdRequest.getNativeAdOptions();
        isAppInstallAdRequested = nativeMediationAdRequest.isAppInstallAdRequested();
        isContentAdRequested = nativeMediationAdRequest.isContentAdRequested();
        gender = nativeMediationAdRequest.getGender();
        birthday = nativeMediationAdRequest.getBirthday();
    }

    int slotId;
    try
    {
        JSONObject json = new JSONObject(serverParameter);
        slotId = json.getInt(SLOT_ID_KEY);
    } catch (Exception e)
    {
        Log.i(TAG, "Unable to get slotId from parameter json. Probably Admob mediation misconfiguration.");
        if (customEventNativeListener != null)
        {
            customEventNativeListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INTERNAL_ERROR);
        }
        return;
    }

    NativeAd nativeAd = new NativeAd(slotId, context.getApplicationContext());

    if (options != null)
    {
        nativeAd.setAutoLoadImages(!options.shouldReturnUrlsForImageAssets());
    }

    nativeAd.getCustomParams().setGender(gender);

    if (birthday != null && birthday.getTime() != -1)
    {
        GregorianCalendar calendar = new GregorianCalendar();
        GregorianCalendar calendarNow = new GregorianCalendar();

        calendar.setTimeInMillis(birthday.getTime());
        int a = calendarNow.get(GregorianCalendar.YEAR) - calendar.get(GregorianCalendar.YEAR);
        if (a >= 0)
        {
            nativeAd.getCustomParams().setAge(a);
        }
    }

    nativeAd.getCustomParams().setCustomParam("mediation", "1");
    nativeAd.setListener(nativeAdListener);
    nativeAd.load();
}
项目:googleads-mobile-android-mediation    文件:SampleCustomEvent.java   
@Override
public void requestNativeAd(Context context,
                            CustomEventNativeListener customEventNativeListener,
                            String serverParameter,
                            NativeMediationAdRequest nativeMediationAdRequest,
                            Bundle extras) {
    // Create one of the Sample SDK's ad loaders from which to request ads.
    SampleNativeAdLoader loader = new SampleNativeAdLoader(context);
    loader.setAdUnit(serverParameter);

    // Create a native request to give to the SampleNativeAdLoader.
    SampleNativeAdRequest request = new SampleNativeAdRequest();

    // The Google Mobile Ads SDK requires the image assets to be downloaded automatically unless
    // the publisher specifies otherwise by using the NativeAdOptions object's
    // shouldReturnUrlsForImageAssets method. If your network doesn't have an option like this
    // and instead only ever returns URLs for images (rather than the images themselves), your
    // adapter should download image assets on behalf of the publisher. See the
    // SampleNativeMediationEventForwarder for information on how to do so.
    request.setShouldDownloadImages(true);

    request.setShouldDownloadMultipleImages(false);
    request.setPreferredImageOrientation(SampleNativeAdRequest.IMAGE_ORIENTATION_ANY);

    NativeAdOptions options = nativeMediationAdRequest.getNativeAdOptions();

    if (options != null) {
        // If the NativeAdOptions' shouldReturnUrlsForImageAssets is true, the adapter should
        // send just the URLs for the images.
        request.setShouldDownloadImages(!options.shouldReturnUrlsForImageAssets());

        // If your network does not support any of the following options, please make sure
        // that it is documented in your adapter's documentation.
        request.setShouldDownloadMultipleImages(options.shouldRequestMultipleImages());
        switch (options.getImageOrientation()) {
            case NativeAdOptions.ORIENTATION_LANDSCAPE:
                request.setPreferredImageOrientation(
                        SampleNativeAdRequest.IMAGE_ORIENTATION_LANDSCAPE);
                break;
            case NativeAdOptions.ORIENTATION_PORTRAIT:
                request.setPreferredImageOrientation(
                        SampleNativeAdRequest.IMAGE_ORIENTATION_PORTRAIT);
                break;
            case NativeAdOptions.ORIENTATION_ANY:
            default:
                request.setPreferredImageOrientation(
                        SampleNativeAdRequest.IMAGE_ORIENTATION_ANY);
        }
    }

    // Set App Install and Content Ad requests.
    //
    // NOTE: Care needs to be taken to make sure the custom event respects the publisher's
    // wishes in regard to native ad formats. For example, if the mediated ad network only
    // provides app install ads, and the publisher requests content ads alone, the custom event
    // must report an error by calling the listener's onAdFailedToLoad method with an error code
    // of AdRequest.ERROR_CODE_INVALID_REQUEST. It should *not* request an app install ad
    // anyway, and then attempt to map it to the content ad format.
    if (!nativeMediationAdRequest.isAppInstallAdRequested()
            && !nativeMediationAdRequest.isContentAdRequested()) {
        customEventNativeListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INVALID_REQUEST);
        return;
    }
    request.setAppInstallAdsRequested(nativeMediationAdRequest.isAppInstallAdRequested());
    request.setContentAdsRequested(nativeMediationAdRequest.isContentAdRequested());

    loader.setNativeAdListener(
            new SampleCustomNativeEventForwarder(customEventNativeListener, options));

    // Begin a request.
    loader.fetchAd(request);
}
项目:googleads-mobile-android-mediation    文件:MyTargetNativeAdapter.java   
@Override
public void requestNativeAd(Context context,
                            MediationNativeListener customEventNativeListener,
                            Bundle serverParameter,
                            NativeMediationAdRequest nativeMediationAdRequest,
                            Bundle customEventExtras) {
    this.customEventNativeListener = customEventNativeListener;
    int slotId = MyTargetTools.checkAndGetSlotId(context, serverParameter);
    Log.d(TAG, "Requesting myTarget mediation, slotId: " + slotId);

    if (slotId < 0) {
        if (customEventNativeListener != null) {
            customEventNativeListener.onAdFailedToLoad(
                    MyTargetNativeAdapter.this, AdRequest.ERROR_CODE_INVALID_REQUEST);
        }
        return;
    }
    NativeAdOptions options = null;
    int gender = 0;
    Date birthday = null;
    boolean contentRequested = false;
    boolean installRequested = false;
    if (nativeMediationAdRequest != null) {
        options = nativeMediationAdRequest.getNativeAdOptions();
        gender = nativeMediationAdRequest.getGender();
        birthday = nativeMediationAdRequest.getBirthday();
        contentRequested = nativeMediationAdRequest.isContentAdRequested();
        installRequested = nativeMediationAdRequest.isAppInstallAdRequested();
    }

    NativeAd nativeAd = new NativeAd(slotId, context);

    boolean autoLoadImages = true;
    if (options != null) {
        autoLoadImages = !options.shouldReturnUrlsForImageAssets();
        Log.d(TAG, "Set autoload images to " + autoLoadImages);
    }
    nativeAd.setAutoLoadImages(autoLoadImages);

    CustomParams params = nativeAd.getCustomParams();
    Log.d(TAG, "Set gender to " + gender);
    params.setGender(gender);

    if (birthday != null && birthday.getTime() != -1) {
        GregorianCalendar calendar = new GregorianCalendar();
        GregorianCalendar calendarNow = new GregorianCalendar();

        calendar.setTimeInMillis(birthday.getTime());
        int age = calendarNow.get(GregorianCalendar.YEAR)
                - calendar.get(GregorianCalendar.YEAR);
        if (age >= 0) {
            params.setAge(age);
        }
    }
    Log.d(TAG, "Content requested: " + contentRequested
            + ", install requested: " + installRequested);
    if (!contentRequested || !installRequested) {
        if (!contentRequested) {
            params.setCustomParam(PARAM_NATIVE_TYPE_REQUEST, PARAM_INSTALL_ONLY);
        } else {
            params.setCustomParam(PARAM_NATIVE_TYPE_REQUEST, PARAM_CONTENT_ONLY);
        }
    }

    MyTargetNativeAdListener nativeAdListener =
            new MyTargetNativeAdListener(nativeAd, nativeMediationAdRequest, context);

    params.setCustomParam(
            MyTargetTools.PARAM_MEDIATION_KEY, MyTargetTools.PARAM_MEDIATION_VALUE);
    nativeAd.setListener(nativeAdListener);
    nativeAd.load();
}
项目:googleads-mobile-android-mediation    文件:FacebookAdapter.java   
private NativeListener(NativeAd nativeAd, NativeMediationAdRequest mediationAdRequest) {
    mNativeAd = nativeAd;
    mMediationAdRequest = mediationAdRequest;
}