From f5fd2308778c4532d0c533243bf91df3699ed2d4 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 10 Feb 2020 14:16:11 +0100 Subject: [PATCH] Precompute text --- .../eu/faircode/email/AdapterMessage.java | 20 +++++++++++-- .../eu/faircode/email/FragmentCompose.java | 29 +++++++++++++++---- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index f1e72720c2..535cc52973 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -103,6 +103,8 @@ import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.Group; import androidx.core.content.FileProvider; import androidx.core.graphics.ColorUtils; +import androidx.core.text.PrecomputedTextCompat; +import androidx.core.widget.TextViewCompat; import androidx.fragment.app.Fragment; import androidx.lifecycle.Lifecycle; import androidx.lifecycle.LifecycleObserver; @@ -1810,7 +1812,16 @@ public class AdapterMessage extends RecyclerView.Adapter() { + new SimpleTask() { @Override protected void onPreExecute(Bundle args) { // Needed to get width for images @@ -4029,7 +4031,7 @@ public class FragmentCompose extends FragmentBase { } @Override - protected Spanned[] onExecute(final Context context, Bundle args) throws Throwable { + protected Object[] onExecute(final Context context, Bundle args) throws Throwable { final long id = args.getLong("id"); final boolean show_images = args.getBoolean("show_images", false); @@ -4094,12 +4096,24 @@ public class FragmentCompose extends FragmentBase { args.putBoolean("ref_has_images", spannedRef != null && spannedRef.getSpans(0, spannedRef.length(), ImageSpan.class).length > 0); - return new Spanned[]{spannedBody, spannedRef}; + PrecomputedTextCompat precomputed = null; + if (spannedRef != null) + try { + precomputed = PrecomputedTextCompat.getTextFuture( + spannedRef, + TextViewCompat.getTextMetricsParams(tvReference), + null) + .get(); + } catch (Throwable ex) { + Log.w(ex); + } + + return new Spanned[]{spannedBody, precomputed == null ? spannedRef : precomputed}; } @Override - protected void onExecuted(Bundle args, Spanned[] text) { - etBody.setText(text[0]); + protected void onExecuted(Bundle args, Object[] text) { + etBody.setText((Spanned) text[0]); etBody.setSelection(0); grpBody.setVisibility(View.VISIBLE); @@ -4111,7 +4125,10 @@ public class FragmentCompose extends FragmentBase { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); boolean ref_hint = prefs.getBoolean("compose_reference", true); - tvReference.setText(text[1]); + if (text[1] instanceof PrecomputedTextCompat) + TextViewCompat.setPrecomputedText(tvReference, (PrecomputedTextCompat) text[1]); + else + tvReference.setText((Spanned) text[1]); tvReference.setVisibility(text[1] == null ? View.GONE : View.VISIBLE); grpReferenceHint.setVisibility(text[1] == null || !ref_hint ? View.GONE : View.VISIBLE); ibReferenceEdit.setVisibility(text[1] == null ? View.GONE : View.VISIBLE);