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

Commit

Permalink
Support new span emoji html rendering.
Browse files Browse the repository at this point in the history
Fixes: #495
  • Loading branch information
Sam1301 committed May 5, 2017
1 parent 50aa60f commit 2058e6a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
9 changes: 1 addition & 8 deletions app/src/main/java/com/zulip/android/models/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,7 @@ public static Spanned formatContent(String source, final ZulipApp app) {
Html.ImageGetter emojiGetter = new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
int imagesIndex = -1;
if (source != null) {
imagesIndex = source.indexOf("images/");
}
if (imagesIndex != -1) {
String filename = source.substring(imagesIndex + "images/".length());

String filename = "emoji/" + source.replace(":", "") + ".png";
try {
Drawable drawable = Drawable.createFromStream(context
.getAssets().open(filename), filename);
Expand All @@ -367,7 +361,6 @@ public Drawable getDrawable(String source) {
} catch (IOException e) {
Log.e("RecyclerMessageAdapter", e.getMessage());
}
}
return null;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public class CustomHtmlToSpannedConverter implements ContentHandler {
private String mBaseUri;
private boolean isCode;
private boolean isStreamLink;
private static boolean isEmoji;

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

private static void startImg(SpannableStringBuilder text,
Attributes attributes, Html.ImageGetter img) {
String src = attributes.getValue("", "src");
String cssClass = attributes.getValue("class");
String src = cssClass != null && cssClass.startsWith("emoji") ? attributes.getValue("title") : attributes.getValue("", "src");
Drawable d = null;

if (img != null) {
Expand Down Expand Up @@ -268,6 +270,10 @@ private static void endSpan(SpannableStringBuilder text) {
int where = text.getSpanStart(obj);
text.removeSpan(obj);
if (where != len) {
if (isEmoji) {
text.delete(where, len);
return;
}
Href h = (Href) obj;
if (h != null && h.mHref != null) {
if (ZulipApp.get().getEmail().equals(h.mHref)) {
Expand Down Expand Up @@ -556,7 +562,13 @@ private void handleStartTag(String tag, Attributes attributes) {
} else if (tag.equalsIgnoreCase("span")
&& "user-mention".equals(attributes.getValue("class"))) {
startSpan(mSpannableStringBuilder, attributes);
} else if (tag.equalsIgnoreCase("u")) {
} else if (tag.equalsIgnoreCase("span")
&& attributes.getValue("class") != null && attributes.getValue("class").startsWith("emoji")) {
isEmoji = true;
startImg(mSpannableStringBuilder, attributes, mEmojiGetter);
startSpan(mSpannableStringBuilder, attributes);
}
else if (tag.equalsIgnoreCase("u")) {
start(mSpannableStringBuilder, new Underline());
} else if (tag.equalsIgnoreCase("sup")) {
start(mSpannableStringBuilder, new Super());
Expand All @@ -580,6 +592,7 @@ private void handleStartTag(String tag, Attributes attributes) {
handleP(mSpannableStringBuilder);
start(mSpannableStringBuilder, new Header(tag.charAt(1) - '1'));
} else if (tag.equalsIgnoreCase("img")) {
// makes emojis backward compatible
String cssClass = attributes.getValue("", "class");
if (cssClass != null && cssClass.equals("emoji")) {
startImg(mSpannableStringBuilder, attributes, mEmojiGetter);
Expand Down Expand Up @@ -639,6 +652,7 @@ private void handleEndTag(String tag) {
}
} else if (tag.equalsIgnoreCase("span")) {
endSpan(mSpannableStringBuilder);
isEmoji = false;
} else if (tag.equalsIgnoreCase("u")) {
end(mSpannableStringBuilder, Underline.class, new UnderlineSpan());
} else if (tag.equalsIgnoreCase("sup")) {
Expand Down

0 comments on commit 2058e6a

Please sign in to comment.