From b5d8fb84dbd72fc8e97b60c3e55e5fcb6a34fa06 Mon Sep 17 00:00:00 2001 From: Alex Balgavy <8124851+thezeroalpha@users.noreply.github.com> Date: Fri, 26 Aug 2022 21:52:23 +0200 Subject: [PATCH] Fix chapter names fallback (#1464) The chapter name used as a fallback evaluates to a long absolute path, which is not easy to read. This commit shortens the name. See https://github.com/PaulWoitaschek/Voice/issues/1447 --- .../src/main/kotlin/voice/app/scanner/MediaAnalyzer.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scanner/src/main/kotlin/voice/app/scanner/MediaAnalyzer.kt b/scanner/src/main/kotlin/voice/app/scanner/MediaAnalyzer.kt index 746fe9f33f..6c11a583c8 100644 --- a/scanner/src/main/kotlin/voice/app/scanner/MediaAnalyzer.kt +++ b/scanner/src/main/kotlin/voice/app/scanner/MediaAnalyzer.kt @@ -2,6 +2,7 @@ package voice.app.scanner import android.content.Context import android.net.Uri +import androidx.documentfile.provider.DocumentFile import kotlinx.serialization.SerializationException import kotlinx.serialization.json.Json import voice.data.MarkData @@ -55,7 +56,7 @@ class MediaAnalyzer return if (duration != null && duration > 0) { Metadata( duration = duration.seconds.inWholeMilliseconds, - chapterName = parsed.findTag(TagType.Title) ?: chapterNameFallback(uri), + chapterName = parsed.findTag(TagType.Title) ?: chapterNameFallback(uri, context), author = parsed.findTag(TagType.Artist), bookName = parsed.findTag(TagType.Album), chapters = parsed.chapters.mapIndexed { index, metaDataChapter -> @@ -80,8 +81,9 @@ class MediaAnalyzer ) } -private fun chapterNameFallback(file: Uri): String { - val name = file.lastPathSegment ?: "Chapter" +private fun chapterNameFallback(uri: Uri, context: Context): String { + val file = DocumentFile.fromSingleUri(context, uri) + val name = file?.name ?: "Chapter" return name.substringBeforeLast(".") .trim() .takeUnless { it.isEmpty() }