From 24fa557e9291483a228b500e57408bebd554037c Mon Sep 17 00:00:00 2001 From: Omar Syam <147588397+syamosx@users.noreply.github.com> Date: Sat, 7 Nov 2026 18:41:53 +0200 Subject: [PATCH 1/2] refactor: normalize filesystem and document-provider media paths --- .../xyz/mpv/rex/utils/storage/MediaPaths.kt | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 app/src/main/kotlin/xyz/mpv/rex/utils/storage/MediaPaths.kt diff --git a/app/src/main/kotlin/xyz/mpv/rex/utils/storage/MediaPaths.kt b/app/src/main/kotlin/xyz/mpv/rex/utils/storage/MediaPaths.kt new file mode 100644 index 000000000..6dae2c5ee --- /dev/null +++ b/app/src/main/kotlin/xyz/mpv/rex/utils/storage/MediaPaths.kt @@ -0,0 +1,36 @@ +package xyz.mpv.rex.utils.storage + +import java.net.URI +import java.net.URLDecoder +import java.io.File + +/** Translate Android's external-storage document IDs to paths used by media scanning. */ +internal fun selectedFolderPath(value: String): String { + if (value.isBlank()) return "" + if (!value.startsWith("content://")) return folderKey(File(value).normalize().path) + return runCatching { + val uri = URI(value) + val raw = uri.rawPath + val id = URLDecoder.decode( + raw.substringAfterLast(if ("/document/" in raw) "/document/" else "/tree/"), + "UTF-8", + ) + if (uri.authority == "com.android.externalstorage.documents" && ':' in id) { + val volume = id.substringBefore(':') + val root = if (volume == "primary") "/storage/emulated/0" else "/storage/$volume" + folderKey("$root/${id.substringAfter(':')}") + } else { + // Tree and document URIs for the same provider must compare by document ID. + "content://${uri.authority}/document/$id".trimEnd('/') + } + }.getOrDefault(value.trimEnd('/')) +} + +internal fun comparableFolderPath(value: String): String = + selectedFolderPath(value).let { + when { + it == "/sdcard" || it.startsWith("/sdcard/") -> "/storage/emulated/0" + it.removePrefix("/sdcard") + else -> it + } + } + From bac0f2ec9f31cd432170d1b950ea76019e57fb35 Mon Sep 17 00:00:00 2001 From: Omar Syam <147588397+syamosx@users.noreply.github.com> Date: Sat, 7 Nov 2026 23:36:17 +0200 Subject: [PATCH 2/2] fix: allow excluding parent folders and their descendants --- .../repository/HybridMediaIndexRepository.kt | 44 ++- .../browser/folderlist/FolderListViewModel.kt | 5 +- .../rex/ui/browser/search/SearchViewModel.kt | 15 +- .../ui/preferences/ExclusionFolderPicker.kt | 363 ++++++++++++++++++ .../preferences/FoldersPreferencesScreen.kt | 242 +++--------- .../mpv/rex/utils/media/MediaMetadataOps.kt | 3 +- .../xyz/mpv/rex/utils/storage/FolderMedia.kt | 4 +- .../xyz/mpv/rex/utils/storage/FolderPaths.kt | 66 ++++ app/src/main/res/values/strings.xml | 26 ++ .../HybridMediaIndexRepositoryTest.kt | 24 +- .../mpv/rex/utils/storage/FolderPathsTest.kt | 118 ++++++ 11 files changed, 696 insertions(+), 214 deletions(-) create mode 100644 app/src/main/kotlin/xyz/mpv/rex/ui/preferences/ExclusionFolderPicker.kt create mode 100644 app/src/main/kotlin/xyz/mpv/rex/utils/storage/FolderPaths.kt create mode 100644 app/src/test/kotlin/xyz/mpv/rex/utils/storage/FolderPathsTest.kt diff --git a/app/src/main/kotlin/xyz/mpv/rex/database/repository/HybridMediaIndexRepository.kt b/app/src/main/kotlin/xyz/mpv/rex/database/repository/HybridMediaIndexRepository.kt index 82564bd36..76b5567ae 100644 --- a/app/src/main/kotlin/xyz/mpv/rex/database/repository/HybridMediaIndexRepository.kt +++ b/app/src/main/kotlin/xyz/mpv/rex/database/repository/HybridMediaIndexRepository.kt @@ -20,6 +20,7 @@ import xyz.mpv.rex.domain.media.model.Video import xyz.mpv.rex.preferences.BrowserPreferences import xyz.mpv.rex.preferences.FoldersPreferences import xyz.mpv.rex.utils.media.MediaFormatter +import xyz.mpv.rex.utils.storage.isPathExcluded import xyz.mpv.rex.utils.storage.FileFilterUtils import xyz.mpv.rex.utils.storage.FileTypeUtils import xyz.mpv.rex.utils.storage.StorageVolumeUtils @@ -84,11 +85,10 @@ class HybridMediaIndexRepository( suspend fun purgeExcludedFolders(excludedPaths: Set = foldersPreferences.blacklistedFolders.get()) = withContext(Dispatchers.IO) { if (excludedPaths.isEmpty()) return@withContext Log.d(TAG, "Purging excluded folders from Room index: $excludedPaths") - excludedPaths.forEach { rawPath -> - val normalized = normalizePath(File(rawPath)) - val escaped = normalized.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_") - dao.deleteByPathPrefix(normalized, "$escaped/%") - } + val locations = dao.getAvailableMedia(includeNoMedia = true) + .filter { isPathExcluded(it.location, excludedPaths) || isPathExcluded(it.parentIdentity, excludedPaths) } + .map { it.location } + locations.chunked(400).forEach { dao.deleteByLocations(it) } } suspend fun removeEntries(paths: List) = withContext(Dispatchers.IO) { @@ -318,13 +318,20 @@ class HybridMediaIndexRepository( } + private suspend fun visibleMedia(includeNoMedia: Boolean): List { + val blacklist = foldersPreferences.blacklistedFolders.get() + return dao.getAvailableMedia(includeNoMedia).filter { item -> + !isPathExcluded(item.location, blacklist) && !isPathExcluded(item.parentIdentity, blacklist) + } + } + suspend fun getFlatFolders( playbackStates: List, thresholdDays: Int, watchedThreshold: Int, includeNoMedia: Boolean = browserPreferences.includeNoMediaContent.get(), ): List = withContext(Dispatchers.IO) { - val items = dao.getAvailableMedia(includeNoMedia) + val items = visibleMedia(includeNoMedia) val stateByIdentity = playbackStates.associateBy { it.mediaTitle } val thresholdMillis = thresholdDays * 24L * 60L * 60L * 1000L val now = System.currentTimeMillis() @@ -381,7 +388,7 @@ class HybridMediaIndexRepository( includeNoMedia: Boolean = browserPreferences.includeNoMediaContent.get(), ): List = withContext(Dispatchers.IO) { val tree = buildFileTree( - dao.getAvailableMedia(includeNoMedia), + visibleMedia(includeNoMedia), playbackStates, thresholdDays, watchedThreshold, @@ -399,7 +406,7 @@ class HybridMediaIndexRepository( includeNoMedia: Boolean = browserPreferences.includeNoMediaContent.get(), ): MediaFolder? = withContext(Dispatchers.IO) { buildFileTree( - dao.getAvailableMedia(includeNoMedia), + visibleMedia(includeNoMedia), playbackStates, thresholdDays, watchedThreshold, @@ -410,7 +417,7 @@ class HybridMediaIndexRepository( parentIdentity: String, includeNoMedia: Boolean = browserPreferences.includeNoMediaContent.get(), ): List