RetrieveAdCallback.java
Overview
`RetrieveAdCallback.java` defines a Java interface used within the ad retrieval workflow of the media playback system. It serves as a contract for callback methods that handle different outcomes of an asynchronous ad fetching operation. The interface allows implementing classes to react appropriately when ads are successfully retrieved, when no ads are found, or when an error occurs during the retrieval process.
This callback mechanism is essential for the system's finite state machine (FSM) or any asynchronous component requesting ad media data, enabling decoupling between the ad retrieval logic and the subsequent handling or UI update logic.
Detailed Explanation
Interface: RetrieveAdCallback
public interface RetrieveAdCallback {
void onReceiveAd(AdMediaModel mediaModels);
void onError();
void onEmptyAdReceived();
}
Purpose
Defines three callback methods for handling different outcomes when attempting to retrieve ad media:
onReceiveAd: Called when ad media is successfully retrieved.
onError: Called when there is an error during ad retrieval.
onEmptyAdReceived: Called when the retrieval process finishes successfully but no ads are returned.
Methods
1. void onReceiveAd(AdMediaModel mediaModels)
Description: Invoked when one or more ads have been successfully retrieved from the ad source.
Parameters:
mediaModels- An instance ofAdMediaModelrepresenting the ad content/media data fetched.
Return: None
Usage Example:
@Override
public void onReceiveAd(AdMediaModel mediaModels) {
// Process the received ads, e.g., prepare for playback or display
playAd(mediaModels);
}
2. void onError()
Description: Invoked when an error occurs during the ad retrieval process, such as network failure, data parsing error, or unexpected conditions.
Parameters: None
Return: None
Usage Example:
@Override
public void onError() {
// Handle error scenario, maybe retry or fallback to default content
showErrorMessage("Failed to load ads.");
}
3. void onEmptyAdReceived()
Description: Invoked when the retrieval completes successfully but no ads are available to serve.
Parameters: None
Return: None
Usage Example:
@Override
public void onEmptyAdReceived() {
// No ads available, proceed without ads or show alternative content
continueWithoutAds();
}
Important Implementation Details
Interface Design: Being an interface, it only declares method signatures, leaving the actual implementation to classes that consume ad media retrieval results.
AdMediaModel Dependency: Relies on the
AdMediaModelclass to encapsulate ad data. The callback passes this model to the consumer.Asynchronous Handling: This interface is typically used in asynchronous workflows where ad retrieval happens in the background, and results are delivered via these callbacks.
Error and Empty States: Explicitly handles different terminal states (error, empty response), enabling robust workflow branching.
Interaction with Other System Components
Ad Retrieval FSM (
com.tubitv.media.fsm): This callback interface is part of the finite state machine subsystem responsible for managing ad states during media playback.Ad Retrieval Service/Manager: The component that fetches ads from a network or cache will invoke these callbacks to notify the caller of the result.
Media Playback Controller: Implements this interface to receive ads and decide how to integrate them into the playback experience.
AdMediaModel Class: Acts as a data carrier for the ads retrieved and is passed through this interface.
Visual Diagram
classDiagram
class RetrieveAdCallback {
<<interface>>
+onReceiveAd(mediaModels: AdMediaModel)
+onError()
+onEmptyAdReceived()
}
class AdMediaModel {
<<model>>
+adId: String
+adContentUrl: String
+duration: int
+getMetadata(): Map<String, String>
}
RetrieveAdCallback ..> AdMediaModel : uses
Summary
`RetrieveAdCallback` defines a minimal but crucial contract for handling ad retrieval results asynchronously within the media playback framework. It cleanly separates concerns by delegating the response handling to implementing classes, supporting successful retrieval, error handling, and empty result scenarios. This design promotes extensibility and robustness in ad delivery workflows.
If you need further documentation on `AdMediaModel` or the state machine handling ad retrieval, please let me know!