Address GlobalScope usage and minor cleanup

This commit is contained in:
FooIbar committed 2024-03-01 16:05:17 +08:00
1 parent fb1b08e7e8
commit 7357724392
4 files changed
+29 -51

No files matched your search

@@ -65,13 +65,10 @@ import eu.kanade.tachiyomi.util.system.logcat
import io.ktor.client.HttpClient
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.plugins.cookies.HttpCookies
import java.io.File
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.launch
import logcat.AndroidLogcatLogger
import logcat.LogPriority
import logcat.LogcatLogger
import moe.tarsin.coroutines.runSuspendCatching
import okhttp3.AsyncDns
import okhttp3.android.AndroidAsyncDns
import okio.Path.Companion.toOkioPath
@@ -110,41 +107,31 @@ class EhApplication : Application(), SingletonImageLoader.Factory {
System.loadLibrary("ehviewer")
ReadableTime.initialize(this)
lifecycleScope.launchIO {
launchIO {
launch {
EhTagDatabase
}
launchIO {
launch {
EhDB
}
launchIO {
launch {
DownloadManager.readPagesFromLocal()
}
launchIO {
runSuspendCatching {
val files = mutableListOf<File>()
AppConfig.externalCrashDir?.listFiles()?.let { files.addAll(it) }
AppConfig.externalParseErrorDir?.listFiles()?.let { files.addAll(it) }
files.sortByDescending { it.lastModified() }
files.forEachIndexed { index, file ->
ensureActive()
if (index > 9) {
file.delete()
}
}
}.onFailure {
logcat(it)
}
launch {
FileUtils.cleanupDirectory(AppConfig.externalCrashDir)
FileUtils.cleanupDirectory(AppConfig.externalParseErrorDir)
}
launchIO {
launch {
cleanupDownload()
}
if (Settings.requestNews) {
launchIO {
launch {
checkDawn()
}
}
launch {
cleanObsoleteCache()
}
}
cleanObsoleteCache(this)
if (BuildConfig.DEBUG) {
StrictMode.enableDefaults()
Snapshot.registerApplyObserver { anies, _ ->
@@ -1,9 +1,7 @@
package com.hippo.ehviewer.legacy
import android.app.Application
import eu.kanade.tachiyomi.util.lang.launchIO
import java.io.File
import kotlinx.coroutines.DelicateCoroutinesApi
import splitties.init.appCtx
private val OBSOLETE_CACHE_DIRS = arrayOf(
"image",
@@ -12,16 +10,13 @@ private val OBSOLETE_CACHE_DIRS = arrayOf(
"spider_info",
)
@OptIn(DelicateCoroutinesApi::class)
fun cleanObsoleteCache(application: Application) {
launchIO {
application.deleteDatabase("hosts.db")
val dir = application.cacheDir
for (subdir in OBSOLETE_CACHE_DIRS) {
val file = File(dir, subdir)
if (file.exists()) {
file.deleteRecursively()
}
fun cleanObsoleteCache() {
appCtx.deleteDatabase("hosts.db")
val dir = appCtx.cacheDir
for (subdir in OBSOLETE_CACHE_DIRS) {
val file = File(dir, subdir)
if (file.exists()) {
file.deleteRecursively()
}
}
}
@@ -80,6 +80,15 @@ object FileUtils {
return success
}
fun cleanupDirectory(dir: File?, maxFiles: Int = 10) {
dir?.listFiles()?.let { files ->
files.sortByDescending { it.lastModified() }
files.forEachIndexed { index, file ->
if (index >= maxFiles) file.delete()
}
}
}
fun sanitizeFilename(rawFilename: String): String {
// Remove forbidden_filename_characters
var filename = rawFilename
@@ -157,6 +166,7 @@ object FileUtils {
}
}
@Suppress("BlockingMethodInNonBlockingContext")
suspend fun ByteReadChannel.copyTo(file: File) {
RandomAccessFile(file, "rw").use {
copyTo(it.channel)
@@ -1,26 +1,12 @@
package eu.kanade.tachiyomi.util.lang
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
/**
* Think twice before using this. This is a delicate API. It is easy to accidentally create resource or memory leaks when GlobalScope is used.
*
* **Possible replacements**
* - suspend function
* - custom scope like view or presenter scope
*/
@DelicateCoroutinesApi
fun launchIO(block: suspend CoroutineScope.() -> Unit): Job =
GlobalScope.launch(Dispatchers.IO, CoroutineStart.DEFAULT, block)
fun CoroutineScope.launchUI(block: suspend CoroutineScope.() -> Unit): Job =
launch(Dispatchers.Main, block = block)