playback: fix crash with state restore [#89]

Fix an esoteric crash with queue synchronization during the playback
restore process.

Auxio will attempt to re-synchronize the queue index whenever it is
desynchronized, however during the check for if it's desynchronized,
Auxio would do a direct index of the queue, which could result in a
crash in situations where the desynchronized index is outside of the
queue bounds.

Fix this by replacing that unprotected access with a protected access,
which not only fixes the crash but also still correctly detects
desynchronization in that case.

Resolves #89.
This commit is contained in:
OxygenCobalt
2022-03-06 19:25:48 -07:00
parent 1b791074ec
commit b5b8767f46
3 changed files with 7 additions and 3 deletions
@@ -40,6 +40,8 @@ import org.oxycblt.auxio.util.logE
*
* All access should be done with [PlaybackStateManager.getInstance].
* @author OxygenCobalt
*
* TODO: Rework this to possibly handle gapless playback and more refined queue management.
*/
class PlaybackStateManager private constructor() {
// Playback
@@ -594,7 +596,9 @@ class PlaybackStateManager private constructor() {
* Do a sanity check to make sure that the index lines up with the current song.
*/
private fun doIndexSanityCheck() {
if (mSong != null && mSong != mQueue[mIndex]) {
// Note: Be careful with how we handle the queue since a possible index desync
// could easily result in an OOB issue.
if (mSong != null && mSong != mQueue.getOrNull(mIndex)) {
val correctedIndex = mQueue.wobblyIndexOfFirst(mIndex, mSong)
if (correctedIndex > -1) {