/*
* The rewarded video type needs to inherit TPRewardAdapter 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
* showAd() implements the logic of displaying the rewarded 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 FacebookInterstitialVideoAdapter extends TPRewardAdapter {
private RewardedVideoAd mRewardedVideoAd;
private String mPlacementId;
private static final String TAG = "FacebookRewardedVideo";
@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")) {
mPlacementId = 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
mRewardedVideoAd = new RewardedVideoAd(context, mPlacementId);
//Set FB monitoring and set monitoring callback
RewardedVideoAdListener rewardedVideoAdListener = new RewardedVideoAdListener() {
@Override
public void onRewardedVideoCompleted() {
Log.i(TAG, "onRewardedVideoCompleted: ");
//Use mShowListener to implement reward callbacks for advertising events
if (mShowListener != null) {
mShowListener.onReward();
}
}
@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(NETWORK_NO_FILL);
tpError.setErrorCode(adError.getErrorCode() +"");
tpError.setErrorMessage(adError.getErrorMessage());
mLoadAdapterListener.loadAdapterLoadFailed(tpError);
}
}
@Override
public void onAdLoaded(Ad ad) {
if (mRewardedVideoAd == null) {
return;
}
Log.i(TAG, "onAdLoaded: ");
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: ");
//Use mShowListener to implement display callback of advertising events
if (mShowListener != null) {
mShowListener.onAdShown();
}
}
@Override
public void onRewardedVideoClosed() {
Log.i(TAG, "onRewardedVideoClosed: ");
//Use mShowListener to implement the closing callback of advertising events
if (mShowListener != null) {
mShowListener.onAdClosed();
}
}
};
//Request Ads
RewardedVideoAd.RewardedVideoAdLoadConfigBuilder rewardedVideoAdLoadConfigBuilder = mRewardedVideoAd.buildLoadAdConfig().withAdListener(rewardedVideoAdListener);
mRewardedVideoAd.loadAd(rewardedVideoAdLoadConfigBuilder.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 (mRewardedVideoAd != null && mRewardedVideoAd.isAdLoaded()) {
mRewardedVideoAd.show();
}else {
if (mShowListener != null) {
mShowListener.onAdVideoError(new TPError(SHOW_FAILED));
}
}
}
@Override
public void clean() {
super.clean();
// Release resources
if (mRewardedVideoAd != null) {
mRewardedVideoAd.destroy();
mRewardedVideoAd = null;
}
}
@Override
public boolean isReady() {
// Used to determine whether the advertisement has expired
if (mRewardedVideoAd != null) {
return mRewardedVideoAd.isAdInvalidated();
}
return false;
}
@Override
public String getNetworkVersion() {
return BuildConfig.VERSION_NAME;
}
@Override
public String getNetworkName() {
return "audience-network";
}
}