From 6c533d63fb4c7a667342d5d59c202aaa8908d99c Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Fri, 8 Jul 2022 16:54:21 +0300 Subject: [PATCH] Hopefully resolved ANRs caused by available fonts taking too long to load --- android/src/com/unciv/app/NativeFontAndroid.kt | 10 ++++++---- core/src/com/unciv/UncivGame.kt | 4 ++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/android/src/com/unciv/app/NativeFontAndroid.kt b/android/src/com/unciv/app/NativeFontAndroid.kt index 2dbddcb271..28b890306d 100755 --- a/android/src/com/unciv/app/NativeFontAndroid.kt +++ b/android/src/com/unciv/app/NativeFontAndroid.kt @@ -22,13 +22,15 @@ class NativeFontAndroid( private val size: Int, private val fontFamily: String ) : NativeFontImplementation { - private val fontList = + private val fontList by lazy{ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) emptySet() else SystemFonts.getAvailableFonts() + } - private val paint = Paint().apply { + private val paint by lazy{ createPaint() } + fun createPaint() = Paint().apply { typeface = if (fontFamily.isNotBlank() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - // Helper within the VERSION_CODES.Q gate: Evaluate a Font's desirability (lower = better) for a given family. + // Helper within the VERSION_CODES.Q gate: Evaluate a Font's desirability (lower = better) for a given family. fun Font.matchesFamily(family: String): Int { val name = file?.nameWithoutExtension ?: return Int.MAX_VALUE if (name == family) return 0 @@ -94,7 +96,7 @@ class NativeFontAndroid( // To get _all_ Languages a user has in their Android settings, we would need more help // from the launcher: (Activity).resources.configuration.locales - val languageTag = Locale.getDefault().toLanguageTag() // e.g. he-IL, corresponds to the _first_ Language in Android settings + val languageTag = Locale.getDefault().toLanguageTag() // e.g. he-IL, corresponds to the _first_ Language in Android settings val supportedLocales = arrayOf(languageTag, "en-US") val supportedLanguages = supportedLocales.map { it.take(2) } return fontList.asSequence() diff --git a/core/src/com/unciv/UncivGame.kt b/core/src/com/unciv/UncivGame.kt index df7239a713..6cc7310449 100644 --- a/core/src/com/unciv/UncivGame.kt +++ b/core/src/com/unciv/UncivGame.kt @@ -144,6 +144,10 @@ class UncivGame(parameters: UncivGameParameters) : Game() { settings.save() } + // Loading available fonts can take a long time on Android phones. + // Therefore we initialize the lazy parameters in the font implementation, while we're in another thread, to avoid ANRs on main thread + fontImplementation?.getCharPixmap('S') + // This stuff needs to run on the main thread because it needs the GL context launchOnGLThread { musicController.chooseTrack(suffixes = listOf(MusicMood.Menu, MusicMood.Ambient),