~singpolyma/cheogram-android

028671e5fca651e66916bc3ca880fb359f21f513 — Stephen Paul Weber 7 months ago 9f10165
Don't produce useless XHTML-IM of multiline text
M src/cheogram/java/com/cheogram/android/SpannedToXHTML.java => src/cheogram/java/com/cheogram/android/SpannedToXHTML.java +11 -1
@@ 30,13 30,23 @@ import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xml.TextNode;

public class SpannedToXHTML {
	public static Element append(Element out, Spanned text) {
	private static SpannableStringBuilder cleanSpans(Spanned text) {
		SpannableStringBuilder newText = new SpannableStringBuilder(text);
		SuggestionSpan[] spans = newText.getSpans(0, newText.length(), SuggestionSpan.class);
		for (SuggestionSpan span : spans) {
			newText.removeSpan(span);
		}
		BaseInputConnection.removeComposingSpans(newText);
		return newText;
	}

	public static boolean isPlainText(Spanned text) {
		SpannableStringBuilder cleanText = cleanSpans(text);
		return cleanText.nextSpanTransition(0, cleanText.length(), CharacterStyle.class) >= cleanText.length();
	}

	public static Element append(Element out, Spanned text) {
		SpannableStringBuilder newText = cleanSpans(text);
		withinParagraph(out, newText, 0, newText.length());
		return out;
	}

M src/main/java/eu/siacs/conversations/entities/Message.java => src/main/java/eu/siacs/conversations/entities/Message.java +8 -8
@@ 554,11 554,12 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable 

    public synchronized void setBody(Spanned span) {
        setBody(span.toString());
        final Element body = getOrMakeHtml();
        body.clearChildren();
        SpannedToXHTML.append(body, span);
        if (body.getContent().equals(span.toString())) {
        if (SpannedToXHTML.isPlainText(span)) {
            this.payloads.remove(getHtml(true));
        } else {
            final Element body = getOrMakeHtml();
            body.clearChildren();
            SpannedToXHTML.append(body, span);
        }
    }



@@ 576,10 577,9 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable 
    }

    public synchronized void appendBody(Spanned append) {
        final Element body = getOrMakeHtml();
        SpannedToXHTML.append(body, append);
        if (body.getContent().equals(this.body + append.toString())) {
            this.payloads.remove(getHtml());
        if (!SpannedToXHTML.isPlainText(append) || getHtml() != null) {
            final Element body = getOrMakeHtml();
            SpannedToXHTML.append(body, append);
        }
        appendBody(append.toString());
    }