From 9612b868f22c0c48baca98b398035f80e08d5c5b Mon Sep 17 00:00:00 2001 From: Tamim Hossain <132823494+CodeWithTamim@users.noreply.github.com> Date: Fri, 25 Oct 2024 16:56:59 +0600 Subject: [PATCH] Improve Error Handling in LogcatActivity (#3770) Refined the error handling in the logcat function of LogcatActivity by ensuring that the progress bar is hidden in both success and failure cases. Added user-friendly error messages using toast and cleaned up the code by using Kotlin's linkedSetOf for the logcat command list. --- .../kotlin/com/v2ray/ang/ui/LogcatActivity.kt | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/LogcatActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/LogcatActivity.kt index c71294eb..7e424efc 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/LogcatActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/LogcatActivity.kt @@ -33,46 +33,42 @@ class LogcatActivity : BaseActivity() { } private fun logcat(shouldFlushLog: Boolean) { + binding.pbWaiting.visibility = View.VISIBLE - try { - binding.pbWaiting.visibility = View.VISIBLE - - lifecycleScope.launch(Dispatchers.Default) { + lifecycleScope.launch(Dispatchers.Default) { + try { if (shouldFlushLog) { - val lst = LinkedHashSet() - lst.add("logcat") - lst.add("-c") + val lst = linkedSetOf("logcat", "-c") withContext(Dispatchers.IO) { val process = Runtime.getRuntime().exec(lst.toTypedArray()) process.waitFor() } } - val lst = LinkedHashSet() - lst.add("logcat") - lst.add("-d") - lst.add("-v") - lst.add("time") - lst.add("-s") - lst.add("GoLog,tun2socks,${ANG_PACKAGE},AndroidRuntime,System.err") + val lst = linkedSetOf( + "logcat", "-d", "-v", "time", "-s", + "GoLog,tun2socks,$ANG_PACKAGE,AndroidRuntime,System.err" + ) val process = withContext(Dispatchers.IO) { Runtime.getRuntime().exec(lst.toTypedArray()) } -// val bufferedReader = BufferedReader( -// InputStreamReader(process.inputStream)) -// val allText = bufferedReader.use(BufferedReader::readText) val allText = process.inputStream.bufferedReader().use { it.readText() } - launch(Dispatchers.Main) { + withContext(Dispatchers.Main) { binding.tvLogcat.text = allText binding.tvLogcat.movementMethod = ScrollingMovementMethod() binding.pbWaiting.visibility = View.GONE Handler(Looper.getMainLooper()).post { binding.svLogcat.fullScroll(View.FOCUS_DOWN) } } + } catch (e: IOException) { + withContext(Dispatchers.Main) { + binding.pbWaiting.visibility = View.GONE + toast(R.string.toast_failure) + } + e.printStackTrace() } - } catch (e: IOException) { - e.printStackTrace() } } + override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.menu_logcat, menu) return super.onCreateOptionsMenu(menu)