/*
* The Banner type needs to inherit TPBannerAdapter and rewrite the following methods
* loadCustomAd() is used to obtain the parameters sent by the server and configured locally, and 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 FacebookBannerAdapter extends TPBannerAdapter {
private AdView mFacebookBanner;
private String placementId;
private TPBannerAdImpl mTpBannerAd;
private static final String TAG = "FacebookBanner";
@Override
public void loadCustomAd(Context context, Map<String, Object> userParams, Map<String, String> tpParams) {
// 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_CONFIGURATION_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
mFacebookBanner = new AdView(context, placementId, AdSize.BANNER_HEIGHT_50);
//Set FB monitoring and set monitoring callback
AdListener adListener = new AdListener() {
@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) {
Log.i(TAG, "onAdLoaded: ");
if (mFacebookBanner == null) {
return;
}
if (mLoadAdapterListener != null) {
//The Banner type needs to create TPBannerAdImpl and pass in the View object that has been successfully loaded.
mTpBannerAd = new TPBannerAdImpl(null, mFacebookBanner);
//Pass the TPBannerAdImpl object to loadAdapterLoaded to implement the ad event loading success callback
mLoadAdapterListener.loadAdapterLoaded(mTpBannerAd);
}
}
@Override
public void onAdClicked(Ad ad) {
Log.i(TAG, "onAdClicked: ");
//Use mTpBannerAd to implement click callback of ad events
if (mTpBannerAd != null) {
mTpBannerAd.adClicked();
}
}
@Override
public void onLoggingImpression(Ad ad) {
Log.i(TAG, "onLoggingImpression: ");
//Use mTpBannerAd to implement display callback of advertising events
if (mTpBannerAd != null)
mTpBannerAd.adShown();
}
};
//Request Ad
mFacebookBanner.loadAd(mFacebookBanner.buildLoadAdConfig().withAdListener(adListener).build());
}
@Override
public void clean() {
Log.i(TAG, "clean: ");
if (mFacebookBanner != null) {
Views.removeFromParent(mFacebookBanner);
mFacebookBanner.destroy();
mFacebookBanner = null;
}
}
@Override
public String getNetworkVersion() {
return BuildConfig.VERSION_NAME;
}
@Override
public String getNetworkName() {
return "audience-network";
}
}