@Override public void onLoad(NativeAd ad) { if (resources == null || ad == null || ad.getBanner() == null) { Log.d(TAG, "MyTargetAdmobCustomEventNative failed to load, resources or ad null"); if (customEventNativeListener != null) { customEventNativeListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INTERNAL_ERROR); } return; } String navigationType = ad.getBanner().getNavigationType(); NativeAdMapper nativeAdMapper = createNativeAdMapper(ad, navigationType); if (nativeAdMapper == null) { Log.d(TAG, "MyTargetAdmobCustomEventNative failed to load, unable to create ad mapper"); if (customEventNativeListener != null) { customEventNativeListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INTERNAL_ERROR); } return; } if (customEventNativeListener != null) { customEventNativeListener.onAdLoaded(nativeAdMapper); } }
@Nullable private NativeAdMapper createNativeAdMapper(@NonNull NativeAd ad, @NonNull String navigationType) { if (NavigationType.STORE.equals(navigationType) || NavigationType.DEEPLINK.equals(navigationType)) { if (!isAppInstallAdRequested) { Log.d(TAG, "MyTargetAdmobCustomEventNative failed to load: got banner with type " + navigationType + " but not allowed in AdMob request"); if (customEventNativeListener != null) { customEventNativeListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INVALID_REQUEST); } return null; } return new MyTargetNativeInstallAdMapper(ad, resources); } else { if (!isContentAdRequested) { Log.d(TAG, "MyTargetAdmobCustomEventNative failed to load: got banner with type " + navigationType + " but not allowed in AdMob request"); if (customEventNativeListener != null) { customEventNativeListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INVALID_REQUEST); } return null; } return new MyTargetNativeContentAdMapper(ad, resources); } }
/** * 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); } } }
private void mapAd(final @NonNull NativeAd nativeAd, final @NonNull NativePromoBanner banner) { if (nativeMediationAdRequest == null) { Log.d(TAG, "Failed to load: resources or nativeMediationAdRequest null"); if (customEventNativeListener != null) { customEventNativeListener.onAdFailedToLoad( MyTargetNativeAdapter.this, AdRequest.ERROR_CODE_INTERNAL_ERROR); } return; } NativeAdMapper nativeAdMapper; String navigationType = banner.getNavigationType(); if (NavigationType.STORE.equals(navigationType) || NavigationType.DEEPLINK.equals(navigationType)) { if (!nativeMediationAdRequest.isAppInstallAdRequested()) { Log.d(TAG, "No ad: AdMob request was without install ad flag, " + "but MyTarget responded with " + navigationType + " navigation type"); if (customEventNativeListener != null) { customEventNativeListener.onAdFailedToLoad( MyTargetNativeAdapter.this, AdRequest.ERROR_CODE_NO_FILL); } return; } if (banner.getTitle() == null || banner.getDescription() == null || banner.getImage() == null || banner.getIcon() == null || banner.getCtaText() == null) { Log.d(TAG, "No ad: Some of the Always Included assets are not available for " + "the ad"); if (customEventNativeListener != null) { customEventNativeListener.onAdFailedToLoad( MyTargetNativeAdapter.this, AdRequest.ERROR_CODE_NO_FILL); } return; } nativeAdMapper = new MyTargetNativeInstallAdMapper(nativeAd, context); } else { if (!nativeMediationAdRequest.isContentAdRequested()) { Log.d(TAG, "No ad: AdMob request was without content ad flag, " + "but MyTarget responded with " + navigationType + " navigation type"); if (customEventNativeListener != null) { customEventNativeListener.onAdFailedToLoad( MyTargetNativeAdapter.this, AdRequest.ERROR_CODE_NO_FILL); } return; } if (banner.getTitle() == null || banner.getDescription() == null || banner.getImage() == null || banner.getCtaText() == null || banner.getDomain() == null) { Log.d(TAG, "No ad: Some of the Always Included assets are not available for " + "the ad"); if (customEventNativeListener != null) { customEventNativeListener.onAdFailedToLoad( MyTargetNativeAdapter.this, AdRequest.ERROR_CODE_NO_FILL); } return; } nativeAdMapper = new MyTargetNativeContentAdMapper(nativeAd, context); } Log.d(TAG, "Ad loaded successfully"); if (customEventNativeListener != null) { customEventNativeListener.onAdLoaded(MyTargetNativeAdapter.this, nativeAdMapper); } }