Commit 091ae734 authored by Max Lv's avatar Max Lv

Fix some bugs

parent 09211cd4
...@@ -82,7 +82,8 @@ data class Profile( ...@@ -82,7 +82,8 @@ data class Profile(
private val userInfoPattern = "^(.+?):(.*)$".toRegex() private val userInfoPattern = "^(.+?):(.*)$".toRegex()
private val legacyPattern = "^(.+?):(.*)@(.+?):(\\d+?)$".toRegex() private val legacyPattern = "^(.+?):(.*)@(.+?):(\\d+?)$".toRegex()
fun findAllUrls(data: CharSequence?, feature: Profile? = null) = pattern.findAll(data ?: "").map { fun findAllUrls(data: CharSequence?, feature: Profile? = null) = pattern.findAll(data
?: "").map {
val uri = it.value.toUri() val uri = it.value.toUri()
try { try {
if (uri.userInfo == null) { if (uri.userInfo == null) {
...@@ -139,13 +140,15 @@ data class Profile( ...@@ -139,13 +140,15 @@ data class Profile(
private val fallbackMap = mutableMapOf<Profile, Profile>() private val fallbackMap = mutableMapOf<Profile, Profile>()
private val JsonElement?.optString get() = (this as? JsonPrimitive)?.asString private val JsonElement?.optString get() = (this as? JsonPrimitive)?.asString
private val JsonElement?.optBoolean get() = // asBoolean attempts to cast everything to boolean private val JsonElement?.optBoolean
(this as? JsonPrimitive)?.run { if (isBoolean) asBoolean else null } get() = // asBoolean attempts to cast everything to boolean
private val JsonElement?.optInt get() = try { (this as? JsonPrimitive)?.run { if (isBoolean) asBoolean else null }
(this as? JsonPrimitive)?.asInt private val JsonElement?.optInt
} catch (_: NumberFormatException) { get() = try {
null (this as? JsonPrimitive)?.asInt
} } catch (_: NumberFormatException) {
null
}
private fun tryParse(json: JsonObject, fallback: Boolean = false): Profile? { private fun tryParse(json: JsonObject, fallback: Boolean = false): Profile? {
val host = json["server"].optString val host = json["server"].optString
...@@ -176,8 +179,11 @@ data class Profile( ...@@ -176,8 +179,11 @@ data class Profile(
(json["proxy_apps"] as? JsonObject)?.also { (json["proxy_apps"] as? JsonObject)?.also {
proxyApps = it["enabled"].optBoolean ?: proxyApps proxyApps = it["enabled"].optBoolean ?: proxyApps
bypass = it["bypass"].optBoolean ?: bypass bypass = it["bypass"].optBoolean ?: bypass
individual = (json["android_list"] as? JsonArray)?.asIterable()?.joinToString("\n") ?: individual = (it["android_list"] as? JsonArray)?.asIterable()?.joinToString("\n")
individual ?: individual
// JSONArray will return strings with "", which should be removed
individual = individual.replace("\"", "")
} }
udpdns = json["udpdns"].optBoolean ?: udpdns udpdns = json["udpdns"].optBoolean ?: udpdns
(json["udp_fallback"] as? JsonObject)?.let { tryParse(it, true) }?.also { fallbackMap[this] = it } (json["udp_fallback"] as? JsonObject)?.let { tryParse(it, true) }?.also { fallbackMap[this] = it }
...@@ -194,6 +200,7 @@ data class Profile( ...@@ -194,6 +200,7 @@ data class Profile(
// ignore other types // ignore other types
} }
} }
fun finalize(create: (Profile) -> Unit) { fun finalize(create: (Profile) -> Unit) {
val profiles = ProfileManager.getAllProfiles() ?: emptyList() val profiles = ProfileManager.getAllProfiles() ?: emptyList()
for ((profile, fallback) in fallbackMap) { for ((profile, fallback) in fallbackMap) {
...@@ -210,6 +217,7 @@ data class Profile( ...@@ -210,6 +217,7 @@ data class Profile(
} }
} }
} }
fun parseJson(json: JsonElement, feature: Profile? = null, create: (Profile) -> Unit) { fun parseJson(json: JsonElement, feature: Profile? = null, create: (Profile) -> Unit) {
JsonParser(feature).run { JsonParser(feature).run {
process(json) process(json)
...@@ -273,6 +281,7 @@ data class Profile( ...@@ -273,6 +281,7 @@ data class Profile(
if (!name.isNullOrEmpty()) builder.fragment(name) if (!name.isNullOrEmpty()) builder.fragment(name)
return builder.build() return builder.build()
} }
override fun toString() = toUri().toString() override fun toString() = toUri().toString()
fun toJson(profiles: LongSparseArray<Profile>? = null): JSONObject = JSONObject().apply { fun toJson(profiles: LongSparseArray<Profile>? = null): JSONObject = JSONObject().apply {
......
...@@ -591,8 +591,12 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { ...@@ -591,8 +591,12 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
profilesAdapter.deepRefreshId(profileId) profilesAdapter.deepRefreshId(profileId)
} }
override fun onDestroyView() { override fun onPause() {
undoManager.flush() undoManager.flush()
super.onPause()
}
override fun onDestroyView() {
nativeAd?.destroy() nativeAd?.destroy()
super.onDestroyView() super.onDestroyView()
} }
......
...@@ -244,19 +244,25 @@ class SubscriptionFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener ...@@ -244,19 +244,25 @@ class SubscriptionFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener
fetchJob = GlobalScope.launch { fetchJob = GlobalScope.launch {
val subscription = Subscription.instance val subscription = Subscription.instance
try { for (url in subscription.urls.asIterable()) {
for (url in subscription.urls.asIterable()) { try {
val connection = url.openConnection() as HttpURLConnection val connection = url.openConnection() as HttpURLConnection
ProfileManager.createProfilesFromJson(sequenceOf(connection.inputStream), replace = true)
} try {
} catch (e: Exception) { ProfileManager.createProfilesFromJson(sequenceOf(connection.inputStream), replace = true)
e.printStackTrace() } catch (e: RuntimeException) {
activity.snackbar(e.readableMessage).show() e.printStackTrace()
} finally { activity.snackbar(e.readableMessage).show()
progress.post { }
progress.visibility = View.INVISIBLE } catch (e: IOException) {
e.printStackTrace()
activity.snackbar(e.readableMessage).show()
} }
} }
progress.post {
progress.visibility = View.INVISIBLE
}
} }
} }
} }
......
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