From 12f4e86998d6f5710918d5e9a62fb1d091790bce Mon Sep 17 00:00:00 2001 From: layou233 Date: Sat, 11 Nov 2023 20:58:46 +0800 Subject: [PATCH] Fix updating view on non-UI thread --- app/build.gradle.kts | 4 ++-- .../mcping/server/MinecraftResolver.kt | 20 +++++++++++++------ .../mcping/ui/discovery/DiscoveryFragment.kt | 11 ++++++---- .../launium/mcping/ui/home/HomeFragment.kt | 8 ++++++-- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index d79dd6a..a505843 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -16,8 +16,8 @@ android { applicationId = "com.launium.mcping" minSdk = 21 targetSdk = 34 - versionCode = 4 - versionName = "0.2.2" + versionCode = 5 + versionName = "0.2.3" setProperty("archivesBaseName", "MCPing-$versionName") testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/src/main/java/com/launium/mcping/server/MinecraftResolver.kt b/app/src/main/java/com/launium/mcping/server/MinecraftResolver.kt index aef12dc..1e53e9a 100644 --- a/app/src/main/java/com/launium/mcping/server/MinecraftResolver.kt +++ b/app/src/main/java/com/launium/mcping/server/MinecraftResolver.kt @@ -14,6 +14,7 @@ import io.ktor.utils.io.core.Input import io.ktor.utils.io.core.readShort import io.ktor.utils.io.core.readTextExactBytes import io.ktor.utils.io.core.readUShort +import io.ktor.utils.io.errors.IOException import io.ktor.utils.io.streams.asInput import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope @@ -101,6 +102,7 @@ class MinecraftResolver(uri: String) { return results } + @OptIn(ExperimentalStdlibApi::class) @RequiresApi(Build.VERSION_CODES.Q) private suspend fun querySRVPlatform(serviceName: String) = withContext(Dispatchers.IO) { return@withContext suspendCoroutine { continuation -> @@ -115,13 +117,19 @@ class MinecraftResolver(uri: String) { override fun onAnswer(answer: ByteArray, rCode: Int) { when (rCode) { - 0 -> continuation.resume( - parseSRVResponse( - ByteArrayInputStream(answer).asInput(), - serviceName, - verifyTransactionID = false + 0 -> try { + continuation.resume( + parseSRVResponse( + ByteArrayInputStream(answer).asInput(), + serviceName, + verifyTransactionID = false + ) ) - ) // 0 stands for NO_ERROR + } catch (e: Exception) { + continuation.resumeWithException(IOException("Fail to parse SRV response: [00000000 ${ + answer.joinToString { it.toHexString() + " " } + }]", e)) + } // 0 stands for NO_ERROR 3 -> continuation.resume(null) // NX_DOMAIN diff --git a/app/src/main/java/com/launium/mcping/ui/discovery/DiscoveryFragment.kt b/app/src/main/java/com/launium/mcping/ui/discovery/DiscoveryFragment.kt index b5d3bb6..8784ffe 100644 --- a/app/src/main/java/com/launium/mcping/ui/discovery/DiscoveryFragment.kt +++ b/app/src/main/java/com/launium/mcping/ui/discovery/DiscoveryFragment.kt @@ -25,8 +25,8 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.joinAll import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Semaphore -import kotlinx.coroutines.withContext import java.net.URL +import java.util.Locale private const val SERVER_LIST_URL = "https://www.jsip.club/api/ajax.php?request=get_line_list" @@ -104,10 +104,11 @@ class DiscoveryFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener { adapter.servers = (resultObject["data"] as JSONArray).map { val serverObject = it as JSONObject return@map MinecraftServer( - serverObject["name"] as String, serverObject["address"] as String + serverObject["name"] as String, + (serverObject["address"] as String).lowercase(Locale.getDefault()) ) } - withContext(Dispatchers.Main) { + activity?.runOnUiThread { adapter.notifyDataSetChanged() } val semaphore = Semaphore(Preferences.maxConcurrentPings) @@ -125,7 +126,9 @@ class DiscoveryFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener { } semaphore.release() if (changed) { - adapter.notifyItemChanged(i) + activity?.runOnUiThread { + adapter.notifyItemChanged(i) + } } } } diff --git a/app/src/main/java/com/launium/mcping/ui/home/HomeFragment.kt b/app/src/main/java/com/launium/mcping/ui/home/HomeFragment.kt index e1e0bbc..09a3fac 100644 --- a/app/src/main/java/com/launium/mcping/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/launium/mcping/ui/home/HomeFragment.kt @@ -55,7 +55,9 @@ class HomeFragment : Fragment() { } lifecycleScope.launch(Dispatchers.IO) { adapter.updateServerList() - binding.container.adapter = adapter + activity?.runOnUiThread { + binding.container.adapter = adapter + } } binding.swipeRefreshLayout.setOnRefreshListener { @@ -75,7 +77,9 @@ class HomeFragment : Fragment() { } semaphore.release() if (changed) { - adapter.notifyItemChanged(i) + activity?.runOnUiThread { + adapter.notifyItemChanged(i) + } ServerManager.serverDao.update(server) } }