diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/AppConfig.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/AppConfig.kt index b9ecf827..adf8663c 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/AppConfig.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/AppConfig.kt @@ -148,6 +148,7 @@ object AppConfig { const val MSG_MEASURE_CONFIG = 7 const val MSG_MEASURE_CONFIG_SUCCESS = 71 const val MSG_MEASURE_CONFIG_CANCEL = 72 + const val MSG_MEASURE_CONFIG_FINISH = 73 /** Notification channel IDs and names. */ const val RAY_NG_CHANNEL_ID = "RAY_NG_M_CH_ID" 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 8d559441..f441af2b 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 @@ -3,6 +3,7 @@ package com.v2ray.ang.service import android.app.Service import android.content.Intent import android.os.IBinder +import com.v2ray.ang.AppConfig import com.v2ray.ang.AppConfig.MSG_MEASURE_CONFIG import com.v2ray.ang.AppConfig.MSG_MEASURE_CONFIG_CANCEL import com.v2ray.ang.AppConfig.MSG_MEASURE_CONFIG_SUCCESS @@ -15,16 +16,26 @@ import com.v2ray.ang.handler.V2rayConfigManager import com.v2ray.ang.util.MessageUtil import com.v2ray.ang.util.Utils import go.Seq +import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Job -import kotlinx.coroutines.asCoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancelChildren import kotlinx.coroutines.launch import libv2ray.Libv2ray -import java.util.concurrent.Executors +import java.util.concurrent.atomic.AtomicInteger class V2RayTestService : Service() { - private val realTestScope by lazy { CoroutineScope(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()).asCoroutineDispatcher()) } + private val realTestJob = SupervisorJob() + private val realDispatcher = Dispatchers.IO.limitedParallelism( + Runtime.getRuntime().availableProcessors() * 3 + ) + private val realTestScope = CoroutineScope( + realTestJob + realDispatcher + CoroutineName("RealTest") + ) + + // simple counter for currently running tasks + private val realTestRunningCount = AtomicInteger(0) /** * Initializes the V2Ray environment. @@ -47,13 +58,19 @@ class V2RayTestService : Service() { MSG_MEASURE_CONFIG -> { val guid = intent.serializable("content") ?: "" realTestScope.launch { - val result = startRealPing(guid) - MessageUtil.sendMsg2UI(this@V2RayTestService, MSG_MEASURE_CONFIG_SUCCESS, Pair(guid, result)) + 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()) + } } } MSG_MEASURE_CONFIG_CANCEL -> { - realTestScope.coroutineContext[Job]?.cancelChildren() + realTestJob.cancelChildren() } } return super.onStartCommand(intent, flags, startId) @@ -68,6 +85,14 @@ class V2RayTestService : Service() { return null } + /** + * Cleans up resources when the service is destroyed. + */ + override fun onDestroy() { + super.onDestroy() + realTestJob.cancel() + } + /** * Starts the real ping test. * @param guid The GUID of the configuration. 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 e47c6cb9..79293a1a 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 @@ -445,6 +445,12 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { MmkvManager.encodeServerTestDelayMillis(resultPair.first, resultPair.second) updateListAction.value = getPosition(resultPair.first) } + + AppConfig.MSG_MEASURE_CONFIG_FINISH -> { + val content = intent.getStringExtra("content") + updateTestResultAction.value = + getApplication().getString(R.string.connection_runing_task_left, content) + } } } } diff --git a/V2rayNG/app/src/main/res/values-ar/strings.xml b/V2rayNG/app/src/main/res/values-ar/strings.xml index 37f2cd55..c46b9f70 100644 --- a/V2rayNG/app/src/main/res/values-ar/strings.xml +++ b/V2rayNG/app/src/main/res/values-ar/strings.xml @@ -316,6 +316,7 @@ رمز الخطأ: #%d متصل، انقر للتحقق من الاتصال غير متصل + Number of running test tasks: %s تم استيراد الاشتراك بنجاح فشل استيراد الاشتراك diff --git a/V2rayNG/app/src/main/res/values-bn/strings.xml b/V2rayNG/app/src/main/res/values-bn/strings.xml index ae5d7947..c71baf49 100644 --- a/V2rayNG/app/src/main/res/values-bn/strings.xml +++ b/V2rayNG/app/src/main/res/values-bn/strings.xml @@ -315,6 +315,7 @@ ত্রুটি কোড: #%d সংযুক্ত, সংযোগ পরীক্ষা করতে ট্যাপ করুন সংযুক্ত নয় + Number of running test tasks: %s সাবস্ক্রিপশন সফলভাবে আমদানি করা হয়েছে সাবস্ক্রিপশন আমদানি ব্যর্থ diff --git a/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml b/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml index 7beadefe..1254629a 100644 --- a/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml +++ b/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml @@ -324,6 +324,7 @@ کود ختا: #%d منپیز هڌ، سی واجۊری کیلیک کوݩ منپیز نؽڌ + Number of running test tasks: %s اشتراک وا مووفقیت زفت زابی اشتراک زفت نوابی diff --git a/V2rayNG/app/src/main/res/values-fa/strings.xml b/V2rayNG/app/src/main/res/values-fa/strings.xml index 46890040..228f8a84 100644 --- a/V2rayNG/app/src/main/res/values-fa/strings.xml +++ b/V2rayNG/app/src/main/res/values-fa/strings.xml @@ -321,6 +321,7 @@ کد خطا: #%d متصل است، برای بررسی اتصال ضربه بزنید متصل نیست + Number of running test tasks: %s اشتراک با موفقیت ذخیره شد ذخیره اشتراک ناموفق بود diff --git a/V2rayNG/app/src/main/res/values-ru/strings.xml b/V2rayNG/app/src/main/res/values-ru/strings.xml index 7c8e6ef7..d762fad6 100644 --- a/V2rayNG/app/src/main/res/values-ru/strings.xml +++ b/V2rayNG/app/src/main/res/values-ru/strings.xml @@ -323,6 +323,7 @@ Код ошибки: #%d Подключено, нажмите для проверки Не подключено + Number of running test tasks: %s Подписка импортирована Невозможно импортировать подписку diff --git a/V2rayNG/app/src/main/res/values-vi/strings.xml b/V2rayNG/app/src/main/res/values-vi/strings.xml index dc5c6427..e01c22a4 100644 --- a/V2rayNG/app/src/main/res/values-vi/strings.xml +++ b/V2rayNG/app/src/main/res/values-vi/strings.xml @@ -316,6 +316,7 @@ Mã lỗi: #%d Đã kết nối, nhấn để kiểm tra kết nối mạng! Chưa kết nối + Number of running test tasks: %s Nhập gói đăng ký thành công! Nhập gói đăng ký không thành công! diff --git a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml index f5b5f4b5..1b084142 100644 --- a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml @@ -313,6 +313,7 @@ "状态码无效(#%d)" "已连接,点击测试连接" "未连接" + 运行中的测试任务数:%s 订阅导入成功 导入订阅失败 diff --git a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml index 7b876d12..0431f336 100644 --- a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml @@ -313,6 +313,7 @@ "錯誤碼:(#%d)" "已連線,輕觸以檢查連線能力" "未連線" + 運行中的測試任務數:%s 匯入訂閱成功 匯入訂閱失敗 diff --git a/V2rayNG/app/src/main/res/values/strings.xml b/V2rayNG/app/src/main/res/values/strings.xml index 14e3d582..e9d6c43d 100644 --- a/V2rayNG/app/src/main/res/values/strings.xml +++ b/V2rayNG/app/src/main/res/values/strings.xml @@ -327,6 +327,7 @@ Error code: #%d Connected, tap to check connection Not connected + Number of running test tasks: %s Subscription imported Successfully Import subscription failed