com.facebook.ads.NativeAd Java Examples

The following examples show how to use com.facebook.ads.NativeAd. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TimelineAdapter.java    From aptoide-client with GNU General Public License v2.0 6 votes vote down vote up
public synchronized void addNativeAd(NativeAd ad, Context context, ArrayList<TimelineListAPKsJson.UserApk> list) {
    if (ad == null) {
        Log.d("pois", "addNativeAd , was null");
        return;
    }
    if (this.ad != null) {
        Log.d("pois","addNativeAd , was old");
        // Clean up the old ad before inserting the new one
        this.ad.unregisterView();
        list.remove(AD_INDEX);
        this.ad = null;
        this.notifyDataSetChanged();
    }
    Log.d("pois","addNativeAd , adding native ad");
    this.ad = ad;
    adView = mInflater.inflate(R.layout.row_timeline_ad_unit, null);
    TimelineListAPKsJson.UserApk ret = new UserAPKAd(ad);
    inflateAd(ad, adView, context);
    list.add(AD_INDEX, ret);
    Log.d("pois","addNativeAd , position on "+AD_INDEX);
    this.notifyDataSetChanged();
}
 
Example #2
Source File: FacebookRtbNativeAd.java    From googleads-mobile-android-mediation with Apache License 2.0 6 votes vote down vote up
@Override
public void trackViews(View view,
    Map<String, View> clickableAssetViews,
    Map<String, View> nonClickableAssetViews) {

  // Facebook does its own click handling.
  setOverrideClickHandling(true);

  ArrayList<View> assetViews = new ArrayList<>(clickableAssetViews.values());
  ImageView iconView = (ImageView) clickableAssetViews.get(UnifiedNativeAdAssetNames.ASSET_ICON);

  if (mNativeAdBase instanceof NativeAd) {
    ((NativeAd) mNativeAdBase).registerViewForInteraction(view, mMediaView, iconView, assetViews);
  } else if (mNativeAdBase instanceof NativeBannerAd) {
    ((NativeBannerAd) mNativeAdBase).registerViewForInteraction(view, iconView, assetViews);
  }
}
 
Example #3
Source File: NativeAdView.java    From react-native-fbads with MIT License 6 votes vote down vote up
/**
 * Called by the view manager when adsManager prop is set. Sends serialised
 * version of a native ad back to Javascript.
 *
 * @param nativeAd
 */
public void setNativeAd(NativeAd nativeAd) {
  if (mNativeAd != null) {
    mNativeAd.unregisterView();
  }

  mNativeAd = nativeAd;

  if (nativeAd == null) {
    mEventEmitter.receiveEvent(getId(), "onAdLoaded", null);
    return;
  }

  WritableMap event = Arguments.createMap();
  event.putString("headline", nativeAd.getAdHeadline());
  event.putString("socialContext", nativeAd.getAdSocialContext());
  event.putString("bodyText", nativeAd.getAdBodyText());
  event.putString("callToActionText", nativeAd.getAdCallToAction());
  event.putString("sponsoredTranslation", nativeAd.getSponsoredTranslation());
  event.putString("advertiserName", nativeAd.getAdvertiserName());
  event.putString("promotedTranslation", nativeAd.getPromotedTranslation());
  event.putString("translation", nativeAd.getAdTranslation());
  event.putString("linkDescription", nativeAd.getAdLinkDescription());

  mEventEmitter.receiveEvent(getId(), "onAdLoaded", event);
}
 
Example #4
Source File: FragmentSocialTimeline.java    From aptoide-client with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onAdLoaded(Ad ad) {
    Logger.d("FragmentSocialTimeline","onAdLoaded , ad: "+ad.toString());
    if(getActivity() != null){
        adapter.addNativeAd((NativeAd) ad,getActivity(),apks);
    }

}
 
Example #5
Source File: FBNativeAdResponse.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
public FBNativeAdResponse(NativeAd ad) {
    this.nativeAd = ad;
    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 (nativeAd != null) {
                nativeAd.setAdListener(null);
                nativeAd.destroy();
                nativeAd = null;
            }
            if(nativeElements != null && !nativeElements.isEmpty()){
                nativeElements.clear();
            }
        }
    };
    fbNativeExpireHandler = new Handler(Looper.getMainLooper());
    fbNativeExpireHandler.postDelayed(runnable, Settings.NATIVE_AD_RESPONSE_EXPIRATION_TIME);
}
 
