Skip to content

Commit 5080d94

Browse files
committed
Add description field to ProfileItem and generate descriptions
1 parent 24539be commit 5080d94

File tree

6 files changed

+45
-14
lines changed

6 files changed

+45
-14
lines changed

V2rayNG/app/src/main/java/com/v2ray/ang/dto/ProfileItem.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ data class ProfileItem(
1515
var addedTime: Long = System.currentTimeMillis(),
1616

1717
var remarks: String = "",
18+
var description: String? = null,
1819
var server: String? = null,
1920
var serverPort: String? = null,
2021

V2rayNG/app/src/main/java/com/v2ray/ang/handler/AngConfigManager.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,14 @@ object AngConfigManager {
271271
) {
272272
try {
273273
val serverList: Array<Any> =
274-
JsonUtil.fromJson(server, Array<Any>::class.java)?: arrayOf()
274+
JsonUtil.fromJson(server, Array<Any>::class.java) ?: arrayOf()
275275

276276
if (serverList.isNotEmpty()) {
277277
var count = 0
278278
for (srv in serverList.reversed()) {
279279
val config = CustomFmt.parse(JsonUtil.toJson(srv)) ?: continue
280280
config.subscriptionId = subid
281+
config.description = generateDescription(config)
281282
val key = MmkvManager.encodeServerConfig("", config)
282283
MmkvManager.encodeServerRaw(key, JsonUtil.toJsonPretty(srv) ?: "")
283284
count += 1
@@ -292,6 +293,7 @@ object AngConfigManager {
292293
// For compatibility
293294
val config = CustomFmt.parse(server) ?: return 0
294295
config.subscriptionId = subid
296+
config.description = generateDescription(config)
295297
val key = MmkvManager.encodeServerConfig("", config)
296298
MmkvManager.encodeServerRaw(key, server)
297299
return 1
@@ -302,6 +304,7 @@ object AngConfigManager {
302304
} else if (server.startsWith("[Interface]") && server.contains("[Peer]")) {
303305
try {
304306
val config = WireguardFmt.parseWireguardConfFile(server) ?: return R.string.toast_incorrect_protocol
307+
config.description = generateDescription(config)
305308
val key = MmkvManager.encodeServerConfig("", config)
306309
MmkvManager.encodeServerRaw(key, server)
307310
return 1
@@ -363,6 +366,7 @@ object AngConfigManager {
363366
}
364367

365368
config.subscriptionId = subid
369+
config.description = generateDescription(config)
366370
val guid = MmkvManager.encodeServerConfig("", config)
367371
if (removedSelectedServer != null &&
368372
config.server == removedSelectedServer.server && config.serverPort == removedSelectedServer.serverPort
@@ -493,4 +497,25 @@ object AngConfigManager {
493497
MmkvManager.encodeSubscription("", subItem)
494498
return 1
495499
}
500+
501+
/** Generates a description for the profile.
502+
*
503+
* @param profile The profile item.
504+
* @return The generated description.
505+
*/
506+
fun generateDescription(profile: ProfileItem): String {
507+
// Hide xxx:xxx:***/xxx.xxx.xxx.***
508+
val server = profile.server
509+
val port = profile.serverPort
510+
if (server.isNullOrBlank() && port.isNullOrBlank()) return ""
511+
512+
val addrPart = server?.let {
513+
if (it.contains(":"))
514+
it.split(":").take(2).joinToString(":", postfix = ":***")
515+
else
516+
it.split('.').dropLast(1).joinToString(".", postfix = ".***")
517+
} ?: ""
518+
519+
return "$addrPart : ${port ?: ""}"
520+
}
496521
}

V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainRecyclerAdapter.kt

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import com.v2ray.ang.databinding.ItemRecyclerFooterBinding
1414
import com.v2ray.ang.databinding.ItemRecyclerMainBinding
1515
import com.v2ray.ang.dto.ProfileItem
1616
import com.v2ray.ang.dto.ServersCache
17+
import com.v2ray.ang.extension.nullIfBlank
18+
import com.v2ray.ang.handler.AngConfigManager
1719
import com.v2ray.ang.handler.MmkvManager
1820
import com.v2ray.ang.helper.ItemTouchHelperAdapter
1921
import com.v2ray.ang.helper.ItemTouchHelperViewHolder
@@ -121,19 +123,7 @@ class MainRecyclerAdapter(
121123
* @return Formatted address string
122124
*/
123125
private fun getAddress(profile: ProfileItem): String {
124-
// Hide xxx:xxx:***/xxx.xxx.xxx.***
125-
val server = profile.server
126-
val port = profile.serverPort
127-
if (server.isNullOrBlank() && port.isNullOrBlank()) return ""
128-
129-
val addrPart = server?.let {
130-
if (it.contains(":"))
131-
it.split(":").take(2).joinToString(":", postfix = ":***")
132-
else
133-
it.split('.').dropLast(1).joinToString(".", postfix = ".***")
134-
} ?: ""
135-
136-
return "$addrPart : ${port ?: ""}"
126+
return profile.description.nullIfBlank() ?: AngConfigManager.generateDescription(profile)
137127
}
138128

139129
/**

V2rayNG/app/src/main/java/com/v2ray/ang/ui/ServerActivity.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.v2ray.ang.dto.ProfileItem
2626
import com.v2ray.ang.extension.isNotNullEmpty
2727
import com.v2ray.ang.extension.toast
2828
import com.v2ray.ang.extension.toastSuccess
29+
import com.v2ray.ang.handler.AngConfigManager
2930
import com.v2ray.ang.handler.MmkvManager
3031
import com.v2ray.ang.util.JsonUtil
3132
import com.v2ray.ang.util.Utils
@@ -494,6 +495,8 @@ class ServerActivity : BaseActivity() {
494495
saveStreamSettings(config)
495496
saveTls(config)
496497

498+
config.description = AngConfigManager.generateDescription(config)
499+
497500
if (config.subscriptionId.isEmpty() && !subscriptionId.isNullOrEmpty()) {
498501
config.subscriptionId = subscriptionId.orEmpty()
499502
}

V2rayNG/app/src/main/java/com/v2ray/ang/ui/ServerCustomConfigActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.v2ray.ang.dto.ProfileItem
1616
import com.v2ray.ang.extension.toast
1717
import com.v2ray.ang.extension.toastSuccess
1818
import com.v2ray.ang.fmt.CustomFmt
19+
import com.v2ray.ang.handler.AngConfigManager
1920
import com.v2ray.ang.handler.MmkvManager
2021
import com.v2ray.ang.util.Utils
2122

@@ -89,6 +90,7 @@ class ServerCustomConfigActivity : BaseActivity() {
8990
}
9091
config.server = profileItem?.server
9192
config.serverPort = profileItem?.serverPort
93+
config.description = AngConfigManager.generateDescription(config)
9294

9395
MmkvManager.encodeServerConfig(editGuid, config)
9496
MmkvManager.encodeServerRaw(editGuid, binding.editor.text.toString())

V2rayNG/app/src/main/java/com/v2ray/ang/ui/ServerGroupActivity.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import com.v2ray.ang.R
1010
import com.v2ray.ang.databinding.ActivityServerGroupBinding
1111
import com.v2ray.ang.enums.EConfigType
1212
import com.v2ray.ang.dto.ProfileItem
13+
import com.v2ray.ang.extension.isNotNullEmpty
1314
import com.v2ray.ang.extension.toast
1415
import com.v2ray.ang.extension.toastSuccess
16+
import com.v2ray.ang.handler.AngConfigManager
1517
import com.v2ray.ang.handler.MmkvManager
1618
import com.v2ray.ang.util.Utils
1719

@@ -66,6 +68,11 @@ class ServerGroupActivity : BaseActivity() {
6668
private fun clearServer(): Boolean {
6769
binding.etRemarks.text = null
6870
binding.etPolicyGroupFilter.text = null
71+
72+
if (subscriptionId.isNotNullEmpty()) {
73+
val pos = subIds.indexOf(subscriptionId).let { if (it >= 0) it else 0 }
74+
binding.spPolicyGroupSubId.setSelection(pos)
75+
}
6976
return true
7077
}
7178

@@ -90,6 +97,9 @@ class ServerGroupActivity : BaseActivity() {
9097
if (config.subscriptionId.isEmpty() && !subscriptionId.isNullOrEmpty()) {
9198
config.subscriptionId = subscriptionId.orEmpty()
9299
}
100+
101+
config.description = "${binding.spPolicyGroupType.selectedItem} - ${binding.spPolicyGroupSubId.selectedItem} - ${config.policyGroupFilter}"
102+
93103
MmkvManager.encodeServerConfig(editGuid, config)
94104
toastSuccess(R.string.toast_success)
95105
finish()

0 commit comments

Comments
 (0)