Commit f7fff2f0 authored by Thomas's avatar Thomas
Browse files

- Fix ANR on long text without spaces and NPE in quote handling

parent 1afa9822
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -144,9 +144,9 @@ public class Status implements Serializable, Cloneable {
        Gson gson = new Gson();
        try{
            quote = gson.fromJson(json, Status.class);
            if(quote.account == null) {
            if(quote != null && quote.account == null) {
                MastodonQuote mastodonQuote = gson.fromJson(json, MastodonQuote.class);
                if(mastodonQuote.quoted_status != null && (mastodonQuote.state != null && mastodonQuote.state.equals("accepted"))) {
                if(mastodonQuote != null && mastodonQuote.quoted_status != null && (mastodonQuote.state != null && mastodonQuote.state.equals("accepted"))) {
                    quote = mastodonQuote.quoted_status;
                }
            }
+11 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.os.Build;
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;

@@ -43,16 +44,26 @@ public class CustomTextView extends AppCompatTextView {
    private final boolean emoji;
    private float emojiSize;

    @SuppressLint("WrongConstant")
    public CustomTextView(Context context) {
        super(context);
        final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
        emoji = sharedpreferences.getBoolean(context.getString(R.string.SET_DISPLAY_EMOJI), false);
        // Use simple break strategy to avoid ANR on very long text without spaces
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            setBreakStrategy(0); // LineBreaker.BREAK_STRATEGY_SIMPLE
        }
    }

    @SuppressLint("WrongConstant")
    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
        emoji = sharedpreferences.getBoolean(context.getString(R.string.SET_DISPLAY_EMOJI), false);
        // Use simple break strategy to avoid ANR on very long text without spaces
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            setBreakStrategy(0); // LineBreaker.BREAK_STRATEGY_SIMPLE
        }

        final Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
        final float defaultEmojiSize = fontMetrics.descent - fontMetrics.ascent;
+3 −4
Original line number Diff line number Diff line
@@ -112,7 +112,7 @@ public class SpannableHelper {
    private static final String patternBottomTags = "\\n{2,}((#[\\w_À-ú-]+)(\\s*| *))+$";

    public static String[] hasBottomTags(String text) {
        if (text == null) {
        if (text == null || text.length() > 10000) {
            return new String[]{};
        }
        SpannableString initialContent;
@@ -183,7 +183,7 @@ public class SpannableHelper {
            mentions.addAll(status.mentions);
        }
        boolean markdownSupport = sharedpreferences.getBoolean(context.getString(R.string.SET_MARKDOWN_SUPPORT), false);
        if(!markdownSupport) {
        if(!markdownSupport && text.length() < 10000) {
            text = text.replaceAll("((<\\s?p\\s?>|<\\s?br\\s?/?>)&gt;(((?!(<\\s?br\\s?/?>|<\\s?/s?p\\s?>)).)*))", "$2<blockquote>$3</blockquote>");
        }
        if(convertHtml) {
@@ -200,7 +200,6 @@ public class SpannableHelper {
            initialContent = new SpannableString(text);
        }


        //Get all links
        SpannableStringBuilder content;
        if (markdownSupport && convertMarkdown) {
@@ -406,7 +405,7 @@ public class SpannableHelper {
        }

        boolean underlineBottomHashTags = sharedpreferences.getBoolean(context.getString(R.string.SET_UNDERLINE_BOTTOM_HASHTAGS), true);
        if(underlineBottomHashTags && !keepOriginalBottomHashTags) {
        if(underlineBottomHashTags && !keepOriginalBottomHashTags && content.length() < 10000) {
            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
            final Pattern bottomTagsPattern = Pattern.compile(patternBottomTags, Pattern.CASE_INSENSITIVE);
            Matcher matcherBottomTags = bottomTagsPattern.matcher(content);
+4 −9
Original line number Diff line number Diff line
@@ -280,17 +280,12 @@ public class TimelineHelper {
            }

            // Append quote content if present
            if (status.reblog == null && status.getQuote() != null) {
            Status quote = status.reblog == null ? status.getQuote() : (status.reblog != null ? status.reblog.getQuote() : null);
            if (quote != null && quote.content != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    content += Html.fromHtml(status.getQuote().content, Html.FROM_HTML_MODE_LEGACY).toString();
                    content += Html.fromHtml(quote.content, Html.FROM_HTML_MODE_LEGACY).toString();
                } else {
                    content += Html.fromHtml(status.getQuote().content).toString();
                }
            } else if (status.reblog != null && status.reblog.getQuote() != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    content += Html.fromHtml(status.reblog.getQuote().content, Html.FROM_HTML_MODE_LEGACY).toString();
                } else {
                    content += Html.fromHtml(status.reblog.getQuote().content).toString();
                    content += Html.fromHtml(quote.content).toString();
                }
            }
            return content;