ReceiveAdState.java
Overview
[ReceiveAdState.java](/projects/288/68333) defines the **ReceiveAdState** class, a concrete state in the media player's finite state machine (FSM) responsible for managing the transition phase when the player has received an advertisement and is preparing to play it.
In the FSM playback system, this state functions as an intermediary step between the ad retrieval phase and the actual ad playback phase. It listens for specific input events to trigger state transitions and monitors the underlying media player to detect any playback errors or lifecycle anomalies before the ad starts playing.
Class: ReceiveAdState
public class ReceiveAdState extends BaseState
Description
`ReceiveAdState` extends `BaseState` and represents the FSM state where the player has received an ad and is ready to transition into ad playback. It primarily listens for the input event signaling to show ads and performs minimal UI update work, focusing mainly on player state validation.
Key Responsibilities
Handle input events relevant to ad playback transition.
Monitor the ExoPlayer instance associated with content playback to detect if the player has unexpectedly gone idle (e.g., the user left the activity).
Trigger error transitions if an abnormal player state is detected.
Maintain minimal UI updates, as the preparation phase does not require UI changes.
Inherited Interfaces and Superclass
BaseState: An abstract class implementing common FSM state functionality, providing default implementations for transition logic and work execution.
Methods
1. transformToState(Input input, StateFactory factory)
@Override
public State transformToState(Input input, StateFactory factory)
Purpose
Handles state transition logic by determining the next FSM state based on the provided input event.
Parameters
Input input– An enum representing the event or trigger received by the FSM.StateFactory factory– A factory object used to instantiate or retrieve singleton state instances.
Returns
State– The next state to transition into. Returnsnullif no valid transition exists for the given input.
Behavior
When the
inputisSHOW_ADS, it transitions to theAdPlayingState, indicating that the ad playback should start.For any other input, returns
null(no state change).
Example Usage
State nextState = receiveAdState.transformToState(Input.SHOW_ADS, stateFactory);
if (nextState != null) {
fsmPlayer.setCurrentState(nextState);
}
2. performWorkAndUpdatePlayerUI(@NonNull FsmPlayer fsmPlayer)
@Override
public void performWorkAndUpdatePlayerUI(@NonNull FsmPlayer fsmPlayer)
Purpose
Executes the work associated with this state and updates the player UI as necessary.
Parameters
FsmPlayer fsmPlayer– The finite state machine player instance that controls playback and state transitions.
Returns
void
Behavior
Calls the superclass's implementation first.
Checks if the
fsmPlayerreference is null to avoid null pointer exceptions.Retrieves the content player (
SimpleExoPlayer) from the controller.Checks if the player is in
ExoPlayer.STATE_IDLEstate, which indicates that playback has stopped or the user has left the activity lifecycle unexpectedly.If this idle state is detected, it triggers a transition with the
ERRORinput, signaling an error state in the FSM.
Important Details
No UI manipulation is performed here because the state is primarily a transitional step.
The check for
STATE_IDLEis a safety mechanism to detect invalid playback states due to activity lifecycle changes (e.g., if the user navigated away from the video player).
Example Usage
receiveAdState.performWorkAndUpdatePlayerUI(fsmPlayer);
Implementation Details and Algorithms
State Transition Logic: Implements a simple switch-case on the input event, enabling transition only on
SHOW_ADS.Player State Monitoring: Uses ExoPlayer's playback state constants to detect if the player is idle, which is interpreted as an abnormal condition requiring an error transition.
Lifecycle Awareness: By checking the ExoPlayer state, the FSM avoids proceeding with ad playback if the app's activity lifecycle is no longer active or if the player has been released.
Interaction with Other Components
FsmPlayer: The central FSM controller that holds the current state and calls
performWorkAndUpdatePlayerUIon it.ReceiveAdStaterelies onFsmPlayerto access the media player instances and perform transitions.StateFactory: Used to instantiate or retrieve the
AdPlayingStatewhen the FSM receives theSHOW_ADSinput.ExoPlayer / SimpleExoPlayer: The underlying media playback engine.
ReceiveAdStatemonitors the content player's playback state to guard against invalid conditions.Input Enum: The input event
SHOW_ADStriggers the transition to the ad playback state.
Usage Example in FSM Flow
// FSM receives an input indicating ads should be shown
State currentState = new ReceiveAdState();
Input input = Input.SHOW_ADS;
StateFactory factory = new StateFactory();
State nextState = currentState.transformToState(input, factory);
if (nextState != null) {
fsmPlayer.setCurrentState(nextState); // Transition to AdPlayingState
nextState.performWorkAndUpdatePlayerUI(fsmPlayer); // Start ad playback
}
Class Diagram
classDiagram
class ReceiveAdState {
+transformToState(input: Input, factory: StateFactory): State
+performWorkAndUpdatePlayerUI(fsmPlayer: FsmPlayer): void
}
ReceiveAdState --|> BaseState
class BaseState {
+transformToState(input: Input, factory: StateFactory): State
+performWorkAndUpdatePlayerUI(fsmPlayer: FsmPlayer): void
}
class StateFactory
class Input
class FsmPlayer
class SimpleExoPlayer
ReceiveAdState --> StateFactory : uses
ReceiveAdState --> Input : uses
ReceiveAdState --> FsmPlayer : uses
FsmPlayer --> SimpleExoPlayer : holds
Summary
ReceiveAdState is a transitional FSM state in the ad playback lifecycle.
It listens specifically for the
SHOW_ADSinput to move toAdPlayingState.It performs minimal UI work but monitors the ExoPlayer playback state for abnormal conditions.
If the player is idle (e.g., due to lifecycle changes), it triggers an
ERRORtransition.This state ensures the FSM only proceeds to ad playback when the player is ready and prevents illegal playback attempts.
This documentation provides a detailed understanding of [ReceiveAdState.java](/projects/288/68333) within the FSM playback architecture, highlighting its role, behavior, and relationships in managing advertisement playback transitions.