Android SDK usage
Initialize
The YsoNetwork.initialize
method is used to initialize the YsoNetwork SDK.
Code Example
if (!YsoNetwork.isInitialize()) {
try {
Application application = ( activity != null )
? activity.getApplication()
: (Application) getApplicationContext();
YsoNetwork.initialize(application);
/* mediation logic on sdk initialize success */
} catch (Exception e) {
/* mediation logic on sdk initialize fail */
}
YNLog.ProdInfo("YSO Network Adapter SDK " + ADAPTER_VERSION);
} else {
/* mediation logic on sdk already initialized */
}
Collect Signal
The YsoNetwork.getSignal
method is used to collect a signal from the YsoNetwork SDK, typically used for filling the bid request.
Code Example
String signal = YsoNetwork.getSignal();
Usage
Call YsoNetwork.getSignal
to retrieve the signal string used to fill the bid request.
Get SDK Version
The YsoNetwork.getSdkVersion
method is used to retrieve the version of the YsoNetwork SDK.
Code Example
String sdkVersion = YsoNetwork.getSdkVersion();
Usage
Call YsoNetwork.getSdkVersion
to get the current SDK version string for logging or display purposes, as well as filling the bid request.
Load Interstitial
The YsoNetwork.interstitialLoad
method is used to load an interstitial ad. It takes a String placementId
and a String adResponse
as parameters, along with a callback function to handle the result of the load operation.
Code Example
YsoNetwork.interstitialLoad(placementId, adResponse, error -> {
if (error == YNEnumActionError.None) {
/* mediation load success logic */
} else {
/* mediation load error logic */
}
});
Usage
Call YsoNetwork.interstitialLoad
to initiate loading an interstitial ad with the specified placementId
and adResponse
. Implement the success and error handling logic within the callback function.
Parameters
placementId
: The identifier for the interstitial ad placement created on the YSO Network dashboard.
adResponse
: The ad response from the bidding process, for example bidResponse.seatbid[0].bid[0].ext.signaldata
.
errorCallback
: The error parameter passed to the callback function to indicate success or the type of error that occurred.
Show Interstitial
The YsoNetwork.interstitialShow
method is used to display an interstitial ad. It takes a String placementId
and an instance of YNManager.ActionDisplay
to handle different ad display events, along with an Activity activity
context.
Code Example
YsoNetwork.interstitialShow(placementId, new YNManager.ActionDisplay() {
@Override
public void onDisplay(View view) {
/* mediation logic on ad display */
}
@Override
public void onClick() {
/* mediation logic on ad click */
}
@Override
public void onClose(boolean display, boolean complete) {
if (display) {
/* mediation logic on ad successfully closed */
} else {
/* mediation logic on ad display error */
}
}
}, activity);
Usage
Call YsoNetwork.interstitialShow
to display an interstitial ad with the specified placementId
. Implement the YNManager.ActionDisplay
interface to define logic for handling ad display, click, and close events.
Parameters
placementId
: The identifier for the interstitial ad placement created on the YSO Network dashboard.
YNManager.ActionDisplay
: An interface that includes callback methods for ad events:
onDisplay(View view)
: Called when the ad is displayed.
onClick()
: Called when the ad is clicked.
onClose(boolean display, boolean complete)
: Called when the ad is closed, with parameters indicating if the ad was displayed and if the ad view was fully completed.
activity
: The current activity context in which the interstitial ad will be displayed.
Load Rewarded
The YsoNetwork.rewardedLoad
method is used to load a rewarded ad. It takes a String placementId
and a String adResponse
as parameters, along with a callback function to handle the result of the load operation.
Code Example
YsoNetwork.rewardedLoad(placementId, adResponse, error -> {
if (error == YNEnumActionError.None) {
/* mediation load success logic */
} else {
/* mediation load error logic */
}
});
Usage
Call YsoNetwork.rewardedLoad
to initiate loading a rewarded ad with the specified placementId
and adResponse
. Implement the success and error handling logic within the callback function.
Parameters
placementId
: The identifier for the rewarded ad placement created on the YSO Network dashboard.
adResponse
: The ad response from the bidding process, for example bidResponse.seatbid[0].bid[0].ext.signaldata
.
errorCallback
: The error parameter passed to the callback function to indicate success or the type of error that occurred.
Show Rewarded
The YsoNetwork.rewardedShow
method is used to display a rewarded ad. It takes a String placementId
and an instance of YNManager.ActionDisplay
to handle different ad display events, along with an Activity activity
context.
Code Example
YsoNetwork.rewardedShow(placementId, new YNManager.ActionDisplay() {
@Override
public void onDisplay(View view) {
/* mediation logic on ad display */
}
@Override
public void onClick() {
/* mediation logic on ad click */
}
@Override
public void onClose(boolean display, boolean complete) {
if (display) {
if (complete) {
/* mediation logic to reward the user */
}
/* mediation logic on ad successfully closed */
} else {
/* mediation logic on ad display error */
}
}
}, activity);
Usage
Call YsoNetwork.rewardedShow
to display a rewarded ad with the specified placementId
. Implement the YNManager.ActionDisplay
interface to define logic for handling ad display, click, and close events, including rewarding the user if the ad is watched completely.
Parameters
placementId
: The identifier for the banner ad placement created on the YSO Network dashboard.
YNManager.ActionDisplay
: An interface that includes callback methods for ad events:
onDisplay(View view)
: Called when the ad is displayed.
onClick()
: Called when the ad is clicked.
onClose(boolean display, boolean complete)
: Called when the ad is closed, with parameters indicating if the ad was displayed and if the ad view was fully completed.
activity
: The current activity context in which the banner ad will be displayed.
Load banner
The YsoNetwork.bannerLoad
method is used to load a banner ad. It takes a String placementId
and a String adResponse
as parameters, and it includes a callback function to handle the result of the load operation. The callback function processes the outcome by checking the error
parameter. If error
equals YNEnumActionError.None
, it indicates a successful load, otherwise, it handles the error scenario.
Code Example
YsoNetwork.bannerLoad(placementId, adResponse, error -> {
if (error == YNEnumActionError.None) {
/* mediation load success logic */
} else {
/* mediation load error logic */
}
});
Usage
Call YsoNetwork.bannerLoad
to initiate the loading of a banner ad with the specified placementId
and adResponse
. Implement the success and error handling logic within the callback function to manage the different outcomes of the load operation.
Parameters
placementId
: The identifier for the banner ad placement created on the YSO Network dashboard.
adResponse
: The ad response from the bidding process, for example bidResponse.seatbid[0].bid[0].ext.signaldata
.
errorCallback
: The error parameter passed to the callback function to indicate success or the type of error that occurred.
Show Banner
The YsoNetwork.bannerShow
method is used to display a banner ad. It takes a String placementId
and an instance of YNManager.ActionDisplay
which includes callback methods to handle different ad display events. These events include when the ad is displayed, clicked, or closed. The method also takes an Activity activity
parameter, which represents the current activity context.
Code Example
YsoNetwork.bannerShow(placementId, new YNManager.ActionDisplay() {
@Override
public void onDisplay(View view) {
/* mediation logic on ad display */
}
@Override
public void onClick() {
/* mediation logic on ad click */
}
@Override
public void onClose(boolean display, boolean complete) {
if (display) {
/* mediation logic on ad successfully closed */
} else {
/* mediation logic on ad display error */
}
}
}, activity);
Usage
Call YsoNetwork.bannerShow
to display a banner ad with the specified String placementId
. Implement the YNManager.ActionDisplay
interface to define the logic for handling ad display, click, and close events.
Parameters
placementId
: The identifier for the banner ad placement created on the YSO Network dashboard.
YNManager.ActionDisplay
: An interface that includes callback methods for ad events:
onDisplay(View view)
: Called when the ad is displayed.
onClick()
: Called when the ad is clicked.
onClose(boolean display, boolean complete)
: Called when the ad is closed, with parameters indicating if the ad was displayed and if the ad view was fully completed.
activity
: The current activity context in which the banner ad will be displayed.
Cleanup
YsoNetwork.destroy();
The YsoNetwork.destroy()
method is called to perform cleanup operations. This method is responsible for releasing any resources, closing connections, and performing any other necessary tear down tasks for the YsoNetwork
class. It ensures that the network is properly shut down and any associated resources are freed, preventing potential memory leaks and other issues.
Ensure that YsoNetwork.destroy()
is called when the network operations are no longer needed, typically during the application's shutdown process or when the network component is being reinitialized.
Error types
YNEnumActionError.SdkNotInitialized
: sdk not initialized
YNEnumActionError.InvalidRequest
: bad request data sent to SDK
YNEnumActionError.InvalidConfig
: invalid ad configuration
YNEnumActionError.Load
: ad load error
YNEnumActionError.Timeout
: timeout loading the ad
YNEnumActionError.Server
: error in the server response
YNEnumActionError.Internal
: other error
Full example adapter
package com.mediation;
import android.app.Activity;
import android.app.Application;
import android.view.View;
import com.mediation.adapters.MediationAdapterBase;
import com.mediation.ysonetwork.BuildConfig;
import com.ysocorp.ysonetwork.utils.YNLog;
import com.ysocorp.ysonetwork.YNManager;
import com.ysocorp.ysonetwork.YsoNetwork;
import com.ysocorp.ysonetwork.enums.YNEnumActionError;
public class YsoNetworkMediationAdapter extends MediationAdapterBase {
public static final String ADAPTER_VERSION = BuildConfig.YSO_NETWORK_ADAPTER_VERSION;
private static InitializationStatus status;
public YsoNetworkMediationAdapter() {
super();
}
@Override
public void onDestroy() {
YsoNetwork.destroy();
}
@Override
public void initialize(AdapterInitializationParameters adapterInitializationParameters, Activity activity, OnCompletionListener listener) {
if (!YsoNetwork.isInitialize()) {
try {
// NOTE: `activity` can only be null in 11.1.0+, and `getApplicationContext()` is introduced in 11.1.0
Application application = ( activity != null ) ? activity.getApplication() : (Application) getApplicationContext();
YsoNetwork.initialize(application);
YsoNetworkMediationAdapter.status = InitializationStatus.INITIALIZED_SUCCESS;
listener.onCompletion(YsoNetworkMediationAdapter.status, null);
} catch (Exception e) {
YsoNetworkMediationAdapter.status = InitializationStatus.INITIALIZED_FAILURE;
listener.onCompletion(YsoNetworkMediationAdapter.status, e.getMessage());
}
YNLog.ProdInfo("YSO Network Adapter SDK " + ADAPTER_VERSION);
} else {
listener.onCompletion(YsoNetworkMediationAdapter.status, null);
}
}
private static AdapterError ActionErrorToAdapterError(YNEnumActionError error) {
switch (error) {
case SdkNotInitialized:
return AdapterError.NOT_INITIALIZED;
case InvalidRequest:
return AdapterError.BAD_REQUEST;
case InvalidConfig:
return AdapterError.INVALID_CONFIGURATION;
case Load:
return AdapterError.INVALID_LOAD_STATE;
case Timeout:
return AdapterError.TIMEOUT;
case Server:
return AdapterError.SERVER_ERROR;
case Internal:
default:
return AdapterError.INTERNAL_ERROR;
}
}
/************************** INTERSTITIAL **************************/
@Override
public void loadInterstitialAd(final String placementId, final String bidResponse, Activity activity, final InterstitialAdapterListener listener) {
YsoNetwork.interstitialLoad(placementId, bidResponse, error -> {
if (error == YNEnumActionError.None) {
listener.onInterstitialAdLoaded();
} else {
listener.onInterstitialAdLoadFailed(YsoNetworkMediationAdapter.ActionErrorToAdapterError(error));
}
});
}
@Override
public void showInterstitialAd(final String placementId, Activity activity, final InterstitialAdapterListener listener) {
YsoNetwork.interstitialShow(placementId, new YNManager.ActionDisplay() {
@Override
public void onDisplay(View view) {
listener.onInterstitialAdDisplayed();
}
@Override
public void onClick() {
listener.onInterstitialAdClicked();
}
@Override
public void onClose(boolean display, boolean complete) {
if (display) {
listener.onInterstitialAdHidden();
} else {
listener.onInterstitialAdDisplayFailed(AdapterError.AD_DISPLAY_FAILED);
}
}
}, activity);
}
/************************** REWARDED **************************/
@Override
public void loadRewardedAd(final String placementId, final String bidResponse, Activity activity, final RewardedAdapterListener listener) {
YsoNetwork.rewardedLoad(placementId, bidResponse, error -> {
if (error == YNEnumActionError.None) {
listener.onRewardedAdLoaded();
} else {
listener.onRewardedAdLoadFailed(YsoNetworkMediationAdapter.ActionErrorToAdapterError(error));
}
});
}
@Override
public void showRewardedAd(final String placementId, Activity activity, final RewardedAdapterListener listener) {
YsoNetwork.rewardedShow(placementId, new YNManager.ActionDisplay() {
@Override
public void onDisplay(View view) {
listener.onRewardedAdDisplayed();
}
@Override
public void onClick() {
listener.onRewardedAdClicked();
}
@Override
public void onClose(boolean display, boolean complete) {
if (display) {
if (complete) {
final Reward reward = getReward();
listener.onUserRewarded( reward );
}
listener.onRewardedAdHidden();
} else {
listener.onRewardedAdDisplayFailed(AdapterError.AD_DISPLAY_FAILED);
}
}
}, activity);
}
/************************** BANNER **************************/
/**
* This method loads a banner from a bid response
* - placementId: the placement id generated from YSO Network's dashboard
* - bidResponse: in the response from our servers to the bidrequest you sent, this corresponds to bidResponse.seatbid[0].bid[0].ext.signaldata
*/
@Override
public void loadBanner(String placementId, final String bidResponse, Activity activity, MBannerAdapterListener listener) {
YsoNetwork.bannerLoad(placementId, bidResponse, error -> {
if (error == YNEnumActionError.None) {
YsoNetwork.bannerShow(placementId, new YNManager.ActionDisplay() {
@Override
public void onDisplay(View view) {
listener.onBannerAdLoaded(view);
listener.onBannerAdDisplayed();
}
@Override
public void onClick() {
listener.onBannerAdClicked();
}
@Override
public void onClose(boolean display, boolean complete) {
if (display) {
listener.onBannerAdHidden();
} else {
listener.onBannerAdDisplayFailed(AdapterError.AD_DISPLAY_FAILED);
}
}
}, activity);
} else {
listener.onABannerAdLoadFailed(YsoNetworkMediationAdapter.ActionErrorToAdapterError(error));
}
});
}
/************************** OTHER **************************/
/**
* This method collects data necessary to our bidding process, which should be sent with every bid request
*/
@Override
public void collectSignal(SignalCollectionListener listener) {
String signal = YsoNetwork.getSignal();
listener.onSignalCollected(signal);
}
/**
* This method collects the SDK version. It needs to be sent with the bid request
*/
@Override
public String getSdkVersion() {
String sdkVersion = YsoNetwork.getSdkVersion();
return sdkVersion;
}
/**
* This method collects the Adapter version. It needs to be sent with the bid request
*/
@Override
public String getAdapterVersion() {
String adapterVersion = YsoNetworkMediationAdapter.ADAPTER_VERSION;
return adapterVersion;
}
}
Last modified: 26 July 2024