From ca16c226ac11864869852c270b129a4580017a3f Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sat, 21 Sep 2024 16:44:15 +0800 Subject: [PATCH] Add preset ruleset routing --- app/src/main/assets/custom_routing_block | 3 --- app/src/main/assets/custom_routing_direct | 1 - app/src/main/assets/custom_routing_proxy | 1 - .../neko/v2ray/ui/RoutingSettingActivity.kt | 23 ++++++++++++++----- .../com/neko/v2ray/util/SettingsManager.kt | 18 ++++++++------- app/src/main/res/values-zh-rCN/arrays.xml | 6 +++++ app/src/main/res/values-zh-rTW/arrays.xml | 6 +++++ app/src/main/res/values/arrays.xml | 6 +++++ 8 files changed, 45 insertions(+), 19 deletions(-) delete mode 100644 app/src/main/assets/custom_routing_block delete mode 100644 app/src/main/assets/custom_routing_direct delete mode 100644 app/src/main/assets/custom_routing_proxy diff --git a/app/src/main/assets/custom_routing_block b/app/src/main/assets/custom_routing_block deleted file mode 100644 index 74febdf2..00000000 --- a/app/src/main/assets/custom_routing_block +++ /dev/null @@ -1,3 +0,0 @@ -geosite:category-ads-all, -geosite:oisd-full, -geosite:rule-ads \ No newline at end of file diff --git a/app/src/main/assets/custom_routing_direct b/app/src/main/assets/custom_routing_direct deleted file mode 100644 index 083428bf..00000000 --- a/app/src/main/assets/custom_routing_direct +++ /dev/null @@ -1 +0,0 @@ -geosite:cn \ No newline at end of file diff --git a/app/src/main/assets/custom_routing_proxy b/app/src/main/assets/custom_routing_proxy deleted file mode 100644 index 618b1401..00000000 --- a/app/src/main/assets/custom_routing_proxy +++ /dev/null @@ -1 +0,0 @@ -geosite:geolocation-!cn \ No newline at end of file diff --git a/app/src/main/kotlin/com/neko/v2ray/ui/RoutingSettingActivity.kt b/app/src/main/kotlin/com/neko/v2ray/ui/RoutingSettingActivity.kt index d88a482c..a35a7f42 100644 --- a/app/src/main/kotlin/com/neko/v2ray/ui/RoutingSettingActivity.kt +++ b/app/src/main/kotlin/com/neko/v2ray/ui/RoutingSettingActivity.kt @@ -31,6 +31,9 @@ class RoutingSettingActivity : BaseActivity() { private val routing_domain_strategy: Array by lazy { resources.getStringArray(R.array.routing_domain_strategy) } + private val preset_rulesets: Array by lazy { + resources.getStringArray(R.array.preset_rulesets) + } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -80,13 +83,21 @@ class RoutingSettingActivity : BaseActivity() { R.id.import_rulesets -> { AlertDialog.Builder(this).setMessage(R.string.routing_settings_import_rulesets_tip) .setPositiveButton(android.R.string.ok) { _, _ -> - lifecycleScope.launch(Dispatchers.IO) { - SettingsManager.resetRoutingRulesets(this@RoutingSettingActivity) - launch(Dispatchers.Main) { - refreshData() - toast(R.string.toast_success) + AlertDialog.Builder(this).setItems(preset_rulesets.asList().toTypedArray()) { _, i -> + try { + lifecycleScope.launch(Dispatchers.IO) { + SettingsManager.resetRoutingRulesets(this@RoutingSettingActivity, i) + launch(Dispatchers.Main) { + refreshData() + toast(R.string.toast_success) + } + } + } catch (e: Exception) { + e.printStackTrace() } - } + }.show() + + } .setNegativeButton(android.R.string.no) { _, _ -> //do noting diff --git a/app/src/main/kotlin/com/neko/v2ray/util/SettingsManager.kt b/app/src/main/kotlin/com/neko/v2ray/util/SettingsManager.kt index 1f5e85e4..32be1fb2 100644 --- a/app/src/main/kotlin/com/neko/v2ray/util/SettingsManager.kt +++ b/app/src/main/kotlin/com/neko/v2ray/util/SettingsManager.kt @@ -2,32 +2,34 @@ package com.neko.v2ray.util import android.content.Context import android.text.TextUtils -import android.util.Log import com.google.gson.Gson import com.neko.v2ray.dto.RulesetItem object SettingsManager { - fun initRoutingRulesets(context: Context) { - Log.d("=====", "initRoutingRuleset") + fun initRoutingRulesets(context: Context, index: Int = 0) { val exist = MmkvManager.decodeRoutingRulesets() + val fileName = when (index) { + 0 -> "custom_routing_white" + 1 -> "custom_routing_black" + 2 -> "custom_routing_global" + else -> "custom_routing_white" + } if (exist.isNullOrEmpty()) { - Log.d("=====", "isNullOrEmpty") - val assets = Utils.readTextFromAssets(context, "custom_routing_white") + val assets = Utils.readTextFromAssets(context, fileName) if (TextUtils.isEmpty(assets)) { return } val rulesetList = Gson().fromJson(assets, Array::class.java).toMutableList() - Log.d("=====", "rulesetList==" + rulesetList.count()) MmkvManager.encodeRoutingRulesets(rulesetList) } } - fun resetRoutingRulesets(context: Context) { + fun resetRoutingRulesets(context: Context, index: Int) { MmkvManager.encodeRoutingRulesets(null) - initRoutingRulesets(context) + initRoutingRulesets(context, index) } fun getRoutingRuleset(index: Int): RulesetItem? { diff --git a/app/src/main/res/values-zh-rCN/arrays.xml b/app/src/main/res/values-zh-rCN/arrays.xml index 73128ec1..477ff52a 100644 --- a/app/src/main/res/values-zh-rCN/arrays.xml +++ b/app/src/main/res/values-zh-rCN/arrays.xml @@ -21,4 +21,10 @@ VPN 仅代理 + + + 绕过大陆(Whitelist) + 黑名单(Blacklist) + 全局(Global) + diff --git a/app/src/main/res/values-zh-rTW/arrays.xml b/app/src/main/res/values-zh-rTW/arrays.xml index 639ff6ae..00009b5c 100644 --- a/app/src/main/res/values-zh-rTW/arrays.xml +++ b/app/src/main/res/values-zh-rTW/arrays.xml @@ -21,4 +21,10 @@ VPN 僅 Proxy + + + 绕过大陆(Whitelist) + 黑名单(Blacklist) + 全局(Global) + diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 9cf7eaa7..8bd82756 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -21,4 +21,10 @@ VPN Proxy only + + + China Whitelist + China Blacklist + Global +