@@ -17,6 +17,7 @@ import kotlinx.coroutines.CompletableDeferred
17
17
import kotlinx.coroutines.CoroutineScope
18
18
import kotlinx.coroutines.Dispatchers
19
19
import kotlinx.coroutines.Job
20
+ import kotlinx.coroutines.launch
20
21
import kotlinx.coroutines.runBlocking
21
22
22
23
/* *
@@ -37,6 +38,7 @@ import kotlinx.coroutines.runBlocking
37
38
class P2PMediaLoader private constructor(
38
39
private val coreConfigJson : String ,
39
40
private val serverPort : Int ,
41
+ private val customJavaScriptInterfaces : List <Pair <String , Any >>,
40
42
private val customEngineImplementationPath : String? ,
41
43
) {
42
44
private val engineStateManager = P2PStateManager ()
@@ -78,6 +80,7 @@ class P2PMediaLoader private constructor(
78
80
scope!! ,
79
81
engineStateManager,
80
82
playbackCalculator,
83
+ customJavaScriptInterfaces,
81
84
onPageLoadFinished = { onWebViewLoaded() },
82
85
)
83
86
@@ -175,9 +178,11 @@ class P2PMediaLoader private constructor(
175
178
manifestParser.reset()
176
179
}
177
180
178
- private suspend fun onWebViewLoaded () {
179
- webViewManager?.initCoreEngine(coreConfigJson)
180
- webViewLoadCompletion?.complete(Unit )
181
+ private fun onWebViewLoaded () {
182
+ scope?.launch {
183
+ webViewManager?.initCoreEngine(coreConfigJson)
184
+ webViewLoadCompletion?.complete(Unit )
185
+ }
181
186
}
182
187
183
188
private fun onServerStarted () {
@@ -198,6 +203,7 @@ class P2PMediaLoader private constructor(
198
203
private var coreConfig: String = " "
199
204
private var serverPort: Int = Constants .DEFAULT_SERVER_PORT
200
205
private var customEngineImplementationPath: String? = null
206
+ private var customJavaScriptInterfaces: MutableList <Pair <String , Any >> = mutableListOf ()
201
207
202
208
/* *
203
209
* Sets core P2P configurations. See [P2PML Core Config](https://novage.github.io/p2p-media-loader/docs/v2.1.0/types/p2p_media_loader_core.CoreConfig.html)
@@ -221,13 +227,29 @@ class P2PMediaLoader private constructor(
221
227
this .customEngineImplementationPath = path
222
228
}
223
229
230
+ /* *
231
+ * Adds a custom JavaScript interface to the WebView.
232
+ * The feature has to be used with custom engine implementation.
233
+ * methods has to be annotated with @JavascriptInterface.
234
+ *
235
+ * @param name Interface name
236
+ * @param obj Object with methods annotated with @JavascriptInterface
237
+ */
238
+ fun addCustomJavaScriptInterface (
239
+ name : String ,
240
+ obj : Any ,
241
+ ) = apply {
242
+ customJavaScriptInterfaces.add(Pair (name, obj))
243
+ }
244
+
224
245
/* *
225
246
* @return A new [P2PMediaLoader] instance.
226
247
*/
227
248
fun build (): P2PMediaLoader =
228
249
P2PMediaLoader (
229
250
coreConfig,
230
251
serverPort,
252
+ customJavaScriptInterfaces,
231
253
customEngineImplementationPath,
232
254
)
233
255
}
0 commit comments