Example #6
Source File: FacebookAdapter.java    From googleads-mobile-android-mediation with Apache License 2.0 5 votes vote down vote up
/**
 * Convert rating to a scale of 1 to 5.
 */
private Double getRating(NativeAd.Rating rating) {
  if (rating == null) {
    return null;
  }
  return (MAX_STAR_RATING * rating.getValue()) / rating.getScale();
}
 
Example #7
Source File: FacebookAdapter.java    From googleads-mobile-android-mediation with Apache License 2.0 5 votes vote down vote up
/**
 * Convert rating to a scale of 1 to 5.
 */
private Double getRating(NativeAd.Rating rating) {
  if (rating == null) {
    return null;
  }
  return (MAX_STAR_RATING * rating.getValue()) / rating.getScale();
}
 
Example #8
Source File: FacebookAdapter.java    From googleads-mobile-android-mediation with Apache License 2.0 5 votes vote down vote up
private void createAndLoadNativeAd(Context context,
    String placementID,
    NativeMediationAdRequest adRequest,
    Bundle mediationExtras) {

  if (mediationExtras != null) {
    isNativeBanner = mediationExtras.getBoolean(NATIVE_BANNER);
  }

  if (isNativeBanner) {
    mNativeBannerAd = new NativeBannerAd(context, placementID);
    buildAdRequest(adRequest);
    mNativeBannerAd.loadAd(
        mNativeBannerAd
            .buildLoadAdConfig()
            .withAdListener(new NativeBannerListener(context, mNativeBannerAd, adRequest))
            .withMediaCacheFlag(NativeAdBase.MediaCacheFlag.ALL)
            .withPreloadedIconView(
                NativeAdBase.NativeAdLoadConfigBuilder.UNKNOWN_IMAGE_SIZE,
                NativeAdBase.NativeAdLoadConfigBuilder.UNKNOWN_IMAGE_SIZE)
            .build());
  } else {
    mMediaView = new MediaView(context);
    mNativeAd = new NativeAd(context, placementID);
    buildAdRequest(adRequest);
    mNativeAd.loadAd(
        mNativeAd
            .buildLoadAdConfig()
            .withAdListener(new NativeListener(context, mNativeAd, adRequest))
            .withMediaCacheFlag(NativeAdBase.MediaCacheFlag.ALL)
            .withPreloadedIconView(
                NativeAdBase.NativeAdLoadConfigBuilder.UNKNOWN_IMAGE_SIZE,
                NativeAdBase.NativeAdLoadConfigBuilder.UNKNOWN_IMAGE_SIZE)
            .build());
  }
}
 
Example #9
Source File: NativeAdChoicesView.java    From react-native-fbads with MIT License 5 votes vote down vote up
public void setNativeAd(NativeAd nativeAd) {
  AdChoicesView adChoicesView = new AdChoicesView(mContext, nativeAd, true);
  adChoicesView.measure(adChoicesView.getMeasuredWidth(), adChoicesView.getMeasuredHeight());
  adChoicesView.layout(0, 0, adChoicesView.getMeasuredWidth(), adChoicesView.getMeasuredHeight());
  adChoicesView.bringToFront();
  addView(adChoicesView);
}
 
Example #10
Source File: FacebookAdapter.java    From googleads-mobile-android-mediation with Apache License 2.0 4 votes vote down vote up
private NativeListener(Context context, NativeAd nativeAd,
    NativeMediationAdRequest mediationAdRequest) {
  mContext = new WeakReference<>(context);
  mNativeAd = nativeAd;
  mMediationAdRequest = mediationAdRequest;
}
 
Example #11
Source File: TimelineAdapter.java    From aptoide-client with GNU General Public License v2.0 4 votes vote down vote up
public UserAPKAd(NativeAd ad){
    this.ad = ad;
}
 
