Minor refactor
This commit is contained in:
1 parent
49eb9d5077
commit
895cfc7bc6
4 files changed
+41
-48
No files matched your search
@@ -8,8 +8,6 @@ import com.hippo.ehviewer.client.data.TagNamespace
|
||||
import com.hippo.unifile.UniFile
|
||||
import com.hippo.unifile.openInputStream
|
||||
import com.hippo.unifile.openOutputStream
|
||||
import java.io.InputStreamReader
|
||||
import java.io.OutputStreamWriter
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
@@ -19,7 +17,7 @@ import kotlinx.serialization.encoding.Decoder
|
||||
import kotlinx.serialization.encoding.Encoder
|
||||
import nl.adaptivity.xmlutil.XmlDeclMode
|
||||
import nl.adaptivity.xmlutil.core.XmlVersion
|
||||
import nl.adaptivity.xmlutil.newGenericWriter
|
||||
import nl.adaptivity.xmlutil.newWriter
|
||||
import nl.adaptivity.xmlutil.serialization.XML
|
||||
import nl.adaptivity.xmlutil.serialization.XmlElement
|
||||
import nl.adaptivity.xmlutil.xmlStreaming
|
||||
@@ -98,21 +96,17 @@ fun ComicInfo.toSimpleTags() = listOfNotNull(
|
||||
).flatten().ifEmpty { null }
|
||||
|
||||
fun ComicInfo.write(file: UniFile) {
|
||||
file.openOutputStream().use {
|
||||
OutputStreamWriter(it, Charsets.UTF_8).use { writer ->
|
||||
xmlStreaming.newGenericWriter(writer).use { xmlWriter ->
|
||||
xml.encodeToWriter(xmlWriter, ComicInfo.serializer(), this)
|
||||
}
|
||||
file.openOutputStream().bufferedWriter().use {
|
||||
xmlStreaming.newWriter(it).use { writer ->
|
||||
xml.encodeToWriter(writer, ComicInfo.serializer(), this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun readComicInfo(file: UniFile): ComicInfo? = runCatching {
|
||||
file.openInputStream().use {
|
||||
InputStreamReader(it, Charsets.UTF_8).use { reader ->
|
||||
xmlStreaming.newGenericReader(reader).use { xmlReader ->
|
||||
xml.decodeFromReader(ComicInfo.serializer(), xmlReader)
|
||||
}
|
||||
file.openInputStream().bufferedReader().use {
|
||||
xmlStreaming.newReader(it).use { reader ->
|
||||
xml.decodeFromReader(ComicInfo.serializer(), reader)
|
||||
}
|
||||
}
|
||||
}.getOrNull()
|
||||
|
||||
@@ -5,9 +5,8 @@ import android.os.Debug
|
||||
import com.hippo.ehviewer.BuildConfig
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import java.io.File
|
||||
import java.io.FileWriter
|
||||
import java.io.OutputStreamWriter
|
||||
import java.io.PrintWriter
|
||||
import java.io.Writer
|
||||
|
||||
private fun joinIfStringArray(any: Any?): String = if (any is Array<*>) any.joinToString() else any.toString()
|
||||
|
||||
@@ -16,7 +15,7 @@ private fun collectClassStaticInfo(clazz: Class<*>): String = clazz.fields.joinT
|
||||
}
|
||||
|
||||
object Crash {
|
||||
fun collectInfo(writer: OutputStreamWriter) {
|
||||
fun collectInfo(writer: Writer) {
|
||||
writer.write("======== PackageInfo ========\n")
|
||||
writer.write("PackageName=${BuildConfig.APPLICATION_ID}\n")
|
||||
writer.write("VersionName=${BuildConfig.VERSION_NAME}\n")
|
||||
@@ -46,12 +45,11 @@ object Crash {
|
||||
writer.flush()
|
||||
}
|
||||
|
||||
private fun getThrowableInfo(t: Throwable, fw: FileWriter) {
|
||||
val printWriter = PrintWriter(fw)
|
||||
t.printStackTrace(printWriter)
|
||||
private fun getThrowableInfo(t: Throwable, writer: PrintWriter) {
|
||||
t.printStackTrace(writer)
|
||||
var cause = t.cause
|
||||
while (cause != null) {
|
||||
cause.printStackTrace(printWriter)
|
||||
cause.printStackTrace(writer)
|
||||
cause = cause.cause
|
||||
}
|
||||
}
|
||||
@@ -62,14 +60,13 @@ object Crash {
|
||||
val fileName = "crash-$nowString.log"
|
||||
val file = File(dir, fileName)
|
||||
runCatching {
|
||||
FileWriter(file).use { fw ->
|
||||
fw.write("TIME=${nowString}\n")
|
||||
fw.write("\n")
|
||||
collectInfo(fw)
|
||||
fw.write("======== CrashInfo ========\n")
|
||||
getThrowableInfo(t, fw)
|
||||
fw.write("\n")
|
||||
fw.flush()
|
||||
file.printWriter().use { writer ->
|
||||
writer.write("TIME=${nowString}\n")
|
||||
writer.write("\n")
|
||||
collectInfo(writer)
|
||||
writer.write("======== CrashInfo ========\n")
|
||||
getThrowableInfo(t, writer)
|
||||
writer.write("\n")
|
||||
}
|
||||
}.onFailure {
|
||||
logcat(it)
|
||||
|
||||
@@ -1,34 +1,35 @@
|
||||
package com.hippo.ehviewer.util
|
||||
|
||||
import android.os.ParcelFileDescriptor
|
||||
import android.os.Build
|
||||
import android.system.Int64Ref
|
||||
import android.system.Os
|
||||
import androidx.annotation.RequiresApi
|
||||
import com.hippo.unifile.UniFile
|
||||
import com.hippo.unifile.openInputStream
|
||||
import com.hippo.unifile.openOutputStream
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import java.io.FileDescriptor
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
|
||||
private fun sendFileTotally(from: FileDescriptor, to: FileDescriptor): Long {
|
||||
@RequiresApi(Build.VERSION_CODES.P)
|
||||
private fun sendFileTotally(from: FileDescriptor, to: FileDescriptor): Long =
|
||||
Os.sendfile(to, from, Int64Ref(0), Long.MAX_VALUE)
|
||||
|
||||
infix fun UniFile.sendTo(file: UniFile): Long {
|
||||
if (isAtLeastP) {
|
||||
// sendfile may fail on some devices
|
||||
try {
|
||||
return Os.sendfile(to, from, Int64Ref(0), Long.MAX_VALUE)
|
||||
return openFileDescriptor("r").use { src ->
|
||||
file.openFileDescriptor("wt").use { dst ->
|
||||
sendFileTotally(src.fileDescriptor, dst.fileDescriptor)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
logcat("sendfile", e)
|
||||
}
|
||||
}
|
||||
return FileInputStream(from).use { src ->
|
||||
FileOutputStream(to).use { dst ->
|
||||
return openInputStream().use { src ->
|
||||
file.openOutputStream().use { dst ->
|
||||
src.channel.transferTo(0, Long.MAX_VALUE, dst.channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
infix fun ParcelFileDescriptor.sendTo(fd: ParcelFileDescriptor) {
|
||||
sendFileTotally(fileDescriptor, fd.fileDescriptor)
|
||||
}
|
||||
|
||||
infix fun UniFile.sendTo(file: UniFile) = openFileDescriptor("r").use { src ->
|
||||
file.openFileDescriptor("wt").use { dst -> src sendTo dst }
|
||||
}
|
||||
@@ -17,7 +17,7 @@ package com.hippo.ehviewer.util
|
||||
|
||||
import android.os.Looper
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import java.io.FileReader
|
||||
import java.io.File
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
@@ -34,8 +34,8 @@ object OSUtils {
|
||||
|
||||
val totalMemory by lazy {
|
||||
runCatching {
|
||||
FileReader(PROCFS_MEMFILE).buffered(64).use { reader ->
|
||||
reader.lineSequence().forEach { line ->
|
||||
File(PROCFS_MEMFILE).useLines {
|
||||
it.forEach { line ->
|
||||
PROCFS_MEMFILE_FORMAT.find(line)?.destructured?.let { (k, v) ->
|
||||
if (k == MEMTOTAL_STRING) {
|
||||
return@runCatching v.toLong() * 1024
|
||||
@@ -44,9 +44,10 @@ object OSUtils {
|
||||
}
|
||||
}
|
||||
-1L
|
||||
}.onFailure {
|
||||
}.getOrElse {
|
||||
logcat(it)
|
||||
}.getOrElse { -1L }
|
||||
-1L
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user