Commit b634ef0d authored by Mygod's avatar Mygod

Name threads for better debugging experience

Also removes an unnecessary thread.
parent 32d64222
...@@ -155,7 +155,7 @@ class AppManager : AppCompatActivity(), Toolbar.OnMenuItemClickListener { ...@@ -155,7 +155,7 @@ class AppManager : AppCompatActivity(), Toolbar.OnMenuItemClickListener {
appListView.visibility = View.GONE appListView.visibility = View.GONE
fastScroller.visibility = View.GONE fastScroller.visibility = View.GONE
loadingView.visibility = View.VISIBLE loadingView.visibility = View.VISIBLE
thread { thread("AppManager-loader") {
val adapter = appListView.adapter as AppsAdapter val adapter = appListView.adapter as AppsAdapter
do { do {
appsLoading.set(true) appsLoading.set(true)
......
...@@ -266,18 +266,20 @@ class MainActivity : AppCompatActivity(), ShadowsocksConnection.Interface, Drawe ...@@ -266,18 +266,20 @@ class MainActivity : AppCompatActivity(), ShadowsocksConnection.Interface, Drawe
++testCount ++testCount
statusText.setText(R.string.connection_test_testing) statusText.setText(R.string.connection_test_testing)
val id = testCount // it would change by other code val id = testCount // it would change by other code
thread { testConnection(id) } thread("ConnectionTest") { testConnection(id) }
} }
} }
fab = findViewById(R.id.fab) fab = findViewById(R.id.fab)
fab.setOnClickListener { fab.setOnClickListener {
if (state == BaseService.CONNECTED) app.stopService() else thread { when {
if (BaseService.usingVpnMode) { state == BaseService.CONNECTED -> app.stopService()
BaseService.usingVpnMode -> {
val intent = VpnService.prepare(this) val intent = VpnService.prepare(this)
if (intent != null) startActivityForResult(intent, REQUEST_CONNECT) if (intent != null) startActivityForResult(intent, REQUEST_CONNECT)
else app.handler.post { onActivityResult(REQUEST_CONNECT, Activity.RESULT_OK, null) } else onActivityResult(REQUEST_CONNECT, Activity.RESULT_OK, null)
} else app.startService() }
else -> app.startService()
} }
} }
......
...@@ -341,7 +341,7 @@ object BaseService { ...@@ -341,7 +341,7 @@ object BaseService {
data.changeState(CONNECTING) data.changeState(CONNECTING)
thread { thread("$tag-Connecting") {
try { try {
if (profile.host == "198.199.101.152") { if (profile.host == "198.199.101.152") {
val client = OkHttpClient.Builder() val client = OkHttpClient.Builder()
......
...@@ -28,6 +28,7 @@ import com.github.shadowsocks.BuildConfig ...@@ -28,6 +28,7 @@ import com.github.shadowsocks.BuildConfig
import com.github.shadowsocks.JniHelper import com.github.shadowsocks.JniHelper
import com.github.shadowsocks.utils.Commandline import com.github.shadowsocks.utils.Commandline
import com.github.shadowsocks.utils.thread import com.github.shadowsocks.utils.thread
import java.io.File
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
import java.util.concurrent.Semaphore import java.util.concurrent.Semaphore
...@@ -42,8 +43,9 @@ class GuardedProcess(private val cmd: List<String>) { ...@@ -42,8 +43,9 @@ class GuardedProcess(private val cmd: List<String>) {
private var isDestroyed = false private var isDestroyed = false
@Volatile @Volatile
private lateinit var process: Process private lateinit var process: Process
private val name = File(cmd.first()).nameWithoutExtension
private fun streamLogger(input: InputStream, logger: (String, String) -> Int) = thread { private fun streamLogger(input: InputStream, logger: (String, String) -> Int) = thread("StreamLogger-$name") {
try { try {
input.bufferedReader().useLines { it.forEach { logger(TAG, it) } } input.bufferedReader().useLines { it.forEach { logger(TAG, it) } }
} catch (_: IOException) { } // ignore } catch (_: IOException) { } // ignore
...@@ -53,7 +55,7 @@ class GuardedProcess(private val cmd: List<String>) { ...@@ -53,7 +55,7 @@ class GuardedProcess(private val cmd: List<String>) {
val semaphore = Semaphore(1) val semaphore = Semaphore(1)
semaphore.acquire() semaphore.acquire()
var ioException: IOException? = null var ioException: IOException? = null
guardThread = thread(name = "GuardThread-" + cmd.first()) { guardThread = thread("GuardThread-$name") {
try { try {
var callback: (() -> Unit)? = null var callback: (() -> Unit)? = null
while (!isDestroyed) { while (!isDestroyed) {
......
...@@ -28,7 +28,7 @@ import com.github.shadowsocks.App.Companion.app ...@@ -28,7 +28,7 @@ import com.github.shadowsocks.App.Companion.app
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
abstract class LocalSocketListener(protected val tag: String) : Thread() { abstract class LocalSocketListener(protected val tag: String) : Thread(tag) {
init { init {
setUncaughtExceptionHandler(app::track) setUncaughtExceptionHandler(app::track)
} }
......
...@@ -52,5 +52,5 @@ object TcpFastOpen { ...@@ -52,5 +52,5 @@ object TcpFastOpen {
"else", "else",
" echo Failed.", " echo Failed.",
"fi"), null, true)?.joinToString("\n") "fi"), null, true)?.joinToString("\n")
fun enabledAsync(value: Boolean) = thread { enabled(value) }.join(1000) fun enabledAsync(value: Boolean) = thread("TcpFastOpen") { enabled(value) }.join(1000)
} }
...@@ -39,8 +39,8 @@ fun broadcastReceiver(callback: (Context, Intent) -> Unit): BroadcastReceiver = ...@@ -39,8 +39,8 @@ fun broadcastReceiver(callback: (Context, Intent) -> Unit): BroadcastReceiver =
/** /**
* Wrapper for kotlin.concurrent.thread that tracks uncaught exceptions. * Wrapper for kotlin.concurrent.thread that tracks uncaught exceptions.
*/ */
fun thread(start: Boolean = true, isDaemon: Boolean = false, contextClassLoader: ClassLoader? = null, fun thread(name: String? = null, start: Boolean = true, isDaemon: Boolean = false,
name: String? = null, priority: Int = -1, block: () -> Unit): Thread { contextClassLoader: ClassLoader? = null, priority: Int = -1, block: () -> Unit): Thread {
val thread = kotlin.concurrent.thread(false, isDaemon, contextClassLoader, name, priority, block) val thread = kotlin.concurrent.thread(false, isDaemon, contextClassLoader, name, priority, block)
thread.setUncaughtExceptionHandler(app::track) thread.setUncaughtExceptionHandler(app::track)
if (start) thread.start() if (start) thread.start()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment