/*
* The opening screen type needs to inherit TPSplashAdapter 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 AdmobSplashAdapter extends TPSplashAdapter {
public static final String TAG = "AdmobSplashAdapter";
private AppOpenAd mAppOpenAd;
private String placementId;
private AdRequest request;
@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;
}
request = new AdRequest.Builder().build();
MobileAds.disableMediationAdapterInitialization(context);
MobileAds.initialize(context, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
Log.i(TAG, "onInitializationComplete: ");
AppOpenAd.load(context, placementId, request, AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT, appOpenAdLoadCallback);
}
});
}
private final AppOpenAd.AppOpenAdLoadCallback appOpenAdLoadCallback = new AppOpenAd.AppOpenAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull AppOpenAd appOpenAd) {
Log.i(TAG, "onAdLoaded: ");
mAppOpenAd = appOpenAd;
if (mLoadAdapterListener != null) {
//Use mLoadAdapterListener to implement the ad event loading success callback
mLoadAdapterListener.loadAdapterLoaded(null);
}
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
Log.i(TAG, "onAdFailedToLoad message: " + loadAdError.getMessage() + ":code:" + loadAdError.getCode());
TPError tpError = new TPError(NETWORK_NO_FILL);
tpError.setErrorCode(String.valueOf(loadAdError.getCode()));
tpError.setErrorMessage(loadAdError.getMessage());
//Use mLoadAdapterListener to achieve the failure of the loading of advertising events
if (mLoadAdapterListener != null)
mLoadAdapterListener.loadAdapterLoadFailed(tpError);
}
};
@Override
public void showAd() {
Activity activity = GlobalTradPlus.getInstance().getActivity();
if (activity == null) {
if (mShowListener != null) {
mShowListener.onAdVideoError(new TPError(ADAPTER_ACTIVITY_ERROR));
}
return;
}
if (mAppOpenAd == null) {
if (mShowListener != null) {
mShowListener.onAdVideoError(new TPError(UNSPECIFIED));
}
return;
}
mAppOpenAd.setFullScreenContentCallback(fullScreenContentCallback);
mAppOpenAd.show(activity);
}
final FullScreenContentCallback fullScreenContentCallback = new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
Log.i(TAG, "onAdDismissedFullScreenContent: ");
//Use mShowListener to implement the closing callback of advertising events
if (mShowListener != null) {
mShowListener.onAdClosed();
}
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
Log.i(TAG, "onAdFailedToShowFullScreenContent msg : " + adError.getMessage() + ":code:" + adError.getCode());
TPError tpError = new TPError(NETWORK_NO_FILL);
tpError.setErrorCode(String.valueOf(adError.getCode()));
tpError.setErrorMessage(adError.getMessage());
if (mLoadAdapterListener != null)
mLoadAdapterListener.loadAdapterLoadFailed(tpError);
}
@Override
public void onAdShowedFullScreenContent() {
Log.i(TAG, "onAdShowedFullScreenContent: ");
//Use mShowListener to implement display callback of advertising events
if (mShowListener != null) {
mShowListener.onAdShown();
}
}
};
@Override
public void clean() {
// Release resources
if (mAppOpenAd != null) {
mAppOpenAd.setFullScreenContentCallback(null);
mAppOpenAd = null;
}
}
@Override
public boolean isReady() {
// Used to determine whether the advertisement has expired
return true;
}
@Override
public String getNetworkName() {
return "Admob";
}
@Override
public String getNetworkVersion() {
return MobileAds.getVersionString();
}
}