Skip to content

Commit

Permalink
service: attempt to band-aid foreground limit
Browse files Browse the repository at this point in the history
  • Loading branch information
OxygenCobalt committed Jan 12, 2025
1 parent ad4b9a3 commit a1289ff
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions app/src/main/java/org/oxycblt/auxio/AuxioService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@ import androidx.media.MediaBrowserServiceCompat
import androidx.media.utils.MediaConstants
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.oxycblt.auxio.music.service.MusicServiceFragment
import org.oxycblt.auxio.playback.service.PlaybackServiceFragment
import timber.log.Timber as L

@AndroidEntryPoint
class AuxioService :
Expand All @@ -46,6 +53,10 @@ class AuxioService :
@Inject lateinit var musicFragmentFactory: MusicServiceFragment.Factory
private lateinit var musicFragment: MusicServiceFragment

private val delayScopeJob = Job() + Dispatchers.Main
private val delayScope = CoroutineScope(delayScopeJob)
private var currentDelayJob: Job? = null

@SuppressLint("WrongConstant")
override fun onCreate() {
super.onCreate()
Expand All @@ -68,9 +79,17 @@ class AuxioService :
}

private fun onHandleForeground(intent: Intent?) {
val startId = intent?.getIntExtra(INTENT_KEY_START_ID, -1) ?: -1
musicFragment.start()
playbackFragment.start(startId)
currentDelayJob?.cancel()
currentDelayJob =
delayScope.launch {
// The foreground limiter is fussy and doesn't like us starting a foreground
// service too early despite having the right to do so at this point. Comply
// and artificially delay (to user detriment...)
delay(250)
val startId = intent?.getIntExtra(INTENT_KEY_START_ID, -1) ?: -1
musicFragment.start()
playbackFragment.start(startId)
}
}

override fun onTaskRemoved(rootIntent: Intent?) {
Expand All @@ -80,6 +99,7 @@ class AuxioService :

override fun onDestroy() {
super.onDestroy()
delayScopeJob.cancel()
musicFragment.release()
playbackFragment.release()
}
Expand Down

0 comments on commit a1289ff

Please sign in to comment.