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

Commit

Permalink
Add preset ruleset routing
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust authored and AnGgIt86 committed Sep 26, 2024
1 parent c1ee5e8 commit ca16c22
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 19 deletions.
3 changes: 0 additions & 3 deletions app/src/main/assets/custom_routing_block

This file was deleted.

1 change: 0 additions & 1 deletion app/src/main/assets/custom_routing_direct

This file was deleted.

1 change: 0 additions & 1 deletion app/src/main/assets/custom_routing_proxy

This file was deleted.

23 changes: 17 additions & 6 deletions app/src/main/kotlin/com/neko/v2ray/ui/RoutingSettingActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class RoutingSettingActivity : BaseActivity() {
private val routing_domain_strategy: Array<out String> by lazy {
resources.getStringArray(R.array.routing_domain_strategy)
}
private val preset_rulesets: Array<out String> by lazy {
resources.getStringArray(R.array.preset_rulesets)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -80,13 +83,21 @@ class RoutingSettingActivity : BaseActivity() {
R.id.import_rulesets -> {
AlertDialog.Builder(this).setMessage(R.string.routing_settings_import_rulesets_tip)
.setPositiveButton(android.R.string.ok) { _, _ ->
lifecycleScope.launch(Dispatchers.IO) {
SettingsManager.resetRoutingRulesets(this@RoutingSettingActivity)
launch(Dispatchers.Main) {
refreshData()
toast(R.string.toast_success)
AlertDialog.Builder(this).setItems(preset_rulesets.asList().toTypedArray()) { _, i ->
try {
lifecycleScope.launch(Dispatchers.IO) {
SettingsManager.resetRoutingRulesets(this@RoutingSettingActivity, i)
launch(Dispatchers.Main) {
refreshData()
toast(R.string.toast_success)
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}.show()


}
.setNegativeButton(android.R.string.no) { _, _ ->
//do noting
Expand Down
18 changes: 10 additions & 8 deletions app/src/main/kotlin/com/neko/v2ray/util/SettingsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,34 @@ package com.neko.v2ray.util

import android.content.Context
import android.text.TextUtils
import android.util.Log
import com.google.gson.Gson
import com.neko.v2ray.dto.RulesetItem

object SettingsManager {

fun initRoutingRulesets(context: Context) {
Log.d("=====", "initRoutingRuleset")
fun initRoutingRulesets(context: Context, index: Int = 0) {
val exist = MmkvManager.decodeRoutingRulesets()

val fileName = when (index) {
0 -> "custom_routing_white"
1 -> "custom_routing_black"
2 -> "custom_routing_global"
else -> "custom_routing_white"
}
if (exist.isNullOrEmpty()) {
Log.d("=====", "isNullOrEmpty")
val assets = Utils.readTextFromAssets(context, "custom_routing_white")
val assets = Utils.readTextFromAssets(context, fileName)
if (TextUtils.isEmpty(assets)) {
return
}

val rulesetList = Gson().fromJson(assets, Array<RulesetItem>::class.java).toMutableList()
Log.d("=====", "rulesetList==" + rulesetList.count())
MmkvManager.encodeRoutingRulesets(rulesetList)
}
}

fun resetRoutingRulesets(context: Context) {
fun resetRoutingRulesets(context: Context, index: Int) {
MmkvManager.encodeRoutingRulesets(null)
initRoutingRulesets(context)
initRoutingRulesets(context, index)
}

fun getRoutingRuleset(index: Int): RulesetItem? {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values-zh-rCN/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@
<item>VPN</item>
<item>仅代理</item>
</string-array>

<string-array name="preset_rulesets">
<item>绕过大陆(Whitelist)</item>
<item>黑名单(Blacklist)</item>
<item>全局(Global)</item>
</string-array>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values-zh-rTW/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@
<item>VPN</item>
<item>僅 Proxy</item>
</string-array>

<string-array name="preset_rulesets">
<item>绕过大陆(Whitelist)</item>
<item>黑名单(Blacklist)</item>
<item>全局(Global)</item>
</string-array>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@
<item>VPN</item>
<item>Proxy only</item>
</string-array>

<string-array name="preset_rulesets">
<item>China Whitelist</item>
<item>China Blacklist</item>
<item>Global</item>
</string-array>
</resources>

0 comments on commit ca16c22

Please sign in to comment.