diff --git a/opencloudApp/src/main/java/eu/opencloud/android/MainApp.kt b/opencloudApp/src/main/java/eu/opencloud/android/MainApp.kt index a610a8a7a8..1658d344af 100644 --- a/opencloudApp/src/main/java/eu/opencloud/android/MainApp.kt +++ b/opencloudApp/src/main/java/eu/opencloud/android/MainApp.kt @@ -90,6 +90,8 @@ import org.koin.android.ext.koin.androidContext import org.koin.core.context.startKoin import org.koin.core.context.stopKoin import timber.log.Timber +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit /** * Main Application of the project @@ -362,6 +364,9 @@ class MainApp : Application() { const val PREFERENCE_KEY_LAST_SEEN_VERSION_CODE = "lastSeenVersionCode" + private const val DEPENDENCY_INJECTION_TIMEOUT_SECONDS = 10L + private val dependencyInjectionReady = CountDownLatch(1) + const val PREFERENCE_KEY_DONT_SHOW_SERVER_ACCOUNT_WARNING_DIALOG = "PREFERENCE_KEY_DONT_SHOW_SERVER_ACCOUNT_WARNING_DIALOG" /** @@ -413,6 +418,12 @@ class MainApp : Application() { return String.format(appString, version) } + fun awaitDependencyInjection() { + if (!dependencyInjectionReady.await(DEPENDENCY_INJECTION_TIMEOUT_SECONDS, TimeUnit.SECONDS)) { + Timber.w("Dependency injection not ready after $DEPENDENCY_INJECTION_TIMEOUT_SECONDS s") + } + } + fun initDependencyInjection() { stopKoin() startKoin { @@ -428,6 +439,7 @@ class MainApp : Application() { ) ) } + dependencyInjectionReady.countDown() } fun getLastSeenVersionCode(): Int { diff --git a/opencloudApp/src/main/java/eu/opencloud/android/presentation/documentsprovider/DocumentsStorageProvider.kt b/opencloudApp/src/main/java/eu/opencloud/android/presentation/documentsprovider/DocumentsStorageProvider.kt index 16d91d5558..5074da7491 100644 --- a/opencloudApp/src/main/java/eu/opencloud/android/presentation/documentsprovider/DocumentsStorageProvider.kt +++ b/opencloudApp/src/main/java/eu/opencloud/android/presentation/documentsprovider/DocumentsStorageProvider.kt @@ -73,7 +73,7 @@ import androidx.work.WorkManager import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import org.koin.android.ext.android.inject +import org.koin.core.context.GlobalContext import timber.log.Timber import java.io.File import java.io.FileNotFoundException @@ -816,6 +816,12 @@ class DocumentsStorageProvider : DocumentsProvider() { if (isVault(file)) throw UnsupportedOperationException("${file.remotePath} is an encrypted vault") } + // Android may start the process for this provider and deliver binder calls before MainApp has started Koin + private inline fun inject(): Lazy = lazy { + MainApp.awaitDependencyInjection() + GlobalContext.get().get() + } + private fun getFileByIdOrException(id: Int): OCFile { val getFileByIdUseCase: GetFileByIdUseCase by inject() val result = getFileByIdUseCase(GetFileByIdUseCase.Params(id.toLong()))