[Android] Create custom application coroutine scope

Geoffrey Métais git at videolan.org
Wed Oct 10 11:48:09 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Oct 10 11:03:50 2018 +0200| [766b02bec65f245e62413e92e09441041b1ded17] | committer: Geoffrey Métais

Create custom application coroutine scope

> https://code.videolan.org/videolan/vlc-android/commit/766b02bec65f245e62413e92e09441041b1ded17
---

 vlc-android/src/org/videolan/vlc/util/Workers.kt | 28 +++++++++++++++++-------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/util/Workers.kt b/vlc-android/src/org/videolan/vlc/util/Workers.kt
index d347cbab9..5a91accca 100644
--- a/vlc-android/src/org/videolan/vlc/util/Workers.kt
+++ b/vlc-android/src/org/videolan/vlc/util/Workers.kt
@@ -1,21 +1,33 @@
 package org.videolan.vlc.util
 
 import android.os.Looper
-import android.os.Process
-import kotlinx.coroutines.experimental.*
-import kotlinx.coroutines.experimental.android.Main
-import kotlinx.coroutines.experimental.android.UI
-import java.util.concurrent.ThreadFactory
+import kotlinx.coroutines.experimental.CoroutineScope
+import kotlinx.coroutines.experimental.Dispatchers
+import kotlinx.coroutines.experimental.Runnable
+import kotlinx.coroutines.experimental.launch
+import kotlin.coroutines.experimental.CoroutineContext
 
 fun runBackground(runnable: Runnable) {
     if (Looper.myLooper() != Looper.getMainLooper()) runnable.run()
-    else GlobalScope.launch(Dispatchers.Default) { runnable.run() }
+    else AppScope.launch(Dispatchers.Default) { runnable.run() }
 }
 
 fun runOnMainThread(runnable: Runnable) {
-    GlobalScope.launch(Dispatchers.Main.immediate) { runnable.run() }
+    AppScope.launch { runnable.run() }
 }
 
 fun runIO(runnable: Runnable) {
-    GlobalScope.launch(Dispatchers.IO) { runnable.run() }
+    AppScope.launch(Dispatchers.IO) { runnable.run() }
 }
+
+object AppScope : CoroutineScope {
+    /**
+     * @suppress **Deprecated**: Deprecated in favor of top-level extension property
+     */
+    @Deprecated(level = DeprecationLevel.HIDDEN, message = "Deprecated in favor of top-level extension property")
+    override val isActive: Boolean
+        get() = true
+
+    override val coroutineContext: CoroutineContext
+        get() = Dispatchers.Main.immediate
+}
\ No newline at end of file



More information about the Android mailing list