diff --git a/src/org/thoughtcrime/securesms/loki/activities/PathActivity.kt b/src/org/thoughtcrime/securesms/loki/activities/PathActivity.kt index 71c49d99e9..6ef05ef967 100644 --- a/src/org/thoughtcrime/securesms/loki/activities/PathActivity.kt +++ b/src/org/thoughtcrime/securesms/loki/activities/PathActivity.kt @@ -53,6 +53,14 @@ class PathActivity : PassphraseRequiredActionBarActivity() { } broadcastReceivers.add(pathsBuiltReceiver) LocalBroadcastManager.getInstance(this).registerReceiver(pathsBuiltReceiver, IntentFilter("pathsBuilt")) + val onionRequestPathCountriesLoadedReceiver: BroadcastReceiver = object : BroadcastReceiver() { + + override fun onReceive(context: Context, intent: Intent) { + handleOnionRequestPathCountriesLoaded() + } + } + broadcastReceivers.add(onionRequestPathCountriesLoadedReceiver) + LocalBroadcastManager.getInstance(this).registerReceiver(onionRequestPathCountriesLoadedReceiver, IntentFilter("onionRequestPathCountriesLoaded")) } override fun onDestroy() { @@ -66,6 +74,7 @@ class PathActivity : PassphraseRequiredActionBarActivity() { // region Updating private fun handleBuildingPathsEvent() { update(false) } private fun handlePathsBuiltEvent() { update(false) } + private fun handleOnionRequestPathCountriesLoaded() { update(false) } private fun update(isAnimated: Boolean) { pathRowsContainer.removeAllViews() @@ -131,7 +140,7 @@ class PathActivity : PassphraseRequiredActionBarActivity() { private fun getPathRow(snode: Snode, location: LineView.Location, dotAnimationStartDelay: Long, dotAnimationRepeatInterval: Long, isGuardSnode: Boolean): LinearLayout { val title = if (isGuardSnode) resources.getString(R.string.activity_path_guard_node_row_title) else resources.getString(R.string.activity_path_service_node_row_title) - val subtitle = IP2Country.shared.getCountry(snode.ip) + val subtitle = IP2Country.shared.countryNamesCache[snode.ip] ?: "Loading..." return getPathRow(title, subtitle, location, dotAnimationStartDelay, dotAnimationRepeatInterval) } // endregion diff --git a/src/org/thoughtcrime/securesms/loki/utilities/IP2Country.kt b/src/org/thoughtcrime/securesms/loki/utilities/IP2Country.kt index c45977d7a2..35bffffae9 100644 --- a/src/org/thoughtcrime/securesms/loki/utilities/IP2Country.kt +++ b/src/org/thoughtcrime/securesms/loki/utilities/IP2Country.kt @@ -14,7 +14,7 @@ import java.io.FileReader class IP2Country private constructor(private val context: Context) { private val pathsBuiltEventReceiver: BroadcastReceiver - private val countryNamesCache = mutableMapOf() + val countryNamesCache = mutableMapOf() private val ipv4Table by lazy { loadFile("geolite2_country_blocks_ipv4.csv") @@ -36,11 +36,11 @@ class IP2Country private constructor(private val context: Context) { } init { - preloadCountriesIfNeeded() + populateCacheIfNeeded() pathsBuiltEventReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { - preloadCountriesIfNeeded() + populateCacheIfNeeded() } } LocalBroadcastManager.getInstance(context).registerReceiver(pathsBuiltEventReceiver, IntentFilter("pathsBuilt")) @@ -67,7 +67,7 @@ class IP2Country private constructor(private val context: Context) { return file } - fun getCountry(ip: String): String { + private fun cacheCountryForIP(ip: String): String { var truncatedIP = ip fun getCountryInternal(): String { val country = countryNamesCache[ip] @@ -103,12 +103,13 @@ class IP2Country private constructor(private val context: Context) { return getCountryInternal() } - private fun preloadCountriesIfNeeded() { + private fun populateCacheIfNeeded() { Thread { val path = OnionRequestAPI.paths.firstOrNull() ?: return@Thread path.forEach { snode -> - getCountry(snode.ip) // Preload if needed + cacheCountryForIP(snode.ip) // Preload if needed } + Broadcaster(context).broadcast("onionRequestPathCountriesLoaded") Log.d("Loki", "Finished preloading onion request path countries.") }.start() }