music: take size into account when opening by uri

Also take the size of the music file into account when it is being
played.

This way, we could remove some situations where two files with the
same name get confused with eachother when opened.
This commit is contained in:
Alexander Capehart 2022-09-10 10:50:44 -06:00
parent e15eb4547d
commit 8aac87e02c
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
2 changed files with 20 additions and 2 deletions

View file

@ -97,6 +97,10 @@ class MusicStore private constructor() {
}
}
/**
* Find a music [T] by its [uid]. If the music does not exist, or if the music is
* not [T], null will be returned.
*/
@Suppress("UNCHECKED_CAST")
fun <T : Music> find(uid: Music.UID): T? = uidMap[uid] as? T
@ -117,14 +121,21 @@ class MusicStore private constructor() {
/** Find a song for a [uri]. */
fun findSongForUri(context: Context, uri: Uri) =
context.contentResolverSafe.useQuery(uri, arrayOf(OpenableColumns.DISPLAY_NAME)) {
context.contentResolverSafe.useQuery(uri, arrayOf(OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE)) {
cursor ->
cursor.moveToFirst()
// We are weirdly limited to DISPLAY_NAME and SIZE when trying to locate a
// song. Do what we can to hopefully find the song the user wanted to open.
val displayName =
cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME))
songs.find { it.path.name == displayName }
val size = cursor.getLong(
cursor.getColumnIndexOrThrow(OpenableColumns.SIZE)
)
songs.find { it.path.name == displayName && it.size == size }
}
}

View file

@ -25,9 +25,16 @@ class CacheLayer {
// STUB: Add cache database
}
/**
* Write a list of newly-indexed raw songs to the database.
*/
fun finalize(rawSongs: List<Song.Raw>) {
// STUB: Add cache database
}
/**
* Maybe copy a cached raw song into this instance, assuming that it has not changed
* since it was last saved.
*/
fun maybePopulateCachedRaw(raw: Song.Raw) = false
}