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 adf8663c..3f4654bd 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/AppConfig.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/AppConfig.kt @@ -66,6 +66,8 @@ object AppConfig { const val PREF_USE_HEV_TUNNEL = "pref_use_hev_tunnel" const val PREF_HEV_TUNNEL_LOGLEVEL = "pref_hev_tunnel_loglevel" const val PREF_HEV_TUNNEL_RW_TIMEOUT = "pref_hev_tunnel_rw_timeout_v2" + const val PREF_AUTO_REMOVE_INVALID_AFTER_TEST = "pref_auto_remove_invalid_after_test" + const val PREF_AUTO_SORT_AFTER_TEST = "pref_auto_sort_after_test" /** Cache keys. */ const val CACHE_SUBSCRIPTION_ID = "cache_subscription_id" @@ -148,7 +150,8 @@ 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 + const val MSG_MEASURE_CONFIG_NOTIFY = 73 + const val MSG_MEASURE_CONFIG_FINISH = 74 /** 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 6f343c57..bb3237e7 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 @@ -22,6 +22,8 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancelChildren import kotlinx.coroutines.launch +import kotlinx.coroutines.joinAll +import kotlinx.coroutines.CancellationException import libv2ray.Libv2ray import java.util.concurrent.atomic.AtomicInteger @@ -47,43 +49,6 @@ class V2RayTestService : Service() { Libv2ray.initCoreEnv(Utils.userAssetPath(this), Utils.getDeviceIdForXUDPBaseKey()) } - /** - * Handles the start command for the service. - * @param intent The intent. - * @param flags The flags. - * @param startId The start ID. - * @return The start mode. - */ - override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - when (intent?.getIntExtra("key", 0)) { - MSG_MEASURE_CONFIG -> { - 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") - } - } - } - } - - MSG_MEASURE_CONFIG_CANCEL -> { - realTestJob.cancelChildren() - } - } - return super.onStartCommand(intent, flags, startId) - } - /** * Binds the service. * @param intent The intent. @@ -101,6 +66,64 @@ class V2RayTestService : Service() { realTestJob.cancel() } + /** + * Handles the start command for the service. + * @param intent The intent. + * @param flags The flags. + * @param startId The start ID. + * @return The start mode. + */ + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + when (intent?.getIntExtra("key", 0)) { + MSG_MEASURE_CONFIG -> { + val guidsList = intent.serializable>("content") + if (guidsList != null && guidsList.isNotEmpty()) { + startBatchRealPing(guidsList) + } + } + + MSG_MEASURE_CONFIG_CANCEL -> { + realTestJob.cancelChildren() + } + } + return super.onStartCommand(intent, flags, startId) + } + + /** + * Starts batch real ping tests. + * @param guidsList The list of GUIDs to test. + */ + private fun startBatchRealPing(guidsList: List) { + val jobs = guidsList.map { guid -> + 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_NOTIFY, "$left / $count") + } + } + } + + realTestScope.launch { + try { + joinAll(*jobs.toTypedArray()) + notifyAllTasksCompleted("0") + } catch (_: CancellationException) { + notifyAllTasksCompleted("-1") + } + } + } + + private fun notifyAllTasksCompleted(status: String) { + MessageUtil.sendMsg2UI(this@V2RayTestService, AppConfig.MSG_MEASURE_CONFIG_FINISH, status) + } + + /** * Starts the real ping test. * @param guid The GUID of the configuration. @@ -121,4 +144,4 @@ class V2RayTestService : Service() { return SpeedtestManager.realPing(configResult.content) } } -} +} \ No newline at end of file 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 9c310ef8..32ea0570 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 @@ -31,6 +31,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.cancelChildren import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import java.util.Collections class MainViewModel(application: Application) : AndroidViewModel(application) { @@ -413,6 +414,22 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { reloadServerList() } + fun onTestsFinished() { + viewModelScope.launch(Dispatchers.Default) { + if (MmkvManager.decodeSettingsBool(AppConfig.PREF_AUTO_REMOVE_INVALID_AFTER_TEST)) { + removeInvalidServer() + } + + if (MmkvManager.decodeSettingsBool(AppConfig.PREF_AUTO_SORT_AFTER_TEST)) { + sortByTestResults() + } + + withContext(Dispatchers.Main) { + reloadServerList() + } + } + } + private val mMsgReceiver = object : BroadcastReceiver() { override fun onReceive(ctx: Context?, intent: Intent?) { when (intent?.getIntExtra("key", 0)) { @@ -448,11 +465,15 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { updateListAction.value = getPosition(resultPair.first) } - AppConfig.MSG_MEASURE_CONFIG_FINISH -> { + AppConfig.MSG_MEASURE_CONFIG_NOTIFY -> { val content = intent.getStringExtra("content") updateTestResultAction.value = getApplication().getString(R.string.connection_runing_task_left, content) } + + AppConfig.MSG_MEASURE_CONFIG_FINISH -> { + onTestsFinished() + } } } } diff --git a/V2rayNG/app/src/main/res/values-ar/strings.xml b/V2rayNG/app/src/main/res/values-ar/strings.xml index c46b9f70..5fda5439 100644 --- a/V2rayNG/app/src/main/res/values-ar/strings.xml +++ b/V2rayNG/app/src/main/res/values-ar/strings.xml @@ -154,6 +154,11 @@ Auto connect at startup Automatically connects to the selected server at startup, which may be unsuccessful + Auto delete invalid config after testing + Test results may not be accurate; deleted config cannot be recovered. + Auto sort after testing + Test results may not be accurate; + إعدادات Mux تمكين Mux أسرع، لكن قد يتسبب في اتصال غير مستقر\nتخصيص كيفية التعامل مع TCP و UDP و QUIC أدناه diff --git a/V2rayNG/app/src/main/res/values-bn/strings.xml b/V2rayNG/app/src/main/res/values-bn/strings.xml index c71baf49..cf60e3e1 100644 --- a/V2rayNG/app/src/main/res/values-bn/strings.xml +++ b/V2rayNG/app/src/main/res/values-bn/strings.xml @@ -152,6 +152,11 @@ Auto connect at startup Automatically connects to the selected server at startup, which may be unsuccessful + Auto delete invalid config after testing + Test results may not be accurate; deleted config cannot be recovered. + Auto sort after testing + Test results may not be accurate; + Mux সেটিংস Mux সক্রিয় করুন দ্রুত, তবে এটি অস্থিতিশীল সংযোগ সৃষ্টি করতে পারে\nTCP, UDP এবং QUIC পরিচালনা কাস্টমাইজ করুন 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 07e8df14..2db15d4b 100644 --- a/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml +++ b/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml @@ -153,6 +153,11 @@ منپیز خوتکار مجال ر ونی مجال ر وندن، خوساخوس و سرور پسند بیڌه منپیز ابۊ که گاشڌ نا مووفق بۊ. + Auto delete invalid config after testing + Test results may not be accurate; deleted config cannot be recovered. + Auto sort after testing + Test results may not be accurate; + سامووا Mux ر وندن Mux زل تر، ٱما گاشڌ منپیز زی قت بۊ\nمخزن ترافیک TCP وا 8 منپیز پؽش فرز، بارت دؽوۉداری UDP وو QUIC ن ای لم سفارشی کۊنین. diff --git a/V2rayNG/app/src/main/res/values-fa/strings.xml b/V2rayNG/app/src/main/res/values-fa/strings.xml index 48d0c2eb..b8c1df09 100644 --- a/V2rayNG/app/src/main/res/values-fa/strings.xml +++ b/V2rayNG/app/src/main/res/values-fa/strings.xml @@ -150,6 +150,11 @@ اتصال خودکار هنگام راه اندازی هنگام راه اندازی به طور خودکار به سرور انتخابی متصل می شود که ممکن است ناموفق باشد. + Auto delete invalid config after testing + Test results may not be accurate; deleted config cannot be recovered. + Auto sort after testing + Test results may not be accurate; + تنظیمات MUX فعال کردن MUX سریعتر است، اما ممکن است باعث اتصال ناپایدار شود\nمخزن ترافیک TCP با 8 اتصال پیش‌فرض، نحوه مدیریت UDP و QUIC را در زیر سفارشی کنید. diff --git a/V2rayNG/app/src/main/res/values-ru/strings.xml b/V2rayNG/app/src/main/res/values-ru/strings.xml index 2d3b58fe..8c99670a 100644 --- a/V2rayNG/app/src/main/res/values-ru/strings.xml +++ b/V2rayNG/app/src/main/res/values-ru/strings.xml @@ -152,6 +152,11 @@ Автоподключение при запуске Автоматически подключаться к выбранному серверу при запуске приложения (может оказаться неудачным) + Auto delete invalid config after testing + Test results may not be accurate; deleted config cannot be recovered. + Auto sort after testing + Test results may not be accurate; + Настройки мультиплексирования Использовать мультиплексирование Быстрее, но это может привести к нестабильному соединению.\nНиже можно настроить обработку TCP, UDP и QUIC. diff --git a/V2rayNG/app/src/main/res/values-vi/strings.xml b/V2rayNG/app/src/main/res/values-vi/strings.xml index e01c22a4..3fb2022d 100644 --- a/V2rayNG/app/src/main/res/values-vi/strings.xml +++ b/V2rayNG/app/src/main/res/values-vi/strings.xml @@ -151,6 +151,11 @@ Auto connect at startup Automatically connects to the selected server at startup, which may be unsuccessful + Auto delete invalid config after testing + Test results may not be accurate; deleted config cannot be recovered. + Auto sort after testing + Test results may not be accurate; + Cài đặt Mux Bật Mux Giảm độ trễ nhưng có thể làm gián đoạn luồng, vì vậy không nên kích hoạt nó. \nCác phương pháp xử lý lưu lượng truy cập TCP, UDP và QUIC có sẵn bên dưới. 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 1b084142..e8ef3a43 100644 --- a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml @@ -150,6 +150,11 @@ 开机时自动连接 开机时自动连接选择的服务器,可能会不成功 + 测试后自动删除无效配置 + 测试结果可能不准确;已删除的配置无法恢复。 + 测试后自动排序 + 测试结果可能不准确; + Mux 多路复用 设置 启用 Mux 多路复用 减低延时,但可能会断流,建议不要启用。\nTCP,UDP 及 QUIC 流量处理方式下方可选。 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 0431f336..259afd64 100644 --- a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml @@ -151,6 +151,11 @@ 開機時自動連線 開機時自動連線選擇的伺服器,可能會不成功 + 測試後自動刪除無效配置 + 測試結果可能不準確;已刪除的配置無法復原。 + 測試後自動排序 + 測試結果可能不準確; + Mux 設定 啟用 Mux 多路復用 減低延時 但可能會斷流\nTCP,UDP 及 QUIC 流量處理方式下方可選 diff --git a/V2rayNG/app/src/main/res/values/strings.xml b/V2rayNG/app/src/main/res/values/strings.xml index e9d6c43d..1a40b120 100644 --- a/V2rayNG/app/src/main/res/values/strings.xml +++ b/V2rayNG/app/src/main/res/values/strings.xml @@ -153,6 +153,11 @@ Auto connect at startup Automatically connects to the selected server at startup, which may be unsuccessful + Auto delete invalid config after testing + Test results may not be accurate; deleted config cannot be recovered. + Auto sort after testing + Test results may not be accurate; + Mux Settings Enable Mux Faster, but it may cause unstable connectivity\nCustomize how to handle TCP, UDP and QUIC below diff --git a/V2rayNG/app/src/main/res/xml/pref_settings.xml b/V2rayNG/app/src/main/res/xml/pref_settings.xml index 5be1e414..a74108f2 100644 --- a/V2rayNG/app/src/main/res/xml/pref_settings.xml +++ b/V2rayNG/app/src/main/res/xml/pref_settings.xml @@ -261,6 +261,18 @@ android:summary="@string/summary_pref_is_booted" android:title="@string/title_pref_is_booted" /> + + + +