[Android] Remove thread from app initialization

Geoffrey Métais git at videolan.org
Mon Oct 26 11:47:23 CET 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Oct 21 09:21:00 2020 +0200| [583944ffa434edeb110c54cb1c53adf3db469640] | committer: Nicolas Pomepuy

Remove thread from app initialization

This was a workaround for a bug in kotlinx.coroutines causing I/O ops in
Dispatchers setup.
We can now call `AppScope.launch` directly.

`AppContextProvider.setLocale` takes less than 1ms (on emulator) so it
has been moved in main thread.

> https://code.videolan.org/videolan/vlc-android/commit/583944ffa434edeb110c54cb1c53adf3db469640
---

 .../org/videolan/mobile/app/AppSetupDelegate.kt    | 26 +++++++++-------------
 1 file changed, 10 insertions(+), 16 deletions(-)

diff --git a/application/app/src/main/java/org/videolan/mobile/app/AppSetupDelegate.kt b/application/app/src/main/java/org/videolan/mobile/app/AppSetupDelegate.kt
index 3e8ccab2cf..45da6efc63 100644
--- a/application/app/src/main/java/org/videolan/mobile/app/AppSetupDelegate.kt
+++ b/application/app/src/main/java/org/videolan/mobile/app/AppSetupDelegate.kt
@@ -24,7 +24,6 @@ import android.content.ComponentName
 import android.content.Context
 import android.content.pm.PackageManager
 import android.os.Build
-import android.util.Log
 import kotlinx.coroutines.DEBUG_PROPERTY_NAME
 import kotlinx.coroutines.DEBUG_PROPERTY_VALUE_ON
 import kotlinx.coroutines.Dispatchers
@@ -45,7 +44,6 @@ import org.videolan.tools.AppScope
 import org.videolan.tools.Settings
 import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.gui.SendCrashActivity
-import org.videolan.vlc.gui.helpers.AudioUtil
 import org.videolan.vlc.gui.helpers.NotificationHelper
 import org.videolan.vlc.util.DialogDelegate
 import org.videolan.vlc.util.SettingsMigration
@@ -68,7 +66,6 @@ class AppSetupDelegate : AppDelegate,
         NotificationHelper.createNotificationChannels(this)
 
         // Service loaders
-        Log.i("AppSetupDelegate", "Registering factories")
         FactoryManager.registerFactory(IMediaFactory.factoryId, MediaFactory())
         FactoryManager.registerFactory(ILibVLCFactory.factoryId, LibVLCFactory())
         System.setProperty(DEBUG_PROPERTY_NAME, DEBUG_PROPERTY_VALUE_ON)
@@ -83,24 +80,21 @@ class AppSetupDelegate : AppDelegate,
                 setupIndexers()
             }
         }
+        AppContextProvider.setLocale(Settings.getInstance(this).getString("set_locale", ""))
 
         //Initiate Kotlinx Dispatchers in a thread to prevent ANR
         backgroundInit()
     }
 
     // init operations executed in background threads
-    private fun Context.backgroundInit() {
-        Thread(Runnable {
-            AppContextProvider.setLocale(Settings.getInstance(this).getString("set_locale", ""))
-
-            AppScope.launch(Dispatchers.IO) {
+    private fun Context.backgroundInit() = AppScope.launch {
+        launch(Dispatchers.IO) {
+            if (!VLCInstance.testCompatibleCPU(AppContextProvider.appContext)) return at launch
+            Dialog.setCallbacks(VLCInstance.getInstance(this at backgroundInit), DialogDelegate)
+        }
+        packageManager.setComponentEnabledSetting(ComponentName(this at backgroundInit, SendCrashActivity::class.java),
+                if (BuildConfig.BETA) PackageManager.COMPONENT_ENABLED_STATE_ENABLED else PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP)
+        SettingsMigration.migrateSettings(this at backgroundInit)
 
-                if (!VLCInstance.testCompatibleCPU(AppContextProvider.appContext)) return at launch
-                Dialog.setCallbacks(VLCInstance.getInstance(this at backgroundInit), DialogDelegate)
-            }
-            packageManager.setComponentEnabledSetting(ComponentName(this, SendCrashActivity::class.java),
-                    if (BuildConfig.BETA) PackageManager.COMPONENT_ENABLED_STATE_ENABLED else PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP)
-            SettingsMigration.migrateSettings(this)
-        }).start()
     }
-}
\ No newline at end of file
+}



More information about the Android mailing list