From 2956fa20308515df1a051954641a36659e8696da Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sat, 3 Oct 2020 11:19:46 -0400 Subject: [PATCH] Replace doasync with Kotlin coroutine Using a custom coroutine scope, we don't need to keep track of the running job. We can simply call cancel children for the scope. --- .../com/v2ray/ang/service/V2RayVpnService.kt | 6 ++- .../kotlin/com/v2ray/ang/ui/LogcatActivity.kt | 10 ++--- .../kotlin/com/v2ray/ang/ui/MainActivity.kt | 39 +++++++++++-------- .../com/v2ray/ang/ui/PerAppProxyActivity.kt | 16 +++++--- .../v2ray/ang/ui/RoutingSettingsFragment.kt | 18 ++++++--- 5 files changed, 54 insertions(+), 35 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt index 823776a1..5b213a67 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt @@ -16,7 +16,9 @@ import com.v2ray.ang.extension.defaultDPreference import com.v2ray.ang.ui.PerAppProxyActivity import com.v2ray.ang.ui.SettingsActivity import com.v2ray.ang.util.Utils -import org.jetbrains.anko.doAsync +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import java.io.File import java.lang.ref.SoftReference @@ -170,7 +172,7 @@ class V2RayVpnService : VpnService(), ServiceControl { val fd = mInterface.fileDescriptor val path = File(Utils.packagePath(applicationContext), "sock_path").absolutePath - doAsync { + GlobalScope.launch(Dispatchers.IO) { var tries = 0 while (true) try { Thread.sleep(50L shl tries) 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 e4f5205c..620ab3a1 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 @@ -11,9 +11,9 @@ import com.v2ray.ang.R import com.v2ray.ang.extension.toast import com.v2ray.ang.util.Utils import kotlinx.android.synthetic.main.activity_logcat.* -import org.jetbrains.anko.doAsync -import org.jetbrains.anko.uiThread - +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import java.io.IOException import java.util.LinkedHashSet @@ -34,7 +34,7 @@ class LogcatActivity : BaseActivity() { try { pb_waiting.visibility = View.VISIBLE - doAsync { + GlobalScope.launch(Dispatchers.Default) { if (shouldFlushLog) { val lst = LinkedHashSet() lst.add("logcat") @@ -54,7 +54,7 @@ class LogcatActivity : BaseActivity() { // InputStreamReader(process.inputStream)) // val allText = bufferedReader.use(BufferedReader::readText) val allText = process.inputStream.bufferedReader().use { it.readText() } - uiThread { + launch(Dispatchers.Main) { tv_logcat.text = allText tv_logcat.movementMethod = ScrollingMovementMethod() pb_waiting.visibility = View.GONE diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt index 04ae2f7b..8fc201d4 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt @@ -63,7 +63,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList private val adapter by lazy { MainRecyclerAdapter(this) } private var mItemTouchHelper: ItemTouchHelper? = null - private val testingJobs = ArrayList() + private val tcpingTestScope by lazy { CoroutineScope(Dispatchers.IO) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -90,9 +90,9 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList val socksPort = 10808//Utils.parseInt(defaultDPreference.getPrefString(SettingsActivity.PREF_SOCKS_PORT, "10808")) tv_test_state.text = getString(R.string.connection_test_testing) - doAsync { + GlobalScope.launch(Dispatchers.IO) { val result = Utils.testConnection(this@MainActivity, socksPort) - uiThread { + launch(Dispatchers.Main) { tv_test_state.text = Utils.getEditable(result) } } @@ -249,10 +249,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList } R.id.ping_all -> { - testingJobs.forEach { - it.cancel() - } - testingJobs.clear() + tcpingTestScope.coroutineContext[Job]?.cancelChildren() Utils.closeAllTcpSockets() for (k in 0 until configs.vmess.count()) { configs.vmess[k].testResult = "" @@ -267,16 +264,14 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList serverAddress = serverOutbound.getServerAddress() ?: continue serverPort = serverOutbound.getServerPort() ?: continue } - testingJobs.add(GlobalScope.launch(Dispatchers.IO) { + tcpingTestScope.launch { configs.vmess.getOrNull(k)?.let { // check null in case array is modified during testing it.testResult = Utils.tcping(serverAddress, serverPort) - val myJob = coroutineContext[Job] launch(Dispatchers.Main) { - testingJobs.remove(myJob) adapter.updateSelectedItem(k) } } - }) + } } true } @@ -392,9 +387,14 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList toast(R.string.toast_invalid_url) return false } - doAsync { - val configText = URL(url).readText() - uiThread { + GlobalScope.launch(Dispatchers.IO) { + val configText = try { + URL(url).readText() + } catch (e: Exception) { + e.printStackTrace() + "" + } + launch(Dispatchers.Main) { importCustomizeConfig(configText) } } @@ -426,9 +426,14 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList continue } Log.d("Main", url) - doAsync { - val configText = URL(url).readText() - uiThread { + GlobalScope.launch(Dispatchers.IO) { + val configText = try { + URL(url).readText() + } catch (e: Exception) { + e.printStackTrace() + "" + } + launch(Dispatchers.Main) { importBatchConfig(Utils.decode(configText), id) } } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/PerAppProxyActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/PerAppProxyActivity.kt index 837b26df..2b5ddd41 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/PerAppProxyActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/PerAppProxyActivity.kt @@ -29,8 +29,9 @@ import com.v2ray.ang.dto.AppInfo import com.v2ray.ang.extension.toast import com.v2ray.ang.extension.v2RayApplication import com.v2ray.ang.util.Utils -import org.jetbrains.anko.doAsync -import org.jetbrains.anko.uiThread +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import java.net.URL class PerAppProxyActivity : BaseActivity() { @@ -220,9 +221,14 @@ class PerAppProxyActivity : BaseActivity() { private fun selectProxyApp() { toast(R.string.msg_downloading_content) val url = AppConfig.androidpackagenamelistUrl - doAsync { - val content = URL(url).readText() - uiThread { + GlobalScope.launch(Dispatchers.IO) { + val content = try { + URL(url).readText() + } catch (e: Exception) { + e.printStackTrace() + "" + } + launch(Dispatchers.Main) { Log.d("selectProxyApp", content) selectProxyApp(content) toast(R.string.toast_success) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingsFragment.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingsFragment.kt index 67d88aa5..97917148 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingsFragment.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingsFragment.kt @@ -14,8 +14,9 @@ import android.view.MenuInflater import com.tbruyelle.rxpermissions.RxPermissions import com.v2ray.ang.AppConfig import com.v2ray.ang.extension.toast -import org.jetbrains.anko.doAsync -import org.jetbrains.anko.uiThread +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import java.net.URL class RoutingSettingsFragment : Fragment() { @@ -112,10 +113,15 @@ class RoutingSettingsFragment : Fragment() { } activity?.toast(R.string.msg_downloading_content) - doAsync { - val content = URL(url).readText() - uiThread { - et_routing_content.text = Utils.getEditable(content!!) + GlobalScope.launch(Dispatchers.IO) { + val content = try { + URL(url).readText() + } catch (e: Exception) { + e.printStackTrace() + "" + } + launch(Dispatchers.Main) { + et_routing_content.text = Utils.getEditable(content) activity?.toast(R.string.toast_success) } }