music: share code between music/excluded locations
Co-authored by Claude.
This commit is contained in:
@@ -38,7 +38,7 @@ graph LR
|
||||
## Module Structure
|
||||
|
||||
### 1. File System (`fs` package)
|
||||
- **OpenedLocation** - Represents a location to search for music
|
||||
- **Location.Opened** - Represents a location to search for music
|
||||
- **DeviceFS** - Provides access to device storage
|
||||
- **FileTreeCache** - Caches filesystem structure for faster indexing
|
||||
|
||||
|
||||
@@ -22,12 +22,12 @@ import org.oxycblt.musikr.cache.MutableCache
|
||||
import org.oxycblt.musikr.covers.Cover
|
||||
import org.oxycblt.musikr.covers.MutableCovers
|
||||
import org.oxycblt.musikr.fs.Location
|
||||
import org.oxycblt.musikr.fs.OpenedLocation
|
||||
import org.oxycblt.musikr.fs.Location.Opened
|
||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||
import org.oxycblt.musikr.tag.interpret.Naming
|
||||
import org.oxycblt.musikr.tag.interpret.Separators
|
||||
|
||||
data class Query(val source: List<OpenedLocation>, val exclude: List<Location>)
|
||||
data class Query(val source: List<Location.Opened>, val exclude: List<Location>)
|
||||
|
||||
/** Side-effect laden [Storage] for use during music loading and [MutableLibrary] operation. */
|
||||
data class Storage(
|
||||
|
||||
@@ -25,52 +25,52 @@ import android.provider.DocumentsContract
|
||||
import org.oxycblt.musikr.fs.device.contentResolverSafe
|
||||
import org.oxycblt.musikr.fs.path.DocumentPathFactory
|
||||
|
||||
class Location private constructor(val uri: Uri, val path: Path) {
|
||||
sealed class Location(val uri: Uri, val path: Path) {
|
||||
override fun equals(other: Any?) = other is Location && uri == other.uri
|
||||
|
||||
override fun hashCode() = 31 * uri.hashCode()
|
||||
|
||||
override fun toString(): String = uri.toString()
|
||||
|
||||
fun open(context: Context): OpenedLocation? {
|
||||
if (isUnopened(context, uri)) {
|
||||
try {
|
||||
context.contentResolverSafe.takePersistableUriPermission(
|
||||
uri,
|
||||
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
|
||||
} catch (e: Exception) {
|
||||
// Not fully sure what happens if I'm disallowed to take the permission,
|
||||
// check for both circumstances (error or no-op)
|
||||
return null
|
||||
}
|
||||
|
||||
class Unopened private constructor(uri: Uri, path: Path) : Location(uri, path) {
|
||||
fun open(context: Context): Opened? {
|
||||
if (isUnopened(context, uri)) {
|
||||
return null
|
||||
try {
|
||||
context.contentResolverSafe.takePersistableUriPermission(
|
||||
uri,
|
||||
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
|
||||
} catch (e: Exception) {
|
||||
// Not fully sure what happens if I'm disallowed to take the permission,
|
||||
// check for both circumstances (error or no-op)
|
||||
return null
|
||||
}
|
||||
|
||||
if (isUnopened(context, uri)) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
return Opened(uri, path)
|
||||
}
|
||||
|
||||
private fun isUnopened(context: Context, uri: Uri) =
|
||||
context.contentResolverSafe.persistedUriPermissions.none {
|
||||
it.uri == uri && it.isReadPermission && it.isWritePermission
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun from(context: Context, uri: Uri): Unopened? {
|
||||
if (!DocumentsContract.isTreeUri(uri)) return null
|
||||
val documentPathFactory = DocumentPathFactory.from(context)
|
||||
val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null
|
||||
return Unopened(uri, path)
|
||||
}
|
||||
}
|
||||
return OpenedLocation(uri, path)
|
||||
}
|
||||
|
||||
private fun isUnopened(context: Context, uri: Uri) =
|
||||
context.contentResolverSafe.persistedUriPermissions.none {
|
||||
it.uri == uri && it.isReadPermission && it.isWritePermission
|
||||
}
|
||||
class Opened internal constructor(uri: Uri, path: Path) : Location(uri, path) {
|
||||
override fun equals(other: Any?) =
|
||||
other is Opened && uri == other.uri && path == other.path
|
||||
|
||||
companion object {
|
||||
fun from(context: Context, uri: Uri): Location? {
|
||||
if (!DocumentsContract.isTreeUri(uri)) return null
|
||||
val documentPathFactory = DocumentPathFactory.from(context)
|
||||
val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null
|
||||
return Location(uri, path)
|
||||
}
|
||||
override fun hashCode() = 31 * uri.hashCode() + path.hashCode()
|
||||
}
|
||||
}
|
||||
|
||||
class OpenedLocation internal constructor(val uri: Uri, val path: Path) {
|
||||
override fun equals(other: Any?) =
|
||||
other is OpenedLocation && uri == other.uri && path == other.path
|
||||
|
||||
override fun hashCode() = 31 * uri.hashCode() + path.hashCode()
|
||||
|
||||
override fun toString(): String = uri.toString()
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ private class DeviceFSImpl(
|
||||
null)
|
||||
}
|
||||
|
||||
private fun exploreDirectoryImpl(
|
||||
private suspend fun exploreDirectoryImpl(
|
||||
rootUri: Uri,
|
||||
treeDocumentId: String,
|
||||
relativePath: Path,
|
||||
|
||||
@@ -22,11 +22,11 @@ import android.content.Context
|
||||
import android.database.ContentObserver
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import org.oxycblt.musikr.fs.OpenedLocation
|
||||
import org.oxycblt.musikr.fs.Location
|
||||
|
||||
internal class LocationObserver(
|
||||
private val context: Context,
|
||||
private val location: OpenedLocation,
|
||||
private val location: Location.Opened,
|
||||
private val listener: UpdateTracker.Callback
|
||||
) : ContentObserver(Handler(Looper.getMainLooper())), Runnable {
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
@@ -19,15 +19,15 @@
|
||||
package org.oxycblt.musikr.track
|
||||
|
||||
import android.content.Context
|
||||
import org.oxycblt.musikr.fs.OpenedLocation
|
||||
import org.oxycblt.musikr.fs.Location
|
||||
|
||||
interface UpdateTracker {
|
||||
fun track(locations: List<OpenedLocation>)
|
||||
fun track(locations: List<Location.Opened>)
|
||||
|
||||
fun release()
|
||||
|
||||
interface Callback {
|
||||
fun onUpdate(location: OpenedLocation)
|
||||
fun onUpdate(location: Location.Opened)
|
||||
}
|
||||
|
||||
companion object {
|
||||
@@ -42,7 +42,7 @@ private class UpdateTrackerImpl(
|
||||
) : UpdateTracker {
|
||||
private val observers = mutableListOf<LocationObserver>()
|
||||
|
||||
override fun track(locations: List<OpenedLocation>) {
|
||||
override fun track(locations: List<Location.Opened>) {
|
||||
release()
|
||||
observers.addAll(locations.map { LocationObserver(context, it, callback) })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user