diff --git a/mobile/src/main/java/com/github/shadowsocks/AppManager.kt b/mobile/src/main/java/com/github/shadowsocks/AppManager.kt index 415fa59b..3d8cca65 100644 --- a/mobile/src/main/java/com/github/shadowsocks/AppManager.kt +++ b/mobile/src/main/java/com/github/shadowsocks/AppManager.kt @@ -155,7 +155,7 @@ class AppManager : AppCompatActivity(), Toolbar.OnMenuItemClickListener { appListView.visibility = View.GONE fastScroller.visibility = View.GONE loadingView.visibility = View.VISIBLE - thread { + thread("AppManager-loader") { val adapter = appListView.adapter as AppsAdapter do { appsLoading.set(true) diff --git a/mobile/src/main/java/com/github/shadowsocks/MainActivity.kt b/mobile/src/main/java/com/github/shadowsocks/MainActivity.kt index c027ecbf..c1c2c3af 100644 --- a/mobile/src/main/java/com/github/shadowsocks/MainActivity.kt +++ b/mobile/src/main/java/com/github/shadowsocks/MainActivity.kt @@ -266,18 +266,20 @@ class MainActivity : AppCompatActivity(), ShadowsocksConnection.Interface, Drawe ++testCount statusText.setText(R.string.connection_test_testing) val id = testCount // it would change by other code - thread { testConnection(id) } + thread("ConnectionTest") { testConnection(id) } } } fab = findViewById(R.id.fab) fab.setOnClickListener { - if (state == BaseService.CONNECTED) app.stopService() else thread { - if (BaseService.usingVpnMode) { + when { + state == BaseService.CONNECTED -> app.stopService() + BaseService.usingVpnMode -> { val intent = VpnService.prepare(this) if (intent != null) startActivityForResult(intent, REQUEST_CONNECT) - else app.handler.post { onActivityResult(REQUEST_CONNECT, Activity.RESULT_OK, null) } - } else app.startService() + else onActivityResult(REQUEST_CONNECT, Activity.RESULT_OK, null) + } + else -> app.startService() } } diff --git a/mobile/src/main/java/com/github/shadowsocks/bg/BaseService.kt b/mobile/src/main/java/com/github/shadowsocks/bg/BaseService.kt index b31511bb..7f0d94b4 100644 --- a/mobile/src/main/java/com/github/shadowsocks/bg/BaseService.kt +++ b/mobile/src/main/java/com/github/shadowsocks/bg/BaseService.kt @@ -341,7 +341,7 @@ object BaseService { data.changeState(CONNECTING) - thread { + thread("$tag-Connecting") { try { if (profile.host == "198.199.101.152") { val client = OkHttpClient.Builder() diff --git a/mobile/src/main/java/com/github/shadowsocks/bg/GuardedProcess.kt b/mobile/src/main/java/com/github/shadowsocks/bg/GuardedProcess.kt index 0ea94155..ddb1e91c 100644 --- a/mobile/src/main/java/com/github/shadowsocks/bg/GuardedProcess.kt +++ b/mobile/src/main/java/com/github/shadowsocks/bg/GuardedProcess.kt @@ -28,6 +28,7 @@ import com.github.shadowsocks.BuildConfig import com.github.shadowsocks.JniHelper import com.github.shadowsocks.utils.Commandline import com.github.shadowsocks.utils.thread +import java.io.File import java.io.IOException import java.io.InputStream import java.util.concurrent.Semaphore @@ -42,8 +43,9 @@ class GuardedProcess(private val cmd: List) { private var isDestroyed = false @Volatile 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 { input.bufferedReader().useLines { it.forEach { logger(TAG, it) } } } catch (_: IOException) { } // ignore @@ -53,7 +55,7 @@ class GuardedProcess(private val cmd: List) { val semaphore = Semaphore(1) semaphore.acquire() var ioException: IOException? = null - guardThread = thread(name = "GuardThread-" + cmd.first()) { + guardThread = thread("GuardThread-$name") { try { var callback: (() -> Unit)? = null while (!isDestroyed) { diff --git a/mobile/src/main/java/com/github/shadowsocks/bg/LocalSocketListener.kt b/mobile/src/main/java/com/github/shadowsocks/bg/LocalSocketListener.kt index 96512c9e..9faf4cff 100644 --- a/mobile/src/main/java/com/github/shadowsocks/bg/LocalSocketListener.kt +++ b/mobile/src/main/java/com/github/shadowsocks/bg/LocalSocketListener.kt @@ -28,7 +28,7 @@ import com.github.shadowsocks.App.Companion.app import java.io.File import java.io.IOException -abstract class LocalSocketListener(protected val tag: String) : Thread() { +abstract class LocalSocketListener(protected val tag: String) : Thread(tag) { init { setUncaughtExceptionHandler(app::track) } diff --git a/mobile/src/main/java/com/github/shadowsocks/utils/TcpFastOpen.kt b/mobile/src/main/java/com/github/shadowsocks/utils/TcpFastOpen.kt index b2f036ea..60901b4f 100644 --- a/mobile/src/main/java/com/github/shadowsocks/utils/TcpFastOpen.kt +++ b/mobile/src/main/java/com/github/shadowsocks/utils/TcpFastOpen.kt @@ -52,5 +52,5 @@ object TcpFastOpen { "else", " echo Failed.", "fi"), null, true)?.joinToString("\n") - fun enabledAsync(value: Boolean) = thread { enabled(value) }.join(1000) + fun enabledAsync(value: Boolean) = thread("TcpFastOpen") { enabled(value) }.join(1000) } diff --git a/mobile/src/main/java/com/github/shadowsocks/utils/Utils.kt b/mobile/src/main/java/com/github/shadowsocks/utils/Utils.kt index f8d3e66f..4038c334 100644 --- a/mobile/src/main/java/com/github/shadowsocks/utils/Utils.kt +++ b/mobile/src/main/java/com/github/shadowsocks/utils/Utils.kt @@ -39,8 +39,8 @@ fun broadcastReceiver(callback: (Context, Intent) -> Unit): BroadcastReceiver = /** * Wrapper for kotlin.concurrent.thread that tracks uncaught exceptions. */ -fun thread(start: Boolean = true, isDaemon: Boolean = false, contextClassLoader: ClassLoader? = null, - name: String? = null, priority: Int = -1, block: () -> Unit): Thread { +fun thread(name: String? = null, start: Boolean = true, isDaemon: Boolean = false, + contextClassLoader: ClassLoader? = null, priority: Int = -1, block: () -> Unit): Thread { val thread = kotlin.concurrent.thread(false, isDaemon, contextClassLoader, name, priority, block) thread.setUncaughtExceptionHandler(app::track) if (start) thread.start()