/** * 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); } } }
@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(); }
@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); }
/** * Creates a new {@code SampleNativeEventForwarder}. * * @param listener An AdMob Mediation {@link CustomEventNativeListener} that should receive * forwarded events. */ public SampleCustomNativeEventForwarder(CustomEventNativeListener listener, NativeAdOptions adOptions) { this.mNativeListener = listener; this.mNativeAdOptions = adOptions; }