Skip to content
Snippets Groups Projects
Unverified Commit 912f2be4 authored by Matthias Urhahn's avatar Matthias Urhahn Committed by GitHub
Browse files

Change the polling based flows. (#1707)


callbackflow wasn't a good fit and this should be a simpler solution.
May fix some class cast exception on MediaTek chipsets.

Co-authored-by: default avatarharambasicluka <64483219+harambasicluka@users.noreply.github.com>
parent 02774137
No related branches found
No related tags found
No related merge requests found
......@@ -5,16 +5,16 @@ import de.rki.coronawarnapp.exception.ExceptionCategory
import de.rki.coronawarnapp.exception.reporting.report
import de.rki.coronawarnapp.util.coroutine.AppScope
import de.rki.coronawarnapp.util.flow.shareLatest
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.isActive
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
......@@ -28,24 +28,26 @@ class DefaultTracingStatus @Inject constructor(
@AppScope val scope: CoroutineScope
) : TracingStatus {
override val isTracingEnabled: Flow<Boolean> = callbackFlow<Boolean> {
override val isTracingEnabled: Flow<Boolean> = flow {
while (true) {
try {
send(pollIsEnabled())
} catch (e: Exception) {
Timber.w(e, "ENF isEnabled failed.")
send(false)
e.report(ExceptionCategory.EXPOSURENOTIFICATION, TAG, null)
cancel("ENF isEnabled failed", e)
emit(pollIsEnabled())
delay(POLLING_DELAY_MS)
} catch (e: CancellationException) {
Timber.d("isBackgroundRestricted was cancelled")
break
}
if (!isActive) break
delay(POLLING_DELAY_MS)
}
}
.distinctUntilChanged()
.onStart { Timber.v("isTracingEnabled FLOW start") }
.onEach { Timber.v("isTracingEnabled FLOW emission: %b", it) }
.onCompletion { Timber.v("isTracingEnabled FLOW completed.") }
.onCompletion { if (it == null) Timber.v("isTracingEnabled FLOW completed.") }
.catch {
Timber.w(it, "ENF isEnabled failed.")
it.report(ExceptionCategory.EXPOSURENOTIFICATION, TAG, null)
emit(false)
}
.shareLatest(
tag = TAG,
scope = scope
......
......@@ -4,13 +4,13 @@ import android.content.Context
import de.rki.coronawarnapp.util.coroutine.AppScope
import de.rki.coronawarnapp.util.di.AppContext
import de.rki.coronawarnapp.util.flow.shareLatest
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.isActive
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onCompletion
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
......@@ -21,41 +21,41 @@ class BackgroundModeStatus @Inject constructor(
@AppScope private val appScope: CoroutineScope
) {
val isBackgroundRestricted: Flow<Boolean?> = callbackFlow<Boolean> {
val isBackgroundRestricted: Flow<Boolean> = flow {
while (true) {
try {
send(pollIsBackgroundRestricted())
} catch (e: Exception) {
Timber.w(e, "isBackgroundRestricted failed.")
cancel("isBackgroundRestricted failed", e)
emit(pollIsBackgroundRestricted())
delay(POLLING_DELAY_MS)
} catch (e: CancellationException) {
Timber.d("isBackgroundRestricted was cancelled")
break
}
if (!isActive) break
delay(POLLING_DELAY_MS)
}
}
.distinctUntilChanged()
.onCompletion {
if (it != null) Timber.w(it, "isBackgroundRestricted failed.")
}
.shareLatest(
tag = "isBackgroundRestricted",
scope = appScope
)
val isAutoModeEnabled: Flow<Boolean> = callbackFlow<Boolean> {
val isAutoModeEnabled: Flow<Boolean> = flow {
while (true) {
try {
send(pollIsAutoMode())
} catch (e: Exception) {
Timber.w(e, "autoModeEnabled failed.")
cancel("autoModeEnabled failed", e)
emit(pollIsAutoMode())
delay(POLLING_DELAY_MS)
} catch (e: CancellationException) {
Timber.d("isAutoModeEnabled was cancelled")
break
}
if (!isActive) break
delay(POLLING_DELAY_MS)
}
}
.distinctUntilChanged()
.onCompletion {
if (it != null) Timber.w(it, "autoModeEnabled failed.")
}
.shareLatest(
tag = "autoModeEnabled",
scope = appScope
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment