Skip to content

SDK Integration ​

Adding the SDK ​

TruexAdRenderer-Android is distributed through the Infillion Maven repository:

  • Repository: https://s3.amazonaws.com/android.truex.com/tar/prod/maven
  • Group ID: com.truex
  • Artifact ID: TruexAdRenderer-Android
  • Version: 2.10.4
kotlin
dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
        maven("https://s3.amazonaws.com/android.truex.com/tar/prod/maven")
    }
}
groovy
dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
        maven { url "https://s3.amazonaws.com/android.truex.com/tar/prod/maven" }
    }
}
kotlin
dependencies {
    implementation("com.truex:TruexAdRenderer-Android:2.10.4")
}
groovy
dependencies {
    implementation 'com.truex:TruexAdRenderer-Android:2.10.4'
}

The SDK classes are in the com.truex.adrenderer package:

  • TruexAdRenderer - renders the ad and is controlled by your app
  • TruexAdOptions - configuration options for init
  • TruexAdEvent - the events the renderer fires, see Events
  • IEventEmitter.IEventHandler - the event listener interface

Renderer Container ​

The renderer shows the ad in a ViewGroup that you provide. Place a full-screen container above your video player:

xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.media3.ui.PlayerView
        android:id="@+id/playerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <FrameLayout
        android:id="@+id/adContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

Hide as much other UI as possible while the ad is showing, including player controls and status bars.

Quick Example ​

A complete start of an Infillion ad, where adParameters is the JSON resolved from the VAST response:

kotlin
private var truexAdRenderer: TruexAdRenderer? = null
private var truexAdCreditReceived = false

private val truexAdEventHandler = IEventEmitter.IEventHandler { event, data ->
    when (event) {
        TruexAdEvent.AD_FREE_POD -> {
            // Remember the reward, act on it on AD_COMPLETED
            truexAdCreditReceived = true
        }
        TruexAdEvent.AD_COMPLETED -> {
            finishInfillionAd(skipRemainingAds = truexAdCreditReceived)
        }
        TruexAdEvent.AD_ERROR,
        TruexAdEvent.NO_ADS_AVAILABLE -> {
            finishInfillionAd(skipRemainingAds = false)
        }
        TruexAdEvent.USER_CANCEL_STREAM -> {
            exitPlayback()
        }
        else -> Unit
    }
}

private fun startInfillionAd(adParameters: JSONObject) {
    truexAdCreditReceived = false
    val options = TruexAdOptions().apply {
        supportsUserCancelStream = true
        appId = packageName
        enableWebViewDebugging = BuildConfig.DEBUG
    }
    truexAdRenderer = TruexAdRenderer(this).also { renderer ->
        renderer.addEventListener(null, truexAdEventHandler)
        renderer.init(adParameters, options)
        renderer.start(binding.adContainer)
    }
}

private fun disposeRenderer() {
    truexAdRenderer?.removeEventListener(null, truexAdEventHandler)
    truexAdRenderer?.stop()
    truexAdRenderer = null
}
java
private TruexAdRenderer truexAdRenderer;
private boolean truexAdCreditReceived = false;

private final IEventEmitter.IEventHandler truexAdEventHandler = (event, data) -> {
    switch (event) {
        case AD_FREE_POD:
            // Remember the reward, act on it on AD_COMPLETED
            truexAdCreditReceived = true;
            break;
        case AD_COMPLETED:
            finishInfillionAd(truexAdCreditReceived);
            break;
        case AD_ERROR:
        case NO_ADS_AVAILABLE:
            finishInfillionAd(false);
            break;
        case USER_CANCEL_STREAM:
            exitPlayback();
            break;
        default:
            break;
    }
};

private void startInfillionAd(JSONObject adParameters) {
    truexAdCreditReceived = false;
    TruexAdOptions options = new TruexAdOptions();
    options.supportsUserCancelStream = true;
    options.appId = getPackageName();
    options.enableWebViewDebugging = BuildConfig.DEBUG;

    truexAdRenderer = new TruexAdRenderer(this);
    truexAdRenderer.addEventListener(null, truexAdEventHandler);
    truexAdRenderer.init(adParameters, options);
    truexAdRenderer.start(adContainer);
}