Example #12
Source File: TimelineAdapter.java    From aptoide-client with GNU General Public License v2.0 4 votes vote down vote up
public void inflateAd(NativeAd nativeAd, View adView, Context context) {
    // Create native UI using the ad metadata.

    ImageView nativeAdIcon = (ImageView) adView.findViewById(R.id.nativeAdIcon);
    TextView nativeAdTitle = (TextView) adView.findViewById(R.id.nativeAdTitle);
    TextView nativeAdTitleRating = (TextView) adView.findViewById(R.id.nativeAdTitleRating);
    TextView nativeAdBody = (TextView) adView.findViewById(R.id.nativeAdBody);

    ImageView nativeAdImage = (ImageView) adView.findViewById(R.id.nativeAdImage);
    TextView nativeAdSocialContext = (TextView) adView.findViewById(R.id.nativeAdSocialContext);
    FaceBookAdButton nativeAdCallToAction = (FaceBookAdButton) adView.findViewById(R.id.nativeAdCallToAction);
    RatingBar nativeAdStarRating = (RatingBar) adView.findViewById(R.id.nativeAdStarRating);

    // Setting the Text
    nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
    nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
    nativeAdTitle.setText(nativeAd.getAdTitle());
    nativeAdTitleRating.setText(nativeAd.getAdTitle());
    nativeAdBody.setText(nativeAd.getAdBody());

    // Downloading and setting the ad icon.
    NativeAd.Image adIcon = nativeAd.getAdIcon();
    NativeAd.downloadAndDisplayImage(adIcon, nativeAdIcon);

    // Downloading and setting the cover image.
    NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();
    int bannerWidth = adCoverImage.getWidth();
    int bannerHeight = adCoverImage.getHeight();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    int screenWidth = metrics.widthPixels;
    int screenHeight = metrics.heightPixels;
    nativeAdImage.setLayoutParams(new LinearLayout.LayoutParams(
            screenWidth,
            Math.min((int) (((double) screenWidth / (double) bannerWidth) * bannerHeight), screenHeight / 3)
    ));
    NativeAd.downloadAndDisplayImage(adCoverImage, nativeAdImage);

    NativeAd.Rating rating = nativeAd.getAdStarRating();
    if (rating != null) {
        nativeAdStarRating.setVisibility(View.VISIBLE);
        nativeAdStarRating.setNumStars((int) rating.getScale());
        nativeAdStarRating.setRating((float) rating.getValue());
    } else {
        nativeAdStarRating.setVisibility(View.GONE);
    }

    // Wire up the View with the native ad, the whole nativeAdContainer will be clickable
    //nativeAd.registerViewForInteraction(adView);


    // Or you can replace the above call with the following function to specify the clickable areas.
    nativeAd.registerViewForInteraction(adView, Arrays.asList(nativeAdCallToAction));

}
 
Example #13
Source File: StoryFragment.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void inflateAd(NativeAd nativeAd){


        try {


        nativeAd.unregisterView();

        // Add the Ad view into the ad container.
        nativeAdLayout = view.findViewById(R.id.native_ad_container);
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        // Inflate the Ad view.  The layout referenced should be the one you created in the last step.
        adView = (LinearLayout) inflater.inflate(R.layout.layout_native_ad_view, nativeAdLayout, false);
        nativeAdLayout.addView(adView);

        // Add the AdOptionsView
        LinearLayout adChoicesContainer = view.findViewById(R.id.ad_choices_container);
        AdOptionsView adOptionsView = new AdOptionsView(getActivity(), nativeAd, nativeAdLayout);
        adChoicesContainer.removeAllViews();
        adChoicesContainer.addView(adOptionsView, 0);

        // Create native UI using the ad metadata.
        AdIconView nativeAdIcon = adView.findViewById(R.id.native_ad_icon);
        TextView nativeAdTitle = adView.findViewById(R.id.native_ad_title);
        MediaView nativeAdMedia = adView.findViewById(R.id.native_ad_media);
        TextView nativeAdSocialContext = adView.findViewById(R.id.native_ad_social_context);
        TextView nativeAdBody = adView.findViewById(R.id.native_ad_body);
        TextView sponsoredLabel = adView.findViewById(R.id.native_ad_sponsored_label);
        Button nativeAdCallToAction = adView.findViewById(R.id.native_ad_call_to_action);

        // Set the Text.
        nativeAdTitle.setText(nativeAd.getAdvertiserName());
        nativeAdBody.setText(nativeAd.getAdBodyText());
        nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
        nativeAdCallToAction.setVisibility(nativeAd.hasCallToAction() ? View.VISIBLE : View.INVISIBLE);
        nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
        sponsoredLabel.setText(nativeAd.getSponsoredTranslation());

        // Create a list of clickable views
        List<View> clickableViews = new ArrayList<>();
        clickableViews.add(nativeAdTitle);
        clickableViews.add(nativeAdCallToAction);

        // Register the Title and CTA button to listen for clicks.
        nativeAd.registerViewForInteraction(
                adView,
                nativeAdMedia,
                nativeAdIcon,
                clickableViews);
        }catch (Exception e){
            Log.e(TAG, "inflateAd: "+e );
        }
    }
 
