Created
March 23, 2020 21:06
-
-
Save thomasmso/8537ebdb9a761434049c8fd5da78809d to your computer and use it in GitHub Desktop.
MAX Tapjoy Adapter - Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.applovin.mediation.adapters; | |
import android.app.Activity; | |
import android.text.TextUtils; | |
import com.applovin.mediation.MaxReward; | |
import com.applovin.mediation.adapter.MaxAdapterError; | |
import com.applovin.mediation.adapter.MaxInterstitialAdapter; | |
import com.applovin.mediation.adapter.MaxRewardedAdapter; | |
import com.applovin.mediation.adapter.MaxSignalProvider; | |
import com.applovin.mediation.adapter.listeners.MaxInterstitialAdapterListener; | |
import com.applovin.mediation.adapter.listeners.MaxRewardedAdapterListener; | |
import com.applovin.mediation.adapter.listeners.MaxSignalCollectionListener; | |
import com.applovin.mediation.adapter.parameters.MaxAdapterInitializationParameters; | |
import com.applovin.mediation.adapter.parameters.MaxAdapterParameters; | |
import com.applovin.mediation.adapter.parameters.MaxAdapterResponseParameters; | |
import com.applovin.mediation.adapter.parameters.MaxAdapterSignalCollectionParameters; | |
import com.applovin.mediation.adapters.tapjoy.BuildConfig; | |
import com.applovin.sdk.AppLovinSdk; | |
import com.applovin.sdk.AppLovinSdkUtils; | |
import com.tapjoy.TJActionRequest; | |
import com.tapjoy.TJConnectListener; | |
import com.tapjoy.TJError; | |
import com.tapjoy.TJPlacement; | |
import com.tapjoy.TJPlacementListener; | |
import com.tapjoy.TJPlacementVideoListener; | |
import com.tapjoy.Tapjoy; | |
import com.tapjoy.TapjoyConnectFlag; | |
import org.json.JSONObject; | |
import java.util.HashMap; | |
import java.util.Hashtable; | |
import java.util.Map; | |
/** | |
* Created by Thomas So on October 20 2018 | |
*/ | |
public class TapjoyMediationAdapter | |
extends MediationAdapterBase | |
implements MaxSignalProvider, MaxInterstitialAdapter, MaxRewardedAdapter | |
{ | |
private TJPlacement interstitialPlacement; | |
private TJPlacement rewardedPlacement; | |
// Explicit default constructor declaration | |
public TapjoyMediationAdapter(final AppLovinSdk sdk) { super( sdk ); } | |
@Override | |
public String getSdkVersion() | |
{ | |
return Tapjoy.getVersion(); | |
} | |
@Override | |
public String getAdapterVersion() | |
{ | |
return BuildConfig.VERSION_NAME; | |
} | |
@Override | |
public void onDestroy() {} | |
@Override | |
public void initialize(final MaxAdapterInitializationParameters parameters, final Activity activity, final OnCompletionListener onCompletionListener) | |
{ | |
log( "Initializing Tapjoy SDK..." ); | |
checkExistence( Tapjoy.class, TJPlacement.class ); | |
if ( !Tapjoy.isConnected() ) | |
{ | |
Tapjoy.setDebugEnabled( parameters.isTesting() ); | |
Hashtable<String, Object> connectFlags = new Hashtable<String, Object>( 1 ); | |
connectFlags.put( TapjoyConnectFlag.ENABLE_LOGGING, String.valueOf( parameters.isTesting() ) ); | |
// Update GDPR settings before initialization | |
updateConsentAndActivity( parameters, activity ); | |
String sdkKey = parameters.getServerParameters().getString( "sdk_key" ); | |
Tapjoy.connect( activity, sdkKey, connectFlags, new TJConnectListener() | |
{ | |
@Override | |
public void onConnectSuccess() | |
{ | |
log( "Tapjoy SDK initialized" ); | |
if ( AppLovinSdk.VERSION_CODE >= 90800 ) // 9.8.0+ | |
{ | |
onCompletionListener.onCompletion( InitializationStatus.INITIALIZED_SUCCESS, null ); | |
} | |
else | |
{ | |
onCompletionListener.onCompletion(); | |
} | |
} | |
@Override | |
public void onConnectFailure() | |
{ | |
log( "Tapjoy SDK failed to initialized" ); | |
if ( AppLovinSdk.VERSION_CODE >= 90800 ) // 9.8.0+ | |
{ | |
onCompletionListener.onCompletion( InitializationStatus.INITIALIZED_FAILURE, null ); | |
} | |
else | |
{ | |
onCompletionListener.onCompletion(); | |
} | |
} | |
} ); | |
} | |
else | |
{ | |
updateConsentAndActivity( parameters, activity ); | |
if ( AppLovinSdk.VERSION_CODE >= 90800 ) // 9.8.0+ | |
{ | |
onCompletionListener.onCompletion( InitializationStatus.INITIALIZED_SUCCESS, null ); | |
} | |
else | |
{ | |
onCompletionListener.onCompletion(); | |
} | |
} | |
} | |
@Override | |
public void collectSignal(final MaxAdapterSignalCollectionParameters parameters, final Activity activity, final MaxSignalCollectionListener callback) | |
{ | |
// Update GDPR settings | |
updateConsentAndActivity( parameters, activity ); | |
String token = Tapjoy.getUserToken(); | |
callback.onSignalCollected( token ); | |
} | |
@Override | |
public void loadInterstitialAd(final MaxAdapterResponseParameters parameters, final Activity activity, final MaxInterstitialAdapterListener listener) | |
{ | |
log( "Loading interstitial..." ); | |
if ( !Tapjoy.isConnected() ) | |
{ | |
log( "Tapjoy SDK is not initialized" ); | |
listener.onInterstitialAdLoadFailed( MaxAdapterError.NOT_INITIALIZED ); | |
return; | |
} | |
// Update GDPR settings | |
updateConsentAndActivity( parameters, activity ); | |
interstitialPlacement = createPlacement( activity, parameters, new InterstitialListener( listener ) ); | |
if ( interstitialPlacement != null ) | |
{ | |
interstitialPlacement.requestContent(); | |
} | |
else | |
{ | |
listener.onInterstitialAdLoadFailed( MaxAdapterError.BAD_REQUEST ); | |
} | |
} | |
@Override | |
public void showInterstitialAd(final MaxAdapterResponseParameters parameters, final Activity activity, final MaxInterstitialAdapterListener listener) | |
{ | |
log( "Showing interstitial..." ); | |
if ( interstitialPlacement.isContentReady() ) | |
{ | |
interstitialPlacement.showContent(); | |
} | |
else | |
{ | |
log( "Interstitial ad not ready" ); | |
listener.onInterstitialAdDisplayFailed( MaxAdapterError.AD_NOT_READY ); | |
} | |
} | |
@Override | |
public void loadRewardedAd(final MaxAdapterResponseParameters parameters, final Activity activity, final MaxRewardedAdapterListener listener) | |
{ | |
log( "Loading rewarded ad..." ); | |
if ( !Tapjoy.isConnected() ) | |
{ | |
log( "Tapjoy SDK is not initialized" ); | |
listener.onRewardedAdLoadFailed( MaxAdapterError.NOT_INITIALIZED ); | |
return; | |
} | |
// Update GDPR settings | |
updateConsentAndActivity( parameters, activity ); | |
rewardedPlacement = createPlacement( activity, parameters, new RewardedListener( listener ) ); | |
if ( rewardedPlacement != null ) | |
{ | |
rewardedPlacement.requestContent(); | |
} | |
else | |
{ | |
listener.onRewardedAdLoadFailed( MaxAdapterError.BAD_REQUEST ); | |
} | |
} | |
@Override | |
public void showRewardedAd(final MaxAdapterResponseParameters parameters, final Activity activity, final MaxRewardedAdapterListener listener) | |
{ | |
log( "Showing rewarded ad..." ); | |
if ( rewardedPlacement.isContentReady() ) | |
{ | |
// Configure userReward from server. | |
configureReward( parameters ); | |
rewardedPlacement.showContent(); | |
} | |
else | |
{ | |
log( "Rewarded ad not ready" ); | |
listener.onRewardedAdDisplayFailed( MaxAdapterError.AD_NOT_READY ); | |
} | |
} | |
//region Utility Methods | |
private void updateConsentAndActivity(MaxAdapterParameters parameters, Activity activity) | |
{ | |
Tapjoy.setUserConsent( parameters.hasUserConsent() ? "1" : "0" ); | |
Tapjoy.belowConsentAge( parameters.isAgeRestrictedUser() ); | |
Tapjoy.setActivity( activity ); | |
} | |
private TJPlacement createPlacement(Activity activity, MaxAdapterResponseParameters parameters, TJPlacementListener listener) | |
{ | |
TJPlacement placement = new TJPlacement( activity, parameters.getThirdPartyAdPlacementId(), listener ); | |
placement.setMediationName( "applovin" ); | |
placement.setAdapterVersion( BuildConfig.VERSION_NAME ); | |
if ( listener instanceof TJPlacementVideoListener ) | |
{ | |
placement.setVideoListener( (TJPlacementVideoListener) listener ); | |
} | |
if ( !TextUtils.isEmpty( parameters.getBidResponse() ) ) | |
{ | |
try | |
{ | |
JSONObject auctionData = new JSONObject( parameters.getBidResponse() ); | |
Map<String, String> auctionDataMap = AppLovinSdkUtils.toMap( auctionData ); | |
placement.setAuctionData( (HashMap) auctionDataMap ); | |
} | |
catch ( Throwable th ) | |
{ | |
log( "Failed to load ad due to JSON deserialization error: ", th ); | |
return null; | |
} | |
} | |
return placement; | |
} | |
private MaxAdapterError maxErrorCodeFromTapjoyError(TJError error) | |
{ | |
final int adapterError; | |
if ( error != null ) | |
{ | |
adapterError = error.code; | |
} | |
else | |
{ | |
adapterError = 0; | |
} | |
final int errorCode; | |
if ( adapterError == 204 ) | |
{ | |
errorCode = MaxAdapterError.ERROR_CODE_NO_FILL; | |
} | |
else if ( adapterError == -1011 ) | |
{ | |
errorCode = MaxAdapterError.ERROR_CODE_NOT_INITIALIZED; | |
} | |
else | |
{ | |
errorCode = MaxAdapterError.ERROR_CODE_UNSPECIFIED; | |
} | |
return new MaxAdapterError( errorCode, adapterError ); | |
} | |
//endregion | |
private class InterstitialListener | |
implements TJPlacementListener, TJPlacementVideoListener | |
{ | |
final MaxInterstitialAdapterListener listener; | |
InterstitialListener(MaxInterstitialAdapterListener listener) | |
{ | |
this.listener = listener; | |
} | |
@Override | |
public void onRequestSuccess(final TJPlacement tjPlacement) | |
{ | |
if ( tjPlacement.isContentAvailable() ) | |
{ | |
log( "Interstitial request succeeded" ); | |
} | |
else | |
{ | |
log( "Interstitial request failed" ); | |
listener.onInterstitialAdLoadFailed( MaxAdapterError.NO_FILL ); | |
} | |
} | |
@Override | |
public void onRequestFailure(final TJPlacement tjPlacement, final TJError tjError) | |
{ | |
log( "Interstitial failed to load with error code: " + tjError.code + " and message: " + tjError.message ); | |
MaxAdapterError adapterErrorCode = maxErrorCodeFromTapjoyError( tjError ); | |
listener.onInterstitialAdLoadFailed( adapterErrorCode ); | |
} | |
@Override | |
public void onContentReady(final TJPlacement tjPlacement) | |
{ | |
log( "Interstitial loaded" ); | |
listener.onInterstitialAdLoaded(); | |
} | |
@Override | |
public void onContentShow(final TJPlacement tjPlacement) | |
{ | |
log( "Interstitial shown" ); | |
} | |
@Override | |
public void onClick(final TJPlacement tjPlacement) | |
{ | |
log( "Interstitial clicked" ); | |
listener.onInterstitialAdClicked(); | |
} | |
@Override | |
public void onContentDismiss(final TJPlacement tjPlacement) | |
{ | |
log( "Interstitial hidden" ); | |
listener.onInterstitialAdHidden(); | |
} | |
@Override | |
public void onVideoStart(final TJPlacement tjPlacement) | |
{ | |
log( "Interstitial video start" ); | |
listener.onInterstitialAdDisplayed(); | |
} | |
@Override | |
public void onVideoError(final TJPlacement tjPlacement, final String s) | |
{ | |
log( "Interstitial failed with error message: " + s ); | |
MaxAdapterError adapterError = new MaxAdapterError( MaxAdapterError.ERROR_CODE_UNSPECIFIED, s ); | |
listener.onInterstitialAdDisplayFailed( adapterError ); | |
} | |
@Override | |
public void onVideoComplete(final TJPlacement tjPlacement) | |
{ | |
log( "Interstitial video completed" ); | |
} | |
@Override | |
public void onPurchaseRequest(final TJPlacement tjPlacement, final TJActionRequest tjActionRequest, final String s) {} | |
@Override | |
public void onRewardRequest(final TJPlacement tjPlacement, final TJActionRequest tjActionRequest, final String s, final int i) {} | |
} | |
private class RewardedListener | |
implements TJPlacementListener, TJPlacementVideoListener | |
{ | |
final MaxRewardedAdapterListener listener; | |
private boolean hasGrantedReward; | |
RewardedListener(MaxRewardedAdapterListener listener) | |
{ | |
this.listener = listener; | |
} | |
@Override | |
public void onRequestSuccess(final TJPlacement tjPlacement) | |
{ | |
if ( tjPlacement.isContentAvailable() ) | |
{ | |
log( "Rewarded request succeeded" ); | |
} | |
else | |
{ | |
log( "Rewarded request failed" ); | |
listener.onRewardedAdLoadFailed( MaxAdapterError.NO_FILL ); | |
} | |
} | |
@Override | |
public void onRequestFailure(final TJPlacement tjPlacement, final TJError tjError) | |
{ | |
log( "Rewarded failed to load with error code: " + tjError.code + " and message: " + tjError.message ); | |
MaxAdapterError adapterErrorCode = maxErrorCodeFromTapjoyError( tjError ); | |
listener.onRewardedAdLoadFailed( adapterErrorCode ); | |
} | |
@Override | |
public void onContentReady(final TJPlacement tjPlacement) | |
{ | |
log( "Rewarded loaded" ); | |
listener.onRewardedAdLoaded(); | |
} | |
@Override | |
public void onContentShow(final TJPlacement tjPlacement) | |
{ | |
log( "Rewarded shown" ); | |
listener.onRewardedAdDisplayed(); | |
} | |
@Override | |
public void onClick(final TJPlacement tjPlacement) | |
{ | |
log( "Rewarded clicked" ); | |
listener.onRewardedAdClicked(); | |
} | |
@Override | |
public void onContentDismiss(final TJPlacement tjPlacement) | |
{ | |
if ( hasGrantedReward || shouldAlwaysRewardUser() ) | |
{ | |
final MaxReward reward = getReward(); | |
log( "Rewarded user with reward: " + reward ); | |
listener.onUserRewarded( reward ); | |
} | |
log( "Rewarded hidden" ); | |
listener.onRewardedAdHidden(); | |
} | |
@Override | |
public void onPurchaseRequest(final TJPlacement tjPlacement, final TJActionRequest tjActionRequest, final String s) {} | |
@Override | |
public void onRewardRequest(final TJPlacement tjPlacement, final TJActionRequest tjActionRequest, final String s, final int i) {} | |
@Override | |
public void onVideoStart(final TJPlacement tjPlacement) | |
{ | |
log( "Rewarded video began" ); | |
listener.onRewardedAdVideoStarted(); | |
} | |
@Override | |
public void onVideoError(final TJPlacement tjPlacement, final String s) | |
{ | |
log( "Rewarded failed with error message: " + s ); | |
MaxAdapterError adapterError = new MaxAdapterError( MaxAdapterError.ERROR_CODE_UNSPECIFIED, s ); | |
listener.onRewardedAdDisplayFailed( adapterError ); | |
} | |
@Override | |
public void onVideoComplete(final TJPlacement tjPlacement) | |
{ | |
log( "Rewarded video completed" ); | |
listener.onRewardedAdVideoCompleted(); | |
hasGrantedReward = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment