From e553744c8e7f18d05a27d7c0cd8bdb66187611a4 Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Wed, 20 Dec 2023 11:59:48 -0700 Subject: [PATCH] music: rename import to external Apparently hilt doesn't like a module with the name import, probably because of a keyword conflict. --- .../org/oxycblt/auxio/music/MusicViewModel.kt | 6 +- .../ExternalModule.kt} | 6 +- .../auxio/music/{import => external}/M3U.kt | 11 +-- .../{import => external}/PlaylistImporter.kt | 33 ++++++--- .../auxio/music/fs/ContentPathResolver.kt | 72 +++++++++++-------- .../org/oxycblt/auxio/music/fs/FsModule.kt | 3 +- 6 files changed, 81 insertions(+), 50 deletions(-) rename app/src/main/java/org/oxycblt/auxio/music/{import/ImportModule.kt => external/ExternalModule.kt} (90%) rename app/src/main/java/org/oxycblt/auxio/music/{import => external}/M3U.kt (93%) rename app/src/main/java/org/oxycblt/auxio/music/{import => external}/PlaylistImporter.kt (52%) diff --git a/app/src/main/java/org/oxycblt/auxio/music/MusicViewModel.kt b/app/src/main/java/org/oxycblt/auxio/music/MusicViewModel.kt index 5d7568c71..3b41b445c 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/MusicViewModel.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/MusicViewModel.kt @@ -28,7 +28,7 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch import org.oxycblt.auxio.list.ListSettings -import org.oxycblt.auxio.music.import.PlaylistImporter +import org.oxycblt.auxio.music.external.PlaylistImporter import org.oxycblt.auxio.util.Event import org.oxycblt.auxio.util.MutableEvent import org.oxycblt.auxio.util.logD @@ -66,7 +66,8 @@ constructor( private val _importError = MutableEvent() /** Flag for when playlist importing failed. Consume this and show an error if active. */ - val importError: Event get() = _importError + val importError: Event + get() = _importError init { musicRepository.addUpdateListener(this) @@ -125,6 +126,7 @@ constructor( /** * Import a playlist from a file [Uri]. Errors pushed to [importError]. + * * @param uri The [Uri] of the file to import. * @see PlaylistImporter */ diff --git a/app/src/main/java/org/oxycblt/auxio/music/import/ImportModule.kt b/app/src/main/java/org/oxycblt/auxio/music/external/ExternalModule.kt similarity index 90% rename from app/src/main/java/org/oxycblt/auxio/music/import/ImportModule.kt rename to app/src/main/java/org/oxycblt/auxio/music/external/ExternalModule.kt index 4473ed9eb..a1824fadc 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/import/ImportModule.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/external/ExternalModule.kt @@ -1,6 +1,6 @@ /* * Copyright (c) 2023 Auxio Project - * ForeignModule.kt is part of Auxio. + * ExternalModule.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 @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -package org.oxycblt.auxio.music.import +package org.oxycblt.auxio.music.external import dagger.Binds import dagger.Module @@ -25,7 +25,7 @@ import dagger.hilt.components.SingletonComponent @Module @InstallIn(SingletonComponent::class) -interface ImportModule { +interface ExternalModule { @Binds fun playlistImporter(playlistImporter: PlaylistImporterImpl): PlaylistImporter @Binds fun m3u(m3u: M3UImpl): M3U diff --git a/app/src/main/java/org/oxycblt/auxio/music/import/M3U.kt b/app/src/main/java/org/oxycblt/auxio/music/external/M3U.kt similarity index 93% rename from app/src/main/java/org/oxycblt/auxio/music/import/M3U.kt rename to app/src/main/java/org/oxycblt/auxio/music/external/M3U.kt index ac897c85d..19b0aa0ec 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/import/M3U.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/external/M3U.kt @@ -16,15 +16,15 @@ * along with this program. If not, see . */ -package org.oxycblt.auxio.music.import +package org.oxycblt.auxio.music.external -import org.oxycblt.auxio.music.fs.Components import java.io.BufferedReader import java.io.InputStream import java.io.InputStreamReader +import javax.inject.Inject +import org.oxycblt.auxio.music.fs.Components import org.oxycblt.auxio.music.fs.Path import org.oxycblt.auxio.util.logW -import javax.inject.Inject interface M3U { fun read(stream: InputStream, workingDirectory: Path): List? @@ -59,7 +59,10 @@ class M3UImpl @Inject constructor() : M3U { return media.ifEmpty { null } } - private fun resolveRelativePath(relative: Components, workingDirectory: Components): Components { + private fun resolveRelativePath( + relative: Components, + workingDirectory: Components + ): Components { var components = workingDirectory for (component in relative.components) { when (component) { diff --git a/app/src/main/java/org/oxycblt/auxio/music/import/PlaylistImporter.kt b/app/src/main/java/org/oxycblt/auxio/music/external/PlaylistImporter.kt similarity index 52% rename from app/src/main/java/org/oxycblt/auxio/music/import/PlaylistImporter.kt rename to app/src/main/java/org/oxycblt/auxio/music/external/PlaylistImporter.kt index a2c2bc7df..dcaad62ae 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/import/PlaylistImporter.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/external/PlaylistImporter.kt @@ -1,16 +1,29 @@ -package org.oxycblt.auxio.music.import +/* + * Copyright (c) 2023 Auxio Project + * PlaylistImporter.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 . + */ + +package org.oxycblt.auxio.music.external import android.content.ContentResolver -import android.content.Context import android.net.Uri import dagger.hilt.android.qualifiers.ApplicationContext -import org.oxycblt.auxio.music.Playlist -import org.oxycblt.auxio.music.Song -import org.oxycblt.auxio.music.device.DeviceLibrary +import javax.inject.Inject import org.oxycblt.auxio.music.fs.ContentPathResolver import org.oxycblt.auxio.music.fs.Path -import org.oxycblt.auxio.music.fs.contentResolverSafe -import javax.inject.Inject interface PlaylistImporter { suspend fun import(uri: Uri): ImportedPlaylist? @@ -18,7 +31,9 @@ interface PlaylistImporter { data class ImportedPlaylist(val name: String?, val paths: List) -class PlaylistImporterImpl @Inject constructor( +class PlaylistImporterImpl +@Inject +constructor( @ApplicationContext private val contentResolver: ContentResolver, private val contentPathResolver: ContentPathResolver, private val m3u: M3U @@ -30,4 +45,4 @@ class PlaylistImporterImpl @Inject constructor( return ImportedPlaylist(null, paths) } } -} \ No newline at end of file +} diff --git a/app/src/main/java/org/oxycblt/auxio/music/fs/ContentPathResolver.kt b/app/src/main/java/org/oxycblt/auxio/music/fs/ContentPathResolver.kt index 84db744c8..e72450450 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/fs/ContentPathResolver.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/fs/ContentPathResolver.kt @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2023 Auxio Project + * ContentPathResolver.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 . + */ + package org.oxycblt.auxio.music.fs import android.content.ContentResolver @@ -7,17 +25,18 @@ import android.os.Build import android.provider.MediaStore import androidx.core.database.getStringOrNull import org.oxycblt.auxio.util.logE -import org.oxycblt.auxio.util.logW -import javax.inject.Inject /** * Resolves a content URI into a [Path] instance. - * TODO: Integrate this with [MediaStoreExtractor]. + * * @author Alexander Capehart (OxygenCobalt) + * + * TODO: Integrate this with [MediaStoreExtractor]. */ interface ContentPathResolver { /** * Resolve a content [Uri] into it's corresponding [Path]. + * * @param uri The content [Uri] to resolve. * @return The corresponding [Path], or null if the [Uri] is invalid. */ @@ -28,7 +47,6 @@ interface ContentPathResolver { when { Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q -> Api29ContentPathResolverImpl(context.contentResolverSafe, volumeManager) - else -> Api21ContentPathResolverImpl(context.contentResolverSafe, volumeManager) } } @@ -39,12 +57,11 @@ private class Api21ContentPathResolverImpl( private val volumeManager: VolumeManager ) : ContentPathResolver { override fun resolve(uri: Uri): Path? { - val rawPath = contentResolver.useQuery( - uri, arrayOf(MediaStore.MediaColumns.DATA) - ) { cursor -> - cursor.moveToFirst() - cursor.getStringOrNull(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)) - } + val rawPath = + contentResolver.useQuery(uri, arrayOf(MediaStore.MediaColumns.DATA)) { cursor -> + cursor.moveToFirst() + cursor.getStringOrNull(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)) + } if (rawPath == null) { logE("No data available for uri $uri") @@ -72,26 +89,19 @@ private class Api29ContentPathResolverImpl( private data class RawPath(val volumeName: String?, val relativePath: String?) override fun resolve(uri: Uri): Path? { - val rawPath = contentResolver.useQuery( - uri, arrayOf( - MediaStore.MediaColumns.VOLUME_NAME, - MediaStore.MediaColumns.RELATIVE_PATH - ) - ) { cursor -> - cursor.moveToFirst() - RawPath( - cursor.getStringOrNull( - cursor.getColumnIndexOrThrow( - MediaStore.MediaColumns.VOLUME_NAME - ) - ), - cursor.getStringOrNull( - cursor.getColumnIndexOrThrow( - MediaStore.MediaColumns.RELATIVE_PATH - ) - ) - ) - } + val rawPath = + contentResolver.useQuery( + uri, + arrayOf( + MediaStore.MediaColumns.VOLUME_NAME, MediaStore.MediaColumns.RELATIVE_PATH)) { + cursor -> + cursor.moveToFirst() + RawPath( + cursor.getStringOrNull( + cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.VOLUME_NAME)), + cursor.getStringOrNull( + cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.RELATIVE_PATH))) + } if (rawPath.volumeName == null || rawPath.relativePath == null) { logE("No data available for uri $uri (raw path obtained: $rawPath)") @@ -108,4 +118,4 @@ private class Api29ContentPathResolverImpl( logE("No volume found for uri $uri") return null } -} \ No newline at end of file +} diff --git a/app/src/main/java/org/oxycblt/auxio/music/fs/FsModule.kt b/app/src/main/java/org/oxycblt/auxio/music/fs/FsModule.kt index 13d0a2d2a..6298e98e1 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/fs/FsModule.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/fs/FsModule.kt @@ -39,5 +39,6 @@ class FsModule { MediaStoreExtractor.from(context, volumeManager) @Provides - fun contentPathResolver(@ApplicationContext context: Context, volumeManager: VolumeManager) = ContentPathResolver.from(context, volumeManager) + fun contentPathResolver(@ApplicationContext context: Context, volumeManager: VolumeManager) = + ContentPathResolver.from(context, volumeManager) }