Example #14
Source File: FacebookNativeAd.java    From mobile-sdk-android with Apache License 2.0 3 votes vote down vote up
/**
 * AppNexus SDk calls this method to request a native ad from Facebook
 *
 * @param context The context from which this class is instantiated.
 * @param uid Facebook placement id, app developer needs to set up account with Facebook.
 * @param mBC The controller that passes callbacks to AppNexus SDK
 * @param tp Targeting parameters that were set in AppNexus API.
 * @return AppNexus NativeAdResponse that wraps Facebook ad.
 */

@Override
public void requestNativeAd(Context context, String parameterString, String uid, final MediatedNativeAdController mBC, TargetingParameters tp) {
    NativeAd nativeAd = new NativeAd(context, uid);
    FBNativeAdResponse response = new FBNativeAdResponse(nativeAd);
    nativeAd.setAdListener(new FacebookNativeAdListener(mBC, response));
    nativeAd.loadAd();
}
 
Example #15
Source File: FacebookAdapter.java    From googleads-mobile-android-mediation with Apache License 2.0 2 votes vote down vote up
/**
 * Default constructor for {@link AppInstallMapper}.
 *
 * @param nativeAd  The Facebook native ad to be mapped.
 * @param adOptions {@link NativeAdOptions} containing the preferences to be used when mapping
 *                  the native ad.
 */
public AppInstallMapper(NativeAd nativeAd, NativeAdOptions adOptions) {
  AppInstallMapper.this.mNativeAd = nativeAd;
  AppInstallMapper.this.mNativeAdOptions = adOptions;
}
 
Example #16
Source File: FacebookAdapter.java    From googleads-mobile-android-mediation with Apache License 2.0 2 votes vote down vote up
/**
 * This method will check whether or not the given Facebook native ad contains all the necessary
 * fields for it to be mapped to Google Mobile Ads' native app install ad.
 *
 * @param nativeAd Facebook native ad.
 * @return {@code true} if the given ad contains all the necessary fields, {@link false}
 * otherwise.
 */
private boolean containsRequiredFieldsForNativeAppInstallAd(NativeAd nativeAd) {
  return ((nativeAd.getAdHeadline() != null) && (nativeAd.getAdCoverImage() != null)
      && (nativeAd.getAdBodyText() != null) && (nativeAd.getAdIcon() != null)
      && (nativeAd.getAdCallToAction() != null) && (mMediaView != null));
}
 
Example #17
Source File: EndlessWrapperAdapter.java    From aptoide-client with GNU General Public License v2.0 2 votes vote down vote up
public void addNativeAd(NativeAd ad,Context context,ArrayList<TimelineListAPKsJson.UserApk> list) {

        tla.addNativeAd(ad,context,list);
    }
 
Example #18
Source File: FacebookAdapter.java    From googleads-mobile-android-mediation with Apache License 2.0 2 votes vote down vote up
/**
 * Default constructor for {@link UnifiedAdMapper}.
 *
 * @param nativeAd  The Facebook native ad to be mapped.
 * @param adOptions {@link NativeAdOptions} containing the preferences to be used when mapping
 *                  the native ad.
 */
public UnifiedAdMapper(NativeAd nativeAd, NativeAdOptions adOptions) {
  UnifiedAdMapper.this.mNativeAd = nativeAd;
  UnifiedAdMapper.this.mNativeAdOptions = adOptions;
}
 
Example #19
Source File: FacebookAdapter.java    From googleads-mobile-android-mediation with Apache License 2.0 2 votes vote down vote up
/**
 * This method will check whether or not the given Facebook native ad contains all the necessary
 * fields for it to be mapped to Google Mobile Ads' Unified install ad.
 *
 * @param nativeAd Facebook native ad.
 * @return {@code true} if the given ad contains all the necessary fields, {@link false}
 * otherwise.
 */
private boolean containsRequiredFieldsForUnifiedNativeAd(NativeAd nativeAd) {
  return ((nativeAd.getAdHeadline() != null) && (nativeAd.getAdCoverImage() != null)
      && (nativeAd.getAdBodyText() != null) && (nativeAd.getAdIcon() != null)
      && (nativeAd.getAdCallToAction() != null) && (mMediaView != null));
}