musikr: improve music location creation

This commit is contained in:
Alexander Capehart
2024-12-07 17:19:30 -07:00
parent 2a38d1ae8d
commit c270759dec
3 changed files with 55 additions and 51 deletions
@@ -6,6 +6,7 @@ import android.net.Uri
import android.provider.DocumentsContract
import dagger.hilt.android.qualifiers.ApplicationContext
import org.oxycblt.musikr.fs.path.DocumentPathFactory
import org.oxycblt.musikr.fs.path.VolumeManager
import javax.inject.Inject
class MusicLocation internal constructor(val uri: Uri, val path: Path) {
@@ -13,10 +14,18 @@ class MusicLocation internal constructor(val uri: Uri, val path: Path) {
other is MusicLocation && uri == other.uri && path == other.path
override fun hashCode() = 31 * uri.hashCode() + path.hashCode()
override fun toString() = "src:$uri=$path"
override fun toString(): String {
val volumeId = when (path.volume) {
is Volume.Internal -> VOLUME_INTERNAL
is Volume.External -> path.volume.id
}
return "$uri=${volumeId}:${path.components.unixString}"
}
interface Factory {
fun create(uri: Uri): MusicLocation?
fun new(uri: Uri): MusicLocation?
fun existing(uri: Uri): MusicLocation?
}
}
@@ -24,13 +33,28 @@ class MusicLocationFactoryImpl @Inject constructor(
@ApplicationContext private val context: Context,
private val documentPathFactory: DocumentPathFactory
) : MusicLocation.Factory {
override fun create(uri: Uri): MusicLocation? {
check(DocumentsContract.isTreeUri(uri)) { "URI $uri is not a document tree URI" }
override fun new(uri: Uri): MusicLocation? {
if (!DocumentsContract.isTreeUri(uri)) return null
val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null
context.contentResolverSafe.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
val notPersisted = context.contentResolverSafe.persistedUriPermissions
.none { it.uri == uri && it.isReadPermission && it.isWritePermission }
if (notPersisted) {
context.contentResolverSafe.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
}
return MusicLocation(uri, path)
}
}
override fun existing(uri: Uri): MusicLocation? {
if (!DocumentsContract.isTreeUri(uri)) return null
val notPersisted = context.contentResolverSafe.persistedUriPermissions
.none { it.uri == uri && it.isReadPermission && it.isWritePermission }
if (notPersisted) return null
val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null
return MusicLocation(uri, path)
}
}
private const val VOLUME_INTERNAL = "internal"