From 338f13c8bcf3db4bac04be3046c25a72614c9041 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 --- .../neko/v2ray/ui/RoutingSettingActivity.kt | 39 +++++++++++++++++++ .../com/neko/v2ray/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-fa/strings.xml | 2 + app/src/main/res/values-in/strings.xml | 2 + app/src/main/res/values-ja/strings.xml | 2 + app/src/main/res/values-jw/strings.xml | 2 + app/src/main/res/values-ru/strings.xml | 2 + app/src/main/res/values-su/strings.xml | 2 + app/src/main/res/values-vi/strings.xml | 2 + app/src/main/res/values-zh-rCN/strings.xml | 2 + app/src/main/res/values-zh-rTW/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + 14 files changed, 93 insertions(+), 1 deletion(-) 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 acc7b8db..98633a3a 100644 --- a/app/src/main/kotlin/com/neko/v2ray/ui/RoutingSettingActivity.kt +++ b/app/src/main/kotlin/com/neko/v2ray/ui/RoutingSettingActivity.kt @@ -12,6 +12,7 @@ import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.appbar.MaterialToolbar import com.google.android.material.appbar.CollapsingToolbarLayout +import com.google.gson.Gson import com.neko.v2ray.AppConfig import com.neko.v2ray.R import com.neko.v2ray.databinding.ActivityRoutingSettingBinding @@ -112,6 +113,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/app/src/main/kotlin/com/neko/v2ray/util/SettingsManager.kt b/app/src/main/kotlin/com/neko/v2ray/util/SettingsManager.kt index f380c4cd..d4e7e95b 100644 --- a/app/src/main/kotlin/com/neko/v2ray/util/SettingsManager.kt +++ b/app/src/main/kotlin/com/neko/v2ray/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/app/src/main/res/menu/menu_routing_setting.xml b/app/src/main/res/menu/menu_routing_setting.xml index afa71bc0..1de8c547 100644 --- a/app/src/main/res/menu/menu_routing_setting.xml +++ b/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/app/src/main/res/values-ar/strings.xml b/app/src/main/res/values-ar/strings.xml index 8aa08255..492a52d6 100644 --- a/app/src/main/res/values-ar/strings.xml +++ b/app/src/main/res/values-ar/strings.xml @@ -246,6 +246,8 @@ إضافة القاعدة استيراد مجموعة القواعد سيتم حذف مجموعات القواعد الحالية، هل أنت متأكد من الاستمرار؟ + استيراد مجموعة القواعد من الحافظة + تصدير مجموعة القواعد إلى الحافظة مقفل، حافظ على هذه القاعدة عند استيراد الإعدادات المسبقة تحقق من الاتصال diff --git a/app/src/main/res/values-fa/strings.xml b/app/src/main/res/values-fa/strings.xml index 6137f387..8bb4630a 100644 --- a/app/src/main/res/values-fa/strings.xml +++ b/app/src/main/res/values-fa/strings.xml @@ -248,6 +248,8 @@ افزودن قانون وارد کردن مجموعه قوانین مجموعه قوانین موجود حذف خواهند شد، آیا مطمئن هستید که ادامه می دهید؟ + وارد کردن مجموعه قوانین از کلیپ بورد + صادر کردن مجموعه قوانین به کلیپ بورد قفل است، این قانون را هنگام وارد کردن از پیش تنظیمات حفظ کنید اتصال را بررسی کنید diff --git a/app/src/main/res/values-in/strings.xml b/app/src/main/res/values-in/strings.xml index 1956e736..43052985 100644 --- a/app/src/main/res/values-in/strings.xml +++ b/app/src/main/res/values-in/strings.xml @@ -246,6 +246,8 @@ Tambah aturan Impor rangkaian aturan Rangkaian aturan yang ada akan dihapus, apakah Anda yakin untuk melanjutkan? + Impor set aturan dari papan klip + Ekspor set aturan ke papan klip Terkunci, pertahankan aturan ini saat mengimpor preset Periksa Konektivitas diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 3bf6f959..58f49f4a 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -246,6 +246,8 @@ ルールを追加する ルールセットをインポートする 既存のルールセットは削除されます。続行しますか? + クリップボードからルールセットをインポート + ルールセットをクリップボードにエクスポート ロックされています。プリセットをインポートするときにこのルールを維持します 接続をチェックする diff --git a/app/src/main/res/values-jw/strings.xml b/app/src/main/res/values-jw/strings.xml index 27af2d7f..3214a36b 100644 --- a/app/src/main/res/values-jw/strings.xml +++ b/app/src/main/res/values-jw/strings.xml @@ -246,6 +246,8 @@ Tambah aturan Set aturan impor Setel aturan sing ana bakal dibusak, sampeyan yakin bakal terus? + Impor aturan saka clipboard + Ekspor set aturan menyang clipboard Dikunci, tetep aturan iki nalika ngimpor preset Priksa Konektivitas diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index ec477b67..f7bb5807 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -246,6 +246,8 @@ Добавление правила Импорт набора правил Существующие наборы правил будут удалены. Вы уверены, что хотите продолжить? + Импортировать набор правил из буфера обмена + Экспорт набора правил в буфер обмена Заблокировано, сохранить это правило при импорте предустановок Проверьте подключение diff --git a/app/src/main/res/values-su/strings.xml b/app/src/main/res/values-su/strings.xml index f7803120..55874ef0 100644 --- a/app/src/main/res/values-su/strings.xml +++ b/app/src/main/res/values-su/strings.xml @@ -246,6 +246,8 @@ Tambahkeun aturan Impor set aturan Setélan aturan anu aya bakal dipupus, naha anjeun yakin bakal neruskeun? + Impor set aturan tina clipboard + Ékspor aturan ka clipboard Dikonci, jaga aturan ieu nalika impor preset Pariksa Konéktipitas diff --git a/app/src/main/res/values-vi/strings.xml b/app/src/main/res/values-vi/strings.xml index 681bc9c1..88f5b08a 100644 --- a/app/src/main/res/values-vi/strings.xml +++ b/app/src/main/res/values-vi/strings.xml @@ -246,6 +246,8 @@ Thêm quy tắc Nhập bộ quy tắc Bộ quy tắc hiện có sẽ bị xóa, bạn có chắc chắn muốn tiếp tục không? + Nhập bộ quy tắc từ clipboard + Xuất bộ quy tắc vào clipboard Đã khóa, giữ nguyên quy tắc này khi nhập cài đặt trước Kiểm tra kết nối diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index eba969bf..abd05828 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -246,6 +246,8 @@ 添加规则 导入规则集 现有规则集将被删除,确定继续吗? + 从剪贴板导入规则集 + 将规则集导出到剪贴板 已锁定,导入预设时保留此规则 检查连接性 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index de12bb3c..7e03b13b 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -246,6 +246,8 @@ 新增規則 匯入規則集 現有規則集將被刪除,確定繼續嗎? + 從剪貼簿匯入規則集 + 將規則集匯出到剪貼簿 鎖定,匯入預設時保留此規則 檢查連接性 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c521d901..95430bec 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -249,6 +249,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 Check Connectivity