From c37f09bfcdba648112d1fe31db8dd42650de929c Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 7 Jan 2025 14:03:26 +0800 Subject: [PATCH] Fix logcat --- .../java/com/v2ray/ang/ui/LogcatActivity.kt | 113 +++++------------- 1 file changed, 31 insertions(+), 82 deletions(-) diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/LogcatActivity.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/LogcatActivity.kt index 83f45d77..c71294eb 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/LogcatActivity.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/LogcatActivity.kt @@ -19,109 +19,57 @@ import kotlinx.coroutines.withContext import java.io.IOException class LogcatActivity : BaseActivity() { - private val binding by lazy { ActivityLogcatBinding.inflate(layoutInflater) } - private val throttleManager = ThrottleManager() + private val binding by lazy { + ActivityLogcatBinding.inflate(layoutInflater) + } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(binding.root) + title = getString(R.string.title_logcat) + logcat(false) } - class ThrottleManager { - private val throttleMap = mutableMapOf() - - companion object { - private const val THROTTLE_DURATION = 1000L - } - - @Synchronized - fun shouldProcess(key: String): Boolean { - val currentTime = System.currentTimeMillis() - val lastProcessTime = throttleMap[key] ?: 0L - - return if (currentTime - lastProcessTime > THROTTLE_DURATION) { - throttleMap[key] = currentTime - true - } else { - false - } - } - - @Synchronized - fun reset(key: String) { - throttleMap.remove(key) - } - } - private fun logcat(shouldFlushLog: Boolean) { - binding.pbWaiting.visibility = View.VISIBLE - lifecycleScope.launch(Dispatchers.Default) { - try { + try { + binding.pbWaiting.visibility = View.VISIBLE + + lifecycleScope.launch(Dispatchers.Default) { if (shouldFlushLog) { - val lst = linkedSetOf("logcat", "-c") + val lst = LinkedHashSet() + lst.add("logcat") + lst.add("-c") withContext(Dispatchers.IO) { val process = Runtime.getRuntime().exec(lst.toTypedArray()) process.waitFor() } } - - val lst = linkedSetOf( - "logcat", "-d", "-v", "time", "-s", - "GoLog,tun2socks,$ANG_PACKAGE,AndroidRuntime,System.err" - ) - + 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 process = withContext(Dispatchers.IO) { Runtime.getRuntime().exec(lst.toTypedArray()) } - - val allLogs = process.inputStream.bufferedReader().use { it.readLines() } - val filteredLogs = processLogs(allLogs) - - withContext(Dispatchers.Main) { - updateLogDisplay(filteredLogs) - } - - } catch (e: IOException) { - withContext(Dispatchers.Main) { +// val bufferedReader = BufferedReader( +// InputStreamReader(process.inputStream)) +// val allText = bufferedReader.use(BufferedReader::readText) + val allText = process.inputStream.bufferedReader().use { it.readText() } + launch(Dispatchers.Main) { + binding.tvLogcat.text = allText + binding.tvLogcat.movementMethod = ScrollingMovementMethod() binding.pbWaiting.visibility = View.GONE - toast(R.string.toast_failure) + Handler(Looper.getMainLooper()).post { binding.svLogcat.fullScroll(View.FOCUS_DOWN) } } - e.printStackTrace() } - } - } - - private fun processLogs(logs: List): List { - val processedLogs = mutableListOf() - var isNotMatch = false - - for (line in logs) { - when { - line.contains("zxing.NotFoundException", ignoreCase = true) -> { - if (!isNotMatch) { - if (throttleManager.shouldProcess("NotFoundException")) { - processedLogs.add(line) - isNotMatch = true - } - } - } - else -> processedLogs.add(line) - } - } - - return processedLogs.take(500) - } - - private fun updateLogDisplay(logs: List) { - binding.tvLogcat.text = logs.joinToString("\n") - binding.tvLogcat.movementMethod = ScrollingMovementMethod() - binding.pbWaiting.visibility = View.GONE - - Handler(Looper.getMainLooper()).post { - binding.svLogcat.fullScroll(View.FOCUS_DOWN) + } catch (e: IOException) { + e.printStackTrace() } } @@ -136,11 +84,12 @@ class LogcatActivity : BaseActivity() { toast(R.string.toast_success) true } + R.id.clear_all -> { - throttleManager.reset("zxing.NotFoundException") logcat(true) true } + else -> super.onOptionsItemSelected(item) } }