From 5d04c4e85035965f0b52fa65d35a88679233f8c2 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Tue, 27 Sep 2022 11:54:06 -0300 Subject: [wallet] re-implement log export very simple, just export to file for now --- .../main/java/net/taler/wallet/MainViewModel.kt | 2 + .../net/taler/wallet/settings/SettingsFragment.kt | 35 +++--------- .../net/taler/wallet/settings/SettingsManager.kt | 62 ++++++++++++++++++++++ wallet/src/main/res/values/strings.xml | 2 + 4 files changed, 74 insertions(+), 27 deletions(-) create mode 100644 wallet/src/main/java/net/taler/wallet/settings/SettingsManager.kt diff --git a/wallet/src/main/java/net/taler/wallet/MainViewModel.kt b/wallet/src/main/java/net/taler/wallet/MainViewModel.kt index 99ac1f9..4dbea50 100644 --- a/wallet/src/main/java/net/taler/wallet/MainViewModel.kt +++ b/wallet/src/main/java/net/taler/wallet/MainViewModel.kt @@ -37,6 +37,7 @@ import net.taler.wallet.payment.PaymentManager import net.taler.wallet.peer.PeerManager import net.taler.wallet.pending.PendingOperationsManager import net.taler.wallet.refund.RefundManager +import net.taler.wallet.settings.SettingsManager import net.taler.wallet.tip.TipManager import net.taler.wallet.transactions.TransactionManager import net.taler.wallet.withdraw.WithdrawManager @@ -95,6 +96,7 @@ class MainViewModel(val app: Application) : AndroidViewModel(app) { val refundManager = RefundManager(api, viewModelScope) val exchangeManager: ExchangeManager = ExchangeManager(api, viewModelScope) val peerManager: PeerManager = PeerManager(api, viewModelScope) + val settingsManager: SettingsManager = SettingsManager(app.applicationContext, viewModelScope) private val mTransactionsEvent = MutableLiveData>() val transactionsEvent: LiveData> = mTransactionsEvent diff --git a/wallet/src/main/java/net/taler/wallet/settings/SettingsFragment.kt b/wallet/src/main/java/net/taler/wallet/settings/SettingsFragment.kt index 0435985..ecf45a6 100644 --- a/wallet/src/main/java/net/taler/wallet/settings/SettingsFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/settings/SettingsFragment.kt @@ -18,7 +18,7 @@ package net.taler.wallet.settings import android.os.Bundle import android.view.View -import android.widget.Toast +import androidx.activity.result.contract.ActivityResultContracts.CreateDocument import androidx.appcompat.app.AlertDialog import androidx.fragment.app.activityViewModels import androidx.preference.Preference @@ -35,11 +35,13 @@ import net.taler.wallet.BuildConfig.WALLET_CORE_VERSION import net.taler.wallet.MainViewModel import net.taler.wallet.R import net.taler.wallet.withdraw.WithdrawTestStatus +import java.lang.System.currentTimeMillis class SettingsFragment : PreferenceFragmentCompat() { private val model: MainViewModel by activityViewModels() + private val settingsManager get() = model.settingsManager private val withdrawManager by lazy { model.withdrawManager } private lateinit var prefBackup: Preference @@ -64,6 +66,10 @@ class SettingsFragment : PreferenceFragmentCompat() { ) } + private val logLauncher = registerForActivityResult(CreateDocument("text/plain")) { uri -> + settingsManager.exportLogcat(uri) + } + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.settings_main, rootKey) prefBackup = findPreference("pref_backup")!! @@ -116,32 +122,7 @@ class SettingsFragment : PreferenceFragmentCompat() { } prefLogcat.setOnPreferenceClickListener { - val toast = - Toast.makeText(requireActivity(), - "Log export currently unavailable", - Toast.LENGTH_LONG) - toast.show() - -// val myPid = android.os.Process.myPid() -// val proc = Runtime.getRuntime() -// .exec(arrayOf("logcat", "-d", "--pid=$myPid", "*:V")) -// val bytes = proc.inputStream.readBytes() -// val f = File(requireActivity().getExternalFilesDir(null), -// "taler-wallet-log-${System.currentTimeMillis()}.txt") -// f.writeBytes(bytes) -// val toast = Toast.makeText(requireActivity(), "Saved to ${f.absolutePath}", Toast.LENGTH_LONG) -// toast.show() -// val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply { -// addCategory(Intent.CATEGORY_OPENABLE) -// type = "application/pdf" -// putExtra(Intent.EXTRA_TITLE, "invoice.pdf") -// -// // Optionally, specify a URI for the directory that should be opened in -// // the system file picker before your app creates the document. -// putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri) -// } -// startActivityForResult(intent, CREATE_FILE) -// ActivityResultContracts.CreateDocument + logLauncher.launch("taler-wallet-log-${currentTimeMillis()}.txt") true } diff --git a/wallet/src/main/java/net/taler/wallet/settings/SettingsManager.kt b/wallet/src/main/java/net/taler/wallet/settings/SettingsManager.kt new file mode 100644 index 0000000..6bf1673 --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/settings/SettingsManager.kt @@ -0,0 +1,62 @@ +/* + * This file is part of GNU Taler + * (C) 2022 Taler Systems S.A. + * + * GNU Taler is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3, or (at your option) any later version. + * + * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * GNU Taler; see the file COPYING. If not, see + */ + +package net.taler.wallet.settings + +import android.content.Context +import android.net.Uri +import android.util.Log +import android.widget.Toast +import android.widget.Toast.LENGTH_LONG +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import net.taler.wallet.R + +class SettingsManager( + private val context: Context, + private val scope: CoroutineScope, +) { + + fun exportLogcat(uri: Uri?) { + if (uri == null) { + onLogExportError() + return + } + scope.launch(Dispatchers.IO) { + try { + context.contentResolver.openOutputStream(uri, "wt")?.use { outputStream -> + val command = arrayOf("logcat", "-d", "*:V") + val proc = Runtime.getRuntime().exec(command) + proc.inputStream.copyTo(outputStream) + } ?: onLogExportError() + } catch (e: Exception) { + Log.e(SettingsManager::class.simpleName, "Error exporting log: ", e) + onLogExportError() + return@launch + } + withContext(Dispatchers.Main) { + Toast.makeText(context, R.string.settings_logcat_success, LENGTH_LONG).show() + } + } + } + + private fun onLogExportError() { + Toast.makeText(context, R.string.settings_logcat_error, LENGTH_LONG).show() + } + +} diff --git a/wallet/src/main/res/values/strings.xml b/wallet/src/main/res/values/strings.xml index 6d5f554..9fbe564 100644 --- a/wallet/src/main/res/values/strings.xml +++ b/wallet/src/main/res/values/strings.xml @@ -204,6 +204,8 @@ GNU Taler is immune against many types of fraud, such as phishing of credit card Get money for testing Debug log View/send internal log + Error exporting log + Log exported to file App Version Wallet Core Version Supported Exchange Versions -- cgit v1.2.3