From 0160684004d452848e6f3c1030637fabfd655ff1 Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Tue, 23 May 2023 11:19:13 +0300 Subject: [PATCH] android: periodic notifications and service start fix when database migrates (#2488) * android: periodic notifications and service start fix when database migrates * less timeout --- .../java/chat/simplex/app/SimplexService.kt | 17 +++++++++++++++-- .../app/views/helpers/MessagesFetcherWorker.kt | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/apps/android/app/src/main/java/chat/simplex/app/SimplexService.kt b/apps/android/app/src/main/java/chat/simplex/app/SimplexService.kt index 63e31cb226..497dc2ac86 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/SimplexService.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/SimplexService.kt @@ -9,9 +9,9 @@ import android.util.Log import androidx.core.app.NotificationCompat import androidx.core.content.ContextCompat import androidx.work.* +import chat.simplex.app.model.ChatController import chat.simplex.app.views.helpers.* -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext +import kotlinx.coroutines.* // based on: // https://robertohuertas.com/2019/06/29/android_foreground_services/ @@ -86,6 +86,7 @@ class SimplexService: Service() { isStartingService = true withApi { val chatController = (application as SimplexApp).chatController + waitDbMigrationEnds(chatController) try { Log.w(TAG, "Starting foreground service") val chatDbStatus = chatController.chatModel.chatDbStatus.value @@ -324,6 +325,18 @@ class SimplexService: Service() { notificationManager.cancel(PASSPHRASE_NOTIFICATION_ID) } + /* + * When the app starts the database is in migration process. It can take from seconds to tens of seconds. + * It happens in background thread, so other places that relies on database should wait til the end of migration + * */ + suspend fun waitDbMigrationEnds(chatController: ChatController) { + var maxWaitTime = 50_000 + while (chatController.chatModel.chatDbStatus.value == null && maxWaitTime > 0) { + delay(50) + maxWaitTime -= 50 + } + } + private fun getPreferences(context: Context): SharedPreferences = context.getSharedPreferences(SHARED_PREFS_ID, Context.MODE_PRIVATE) } } diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/helpers/MessagesFetcherWorker.kt b/apps/android/app/src/main/java/chat/simplex/app/views/helpers/MessagesFetcherWorker.kt index 17b856017d..5b18ff5220 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/helpers/MessagesFetcherWorker.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/helpers/MessagesFetcherWorker.kt @@ -56,6 +56,7 @@ class MessagesFetcherWork( try { withTimeout(durationSeconds * 1000L) { val chatController = (applicationContext as SimplexApp).chatController + SimplexService.waitDbMigrationEnds(chatController) val chatDbStatus = chatController.chatModel.chatDbStatus.value if (chatDbStatus != DBMigrationResult.OK) { Log.w(TAG, "Worker: problem with the database: $chatDbStatus")