From b8b118bdebe99cec25c69a8e8424fd4605f7b200 Mon Sep 17 00:00:00 2001 From: arkon Date: Thu, 26 Dec 2019 12:39:20 -0500 Subject: [PATCH] Add .nomedia file in each chapter download folder (#2199) * Move .nomedia creation to directory fetch * Add .nomedia file to all chapter download directories --- .../tachiyomi/data/download/DownloadProvider.kt | 4 +++- .../kanade/tachiyomi/data/download/Downloader.kt | 2 ++ .../ui/setting/SettingsDownloadController.kt | 10 ---------- .../main/java/eu/kanade/tachiyomi/util/DiskUtil.kt | 14 ++++++++++++++ 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt index e38bb88e2c..bed8c5e369 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt @@ -28,7 +28,9 @@ class DownloadProvider(private val context: Context) { * The root directory for downloads. */ private var downloadsDir = preferences.downloadsDirectory().getOrDefault().let { - UniFile.fromUri(context, Uri.parse(it)) + val dir = UniFile.fromUri(context, Uri.parse(it)) + DiskUtil.createNoMediaFile(dir, context) + dir } init { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt index f49585bc2e..15ce3dc680 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt @@ -407,6 +407,8 @@ class Downloader( if (download.status == Download.DOWNLOADED) { tmpDir.renameTo(dirname) cache.addChapter(dirname, mangaDir, download.manga) + + DiskUtil.createNoMediaFile(tmpDir, context) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt index 1b6119b13c..6a06ec8ca3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt @@ -17,7 +17,6 @@ import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.ui.base.controller.DialogController -import eu.kanade.tachiyomi.util.DiskUtil import eu.kanade.tachiyomi.util.getFilePicker import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -45,15 +44,6 @@ class SettingsDownloadController : SettingsController() { .subscribeUntilDestroy { path -> val dir = UniFile.fromUri(context, Uri.parse(path)) summary = dir.filePath ?: path - - // Don't display downloaded chapters in gallery apps creating .nomedia - if (dir != null && dir.exists()) { - val nomedia = dir.findFile(".nomedia") - if (nomedia == null) { - dir.createFile(".nomedia") - applicationContext?.let { DiskUtil.scanMedia(it, dir.uri) } - } - } } } switchPreference { diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/DiskUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/DiskUtil.kt index edff38614f..c735c0b8d7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/DiskUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/DiskUtil.kt @@ -7,6 +7,7 @@ import android.os.Build import android.os.Environment import android.support.v4.content.ContextCompat import android.support.v4.os.EnvironmentCompat +import com.hippo.unifile.UniFile import java.io.File object DiskUtil { @@ -54,6 +55,19 @@ object DiskUtil { return directories } + /** + * Don't display downloaded chapters in gallery apps creating `.nomedia`. + */ + fun createNoMediaFile(dir: UniFile?, context: Context?) { + if (dir != null && dir.exists()) { + val nomedia = dir.findFile(".nomedia") + if (nomedia == null) { + dir.createFile(".nomedia") + context?.let { scanMedia(it, dir.uri) } + } + } + } + /** * Scans the given file so that it can be shown in gallery apps, for example. */