From 3ad6464595116e5f6bb61b6e96cb9a13bd1bb767 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sat, 10 Jan 2026 18:25:38 +0800 Subject: [PATCH] Refactor V2RayTestService --- .../com/v2ray/ang/service/V2RayTestService.kt | 26 ++++++++++++------- .../com/v2ray/ang/viewmodel/MainViewModel.kt | 6 +++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayTestService.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayTestService.kt index f441af2b..6f343c57 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayTestService.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayTestService.kt @@ -36,6 +36,7 @@ class V2RayTestService : Service() { // simple counter for currently running tasks private val realTestRunningCount = AtomicInteger(0) + private val realTestCount = AtomicInteger(0) /** * Initializes the V2Ray environment. @@ -56,15 +57,22 @@ class V2RayTestService : Service() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { when (intent?.getIntExtra("key", 0)) { MSG_MEASURE_CONFIG -> { - val guid = intent.serializable("content") ?: "" - realTestScope.launch { - realTestRunningCount.incrementAndGet() - try { - val result = startRealPing(guid) - MessageUtil.sendMsg2UI(this@V2RayTestService, MSG_MEASURE_CONFIG_SUCCESS, Pair(guid, result)) - } finally { - val left = realTestRunningCount.decrementAndGet() - MessageUtil.sendMsg2UI(this@V2RayTestService, AppConfig.MSG_MEASURE_CONFIG_FINISH, left.toString()) + val guidsList = intent.serializable>("content") + if (guidsList == null || guidsList.isEmpty()) { + return super.onStartCommand(intent, flags, startId) + } + for (guid in guidsList) { + realTestCount.incrementAndGet() + realTestScope.launch { + realTestRunningCount.incrementAndGet() + try { + val result = startRealPing(guid) + MessageUtil.sendMsg2UI(this@V2RayTestService, MSG_MEASURE_CONFIG_SUCCESS, Pair(guid, result)) + } finally { + val count = realTestCount.decrementAndGet() + val left = realTestRunningCount.decrementAndGet() + MessageUtil.sendMsg2UI(this@V2RayTestService, AppConfig.MSG_MEASURE_CONFIG_FINISH, "$left / $count") + } } } } diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/viewmodel/MainViewModel.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/viewmodel/MainViewModel.kt index 1568861d..b909a078 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/viewmodel/MainViewModel.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/viewmodel/MainViewModel.kt @@ -237,9 +237,11 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { val serversCopy = serversCache.toList() viewModelScope.launch(Dispatchers.Default) { - for (item in serversCopy) { - MessageUtil.sendMsg2TestService(getApplication(), AppConfig.MSG_MEASURE_CONFIG, item.guid) + val guids = ArrayList(serversCopy.map { it.guid }) + if (guids.isEmpty()) { + return@launch } + MessageUtil.sendMsg2TestService(getApplication(), AppConfig.MSG_MEASURE_CONFIG, guids) } }