music: fix crash below android 8 [#95]

Fix a crash that stemmed from the use of the API 30+ database field
CD_TRACK_NUMBER.

I switched to CD_TRACK_NUMBER in 1e39ceb to fix [#88], however I did
not realize that CD_TRACK_NUMBER was actually only supported on API
30 onwards. This led to a load failure on versions before API 30.
This commit is contained in:
OxygenCobalt 2022-03-07 07:18:15 -07:00
parent b5b8767f46
commit d3d6d18d5d
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
2 changed files with 10 additions and 12 deletions

View file

@ -112,7 +112,6 @@ class MusicLoader {
) )
} }
@Suppress("InlinedApi")
private fun loadSongs(context: Context): List<Song> { private fun loadSongs(context: Context): List<Song> {
var songs = mutableListOf<Song>() var songs = mutableListOf<Song>()
val blacklistDatabase = ExcludedDatabase.getInstance(context) val blacklistDatabase = ExcludedDatabase.getInstance(context)
@ -137,7 +136,7 @@ class MusicLoader {
MediaStore.Audio.AudioColumns._ID, MediaStore.Audio.AudioColumns._ID,
MediaStore.Audio.AudioColumns.TITLE, MediaStore.Audio.AudioColumns.TITLE,
MediaStore.Audio.AudioColumns.DISPLAY_NAME, MediaStore.Audio.AudioColumns.DISPLAY_NAME,
MediaStore.Audio.AudioColumns.CD_TRACK_NUMBER, MediaStore.Audio.AudioColumns.TRACK,
MediaStore.Audio.AudioColumns.DURATION, MediaStore.Audio.AudioColumns.DURATION,
MediaStore.Audio.AudioColumns.YEAR, MediaStore.Audio.AudioColumns.YEAR,
MediaStore.Audio.AudioColumns.ALBUM, MediaStore.Audio.AudioColumns.ALBUM,
@ -150,7 +149,7 @@ class MusicLoader {
val idIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns._ID) val idIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns._ID)
val titleIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.TITLE) val titleIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.TITLE)
val fileIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.DISPLAY_NAME) val fileIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.DISPLAY_NAME)
val trackIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.CD_TRACK_NUMBER) val trackIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.TRACK)
val durationIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.DURATION) val durationIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.DURATION)
val yearIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.YEAR) val yearIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.YEAR)
val albumIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.ALBUM) val albumIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.ALBUM)
@ -163,12 +162,12 @@ class MusicLoader {
val title = cursor.getString(titleIndex) val title = cursor.getString(titleIndex)
val fileName = cursor.getString(fileIndex) val fileName = cursor.getString(fileIndex)
// CD_TRACK_NUMBER formats tracks as NN/TT or NN where N is the track number and // The TRACK field is for some reason formatted as DTTT, where D is the disk
// T Is the total. Parse out the NN and ignore the rest. This has to be a highly // and T is the track. This is dumb and insane and forces me to mangle track
// redundant process, as there seems to be a weird amount of edge-cases. // numbers above 1000 but there is nothing we can do that won't break the app
val track = cursor.getStringOrNull(trackIndex)?.run { // below API 30.
split("/").getOrNull(0)?.toIntOrNull() // TODO: Disk number support?
} val track = cursor.getIntOrNull(trackIndex)?.mod(1000)
val duration = cursor.getLong(durationIndex) val duration = cursor.getLong(durationIndex)
val year = cursor.getIntOrNull(yearIndex) val year = cursor.getIntOrNull(yearIndex)

View file

@ -596,11 +596,10 @@ class PlaybackStateManager private constructor() {
* Do a sanity check to make sure that the index lines up with the current song. * Do a sanity check to make sure that the index lines up with the current song.
*/ */
private fun doIndexSanityCheck() { private fun doIndexSanityCheck() {
// Note: Be careful with how we handle the queue since a possible index desync // Be careful with how we handle the queue since a possible index de-sync
// could easily result in an OOB issue. // could easily result in an OOB crash.
if (mSong != null && mSong != mQueue.getOrNull(mIndex)) { if (mSong != null && mSong != mQueue.getOrNull(mIndex)) {
val correctedIndex = mQueue.wobblyIndexOfFirst(mIndex, mSong) val correctedIndex = mQueue.wobblyIndexOfFirst(mIndex, mSong)
if (correctedIndex > -1) { if (correctedIndex > -1) {
logD("Correcting malformed index to $correctedIndex") logD("Correcting malformed index to $correctedIndex")
mIndex = correctedIndex mIndex = correctedIndex