musikr: re-add date added support
This commit is contained in:
@@ -43,6 +43,7 @@ data class Directory(
|
|||||||
data class File(
|
data class File(
|
||||||
override val uri: Uri,
|
override val uri: Uri,
|
||||||
override val path: Path,
|
override val path: Path,
|
||||||
|
val addedMs: AddedMs,
|
||||||
val modifiedMs: Long,
|
val modifiedMs: Long,
|
||||||
val mimeType: String,
|
val mimeType: String,
|
||||||
val size: Long,
|
val size: Long,
|
||||||
@@ -52,3 +53,7 @@ data class File(
|
|||||||
sealed interface FSUpdate {
|
sealed interface FSUpdate {
|
||||||
data class LocationChanged(val location: Location.Opened?) : FSUpdate
|
data class LocationChanged(val location: Location.Opened?) : FSUpdate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface AddedMs {
|
||||||
|
suspend fun resolve(): Long?
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ package org.oxycblt.musikr.fs.mediastore
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import android.os.Build
|
||||||
|
import androidx.annotation.RequiresApi
|
||||||
import android.provider.MediaStore as AOSPMediaStore
|
import android.provider.MediaStore as AOSPMediaStore
|
||||||
import androidx.core.database.getStringOrNull
|
import androidx.core.database.getStringOrNull
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
@@ -31,6 +33,7 @@ import kotlinx.coroutines.yield
|
|||||||
import org.oxycblt.musikr.fs.FS
|
import org.oxycblt.musikr.fs.FS
|
||||||
import org.oxycblt.musikr.fs.FSUpdate
|
import org.oxycblt.musikr.fs.FSUpdate
|
||||||
import org.oxycblt.musikr.fs.File
|
import org.oxycblt.musikr.fs.File
|
||||||
|
import org.oxycblt.musikr.fs.AddedMs
|
||||||
import org.oxycblt.musikr.fs.Location
|
import org.oxycblt.musikr.fs.Location
|
||||||
import org.oxycblt.musikr.fs.path.MediaStorePathInterpreter
|
import org.oxycblt.musikr.fs.path.MediaStorePathInterpreter
|
||||||
import org.oxycblt.musikr.fs.path.VolumeManager
|
import org.oxycblt.musikr.fs.path.VolumeManager
|
||||||
@@ -94,6 +97,8 @@ private constructor(
|
|||||||
val mimeTypeIndex =
|
val mimeTypeIndex =
|
||||||
cursor.getColumnIndexOrThrow(AOSPMediaStore.Audio.AudioColumns.MIME_TYPE)
|
cursor.getColumnIndexOrThrow(AOSPMediaStore.Audio.AudioColumns.MIME_TYPE)
|
||||||
val sizeIndex = cursor.getColumnIndexOrThrow(AOSPMediaStore.Audio.AudioColumns.SIZE)
|
val sizeIndex = cursor.getColumnIndexOrThrow(AOSPMediaStore.Audio.AudioColumns.SIZE)
|
||||||
|
val dateAddedIndex =
|
||||||
|
cursor.getColumnIndexOrThrow(AOSPMediaStore.Audio.AudioColumns.DATE_ADDED)
|
||||||
val dateModifiedIndex =
|
val dateModifiedIndex =
|
||||||
cursor.getColumnIndexOrThrow(AOSPMediaStore.Audio.AudioColumns.DATE_MODIFIED)
|
cursor.getColumnIndexOrThrow(AOSPMediaStore.Audio.AudioColumns.DATE_MODIFIED)
|
||||||
|
|
||||||
@@ -106,6 +111,8 @@ private constructor(
|
|||||||
AOSPMediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id.toString())
|
AOSPMediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id.toString())
|
||||||
val mimeType = cursor.getStringOrNull(mimeTypeIndex) ?: "audio/*"
|
val mimeType = cursor.getStringOrNull(mimeTypeIndex) ?: "audio/*"
|
||||||
val size = cursor.getLong(sizeIndex)
|
val size = cursor.getLong(sizeIndex)
|
||||||
|
val dateAdded =
|
||||||
|
cursor.getLong(dateAddedIndex) * 1000 // Convert to milliseconds
|
||||||
val dateModified =
|
val dateModified =
|
||||||
cursor.getLong(dateModifiedIndex) * 1000 // Convert to milliseconds
|
cursor.getLong(dateModifiedIndex) * 1000 // Convert to milliseconds
|
||||||
|
|
||||||
@@ -117,6 +124,7 @@ private constructor(
|
|||||||
modifiedMs = dateModified,
|
modifiedMs = dateModified,
|
||||||
mimeType = mimeType,
|
mimeType = mimeType,
|
||||||
size = size,
|
size = size,
|
||||||
|
addedMs = ForwardDateAdded(dateAdded),
|
||||||
parent = null)
|
parent = null)
|
||||||
|
|
||||||
allFiles.add(deviceFile)
|
allFiles.add(deviceFile)
|
||||||
@@ -145,6 +153,10 @@ private constructor(
|
|||||||
EXCLUDE
|
EXCLUDE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class ForwardDateAdded(val dateAdded: Long) : AddedMs {
|
||||||
|
override suspend fun resolve() = dateAdded
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun from(context: Context, query: Query) =
|
fun from(context: Context, query: Query) =
|
||||||
MediaStore(
|
MediaStore(
|
||||||
@@ -160,6 +172,7 @@ private constructor(
|
|||||||
private val BASE_PROJECTION =
|
private val BASE_PROJECTION =
|
||||||
arrayOf(
|
arrayOf(
|
||||||
AOSPMediaStore.Audio.AudioColumns._ID,
|
AOSPMediaStore.Audio.AudioColumns._ID,
|
||||||
|
AOSPMediaStore.Audio.AudioColumns.DATE_ADDED,
|
||||||
AOSPMediaStore.Audio.AudioColumns.DATE_MODIFIED,
|
AOSPMediaStore.Audio.AudioColumns.DATE_MODIFIED,
|
||||||
AOSPMediaStore.Audio.AudioColumns.SIZE,
|
AOSPMediaStore.Audio.AudioColumns.SIZE,
|
||||||
AOSPMediaStore.Audio.AudioColumns.MIME_TYPE)
|
AOSPMediaStore.Audio.AudioColumns.MIME_TYPE)
|
||||||
|
|||||||
@@ -21,7 +21,10 @@ package org.oxycblt.musikr.fs.saf
|
|||||||
import android.content.ContentResolver
|
import android.content.ContentResolver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import android.os.Build
|
||||||
import android.provider.DocumentsContract
|
import android.provider.DocumentsContract
|
||||||
|
import android.util.Log
|
||||||
|
import androidx.annotation.RequiresApi
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
import kotlinx.coroutines.Deferred
|
import kotlinx.coroutines.Deferred
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
@@ -33,13 +36,16 @@ import kotlinx.coroutines.flow.emitAll
|
|||||||
import kotlinx.coroutines.flow.flatMapMerge
|
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.AddedMs
|
||||||
import org.oxycblt.musikr.fs.Directory
|
import org.oxycblt.musikr.fs.Directory
|
||||||
import org.oxycblt.musikr.fs.FS
|
import org.oxycblt.musikr.fs.FS
|
||||||
import org.oxycblt.musikr.fs.FSUpdate
|
import org.oxycblt.musikr.fs.FSUpdate
|
||||||
import org.oxycblt.musikr.fs.File
|
import org.oxycblt.musikr.fs.File
|
||||||
import org.oxycblt.musikr.fs.Location
|
import org.oxycblt.musikr.fs.Location
|
||||||
import org.oxycblt.musikr.fs.Path
|
import org.oxycblt.musikr.fs.Path
|
||||||
|
import org.oxycblt.musikr.fs.mediastore.MediaStore
|
||||||
import org.oxycblt.musikr.fs.track.LocationObserver
|
import org.oxycblt.musikr.fs.track.LocationObserver
|
||||||
|
import android.provider.MediaStore as AOSPMediaStore
|
||||||
|
|
||||||
@OptIn(ExperimentalCoroutinesApi::class)
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
class SAF
|
class SAF
|
||||||
@@ -127,7 +133,12 @@ private constructor(
|
|||||||
path = newPath,
|
path = newPath,
|
||||||
size = size,
|
size = size,
|
||||||
modifiedMs = lastModified,
|
modifiedMs = lastModified,
|
||||||
parent = directoryDeferred)
|
parent = directoryDeferred,
|
||||||
|
addedMs = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
|
JoinAddedMs(context, childUri)
|
||||||
|
} else {
|
||||||
|
NullAddedMs
|
||||||
|
})
|
||||||
children.add(file)
|
children.add(file)
|
||||||
emit(file)
|
emit(file)
|
||||||
}
|
}
|
||||||
@@ -143,6 +154,25 @@ private constructor(
|
|||||||
val withHidden: Boolean
|
val withHidden: Boolean
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@RequiresApi(Build.VERSION_CODES.Q)
|
||||||
|
private class JoinAddedMs(private val context: Context, private val uri: Uri) : AddedMs {
|
||||||
|
override suspend fun resolve(): Long? {
|
||||||
|
val mediaUri = AOSPMediaStore.getMediaUri(context, uri) ?: return null
|
||||||
|
return context.contentResolverSafe.useQuery(mediaUri, arrayOf(AOSPMediaStore.Files.FileColumns.DATE_ADDED)) {
|
||||||
|
if (it.moveToFirst()) {
|
||||||
|
val dateAddedIndex = it.getColumnIndexOrThrow(AOSPMediaStore.Files.FileColumns.DATE_ADDED)
|
||||||
|
it.getLong(dateAddedIndex) * 1000
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private object NullAddedMs : AddedMs {
|
||||||
|
override suspend fun resolve(): Long? = null
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun from(context: Context, query: Query) = SAF(context, context.contentResolverSafe, query)
|
fun from(context: Context, query: Query) = SAF(context, context.contentResolverSafe, query)
|
||||||
|
|
||||||
|
|||||||
@@ -50,7 +50,6 @@ internal interface ExploreStep {
|
|||||||
|
|
||||||
private class ExploreStepImpl(private val fs: FS, private val storage: Storage) : ExploreStep {
|
private class ExploreStepImpl(private val fs: FS, private val storage: Storage) : ExploreStep {
|
||||||
override fun explore(): Flow<Explored> {
|
override fun explore(): Flow<Explored> {
|
||||||
val addingMs = System.currentTimeMillis()
|
|
||||||
return merge(
|
return merge(
|
||||||
fs.explore()
|
fs.explore()
|
||||||
.filter { it.mimeType.startsWith("audio/") || it.mimeType == M3U.MIME_TYPE }
|
.filter { it.mimeType.startsWith("audio/") || it.mimeType == M3U.MIME_TYPE }
|
||||||
@@ -58,8 +57,8 @@ private class ExploreStepImpl(private val fs: FS, private val storage: Storage)
|
|||||||
when (val cacheResult = storage.cache.read(file)) {
|
when (val cacheResult = storage.cache.read(file)) {
|
||||||
is CacheResult.Hit -> NeedsCover(cacheResult.song)
|
is CacheResult.Hit -> NeedsCover(cacheResult.song)
|
||||||
is CacheResult.Stale ->
|
is CacheResult.Stale ->
|
||||||
Finalized(NewSong(cacheResult.file, cacheResult.addedMs))
|
Finalized(NewSong(cacheResult.file))
|
||||||
is CacheResult.Miss -> Finalized(NewSong(cacheResult.file, addingMs))
|
is CacheResult.Miss -> Finalized(NewSong(cacheResult.file))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.flowOn(Dispatchers.IO)
|
.flowOn(Dispatchers.IO)
|
||||||
@@ -74,7 +73,7 @@ private class ExploreStepImpl(private val fs: FS, private val storage: Storage)
|
|||||||
Finalized(it.cachedSong.toRawSong(coverResult.cover))
|
Finalized(it.cachedSong.toRawSong(coverResult.cover))
|
||||||
null -> Finalized(it.cachedSong.toRawSong(null))
|
null -> Finalized(it.cachedSong.toRawSong(null))
|
||||||
else ->
|
else ->
|
||||||
Finalized(NewSong(it.cachedSong.file, it.cachedSong.addedMs))
|
Finalized(NewSong(it.cachedSong.file))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
package org.oxycblt.musikr.pipeline
|
package org.oxycblt.musikr.pipeline
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.util.Log
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.channels.Channel
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
@@ -56,6 +57,7 @@ private class ExtractStepImpl(
|
|||||||
private val covers: MutableCovers<out Cover>
|
private val covers: MutableCovers<out Cover>
|
||||||
) : ExtractStep {
|
) : ExtractStep {
|
||||||
override fun extract(nodes: Flow<Explored>): Flow<Extracted> {
|
override fun extract(nodes: Flow<Explored>): Flow<Extracted> {
|
||||||
|
val addingMs = System.currentTimeMillis()
|
||||||
val exclude = mutableListOf<CachedSong>()
|
val exclude = mutableListOf<CachedSong>()
|
||||||
return nodes
|
return nodes
|
||||||
// Cover art is huge, so we have to kneecap the concurrency here to avoid excessive
|
// Cover art is huge, so we have to kneecap the concurrency here to avoid excessive
|
||||||
@@ -88,7 +90,14 @@ private class ExtractStepImpl(
|
|||||||
it.metadata.properties,
|
it.metadata.properties,
|
||||||
tags,
|
tags,
|
||||||
cover,
|
cover,
|
||||||
it.newSong.addedMs))
|
// The thing about date added is that it's resolution can actually
|
||||||
|
// be expensive in some modes (ex. saf backend), so we resolve this by
|
||||||
|
// moving date added extraction as an extraction operation rather
|
||||||
|
// than doing the redundant work during exploration (well, kind of,
|
||||||
|
// MediaStore's date added query is basically free, it's only saf
|
||||||
|
// that has it's slow hacky workaround that we must accommodate
|
||||||
|
// here.)
|
||||||
|
it.newSong.file.addedMs.resolve() ?: addingMs))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ internal sealed interface Explored : PipelineItem {
|
|||||||
sealed interface Known : Explored, Complete
|
sealed interface Known : Explored, Complete
|
||||||
}
|
}
|
||||||
|
|
||||||
internal data class NewSong(val file: File, val addedMs: Long) : Explored.New
|
internal data class NewSong(val file: File) : Explored.New
|
||||||
|
|
||||||
internal sealed interface Extracted : PipelineItem {
|
internal sealed interface Extracted : PipelineItem {
|
||||||
sealed interface Valid : Complete, Extracted
|
sealed interface Valid : Complete, Extracted
|
||||||
|
|||||||
Reference in New Issue
Block a user