Use couroutines for debug export functions

This commit is contained in:
MM20 2021-09-26 17:23:59 +02:00
parent 07e1e5ebdb
commit f50a87d681
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
2 changed files with 55 additions and 39 deletions

View File

@ -5,6 +5,7 @@ import android.content.pm.PackageManager
import android.os.Bundle import android.os.Bundle
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceCategory import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
@ -14,6 +15,7 @@ import de.mm20.launcher2.crashreporter.CrashReporter
import de.mm20.launcher2.debug.DebugInformationDumper import de.mm20.launcher2.debug.DebugInformationDumper
import de.mm20.launcher2.licenses.AppLicense import de.mm20.launcher2.licenses.AppLicense
import de.mm20.launcher2.licenses.OpenSourceLicenses import de.mm20.launcher2.licenses.OpenSourceLicenses
import kotlinx.coroutines.launch
class PreferencesAboutFragment : PreferenceFragmentCompat() { class PreferencesAboutFragment : PreferenceFragmentCompat() {
@ -77,8 +79,10 @@ class PreferencesAboutFragment : PreferenceFragmentCompat() {
R.anim.preference_fragment_parent_enter, R.anim.preference_fragment_parent_enter,
R.anim.preference_fragment_child_exit R.anim.preference_fragment_child_exit
) )
.replace(android.R.id.content, .replace(
PreferencesLicenseFragment(l)) android.R.id.content,
PreferencesLicenseFragment(l)
)
.addToBackStack(null) .addToBackStack(null)
.commit() .commit()
true true
@ -90,29 +94,35 @@ class PreferencesAboutFragment : PreferenceFragmentCompat() {
true true
} }
findPreference<Preference>("export_debug")?.setOnPreferenceClickListener { findPreference<Preference>("export_debug")?.setOnPreferenceClickListener {
lifecycleScope.launch {
val path = DebugInformationDumper().dump(requireContext())
Toast.makeText( Toast.makeText(
activity, activity,
getString( getString(
R.string.debug_export_information_file, R.string.debug_export_information_file,
DebugInformationDumper().dump(requireContext()) path
), ),
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
).show() ).show()
}
true true
} }
findPreference<Preference>("export_databases")?.setOnPreferenceClickListener { findPreference<Preference>("export_databases")?.setOnPreferenceClickListener {
MaterialDialog(requireContext()).show { MaterialDialog(requireContext()).show {
message(res = R.string.debug_export_databases_warning) message(res = R.string.debug_export_databases_warning)
positiveButton(res = R.string.dialog_continue, click = { positiveButton(res = R.string.dialog_continue, click = {
lifecycleScope.launch {
val path = DebugInformationDumper().exportDatabases(requireContext())
Toast.makeText( Toast.makeText(
activity, activity,
getString( getString(
R.string.debug_export_information_file, R.string.debug_export_information_file,
DebugInformationDumper().exportDatabases(requireContext()) path
), ),
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
).show() ).show()
it.dismiss() it.dismiss()
}
}) })
negativeButton(res = android.R.string.cancel, click = { negativeButton(res = android.R.string.cancel, click = {
it.cancel() it.cancel()
@ -130,8 +140,10 @@ class PreferencesAboutFragment : PreferenceFragmentCompat() {
R.anim.preference_fragment_parent_enter, R.anim.preference_fragment_parent_enter,
R.anim.preference_fragment_child_exit R.anim.preference_fragment_child_exit
) )
.replace(android.R.id.content, .replace(
PreferencesLicenseFragment(AppLicense.get(requireContext()))) android.R.id.content,
PreferencesLicenseFragment(AppLicense.get(requireContext()))
)
.addToBackStack(null) .addToBackStack(null)
.commit() .commit()
true true

View File

@ -3,8 +3,6 @@ package de.mm20.launcher2.debug
import android.content.Context import android.content.Context
import android.os.Build import android.os.Build
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.File import java.io.File
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
@ -12,33 +10,39 @@ import java.util.*
class DebugInformationDumper { class DebugInformationDumper {
fun dump(context: Context): String { suspend fun dump(context: Context): String {
val df = SimpleDateFormat("yyyy-MM-dd-HHmmss") val df = SimpleDateFormat("yyyy-MM-dd-HHmmss")
val file = File(context.getExternalFilesDir(null), "kvaesitso-log-${df.format(Date(System.currentTimeMillis()))}") 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() val fos = file.outputStream().writer()
fos.write("Device: ${Build.DEVICE}\n") fos.write("Device: ${Build.DEVICE}\n")
fos.write("SDK version: ${Build.VERSION.SDK_INT}\n") fos.write("SDK version: ${Build.VERSION.SDK_INT}\n")
fos.write("====================================\n") fos.write("====================================\n")
Thread { val input =
val input = Runtime.getRuntime().exec("/system/bin/sh -c logcat").inputStream.bufferedReader() Runtime.getRuntime().exec("/system/bin/sh -c logcat").inputStream.bufferedReader()
var line = input.readLine() var line = input.readLine()
while (line != null) { while (line != null && System.currentTimeMillis() < timeout) {
line = input.readLine() line = input.readLine()
fos.write("$line\n") fos.write("$line\n")
} }
fos.close() fos.close()
}.start() }
return file.absolutePath return file.absolutePath
} }
fun exportDatabases(context: Context): String { suspend fun exportDatabases(context: Context): String {
val df = SimpleDateFormat("yyyy-MM-dd-HHmmss") val df = SimpleDateFormat("yyyy-MM-dd-HHmmss")
val exportFile = File(context.getExternalFilesDir(null), "room-${df.format(Date(System.currentTimeMillis()))}.db") val exportFile = File(
GlobalScope.launch { context.getExternalFilesDir(null),
"room-${df.format(Date(System.currentTimeMillis()))}.db"
)
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
context.getDatabasePath("room").copyTo(exportFile) context.getDatabasePath("room").copyTo(exportFile)
} }
}
return exportFile.absolutePath return exportFile.absolutePath
} }