Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Import and export ruleset via clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust authored and AnGgIt86 committed Sep 30, 2024
1 parent fd63c83 commit 338f13c
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 1 deletion.
39 changes: 39 additions & 0 deletions app/src/main/kotlin/com/neko/v2ray/ui/RoutingSettingActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
25 changes: 24 additions & 1 deletion app/src/main/kotlin/com/neko/v2ray/util/SettingsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,37 @@ 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<RulesetItem>::class.java).toMutableList()
if (rulesetList.isNullOrEmpty()) {
return false
}

resetRoutingRulesetsCommon(rulesetList)
return true
} catch (e: Exception) {
e.printStackTrace()
return false
}
}

private fun resetRoutingRulesetsCommon(rulesetList: MutableList<RulesetItem>) {
val rulesetNew: MutableList<RulesetItem> = mutableListOf()
MmkvManager.decodeRoutingRulesets()?.forEach { key ->
if (key.looked == true) {
rulesetNew.add(key)
}
}

val rulesetList = getPresetRoutingRulesets(context, index) ?: return
rulesetNew.addAll(rulesetList)
MmkvManager.encodeRoutingRulesets(rulesetNew)
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/menu/menu_routing_setting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@
android:id="@+id/import_rulesets"
android:title="@string/routing_settings_import_rulesets"
app:showAsAction="never" />
<item
android:id="@+id/import_rulesets_from_clipboard"
android:title="@string/routing_settings_import_rulesets_from_clipboard"
app:showAsAction="never" />
<item
android:id="@+id/export_rulesets_to_clipboard"
android:title="@string/routing_settings_export_rulesets_to_clipboard"
app:showAsAction="never" />

</menu>
2 changes: 2 additions & 0 deletions app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@
<string name="routing_settings_add_rule">إضافة القاعدة</string>
<string name="routing_settings_import_rulesets">استيراد مجموعة القواعد</string>
<string name="routing_settings_import_rulesets_tip">سيتم حذف مجموعات القواعد الحالية، هل أنت متأكد من الاستمرار؟</string>
<string name="routing_settings_import_rulesets_from_clipboard">استيراد مجموعة القواعد من الحافظة</string>
<string name="routing_settings_export_rulesets_to_clipboard">تصدير مجموعة القواعد إلى الحافظة</string>
<string name="routing_settings_locked">مقفل، حافظ على هذه القاعدة عند استيراد الإعدادات المسبقة</string>

<string name="connection_test_pending">تحقق من الاتصال</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-fa/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@
<string name="routing_settings_add_rule">افزودن قانون</string>
<string name="routing_settings_import_rulesets">وارد کردن مجموعه قوانین</string>
<string name="routing_settings_import_rulesets_tip">مجموعه قوانین موجود حذف خواهند شد، آیا مطمئن هستید که ادامه می دهید؟</string>
<string name="routing_settings_import_rulesets_from_clipboard">وارد کردن مجموعه قوانین از کلیپ بورد</string>
<string name="routing_settings_export_rulesets_to_clipboard">صادر کردن مجموعه قوانین به کلیپ بورد</string>
<string name="routing_settings_locked">قفل است، این قانون را هنگام وارد کردن از پیش تنظیمات حفظ کنید</string>

<string name="connection_test_pending">اتصال را بررسی کنید</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-in/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@
<string name="routing_settings_add_rule">Tambah aturan</string>
<string name="routing_settings_import_rulesets">Impor rangkaian aturan</string>
<string name="routing_settings_import_rulesets_tip">Rangkaian aturan yang ada akan dihapus, apakah Anda yakin untuk melanjutkan?</string>
<string name="routing_settings_import_rulesets_from_clipboard">Impor set aturan dari papan klip</string>
<string name="routing_settings_export_rulesets_to_clipboard">Ekspor set aturan ke papan klip</string>
<string name="routing_settings_locked">Terkunci, pertahankan aturan ini saat mengimpor preset</string>

<string name="connection_test_pending">Periksa Konektivitas</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@
<string name="routing_settings_add_rule">ルールを追加する</string>
<string name="routing_settings_import_rulesets">ルールセットをインポートする</string>
<string name="routing_settings_import_rulesets_tip">既存のルールセットは削除されます。続行しますか?</string>
<string name="routing_settings_import_rulesets_from_clipboard">クリップボードからルールセットをインポート</string>
<string name="routing_settings_export_rulesets_to_clipboard">ルールセットをクリップボードにエクスポート</string>
<string name="routing_settings_locked">ロックされています。プリセットをインポートするときにこのルールを維持します</string>

<string name="connection_test_pending">接続をチェックする</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-jw/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@
<string name="routing_settings_add_rule">Tambah aturan</string>
<string name="routing_settings_import_rulesets">Set aturan impor</string>
<string name="routing_settings_import_rulesets_tip">Setel aturan sing ana bakal dibusak, sampeyan yakin bakal terus?</string>
<string name="routing_settings_import_rulesets_from_clipboard">Impor aturan saka clipboard</string>
<string name="routing_settings_export_rulesets_to_clipboard">Ekspor set aturan menyang clipboard</string>
<string name="routing_settings_locked">Dikunci, tetep aturan iki nalika ngimpor preset</string>

<string name="connection_test_pending">Priksa Konektivitas</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@
<string name="routing_settings_add_rule">Добавление правила</string>
<string name="routing_settings_import_rulesets">Импорт набора правил</string>
<string name="routing_settings_import_rulesets_tip">Существующие наборы правил будут удалены. Вы уверены, что хотите продолжить?</string>
<string name="routing_settings_import_rulesets_from_clipboard">Импортировать набор правил из буфера обмена</string>
<string name="routing_settings_export_rulesets_to_clipboard">Экспорт набора правил в буфер обмена</string>
<string name="routing_settings_locked">Заблокировано, сохранить это правило при импорте предустановок</string>

<string name="connection_test_pending">Проверьте подключение</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-su/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@
<string name="routing_settings_add_rule">Tambahkeun aturan</string>
<string name="routing_settings_import_rulesets">Impor set aturan</string>
<string name="routing_settings_import_rulesets_tip">Setélan aturan anu aya bakal dipupus, naha anjeun yakin bakal neruskeun?</string>
<string name="routing_settings_import_rulesets_from_clipboard">Impor set aturan tina clipboard</string>
<string name="routing_settings_export_rulesets_to_clipboard">Ékspor aturan ka clipboard</string>
<string name="routing_settings_locked">Dikonci, jaga aturan ieu nalika impor preset</string>

<string name="connection_test_pending">Pariksa Konéktipitas</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-vi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@
<string name="routing_settings_add_rule">Thêm quy tắc</string>
<string name="routing_settings_import_rulesets">Nhập bộ quy tắc</string>
<string name="routing_settings_import_rulesets_tip">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?</string>
<string name="routing_settings_import_rulesets_from_clipboard">Nhập bộ quy tắc từ clipboard</string>
<string name="routing_settings_export_rulesets_to_clipboard">Xuất bộ quy tắc vào clipboard</string>
<string name="routing_settings_locked">Đã khóa, giữ nguyên quy tắc này khi nhập cài đặt trước</string>

<string name="connection_test_pending">Kiểm tra kết nối</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@
<string name="routing_settings_add_rule">添加规则</string>
<string name="routing_settings_import_rulesets">导入规则集</string>
<string name="routing_settings_import_rulesets_tip">现有规则集将被删除,确定继续吗?</string>
<string name="routing_settings_import_rulesets_from_clipboard">从剪贴板导入规则集</string>
<string name="routing_settings_export_rulesets_to_clipboard">将规则集导出到剪贴板</string>
<string name="routing_settings_locked">已锁定,导入预设时保留此规则</string>

<string name="connection_test_pending">检查连接性</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@
<string name="routing_settings_add_rule">新增規則</string>
<string name="routing_settings_import_rulesets">匯入規則集</string>
<string name="routing_settings_import_rulesets_tip">現有規則集將被刪除,確定繼續嗎?</string>
<string name="routing_settings_import_rulesets_from_clipboard">從剪貼簿匯入規則集</string>
<string name="routing_settings_export_rulesets_to_clipboard">將規則集匯出到剪貼簿</string>
<string name="routing_settings_locked">鎖定,匯入預設時保留此規則</string>

<string name="connection_test_pending">檢查連接性</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@
<string name="routing_settings_add_rule">Add rule</string>
<string name="routing_settings_import_rulesets">Import ruleset</string>
<string name="routing_settings_import_rulesets_tip">Existing rulesets will be deleted, are you sure to continue?</string>
<string name="routing_settings_import_rulesets_from_clipboard">Import ruleset from clipboard</string>
<string name="routing_settings_export_rulesets_to_clipboard">Export ruleset to clipboard</string>
<string name="routing_settings_locked">Locked, keep this rule when import presets</string>

<string name="connection_test_pending">Check Connectivity</string>
Expand Down

0 comments on commit 338f13c

Please sign in to comment.