Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit 2058e6a

Browse files
committed
Support new span emoji html rendering.
Fixes: #495
1 parent 50aa60f commit 2058e6a

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

app/src/main/java/com/zulip/android/models/Message.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,7 @@ public static Spanned formatContent(String source, final ZulipApp app) {
340340
Html.ImageGetter emojiGetter = new Html.ImageGetter() {
341341
@Override
342342
public Drawable getDrawable(String source) {
343-
int imagesIndex = -1;
344-
if (source != null) {
345-
imagesIndex = source.indexOf("images/");
346-
}
347-
if (imagesIndex != -1) {
348-
String filename = source.substring(imagesIndex + "images/".length());
349-
343+
String filename = "emoji/" + source.replace(":", "") + ".png";
350344
try {
351345
Drawable drawable = Drawable.createFromStream(context
352346
.getAssets().open(filename), filename);
@@ -367,7 +361,6 @@ public Drawable getDrawable(String source) {
367361
} catch (IOException e) {
368362
Log.e("RecyclerMessageAdapter", e.getMessage());
369363
}
370-
}
371364
return null;
372365
}
373366
};

app/src/main/java/com/zulip/android/util/CustomHtmlToSpannedConverter.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public class CustomHtmlToSpannedConverter implements ContentHandler {
8787
private String mBaseUri;
8888
private boolean isCode;
8989
private boolean isStreamLink;
90+
private static boolean isEmoji;
9091

9192
public CustomHtmlToSpannedConverter(String source,
9293
Html.ImageGetter imageGetter, Html.TagHandler tagHandler,
@@ -174,7 +175,8 @@ private static void end(SpannableStringBuilder text, Class kind, Object repl) {
174175

175176
private static void startImg(SpannableStringBuilder text,
176177
Attributes attributes, Html.ImageGetter img) {
177-
String src = attributes.getValue("", "src");
178+
String cssClass = attributes.getValue("class");
179+
String src = cssClass != null && cssClass.startsWith("emoji") ? attributes.getValue("title") : attributes.getValue("", "src");
178180
Drawable d = null;
179181

180182
if (img != null) {
@@ -268,6 +270,10 @@ private static void endSpan(SpannableStringBuilder text) {
268270
int where = text.getSpanStart(obj);
269271
text.removeSpan(obj);
270272
if (where != len) {
273+
if (isEmoji) {
274+
text.delete(where, len);
275+
return;
276+
}
271277
Href h = (Href) obj;
272278
if (h != null && h.mHref != null) {
273279
if (ZulipApp.get().getEmail().equals(h.mHref)) {
@@ -556,7 +562,13 @@ private void handleStartTag(String tag, Attributes attributes) {
556562
} else if (tag.equalsIgnoreCase("span")
557563
&& "user-mention".equals(attributes.getValue("class"))) {
558564
startSpan(mSpannableStringBuilder, attributes);
559-
} else if (tag.equalsIgnoreCase("u")) {
565+
} else if (tag.equalsIgnoreCase("span")
566+
&& attributes.getValue("class") != null && attributes.getValue("class").startsWith("emoji")) {
567+
isEmoji = true;
568+
startImg(mSpannableStringBuilder, attributes, mEmojiGetter);
569+
startSpan(mSpannableStringBuilder, attributes);
570+
}
571+
else if (tag.equalsIgnoreCase("u")) {
560572
start(mSpannableStringBuilder, new Underline());
561573
} else if (tag.equalsIgnoreCase("sup")) {
562574
start(mSpannableStringBuilder, new Super());
@@ -580,6 +592,7 @@ private void handleStartTag(String tag, Attributes attributes) {
580592
handleP(mSpannableStringBuilder);
581593
start(mSpannableStringBuilder, new Header(tag.charAt(1) - '1'));
582594
} else if (tag.equalsIgnoreCase("img")) {
595+
// makes emojis backward compatible
583596
String cssClass = attributes.getValue("", "class");
584597
if (cssClass != null && cssClass.equals("emoji")) {
585598
startImg(mSpannableStringBuilder, attributes, mEmojiGetter);
@@ -639,6 +652,7 @@ private void handleEndTag(String tag) {
639652
}
640653
} else if (tag.equalsIgnoreCase("span")) {
641654
endSpan(mSpannableStringBuilder);
655+
isEmoji = false;
642656
} else if (tag.equalsIgnoreCase("u")) {
643657
end(mSpannableStringBuilder, Underline.class, new UnderlineSpan());
644658
} else if (tag.equalsIgnoreCase("sup")) {

0 commit comments

Comments
 (0)