Skip to content

Commit

Permalink
Track app foreground state via AppTheme using separate object (#1335)
Browse files Browse the repository at this point in the history
* Track app in foreground state via AppTheme using separate object

* Make inForeground read only; add kdoc

* Remove type
  • Loading branch information
sunkup authored Mar 5, 2025
1 parent ced6abe commit 0012dec
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 16 deletions.
10 changes: 2 additions & 8 deletions app/src/main/kotlin/at/bitfire/davdroid/network/HttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import at.bitfire.davdroid.db.Credentials
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.settings.Settings
import at.bitfire.davdroid.settings.SettingsManager
import at.bitfire.davdroid.ui.ForegroundTracker
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow
import net.openid.appauth.AuthState
import net.openid.appauth.AuthorizationService
import okhttp3.Authenticator
Expand Down Expand Up @@ -120,12 +120,6 @@ class HttpClient(
return this
}

private var appInForeground: MutableStateFlow<Boolean>? = MutableStateFlow(false)
fun inForeground(foreground: Boolean): Builder {
appInForeground?.value = foreground
return this
}

private var cache: Cache? = null
@Suppress("unused")
fun withDiskCache(maxSize: Long = 10*1024*1024): Builder {
Expand Down Expand Up @@ -240,7 +234,7 @@ class HttpClient(
val certManager = CustomCertManager(
context = context,
trustSystemCerts = !settingsManager.getBoolean(Settings.DISTRUST_SYSTEM_CERTIFICATES),
appInForeground = appInForeground
appInForeground = ForegroundTracker.inForeground
)

val sslContext = SSLContext.getInstance("TLS")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class NextcloudLoginFlow @Inject constructor(
}

val httpClient = httpClientBuilder
.inForeground(true)
.build()

override fun close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ class PushRegistrationWorker @AssistedInject constructor(
private suspend fun registerPushSubscription(collection: Collection, account: Account, endpoint: String) {
httpClientBuilder.get()
.fromAccount(account)
.inForeground(true)
.build()
.use { client ->
runInterruptible {
Expand Down Expand Up @@ -151,7 +150,6 @@ class PushRegistrationWorker @AssistedInject constructor(
private suspend fun unregisterPushSubscription(collection: Collection, account: Account, url: HttpUrl) {
httpClientBuilder.get()
.fromAccount(account)
.inForeground(true)
.build()
.use { httpClient ->
runInterruptible {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ class DavCollectionRepository @Inject constructor(

httpClientBuilder.get()
.fromAccount(account)
.inForeground(true)
.build()
.use { httpClient ->
runInterruptible(Dispatchers.IO) {
Expand Down Expand Up @@ -296,7 +295,6 @@ class DavCollectionRepository @Inject constructor(
private suspend fun createOnServer(account: Account, url: HttpUrl, method: String, xmlBody: String) {
httpClientBuilder.get()
.fromAccount(account)
.inForeground(true)
.build()
.use { httpClient ->
runInterruptible(Dispatchers.IO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ class DavResourceFinder @AssistedInject constructor(
private var encountered401 = false

private val httpClient = httpClientBuilder
.inForeground(true)
.setLogger(log)
.apply {
if (credentials != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ class RefreshCollectionsWorker @AssistedInject constructor(
// create authenticating OkHttpClient (credentials taken from account settings)
httpClientBuilder
.fromAccount(account)
.inForeground(true)
.build()
.use { httpClient ->
runInterruptible {
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/kotlin/at/bitfire/davdroid/ui/AppTheme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.platform.LocalView
import androidx.lifecycle.compose.LifecycleResumeEffect
import at.bitfire.davdroid.ui.composable.SafeAndroidUriHandler

@Composable
Expand Down Expand Up @@ -46,4 +47,12 @@ fun AppTheme(
content = content
)
}

// Track if the app is in the foreground
LifecycleResumeEffect(view) {
ForegroundTracker.onResume()
onPauseOrDispose {
ForegroundTracker.onPaused()
}
}
}
35 changes: 35 additions & 0 deletions app/src/main/kotlin/at/bitfire/davdroid/ui/ForegroundTracker.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
*/

package at.bitfire.davdroid.ui

import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow

/**
* Used to track whether the app is in foreground (visible to user) or not.
*/
object ForegroundTracker {

private val _inForeground = MutableStateFlow(false)

/**
* Whether the app is in foreground or not.
*/
val inForeground = _inForeground.asStateFlow()

/**
* Called when the app is resumed (at [androidx.lifecycle.Lifecycle.Event.ON_RESUME])
*/
fun onResume() {
_inForeground.value = true
}

/**
* Called when the app is paused (at [androidx.lifecycle.Lifecycle.Event.ON_PAUSE])
*/
fun onPaused() {
_inForeground.value = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ class WebDavMountRepository @Inject constructor(
val validVersions = arrayOf("1", "2", "3")

val builder = httpClientBuilder.get()
.inForeground(true)

if (credentials != null)
builder.authenticate(
Expand Down

0 comments on commit 0012dec

Please sign in to comment.