diff --git a/V2rayNG/app/build.gradle b/V2rayNG/app/build.gradle index a1ff9805..06356c83 100644 --- a/V2rayNG/app/build.gradle +++ b/V2rayNG/app/build.gradle @@ -84,6 +84,10 @@ dependencies { implementation project(':dpreference') implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion" + implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.2" + implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.2" // 1.3.x has compile error: + // More than one file was found with OS independent path 'META-INF/proguard/coroutines.pro' + // Android support library implementation "com.android.support:support-v4:$supportLibVersion" implementation "com.android.support:appcompat-v7:$supportLibVersion" 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 045f681e..a872572c 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 @@ -33,6 +33,7 @@ import rx.android.schedulers.AndroidSchedulers import java.util.concurrent.TimeUnit import com.v2ray.ang.helper.SimpleItemTouchHelperCallback import com.v2ray.ang.util.AngConfigManager.configs +import kotlinx.coroutines.* class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedListener { companion object { @@ -58,6 +59,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList private val adapter by lazy { MainRecyclerAdapter(this) } private var mItemTouchHelper: ItemTouchHelper? = null + private val testingJobs = ArrayList() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -240,18 +242,25 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList } R.id.ping_all -> { + testingJobs.forEach { + it.cancel() + } + testingJobs.clear() + Utils.closeAllTcpSockets() for (k in 0 until configs.vmess.count()) { configs.vmess[k].testResult = "" adapter.updateConfigList() } for (k in 0 until configs.vmess.count()) { if (configs.vmess[k].configType != AppConfig.EConfigType.Custom) { - doAsync { + testingJobs.add(GlobalScope.launch(Dispatchers.IO) { configs.vmess[k].testResult = Utils.tcping(configs.vmess[k].address, configs.vmess[k].port) - uiThread { + val myJob = coroutineContext[Job] + launch(Dispatchers.Main) { + testingJobs.remove(myJob) adapter.updateSelectedItem(k) } - } + }) } } true @@ -570,4 +579,4 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList drawer_layout.closeDrawer(GravityCompat.START) return true } -} \ No newline at end of file +} diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt index b986add4..e50a0075 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt @@ -31,6 +31,7 @@ import com.v2ray.ang.extension.v2RayApplication import com.v2ray.ang.service.V2RayVpnService import com.v2ray.ang.ui.SettingsActivity import kotlinx.android.synthetic.main.activity_logcat.* +import kotlinx.coroutines.isActive import me.dozen.dpreference.DPreference import org.jetbrains.anko.toast import org.jetbrains.anko.uiThread @@ -44,10 +45,13 @@ import java.util.regex.Pattern import java.math.BigInteger import java.util.concurrent.TimeUnit import libv2ray.Libv2ray +import kotlin.coroutines.coroutineContext object Utils { + val tcpTestingSockets = ArrayList() + /** * convert string to editalbe for kotlin * @@ -483,10 +487,13 @@ object Utils { /** * tcping */ - fun tcping(url: String, port: Int): String { + suspend fun tcping(url: String, port: Int): String { var time = -1L for (k in 0 until 2) { val one = socketConnectTime(url, port) + if (!coroutineContext.isActive) { + break + } if (one != -1L ) if(time == -1L || one < time) { time = one @@ -497,19 +504,35 @@ object Utils { fun socketConnectTime(url: String, port: Int): Long { try { + val socket = Socket() + synchronized(this) { + tcpTestingSockets.add(socket) + } val start = System.currentTimeMillis() - val socket = Socket(url, port) + socket.connect(InetSocketAddress(url, port)) val time = System.currentTimeMillis() - start + synchronized(this) { + tcpTestingSockets.remove(socket) + } socket.close() return time } catch (e: UnknownHostException) { e.printStackTrace() } catch (e: IOException) { - e.printStackTrace() + Log.d(AppConfig.ANG_PACKAGE, "socketConnectTime IOException: $e") } catch (e: Exception) { e.printStackTrace() } return -1 } + + fun closeAllTcpSockets() { + synchronized(this) { + tcpTestingSockets.forEach { + it?.close() + } + tcpTestingSockets.clear() + } + } }