Use couroutines for debug export functions

This commit is contained in:
MM20
2021-09-26 17:23:59 +02:00
parent 07e1e5ebdb
commit f50a87d681
2 changed files with 55 additions and 39 deletions
@@ -3,8 +3,6 @@ package de.mm20.launcher2.debug
import android.content.Context
import android.os.Build
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.text.SimpleDateFormat
@@ -12,32 +10,38 @@ import java.util.*
class DebugInformationDumper {
fun dump(context: Context): String {
suspend fun dump(context: Context): String {
val df = SimpleDateFormat("yyyy-MM-dd-HHmmss")
val file = File(context.getExternalFilesDir(null), "kvaesitso-log-${df.format(Date(System.currentTimeMillis()))}")
val fos = file.outputStream().writer()
fos.write("Device: ${Build.DEVICE}\n")
fos.write("SDK version: ${Build.VERSION.SDK_INT}\n")
fos.write("====================================\n")
Thread {
val input = Runtime.getRuntime().exec("/system/bin/sh -c logcat").inputStream.bufferedReader()
val file = File(
context.getExternalFilesDir(null),
"kvaesitso-log-${df.format(Date(System.currentTimeMillis()))}"
)
withContext(Dispatchers.IO) {
val timeout = System.currentTimeMillis() + 5000
val fos = file.outputStream().writer()
fos.write("Device: ${Build.DEVICE}\n")
fos.write("SDK version: ${Build.VERSION.SDK_INT}\n")
fos.write("====================================\n")
val input =
Runtime.getRuntime().exec("/system/bin/sh -c logcat").inputStream.bufferedReader()
var line = input.readLine()
while (line != null) {
while (line != null && System.currentTimeMillis() < timeout) {
line = input.readLine()
fos.write("$line\n")
}
fos.close()
}.start()
}
return file.absolutePath
}
fun exportDatabases(context: Context): String {
suspend fun exportDatabases(context: Context): String {
val df = SimpleDateFormat("yyyy-MM-dd-HHmmss")
val exportFile = File(context.getExternalFilesDir(null), "room-${df.format(Date(System.currentTimeMillis()))}.db")
GlobalScope.launch {
withContext(Dispatchers.IO) {
context.getDatabasePath("room").copyTo(exportFile)
}
val exportFile = File(
context.getExternalFilesDir(null),
"room-${df.format(Date(System.currentTimeMillis()))}.db"
)
withContext(Dispatchers.IO) {
context.getDatabasePath("room").copyTo(exportFile)
}
return exportFile.absolutePath
}