FsmComonent.java
Overview
`FsmComonent.java` defines a Dagger 2 **component interface** responsible for dependency injection in the media player application, specifically targeting the `DoubleViewTubiPlayerActivity`. It provides scoped lifecycle management for player-related dependencies such as the finite state machine (FSM) player factory (`StateFactory`) and enables injection of these dependencies into the activity.
The component uses the `PlayerModuleDefault` module, which supplies default implementations of the FSM player, controllers, ad interfaces, and related dependencies optimized for demo or testing scenarios.
Detailed Explanation
Package and Imports
Package:
com.tubitv.media.di.component
Organizes the Dagger component interfaces relating to dependency injection within the media playback module.Imports:
DoubleViewTubiPlayerActivity: The injection target for this component.PlayerModuleDefault: The Dagger module that provides default player-related dependencies.ActicityScope: Custom scope annotation specifying lifecycle tied to the activity.StateFactory: Factory class for creating FSM states.dagger.Component: Dagger annotation to define the component interface.
Class: FsmComonent
@ActicityScope
@Component(modules = PlayerModuleDefault.class)
public interface FsmComonent {
StateFactory getStateFactory();
void inject(DoubleViewTubiPlayerActivity activity);
}
Purpose
Acts as a Dagger component for injecting dependencies scoped to an activity lifecycle.
Facilitates injecting FSM player dependencies into
DoubleViewTubiPlayerActivity.Exposes the
StateFactoryinstance publicly for testing or internal FSM state creation.
Annotations
@ActicityScope
A custom scope annotation ensuring all dependencies provided and injected by this component share the same lifecycle bound to the activity. Prevents multiple instantiations and memory leaks.@Component(modules = PlayerModuleDefault.class)
Specifies that this component usesPlayerModuleDefaultas its dependency provider module.
Methods
Method | Description | Parameters | Returns | Usage Example |
|---|---|---|---|---|
`StateFactory getStateFactory()` | Returns the `StateFactory` instance to allow creation or inspection of FSM states, mainly for testing. | None | `StateFactory` | `StateFactory factory = fsmComponent.getStateFactory();` |
`void inject(DoubleViewTubiPlayerActivity activity)` | Injects all required dependencies into the `DoubleViewTubiPlayerActivity` instance. | `DoubleViewTubiPlayerActivity` instance | `void` | `fsmComponent.inject(doubleViewTubiPlayerActivityInstance);` |
Usage Example
In the activity setup, you typically create or obtain an instance of `FsmComonent` and perform injection like this:
FsmComonent fsmComponent = DaggerFsmComonent.create();
fsmComponent.inject(this); // 'this' refers to DoubleViewTubiPlayerActivity instance
This call injects all necessary dependencies into the activity, including the FSM player and related controllers configured in `PlayerModuleDefault`.
Important Implementation Details
Component-Module Binding:
The component binds toPlayerModuleDefault, which defines how the FSM player and related objects are constructed. This module provides a default or stub implementation ideal for testing or demo purposes.Scope Management:
Using@ActicityScopeensures that all injected dependencies are effectively singletons within the lifecycle of the activity, avoiding unnecessary recreation and supporting consistent state during playback.Exposed Factory:
The exposedStateFactorymethod is unusual for a component interface but is provided to allow external consumers (such as unit tests) to obtain the state factory directly, facilitating FSM state creation or verification.Injection Target:
The only injection target isDoubleViewTubiPlayerActivity, an activity designed to handle media playback with dual view players, presumably requiring FSM player logic and controllers injected for proper function.
Interaction with Other System Parts
Modules:
Relies onPlayerModuleDefaultto supply all player-related objects including FSM player, UI controllers, and ad logic controllers.Activity:
Injects dependencies intoDoubleViewTubiPlayerActivity, enabling it to operate the media playback FSM with appropriate state management and UI control without manual dependency construction.FSM and State Management:
TheStateFactoryexposed by this component creates FSM states, which are central to controlling playback behavior (e.g., loading ads, cue points, playing media).Testing and Extensibility:
By exposingStateFactoryand isolating dependencies via Dagger, this component facilitates easy testing and swapping of implementations without changing activity code.
Visual Diagram: Class Structure
classDiagram
class FsmComonent {
<<interface>>
+getStateFactory(): StateFactory
+inject(activity: DoubleViewTubiPlayerActivity): void
}
FsmComonent ..> PlayerModuleDefault : uses
FsmComonent ..> ActicityScope : annotated with
FsmComonent ..> DoubleViewTubiPlayerActivity : injects
Summary
FsmComonentis a scoped Dagger component interface for injecting player FSM-related dependencies intoDoubleViewTubiPlayerActivity.It uses the
PlayerModuleDefaultmodule to get default implementations suitable for demo/testing.Provides lifecycle management with
@ActicityScope.Exposes the
StateFactoryfor FSM state creation and testing convenience.Fits within a modular DI architecture supporting clean separation of concerns, testability, and maintainability.
This file is a key integration point in the dependency injection setup for the media player FSM, connecting the default module implementations with the UI activity that drives playback.