Hopefully resolved ANRs caused by available fonts taking too long to load

This commit is contained in:
Yair Morgenstern
2022-07-08 16:54:21 +03:00
parent 550ac30bff
commit 6c533d63fb
2 changed files with 10 additions and 4 deletions

View File

@ -22,11 +22,13 @@ class NativeFontAndroid(
private val size: Int, private val size: Int,
private val fontFamily: String private val fontFamily: String
) : NativeFontImplementation { ) : NativeFontImplementation {
private val fontList = private val fontList by lazy{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) emptySet() if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) emptySet()
else SystemFonts.getAvailableFonts() 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) { 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 { fun Font.matchesFamily(family: String): Int {

View File

@ -144,6 +144,10 @@ class UncivGame(parameters: UncivGameParameters) : Game() {
settings.save() 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 // This stuff needs to run on the main thread because it needs the GL context
launchOnGLThread { launchOnGLThread {
musicController.chooseTrack(suffixes = listOf(MusicMood.Menu, MusicMood.Ambient), musicController.chooseTrack(suffixes = listOf(MusicMood.Menu, MusicMood.Ambient),