diff --git a/apps/android/app/src/main/java/chat/simplex/app/SimplexApp.kt b/apps/android/app/src/main/java/chat/simplex/app/SimplexApp.kt index e0eded442a..aaaccbe681 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/SimplexApp.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/SimplexApp.kt @@ -57,6 +57,7 @@ class SimplexApp: Application(), LifecycleEventObserver { chatModel.onboardingStage.value = OnboardingStage.Step1_SimpleXInfo } else { chatController.startChat(user) + SimplexService.start(applicationContext) chatController.showBackgroundServiceNoticeIfNeeded() } } @@ -67,9 +68,9 @@ class SimplexApp: Application(), LifecycleEventObserver { withApi { when (event) { Lifecycle.Event.ON_STOP -> - if (appPreferences.runServiceInBackground.get() && chatModel.chatRunning.value != false) SimplexService.start(applicationContext) + if (!appPreferences.runServiceInBackground.get()) SimplexService.stop(applicationContext) Lifecycle.Event.ON_START -> - SimplexService.stop(applicationContext) + if (chatModel.chatRunning.value != false) SimplexService.start(applicationContext) Lifecycle.Event.ON_RESUME -> if (chatModel.onboardingStage.value == OnboardingStage.OnboardingComplete) { chatController.showBackgroundServiceNoticeIfNeeded() 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 6163e6d97b..516b542187 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 @@ -7,7 +7,6 @@ import android.util.Log import androidx.core.app.NotificationCompat import androidx.core.content.ContextCompat import androidx.work.* -import chat.simplex.app.model.AppPreferences import chat.simplex.app.views.helpers.withApi import chat.simplex.app.views.onboarding.OnboardingStage import kotlinx.coroutines.Dispatchers @@ -21,7 +20,6 @@ class SimplexService: Service() { private var wakeLock: PowerManager.WakeLock? = null private var isServiceStarted = false private var isStartingService = false - private var isStoppingService = false private var notificationManager: NotificationManager? = null private var serviceNotification: Notification? = null private val chatController by lazy { (application as SimplexApp).chatController } @@ -88,8 +86,6 @@ class SimplexService: Service() { private fun stopService() { Log.d(TAG, "Stopping foreground service") - if (isStoppingService) return - isStoppingService = true try { wakeLock?.let { while (it.isHeld) it.release() // release all, in case acquired more than once @@ -100,7 +96,6 @@ class SimplexService: Service() { } catch (e: Exception) { Log.d(TAG, "Service stopped without being started: ${e.message}") } - isStoppingService = false isServiceStarted = false saveServiceState(this, ServiceState.STOPPED) } @@ -128,7 +123,7 @@ class SimplexService: Service() { .setContentTitle(title) .setContentText(text) .setContentIntent(pendingIntent) - .setSound(null) + .setSilent(true) .setShowWhen(false) // no date/time .build() } @@ -215,7 +210,6 @@ class SimplexService: Service() { suspend fun stop(context: Context) = serviceAction(context, Action.STOP) private suspend fun serviceAction(context: Context, action: Action) { - if (!AppPreferences(context).runServiceInBackground.get()) { return } Log.d(TAG, "SimplexService serviceAction: ${action.name}") withContext(Dispatchers.IO) { Intent(context, SimplexService::class.java).also { diff --git a/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt b/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt index 96766e10ab..d47dd178ef 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt @@ -145,6 +145,7 @@ open class ChatController(private val ctrl: ChatCtrl, val ntfManager: NtfManager suspend fun startChat(user: User) { Log.d(TAG, "user: $user") try { + if (chatModel.chatRunning.value == true) return val justStarted = apiStartChat() apiSetFilesFolder(getAppFilesDirectory(appContext)) chatModel.userAddress.value = apiGetUserAddress() diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/WelcomeView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/WelcomeView.kt index 862e476841..b7faf8e8b6 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/WelcomeView.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/WelcomeView.kt @@ -115,6 +115,7 @@ fun createProfile(chatModel: ChatModel, displayName: String, fullName: String) { ) chatModel.controller.startChat(user) chatModel.controller.showBackgroundServiceNoticeIfNeeded() + SimplexService.start(chatModel.controller.appContext) chatModel.onboardingStage.value = OnboardingStage.OnboardingComplete } } diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/database/DatabaseView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/database/DatabaseView.kt index 8a23e304e8..61d2ab1942 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/database/DatabaseView.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/database/DatabaseView.kt @@ -24,8 +24,8 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import chat.simplex.app.* import chat.simplex.app.R -import chat.simplex.app.TAG import chat.simplex.app.model.* import chat.simplex.app.ui.theme.HighOrLowlight import chat.simplex.app.ui.theme.SimpleXTheme @@ -69,8 +69,8 @@ fun DatabaseView( chatArchiveName, chatArchiveTime, chatLastStart, - startChat = { startChat(m, runChat, chatLastStart) }, - stopChatAlert = { stopChatAlert(m, runChat) }, + startChat = { startChat(m, runChat, chatLastStart, context) }, + stopChatAlert = { stopChatAlert(m, runChat, context) }, exportArchive = { exportArchive(context, m, progressIndicator, chatArchiveName, chatArchiveTime, chatArchiveFile, saveArchiveLauncher) }, deleteChatAlert = { deleteChatAlert(m, progressIndicator) }, showSettingsModal @@ -230,7 +230,7 @@ fun SettingsSectionFooter(text: String) { Text(text, color = HighOrLowlight, modifier = Modifier.padding(start = 16.dp, top = 5.dp).fillMaxWidth(0.9F), fontSize = 12.sp) } -private fun startChat(m: ChatModel, runChat: MutableState, chatLastStart: MutableState) { +private fun startChat(m: ChatModel, runChat: MutableState, chatLastStart: MutableState, context: Context) { withApi { try { m.controller.apiStartChat() @@ -239,6 +239,7 @@ private fun startChat(m: ChatModel, runChat: MutableState, chatLastStar val ts = Clock.System.now() m.controller.appPrefs.chatLastStart.set(ts) chatLastStart.value = ts + SimplexService.start(context) } catch (e: Error) { runChat.value = false AlertManager.shared.showAlertMsg(generalGetString(R.string.error_starting_chat), e.toString()) @@ -246,22 +247,23 @@ private fun startChat(m: ChatModel, runChat: MutableState, chatLastStar } } -private fun stopChatAlert(m: ChatModel, runChat: MutableState) { +private fun stopChatAlert(m: ChatModel, runChat: MutableState, context: Context) { AlertManager.shared.showAlertDialog( title = generalGetString(R.string.stop_chat_question), text = generalGetString(R.string.stop_chat_to_export_import_or_delete_chat_database), confirmText = generalGetString(R.string.stop_chat_confirmation), - onConfirm = { stopChat(m, runChat) }, + onConfirm = { stopChat(m, runChat, context) }, onDismiss = { runChat.value = true } ) } -private fun stopChat(m: ChatModel, runChat: MutableState) { +private fun stopChat(m: ChatModel, runChat: MutableState, context: Context) { withApi { try { m.controller.apiStopChat() runChat.value = false m.chatRunning.value = false + SimplexService.stop(context) } catch (e: Error) { runChat.value = true AlertManager.shared.showAlertMsg(generalGetString(R.string.error_starting_chat), e.toString())