From 583944ffa434edeb110c54cb1c53adf3db469640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geoffrey=20M=C3=A9tais?= Date: Wed, 21 Oct 2020 09:21:00 +0200 Subject: [PATCH] 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. --- .../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 3e8ccab2c..45da6efc6 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@launch + Dialog.setCallbacks(VLCInstance.getInstance(this@backgroundInit), DialogDelegate) + } + packageManager.setComponentEnabledSetting(ComponentName(this@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@backgroundInit) - if (!VLCInstance.testCompatibleCPU(AppContextProvider.appContext)) return@launch - Dialog.setCallbacks(VLCInstance.getInstance(this@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 +}