-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Polling only happens when app is in foreground
- Loading branch information
1 parent
0ce7b0b
commit 93d0403
Showing
3 changed files
with
60 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
app/src/main/java/tech/relaycorp/courier/background/ForegroundAppMonitor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tech.relaycorp.courier.background | ||
|
||
import android.app.Activity | ||
import android.app.Application | ||
import android.os.Bundle | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class ForegroundAppMonitor | ||
@Inject constructor() : Application.ActivityLifecycleCallbacks { | ||
private val activityCountFlow = MutableStateFlow(0) | ||
|
||
fun observe() = activityCountFlow.map { if (it == 0) State.Background else State.Foreground } | ||
|
||
override fun onActivityStarted(activity: Activity) { | ||
activityCountFlow.value++ | ||
} | ||
|
||
override fun onActivityStopped(activity: Activity) { | ||
activityCountFlow.value-- | ||
} | ||
|
||
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) = Unit | ||
override fun onActivityResumed(activity: Activity) = Unit | ||
override fun onActivityPaused(activity: Activity) = Unit | ||
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) = Unit | ||
override fun onActivityDestroyed(activity: Activity) = Unit | ||
|
||
enum class State { | ||
Foreground, Background | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters