service: try fixing foreground errors
Probably caused by services being sticky.
This commit is contained in:
parent
9ae3587a7e
commit
dddab1eda7
2 changed files with 14 additions and 24 deletions
|
@ -34,14 +34,9 @@ import androidx.media.MediaBrowserServiceCompat
|
||||||
import androidx.media.utils.MediaConstants
|
import androidx.media.utils.MediaConstants
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import javax.inject.Inject
|
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.music.service.MusicServiceFragment
|
||||||
import org.oxycblt.auxio.playback.service.PlaybackServiceFragment
|
import org.oxycblt.auxio.playback.service.PlaybackServiceFragment
|
||||||
|
import timber.log.Timber
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class AuxioService :
|
class AuxioService :
|
||||||
|
@ -52,10 +47,6 @@ class AuxioService :
|
||||||
@Inject lateinit var musicFragmentFactory: MusicServiceFragment.Factory
|
@Inject lateinit var musicFragmentFactory: MusicServiceFragment.Factory
|
||||||
private lateinit var musicFragment: MusicServiceFragment
|
private lateinit var musicFragment: MusicServiceFragment
|
||||||
|
|
||||||
private val delayScopeJob = Job() + Dispatchers.Main
|
|
||||||
private val delayScope = CoroutineScope(delayScopeJob)
|
|
||||||
private var currentDelayJob: Job? = null
|
|
||||||
|
|
||||||
@SuppressLint("WrongConstant")
|
@SuppressLint("WrongConstant")
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
|
@ -63,32 +54,31 @@ class AuxioService :
|
||||||
musicFragment = musicFragmentFactory.create(this, this, this)
|
musicFragment = musicFragmentFactory.create(this, this, this)
|
||||||
sessionToken = playbackFragment.attach()
|
sessionToken = playbackFragment.attach()
|
||||||
musicFragment.attach()
|
musicFragment.attach()
|
||||||
|
Timber.d("Service Created")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
// TODO: Start command occurring from a foreign service basically implies a detached
|
// TODO: Start command occurring from a foreign service basically implies a detached
|
||||||
// service, we might need more handling here.
|
// service, we might need more handling here.
|
||||||
|
super.onStartCommand(intent, flags, startId)
|
||||||
onHandleForeground(intent)
|
onHandleForeground(intent)
|
||||||
return super.onStartCommand(intent, flags, startId)
|
// If we die we want to not restart, we will immediately try to foreground in and just
|
||||||
|
// fail to start again since the activity will be dead too. This is not the semantically
|
||||||
|
// "correct" flag (normally you want START_STICKY for playback) but we need this to avoid
|
||||||
|
// weird foreground errors.
|
||||||
|
return START_NOT_STICKY
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBind(intent: Intent): IBinder? {
|
override fun onBind(intent: Intent): IBinder? {
|
||||||
|
val binder = super.onBind(intent)
|
||||||
onHandleForeground(intent)
|
onHandleForeground(intent)
|
||||||
return super.onBind(intent)
|
return binder
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onHandleForeground(intent: Intent?) {
|
private fun onHandleForeground(intent: Intent?) {
|
||||||
currentDelayJob?.cancel()
|
val startId = intent?.getIntExtra(INTENT_KEY_START_ID, -1) ?: -1
|
||||||
currentDelayJob =
|
musicFragment.start()
|
||||||
delayScope.launch {
|
playbackFragment.start(startId)
|
||||||
// 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(1000)
|
|
||||||
val startId = intent?.getIntExtra(INTENT_KEY_START_ID, -1) ?: -1
|
|
||||||
musicFragment.start()
|
|
||||||
playbackFragment.start(startId)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onTaskRemoved(rootIntent: Intent?) {
|
override fun onTaskRemoved(rootIntent: Intent?) {
|
||||||
|
@ -98,7 +88,6 @@ class AuxioService :
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
delayScopeJob.cancel()
|
|
||||||
musicFragment.release()
|
musicFragment.release()
|
||||||
playbackFragment.release()
|
playbackFragment.release()
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,6 +92,7 @@ constructor(
|
||||||
|
|
||||||
fun start() {
|
fun start() {
|
||||||
if (musicRepository.indexingState == null) {
|
if (musicRepository.indexingState == null) {
|
||||||
|
L.d("Requesting index")
|
||||||
musicRepository.requestIndex(true)
|
musicRepository.requestIndex(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue