[Android] Start medialibrary from GlobalScope

Geoffrey Métais git at videolan.org
Tue Jan 15 15:32:09 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Jan 15 14:59:33 2019 +0100| [85087c0052bf7ad5ea4dadbccb7d582cbba7753e] | committer: Geoffrey Métais

Start medialibrary from GlobalScope

Workaround to avoid ANR caused by:
https://github.com/Kotlin/kotlinx.coroutines/issues/878

> https://code.videolan.org/videolan/vlc-android/commit/85087c0052bf7ad5ea4dadbccb7d582cbba7753e
---

 vlc-android/src/org/videolan/vlc/StartActivity.java | 14 +++++++++++---
 vlc-android/src/org/videolan/vlc/util/Workers.kt    |  4 ++--
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/StartActivity.java b/vlc-android/src/org/videolan/vlc/StartActivity.java
index 795864e77..dad99750e 100644
--- a/vlc-android/src/org/videolan/vlc/StartActivity.java
+++ b/vlc-android/src/org/videolan/vlc/StartActivity.java
@@ -42,6 +42,7 @@ import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.Constants;
 import org.videolan.vlc.util.FileUtils;
 import org.videolan.vlc.util.Settings;
+import org.videolan.vlc.util.WorkersKt;
 
 import androidx.core.content.ContextCompat;
 import androidx.fragment.app.FragmentActivity;
@@ -114,9 +115,16 @@ public class StartActivity extends FragmentActivity {
         finish();
     }
 
-    private void startApplication(boolean tv, boolean firstRun, boolean upgrade, int target) {
-        MediaParsingServiceKt.startMedialibrary(this, firstRun, upgrade, true);
-        final Intent intent = new Intent(this, tv ? MainTvActivity.class : MainActivity.class)
+    private void startApplication(final boolean tv, final boolean firstRun, final boolean upgrade, final int target) {
+        // Start Medialibrary from background to workaround Dispatchers.Main causing ANR
+        // cf https://github.com/Kotlin/kotlinx.coroutines/issues/878
+        WorkersKt.runBackground(new Runnable() {
+            @Override
+            public void run() {
+                MediaParsingServiceKt.startMedialibrary(StartActivity.this, firstRun, upgrade, true);
+            }
+        });
+        final Intent intent = new Intent(StartActivity.this, tv ? MainTvActivity.class : MainActivity.class)
                 .putExtra(Constants.EXTRA_FIRST_RUN, firstRun);
         if (tv && getIntent().hasExtra(Constants.EXTRA_PATH)) intent.putExtra(Constants.EXTRA_PATH, getIntent().getStringExtra(Constants.EXTRA_PATH));
         if (target != 0) intent.putExtra(Constants.EXTRA_TARGET, target);
diff --git a/vlc-android/src/org/videolan/vlc/util/Workers.kt b/vlc-android/src/org/videolan/vlc/util/Workers.kt
index 1d3224d9e..2e614eff6 100644
--- a/vlc-android/src/org/videolan/vlc/util/Workers.kt
+++ b/vlc-android/src/org/videolan/vlc/util/Workers.kt
@@ -5,7 +5,7 @@ import kotlinx.coroutines.*
 
 fun runBackground(runnable: Runnable) {
     if (Looper.myLooper() != Looper.getMainLooper()) runnable.run()
-    else AppScope.launch(Dispatchers.Default) { runnable.run() }
+    else GlobalScope.launch(Dispatchers.Default) { runnable.run() }
 }
 
 fun runOnMainThread(runnable: Runnable) {
@@ -13,7 +13,7 @@ fun runOnMainThread(runnable: Runnable) {
 }
 
 fun runIO(runnable: Runnable) {
-    AppScope.launch(Dispatchers.IO) { runnable.run() }
+    GlobalScope.launch(Dispatchers.IO) { runnable.run() }
 }
 
 object AppScope : CoroutineScope {



More information about the Android mailing list