[Android] Use regular thread on startup

Geoffrey Métais git at videolan.org
Mon Jan 21 16:23:19 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Jan 21 15:53:23 2019 +0100| [6673d4a552b7fe46cf0d4559a741e2e59dd250ac] | committer: Geoffrey Métais

Use regular thread on startup

kotlinx dispatchers can take some time to initialize.

> https://code.videolan.org/videolan/vlc-android/commit/6673d4a552b7fe46cf0d4559a741e2e59dd250ac
---

 .../src/org/videolan/vlc/StartActivity.java        |  5 +--
 .../src/org/videolan/vlc/VLCApplication.java       | 47 +++++++++++++---------
 vlc-android/src/org/videolan/vlc/util/Workers.kt   |  5 ++-
 3 files changed, 33 insertions(+), 24 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/StartActivity.java b/vlc-android/src/org/videolan/vlc/StartActivity.java
index 7c8543fb5..f1aae00da 100644
--- a/vlc-android/src/org/videolan/vlc/StartActivity.java
+++ b/vlc-android/src/org/videolan/vlc/StartActivity.java
@@ -42,7 +42,6 @@ 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;
@@ -118,12 +117,12 @@ public class StartActivity extends FragmentActivity {
     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() {
+        new Thread(new Runnable() {
             @Override
             public void run() {
                 MediaParsingServiceKt.startMedialibrary(StartActivity.this, firstRun, upgrade, true);
             }
-        });
+        }).start();
         final Intent intent = new Intent(StartActivity.this, tv ? MainTvActivity.class : MainActivity.class)
                 .putExtra(Constants.EXTRA_FIRST_RUN, firstRun)
                 .putExtra(Constants.EXTRA_UPGRADE, upgrade);
diff --git a/vlc-android/src/org/videolan/vlc/VLCApplication.java b/vlc-android/src/org/videolan/vlc/VLCApplication.java
index e403e4d67..8a13353d5 100644
--- a/vlc-android/src/org/videolan/vlc/VLCApplication.java
+++ b/vlc-android/src/org/videolan/vlc/VLCApplication.java
@@ -20,11 +20,13 @@
 package org.videolan.vlc;
 
 import android.annotation.SuppressLint;
+import android.annotation.TargetApi;
 import android.app.Application;
 import android.content.Context;
 import android.content.Intent;
 import android.content.res.Configuration;
 import android.content.res.Resources;
+import android.os.Build;
 import android.util.Log;
 
 import org.videolan.libvlc.Dialog;
@@ -77,28 +79,35 @@ public class VLCApplication extends Application {
     @Override
     public void onCreate() {
         super.onCreate();
-        WorkersKt.runIO(new Runnable() {
+        //Initiate Kotlinx Dispatchers in a thread to prevent ANR
+        new Thread(new Runnable() {
             @Override
             public void run() {
-                locale = Settings.INSTANCE.getInstance(instance).getString("set_locale", "");
-
-                // Set the locale for API < 24 and set application resources and direction for API >=24
-                setLocale(getAppContext());
-            }
-        });
-
-        WorkersKt.runIO(new Runnable() {
-            @Override
-            public void run() {
-
-                if (AndroidUtil.isOOrLater) NotificationHelper.createNotificationChannels(VLCApplication.this);
-                // Prepare cache folder constants
-                AudioUtil.prepareCacheFolder(getAppContext());
-
-                if (!VLCInstance.testCompatibleCPU(getAppContext())) return;
-                Dialog.setCallbacks(VLCInstance.get(), mDialogCallbacks);
+                WorkersKt.runIO(new Runnable() {
+                    @Override
+                    public void run() {
+                        locale = Settings.INSTANCE.getInstance(instance).getString("set_locale", "");
+
+                        // Set the locale for API < 24 and set application resources and direction for API >=24
+                        setLocale(getAppContext());
+                    }
+                });
+
+                WorkersKt.runIO(new Runnable() {
+                    @TargetApi(Build.VERSION_CODES.O)
+                    @Override
+                    public void run() {
+
+                        if (AndroidUtil.isOOrLater) NotificationHelper.createNotificationChannels(VLCApplication.this);
+                        // Prepare cache folder constants
+                        AudioUtil.prepareCacheFolder(getAppContext());
+
+                        if (!VLCInstance.testCompatibleCPU(getAppContext())) return;
+                        Dialog.setCallbacks(VLCInstance.get(), mDialogCallbacks);
+                    }
+                });
             }
-        });
+        }).start();
     }
 
     @Override
diff --git a/vlc-android/src/org/videolan/vlc/util/Workers.kt b/vlc-android/src/org/videolan/vlc/util/Workers.kt
index 2e614eff6..b3212b20a 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 GlobalScope.launch(Dispatchers.Default) { runnable.run() }
+    else AppScope.launch(Dispatchers.Default) { runnable.run() }
 }
 
 fun runOnMainThread(runnable: Runnable) {
@@ -13,9 +13,10 @@ fun runOnMainThread(runnable: Runnable) {
 }
 
 fun runIO(runnable: Runnable) {
-    GlobalScope.launch(Dispatchers.IO) { runnable.run() }
+    AppScope.launch(Dispatchers.IO) { runnable.run() }
 }
 
 object AppScope : CoroutineScope {
+    @ExperimentalCoroutinesApi
     override val coroutineContext = Dispatchers.Main.immediate + SupervisorJob()
 }
\ No newline at end of file



More information about the Android mailing list