Add policy group config type and UI support
Introduces a new POLICYGROUP config type for server grouping and selection. Adds ServerGroupActivity and related UI, updates menu options, and removes legacy intelligent selection logic. Refactors config management and filtering to support policy groups, and updates translations and resources accordingly.
This commit is contained in:
@@ -58,7 +58,6 @@ object AppConfig {
|
||||
const val PREF_DELAY_TEST_URL = "pref_delay_test_url"
|
||||
const val PREF_LOGLEVEL = "pref_core_loglevel"
|
||||
const val PREF_OUTBOUND_DOMAIN_RESOLVE_METHOD = "pref_outbound_domain_resolve_method"
|
||||
const val PREF_INTELLIGENT_SELECTION_METHOD = "pref_intelligent_selection_method"
|
||||
const val PREF_MODE = "pref_mode"
|
||||
const val PREF_IS_BOOTED = "pref_is_booted"
|
||||
const val PREF_CHECK_UPDATE_PRE_RELEASE = "pref_check_update_pre_release"
|
||||
|
||||
@@ -14,7 +14,8 @@ enum class EConfigType(val value: Int, val protocolScheme: String) {
|
||||
|
||||
// TUIC(8, AppConfig.TUIC),
|
||||
HYSTERIA2(9, AppConfig.HYSTERIA2),
|
||||
HTTP(10, AppConfig.HTTP);
|
||||
HTTP(10, AppConfig.HTTP),
|
||||
POLICYGROUP (101, AppConfig.CUSTOM);
|
||||
|
||||
companion object {
|
||||
fun fromInt(value: Int) = entries.firstOrNull { it.value == value }
|
||||
|
||||
@@ -59,6 +59,10 @@ data class ProfileItem(
|
||||
var bandwidthDown: String? = null,
|
||||
var bandwidthUp: String? = null,
|
||||
|
||||
var policyGroupType: String? = null,
|
||||
var policyGroupSubscriptionId: String? = null,
|
||||
var policyGroupFilter: String? = null,
|
||||
|
||||
) {
|
||||
companion object {
|
||||
fun create(configType: EConfigType): ProfileItem {
|
||||
|
||||
@@ -11,7 +11,6 @@ data class SubscriptionItem(
|
||||
var prevProfile: String? = null,
|
||||
var nextProfile: String? = null,
|
||||
var filter: String? = null,
|
||||
var intelligentSelectionFilter: String? = null,
|
||||
var allowInsecureUrl: Boolean = false,
|
||||
var userAgent: String? = null,
|
||||
)
|
||||
|
||||
@@ -148,6 +148,7 @@ object AngConfigManager {
|
||||
EConfigType.TROJAN -> TrojanFmt.toUri(config)
|
||||
EConfigType.WIREGUARD -> WireguardFmt.toUri(config)
|
||||
EConfigType.HYSTERIA2 -> Hysteria2Fmt.toUri(config)
|
||||
EConfigType.POLICYGROUP -> ""
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(AppConfig.TAG, "Failed to share config for GUID: $guid", e)
|
||||
@@ -491,31 +492,4 @@ object AngConfigManager {
|
||||
MmkvManager.encodeSubscription("", subItem)
|
||||
return 1
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an intelligent selection configuration based on multiple server configurations.
|
||||
*
|
||||
* @param context The application context used for configuration generation.
|
||||
* @param guidList The list of server GUIDs to be included in the intelligent selection.
|
||||
* Each GUID represents a server configuration that will be combined.
|
||||
* @param subid The subscription ID to associate with the generated configuration.
|
||||
* This helps organize the configuration under a specific subscription.
|
||||
* @return The GUID key of the newly created intelligent selection configuration,
|
||||
* or null if the operation fails (e.g., empty guidList or configuration parsing error).
|
||||
*/
|
||||
fun createIntelligentSelection(
|
||||
context: Context,
|
||||
guidList: List<String>,
|
||||
subid: String
|
||||
): String? {
|
||||
if (guidList.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
val result = V2rayConfigManager.genV2rayConfig(context, guidList) ?: return null
|
||||
val config = CustomFmt.parse(JsonUtil.toJson(result)) ?: return null
|
||||
config.subscriptionId = subid
|
||||
val key = MmkvManager.encodeServerConfig("", config)
|
||||
MmkvManager.encodeServerRaw(key, JsonUtil.toJsonPretty(result) ?: "")
|
||||
return key
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.v2ray.ang.AppConfig
|
||||
@@ -17,7 +16,6 @@ import com.v2ray.ang.service.ServiceControl
|
||||
import com.v2ray.ang.service.V2RayProxyOnlyService
|
||||
import com.v2ray.ang.service.V2RayVpnService
|
||||
import com.v2ray.ang.util.MessageUtil
|
||||
import com.v2ray.ang.handler.PluginServiceManager
|
||||
import com.v2ray.ang.util.Utils
|
||||
import go.Seq
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
@@ -100,6 +98,7 @@ object V2RayServiceManager {
|
||||
val guid = MmkvManager.getSelectServer() ?: return
|
||||
val config = MmkvManager.decodeServerConfig(guid) ?: return
|
||||
if (config.configType != EConfigType.CUSTOM
|
||||
&& config.configType != EConfigType.POLICYGROUP
|
||||
&& !Utils.isValidUrl(config.server)
|
||||
&& !Utils.isPureIpAddress(config.server.orEmpty())
|
||||
) return
|
||||
|
||||
@@ -44,6 +44,8 @@ object V2rayConfigManager {
|
||||
val config = MmkvManager.decodeServerConfig(guid) ?: return ConfigResult(false)
|
||||
return if (config.configType == EConfigType.CUSTOM) {
|
||||
getV2rayCustomConfig(guid, config)
|
||||
} else if (config.configType == EConfigType.POLICYGROUP) {
|
||||
getV2rayGroupConfig(context, guid, config)
|
||||
} else {
|
||||
getV2rayNormalConfig(context, guid, config)
|
||||
}
|
||||
@@ -53,27 +55,6 @@ object V2rayConfigManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a V2ray configuration from multiple server profiles.
|
||||
*
|
||||
* @param context The context of the caller.
|
||||
* @param guidList A list of server GUIDs to be included in the generated configuration.
|
||||
* Each GUID represents a unique server profile stored in the system.
|
||||
* @return A V2rayConfig object containing the combined configuration of all specified servers,
|
||||
* or null if the operation fails (e.g., no valid configurations found, parsing errors)
|
||||
*/
|
||||
fun genV2rayConfig(context: Context, guidList: List<String>): V2rayConfig? {
|
||||
try {
|
||||
val configList = guidList.mapNotNull { guid ->
|
||||
MmkvManager.decodeServerConfig(guid)
|
||||
}
|
||||
return genV2rayMultipleConfig(context, configList)
|
||||
} catch (e: Exception) {
|
||||
Log.e(AppConfig.TAG, "Failed to generate V2ray config", e)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the speedtest V2ray configuration for the given GUID.
|
||||
*
|
||||
@@ -86,6 +67,9 @@ object V2rayConfigManager {
|
||||
val config = MmkvManager.decodeServerConfig(guid) ?: return ConfigResult(false)
|
||||
return if (config.configType == EConfigType.CUSTOM) {
|
||||
getV2rayCustomConfig(guid, config)
|
||||
} else if (config.configType == EConfigType.POLICYGROUP) {
|
||||
// The number of policy groups will not be very large, so no special handling is needed.
|
||||
getV2rayGroupConfig(context, guid, config)
|
||||
} else {
|
||||
getV2rayNormalConfig4Speedtest(context, guid, config)
|
||||
}
|
||||
@@ -107,6 +91,50 @@ object V2rayConfigManager {
|
||||
return ConfigResult(true, guid, raw)
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the group V2ray configuration.
|
||||
*
|
||||
* @param context The context in which the function is called.
|
||||
* @param guid The unique identifier for the V2ray configuration.
|
||||
* @param config The profile item containing the configuration details.
|
||||
* @return A ConfigResult object containing the result of the configuration retrieval.
|
||||
*/
|
||||
private fun getV2rayGroupConfig(context: Context, guid: String, config: ProfileItem): ConfigResult {
|
||||
val result = ConfigResult(false)
|
||||
|
||||
val serverList = MmkvManager.decodeServerList()
|
||||
val configList = serverList
|
||||
.mapNotNull { id -> MmkvManager.decodeServerConfig(id) }
|
||||
.filter { profile ->
|
||||
val subscriptionId = config.policyGroupSubscriptionId
|
||||
if (subscriptionId.isNullOrBlank()) {
|
||||
true
|
||||
} else {
|
||||
profile.subscriptionId == subscriptionId
|
||||
}
|
||||
}
|
||||
.filter { profile ->
|
||||
val filter = config.policyGroupFilter
|
||||
if (filter.isNullOrBlank()) {
|
||||
true
|
||||
} else {
|
||||
try {
|
||||
Regex(filter).containsMatchIn(profile.remarks)
|
||||
} catch (e: Exception) {
|
||||
profile.remarks.contains(filter)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val v2rayConfig = getV2rayMultipleConfig(context, config, configList) ?: return result
|
||||
|
||||
result.status = true
|
||||
result.content = JsonUtil.toJsonPretty(v2rayConfig) ?: ""
|
||||
result.guid = guid
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the normal V2ray configuration.
|
||||
*
|
||||
@@ -164,21 +192,13 @@ object V2rayConfigManager {
|
||||
return result
|
||||
}
|
||||
|
||||
private fun genV2rayMultipleConfig(context: Context, configList: List<ProfileItem>): V2rayConfig? {
|
||||
private fun getV2rayMultipleConfig(context: Context, config: ProfileItem, configList: List<ProfileItem>): V2rayConfig? {
|
||||
val validConfigs = configList.asSequence().filter { it.server.isNotNullEmpty() }
|
||||
.filter { !Utils.isPureIpAddress(it.server!!) || Utils.isValidUrl(it.server!!) }
|
||||
.filter { it.configType != EConfigType.CUSTOM }
|
||||
.filter { it.configType != EConfigType.HYSTERIA2 }
|
||||
.filter { config ->
|
||||
if (config.subscriptionId.isEmpty()) {
|
||||
return@filter true
|
||||
}
|
||||
val subItem = MmkvManager.decodeSubscription(config.subscriptionId)
|
||||
if (subItem?.intelligentSelectionFilter.isNullOrEmpty() || config.remarks.isEmpty()) {
|
||||
return@filter true
|
||||
}
|
||||
Regex(pattern = subItem.intelligentSelectionFilter.orEmpty()).containsMatchIn(input = config.remarks)
|
||||
}.toList()
|
||||
.filter { it.configType != EConfigType.POLICYGROUP }
|
||||
.toList()
|
||||
|
||||
if (validConfigs.isEmpty()) {
|
||||
Log.w(AppConfig.TAG, "All configs are invalid")
|
||||
@@ -187,16 +207,7 @@ object V2rayConfigManager {
|
||||
|
||||
val v2rayConfig = initV2rayConfig(context) ?: return null
|
||||
v2rayConfig.log.loglevel = MmkvManager.decodeSettingsString(AppConfig.PREF_LOGLEVEL) ?: "warning"
|
||||
|
||||
val subIds = configList.map { it.subscriptionId }.toHashSet()
|
||||
val remarks = if (subIds.size == 1 && subIds.first().isNotEmpty()) {
|
||||
val sub = MmkvManager.decodeSubscription(subIds.first())
|
||||
(sub?.remarks ?: "") + context.getString(R.string.intelligent_selection)
|
||||
} else {
|
||||
context.getString(R.string.intelligent_selection)
|
||||
}
|
||||
|
||||
v2rayConfig.remarks = remarks
|
||||
v2rayConfig.remarks = config.remarks
|
||||
|
||||
getInbounds(v2rayConfig)
|
||||
|
||||
@@ -220,12 +231,12 @@ object V2rayConfigManager {
|
||||
|
||||
getDns(v2rayConfig)
|
||||
|
||||
getBalance(v2rayConfig)
|
||||
getBalance(v2rayConfig, config)
|
||||
|
||||
if (MmkvManager.decodeSettingsBool(AppConfig.PREF_LOCAL_DNS_ENABLED) == true) {
|
||||
if (MmkvManager.decodeSettingsBool(AppConfig.PREF_LOCAL_DNS_ENABLED)) {
|
||||
getCustomLocalDns(v2rayConfig)
|
||||
}
|
||||
if (MmkvManager.decodeSettingsBool(AppConfig.PREF_SPEED_ENABLED) != true) {
|
||||
if (!MmkvManager.decodeSettingsBool(AppConfig.PREF_SPEED_ENABLED)) {
|
||||
v2rayConfig.stats = null
|
||||
v2rayConfig.policy = null
|
||||
}
|
||||
@@ -469,7 +480,7 @@ object V2rayConfigManager {
|
||||
)
|
||||
}
|
||||
|
||||
if (MmkvManager.decodeSettingsBool(AppConfig.PREF_USE_HEV_TUNNEL ,true) == false) {
|
||||
if (MmkvManager.decodeSettingsBool(AppConfig.PREF_USE_HEV_TUNNEL, true) == false) {
|
||||
|
||||
// DNS inbound
|
||||
val remoteDns = SettingsManager.getRemoteDnsServers()
|
||||
@@ -850,8 +861,7 @@ object V2rayConfigManager {
|
||||
*
|
||||
* @param v2rayConfig The V2ray configuration object to be modified with balancing settings
|
||||
*/
|
||||
private fun getBalance(v2rayConfig: V2rayConfig)
|
||||
{
|
||||
private fun getBalance(v2rayConfig: V2rayConfig, config: ProfileItem) {
|
||||
try {
|
||||
v2rayConfig.routing.rules.forEach { rule ->
|
||||
if (rule.outboundTag == "proxy") {
|
||||
@@ -860,7 +870,7 @@ object V2rayConfigManager {
|
||||
}
|
||||
}
|
||||
|
||||
if (MmkvManager.decodeSettingsString(AppConfig.PREF_INTELLIGENT_SELECTION_METHOD, "0") == "0") {
|
||||
if (config.policyGroupType == "0") {
|
||||
val balancer = V2rayConfig.RoutingBean.BalancerBean(
|
||||
tag = "proxy-round",
|
||||
selector = listOf("proxy-"),
|
||||
@@ -1054,6 +1064,7 @@ object V2rayConfigManager {
|
||||
EConfigType.WIREGUARD -> WireguardFmt.toOutbound(profileItem)
|
||||
EConfigType.HYSTERIA2 -> null
|
||||
EConfigType.HTTP -> HttpFmt.toOutbound(profileItem)
|
||||
EConfigType.POLICYGROUP -> null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1104,6 +1115,7 @@ object V2rayConfigManager {
|
||||
)
|
||||
|
||||
EConfigType.CUSTOM -> null
|
||||
EConfigType.POLICYGROUP -> null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -313,6 +313,11 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
|
||||
true
|
||||
}
|
||||
|
||||
R.id.import_manually_policy_group -> {
|
||||
importManually(EConfigType.POLICYGROUP.value)
|
||||
true
|
||||
}
|
||||
|
||||
R.id.import_manually_vmess -> {
|
||||
importManually(EConfigType.VMESS.value)
|
||||
true
|
||||
@@ -370,14 +375,6 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
|
||||
true
|
||||
}
|
||||
|
||||
R.id.intelligent_selection_all -> {
|
||||
if (MmkvManager.decodeSettingsString(AppConfig.PREF_OUTBOUND_DOMAIN_RESOLVE_METHOD, "1") != "0") {
|
||||
toast(getString(R.string.pre_resolving_domain))
|
||||
}
|
||||
mainViewModel.createIntelligentSelectionAll()
|
||||
true
|
||||
}
|
||||
|
||||
R.id.service_restart -> {
|
||||
restartV2Ray()
|
||||
true
|
||||
@@ -413,12 +410,20 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
|
||||
}
|
||||
|
||||
private fun importManually(createConfigType: Int) {
|
||||
startActivity(
|
||||
Intent()
|
||||
.putExtra("createConfigType", createConfigType)
|
||||
.putExtra("subscriptionId", mainViewModel.subscriptionId)
|
||||
.setClass(this, ServerActivity::class.java)
|
||||
)
|
||||
if (createConfigType == EConfigType.POLICYGROUP.value) {
|
||||
startActivity(
|
||||
Intent()
|
||||
.putExtra("subscriptionId", mainViewModel.subscriptionId)
|
||||
.setClass(this, ServerGroupActivity::class.java)
|
||||
)
|
||||
} else {
|
||||
startActivity(
|
||||
Intent()
|
||||
.putExtra("createConfigType", createConfigType)
|
||||
.putExtra("subscriptionId", mainViewModel.subscriptionId)
|
||||
.setClass(this, ServerActivity::class.java)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -57,7 +57,7 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai
|
||||
if (holder is MainViewHolder) {
|
||||
val guid = mActivity.mainViewModel.serversCache[position].guid
|
||||
val profile = mActivity.mainViewModel.serversCache[position].profile
|
||||
val isCustom = profile.configType == EConfigType.CUSTOM
|
||||
val isCustom = profile.configType == EConfigType.CUSTOM || profile.configType == EConfigType.POLICYGROUP
|
||||
|
||||
holder.itemView.setBackgroundColor(Color.TRANSPARENT)
|
||||
|
||||
@@ -144,14 +144,18 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai
|
||||
*/
|
||||
private fun getAddress(profile: ProfileItem): String {
|
||||
// Hide xxx:xxx:***/xxx.xxx.xxx.***
|
||||
return "${
|
||||
profile.server?.let {
|
||||
if (it.contains(":"))
|
||||
it.split(":").take(2).joinToString(":", postfix = ":***")
|
||||
else
|
||||
it.split('.').dropLast(1).joinToString(".", postfix = ".***")
|
||||
}
|
||||
} : ${profile.serverPort}"
|
||||
val server = profile.server
|
||||
val port = profile.serverPort
|
||||
if (server.isNullOrBlank() && port.isNullOrBlank()) return ""
|
||||
|
||||
val addrPart = server?.let {
|
||||
if (it.contains(":"))
|
||||
it.split(":").take(2).joinToString(":", postfix = ":***")
|
||||
else
|
||||
it.split('.').dropLast(1).joinToString(".", postfix = ".***")
|
||||
} ?: ""
|
||||
|
||||
return "$addrPart : ${port ?: ""}"
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -250,6 +254,8 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai
|
||||
.putExtra("createConfigType", profile.configType.value)
|
||||
if (profile.configType == EConfigType.CUSTOM) {
|
||||
mActivity.startActivity(intent.setClass(mActivity, ServerCustomConfigActivity::class.java))
|
||||
} else if (profile.configType == EConfigType.POLICYGROUP) {
|
||||
mActivity.startActivity(intent.setClass(mActivity, ServerGroupActivity::class.java))
|
||||
} else {
|
||||
mActivity.startActivity(intent.setClass(mActivity, ServerActivity::class.java))
|
||||
}
|
||||
|
||||
@@ -134,9 +134,9 @@ class ServerActivity : BaseActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
title = getString(R.string.title_server)
|
||||
|
||||
val config = MmkvManager.decodeServerConfig(editGuid)
|
||||
title = (config?.configType ?: createConfigType).toString()
|
||||
when (config?.configType ?: createConfigType) {
|
||||
EConfigType.VMESS -> setContentView(R.layout.activity_server_vmess)
|
||||
EConfigType.CUSTOM -> return
|
||||
@@ -147,6 +147,7 @@ class ServerActivity : BaseActivity() {
|
||||
EConfigType.TROJAN -> setContentView(R.layout.activity_server_trojan)
|
||||
EConfigType.WIREGUARD -> setContentView(R.layout.activity_server_wireguard)
|
||||
EConfigType.HYSTERIA2 -> setContentView(R.layout.activity_server_hysteria2)
|
||||
EConfigType.POLICYGROUP -> return
|
||||
}
|
||||
sp_network?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
||||
override fun onItemSelected(
|
||||
|
||||
@@ -32,7 +32,7 @@ class ServerCustomConfigActivity : BaseActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(binding.root)
|
||||
title = getString(R.string.title_server)
|
||||
title = EConfigType.CUSTOM.toString()
|
||||
|
||||
if (!Utils.getDarkModeStatus(this)) {
|
||||
binding.editor.colorScheme = EditorTheme.INTELLIJ_LIGHT
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
package com.v2ray.ang.ui
|
||||
|
||||
import android.os.Bundle
|
||||
import android.text.TextUtils
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import com.v2ray.ang.R
|
||||
import com.v2ray.ang.databinding.ActivityServerGroupBinding
|
||||
import com.v2ray.ang.dto.EConfigType
|
||||
import com.v2ray.ang.dto.ProfileItem
|
||||
import com.v2ray.ang.extension.toast
|
||||
import com.v2ray.ang.extension.toastSuccess
|
||||
import android.widget.ArrayAdapter
|
||||
import com.v2ray.ang.handler.MmkvManager
|
||||
import com.v2ray.ang.util.Utils
|
||||
|
||||
class ServerGroupActivity : BaseActivity() {
|
||||
private val binding by lazy { ActivityServerGroupBinding.inflate(layoutInflater) }
|
||||
|
||||
private val editGuid by lazy { intent.getStringExtra("guid").orEmpty() }
|
||||
private val isRunning by lazy {
|
||||
intent.getBooleanExtra("isRunning", false)
|
||||
&& editGuid.isNotEmpty()
|
||||
&& editGuid == MmkvManager.getSelectServer()
|
||||
}
|
||||
private val subscriptionId by lazy {
|
||||
intent.getStringExtra("subscriptionId")
|
||||
}
|
||||
private val subIds = mutableListOf<String>()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(binding.root)
|
||||
title = EConfigType.POLICYGROUP.toString()
|
||||
|
||||
val config = MmkvManager.decodeServerConfig(editGuid)
|
||||
populateSubscriptionSpinner()
|
||||
|
||||
if (config != null) {
|
||||
bindingServer(config)
|
||||
} else {
|
||||
clearServer()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Binding selected server config
|
||||
*/
|
||||
private fun bindingServer(config: ProfileItem): Boolean {
|
||||
binding.etRemarks.text = Utils.getEditable(config.remarks)
|
||||
binding.etPolicyGroupFilter.text = Utils.getEditable(config.policyGroupFilter)
|
||||
|
||||
val type = config.policyGroupType?.toInt() ?: 0
|
||||
binding.spPolicyGroupType.setSelection(type)
|
||||
|
||||
val pos = subIds.indexOf(config.policyGroupSubscriptionId ?: "").let { if (it >= 0) it else 0 }
|
||||
binding.spPolicyGroupSubId.setSelection(pos)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* clear or init server config
|
||||
*/
|
||||
private fun clearServer(): Boolean {
|
||||
binding.etRemarks.text = null
|
||||
binding.etPolicyGroupFilter.text = null
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* save server config
|
||||
*/
|
||||
private fun saveServer(): Boolean {
|
||||
if (TextUtils.isEmpty(binding.etRemarks.text.toString())) {
|
||||
toast(R.string.server_lab_remarks)
|
||||
return false
|
||||
}
|
||||
|
||||
val config = MmkvManager.decodeServerConfig(editGuid) ?: ProfileItem.create(EConfigType.POLICYGROUP)
|
||||
config.remarks = binding.etRemarks.text.toString().trim()
|
||||
config.policyGroupFilter = binding.etPolicyGroupFilter.text.toString().trim()
|
||||
|
||||
config.policyGroupType = binding.spPolicyGroupType.selectedItemPosition.toString()
|
||||
|
||||
val selPos = binding.spPolicyGroupSubId.selectedItemPosition
|
||||
config.policyGroupSubscriptionId = if (selPos >= 0 && selPos < subIds.size) subIds[selPos] else null
|
||||
|
||||
if (config.subscriptionId.isEmpty() && !subscriptionId.isNullOrEmpty()) {
|
||||
config.subscriptionId = subscriptionId.orEmpty()
|
||||
}
|
||||
MmkvManager.encodeServerConfig(editGuid, config)
|
||||
toastSuccess(R.string.toast_success)
|
||||
finish()
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* save server config
|
||||
*/
|
||||
private fun deleteServer(): Boolean {
|
||||
if (editGuid.isNotEmpty()) {
|
||||
AlertDialog.Builder(this).setMessage(R.string.del_config_comfirm)
|
||||
.setPositiveButton(android.R.string.ok) { _, _ ->
|
||||
MmkvManager.removeServer(editGuid)
|
||||
finish()
|
||||
}
|
||||
.setNegativeButton(android.R.string.cancel) { _, _ ->
|
||||
// do nothing
|
||||
}
|
||||
.show()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun populateSubscriptionSpinner() {
|
||||
val subs = MmkvManager.decodeSubscriptions()
|
||||
val displayList = mutableListOf(getString(R.string.filter_config_all)) //none
|
||||
subIds.clear()
|
||||
subIds.add("") // index 0 => All
|
||||
subs.forEach { (id, item) ->
|
||||
val name = when {
|
||||
item.remarks.isNotBlank() -> item.remarks
|
||||
else -> id
|
||||
}
|
||||
displayList.add(name)
|
||||
subIds.add(id)
|
||||
}
|
||||
val subAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, displayList)
|
||||
subAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
||||
binding.spPolicyGroupSubId.adapter = subAdapter
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
menuInflater.inflate(R.menu.action_server, menu)
|
||||
val delButton = menu.findItem(R.id.del_config)
|
||||
val saveButton = menu.findItem(R.id.save_config)
|
||||
|
||||
if (editGuid.isNotEmpty()) {
|
||||
if (isRunning) {
|
||||
delButton?.isVisible = false
|
||||
saveButton?.isVisible = false
|
||||
}
|
||||
} else {
|
||||
delButton?.isVisible = false
|
||||
}
|
||||
|
||||
return super.onCreateOptionsMenu(menu)
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
|
||||
R.id.del_config -> {
|
||||
deleteServer()
|
||||
true
|
||||
}
|
||||
|
||||
R.id.save_config -> {
|
||||
saveServer()
|
||||
true
|
||||
}
|
||||
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
}
|
||||
}
|
||||
@@ -285,7 +285,6 @@ class SettingsActivity : BaseActivity() {
|
||||
AppConfig.PREF_UI_MODE_NIGHT,
|
||||
AppConfig.PREF_LOGLEVEL,
|
||||
AppConfig.PREF_OUTBOUND_DOMAIN_RESOLVE_METHOD,
|
||||
AppConfig.PREF_INTELLIGENT_SELECTION_METHOD,
|
||||
AppConfig.PREF_MODE,
|
||||
AppConfig.PREF_HEV_TUNNEL_LOGLEVEL
|
||||
).forEach { key ->
|
||||
|
||||
@@ -46,7 +46,6 @@ class SubEditActivity : BaseActivity() {
|
||||
binding.etUrl.text = Utils.getEditable(subItem.url)
|
||||
binding.etUserAgent.text = Utils.getEditable(subItem.userAgent)
|
||||
binding.etFilter.text = Utils.getEditable(subItem.filter)
|
||||
binding.etIntelligentSelectionFilter.text = Utils.getEditable(subItem.intelligentSelectionFilter)
|
||||
binding.chkEnable.isChecked = subItem.enabled
|
||||
binding.autoUpdateCheck.isChecked = subItem.autoUpdate
|
||||
binding.allowInsecureUrl.isChecked = subItem.allowInsecureUrl
|
||||
@@ -62,7 +61,6 @@ class SubEditActivity : BaseActivity() {
|
||||
binding.etRemarks.text = null
|
||||
binding.etUrl.text = null
|
||||
binding.etFilter.text = null
|
||||
binding.etIntelligentSelectionFilter.text = null
|
||||
binding.chkEnable.isChecked = true
|
||||
binding.etPreProfile.text = null
|
||||
binding.etNextProfile.text = null
|
||||
@@ -79,7 +77,6 @@ class SubEditActivity : BaseActivity() {
|
||||
subItem.url = binding.etUrl.text.toString()
|
||||
subItem.userAgent = binding.etUserAgent.text.toString()
|
||||
subItem.filter = binding.etFilter.text.toString()
|
||||
subItem.intelligentSelectionFilter = binding.etIntelligentSelectionFilter.text.toString()
|
||||
subItem.enabled = binding.chkEnable.isChecked
|
||||
subItem.autoUpdate = binding.autoUpdateCheck.isChecked
|
||||
subItem.prevProfile = binding.etPreProfile.text.toString()
|
||||
|
||||
@@ -384,29 +384,6 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
MmkvManager.encodeServerList(serverList)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an intelligent selection configuration containing all currently filtered servers.
|
||||
*/
|
||||
fun createIntelligentSelectionAll() {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val key = AngConfigManager.createIntelligentSelection(
|
||||
getApplication<AngApplication>(),
|
||||
serversCache.map { it.guid }.toList(),
|
||||
subscriptionId
|
||||
)
|
||||
|
||||
launch(Dispatchers.Main) {
|
||||
if (key.isNullOrEmpty()) {
|
||||
getApplication<AngApplication>().toastError(R.string.toast_failure)
|
||||
} else {
|
||||
getApplication<AngApplication>().toastSuccess(R.string.toast_success)
|
||||
MmkvManager.setSelectServer(key)
|
||||
reloadServerList()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes assets.
|
||||
* @param assets The asset manager.
|
||||
|
||||
@@ -51,7 +51,6 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
|
||||
AppConfig.PREF_SOCKS_PORT,
|
||||
AppConfig.PREF_LOGLEVEL,
|
||||
AppConfig.PREF_OUTBOUND_DOMAIN_RESOLVE_METHOD,
|
||||
AppConfig.PREF_INTELLIGENT_SELECTION_METHOD,
|
||||
AppConfig.PREF_LANGUAGE,
|
||||
AppConfig.PREF_UI_MODE_NIGHT,
|
||||
AppConfig.PREF_ROUTING_DOMAIN_STRATEGY,
|
||||
|
||||
Reference in New Issue
Block a user