@Override public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { if (viewHolder==null) return; int itemViewType = viewHolder.getItemViewType(); if(itemViewType == getViewTypeAdInstall()) { NativeAppInstallAdView lvi1 = (NativeAppInstallAdView) viewHolder.itemView; NativeAppInstallAd ad1 = (NativeAppInstallAd) getItem(position); getInstallAdsLayoutContext().bind(lvi1, ad1); } else if(itemViewType == getViewTypeAdContent()) { NativeContentAdView lvi2 = (NativeContentAdView) viewHolder.itemView; NativeContentAd ad2 = (NativeContentAd) getItem(position); getContentAdsLayoutContext().bind(lvi2, ad2); } else{ int origPos = AdapterCalculator.getOriginalContentPosition(position, adFetcher.getFetchedAdsCount(), mAdapter.getItemCount()); mAdapter.onBindViewHolder(viewHolder, origPos); } }
/** * Populates the asset {@link View}s contained it the {@link NativeAppInstallAdView} with data * from the {@link NativeAppInstallAd} object. This method is invoked when an * {@link AppInstallAdFetcher} has successfully loaded a {@link NativeAppInstallAd}. * * @param appInstallAd the ad that is to be displayed */ public void populateView(NativeAppInstallAd appInstallAd) { ((TextView) mAdView.getHeadlineView()).setText(appInstallAd.getHeadline()); ((TextView) mAdView.getBodyView()).setText(appInstallAd.getBody()); ((TextView) mAdView.getPriceView()).setText(appInstallAd.getPrice()); ((TextView) mAdView.getStoreView()).setText(appInstallAd.getStore()); ((Button) mAdView.getCallToActionView()).setText(appInstallAd.getCallToAction()); ((ImageView) mAdView.getIconView()).setImageDrawable(appInstallAd.getIcon().getDrawable()); ((RatingBar) mAdView.getStarRatingView()) .setRating(appInstallAd.getStarRating().floatValue()); List<NativeAd.Image> images = appInstallAd.getImages(); if (images.size() > 0) { ((ImageView) mAdView.getImageView()) .setImageDrawable(images.get(0).getDrawable()); } // assign native ad object to the native view and make visible mAdView.setNativeAd(appInstallAd); mAdView.setVisibility(View.VISIBLE); }
/** * Determines the view type for the given position. */ @Override public int getItemViewType(int position) { Object recyclerViewItem = mRecyclerViewItems.get(position); if (recyclerViewItem instanceof NativeAppInstallAd) { return NATIVE_APP_INSTALL_AD_VIEW_TYPE; } else if (recyclerViewItem instanceof NativeContentAd) { return NATIVE_CONTENT_AD_VIEW_TYPE; } return MENU_ITEM_VIEW_TYPE; }
/** * Replaces the content in the views that make up the menu item view and the * Native Express ad view. This method is invoked by the layout manager. */ @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { int viewType = getItemViewType(position); switch (viewType) { case NATIVE_APP_INSTALL_AD_VIEW_TYPE: NativeAppInstallAd appInstallAd = (NativeAppInstallAd) mRecyclerViewItems.get(position); populateAppInstallAdView(appInstallAd, (NativeAppInstallAdView) holder.itemView); break; case NATIVE_CONTENT_AD_VIEW_TYPE: NativeContentAd contentAd = (NativeContentAd) mRecyclerViewItems.get(position); populateContentAdView(contentAd, (NativeContentAdView) holder.itemView); break; case MENU_ITEM_VIEW_TYPE: // fall through default: MenuItemViewHolder menuItemHolder = (MenuItemViewHolder) holder; MenuItem menuItem = (MenuItem) mRecyclerViewItems.get(position); // Get the menu item image resource ID. String imageName = menuItem.getImageName(); int imageResID = mContext.getResources().getIdentifier(imageName, "drawable", mContext.getPackageName()); // Add the menu item details to the menu item view. menuItemHolder.menuItemImage.setImageResource(imageResID); menuItemHolder.menuItemName.setText(menuItem.getName()); menuItemHolder.menuItemPrice.setText(menuItem.getPrice()); menuItemHolder.menuItemCategory.setText(menuItem.getCategory()); menuItemHolder.menuItemDescription.setText(menuItem.getDescription()); } }
private void populateAppInstallAdView(NativeAppInstallAd nativeAppInstallAd, NativeAppInstallAdView adView) { // Some assets are guaranteed to be in every NativeAppInstallAd. ((ImageView) adView.getIconView()).setImageDrawable(nativeAppInstallAd.getIcon() .getDrawable()); ((TextView) adView.getHeadlineView()).setText(nativeAppInstallAd.getHeadline()); ((TextView) adView.getBodyView()).setText(nativeAppInstallAd.getBody()); ((Button) adView.getCallToActionView()).setText(nativeAppInstallAd.getCallToAction()); // These assets aren't guaranteed to be in every NativeAppInstallAd, so it's important to // check before trying to display them. if (nativeAppInstallAd.getPrice() == null) { adView.getPriceView().setVisibility(View.INVISIBLE); } else { adView.getPriceView().setVisibility(View.VISIBLE); ((TextView) adView.getPriceView()).setText(nativeAppInstallAd.getPrice()); } if (nativeAppInstallAd.getStore() == null) { adView.getStoreView().setVisibility(View.INVISIBLE); } else { adView.getStoreView().setVisibility(View.VISIBLE); ((TextView) adView.getStoreView()).setText(nativeAppInstallAd.getStore()); } if (nativeAppInstallAd.getStarRating() == null) { adView.getStarRatingView().setVisibility(View.INVISIBLE); } else { ((RatingBar) adView.getStarRatingView()) .setRating(nativeAppInstallAd.getStarRating().floatValue()); adView.getStarRatingView().setVisibility(View.VISIBLE); } // Assign native ad object to the native view. adView.setNativeAd(nativeAppInstallAd); }
@Override protected void prepare(@NonNull final NativeAppInstallAdView adView, @NonNull final NativeAppInstallAd nativeAd) { final TextView adTitle = (TextView)adView.findViewById(R.id.ad_title); final TextView adBody = (TextView)adView.findViewById(R.id.ad_body); final ImageView adImage = (ImageView)adView.findViewById(R.id.ad_image); final TextView adCallToAction = (TextView)adView.findViewById(R.id.ad_call_to_action); adView.setHeadlineView(adTitle); adView.setBodyView(adBody); adView.setImageView(adImage); adView.setCallToActionView(adCallToAction); }
@Override public void onAppInstallAdLoaded(final NativeAppInstallAd nativeAd) { this.nativeAppInstallAd = nativeAd; this.render(this.nativeAppInstallAd); this.setImpressionMinTimeViewed(BaseStaticNativeAd.IMPRESSION_MIN_TIME); }
@Override public void onAppInstallAdLoaded(NativeAppInstallAd nativeAppInstallAd) { mNativeAd = nativeAppInstallAd; if (mListener != null) { mListener.onLoadFinish(); } }
/** * Subscribing to the native ads events */ protected synchronized void setupAds() { String unitId = getReleaseUnitId() != null ? getReleaseUnitId() : getDefaultUnitId(); AdLoader.Builder adloaderBuilder = new AdLoader.Builder(mContext.get(), unitId) .withAdListener(new AdListener() { @Override public void onAdFailedToLoad(int errorCode) { // Handle the failure by logging, altering the UI, etc. Log.i(TAG, "onAdFailedToLoad " + errorCode); lockFetch.set(false); mFetchFailCount++; mFetchingAdsCnt--; ensurePrefetchAmount(); onAdFailed( mPrefetchedAdList.size(), errorCode, null); } }) .withNativeAdOptions(new NativeAdOptions.Builder() // Methods in the NativeAdOptions.Builder class can be // used here to specify individual options settings. .build()); if(getAdTypeToFetch().contains(EAdType.ADVANCED_INSTALLAPP)) adloaderBuilder.forAppInstallAd(new NativeAppInstallAd.OnAppInstallAdLoadedListener() { @Override public void onAppInstallAdLoaded(NativeAppInstallAd appInstallAd) { onAdFetched(appInstallAd); } }); if(getAdTypeToFetch().contains(EAdType.ADVANCED_CONTENT)) adloaderBuilder.forContentAd(new NativeContentAd.OnContentAdLoadedListener() { @Override public void onContentAdLoaded(NativeContentAd contentAd) { onAdFetched(contentAd); } }); adLoader = adloaderBuilder.build(); }
@Override public int getItemViewType(int position) { if (AdapterCalculator.canShowAdAtPosition(position, adFetcher.getFetchedAdsCount())) { int adPos = AdapterCalculator.getAdIndex(position); NativeAd ad = adFetcher.getAdForIndex(adPos); return ad instanceof NativeAppInstallAd ? getViewTypeAdInstall() : getViewTypeAdContent(); } else { int origPos = AdapterCalculator.getOriginalContentPosition(position, adFetcher.getFetchedAdsCount(), mAdapter.getItemCount()); return mAdapter.getItemViewType(origPos); } }
AdMobNativeAdResponse(NativeAd ad, AdMobNativeSettings.AdMobNativeType type) { this.nativeAd = ad; this.type = type; runnable = new Runnable() { @Override public void run() { if (coverImage != null) { coverImage.recycle(); coverImage = null; } if (icon != null) { icon.recycle(); icon = null; } listener = null; expired = true; if (AdMobNativeAdResponse.this.nativeAd != null) { try { switch (AdMobNativeAdResponse.this.type) { case APP_INSTALL: NativeAppInstallAd appInstallAd = (NativeAppInstallAd) AdMobNativeAdResponse.this.nativeAd; appInstallAd.destroy(); break; case CONTENT_AD: NativeContentAd contentAd = (NativeContentAd) AdMobNativeAdResponse.this.nativeAd; contentAd.destroy(); break; } } catch (ClassCastException e) { } } } }; nativeExpireHandler = new Handler(Looper.getMainLooper()); nativeExpireHandler.postDelayed(runnable, Settings.NATIVE_AD_RESPONSE_EXPIRATION_TIME); loadAssets(); }
@Override public void onAppInstallAdLoaded(NativeAppInstallAd nativeAppInstallAd) { if (mBC != null) { mBC.onAdLoaded(new AdMobNativeAdResponse(nativeAppInstallAd, AdMobNativeSettings.AdMobNativeType.APP_INSTALL)); } }
@Override public void onBindViewHolder(final NativeAdViewHolder holder, final int position) { final NativeAd nativeAd = this.nativeAds.get(position); if (nativeAd != null) { holder.itemView.addOnAttachStateChangeListener(this); // Not sure what the previous ad type is, so just clear all of them NativeAdAdapter.clear(nativeAd, holder, NativeAdAdapter.ID_ADMOB_APP_INSTALL_AD_VIEW); NativeAdAdapter.clear(nativeAd, holder, NativeAdAdapter.ID_ADMOB_CONTENT_AD_VIEW); NativeAdAdapter.clear(nativeAd, holder, NativeAdAdapter.ID_ADMOB_NORMAL_AD_VIEW); final StaticNativeAd staticNativeAd = (StaticNativeAd)nativeAd.getBaseNativeAd(); int viewId = 0; // AdMob native ads need special handling if (staticNativeAd instanceof AdMobNativeAd) { final com.google.android.gms.ads.formats.NativeAd ad = ((AdMobNativeAd)staticNativeAd).getNativeAd(); if (ad instanceof NativeAppInstallAd) { holder.setAdType(NativeAdViewHolder.AD_TYPE_ADMOB_APP_INSTALL); viewId = NativeAdAdapter.ID_ADMOB_APP_INSTALL_AD_VIEW; } else if (ad instanceof NativeContentAd) { holder.setAdType(NativeAdViewHolder.AD_TYPE_ADMOB_CONTENT); viewId = NativeAdAdapter.ID_ADMOB_CONTENT_AD_VIEW; } else { Log.w(this.getClass().getSimpleName(), String.format("Unexpected AdMob native ad type: %s", ad.getClass())); } } else { holder.setAdType(NativeAdViewHolder.AD_TYPE_NORMAL); viewId = NativeAdAdapter.ID_ADMOB_NORMAL_AD_VIEW; } if (viewId > 0) { nativeAd.renderAdView(holder.adContainer.findViewById(viewId)); nativeAd.prepare(holder.adContainer.findViewById(viewId)); } NativeAdAdapter.resizeAdImage(holder, R.id.ad_image); } }
private void setHeadline(@NonNull final NativeAppInstallAd nativeAd) { if (!TextUtils.isEmpty(nativeAd.getHeadline())) this.setTitle(nativeAd.getHeadline().toString()); }
private void setBody(@NonNull final NativeAppInstallAd nativeAd) { if (!TextUtils.isEmpty(nativeAd.getBody())) this.setText(nativeAd.getBody().toString()); }
private void setCallToAction(@NonNull final NativeAppInstallAd nativeAd) { if (!TextUtils.isEmpty(nativeAd.getCallToAction())) this.setCallToAction(nativeAd.getCallToAction().toString()); }
private void setStarRating(@NonNull final NativeAppInstallAd nativeAd) { if (nativeAd.getStarRating() != null && nativeAd.getStarRating() > 0) this.setStarRating(nativeAd.getStarRating()); }
/** * Populates a {@link NativeAppInstallAdView} object with data from a given * {@link NativeAppInstallAd}. * * @param nativeAppInstallAd the object containing the ad's assets * @param adView the view to be populated */ private void populateAppInstallAdView(NativeAppInstallAd nativeAppInstallAd, NativeAppInstallAdView adView) { VideoController videoController = nativeAppInstallAd.getVideoController(); // Assign native ad object to the native view. adView.setNativeAd(nativeAppInstallAd); adView.setHeadlineView(adView.findViewById(R.id.appinstall_headline)); adView.setBodyView(adView.findViewById(R.id.appinstall_body)); adView.setCallToActionView(adView.findViewById(R.id.appinstall_call_to_action)); adView.setIconView(adView.findViewById(R.id.appinstall_app_icon)); adView.setPriceView(adView.findViewById(R.id.appinstall_price)); adView.setStarRatingView(adView.findViewById(R.id.appinstall_stars)); adView.setStoreView(adView.findViewById(R.id.appinstall_store)); // Some assets are guaranteed to be in every NativeAppInstallAd. ((TextView) adView.getHeadlineView()).setText(nativeAppInstallAd.getHeadline()); ((TextView) adView.getBodyView()).setText(nativeAppInstallAd.getBody()); ((Button) adView.getCallToActionView()).setText(nativeAppInstallAd.getCallToAction()); ((ImageView) adView.getIconView()).setImageDrawable(nativeAppInstallAd.getIcon() .getDrawable()); MediaView sampleMediaView = adView.findViewById(R.id.appinstall_media); ImageView imageView = adView.findViewById(R.id.appinstall_image); if (videoController.hasVideoContent()) { imageView.setVisibility(View.GONE); adView.setMediaView(sampleMediaView); } else { sampleMediaView.setVisibility(View.GONE); adView.setImageView(imageView); List<NativeAd.Image> images = nativeAppInstallAd.getImages(); if (images.size() > 0) { ((ImageView) adView.getImageView()).setImageDrawable(images.get(0).getDrawable()); } } // Some aren't guaranteed, however, and should be checked. if (nativeAppInstallAd.getPrice() == null) { adView.getPriceView().setVisibility(View.INVISIBLE); } else { adView.getPriceView().setVisibility(View.VISIBLE); ((TextView) adView.getPriceView()).setText(nativeAppInstallAd.getPrice()); } if (nativeAppInstallAd.getStore() == null) { adView.getStoreView().setVisibility(View.INVISIBLE); } else { adView.getStoreView().setVisibility(View.VISIBLE); ((TextView) adView.getStoreView()).setText(nativeAppInstallAd.getStore()); } if (nativeAppInstallAd.getStarRating() == null) { adView.getStarRatingView().setVisibility(View.INVISIBLE); } else { ((RatingBar) adView.getStarRatingView()) .setRating(nativeAppInstallAd.getStarRating().floatValue()); adView.getStarRatingView().setVisibility(View.VISIBLE); } // Handle the fact that this could be a Sample SDK native ad, which includes a // "degree of awesomeness" field. Bundle extras = nativeAppInstallAd.getExtras(); if (extras.containsKey(SampleCustomEvent.DEGREE_OF_AWESOMENESS)) { TextView degree = (TextView) adView.findViewById(R.id.appinstall_degreeofawesomeness); degree.setVisibility(View.VISIBLE); degree.setText(extras.getString(SampleCustomEvent.DEGREE_OF_AWESOMENESS)); } }
@Override public void bind(NativeAdView nativeAdView, NativeAd nativeAd) throws ClassCastException{ if (nativeAdView == null || nativeAd == null) return; if(!(nativeAd instanceof NativeAppInstallAd) || !(nativeAdView instanceof NativeAppInstallAdView)) throw new ClassCastException(); NativeAppInstallAd ad = (NativeAppInstallAd) nativeAd; NativeAppInstallAdView adView = (NativeAppInstallAdView) nativeAdView; // Locate the view that will hold the headline, set its text, and call the // NativeAppInstallAdView's setHeadlineView method to register it. TextView tvHeader = (TextView) adView.findViewById(R.id.tvHeader); tvHeader.setText(ad.getHeadline()); adView.setHeadlineView(tvHeader); TextView tvDescription = (TextView) adView.findViewById(R.id.tvDescription); tvDescription.setText(ad.getBody()); adView.setBodyView(tvDescription); ImageView ivLogo = (ImageView) adView.findViewById(R.id.ivLogo); if(ad.getIcon()!=null) ivLogo.setImageDrawable(ad.getIcon().getDrawable()); adView.setIconView(ivLogo); Button btnAction = (Button) adView.findViewById(R.id.btnAction); btnAction.setText(ad.getCallToAction()); adView.setCallToActionView(btnAction); TextView tvStore = (TextView) adView.findViewById(R.id.tvStore); tvStore.setText(ad.getStore()); adView.setStoreView(tvStore); TextView tvPrice = (TextView) adView.findViewById(R.id.tvPrice); tvPrice.setText(ad.getPrice()); adView.setPriceView(tvPrice); ImageView ivImage = (ImageView) adView.findViewById(R.id.ivImage); if (ad.getImages() != null && ad.getImages().size() > 0) { ivImage.setImageDrawable(ad.getImages().get(0).getDrawable()); ivImage.setVisibility(View.VISIBLE); } else ivImage.setVisibility(View.GONE); adView.setImageView(ivImage); // Call the NativeAppInstallAdView's setNativeAd method to register the // NativeAd. adView.setNativeAd(ad); }
/** * Loads a {@link NativeAppInstallAd} and calls {@link AppInstallAdViewHolder} methods to * display its assets or handle failure by hiding the view. */ public void loadAd(Context context, AppInstallAdViewHolder viewHolder) { synchronized (mSyncObject) { mViewHolder = viewHolder; if ((mAdLoader != null) && mAdLoader.isLoading()) { Log.d(MainActivity.LOG_TAG, "AppInstallAdFetcher is already loading an ad."); return; } // If an ad previously loaded, reuse it instead of requesting a new one. if (mAppInstallAd != null) { mViewHolder.populateView(mAppInstallAd); return; } NativeAppInstallAd.OnAppInstallAdLoadedListener appInstallAdListener = new NativeAppInstallAd.OnAppInstallAdLoadedListener() { public void onAppInstallAdLoaded(NativeAppInstallAd ad) { mAppInstallAd = ad; mViewHolder.populateView(mAppInstallAd); } }; if (mAdLoader == null) { mAdLoader = new AdLoader.Builder(context, mAdUnitId) .forAppInstallAd(appInstallAdListener) .withAdListener(new AdListener() { @Override public void onAdFailedToLoad(int errorCode) { mViewHolder.hideView(); Log.e(MainActivity.LOG_TAG, "App Install Ad Failed to load: " + errorCode); } }).build(); } mAdLoader.loadAd(new PublisherAdRequest.Builder().build()); } }
/** * To render ad view properly, implementation is expected to call {@link NativeAppInstallAdView#setHeadlineView(View)}, {@link NativeAppInstallAdView#setBodyView(View)}, {@link NativeAppInstallAdView#setCallToActionView(View)}, {@link NativeAppInstallAdView#setImageView(View)}, {@link NativeAppInstallAdView#setIconView(View)}, and/or {@link NativeAppInstallAdView#setStarRatingView(View)} methods, * and render the ad view text/images using {@link NativeAppInstallAd#getHeadline()}, {@link NativeAppInstallAd#getBody()}, {@link NativeAppInstallAd#getCallToAction()}, {@link NativeAppInstallAd#getImages()}, {@link NativeAppInstallAd#getIcon()}, and/or {@link NativeAppInstallAd#getStarRating()} methods. * @param adView The native ad view to render the ad. * @param nativeAd The native ad instance used to render the ad view. */ protected abstract void prepare(@NonNull NativeAppInstallAdView adView, @NonNull NativeAppInstallAd nativeAd);