Refactor EConfigType to be an enum

Using enum will simplify logic and conveniently providing Int value
and a String name
This commit is contained in:
yuhan6665
2020-08-15 19:37:59 -04:00
parent 1361e0dacf
commit dc31380cc2
7 changed files with 49 additions and 67 deletions
@@ -50,12 +50,4 @@ object AppConfig {
const val MSG_STATE_STOP = 4
const val MSG_STATE_STOP_SUCCESS = 41
const val MSG_STATE_RESTART = 5
object EConfigType {
val Vmess = 1
val Custom = 2
val Shadowsocks = 3
val Socks = 4
}
}
@@ -0,0 +1,12 @@
package com.v2ray.ang.dto
enum class EConfigType(val value: Int) {
VMESS(1),
CUSTOM(2),
SHADOWSOCKS(3),
SOCKS(4);
companion object {
fun fromInt(value: Int) = values().firstOrNull { it.value == value }
}
}
@@ -27,6 +27,7 @@ import android.support.v4.view.GravityCompat
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.widget.helper.ItemTouchHelper
import android.util.Log
import com.v2ray.ang.dto.EConfigType
//import com.v2ray.ang.InappBuyActivity
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
@@ -252,7 +253,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
adapter.updateConfigList()
}
for (k in 0 until configs.vmess.count()) {
if (configs.vmess[k].configType != AppConfig.EConfigType.Custom) {
if (configs.vmess[k].configType != EConfigType.CUSTOM.value) {
testingJobs.add(GlobalScope.launch(Dispatchers.IO) {
configs.vmess[k].testResult = Utils.tcping(configs.vmess[k].address, configs.vmess[k].port)
val myJob = coroutineContext[Job]
@@ -8,6 +8,7 @@ import android.view.ViewGroup
import com.v2ray.ang.AppConfig
import com.v2ray.ang.R
import com.v2ray.ang.dto.AngConfig
import com.v2ray.ang.dto.EConfigType
import com.v2ray.ang.helper.ItemTouchHelperAdapter
import com.v2ray.ang.helper.ItemTouchHelperViewHolder
import com.v2ray.ang.util.AngConfigManager
@@ -18,7 +19,6 @@ import org.jetbrains.anko.*
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import java.util.concurrent.TimeUnit
import com.v2ray.ang.extension.defaultDPreference
class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<MainRecyclerAdapter.BaseViewHolder>()
, ItemTouchHelperAdapter {
@@ -49,7 +49,7 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai
override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
if (holder is MainViewHolder) {
val configType = configs.vmess[position].configType
val configType = EConfigType.fromInt(configs.vmess[position].configType)
val remarks = configs.vmess[position].remarks
val subid = configs.vmess[position].subid
val address = configs.vmess[position].address
@@ -67,20 +67,12 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai
holder.subid.text = "S"
}
if (configType == AppConfig.EConfigType.Vmess) {
holder.type.text = "vmess"
holder.statistics.text = "$address : $port"
holder.layout_share.visibility = View.VISIBLE
} else if (configType == AppConfig.EConfigType.Custom) {
if (configType == EConfigType.CUSTOM) {
holder.type.text = mActivity.getString(R.string.server_customize_config)
holder.statistics.text = ""//mActivity.getString(R.string.server_customize_config)
holder.layout_share.visibility = View.INVISIBLE
} else if (configType == AppConfig.EConfigType.Shadowsocks) {
holder.type.text = "shadowsocks"
holder.statistics.text = "$address : $port"
holder.layout_share.visibility = View.VISIBLE
} else if (configType == AppConfig.EConfigType.Socks) {
holder.type.text = "socks"
} else {
holder.type.text = configType?.name?.toLowerCase()
holder.statistics.text = "$address : $port"
holder.layout_share.visibility = View.VISIBLE
}
@@ -125,13 +117,13 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai
}
holder.layout_edit.setOnClickListener {
if (configType == AppConfig.EConfigType.Vmess) {
if (configType == EConfigType.VMESS) {
mActivity.startActivity<ServerActivity>("position" to position, "isRunning" to !changeable)
} else if (configType == AppConfig.EConfigType.Custom) {
} else if (configType == EConfigType.CUSTOM) {
mActivity.startActivity<Server2Activity>("position" to position, "isRunning" to !changeable)
} else if (configType == AppConfig.EConfigType.Shadowsocks) {
} else if (configType == EConfigType.SHADOWSOCKS) {
mActivity.startActivity<Server3Activity>("position" to position, "isRunning" to !changeable)
} else if (configType == AppConfig.EConfigType.Socks) {
} else if (configType == EConfigType.SOCKS) {
mActivity.startActivity<Server4Activity>("position" to position, "isRunning" to !changeable)
}
}
@@ -14,6 +14,7 @@ import com.v2ray.ang.AppConfig.SS_PROTOCOL
import com.v2ray.ang.AppConfig.VMESS_PROTOCOL
import com.v2ray.ang.R
import com.v2ray.ang.dto.AngConfig
import com.v2ray.ang.dto.EConfigType
import com.v2ray.ang.dto.VmessQRCode
import java.net.URI
import java.net.URLDecoder
@@ -65,7 +66,7 @@ object AngConfigManager {
fun addServer(vmess: AngConfig.VmessBean, index: Int): Int {
try {
vmess.configVersion = 2
vmess.configType = AppConfig.EConfigType.Vmess
vmess.configType = EConfigType.VMESS.value
if (index >= 0) {
//edit
@@ -208,14 +209,14 @@ object AngConfigManager {
return app.defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG, "")
}
fun currConfigType(): Int {
fun currConfigType(): EConfigType? {
if (angConfig.index < 0
|| angConfig.vmess.count() <= 0
|| angConfig.index > angConfig.vmess.count() - 1
) {
return -1
return null
}
return angConfig.vmess[angConfig.index].configType
return EConfigType.fromInt(angConfig.vmess[angConfig.index].configType)
}
fun currConfigName(): String {
@@ -275,7 +276,7 @@ object AngConfigManager {
return R.string.toast_incorrect_protocol
}
vmess.configType = AppConfig.EConfigType.Vmess
vmess.configType = EConfigType.VMESS.value
vmess.security = "auto"
vmess.network = "tcp"
vmess.headerType = "none"
@@ -478,7 +479,7 @@ object AngConfigManager {
}
val vmess = angConfig.vmess[index]
if (angConfig.vmess[index].configType == AppConfig.EConfigType.Vmess) {
if (angConfig.vmess[index].configType == EConfigType.VMESS.value) {
val vmessQRCode = VmessQRCode()
vmessQRCode.v = vmess.configVersion.toString()
@@ -496,7 +497,7 @@ object AngConfigManager {
val conf = VMESS_PROTOCOL + Utils.encode(json)
return conf
} else if (angConfig.vmess[index].configType == AppConfig.EConfigType.Shadowsocks) {
} else if (angConfig.vmess[index].configType == EConfigType.SHADOWSOCKS.value) {
val remark = "#" + Utils.urlEncode(vmess.remarks)
val url = String.format("%s:%s@%s:%s",
vmess.security,
@@ -504,7 +505,7 @@ object AngConfigManager {
vmess.address,
vmess.port)
return SS_PROTOCOL + Utils.encode(url) + remark
} else if (angConfig.vmess[index].configType == AppConfig.EConfigType.Socks) {
} else if (angConfig.vmess[index].configType == EConfigType.SOCKS.value) {
val remark = "#" + Utils.urlEncode(vmess.remarks)
val url = String.format("%s:%s",
vmess.address,
@@ -613,7 +614,7 @@ object AngConfigManager {
//add
val vmess = AngConfig.VmessBean()
vmess.configVersion = 2
vmess.configType = AppConfig.EConfigType.Custom
vmess.configType = EConfigType.CUSTOM.value
vmess.guid = guid
vmess.remarks = vmess.guid
@@ -716,7 +717,7 @@ object AngConfigManager {
fun addCustomServer(vmess: AngConfig.VmessBean, index: Int): Int {
try {
vmess.configVersion = 2
vmess.configType = AppConfig.EConfigType.Custom
vmess.configType = EConfigType.CUSTOM.value
if (index >= 0) {
//edit
@@ -741,7 +742,7 @@ object AngConfigManager {
fun addShadowsocksServer(vmess: AngConfig.VmessBean, index: Int): Int {
try {
vmess.configVersion = 2
vmess.configType = AppConfig.EConfigType.Shadowsocks
vmess.configType = EConfigType.SHADOWSOCKS.value
if (index >= 0) {
//edit
@@ -766,7 +767,7 @@ object AngConfigManager {
fun addSocksServer(vmess: AngConfig.VmessBean, index: Int): Int {
try {
vmess.configVersion = 2
vmess.configType = AppConfig.EConfigType.Socks
vmess.configType = EConfigType.SOCKS.value
if (index >= 0) {
//edit
@@ -14,36 +14,25 @@ import kotlin.collections.HashMap
import android.app.ActivityManager
import android.content.ClipData
import android.content.Intent
import android.content.res.AssetManager
import android.net.Uri
import android.os.SystemClock
import android.text.TextUtils
import android.text.method.ScrollingMovementMethod
import android.util.Log
import android.util.Patterns
import android.view.View
import android.webkit.URLUtil
import com.v2ray.ang.AngApplication
import com.v2ray.ang.AppConfig
import com.v2ray.ang.R
import com.v2ray.ang.dto.EConfigType
import com.v2ray.ang.extension.responseLength
import com.v2ray.ang.extension.v2RayApplication
import com.v2ray.ang.service.V2RayVpnService
import com.v2ray.ang.ui.SettingsActivity
import kotlinx.android.synthetic.main.activity_logcat.*
import kotlinx.coroutines.isActive
import me.dozen.dpreference.DPreference
import org.jetbrains.anko.toast
import org.jetbrains.anko.uiThread
import java.io.BufferedReader
import java.io.File
import java.io.IOException
import java.io.InputStreamReader
import java.net.*
import java.util.regex.Matcher
import java.util.regex.Pattern
import java.math.BigInteger
import java.util.concurrent.TimeUnit
import libv2ray.Libv2ray
import kotlin.coroutines.coroutineContext
@@ -324,7 +313,7 @@ object Utils {
if (AngConfigManager.genStoreV2rayConfig(-1)) {
val configContent = AngConfigManager.currGeneratedV2rayConfig()
val configType = AngConfigManager.currConfigType()
if (configType == AppConfig.EConfigType.Custom) {
if (configType == EConfigType.CUSTOM) {
try {
Libv2ray.testConfig(configContent)
} catch (e: Exception) {
@@ -14,6 +14,7 @@ import org.json.JSONException
import org.json.JSONObject
import org.json.JSONArray
import com.google.gson.JsonObject
import com.v2ray.ang.dto.EConfigType
object V2rayConfigUtil {
private val requestObj: JsonObject by lazy {
@@ -40,13 +41,9 @@ object V2rayConfigUtil {
// return result
// }
if (vmess.configType == AppConfig.EConfigType.Vmess) {
result = getV2rayConfigType1(app, vmess)
} else if (vmess.configType == AppConfig.EConfigType.Custom) {
if (vmess.configType == EConfigType.CUSTOM.value) {
result = getV2rayConfigType2(app, vmess)
} else if (vmess.configType == AppConfig.EConfigType.Shadowsocks) {
result = getV2rayConfigType1(app, vmess)
} else if (vmess.configType == AppConfig.EConfigType.Socks) {
} else {
result = getV2rayConfigType1(app, vmess)
}
@@ -163,8 +160,12 @@ object V2rayConfigUtil {
try {
val outbound = v2rayConfig.outbounds[0]
when (vmess.configType) {
AppConfig.EConfigType.Vmess -> {
val configType = EConfigType.fromInt(vmess.configType)
if (configType != null) {
outbound.protocol = configType.name.toLowerCase()
}
when (configType) {
EConfigType.VMESS -> {
outbound.settings?.servers = null
val vnext = v2rayConfig.outbounds[0].settings?.vnext?.get(0)
@@ -182,10 +183,8 @@ object V2rayConfigUtil {
//远程服务器底层传输配置
outbound.streamSettings = boundStreamSettings(vmess)
outbound.protocol = "vmess"
}
AppConfig.EConfigType.Shadowsocks -> {
EConfigType.SHADOWSOCKS -> {
outbound.settings?.vnext = null
val server = outbound.settings?.servers?.get(0)
@@ -198,10 +197,8 @@ object V2rayConfigUtil {
//Mux
outbound.mux?.enabled = false
outbound.protocol = "shadowsocks"
}
AppConfig.EConfigType.Socks -> {
EConfigType.SOCKS -> {
outbound.settings?.vnext = null
val server = outbound.settings?.servers?.get(0)
@@ -210,8 +207,6 @@ object V2rayConfigUtil {
//Mux
outbound.mux?.enabled = false
outbound.protocol = "socks"
}
else -> {
}