Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Improvement Intent.serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust authored and AnGgIt86 committed Sep 30, 2024
1 parent 7fb8fac commit 3335ef4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
9 changes: 9 additions & 0 deletions app/src/main/kotlin/com/neko/v2ray/dto/ConfigResult.kt
Original file line number Diff line number Diff line change
@@ -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,
)

14 changes: 13 additions & 1 deletion app/src/main/kotlin/com/neko/v2ray/extension/_Ext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -81,4 +83,14 @@ fun Context.listenForPackageChanges(onetime: Boolean = true, callback: () -> Uni
addDataScheme("package")
})
}
}
}

inline fun <reified T : Serializable> 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 <reified T : Serializable> 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
}
10 changes: 7 additions & 3 deletions app/src/main/kotlin/com/neko/v2ray/service/V2RayTestService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, String>
val content = intent.serializable<String>("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))
}
}

Expand Down
19 changes: 9 additions & 10 deletions app/src/main/kotlin/com/neko/v2ray/util/V2rayConfigUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 3 additions & 6 deletions app/src/main/kotlin/com/neko/v2ray/viewmodel/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
)
}
}
Expand Down Expand Up @@ -395,11 +396,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
}

AppConfig.MSG_MEASURE_CONFIG_SUCCESS -> {
val resultPair: Pair<String, Long> = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getSerializableExtra("content", Pair::class.java) as Pair<String, Long>
} else {
intent.getSerializableExtra("content") as Pair<String, Long>
}
val resultPair = intent.serializable<Pair<String, Long>>("content") ?: return
MmkvManager.encodeServerTestDelayMillis(resultPair.first, resultPair.second)
updateListAction.value = getPosition(resultPair.first)
}
Expand Down

0 comments on commit 3335ef4

Please sign in to comment.