Add button to export logs

This commit is contained in:
ByteHamster 2021-10-22 19:46:55 +02:00 committed by cketti
parent 3887681d58
commit 5e741a4d56
3 changed files with 60 additions and 1 deletions

View file

@ -2,18 +2,31 @@ package com.fsck.k9.ui.settings.general
import android.os.Build
import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.lifecycleScope
import androidx.preference.ListPreference
import com.fsck.k9.ui.R
import com.fsck.k9.ui.withArguments
import com.google.android.material.snackbar.Snackbar
import com.takisoft.preferencex.PreferenceFragmentCompat
import java.io.IOException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.apache.commons.io.IOUtils
import org.koin.android.ext.android.inject
class GeneralSettingsFragment : PreferenceFragmentCompat() {
private val dataStore: GeneralSettingsDataStore by inject()
private var rootKey: String? = null
override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.preferenceDataStore = dataStore
this.rootKey = rootKey
setHasOptionsMenu(true)
setPreferencesFromResource(R.xml.general_settings, rootKey)
initializeTheme()
@ -24,6 +37,40 @@ class GeneralSettingsFragment : PreferenceFragmentCompat() {
activity?.title = preferenceScreen.title
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
if (rootKey == "debug_preferences") {
inflater.inflate(R.menu.debug_settings_option, menu)
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == R.id.exportLogs) {
exportLogsResultContract.launch("k9mail-logs.txt")
return true
}
return super.onOptionsItemSelected(item)
}
private val exportLogsResultContract = registerForActivityResult(ActivityResultContracts.CreateDocument()) { uri ->
if (uri != null) {
lifecycleScope.launch(Dispatchers.IO) {
val message = try {
requireContext().contentResolver.openOutputStream(uri).use { outputFile ->
Runtime.getRuntime().exec("logcat -d").inputStream.use { logOutput ->
IOUtils.copy(logOutput, outputFile)
}
}
getString(R.string.debug_export_logs_success)
} catch (e: IOException) {
e.message.toString()
}
withContext(Dispatchers.Main) {
Snackbar.make(requireView(), message, Snackbar.LENGTH_LONG).show()
}
}
}
}
private fun initializeTheme() {
(findPreference(PREFERENCE_THEME) as? ListPreference)?.apply {
if (Build.VERSION.SDK_INT < 28) {

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/exportLogs"
android:title="@string/debug_export_logs_title"
app:showAsAction="never" />
</menu>

View file

@ -245,6 +245,8 @@ Please submit bug reports, contribute new features and ask questions at
<string name="debug_enable_debug_logging_summary">Log extra diagnostic information</string>
<string name="debug_enable_sensitive_logging_title">Log sensitive information</string>
<string name="debug_enable_sensitive_logging_summary">May show passwords in logs.</string>
<string name="debug_export_logs_title">Export logs</string>
<string name="debug_export_logs_success">Export successful. Logs might contain sensitive information. Be careful who you send them to.</string>
<string name="message_list_load_more_messages_action">Load more messages</string>
<string name="message_to_fmt">To:<xliff:g id="counterParty">%s</xliff:g></string>