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 aa09076145..4f462256f5 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 @@ -37,6 +37,8 @@ external fun chatParseServer(str: String): String class SimplexApp: Application(), LifecycleEventObserver { lateinit var chatController: ChatController + var isAppOnForeground: Boolean = false + fun initChatController(useKey: String? = null, startChat: Boolean = true) { val dbKey = useKey ?: DatabaseUtils.useDatabaseKey() ?: "" val dbAbsolutePathPrefix = getFilesDirectory(SimplexApp.context) @@ -96,6 +98,7 @@ class SimplexApp: Application(), LifecycleEventObserver { withApi { when (event) { Lifecycle.Event.ON_START -> { + isAppOnForeground = true if (chatModel.chatRunning.value == true) { kotlin.runCatching { val chats = chatController.apiGetChats() @@ -104,6 +107,7 @@ class SimplexApp: Application(), LifecycleEventObserver { } } Lifecycle.Event.ON_RESUME -> { + isAppOnForeground = true if (chatModel.onboardingStage.value == OnboardingStage.OnboardingComplete) { chatController.showBackgroundServiceNoticeIfNeeded() } @@ -115,7 +119,7 @@ class SimplexApp: Application(), LifecycleEventObserver { if (chatModel.chatRunning.value != false && appPreferences.notificationsMode.get() == NotificationsMode.SERVICE.name) SimplexService.start(applicationContext) } - else -> {} + else -> isAppOnForeground = false } } } diff --git a/apps/android/app/src/main/java/chat/simplex/app/model/NtfManager.kt b/apps/android/app/src/main/java/chat/simplex/app/model/NtfManager.kt index 596a2f8cde..033740ba2a 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/model/NtfManager.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/model/NtfManager.kt @@ -153,7 +153,7 @@ class NtfManager(val context: Context, private val appPreferences: AppPreference } fun notifyCallInvitation(invitation: RcvCallInvitation) { - val inForeground = isAppOnForeground(context) + if (SimplexApp.context.isAppOnForeground) return val keyguardManager = getKeyguardManager(context) Log.d(TAG, "notifyCallInvitation pre-requests: device locked ${keyguardManager.isDeviceLocked}, " + @@ -161,7 +161,6 @@ class NtfManager(val context: Context, private val appPreferences: AppPreference "callOnLockScreen ${appPreferences.callOnLockScreen.get()}, " + "inForeground $inForeground" ) - if (inForeground) return val contactId = invitation.contact.id Log.d(TAG, "notifyCallInvitation $contactId") val image = invitation.contact.image 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 0bd602b82a..b82fe0a589 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 @@ -1,8 +1,6 @@ package chat.simplex.app.model import android.annotation.SuppressLint -import android.app.ActivityManager -import android.app.ActivityManager.RunningAppProcessInfo import android.app.Application import android.content.* import android.net.Uri @@ -42,18 +40,6 @@ import java.util.Date typealias ChatCtrl = Long -fun isAppOnForeground(context: Context): Boolean { - val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager - val appProcesses = activityManager.runningAppProcesses ?: return false - val packageName = context.packageName - for (appProcess in appProcesses) { - if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName == packageName) { - return true - } - } - return false -} - enum class CallOnLockScreen { DISABLE, SHOW, @@ -1065,7 +1051,7 @@ open class ChatController(var ctrl: ChatCtrl?, val ntfManager: NtfManager, val a } else if (cItem.content.msgContent is MsgContent.MCVoice && file != null && file.fileSize <= MAX_VOICE_SIZE_AUTO_RCV && file.fileSize > MAX_VOICE_SIZE_FOR_SENDING && appPrefs.privacyAcceptImages.get()) { withApi { receiveFile(file.fileId) } // TODO check inlineFileMode != IFMSent } - if (cItem.showNotification && (!isAppOnForeground(appContext) || chatModel.chatId.value != cInfo.id)) { + if (cItem.showNotification && (!SimplexApp.context.isAppOnForeground || chatModel.chatId.value != cInfo.id)) { ntfManager.notifyMessageReceived(cInfo, cItem) } }