From 5373579bd588e0c08700114f4f1bafa29e51d146 Mon Sep 17 00:00:00 2001 From: Chocolate4U Date: Wed, 20 Nov 2024 04:53:08 +0330 Subject: [PATCH] Import rulesets from qrcode (#3991) * Renamed functions to be more semantically accurate * Import rulesets from qrcode Add capability to import rulesets from qrcode fixes and improvements --- .../com/v2ray/ang/handler/SettingsManager.kt | 4 +- .../v2ray/ang/ui/RoutingSettingActivity.kt | 49 +++++++++++++++++-- .../main/res/menu/menu_routing_setting.xml | 8 ++- .../app/src/main/res/values-ar/strings.xml | 3 +- .../app/src/main/res/values-bn/strings.xml | 3 +- .../src/main/res/values-bqi-rIR/strings.xml | 3 +- .../app/src/main/res/values-fa/strings.xml | 3 +- .../app/src/main/res/values-ru/strings.xml | 3 +- .../app/src/main/res/values-vi/strings.xml | 3 +- .../src/main/res/values-zh-rCN/strings.xml | 3 +- .../src/main/res/values-zh-rTW/strings.xml | 3 +- V2rayNG/app/src/main/res/values/strings.xml | 3 +- 12 files changed, 72 insertions(+), 16 deletions(-) diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/handler/SettingsManager.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/handler/SettingsManager.kt index 29ebfb8e8..08d49b5af 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/handler/SettingsManager.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/handler/SettingsManager.kt @@ -43,12 +43,12 @@ object SettingsManager { } - fun resetRoutingRulesets(context: Context, index: Int) { + fun resetRoutingRulesetsFromPresets(context: Context, index: Int) { val rulesetList = getPresetRoutingRulesets(context, index) ?: return resetRoutingRulesetsCommon(rulesetList) } - fun resetRoutingRulesetsFromClipboard(content: String?): Boolean { + fun resetRoutingRulesets(content: String?): Boolean { if (content.isNullOrEmpty()) { return false } diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/RoutingSettingActivity.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/RoutingSettingActivity.kt index 2f267fd67..d3820305f 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/RoutingSettingActivity.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/RoutingSettingActivity.kt @@ -1,15 +1,18 @@ package com.v2ray.ang.ui +import android.Manifest import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.View import android.widget.AdapterView +import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AlertDialog import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager +import com.tbruyelle.rxpermissions3.RxPermissions import com.v2ray.ang.AppConfig import com.v2ray.ang.R import com.v2ray.ang.databinding.ActivityRoutingSettingBinding @@ -83,13 +86,13 @@ class RoutingSettingActivity : BaseActivity() { true } - R.id.import_rulesets -> { + R.id.import_predefined_rulesets -> { AlertDialog.Builder(this).setMessage(R.string.routing_settings_import_rulesets_tip) .setPositiveButton(android.R.string.ok) { _, _ -> AlertDialog.Builder(this).setItems(preset_rulesets.asList().toTypedArray()) { _, i -> try { lifecycleScope.launch(Dispatchers.IO) { - SettingsManager.resetRoutingRulesets(this@RoutingSettingActivity, i) + SettingsManager.resetRoutingRulesetsFromPresets(this@RoutingSettingActivity, i) launch(Dispatchers.Main) { refreshData() toast(R.string.toast_success) @@ -120,7 +123,7 @@ class RoutingSettingActivity : BaseActivity() { return@setPositiveButton } lifecycleScope.launch(Dispatchers.IO) { - val result = SettingsManager.resetRoutingRulesetsFromClipboard(clipboard) + val result = SettingsManager.resetRoutingRulesets(clipboard) withContext(Dispatchers.Main) { if (result) { refreshData() @@ -138,6 +141,18 @@ class RoutingSettingActivity : BaseActivity() { true } + R.id.import_rulesets_from_qrcode -> { + RxPermissions(this) + .request(Manifest.permission.CAMERA) + .subscribe { + if (it) + scanQRcodeForRulesets.launch(Intent(this, ScannerActivity::class.java)) + else + toast(R.string.toast_permission_denied) + } + true + } + R.id.export_rulesets_to_clipboard -> { val rulesetList = MmkvManager.decodeRoutingRulesets() @@ -153,6 +168,34 @@ class RoutingSettingActivity : BaseActivity() { else -> super.onOptionsItemSelected(item) } + private val scanQRcodeForRulesets = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { + if (it.resultCode == RESULT_OK) { + importRulesetsFromQRcode(it.data?.getStringExtra("SCAN_RESULT")) + } + } + + private fun importRulesetsFromQRcode(qrcode: String?): Boolean { + AlertDialog.Builder(this).setMessage(R.string.routing_settings_import_rulesets_tip) + .setPositiveButton(android.R.string.ok) { _, _ -> + lifecycleScope.launch(Dispatchers.IO) { + val result = SettingsManager.resetRoutingRulesets(qrcode) + withContext(Dispatchers.Main) { + if (result) { + refreshData() + toast(R.string.toast_success) + } else { + toast(R.string.toast_failure) + } + } + } + } + .setNegativeButton(android.R.string.no) { _, _ -> + //do nothing + } + .show() + return true + } + fun refreshData() { rulesets.clear() rulesets.addAll(MmkvManager.decodeRoutingRulesets() ?: mutableListOf()) 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 1de8c547b..86a9c2f8d 100644 --- a/V2rayNG/app/src/main/res/menu/menu_routing_setting.xml +++ b/V2rayNG/app/src/main/res/menu/menu_routing_setting.xml @@ -11,13 +11,17 @@ android:icon="@drawable/ic_file_24dp" android:title="@string/title_user_asset_setting" /> + مسح Routing Rule Settings 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 620796d42..605677664 100644 --- a/V2rayNG/app/src/main/res/values-bn/strings.xml +++ b/V2rayNG/app/src/main/res/values-bn/strings.xml @@ -265,9 +265,10 @@ মুছে ফেলুন Routing Rule Settings Add rule - Import ruleset + পূর্বনির্ধারিত নিয়মাবলী আমদানি করুন Existing rulesets will be deleted, are you sure to continue? Import ruleset from clipboard + QRcode থেকে রুলসেট আমদানি করুন Export ruleset to clipboard Locked, keep this rule when import presets diff --git a/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml b/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml index 250e9488f..e1e88df0a 100644 --- a/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml +++ b/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml @@ -267,9 +267,10 @@ روفتن سامووا قانۉݩ تور جوستن ازاف کردن قانۉݩ - و من ٱووردن قانووا + و من ٱووردن قانووا قانووایی ک هیم سکو هڌسۉݩ پاک ابۊن، هنی هم اخۊی پاکسۉݩ کۊنی؟ و من ٱووردن قانووا ز کلیپ بورد + و من ٱووردن قانووا ز QRcode و در کشیڌن قانووا وو زفت من کلیپ بورد چفت هڌ، ای قانؤنن مجال و من ٱووردن ز پؽش سامووا زفت کۊنین domain diff --git a/V2rayNG/app/src/main/res/values-fa/strings.xml b/V2rayNG/app/src/main/res/values-fa/strings.xml index a15ba6fbb..71174eb9a 100644 --- a/V2rayNG/app/src/main/res/values-fa/strings.xml +++ b/V2rayNG/app/src/main/res/values-fa/strings.xml @@ -263,9 +263,10 @@ حذف تنظیمات قانون مسیریابی اضافه کردن قانون - وارد کردن مجموعه قوانین + وارد کردن مجموعه قوانین از پیش تعریف شده مجموعه قوانین موجود حذف خواهند شد، آیا مطمئن هستید که ادامه می دهید؟ وارد کردن مجموعه قوانین از کلیپ بورد + وارد کردن مجموعه قوانین از QRcode صادر کردن مجموعه قوانین به کلیپ بورد قفل است، این قانون را هنگام وارد کردن از پیش تنظیم‌ها حفظ کنید diff --git a/V2rayNG/app/src/main/res/values-ru/strings.xml b/V2rayNG/app/src/main/res/values-ru/strings.xml index 6c9084a7b..a32897765 100644 --- a/V2rayNG/app/src/main/res/values-ru/strings.xml +++ b/V2rayNG/app/src/main/res/values-ru/strings.xml @@ -266,9 +266,10 @@ Очистить Настройка правил маршрутизации Добавить правило - Импорт правил + Импорт предопределенных наборов правил Существующие правила будут удалены. Продолжить? Импорт правил из буфера обмена + Импорт набора правил из QRcode Экспорт правил в буфер обмена Постоянное (сохранится при импорте правил) Домен diff --git a/V2rayNG/app/src/main/res/values-vi/strings.xml b/V2rayNG/app/src/main/res/values-vi/strings.xml index 1cdcd98cb..9854892e1 100644 --- a/V2rayNG/app/src/main/res/values-vi/strings.xml +++ b/V2rayNG/app/src/main/res/values-vi/strings.xml @@ -266,9 +266,10 @@ Xoá Routing Rule Settings Add rule - Import ruleset + Nhập các bộ quy tắc được xác định trước Existing rulesets will be deleted, are you sure to continue? Import ruleset from clipboard + Nhập bộ quy tắc từ QRcode Export ruleset to clipboard Locked, keep this rule when import presets 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 3254fc9d2..213a945ec 100644 --- a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml @@ -263,9 +263,10 @@ 清空 路由规则设置 添加规则 - 导入预设规则集 + 导入预定义规则集 将删除现有的规则集,是否确定继续? 从剪贴板导入规则集 + 从 QRcode 导入规则集 导出规则集至剪贴板 锁定中,导入预设时不删除此规则 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 5334ef073..bc74f9b89 100644 --- a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml @@ -265,9 +265,10 @@ 清除 路由規則設定 新增規則 - 匯入預設規則集 + 匯入預先定義的規則集 將刪除現有的規則集,是否確定繼續? 從剪貼簿匯入規則集 + 從 QRcode 匯入規則集 匯出規則集至剪貼簿 鎖定中,匯入預設時不刪除此規則 diff --git a/V2rayNG/app/src/main/res/values/strings.xml b/V2rayNG/app/src/main/res/values/strings.xml index 5dc20105d..fe9c4cc10 100644 --- a/V2rayNG/app/src/main/res/values/strings.xml +++ b/V2rayNG/app/src/main/res/values/strings.xml @@ -270,9 +270,10 @@ Clear Routing Rule Settings Add rule - Import ruleset + Import predefined rulesets Existing rulesets will be deleted, are you sure to continue? Import ruleset from clipboard + Import ruleset from QRcode Export ruleset to clipboard Locked, keep this rule when import presets domain