private void disposeRenderer() {
    if (truexAdRenderer != null) {
        truexAdRenderer.removeEventListener(null, truexAdEventHandler);
        truexAdRenderer.stop();
        truexAdRenderer = null;
    }
}

finishInfillionAd and exitPlayback are your app's code - what they have to do is described in Handling Terminal Events.

TruexAdRenderer Methods ​

init ​

kotlin
fun init(adParameters: JSONObject)
fun init(adParameters: JSONObject, options: TruexAdOptions)
java
public void init(JSONObject adParameters)
public void init(JSONObject adParameters, TruexAdOptions options)

Initializes the renderer with the adParameters JSON and starts the ad request to the Infillion ad server. The renderer fires AD_FETCH_COMPLETED when the request is done.

You can call init early, a few seconds before the ad break starts, to give the renderer more time for the request, and call start once the player reaches the ad.

The parameters are:

start ​

kotlin
fun start(baseView: ViewGroup)
java
public void start(ViewGroup baseView)

Shows the ad to the user. Call it when your app is ready to display the ad; it can be called any time after init. If the ad request from init is not finished yet, the renderer waits for it, then adds its view to baseView and fires AD_STARTED.

After calling start, wait for a terminal event and keep track of whether AD_FREE_POD has fired.

  • baseView: the ViewGroup the renderer adds its view to. It should cover the entire screen.

stop ​

kotlin
fun stop()
java
public void stop()

Stops the renderer immediately and releases its resources. Call it after a terminal event, and also when:

  • the user leaves the playback screen while the ad is showing
  • an unrelated error requires your app to stop the ad
  • your own timeout for AD_FETCH_COMPLETED or AD_STARTED is reached

A stopped renderer cannot be resumed or reused - remove all references to it.

pause ​

kotlin
fun pause()
java
public void pause()

Pauses the ad's videos, audio, and timers. Call it when your app is paused, for example from onPause, and when your app shows a popup for POPUP_WEBSITE.

resume ​

kotlin
fun resume()
java
public void resume()

Resumes a paused renderer.

addEventListener / removeEventListener ​

kotlin
fun addEventListener(event: TruexAdEvent?, handler: IEventEmitter.IEventHandler)
fun removeEventListener(event: TruexAdEvent?, handler: IEventEmitter.IEventHandler)
java
public void addEventListener(TruexAdEvent event, IEventEmitter.IEventHandler handler)
public void removeEventListener(TruexAdEvent event, IEventEmitter.IEventHandler handler)

Registers or removes a listener for one event. Pass null as the event to register one handler for all events, as in the Quick Example. See Events for the full list.

TruexAdOptions ​

OptionDefaultDescription
supportsUserCancelStreamfalseIf true, a Back press during the ad fires USER_CANCEL_STREAM, so your app can exit the stream. If false, Back on a TrueX choice card fires OPT_OUT and then AD_COMPLETED, and Back during an IDVx ad does nothing.
appIdpackage nameUsed for internal tracking. Computed from the context's getPackageName() if not set.
enableWebViewDebuggingfalseIf true, remote WebView debugging through chrome://inspect is enabled, even for release builds. Otherwise it is allowed only for debug builds and for QA ads (from qa-get.truex.com).

Activity Lifecycle ​

Forward your activity lifecycle to the renderer while it is active, and release it when the activity is destroyed:

kotlin
override fun onPause() {
    if (truexAdRenderer != null) {
        truexAdRenderer?.pause()
    } else {
        player.pause()
    }
    super.onPause()
}

override fun onResume() {
    super.onResume()
    if (truexAdRenderer != null) {
        truexAdRenderer?.resume()
    } else {
        player.play()
    }
}

override fun onDestroy() {
    disposeRenderer()
    player.release()
    super.onDestroy()
}
java
@Override
protected void onPause() {
    if (truexAdRenderer != null) {
        truexAdRenderer.pause();
    } else {
        player.pause();
    }
    super.onPause();
}

@Override
protected void onResume() {
    super.onResume();
    if (truexAdRenderer != null) {
        truexAdRenderer.resume();
    } else {
        player.play();
    }
}

@Override
protected void onDestroy() {
    disposeRenderer();
    player.release();
    super.onDestroy();
}

Integration documentation for Infillion Ads