Skip to content

Commit 41bc064

Browse files
author
886963226
authored
Optimization logcat after change quickie-foss (#4171)
After changed quickie-foss, scanning cause lot of log. throttle log com.google.zxing.NotFoundException.
1 parent eb8562e commit 41bc064

File tree

1 file changed

+67
-12
lines changed

1 file changed

+67
-12
lines changed

V2rayNG/app/src/main/java/com/v2ray/ang/ui/LogcatActivity.kt

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,42 @@ import kotlinx.coroutines.withContext
1919
import java.io.IOException
2020

2121
class LogcatActivity : BaseActivity() {
22-
private val binding by lazy {
23-
ActivityLogcatBinding.inflate(layoutInflater)
24-
}
22+
private val binding by lazy { ActivityLogcatBinding.inflate(layoutInflater) }
23+
private val throttleManager = ThrottleManager()
2524

2625
override fun onCreate(savedInstanceState: Bundle?) {
2726
super.onCreate(savedInstanceState)
2827
setContentView(binding.root)
29-
3028
title = getString(R.string.title_logcat)
31-
3229
logcat(false)
3330
}
3431

32+
class ThrottleManager {
33+
private val throttleMap = mutableMapOf<String, Long>()
34+
35+
companion object {
36+
private const val THROTTLE_DURATION = 1000L
37+
}
38+
39+
@Synchronized
40+
fun shouldProcess(key: String): Boolean {
41+
val currentTime = System.currentTimeMillis()
42+
val lastProcessTime = throttleMap[key] ?: 0L
43+
44+
return if (currentTime - lastProcessTime > THROTTLE_DURATION) {
45+
throttleMap[key] = currentTime
46+
true
47+
} else {
48+
false
49+
}
50+
}
51+
52+
@Synchronized
53+
fun reset(key: String) {
54+
throttleMap.remove(key)
55+
}
56+
}
57+
3558
private fun logcat(shouldFlushLog: Boolean) {
3659
binding.pbWaiting.visibility = View.VISIBLE
3760

@@ -44,20 +67,23 @@ class LogcatActivity : BaseActivity() {
4467
process.waitFor()
4568
}
4669
}
70+
4771
val lst = linkedSetOf(
4872
"logcat", "-d", "-v", "time", "-s",
4973
"GoLog,tun2socks,$ANG_PACKAGE,AndroidRuntime,System.err"
5074
)
75+
5176
val process = withContext(Dispatchers.IO) {
5277
Runtime.getRuntime().exec(lst.toTypedArray())
5378
}
54-
val allText = process.inputStream.bufferedReader().use { it.readText() }
79+
80+
val allLogs = process.inputStream.bufferedReader().use { it.readLines() }
81+
val filteredLogs = processLogs(allLogs)
82+
5583
withContext(Dispatchers.Main) {
56-
binding.tvLogcat.text = allText
57-
binding.tvLogcat.movementMethod = ScrollingMovementMethod()
58-
binding.pbWaiting.visibility = View.GONE
59-
Handler(Looper.getMainLooper()).post { binding.svLogcat.fullScroll(View.FOCUS_DOWN) }
84+
updateLogDisplay(filteredLogs)
6085
}
86+
6187
} catch (e: IOException) {
6288
withContext(Dispatchers.Main) {
6389
binding.pbWaiting.visibility = View.GONE
@@ -68,6 +94,36 @@ class LogcatActivity : BaseActivity() {
6894
}
6995
}
7096

97+
private fun processLogs(logs: List<String>): List<String> {
98+
val processedLogs = mutableListOf<String>()
99+
var isNotMatch = false
100+
101+
for (line in logs) {
102+
when {
103+
line.contains("zxing.NotFoundException", ignoreCase = true) -> {
104+
if (!isNotMatch) {
105+
if (throttleManager.shouldProcess("NotFoundException")) {
106+
processedLogs.add(line)
107+
isNotMatch = true
108+
}
109+
}
110+
}
111+
else -> processedLogs.add(line)
112+
}
113+
}
114+
115+
return processedLogs.take(500)
116+
}
117+
118+
private fun updateLogDisplay(logs: List<String>) {
119+
binding.tvLogcat.text = logs.joinToString("\n")
120+
binding.tvLogcat.movementMethod = ScrollingMovementMethod()
121+
binding.pbWaiting.visibility = View.GONE
122+
123+
Handler(Looper.getMainLooper()).post {
124+
binding.svLogcat.fullScroll(View.FOCUS_DOWN)
125+
}
126+
}
71127

72128
override fun onCreateOptionsMenu(menu: Menu): Boolean {
73129
menuInflater.inflate(R.menu.menu_logcat, menu)
@@ -80,12 +136,11 @@ class LogcatActivity : BaseActivity() {
80136
toast(R.string.toast_success)
81137
true
82138
}
83-
84139
R.id.clear_all -> {
140+
throttleManager.reset("zxing.NotFoundException")
85141
logcat(true)
86142
true
87143
}
88-
89144
else -> super.onOptionsItemSelected(item)
90145
}
91146
}

0 commit comments

Comments
 (0)