musikr: implement mediastore backend
This commit is contained in:
@@ -2,6 +2,9 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
|
<!-- Android 13 uses READ_MEDIA_AUDIO instead of READ_EXTERNAL_STORAGE -->
|
||||||
|
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
|
||||||
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ import org.oxycblt.musikr.Playlist
|
|||||||
import org.oxycblt.musikr.Song
|
import org.oxycblt.musikr.Song
|
||||||
import org.oxycblt.musikr.Storage
|
import org.oxycblt.musikr.Storage
|
||||||
import org.oxycblt.musikr.cache.MutableCache
|
import org.oxycblt.musikr.cache.MutableCache
|
||||||
import org.oxycblt.musikr.fs.saf.SAF
|
import org.oxycblt.musikr.fs.mediastore.MediaStoreFS
|
||||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||||
import org.oxycblt.musikr.tag.interpret.Naming
|
import org.oxycblt.musikr.tag.interpret.Naming
|
||||||
import org.oxycblt.musikr.tag.interpret.Separators
|
import org.oxycblt.musikr.tag.interpret.Separators
|
||||||
@@ -394,7 +394,8 @@ constructor(
|
|||||||
val newRevision = currentRevision?.takeIf { withCache } ?: UUID.randomUUID()
|
val newRevision = currentRevision?.takeIf { withCache } ?: UUID.randomUUID()
|
||||||
val cache = if (withCache) cache else WriteOnlyMutableCache(cache)
|
val cache = if (withCache) cache else WriteOnlyMutableCache(cache)
|
||||||
val covers = settingCovers.mutate(context, newRevision)
|
val covers = settingCovers.mutate(context, newRevision)
|
||||||
val fs = SAF.from(context, SAF.Query(locations, excludedLocations, withHidden))
|
// val fs = SAF.from(context, SAF.Query(locations, excludedLocations, withHidden))
|
||||||
|
val fs = MediaStoreFS.from(context)
|
||||||
val storage = Storage(cache, covers, storedPlaylists)
|
val storage = Storage(cache, covers, storedPlaylists)
|
||||||
val interpretation = Interpretation(nameFactory, separators)
|
val interpretation = Interpretation(nameFactory, separators)
|
||||||
val config = Config(fs, storage, interpretation)
|
val config = Config(fs, storage, interpretation)
|
||||||
|
|||||||
@@ -27,15 +27,15 @@ interface FS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sealed interface DeviceFSEntry {
|
sealed interface DeviceFSEntry {
|
||||||
val uri: Uri
|
val uri: Uri?
|
||||||
val path: Path
|
val path: Path
|
||||||
}
|
}
|
||||||
|
|
||||||
data class DeviceDirectory(
|
data class DeviceDirectory(
|
||||||
override val uri: Uri,
|
override val uri: Uri?,
|
||||||
override val path: Path,
|
override val path: Path,
|
||||||
val parent: Deferred<DeviceDirectory>?,
|
val parent: Deferred<DeviceDirectory>?,
|
||||||
var children: List<DeviceFSEntry>
|
var children: List<DeviceFile>
|
||||||
) : DeviceFSEntry
|
) : DeviceFSEntry
|
||||||
|
|
||||||
data class DeviceFile(
|
data class DeviceFile(
|
||||||
|
|||||||
@@ -154,7 +154,7 @@ value class Components private constructor(val components: List<String>) {
|
|||||||
|
|
||||||
fun containing(other: Components) = Components(other.components.drop(components.size))
|
fun containing(other: Components) = Components(other.components.drop(components.size))
|
||||||
|
|
||||||
internal companion object {
|
companion object {
|
||||||
/**
|
/**
|
||||||
* Parses a path string into a [Components] instance by the unix path separator (/).
|
* Parses a path string into a [Components] instance by the unix path separator (/).
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -0,0 +1,208 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2025 Auxio Project
|
||||||
|
* MediaStore.kt is part of Auxio.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.oxycblt.musikr.fs.mediastore
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
|
import android.provider.MediaStore
|
||||||
|
import androidx.core.database.getStringOrNull
|
||||||
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.flow
|
||||||
|
import kotlinx.coroutines.yield
|
||||||
|
import org.oxycblt.musikr.fs.DeviceDirectory
|
||||||
|
import org.oxycblt.musikr.fs.DeviceFile
|
||||||
|
import org.oxycblt.musikr.fs.FS
|
||||||
|
import org.oxycblt.musikr.fs.Path
|
||||||
|
import org.oxycblt.musikr.fs.path.MediaStorePathInterpreter
|
||||||
|
import org.oxycblt.musikr.fs.path.VolumeManager
|
||||||
|
import org.oxycblt.musikr.fs.saf.contentResolverSafe
|
||||||
|
import org.oxycblt.musikr.fs.saf.useQuery
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MediaStore implementation of [FS] that queries the Android MediaStore database for audio files
|
||||||
|
* and yields them as [DeviceFile] instances.
|
||||||
|
*/
|
||||||
|
class MediaStoreFS
|
||||||
|
private constructor(
|
||||||
|
private val context: Context,
|
||||||
|
private val volumeManager: VolumeManager,
|
||||||
|
private val excludeNonMusic: Boolean = true
|
||||||
|
) : FS {
|
||||||
|
private val pathInterpreterFactory = MediaStorePathInterpreter.Factory.from(volumeManager)
|
||||||
|
|
||||||
|
private fun buildDirectoryHierarchy(files: List<DeviceFile>) {
|
||||||
|
// Map to store directories by their path
|
||||||
|
val directoryMap = mutableMapOf<Path, DeviceDirectory>()
|
||||||
|
|
||||||
|
// Group files by their parent directory path
|
||||||
|
val filesByParent = files.groupBy { it.path.directory }
|
||||||
|
|
||||||
|
// First pass: Create all directories and complete file parent deferreds
|
||||||
|
for ((dirPath, dirFiles) in filesByParent) {
|
||||||
|
val directory =
|
||||||
|
directoryMap.getOrPut(dirPath) {
|
||||||
|
// Create parent deferred for this directory
|
||||||
|
val parentDeferred =
|
||||||
|
if (dirPath.components.components.isEmpty()) {
|
||||||
|
// Root directory has no parent
|
||||||
|
null
|
||||||
|
} else {
|
||||||
|
CompletableDeferred<DeviceDirectory>()
|
||||||
|
}
|
||||||
|
|
||||||
|
DeviceDirectory(
|
||||||
|
uri = null, // Directories don't have MediaStore URIs
|
||||||
|
path = dirPath,
|
||||||
|
parent = parentDeferred,
|
||||||
|
children = dirFiles)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update children if directory already existed
|
||||||
|
if (directory.children.isEmpty()) {
|
||||||
|
directory.children = dirFiles
|
||||||
|
}
|
||||||
|
|
||||||
|
// Complete parent deferreds for all files in this directory
|
||||||
|
dirFiles.forEach { file ->
|
||||||
|
(file.parent as CompletableDeferred<DeviceDirectory>).complete(directory)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Second pass: Create any missing parent directories
|
||||||
|
val directories = directoryMap.values.toList()
|
||||||
|
for (directory in directories) {
|
||||||
|
var currentPath = directory.path
|
||||||
|
while (currentPath.components.components.isNotEmpty()) {
|
||||||
|
val parentPath = currentPath.directory
|
||||||
|
if (!directoryMap.containsKey(parentPath)) {
|
||||||
|
val grandParentDeferred =
|
||||||
|
if (parentPath.components.components.isEmpty()) {
|
||||||
|
null
|
||||||
|
} else {
|
||||||
|
CompletableDeferred<DeviceDirectory>()
|
||||||
|
}
|
||||||
|
|
||||||
|
directoryMap[parentPath] =
|
||||||
|
DeviceDirectory(
|
||||||
|
uri = null,
|
||||||
|
path = parentPath,
|
||||||
|
parent = grandParentDeferred,
|
||||||
|
children = emptyList())
|
||||||
|
}
|
||||||
|
currentPath = parentPath
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Third pass: Complete all directory parent deferreds
|
||||||
|
for (directory in directoryMap.values) {
|
||||||
|
val parentDeferred = directory.parent as? CompletableDeferred<DeviceDirectory>
|
||||||
|
if (parentDeferred != null && !parentDeferred.isCompleted) {
|
||||||
|
val parentPath = directory.path.directory
|
||||||
|
val parentDirectory = directoryMap[parentPath]
|
||||||
|
if (parentDirectory != null) {
|
||||||
|
parentDeferred.complete(parentDirectory)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
|
override fun explore(): Flow<DeviceFile> = flow {
|
||||||
|
val projection = BASE_PROJECTION + pathInterpreterFactory.projection
|
||||||
|
var selector = BASE_SELECTOR
|
||||||
|
val args = mutableListOf<String>()
|
||||||
|
|
||||||
|
// Filter out audio that is not music, if enabled
|
||||||
|
if (excludeNonMusic) {
|
||||||
|
selector += " AND ${MediaStore.Audio.AudioColumns.IS_MUSIC}=1"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collect all files and track unique directories
|
||||||
|
val allFiles = mutableListOf<DeviceFile>()
|
||||||
|
|
||||||
|
context.contentResolverSafe.useQuery(
|
||||||
|
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||||
|
projection,
|
||||||
|
selector,
|
||||||
|
args.toTypedArray()) { cursor ->
|
||||||
|
val pathInterpreter = pathInterpreterFactory.wrap(cursor)
|
||||||
|
val idIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns._ID)
|
||||||
|
val mimeTypeIndex =
|
||||||
|
cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.MIME_TYPE)
|
||||||
|
val sizeIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.SIZE)
|
||||||
|
val dateModifiedIndex =
|
||||||
|
cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.DATE_MODIFIED)
|
||||||
|
|
||||||
|
while (cursor.moveToNext()) {
|
||||||
|
val path = pathInterpreter.extract() ?: continue
|
||||||
|
|
||||||
|
val id = cursor.getLong(idIndex)
|
||||||
|
val uri =
|
||||||
|
Uri.withAppendedPath(
|
||||||
|
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id.toString())
|
||||||
|
val mimeType = cursor.getStringOrNull(mimeTypeIndex) ?: "audio/*"
|
||||||
|
val size = cursor.getLong(sizeIndex)
|
||||||
|
val dateModified =
|
||||||
|
cursor.getLong(dateModifiedIndex) * 1000 // Convert to milliseconds
|
||||||
|
|
||||||
|
// Create file with empty deferred parent
|
||||||
|
val parentDeferred = CompletableDeferred<DeviceDirectory>()
|
||||||
|
val deviceFile =
|
||||||
|
DeviceFile(
|
||||||
|
uri = uri,
|
||||||
|
path = path,
|
||||||
|
modifiedMs = dateModified,
|
||||||
|
mimeType = mimeType,
|
||||||
|
size = size,
|
||||||
|
parent = parentDeferred)
|
||||||
|
|
||||||
|
allFiles.add(deviceFile)
|
||||||
|
emit(deviceFile)
|
||||||
|
yield()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build directory hierarchy
|
||||||
|
buildDirectoryHierarchy(allFiles)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun from(context: Context) =
|
||||||
|
MediaStoreFS(
|
||||||
|
context = context,
|
||||||
|
volumeManager = VolumeManager.from(context),
|
||||||
|
excludeNonMusic = true)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The base selector that works across all versions of android. Excludes files with zero
|
||||||
|
* size.
|
||||||
|
*/
|
||||||
|
private const val BASE_SELECTOR = "NOT ${MediaStore.Audio.Media.SIZE}=0"
|
||||||
|
|
||||||
|
/** The base projection that works across all versions of android. */
|
||||||
|
private val BASE_PROJECTION =
|
||||||
|
arrayOf(
|
||||||
|
MediaStore.Audio.AudioColumns._ID,
|
||||||
|
MediaStore.Audio.AudioColumns.DATE_MODIFIED,
|
||||||
|
MediaStore.Audio.AudioColumns.SIZE,
|
||||||
|
MediaStore.Audio.AudioColumns.MIME_TYPE)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,7 +32,6 @@ import kotlinx.coroutines.flow.flatMapMerge
|
|||||||
import kotlinx.coroutines.flow.flattenMerge
|
import kotlinx.coroutines.flow.flattenMerge
|
||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
import org.oxycblt.musikr.fs.DeviceDirectory
|
import org.oxycblt.musikr.fs.DeviceDirectory
|
||||||
import org.oxycblt.musikr.fs.DeviceFSEntry
|
|
||||||
import org.oxycblt.musikr.fs.DeviceFile
|
import org.oxycblt.musikr.fs.DeviceFile
|
||||||
import org.oxycblt.musikr.fs.FS
|
import org.oxycblt.musikr.fs.FS
|
||||||
import org.oxycblt.musikr.fs.Location
|
import org.oxycblt.musikr.fs.Location
|
||||||
@@ -62,7 +61,7 @@ private constructor(private val contentResolver: ContentResolver, private val qu
|
|||||||
val uri = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, treeDocumentId)
|
val uri = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, treeDocumentId)
|
||||||
val directoryDeferred = CompletableDeferred<DeviceDirectory>()
|
val directoryDeferred = CompletableDeferred<DeviceDirectory>()
|
||||||
val recursive = mutableListOf<Flow<DeviceFile>>()
|
val recursive = mutableListOf<Flow<DeviceFile>>()
|
||||||
val children = mutableListOf<DeviceFSEntry>()
|
val children = mutableListOf<DeviceFile>()
|
||||||
contentResolver.useQuery(uri, PROJECTION) { cursor ->
|
contentResolver.useQuery(uri, PROJECTION) { cursor ->
|
||||||
val childUriIndex =
|
val childUriIndex =
|
||||||
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_DOCUMENT_ID)
|
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_DOCUMENT_ID)
|
||||||
|
|||||||
Reference in New Issue
Block a user