TubiVPAID.java


Overview

`TubiVPAID` is a helper class designed to enable playback of **VPAID (Video Player Ad-Serving Interface Definition)** ads within an Android native application by embedding a **WebView**. Since VPAID ads are primarily built for mobile web environments and rely heavily on JavaScript interaction, this class manages a WebView configured to load a VPAID-compliant ad player hosted remotely (e.g., on tubitv.com). It acts as a bridge between the native Android media playback finite state machine (`FsmPlayer`) and the JavaScript environment running the VPAID ad, facilitating two-way communication and lifecycle management.

The class implements the [VpaidClient](/projects/288/68374) interface and manages:

This design ensures seamless integration of interactive VPAID ads within the native media playback workflow controlled by the FSM.


Detailed Explanation

Class: TubiVPAID

Implements the [VpaidClient](/projects/288/68374) interface, encapsulating all logic necessary to play VPAID ads inside a WebView and communicate with the FSM player.

Properties

Property

Type

Description

`TAG`

`String`

Log tag for debugging and error messages

`mVPAIDWebView`

`WebView`

The WebView instance hosting the VPAID player

`mHandler`

`Handler`

UI thread handler to post runnable actions

`vastXml`

`String`

VAST XML ad data string fetched from `Vastxml`

`fsmPlayer`

`FsmAdController`

Reference to the FSM player managing ad states

Constructor

public TubiVPAID(@NonNull WebView webView, @NonNull Handler uiHandler, @NonNull FsmPlayer fsmPlayer)
WebView vpaidWebView = findViewById(R.id.vpaidWebView);
Handler uiHandler = new Handler(Looper.getMainLooper());
FsmPlayer fsmPlayer = ...; // Obtain FSM player instance
TubiVPAID vpaidClient = new TubiVPAID(vpaidWebView, uiHandler, fsmPlayer);

Method: init

@SuppressLint("SetJavaScriptEnabled")
@TargetApi(21)
public void init(@Nullable MediaModel mediaModel)
vpaidClient.init(null);

JavaScript Interface Methods

These methods are exposed to JavaScript inside the WebView via the `@JavascriptInterface` annotation, allowing the VPAID ad player to communicate with the native app.

notifyVideoEnd
@JavascriptInterface
public void notifyVideoEnd()
window.TubiNativeJSInterface.notifyVideoEnd();

notifyAdError
@JavascriptInterface
public void notifyAdError(int code, String error)
window.TubiNativeJSInterface.notifyAdError(404, "Ad creative not found");

getVastXml
@JavascriptInterface
public String getVastXml()
var vastXml = window.TubiNativeJSInterface.getVastXml();

Inner Classes

VPAidWebViewClient (extends WebViewClient)

Custom WebViewClient to handle URL loading and error handling specifically for the VPAID WebView.


VPAIDWebChromeClient (extends WebChromeClient)

Custom WebChromeClient to manage video loading UI and permission requests.


Important Implementation Details


Interaction with Other System Components


Usage Flow Summary

  1. Initialization:
    The TubiVPAID client is instantiated with a WebView, UI thread handler, and FSM player.

  2. WebView Configuration:
    init() configures the WebView and loads an empty URL to reset it.

  3. JavaScript Interface Setup:
    The JavaScript interface (TubiNativeJSInterface) is added to the WebView, exposing native methods to JS.

  4. VPAID Ad Loading:
    The WebView loads a URL hosting the VPAID ad player that calls back the native interface for VAST XML and event notifications.

  5. Ad Playback and Events:
    The JS player requests VAST XML, plays the ad, and notifies notifyVideoEnd() or notifyAdError() on completion or error.

  6. FSM Transition:
    Upon notification, the TubiVPAID client posts a runnable to the UI thread that calls fsmPlayer.removePlayedAdAndTransitToNextState() to continue playback.


Visual Diagram: Class Structure of TubiVPAID

classDiagram
    class TubiVPAID {
        -static final String TAG
        -WebView mVPAIDWebView
        -Handler mHandler
        -String vastXml
        -FsmAdController fsmPlayer
        +TubiVPAID(WebView, Handler, FsmPlayer)
        +void init(MediaModel)
        +void notifyVideoEnd()
        +void notifyAdError(int, String)
        +String getVastXml()
    }

    class VPAidWebViewClient {
        +boolean shouldOverrideUrlLoading(WebView, String)
        +void onReceivedError(WebView, int, String, String)
        +void onReceivedError(WebView, WebResourceRequest, WebResourceError)
    }

    class VPAIDWebChromeClient {
        -View mVideoProgressView
        +VPAIDWebChromeClient()
        +VPAIDWebChromeClient(WebView, View)
        +View getVideoLoadingProgressView()
        +void onPermissionRequest(PermissionRequest)
    }

    TubiVPAID --> VPAidWebViewClient : uses
    TubiVPAID --> VPAIDWebChromeClient : uses

Summary

The `TubiVPAID` class is a specialized client that enables playback of interactive VPAID video ads within an Android application by embedding a WebView configured for this purpose. It manages the WebView lifecycle, configures necessary settings, and exposes a JavaScript interface for the VPAID ad player to communicate ad playback events and request VAST XML data. The class integrates tightly with the media player's finite state machine (`FsmPlayer`), enabling smooth transitions between content playback and VPAID ads, handling ad completion and error events gracefully, and ensuring the user experience remains seamless. The class's design encapsulates complexity related to WebView handling and JavaScript communication, thereby isolating VPAID ad functionality from the rest of the media player system.


End of Documentation for TubiVPAID.java