diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/dto/TestServiceMessage.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/dto/TestServiceMessage.kt new file mode 100644 index 00000000..a9f4ce91 --- /dev/null +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/dto/TestServiceMessage.kt @@ -0,0 +1,10 @@ +package com.v2ray.ang.dto + +import java.io.Serializable + +data class TestServiceMessage( + val key: Int, + val subscriptionId: String = "", + val serverGuids: List = emptyList() +) : Serializable + 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 ec7d5602..179f8e25 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 @@ -6,6 +6,8 @@ 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.dto.TestServiceMessage +import com.v2ray.ang.extension.serializable import com.v2ray.ang.handler.MmkvManager import com.v2ray.ang.handler.V2RayNativeManager import com.v2ray.ang.util.MessageUtil @@ -52,13 +54,15 @@ class V2RayTestService : Service() { * @return The start mode. */ override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - when (intent?.getIntExtra("key", 0)) { + val message = intent?.serializable("content") ?: return super.onStartCommand(intent, flags, startId) + when (message.key) { MSG_MEASURE_CONFIG -> { - val subscriptionId = intent.getStringExtra("content").orEmpty() - val guidsList = if (subscriptionId.isEmpty()) { - MmkvManager.decodeAllServerList() + val guidsList = if (message.serverGuids.isNotEmpty()) { + message.serverGuids + } else if (message.subscriptionId.isNotEmpty()) { + MmkvManager.decodeServerList(message.subscriptionId) } else { - MmkvManager.decodeServerList(subscriptionId) + MmkvManager.decodeAllServerList() } if (guidsList.isNotEmpty()) { diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/util/MessageUtil.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/util/MessageUtil.kt index c84443c7..64a2d600 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/util/MessageUtil.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/util/MessageUtil.kt @@ -5,6 +5,7 @@ import android.content.Context import android.content.Intent import android.util.Log import com.v2ray.ang.AppConfig +import com.v2ray.ang.dto.TestServiceMessage import com.v2ray.ang.service.V2RayTestService import java.io.Serializable @@ -37,15 +38,13 @@ object MessageUtil { * Sends a message to the test service. * * @param ctx The context. - * @param what The message identifier. - * @param content The message content. + * @param message The test service message containing key, subscriptionId, and serverGuids. */ - fun sendMsg2TestService(ctx: Context, what: Int, content: Serializable) { + fun sendMsg2TestService(ctx: Context, message: TestServiceMessage) { try { val intent = Intent() intent.component = ComponentName(ctx, V2RayTestService::class.java) - intent.putExtra("key", what) - intent.putExtra("content", content) + intent.putExtra("content", message) ctx.startService(intent) } catch (e: Exception) { Log.e(AppConfig.TAG, "Failed to send message to test service", e) 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 4d4fe5d8..3f2042da 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 @@ -18,6 +18,7 @@ import com.v2ray.ang.dto.GroupMapItem import com.v2ray.ang.dto.ServersCache import com.v2ray.ang.dto.SubscriptionCache import com.v2ray.ang.dto.SubscriptionUpdateResult +import com.v2ray.ang.dto.TestServiceMessage import com.v2ray.ang.extension.serializable import com.v2ray.ang.extension.toastError import com.v2ray.ang.extension.toastSuccess @@ -196,7 +197,10 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { * Tests the real ping for all servers. */ fun testAllRealPing() { - MessageUtil.sendMsg2TestService(getApplication(), AppConfig.MSG_MEASURE_CONFIG_CANCEL, "") + MessageUtil.sendMsg2TestService( + getApplication(), + TestServiceMessage(key = AppConfig.MSG_MEASURE_CONFIG_CANCEL) + ) MmkvManager.clearAllTestDelayResults(serversCache.map { it.guid }.toList()) updateListAction.value = -1 @@ -204,7 +208,14 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { if (serversCache.isEmpty()) { return@launch } - MessageUtil.sendMsg2TestService(getApplication(), AppConfig.MSG_MEASURE_CONFIG, subscriptionId) + MessageUtil.sendMsg2TestService( + getApplication(), + TestServiceMessage( + key = AppConfig.MSG_MEASURE_CONFIG, + subscriptionId = subscriptionId, + serverGuids = if (keywordFilter.isNotEmpty()) serversCache.map { it.guid } else emptyList() + ) + ) } }