/*
* The interstitial type needs to inherit TPInterstitialAdapter 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
* showAd() implements the logic of displaying the incentive video of the custom advertising platform
* isReady() is used to determine whether the custom ad is expired before displaying the ad
* clean() is used to release resources
* getNetworkVersion() The version number of the custom third-party source
* getNetworkName The name of the custom third-party source
* */
public class FacebookInterstitialAdapter extends TPInterstitialAdapter {
private InterstitialAd mFacebookInterstitial;
private String placementId;
private static final String TAG = "FacebookInterstitial";
@Override
public void loadCustomAd(final Context context,
final Map<String, Object> userParams,
final 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
mFacebookInterstitial = new InterstitialAd(context, placementId);
//Set FB monitoring and set monitoring callback
InterstitialAdListener interstitialAdListener = new InterstitialAdListener() {
@Override
public void onError(Ad ad, AdError adError) {
Log.i(TAG, "onError: code :" + adError.getErrorCode() + " , msg:" + adError.getErrorMessage());
//NETWORK_NO_FILL
if (mLoadAdapterListener != null) {
TPError tpError = new TPError(NETWORK_NO_FILL);
tpError.setErrorCode(adError.getErrorCode() + "");
tpError.setErrorMessage(adError.getErrorMessage());
mLoadAdapterListener.loadAdapterLoadFailed(tpError);
}
}
@Override
public void onAdLoaded(Ad ad) {
Log.i(TAG, "onAdLoaded: ");
if (mFacebookInterstitial == null) {
return;
}
if (mLoadAdapterListener != null) {
//Use mLoadAdapterListener to implement the ad event loading success callback
mLoadAdapterListener.loadAdapterLoaded(null);
}
}
@Override
public void onAdClicked(Ad ad) {
Log.i(TAG, "onAdClicked: ");
//Use mShowListener to implement click callback of advertising events
if (mShowListener != null) {
mShowListener.onAdClicked();
}
}
@Override
public void onLoggingImpression(Ad ad) {
Log.i(TAG, "onLoggingImpression: ");
}
@Override
public void onInterstitialDisplayed(Ad ad) {
Log.i(TAG, "onInterstitialDisplayed: ");
//Use mShowListener to implement display callback of advertising events
if (mShowListener != null) {
mShowListener.onAdShown();
}
}
@Override
public void onInterstitialDismissed(Ad ad) {
Log.i(TAG, "onInterstitialDismissed: ");
//Use mShowListener to implement the closing callback of advertising events
if (mShowListener != null) {
mShowListener.onAdClosed();
}
}
};
// request Ad
InterstitialAd.InterstitialAdLoadConfigBuilder interstitialAdLoadConfigBuilder = mFacebookInterstitial.buildLoadAdConfig().withAdListener(interstitialAdListener);
mFacebookInterstitial.loadAd(interstitialAdLoadConfigBuilder.build());
}
@Override
public void showAd() {
/*
* mShowListener is generated when showAd() is rewritten, and the user implements the event callback after calling show()
* Callback method onAdShown: Ad starts to display
* Callback method onAdClosed: Ad is closed
* Callback method onAdVideoError: Ad display failed, parameter 1: ErrorCode error code; parameter 2: ErrorMsg error message
* Callback method onAdClicked: Ad is clicked
* Callback method onReward: Ad reward
* */
if (mFacebookInterstitial != null && mFacebookInterstitial.isAdLoaded()) {
mFacebookInterstitial.show();
}else {
if (mShowListener != null) {
mShowListener.onAdVideoError(new TPError(SHOW_FAILED));
}
}
}
@Override
public void clean() {
// Release resources
if (mFacebookInterstitial != null) {
mFacebookInterstitial.destroy();
mFacebookInterstitial = null;
}
}
@Override
public boolean isReady() {
// Used to determine whether the advertisement has expired
if (mFacebookInterstitial != null) {
return !isAdsTimeOut() && !mFacebookInterstitial.isAdInvalidated();
}
return false;
}
@Override
public String getNetworkVersion() {
return BuildConfig.VERSION_NAME;
}
@Override
public String getNetworkName() {
return "audience-network";
}
}