/*
* Native type needs to inherit TPNativeAdapter and rewrite the following methods
* loadCustomAd() is used to obtain the parameters sent by the server and locally configured to implement the loading logic of the custom advertising platform
* clean() Release resources after the Banner ad is removed from the screen
* getNetworkVersion() Custom third-party source version number
* getNetworkName Custom third-party source name
* */
public class FacebookNativeAdapter extends TPNativeAdapter {
private NativeAd mFacebookNative;
private FacebookNativeAd mFacebookNativeAd;
private NativeBannerAd mNativeBannerAd;
private String placementId;
private Context mContext;
private static final String TAG = "FacebookNative";
@Override
public void loadCustomAd(Context context, Map<String, Object> userParams, Map<String, String> tpParams) {
mContext = context;
// tpParams Get the fields sent from the server
if (tpParams.size() > 0 && tpParams.containsKey("placemntId")) {
placementId = tpParams.get("placemntId");
} else {
/*
* mLoadAdapterListener is generated synchronously when oadCustomAd is rewritten
* Callback method loadAdapterLoaded: Ad loading is successful
* Callback method loadAdapterLoadFailed: Ad loading fails
* Construct TPError method, ADAPTER_CONFIGURATION_ERROR server sends parameter error
* Method setTpErrorCode sets the third-party ErrorCode error code
* Method setErrorMessage sets the third-party ErrorMsg error message
*
* */
if (mLoadAdapterListener != null) {
mLoadAdapterListener.loadAdapterLoadFailed(new TPError(ADAPTER_ACTIVITY_ERROR));
}
return;
}
// userParams Get the parameters from the local configuration
// For example: Overseas sources need to set CCPA and COPPA. For specific access, refer to the Advanced Features - Privacy Specifications section.
FacebookInitializeHelper.setUserParams(userParams, mLoadAdapterListener);
//Initialize SDK
FacebookInitializeHelper.initialize(context);
//Create a three-party ad slot object
mFacebookNative = new NativeAd(context, placementId);
// Request Native Template
mFacebookNative.loadAd(
mFacebookNative.buildLoadAdConfig()
.withAdListener(nativeAdListener)
.withMediaCacheFlag(NativeAdBase.MediaCacheFlag.ALL)
.build());
}
//Set FB monitoring and set monitoring callback
NativeAdListener nativeAdListener = new NativeAdListener() {
@Override
public void onError(Ad ad, AdError adError) {
Log.i(TAG, "onError: code :" + adError.getErrorCode() + " , msg:" + adError.getErrorMessage());
if (mLoadAdapterListener != null) {
TPError tpError = new TPError(SHOW_FAILED);
tpError.setErrorCode(adError.getErrorCode() + "");
tpError.setErrorMessage(adError.getErrorMessage());
mLoadAdapterListener.loadAdapterLoadFailed(tpError);
}
}
@Override
public void onAdLoaded(Ad ad) {
// Native Template
if (mFacebookNative != null) {
Log.i(TAG, "TemplateNativeAdLoaded: ");
View adView = NativeAdView.render(mContext, mFacebookNative);
// Add the Native Ad View to your ad container.
// The recommended dimensions for the ad container are:
// Width: 280dp - 500dp
// Height: 250dp - 500dp
RelativeLayout relativeLayout = new RelativeLayout(mContext);
relativeLayout.addView(adView, new RelativeLayout.LayoutParams(DeviceUtils.dip2px(mContext, 320)
, DeviceUtils.dip2px(mContext, 340)));
relativeLayout.setGravity(CENTER);
//After the Native type ad is loaded successfully, the ad information of the custom ad platform needs to be packaged in the inherited TPBaseAd object, and then called back through the mLoadAdapterListener.loadAdapterLoaded() method
//Parameter 2: Native type AppKeyManager.TEMPLATE_RENDERING_YES is the native template
mFacebookNativeAd = new FacebookNativeAd(relativeLayout, AppKeyManager.TEMPLATE_RENDERING_YES);
if (mLoadAdapterListener != null) {
mLoadAdapterListener.loadAdapterLoaded(mFacebookNativeAd);
}
} else {
/*
* Use mLoadAdapterListener to implement loading failure callback of advertising events
* */
if (mLoadAdapterListener != null) {
mLoadAdapterListener.loadAdapterLoadFailed(new TPError(UNSPECIFIED));
}
}
}
@Override
public void onAdClicked(Ad ad) {
Log.i(TAG, "onAdClicked: ");
//Execute the click callback of the ad event through the method in mFacebookNativeAd
if (mFacebookNativeAd != null)
mFacebookNativeAd.onAdViewClicked();
}
@Override
public void onLoggingImpression(Ad ad) {
Log.i(TAG, "onLoggingImpression: ");
//Execute the display callback of the ad event through the method in mFacebookNativeAd
if (mFacebookNativeAd != null)
mFacebookNativeAd.onAdViewExpanded();
}
@Override
public void onMediaDownloaded(Ad ad) {
}
};
@Override
public void clean() {
// Release resources
AdSettings.clearTestDevices();
if (mFacebookNativeAd != null)
mFacebookNativeAd.clean();
}
@Override
public String getNetworkVersion() {
return BuildConfig.VERSION_NAME;
}
@Override
public String getNetworkName() {
return "audience-network";
}
}