From ece35b9a1c42e30f3caed47159a0a1eed38f4ead Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sun, 29 Sep 2024 15:34:54 +0800 Subject: [PATCH] Import and export ruleset via clipboard https://github.com/2dust/v2rayCustomRoutingList/blob/master/custom_routing_rules_blacklist https://github.com/2dust/v2rayCustomRoutingList/blob/master/custom_routing_rules_whitelist --- .../v2ray/ang/ui/RoutingSettingActivity.kt | 39 +++++++++++++++++++ .../com/v2ray/ang/util/SettingsManager.kt | 25 +++++++++++- .../main/res/menu/menu_routing_setting.xml | 8 ++++ .../app/src/main/res/values-ar/strings.xml | 2 + .../app/src/main/res/values-bn/strings.xml | 2 + .../app/src/main/res/values-fa/strings.xml | 2 + .../app/src/main/res/values-ru/strings.xml | 2 + .../app/src/main/res/values-vi/strings.xml | 2 + .../src/main/res/values-zh-rCN/strings.xml | 2 + .../src/main/res/values-zh-rTW/strings.xml | 2 + V2rayNG/app/src/main/res/values/strings.xml | 2 + 11 files changed, 87 insertions(+), 1 deletion(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingActivity.kt index b67915c6..0789934d 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingActivity.kt @@ -10,6 +10,7 @@ import androidx.appcompat.app.AlertDialog import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager +import com.google.gson.Gson import com.v2ray.ang.AppConfig import com.v2ray.ang.R import com.v2ray.ang.databinding.ActivityRoutingSettingBinding @@ -108,6 +109,44 @@ class RoutingSettingActivity : BaseActivity() { true } + R.id.import_rulesets_from_clipboard -> { + AlertDialog.Builder(this).setMessage(R.string.routing_settings_import_rulesets_tip) + .setPositiveButton(android.R.string.ok) { _, _ -> + try { + val clipboard = Utils.getClipboard(this) + lifecycleScope.launch(Dispatchers.IO) { + val ret = SettingsManager.resetRoutingRulesetsFromClipboard(clipboard) + launch(Dispatchers.Main) { + if (ret) { + refreshData() + toast(R.string.toast_success) + } else { + toast(R.string.toast_failure) + } + } + } + } catch (e: Exception) { + e.printStackTrace() + } + } + .setNegativeButton(android.R.string.no) { _, _ -> + //do noting + } + .show() + true + } + + R.id.export_rulesets_to_clipboard -> { + val rulesetList = MmkvManager.decodeRoutingRulesets() + if (rulesetList.isNullOrEmpty()) { + toast(R.string.toast_failure) + } else { + Utils.setClipboard(this, Gson().toJson(rulesetList)) + toast(R.string.toast_success) + } + true + } + else -> super.onOptionsItemSelected(item) } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/SettingsManager.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/SettingsManager.kt index 22c73c7d..c8eaf7e5 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/SettingsManager.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/SettingsManager.kt @@ -36,6 +36,30 @@ object SettingsManager { } fun resetRoutingRulesets(context: Context, index: Int) { + val rulesetList = getPresetRoutingRulesets(context, index) ?: return + resetRoutingRulesetsCommon(rulesetList) + } + + fun resetRoutingRulesetsFromClipboard(content: String?): Boolean { + if (content.isNullOrEmpty()) { + return false + } + + try { + val rulesetList = Gson().fromJson(content, Array::class.java).toMutableList() + if (rulesetList.isNullOrEmpty()) { + return false + } + + resetRoutingRulesetsCommon(rulesetList) + return true + } catch (e: Exception) { + e.printStackTrace() + return false + } + } + + private fun resetRoutingRulesetsCommon(rulesetList: MutableList) { val rulesetNew: MutableList = mutableListOf() MmkvManager.decodeRoutingRulesets()?.forEach { key -> if (key.looked == true) { @@ -43,7 +67,6 @@ object SettingsManager { } } - val rulesetList = getPresetRoutingRulesets(context, index) ?: return rulesetNew.addAll(rulesetList) MmkvManager.encodeRoutingRulesets(rulesetNew) } diff --git a/V2rayNG/app/src/main/res/menu/menu_routing_setting.xml b/V2rayNG/app/src/main/res/menu/menu_routing_setting.xml index afa71bc0..1de8c547 100644 --- a/V2rayNG/app/src/main/res/menu/menu_routing_setting.xml +++ b/V2rayNG/app/src/main/res/menu/menu_routing_setting.xml @@ -14,5 +14,13 @@ android:id="@+id/import_rulesets" android:title="@string/routing_settings_import_rulesets" app:showAsAction="never" /> + + \ No newline at end of file diff --git a/V2rayNG/app/src/main/res/values-ar/strings.xml b/V2rayNG/app/src/main/res/values-ar/strings.xml index 83b5399c..029afdf0 100644 --- a/V2rayNG/app/src/main/res/values-ar/strings.xml +++ b/V2rayNG/app/src/main/res/values-ar/strings.xml @@ -261,6 +261,8 @@ Add rule Import ruleset Existing rulesets will be deleted, are you sure to continue? + Import ruleset from clipboard + Export ruleset to clipboard Locked, keep this rule when import presets التحقق من الاتصال diff --git a/V2rayNG/app/src/main/res/values-bn/strings.xml b/V2rayNG/app/src/main/res/values-bn/strings.xml index a147c5be..f4231d5d 100644 --- a/V2rayNG/app/src/main/res/values-bn/strings.xml +++ b/V2rayNG/app/src/main/res/values-bn/strings.xml @@ -259,6 +259,8 @@ Add rule Import ruleset Existing rulesets will be deleted, are you sure to continue? + Import ruleset from clipboard + Export ruleset to clipboard Locked, keep this rule when import presets সংযোগ পরীক্ষা করুন diff --git a/V2rayNG/app/src/main/res/values-fa/strings.xml b/V2rayNG/app/src/main/res/values-fa/strings.xml index 4993e6af..17413dd2 100644 --- a/V2rayNG/app/src/main/res/values-fa/strings.xml +++ b/V2rayNG/app/src/main/res/values-fa/strings.xml @@ -257,6 +257,8 @@ Add rule Import ruleset Existing rulesets will be deleted, are you sure to continue? + Import ruleset from clipboard + Export ruleset to clipboard Locked, keep this rule when import presets اتصال را بررسی کنید diff --git a/V2rayNG/app/src/main/res/values-ru/strings.xml b/V2rayNG/app/src/main/res/values-ru/strings.xml index 9489aeb3..215f08eb 100644 --- a/V2rayNG/app/src/main/res/values-ru/strings.xml +++ b/V2rayNG/app/src/main/res/values-ru/strings.xml @@ -260,6 +260,8 @@ Добавить правило Импорт правил Существующие правила будут удалены. Продолжить? + Import ruleset from clipboard + Export ruleset to clipboard Постоянное (сохранится при импорте правил) Домен IP diff --git a/V2rayNG/app/src/main/res/values-vi/strings.xml b/V2rayNG/app/src/main/res/values-vi/strings.xml index 7c2cfbaa..6fd61e59 100644 --- a/V2rayNG/app/src/main/res/values-vi/strings.xml +++ b/V2rayNG/app/src/main/res/values-vi/strings.xml @@ -260,6 +260,8 @@ Add rule Import ruleset Existing rulesets will be deleted, are you sure to continue? + Import ruleset from clipboard + Export ruleset to clipboard Locked, keep this rule when import presets Kiểm tra kết nối diff --git a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml index 147b61b5..8ff73af0 100644 --- a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml @@ -257,6 +257,8 @@ 添加规则 导入预设规则集 将删除现有的规则集,是否确定继续? + 从剪贴板导入规则集 + 导出规则集至剪贴板 锁定中,导入预设时不删除此规则 "检查网络连接" diff --git a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml index e9b1b472..c36bd372 100644 --- a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml @@ -259,6 +259,8 @@ 新增規則 匯入預設規則集 將刪除現有的規則集,是否確定繼續? + 從剪貼簿匯入規則集 + 匯出規則集至剪貼簿 鎖定中,匯入預設時不刪除此規則 "測試連線能力" diff --git a/V2rayNG/app/src/main/res/values/strings.xml b/V2rayNG/app/src/main/res/values/strings.xml index b93b66a3..9ee304d8 100644 --- a/V2rayNG/app/src/main/res/values/strings.xml +++ b/V2rayNG/app/src/main/res/values/strings.xml @@ -263,6 +263,8 @@ Add rule Import ruleset Existing rulesets will be deleted, are you sure to continue? + Import ruleset from clipboard + Export ruleset to clipboard Locked, keep this rule when import presets domain ip