From 8aac87e02cdf8d3c0bdc83958c299dc56f6d8b28 Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Sat, 10 Sep 2022 10:50:44 -0600 Subject: [PATCH] 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. --- .../java/org/oxycblt/auxio/music/MusicStore.kt | 15 +++++++++++++-- .../oxycblt/auxio/music/extractor/CacheLayer.kt | 7 +++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/oxycblt/auxio/music/MusicStore.kt b/app/src/main/java/org/oxycblt/auxio/music/MusicStore.kt index 86dad56f9..fa2062997 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/MusicStore.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/MusicStore.kt @@ -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 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 } } } diff --git a/app/src/main/java/org/oxycblt/auxio/music/extractor/CacheLayer.kt b/app/src/main/java/org/oxycblt/auxio/music/extractor/CacheLayer.kt index 5e4efde73..25bf864e0 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/extractor/CacheLayer.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/extractor/CacheLayer.kt @@ -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) { // 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 }