From 3335ef4d5ecf33ff34c9457876c513aa25c1ff6d Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Mon, 30 Sep 2024 14:20:45 +0800 Subject: [PATCH] Improvement Intent.serializable --- .../kotlin/com/neko/v2ray/dto/ConfigResult.kt | 9 +++++++++ .../kotlin/com/neko/v2ray/extension/_Ext.kt | 14 +++++++++++++- .../neko/v2ray/service/V2RayTestService.kt | 10 +++++++--- .../com/neko/v2ray/util/V2rayConfigUtil.kt | 19 +++++++++---------- .../com/neko/v2ray/viewmodel/MainViewModel.kt | 9 +++------ 5 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 app/src/main/kotlin/com/neko/v2ray/dto/ConfigResult.kt diff --git a/app/src/main/kotlin/com/neko/v2ray/dto/ConfigResult.kt b/app/src/main/kotlin/com/neko/v2ray/dto/ConfigResult.kt new file mode 100644 index 00000000..2c01ab6a --- /dev/null +++ b/app/src/main/kotlin/com/neko/v2ray/dto/ConfigResult.kt @@ -0,0 +1,9 @@ +package com.neko.v2ray.dto + +data class ConfigResult ( + var status: Boolean, + var guid: String? = null, + var content: String = "", + var domainPort: String? = null, +) + diff --git a/app/src/main/kotlin/com/neko/v2ray/extension/_Ext.kt b/app/src/main/kotlin/com/neko/v2ray/extension/_Ext.kt index c92ddf7e..eb9fc864 100644 --- a/app/src/main/kotlin/com/neko/v2ray/extension/_Ext.kt +++ b/app/src/main/kotlin/com/neko/v2ray/extension/_Ext.kt @@ -5,10 +5,12 @@ import android.content.Context import android.content.Intent import android.content.IntentFilter import android.os.Build +import android.os.Bundle import android.widget.Toast import com.neko.v2ray.AngApplication import me.drakeet.support.toast.ToastCompat import org.json.JSONObject +import java.io.Serializable import java.net.URI import java.net.URLConnection @@ -81,4 +83,14 @@ fun Context.listenForPackageChanges(onetime: Boolean = true, callback: () -> Uni addDataScheme("package") }) } - } \ No newline at end of file + } + +inline fun Bundle.serializable(key: String): T? = when { + Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getSerializable(key, T::class.java) + else -> @Suppress("DEPRECATION") getSerializable(key) as? T +} + +inline fun Intent.serializable(key: String): T? = when { + Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getSerializableExtra(key, T::class.java) + else -> @Suppress("DEPRECATION") getSerializableExtra(key) as? T +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/neko/v2ray/service/V2RayTestService.kt b/app/src/main/kotlin/com/neko/v2ray/service/V2RayTestService.kt index e54ef055..857d7a42 100644 --- a/app/src/main/kotlin/com/neko/v2ray/service/V2RayTestService.kt +++ b/app/src/main/kotlin/com/neko/v2ray/service/V2RayTestService.kt @@ -3,9 +3,12 @@ package com.neko.v2ray.service import android.app.Service import android.content.Intent import android.os.IBinder +import com.google.gson.Gson import com.neko.v2ray.AppConfig.MSG_MEASURE_CONFIG import com.neko.v2ray.AppConfig.MSG_MEASURE_CONFIG_CANCEL import com.neko.v2ray.AppConfig.MSG_MEASURE_CONFIG_SUCCESS +import com.neko.v2ray.dto.ConfigResult +import com.neko.v2ray.extension.serializable import com.neko.v2ray.util.MessageUtil import com.neko.v2ray.util.SpeedtestUtil import com.neko.v2ray.util.Utils @@ -30,10 +33,11 @@ class V2RayTestService : Service() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { when (intent?.getIntExtra("key", 0)) { MSG_MEASURE_CONFIG -> { - val contentPair = intent.getSerializableExtra("content") as Pair + val content = intent.serializable("content") + val config = Gson().fromJson(content, ConfigResult::class.java) realTestScope.launch { - val result = SpeedtestUtil.realPing(contentPair.second) - MessageUtil.sendMsg2UI(this@V2RayTestService, MSG_MEASURE_CONFIG_SUCCESS, Pair(contentPair.first, result)) + val result = SpeedtestUtil.realPing(config.content) + MessageUtil.sendMsg2UI(this@V2RayTestService, MSG_MEASURE_CONFIG_SUCCESS, Pair(config.guid, result)) } } diff --git a/app/src/main/kotlin/com/neko/v2ray/util/V2rayConfigUtil.kt b/app/src/main/kotlin/com/neko/v2ray/util/V2rayConfigUtil.kt index 65bef7dd..624dd35c 100644 --- a/app/src/main/kotlin/com/neko/v2ray/util/V2rayConfigUtil.kt +++ b/app/src/main/kotlin/com/neko/v2ray/util/V2rayConfigUtil.kt @@ -14,6 +14,7 @@ import com.neko.v2ray.AppConfig.TAG_FRAGMENT import com.neko.v2ray.AppConfig.TAG_PROXY import com.neko.v2ray.AppConfig.WIREGUARD_LOCAL_ADDRESS_V4 import com.neko.v2ray.AppConfig.WIREGUARD_LOCAL_ADDRESS_V6 +import com.neko.v2ray.dto.ConfigResult import com.neko.v2ray.dto.EConfigType import com.neko.v2ray.dto.RulesetItem import com.neko.v2ray.dto.ServerConfig @@ -25,34 +26,32 @@ import com.neko.v2ray.util.MmkvManager.settingsStorage object V2rayConfigUtil { - data class Result(var status: Boolean, var content: String = "", var domainPort: String? = null) - - fun getV2rayConfig(context: Context, guid: String): Result { + fun getV2rayConfig(context: Context, guid: String): ConfigResult { try { - val config = MmkvManager.decodeServerConfig(guid) ?: return Result(false) + val config = MmkvManager.decodeServerConfig(guid) ?: return ConfigResult(false) if (config.configType == EConfigType.CUSTOM) { val raw = MmkvManager.decodeServerRaw(guid) val customConfig = if (raw.isNullOrBlank()) { - config.fullConfig?.toPrettyPrinting() ?: return Result(false) + config.fullConfig?.toPrettyPrinting() ?: return ConfigResult(false) } else { raw } val domainPort = config.getProxyOutbound()?.getServerAddressAndPort() - return Result(true, customConfig, domainPort) + return ConfigResult(true, guid, customConfig, domainPort) } val result = getV2rayNonCustomConfig(context, config) //Log.d(ANG_PACKAGE, result.content) - Log.d(ANG_PACKAGE, result.domainPort ?: "") + result.guid = guid return result } catch (e: Exception) { e.printStackTrace() - return Result(false) + return ConfigResult(false) } } - private fun getV2rayNonCustomConfig(context: Context, config: ServerConfig): Result { - val result = Result(false) + private fun getV2rayNonCustomConfig(context: Context, config: ServerConfig): ConfigResult { + val result = ConfigResult(false) val outbound = config.getProxyOutbound() ?: return result val address = outbound.getServerAddress() ?: return result diff --git a/app/src/main/kotlin/com/neko/v2ray/viewmodel/MainViewModel.kt b/app/src/main/kotlin/com/neko/v2ray/viewmodel/MainViewModel.kt index b90b4a69..18458a1c 100644 --- a/app/src/main/kotlin/com/neko/v2ray/viewmodel/MainViewModel.kt +++ b/app/src/main/kotlin/com/neko/v2ray/viewmodel/MainViewModel.kt @@ -21,6 +21,7 @@ import com.neko.v2ray.dto.ProfileItem import com.neko.v2ray.dto.ServerConfig import com.neko.v2ray.dto.ServersCache import com.neko.v2ray.dto.V2rayConfig +import com.neko.v2ray.extension.serializable import com.neko.v2ray.extension.toast import com.neko.v2ray.util.AngConfigManager import com.neko.v2ray.util.AngConfigManager.updateConfigViaSub @@ -217,7 +218,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { MessageUtil.sendMsg2TestService( getApplication(), AppConfig.MSG_MEASURE_CONFIG, - Pair(item.guid, config.content) + Gson().toJson(config) ) } } @@ -395,11 +396,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { } AppConfig.MSG_MEASURE_CONFIG_SUCCESS -> { - val resultPair: Pair = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - intent.getSerializableExtra("content", Pair::class.java) as Pair - } else { - intent.getSerializableExtra("content") as Pair - } + val resultPair = intent.serializable>("content") ?: return MmkvManager.encodeServerTestDelayMillis(resultPair.first, resultPair.second) updateListAction.value = getPosition(resultPair.first) }