Modify .nomedia status in background thread
This commit is contained in:
1 parent
b6d8a340e5
commit
2db6f7a9ae
4 files changed
+39
-63
No files matched your search
@@ -38,9 +38,8 @@ import com.hippo.ehviewer.dailycheck.checkDawn
|
||||
import com.hippo.ehviewer.dao.buildMainDB
|
||||
import com.hippo.ehviewer.download.DownloadManager
|
||||
import com.hippo.ehviewer.legacy.cleanObsoleteCache
|
||||
import com.hippo.ehviewer.ui.CommonOperations
|
||||
import com.hippo.ehviewer.ui.EhActivity
|
||||
import com.hippo.util.ExceptionUtils
|
||||
import com.hippo.ehviewer.ui.keepNoMediaFileStatus
|
||||
import com.hippo.util.ReadableTime
|
||||
import com.hippo.yorozuya.FileUtils
|
||||
import com.hippo.yorozuya.IntIdGenerator
|
||||
@@ -119,24 +118,15 @@ class EhApplication : Application(), DefaultLifecycleObserver, ImageLoaderFactor
|
||||
}
|
||||
|
||||
private fun cleanupDownload() {
|
||||
try {
|
||||
val downloadLocation = Settings.downloadLocation
|
||||
if (Settings.mediaScan) {
|
||||
CommonOperations.removeNoMediaFile(downloadLocation)
|
||||
} else {
|
||||
CommonOperations.ensureNoMediaFile(downloadLocation)
|
||||
}
|
||||
} catch (t: Throwable) {
|
||||
t.printStackTrace()
|
||||
ExceptionUtils.throwIfFatal(t)
|
||||
runCatching {
|
||||
keepNoMediaFileStatus()
|
||||
}.onFailure {
|
||||
it.printStackTrace()
|
||||
}
|
||||
|
||||
// Clear temp files
|
||||
try {
|
||||
runCatching {
|
||||
clearTempDir()
|
||||
} catch (t: Throwable) {
|
||||
t.printStackTrace()
|
||||
ExceptionUtils.throwIfFatal(t)
|
||||
}.onFailure {
|
||||
it.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,8 +24,6 @@ import androidx.compose.ui.unit.dp
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.hippo.ehviewer.EhApplication.Companion.application
|
||||
import com.hippo.ehviewer.client.data.FavListUrlBuilder
|
||||
import com.hippo.ehviewer.ui.CommonOperations.ensureNoMediaFile
|
||||
import com.hippo.ehviewer.ui.CommonOperations.removeNoMediaFile
|
||||
import com.hippo.ehviewer.ui.scene.GalleryListScene
|
||||
import com.hippo.unifile.UniFile
|
||||
import com.hippo.yorozuya.NumberUtils
|
||||
@@ -427,11 +425,6 @@ object Settings {
|
||||
putString(KEY_DOWNLOAD_SAVE_PATH, uri.encodedPath)
|
||||
putString(KEY_DOWNLOAD_SAVE_QUERY, uri.encodedQuery)
|
||||
putString(KEY_DOWNLOAD_SAVE_FRAGMENT, uri.encodedFragment)
|
||||
if (mediaScan) {
|
||||
removeNoMediaFile(location)
|
||||
} else {
|
||||
ensureNoMediaFile(location)
|
||||
}
|
||||
}
|
||||
|
||||
val mediaScan: Boolean
|
||||
|
||||
@@ -18,7 +18,6 @@ package com.hippo.ehviewer.ui
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.ParcelFileDescriptor
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.hippo.app.ListCheckBoxDialogBuilder
|
||||
@@ -33,9 +32,7 @@ import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.download.DownloadService
|
||||
import com.hippo.ehviewer.ui.scene.BaseScene
|
||||
import com.hippo.unifile.UniFile
|
||||
import com.hippo.yorozuya.IOUtils
|
||||
import com.hippo.yorozuya.collect.LongList
|
||||
import java.io.IOException
|
||||
import com.hippo.ehviewer.download.DownloadManager as downloadManager
|
||||
|
||||
object CommonOperations {
|
||||
@@ -221,33 +218,6 @@ object CommonOperations {
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun ensureNoMediaFile(file: UniFile?) {
|
||||
if (null == file) {
|
||||
return
|
||||
}
|
||||
val noMedia = file.createFile(".nomedia") ?: return
|
||||
var `is`: ParcelFileDescriptor? = null
|
||||
try {
|
||||
`is` = noMedia.openFileDescriptor("r")
|
||||
} catch (e: IOException) {
|
||||
// Ignore
|
||||
} finally {
|
||||
IOUtils.closeQuietly(`is`)
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun removeNoMediaFile(file: UniFile?) {
|
||||
if (null == file) {
|
||||
return
|
||||
}
|
||||
val noMedia = file.subFile(".nomedia")
|
||||
if (null != noMedia && noMedia.isFile) {
|
||||
noMedia.delete()
|
||||
}
|
||||
}
|
||||
|
||||
private class DelegateFavoriteCallback(
|
||||
private val delegate: EhClient.Callback<Unit>, private val info: GalleryInfo,
|
||||
private val newFavoriteName: String?, private val slot: Int
|
||||
@@ -267,4 +237,23 @@ object CommonOperations {
|
||||
delegate.onCancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun removeNoMediaFile(downloadDir: UniFile) {
|
||||
val noMedia = downloadDir.subFile(".nomedia") ?: return
|
||||
noMedia.delete()
|
||||
}
|
||||
|
||||
private fun ensureNoMediaFile(downloadDir: UniFile) {
|
||||
downloadDir.createFile(".nomedia") ?: return
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun keepNoMediaFileStatus() {
|
||||
val downloadLocation = Settings.downloadLocation ?: return
|
||||
if (Settings.mediaScan) {
|
||||
removeNoMediaFile(downloadLocation)
|
||||
} else {
|
||||
ensureNoMediaFile(downloadLocation)
|
||||
}
|
||||
}
|
||||
@@ -19,16 +19,17 @@ import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.preference.Preference
|
||||
import com.hippo.app.BaseDialogBuilder
|
||||
import com.hippo.ehviewer.AppConfig
|
||||
import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.ui.CommonOperations.ensureNoMediaFile
|
||||
import com.hippo.ehviewer.ui.CommonOperations.removeNoMediaFile
|
||||
import com.hippo.ehviewer.ui.keepNoMediaFileStatus
|
||||
import com.hippo.ehviewer.ui.scene.BaseScene
|
||||
import com.hippo.unifile.UniFile
|
||||
import com.hippo.util.ExceptionUtils
|
||||
import eu.kanade.tachiyomi.util.lang.launchIO
|
||||
|
||||
class DownloadFragment : BasePreferenceFragment() {
|
||||
private var mDownloadLocation: Preference? = null
|
||||
@@ -43,6 +44,9 @@ class DownloadFragment : BasePreferenceFragment() {
|
||||
val uniFile = UniFile.fromTreeUri(activity, treeUri)
|
||||
if (uniFile != null) {
|
||||
Settings.putDownloadLocation(uniFile)
|
||||
lifecycleScope.launchIO {
|
||||
keepNoMediaFileStatus()
|
||||
}
|
||||
onUpdateDownloadLocation()
|
||||
} else {
|
||||
showTip(
|
||||
@@ -96,6 +100,9 @@ class DownloadFragment : BasePreferenceFragment() {
|
||||
val uniFile = UniFile.fromFile(AppConfig.getDefaultDownloadDir())
|
||||
if (uniFile != null) {
|
||||
Settings.putDownloadLocation(uniFile)
|
||||
lifecycleScope.launchIO {
|
||||
keepNoMediaFileStatus()
|
||||
}
|
||||
onUpdateDownloadLocation()
|
||||
} else {
|
||||
showTip(
|
||||
@@ -126,11 +133,8 @@ class DownloadFragment : BasePreferenceFragment() {
|
||||
val key = preference.key
|
||||
if (Settings.KEY_MEDIA_SCAN == key) {
|
||||
if (newValue is Boolean) {
|
||||
val downloadLocation = Settings.downloadLocation
|
||||
if (newValue) {
|
||||
removeNoMediaFile(downloadLocation)
|
||||
} else {
|
||||
ensureNoMediaFile(downloadLocation)
|
||||
lifecycleScope.launchIO {
|
||||
keepNoMediaFileStatus()
|
||||
}
|
||||
}
|
||||
return true
|
||||
|
||||
Reference in new issue
Block a user