diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 0514dcb80a2..bae74e567ec 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -130,6 +130,7 @@ dependencies { implementation(libs.androidx.lifecycle.viewModel) implementation(libs.androidx.lifecycle.viewModelCompose) implementation(libs.androidx.lifecycle.liveData) + implementation(libs.androidx.lifecycle.process) implementation(libs.androidx.lifecycle.runtime) implementation(libs.androidx.lifecycle.viewModelSavedState) diff --git a/app/src/main/kotlin/com/wire/android/WireApplication.kt b/app/src/main/kotlin/com/wire/android/WireApplication.kt index 432b8d95170..034338ed1e8 100644 --- a/app/src/main/kotlin/com/wire/android/WireApplication.kt +++ b/app/src/main/kotlin/com/wire/android/WireApplication.kt @@ -22,6 +22,10 @@ import android.app.Application import android.content.ComponentCallbacks2 import android.os.Build import android.os.StrictMode +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleEventObserver +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.ProcessLifecycleOwner import androidx.work.Configuration import co.touchlab.kermit.platformLogWriter import com.wire.android.datastore.GlobalDataStore @@ -43,8 +47,10 @@ import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import javax.inject.Inject +var isAppInForeground = false + @HiltAndroidApp -class WireApplication : Application(), Configuration.Provider { +class WireApplication : Application(), Configuration.Provider, LifecycleEventObserver { @Inject @KaliumCoreLogic @@ -79,6 +85,7 @@ class WireApplication : Application(), Configuration.Provider { super.onCreate() enableStrictMode() + ProcessLifecycleOwner.get().lifecycle.addObserver(this) globalAppScope.launch { initializeApplicationLoggingFrameworks() @@ -95,6 +102,10 @@ class WireApplication : Application(), Configuration.Provider { } } + override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { + isAppInForeground = event == Lifecycle.Event.ON_RESUME + } + private fun enableStrictMode() { if (BuildConfig.DEBUG) { StrictMode.setThreadPolicy( diff --git a/app/src/main/kotlin/com/wire/android/util/lifecycle/ConnectionPolicyManager.kt b/app/src/main/kotlin/com/wire/android/util/lifecycle/ConnectionPolicyManager.kt index 61fdf861c57..2e3654ed062 100644 --- a/app/src/main/kotlin/com/wire/android/util/lifecycle/ConnectionPolicyManager.kt +++ b/app/src/main/kotlin/com/wire/android/util/lifecycle/ConnectionPolicyManager.kt @@ -20,6 +20,7 @@ package com.wire.android.util.lifecycle import com.wire.android.appLogger import com.wire.android.di.KaliumCoreLogic +import com.wire.android.isAppInForeground import com.wire.android.migration.MigrationManager import com.wire.android.util.CurrentScreenManager import com.wire.android.util.dispatchers.DispatcherProvider @@ -36,8 +37,6 @@ import com.wire.kalium.logic.functional.isLeft import com.wire.kalium.logic.functional.map import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.delay -import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import javax.inject.Inject import javax.inject.Singleton @@ -68,13 +67,9 @@ class ConnectionPolicyManager @Inject constructor( */ fun startObservingAppLifecycle() { CoroutineScope(dispatcherProvider.default()).launch { - combine( - currentScreenManager.isAppVisibleFlow(), - migrationManager.isMigrationCompletedFlow(), - ::Pair - ).collect { (isVisible, isMigrationCompleted) -> + migrationManager.isMigrationCompletedFlow().collect {isMigrationCompleted -> if (isMigrationCompleted) { - setPolicyForSessions(allValidSessions(), isVisible) + setPolicyForSessions(allValidSessions(), isAppInForeground) } } } @@ -118,9 +113,7 @@ class ConnectionPolicyManager @Inject constructor( private suspend fun UserSessionScope.downgradePolicyIfNeeded( userId: UserId ) { - val isAppVisible = currentScreenManager.isAppVisibleFlow().first() - logger.d("$TAG isAppVisible = $isAppVisible") - if (!isAppVisible) { + if (!isAppInForeground) { logger.d("$TAG ${userId.toString().obfuscateId()} Downgrading policy as conditions to KEEP_ALIVE are not met") setConnectionPolicy(ConnectionPolicy.DISCONNECT_AFTER_PENDING_EVENTS) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 32ae97c1b02..fe23043e22d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -156,6 +156,7 @@ firebase-fcm = { module = "com.google.firebase:firebase-messaging-ktx" } ## AndroidX - Lifecycle androidx-lifecycle-liveData = { module = "androidx.lifecycle:lifecycle-livedata-ktx", version.ref = "androidx-lifecycle" } +androidx-lifecycle-process = { module = "androidx.lifecycle:lifecycle-process", version.ref = "androidx-lifecycle" } androidx-lifecycle-runtime = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "androidx-lifecycle" } androidx-lifecycle-viewModel = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "androidx-lifecycle" } androidx-lifecycle-viewModelCompose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidx-lifecycle" }