[Android] getFromMl util extension function
Geoffrey Métais
git at videolan.org
Tue Jul 24 17:00:53 CEST 2018
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Jul 23 17:10:24 2018 +0200| [8d6e0286daa9753629998ef9b22a330be19a3ca0] | committer: Geoffrey Métais
getFromMl util extension function
> https://code.videolan.org/videolan/vlc-android/commit/8d6e0286daa9753629998ef9b22a330be19a3ca0
---
.../src/org/videolan/vlc/util/Kextensions.kt | 25 +++++++++++++++++++---
1 file changed, 22 insertions(+), 3 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/util/Kextensions.kt b/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
index 1def2990c..bb943cef1 100644
--- a/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
+++ b/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
@@ -7,11 +7,15 @@ import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
import android.support.v7.preference.PreferenceManager
import kotlinx.coroutines.experimental.delay
+import kotlinx.coroutines.experimental.withContext
import org.videolan.libvlc.Media
+import org.videolan.medialibrary.Medialibrary
+import org.videolan.vlc.startMedialibrary
import java.io.File
import java.net.URI
import java.net.URISyntaxException
import java.util.*
+import kotlin.coroutines.experimental.suspendCoroutine
fun String.validateLocation(): Boolean {
var location = this
@@ -50,8 +54,23 @@ suspend fun retry (
fun Media?.canExpand() = this != null && (type == Media.Type.Directory || type == Media.Type.Playlist)
-fun Context.getAppSystemService(name: String) = applicationContext.getSystemService(name)
+fun Context.getAppSystemService(name: String) = applicationContext.getSystemService(name)!!
-fun Context.getPreferences() = PreferenceManager.getDefaultSharedPreferences(this)
+fun Context.getPreferences() = PreferenceManager.getDefaultSharedPreferences(this)!!
-fun Long.random() = (Random().nextFloat() * this).toLong()
\ No newline at end of file
+fun Long.random() = (Random().nextFloat() * this).toLong()
+
+suspend inline fun <reified T> Context.getFromMl(crossinline block: Medialibrary.() -> T) = withContext(VLCIO) {
+ val ml = Medialibrary.getInstance()
+ if (ml.isInitiated) block.invoke(ml)
+ else suspendCoroutine { continuation ->
+ ml.addOnMedialibraryReadyListener(object : Medialibrary.OnMedialibraryReadyListener {
+ override fun onMedialibraryReady() {
+ ml.removeOnMedialibraryReadyListener(this)
+ continuation.resume(block.invoke(ml))
+ }
+ override fun onMedialibraryIdle() {}
+ })
+ startMedialibrary(false, false, false)
+ }
+}
More information about the Android
mailing list