-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathReactTHEOplayerView.kt
More file actions
148 lines (131 loc) · 4.32 KB
/
Copy pathReactTHEOplayerView.kt
File metadata and controls
148 lines (131 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.theoplayer
import android.annotation.SuppressLint
import android.util.Log
import android.view.ViewGroup
import android.widget.FrameLayout
import com.facebook.react.bridge.*
import com.facebook.react.uimanager.ThemedReactContext
import com.theoplayer.android.api.ads.wrapper.AdsApiWrapper
import com.theoplayer.android.api.cast.Cast
import com.theoplayer.android.api.error.THEOplayerException
import com.theoplayer.android.api.player.Player
import com.theoplayer.broadcast.EventBroadcastAdapter
import com.theoplayer.presentation.PresentationManager
import com.theoplayer.source.SourceAdapter
private const val TAG = "ReactTHEOplayerView"
@SuppressLint("ViewConstructor")
class ReactTHEOplayerView(private val reactContext: ThemedReactContext) :
FrameLayout(reactContext), LifecycleEventListener {
val eventEmitter: PlayerEventEmitter =
PlayerEventEmitter(reactContext.reactApplicationContext, this)
val broadcast = EventBroadcastAdapter(this)
var presentationManager: PresentationManager? = null
var playerContext: ReactTHEOplayerContext? = null
var isInitialized: Boolean = false
private var config: PlayerConfigAdapter? = null
val adsApi: AdsApiWrapper
val castApi: Cast?
get() = playerContext?.playerView?.cast
val player: Player?
get() = playerContext?.player
init {
reactContext.addLifecycleEventListener(this)
adsApi = AdsApiWrapper()
}
fun initialize(config: PlayerConfigAdapter) {
if (BuildConfig.LOG_VIEW_EVENTS) {
Log.d(TAG, "Initialize view")
}
if (isInitialized) {
Log.w(TAG, "Already initialized view")
return
}
this.config = config
if (!isAttachedToWindow && BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
// The view is not attached to the window yet, postpone the initialization.
return
}
isInitialized = true
playerContext = ReactTHEOplayerContext.create(
reactContext,
config
)
playerContext?.apply {
adsApi.initialize(player, imaIntegration, daiIntegration)
val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
playerView.layoutParams = layoutParams
(playerView.parent as? ViewGroup)?.removeView(playerView)
addView(playerView, 0, layoutParams)
presentationManager = PresentationManager(
this,
reactContext,
eventEmitter
)
eventEmitter.preparePlayer(player)
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (!isInitialized && BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
config?.let { initialize(it) }
}
}
override fun setId(id: Int) {
super.setId(id)
eventEmitter.setViewId(id)
}
/**
* Called when host activity receives pause event (e.g. {@link Activity#onPause}. Always called
* for the most current activity.
*/
override fun onHostPause() {
if (BuildConfig.LOG_VIEW_EVENTS) {
Log.d(TAG, "onHostPause")
}
playerContext?.onHostPause()
}
/**
* Called either when the host activity receives a resume event (e.g. {@link Activity#onResume} or
* if the native module that implements this is initialized while the host activity is already
* resumed. Always called for the most current activity.
*/
override fun onHostResume() {
if (BuildConfig.LOG_VIEW_EVENTS) {
Log.d(TAG, "onHostResume")
}
playerContext?.onHostResume()
}
/**
* Called when host activity receives destroy event (e.g. {@link Activity#onDestroy}. Only called
* for the last React activity to be destroyed.
*/
override fun onHostDestroy() {
if (BuildConfig.LOG_VIEW_EVENTS) {
Log.d(TAG, "onHostDestroy")
}
releasePlayer()
}
fun releasePlayer() {
if (BuildConfig.LOG_VIEW_EVENTS) {
Log.d(TAG, "releasePlayer")
}
reactContext.removeLifecycleEventListener(this)
adsApi.destroy()
if (isInitialized) {
eventEmitter.removeListeners(player)
presentationManager?.destroy()
playerContext?.destroy()
isInitialized = false
}
}
fun setSource(source: ReadableMap?) {
try {
val sourceDescription = SourceAdapter().parseSourceFromJS(source)
adsApi.setSource(sourceDescription)
player?.source = sourceDescription
} catch (exception: THEOplayerException) {
Log.e(TAG, exception.message ?: "")
eventEmitter.emitError(exception)
}
}
}