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

Commit

Permalink
Add routing rule lock, keep this rule when import presets
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust authored and AnGgIt86 committed Sep 29, 2024
1 parent 9744ca6 commit 92eb43f
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 19 deletions.
1 change: 1 addition & 0 deletions app/src/main/kotlin/com/neko/v2ray/dto/RulesetItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ data class RulesetItem(
var network: String? = null,
var protocol: List<String>? = null,
var enabled: Boolean = true,
var looked: Boolean? = false,
)
13 changes: 8 additions & 5 deletions app/src/main/kotlin/com/neko/v2ray/ui/MainRecyclerAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai
}

// 隐藏主页服务器地址为xxx:xxx:***/xxx.xxx.xxx.***
val strState = "${profile.server?.let {
if (it.contains(":"))
it.split(":").take(2).joinToString(":") + ":***"
else
it.split('.').dropLast(1).joinToString(".", postfix = ".***") }} : ${profile.serverPort}"
val strState = "${
profile.server?.let {
if (it.contains(":"))
it.split(":").take(2).joinToString(":", postfix = ":***")
else
it.split('.').dropLast(1).joinToString(".", postfix = ".***")
}
} : ${profile.serverPort}"

holder.itemMainBinding.tvStatistics.text = strState

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/kotlin/com/neko/v2ray/ui/RoutingEditActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class RoutingEditActivity : BaseActivity() {

private fun bindingServer(rulesetItem: RulesetItem): Boolean {
binding.etRemarks.text = Utils.getEditable(rulesetItem.remarks)
binding.chkLocked.isChecked = rulesetItem.looked ?: false
binding.etDomain.text = Utils.getEditable(rulesetItem.domain?.joinToString(","))
binding.etIp.text = Utils.getEditable(rulesetItem.ip?.joinToString(","))
binding.etPort.text = Utils.getEditable(rulesetItem.port)
Expand All @@ -112,6 +113,7 @@ class RoutingEditActivity : BaseActivity() {
val rulesetItem = SettingsManager.getRoutingRuleset(position) ?: RulesetItem()

rulesetItem.remarks = binding.etRemarks.text.toString()
rulesetItem.looked = binding.chkLocked.isChecked
binding.etDomain.text.toString().let { rulesetItem.domain = if (it.isEmpty()) null else it.split(",").map { itt -> itt.trim() }.filter { itt -> itt.isNotEmpty() } }
binding.etIp.text.toString().let { rulesetItem.ip = if (it.isEmpty()) null else it.split(",").map { itt -> itt.trim() }.filter { itt -> itt.isNotEmpty() } }
binding.etProtocol.text.toString().let { rulesetItem.protocol = if (it.isEmpty()) null else it.split(",").map { itt -> itt.trim() }.filter { itt -> itt.isNotEmpty() } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.neko.v2ray.databinding.ItemRecyclerRoutingSettingBinding
import com.neko.v2ray.helper.ItemTouchHelperAdapter
Expand All @@ -25,6 +26,7 @@ class RoutingSettingRecyclerAdapter(val activity: RoutingSettingActivity) : Recy
holder.itemRoutingSettingBinding.domainIp.text = (ruleset.domain ?: ruleset.ip ?: ruleset.port)?.toString()
holder.itemRoutingSettingBinding.outboundTag.text = ruleset.outboundTag
holder.itemRoutingSettingBinding.chkEnable.isChecked = ruleset.enabled
holder.itemRoutingSettingBinding.imgLocked.isVisible = ruleset.looked ?: false
holder.itemView.setBackgroundColor(Color.TRANSPARENT)

holder.itemRoutingSettingBinding.layoutEdit.setOnClickListener {
Expand Down
33 changes: 22 additions & 11 deletions app/src/main/kotlin/com/neko/v2ray/util/SettingsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,40 @@ import java.util.Collections

object SettingsManager {

fun initRoutingRulesets(context: Context, index: Int = 0) {
fun initRoutingRulesets(context: Context) {
val exist = MmkvManager.decodeRoutingRulesets()
if (exist.isNullOrEmpty()) {
val rulesetList = getPresetRoutingRulesets(context)
MmkvManager.encodeRoutingRulesets(rulesetList)
}
}

private fun getPresetRoutingRulesets(context: Context, index: Int = 0): MutableList<RulesetItem>? {
val fileName = when (index) {
0 -> "custom_routing_white"
1 -> "custom_routing_black"
2 -> "custom_routing_global"
else -> "custom_routing_white"
}
if (exist.isNullOrEmpty()) {
val assets = Utils.readTextFromAssets(context, fileName)
if (TextUtils.isEmpty(assets)) {
return
}

val rulesetList = Gson().fromJson(assets, Array<RulesetItem>::class.java).toMutableList()
MmkvManager.encodeRoutingRulesets(rulesetList)
val assets = Utils.readTextFromAssets(context, fileName)
if (TextUtils.isEmpty(assets)) {
return null
}

return Gson().fromJson(assets, Array<RulesetItem>::class.java).toMutableList()
}

fun resetRoutingRulesets(context: Context, index: Int) {
MmkvManager.encodeRoutingRulesets(null)
initRoutingRulesets(context, index)
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)
}

fun getRoutingRuleset(index: Int): RulesetItem? {
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/ic_lock_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="1024"
android:viewportHeight="1024">

<path
android:fillColor="#FF000000"
android:pathData="M742.4,409.6L716.8,409.6L716.8,332.8C716.8,205.8 613.4,102.4 486.4,102.4S256,205.8 256,332.8L256,409.6h-25.6C188.1,409.6 153.6,444.1 153.6,486.4v409.6c0,42.3 34.5,76.8 76.8,76.8h512c42.3,0 76.8,-34.5 76.8,-76.8v-409.6c0,-42.3 -34.5,-76.8 -76.8,-76.8zM307.2,332.8C307.2,234 387.6,153.6 486.4,153.6S665.6,234 665.6,332.8L665.6,409.6L307.2,409.6L307.2,332.8zM768,896a25.6,25.6 0,0 1,-25.6 25.6h-512a25.6,25.6 0,0 1,-25.6 -25.6v-409.6a25.6,25.6 0,0 1,25.6 -25.6h512a25.6,25.6 0,0 1,25.6 25.6v409.6z" />

</vector>
18 changes: 18 additions & 0 deletions app/src/main/res/layout/activity_routing_edit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,24 @@
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8.0dip"
android:layout_marginBottom="8.0dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/routing_settings_locked" />
<com.neko.switchbutton.SwitchButton
android:id="@+id/chk_locked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:kswAnimationDuration="300"
app:kswBackDrawable="@drawable/uwu_switch_track"
app:kswThumbDrawable="@drawable/uwu_switch_thumb" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/res/layout/item_recycler_routing_setting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,37 @@
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:paddingStart="@dimen/padding_start">
<<<<<<< HEAD:app/src/main/res/layout/item_recycler_routing_setting.xml
<TextView
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
android:id="@+id/remarks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10.0dip" />
=======

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/remarks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead" />

<ImageView
android:id="@+id/img_locked"
android:layout_gravity="center"
android:layout_marginStart="@dimen/padding_start"
android:layout_width="@dimen/padding"
android:layout_height="@dimen/padding"
app:srcCompat="@drawable/ic_lock_24dp" />

</LinearLayout>

>>>>>>> 9b86ba9e (Add routing rule lock, keep this rule when import presets):V2rayNG/app/src/main/res/layout/item_recycler_routing_setting.xml
<TextView
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:id="@+id/domainIp"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,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_locked">مقفل، حافظ على هذه القاعدة عند استيراد الإعدادات المسبقة</string>

<string name="connection_test_pending">تحقق من الاتصال</string>
<string name="connection_test_testing">اختبارات…</string>
<string name="connection_test_available">نجاح: استغرق اتصال HTTP %dms</string>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values-fa/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,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_locked">قفل است، این قانون را هنگام وارد کردن از پیش تنظیمات حفظ کنید</string>

<string name="connection_test_pending">اتصال را بررسی کنید</string>
<string name="connection_test_testing">آزمایش کردن…</string>
<string name="connection_test_available">موفقیت: اتصال HTTP %dms طول کشید</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-in/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@
<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_locked">Terkunci, pertahankan aturan ini saat mengimpor preset</string>

<string name="connection_test_pending">Periksa Konektivitas</string>
<string name="connection_test_testing">Pengujian…</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@
<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_locked">ロックされています。プリセットをインポートするときにこのルールを維持します</string>

<string name="connection_test_pending">接続をチェックする</string>
<string name="connection_test_testing">テスト中…</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-jw/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@
<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_locked">Dikunci, tetep aturan iki nalika ngimpor preset</string>

<string name="connection_test_pending">Priksa Konektivitas</string>
<string name="connection_test_testing">Tes…</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@
<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_locked">Заблокировано, сохранить это правило при импорте предустановок</string>

<string name="connection_test_pending">Проверьте подключение</string>
<string name="connection_test_testing">Тестирование…</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-su/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@
<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_locked">Dikonci, jaga aturan ieu nalika impor preset</string>

<string name="connection_test_pending">Pariksa Konéktipitas</string>
<string name="connection_test_testing">Nguji…</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-vi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@
<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_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>
<string name="connection_test_testing">Đang thử nghiệm…</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@
<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_locked">已锁定,导入预设时保留此规则</string>

<string name="connection_test_pending">检查连接性</string>
<string name="connection_test_testing">测试...</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@
<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_locked">鎖定,匯入預設時保留此規則</string>

<string name="connection_test_pending">檢查連接性</string>
<string name="connection_test_testing">測試...</string>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,14 @@

<string name="routing_settings_domain_strategy">Domain strategy</string>
<string name="routing_settings_title">Routing Settings</string>
<string name="routing_settings_tips">Separated by commas(,)</string>
<string name="routing_settings_tips">Separated by commas(,),choose domain or ip</string>
<string name="routing_settings_save">Save</string>
<string name="routing_settings_delete">Clear</string>
<string name="routing_settings_rule_title">Routing Rule Settings</string>
<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_locked">Locked, keep this rule when import presets</string>

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

0 comments on commit 92eb43f

Please sign in to comment.