Commit 36bf02de authored by Mygod's avatar Mygod

Regard udpFallback as server settings

parent 08b2b87c
...@@ -55,13 +55,13 @@ object ProfileManager { ...@@ -55,13 +55,13 @@ object ProfileManager {
fun createProfilesFromSubscription(jsons: Sequence<InputStream>) { fun createProfilesFromSubscription(jsons: Sequence<InputStream>) {
val currentId = DataStore.profileId val currentId = DataStore.profileId
val profiles = getAllProfiles() val profiles = getAllProfiles()
val subscriptions = mutableMapOf<String, Profile>() val subscriptions = mutableMapOf<Pair<String?, String>, Profile>()
val toUpdate = mutableSetOf<Long>() val toUpdate = mutableSetOf<Long>()
var feature: Profile? = null var feature: Profile? = null
profiles?.forEach { profile -> // preprocessing phase profiles?.forEach { profile -> // preprocessing phase
if (currentId == profile.id) feature = profile if (currentId == profile.id) feature = profile
if (profile.subscription == Profile.SubscriptionStatus.UserConfigured) return@forEach if (profile.subscription == Profile.SubscriptionStatus.UserConfigured) return@forEach
if (subscriptions.putIfAbsentCompat(profile.formattedAddress, profile) != null) { if (subscriptions.putIfAbsentCompat(profile.name to profile.formattedAddress, profile) != null) {
delProfile(profile.id) delProfile(profile.id)
if (currentId == profile.id) DataStore.profileId = 0 if (currentId == profile.id) DataStore.profileId = 0
} else if (profile.subscription == Profile.SubscriptionStatus.Active) { } else if (profile.subscription == Profile.SubscriptionStatus.Active) {
...@@ -72,18 +72,19 @@ object ProfileManager { ...@@ -72,18 +72,19 @@ object ProfileManager {
jsons.asIterable().forEachTry { json -> jsons.asIterable().forEachTry { json ->
Profile.parseJson(JsonStreamParser(json.bufferedReader()).asSequence().single(), feature) { Profile.parseJson(JsonStreamParser(json.bufferedReader()).asSequence().single(), feature) {
val oldProfile = subscriptions[it.formattedAddress] subscriptions.computeCompat(it.name to it.formattedAddress) { _, oldProfile ->
when (oldProfile?.subscription) { when (oldProfile?.subscription) {
Profile.SubscriptionStatus.Active -> { } // skip dup subscription Profile.SubscriptionStatus.Active -> oldProfile // skip dup subscription
Profile.SubscriptionStatus.Obsolete -> { Profile.SubscriptionStatus.Obsolete -> {
oldProfile.password = it.password oldProfile.password = it.password
oldProfile.method = it.method oldProfile.method = it.method
oldProfile.plugin = it.plugin
oldProfile.udpFallback = it.udpFallback
oldProfile.subscription = Profile.SubscriptionStatus.Active oldProfile.subscription = Profile.SubscriptionStatus.Active
oldProfile
}
else -> createProfile(it.apply { subscription = Profile.SubscriptionStatus.Active })
} }
else -> createProfile(it.apply {
subscription = Profile.SubscriptionStatus.Active
subscriptions[it.formattedAddress] = it
})
} }
} }
} }
......
...@@ -89,6 +89,12 @@ fun String?.parseNumericAddress(): InetAddress? = Os.inet_pton(OsConstants.AF_IN ...@@ -89,6 +89,12 @@ fun String?.parseNumericAddress(): InetAddress? = Os.inet_pton(OsConstants.AF_IN
if (Build.VERSION.SDK_INT >= 29) it else parseNumericAddress.invoke(null, this) as InetAddress if (Build.VERSION.SDK_INT >= 29) it else parseNumericAddress.invoke(null, this) as InetAddress
} }
fun <K, V> MutableMap<K, V>.computeCompat(key: K, remappingFunction: (K, V?) -> V?) = if (Build.VERSION.SDK_INT < 24) {
val oldValue = get(key)
remappingFunction(key, oldValue).also { newValue ->
if (newValue != null) put(key, newValue) else if (oldValue != null || containsKey(key)) remove(key)
}
} else compute(key) { k, oldValue -> remappingFunction(k, oldValue) }
fun <K, V> MutableMap<K, V>.computeIfAbsentCompat(key: K, value: () -> V) = if (Build.VERSION.SDK_INT < 24) { fun <K, V> MutableMap<K, V>.computeIfAbsentCompat(key: K, value: () -> V) = if (Build.VERSION.SDK_INT < 24) {
this[key] ?: value().also { put(key, it) } this[key] ?: value().also { put(key, it) }
} else computeIfAbsent(key) { value() } } else computeIfAbsent(key) { value() }
......
...@@ -126,9 +126,9 @@ class ProfileConfigFragment : PreferenceFragmentCompat(), ...@@ -126,9 +126,9 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
findPreference<Preference>(Key.password)!!.isEnabled = false findPreference<Preference>(Key.password)!!.isEnabled = false
findPreference<Preference>(Key.method)!!.isEnabled = false findPreference<Preference>(Key.method)!!.isEnabled = false
findPreference<Preference>(Key.remotePort)!!.isEnabled = false findPreference<Preference>(Key.remotePort)!!.isEnabled = false
findPreference<Preference>(Key.plugin)!!.isEnabled = false plugin.isEnabled = false
findPreference<Preference>(Key.pluginConfigure)!!.isEnabled = false pluginConfigure.isEnabled = false
udpFallback.isEnabled = false
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment