Fix font networking main thread crash.

This commit is contained in:
Cody Henthorne 2022-04-04 20:22:42 -04:00
parent bb66c3fa68
commit be241524db
4 changed files with 48 additions and 26 deletions

View file

@ -1367,7 +1367,7 @@ public class ThreadDatabase extends Database {
try {
type = SignalDatabase.mmsSms().getConversationSnippetType(threadId);
} catch (NoSuchMessageException e) {
Log.w(TAG, "Unable to find snippet message for thread: " + threadId, e);
Log.w(TAG, "Unable to find snippet message for thread: " + threadId);
return;
}

View file

@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.fonts
import android.content.Context
import android.graphics.Typeface
import android.os.Build
import org.signal.imageeditor.core.Renderer
import org.signal.imageeditor.core.RendererContext
import org.thoughtcrime.securesms.util.FutureTaskListener
@ -18,28 +19,41 @@ class FontTypefaceProvider : RendererContext.TypefaceProvider {
private var cachedLocale: Locale? = null
override fun getSelectedTypeface(context: Context, renderer: Renderer, invalidate: RendererContext.Invalidate): Typeface {
val typeface = cachedTypeface
if (typeface != null && cachedLocale == LocaleUtil.getFirstLocale()) {
return typeface
}
return getTypeface()
//TODO [cody] Need to rework Fonts.kt to not hit network on main, reverting to old typeface for now
// val typeface = cachedTypeface
// if (typeface != null && cachedLocale == LocaleUtil.getFirstLocale()) {
// return typeface
// }
//
// return when (val fontResult = Fonts.resolveFont(context, TextFont.BOLD)) {
// is Fonts.FontResult.Immediate -> {
// cachedTypeface = fontResult.typeface
// cachedLocale = LocaleUtil.getFirstLocale()
// fontResult.typeface
// }
// is Fonts.FontResult.Async -> {
// fontResult.future.addListener(object : FutureTaskListener<Typeface> {
// override fun onSuccess(result: Typeface?) {
// invalidate.onInvalidate(renderer)
// }
//
// override fun onFailure(exception: ExecutionException?) = Unit
// })
//
// fontResult.placeholder
// }
// }
}
return when (val fontResult = Fonts.resolveFont(context, TextFont.BOLD)) {
is Fonts.FontResult.Immediate -> {
cachedTypeface = fontResult.typeface
cachedLocale = LocaleUtil.getFirstLocale()
fontResult.typeface
}
is Fonts.FontResult.Async -> {
fontResult.future.addListener(object : FutureTaskListener<Typeface> {
override fun onSuccess(result: Typeface?) {
invalidate.onInvalidate(renderer)
}
override fun onFailure(exception: ExecutionException?) = Unit
})
fontResult.placeholder
}
private fun getTypeface(): Typeface {
return if (Build.VERSION.SDK_INT < 26) {
Typeface.create(Typeface.DEFAULT, Typeface.BOLD)
} else {
Typeface.Builder("")
.setFallback("sans-serif")
.setWeight(900)
.build()
}
}
}

View file

@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.fonts
import android.content.Context
import android.graphics.Typeface
import androidx.annotation.WorkerThread
import org.signal.core.util.ThreadUtil
import org.signal.core.util.concurrent.SignalExecutors
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.s3.S3
@ -57,6 +58,7 @@ object Fonts {
*/
@WorkerThread
fun resolveFont(context: Context, font: TextFont, guessedScript: SupportedScript = SupportedScript.UNKNOWN): FontResult {
ThreadUtil.assertNotMainThread()
synchronized(this) {
val errorFallback = FontResult.Immediate(Typeface.create(font.fallbackFamily, font.fallbackStyle))
val version = FontVersion.get(context)

View file

@ -12,6 +12,7 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.doOnNextLayout
import androidx.core.view.isVisible
import com.google.android.material.imageview.ShapeableImageView
import org.signal.core.util.concurrent.SimpleTask
import org.thoughtcrime.securesms.R
import org.thoughtcrime.securesms.conversation.colors.ChatColors
import org.thoughtcrime.securesms.database.model.databaseprotos.StoryTextPost
@ -153,10 +154,15 @@ class StoryTextPostView @JvmOverloads constructor(
setTextBackgroundColor(storyTextPost.textBackgroundColor)
setTextGravity(TextAlignment.CENTER)
when (val fontResult = Fonts.resolveFont(context, font, TextToScript.guessScript(storyTextPost.body))) {
is Fonts.FontResult.Immediate -> setTypeface(fontResult.typeface)
is Fonts.FontResult.Async -> setTypeface(fontResult.future.get())
}
SimpleTask.run(
{
when (val fontResult = Fonts.resolveFont(context, font, TextToScript.guessScript(storyTextPost.body))) {
is Fonts.FontResult.Immediate -> fontResult.typeface
is Fonts.FontResult.Async -> fontResult.future.get()
}
},
{ typeface -> setTypeface(typeface) }
)
hideCloseButton()