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

Fix some bugs

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