WebviewActivity.java
Overview
`WebviewActivity.java` defines an Android `Activity` that serves as a container for rendering VPAID (Video Player Ad-Serving Interface Definition) ads using a `WebView`. This activity initializes the WebView environment, integrates it with a native VPAID client (`TubiVPAID`), and manages user interactions such as back navigation. It acts as the UI host for VPAID ads, bridging the gap between native Android components and the interactive JavaScript ad content loaded in the WebView.
Class: WebviewActivity
Purpose
`WebviewActivity` manages the lifecycle and setup of the WebView component that displays VPAID ads. It initializes the `TubiVPAID` client with the WebView and a UI thread handler, configures the WebView for JavaScript interface communication, and loads the VPAID ad content URL.
Class Structure
Property | Type | Description |
|---|---|---|
`VPAID_URL` | `String` | URL of the web page hosting the VPAID ads (`http://tubitv.com/`) |
`tubiVPAID` | `TubiVPAID` | Native client managing VPAID ad playback via WebView interface |
`myHandler` | `Handler` | Android Handler for posting UI thread tasks |
`webView` | `WebView` | Android WebView component for rendering ads |
Methods
protected void onCreate(@Nullable Bundle savedInstanceState)
Description:
Android lifecycle method called when the activity is created. Sets the content view, initializes the WebView and Handler, and triggers the setup of the VPAID WebView environment.Parameters:
savedInstanceState: Bundle containing the activity's previously saved state (nullable).
Annotations:
@RequiresApi(Build.VERSION_CODES.KITKAT) - Requires API level 19 or higher for WebView features.
Usage Example:
Called automatically by the Android framework when the activity starts.
private void initVpaidWebview(WebView view, Handler handler)
Description:
Initializes theTubiVPAIDclient with the provided WebView and Handler. Sets up the JavaScript interface binding, makes the WebView visible and focused, and loads the VPAID ad URL.Parameters:
view: The WebView instance where ads will be displayed.handler: Handler to post UI thread operations.
Annotations:
@RequiresApi(Build.VERSION_CODES.KITKAT) - Requires API level 19 or higher.
Implementation Details:
Instantiates
TubiVPAIDwith the WebView and Handler.Calls tubiVPAID.init(null) to initialize without a specific ad model (demo mode).
Configures WebView visibility and z-order to ensure it’s on top.
Adds the
tubiVPAIDinstance as a JavaScript interface named"TubiNativeJSInterface".Loads the URL defined by
VPAID_URL.
Usage Example:
initVpaidWebview(webView, myHandler);
public void onBackPressed()
Description:
Overrides the Android back button behavior. If the WebView has navigation history, it navigates back within the WebView instead of exiting the activity. Otherwise, it performs the default back action.Usage:
Called automatically when the user presses the back button.Implementation Details:
Uses
webView.canGoBack()to check if the WebView can navigate back.Calls
webView.goBack()if possible; otherwise, invokessuper.onBackPressed().
Important Implementation Details
WebView Configuration:
The WebView is set up to load a predefined URL hosting the VPAID ad content. The activity ensures the WebView is visible and interactive by bringing it to the front and invalidating to refresh rendering.JavaScript Interface:
TheTubiVPAIDclient is added as a JavaScript interface (TubiNativeJSInterface) to enable JavaScript code in the WebView to invoke native Android methods, facilitating communication such as ad events and completion notifications.Handler Usage:
AHandlerassociated with the UI thread is passed toTubiVPAID, allowing it to post runnable tasks safely on the UI thread from JavaScript callbacks.API Level Requirement:
The activity requires Android KitKat (API 19) or higher due to WebView and JavaScript interface features used.
Interaction with Other Components
TubiVPAID Client:
WebviewActivitycreates and manages an instance ofTubiVPAID, which encapsulates the logic for communicating with the VPAID JavaScript player running inside the WebView.VPAID Ad System:
This activity is the UI layer that hosts the VPAID ad playback. It is typically integrated into a larger media playback system where theFsmPlayerfinite state machine controls the ad playback lifecycle.VPAID JavaScript Player:
The WebView loads the VPAID ad content URL, which hosts the JavaScript player. The JavaScript code calls back into native Android via the JavaScript interface to notify about ad events.Back Navigation:
The activity handles user back navigation in a way that respects WebView history, improving user experience when interacting with ads.
Usage Example
A typical usage scenario is an Android app that plays video content and occasionally needs to display interactive VPAID ads. When a VPAID ad is triggered, the app launches `WebviewActivity`, which sets up the WebView and loads the ad. The activity manages the ad playback lifecycle and user interactions until the ad completes or errors out.
Visual Diagram
classDiagram
class WebviewActivity {
-static final String VPAID_URL
-TubiVPAID tubiVPAID
-Handler myHandler
-WebView webView
+void onCreate(Bundle savedInstanceState)
-void initVpaidWebview(WebView view, Handler handler)
+void onBackPressed()
}
WebviewActivity --> WebView : uses
WebviewActivity --> Handler : uses
WebviewActivity --> TubiVPAID : creates & manages
Summary
`WebviewActivity.java` is a focused Android activity designed to host and manage VPAID ad playback via a WebView component. By initializing the `TubiVPAID` client and binding it as a JavaScript interface, it enables seamless communication between native Android code and the interactive VPAID ads running in JavaScript. The activity handles lifecycle events such as creation and back navigation to provide a smooth user experience during ad playback. This file forms the UI layer of the VPAID ad integration subsystem, complementing the underlying ad logic and state management handled by other components like `TubiVPAID` and the media player's FSM.