diff options
4 files changed, 74 insertions, 27 deletions
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<Event<String>>() val transactionsEvent: LiveData<Event<String>> = 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 <http://www.gnu.org/licenses/> + */ + +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 <string name="settings_withdraw_testkudos_summary">Get money for testing</string> <string name="settings_logcat">Debug log</string> <string name="settings_logcat_summary">View/send internal log</string> + <string name="settings_logcat_error">Error exporting log</string> + <string name="settings_logcat_success">Log exported to file</string> <string name="settings_version_app">App Version</string> <string name="settings_version_core">Wallet Core Version</string> <string name="settings_version_protocol_exchange">Supported Exchange Versions</string> |